projects/guix·blob javascript-team · refs

386 lines · 13.9 KiB

1;;; GNU Guix --- Functional package management for GNU
2;;; Copyright © 2017 David Craven <[email protected]>
3;;; Copyright © 2017, 2019 Mathieu Othacehe <[email protected]>
4;;; Copyright © 2020 Julien Lepiller <[email protected]>
5;;; Copyright © 2020 Jan (janneke) Nieuwenhuizen <[email protected]>
6;;; Copyright © 2022 Maxim Cournoyer <[email protected]>
7;;; Copyright © 2023 Efraim Flashner <[email protected]>
8;;; Copyright © 2023-2024 Herman Rimm <[email protected]>
9;;; Copyright © 2024 Zheng Junjie <[email protected]>
10;;; Copyright © 2025 dan <[email protected]>
11;;; Copyright © 2026 Artyom V. Poptsov <[email protected]>
12;;;
13;;; This file is part of GNU Guix.
14;;;
15;;; GNU Guix is free software; you can redistribute it and/or modify it
16;;; under the terms of the GNU General Public License as published by
17;;; the Free Software Foundation; either version 3 of the License, or (at
18;;; your option) any later version.
19;;;
20;;; GNU Guix is distributed in the hope that it will be useful, but
21;;; WITHOUT ANY WARRANTY; without even the implied warranty of
22;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23;;; GNU General Public License for more details.
24;;;
25;;; You should have received a copy of the GNU General Public License
26;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
27
28(define-module (gnu bootloader u-boot)
29 #:use-module (gnu bootloader extlinux)
30 #:use-module (gnu bootloader)
31 #:use-module (gnu packages bootloaders)
32 #:use-module (guix gexp)
33 #:use-module (ice-9 match)
34 #:export (u-boot-bootloader
35 u-boot-a20-olinuxino-lime-bootloader
36 u-boot-a20-olinuxino-lime2-bootloader
37 u-boot-a20-olinuxino-micro-bootloader
38 u-boot-bananapi-m2-ultra-bootloader
39 u-boot-am335x-evm-bootloader
40 u-boot-beaglebone-black-bootloader
41 u-boot-cubietruck-bootloader
42 u-boot-firefly-rk3399-bootloader
43 u-boot-mx6cuboxi-bootloader
44 u-boot-novena-bootloader
45 u-boot-orangepi-r1-plus-lts-rk3328-bootloader
46 u-boot-orangepi-zero2w-bootloader
47 u-boot-pine64-plus-bootloader
48 u-boot-pine64-lts-bootloader
49 u-boot-pinebook-bootloader
50 u-boot-pinebook-pro-rk3399-bootloader
51 u-boot-nanopi-r4s-rk3399-bootloader
52 u-boot-puma-rk3399-bootloader
53 u-boot-rock-4c-plus-rk3399-bootloader
54 u-boot-rock64-rk3328-bootloader
55 u-boot-rockpro64-rk3399-bootloader
56 u-boot-sifive-unmatched-bootloader
57 u-boot-qemu-riscv64-bootloader
58 u-boot-starfive-visionfive2-bootloader
59 u-boot-ts7970-q-2g-1000mhz-c-bootloader
60 u-boot-wandboard-bootloader))
61
62(define (make-u-boot-installer file)
63 (let ((file
64 (match file
65 ((? string?)
66 (list #~(install-file (string-append bootloader #$file)
67 install-dir)))
68 ((? file-like?)
69 (list #~(mkdir-p install-dir)
70 #~(copy-file #$file
71 (string-append install-dir
72 "/"
73 #$(plain-file-name file)))))
74 (#f '()))))
75 #~(lambda (bootloader device mount-point)
76 (let ((install-dir (string-append mount-point "/boot")))
77 #$@file))))
78
79(define (write-u-boot-image files block-size)
80 "FILES is a list of (FILE COUNT OFFSET) tuples. Each FILE is written
81to the target image at BLOCK-SIZE * OFFSET. The number of bytes written
82is BLOCK-SIZE * COUNT, or FILE size if COUNT is not given."
83 (define (write-file-to-image file)
84 (match file
85 ((file count ... offset)
86 (let* ((file #~(string-append bootloader "/libexec/" #$file))
87 (size (match count
88 (() #~(stat:size (stat #$file)))
89 ((count) (* count block-size)))))
90 #~(write-file-on-device #$file #$size image
91 #$(* offset block-size))))))
92 #~(lambda (bootloader _ image)
93 #$@(map write-file-to-image files)))
94
95(define install-u-boot
96 #~(lambda (bootloader root-index image)
97 (if bootloader
98 (error "Failed to install U-Boot"))))
99
100(define install-am335x-evm-u-boot
101 (write-u-boot-image
102 ;; According to the "26.1.8.5.5 MMC/SD Read Sector Procedure in Raw Mode"
103 ;; section in the "AM335x and AMIC110 Sitara™ Processors Technical
104 ;; Reference Manual", we have offset, the bootrom tries to load code at the
105 ;; offsets 0x0, 0x20000 (128KB), 0x40000 (256KB) and 0x60000 (384KB).
106 ;;
107 ;; The MBR is 512 Bytes
108 ;; (https://en.wikipedia.org/wiki/Master_boot_record#Sector_layout).
109 ;;
110 ;; At the time of writing, u-boot.img is 1.4 MiB, and it is at offset
111 ;; 0x60000 (384KB) as per CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR.
112 ;;
113 ;; We also need to align the first partition to make sure read/writes are
114 ;; properly aligned.
115 ;;
116 ;; Given these constraints, we chose the following partitioning:
117 ;;
118 ;; 0 512 Bytes 128 KiB
119 ;; | | (0x20000) 256 KiB
120 ;; | | / (0x40000) 384 KiB 4 MiB
121 ;; | | / | (0x60000) |
122 ;; | | / | | |
123 ;; v v / v v v
124 ;; +-------+ +--------------+---------------+----------------+
125 ;; | MBR | ... | First MLO | Second MLO | u-boot.img |
126 ;; +-------+ +--------------+---------------+----------------+
127 ;; <-512 B-> ... <-- 128 KiB --> <-- 128 KiB --> <-- 3712 KiB -->
128 ;;
129 ;; The rationale for that is as follows:
130 ;;
131 ;; - We add a second copy of the MLO at 256 KiB for improving reliability
132 ;; as the doc/board/ti/am335x_evm.rst documentation in u-boot advise us
133 ;; to do that, and also because the space there isn't used.
134 ;;
135 ;; - All can fit into 2MiB (u-boot.img can grow up to 1664 KiB), but we
136 ;; want a bit more space in case u-boot.img grows over time, so we allow
137 ;; u-boot to grow until the 4 MiB offset.
138 '(("MLO" 256 256) ;; First MLO
139 ("MLO" 256 512) ;; Second MLO
140 ("u-boot.img" 7424 768)) 512))
141
142(define install-beaglebone-black-u-boot
143 ;; http://wiki.beyondlogic.org/index.php?title=BeagleBoneBlack_Upgrading_uBoot
144 ;; This first stage bootloader called MLO (U-Boot SPL) is expected at
145 ;; 0x20000 by BBB ROM code. The second stage bootloader will be loaded by
146 ;; the MLO and is expected at 0x60000. Write both first stage ("MLO") and
147 ;; second stage ("u-boot.img") images, read in BOOTLOADER directory, to the
148 ;; specified DEVICE.
149 (write-u-boot-image '(("MLO" 256 256) ("u-boot.img" 768 1024)) 512))
150
151(define install-allwinner-u-boot
152 (write-u-boot-image '(("u-boot-sunxi-with-spl.bin" 8)) 1024))
153
154(define install-allwinner64-u-boot
155 (write-u-boot-image '(("u-boot-sunxi-with-spl.bin" 8)
156 ("u-boot-sunxi-with-spl.fit.itb" 40))
157 1024))
158
159(define install-imx-u-boot
160 (write-u-boot-image '(("SPL" 1) ("u-boot.img" 69)) 1024))
161
162(define install-puma-rk3399-u-boot
163 (write-u-boot-image '(("idbloader.img" 64) ("u-boot.itb" 512)) 512))
164
165(define install-rockchip-u-boot
166 (write-u-boot-image '(("idbloader.img" 64) ("u-boot.itb" 16384)) 512))
167
168(define install-sifive-unmatched-u-boot
169 (write-u-boot-image '(("spl/u-boot-spl.bin" 34) ("u-boot.itb" 2082))
170 512))
171
172(define install-starfive-visionfive2-u-boot
173 (write-u-boot-image '(("spl/u-boot-spl.bin.normal.out" 34)
174 ("u-boot.itb" 2082))
175 512))
176
177(define install-starfive-visionfive2-uEnv.txt
178 (make-u-boot-installer
179 ;; If the board SPI uses the vendor's U-Boot, it will find starfive/
180 ;; starfive_visionfive2.dtb. We cannot guarantee that users will
181 ;; update this U-Boot, so set the FDT explicitly.
182 (plain-file "uEnv.txt"
183 (string-join
184 (list
185 "boot_targets=mmc1 nvme0 mmc0 dhcp"
186 "bootcmd_mmc1=bootflow scan"
187 "fdtfile=starfive/jh7110-starfive-visionfive-2-v1.3b.dtb")
188 "\n"))))
189
190
191;;;
192;;; Bootloader definitions.
193;;;
194
195(define u-boot-bootloader
196 (bootloader
197 (inherit extlinux-bootloader)
198 (name 'u-boot)
199 (package #f)
200 (installer #f)
201 (disk-image-installer install-u-boot)))
202
203(define u-boot-am335x-evm-bootloader
204 (bootloader
205 (inherit u-boot-bootloader)
206 ;; This bootloader supports the following computers:
207 ;; - SanCloud BeagleBone Enhanced
208 ;; - SanCloud BeagleBone Enhanced Extended WiFi
209 ;; - SanCloud BeagleBone Enhanced Lite
210 ;; - Seeed Studio BeagleBone Green Eco
211 ;; - TI AM3359 ICE-V2
212 ;; - TI AM335x BeagleBone
213 ;; - TI AM335x BeagleBone Black
214 ;; - TI AM335x BeagleBone Green
215 ;; - TI AM335x EVM
216 ;; - TI AM335x EVM-SK
217 ;; - TI AM335x PocketBeagle
218 ;;
219 ;; This list was made by retrieving the list of Devicetrees used in
220 ;; configs/am335x_evm_defconfig in u-boot source code. Once we have that we
221 ;; can look at each Devicetree in arch/arm/boot/dts/ti/omap/ inside Linux,
222 ;; and in each file, look at the model property to find the name of the
223 ;; computers that this image supports.
224 (package u-boot-am335x-evm)
225 ;; At the time of writing, our u-boot.img is 1.4 MiB. This is why we can't
226 ;; reuse install-beaglebone-black-u-boot. And having the same image being
227 ;; able to boot on multiple computers is useful.
228 (disk-image-installer install-am335x-evm-u-boot)))
229
230(define u-boot-beaglebone-black-bootloader
231 (bootloader
232 (inherit u-boot-bootloader)
233 (package u-boot-am335x-boneblack)
234 (disk-image-installer install-beaglebone-black-u-boot)))
235
236(define u-boot-allwinner-bootloader
237 (bootloader
238 (inherit u-boot-bootloader)
239 (disk-image-installer install-allwinner-u-boot)))
240
241(define u-boot-allwinner64-bootloader
242 (bootloader
243 (inherit u-boot-bootloader)
244 (disk-image-installer install-allwinner64-u-boot)))
245
246(define u-boot-imx-bootloader
247 (bootloader
248 (inherit u-boot-bootloader)
249 (disk-image-installer install-imx-u-boot)))
250
251(define u-boot-rockchip-bootloader
252 ;; SD and eMMC use the same format
253 (bootloader
254 (inherit u-boot-bootloader)
255 (disk-image-installer install-rockchip-u-boot)))
256
257(define u-boot-a20-olinuxino-lime-bootloader
258 (bootloader
259 (inherit u-boot-allwinner-bootloader)
260 (package u-boot-a20-olinuxino-lime)))
261
262(define u-boot-a20-olinuxino-lime2-bootloader
263 (bootloader
264 (inherit u-boot-allwinner-bootloader)
265 (package u-boot-a20-olinuxino-lime2)))
266
267(define u-boot-a20-olinuxino-micro-bootloader
268 (bootloader
269 (inherit u-boot-allwinner-bootloader)
270 (package u-boot-a20-olinuxino-micro)))
271
272(define u-boot-bananapi-m2-ultra-bootloader
273 (bootloader
274 (inherit u-boot-allwinner-bootloader)
275 (package u-boot-bananapi-m2-ultra)))
276
277(define u-boot-cubietruck-bootloader
278 (bootloader
279 (inherit u-boot-allwinner-bootloader)
280 (package u-boot-cubietruck)))
281
282(define u-boot-firefly-rk3399-bootloader
283 (bootloader
284 (inherit u-boot-rockchip-bootloader)
285 (package u-boot-firefly-rk3399)))
286
287(define u-boot-mx6cuboxi-bootloader
288 (bootloader
289 (inherit u-boot-imx-bootloader)
290 (package u-boot-mx6cuboxi)))
291
292(define u-boot-wandboard-bootloader
293 (bootloader
294 (inherit u-boot-imx-bootloader)
295 (package u-boot-wandboard)))
296
297(define u-boot-novena-bootloader
298 (bootloader
299 (inherit u-boot-imx-bootloader)
300 (package u-boot-novena)))
301
302(define u-boot-orangepi-r1-plus-lts-rk3328-bootloader
303 (bootloader
304 (inherit u-boot-rockchip-bootloader)
305 (package u-boot-orangepi-r1-plus-lts-rk3328)))
306
307(define u-boot-orangepi-zero2w-bootloader
308 (bootloader
309 (inherit u-boot-allwinner-bootloader)
310 (package u-boot-orangepi-zero2w)))
311
312(define u-boot-pine64-plus-bootloader
313 (bootloader
314 (inherit u-boot-allwinner64-bootloader)
315 (package u-boot-pine64-plus)))
316
317(define u-boot-pine64-lts-bootloader
318 (bootloader
319 (inherit u-boot-allwinner-bootloader)
320 (package u-boot-pine64-lts)))
321
322(define u-boot-pinebook-bootloader
323 (bootloader
324 (inherit u-boot-allwinner64-bootloader)
325 (package u-boot-pinebook)))
326
327(define u-boot-puma-rk3399-bootloader
328 (bootloader
329 (inherit u-boot-bootloader)
330 (package u-boot-puma-rk3399)
331 (disk-image-installer install-puma-rk3399-u-boot)))
332
333(define u-boot-rock-4c-plus-rk3399-bootloader
334 (bootloader
335 (inherit u-boot-rockchip-bootloader)
336 (package u-boot-rock-4c-plus-rk3399)))
337
338(define u-boot-rock64-rk3328-bootloader
339 (bootloader
340 (inherit u-boot-rockchip-bootloader)
341 (package u-boot-rock64-rk3328)))
342
343(define u-boot-rockpro64-rk3399-bootloader
344 (bootloader
345 (inherit u-boot-rockchip-bootloader)
346 (package u-boot-rockpro64-rk3399)))
347
348(define u-boot-pinebook-pro-rk3399-bootloader
349 (bootloader
350 (inherit u-boot-rockchip-bootloader)
351 (package u-boot-pinebook-pro-rk3399)))
352
353(define u-boot-nanopi-r4s-rk3399-bootloader
354 (bootloader
355 (inherit u-boot-rockchip-bootloader)
356 (package u-boot-nanopi-r4s-rk3399)))
357
358(define u-boot-ts7970-q-2g-1000mhz-c-bootloader
359 ;; This bootloader doesn't really need to be installed, as it is read from
360 ;; an SPI memory chip, not the SD card. It is copied to /boot/u-boot.imx
361 ;; for convenience and should be manually flashed at the U-Boot prompt.
362 (bootloader
363 (inherit u-boot-bootloader)
364 (package u-boot-ts7970-q-2g-1000mhz-c)
365 (installer (make-u-boot-installer "libexec/u-boot.imx"))
366 (disk-image-installer #f)))
367
368(define u-boot-sifive-unmatched-bootloader
369 (bootloader
370 (inherit u-boot-bootloader)
371 (package u-boot-sifive-unmatched)
372 (disk-image-installer install-sifive-unmatched-u-boot)))
373
374(define u-boot-starfive-visionfive2-bootloader
375 (bootloader
376 (inherit u-boot-bootloader)
377 (package u-boot-starfive-visionfive2)
378 (installer install-starfive-visionfive2-uEnv.txt)
379 (disk-image-installer install-starfive-visionfive2-u-boot)))
380
381(define u-boot-qemu-riscv64-bootloader
382 (bootloader
383 (inherit u-boot-bootloader)
384 (package u-boot-qemu-riscv64)
385 (installer (make-u-boot-installer "libexec/u-boot.bin"))
386 (disk-image-installer #f)))