-
Notifications
You must be signed in to change notification settings - Fork 1
/
imager
executable file
·355 lines (317 loc) · 11.1 KB
/
imager
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
#!/usr/bin/env python3
import argparse
import collections
import os
import shutil
import subprocess
import sys
import uuid
Model = collections.namedtuple(
"Model",
["name", "dtb", "uboot_config", "arch", "start_fixup"],
defaults=[None],
)
models = {
m.name: m
for m in [
Model(
"3b",
"http://http.debian.net/debian/dists/buster/main/installer-arm64/"
"current/images/device-tree/broadcom/bcm2837-rpi-3-b.dtb",
"rpi_3_defconfig",
"arm64",
),
Model("4b", "bcm2711-rpi-4-b.dtb", "rpi_4_defconfig", "arm64", "4"),
]
}
cross = {"armhf": "arm-linux-gnueabihf-", "arm64": "aarch64-linux-gnu-"}
grub_pkg = {
"armhf": (
"http://http.debian.net/debian/pool/main/g/grub2/"
"grub-efi-arm-bin_2.04-4_armhf.deb"
),
"arm64": (
"http://http.debian.net/debian/pool/main/g/grub2/"
"grub-efi-arm64-bin_2.04-4_arm64.deb"
),
}
debian_installer = {
"arm64": {
"_root_url": (
"http://http.debian.net/debian/dists/buster/main/installer-arm64/"
"current/images/netboot/"
),
"linux": "debian-installer/arm64/linux",
"initrd.gz": "debian-installer/arm64/initrd.gz",
"grub.cfg": "debian-installer/arm64/grub/grub.cfg",
}
}
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
"--firmware-partition-size",
type=int,
help="size of firmware partition in MB",
default=16,
)
parser.add_argument(
"--efi-partition-size",
type=int,
help="size of EFI partition in MB",
default=16,
)
parser.add_argument("model")
parser.add_argument("output")
args = parser.parse_args()
try:
model = models[args.model]
except Exception:
sys.stderr.write(
f"Model must be one of: { ', '.join(sorted(models)) }\n"
)
raise SystemExit(1)
debian_installer_download = f"download/debian-installer-{ model.arch }"
for dirname in "build", "download", debian_installer_download:
try:
os.mkdir(dirname)
except FileExistsError:
...
tmpdir = f"build/{ uuid.uuid4() }"
os.mkdir(tmpdir)
##########################################################################
# devicetree
if "://" in model.dtb:
dtb = f"download/{ model.dtb.rsplit('/', 1)[1] }"
if not os.path.exists(dtb):
subprocess.check_call(["wget", model.dtb], cwd="download")
else:
dtb = f"firmware/boot/{ model.dtb }"
##########################################################################
# Build u-boot
# subprocess.check_call(["git", "clean", "-fdx"], cwd="u-boot")
subprocess.check_call(["git", "checkout", "--", "."], cwd="u-boot")
with open(f"u-boot/configs/{ model.uboot_config }", "a") as f:
f.write(
"\n"
"CONFIG_USE_BOOTCOMMAND=y\n"
"CONFIG_BOOTCOMMAND="
'"fatload mmc 0:1 ${scriptaddr} /boot.scr; source ${scriptaddr}"\n'
)
subprocess.check_call(["make", model.uboot_config], cwd="u-boot")
subprocess.check_call(
["make", "u-boot.bin", "-j"],
cwd="u-boot",
env={**os.environ, "CROSS_COMPILE": cross[model.arch]},
)
##########################################################################
# u-boot script
uboot_scr = f"{ tmpdir }/boot.scr"
uboot_scr_txt = uboot_scr + ".txt"
with open(uboot_scr_txt, "w") as f:
f.write(
"load mmc 0:2 $kernel_addr_r EFI/debian/grubaa64.efi\n"
"setenv bootargs prefix=(hd0,msdos2)/EFI/debian\n"
"bootefi $kernel_addr_r 0x100\n"
)
subprocess.check_call(
[
"mkimage",
"-A",
"arm64",
"-O",
"linux",
"-T",
"script",
"-C",
"none",
"-n",
"boot.scr",
"-d",
uboot_scr_txt,
uboot_scr,
]
)
##########################################################################
# config.txt
config_txt = f"{ tmpdir }/config.txt"
with open(config_txt, "w") as f:
f.write(f"device_tree={ dtb.rsplit('/',1)[-1] }\n")
f.write("device_tree_address=0x100\n")
f.write("hdmi_group=2\nhdmi_mode=14\ndisable_overscan=1\n")
if model.arch == "arm64":
f.write("arm_64bit=1\n")
f.write("kernel=u-boot.bin\n")
##########################################################################
# Make firmware partition
part1 = f"{ tmpdir }/part1.vfat"
mkfs_vfat(
part1, args.firmware_partition_size * 1024 ** 2, label="FIRMWARE"
)
filenames = [
f"firmware/boot/{ fn }"
for fn in (
"bootcode.bin",
"COPYING.linux",
f"fixup{ model.start_fixup or '' }.dat",
f"fixup{ model.start_fixup or '_' }cd.dat",
f"fixup{ model.start_fixup or '_' }db.dat",
f"fixup{ model.start_fixup or '_' }x.dat",
"LICENCE.broadcom",
"overlays",
f"start{ model.start_fixup or '' }.elf",
f"start{ model.start_fixup or '_' }cd.elf",
f"start{ model.start_fixup or '_' }db.elf",
f"start{ model.start_fixup or '_' }x.elf",
)
] + ["u-boot/u-boot.bin", uboot_scr, config_txt, dtb]
subprocess.check_call(["mcopy", "-s", "-b", "-i", part1, *filenames, "::"])
##########################################################################
# grub.cfg
grub_cfg = f"{ tmpdir }/grub.cfg"
with open(grub_cfg, "w") as f:
f.write(
"insmod part_msdos\n"
"insmod squash4\n"
"set root=(hd0,msdos3)\n"
f"configfile /{ debian_installer[model.arch]['grub.cfg'] }\n"
)
##########################################################################
# Build GRUB image
os.mkdir(f"{ tmpdir }/EFI")
os.mkdir(f"{ tmpdir }/EFI/debian")
grub_deb_url = grub_pkg[model.arch]
grub_deb_fn = "download/" + grub_deb_url.rsplit("/", 1)[1]
if not os.path.exists(grub_deb_fn):
subprocess.check_call(["wget", grub_deb_url], cwd="download")
subprocess.check_call(["dpkg", "-x", grub_deb_fn, f"{ tmpdir }/debs"])
subprocess.check_call(
[
"grub-mkstandalone",
"--directory",
f"{ tmpdir }/debs/usr/lib/grub/arm64-efi",
"-O",
"arm64-efi",
"-o",
f"{ tmpdir }/EFI/debian/grubaa64.efi",
f"/boot/grub/grub.cfg={ grub_cfg }",
]
)
##########################################################################
# Make EFI partition
part2 = f"{ tmpdir }/part2.vfat"
mkfs_vfat(part2, args.efi_partition_size * 1024 ** 2, label="EFI")
subprocess.check_call(
["mcopy", "-s", "-b", "-i", part2, f"{ tmpdir }/EFI", "::"]
)
##########################################################################
# Extra files for debian-installer initrd
extra_files = f"{ tmpdir }/di-initrd-extra-files"
os.mkdir(extra_files)
with open(f"{ extra_files}/preseed.cfg", "w") as f:
f.write("grub-efi-arm64 grub2/update_nvram boolean false\n")
##########################################################################
# Make debian-installer partition
part3 = f"{ tmpdir }/part3.squashfs"
debinst = debian_installer[model.arch]
root_url = debinst["_root_url"]
debinst_dir = f"{ tmpdir }/debian-installer-partition"
os.mkdir(debinst_dir)
for key, rpath in debinst.items():
if key.startswith("_"):
continue
download_path = os.path.join(debian_installer_download, key)
if not os.path.exists(download_path):
url = root_url + rpath
try:
os.makedirs(os.path.dirname(download_path))
except FileExistsError:
...
subprocess.check_call(["wget", "-O", download_path, url])
path = os.path.join(debinst_dir, rpath)
try:
os.makedirs(os.path.dirname(path))
except FileExistsError:
...
shutil.copy2(download_path, path)
if key == "initrd.gz":
with open(path, "ab") as initrd:
gzip_proc = subprocess.Popen(
["gzip", "--no-name", "-9"],
stdout=initrd,
stdin=subprocess.PIPE,
)
cpio_proc = subprocess.Popen(
["cpio", "-o", "-H", "newc"],
cwd=extra_files,
stdout=gzip_proc.stdin,
stdin=subprocess.PIPE,
)
for dirpath, dirnames, filenames in os.walk(extra_files):
prefix = os.path.relpath(dirpath, extra_files)
prefix = "" if prefix == "." else f"{ prefix }/"
for seq in dirnames, filenames:
for item in seq:
cpio_proc.stdin.write(
f"{ prefix }{ item }\n".encode("utf-8")
)
cpio_proc.stdin.close()
if 0 != cpio_proc.wait():
raise Exception("cpio failed")
gzip_proc.stdin.close()
if 0 != gzip_proc.wait():
raise Exception("gzip failed")
subprocess.check_call(["mksquashfs", debinst_dir, part3, "-all-root"])
installer_size_mb = os.stat(part3).st_size // 1024 ** 2 + 1
##########################################################################
# Create image
imgfile = args.output
with open(imgfile, "wb") as f:
f.truncate(
(
1
+ args.firmware_partition_size
+ args.efi_partition_size
+ installer_size_mb
)
* 1024 ** 2
)
proc = subprocess.Popen(
["/sbin/sfdisk", imgfile], stdin=subprocess.PIPE, encoding="utf-8"
)
proc.stdin.write("label: dos\n")
proc.stdin.write("unit: sectors\n")
start = 1
dd_commands = []
for part, size, type in [
(part1, args.firmware_partition_size, "c,bootable"),
(part2, args.efi_partition_size, "ef"),
(part3, installer_size_mb, "83"),
]:
proc.stdin.write(
f"start={ start*2048 }, size={ size*2048 }, type={ type }\n"
)
dd_commands.append(
[
"dd",
f"if={ part }",
f"of={ imgfile }",
"bs=1M",
"conv=sparse",
f"seek={ start }",
]
)
start += size
proc.stdin.close()
if 0 != proc.wait():
raise Exception("sfdisk returned non-zero exit code")
for ddc in dd_commands:
subprocess.check_call(ddc)
def mkfs_vfat(filename, size, *, label=None):
with open(filename, "wb") as f:
f.truncate(size)
args = []
if label is not None:
args.extend(("-n", label))
subprocess.check_call(["/sbin/mkfs.vfat", *args, filename])
if __name__ == "__main__":
main()