efi.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  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: disk parsed partitions
  231. * @lba: the Logical Block Address of the partition table
  232. * @buffer: destination buffer
  233. * @count: bytes to read
  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: disk parsed partitions
  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 = (size_t)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: disk parsed partitions
  296. * @lba: 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: disk parsed partitions
  320. * @lba: logical block address of the GPT header to test
  321. * @gpt: GPT header ptr, filled on return.
  322. * @ptes: 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, pt_size;
  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 Partition Entry Size check failed.\n");
  402. goto fail;
  403. }
  404. /* Sanity check partition table size */
  405. pt_size = (u64)le32_to_cpu((*gpt)->num_partition_entries) *
  406. le32_to_cpu((*gpt)->sizeof_partition_entry);
  407. if (pt_size > KMALLOC_MAX_SIZE) {
  408. pr_debug("GUID Partition Table is too large: %llu > %lu bytes\n",
  409. (unsigned long long)pt_size, KMALLOC_MAX_SIZE);
  410. goto fail;
  411. }
  412. if (!(*ptes = alloc_read_gpt_entries(state, *gpt)))
  413. goto fail;
  414. /* Check the GUID Partition Entry Array CRC */
  415. crc = efi_crc32((const unsigned char *) (*ptes), pt_size);
  416. if (crc != le32_to_cpu((*gpt)->partition_entry_array_crc32)) {
  417. pr_debug("GUID Partition Entry Array CRC check failed.\n");
  418. goto fail_ptes;
  419. }
  420. /* We're done, all's well */
  421. return 1;
  422. fail_ptes:
  423. kfree(*ptes);
  424. *ptes = NULL;
  425. fail:
  426. kfree(*gpt);
  427. *gpt = NULL;
  428. return 0;
  429. }
  430. /**
  431. * is_pte_valid() - tests one PTE for validity
  432. * @pte:pte to check
  433. * @lastlba: last lba of the disk
  434. *
  435. * Description: returns 1 if valid, 0 on error.
  436. */
  437. static inline int
  438. is_pte_valid(const gpt_entry *pte, const u64 lastlba)
  439. {
  440. if ((!efi_guidcmp(pte->partition_type_guid, NULL_GUID)) ||
  441. le64_to_cpu(pte->starting_lba) > lastlba ||
  442. le64_to_cpu(pte->ending_lba) > lastlba)
  443. return 0;
  444. return 1;
  445. }
  446. /**
  447. * compare_gpts() - Search disk for valid GPT headers and PTEs
  448. * @pgpt: primary GPT header
  449. * @agpt: alternate GPT header
  450. * @lastlba: last LBA number
  451. *
  452. * Description: Returns nothing. Sanity checks pgpt and agpt fields
  453. * and prints warnings on discrepancies.
  454. *
  455. */
  456. static void
  457. compare_gpts(gpt_header *pgpt, gpt_header *agpt, u64 lastlba)
  458. {
  459. int error_found = 0;
  460. if (!pgpt || !agpt)
  461. return;
  462. if (le64_to_cpu(pgpt->my_lba) != le64_to_cpu(agpt->alternate_lba)) {
  463. pr_warn("GPT:Primary header LBA != Alt. header alternate_lba\n");
  464. pr_warn("GPT:%lld != %lld\n",
  465. (unsigned long long)le64_to_cpu(pgpt->my_lba),
  466. (unsigned long long)le64_to_cpu(agpt->alternate_lba));
  467. error_found++;
  468. }
  469. if (le64_to_cpu(pgpt->alternate_lba) != le64_to_cpu(agpt->my_lba)) {
  470. pr_warn("GPT:Primary header alternate_lba != Alt. header my_lba\n");
  471. pr_warn("GPT:%lld != %lld\n",
  472. (unsigned long long)le64_to_cpu(pgpt->alternate_lba),
  473. (unsigned long long)le64_to_cpu(agpt->my_lba));
  474. error_found++;
  475. }
  476. if (le64_to_cpu(pgpt->first_usable_lba) !=
  477. le64_to_cpu(agpt->first_usable_lba)) {
  478. pr_warn("GPT:first_usable_lbas don't match.\n");
  479. pr_warn("GPT:%lld != %lld\n",
  480. (unsigned long long)le64_to_cpu(pgpt->first_usable_lba),
  481. (unsigned long long)le64_to_cpu(agpt->first_usable_lba));
  482. error_found++;
  483. }
  484. if (le64_to_cpu(pgpt->last_usable_lba) !=
  485. le64_to_cpu(agpt->last_usable_lba)) {
  486. pr_warn("GPT:last_usable_lbas don't match.\n");
  487. pr_warn("GPT:%lld != %lld\n",
  488. (unsigned long long)le64_to_cpu(pgpt->last_usable_lba),
  489. (unsigned long long)le64_to_cpu(agpt->last_usable_lba));
  490. error_found++;
  491. }
  492. if (efi_guidcmp(pgpt->disk_guid, agpt->disk_guid)) {
  493. pr_warn("GPT:disk_guids don't match.\n");
  494. error_found++;
  495. }
  496. if (le32_to_cpu(pgpt->num_partition_entries) !=
  497. le32_to_cpu(agpt->num_partition_entries)) {
  498. pr_warn("GPT:num_partition_entries don't match: "
  499. "0x%x != 0x%x\n",
  500. le32_to_cpu(pgpt->num_partition_entries),
  501. le32_to_cpu(agpt->num_partition_entries));
  502. error_found++;
  503. }
  504. if (le32_to_cpu(pgpt->sizeof_partition_entry) !=
  505. le32_to_cpu(agpt->sizeof_partition_entry)) {
  506. pr_warn("GPT:sizeof_partition_entry values don't match: "
  507. "0x%x != 0x%x\n",
  508. le32_to_cpu(pgpt->sizeof_partition_entry),
  509. le32_to_cpu(agpt->sizeof_partition_entry));
  510. error_found++;
  511. }
  512. if (le32_to_cpu(pgpt->partition_entry_array_crc32) !=
  513. le32_to_cpu(agpt->partition_entry_array_crc32)) {
  514. pr_warn("GPT:partition_entry_array_crc32 values don't match: "
  515. "0x%x != 0x%x\n",
  516. le32_to_cpu(pgpt->partition_entry_array_crc32),
  517. le32_to_cpu(agpt->partition_entry_array_crc32));
  518. error_found++;
  519. }
  520. if (le64_to_cpu(pgpt->alternate_lba) != lastlba) {
  521. pr_warn("GPT:Primary header thinks Alt. header is not at the end of the disk.\n");
  522. pr_warn("GPT:%lld != %lld\n",
  523. (unsigned long long)le64_to_cpu(pgpt->alternate_lba),
  524. (unsigned long long)lastlba);
  525. error_found++;
  526. }
  527. if (le64_to_cpu(agpt->my_lba) != lastlba) {
  528. pr_warn("GPT:Alternate GPT header not at the end of the disk.\n");
  529. pr_warn("GPT:%lld != %lld\n",
  530. (unsigned long long)le64_to_cpu(agpt->my_lba),
  531. (unsigned long long)lastlba);
  532. error_found++;
  533. }
  534. if (error_found)
  535. pr_warn("GPT: Use GNU Parted to correct GPT errors.\n");
  536. return;
  537. }
  538. /**
  539. * find_valid_gpt() - Search disk for valid GPT headers and PTEs
  540. * @state: disk parsed partitions
  541. * @gpt: GPT header ptr, filled on return.
  542. * @ptes: PTEs ptr, filled on return.
  543. *
  544. * Description: Returns 1 if valid, 0 on error.
  545. * If valid, returns pointers to newly allocated GPT header and PTEs.
  546. * Validity depends on PMBR being valid (or being overridden by the
  547. * 'gpt' kernel command line option) and finding either the Primary
  548. * GPT header and PTEs valid, or the Alternate GPT header and PTEs
  549. * valid. If the Primary GPT header is not valid, the Alternate GPT header
  550. * is not checked unless the 'gpt' kernel command line option is passed.
  551. * This protects against devices which misreport their size, and forces
  552. * the user to decide to use the Alternate GPT.
  553. */
  554. static int find_valid_gpt(struct parsed_partitions *state, gpt_header **gpt,
  555. gpt_entry **ptes)
  556. {
  557. int good_pgpt = 0, good_agpt = 0, good_pmbr = 0;
  558. gpt_header *pgpt = NULL, *agpt = NULL;
  559. gpt_entry *pptes = NULL, *aptes = NULL;
  560. legacy_mbr *legacymbr;
  561. sector_t total_sectors = i_size_read(state->bdev->bd_inode) >> 9;
  562. u64 lastlba;
  563. if (!ptes)
  564. return 0;
  565. lastlba = last_lba(state->bdev);
  566. if (!force_gpt) {
  567. /* This will be added to the EFI Spec. per Intel after v1.02. */
  568. legacymbr = kzalloc(sizeof(*legacymbr), GFP_KERNEL);
  569. if (!legacymbr)
  570. goto fail;
  571. read_lba(state, 0, (u8 *)legacymbr, sizeof(*legacymbr));
  572. good_pmbr = is_pmbr_valid(legacymbr, total_sectors);
  573. kfree(legacymbr);
  574. if (!good_pmbr)
  575. goto fail;
  576. pr_debug("Device has a %s MBR\n",
  577. good_pmbr == GPT_MBR_PROTECTIVE ?
  578. "protective" : "hybrid");
  579. }
  580. good_pgpt = is_gpt_valid(state, GPT_PRIMARY_PARTITION_TABLE_LBA,
  581. &pgpt, &pptes);
  582. if (good_pgpt)
  583. good_agpt = is_gpt_valid(state,
  584. le64_to_cpu(pgpt->alternate_lba),
  585. &agpt, &aptes);
  586. if (!good_agpt && force_gpt)
  587. good_agpt = is_gpt_valid(state, lastlba, &agpt, &aptes);
  588. /* The obviously unsuccessful case */
  589. if (!good_pgpt && !good_agpt)
  590. goto fail;
  591. compare_gpts(pgpt, agpt, lastlba);
  592. /* The good cases */
  593. if (good_pgpt) {
  594. *gpt = pgpt;
  595. *ptes = pptes;
  596. kfree(agpt);
  597. kfree(aptes);
  598. if (!good_agpt)
  599. pr_warn("Alternate GPT is invalid, using primary GPT.\n");
  600. return 1;
  601. }
  602. else if (good_agpt) {
  603. *gpt = agpt;
  604. *ptes = aptes;
  605. kfree(pgpt);
  606. kfree(pptes);
  607. pr_warn("Primary GPT is invalid, using alternate GPT.\n");
  608. return 1;
  609. }
  610. fail:
  611. kfree(pgpt);
  612. kfree(agpt);
  613. kfree(pptes);
  614. kfree(aptes);
  615. *gpt = NULL;
  616. *ptes = NULL;
  617. return 0;
  618. }
  619. /**
  620. * efi_partition(struct parsed_partitions *state)
  621. * @state: disk parsed partitions
  622. *
  623. * Description: called from check.c, if the disk contains GPT
  624. * partitions, sets up partition entries in the kernel.
  625. *
  626. * If the first block on the disk is a legacy MBR,
  627. * it will get handled by msdos_partition().
  628. * If it's a Protective MBR, we'll handle it here.
  629. *
  630. * We do not create a Linux partition for GPT, but
  631. * only for the actual data partitions.
  632. * Returns:
  633. * -1 if unable to read the partition table
  634. * 0 if this isn't our partition table
  635. * 1 if successful
  636. *
  637. */
  638. int efi_partition(struct parsed_partitions *state)
  639. {
  640. gpt_header *gpt = NULL;
  641. gpt_entry *ptes = NULL;
  642. u32 i;
  643. unsigned ssz = bdev_logical_block_size(state->bdev) / 512;
  644. if (!find_valid_gpt(state, &gpt, &ptes) || !gpt || !ptes) {
  645. kfree(gpt);
  646. kfree(ptes);
  647. return 0;
  648. }
  649. pr_debug("GUID Partition Table is valid! Yea!\n");
  650. for (i = 0; i < le32_to_cpu(gpt->num_partition_entries) && i < state->limit-1; i++) {
  651. struct partition_meta_info *info;
  652. unsigned label_count = 0;
  653. unsigned label_max;
  654. u64 start = le64_to_cpu(ptes[i].starting_lba);
  655. u64 size = le64_to_cpu(ptes[i].ending_lba) -
  656. le64_to_cpu(ptes[i].starting_lba) + 1ULL;
  657. if (!is_pte_valid(&ptes[i], last_lba(state->bdev)))
  658. continue;
  659. put_partition(state, i+1, start * ssz, size * ssz);
  660. /* If this is a RAID volume, tell md */
  661. if (!efi_guidcmp(ptes[i].partition_type_guid, PARTITION_LINUX_RAID_GUID))
  662. state->parts[i + 1].flags = ADDPART_FLAG_RAID;
  663. info = &state->parts[i + 1].info;
  664. efi_guid_to_str(&ptes[i].unique_partition_guid, info->uuid);
  665. /* Naively convert UTF16-LE to 7 bits. */
  666. label_max = min(ARRAY_SIZE(info->volname) - 1,
  667. ARRAY_SIZE(ptes[i].partition_name));
  668. info->volname[label_max] = 0;
  669. while (label_count < label_max) {
  670. u8 c = ptes[i].partition_name[label_count] & 0xff;
  671. if (c && !isprint(c))
  672. c = '!';
  673. info->volname[label_count] = c;
  674. label_count++;
  675. }
  676. state->parts[i + 1].has_info = true;
  677. }
  678. kfree(ptes);
  679. kfree(gpt);
  680. strlcat(state->pp_buf, "\n", PAGE_SIZE);
  681. return 1;
  682. }