part_efi.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. /*
  2. * Copyright (C) 2008 RuggedCom, Inc.
  3. * Richard Retanubun <RichardRetanubun@RuggedCom.com>
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. /*
  24. * Problems with CONFIG_SYS_64BIT_LBA:
  25. *
  26. * struct disk_partition.start in include/part.h is sized as ulong.
  27. * When CONFIG_SYS_64BIT_LBA is activated, lbaint_t changes from ulong to uint64_t.
  28. * For now, it is cast back to ulong at assignment.
  29. *
  30. * This limits the maximum size of addressable storage to < 2 Terra Bytes
  31. */
  32. #include <common.h>
  33. #include <command.h>
  34. #include <ide.h>
  35. #include <malloc.h>
  36. #include <part_efi.h>
  37. #include <linux/ctype.h>
  38. #if defined(CONFIG_CMD_IDE) || \
  39. defined(CONFIG_CMD_SATA) || \
  40. defined(CONFIG_CMD_SCSI) || \
  41. defined(CONFIG_CMD_USB) || \
  42. defined(CONFIG_MMC) || \
  43. defined(CONFIG_SYSTEMACE)
  44. /**
  45. * efi_crc32() - EFI version of crc32 function
  46. * @buf: buffer to calculate crc32 of
  47. * @len - length of buf
  48. *
  49. * Description: Returns EFI-style CRC32 value for @buf
  50. */
  51. static inline u32 efi_crc32(const void *buf, u32 len)
  52. {
  53. return crc32(0, buf, len);
  54. }
  55. /*
  56. * Private function prototypes
  57. */
  58. static int pmbr_part_valid(struct partition *part);
  59. static int is_pmbr_valid(legacy_mbr * mbr);
  60. static int is_gpt_valid(block_dev_desc_t * dev_desc, unsigned long long lba,
  61. gpt_header * pgpt_head, gpt_entry ** pgpt_pte);
  62. static gpt_entry *alloc_read_gpt_entries(block_dev_desc_t * dev_desc,
  63. gpt_header * pgpt_head);
  64. static int is_pte_valid(gpt_entry * pte);
  65. static char *print_efiname(gpt_entry *pte)
  66. {
  67. static char name[PARTNAME_SZ + 1];
  68. int i;
  69. for (i = 0; i < PARTNAME_SZ; i++) {
  70. u8 c;
  71. c = pte->partition_name[i] & 0xff;
  72. c = (c && !isprint(c)) ? '.' : c;
  73. name[i] = c;
  74. }
  75. name[PARTNAME_SZ] = 0;
  76. return name;
  77. }
  78. static void uuid_string(unsigned char *uuid, char *str)
  79. {
  80. static const u8 le[16] = {3, 2, 1, 0, 5, 4, 7, 6, 8, 9, 10, 11,
  81. 12, 13, 14, 15};
  82. int i;
  83. for (i = 0; i < 16; i++) {
  84. sprintf(str, "%02x", uuid[le[i]]);
  85. str += 2;
  86. switch (i) {
  87. case 3:
  88. case 5:
  89. case 7:
  90. case 9:
  91. *str++ = '-';
  92. break;
  93. }
  94. }
  95. }
  96. static efi_guid_t system_guid = PARTITION_SYSTEM_GUID;
  97. static inline int is_bootable(gpt_entry *p)
  98. {
  99. return p->attributes.fields.legacy_bios_bootable ||
  100. !memcmp(&(p->partition_type_guid), &system_guid,
  101. sizeof(efi_guid_t));
  102. }
  103. /*
  104. * Public Functions (include/part.h)
  105. */
  106. void print_part_efi(block_dev_desc_t * dev_desc)
  107. {
  108. ALLOC_CACHE_ALIGN_BUFFER(gpt_header, gpt_head, 1);
  109. gpt_entry *gpt_pte = NULL;
  110. int i = 0;
  111. char uuid[37];
  112. if (!dev_desc) {
  113. printf("%s: Invalid Argument(s)\n", __func__);
  114. return;
  115. }
  116. /* This function validates AND fills in the GPT header and PTE */
  117. if (is_gpt_valid(dev_desc, GPT_PRIMARY_PARTITION_TABLE_LBA,
  118. gpt_head, &gpt_pte) != 1) {
  119. printf("%s: *** ERROR: Invalid GPT ***\n", __func__);
  120. return;
  121. }
  122. debug("%s: gpt-entry at %p\n", __func__, gpt_pte);
  123. printf("Part\tStart LBA\tEnd LBA\t\tName\n");
  124. printf("\tAttributes\n");
  125. printf("\tType UUID\n");
  126. printf("\tPartition UUID\n");
  127. for (i = 0; i < le32_to_cpu(gpt_head->num_partition_entries); i++) {
  128. /* Stop at the first non valid PTE */
  129. if (!is_pte_valid(&gpt_pte[i]))
  130. break;
  131. printf("%3d\t0x%08llx\t0x%08llx\t\"%s\"\n", (i + 1),
  132. le64_to_cpu(gpt_pte[i].starting_lba),
  133. le64_to_cpu(gpt_pte[i].ending_lba),
  134. print_efiname(&gpt_pte[i]));
  135. printf("\tattrs:\t0x%016llx\n", gpt_pte[i].attributes.raw);
  136. uuid_string(gpt_pte[i].partition_type_guid.b, uuid);
  137. printf("\ttype:\t%s\n", uuid);
  138. uuid_string(gpt_pte[i].unique_partition_guid.b, uuid);
  139. printf("\tuuid:\t%s\n", uuid);
  140. }
  141. /* Remember to free pte */
  142. free(gpt_pte);
  143. return;
  144. }
  145. int get_partition_info_efi(block_dev_desc_t * dev_desc, int part,
  146. disk_partition_t * info)
  147. {
  148. ALLOC_CACHE_ALIGN_BUFFER(gpt_header, gpt_head, 1);
  149. gpt_entry *gpt_pte = NULL;
  150. /* "part" argument must be at least 1 */
  151. if (!dev_desc || !info || part < 1) {
  152. printf("%s: Invalid Argument(s)\n", __func__);
  153. return -1;
  154. }
  155. /* This function validates AND fills in the GPT header and PTE */
  156. if (is_gpt_valid(dev_desc, GPT_PRIMARY_PARTITION_TABLE_LBA,
  157. gpt_head, &gpt_pte) != 1) {
  158. printf("%s: *** ERROR: Invalid GPT ***\n", __func__);
  159. return -1;
  160. }
  161. if (part > le32_to_cpu(gpt_head->num_partition_entries) ||
  162. !is_pte_valid(&gpt_pte[part - 1])) {
  163. printf("%s: *** ERROR: Invalid partition number %d ***\n",
  164. __func__, part);
  165. return -1;
  166. }
  167. /* The ulong casting limits the maximum disk size to 2 TB */
  168. info->start = (u64)le64_to_cpu(gpt_pte[part - 1].starting_lba);
  169. /* The ending LBA is inclusive, to calculate size, add 1 to it */
  170. info->size = ((u64)le64_to_cpu(gpt_pte[part - 1].ending_lba) + 1)
  171. - info->start;
  172. info->blksz = GPT_BLOCK_SIZE;
  173. sprintf((char *)info->name, "%s",
  174. print_efiname(&gpt_pte[part - 1]));
  175. sprintf((char *)info->type, "U-Boot");
  176. info->bootable = is_bootable(&gpt_pte[part - 1]);
  177. #ifdef CONFIG_PARTITION_UUIDS
  178. uuid_string(gpt_pte[part - 1].unique_partition_guid.b, info->uuid);
  179. #endif
  180. debug("%s: start 0x%lX, size 0x%lX, name %s", __func__,
  181. info->start, info->size, info->name);
  182. /* Remember to free pte */
  183. free(gpt_pte);
  184. return 0;
  185. }
  186. int test_part_efi(block_dev_desc_t * dev_desc)
  187. {
  188. ALLOC_CACHE_ALIGN_BUFFER(legacy_mbr, legacymbr, 1);
  189. /* Read legacy MBR from block 0 and validate it */
  190. if ((dev_desc->block_read(dev_desc->dev, 0, 1, (ulong *)legacymbr) != 1)
  191. || (is_pmbr_valid(legacymbr) != 1)) {
  192. return -1;
  193. }
  194. return 0;
  195. }
  196. /*
  197. * Private functions
  198. */
  199. /*
  200. * pmbr_part_valid(): Check for EFI partition signature
  201. *
  202. * Returns: 1 if EFI GPT partition type is found.
  203. */
  204. static int pmbr_part_valid(struct partition *part)
  205. {
  206. if (part->sys_ind == EFI_PMBR_OSTYPE_EFI_GPT &&
  207. le32_to_cpu(part->start_sect) == 1UL) {
  208. return 1;
  209. }
  210. return 0;
  211. }
  212. /*
  213. * is_pmbr_valid(): test Protective MBR for validity
  214. *
  215. * Returns: 1 if PMBR is valid, 0 otherwise.
  216. * Validity depends on two things:
  217. * 1) MSDOS signature is in the last two bytes of the MBR
  218. * 2) One partition of type 0xEE is found, checked by pmbr_part_valid()
  219. */
  220. static int is_pmbr_valid(legacy_mbr * mbr)
  221. {
  222. int i = 0;
  223. if (!mbr || le16_to_cpu(mbr->signature) != MSDOS_MBR_SIGNATURE)
  224. return 0;
  225. for (i = 0; i < 4; i++) {
  226. if (pmbr_part_valid(&mbr->partition_record[i])) {
  227. return 1;
  228. }
  229. }
  230. return 0;
  231. }
  232. /**
  233. * is_gpt_valid() - tests one GPT header and PTEs for validity
  234. *
  235. * lba is the logical block address of the GPT header to test
  236. * gpt is a GPT header ptr, filled on return.
  237. * ptes is a PTEs ptr, filled on return.
  238. *
  239. * Description: returns 1 if valid, 0 on error.
  240. * If valid, returns pointers to PTEs.
  241. */
  242. static int is_gpt_valid(block_dev_desc_t * dev_desc, unsigned long long lba,
  243. gpt_header * pgpt_head, gpt_entry ** pgpt_pte)
  244. {
  245. u32 crc32_backup = 0;
  246. u32 calc_crc32;
  247. unsigned long long lastlba;
  248. if (!dev_desc || !pgpt_head) {
  249. printf("%s: Invalid Argument(s)\n", __func__);
  250. return 0;
  251. }
  252. /* Read GPT Header from device */
  253. if (dev_desc->block_read(dev_desc->dev, lba, 1, pgpt_head) != 1) {
  254. printf("*** ERROR: Can't read GPT header ***\n");
  255. return 0;
  256. }
  257. /* Check the GPT header signature */
  258. if (le64_to_cpu(pgpt_head->signature) != GPT_HEADER_SIGNATURE) {
  259. printf("GUID Partition Table Header signature is wrong:"
  260. "0x%llX != 0x%llX\n",
  261. le64_to_cpu(pgpt_head->signature),
  262. GPT_HEADER_SIGNATURE);
  263. return 0;
  264. }
  265. /* Check the GUID Partition Table CRC */
  266. memcpy(&crc32_backup, &pgpt_head->header_crc32, sizeof(crc32_backup));
  267. memset(&pgpt_head->header_crc32, 0, sizeof(pgpt_head->header_crc32));
  268. calc_crc32 = efi_crc32((const unsigned char *)pgpt_head,
  269. le32_to_cpu(pgpt_head->header_size));
  270. memcpy(&pgpt_head->header_crc32, &crc32_backup, sizeof(crc32_backup));
  271. if (calc_crc32 != le32_to_cpu(crc32_backup)) {
  272. printf("GUID Partition Table Header CRC is wrong:"
  273. "0x%x != 0x%x\n",
  274. le32_to_cpu(crc32_backup), calc_crc32);
  275. return 0;
  276. }
  277. /* Check that the my_lba entry points to the LBA that contains the GPT */
  278. if (le64_to_cpu(pgpt_head->my_lba) != lba) {
  279. printf("GPT: my_lba incorrect: %llX != %llX\n",
  280. le64_to_cpu(pgpt_head->my_lba),
  281. lba);
  282. return 0;
  283. }
  284. /* Check the first_usable_lba and last_usable_lba are within the disk. */
  285. lastlba = (unsigned long long)dev_desc->lba;
  286. if (le64_to_cpu(pgpt_head->first_usable_lba) > lastlba) {
  287. printf("GPT: first_usable_lba incorrect: %llX > %llX\n",
  288. le64_to_cpu(pgpt_head->first_usable_lba), lastlba);
  289. return 0;
  290. }
  291. if (le64_to_cpu(pgpt_head->last_usable_lba) > lastlba) {
  292. printf("GPT: last_usable_lba incorrect: %llX > %llX\n",
  293. (u64) le64_to_cpu(pgpt_head->last_usable_lba), lastlba);
  294. return 0;
  295. }
  296. debug("GPT: first_usable_lba: %llX last_usable_lba %llX last lba %llX\n",
  297. le64_to_cpu(pgpt_head->first_usable_lba),
  298. le64_to_cpu(pgpt_head->last_usable_lba), lastlba);
  299. /* Read and allocate Partition Table Entries */
  300. *pgpt_pte = alloc_read_gpt_entries(dev_desc, pgpt_head);
  301. if (*pgpt_pte == NULL) {
  302. printf("GPT: Failed to allocate memory for PTE\n");
  303. return 0;
  304. }
  305. /* Check the GUID Partition Table Entry Array CRC */
  306. calc_crc32 = efi_crc32((const unsigned char *)*pgpt_pte,
  307. le32_to_cpu(pgpt_head->num_partition_entries) *
  308. le32_to_cpu(pgpt_head->sizeof_partition_entry));
  309. if (calc_crc32 != le32_to_cpu(pgpt_head->partition_entry_array_crc32)) {
  310. printf("GUID Partition Table Entry Array CRC is wrong:"
  311. "0x%x != 0x%x\n",
  312. le32_to_cpu(pgpt_head->partition_entry_array_crc32),
  313. calc_crc32);
  314. free(*pgpt_pte);
  315. return 0;
  316. }
  317. /* We're done, all's well */
  318. return 1;
  319. }
  320. /**
  321. * alloc_read_gpt_entries(): reads partition entries from disk
  322. * @dev_desc
  323. * @gpt - GPT header
  324. *
  325. * Description: Returns ptes on success, NULL on error.
  326. * Allocates space for PTEs based on information found in @gpt.
  327. * Notes: remember to free pte when you're done!
  328. */
  329. static gpt_entry *alloc_read_gpt_entries(block_dev_desc_t * dev_desc,
  330. gpt_header * pgpt_head)
  331. {
  332. size_t count = 0;
  333. gpt_entry *pte = NULL;
  334. if (!dev_desc || !pgpt_head) {
  335. printf("%s: Invalid Argument(s)\n", __func__);
  336. return NULL;
  337. }
  338. count = le32_to_cpu(pgpt_head->num_partition_entries) *
  339. le32_to_cpu(pgpt_head->sizeof_partition_entry);
  340. debug("%s: count = %u * %u = %zu\n", __func__,
  341. (u32) le32_to_cpu(pgpt_head->num_partition_entries),
  342. (u32) le32_to_cpu(pgpt_head->sizeof_partition_entry), count);
  343. /* Allocate memory for PTE, remember to FREE */
  344. if (count != 0) {
  345. pte = memalign(ARCH_DMA_MINALIGN, count);
  346. }
  347. if (count == 0 || pte == NULL) {
  348. printf("%s: ERROR: Can't allocate 0x%zX "
  349. "bytes for GPT Entries\n",
  350. __func__, count);
  351. return NULL;
  352. }
  353. /* Read GPT Entries from device */
  354. if (dev_desc->block_read (dev_desc->dev,
  355. le64_to_cpu(pgpt_head->partition_entry_lba),
  356. (lbaint_t) (count / GPT_BLOCK_SIZE), pte)
  357. != (count / GPT_BLOCK_SIZE)) {
  358. printf("*** ERROR: Can't read GPT Entries ***\n");
  359. free(pte);
  360. return NULL;
  361. }
  362. return pte;
  363. }
  364. /**
  365. * is_pte_valid(): validates a single Partition Table Entry
  366. * @gpt_entry - Pointer to a single Partition Table Entry
  367. *
  368. * Description: returns 1 if valid, 0 on error.
  369. */
  370. static int is_pte_valid(gpt_entry * pte)
  371. {
  372. efi_guid_t unused_guid;
  373. if (!pte) {
  374. printf("%s: Invalid Argument(s)\n", __func__);
  375. return 0;
  376. }
  377. /* Only one validation for now:
  378. * The GUID Partition Type != Unused Entry (ALL-ZERO)
  379. */
  380. memset(unused_guid.b, 0, sizeof(unused_guid.b));
  381. if (memcmp(pte->partition_type_guid.b, unused_guid.b,
  382. sizeof(unused_guid.b)) == 0) {
  383. debug("%s: Found an unused PTE GUID at 0x%08X\n", __func__,
  384. (unsigned int)(uintptr_t)pte);
  385. return 0;
  386. } else {
  387. return 1;
  388. }
  389. }
  390. #endif