README.gpt 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. #
  2. # Copyright (C) 2012 Samsung Electronics
  3. #
  4. # Lukasz Majewski <l.majewski@samsung.com>
  5. #
  6. #
  7. # SPDX-License-Identifier: GPL-2.0+
  8. Glossary:
  9. ========
  10. - UUID -(Universally Unique Identifier)
  11. - GUID - (Globally Unique ID)
  12. - EFI - (Extensible Firmware Interface)
  13. - UEFI - (Unified EFI) - EFI evolution
  14. - GPT (GUID Partition Table) - it is the EFI standard part
  15. - partitions - lists of available partitions (defined at u-boot):
  16. ./include/configs/{target}.h
  17. Introduction:
  18. =============
  19. This document describes the GPT partition table format and usage of
  20. the gpt command in u-boot.
  21. UUID introduction:
  22. ====================
  23. GPT for marking disks/partitions is using the UUID. It is supposed to be a
  24. globally unique value. A UUID is a 16-byte (128-bit) number. The number of
  25. theoretically possible UUIDs is therefore about 3 x 10^38.
  26. More often UUID is displayed as 32 hexadecimal digits, in 5 groups,
  27. separated by hyphens, in the form 8-4-4-4-12 for a total of 36 characters
  28. (32 digits and 4 hyphens)
  29. For instance, GUID of Basic data partition: EBD0A0A2-B9E5-4433-87C0-68B6B72699C7
  30. and GUID of Linux filesystem data: 0FC63DAF-8483-4772-8E79-3D69D8477DE4
  31. Historically there are 5 methods to generate this number. The oldest one is
  32. combining machine's MAC address and timer (epoch) value.
  33. Successive versions are using MD5 hash, random numbers and SHA-1 hash. All major
  34. OSes and programming languages are providing libraries to compute UUID (e.g.
  35. uuid command line tool).
  36. GPT brief explanation:
  37. ======================
  38. Layout:
  39. -------
  40. --------------------------------------------------
  41. LBA 0 |Protective MBR |
  42. ----------------------------------------------------------
  43. LBA 1 |Primary GPT Header | Primary
  44. -------------------------------------------------- GPT
  45. LBA 2 |Entry 1|Entry 2| Entry 3| Entry 4|
  46. --------------------------------------------------
  47. LBA 3 |Entries 5 - 128 |
  48. | |
  49. | |
  50. ----------------------------------------------------------
  51. LBA 34 |Partition 1 |
  52. | |
  53. -----------------------------------
  54. |Partition 2 |
  55. | |
  56. -----------------------------------
  57. |Partition n |
  58. | |
  59. ----------------------------------------------------------
  60. LBA -34 |Entry 1|Entry 2| Entry 3| Entry 4| Backup
  61. -------------------------------------------------- GPT
  62. LBA -33 |Entries 5 - 128 |
  63. | |
  64. | |
  65. LBA -2 | |
  66. --------------------------------------------------
  67. LBA -1 |Backup GPT Header |
  68. ----------------------------------------------------------
  69. For a legacy reasons, GPT's LBA 0 sector has a MBR structure. It is called
  70. "protective MBR".
  71. Its first partition entry ID has 0xEE value, and disk software, which is not
  72. handling the GPT sees it as a storage device without free space.
  73. It is possible to define 128 linearly placed partition entries.
  74. "LBA -1" means the last addressable block (in the mmc subsystem:
  75. "dev_desc->lba - 1")
  76. Primary/Backup GPT header:
  77. ----------------------------
  78. Offset Size Description
  79. 0 8 B Signature ("EFI PART", 45 46 49 20 50 41 52 54)
  80. 8 4 B Revision (For version 1.0, the value is 00 00 01 00)
  81. 12 4 B Header size (in bytes, usually 5C 00 00 00 meaning 92 bytes)
  82. 16 4 B CRC32 of header (0 to header size), with this field zeroed
  83. during calculation
  84. 20 4 B Reserved (ZERO);
  85. 24 8 B Current LBA (location of this header copy)
  86. 32 8 B Backup LBA (location of the other header copy)
  87. 40 8 B First usable LBA for partitions (primary partition table last
  88. LBA + 1)
  89. 48 8 B Last usable LBA (secondary partition table first LBA - 1)
  90. 56 16 B Disk GUID (also referred as UUID on UNIXes)
  91. 72 8 B Partition entries starting LBA (always 2 in primary copy)
  92. 80 4 B Number of partition entries
  93. 84 4 B Size of a partition entry (usually 128)
  94. 88 4 B CRC32 of partition array
  95. 92 * Reserved; must be ZERO (420 bytes for a 512-byte LBA)
  96. TOTAL: 512 B
  97. IMPORTANT:
  98. GPT headers and partition entries are protected by CRC32 (the POSIX CRC32).
  99. Primary GPT header and Backup GPT header have swapped values of "Current LBA"
  100. and "Backup LBA" and therefore different CRC32 check-sum.
  101. CRC32 for GPT headers (field "CRC of header") are calculated up till
  102. "Header size" (92), NOT 512 bytes.
  103. CRC32 for partition entries (field "CRC32 of partition array") is calculated for
  104. the whole array entry ( Number_of_partition_entries *
  105. sizeof(partition_entry_size (usually 128)))
  106. Observe, how Backup GPT is placed in the memory. It is NOT a mirror reflect
  107. of the Primary.
  108. Partition Entry Format:
  109. ----------------------
  110. Offset Size Description
  111. 0 16 B Partition type GUID (Big Endian)
  112. 16 16 B Unique partition GUID in (Big Endian)
  113. 32 8 B First LBA (Little Endian)
  114. 40 8 B Last LBA (inclusive)
  115. 48 8 B Attribute flags [+]
  116. 56 72 B Partition name (text)
  117. Attribute flags:
  118. Bit 0 - System partition
  119. Bit 1 - Hide from EFI
  120. Bit 2 - Legacy BIOS bootable
  121. Bit 48-63 - Defined and used by the individual partition type
  122. For Basic data partition :
  123. Bit 60 - Read-only
  124. Bit 62 - Hidden
  125. Bit 63 - Not mount
  126. Creating GPT partitions in U-Boot:
  127. ==============
  128. To restore GUID partition table one needs to:
  129. 1. Define partition layout in the environment.
  130. Format of partitions layout:
  131. "partitions=uuid_disk=...;name=u-boot,size=60MiB,uuid=...;
  132. name=kernel,size=60MiB,uuid=...;"
  133. or
  134. "partitions=uuid_disk=${uuid_gpt_disk};name=${uboot_name},
  135. size=${uboot_size},uuid=${uboot_uuid};"
  136. The fields 'name' and 'size' are mandatory for every partition.
  137. The field 'start' is optional.
  138. If field 'size' of the last partition is 0, the partition is extended
  139. up to the end of the device.
  140. The fields 'uuid' and 'uuid_disk' are optional if CONFIG_RANDOM_UUID is
  141. enabled. A random uuid will be used if omitted or they point to an empty/
  142. non-existent environment variable. The environment variable will be set to
  143. the generated UUID.
  144. The field 'bootable' is optional, it is used to mark the GPT partition
  145. bootable (set attribute flags "Legacy BIOS bootable").
  146. "name=u-boot,size=60MiB;name=boot,size=60Mib,bootable;name=rootfs,size=0"
  147. It can be used to locate bootable disks with command
  148. "part list <interface> <dev> -bootable <varname>",
  149. please check out doc/README.distro for use.
  150. 2. Define 'CONFIG_EFI_PARTITION' and 'CONFIG_CMD_GPT'
  151. 3. From u-boot prompt type:
  152. gpt write mmc 0 $partitions
  153. Checking (validating) GPT partitions in U-Boot:
  154. ===============================================
  155. Procedure is the same as above. The only change is at point 3.
  156. At u-boot prompt one needs to write:
  157. gpt verify mmc 0 [$partitions]
  158. where [$partitions] is an optional parameter.
  159. When it is not provided, only basic checks based on CRC32 calculation for GPT
  160. header and PTEs are performed.
  161. When provided, additionally partition data - name, size and starting
  162. offset (last two in LBA) - are compared with data defined in '$partitions'
  163. environment variable.
  164. After running this command, return code is set to 0 if no errors found in
  165. on non-volatile medium stored GPT.
  166. Following line can be used to assess if GPT verification has succeed:
  167. U-BOOT> gpt verify mmc 0 $partitions
  168. U-BOOT> if test $? = 0; then echo "GPT OK"; else echo "GPT ERR"; fi
  169. Partition type GUID:
  170. ====================
  171. For created partition, the used partition type GUID is
  172. PARTITION_BASIC_DATA_GUID (EBD0A0A2-B9E5-4433-87C0-68B6B72699C7).
  173. If you define 'CONFIG_PARTITION_TYPE_GUID', a optionnal parameter 'type'
  174. can specify a other partition type guid:
  175. "partitions=uuid_disk=...;name=u-boot,size=60MiB,uuid=...;
  176. name=kernel,size=60MiB,uuid=...,
  177. type=0FC63DAF-8483-4772-8E79-3D69D8477DE4;"
  178. Some strings can be also used at the place of known GUID :
  179. "system" = PARTITION_SYSTEM_GUID
  180. (C12A7328-F81F-11D2-BA4B-00A0C93EC93B)
  181. "mbr" = LEGACY_MBR_PARTITION_GUID
  182. (024DEE41-33E7-11D3-9D69-0008C781F39F)
  183. "msft" = PARTITION_MSFT_RESERVED_GUID
  184. (E3C9E316-0B5C-4DB8-817D-F92DF00215AE)
  185. "data" = PARTITION_BASIC_DATA_GUID
  186. (EBD0A0A2-B9E5-4433-87C0-68B6B72699C7)
  187. "linux" = PARTITION_LINUX_FILE_SYSTEM_DATA_GUID
  188. (0FC63DAF-8483-4772-8E79-3D69D8477DE4)
  189. "raid" = PARTITION_LINUX_RAID_GUID
  190. (A19D880F-05FC-4D3B-A006-743F0F84911E)
  191. "swap" = PARTITION_LINUX_SWAP_GUID
  192. (0657FD6D-A4AB-43C4-84E5-0933C84B4F4F)
  193. "lvm" = PARTITION_LINUX_LVM_GUID
  194. (E6D6D379-F507-44C2-A23C-238F2A3DF928)
  195. "partitions=uuid_disk=...;name=u-boot,size=60MiB,uuid=...;
  196. name=kernel,size=60MiB,uuid=...,type=linux;"
  197. They are also used to display the type of partition in "part list" command.
  198. Useful info:
  199. ============
  200. Two programs, namely: 'gdisk' and 'parted' are recommended to work with GPT
  201. recovery. Both are able to handle GUID partitions.
  202. Please, pay attention at -l switch for parted.
  203. "uuid" program is recommended to generate UUID string. Moreover it can decode
  204. (-d switch) passed in UUID string. It can be used to generate partitions UUID
  205. passed to u-boot environment variables.
  206. If optional CONFIG_RANDOM_UUID is defined then for any partition which environment
  207. uuid is unset, uuid is randomly generated and stored in correspond environment
  208. variable.
  209. note:
  210. Each string block of UUID generated by program "uuid" is in big endian and it is
  211. also stored in big endian in disk GPT.
  212. Partitions layout can be printed by typing "mmc part". Note that each partition
  213. GUID has different byte order than UUID generated before, this is because first
  214. three blocks of GUID string are in Little Endian.