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