post-image-efi-gpt.sh 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #!/bin/sh
  2. set -e
  3. cd ${BINARIES_DIR}
  4. # GPT partition type UUIDs
  5. esp_type=c12a7328-f81f-11d2-ba4b-00a0c93ec93b
  6. linux_type=44479540-f297-41b2-9af7-d131d5f0458a
  7. # Partition UUIDs
  8. efi_part_uuid=$(uuidgen)
  9. root_part_uuid=$(uuidgen)
  10. # Boot partition offset and size, in 512-byte sectors
  11. efi_part_start=64
  12. efi_part_size=32768
  13. # Rootfs partition offset and size, in 512-byte sectors
  14. root_part_start=$(( efi_part_start + efi_part_size ))
  15. root_part_size=$(( $(stat -c %s rootfs.ext2) / 512 ))
  16. first_lba=34
  17. last_lba=$(( root_part_start + root_part_size ))
  18. # Disk image size in 512-byte sectors
  19. image_size=$(( last_lba + first_lba ))
  20. cat > efi-part/EFI/BOOT/grub.cfg <<EOF
  21. set default="0"
  22. set timeout="5"
  23. menuentry "Buildroot" {
  24. linux /bzImage root=PARTUUID=$root_part_uuid rootwait console=tty1
  25. }
  26. EOF
  27. # Create EFI system partition
  28. rm -f efi-part.vfat
  29. dd if=/dev/zero of=efi-part.vfat bs=512 count=0 seek=$efi_part_size
  30. mkdosfs efi-part.vfat
  31. mcopy -bsp -i efi-part.vfat efi-part/startup.nsh ::startup.nsh
  32. mcopy -bsp -i efi-part.vfat efi-part/EFI ::EFI
  33. mcopy -bsp -i efi-part.vfat bzImage ::bzImage
  34. rm -f disk.img
  35. dd if=/dev/zero of=disk.img bs=512 count=0 seek=$image_size
  36. sfdisk disk.img <<EOF
  37. label: gpt
  38. label-id: $(uuidgen)
  39. device: /dev/foobar0
  40. unit: sectors
  41. first-lba: $first_lba
  42. last-lba: $last_lba
  43. /dev/foobar0p1 : start=$efi_part_start, size=$efi_part_size, type=$esp_type, uuid=$efi_part_uuid, name="efi-part.vfat"
  44. /dev/foobar0p2 : start=$root_part_start, size=$root_part_size, type=$linux_type, uuid=$root_part_uuid, name="rootfs.ext2"
  45. EOF
  46. dd if=efi-part.vfat of=disk.img bs=512 count=$efi_part_size seek=$efi_part_start conv=notrunc
  47. dd if=rootfs.ext2 of=disk.img bs=512 count=$root_part_size seek=$root_part_start conv=notrunc