bootloader.scm 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. ;;;
  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. (define-module (gnu build bootloader)
  20. #:use-module (guix build utils)
  21. #:use-module (guix utils)
  22. #:use-module (ice-9 binary-ports)
  23. #:use-module (ice-9 format)
  24. #:use-module (rnrs io ports)
  25. #:use-module (rnrs io simple)
  26. #:export (write-file-on-device
  27. install-efi-loader))
  28. ;;;
  29. ;;; Writing utils.
  30. ;;;
  31. (define (write-file-on-device file size device offset)
  32. "Write SIZE bytes from FILE to DEVICE starting at OFFSET."
  33. (call-with-input-file file
  34. (lambda (input)
  35. (let ((bv (get-bytevector-n input size)))
  36. (call-with-port
  37. ;; Do not use "call-with-output-file" that would truncate the file.
  38. (open-file-output-port device
  39. (file-options no-truncate no-fail)
  40. (buffer-mode block)
  41. ;; Use the binary-friendly ISO-8859-1
  42. ;; encoding.
  43. (make-transcoder (latin-1-codec)))
  44. (lambda (output)
  45. (seek output offset SEEK_SET)
  46. (put-bytevector output bv)))))))
  47. ;;;
  48. ;;; EFI bootloader.
  49. ;;;
  50. (define (install-efi grub grub-config esp)
  51. "Write a self-contained GRUB EFI loader to the mounted ESP using GRUB-CONFIG."
  52. (let* ((system %host-type)
  53. ;; Hard code the output location to a well-known path recognized by
  54. ;; compliant firmware. See "3.5.1.1 Removable Media Boot Behaviour":
  55. ;; http://www.uefi.org/sites/default/files/resources/UEFI%20Spec%202_6.pdf
  56. (grub-mkstandalone (string-append grub "/bin/grub-mkstandalone"))
  57. (efi-directory (string-append esp "/EFI/BOOT"))
  58. ;; Map grub target names to boot file names.
  59. (efi-targets (cond ((string-prefix? "x86_64" system)
  60. '("x86_64-efi" . "BOOTX64.EFI"))
  61. ((string-prefix? "i686" system)
  62. '("i386-efi" . "BOOTIA32.EFI"))
  63. ((string-prefix? "armhf" system)
  64. '("arm-efi" . "BOOTARM.EFI"))
  65. ((string-prefix? "aarch64" system)
  66. '("arm64-efi" . "BOOTAA64.EFI")))))
  67. ;; grub-mkstandalone requires a TMPDIR to prepare the firmware image.
  68. (setenv "TMPDIR" esp)
  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. (define (install-efi-loader grub-efi esp)
  76. "Install in ESP directory the given GRUB-EFI bootloader. Configure it to
  77. load the Grub bootloader located in the 'Guix_image' root partition."
  78. (let ((grub-config "grub.cfg"))
  79. (call-with-output-file grub-config
  80. (lambda (port)
  81. ;; Create a tiny configuration file telling the embedded grub where to
  82. ;; load the real thing. XXX This is quite fragile, and can prevent
  83. ;; the image from booting when there's more than one volume with this
  84. ;; label present. Reproducible almost-UUIDs could reduce the risk
  85. ;; (not eliminate it).
  86. (format port
  87. "insmod part_msdos~@
  88. insmod part_gpt~@
  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)))