1*4882a593SmuzhiyunNotes on using Grub2 for BIOS-based platforms 2*4882a593Smuzhiyun============================================= 3*4882a593Smuzhiyun 4*4882a593Smuzhiyun1. Create a disk image 5*4882a593Smuzhiyun dd if=/dev/zero of=disk.img bs=1M count=32 6*4882a593Smuzhiyun2. Partition it (either legacy or GPT style partitions work) 7*4882a593Smuzhiyun cfdisk disk.img 8*4882a593Smuzhiyun - Create one partition, type Linux, for the root 9*4882a593Smuzhiyun filesystem. The only constraint is to make sure there 10*4882a593Smuzhiyun is enough free space *before* the first partition to 11*4882a593Smuzhiyun store Grub2. Leaving 1 MB of free space is safe. 12*4882a593Smuzhiyun3. Setup loop device and loop partitions 13*4882a593Smuzhiyun sudo losetup -f disk.img 14*4882a593Smuzhiyun sudo partx -a /dev/loop0 15*4882a593Smuzhiyun4. Prepare the root partition 16*4882a593Smuzhiyun sudo mkfs.ext3 -L root /dev/loop0p1 17*4882a593Smuzhiyun sudo mount /dev/loop0p1 /mnt 18*4882a593Smuzhiyun sudo tar -C /mnt -xf output/images/rootfs.tar 19*4882a593Smuzhiyun sudo umount /mnt 20*4882a593Smuzhiyun5. Install Grub2 21*4882a593Smuzhiyun sudo ./output/host/sbin/grub-bios-setup \ 22*4882a593Smuzhiyun -b ./output/host/lib/grub/i386-pc/boot.img \ 23*4882a593Smuzhiyun -c ./output/images/grub.img -d . /dev/loop0 24*4882a593Smuzhiyun6. Cleanup loop device 25*4882a593Smuzhiyun sudo partx -d /dev/loop0 26*4882a593Smuzhiyun sudo losetup -d /dev/loop0 27*4882a593Smuzhiyun7. Your disk.img is ready! 28*4882a593Smuzhiyun 29*4882a593SmuzhiyunUsing genimage 30*4882a593Smuzhiyun-------------- 31*4882a593Smuzhiyun 32*4882a593SmuzhiyunIf you use genimage to generate your complete image, 33*4882a593Smuzhiyuninstalling Grub can be tricky. Here is how to achieve Grub's 34*4882a593Smuzhiyuninstallation with genimage: 35*4882a593Smuzhiyun 36*4882a593Smuzhiyunpartition boot { 37*4882a593Smuzhiyun in-partition-table = "no" 38*4882a593Smuzhiyun image = "path_to_boot.img" 39*4882a593Smuzhiyun offset = 0 40*4882a593Smuzhiyun size = 512 41*4882a593Smuzhiyun} 42*4882a593Smuzhiyunpartition grub { 43*4882a593Smuzhiyun in-partition-table = "no" 44*4882a593Smuzhiyun image = "path_to_grub.img" 45*4882a593Smuzhiyun offset = 512 46*4882a593Smuzhiyun} 47*4882a593Smuzhiyun 48*4882a593SmuzhiyunThe result is not byte to byte identical to what 49*4882a593Smuzhiyungrub-bios-setup does but it works anyway. 50*4882a593Smuzhiyun 51*4882a593SmuzhiyunTo test your BIOS image in Qemu 52*4882a593Smuzhiyun------------------------------- 53*4882a593Smuzhiyun 54*4882a593Smuzhiyunqemu-system-{i386,x86-64} -hda disk.img 55*4882a593Smuzhiyun 56*4882a593SmuzhiyunNotes on using Grub2 for x86/x86_64 EFI-based platforms 57*4882a593Smuzhiyun======================================================= 58*4882a593Smuzhiyun 59*4882a593Smuzhiyun1. Create a disk image 60*4882a593Smuzhiyun dd if=/dev/zero of=disk.img bs=1M count=32 61*4882a593Smuzhiyun2. Partition it with GPT partitions 62*4882a593Smuzhiyun cgdisk disk.img 63*4882a593Smuzhiyun - Create a first partition, type EF00, for the 64*4882a593Smuzhiyun bootloader and kernel image 65*4882a593Smuzhiyun - Create a second partition, type 8300, for the root 66*4882a593Smuzhiyun filesystem. 67*4882a593Smuzhiyun3. Setup loop device and loop partitions 68*4882a593Smuzhiyun sudo losetup -f disk.img 69*4882a593Smuzhiyun sudo partx -a /dev/loop0 70*4882a593Smuzhiyun4. Prepare the boot partition 71*4882a593Smuzhiyun sudo mkfs.vfat -n boot /dev/loop0p1 72*4882a593Smuzhiyun sudo mount /dev/loop0p1 /mnt 73*4882a593Smuzhiyun sudo cp -a output/images/efi-part/* /mnt/ 74*4882a593Smuzhiyun sudo cp output/images/bzImage /mnt/ 75*4882a593Smuzhiyun sudo umount /mnt 76*4882a593Smuzhiyun5. Prepare the root partition 77*4882a593Smuzhiyun sudo mkfs.ext3 -L root /dev/loop0p2 78*4882a593Smuzhiyun sudo mount /dev/loop0p2 /mnt 79*4882a593Smuzhiyun sudo tar -C /mnt -xf output/images/rootfs.tar 80*4882a593Smuzhiyun sudo umount /mnt 81*4882a593Smuzhiyun6 Cleanup loop device 82*4882a593Smuzhiyun sudo partx -d /dev/loop0 83*4882a593Smuzhiyun sudo losetup -d /dev/loop0 84*4882a593Smuzhiyun7. Your disk.img is ready! 85*4882a593Smuzhiyun 86*4882a593SmuzhiyunTo test your i386/x86-64 EFI image in Qemu 87*4882a593Smuzhiyun------------------------------------------ 88*4882a593Smuzhiyun 89*4882a593Smuzhiyun1. Download the EFI BIOS for Qemu 90*4882a593Smuzhiyun Version IA32 or X64 depending on the chosen Grub2 91*4882a593Smuzhiyun platform (i386-efi vs. x86-64-efi) 92*4882a593Smuzhiyun https://www.kraxel.org/repos/jenkins/edk2/ 93*4882a593Smuzhiyun (or use one provided by your distribution as OVMF) 94*4882a593Smuzhiyun2. Extract, and rename OVMF.fd to bios.bin and 95*4882a593Smuzhiyun CirrusLogic5446.rom to vgabios-cirrus.bin. 96*4882a593Smuzhiyun3. qemu-system-{i386,x86-64} -L ovmf-dir/ -hda disk.img 97*4882a593Smuzhiyun4. Make sure to pass pci=nocrs to the kernel command line, 98*4882a593Smuzhiyun to workaround a bug in the EFI BIOS regarding the 99*4882a593Smuzhiyun EFI framebuffer. 100*4882a593Smuzhiyun 101*4882a593SmuzhiyunNotes on using Grub2 for ARM u-boot-based platforms 102*4882a593Smuzhiyun=================================================== 103*4882a593Smuzhiyun 104*4882a593SmuzhiyunThe following steps show how to use the Grub2 arm-uboot platform 105*4882a593Smuzhiyunsupport in the simplest way possible and with a single 106*4882a593Smuzhiyunbuildroot-generated filesystem. 107*4882a593Smuzhiyun 108*4882a593Smuzhiyun 1. Load qemu_arm_vexpress_defconfig 109*4882a593Smuzhiyun 110*4882a593Smuzhiyun 2. Enable u-boot with the vexpress_ca9x4 board name and with 111*4882a593Smuzhiyun u-boot.elf image format. 112*4882a593Smuzhiyun 113*4882a593Smuzhiyun 3. Enable grub2 for the arm-uboot platform. 114*4882a593Smuzhiyun 115*4882a593Smuzhiyun 4. Enable "Install kernel image to /boot in target" in the kernel 116*4882a593Smuzhiyun menu to populate a /boot directory with zImage in it. 117*4882a593Smuzhiyun 118*4882a593Smuzhiyun 5. The upstream u-boot vexpress_ca9x4 doesn't have CONFIG_API enabled 119*4882a593Smuzhiyun by default, which is required. 120*4882a593Smuzhiyun 121*4882a593Smuzhiyun Before building, patch u-boot (for example, make u-boot-extract to 122*4882a593Smuzhiyun edit the source before building) file 123*4882a593Smuzhiyun include/configs/vexpress_common.h to define: 124*4882a593Smuzhiyun 125*4882a593Smuzhiyun #define CONFIG_API 126*4882a593Smuzhiyun #define CONFIG_SYS_MMC_MAX_DEVICE 1 127*4882a593Smuzhiyun 128*4882a593Smuzhiyun 6. Create a custom grub2 config file with the following contents and 129*4882a593Smuzhiyun set its path in BR2_TARGET_GRUB2_CFG: 130*4882a593Smuzhiyun 131*4882a593Smuzhiyun set default="0" 132*4882a593Smuzhiyun set timeout="5" 133*4882a593Smuzhiyun 134*4882a593Smuzhiyun menuentry "Buildroot" { 135*4882a593Smuzhiyun set root='(hd0)' 136*4882a593Smuzhiyun linux /boot/zImage root=/dev/mmcblk0 console=ttyAMA0 137*4882a593Smuzhiyun devicetree /boot/vexpress-v2p-ca9.dtb 138*4882a593Smuzhiyun } 139*4882a593Smuzhiyun 140*4882a593Smuzhiyun 7. Create a custom builtin config file with the following contents 141*4882a593Smuzhiyun and set its path in BR2_TARGET_GRUB2_BUILTIN_CONFIG: 142*4882a593Smuzhiyun 143*4882a593Smuzhiyun set root=(hd0) 144*4882a593Smuzhiyun set prefix=/boot/grub 145*4882a593Smuzhiyun 146*4882a593Smuzhiyun 8. Create a custom post-build script which copies files from 147*4882a593Smuzhiyun ${BINARIES_DIR}/boot-part to $(TARGET_DIR)/boot (set its path in 148*4882a593Smuzhiyun BR2_ROOTFS_POST_BUILD_SCRIPT): 149*4882a593Smuzhiyun 150*4882a593Smuzhiyun #!/bin/sh 151*4882a593Smuzhiyun cp -r ${BINARIES_DIR}/boot-part/* ${TARGET_DIR}/boot/ 152*4882a593Smuzhiyun 153*4882a593Smuzhiyun 9. make 154*4882a593Smuzhiyun 155*4882a593Smuzhiyun10. Run qemu with: 156*4882a593Smuzhiyun 157*4882a593Smuzhiyun qemu-system-arm -M vexpress-a9 -kernel output/images/u-boot -m 1024 \ 158*4882a593Smuzhiyun -nographic -sd output/images/rootfs.ext2 159*4882a593Smuzhiyun 160*4882a593Smuzhiyun11. In u-boot, stop at the prompt and run grub2 with: 161*4882a593Smuzhiyun 162*4882a593Smuzhiyun => ext2load mmc 0:0 ${loadaddr} /boot/grub/grub.img 163*4882a593Smuzhiyun => bootm 164*4882a593Smuzhiyun 165*4882a593Smuzhiyun12. This should bring the grub2 menu, upon which selecting the "Buildroot" 166*4882a593Smuzhiyun entry should boot Linux. 167*4882a593Smuzhiyun 168*4882a593Smuzhiyun 169*4882a593SmuzhiyunNotes on using Grub2 for Aarch64 EFI-based platforms 170*4882a593Smuzhiyun==================================================== 171*4882a593Smuzhiyun 172*4882a593SmuzhiyunThe following steps show how to use the Grub2 arm64-efi platform, 173*4882a593Smuzhiyunusing qemu and EFI firmware built for qemu. 174*4882a593Smuzhiyun 175*4882a593Smuzhiyun 1. Load aarch64_efi_defconfig 176*4882a593Smuzhiyun 177*4882a593Smuzhiyun 2. make 178*4882a593Smuzhiyun 179*4882a593Smuzhiyun 3. Download the EFI firmware for qemu aarch64 180*4882a593Smuzhiyun https://www.kraxel.org/repos/jenkins/edk2/ 181*4882a593Smuzhiyun (or use one provided by your distribution as OVMF-aarch64 or AAVMF) 182*4882a593Smuzhiyun 183*4882a593Smuzhiyun 4. Run qemu with: 184*4882a593Smuzhiyun 185*4882a593Smuzhiyun qemu-system-aarch64 -M virt -cpu cortex-a57 -m 512 -nographic \ 186*4882a593Smuzhiyun -bios <path/to/EDK2>/QEMU_EFI.fd -hda output/images/disk.img \ 187*4882a593Smuzhiyun -netdev user,id=eth0 -device virtio-net-device,netdev=eth0 188*4882a593Smuzhiyun 189*4882a593Smuzhiyun 5. This should bring the grub2 menu, upon which selecting the 190*4882a593Smuzhiyun "Buildroot" entry should boot Linux. 191