partition_scripts/test-partitioning_sgdisk.sh

194 lines
9.8 KiB
Bash
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

set -ex
# https://opensource.rock-chips.com/wiki_Partitions#GPT_partition_table_in_U-boot
# Default storage map
# ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# Partition Start Sector Number of Sectors Partition Size PartNum in GPT Requirements``
# ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# MBR 0 00000000 1 00000001 512 0.5KB
# Primary GPT 1 00000001 63 0000003F 32256 31.5KB
# loader1 64 00000040 7104 00001bc0 4096000 2.5MB 1 preloader (miniloader or U-Boot SPL)
# Vendor Storage 7168 00001c00 512 00000200 262144 256KB SN, MAC and etc.
# Reserved Space 7680 00001e00 384 00000180 196608 192KB Not used
# reserved1 8064 00001f80 128 00000080 65536 64KB legacy DRM key
# U-Boot ENV 8128 00001fc0 64 00000040 32768 32KB
# reserved2 8192 00002000 8192 00002000 4194304 4MB legacy parameter
# loader2 16384 00004000 8192 00002000 4194304 4MB 2 U-Boot or UEFI
# trust 24576 00006000 8192 00002000 4194304 4MB 3 trusted-os like ATF, OP-TEE
# bootbootable must be set 32768 00008000 229376 00038000 117440512 112MB 4 kernel, dtb, extlinux.conf, ramdisk
# rootfs 262144 00040000 - - - -MB 5 Linux system
# Secondary GPT 16777183 00FFFFDF 33 00000021 16896 16.5KB
# -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# loader1 64 00000040 7104 00001bc0 4096000 2.5MB 1 preloader (miniloader or U-Boot SPL)
# reserved1 8064 00001f80 128 00000080 65536 64KB legacy DRM key
# reserved2 8192 00002000 8192 00002000 4194304 4MB legacy parameter
# loader2 16384 00004000 8192 00002000 4194304 4MB 2 U-Boot or UEFI
# trust 24576 00006000 8192 00002000 4194304 4MB 3 trusted-os like ATF, OP-TEE
# bootbootable must be set 32768 00008000 229376 00038000 117440512 112MB 4 kernel, dtb, extlinux.conf, ramdisk
# rootfs 262144 00040000 - - - -MB 5 Linux system
# -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#
# From u-boot
# "uuid_disk=${uuid_gpt_disk};" \
# "name=loader1,start=32K,size=4000K,uuid=${uuid_gpt_loader1};" \
# "name=reserved1,size=64K,uuid=${uuid_gpt_reserved1};" \
# "name=reserved2,size=4M,uuid=${uuid_gpt_reserved2};" \
# "name=loader2,size=4MB,uuid=${uuid_gpt_loader2};" \
# "name=atf,size=4M,uuid=${uuid_gpt_atf};" \
# "name=boot,size=128M,bootable,uuid=${uuid_gpt_boot};" \
# "name=rootfs,size=-,uuid=${uuid_gpt_rootfs};\0" \
#
#u-boot/out_mmc/built_in_env.txt:
#partitions=uuid_disk=${uuid_gpt_disk};
# name=loader1,start=32K,size=4000K,uuid=${uuid_gpt_loader1};
# name=loader2,start=8MB,size=4MB,uuid=${uuid_gpt_loader2};
# name=trust,size=4M,uuid=${uuid_gpt_atf};
# name=boot,size=112M,bootable,uuid=${uuid_gpt_boot};
# name=rootfs,size=-,uuid=B921B045-1DF0-41C3-AF44-4C6F280D3FAE;
DISK_NAME=${PWD}/decode-sgdisk.img
DISK_NAME=/dev/sdb
# cleanup
#touch $DISK_NAME
#rm $DISK_NAME
# Partition sizes in MiB
IDBLOADER_SIZE=4
UBOOT_SIZE=4
TRUST_SIZE=4
BOOT_SIZE=112
ROOT_SIZE=128
# Define max sizes and offsets
IDBLOADER_START=64 # was 64 but report it is not aligned, 2048, which is 1MB alignment.
#IDBLOADER_END=7168
#IDBLOADER_OFFSET=$((${IDBLOADER_SIZE}*1024*1024/512)) # 8192 4MiB 8192
IDBLOADER_OFFSET=7104 # 7104 2.5MiB 7104
UBOOT_START=16384
UBOOT_OFFSET=$((${UBOOT_SIZE}*1024*1024/512)) # 16384 8MiB 16384
TRUST_OFFSET=$((${TRUST_SIZE}*1024*1024/512)) # 24575 12MiB 24575
BOOT_OFFSET=$((${BOOT_SIZE}*1024*1024/512)) # 32768 16MiB 32768
ROOT_OFFSET=$((${ROOT_SIZE}*1024*1024/512)) # 524288 128MiB 262144
MIN_SIZE=$((500*1000*1000/512)) # 976562 500MiB
SIZE_STEP=$((100*1000*1000/512)) # 195312 100MiB
MAX_SIZE=$((8*1000*1000*1000/512)) # 15625000 8GiB
# https://github.com/uapi-group/specifications/blob/main/specs/discoverable_partitions_specification.md
SD_GPT_ROOT_ARM64="b921b045-1df0-41c3-af44-4c6f280d3fae"
SD_GPT_KERNEL_ARM64="b921b045-1df0-41c3-af44-4c6f280d3faf"
SD_GPT_USR_ARM64="b0e01050-ee5f-4390-949a-9101b17104e9"
SD_GPT_KERNEL="933ac7e1-2eb4-4f13-b844-0e14e2aef917"
SD_GPT_ROOT="933ac7e1-2eb4-4f13-b844-0e14e2aef916"
SD_GPT_BOOT="3b8f8425-20e0-4f3b-907f-1a25a76f98ea"
SD_GPT_ATF="3b8f8425-20e0-4f3b-907f-1a25a76f98eb"
SD_GPT_LOADER="3b8f8425-20e0-4f3b-907f-1a25a76f98ee"
SD_GPT_RESERVED="3b8f8425-20e0-4f3b-907f-1a25a76f98ec"
SD_GPT_HOME="933ac7e1-2eb4-4f13-b844-0e14e2aef915A" # 8302
SD_GPT_SRV="3b8f8425-20e0-4f3b-907f-1a25a76f98e8" # 8306
SD_GPT_VAR="4d21b016-b534-45c2-a9fb-5c16e091fd2d" # 8310
SD_GPT_VARTMP="7ec6f557-3bc5-4aca-b293-16ef5df639d1" # 8311
SD_GPT_TMP="7ec6f557-3bc5-4aca-b293-16ef5df639d1"
SD_GPT_SWAP="0657fd6d-a4ab-43c4-84e5-0933c84b4f4f" # 8200
SD_GPT_DATA="3b8f8425-20e0-4f3b-907f-1a25a76f98e9"
SD_GPT_SYSTEM="3b8f8425-20e0-4f3b-907f-1a25a76f98ed"
SD_GPT_ESP="c12a7328-f81f-11d2-ba4b-00a0c93ec93b" # ef00
SD_GPT_UBOOT="3de21764-95bd-54bd-a5c3-4abe786f38a8"
SD_GPT_LINUX="0fc63daf-8483-4772-8e79-3d69d8477de4" # 8300
SD_GPT_ZFS="516E7CBA-6ECF-11D6-8FF8-00022D09712B"
# Extended boot loader, automatically mounted to /boot, second loctation for menu entries, kernel and intramfs separate from ESP/EFI
SD_GPT_XBOOTLDR="bc13c2ff-59e6-4262-a352-b275fd6f7172"
# Partition type Mountpoint gdisk's Partition type GUID
# code
# BIOS boot partition None ef02 21686148-6449-6e6f-744e-656564454649
# Linux x86-64 root (/) / 8304 4f68bce3-e8cd-4db1-96e7-fbcaf984b709
# Linux LVM Any 8e00 e6d6d379-f507-44c2-a23c-238f2a3df928
# Linux RAID Any fd00 a19d880f-05fc-4d3b-a006-743f0f84911e
# Linux LUKS Any 8309 ca7d7ccb-63ed-4c53-861c-1742536059cc
# Linux dm-crypt Any 8308 7ffec5c9-2d00-49b7-8941-3ea10a5586b7
# Partition Attribute Flags
# sgdisk --attribute="${PARTNUM}:=:"${SD_GPT_FLAG_READ_ONLY}" ${TARGET}
SD_GPT_FLAG_READ_ONLY="0x1000000000000000"
SD_GPT_FLAG_NO_AUTO="0x8000000000000000"
SD_GPT_FLAG_GROWFS="0x2000000000000000"
SD_GPT_FLAG_KERNEL="0x015A000000000000"
# set chip specific UUIDS
SD_GPT_ROOT=SD_GPT_ROOT_ARM64
SD_GPT_KERNEL=SD_GPT_KERNEL_ARM64
SD_GPT_USR=SD_GPT_USR_ARM64
# Create
rm -f "$DISK_NAME"
truncate --size "$((MAX_SIZE*512))" "$DISK_NAME"
#bootfs="fat32"
bootfs="ext2"
rootfs="ext4"
alignment_type="optimal" # none, cylinder, minimal, optimal
# Create partitions
echo "Updating GPT..."
TARGET="${DISK_NAME}"
/sbin/sgdisk --zap-all "${TARGET}"
## Partition 1 - Create partition using start and end values for partitioning the `idbloader` partition.
echo "**** SD_GPT_LOADER=${SD_GPT_LOADER}"
#STARTSECTOR=`/sbin/sgdisk --first-aligned-in-largest "${TARGET}"|sed -sn 2p`
STARTSECTOR="${IDBLOADER_START}" # 64 set above
PARTNUM=1
PARTLABEL="loader1"
#/sbin/sgdisk --print --set-alignment=${IDBLOADER_START} --mbrtogpt --new="${PARTNUM}":"${IDBLOADER_START}":+"${IDBLOADER_SIZE}"MB "${TARGET}"
#/sbin/sgdisk --print --set-alignment=1 --mbrtogpt --new="${PARTNUM}":"${IDBLOADER_START}":+"${IDBLOADER_SIZE}"MB "${TARGET}"
#/sbin/sgdisk --print --set-alignment=1 --mbrtogpt --new="${PARTNUM}":"${IDBLOADER_START}":8063 "${TARGET}"
/sbin/sgdisk --print --set-alignment=1 --mbrtogpt --new="${PARTNUM}":"${IDBLOADER_START}":+"${IDBLOADER_OFFSET}" "${TARGET}"
/sbin/sgdisk --print --change-name="${PARTNUM}":"${PARTLABEL}" --typecode="${PARTNUM}":"${SD_GPT_LOADER}" "${TARGET}"
## Partition 2 - Create partition using start and end values for partitioning the `uboot` partition to leave space for reserved1 and reserved2
echo "**** SD_GPT_UBOOT=${SD_GPT_UBOOT}"
PARTNUM=2
PARTLABEL="loader2"
#ENDSECTOR=$(/sbin/sgdisk --last-on-largest "${TARGET}"|sed -sn 2p)
#/sbin/sgdisk --print --new="${PARTNUM}":"${UBOOT_START}":-"${UBOOT_SIZE}"MB:"${ENDSECTOR}" "${TARGET}"
/sbin/sgdisk --print --new="${PARTNUM}":${UBOOT_START}:+"${UBOOT_SIZE}"MB "${TARGET}"
/sbin/sgdisk --print --change-name="${PARTNUM}":"${PARTLABEL}" --typecode=${PARTNUM}:"${SD_GPT_UBOOT}" "${TARGET}"
## Partition 5 - the 12MB partition for trust partition
echo "**** SD_GPT_ATF=${SD_GPT_ATF}"
PARTNUM=3
PARTLABEL="trust"
#ENDSECTOR=$(/sbin/sgdisk --last-on-largest "${TARGET}"|sed -sn 2p)
#/sbin/sgdisk --print --new="${PARTNUM}":-"${TRUST_SIZE}"MB:"${ENDSECTOR}" "${TARGET}"
/sbin/sgdisk --print --new="${PARTNUM}"::+"${TRUST_SIZE}"MB "${TARGET}"
/sbin/sgdisk --print --change-name="${PARTNUM}":"${PARTLABEL}" --typecode="${PARTNUM}":"${SD_GPT_ATF}" "${TARGET}"
## Partition 1 - the 16MB partition for bootfs
echo "**** SD_GPT_BOOT=${SD_GPT_BOOT}"
PARTNUM=4
PARTLABEL="boot"
#ENDSECTOR=$(/sbin/sgdisk --last-on-largest "${TARGET}"|sed -sn 2p)
#/sbin/sgdisk --print --new="${PARTNUM}":-"${BOOT_SIZE}"MB:${ENDSECTOR} "${TARGET}"
/sbin/sgdisk --print --new="${PARTNUM}"::+"${BOOT_SIZE}"MB "${TARGET}"
/sbin/sgdisk --print --change-name="${PARTNUM}":"${PARTLABEL}" --typecode="${PARTNUM}":"${SD_GPT_BOOT}" "${TARGET}"
## Partition 2 - the main partition for root
echo "**** SD_GPT_BOOT_ARM64=${SD_GPT_ROOT}"
PARTNUM=5
PARTLABEL="rootfs"
/sbin/sgdisk --print --largest-new="${PARTNUM}" "${TARGET}"
/sbin/sgdisk --print --change-name="${PARTNUM}":${PARTLABEL} --typecode="${PARTNUM}":${SD_GPT_ROOT_ARM64} "${TARGET}"
/usr/bin/cgpt show "${TARGET}"
/sbin/sgdisk --move-second-header "${TARGET}"
partprobe "${TARGET}"