Subject: [PATCH v2 06/15] gnu: bootloader: Add raspberry pi bootloader.

From: Lilah Tascheter <[email-masked]>
Date: Sun, 4 Aug 2024 13:06:05 -0500

Less adding and more making it an actual bootloader rather than some
weirdly specified packages.

* gnu/bootloader/u-boot.scm (rpi-config, install-rpi): New procedures.
  (define-u-bootloader-rpi): New macro.
  (u-boot-rpi-2-bootloader, u-boot-rpi-3-bootloader,
  u-boot-rpi-4-bootloader, u-boot-rpi-bootloader): New variables.

* gnu/packages/bootloaders.scm (make-u-boot-bin-package): Delete
  procedure.
  (%u-boot-rpi-efi-description, %u-boot-rpi-efi-description-32-bit,
  u-boot-rpi-2-efi, u-boot-rpi-3-32b-efi, u-boot-rpi-4-32b-efi,
  u-boot-rpi-arm64-efi, u-boot-rpi-2-bin, u-boot-rpi-3_32b-bin,
  u-boot-rpi-4_32b-bin, u-boot-rpi-arm64-bin, u-boot-rpi-2-efi-bin,
  u-boot-rpi-3-32b-efi-bin, u-boot-rpi-4-32b-efi-bin,
  u-boot-rpi-arm64-efi-bin): Delete variables.

Change-Id: I5139a0b00ec89189e8e7c84e06a7a3b7240259cd
---
 gnu/bootloader/u-boot.scm    | 66 ++++++++++++++++++++++++-
 gnu/packages/bootloaders.scm | 94 +++---------------------------------
 2 files changed, 71 insertions(+), 89 deletions(-)

