about summary refs log tree commit diff
path: root/nixpkgs/pkgs/os-specific/linux/minimal-bootstrap/stage0-posix/mescc-tools-boot.nix
blob: 2114ffc707b18741670d9712e404b693f26acab2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# Mes --- Maxwell Equations of Software
# Copyright © 2017,2019 Jan Nieuwenhuizen <janneke@gnu.org>
# Copyright © 2017,2019 Jeremiah Orians
#
# This file is part of Mes.
#
# Mes is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or (at
# your option) any later version.
#
# Mes is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Mes.  If not, see <http://www.gnu.org/licenses/>.

# This is a translation of stage0-posix/stage0-posix/x86/mescc-tools-mini-kaem.kaem to nix
# https://github.com/oriansj/stage0-posix-x86/blob/56e6b8df3e95f4bc04f8b420a4cd8c82c70b9efa/mescc-tools-mini-kaem.kaem
#
# We have access to mini-kaem at this point but it doesn't support substituting
# environment variables. Without variables there's no way of passing in store inputs,
# or the $out path, other than as command line arguments directly

# Warning all binaries prior to the use of blood-elf will not be readable by
# Objdump, you may need to use ndism or gdb to view the assembly in the binary.

{ lib
, derivationWithMeta
, hostPlatform
, hex0
, m2libc
, src
, version
, platforms
, stage0Arch
, m2libcArch
, baseAddress
}:
rec {
  out = placeholder "out";

  endianFlag = if hostPlatform.isLittleEndian then "--little-endian" else "--big-endian";

  bloodFlags = lib.optional hostPlatform.is64bit "--64";

  run = pname: builder: args:
    derivationWithMeta {
      inherit pname version builder args;

      meta = with lib; {
        description = "Collection of tools written for use in bootstrapping";
        homepage = "https://github.com/oriansj/stage0-posix";
        license = licenses.gpl3Plus;
        maintainers = teams.minimal-bootstrap.members;
        inherit platforms;
      };
    };

  ################################
  # Phase-1 Build hex1 from hex0 #
  ################################

  hex1 = run "hex1" hex0 ["${src}/${stage0Arch}/hex1_${stage0Arch}.hex0" out];

  # hex1 adds support for single character labels and is available in various forms
  # in mescc-tools/x86_bootstrap to allow you various ways to verify correctness

  ################################
  # Phase-2 Build hex2 from hex1 #
  ################################

  hex2-0 = run "hex2" hex1 ["${src}/${stage0Arch}/hex2_${stage0Arch}.hex1" out];

  # hex2 adds support for long labels and absolute addresses thus allowing it
  # to function as an effective linker for later stages of the bootstrap
  # This is a minimal version which will be used to bootstrap a much more advanced
  # version in a later stage.

  #################################
  # Phase-2b Build catm from hex2 #
  #################################

  catm =
    if hostPlatform.isAarch64 then
      run "catm" hex1 ["${src}/${stage0Arch}/catm_${stage0Arch}.hex1" out]
    else
      run "catm" hex2-0 ["${src}/${stage0Arch}/catm_${stage0Arch}.hex2" out];

  # catm removes the need for cat or shell support for redirection by providing
  # equivalent functionality via catm output_file input1 input2 ... inputN

  ##############################
  # Phase-3 Build M0 from hex2 #
  ##############################

  M0_hex2 = run "M0.hex2" catm [out "${m2libc}/${m2libcArch}/ELF-${m2libcArch}.hex2" "${src}/${stage0Arch}/M0_${stage0Arch}.hex2"];
  M0 = run "M0" hex2-0 [M0_hex2 out];

  # M0 is the architecture specific version of M1 and is by design single
  # architecture only and will be replaced by the C code version of M1

  ################################
  # Phase-4 Build cc_arch from M0 #
  ################################

  cc_arch-0_hex2 = run "cc_arch-0.hex2" M0 ["${src}/${stage0Arch}/cc_${m2libcArch}.M1" out];
  cc_arch-1_hex2 = run "cc_arch-1.hex2" catm [out "${m2libc}/${m2libcArch}/ELF-${m2libcArch}.hex2" cc_arch-0_hex2];
  cc_arch = run "cc_arch" hex2-0 [cc_arch-1_hex2 out];

  ########################################
  # Phase-5 Build M2-Planet from cc_arch #
  ########################################

  M2-0_c = run "M2-0.c" catm [
    out
    "${m2libc}/${m2libcArch}/linux/bootstrap.c"
    "${src}/M2-Planet/cc.h"
    "${m2libc}/bootstrappable.c"
    "${src}/M2-Planet/cc_globals.c"
    "${src}/M2-Planet/cc_reader.c"
    "${src}/M2-Planet/cc_strings.c"
    "${src}/M2-Planet/cc_types.c"
    "${src}/M2-Planet/cc_core.c"
    "${src}/M2-Planet/cc_macro.c"
    "${src}/M2-Planet/cc.c"
  ];
  M2-0_M1 = run "M2-0.M1" cc_arch [M2-0_c out];
  M2-0-0_M1 = run "M2-0-0.M1" catm [out "${m2libc}/${m2libcArch}/${m2libcArch}_defs.M1" "${m2libc}/${m2libcArch}/libc-core.M1" M2-0_M1];
  M2-0_hex2 = run "M2-0.hex2" M0 [M2-0-0_M1 out];
  M2-0-0_hex2 = run "M2-0-0.hex2" catm [out "${m2libc}/${m2libcArch}/ELF-${m2libcArch}.hex2" M2-0_hex2];
  M2 = run "M2" hex2-0 [M2-0-0_hex2 out];

  ############################################
  # Phase-6 Build blood-elf-0 from C sources #
  ############################################

  blood-elf-0_M1 = run "blood-elf-0.M1" M2 [
    "--architecture" m2libcArch
    "-f" "${m2libc}/${m2libcArch}/linux/bootstrap.c"
    "-f" "${m2libc}/bootstrappable.c"
    "-f" "${src}/mescc-tools/stringify.c"
    "-f" "${src}/mescc-tools/blood-elf.c"
    "--bootstrap-mode"
    "-o" out
  ];

  blood-elf-0-0_M1 = run "blood-elf-0-0.M1" catm [out "${m2libc}/${m2libcArch}/${m2libcArch}_defs.M1" "${m2libc}/${m2libcArch}/libc-core.M1" blood-elf-0_M1];
  blood-elf-0_hex2 = run "blood-elf-0.hex2" M0 [blood-elf-0-0_M1 out];
  blood-elf-0-0_hex2 = run "blood-elf-0-0.hex2" catm [out "${m2libc}/${m2libcArch}/ELF-${m2libcArch}.hex2" blood-elf-0_hex2];
  blood-elf-0 = run "blood-elf-0" hex2-0 [blood-elf-0-0_hex2 out];

  # This is the last stage where the binaries will not have debug info
  # and the last piece built that isn't part of the output binaries

  #####################################
  # Phase-7 Build M1-0 from C sources #
  #####################################

  M1-macro-0_M1 = run "M1-macro-0.M1" M2 [
    "--architecture" m2libcArch
    "-f" "${m2libc}/${m2libcArch}/linux/bootstrap.c"
    "-f" "${m2libc}/bootstrappable.c"
    "-f" "${src}/mescc-tools/stringify.c"
    "-f" "${src}/mescc-tools/M1-macro.c"
    "--bootstrap-mode"
    "--debug"
    "-o" out
  ];

  M1-macro-0-footer_M1 = run "M1-macro-0-footer.M1" blood-elf-0 (bloodFlags ++ ["-f" M1-macro-0_M1 endianFlag "-o" out]);
  M1-macro-0-0_M1 = run "M1-macro-0-0.M1" catm [out "${m2libc}/${m2libcArch}/${m2libcArch}_defs.M1" "${m2libc}/${m2libcArch}/libc-core.M1" M1-macro-0_M1 M1-macro-0-footer_M1];
  M1-macro-0_hex2 = run "M1-macro-0.hex2" M0 [M1-macro-0-0_M1 out];
  M1-macro-0-0_hex2 = run "M1-macro-0-0.hex2" catm [out "${m2libc}/${m2libcArch}/ELF-${m2libcArch}-debug.hex2" M1-macro-0_hex2];
  M1-0 = run "M1-0" hex2-0 [M1-macro-0-0_hex2 out];

  # This is the last stage where catm will need to be used and the last stage where
  # M0 is used, as we will being using it's much more powerful and cross-platform
  # version with a bunch of extra goodies.

  #######################################
  # Phase-8 Build hex2-1 from C sources #
  #######################################

  hex2_linker-0_M1 = run "hex2_linker-0.M1" M2 [
    "--architecture" m2libcArch
    "-f" "${m2libc}/sys/types.h"
    "-f" "${m2libc}/stddef.h"
    "-f" "${m2libc}/${m2libcArch}/linux/unistd.c"
    "-f" "${m2libc}/${m2libcArch}/linux/fcntl.c"
    "-f" "${m2libc}/fcntl.c"
    "-f" "${m2libc}/${m2libcArch}/linux/sys/stat.c"
    "-f" "${m2libc}/stdlib.c"
    "-f" "${m2libc}/stdio.h"
    "-f" "${m2libc}/stdio.c"
    "-f" "${m2libc}/bootstrappable.c"
    "-f" "${src}/mescc-tools/hex2.h"
    "-f" "${src}/mescc-tools/hex2_linker.c"
    "-f" "${src}/mescc-tools/hex2_word.c"
    "-f" "${src}/mescc-tools/hex2.c"
    "--debug"
    "-o" out
  ];

  hex2_linker-0-footer_M1 = run "hex2_linker-0-footer.M1" blood-elf-0 (bloodFlags ++ ["-f" hex2_linker-0_M1 endianFlag "-o" out]);

  hex2_linker-0_hex2 = run "hex2_linker-0.hex2" M1-0 [
    "--architecture" m2libcArch
    endianFlag
    "-f" "${m2libc}/${m2libcArch}/${m2libcArch}_defs.M1"
    "-f" "${m2libc}/${m2libcArch}/libc-full.M1"
    "-f" hex2_linker-0_M1
    "-f" hex2_linker-0-footer_M1
    "-o" out
  ];

  hex2_linker-0-0_hex2 = run "hex2_linker-0-0.hex2" catm [out "${m2libc}/${m2libcArch}/ELF-${m2libcArch}-debug.hex2" hex2_linker-0_hex2];

  hex2-1 = run "hex2-1" hex2-0 [hex2_linker-0-0_hex2 out];

  # This is the last stage where we will be using the handwritten hex2 and instead
  # be using the far more powerful, cross-platform version with a bunch more goodies

  ###################################
  # Phase-9 Build M1 from C sources #
  ###################################

  M1-macro-1_M1 = run "M1-macro-1.M1" M2 [
    "--architecture" m2libcArch
    "-f" "${m2libc}/sys/types.h"
    "-f" "${m2libc}/stddef.h"
    "-f" "${m2libc}/${m2libcArch}/linux/fcntl.c"
    "-f" "${m2libc}/fcntl.c"
    "-f" "${m2libc}/${m2libcArch}/linux/unistd.c"
    "-f" "${m2libc}/string.c"
    "-f" "${m2libc}/stdlib.c"
    "-f" "${m2libc}/stdio.h"
    "-f" "${m2libc}/stdio.c"
    "-f" "${m2libc}/bootstrappable.c"
    "-f" "${src}/mescc-tools/stringify.c"
    "-f" "${src}/mescc-tools/M1-macro.c"
    "--debug"
    "-o" out
  ];

  M1-macro-1-footer_M1 = run "M1-macro-1-footer.M1" blood-elf-0 (bloodFlags ++ ["-f" M1-macro-1_M1 endianFlag "-o" out]);

  M1-macro-1_hex2 = run "M1-macro-1.hex2" M1-0 [
    "--architecture" m2libcArch
    endianFlag
    "-f" "${m2libc}/${m2libcArch}/${m2libcArch}_defs.M1"
    "-f" "${m2libc}/${m2libcArch}/libc-full.M1"
    "-f" M1-macro-1_M1
    "-f" M1-macro-1-footer_M1
    "-o" out
  ];

  M1 = run "M1" hex2-1 [
    "--architecture" m2libcArch
    endianFlag
    "--base-address" baseAddress
    "-f" "${m2libc}/${m2libcArch}/ELF-${m2libcArch}-debug.hex2"
    "-f" M1-macro-1_hex2
    "-o" out
  ];

  ######################################
  # Phase-10 Build hex2 from C sources #
  ######################################

  hex2_linker-2_M1 = run "hex2_linker-2.M1" M2 [
    "--architecture" m2libcArch
    "-f" "${m2libc}/sys/types.h"
    "-f" "${m2libc}/stddef.h"
    "-f" "${m2libc}/${m2libcArch}/linux/unistd.c"
    "-f" "${m2libc}/${m2libcArch}/linux/fcntl.c"
    "-f" "${m2libc}/fcntl.c"
    "-f" "${m2libc}/${m2libcArch}/linux/sys/stat.c"
    "-f" "${m2libc}/stdlib.c"
    "-f" "${m2libc}/stdio.h"
    "-f" "${m2libc}/stdio.c"
    "-f" "${m2libc}/bootstrappable.c"
    "-f" "${src}/mescc-tools/hex2.h"
    "-f" "${src}/mescc-tools/hex2_linker.c"
    "-f" "${src}/mescc-tools/hex2_word.c"
    "-f" "${src}/mescc-tools/hex2.c"
    "--debug"
    "-o" out
  ];

  hex2_linker-2-footer_M1 = run "hex2_linker-2-footer.M1" blood-elf-0 (bloodFlags ++ ["-f" hex2_linker-2_M1 endianFlag "-o" out]);

  hex2_linker-2_hex2 = run "hex2_linker-2.hex2" M1 [
    "--architecture" m2libcArch
    endianFlag
    "-f" "${m2libc}/${m2libcArch}/${m2libcArch}_defs.M1"
    "-f" "${m2libc}/${m2libcArch}/libc-full.M1"
    "-f" hex2_linker-2_M1
    "-f" hex2_linker-2-footer_M1
    "-o" out
  ];

  hex2 = run "hex2" hex2-1 [
    "--architecture" m2libcArch
    endianFlag
    "--base-address" baseAddress
    "-f" "${m2libc}/${m2libcArch}/ELF-${m2libcArch}-debug.hex2"
    "-f" hex2_linker-2_hex2
    "-o" out
  ];

  ######################################
  # Phase-11 Build kaem from C sources #
  ######################################

  kaem_M1 = run "kaem.M1" M2 [
    "--architecture" m2libcArch
    "-f" "${m2libc}/sys/types.h"
    "-f" "${m2libc}/stddef.h"
    "-f" "${m2libc}/string.c"
    "-f" "${m2libc}/${m2libcArch}/linux/unistd.c"
    "-f" "${m2libc}/${m2libcArch}/linux/fcntl.c"
    "-f" "${m2libc}/fcntl.c"
    "-f" "${m2libc}/stdlib.c"
    "-f" "${m2libc}/stdio.h"
    "-f" "${m2libc}/stdio.c"
    "-f" "${m2libc}/bootstrappable.c"
    "-f" "${src}/mescc-tools/Kaem/kaem.h"
    "-f" "${src}/mescc-tools/Kaem/variable.c"
    "-f" "${src}/mescc-tools/Kaem/kaem_globals.c"
    "-f" "${src}/mescc-tools/Kaem/kaem.c"
    "--debug"
    "-o" out
  ];

  kaem-footer_M1 = run "kaem-footer.M1" blood-elf-0 (bloodFlags ++ ["-f" kaem_M1 endianFlag "-o" out]);

  kaem_hex2 = run "kaem.hex2" M1 [
    "--architecture" m2libcArch
    endianFlag
    "-f" "${m2libc}/${m2libcArch}/${m2libcArch}_defs.M1"
    "-f" "${m2libc}/${m2libcArch}/libc-full.M1"
    "-f" kaem_M1
    "-f" kaem-footer_M1
    "-o" out
  ];

  kaem-unwrapped = run "kaem-unwrapped" hex2 [
    "--architecture" m2libcArch
    endianFlag
    "-f" "${m2libc}/${m2libcArch}/ELF-${m2libcArch}-debug.hex2"
    "-f" kaem_hex2
    "--base-address" baseAddress
    "-o" out
  ];
}