Skip to contentSkip to conversation

configs/pc: add a configuration to build a UEFI+GPT image

Buildroot Carlos Santos 2 participants 3 messages Aug 31, 2018 – Sep 6, 2018

lore
Share & tools
lore
#0 lore raw

This is ane example of how to craft a disk image with GPT partitioning instead of MBR. This is achieved by means of a post-image script which uses mkdosfs+mcopy+sfdisk, since genimage is unable to deal with GPT.

The script was kept as simple as possible to make it easy to understand and adapt for other purposes.

The root filesystem location is passed to the kernel by a partition UUID, so the it is possible to boot on QEMU, directly from the disk image, or dump the image to a physical device.

---
 board/pc/post-image-efi-gpt.sh      | 62 ++++++++++++++++++++++++++++
 board/pc/readme.txt                 |  6 ++-
 configs/pc_x86_64_efi_gpt_defconfig | 64 +++++++++++++++++++++++++++++
 3 files changed, 131 insertions(+), 1 deletion(-)
 create mode 100755 board/pc/post-image-efi-gpt.sh
 create mode 100644 configs/pc_x86_64_efi_gpt_defconfig
board/pc/post-image-efi-gpt.sh new file +62 -0
new file mode 100755
index 0000000000..d2acd8f852
@@ -0,0 +1,62 @@
+#!/bin/sh
+
+set -e
+
+cd ${BINARIES_DIR}
+
+# GPT partition type UUIDs
+esp_type=c12a7328-f81f-11d2-ba4b-00a0c93ec93b
+linux_type=44479540-f297-41b2-9af7-d131d5f0458a
+
+# Partition UUIDs
+efi_part_uuid=$(uuidgen)
+root_part_uuid=$(uuidgen)
+
+# Boot partition offset and size, in 512-byte sectors
+efi_part_start=64
+efi_part_size=32768
+
+# Rootfs partition offset and size, in 512-byte sectors
+root_part_start=$(( efi_part_start + efi_part_size ))
+root_part_size=$(( $(stat -c %s rootfs.ext2) / 512 ))
+
+first_lba=34
+last_lba=$(( root_part_start + root_part_size ))
+
+# Disk image size in 512-byte sectors
+image_size=$(( last_lba + first_lba ))
+
+cat > efi-part/EFI/BOOT/grub.cfg <<EOF
+set default="0"
+set timeout="5"
+
+menuentry "Buildroot" {
+	linux /bzImage root=PARTUUID=$root_part_uuid rootwait console=tty1
+}
+EOF
+
+# Create EFI system partition
+rm -f efi-part.vfat
+dd if=/dev/zero of=efi-part.vfat bs=512 count=0 seek=$efi_part_size
+mkdosfs  efi-part.vfat
+mcopy -bsp -i efi-part.vfat efi-part/startup.nsh ::startup.nsh
+mcopy -bsp -i efi-part.vfat efi-part/EFI ::EFI
+mcopy -bsp -i efi-part.vfat bzImage ::bzImage
+
+rm -f disk.img
+dd if=/dev/zero of=disk.img bs=512 count=0 seek=$image_size
+
+sfdisk disk.img <<EOF
+label: gpt
+label-id: $(uuidgen)
+device: /dev/foobar0
+unit: sectors
+first-lba: $first_lba
+last-lba: $last_lba
+
+/dev/foobar0p1 : start=$efi_part_start,  size=$efi_part_size,  type=$esp_type,   uuid=$efi_part_uuid,  name="efi-part.vfat"
+/dev/foobar0p2 : start=$root_part_start, size=$root_part_size, type=$linux_type, uuid=$root_part_uuid, name="rootfs.ext2"
+EOF
+
+dd if=efi-part.vfat of=disk.img bs=512 count=$efi_part_size seek=$efi_part_start conv=notrunc
+dd if=rootfs.ext2   of=disk.img bs=512 count=$root_part_size seek=$root_part_start conv=notrunc
board/pc/readme.txt +5 -1
index ca3b5123c1..73d8c24838 100644
@@ -9,10 +9,14 @@ Bare PC sample config
 
   $ make pc_x86_64_bios_defconfig
 
-  Or for EFI:
+  For EFI-based boot strategy on a MBR-partitioned disk:
 
   $ make pc_x86_64_efi_defconfig
 
+  Or for EFI-based boot strategy on a GPT-partitioned disk:
+
+  $ make pc_x86_64_efi_gpt_defconfig
+
   Add any additional packages required and build:
 
   $ make
configs/pc_x86_64_efi_gpt_defconfig new file +64 -0

64 lines not on this page · raw message

Signature · 1 line
2.17.1
#1 lore raw

Hello,

Quoted 4 lines · Carlos Santos

This is ane example of how to craft a disk image with GPT partitioning instead of MBR. This is achieved by means of a post-image script which uses mkdosfs+mcopy+sfdisk, since genimage is unable to deal with GPT.

Quoted 8 lines

The script was kept as simple as possible to make it easy to understand and adapt for other purposes.

The root filesystem location is passed to the kernel by a partition UUID, so the it is possible to boot on QEMU, directly from the disk image, or dump the image to a physical device.

Thanks for submitting. I don't find the post-image-efi-gpt.sh script really nice, but since genimage currently doesn't have GPT partition table support, that's pretty much all what can be done, and it's good to have an example in Buildroot.

Therefore, if there is no opposition against this patch within the next days, I will apply it.

Thanks!

Thomas

Signature · 3 lines
Thomas Petazzoni, CTO, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
#2 lore raw
In reply to Carlos Santos

Hello,

Quoted 4 lines · Carlos Santos

This is ane example of how to craft a disk image with GPT partitioning instead of MBR. This is achieved by means of a post-image script which uses mkdosfs+mcopy+sfdisk, since genimage is unable to deal with GPT.

Quoted 8 lines

The script was kept as simple as possible to make it easy to understand and adapt for other purposes.

The root filesystem location is passed to the kernel by a partition UUID, so the it is possible to boot on QEMU, directly from the disk image, or dump the image to a physical device.

Signature · 3 lines
Thomas Petazzoni, CTO, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com