diff --git a/gnu/bootloader/u-boot.scm b/gnu/bootloader/u-boot.scm
index 7d3e202f8c..e8dfe9b3a2 100644
--- a/gnu/bootloader/u-boot.scm
+++ b/gnu/bootloader/u-boot.scm
@@ -28,7 +28,10 @@ (define-module (gnu bootloader u-boot)
   #:use-module (gnu bootloader)
   #:use-module (gnu bootloader extlinux)
   #:use-module (gnu packages bootloaders)
+  #:use-module (gnu packages raspberry-pi)
+  #:use-module (gnu system boot)
   #:use-module (guix gexp)
+  #:use-module (guix utils)
   #:export (u-boot-a20-olinuxino-lime-bootloader
             u-boot-a20-olinuxino-lime2-bootloader
             u-boot-a20-olinuxino-micro-bootloader
@@ -51,7 +54,11 @@ (define-module (gnu bootloader u-boot)
             u-boot-qemu-riscv64-bootloader
             u-boot-starfive-visionfive2-bootloader
             u-boot-ts7970-q-2g-1000mhz-c-bootloader
-            u-boot-wandboard-bootloader))
+            u-boot-wandboard-bootloader
+            u-boot-rpi-2-bootloader
+            u-boot-rpi-3-bootloader
+            u-boot-rpi-4-bootloader
+            u-boot-rpi-bootloader))
 
 (define (make-install-u-boot firmware installers)
   (lambda* (#:key bootloader-config #:allow-other-keys . args)
@@ -222,3 +229,60 @@ (define-u-bootloader-copy u-boot-ts7970-q-2g-1000mhz-c-bootloader
 
 (define-u-bootloader-copy u-boot-qemu-riscv64-bootloader
   u-boot-qemu-riscv64 "u-boot.bin")
+
+
+;;;
+;;; RasPi bootloader definitions.
+;;;
+
+(define (rpi-config 32?)
+  ;; allows a user-specified custom.txt
+  (plain-file "config.txt"
+    (format #f
+      "arm_64bit=~a~%enable_uart=1~%kernel=u-boot.bin~%include custom.txt~%"
+      (if (or 32? (not (target-64bit?))) "0" "1"))))
+
+(define (install-rpi u-boot-32 u-boot-64)
+  (lambda* (#:key bootloader-config #:allow-other-keys . args)
+    (with-targets (bootloader-configuration-targets bootloader-config)
+      ('install (apply install-extlinux-config args))
+      (('firmware => (firmware :path))
+       (let* ((32? (bootloader-configuration-32bit? bootloader-config))
+              (use-32? (or 32? (not (target-64bit?)) (not u-boot-64))))
+         #~(begin
+             (atomic-copy #$(file-append (if use-32? u-boot-32 u-boot-64)
+                                         "/libexec/u-boot.bin")
+                          (string-append #$firmware "/u-boot.bin"))
+             (atomic-copy #$(rpi-config use-32?)
+                          (string-append #$firmware "/config.txt"))))))))
+
+(define-syntax-rule (define-u-bootloader-rpi def-name u-boot-32 u-boot-64)
+  (define def-name
+    (bootloader (name 'u-boot)
+                (default-targets
+                  (list (bootloader-target (type 'install)
+                                           (offset 'firmware)
+                                           (path "extlinux"))
+                        (bootloader-target (type 'firmware)
+                                           (offset 'root)
+                                           (path "boot"))))
+                (installer (install-rpi u-boot-32 u-boot-64)))))
+
+
+;; These neither install firmware nor device-tree files for the Raspberry Pi.
+;; They just assume them to be existing in 'install in the same way that some
+;; UEFI firmware with ACPI data is usually assumed to be existing on PCs.
+;; They can be used with either extlinux or as UEFI firmware (alongside, eg,
+;; GRUB).
+(define-u-bootloader-rpi u-boot-rpi-2-bootloader
+  u-boot-rpi-2 #f)
+
+(define-u-bootloader-rpi u-boot-rpi-3-bootloader
+  u-boot-rpi-3-32b u-boot-rpi-arm64)
+
+(define-u-bootloader-rpi u-boot-rpi-4-bootloader
+  u-boot-rpi-4-32b u-boot-rpi-arm64)
+
+;; Usable for any 64-bit raspberry pi.
+(define-u-bootloader-rpi u-boot-rpi-bootloader
+  #f u-boot-rpi-arm64)
diff --git a/gnu/packages/bootloaders.scm b/gnu/packages/bootloaders.scm
index 12f918a123..e78602379d 100644
--- a/gnu/packages/bootloaders.scm
+++ b/gnu/packages/bootloaders.scm
@@ -1409,40 +1409,8 @@ (define-public u-boot-pinebook-pro-rk3399
        (modify-inputs (package-inputs base)
          (append arm-trusted-firmware-rk3399))))))
 
-(define*-public (make-u-boot-bin-package u-boot-package
-                                         #:key
-                                         (u-boot-bin "u-boot.bin"))
-  "Return a package with a single U-BOOT-BIN file from the U-BOOT-PACKAGE.
-The package name will be that of the U-BOOT package suffixed with \"-bin\"."
-  (package
-    (name (string-append (package-name u-boot-package) "-bin"))
-    (version (package-version u-boot-package))
-    (source #f)
-    (build-system trivial-build-system)
-    (arguments
-     (list
-      #:builder
-      (with-imported-modules '((guix build utils))
-        #~(begin
-            (use-modules (guix build utils))
-            (mkdir #$output)
-            (symlink (search-input-file %build-inputs
-                                        (string-append "libexec/" #$u-boot-bin))
-                     (string-append #$output "/" #$u-boot-bin))))))
-    (inputs (list u-boot-package))
-    (home-page (package-home-page u-boot-package))
-    (synopsis (package-synopsis u-boot-package))
-    (description (string-append
-                  (package-description u-boot-package)
-                  "\n\n"
-                  (format #f
-                          "This package only contains the file ~a."
-                          u-boot-bin)))
-    (license (package-license u-boot-package))))
-
-(define-public %u-boot-rpi-efi-configs
-  '("CONFIG_OF_EMBED"
-    "CONFIG_OF_BOARD=y"))
+;; get dtbs from firmware to support dtoverlays
+(define-public %u-boot-rpi-configs '("CONFIG_OF_EMBED" "CONFIG_OF_BOARD=y"))
 
 (define %u-boot-rpi-description-32-bit
   "This is a 32-bit build of U-Boot.")
@@ -1451,76 +1419,26 @@ (define %u-boot-rpi-description-64-bit
   "This is a common 64-bit build of U-Boot for all 64-bit capable Raspberry Pi
 variants.")
 
-(define %u-boot-rpi-efi-description
-  "It allows network booting and uses the device-tree from the firmware,
-allowing the usage of overlays.  It can act as an EFI firmware for the
-grub-efi-netboot-removable-bootloader.")
-
-(define %u-boot-rpi-efi-description-32-bit
-  (string-append %u-boot-rpi-efi-description "  "
-                 %u-boot-rpi-description-32-bit))
-
 (define-public u-boot-rpi-2
   (make-u-boot-package "rpi_2" "arm-linux-gnueabihf"
+                       #:configs %u-boot-rpi-configs
                        #:append-description %u-boot-rpi-description-32-bit))
 
 (define-public u-boot-rpi-3-32b
   (make-u-boot-package "rpi_3_32b" "arm-linux-gnueabihf"
+                       #:configs %u-boot-rpi-configs
                        #:append-description %u-boot-rpi-description-32-bit))
 
 (define-public u-boot-rpi-4-32b
   (make-u-boot-package "rpi_4_32b" "arm-linux-gnueabihf"
+                       #:configs %u-boot-rpi-configs
                        #:append-description %u-boot-rpi-description-32-bit))
 
 (define-public u-boot-rpi-arm64
   (make-u-boot-package "rpi_arm64" "aarch64-linux-gnu"
+                       #:configs %u-boot-rpi-configs
                        #:append-description %u-boot-rpi-description-64-bit))
 
-(define-public u-boot-rpi-2-efi
-  (make-u-boot-package "rpi_2" "arm-linux-gnueabihf"
-                       #:name-suffix "-efi"
-                       #:configs %u-boot-rpi-efi-configs
-                       #:append-description %u-boot-rpi-efi-description-32-bit))
-
-(define-public u-boot-rpi-3-32b-efi
-  (make-u-boot-package "rpi_3_32b" "arm-linux-gnueabihf"
-                       #:name-suffix "-efi"
-                       #:configs %u-boot-rpi-efi-configs
-                       #:append-description %u-boot-rpi-efi-description-32-bit))
-
-(define-public u-boot-rpi-4-32b-efi
-  (make-u-boot-package "rpi_4_32b" "arm-linux-gnueabihf"
-                       #:name-suffix "-efi"
-                       #:configs %u-boot-rpi-efi-configs
-                       #:append-description %u-boot-rpi-efi-description-32-bit))
-
-(define-public u-boot-rpi-arm64-efi
-  (make-u-boot-package "rpi_arm64""aarch64-linux-gnu"
-                       #:name-suffix "-efi"
-                       #:configs %u-boot-rpi-efi-configs
-                       #:append-description (string-append
-                                             %u-boot-rpi-efi-description "  "
-                                             %u-boot-rpi-description-64-bit)))
-
-(define-public u-boot-rpi-2-bin (make-u-boot-bin-package u-boot-rpi-2))
-
-(define-public u-boot-rpi-3_32b-bin (make-u-boot-bin-package u-boot-rpi-3-32b))
-
-(define-public u-boot-rpi-4_32b-bin (make-u-boot-bin-package u-boot-rpi-4-32b))
-
-(define-public u-boot-rpi-arm64-bin (make-u-boot-bin-package u-boot-rpi-arm64))
-
-(define-public u-boot-rpi-2-efi-bin (make-u-boot-bin-package u-boot-rpi-2-efi))
-
-(define-public u-boot-rpi-3-32b-efi-bin
-  (make-u-boot-bin-package u-boot-rpi-3-32b-efi))
-
-(define-public u-boot-rpi-4-32b-efi-bin
-  (make-u-boot-bin-package u-boot-rpi-4-32b-efi))
-
-(define-public u-boot-rpi-arm64-efi-bin
-  (make-u-boot-bin-package u-boot-rpi-arm64-efi))
-
 (define u-boot-ts-mx6
   ;; There is no release; use the latest commit of the
   ;; 'imx_v2015.04_3.14.52_1.1.0_ga' branch.
-- 
2.45.2