efi.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  1. /************************************************************
  2. * EFI GUID Partition Table handling
  3. *
  4. * http://www.uefi.org/specs/
  5. * http://www.intel.com/technology/efi/
  6. *
  7. * efi.[ch] by Matt Domsch <Matt_Domsch@dell.com>
  8. * Copyright 2000,2001,2002,2004 Dell Inc.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. *
  24. *
  25. * TODO:
  26. *
  27. * Changelog:
  28. * Mon August 5th, 2013 Davidlohr Bueso <davidlohr@hp.com>
  29. * - detect hybrid MBRs, tighter pMBR checking & cleanups.
  30. *
  31. * Mon Nov 09 2004 Matt Domsch <Matt_Domsch@dell.com>
  32. * - test for valid PMBR and valid PGPT before ever reading
  33. * AGPT, allow override with 'gpt' kernel command line option.
  34. * - check for first/last_usable_lba outside of size of disk
  35. *
  36. * Tue Mar 26 2002 Matt Domsch <Matt_Domsch@dell.com>
  37. * - Ported to 2.5.7-pre1 and 2.5.7-dj2
  38. * - Applied patch to avoid fault in alternate header handling
  39. * - cleaned up find_valid_gpt
  40. * - On-disk structure and copy in memory is *always* LE now -
  41. * swab fields as needed
  42. * - remove print_gpt_header()
  43. * - only use first max_p partition entries, to keep the kernel minor number
  44. * and partition numbers tied.
  45. *
  46. * Mon Feb 04 2002 Matt Domsch <Matt_Domsch@dell.com>
  47. * - Removed __PRIPTR_PREFIX - not being used
  48. *
  49. * Mon Jan 14 2002 Matt Domsch <Matt_Domsch@dell.com>
  50. * - Ported to 2.5.2-pre11 + library crc32 patch Linus applied
  51. *
  52. * Thu Dec 6 2001 Matt Domsch <Matt_Domsch@dell.com>
  53. * - Added compare_gpts().
  54. * - moved le_efi_guid_to_cpus() back into this file. GPT is the only
  55. * thing that keeps EFI GUIDs on disk.
  56. * - Changed gpt structure names and members to be simpler and more Linux-like.
  57. *
  58. * Wed Oct 17 2001 Matt Domsch <Matt_Domsch@dell.com>
  59. * - Removed CONFIG_DEVFS_VOLUMES_UUID code entirely per Martin Wilck
  60. *
  61. * Wed Oct 10 2001 Matt Domsch <Matt_Domsch@dell.com>
  62. * - Changed function comments to DocBook style per Andreas Dilger suggestion.
  63. *
  64. * Mon Oct 08 2001 Matt Domsch <Matt_Domsch@dell.com>
  65. * - Change read_lba() to use the page cache per Al Viro's work.
  66. * - print u64s properly on all architectures
  67. * - fixed debug_printk(), now Dprintk()
  68. *
  69. * Mon Oct 01 2001 Matt Domsch <Matt_Domsch@dell.com>
  70. * - Style cleanups
  71. * - made most functions static
  72. * - Endianness addition
  73. * - remove test for second alternate header, as it's not per spec,
  74. * and is unnecessary. There's now a method to read/write the last
  75. * sector of an odd-sized disk from user space. No tools have ever
  76. * been released which used this code, so it's effectively dead.
  77. * - Per Asit Mallick of Intel, added a test for a valid PMBR.
  78. * - Added kernel command line option 'gpt' to override valid PMBR test.
  79. *
  80. * Wed Jun 6 2001 Martin Wilck <Martin.Wilck@Fujitsu-Siemens.com>
  81. * - added devfs volume UUID support (/dev/volumes/uuids) for
  82. * mounting file systems by the partition GUID.
  83. *
  84. * Tue Dec 5 2000 Matt Domsch <Matt_Domsch@dell.com>
  85. * - Moved crc32() to linux/lib, added efi_crc32().
  86. *
  87. * Thu Nov 30 2000 Matt Domsch <Matt_Domsch@dell.com>
  88. * - Replaced Intel's CRC32 function with an equivalent
  89. * non-license-restricted version.
  90. *
  91. * Wed Oct 25 2000 Matt Domsch <Matt_Domsch@dell.com>
  92. * - Fixed the last_lba() call to return the proper last block
  93. *
  94. * Thu Oct 12 2000 Matt Domsch <Matt_Domsch@dell.com>
  95. * - Thanks to Andries Brouwer for his debugging assistance.
  96. * - Code works, detects all the partitions.
  97. *
  98. ************************************************************/
  99. #include <linux/kernel.h>
  100. #include <linux/crc32.h>
  101. #include <linux/ctype.h>
  102. #include <linux/math64.h>
  103. #include <linux/slab.h>
  104. #include "check.h"
  105. #include "efi.h"
  106. /* This allows a kernel command line option 'gpt' to override
  107. * the test for invalid PMBR. Not __initdata because reloading
  108. * the partition tables happens after init too.
  109. */
  110. static int force_gpt;
  111. static int __init
  112. force_gpt_fn(char *str)
  113. {
  114. force_gpt = 1;
  115. return 1;
  116. }
  117. __setup("gpt", force_gpt_fn);
  118. /**
  119. * efi_crc32() - EFI version of crc32 function
  120. * @buf: buffer to calculate crc32 of
  121. * @len - length of buf
  122. *
  123. * Description: Returns EFI-style CRC32 value for @buf
  124. *
  125. * This function uses the little endian Ethernet polynomial
  126. * but seeds the function with ~0, and xor's with ~0 at the end.
  127. * Note, the EFI Specification, v1.02, has a reference to
  128. * Dr. Dobbs Journal, May 1994 (actually it's in May 1992).
  129. */
  130. static inline u32
  131. efi_crc32(const void *buf, unsigned long len)
  132. {
  133. return (crc32(~0L, buf, len) ^ ~0L);
  134. }
  135. /**
  136. * last_lba(): return number of last logical block of device
  137. * @bdev: block device
  138. *
  139. * Description: Returns last LBA value on success, 0 on error.
  140. * This is stored (by sd and ide-geometry) in
  141. * the part[0] entry for this disk, and is the number of
  142. * physical sectors available on the disk.
  143. */
  144. static u64 last_lba(struct block_device *bdev)
  145. {
  146. if (!bdev || !bdev->bd_inode)
  147. return 0;
  148. return div_u64(bdev->bd_inode->i_size,
  149. bdev_logical_block_size(bdev)) - 1ULL;
  150. }
  151. static inline int pmbr_part_valid(gpt_mbr_record *part)
  152. {
  153. if (part->os_type != EFI_PMBR_OSTYPE_EFI_GPT)
  154. goto invalid;
  155. /* set to 0x00000001 (i.e., the LBA of the GPT Partition Header) */
  156. if (le32_to_cpu(part->starting_lba) != GPT_PRIMARY_PARTITION_TABLE_LBA)
  157. goto invalid;
  158. return GPT_MBR_PROTECTIVE;
  159. invalid:
  160. return 0;
  161. }
  162. /**
  163. * is_pmbr_valid(): test Protective MBR for validity
  164. * @mbr: pointer to a legacy mbr structure
  165. * @total_sectors: amount of sectors in the device
  166. *
  167. * Description: Checks for a valid protective or hybrid
  168. * master boot record (MBR). The validity of a pMBR depends
  169. * on all of the following properties:
  170. * 1) MSDOS signature is in the last two bytes of the MBR
  171. * 2) One partition of type 0xEE is found
  172. *
  173. * In addition, a hybrid MBR will have up to three additional
  174. * primary partitions, which point to the same space that's
  175. * marked out by up to three GPT partitions.
  176. *
  177. * Returns 0 upon invalid MBR, or GPT_MBR_PROTECTIVE or
  178. * GPT_MBR_HYBRID depending on the device layout.
  179. */
  180. static int is_pmbr_valid(legacy_mbr *mbr, sector_t total_sectors)
  181. {
  182. uint32_t sz = 0;
  183. int i, part = 0, ret = 0; /* invalid by default */
  184. if (!mbr || le16_to_cpu(mbr->signature) != MSDOS_MBR_SIGNATURE)
  185. goto done;
  186. for (i = 0; i < 4; i++) {
  187. ret = pmbr_part_valid(&mbr->partition_record[i]);
  188. if (ret == GPT_MBR_PROTECTIVE) {
  189. part = i;
  190. /*
  191. * Ok, we at least know that there's a protective MBR,
  192. * now check if there are other partition types for
  193. * hybrid MBR.
  194. */
  195. goto check_hybrid;
  196. }
  197. }
  198. if (ret != GPT_MBR_PROTECTIVE)
  199. goto done;
  200. check_hybrid:
  201. for (i = 0; i < 4; i++)
  202. if ((mbr->partition_record[i].os_type !=
  203. EFI_PMBR_OSTYPE_EFI_GPT) &&
  204. (mbr->partition_record[i].os_type != 0x00))
  205. ret = GPT_MBR_HYBRID;
  206. /*
  207. * Protective MBRs take up the lesser of the whole disk
  208. * or 2 TiB (32bit LBA), ignoring the rest of the disk.
  209. * Some partitioning programs, nonetheless, choose to set
  210. * the size to the maximum 32-bit limitation, disregarding
  211. * the disk size.
  212. *
  213. * Hybrid MBRs do not necessarily comply with this.
  214. *
  215. * Consider a bad value here to be a warning to support dd'ing
  216. * an image from a smaller disk to a larger disk.
  217. */
  218. if (ret == GPT_MBR_PROTECTIVE) {
  219. sz = le32_to_cpu(mbr->partition_record[part].size_in_lba);
  220. if (sz != (uint32_t) total_sectors - 1 && sz != 0xFFFFFFFF)
  221. pr_debug("GPT: mbr size in lba (%u) different than whole disk (%u).\n",
  222. sz, min_t(uint32_t,
  223. total_sectors - 1, 0xFFFFFFFF));
  224. }
  225. done:
  226. return ret;
  227. }
  228. /**
  229. * read_lba(): Read bytes from disk, starting at given LBA
  230. * @state
  231. * @lba
  232. * @buffer
  233. * @size_t
  234. *
  235. * Description: Reads @count bytes from @state->bdev into @buffer.
  236. * Returns number of bytes read on success, 0 on error.
  237. */
  238. static size_t read_lba(struct parsed_partitions *state,
  239. u64 lba, u8 *buffer, size_t count)
  240. {
  241. size_t totalreadcount = 0;
  242. struct block_device *bdev = state->bdev;
  243. sector_t n = lba * (bdev_logical_block_size(bdev) / 512);
  244. if (!buffer || lba > last_lba(bdev))
  245. return 0;
  246. while (count) {
  247. int copied = 512;
  248. Sector sect;
  249. unsigned char *data = read_part_sector(state, n++, &sect);
  250. if (!data)
  251. break;
  252. if (copied > count)
  253. copied = count;
  254. memcpy(buffer, data, copied);
  255. put_dev_sector(sect);
  256. buffer += copied;
  257. totalreadcount +=copied;
  258. count -= copied;
  259. }
  260. return totalreadcount;
  261. }
  262. /**
  263. * alloc_read_gpt_entries(): reads partition entries from disk
  264. * @state
  265. * @gpt - GPT header
  266. *
  267. * Description: Returns ptes on success, NULL on error.
  268. * Allocates space for PTEs based on information found in @gpt.
  269. * Notes: remember to free pte when you're done!
  270. */
  271. static gpt_entry *alloc_read_gpt_entries(struct parsed_partitions *state,
  272. gpt_header *gpt)
  273. {
  274. size_t count;
  275. gpt_entry *pte;
  276. if (!gpt)
  277. return NULL;
  278. count = le32_to_cpu(gpt->num_partition_entries) *
  279. le32_to_cpu(gpt->sizeof_partition_entry);
  280. if (!count)
  281. return NULL;
  282. pte = kmalloc(count, GFP_KERNEL);
  283. if (!pte)
  284. return NULL;
  285. if (read_lba(state, le64_to_cpu(gpt->partition_entry_lba),
  286. (u8 *) pte, count) < count) {
  287. kfree(pte);
  288. pte=NULL;
  289. return NULL;
  290. }
  291. return pte;
  292. }
  293. /**
  294. * alloc_read_gpt_header(): Allocates GPT header, reads into it from disk
  295. * @state
  296. * @lba is the Logical Block Address of the partition table
  297. *
  298. * Description: returns GPT header on success, NULL on error. Allocates
  299. * and fills a GPT header starting at @ from @state->bdev.
  300. * Note: remember to free gpt when finished with it.
  301. */
  302. static gpt_header *alloc_read_gpt_header(struct parsed_partitions *state,
  303. u64 lba)
  304. {
  305. gpt_header *gpt;
  306. unsigned ssz = bdev_logical_block_size(state->bdev);
  307. gpt = kmalloc(ssz, GFP_KERNEL);
  308. if (!gpt)
  309. return NULL;
  310. if (read_lba(state, lba, (u8 *) gpt, ssz) < ssz) {
  311. kfree(gpt);
  312. gpt=NULL;
  313. return NULL;
  314. }
  315. return gpt;
  316. }
  317. /**
  318. * is_gpt_valid() - tests one GPT header and PTEs for validity
  319. * @state
  320. * @lba is the logical block address of the GPT header to test
  321. * @gpt is a GPT header ptr, filled on return.
  322. * @ptes is a PTEs ptr, filled on return.
  323. *
  324. * Description: returns 1 if valid, 0 on error.
  325. * If valid, returns pointers to newly allocated GPT header and PTEs.
  326. */
  327. static int is_gpt_valid(struct parsed_partitions *state, u64 lba,
  328. gpt_header **gpt, gpt_entry **ptes)
  329. {
  330. u32 crc, origcrc;
  331. u64 lastlba;
  332. if (!ptes)
  333. return 0;
  334. if (!(*gpt = alloc_read_gpt_header(state, lba)))
  335. return 0;
  336. /* Check the GUID Partition Table signature */
  337. if (le64_to_cpu((*gpt)->signature) != GPT_HEADER_SIGNATURE) {
  338. pr_debug("GUID Partition Table Header signature is wrong:"
  339. "%lld != %lld\n",
  340. (unsigned long long)le64_to_cpu((*gpt)->signature),
  341. (unsigned long long)GPT_HEADER_SIGNATURE);
  342. goto fail;
  343. }
  344. /* Check the GUID Partition Table header size is too big */
  345. if (le32_to_cpu((*gpt)->header_size) >
  346. bdev_logical_block_size(state->bdev)) {
  347. pr_debug("GUID Partition Table Header size is too large: %u > %u\n",
  348. le32_to_cpu((*gpt)->header_size),
  349. bdev_logical_block_size(state->bdev));
  350. goto fail;
  351. }
  352. /* Check the GUID Partition Table header size is too small */
  353. if (le32_to_cpu((*gpt)->header_size) < sizeof(gpt_header)) {
  354. pr_debug("GUID Partition Table Header size is too small: %u < %zu\n",
  355. le32_to_cpu((*gpt)->header_size),
  356. sizeof(gpt_header));
  357. goto fail;
  358. }
  359. /* Check the GUID Partition Table CRC */
  360. origcrc = le32_to_cpu((*gpt)->header_crc32);
  361. (*gpt)->header_crc32 = 0;
  362. crc = efi_crc32((const unsigned char *) (*gpt), le32_to_cpu((*gpt)->header_size));
  363. if (crc != origcrc) {
  364. pr_debug("GUID Partition Table Header CRC is wrong: %x != %x\n",
  365. crc, origcrc);
  366. goto fail;
  367. }
  368. (*gpt)->header_crc32 = cpu_to_le32(origcrc);
  369. /* Check that the my_lba entry points to the LBA that contains
  370. * the GUID Partition Table */
  371. if (le64_to_cpu((*gpt)->my_lba) != lba) {
  372. pr_debug("GPT my_lba incorrect: %lld != %lld\n",
  373. (unsigned long long)le64_to_cpu((*gpt)->my_lba),
  374. (unsigned long long)lba);
  375. goto fail;
  376. }
  377. /* Check the first_usable_lba and last_usable_lba are
  378. * within the disk.
  379. */
  380. lastlba = last_lba(state->bdev);
  381. if (le64_to_cpu((*gpt)->first_usable_lba) > lastlba) {
  382. pr_debug("GPT: first_usable_lba incorrect: %lld > %lld\n",
  383. (unsigned long long)le64_to_cpu((*gpt)->first_usable_lba),
  384. (unsigned long long)lastlba);
  385. goto fail;
  386. }
  387. if (le64_to_cpu((*gpt)->last_usable_lba) > lastlba) {
  388. pr_debug("GPT: last_usable_lba incorrect: %lld > %lld\n",
  389. (unsigned long long)le64_to_cpu((*gpt)->last_usable_lba),
  390. (unsigned long long)lastlba);
  391. goto fail;
  392. }
  393. if (le64_to_cpu((*gpt)->last_usable_lba) < le64_to_cpu((*gpt)->first_usable_lba)) {
  394. pr_debug("GPT: last_usable_lba incorrect: %lld > %lld\n",
  395. (unsigned long long)le64_to_cpu((*gpt)->last_usable_lba),
  396. (unsigned long long)le64_to_cpu((*gpt)->first_usable_lba));
  397. goto fail;
  398. }
  399. /* Check that sizeof_partition_entry has the correct value */
  400. if (le32_to_cpu((*gpt)->sizeof_partition_entry) != sizeof(gpt_entry)) {
  401. pr_debug("GUID Partitition Entry Size check failed.\n");
  402. goto fail;
  403. }
  404. if (!(*ptes = alloc_read_gpt_entries(state, *gpt)))
  405. goto fail;
  406. /* Check the GUID Partition Entry Array CRC */
  407. crc = efi_crc32((const unsigned char *) (*ptes),
  408. le32_to_cpu((*gpt)->num_partition_entries) *
  409. le32_to_cpu((*gpt)->sizeof_partition_entry));
  410. if (crc != le32_to_cpu((*gpt)->partition_entry_array_crc32)) {
  411. pr_debug("GUID Partitition Entry Array CRC check failed.\n");
  412. goto fail_ptes;
  413. }
  414. /* We're done, all's well */
  415. return 1;
  416. fail_ptes:
  417. kfree(*ptes);
  418. *ptes = NULL;
  419. fail:
  420. kfree(*gpt);
  421. *gpt = NULL;
  422. return 0;
  423. }
  424. /**
  425. * is_pte_valid() - tests one PTE for validity
  426. * @pte is the pte to check
  427. * @lastlba is last lba of the disk
  428. *
  429. * Description: returns 1 if valid, 0 on error.
  430. */
  431. static inline int
  432. is_pte_valid(const gpt_entry *pte, const u64 lastlba)
  433. {
  434. if ((!efi_guidcmp(pte->partition_type_guid, NULL_GUID)) ||
  435. le64_to_cpu(pte->starting_lba) > lastlba ||
  436. le64_to_cpu(pte->ending_lba) > lastlba)
  437. return 0;
  438. return 1;
  439. }
  440. /**
  441. * compare_gpts() - Search disk for valid GPT headers and PTEs
  442. * @pgpt is the primary GPT header
  443. * @agpt is the alternate GPT header
  444. * @lastlba is the last LBA number
  445. * Description: Returns nothing. Sanity checks pgpt and agpt fields
  446. * and prints warnings on discrepancies.
  447. *
  448. */
  449. static void
  450. compare_gpts(gpt_header *pgpt, gpt_header *agpt, u64 lastlba)
  451. {
  452. int error_found = 0;
  453. if (!pgpt || !agpt)
  454. return;
  455. if (le64_to_cpu(pgpt->my_lba) != le64_to_cpu(agpt->alternate_lba)) {
  456. pr_warn("GPT:Primary header LBA != Alt. header alternate_lba\n");
  457. pr_warn("GPT:%lld != %lld\n",
  458. (unsigned long long)le64_to_cpu(pgpt->my_lba),
  459. (unsigned long long)le64_to_cpu(agpt->alternate_lba));
  460. error_found++;
  461. }
  462. if (le64_to_cpu(pgpt->alternate_lba) != le64_to_cpu(agpt->my_lba)) {
  463. pr_warn("GPT:Primary header alternate_lba != Alt. header my_lba\n");
  464. pr_warn("GPT:%lld != %lld\n",
  465. (unsigned long long)le64_to_cpu(pgpt->alternate_lba),
  466. (unsigned long long)le64_to_cpu(agpt->my_lba));
  467. error_found++;
  468. }
  469. if (le64_to_cpu(pgpt->first_usable_lba) !=
  470. le64_to_cpu(agpt->first_usable_lba)) {
  471. pr_warn("GPT:first_usable_lbas don't match.\n");
  472. pr_warn("GPT:%lld != %lld\n",
  473. (unsigned long long)le64_to_cpu(pgpt->first_usable_lba),
  474. (unsigned long long)le64_to_cpu(agpt->first_usable_lba));
  475. error_found++;
  476. }
  477. if (le64_to_cpu(pgpt->last_usable_lba) !=
  478. le64_to_cpu(agpt->last_usable_lba)) {
  479. pr_warn("GPT:last_usable_lbas don't match.\n");
  480. pr_warn("GPT:%lld != %lld\n",
  481. (unsigned long long)le64_to_cpu(pgpt->last_usable_lba),
  482. (unsigned long long)le64_to_cpu(agpt->last_usable_lba));
  483. error_found++;
  484. }
  485. if (efi_guidcmp(pgpt->disk_guid, agpt->disk_guid)) {
  486. pr_warn("GPT:disk_guids don't match.\n");
  487. error_found++;
  488. }
  489. if (le32_to_cpu(pgpt->num_partition_entries) !=
  490. le32_to_cpu(agpt->num_partition_entries)) {
  491. pr_warn("GPT:num_partition_entries don't match: "
  492. "0x%x != 0x%x\n",
  493. le32_to_cpu(pgpt->num_partition_entries),
  494. le32_to_cpu(agpt->num_partition_entries));
  495. error_found++;
  496. }
  497. if (le32_to_cpu(pgpt->sizeof_partition_entry) !=
  498. le32_to_cpu(agpt->sizeof_partition_entry)) {
  499. pr_warn("GPT:sizeof_partition_entry values don't match: "
  500. "0x%x != 0x%x\n",
  501. le32_to_cpu(pgpt->sizeof_partition_entry),
  502. le32_to_cpu(agpt->sizeof_partition_entry));
  503. error_found++;
  504. }
  505. if (le32_to_cpu(pgpt->partition_entry_array_crc32) !=
  506. le32_to_cpu(agpt->partition_entry_array_crc32)) {
  507. pr_warn("GPT:partition_entry_array_crc32 values don't match: "
  508. "0x%x != 0x%x\n",
  509. le32_to_cpu(pgpt->partition_entry_array_crc32),
  510. le32_to_cpu(agpt->partition_entry_array_crc32));
  511. error_found++;
  512. }
  513. if (le64_to_cpu(pgpt->alternate_lba) != lastlba) {
  514. pr_warn("GPT:Primary header thinks Alt. header is not at the end of the disk.\n");
  515. pr_warn("GPT:%lld != %lld\n",
  516. (unsigned long long)le64_to_cpu(pgpt->alternate_lba),
  517. (unsigned long long)lastlba);
  518. error_found++;
  519. }
  520. if (le64_to_cpu(agpt->my_lba) != lastlba) {
  521. pr_warn("GPT:Alternate GPT header not at the end of the disk.\n");
  522. pr_warn("GPT:%lld != %lld\n",
  523. (unsigned long long)le64_to_cpu(agpt->my_lba),
  524. (unsigned long long)lastlba);
  525. error_found++;
  526. }
  527. if (error_found)
  528. pr_warn("GPT: Use GNU Parted to correct GPT errors.\n");
  529. return;
  530. }
  531. /**
  532. * find_valid_gpt() - Search disk for valid GPT headers and PTEs
  533. * @state
  534. * @gpt is a GPT header ptr, filled on return.
  535. * @ptes is a PTEs ptr, filled on return.
  536. * Description: Returns 1 if valid, 0 on error.
  537. * If valid, returns pointers to newly allocated GPT header and PTEs.
  538. * Validity depends on PMBR being valid (or being overridden by the
  539. * 'gpt' kernel command line option) and finding either the Primary
  540. * GPT header and PTEs valid, or the Alternate GPT header and PTEs
  541. * valid. If the Primary GPT header is not valid, the Alternate GPT header
  542. * is not checked unless the 'gpt' kernel command line option is passed.
  543. * This protects against devices which misreport their size, and forces
  544. * the user to decide to use the Alternate GPT.
  545. */
  546. static int find_valid_gpt(struct parsed_partitions *state, gpt_header **gpt,
  547. gpt_entry **ptes)
  548. {
  549. int good_pgpt = 0, good_agpt = 0, good_pmbr = 0;
  550. gpt_header *pgpt = NULL, *agpt = NULL;
  551. gpt_entry *pptes = NULL, *aptes = NULL;
  552. legacy_mbr *legacymbr;
  553. sector_t total_sectors = i_size_read(state->bdev->bd_inode) >> 9;
  554. u64 lastlba;
  555. if (!ptes)
  556. return 0;
  557. lastlba = last_lba(state->bdev);
  558. if (!force_gpt) {
  559. /* This will be added to the EFI Spec. per Intel after v1.02. */
  560. legacymbr = kzalloc(sizeof(*legacymbr), GFP_KERNEL);
  561. if (!legacymbr)
  562. goto fail;
  563. read_lba(state, 0, (u8 *)legacymbr, sizeof(*legacymbr));
  564. good_pmbr = is_pmbr_valid(legacymbr, total_sectors);
  565. kfree(legacymbr);
  566. if (!good_pmbr)
  567. goto fail;
  568. pr_debug("Device has a %s MBR\n",
  569. good_pmbr == GPT_MBR_PROTECTIVE ?
  570. "protective" : "hybrid");
  571. }
  572. good_pgpt = is_gpt_valid(state, GPT_PRIMARY_PARTITION_TABLE_LBA,
  573. &pgpt, &pptes);
  574. if (good_pgpt)
  575. good_agpt = is_gpt_valid(state,
  576. le64_to_cpu(pgpt->alternate_lba),
  577. &agpt, &aptes);
  578. if (!good_agpt && force_gpt)
  579. good_agpt = is_gpt_valid(state, lastlba, &agpt, &aptes);
  580. /* The obviously unsuccessful case */
  581. if (!good_pgpt && !good_agpt)
  582. goto fail;
  583. compare_gpts(pgpt, agpt, lastlba);
  584. /* The good cases */
  585. if (good_pgpt) {
  586. *gpt = pgpt;
  587. *ptes = pptes;
  588. kfree(agpt);
  589. kfree(aptes);
  590. if (!good_agpt)
  591. pr_warn("Alternate GPT is invalid, using primary GPT.\n");
  592. return 1;
  593. }
  594. else if (good_agpt) {
  595. *gpt = agpt;
  596. *ptes = aptes;
  597. kfree(pgpt);
  598. kfree(pptes);
  599. pr_warn("Primary GPT is invalid, using alternate GPT.\n");
  600. return 1;
  601. }
  602. fail:
  603. kfree(pgpt);
  604. kfree(agpt);
  605. kfree(pptes);
  606. kfree(aptes);
  607. *gpt = NULL;
  608. *ptes = NULL;
  609. return 0;
  610. }
  611. /**
  612. * efi_partition(struct parsed_partitions *state)
  613. * @state
  614. *
  615. * Description: called from check.c, if the disk contains GPT
  616. * partitions, sets up partition entries in the kernel.
  617. *
  618. * If the first block on the disk is a legacy MBR,
  619. * it will get handled by msdos_partition().
  620. * If it's a Protective MBR, we'll handle it here.
  621. *
  622. * We do not create a Linux partition for GPT, but
  623. * only for the actual data partitions.
  624. * Returns:
  625. * -1 if unable to read the partition table
  626. * 0 if this isn't our partition table
  627. * 1 if successful
  628. *
  629. */
  630. int efi_partition(struct parsed_partitions *state)
  631. {
  632. gpt_header *gpt = NULL;
  633. gpt_entry *ptes = NULL;
  634. u32 i;
  635. unsigned ssz = bdev_logical_block_size(state->bdev) / 512;
  636. if (!find_valid_gpt(state, &gpt, &ptes) || !gpt || !ptes) {
  637. kfree(gpt);
  638. kfree(ptes);
  639. return 0;
  640. }
  641. pr_debug("GUID Partition Table is valid! Yea!\n");
  642. for (i = 0; i < le32_to_cpu(gpt->num_partition_entries) && i < state->limit-1; i++) {
  643. struct partition_meta_info *info;
  644. unsigned label_count = 0;
  645. unsigned label_max;
  646. u64 start = le64_to_cpu(ptes[i].starting_lba);
  647. u64 size = le64_to_cpu(ptes[i].ending_lba) -
  648. le64_to_cpu(ptes[i].starting_lba) + 1ULL;
  649. if (!is_pte_valid(&ptes[i], last_lba(state->bdev)))
  650. continue;
  651. put_partition(state, i+1, start * ssz, size * ssz);
  652. /* If this is a RAID volume, tell md */
  653. if (!efi_guidcmp(ptes[i].partition_type_guid, PARTITION_LINUX_RAID_GUID))
  654. state->parts[i + 1].flags = ADDPART_FLAG_RAID;
  655. info = &state->parts[i + 1].info;
  656. efi_guid_unparse(&ptes[i].unique_partition_guid, info->uuid);
  657. /* Naively convert UTF16-LE to 7 bits. */
  658. label_max = min(ARRAY_SIZE(info->volname) - 1,
  659. ARRAY_SIZE(ptes[i].partition_name));
  660. info->volname[label_max] = 0;
  661. while (label_count < label_max) {
  662. u8 c = ptes[i].partition_name[label_count] & 0xff;
  663. if (c && !isprint(c))
  664. c = '!';
  665. info->volname[label_count] = c;
  666. label_count++;
  667. }
  668. state->parts[i + 1].has_info = true;
  669. }
  670. kfree(ptes);
  671. kfree(gpt);
  672. strlcat(state->pp_buf, "\n", PAGE_SIZE);
  673. return 1;
  674. }