part_efi.c 12 KB

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