efi.c 23 KB

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