;;; GNU Guix --- Functional package management for GNU ;;; Copyright © 2017 David Craven ;;; Copyright © 2017, 2020, 2022 Mathieu Othacehe ;;; Copyright © 2017 Leo Famulari ;;; Copyright © 2019, 2021, 2023 Ludovic Courtès ;;; Copyright © 2020 Jan (janneke) Nieuwenhuizen ;;; Copyright © 2022 Josselin Poiret ;;; Copyright © 2022 Reza Alizadeh Majd ;;; Copyright © 2024 Tomas Volf <~@wolfsden.cz> ;;; Copyright © 2024 Lilah Tascheter ;;; ;;; This file is part of GNU Guix. ;;; ;;; GNU Guix is free software; you can redistribute it and/or modify it ;;; under the terms of the GNU General Public License as published by ;;; the Free Software Foundation; either version 3 of the License, or (at ;;; your option) any later version. ;;; ;;; GNU Guix is distributed in the hope that it will be useful, but ;;; WITHOUT ANY WARRANTY; without even the implied warranty of ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ;;; GNU General Public License for more details. ;;; ;;; You should have received a copy of the GNU General Public License ;;; along with GNU Guix. If not, see . (define-module (gnu bootloader) #:autoload (gnu build file-systems) (read-partition-label read-partition-uuid find-partition-by-label find-partition-by-uuid) #:use-module (gnu system file-systems) #:use-module (gnu system uuid) #:autoload (guix build syscalls) (mounts mount-source mount-point mount-type) #:use-module (guix deprecation) #:use-module (guix diagnostics) #:use-module (guix gexp) #:use-module (guix i18n) #:use-module (guix modules) #:use-module (guix profiles) #:use-module (guix records) #:use-module (guix utils) #:use-module (ice-9 match) #:use-module (ice-9 receive) #:use-module (rnrs bytevectors) #:use-module (srfi srfi-1) #:use-module (srfi srfi-26) #:use-module (srfi srfi-34) #:use-module (srfi srfi-35) #:export ( menu-entry menu-entry? menu-entry-label menu-entry-device menu-entry-device-mount-point menu-entry-device-subvol menu-entry-linux menu-entry-linux-arguments menu-entry-initrd menu-entry-multiboot-kernel menu-entry-multiboot-arguments menu-entry-multiboot-modules menu-entry-chain-loader normalize-file menu-entry->sexp sexp->menu-entry bootloader bootloader? bootloader-name bootloader-default-targets bootloader-installer bootloader-target bootloader-target? bootloader-target-type bootloader-target-expected? bootloader-target-path bootloader-target-offset bootloader-target-device bootloader-target-file-system bootloader-target-label bootloader-target-uuid target-error? target-error-type target-error-targets gbegin :path :devpath :device :fs :label :uuid with-targets bootloader-configuration bootloader-configuration? bootloader-configuration-bootloader bootloader-configuration-targets bootloader-configuration-menu-entries bootloader-configuration-default-entry bootloader-configuration-efi-removable? bootloader-configuration-32bit? bootloader-configuration-timeout bootloader-configuration-keyboard-layout bootloader-configuration-theme bootloader-configuration-terminal-outputs bootloader-configuration-terminal-inputs bootloader-configuration-serial-unit bootloader-configuration-serial-speed bootloader-configuration-device-tree-support? bootloader-configuration-extra-initrd bootloader-configuration->gexp bootloader-configurations->gexp %efi-supported-systems efi-arch install-efi)) ;;; ;;; Menu-entry record. ;;; (define-record-type* menu-entry make-menu-entry menu-entry? (label menu-entry-label) (device menu-entry-device ; file system uuid, label, or #f (default #f)) (device-mount-point menu-entry-device-mount-point (default #f)) (device-subvol menu-entry-device-subvol (default #f)) (linux menu-entry-linux (default #f)) (linux-arguments menu-entry-linux-arguments (default '())) ; list of string-valued gexps (initrd menu-entry-initrd ; file name of the initrd as a gexp (default #f)) (multiboot-kernel menu-entry-multiboot-kernel (default #f)) (multiboot-arguments menu-entry-multiboot-arguments (default '())) ; list of string-valued gexps (multiboot-modules menu-entry-multiboot-modules (default '())) ; list of multiboot commands, where ; a command is a list of (chain-loader menu-entry-chain-loader (default #f))) ; string, path of efi file (define (normalize-file entry val) "Normalize a file VAL stored in a menu entry into one suitable for a bootloader. Realizes device-mount-point and device-subvol." (match-record entry (device-mount-point device-subvol) #~(let* ((rel (lambda (s) (substring s (if (string-prefix? "/" s) 1 0)))) (file (rel #$val)) (subvol (and=> #$device-subvol rel)) (mount (and=> #$device-mount-point rel))) (string-append (if subvol (string-append "/" subvol "/") "/") (if (and mount (string-prefix? mount file)) (substring file (string-length mount)) file))))) (define (report-menu-entry-error menu-entry) (raise (condition (&message (message (format #f (G_ "invalid menu-entry: ~a") menu-entry))) (&fix-hint (hint (G_ "Please chose only one of: @enumerate @item direct boot by specifying fields @code{linux}, @code{linux-arguments} and @code{linux-modules}, @item multiboot by specifying fields @code{multiboot-kernel}, @code{multiboot-arguments} and @code{multiboot-modules}, @item chain-loader by specifying field @code{chain-loader}. @end enumerate")))))) (define (menu-entry->sexp entry) "Return ENTRY serialized as an sexp." (define (device->sexp device) (match device ((? uuid? uuid) `(uuid ,(uuid-type uuid) ,(uuid->string uuid))) ((? file-system-label? label) `(label ,(file-system-label->string label))) (_ device))) (match entry (($ label device mount-point subvol (? identity linux) linux-arguments (? identity initrd) #f () () #f) `(menu-entry (version 0) (label ,label) (device ,(device->sexp device)) (device-mount-point ,mount-point) (linux ,linux) (linux-arguments ,linux-arguments) (initrd ,initrd) (device-subvol ,subvol))) (($ label device mount-point subvol #f () #f (? identity multiboot-kernel) multiboot-arguments multiboot-modules #f) `(menu-entry (version 0) (label ,label) (device ,(device->sexp device)) (device-mount-point ,mount-point) (multiboot-kernel ,multiboot-kernel) (multiboot-arguments ,multiboot-arguments) (multiboot-modules ,multiboot-modules) (device-subvol ,subvol))) (($ label device mount-point subvol #f () #f #f () () (? identity chain-loader)) `(menu-entry (version 0) (label ,label) (device ,(device->sexp device)) (device-mount-point ,mount-point) (chain-loader ,chain-loader) (device-subvol ,subvol))) (_ (report-menu-entry-error entry)))) (define (sexp->menu-entry sexp) "Turn SEXP, an sexp as returned by 'menu-entry->sexp', into a record." ;; XXX: rely on shadowing to support the match ors below (define subvol #f) (define (sexp->device device-sexp) (match device-sexp (('uuid type uuid-string) (uuid uuid-string type)) (('label label) (file-system-label label)) (_ device-sexp))) (match sexp (('menu-entry ('version 0) ('label label) ('device device) ('device-mount-point mount-point) ('linux linux) ('linux-arguments linux-arguments) ('initrd initrd) (or ('device-subvol subvol _ ...) (_ ...))) (menu-entry (label label) (device (sexp->device device)) (device-mount-point mount-point) (device-subvol subvol) (linux linux) (linux-arguments linux-arguments) (initrd initrd))) (('menu-entry ('version 0) ('label label) ('device device) ('device-mount-point mount-point) ('device-subvol subvol) ('multiboot-kernel multiboot-kernel) ('multiboot-arguments multiboot-arguments) ('multiboot-modules multiboot-modules) (or ('device-subvol subvol _ ...) (_ ...))) (menu-entry (label label) (device (sexp->device device)) (device-mount-point mount-point) (device-subvol subvol) (multiboot-kernel multiboot-kernel) (multiboot-arguments multiboot-arguments) (multiboot-modules multiboot-modules))) (('menu-entry ('version 0) ('label label) ('device device) ('device-mount-point mount-point) ('device-subvol subvol) ('chain-loader chain-loader) (or ('device-subvol subvol _ ...) (_ ...))) (menu-entry (label label) (device (sexp->device device)) (device-mount-point mount-point) (device-subvol subvol) (chain-loader chain-loader))))) ;;; ;;; Bootloader record. ;;; ;; The record contains fields expressing how the bootloader ;; should be installed. Every bootloader in gnu/bootloader/ directory ;; has to be described by this record. (define-record-type* bootloader make-bootloader bootloader? (name bootloader-name) (default-targets bootloader-default-targets (default '())) (installer bootloader-installer)) ;;; ;;; Bootloader target record. ;;; ;; represents different kinds of targets in a normalized form. (define-record-type* bootloader-target make-bootloader-target bootloader-target? (type bootloader-target-type) ; symbol (expected? bootloader-target-expected? (default #f)) ; bool (path bootloader-target-path (default #f)) ; string|#f (offset bootloader-target-offset (thunked) ; symbol|#f (default (and (bootloader-target-path this-record) (not (eq? (bootloader-target-type this-record) 'root)) 'root))) (device bootloader-target-device (default #f)) ; string|#f (file-system bootloader-target-file-system (default #f)) ; string|#f (label bootloader-target-label (default #f)) ; string|#f (uuid bootloader-target-uuid (default #f))) ; uuid|#f (define-condition-type &target-error &error target-error? (type target-error-type) (targets target-error-targets)) (define (pathcat p1 p2) (string-append (string-trim-right p1 #\/) "/" (string-trim p2 #\/))) (define* (get-target-of-type type targets #:optional (require? #f)) "Finds a target in TARGETS of type TYPE, optionally providing an error when not found if REQUIRE? is provided." (let* ((pred (lambda (target) (eq? type (bootloader-target-type target)))) (candidates (filter pred targets)) (ret (if (pair? candidates) (car candidates) #f))) (if (and require? (not ret)) (raise (condition (&message (message (G_ "required, but not provided"))) (&target-error (type type) (targets targets)))) ret))) (define (parent-of target targets) "Resolves the parent of a target in targets, or #f if parentless." (and=> (bootloader-target-offset target) (cut get-target-of-type <> targets #t))) (define (unfold-pathcat target targets) "Finds the full VFS path of a target." (let ((quit (lambda (t) (not (and=> t bootloader-target-path))))) (reduce pathcat #f (unfold quit bootloader-target-path (cut parent-of <> targets) target)))) (define (target-base? t) (or (not t) (match-record t (expected? offset device label uuid) (or device label uuid (not offset) expected?)))) (define (type-major? target) (memq target '(root esp disk))) (define (ensure types targets end) (let* ((used-in (cute unfold end identity (cut parent-of <> targets) <>)) (cons-in (lambda (t) (cons t (used-in t)))) (ensure (map (cut get-target-of-type <> targets #t) types))) (filter ->bool (apply append (map cons-in ensure))))) (define* (ensure-target-types types targets #:optional (base? #f)) "Ensures all TYPES are provided in TARGETS. Returns #t iff every ensured target and its requirements are fully provided. Errors out when a required TYPE isn't provided. When BASE?, only ensure path requirements up to a device." (not (any bootloader-target-expected? (ensure types targets (if base? target-base? not))))) (define (ensure-majors types targets) "Errors out when a required TYPE isn't provided, or when use of multiple major targets is detected." (let* ((all (map bootloader-target-type (ensure types targets target-base?))) (majors (delete-duplicates (filter type-major? all) eq?))) (if (< (length majors) 2) #t (raise (condition (&message (message (G_ "multiple major targets used"))) (&target-error (type majors) (targets targets))))))) (define (gbegin . gex) "Sequence provided g-expressions." (case (length gex) ((0) #f) ((1) (car gex)) (else #~(begin #$@gex)))) ;; syntax matching on free literals breaks easily, so bind them (define-syntax-rule (define-literal id) (define-syntax id (syntax-rules ()))) (define-literal :path) (define-literal :devpath) (define-literal :device) (define-literal :fs) (define-literal :label) (define-literal :uuid) (define-syntax with-targets (cut syntax-case <> () ((_ targets-expr block ...) (let* ((genvars (compose generate-temporaries iota)) (targets (car (genvars 1))) (path? (cut syntax-case <> (:path) ((_ :path) #t) (_ #f))) (qualified? (cut syntax-case <> (=>) ((_ => spec ...) (any path? #'(spec ...))) (_ #f))) (resolve (lambda (in target base) (with-syntax ((target target) (base base) (targets targets)) (syntax-case in (:path :devpath :device :fs :label :uuid) ((name _) (not (identifier? #'name)) #`(_ (syntax-error "binds must be to identifiers" #,in))) ((name :device) #'(name (bootloader-target-device base))) ((name :label) #'(name (bootloader-target-label base))) ((name :uuid) #'(name (bootloader-target-uuid base))) ((name :fs) #'(name (bootloader-target-file-system base))) ((name :path) #'(name (unfold-pathcat target targets))) ((name :devpath) #'(name (pathcat "/" (bootloader-target-path target)))) (_ #`(_ (syntax-error "invalid binding spec" #,in))))))) (binds (lambda (spec) (syntax-case spec (=>) ((type => binds ...) (with-syntax (((target base) (genvars 2)) (targets targets)) (append #`((get (lambda (t) (get-target-of-type t targets #t))) (target (get type)) (base (if (target-base? target) target (get (bootloader-target-offset target))))) (map (cut resolve <> #'target #'base) #'(binds ...))))) (_ #f)))) (blocks (cut syntax-case <> () ((spec ... expr) (let* ((specs #'(spec ...)) (lets (apply append (filter-map binds specs))) (type (cut syntax-case <> (=>) ((t => _ ...) #'t) (t #'t)))) (receive (full part) (partition qualified? specs) #`(and (ensure-majors (list #,@(map type specs)) #,targets) (ensure-target-types (list #,@(map type part)) #,targets #t) (ensure-target-types (list #,@(map type full)) #,targets #f) (let* #,lets expr))))) (bad #'(syntax-error "malformed block" bad))))) "Using the list TARGETS, evaluate and sequence each BLOCK to produce a gexp. BLOCK is a set of SPECs followed by an EXPR (evaluating to a gexp). Each SPEC denotes a type of target to guard EXPR on their existance and full-qualification. This procedure is linear in regards to BLOCKs. SPEC may be of the following forms: @itemize @item 'TYPE Requires TYPE to be fully present or promised. Errors otherwise. @item ('TYPE => (VAR COMPONENT) ...): As type, but also binds variables. TYPE's COMPONENT is bound to the variable VAR as described below. @end itemize Available COMPONENTs are: @itemize @item :path (fully-qualified) @item :devpath (relative from device) @item :device (auto-detected from uuid and label if not user-provided) @item :fs @item :label @item :uuid @end itemize Note that installers may be called multiple times with different targets being fully-qualified. To ensure that targets aren't installed multiple times, make sure that each BLOCK ensures at least one major target, either directly or indirectly. Corrolarily, at most one major target should be ensured per BLOCK, under the same conditions. Major targets originate from disk image handling, and are currently: @itemize @item disk @item root @item esp @end itemize" #`(let ((#,targets targets-expr)) (apply gbegin (filter ->bool (list #,@(map blocks #'(block ...)))))))) (bad #'(syntax-error "must provide targets" bad)))) ;;; ;;; Bootloader configuration record. ;;; ;; The record contains bootloader independant ;; configuration used to fill bootloader configuration file. (define-with-syntax-properties (warn-update-targets (value properties)) (let ((loc (source-properties->location properties))) (define update (match-lambda ((? bootloader-target? target) (cons #f target)) ((? string? s) (cons #t (if (string-prefix? "/dev" s) (bootloader-target (type 'disk) (device s)) (bootloader-target (type 'esp) (offset 'root) (path s))))) (x (error loc (G_ "invalid target '~a'~%") x)))) (let* ((updated (map update (if (list? value) value (list value)))) (targets (map cdr updated)) (types (map bootloader-target-type targets))) ;; XXX: should this be an error? (when (any car updated) (warning loc (G_ "the 'targets' field should now contain \ records. inferring a best guess (this might break!)...~%"))) (when (not (eqv? (length types) (length (delete-duplicates types)))) (error loc (G_ "the 'targets' field may not contain duplicates~%"))) targets))) (define-record-type* bootloader-configuration make-bootloader-configuration bootloader-configuration? (bootloader bootloader-configuration-bootloader) ; (targets bootloader-configuration-targets (default '()) ;list of strings (sanitize warn-update-targets)) (menu-entries bootloader-configuration-menu-entries (default '())) ;list of (default-entry bootloader-configuration-default-entry (default 0)) ;integer (efi-removable? bootloader-configuration-efi-removable? (default #f)) ;bool (32bit? bootloader-configuration-32bit? (default #f)) ;bool (timeout bootloader-configuration-timeout (default 5)) ;seconds as integer (keyboard-layout bootloader-configuration-keyboard-layout (default #f)) ; | #f (theme bootloader-configuration-theme (default #f)) ;bootloader-specific theme (terminal-outputs bootloader-configuration-terminal-outputs (default #f)) ;list of symbols | #f (default outs) (terminal-inputs bootloader-configuration-terminal-inputs (default #f)) ;list of symbols | #f (default ins) (serial-unit bootloader-configuration-serial-unit (default #f)) ;integer | #f (serial-speed bootloader-configuration-serial-speed (default #f)) ;integer | #f (device-tree-support? bootloader-configuration-device-tree-support? (default #t)) ;boolean (extra-initrd bootloader-configuration-extra-initrd (default #f))) ;string | #f ;;; ;;; Bootloader installation paths. ;;; ;; highest -> lowest priority (define (target-overrides . layers) (let* ((types (fold append '() (map (cute map bootloader-target-type <>) layers))) (pred (lambda (type layer found) (or found (get-target-of-type type layer)))) (find (lambda (type) (fold (cute pred type <> <>) #f layers)))) (filter ->bool (map find (delete-duplicates types))))) (define (normalize targets) "Augments user-supplied targets with filesystem information at runtime, allowing users to specify a lot less information. Relatively minimal to prevent errors. Puts targets into a normal form, where all paths are fully specified up to a device offset." (let* ((mass (lambda (m) `((,(mount-source m) . ,m) (,(mount-point m) . ,m)))) (amounts (delay (apply append (map mass (mounts))))) (accessible=> (lambda (d f) (and d (access? d R_OK) (f d)))) (assoc-mnt (lambda (f) (lambda (v) (and=> (assoc-ref (force amounts) v) f))))) (define (scrape target) (match-record target (expected? path offset device label uuid file-system) (if expected? target (bootloader-target (inherit target) (device (or device (false-if-exception (or (and=> uuid find-partition-by-uuid) (and=> label find-partition-by-label))) (and path ((assoc-mnt mount-source) (unfold-pathcat target targets))))) (label (or label (accessible=> device read-partition-label))) (uuid (or uuid (accessible=> device read-partition-uuid))) (file-system (or file-system (and=> device (assoc-mnt mount-type)))) (offset (and path offset)) (path (or path (and=> device (assoc-mnt mount-point)))))))) (define (fixuuid target) (match-record target (uuid file-system) (let ((type (cond ((not file-system) 'dce) ((member file-system '("vfat" "fat32")) 'fat) ((string=? file-system "ntfs") 'ntfs) ((string=? file-system "iso9660") 'iso9660) (else 'dce)))) (bootloader-target (inherit target) (uuid (cond ((uuid? uuid) uuid) ((bytevector? uuid) (bytevector->uuid uuid type)) ((string? uuid) (string->uuid uuid type)) (else #f))))))) (define (arborify target targets) (let* ((up (lambda (t) (and t (parent-of t targets)))) (proto (unfold target-base? identity up (up target) list)) (chain (reverse (cons target proto)))) (bootloader-target (inherit target) (offset (and=> (car chain) bootloader-target-type)) (path (reduce pathcat #f (map bootloader-target-path (cdr chain))))))) (let ((mid (map (compose fixuuid scrape) targets))) (map (cut arborify <> mid) mid)))) (define* (bootloader-configuration->gexp bootloader-config args #:key (root-offset "/") (overrides '())) "Returns a gexp to install BOOTLOADER-CONFIG to its targets, passing ARGS to each installer alongside the additional #:bootloader-config keyword arguments. Target OVERRIDES are applied and all path targets have ROOT-OFFSET applied. The following keyword arguments are expected in ARGS: @enumerate @item current-boot-alternative @item old-boot-alternatives @item locale (from bootmeta) @item store-directory-prefix (from bootmeta) @item store-crypto-devices (from bootmeta) @end enumerate" (let* ((bootloader (bootloader-configuration-bootloader bootloader-config)) (installer (bootloader-installer bootloader)) (auto-targets (list (bootloader-target (type 'root) (path root-offset) (offset #f)))) (targets (target-overrides overrides (bootloader-configuration-targets bootloader-config) auto-targets (bootloader-default-targets bootloader))) (conf (bootloader-configuration (inherit bootloader-config) (targets (normalize targets))))) (apply installer #:bootloader-config conf args))) (define (bootloader-configurations->gexp bootloader-configs . rest) (apply gbegin (filter-map (cut apply bootloader-configuration->gexp <> rest) bootloader-configs))) = ;;; EFI shit ;;; ;; systems currently supported by efi-arch. should be used for packages relying ;; on it. (define %efi-supported-systems '("i686-linux" "x86_64-linux" "armhf-linux" "aarch64-linux" "riscv64-linux")) (define* (efi-arch #:key (target (or (%current-target-system) (%current-system))) (32? #f)) "Returns the UEFI architecture name for the current target, in lowercase." (cond ((target-x86-32? target) "ia32") ((target-x86-64? target) (if 32? "ia32" "x64")) ((target-arm32? target) "arm") ((target-aarch64? target) (if 32? "arm" "aa64")) ((target-riscv64? target) (if 32? "riscv32" "riscv64")) (else (raise (formatted-message (G_ "no UEFI standard arch for ~a!") target))))) (define (lazy-efibootmgr) "Lazy-loaded efibootmgr package, in order to prevent circular refs." (module-ref (resolve-interface '(gnu packages linux)) 'efibootmgr)) (define (install-efi bootloader-config plan) "Returns a gexp installing PLAN to the ESP, as denoted by the 'vendir target. PLAN is a gexp of a list of '(BUILDER DEST-BASENAME . LABEL) triples, that should be in boot order. If the user selects a removable bootloader, only the first entry in PLAN is used." (match-record bootloader-config (targets efi-removable? 32bit?) (if efi-removable? ;; Hard code the output location to a well-known path recognized by ;; compliant firmware. See "3.5.1.1 Removable Media Boot Behaviour": ;; http://www.uefi.org/sites/default/files/resources/UEFI%20Spec%202_6.pdf (with-targets targets (('esp => (path :path)) #~(let ((boot #$(string-append path "/EFI/BOOT")) (arch #$(string-upcase (efi-arch #:32? 32bit?))) (builder (car (car #$plan)))) (mkdir-p boot) ;; only realize first planspec (builder (string-append boot "/BOOT" arch ".EFI"))))) ;; normal install when not doing a removable config (with-targets targets (('vendir => (vendir :path) (loader :devpath) (disk :device)) #~(install-efi #+(file-append (lazy-efibootmgr) "/sbin/efibootmgr") #$vendir #$loader #$disk #$plan))))))