efi.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  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 <[email protected]>
  9. * Copyright 2000,2001,2002,2004 Dell Inc.
  10. *
  11. * TODO:
  12. *
  13. * Changelog:
  14. * Mon August 5th, 2013 Davidlohr Bueso <[email protected]>
  15. * - detect hybrid MBRs, tighter pMBR checking & cleanups.
  16. *
  17. * Mon Nov 09 2004 Matt Domsch <[email protected]>
  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 <[email protected]>
  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 <[email protected]>
  33. * - Removed __PRIPTR_PREFIX - not being used
  34. *
  35. * Mon Jan 14 2002 Matt Domsch <[email protected]>
  36. * - Ported to 2.5.2-pre11 + library crc32 patch Linus applied
  37. *
  38. * Thu Dec 6 2001 Matt Domsch <[email protected]>
  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 <[email protected]>
  45. * - Removed CONFIG_DEVFS_VOLUMES_UUID code entirely per Martin Wilck
  46. *
  47. * Wed Oct 10 2001 Matt Domsch <[email protected]>
  48. * - Changed function comments to DocBook style per Andreas Dilger suggestion.
  49. *
  50. * Mon Oct 08 2001 Matt Domsch <[email protected]>
  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 <[email protected]>
  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 <[email protected]>
  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 <[email protected]>
  71. * - Moved crc32() to linux/lib, added efi_crc32().
  72. *
  73. * Thu Nov 30 2000 Matt Domsch <[email protected]>
  74. * - Replaced Intel's CRC32 function with an equivalent
  75. * non-license-restricted version.
  76. *
  77. * Wed Oct 25 2000 Matt Domsch <[email protected]>
  78. * - Fixed the last_lba() call to return the proper last block
  79. *
  80. * Thu Oct 12 2000 Matt Domsch <[email protected]>
  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, min_t(uint32_t,
  207. total_sectors - 1, 0xFFFFFFFF));
  208. }
  209. done:
  210. return ret;
  211. }
  212. /**
  213. * read_lba(): Read bytes from disk, starting at given LBA
  214. * @state: disk parsed partitions
  215. * @lba: the Logical Block Address of the partition table
  216. * @buffer: destination buffer
  217. * @count: bytes to read
  218. *
  219. * Description: Reads @count bytes from @state->disk into @buffer.
  220. * Returns number of bytes read on success, 0 on error.
  221. */
  222. static size_t read_lba(struct parsed_partitions *state,
  223. u64 lba, u8 *buffer, size_t count)
  224. {
  225. size_t totalreadcount = 0;
  226. sector_t n = lba *
  227. (queue_logical_block_size(state->disk->queue) / 512);
  228. if (!buffer || lba > last_lba(state->disk))
  229. return 0;
  230. while (count) {
  231. int copied = 512;
  232. Sector sect;
  233. unsigned char *data = read_part_sector(state, n++, &sect);
  234. if (!data)
  235. break;
  236. if (copied > count)
  237. copied = count;
  238. memcpy(buffer, data, copied);
  239. put_dev_sector(sect);
  240. buffer += copied;
  241. totalreadcount +=copied;
  242. count -= copied;
  243. }
  244. return totalreadcount;
  245. }
  246. /**
  247. * alloc_read_gpt_entries(): reads partition entries from disk
  248. * @state: disk parsed partitions
  249. * @gpt: GPT header
  250. *
  251. * Description: Returns ptes on success, NULL on error.
  252. * Allocates space for PTEs based on information found in @gpt.
  253. * Notes: remember to free pte when you're done!
  254. */
  255. static gpt_entry *alloc_read_gpt_entries(struct parsed_partitions *state,
  256. gpt_header *gpt)
  257. {
  258. size_t count;
  259. gpt_entry *pte;
  260. if (!gpt)
  261. return NULL;
  262. count = (size_t)le32_to_cpu(gpt->num_partition_entries) *
  263. le32_to_cpu(gpt->sizeof_partition_entry);
  264. if (!count)
  265. return NULL;
  266. pte = kmalloc(count, GFP_KERNEL);
  267. if (!pte)
  268. return NULL;
  269. if (read_lba(state, le64_to_cpu(gpt->partition_entry_lba),
  270. (u8 *) pte, count) < count) {
  271. kfree(pte);
  272. pte=NULL;
  273. return NULL;
  274. }
  275. return pte;
  276. }
  277. /**
  278. * alloc_read_gpt_header(): Allocates GPT header, reads into it from disk
  279. * @state: disk parsed partitions
  280. * @lba: the Logical Block Address of the partition table
  281. *
  282. * Description: returns GPT header on success, NULL on error. Allocates
  283. * and fills a GPT header starting at @ from @state->disk.
  284. * Note: remember to free gpt when finished with it.
  285. */
  286. static gpt_header *alloc_read_gpt_header(struct parsed_partitions *state,
  287. u64 lba)
  288. {
  289. gpt_header *gpt;
  290. unsigned ssz = queue_logical_block_size(state->disk->queue);
  291. gpt = kmalloc(ssz, GFP_KERNEL);
  292. if (!gpt)
  293. return NULL;
  294. if (read_lba(state, lba, (u8 *) gpt, ssz) < ssz) {
  295. kfree(gpt);
  296. gpt=NULL;
  297. return NULL;
  298. }
  299. return gpt;
  300. }
  301. /**
  302. * is_gpt_valid() - tests one GPT header and PTEs for validity
  303. * @state: disk parsed partitions
  304. * @lba: logical block address of the GPT header to test
  305. * @gpt: GPT header ptr, filled on return.
  306. * @ptes: PTEs ptr, filled on return.
  307. *
  308. * Description: returns 1 if valid, 0 on error.
  309. * If valid, returns pointers to newly allocated GPT header and PTEs.
  310. */
  311. static int is_gpt_valid(struct parsed_partitions *state, u64 lba,
  312. gpt_header **gpt, gpt_entry **ptes)
  313. {
  314. u32 crc, origcrc;
  315. u64 lastlba, pt_size;
  316. if (!ptes)
  317. return 0;
  318. if (!(*gpt = alloc_read_gpt_header(state, lba)))
  319. return 0;
  320. /* Check the GUID Partition Table signature */
  321. if (le64_to_cpu((*gpt)->signature) != GPT_HEADER_SIGNATURE) {
  322. pr_debug("GUID Partition Table Header signature is wrong:"
  323. "%lld != %lld\n",
  324. (unsigned long long)le64_to_cpu((*gpt)->signature),
  325. (unsigned long long)GPT_HEADER_SIGNATURE);
  326. goto fail;
  327. }
  328. /* Check the GUID Partition Table header size is too big */
  329. if (le32_to_cpu((*gpt)->header_size) >
  330. queue_logical_block_size(state->disk->queue)) {
  331. pr_debug("GUID Partition Table Header size is too large: %u > %u\n",
  332. le32_to_cpu((*gpt)->header_size),
  333. queue_logical_block_size(state->disk->queue));
  334. goto fail;
  335. }
  336. /* Check the GUID Partition Table header size is too small */
  337. if (le32_to_cpu((*gpt)->header_size) < sizeof(gpt_header)) {
  338. pr_debug("GUID Partition Table Header size is too small: %u < %zu\n",
  339. le32_to_cpu((*gpt)->header_size),
  340. sizeof(gpt_header));
  341. goto fail;
  342. }
  343. /* Check the GUID Partition Table CRC */
  344. origcrc = le32_to_cpu((*gpt)->header_crc32);
  345. (*gpt)->header_crc32 = 0;
  346. crc = efi_crc32((const unsigned char *) (*gpt), le32_to_cpu((*gpt)->header_size));
  347. if (crc != origcrc) {
  348. pr_debug("GUID Partition Table Header CRC is wrong: %x != %x\n",
  349. crc, origcrc);
  350. goto fail;
  351. }
  352. (*gpt)->header_crc32 = cpu_to_le32(origcrc);
  353. /* Check that the my_lba entry points to the LBA that contains
  354. * the GUID Partition Table */
  355. if (le64_to_cpu((*gpt)->my_lba) != lba) {
  356. pr_debug("GPT my_lba incorrect: %lld != %lld\n",
  357. (unsigned long long)le64_to_cpu((*gpt)->my_lba),
  358. (unsigned long long)lba);
  359. goto fail;
  360. }
  361. /* Check the first_usable_lba and last_usable_lba are
  362. * within the disk.
  363. */
  364. lastlba = last_lba(state->disk);
  365. if (le64_to_cpu((*gpt)->first_usable_lba) > lastlba) {
  366. pr_debug("GPT: first_usable_lba incorrect: %lld > %lld\n",
  367. (unsigned long long)le64_to_cpu((*gpt)->first_usable_lba),
  368. (unsigned long long)lastlba);
  369. goto fail;
  370. }
  371. if (le64_to_cpu((*gpt)->last_usable_lba) > lastlba) {
  372. pr_debug("GPT: last_usable_lba incorrect: %lld > %lld\n",
  373. (unsigned long long)le64_to_cpu((*gpt)->last_usable_lba),
  374. (unsigned long long)lastlba);
  375. goto fail;
  376. }
  377. if (le64_to_cpu((*gpt)->last_usable_lba) < le64_to_cpu((*gpt)->first_usable_lba)) {
  378. pr_debug("GPT: last_usable_lba incorrect: %lld > %lld\n",
  379. (unsigned long long)le64_to_cpu((*gpt)->last_usable_lba),
  380. (unsigned long long)le64_to_cpu((*gpt)->first_usable_lba));
  381. goto fail;
  382. }
  383. /* Check that sizeof_partition_entry has the correct value */
  384. if (le32_to_cpu((*gpt)->sizeof_partition_entry) != sizeof(gpt_entry)) {
  385. pr_debug("GUID Partition Entry Size check failed.\n");
  386. goto fail;
  387. }
  388. /* Sanity check partition table size */
  389. pt_size = (u64)le32_to_cpu((*gpt)->num_partition_entries) *
  390. le32_to_cpu((*gpt)->sizeof_partition_entry);
  391. if (pt_size > KMALLOC_MAX_SIZE) {
  392. pr_debug("GUID Partition Table is too large: %llu > %lu bytes\n",
  393. (unsigned long long)pt_size, KMALLOC_MAX_SIZE);
  394. goto fail;
  395. }
  396. if (!(*ptes = alloc_read_gpt_entries(state, *gpt)))
  397. goto fail;
  398. /* Check the GUID Partition Entry Array CRC */
  399. crc = efi_crc32((const unsigned char *) (*ptes), pt_size);
  400. if (crc != le32_to_cpu((*gpt)->partition_entry_array_crc32)) {
  401. pr_debug("GUID Partition Entry Array CRC check failed.\n");
  402. goto fail_ptes;
  403. }
  404. /* We're done, all's well */
  405. return 1;
  406. fail_ptes:
  407. kfree(*ptes);
  408. *ptes = NULL;
  409. fail:
  410. kfree(*gpt);
  411. *gpt = NULL;
  412. return 0;
  413. }
  414. /**
  415. * is_pte_valid() - tests one PTE for validity
  416. * @pte:pte to check
  417. * @lastlba: last lba of the disk
  418. *
  419. * Description: returns 1 if valid, 0 on error.
  420. */
  421. static inline int
  422. is_pte_valid(const gpt_entry *pte, const u64 lastlba)
  423. {
  424. if ((!efi_guidcmp(pte->partition_type_guid, NULL_GUID)) ||
  425. le64_to_cpu(pte->starting_lba) > lastlba ||
  426. le64_to_cpu(pte->ending_lba) > lastlba)
  427. return 0;
  428. return 1;
  429. }
  430. /**
  431. * compare_gpts() - Search disk for valid GPT headers and PTEs
  432. * @pgpt: primary GPT header
  433. * @agpt: alternate GPT header
  434. * @lastlba: last LBA number
  435. *
  436. * Description: Returns nothing. Sanity checks pgpt and agpt fields
  437. * and prints warnings on discrepancies.
  438. *
  439. */
  440. static void
  441. compare_gpts(gpt_header *pgpt, gpt_header *agpt, u64 lastlba)
  442. {
  443. int error_found = 0;
  444. if (!pgpt || !agpt)
  445. return;
  446. if (le64_to_cpu(pgpt->my_lba) != le64_to_cpu(agpt->alternate_lba)) {
  447. pr_warn("GPT:Primary header LBA != Alt. header alternate_lba\n");
  448. pr_warn("GPT:%lld != %lld\n",
  449. (unsigned long long)le64_to_cpu(pgpt->my_lba),
  450. (unsigned long long)le64_to_cpu(agpt->alternate_lba));
  451. error_found++;
  452. }
  453. if (le64_to_cpu(pgpt->alternate_lba) != le64_to_cpu(agpt->my_lba)) {
  454. pr_warn("GPT:Primary header alternate_lba != Alt. header my_lba\n");
  455. pr_warn("GPT:%lld != %lld\n",
  456. (unsigned long long)le64_to_cpu(pgpt->alternate_lba),
  457. (unsigned long long)le64_to_cpu(agpt->my_lba));
  458. error_found++;
  459. }
  460. if (le64_to_cpu(pgpt->first_usable_lba) !=
  461. le64_to_cpu(agpt->first_usable_lba)) {
  462. pr_warn("GPT:first_usable_lbas don't match.\n");
  463. pr_warn("GPT:%lld != %lld\n",
  464. (unsigned long long)le64_to_cpu(pgpt->first_usable_lba),
  465. (unsigned long long)le64_to_cpu(agpt->first_usable_lba));
  466. error_found++;
  467. }
  468. if (le64_to_cpu(pgpt->last_usable_lba) !=
  469. le64_to_cpu(agpt->last_usable_lba)) {
  470. pr_warn("GPT:last_usable_lbas don't match.\n");
  471. pr_warn("GPT:%lld != %lld\n",
  472. (unsigned long long)le64_to_cpu(pgpt->last_usable_lba),
  473. (unsigned long long)le64_to_cpu(agpt->last_usable_lba));
  474. error_found++;
  475. }
  476. if (efi_guidcmp(pgpt->disk_guid, agpt->disk_guid)) {
  477. pr_warn("GPT:disk_guids don't match.\n");
  478. error_found++;
  479. }
  480. if (le32_to_cpu(pgpt->num_partition_entries) !=
  481. le32_to_cpu(agpt->num_partition_entries)) {
  482. pr_warn("GPT:num_partition_entries don't match: "
  483. "0x%x != 0x%x\n",
  484. le32_to_cpu(pgpt->num_partition_entries),
  485. le32_to_cpu(agpt->num_partition_entries));
  486. error_found++;
  487. }
  488. if (le32_to_cpu(pgpt->sizeof_partition_entry) !=
  489. le32_to_cpu(agpt->sizeof_partition_entry)) {
  490. pr_warn("GPT:sizeof_partition_entry values don't match: "
  491. "0x%x != 0x%x\n",
  492. le32_to_cpu(pgpt->sizeof_partition_entry),
  493. le32_to_cpu(agpt->sizeof_partition_entry));
  494. error_found++;
  495. }
  496. if (le32_to_cpu(pgpt->partition_entry_array_crc32) !=
  497. le32_to_cpu(agpt->partition_entry_array_crc32)) {
  498. pr_warn("GPT:partition_entry_array_crc32 values don't match: "
  499. "0x%x != 0x%x\n",
  500. le32_to_cpu(pgpt->partition_entry_array_crc32),
  501. le32_to_cpu(agpt->partition_entry_array_crc32));
  502. error_found++;
  503. }
  504. if (le64_to_cpu(pgpt->alternate_lba) != lastlba) {
  505. pr_warn("GPT:Primary header thinks Alt. header is not at the end of the disk.\n");
  506. pr_warn("GPT:%lld != %lld\n",
  507. (unsigned long long)le64_to_cpu(pgpt->alternate_lba),
  508. (unsigned long long)lastlba);
  509. error_found++;
  510. }
  511. if (le64_to_cpu(agpt->my_lba) != lastlba) {
  512. pr_warn("GPT:Alternate GPT header not at the end of the disk.\n");
  513. pr_warn("GPT:%lld != %lld\n",
  514. (unsigned long long)le64_to_cpu(agpt->my_lba),
  515. (unsigned long long)lastlba);
  516. error_found++;
  517. }
  518. if (error_found)
  519. pr_warn("GPT: Use GNU Parted to correct GPT errors.\n");
  520. return;
  521. }
  522. /**
  523. * find_valid_gpt() - Search disk for valid GPT headers and PTEs
  524. * @state: disk parsed partitions
  525. * @gpt: GPT header ptr, filled on return.
  526. * @ptes: PTEs ptr, filled on return.
  527. *
  528. * Description: Returns 1 if valid, 0 on error.
  529. * If valid, returns pointers to newly allocated GPT header and PTEs.
  530. * Validity depends on PMBR being valid (or being overridden by the
  531. * 'gpt' kernel command line option) and finding either the Primary
  532. * GPT header and PTEs valid, or the Alternate GPT header and PTEs
  533. * valid. If the Primary GPT header is not valid, the Alternate GPT header
  534. * is not checked unless the 'gpt' kernel command line option is passed.
  535. * This protects against devices which misreport their size, and forces
  536. * the user to decide to use the Alternate GPT.
  537. */
  538. static int find_valid_gpt(struct parsed_partitions *state, gpt_header **gpt,
  539. gpt_entry **ptes)
  540. {
  541. int good_pgpt = 0, good_agpt = 0, good_pmbr = 0;
  542. gpt_header *pgpt = NULL, *agpt = NULL;
  543. gpt_entry *pptes = NULL, *aptes = NULL;
  544. legacy_mbr *legacymbr;
  545. struct gendisk *disk = state->disk;
  546. const struct block_device_operations *fops = disk->fops;
  547. sector_t total_sectors = get_capacity(state->disk);
  548. u64 lastlba;
  549. if (!ptes)
  550. return 0;
  551. lastlba = last_lba(state->disk);
  552. if (!force_gpt) {
  553. /* This will be added to the EFI Spec. per Intel after v1.02. */
  554. legacymbr = kzalloc(sizeof(*legacymbr), GFP_KERNEL);
  555. if (!legacymbr)
  556. goto fail;
  557. read_lba(state, 0, (u8 *)legacymbr, sizeof(*legacymbr));
  558. good_pmbr = is_pmbr_valid(legacymbr, total_sectors);
  559. kfree(legacymbr);
  560. if (!good_pmbr)
  561. goto fail;
  562. pr_debug("Device has a %s MBR\n",
  563. good_pmbr == GPT_MBR_PROTECTIVE ?
  564. "protective" : "hybrid");
  565. }
  566. good_pgpt = is_gpt_valid(state, GPT_PRIMARY_PARTITION_TABLE_LBA,
  567. &pgpt, &pptes);
  568. if (good_pgpt)
  569. good_agpt = is_gpt_valid(state,
  570. le64_to_cpu(pgpt->alternate_lba),
  571. &agpt, &aptes);
  572. if (!good_agpt && force_gpt)
  573. good_agpt = is_gpt_valid(state, lastlba, &agpt, &aptes);
  574. if (!good_agpt && force_gpt && fops->alternative_gpt_sector) {
  575. sector_t agpt_sector;
  576. int err;
  577. err = fops->alternative_gpt_sector(disk, &agpt_sector);
  578. if (!err)
  579. good_agpt = is_gpt_valid(state, agpt_sector,
  580. &agpt, &aptes);
  581. }
  582. /* The obviously unsuccessful case */
  583. if (!good_pgpt && !good_agpt)
  584. goto fail;
  585. compare_gpts(pgpt, agpt, lastlba);
  586. /* The good cases */
  587. if (good_pgpt) {
  588. *gpt = pgpt;
  589. *ptes = pptes;
  590. kfree(agpt);
  591. kfree(aptes);
  592. if (!good_agpt)
  593. pr_warn("Alternate GPT is invalid, using primary GPT.\n");
  594. return 1;
  595. }
  596. else if (good_agpt) {
  597. *gpt = agpt;
  598. *ptes = aptes;
  599. kfree(pgpt);
  600. kfree(pptes);
  601. pr_warn("Primary GPT is invalid, using alternate GPT.\n");
  602. return 1;
  603. }
  604. fail:
  605. kfree(pgpt);
  606. kfree(agpt);
  607. kfree(pptes);
  608. kfree(aptes);
  609. *gpt = NULL;
  610. *ptes = NULL;
  611. return 0;
  612. }
  613. /**
  614. * utf16_le_to_7bit(): Naively converts a UTF-16LE string to 7-bit ASCII characters
  615. * @in: input UTF-16LE string
  616. * @size: size of the input string
  617. * @out: output string ptr, should be capable to store @size+1 characters
  618. *
  619. * Description: Converts @size UTF16-LE symbols from @in string to 7-bit
  620. * ASCII characters and stores them to @out. Adds trailing zero to @out array.
  621. */
  622. static void utf16_le_to_7bit(const __le16 *in, unsigned int size, u8 *out)
  623. {
  624. unsigned int i = 0;
  625. out[size] = 0;
  626. while (i < size) {
  627. u8 c = le16_to_cpu(in[i]) & 0xff;
  628. if (c && !isprint(c))
  629. c = '!';
  630. out[i] = c;
  631. i++;
  632. }
  633. }
  634. /**
  635. * efi_partition - scan for GPT partitions
  636. * @state: disk parsed partitions
  637. *
  638. * Description: called from check.c, if the disk contains GPT
  639. * partitions, sets up partition entries in the kernel.
  640. *
  641. * If the first block on the disk is a legacy MBR,
  642. * it will get handled by msdos_partition().
  643. * If it's a Protective MBR, we'll handle it here.
  644. *
  645. * We do not create a Linux partition for GPT, but
  646. * only for the actual data partitions.
  647. * Returns:
  648. * -1 if unable to read the partition table
  649. * 0 if this isn't our partition table
  650. * 1 if successful
  651. *
  652. */
  653. int efi_partition(struct parsed_partitions *state)
  654. {
  655. gpt_header *gpt = NULL;
  656. gpt_entry *ptes = NULL;
  657. u32 i;
  658. unsigned ssz = queue_logical_block_size(state->disk->queue) / 512;
  659. if (!find_valid_gpt(state, &gpt, &ptes) || !gpt || !ptes) {
  660. kfree(gpt);
  661. kfree(ptes);
  662. return 0;
  663. }
  664. pr_debug("GUID Partition Table is valid! Yea!\n");
  665. for (i = 0; i < le32_to_cpu(gpt->num_partition_entries) && i < state->limit-1; i++) {
  666. struct partition_meta_info *info;
  667. unsigned label_max;
  668. u64 start = le64_to_cpu(ptes[i].starting_lba);
  669. u64 size = le64_to_cpu(ptes[i].ending_lba) -
  670. le64_to_cpu(ptes[i].starting_lba) + 1ULL;
  671. if (!is_pte_valid(&ptes[i], last_lba(state->disk)))
  672. continue;
  673. put_partition(state, i+1, start * ssz, size * ssz);
  674. /* If this is a RAID volume, tell md */
  675. if (!efi_guidcmp(ptes[i].partition_type_guid, PARTITION_LINUX_RAID_GUID))
  676. state->parts[i + 1].flags = ADDPART_FLAG_RAID;
  677. info = &state->parts[i + 1].info;
  678. efi_guid_to_str(&ptes[i].unique_partition_guid, info->uuid);
  679. /* Naively convert UTF16-LE to 7 bits. */
  680. label_max = min(ARRAY_SIZE(info->volname) - 1,
  681. ARRAY_SIZE(ptes[i].partition_name));
  682. utf16_le_to_7bit(ptes[i].partition_name, label_max, info->volname);
  683. state->parts[i + 1].has_info = true;
  684. }
  685. kfree(ptes);
  686. kfree(gpt);
  687. strlcat(state->pp_buf, "\n", PAGE_SIZE);
  688. return 1;
  689. }