NixOS / nixpkgs

Nix Packages collection & NixOS
MIT License
17.15k stars 13.42k forks source link

Support Clover as a boot loader #124132

Open tie opened 3 years ago

tie commented 3 years ago

Project description

Clover is a boot loader for UEFI and legacy BIOS systems and allows emulating UEFI firmware when booting from legacy BIOS.

It also supports adding missing UEFI functionality like booting from NVMe SSDs when motherboard’s firmware does not support that—I’ve actually set that up in a kinda non-reproducible manner on my friend’s PC for dual booting Windows and Fedora using the default config from Clover’s live CD ISO.

The configuration file uses Apple’s plist format (see config-sample.plist) which nix already supports via lib.generators.toPlist. Drivers may additionally be copied to /EFI/CLOVER/drivers/{EFI,BIOS}/ on EFI partition.

Since the documentation seems to be scattered all over the place, I’ve tried to gather useful bits:

``` # 0. Clover release. version=5135 curl -L -O "https://github.com/CloverHackyColor/CloverBootloader/releases/download/$version/CloverV2-${version}.zip" unzip CloverV2-5135.zip clover=$PWD/CloverV2 ``` ``` # 1. MBR sector; diskloader in Clover’s postinstall. ## Option 1.1: MBR sector that searches for active partition in MBR table (then GPT table). boot0=$clover/BootSectors/boot0af ## Option 1.2: MBR sector that searches for partition with valid PBR signature regardless if it's active or not. boot0=$clover/BootSectors/boot0ss # 2. PBR sector; ## PBR sector for FAT32 formatted partition. Searches for file "boot" in the root of the partition. boot1=$clover/BootSectors/boot1f32 # 3. Boot loader; partitionloader in Clover’s postinstall. ## Option 3.1: Default stage2 loader. boot2=$clover/Bootloaders/x64/boot6 ## Option 3.2: For some buggy BIOS where BiosBlockIO is needed. boot2=$clover/Bootloaders/x64/boot7 # 4. Disk and partition. ## Disk drive to use. disk=/dev/sda ## EFI/FAT32 partition to use. part=/dev/sda1 ``` The following steps are potentially destructive. ``` # 5.1 Merge MBR sector. # See https://wiki.osdev.org/MBR_(x86)#MBR_Format dd if=$disk of=mbr bs=512 count=1 dd if=$boot0 of=mbr bs=1 count=440 conv=notrunc # 5.2 Write MBR sector. dd if=mbr of=$disk count=1 bs=512 # 6.1. Merge PBR sector. # See https://wiki.osdev.org/FAT#BPB_.28BIOS_Parameter_Block.29 # and https://wiki.osdev.org/FAT#FAT_32_2 cp $boot1 newbs dd if=$part of=oldbs bs=512 count=1 dd if=oldbs of=newbs skip=3 seek=3 bs=1 count=87 conv=notrunc # 6.2. Write PBR sector. dd if=newbs of=$part bs=512 count=1 ``` The following steps may run before boot sector rewrite. ``` # 7. Mount partition. mount $part /esp # 8. Copy stage2 boot loader file to EFI partition. cp $boot2 /esp/boot ``` ``` # 9. Copy EFI files. Note that default config from zip probably won’t boot on # most systems. Using config from iso file should work, but ideally it should # be generated by Nix. cp -a $clover/EFI/CLOVER /esp/EFI/CLOVER ``` Example of what dd skip from step 6.1 does (zeroes represent original data):
Expand to see output ```sh mkfile 512 old LANG=C tr '\000' '\377' new dd if=old of=new skip=3 seek=3 bs=1 count=87 conv=notrunc xxd new ``` ``` 00000000: ffff ff00 0000 0000 0000 0000 0000 0000 ................ 00000010: 0000 0000 0000 0000 0000 0000 0000 0000 ................ 00000020: 0000 0000 0000 0000 0000 0000 0000 0000 ................ 00000030: 0000 0000 0000 0000 0000 0000 0000 0000 ................ 00000040: 0000 0000 0000 0000 0000 0000 0000 0000 ................ 00000050: 0000 0000 0000 0000 0000 ffff ffff ffff ................ 00000060: ffff ffff ffff ffff ffff ffff ffff ffff ................ 00000070: ffff ffff ffff ffff ffff ffff ffff ffff ................ 00000080: ffff ffff ffff ffff ffff ffff ffff ffff ................ 00000090: ffff ffff ffff ffff ffff ffff ffff ffff ................ 000000a0: ffff ffff ffff ffff ffff ffff ffff ffff ................ 000000b0: ffff ffff ffff ffff ffff ffff ffff ffff ................ 000000c0: ffff ffff ffff ffff ffff ffff ffff ffff ................ 000000d0: ffff ffff ffff ffff ffff ffff ffff ffff ................ 000000e0: ffff ffff ffff ffff ffff ffff ffff ffff ................ 000000f0: ffff ffff ffff ffff ffff ffff ffff ffff ................ 00000100: ffff ffff ffff ffff ffff ffff ffff ffff ................ 00000110: ffff ffff ffff ffff ffff ffff ffff ffff ................ 00000120: ffff ffff ffff ffff ffff ffff ffff ffff ................ 00000130: ffff ffff ffff ffff ffff ffff ffff ffff ................ 00000140: ffff ffff ffff ffff ffff ffff ffff ffff ................ 00000150: ffff ffff ffff ffff ffff ffff ffff ffff ................ 00000160: ffff ffff ffff ffff ffff ffff ffff ffff ................ 00000170: ffff ffff ffff ffff ffff ffff ffff ffff ................ 00000180: ffff ffff ffff ffff ffff ffff ffff ffff ................ 00000190: ffff ffff ffff ffff ffff ffff ffff ffff ................ 000001a0: ffff ffff ffff ffff ffff ffff ffff ffff ................ 000001b0: ffff ffff ffff ffff ffff ffff ffff ffff ................ 000001c0: ffff ffff ffff ffff ffff ffff ffff ffff ................ 000001d0: ffff ffff ffff ffff ffff ffff ffff ffff ................ 000001e0: ffff ffff ffff ffff ffff ffff ffff ffff ................ 000001f0: ffff ffff ffff ffff ffff ffff ffff ffff ................ ```
See also - https://wiki.archlinux.org/title/Clover#BIOS_Systems - https://github.com/m13253/clover-linux-installer#for-some-buggy-bios - https://github.com/CloverHackyColor/CloverBootloader/blob/36e63bd7f08a82d39a3f9354a8a077eb87784dcd/BootHFS/Installation.txt#L56-L61 - https://github.com/CloverHackyColor/CloverBootloader/blob/36e63bd7f08a82d39a3f9354a8a077eb87784dcd/BootHFS/Description.txt#L11-L19 - https://github.com/CloverHackyColor/CloverBootloader/blob/36e63bd7f08a82d39a3f9354a8a077eb87784dcd/CloverPackage/CloverV2/EFI/CLOVER/doc/UEFI%20boot%20with%20Clover.rtf - https://github.com/CloverHackyColor/CloverBootloader/blob/36e63bd7f08a82d39a3f9354a8a077eb87784dcd/CloverPackage/package/Scripts.templates/InstallBootsectors/postinstall - https://github.com/CloverHackyColor/CloverBootloader/blob/36e63bd7f08a82d39a3f9354a8a077eb87784dcd/CloverPackage/package/Scripts.templates/EFIFolder/preinstall - https://sourceforge.net/p/cloverefiboot/wiki/Configuration/#configplist-structure - https://github.com/CloverHackyColor/CloverBootloader/releases/tag/5129 (PDF docs attachment, though it’s written in Russian)

That said, it’s enough for me to have a “standard” way to install Clover on BIOS system (similar to rEFInd package). Then it’d be possible to use any other supported UEFI boot loader (e.g. systemd-boot) for NixOS installation.

Metadata

aviallon commented 2 years ago

I would love that so much. I have a weird laptop with a Intel i3 2d-gen, and... a legacy BIOS. I sure could use some Clover here, because I'd love to be able to use my 4TB HDD in my laptop :P

arbv commented 2 years ago

It is possible to install Clover manually and then chainload sytemd-boot (which is natively supported by NixOS).

Shados commented 1 year ago

It also supports adding missing UEFI functionality like booting from NVMe SSDs when motherboard’s firmware does not support that—I’ve actually set that up in a kinda non-reproducible manner on my friend’s PC for dual booting Windows and Fedora using the default config from Clover’s live CD ISO.

In theory, you shouldn't need Clover for this if your motherboard is using UEFI, as dynamic driver loading from EFI system partitions is standard UEFI functionality. efibootmgr can manipulate the DriverXXXX and DriverOrder variables needed to tell your UEFI to do this, using the --driver/-r flag.

In practice: I don't know if UEFI implementations in the wild adhere to that part of the specification. I hope they do.