bootloader.scm 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2017 Mathieu Othacehe <m.othacehe@gmail.com>
  3. ;;; Copyright © 2019 Ludovic Courtès <ludo@gnu.org>
  4. ;;; Copyright © 2022 Denis 'GNUtoo' Carikli <GNUtoo@cyberdimension.org>
  5. ;;; Copyright © 2022 Timothy Sample <samplet@ngyro.com>
  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. (define-module (gnu build bootloader)
  22. #:use-module (guix build utils)
  23. #:use-module (guix utils)
  24. #:use-module (ice-9 binary-ports)
  25. #:use-module (ice-9 format)
  26. #:use-module (rnrs io ports)
  27. #:use-module (rnrs io simple)
  28. #:export (write-file-on-device
  29. install-efi-loader))
  30. ;;;
  31. ;;; Writing utils.
  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-port
  39. ;; Do not use "call-with-output-file" that would truncate the file.
  40. (open-file-output-port device
  41. (file-options no-truncate no-fail)
  42. (buffer-mode block)
  43. ;; Use the binary-friendly ISO-8859-1
  44. ;; encoding.
  45. (make-transcoder (latin-1-codec)))
  46. (lambda (output)
  47. (seek output offset SEEK_SET)
  48. (put-bytevector output bv)))))))
  49. ;;;
  50. ;;; EFI bootloader.
  51. ;;;
  52. (define* (install-efi grub grub-config esp #:key targets)
  53. "Write a self-contained GRUB EFI loader to the mounted ESP using
  54. GRUB-CONFIG.
  55. If TARGETS is set, use its car as the GRUB image format and its cdr as
  56. the output filename. Otherwise, use defaults for the host platform."
  57. (let* ((system %host-type)
  58. ;; Hard code the output location to a well-known path recognized by
  59. ;; compliant firmware. See "3.5.1.1 Removable Media Boot Behaviour":
  60. ;; http://www.uefi.org/sites/default/files/resources/UEFI%20Spec%202_6.pdf
  61. (grub-mkstandalone (string-append grub "/bin/grub-mkstandalone"))
  62. (efi-directory (string-append esp "/EFI/BOOT"))
  63. ;; Map grub target names to boot file names.
  64. (efi-targets (or targets
  65. (cond ((string-prefix? "x86_64" system)
  66. '("x86_64-efi" . "BOOTX64.EFI"))
  67. ((string-prefix? "i686" system)
  68. '("i386-efi" . "BOOTIA32.EFI"))
  69. ((string-prefix? "armhf" system)
  70. '("arm-efi" . "BOOTARM.EFI"))
  71. ((string-prefix? "aarch64" system)
  72. '("arm64-efi" . "BOOTAA64.EFI"))))))
  73. ;; grub-mkstandalone requires a TMPDIR to prepare the firmware image.
  74. (setenv "TMPDIR" esp)
  75. (mkdir-p efi-directory)
  76. (invoke grub-mkstandalone "-O" (car efi-targets)
  77. "-o" (string-append efi-directory "/"
  78. (cdr efi-targets))
  79. ;; Graft the configuration file onto the image.
  80. (string-append "boot/grub/grub.cfg=" grub-config))))
  81. (define* (install-efi-loader grub-efi esp #:key targets)
  82. "Install in ESP directory the given GRUB-EFI bootloader. Configure it to
  83. load the Grub bootloader located in the 'Guix_image' root partition.
  84. If TARGETS is set, use its car as the GRUB image format and its cdr as
  85. the output filename. Otherwise, use defaults for the host platform."
  86. (let ((grub-config "grub.cfg"))
  87. (call-with-output-file grub-config
  88. (lambda (port)
  89. ;; Create a tiny configuration file telling the embedded grub where to
  90. ;; load the real thing. XXX This is quite fragile, and can prevent
  91. ;; the image from booting when there's more than one volume with this
  92. ;; label present. Reproducible almost-UUIDs could reduce the risk
  93. ;; (not eliminate it).
  94. (format port
  95. "insmod part_msdos~@
  96. insmod part_gpt~@
  97. search --set=root --label Guix_image~@
  98. configfile /boot/grub/grub.cfg~%")))
  99. (install-efi grub-efi grub-config esp #:targets targets)
  100. (delete-file grub-config)))