| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 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 | |
| 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 | |
| 44 | (open-file-output-port device
|
| 45 | (file-options no-truncate no-fail)
|
| 46 | (buffer-mode block)
|
| 47 | |
| 48 | |
| 49 | (make-transcoder (latin-1-codec)))
|
| 50 | (lambda (output)
|
| 51 | (seek output offset SEEK_SET)
|
| 52 | (put-bytevector output bv)))))))
|
| 53 |
|
| 54 |
|
| 55 | |
| 56 | |
| 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
|
| 61 | GRUB-CONFIG.
|
| 62 |
|
| 63 | If TARGETS is set, use its car as the GRUB image format and its cdr as
|
| 64 | the output filename. Otherwise, use defaults for the host platform."
|
| 65 | (let* ((system %host-type)
|
| 66 | |
| 67 | |
| 68 | |
| 69 | (grub-mkstandalone (string-append grub "/bin/grub-mkstandalone"))
|
| 70 | (efi-directory (string-append esp "/EFI/BOOT"))
|
| 71 | |
| 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 | |
| 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 | |
| 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
|
| 95 | load the Grub bootloader located in the 'Guix_image' root partition.
|
| 96 |
|
| 97 | If TARGETS is set, use its car as the GRUB image format and its cdr as
|
| 98 | the 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 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 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 |
|