1*4882a593Smuzhiyun# 2*4882a593Smuzhiyun# Copyright (C) 2012 Samsung Electronics 3*4882a593Smuzhiyun# 4*4882a593Smuzhiyun# Lukasz Majewski <l.majewski@samsung.com> 5*4882a593Smuzhiyun# 6*4882a593Smuzhiyun# 7*4882a593Smuzhiyun# SPDX-License-Identifier: GPL-2.0+ 8*4882a593Smuzhiyun 9*4882a593SmuzhiyunGlossary: 10*4882a593Smuzhiyun======== 11*4882a593Smuzhiyun- UUID -(Universally Unique Identifier) 12*4882a593Smuzhiyun- GUID - (Globally Unique ID) 13*4882a593Smuzhiyun- EFI - (Extensible Firmware Interface) 14*4882a593Smuzhiyun- UEFI - (Unified EFI) - EFI evolution 15*4882a593Smuzhiyun- GPT (GUID Partition Table) - it is the EFI standard part 16*4882a593Smuzhiyun- partitions - lists of available partitions (defined at u-boot): 17*4882a593Smuzhiyun ./include/configs/{target}.h 18*4882a593Smuzhiyun 19*4882a593SmuzhiyunIntroduction: 20*4882a593Smuzhiyun============= 21*4882a593SmuzhiyunThis document describes the GPT partition table format and usage of 22*4882a593Smuzhiyunthe gpt command in u-boot. 23*4882a593Smuzhiyun 24*4882a593SmuzhiyunUUID introduction: 25*4882a593Smuzhiyun==================== 26*4882a593Smuzhiyun 27*4882a593SmuzhiyunGPT for marking disks/partitions is using the UUID. It is supposed to be a 28*4882a593Smuzhiyunglobally unique value. A UUID is a 16-byte (128-bit) number. The number of 29*4882a593Smuzhiyuntheoretically possible UUIDs is therefore about 3 x 10^38. 30*4882a593SmuzhiyunMore often UUID is displayed as 32 hexadecimal digits, in 5 groups, 31*4882a593Smuzhiyunseparated by hyphens, in the form 8-4-4-4-12 for a total of 36 characters 32*4882a593Smuzhiyun(32 digits and 4 hyphens) 33*4882a593Smuzhiyun 34*4882a593SmuzhiyunFor instance, GUID of Basic data partition: EBD0A0A2-B9E5-4433-87C0-68B6B72699C7 35*4882a593Smuzhiyunand GUID of Linux filesystem data: 0FC63DAF-8483-4772-8E79-3D69D8477DE4 36*4882a593Smuzhiyun 37*4882a593SmuzhiyunHistorically there are 5 methods to generate this number. The oldest one is 38*4882a593Smuzhiyuncombining machine's MAC address and timer (epoch) value. 39*4882a593Smuzhiyun 40*4882a593SmuzhiyunSuccessive versions are using MD5 hash, random numbers and SHA-1 hash. All major 41*4882a593SmuzhiyunOSes and programming languages are providing libraries to compute UUID (e.g. 42*4882a593Smuzhiyunuuid command line tool). 43*4882a593Smuzhiyun 44*4882a593SmuzhiyunGPT brief explanation: 45*4882a593Smuzhiyun====================== 46*4882a593Smuzhiyun 47*4882a593Smuzhiyun Layout: 48*4882a593Smuzhiyun ------- 49*4882a593Smuzhiyun 50*4882a593Smuzhiyun -------------------------------------------------- 51*4882a593Smuzhiyun LBA 0 |Protective MBR | 52*4882a593Smuzhiyun ---------------------------------------------------------- 53*4882a593Smuzhiyun LBA 1 |Primary GPT Header | Primary 54*4882a593Smuzhiyun -------------------------------------------------- GPT 55*4882a593Smuzhiyun LBA 2 |Entry 1|Entry 2| Entry 3| Entry 4| 56*4882a593Smuzhiyun -------------------------------------------------- 57*4882a593Smuzhiyun LBA 3 |Entries 5 - 128 | 58*4882a593Smuzhiyun | | 59*4882a593Smuzhiyun | | 60*4882a593Smuzhiyun ---------------------------------------------------------- 61*4882a593Smuzhiyun LBA 34 |Partition 1 | 62*4882a593Smuzhiyun | | 63*4882a593Smuzhiyun ----------------------------------- 64*4882a593Smuzhiyun |Partition 2 | 65*4882a593Smuzhiyun | | 66*4882a593Smuzhiyun ----------------------------------- 67*4882a593Smuzhiyun |Partition n | 68*4882a593Smuzhiyun | | 69*4882a593Smuzhiyun ---------------------------------------------------------- 70*4882a593Smuzhiyun LBA -34 |Entry 1|Entry 2| Entry 3| Entry 4| Backup 71*4882a593Smuzhiyun -------------------------------------------------- GPT 72*4882a593Smuzhiyun LBA -33 |Entries 5 - 128 | 73*4882a593Smuzhiyun | | 74*4882a593Smuzhiyun | | 75*4882a593Smuzhiyun LBA -2 | | 76*4882a593Smuzhiyun -------------------------------------------------- 77*4882a593Smuzhiyun LBA -1 |Backup GPT Header | 78*4882a593Smuzhiyun ---------------------------------------------------------- 79*4882a593Smuzhiyun 80*4882a593SmuzhiyunFor a legacy reasons, GPT's LBA 0 sector has a MBR structure. It is called 81*4882a593Smuzhiyun"protective MBR". 82*4882a593SmuzhiyunIts first partition entry ID has 0xEE value, and disk software, which is not 83*4882a593Smuzhiyunhandling the GPT sees it as a storage device without free space. 84*4882a593Smuzhiyun 85*4882a593SmuzhiyunIt is possible to define 128 linearly placed partition entries. 86*4882a593Smuzhiyun 87*4882a593Smuzhiyun"LBA -1" means the last addressable block (in the mmc subsystem: 88*4882a593Smuzhiyun"dev_desc->lba - 1") 89*4882a593Smuzhiyun 90*4882a593SmuzhiyunPrimary/Backup GPT header: 91*4882a593Smuzhiyun---------------------------- 92*4882a593SmuzhiyunOffset Size Description 93*4882a593Smuzhiyun 94*4882a593Smuzhiyun0 8 B Signature ("EFI PART", 45 46 49 20 50 41 52 54) 95*4882a593Smuzhiyun8 4 B Revision (For version 1.0, the value is 00 00 01 00) 96*4882a593Smuzhiyun12 4 B Header size (in bytes, usually 5C 00 00 00 meaning 92 bytes) 97*4882a593Smuzhiyun16 4 B CRC32 of header (0 to header size), with this field zeroed 98*4882a593Smuzhiyun during calculation 99*4882a593Smuzhiyun20 4 B Reserved (ZERO); 100*4882a593Smuzhiyun24 8 B Current LBA (location of this header copy) 101*4882a593Smuzhiyun32 8 B Backup LBA (location of the other header copy) 102*4882a593Smuzhiyun40 8 B First usable LBA for partitions (primary partition table last 103*4882a593Smuzhiyun LBA + 1) 104*4882a593Smuzhiyun48 8 B Last usable LBA (secondary partition table first LBA - 1) 105*4882a593Smuzhiyun56 16 B Disk GUID (also referred as UUID on UNIXes) 106*4882a593Smuzhiyun72 8 B Partition entries starting LBA (always 2 in primary copy) 107*4882a593Smuzhiyun80 4 B Number of partition entries 108*4882a593Smuzhiyun84 4 B Size of a partition entry (usually 128) 109*4882a593Smuzhiyun88 4 B CRC32 of partition array 110*4882a593Smuzhiyun92 * Reserved; must be ZERO (420 bytes for a 512-byte LBA) 111*4882a593Smuzhiyun 112*4882a593SmuzhiyunTOTAL: 512 B 113*4882a593Smuzhiyun 114*4882a593Smuzhiyun 115*4882a593SmuzhiyunIMPORTANT: 116*4882a593Smuzhiyun 117*4882a593SmuzhiyunGPT headers and partition entries are protected by CRC32 (the POSIX CRC32). 118*4882a593Smuzhiyun 119*4882a593SmuzhiyunPrimary GPT header and Backup GPT header have swapped values of "Current LBA" 120*4882a593Smuzhiyunand "Backup LBA" and therefore different CRC32 check-sum. 121*4882a593Smuzhiyun 122*4882a593SmuzhiyunCRC32 for GPT headers (field "CRC of header") are calculated up till 123*4882a593Smuzhiyun"Header size" (92), NOT 512 bytes. 124*4882a593Smuzhiyun 125*4882a593SmuzhiyunCRC32 for partition entries (field "CRC32 of partition array") is calculated for 126*4882a593Smuzhiyunthe whole array entry ( Number_of_partition_entries * 127*4882a593Smuzhiyunsizeof(partition_entry_size (usually 128))) 128*4882a593Smuzhiyun 129*4882a593SmuzhiyunObserve, how Backup GPT is placed in the memory. It is NOT a mirror reflect 130*4882a593Smuzhiyunof the Primary. 131*4882a593Smuzhiyun 132*4882a593Smuzhiyun Partition Entry Format: 133*4882a593Smuzhiyun ---------------------- 134*4882a593Smuzhiyun Offset Size Description 135*4882a593Smuzhiyun 136*4882a593Smuzhiyun 0 16 B Partition type GUID (Big Endian) 137*4882a593Smuzhiyun 16 16 B Unique partition GUID in (Big Endian) 138*4882a593Smuzhiyun 32 8 B First LBA (Little Endian) 139*4882a593Smuzhiyun 40 8 B Last LBA (inclusive) 140*4882a593Smuzhiyun 48 8 B Attribute flags [+] 141*4882a593Smuzhiyun 56 72 B Partition name (text) 142*4882a593Smuzhiyun 143*4882a593Smuzhiyun Attribute flags: 144*4882a593Smuzhiyun Bit 0 - System partition 145*4882a593Smuzhiyun Bit 1 - Hide from EFI 146*4882a593Smuzhiyun Bit 2 - Legacy BIOS bootable 147*4882a593Smuzhiyun Bit 48-63 - Defined and used by the individual partition type 148*4882a593Smuzhiyun For Basic data partition : 149*4882a593Smuzhiyun Bit 60 - Read-only 150*4882a593Smuzhiyun Bit 62 - Hidden 151*4882a593Smuzhiyun Bit 63 - Not mount 152*4882a593Smuzhiyun 153*4882a593SmuzhiyunCreating GPT partitions in U-Boot: 154*4882a593Smuzhiyun============== 155*4882a593Smuzhiyun 156*4882a593SmuzhiyunTo restore GUID partition table one needs to: 157*4882a593Smuzhiyun1. Define partition layout in the environment. 158*4882a593Smuzhiyun Format of partitions layout: 159*4882a593Smuzhiyun "uuid_disk=...;name=u-boot,size=60MiB,uuid=...; 160*4882a593Smuzhiyun name=kernel,size=60MiB,uuid=...;" 161*4882a593Smuzhiyun or 162*4882a593Smuzhiyun "uuid_disk=${uuid_gpt_disk};name=${uboot_name}, 163*4882a593Smuzhiyun size=${uboot_size},uuid=${uboot_uuid};" 164*4882a593Smuzhiyun 165*4882a593Smuzhiyun The fields 'name' and 'size' are mandatory for every partition. 166*4882a593Smuzhiyun The field 'start' is optional. 167*4882a593Smuzhiyun 168*4882a593Smuzhiyun If field 'size' of the last partition is 0, the partition is extended 169*4882a593Smuzhiyun up to the end of the device. 170*4882a593Smuzhiyun 171*4882a593Smuzhiyun The fields 'uuid' and 'uuid_disk' are optional if CONFIG_RANDOM_UUID is 172*4882a593Smuzhiyun enabled. A random uuid will be used if omitted or they point to an empty/ 173*4882a593Smuzhiyun non-existent environment variable. The environment variable will be set to 174*4882a593Smuzhiyun the generated UUID. The 'gpt guid' command reads the current value of the 175*4882a593Smuzhiyun uuid_disk from the GPT. 176*4882a593Smuzhiyun 177*4882a593Smuzhiyun The field 'bootable' is optional, it is used to mark the GPT partition 178*4882a593Smuzhiyun bootable (set attribute flags "Legacy BIOS bootable"). 179*4882a593Smuzhiyun "name=u-boot,size=60MiB;name=boot,size=60Mib,bootable;name=rootfs,size=0" 180*4882a593Smuzhiyun It can be used to locate bootable disks with command 181*4882a593Smuzhiyun "part list <interface> <dev> -bootable <varname>", 182*4882a593Smuzhiyun please check out doc/README.distro for use. 183*4882a593Smuzhiyun 184*4882a593Smuzhiyun2. Define 'CONFIG_EFI_PARTITION' and 'CONFIG_CMD_GPT' 185*4882a593Smuzhiyun 186*4882a593Smuzhiyun3. From u-boot prompt type: 187*4882a593Smuzhiyun gpt write mmc 0 $partitions 188*4882a593Smuzhiyun 189*4882a593SmuzhiyunChecking (validating) GPT partitions in U-Boot: 190*4882a593Smuzhiyun=============================================== 191*4882a593Smuzhiyun 192*4882a593SmuzhiyunProcedure is the same as above. The only change is at point 3. 193*4882a593Smuzhiyun 194*4882a593SmuzhiyunAt u-boot prompt one needs to write: 195*4882a593Smuzhiyun gpt verify mmc 0 [$partitions] 196*4882a593Smuzhiyun 197*4882a593Smuzhiyunwhere [$partitions] is an optional parameter. 198*4882a593Smuzhiyun 199*4882a593SmuzhiyunWhen it is not provided, only basic checks based on CRC32 calculation for GPT 200*4882a593Smuzhiyunheader and PTEs are performed. 201*4882a593SmuzhiyunWhen provided, additionally partition data - name, size and starting 202*4882a593Smuzhiyunoffset (last two in LBA) - are compared with data defined in '$partitions' 203*4882a593Smuzhiyunenvironment variable. 204*4882a593Smuzhiyun 205*4882a593SmuzhiyunAfter running this command, return code is set to 0 if no errors found in 206*4882a593Smuzhiyunon non-volatile medium stored GPT. 207*4882a593Smuzhiyun 208*4882a593SmuzhiyunFollowing line can be used to assess if GPT verification has succeed: 209*4882a593Smuzhiyun 210*4882a593SmuzhiyunU-BOOT> gpt verify mmc 0 $partitions 211*4882a593SmuzhiyunU-BOOT> if test $? = 0; then echo "GPT OK"; else echo "GPT ERR"; fi 212*4882a593Smuzhiyun 213*4882a593SmuzhiyunRenaming GPT partitions from U-Boot: 214*4882a593Smuzhiyun==================================== 215*4882a593Smuzhiyun 216*4882a593SmuzhiyunGPT partition names are a mechanism via which userspace and U-Boot can 217*4882a593Smuzhiyuncommunicate about software updates and boot failure. The 'gpt guid', 218*4882a593Smuzhiyun'gpt read', 'gpt rename' and 'gpt swap' commands facilitate 219*4882a593Smuzhiyunprogrammatic renaming of partitions from bootscripts by generating and 220*4882a593Smuzhiyunmodifying the partitions layout string. Here is an illustration of 221*4882a593Smuzhiyunemploying 'swap' to exchange 'primary' and 'backup' partition names: 222*4882a593Smuzhiyun 223*4882a593SmuzhiyunU-BOOT> gpt swap mmc 0 primary backup 224*4882a593Smuzhiyun 225*4882a593SmuzhiyunAfterwards, all partitions previously named 'primary' will be named 226*4882a593Smuzhiyun'backup', and vice-versa. Alternatively, single partitions may be 227*4882a593Smuzhiyunrenamed. In this example, mmc0's first partition will be renamed 228*4882a593Smuzhiyun'primary': 229*4882a593Smuzhiyun 230*4882a593SmuzhiyunU-BOOT> gpt rename mmc 0 1 primary 231*4882a593Smuzhiyun 232*4882a593SmuzhiyunThe GPT functionality may be tested with the 'sandbox' board by 233*4882a593Smuzhiyuncreating a disk image as described under 'Block Device Emulation' in 234*4882a593Smuzhiyunboard/sandbox/README.sandbox: 235*4882a593Smuzhiyun 236*4882a593Smuzhiyun=>host bind 0 ./disk.raw 237*4882a593Smuzhiyun=> gpt read host 0 238*4882a593Smuzhiyun[ . . . ] 239*4882a593Smuzhiyun=> gpt swap host 0 name othername 240*4882a593Smuzhiyun[ . . . ] 241*4882a593Smuzhiyun 242*4882a593SmuzhiyunPartition type GUID: 243*4882a593Smuzhiyun==================== 244*4882a593Smuzhiyun 245*4882a593SmuzhiyunFor created partition, the used partition type GUID is 246*4882a593SmuzhiyunPARTITION_BASIC_DATA_GUID (EBD0A0A2-B9E5-4433-87C0-68B6B72699C7). 247*4882a593Smuzhiyun 248*4882a593SmuzhiyunIf you define 'CONFIG_PARTITION_TYPE_GUID', a optionnal parameter 'type' 249*4882a593Smuzhiyuncan specify a other partition type guid: 250*4882a593Smuzhiyun 251*4882a593Smuzhiyun "uuid_disk=...;name=u-boot,size=60MiB,uuid=...; 252*4882a593Smuzhiyun name=kernel,size=60MiB,uuid=..., 253*4882a593Smuzhiyun type=0FC63DAF-8483-4772-8E79-3D69D8477DE4;" 254*4882a593Smuzhiyun 255*4882a593SmuzhiyunSome strings can be also used at the place of known GUID : 256*4882a593Smuzhiyun "system" = PARTITION_SYSTEM_GUID 257*4882a593Smuzhiyun (C12A7328-F81F-11D2-BA4B-00A0C93EC93B) 258*4882a593Smuzhiyun "mbr" = LEGACY_MBR_PARTITION_GUID 259*4882a593Smuzhiyun (024DEE41-33E7-11D3-9D69-0008C781F39F) 260*4882a593Smuzhiyun "msft" = PARTITION_MSFT_RESERVED_GUID 261*4882a593Smuzhiyun (E3C9E316-0B5C-4DB8-817D-F92DF00215AE) 262*4882a593Smuzhiyun "data" = PARTITION_BASIC_DATA_GUID 263*4882a593Smuzhiyun (EBD0A0A2-B9E5-4433-87C0-68B6B72699C7) 264*4882a593Smuzhiyun "linux" = PARTITION_LINUX_FILE_SYSTEM_DATA_GUID 265*4882a593Smuzhiyun (0FC63DAF-8483-4772-8E79-3D69D8477DE4) 266*4882a593Smuzhiyun "raid" = PARTITION_LINUX_RAID_GUID 267*4882a593Smuzhiyun (A19D880F-05FC-4D3B-A006-743F0F84911E) 268*4882a593Smuzhiyun "swap" = PARTITION_LINUX_SWAP_GUID 269*4882a593Smuzhiyun (0657FD6D-A4AB-43C4-84E5-0933C84B4F4F) 270*4882a593Smuzhiyun "lvm" = PARTITION_LINUX_LVM_GUID 271*4882a593Smuzhiyun (E6D6D379-F507-44C2-A23C-238F2A3DF928) 272*4882a593Smuzhiyun 273*4882a593Smuzhiyun "uuid_disk=...;name=u-boot,size=60MiB,uuid=...; 274*4882a593Smuzhiyun name=kernel,size=60MiB,uuid=...,type=linux;" 275*4882a593Smuzhiyun 276*4882a593SmuzhiyunThey are also used to display the type of partition in "part list" command. 277*4882a593Smuzhiyun 278*4882a593Smuzhiyun 279*4882a593SmuzhiyunUseful info: 280*4882a593Smuzhiyun============ 281*4882a593Smuzhiyun 282*4882a593SmuzhiyunTwo programs, namely: 'gdisk' and 'parted' are recommended to work with GPT 283*4882a593Smuzhiyunrecovery. Both are able to handle GUID partitions. 284*4882a593SmuzhiyunPlease, pay attention at -l switch for parted. 285*4882a593Smuzhiyun 286*4882a593Smuzhiyun"uuid" program is recommended to generate UUID string. Moreover it can decode 287*4882a593Smuzhiyun(-d switch) passed in UUID string. It can be used to generate partitions UUID 288*4882a593Smuzhiyunpassed to u-boot environment variables. 289*4882a593SmuzhiyunIf optional CONFIG_RANDOM_UUID is defined then for any partition which environment 290*4882a593Smuzhiyunuuid is unset, uuid is randomly generated and stored in correspond environment 291*4882a593Smuzhiyunvariable. 292*4882a593Smuzhiyun 293*4882a593Smuzhiyunnote: 294*4882a593SmuzhiyunEach string block of UUID generated by program "uuid" is in big endian and it is 295*4882a593Smuzhiyunalso stored in big endian in disk GPT. 296*4882a593SmuzhiyunPartitions layout can be printed by typing "mmc part". Note that each partition 297*4882a593SmuzhiyunGUID has different byte order than UUID generated before, this is because first 298*4882a593Smuzhiyunthree blocks of GUID string are in Little Endian. 299