part_efi.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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. /* Convert char[2] in little endian format to the host format integer
  45. */
  46. static inline unsigned short le16_to_int(unsigned char *le16)
  47. {
  48. return ((le16[1] << 8) + le16[0]);
  49. }
  50. /* Convert char[4] in little endian format to the host format integer
  51. */
  52. static inline unsigned long le32_to_int(unsigned char *le32)
  53. {
  54. return ((le32[3] << 24) + (le32[2] << 16) + (le32[1] << 8) + le32[0]);
  55. }
  56. /* Convert char[8] in little endian format to the host format integer
  57. */
  58. static inline unsigned long long le64_to_int(unsigned char *le64)
  59. {
  60. return (((unsigned long long)le64[7] << 56) +
  61. ((unsigned long long)le64[6] << 48) +
  62. ((unsigned long long)le64[5] << 40) +
  63. ((unsigned long long)le64[4] << 32) +
  64. ((unsigned long long)le64[3] << 24) +
  65. ((unsigned long long)le64[2] << 16) +
  66. ((unsigned long long)le64[1] << 8) +
  67. (unsigned long long)le64[0]);
  68. }
  69. /**
  70. * efi_crc32() - EFI version of crc32 function
  71. * @buf: buffer to calculate crc32 of
  72. * @len - length of buf
  73. *
  74. * Description: Returns EFI-style CRC32 value for @buf
  75. */
  76. static inline unsigned long efi_crc32(const void *buf, unsigned long len)
  77. {
  78. return crc32(0, buf, len);
  79. }
  80. /*
  81. * Private function prototypes
  82. */
  83. static int pmbr_part_valid(struct partition *part);
  84. static int is_pmbr_valid(legacy_mbr * mbr);
  85. static int is_gpt_valid(block_dev_desc_t * dev_desc, unsigned long long lba,
  86. gpt_header * pgpt_head, gpt_entry ** pgpt_pte);
  87. static gpt_entry *alloc_read_gpt_entries(block_dev_desc_t * dev_desc,
  88. gpt_header * pgpt_head);
  89. static int is_pte_valid(gpt_entry * pte);
  90. static char *print_efiname(gpt_entry *pte)
  91. {
  92. static char name[PARTNAME_SZ + 1];
  93. int i;
  94. for (i = 0; i < PARTNAME_SZ; i++) {
  95. u8 c;
  96. c = pte->partition_name[i] & 0xff;
  97. c = (c && !isprint(c)) ? '.' : c;
  98. name[i] = c;
  99. }
  100. name[PARTNAME_SZ] = 0;
  101. return name;
  102. }
  103. static void uuid_string(unsigned char *uuid, char *str)
  104. {
  105. static const u8 le[16] = {3, 2, 1, 0, 5, 4, 7, 6, 8, 9, 10, 11,
  106. 12, 13, 14, 15};
  107. int i;
  108. for (i = 0; i < 16; i++) {
  109. sprintf(str, "%02x", uuid[le[i]]);
  110. str += 2;
  111. switch (i) {
  112. case 3:
  113. case 5:
  114. case 7:
  115. case 9:
  116. *str++ = '-';
  117. break;
  118. }
  119. }
  120. }
  121. static efi_guid_t system_guid = PARTITION_SYSTEM_GUID;
  122. static inline int is_bootable(gpt_entry *p)
  123. {
  124. return p->attributes.fields.legacy_bios_bootable ||
  125. !memcmp(&(p->partition_type_guid), &system_guid,
  126. sizeof(efi_guid_t));
  127. }
  128. /*
  129. * Public Functions (include/part.h)
  130. */
  131. void print_part_efi(block_dev_desc_t * dev_desc)
  132. {
  133. ALLOC_CACHE_ALIGN_BUFFER(gpt_header, gpt_head, 1);
  134. gpt_entry *gpt_pte = NULL;
  135. int i = 0;
  136. char uuid[37];
  137. if (!dev_desc) {
  138. printf("%s: Invalid Argument(s)\n", __func__);
  139. return;
  140. }
  141. /* This function validates AND fills in the GPT header and PTE */
  142. if (is_gpt_valid(dev_desc, GPT_PRIMARY_PARTITION_TABLE_LBA,
  143. gpt_head, &gpt_pte) != 1) {
  144. printf("%s: *** ERROR: Invalid GPT ***\n", __func__);
  145. return;
  146. }
  147. debug("%s: gpt-entry at %p\n", __func__, gpt_pte);
  148. printf("Part\tStart LBA\tEnd LBA\t\tName\n");
  149. printf("\tAttributes\n");
  150. printf("\tType UUID\n");
  151. printf("\tPartition UUID\n");
  152. for (i = 0; i < le32_to_int(gpt_head->num_partition_entries); i++) {
  153. /* Stop at the first non valid PTE */
  154. if (!is_pte_valid(&gpt_pte[i]))
  155. break;
  156. printf("%3d\t0x%08llx\t0x%08llx\t\"%s\"\n", (i + 1),
  157. le64_to_int(gpt_pte[i].starting_lba),
  158. le64_to_int(gpt_pte[i].ending_lba),
  159. print_efiname(&gpt_pte[i]));
  160. printf("\tattrs:\t0x%016llx\n", gpt_pte[i].attributes.raw);
  161. uuid_string(gpt_pte[i].partition_type_guid.b, uuid);
  162. printf("\ttype:\t%s\n", uuid);
  163. uuid_string(gpt_pte[i].unique_partition_guid.b, uuid);
  164. printf("\tuuid:\t%s\n", uuid);
  165. }
  166. /* Remember to free pte */
  167. free(gpt_pte);
  168. return;
  169. }
  170. int get_partition_info_efi(block_dev_desc_t * dev_desc, int part,
  171. disk_partition_t * info)
  172. {
  173. ALLOC_CACHE_ALIGN_BUFFER(gpt_header, gpt_head, 1);
  174. gpt_entry *gpt_pte = NULL;
  175. /* "part" argument must be at least 1 */
  176. if (!dev_desc || !info || part < 1) {
  177. printf("%s: Invalid Argument(s)\n", __func__);
  178. return -1;
  179. }
  180. /* This function validates AND fills in the GPT header and PTE */
  181. if (is_gpt_valid(dev_desc, GPT_PRIMARY_PARTITION_TABLE_LBA,
  182. gpt_head, &gpt_pte) != 1) {
  183. printf("%s: *** ERROR: Invalid GPT ***\n", __func__);
  184. return -1;
  185. }
  186. if (part > le32_to_int(gpt_head->num_partition_entries) ||
  187. !is_pte_valid(&gpt_pte[part - 1])) {
  188. printf("%s: *** ERROR: Invalid partition number %d ***\n",
  189. __func__, part);
  190. return -1;
  191. }
  192. /* The ulong casting limits the maximum disk size to 2 TB */
  193. info->start = (ulong) le64_to_int(gpt_pte[part - 1].starting_lba);
  194. /* The ending LBA is inclusive, to calculate size, add 1 to it */
  195. info->size = ((ulong)le64_to_int(gpt_pte[part - 1].ending_lba) + 1)
  196. - info->start;
  197. info->blksz = GPT_BLOCK_SIZE;
  198. sprintf((char *)info->name, "%s",
  199. print_efiname(&gpt_pte[part - 1]));
  200. sprintf((char *)info->type, "U-Boot");
  201. info->bootable = is_bootable(&gpt_pte[part - 1]);
  202. #ifdef CONFIG_PARTITION_UUIDS
  203. uuid_string(gpt_pte[part - 1].unique_partition_guid.b, info->uuid);
  204. #endif
  205. debug("%s: start 0x%lX, size 0x%lX, name %s", __func__,
  206. info->start, info->size, info->name);
  207. /* Remember to free pte */
  208. free(gpt_pte);
  209. return 0;
  210. }
  211. int test_part_efi(block_dev_desc_t * dev_desc)
  212. {
  213. ALLOC_CACHE_ALIGN_BUFFER(legacy_mbr, legacymbr, 1);
  214. /* Read legacy MBR from block 0 and validate it */
  215. if ((dev_desc->block_read(dev_desc->dev, 0, 1, (ulong *)legacymbr) != 1)
  216. || (is_pmbr_valid(legacymbr) != 1)) {
  217. return -1;
  218. }
  219. return 0;
  220. }
  221. /*
  222. * Private functions
  223. */
  224. /*
  225. * pmbr_part_valid(): Check for EFI partition signature
  226. *
  227. * Returns: 1 if EFI GPT partition type is found.
  228. */
  229. static int pmbr_part_valid(struct partition *part)
  230. {
  231. if (part->sys_ind == EFI_PMBR_OSTYPE_EFI_GPT &&
  232. le32_to_int(part->start_sect) == 1UL) {
  233. return 1;
  234. }
  235. return 0;
  236. }
  237. /*
  238. * is_pmbr_valid(): test Protective MBR for validity
  239. *
  240. * Returns: 1 if PMBR is valid, 0 otherwise.
  241. * Validity depends on two things:
  242. * 1) MSDOS signature is in the last two bytes of the MBR
  243. * 2) One partition of type 0xEE is found, checked by pmbr_part_valid()
  244. */
  245. static int is_pmbr_valid(legacy_mbr * mbr)
  246. {
  247. int i = 0;
  248. if (!mbr || le16_to_int(mbr->signature) != MSDOS_MBR_SIGNATURE) {
  249. return 0;
  250. }
  251. for (i = 0; i < 4; i++) {
  252. if (pmbr_part_valid(&mbr->partition_record[i])) {
  253. return 1;
  254. }
  255. }
  256. return 0;
  257. }
  258. /**
  259. * is_gpt_valid() - tests one GPT header and PTEs for validity
  260. *
  261. * lba is the logical block address of the GPT header to test
  262. * gpt is a GPT header ptr, filled on return.
  263. * ptes is a PTEs ptr, filled on return.
  264. *
  265. * Description: returns 1 if valid, 0 on error.
  266. * If valid, returns pointers to PTEs.
  267. */
  268. static int is_gpt_valid(block_dev_desc_t * dev_desc, unsigned long long lba,
  269. gpt_header * pgpt_head, gpt_entry ** pgpt_pte)
  270. {
  271. unsigned char crc32_backup[4] = { 0 };
  272. unsigned long calc_crc32;
  273. unsigned long long lastlba;
  274. if (!dev_desc || !pgpt_head) {
  275. printf("%s: Invalid Argument(s)\n", __func__);
  276. return 0;
  277. }
  278. /* Read GPT Header from device */
  279. if (dev_desc->block_read(dev_desc->dev, lba, 1, pgpt_head) != 1) {
  280. printf("*** ERROR: Can't read GPT header ***\n");
  281. return 0;
  282. }
  283. /* Check the GPT header signature */
  284. if (le64_to_int(pgpt_head->signature) != GPT_HEADER_SIGNATURE) {
  285. printf("GUID Partition Table Header signature is wrong:"
  286. "0x%llX != 0x%llX\n",
  287. (unsigned long long)le64_to_int(pgpt_head->signature),
  288. (unsigned long long)GPT_HEADER_SIGNATURE);
  289. return 0;
  290. }
  291. /* Check the GUID Partition Table CRC */
  292. memcpy(crc32_backup, pgpt_head->header_crc32, sizeof(crc32_backup));
  293. memset(pgpt_head->header_crc32, 0, sizeof(pgpt_head->header_crc32));
  294. calc_crc32 = efi_crc32((const unsigned char *)pgpt_head,
  295. le32_to_int(pgpt_head->header_size));
  296. memcpy(pgpt_head->header_crc32, crc32_backup, sizeof(crc32_backup));
  297. if (calc_crc32 != le32_to_int(crc32_backup)) {
  298. printf("GUID Partition Table Header CRC is wrong:"
  299. "0x%08lX != 0x%08lX\n",
  300. le32_to_int(crc32_backup), calc_crc32);
  301. return 0;
  302. }
  303. /* Check that the my_lba entry points to the LBA that contains the GPT */
  304. if (le64_to_int(pgpt_head->my_lba) != lba) {
  305. printf("GPT: my_lba incorrect: %llX != %llX\n",
  306. (unsigned long long)le64_to_int(pgpt_head->my_lba),
  307. (unsigned long long)lba);
  308. return 0;
  309. }
  310. /* Check the first_usable_lba and last_usable_lba are within the disk. */
  311. lastlba = (unsigned long long)dev_desc->lba;
  312. if (le64_to_int(pgpt_head->first_usable_lba) > lastlba) {
  313. printf("GPT: first_usable_lba incorrect: %llX > %llX\n",
  314. le64_to_int(pgpt_head->first_usable_lba), lastlba);
  315. return 0;
  316. }
  317. if (le64_to_int(pgpt_head->last_usable_lba) > lastlba) {
  318. printf("GPT: last_usable_lba incorrect: %llX > %llX\n",
  319. le64_to_int(pgpt_head->last_usable_lba), lastlba);
  320. return 0;
  321. }
  322. debug("GPT: first_usable_lba: %llX last_usable_lba %llX last lba %llX\n",
  323. le64_to_int(pgpt_head->first_usable_lba),
  324. le64_to_int(pgpt_head->last_usable_lba), lastlba);
  325. /* Read and allocate Partition Table Entries */
  326. *pgpt_pte = alloc_read_gpt_entries(dev_desc, pgpt_head);
  327. if (*pgpt_pte == NULL) {
  328. printf("GPT: Failed to allocate memory for PTE\n");
  329. return 0;
  330. }
  331. /* Check the GUID Partition Table Entry Array CRC */
  332. calc_crc32 = efi_crc32((const unsigned char *)*pgpt_pte,
  333. le32_to_int(pgpt_head->num_partition_entries) *
  334. le32_to_int(pgpt_head->sizeof_partition_entry));
  335. if (calc_crc32 != le32_to_int(pgpt_head->partition_entry_array_crc32)) {
  336. printf("GUID Partition Table Entry Array CRC is wrong:"
  337. "0x%08lX != 0x%08lX\n",
  338. le32_to_int(pgpt_head->partition_entry_array_crc32),
  339. calc_crc32);
  340. free(*pgpt_pte);
  341. return 0;
  342. }
  343. /* We're done, all's well */
  344. return 1;
  345. }
  346. /**
  347. * alloc_read_gpt_entries(): reads partition entries from disk
  348. * @dev_desc
  349. * @gpt - GPT header
  350. *
  351. * Description: Returns ptes on success, NULL on error.
  352. * Allocates space for PTEs based on information found in @gpt.
  353. * Notes: remember to free pte when you're done!
  354. */
  355. static gpt_entry *alloc_read_gpt_entries(block_dev_desc_t * dev_desc,
  356. gpt_header * pgpt_head)
  357. {
  358. size_t count = 0;
  359. gpt_entry *pte = NULL;
  360. if (!dev_desc || !pgpt_head) {
  361. printf("%s: Invalid Argument(s)\n", __func__);
  362. return NULL;
  363. }
  364. count = le32_to_int(pgpt_head->num_partition_entries) *
  365. le32_to_int(pgpt_head->sizeof_partition_entry);
  366. debug("%s: count = %lu * %lu = %u\n", __func__,
  367. le32_to_int(pgpt_head->num_partition_entries),
  368. le32_to_int(pgpt_head->sizeof_partition_entry), count);
  369. /* Allocate memory for PTE, remember to FREE */
  370. if (count != 0) {
  371. pte = memalign(ARCH_DMA_MINALIGN, count);
  372. }
  373. if (count == 0 || pte == NULL) {
  374. printf("%s: ERROR: Can't allocate 0x%X bytes for GPT Entries\n",
  375. __func__, count);
  376. return NULL;
  377. }
  378. /* Read GPT Entries from device */
  379. if (dev_desc->block_read (dev_desc->dev,
  380. (unsigned long)le64_to_int(pgpt_head->partition_entry_lba),
  381. (lbaint_t) (count / GPT_BLOCK_SIZE), pte)
  382. != (count / GPT_BLOCK_SIZE)) {
  383. printf("*** ERROR: Can't read GPT Entries ***\n");
  384. free(pte);
  385. return NULL;
  386. }
  387. return pte;
  388. }
  389. /**
  390. * is_pte_valid(): validates a single Partition Table Entry
  391. * @gpt_entry - Pointer to a single Partition Table Entry
  392. *
  393. * Description: returns 1 if valid, 0 on error.
  394. */
  395. static int is_pte_valid(gpt_entry * pte)
  396. {
  397. efi_guid_t unused_guid;
  398. if (!pte) {
  399. printf("%s: Invalid Argument(s)\n", __func__);
  400. return 0;
  401. }
  402. /* Only one validation for now:
  403. * The GUID Partition Type != Unused Entry (ALL-ZERO)
  404. */
  405. memset(unused_guid.b, 0, sizeof(unused_guid.b));
  406. if (memcmp(pte->partition_type_guid.b, unused_guid.b,
  407. sizeof(unused_guid.b)) == 0) {
  408. debug("%s: Found an unused PTE GUID at 0x%08X\n", __func__,
  409. (unsigned int)pte);
  410. return 0;
  411. } else {
  412. return 1;
  413. }
  414. }
  415. #endif