1*83d290c5STom Rini# SPDX-License-Identifier: GPL-2.0+ 236f2e8e0SLukasz Majewski# 336f2e8e0SLukasz Majewski# Copyright (C) 2012 Samsung Electronics 436f2e8e0SLukasz Majewski# 536f2e8e0SLukasz Majewski# Lukasz Majewski <l.majewski@samsung.com> 636f2e8e0SLukasz Majewski 736f2e8e0SLukasz MajewskiGlossary: 836f2e8e0SLukasz Majewski======== 936f2e8e0SLukasz Majewski- UUID -(Universally Unique Identifier) 1036f2e8e0SLukasz Majewski- GUID - (Globally Unique ID) 1136f2e8e0SLukasz Majewski- EFI - (Extensible Firmware Interface) 1236f2e8e0SLukasz Majewski- UEFI - (Unified EFI) - EFI evolution 1336f2e8e0SLukasz Majewski- GPT (GUID Partition Table) - it is the EFI standard part 1436f2e8e0SLukasz Majewski- partitions - lists of available partitions (defined at u-boot): 1536f2e8e0SLukasz Majewski ./include/configs/{target}.h 1636f2e8e0SLukasz Majewski 1736f2e8e0SLukasz MajewskiIntroduction: 1836f2e8e0SLukasz Majewski============= 1936f2e8e0SLukasz MajewskiThis document describes the GPT partition table format and usage of 2036f2e8e0SLukasz Majewskithe gpt command in u-boot. 2136f2e8e0SLukasz Majewski 2236f2e8e0SLukasz MajewskiUUID introduction: 2336f2e8e0SLukasz Majewski==================== 2436f2e8e0SLukasz Majewski 2536f2e8e0SLukasz MajewskiGPT for marking disks/partitions is using the UUID. It is supposed to be a 2636f2e8e0SLukasz Majewskiglobally unique value. A UUID is a 16-byte (128-bit) number. The number of 2736f2e8e0SLukasz Majewskitheoretically possible UUIDs is therefore about 3 x 10^38. 2836f2e8e0SLukasz MajewskiMore often UUID is displayed as 32 hexadecimal digits, in 5 groups, 2936f2e8e0SLukasz Majewskiseparated by hyphens, in the form 8-4-4-4-12 for a total of 36 characters 3036f2e8e0SLukasz Majewski(32 digits and 4 hyphens) 3136f2e8e0SLukasz Majewski 32b38c108aSPatrick DelaunayFor instance, GUID of Basic data partition: EBD0A0A2-B9E5-4433-87C0-68B6B72699C7 33b38c108aSPatrick Delaunayand GUID of Linux filesystem data: 0FC63DAF-8483-4772-8E79-3D69D8477DE4 3436f2e8e0SLukasz Majewski 3536f2e8e0SLukasz MajewskiHistorically there are 5 methods to generate this number. The oldest one is 3636f2e8e0SLukasz Majewskicombining machine's MAC address and timer (epoch) value. 3736f2e8e0SLukasz Majewski 3836f2e8e0SLukasz MajewskiSuccessive versions are using MD5 hash, random numbers and SHA-1 hash. All major 3936f2e8e0SLukasz MajewskiOSes and programming languages are providing libraries to compute UUID (e.g. 4036f2e8e0SLukasz Majewskiuuid command line tool). 4136f2e8e0SLukasz Majewski 4236f2e8e0SLukasz MajewskiGPT brief explanation: 4336f2e8e0SLukasz Majewski====================== 4436f2e8e0SLukasz Majewski 4536f2e8e0SLukasz Majewski Layout: 4636f2e8e0SLukasz Majewski ------- 4736f2e8e0SLukasz Majewski 4836f2e8e0SLukasz Majewski -------------------------------------------------- 4936f2e8e0SLukasz Majewski LBA 0 |Protective MBR | 5036f2e8e0SLukasz Majewski ---------------------------------------------------------- 5136f2e8e0SLukasz Majewski LBA 1 |Primary GPT Header | Primary 5236f2e8e0SLukasz Majewski -------------------------------------------------- GPT 5336f2e8e0SLukasz Majewski LBA 2 |Entry 1|Entry 2| Entry 3| Entry 4| 5436f2e8e0SLukasz Majewski -------------------------------------------------- 5536f2e8e0SLukasz Majewski LBA 3 |Entries 5 - 128 | 5636f2e8e0SLukasz Majewski | | 5736f2e8e0SLukasz Majewski | | 5836f2e8e0SLukasz Majewski ---------------------------------------------------------- 5936f2e8e0SLukasz Majewski LBA 34 |Partition 1 | 6036f2e8e0SLukasz Majewski | | 6136f2e8e0SLukasz Majewski ----------------------------------- 6236f2e8e0SLukasz Majewski |Partition 2 | 6336f2e8e0SLukasz Majewski | | 6436f2e8e0SLukasz Majewski ----------------------------------- 6536f2e8e0SLukasz Majewski |Partition n | 6636f2e8e0SLukasz Majewski | | 6736f2e8e0SLukasz Majewski ---------------------------------------------------------- 68ae95fad5SSteve Rae LBA -34 |Entry 1|Entry 2| Entry 3| Entry 4| Backup 69ae95fad5SSteve Rae -------------------------------------------------- GPT 70ae95fad5SSteve Rae LBA -33 |Entries 5 - 128 | 7136f2e8e0SLukasz Majewski | | 7236f2e8e0SLukasz Majewski | | 7336f2e8e0SLukasz Majewski LBA -2 | | 7436f2e8e0SLukasz Majewski -------------------------------------------------- 75ae95fad5SSteve Rae LBA -1 |Backup GPT Header | 7636f2e8e0SLukasz Majewski ---------------------------------------------------------- 7736f2e8e0SLukasz Majewski 7836f2e8e0SLukasz MajewskiFor a legacy reasons, GPT's LBA 0 sector has a MBR structure. It is called 7936f2e8e0SLukasz Majewski"protective MBR". 8036f2e8e0SLukasz MajewskiIts first partition entry ID has 0xEE value, and disk software, which is not 8136f2e8e0SLukasz Majewskihandling the GPT sees it as a storage device without free space. 8236f2e8e0SLukasz Majewski 8336f2e8e0SLukasz MajewskiIt is possible to define 128 linearly placed partition entries. 8436f2e8e0SLukasz Majewski 8536f2e8e0SLukasz Majewski"LBA -1" means the last addressable block (in the mmc subsystem: 8636f2e8e0SLukasz Majewski"dev_desc->lba - 1") 8736f2e8e0SLukasz Majewski 88ae95fad5SSteve RaePrimary/Backup GPT header: 8936f2e8e0SLukasz Majewski---------------------------- 9036f2e8e0SLukasz MajewskiOffset Size Description 9136f2e8e0SLukasz Majewski 9236f2e8e0SLukasz Majewski0 8 B Signature ("EFI PART", 45 46 49 20 50 41 52 54) 9336f2e8e0SLukasz Majewski8 4 B Revision (For version 1.0, the value is 00 00 01 00) 9436f2e8e0SLukasz Majewski12 4 B Header size (in bytes, usually 5C 00 00 00 meaning 92 bytes) 9536f2e8e0SLukasz Majewski16 4 B CRC32 of header (0 to header size), with this field zeroed 9636f2e8e0SLukasz Majewski during calculation 9736f2e8e0SLukasz Majewski20 4 B Reserved (ZERO); 9836f2e8e0SLukasz Majewski24 8 B Current LBA (location of this header copy) 9936f2e8e0SLukasz Majewski32 8 B Backup LBA (location of the other header copy) 10036f2e8e0SLukasz Majewski40 8 B First usable LBA for partitions (primary partition table last 10136f2e8e0SLukasz Majewski LBA + 1) 10236f2e8e0SLukasz Majewski48 8 B Last usable LBA (secondary partition table first LBA - 1) 10336f2e8e0SLukasz Majewski56 16 B Disk GUID (also referred as UUID on UNIXes) 10436f2e8e0SLukasz Majewski72 8 B Partition entries starting LBA (always 2 in primary copy) 10536f2e8e0SLukasz Majewski80 4 B Number of partition entries 10636f2e8e0SLukasz Majewski84 4 B Size of a partition entry (usually 128) 10736f2e8e0SLukasz Majewski88 4 B CRC32 of partition array 10836f2e8e0SLukasz Majewski92 * Reserved; must be ZERO (420 bytes for a 512-byte LBA) 10936f2e8e0SLukasz Majewski 11036f2e8e0SLukasz MajewskiTOTAL: 512 B 11136f2e8e0SLukasz Majewski 11236f2e8e0SLukasz Majewski 11336f2e8e0SLukasz MajewskiIMPORTANT: 11436f2e8e0SLukasz Majewski 11536f2e8e0SLukasz MajewskiGPT headers and partition entries are protected by CRC32 (the POSIX CRC32). 11636f2e8e0SLukasz Majewski 117ae95fad5SSteve RaePrimary GPT header and Backup GPT header have swapped values of "Current LBA" 11836f2e8e0SLukasz Majewskiand "Backup LBA" and therefore different CRC32 check-sum. 11936f2e8e0SLukasz Majewski 12036f2e8e0SLukasz MajewskiCRC32 for GPT headers (field "CRC of header") are calculated up till 12136f2e8e0SLukasz Majewski"Header size" (92), NOT 512 bytes. 12236f2e8e0SLukasz Majewski 12336f2e8e0SLukasz MajewskiCRC32 for partition entries (field "CRC32 of partition array") is calculated for 12436f2e8e0SLukasz Majewskithe whole array entry ( Number_of_partition_entries * 12536f2e8e0SLukasz Majewskisizeof(partition_entry_size (usually 128))) 12636f2e8e0SLukasz Majewski 127ae95fad5SSteve RaeObserve, how Backup GPT is placed in the memory. It is NOT a mirror reflect 12836f2e8e0SLukasz Majewskiof the Primary. 12936f2e8e0SLukasz Majewski 13036f2e8e0SLukasz Majewski Partition Entry Format: 13136f2e8e0SLukasz Majewski ---------------------- 13236f2e8e0SLukasz Majewski Offset Size Description 13336f2e8e0SLukasz Majewski 13439206382SPrzemyslaw Marczak 0 16 B Partition type GUID (Big Endian) 13539206382SPrzemyslaw Marczak 16 16 B Unique partition GUID in (Big Endian) 13636f2e8e0SLukasz Majewski 32 8 B First LBA (Little Endian) 13736f2e8e0SLukasz Majewski 40 8 B Last LBA (inclusive) 13836f2e8e0SLukasz Majewski 48 8 B Attribute flags [+] 13936f2e8e0SLukasz Majewski 56 72 B Partition name (text) 14036f2e8e0SLukasz Majewski 14136f2e8e0SLukasz Majewski Attribute flags: 14236f2e8e0SLukasz Majewski Bit 0 - System partition 143cfdaf4caSPatrick Delaunay Bit 1 - Hide from EFI 144cfdaf4caSPatrick Delaunay Bit 2 - Legacy BIOS bootable 145cfdaf4caSPatrick Delaunay Bit 48-63 - Defined and used by the individual partition type 146cfdaf4caSPatrick Delaunay For Basic data partition : 14736f2e8e0SLukasz Majewski Bit 60 - Read-only 14836f2e8e0SLukasz Majewski Bit 62 - Hidden 14936f2e8e0SLukasz Majewski Bit 63 - Not mount 15036f2e8e0SLukasz Majewski 15136f2e8e0SLukasz MajewskiCreating GPT partitions in U-Boot: 15236f2e8e0SLukasz Majewski============== 15336f2e8e0SLukasz Majewski 15436f2e8e0SLukasz MajewskiTo restore GUID partition table one needs to: 15536f2e8e0SLukasz Majewski1. Define partition layout in the environment. 15636f2e8e0SLukasz Majewski Format of partitions layout: 1570a242386SAlison Chaiken "uuid_disk=...;name=u-boot,size=60MiB,uuid=...; 15836f2e8e0SLukasz Majewski name=kernel,size=60MiB,uuid=...;" 15936f2e8e0SLukasz Majewski or 1600a242386SAlison Chaiken "uuid_disk=${uuid_gpt_disk};name=${uboot_name}, 16136f2e8e0SLukasz Majewski size=${uboot_size},uuid=${uboot_uuid};" 16236f2e8e0SLukasz Majewski 1630c7e8d13SRob Herring The fields 'name' and 'size' are mandatory for every partition. 16436f2e8e0SLukasz Majewski The field 'start' is optional. 16536f2e8e0SLukasz Majewski 16662a3b7ddSRobert P. J. Day If field 'size' of the last partition is 0, the partition is extended 167cfdaf4caSPatrick Delaunay up to the end of the device. 168cfdaf4caSPatrick Delaunay 1690c7e8d13SRob Herring The fields 'uuid' and 'uuid_disk' are optional if CONFIG_RANDOM_UUID is 1700c7e8d13SRob Herring enabled. A random uuid will be used if omitted or they point to an empty/ 1710c7e8d13SRob Herring non-existent environment variable. The environment variable will be set to 17273d6d18bSAlison Chaiken the generated UUID. The 'gpt guid' command reads the current value of the 17373d6d18bSAlison Chaiken uuid_disk from the GPT. 17439206382SPrzemyslaw Marczak 175cfdaf4caSPatrick Delaunay The field 'bootable' is optional, it is used to mark the GPT partition 176cfdaf4caSPatrick Delaunay bootable (set attribute flags "Legacy BIOS bootable"). 177cfdaf4caSPatrick Delaunay "name=u-boot,size=60MiB;name=boot,size=60Mib,bootable;name=rootfs,size=0" 178cfdaf4caSPatrick Delaunay It can be used to locate bootable disks with command 179cfdaf4caSPatrick Delaunay "part list <interface> <dev> -bootable <varname>", 180cfdaf4caSPatrick Delaunay please check out doc/README.distro for use. 181cfdaf4caSPatrick Delaunay 18236f2e8e0SLukasz Majewski2. Define 'CONFIG_EFI_PARTITION' and 'CONFIG_CMD_GPT' 18336f2e8e0SLukasz Majewski 184dd445879SLukasz Majewski3. From u-boot prompt type: 18536f2e8e0SLukasz Majewski gpt write mmc 0 $partitions 18636f2e8e0SLukasz Majewski 187dd445879SLukasz MajewskiChecking (validating) GPT partitions in U-Boot: 188dd445879SLukasz Majewski=============================================== 189dd445879SLukasz Majewski 190dd445879SLukasz MajewskiProcedure is the same as above. The only change is at point 3. 191dd445879SLukasz Majewski 192dd445879SLukasz MajewskiAt u-boot prompt one needs to write: 193dd445879SLukasz Majewski gpt verify mmc 0 [$partitions] 194dd445879SLukasz Majewski 195dd445879SLukasz Majewskiwhere [$partitions] is an optional parameter. 196dd445879SLukasz Majewski 197dd445879SLukasz MajewskiWhen it is not provided, only basic checks based on CRC32 calculation for GPT 198dd445879SLukasz Majewskiheader and PTEs are performed. 199dd445879SLukasz MajewskiWhen provided, additionally partition data - name, size and starting 200dd445879SLukasz Majewskioffset (last two in LBA) - are compared with data defined in '$partitions' 201dd445879SLukasz Majewskienvironment variable. 202dd445879SLukasz Majewski 203dd445879SLukasz MajewskiAfter running this command, return code is set to 0 if no errors found in 204dd445879SLukasz Majewskion non-volatile medium stored GPT. 205dd445879SLukasz Majewski 206dd445879SLukasz MajewskiFollowing line can be used to assess if GPT verification has succeed: 207dd445879SLukasz Majewski 208dd445879SLukasz MajewskiU-BOOT> gpt verify mmc 0 $partitions 209dd445879SLukasz MajewskiU-BOOT> if test $? = 0; then echo "GPT OK"; else echo "GPT ERR"; fi 210dd445879SLukasz Majewski 211203f9b48SAlison ChaikenRenaming GPT partitions from U-Boot: 212203f9b48SAlison Chaiken==================================== 213203f9b48SAlison Chaiken 214203f9b48SAlison ChaikenGPT partition names are a mechanism via which userspace and U-Boot can 215203f9b48SAlison Chaikencommunicate about software updates and boot failure. The 'gpt guid', 216203f9b48SAlison Chaiken'gpt read', 'gpt rename' and 'gpt swap' commands facilitate 217203f9b48SAlison Chaikenprogrammatic renaming of partitions from bootscripts by generating and 218203f9b48SAlison Chaikenmodifying the partitions layout string. Here is an illustration of 219203f9b48SAlison Chaikenemploying 'swap' to exchange 'primary' and 'backup' partition names: 220203f9b48SAlison Chaiken 221203f9b48SAlison ChaikenU-BOOT> gpt swap mmc 0 primary backup 222203f9b48SAlison Chaiken 223203f9b48SAlison ChaikenAfterwards, all partitions previously named 'primary' will be named 224203f9b48SAlison Chaiken'backup', and vice-versa. Alternatively, single partitions may be 225203f9b48SAlison Chaikenrenamed. In this example, mmc0's first partition will be renamed 226203f9b48SAlison Chaiken'primary': 227203f9b48SAlison Chaiken 228203f9b48SAlison ChaikenU-BOOT> gpt rename mmc 0 1 primary 229dd445879SLukasz Majewski 230564cf25dSAlison ChaikenThe GPT functionality may be tested with the 'sandbox' board by 231564cf25dSAlison Chaikencreating a disk image as described under 'Block Device Emulation' in 232564cf25dSAlison Chaikenboard/sandbox/README.sandbox: 233564cf25dSAlison Chaiken 234564cf25dSAlison Chaiken=>host bind 0 ./disk.raw 235564cf25dSAlison Chaiken=> gpt read host 0 236564cf25dSAlison Chaiken[ . . . ] 237203f9b48SAlison Chaiken=> gpt swap host 0 name othername 23852791db7SAlison Chaiken[ . . . ] 23952791db7SAlison Chaiken 2407561b258SPatrick DelaunayPartition type GUID: 2417561b258SPatrick Delaunay==================== 2427561b258SPatrick Delaunay 2437561b258SPatrick DelaunayFor created partition, the used partition type GUID is 2447561b258SPatrick DelaunayPARTITION_BASIC_DATA_GUID (EBD0A0A2-B9E5-4433-87C0-68B6B72699C7). 2457561b258SPatrick Delaunay 2467561b258SPatrick DelaunayIf you define 'CONFIG_PARTITION_TYPE_GUID', a optionnal parameter 'type' 2477561b258SPatrick Delaunaycan specify a other partition type guid: 2487561b258SPatrick Delaunay 2490a242386SAlison Chaiken "uuid_disk=...;name=u-boot,size=60MiB,uuid=...; 2507561b258SPatrick Delaunay name=kernel,size=60MiB,uuid=..., 2517561b258SPatrick Delaunay type=0FC63DAF-8483-4772-8E79-3D69D8477DE4;" 2527561b258SPatrick Delaunay 253bcb41dcaSPatrick DelaunaySome strings can be also used at the place of known GUID : 254bcb41dcaSPatrick Delaunay "system" = PARTITION_SYSTEM_GUID 255bcb41dcaSPatrick Delaunay (C12A7328-F81F-11D2-BA4B-00A0C93EC93B) 256bcb41dcaSPatrick Delaunay "mbr" = LEGACY_MBR_PARTITION_GUID 257bcb41dcaSPatrick Delaunay (024DEE41-33E7-11D3-9D69-0008C781F39F) 258bcb41dcaSPatrick Delaunay "msft" = PARTITION_MSFT_RESERVED_GUID 259bcb41dcaSPatrick Delaunay (E3C9E316-0B5C-4DB8-817D-F92DF00215AE) 260bcb41dcaSPatrick Delaunay "data" = PARTITION_BASIC_DATA_GUID 261bcb41dcaSPatrick Delaunay (EBD0A0A2-B9E5-4433-87C0-68B6B72699C7) 262bcb41dcaSPatrick Delaunay "linux" = PARTITION_LINUX_FILE_SYSTEM_DATA_GUID 263bcb41dcaSPatrick Delaunay (0FC63DAF-8483-4772-8E79-3D69D8477DE4) 264bcb41dcaSPatrick Delaunay "raid" = PARTITION_LINUX_RAID_GUID 265bcb41dcaSPatrick Delaunay (A19D880F-05FC-4D3B-A006-743F0F84911E) 266bcb41dcaSPatrick Delaunay "swap" = PARTITION_LINUX_SWAP_GUID 267bcb41dcaSPatrick Delaunay (0657FD6D-A4AB-43C4-84E5-0933C84B4F4F) 268bcb41dcaSPatrick Delaunay "lvm" = PARTITION_LINUX_LVM_GUID 269bcb41dcaSPatrick Delaunay (E6D6D379-F507-44C2-A23C-238F2A3DF928) 270bcb41dcaSPatrick Delaunay 2710a242386SAlison Chaiken "uuid_disk=...;name=u-boot,size=60MiB,uuid=...; 272bcb41dcaSPatrick Delaunay name=kernel,size=60MiB,uuid=...,type=linux;" 273bcb41dcaSPatrick Delaunay 274bcb41dcaSPatrick DelaunayThey are also used to display the type of partition in "part list" command. 275bcb41dcaSPatrick Delaunay 276bcb41dcaSPatrick Delaunay 27736f2e8e0SLukasz MajewskiUseful info: 27836f2e8e0SLukasz Majewski============ 27936f2e8e0SLukasz Majewski 28039206382SPrzemyslaw MarczakTwo programs, namely: 'gdisk' and 'parted' are recommended to work with GPT 28139206382SPrzemyslaw Marczakrecovery. Both are able to handle GUID partitions. 28236f2e8e0SLukasz MajewskiPlease, pay attention at -l switch for parted. 28336f2e8e0SLukasz Majewski 28436f2e8e0SLukasz Majewski"uuid" program is recommended to generate UUID string. Moreover it can decode 28536f2e8e0SLukasz Majewski(-d switch) passed in UUID string. It can be used to generate partitions UUID 28636f2e8e0SLukasz Majewskipassed to u-boot environment variables. 28739206382SPrzemyslaw MarczakIf optional CONFIG_RANDOM_UUID is defined then for any partition which environment 28839206382SPrzemyslaw Marczakuuid is unset, uuid is randomly generated and stored in correspond environment 28939206382SPrzemyslaw Marczakvariable. 29039206382SPrzemyslaw Marczak 29139206382SPrzemyslaw Marczaknote: 29239206382SPrzemyslaw MarczakEach string block of UUID generated by program "uuid" is in big endian and it is 29339206382SPrzemyslaw Marczakalso stored in big endian in disk GPT. 29439206382SPrzemyslaw MarczakPartitions layout can be printed by typing "mmc part". Note that each partition 29539206382SPrzemyslaw MarczakGUID has different byte order than UUID generated before, this is because first 29639206382SPrzemyslaw Marczakthree blocks of GUID string are in Little Endian. 297