ryan4yin / nixos-rk3588

Minimal NixOS running on RK3588/RK3588s based SBC(Orange Pi 5 Plus, Orange Pi 5, Rock 5A, etc)
MIT License
152 stars 28 forks source link

create a nixos-generators custom raw-efi format and use it to copy DTB files into /boot/dtb/base #45

Closed cessationoftime closed 1 month ago

cessationoftime commented 1 month ago

create a nixos-generators custom format and use it to copy DTB files into /boot/dtb/base during the boot loader install process of building the image.

I am not 100% sure if this line is correct for a cross build, everything works when building the image natively though.

nixpkgs.hostPlatform = aarch64System

It looks like it is possible to use the custom format to also set the efi boot variables so it wont be necessary to open up the UEFI boot menu and set them manually.

cessationoftime commented 1 month ago

There are a bunch of extra comments in the rk3588-raw-efi file. The information on exactly how contents = [] works to install files to the image wasnt easy to find and required a lot of trial and error. So you may want to leave the comments there just in case someone needs the information to customize the image.

And sadly it turned out I couldn't use contents = [] to install files to the /boot partition anyways.

We should also be able to use this to change EFI variables.

We might be able to put the grub extraInstallCommands in just an ordinary module, but you probably dont need them for U-BOOT so this file is a good way of separating them for UEFI.

Edit: nevermind I deleted those comments after I tried modifying the EFI vars and it didnt work well.

cessationoftime commented 1 month ago

I may be able to include EFI variables in this shortly and then I will clean up what isn't absolutely necessary.

cessationoftime commented 1 month ago

I was trying to make EFI vars be set up automatically but I cant figure out how to get the efivar command to work. It can be done with printf but it probably reduces the safety of the operation. I think I will give up on the EFI var portion because doing it wrong could prevent booting.

this is what I had though:

{
  config,
  lib,
  options,
  pkgs,
  modulesPath,
  specialArgs,
  ...
}: let
  # Import raw-efi from nixos-generators
  raw-efi = specialArgs.nixos-generators.nixosModules.raw-efi;
  inherit (import "${specialArgs.nixos-generators}/lib.nix" {inherit lib options;}) maybe;
in {
  # Reuse and extend the raw-efi format
  imports = [raw-efi];

  boot.loader = {
    efi = {
      canTouchEfiVariables = true;
    };

  # Note: boot.loader.systemd-boot.extraInstallCommands is also available, if someone wants to install systemd-boot instead.

    grub = {
        # 1. Copy the DTB files from the kernel package to /boot/dtb/base
        # 2. set EFI variables

        # Setting the EFI variables should set the following UEFI boot menu entries:
        # In the UEFI boot menu
        # [Device Manager] => [Rockchip Platform Configuration] => [ACPI / Device Tree]
        # Change [Config Table Mode] to Both.
        # Change [Support DTB override & overlays] to Enabled.

        # FdtSupportOverrides: Enabled = 0x01, Disabled = 0x00
        # ConfigTableMode: ACPI = 0x00000001, DeviceTree = 0x00000002, Both = 0x00000003
  extraInstallCommands = ''
        mkdir -p /boot/dtb/base
        cp -r ${config.boot.kernelPackages.kernel}/dtbs/rockchip/* /boot/dtb/base/

        sudo chattr -i /sys/firmware/efi/efivars/FdtSupportOverrides-10f41c33-a468-42cd-85ee-7043213f73a3
        efivar --write --name=10f41c33-a468-42cd-85ee-7043213f73a3-FdtSupportOverrides --value=0001
        sudo chattr +i /sys/firmware/efi/efivars/FdtSupportOverrides-10f41c33-a468-42cd-85ee-7043213f73a3

        sudo chattr -i /sys/firmware/efi/efivars/ConfigTableMode-10f41c33-a468-42cd-85ee-7043213f73a3        
        efivar --write --name=10f41c33-a468-42cd-85ee-7043213f73a3-ConfigTableMode --value=0003
        sudo chattr +i /sys/firmware/efi/efivars/ConfigTableMode-10f41c33-a468-42cd-85ee-7043213f73a3

        sync
      '';
    };
  };

  # Override the system.build.raw to include the custom disk image: mkOverride must be < 99
  system.build.raw = maybe.mkOverride 95 (import "${toString modulesPath}/../lib/make-disk-image.nix" {
    inherit lib config pkgs;
    partitionTableType = "efi";
    diskSize = specialArgs.diskSize or "auto";
    format = "raw";

    # allow modification of EFI variables during image creation
    touchEFIVars = true;

    # this writes to the root partition and not the boot partition! So writing to /etc works, but not to /boot (it writes to the boot mount point)
    # contents = [{
    #   source = "${config.boot.kernelPackages.kernel}/dtbs/rockchip/*";
    #   target = "etc/dtb/base";
    #   mode = "755";
    # }];

  });
}

'--value=' is clearly wrong So you can go ahead and pull/close this.

cessationoftime commented 1 month ago

I was able to get systemd-boot working with the orange pi 5 plus instead of grub after setting this up so I could create an image with the dtb files and systemd easily.

cessationoftime commented 1 month ago

This has been updated and rebased, it should be ready for you to pull it in.