projects/guix·blob python-team · refs

114 lines · 4.9 KiB

1;;; GNU Guix --- Functional package management for GNU
2;;; Copyright © 2017 Mathieu Othacehe <[email protected]>
3;;; Copyright © 2019 Ludovic Courtès <[email protected]>
4;;; Copyright © 2022 Denis 'GNUtoo' Carikli <[email protected]>
5;;; Copyright © 2022 Timothy Sample <[email protected]>
6;;;
7;;; This file is part of GNU Guix.
8;;;
9;;; GNU Guix is free software; you can redistribute it and/or modify it
10;;; under the terms of the GNU General Public License as published by
11;;; the Free Software Foundation; either version 3 of the License, or (at
12;;; your option) any later version.
13;;;
14;;; GNU Guix is distributed in the hope that it will be useful, but
15;;; WITHOUT ANY WARRANTY; without even the implied warranty of
16;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17;;; GNU General Public License for more details.
18;;;
19;;; You should have received a copy of the GNU General Public License
20;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
21
22(define-module (gnu build bootloader)
23 #:use-module (guix build utils)
24 #:use-module (guix utils)
25 #:use-module (ice-9 binary-ports)
26 #:use-module (ice-9 format)
27 #:use-module (rnrs io ports)
28 #:use-module (rnrs io simple)
29 #:export (write-file-on-device
30 install-efi-loader))
31
32
33;;;
34;;; Writing utils.
35;;;
36
37(define (write-file-on-device file size device offset)
38 "Write SIZE bytes from FILE to DEVICE starting at OFFSET."
39 (call-with-input-file file
40 (lambda (input)
41 (let ((bv (get-bytevector-n input size)))
42 (call-with-port
43 ;; Do not use "call-with-output-file" that would truncate the file.
44 (open-file-output-port device
45 (file-options no-truncate no-fail)
46 (buffer-mode block)
47 ;; Use the binary-friendly ISO-8859-1
48 ;; encoding.
49 (make-transcoder (latin-1-codec)))
50 (lambda (output)
51 (seek output offset SEEK_SET)
52 (put-bytevector output bv)))))))
53
54
55;;;
56;;; EFI bootloader.
57;;;
58
59(define* (install-efi grub grub-config esp #:key targets)
60 "Write a self-contained GRUB EFI loader to the mounted ESP using
61GRUB-CONFIG.
62
63If TARGETS is set, use its car as the GRUB image format and its cdr as
64the output filename. Otherwise, use defaults for the host platform."
65 (let* ((system %host-type)
66 ;; Hard code the output location to a well-known path recognized by
67 ;; compliant firmware. See "3.5.1.1 Removable Media Boot Behaviour":
68 ;; http://www.uefi.org/sites/default/files/resources/UEFI%20Spec%202_6.pdf
69 (grub-mkstandalone (string-append grub "/bin/grub-mkstandalone"))
70 (efi-directory (string-append esp "/EFI/BOOT"))
71 ;; Map grub target names to boot file names.
72 (efi-targets (or targets
73 (cond ((string-prefix? "x86_64" system)
74 '("x86_64-efi" . "BOOTX64.EFI"))
75 ((string-prefix? "i686" system)
76 '("i386-efi" . "BOOTIA32.EFI"))
77 ((string-prefix? "armhf" system)
78 '("arm-efi" . "BOOTARM.EFI"))
79 ((string-prefix? "aarch64" system)
80 '("arm64-efi" . "BOOTAA64.EFI"))
81 ((string-prefix? "riscv64" system)
82 '("riscv64-efi" . "BOOTRISCV64.EFI"))))))
83 ;; grub-mkstandalone requires a TMPDIR to prepare the firmware image.
84 (setenv "TMPDIR" esp)
85
86 (mkdir-p efi-directory)
87 (invoke grub-mkstandalone "-O" (car efi-targets)
88 "-o" (string-append efi-directory "/"
89 (cdr efi-targets))
90 ;; Graft the configuration file onto the image.
91 (string-append "boot/grub/grub.cfg=" grub-config))))
92
93(define* (install-efi-loader grub-efi esp #:key targets)
94 "Install in ESP directory the given GRUB-EFI bootloader. Configure it to
95load the Grub bootloader located in the 'Guix_image' root partition.
96
97If TARGETS is set, use its car as the GRUB image format and its cdr as
98the output filename. Otherwise, use defaults for the host platform."
99 (let ((grub-config "grub.cfg"))
100 (call-with-output-file grub-config
101 (lambda (port)
102 ;; Create a tiny configuration file telling the embedded grub where to
103 ;; load the real thing. XXX This is quite fragile, and can prevent
104 ;; the image from booting when there's more than one volume with this
105 ;; label present. Reproducible almost-UUIDs could reduce the risk
106 ;; (not eliminate it).
107 (format port
108 "insmod part_msdos~@
109 insmod part_gpt~@
110 search --set=root --label Guix_image~@
111 configfile /boot/grub/grub.cfg~%")))
112 (install-efi grub-efi grub-config esp #:targets targets)
113 (delete-file grub-config)))
114