File Index
Files and directories
Sign in
FILE

efi.c

File preview with syntax highlighting.

linux-6.19.14/block/partitions/efi.c C
  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
 93/* This allows a kernel command line option 'gpt' to override
 94 * the test for invalid PMBR.  Not __initdata because reloading
 95 * the partition tables happens after init too.
 96 */
 97static int force_gpt;
 98static int __init
 99force_gpt_fn(char *str)
100{
101	force_gpt = 1;
102	return 1;
103}
104__setup("gpt", force_gpt_fn);
105
106
107/**
108 * efi_crc32() - EFI version of crc32 function
109 * @buf: buffer to calculate crc32 of
110 * @len: length of buf
111 *
112 * Description: Returns EFI-style CRC32 value for @buf
113 * 
114 * This function uses the little endian Ethernet polynomial
115 * but seeds the function with ~0, and xor's with ~0 at the end.
116 * Note, the EFI Specification, v1.02, has a reference to
117 * Dr. Dobbs Journal, May 1994 (actually it's in May 1992).
118 */
119static inline u32
120efi_crc32(const void *buf, unsigned long len)
121{
122	return (crc32(~0L, buf, len) ^ ~0L);
123}
124
125/**
126 * last_lba(): return number of last logical block of device
127 * @disk: block device
128 * 
129 * Description: Returns last LBA value on success, 0 on error.
130 * This is stored (by sd and ide-geometry) in
131 *  the part[0] entry for this disk, and is the number of
132 *  physical sectors available on the disk.
133 */
134static u64 last_lba(struct gendisk *disk)
135{
136	return div_u64(bdev_nr_bytes(disk->part0),
137		       queue_logical_block_size(disk->queue)) - 1ULL;
138}
139
140static inline int pmbr_part_valid(gpt_mbr_record *part)
141{
142	if (part->os_type != EFI_PMBR_OSTYPE_EFI_GPT)
143		goto invalid;
144
145	/* set to 0x00000001 (i.e., the LBA of the GPT Partition Header) */
146	if (le32_to_cpu(part->starting_lba) != GPT_PRIMARY_PARTITION_TABLE_LBA)
147		goto invalid;
148
149	return GPT_MBR_PROTECTIVE;
150invalid:
151	return 0;
152}
153
154/**
155 * is_pmbr_valid(): test Protective MBR for validity
156 * @mbr: pointer to a legacy mbr structure
157 * @total_sectors: amount of sectors in the device
158 *
159 * Description: Checks for a valid protective or hybrid
160 * master boot record (MBR). The validity of a pMBR depends
161 * on all of the following properties:
162 *  1) MSDOS signature is in the last two bytes of the MBR
163 *  2) One partition of type 0xEE is found
164 *
165 * In addition, a hybrid MBR will have up to three additional
166 * primary partitions, which point to the same space that's
167 * marked out by up to three GPT partitions.
168 *
169 * Returns 0 upon invalid MBR, or GPT_MBR_PROTECTIVE or
170 * GPT_MBR_HYBRID depending on the device layout.
171 */
172static int is_pmbr_valid(legacy_mbr *mbr, sector_t total_sectors)
173{
174	uint32_t sz = 0;
175	int i, part = 0, ret = 0; /* invalid by default */
176
177	if (!mbr || le16_to_cpu(mbr->signature) != MSDOS_MBR_SIGNATURE)
178		goto done;
179
180	for (i = 0; i < 4; i++) {
181		ret = pmbr_part_valid(&mbr->partition_record[i]);
182		if (ret == GPT_MBR_PROTECTIVE) {
183			part = i;
184			/*
185			 * Ok, we at least know that there's a protective MBR,
186			 * now check if there are other partition types for
187			 * hybrid MBR.
188			 */
189			goto check_hybrid;
190		}
191	}
192
193	if (ret != GPT_MBR_PROTECTIVE)
194		goto done;
195check_hybrid:
196	for (i = 0; i < 4; i++)
197		if ((mbr->partition_record[i].os_type !=
198			EFI_PMBR_OSTYPE_EFI_GPT) &&
199		    (mbr->partition_record[i].os_type != 0x00))
200			ret = GPT_MBR_HYBRID;
201
202	/*
203	 * Protective MBRs take up the lesser of the whole disk
204	 * or 2 TiB (32bit LBA), ignoring the rest of the disk.
205	 * Some partitioning programs, nonetheless, choose to set
206	 * the size to the maximum 32-bit limitation, disregarding
207	 * the disk size.
208	 *
209	 * Hybrid MBRs do not necessarily comply with this.
210	 *
211	 * Consider a bad value here to be a warning to support dd'ing
212	 * an image from a smaller disk to a larger disk.
213	 */
214	if (ret == GPT_MBR_PROTECTIVE) {
215		sz = le32_to_cpu(mbr->partition_record[part].size_in_lba);
216		if (sz != (uint32_t) total_sectors - 1 && sz != 0xFFFFFFFF)
217			pr_debug("GPT: mbr size in lba (%u) different than whole disk (%u).\n",
218				 sz, (uint32_t)min(total_sectors - 1, 0xFFFFFFFF));
219	}
220done:
221	return ret;
222}
223
224/**
225 * read_lba(): Read bytes from disk, starting at given LBA
226 * @state: disk parsed partitions
227 * @lba: the Logical Block Address of the partition table
228 * @buffer: destination buffer
229 * @count: bytes to read
230 *
231 * Description: Reads @count bytes from @state->disk into @buffer.
232 * Returns number of bytes read on success, 0 on error.
233 */
234static size_t read_lba(struct parsed_partitions *state,
235		       u64 lba, u8 *buffer, size_t count)
236{
237	size_t totalreadcount = 0;
238	sector_t n = lba *
239		(queue_logical_block_size(state->disk->queue) / 512);
240
241	if (!buffer || lba > last_lba(state->disk))
242                return 0;
243
244	while (count) {
245		int copied = 512;
246		Sector sect;
247		unsigned char *data = read_part_sector(state, n++, &sect);
248		if (!data)
249			break;
250		if (copied > count)
251			copied = count;
252		memcpy(buffer, data, copied);
253		put_dev_sector(sect);
254		buffer += copied;
255		totalreadcount +=copied;
256		count -= copied;
257	}
258	return totalreadcount;
259}
260
261/**
262 * alloc_read_gpt_entries(): reads partition entries from disk
263 * @state: disk parsed partitions
264 * @gpt: GPT header
265 * 
266 * Description: Returns ptes on success,  NULL on error.
267 * Allocates space for PTEs based on information found in @gpt.
268 * Notes: remember to free pte when you're done!
269 */
270static gpt_entry *alloc_read_gpt_entries(struct parsed_partitions *state,
271					 gpt_header *gpt)
272{
273	size_t count;
274	gpt_entry *pte;
275
276	if (!gpt)
277		return NULL;
278
279	count = (size_t)le32_to_cpu(gpt->num_partition_entries) *
280                le32_to_cpu(gpt->sizeof_partition_entry);
281	if (!count)
282		return NULL;
283	pte = kmalloc(count, GFP_KERNEL);
284	if (!pte)
285		return NULL;
286
287	if (read_lba(state, le64_to_cpu(gpt->partition_entry_lba),
288			(u8 *) pte, count) < count) {
289		kfree(pte);
290                pte=NULL;
291		return NULL;
292	}
293	return pte;
294}
295
296/**
297 * alloc_read_gpt_header(): Allocates GPT header, reads into it from disk
298 * @state: disk parsed partitions
299 * @lba: the Logical Block Address of the partition table
300 * 
301 * Description: returns GPT header on success, NULL on error.   Allocates
302 * and fills a GPT header starting at @ from @state->disk.
303 * Note: remember to free gpt when finished with it.
304 */
305static gpt_header *alloc_read_gpt_header(struct parsed_partitions *state,
306					 u64 lba)
307{
308	gpt_header *gpt;
309	unsigned ssz = queue_logical_block_size(state->disk->queue);
310
311	gpt = kmalloc(ssz, GFP_KERNEL);
312	if (!gpt)
313		return NULL;
314
315	if (read_lba(state, lba, (u8 *) gpt, ssz) < ssz) {
316		kfree(gpt);
317                gpt=NULL;
318		return NULL;
319	}
320
321	return gpt;
322}
323
324/**
325 * is_gpt_valid() - tests one GPT header and PTEs for validity
326 * @state: disk parsed partitions
327 * @lba: logical block address of the GPT header to test
328 * @gpt: GPT header ptr, filled on return.
329 * @ptes: PTEs ptr, filled on return.
330 *
331 * Description: returns 1 if valid,  0 on error.
332 * If valid, returns pointers to newly allocated GPT header and PTEs.
333 */
334static int is_gpt_valid(struct parsed_partitions *state, u64 lba,
335			gpt_header **gpt, gpt_entry **ptes)
336{
337	u32 crc, origcrc;
338	u64 lastlba, pt_size;
339
340	if (!ptes)
341		return 0;
342	if (!(*gpt = alloc_read_gpt_header(state, lba)))
343		return 0;
344
345	/* Check the GUID Partition Table signature */
346	if (le64_to_cpu((*gpt)->signature) != GPT_HEADER_SIGNATURE) {
347		pr_debug("GUID Partition Table Header signature is wrong:"
348			 "%lld != %lld\n",
349			 (unsigned long long)le64_to_cpu((*gpt)->signature),
350			 (unsigned long long)GPT_HEADER_SIGNATURE);
351		goto fail;
352	}
353
354	/* Check the GUID Partition Table header size is too big */
355	if (le32_to_cpu((*gpt)->header_size) >
356			queue_logical_block_size(state->disk->queue)) {
357		pr_debug("GUID Partition Table Header size is too large: %u > %u\n",
358			le32_to_cpu((*gpt)->header_size),
359			queue_logical_block_size(state->disk->queue));
360		goto fail;
361	}
362
363	/* Check the GUID Partition Table header size is too small */
364	if (le32_to_cpu((*gpt)->header_size) < sizeof(gpt_header)) {
365		pr_debug("GUID Partition Table Header size is too small: %u < %zu\n",
366			le32_to_cpu((*gpt)->header_size),
367			sizeof(gpt_header));
368		goto fail;
369	}
370
371	/* Check the GUID Partition Table CRC */
372	origcrc = le32_to_cpu((*gpt)->header_crc32);
373	(*gpt)->header_crc32 = 0;
374	crc = efi_crc32((const unsigned char *) (*gpt), le32_to_cpu((*gpt)->header_size));
375
376	if (crc != origcrc) {
377		pr_debug("GUID Partition Table Header CRC is wrong: %x != %x\n",
378			 crc, origcrc);
379		goto fail;
380	}
381	(*gpt)->header_crc32 = cpu_to_le32(origcrc);
382
383	/* Check that the my_lba entry points to the LBA that contains
384	 * the GUID Partition Table */
385	if (le64_to_cpu((*gpt)->my_lba) != lba) {
386		pr_debug("GPT my_lba incorrect: %lld != %lld\n",
387			 (unsigned long long)le64_to_cpu((*gpt)->my_lba),
388			 (unsigned long long)lba);
389		goto fail;
390	}
391
392	/* Check the first_usable_lba and last_usable_lba are
393	 * within the disk.
394	 */
395	lastlba = last_lba(state->disk);
396	if (le64_to_cpu((*gpt)->first_usable_lba) > lastlba) {
397		pr_debug("GPT: first_usable_lba incorrect: %lld > %lld\n",
398			 (unsigned long long)le64_to_cpu((*gpt)->first_usable_lba),
399			 (unsigned long long)lastlba);
400		goto fail;
401	}
402	if (le64_to_cpu((*gpt)->last_usable_lba) > lastlba) {
403		pr_debug("GPT: last_usable_lba incorrect: %lld > %lld\n",
404			 (unsigned long long)le64_to_cpu((*gpt)->last_usable_lba),
405			 (unsigned long long)lastlba);
406		goto fail;
407	}
408	if (le64_to_cpu((*gpt)->last_usable_lba) < le64_to_cpu((*gpt)->first_usable_lba)) {
409		pr_debug("GPT: last_usable_lba incorrect: %lld > %lld\n",
410			 (unsigned long long)le64_to_cpu((*gpt)->last_usable_lba),
411			 (unsigned long long)le64_to_cpu((*gpt)->first_usable_lba));
412		goto fail;
413	}
414	/* Check that sizeof_partition_entry has the correct value */
415	if (le32_to_cpu((*gpt)->sizeof_partition_entry) != sizeof(gpt_entry)) {
416		pr_debug("GUID Partition Entry Size check failed.\n");
417		goto fail;
418	}
419
420	/* Sanity check partition table size */
421	pt_size = (u64)le32_to_cpu((*gpt)->num_partition_entries) *
422		le32_to_cpu((*gpt)->sizeof_partition_entry);
423	if (pt_size > KMALLOC_MAX_SIZE) {
424		pr_debug("GUID Partition Table is too large: %llu > %lu bytes\n",
425			 (unsigned long long)pt_size, KMALLOC_MAX_SIZE);
426		goto fail;
427	}
428
429	if (!(*ptes = alloc_read_gpt_entries(state, *gpt)))
430		goto fail;
431
432	/* Check the GUID Partition Entry Array CRC */
433	crc = efi_crc32((const unsigned char *) (*ptes), pt_size);
434
435	if (crc != le32_to_cpu((*gpt)->partition_entry_array_crc32)) {
436		pr_debug("GUID Partition Entry Array CRC check failed.\n");
437		goto fail_ptes;
438	}
439
440	/* We're done, all's well */
441	return 1;
442
443 fail_ptes:
444	kfree(*ptes);
445	*ptes = NULL;
446 fail:
447	kfree(*gpt);
448	*gpt = NULL;
449	return 0;
450}
451
452/**
453 * is_pte_valid() - tests one PTE for validity
454 * @pte:pte to check
455 * @lastlba: last lba of the disk
456 *
457 * Description: returns 1 if valid,  0 on error.
458 */
459static inline int
460is_pte_valid(const gpt_entry *pte, const u64 lastlba)
461{
462	if ((!efi_guidcmp(pte->partition_type_guid, NULL_GUID)) ||
463	    le64_to_cpu(pte->starting_lba) > lastlba         ||
464	    le64_to_cpu(pte->ending_lba)   > lastlba)
465		return 0;
466	return 1;
467}
468
469/**
470 * compare_gpts() - Search disk for valid GPT headers and PTEs
471 * @pgpt: primary GPT header
472 * @agpt: alternate GPT header
473 * @lastlba: last LBA number
474 *
475 * Description: Returns nothing.  Sanity checks pgpt and agpt fields
476 * and prints warnings on discrepancies.
477 * 
478 */
479static void
480compare_gpts(gpt_header *pgpt, gpt_header *agpt, u64 lastlba)
481{
482	int error_found = 0;
483	if (!pgpt || !agpt)
484		return;
485	if (le64_to_cpu(pgpt->my_lba) != le64_to_cpu(agpt->alternate_lba)) {
486		pr_warn("GPT:Primary header LBA != Alt. header alternate_lba\n");
487		pr_warn("GPT:%lld != %lld\n",
488		       (unsigned long long)le64_to_cpu(pgpt->my_lba),
489                       (unsigned long long)le64_to_cpu(agpt->alternate_lba));
490		error_found++;
491	}
492	if (le64_to_cpu(pgpt->alternate_lba) != le64_to_cpu(agpt->my_lba)) {
493		pr_warn("GPT:Primary header alternate_lba != Alt. header my_lba\n");
494		pr_warn("GPT:%lld != %lld\n",
495		       (unsigned long long)le64_to_cpu(pgpt->alternate_lba),
496                       (unsigned long long)le64_to_cpu(agpt->my_lba));
497		error_found++;
498	}
499	if (le64_to_cpu(pgpt->first_usable_lba) !=
500            le64_to_cpu(agpt->first_usable_lba)) {
501		pr_warn("GPT:first_usable_lbas don't match.\n");
502		pr_warn("GPT:%lld != %lld\n",
503		       (unsigned long long)le64_to_cpu(pgpt->first_usable_lba),
504                       (unsigned long long)le64_to_cpu(agpt->first_usable_lba));
505		error_found++;
506	}
507	if (le64_to_cpu(pgpt->last_usable_lba) !=
508            le64_to_cpu(agpt->last_usable_lba)) {
509		pr_warn("GPT:last_usable_lbas don't match.\n");
510		pr_warn("GPT:%lld != %lld\n",
511		       (unsigned long long)le64_to_cpu(pgpt->last_usable_lba),
512                       (unsigned long long)le64_to_cpu(agpt->last_usable_lba));
513		error_found++;
514	}
515	if (efi_guidcmp(pgpt->disk_guid, agpt->disk_guid)) {
516		pr_warn("GPT:disk_guids don't match.\n");
517		error_found++;
518	}
519	if (le32_to_cpu(pgpt->num_partition_entries) !=
520            le32_to_cpu(agpt->num_partition_entries)) {
521		pr_warn("GPT:num_partition_entries don't match: "
522		       "0x%x != 0x%x\n",
523		       le32_to_cpu(pgpt->num_partition_entries),
524		       le32_to_cpu(agpt->num_partition_entries));
525		error_found++;
526	}
527	if (le32_to_cpu(pgpt->sizeof_partition_entry) !=
528            le32_to_cpu(agpt->sizeof_partition_entry)) {
529		pr_warn("GPT:sizeof_partition_entry values don't match: "
530		       "0x%x != 0x%x\n",
531                       le32_to_cpu(pgpt->sizeof_partition_entry),
532		       le32_to_cpu(agpt->sizeof_partition_entry));
533		error_found++;
534	}
535	if (le32_to_cpu(pgpt->partition_entry_array_crc32) !=
536            le32_to_cpu(agpt->partition_entry_array_crc32)) {
537		pr_warn("GPT:partition_entry_array_crc32 values don't match: "
538		       "0x%x != 0x%x\n",
539                       le32_to_cpu(pgpt->partition_entry_array_crc32),
540		       le32_to_cpu(agpt->partition_entry_array_crc32));
541		error_found++;
542	}
543	if (le64_to_cpu(pgpt->alternate_lba) != lastlba) {
544		pr_warn("GPT:Primary header thinks Alt. header is not at the end of the disk.\n");
545		pr_warn("GPT:%lld != %lld\n",
546			(unsigned long long)le64_to_cpu(pgpt->alternate_lba),
547			(unsigned long long)lastlba);
548		error_found++;
549	}
550
551	if (le64_to_cpu(agpt->my_lba) != lastlba) {
552		pr_warn("GPT:Alternate GPT header not at the end of the disk.\n");
553		pr_warn("GPT:%lld != %lld\n",
554			(unsigned long long)le64_to_cpu(agpt->my_lba),
555			(unsigned long long)lastlba);
556		error_found++;
557	}
558
559	if (error_found)
560		pr_warn("GPT: Use GNU Parted to correct GPT errors.\n");
561	return;
562}
563
564/**
565 * find_valid_gpt() - Search disk for valid GPT headers and PTEs
566 * @state: disk parsed partitions
567 * @gpt: GPT header ptr, filled on return.
568 * @ptes: PTEs ptr, filled on return.
569 *
570 * Description: Returns 1 if valid, 0 on error.
571 * If valid, returns pointers to newly allocated GPT header and PTEs.
572 * Validity depends on PMBR being valid (or being overridden by the
573 * 'gpt' kernel command line option) and finding either the Primary
574 * GPT header and PTEs valid, or the Alternate GPT header and PTEs
575 * valid.  If the Primary GPT header is not valid, the Alternate GPT header
576 * is not checked unless the 'gpt' kernel command line option is passed.
577 * This protects against devices which misreport their size, and forces
578 * the user to decide to use the Alternate GPT.
579 */
580static int find_valid_gpt(struct parsed_partitions *state, gpt_header **gpt,
581			  gpt_entry **ptes)
582{
583	int good_pgpt = 0, good_agpt = 0, good_pmbr = 0;
584	gpt_header *pgpt = NULL, *agpt = NULL;
585	gpt_entry *pptes = NULL, *aptes = NULL;
586	legacy_mbr *legacymbr;
587	struct gendisk *disk = state->disk;
588	const struct block_device_operations *fops = disk->fops;
589	sector_t total_sectors = get_capacity(state->disk);
590	u64 lastlba;
591
592	if (!ptes)
593		return 0;
594
595	lastlba = last_lba(state->disk);
596        if (!force_gpt) {
597		/* This will be added to the EFI Spec. per Intel after v1.02. */
598		legacymbr = kzalloc(sizeof(*legacymbr), GFP_KERNEL);
599		if (!legacymbr)
600			goto fail;
601
602		read_lba(state, 0, (u8 *)legacymbr, sizeof(*legacymbr));
603		good_pmbr = is_pmbr_valid(legacymbr, total_sectors);
604		kfree(legacymbr);
605
606		if (!good_pmbr)
607			goto fail;
608
609		pr_debug("Device has a %s MBR\n",
610			 good_pmbr == GPT_MBR_PROTECTIVE ?
611						"protective" : "hybrid");
612	}
613
614	good_pgpt = is_gpt_valid(state, GPT_PRIMARY_PARTITION_TABLE_LBA,
615				 &pgpt, &pptes);
616        if (good_pgpt)
617		good_agpt = is_gpt_valid(state,
618					 le64_to_cpu(pgpt->alternate_lba),
619					 &agpt, &aptes);
620        if (!good_agpt && force_gpt)
621                good_agpt = is_gpt_valid(state, lastlba, &agpt, &aptes);
622
623	if (!good_agpt && force_gpt && fops->alternative_gpt_sector) {
624		sector_t agpt_sector;
625		int err;
626
627		err = fops->alternative_gpt_sector(disk, &agpt_sector);
628		if (!err)
629			good_agpt = is_gpt_valid(state, agpt_sector,
630						 &agpt, &aptes);
631	}
632
633        /* The obviously unsuccessful case */
634        if (!good_pgpt && !good_agpt)
635                goto fail;
636
637        compare_gpts(pgpt, agpt, lastlba);
638
639        /* The good cases */
640        if (good_pgpt) {
641                *gpt  = pgpt;
642                *ptes = pptes;
643                kfree(agpt);
644                kfree(aptes);
645		if (!good_agpt)
646                        pr_warn("Alternate GPT is invalid, using primary GPT.\n");
647                return 1;
648        }
649        else if (good_agpt) {
650                *gpt  = agpt;
651                *ptes = aptes;
652                kfree(pgpt);
653                kfree(pptes);
654		pr_warn("Primary GPT is invalid, using alternate GPT.\n");
655                return 1;
656        }
657
658 fail:
659        kfree(pgpt);
660        kfree(agpt);
661        kfree(pptes);
662        kfree(aptes);
663        *gpt = NULL;
664        *ptes = NULL;
665        return 0;
666}
667
668/**
669 * utf16_le_to_7bit(): Naively converts a UTF-16LE string to 7-bit ASCII characters
670 * @in: input UTF-16LE string
671 * @size: size of the input string
672 * @out: output string ptr, should be capable to store @size+1 characters
673 *
674 * Description: Converts @size UTF16-LE symbols from @in string to 7-bit
675 * ASCII characters and stores them to @out. Adds trailing zero to @out array.
676 */
677static void utf16_le_to_7bit(const __le16 *in, unsigned int size, u8 *out)
678{
679	unsigned int i = 0;
680
681	out[size] = 0;
682
683	while (i < size) {
684		u8 c = le16_to_cpu(in[i]) & 0x7f;
685
686		if (c && !isprint(c))
687			c = '!';
688		out[i] = c;
689		i++;
690	}
691}
692
693/**
694 * efi_partition - scan for GPT partitions
695 * @state: disk parsed partitions
696 *
697 * Description: called from check.c, if the disk contains GPT
698 * partitions, sets up partition entries in the kernel.
699 *
700 * If the first block on the disk is a legacy MBR,
701 * it will get handled by msdos_partition().
702 * If it's a Protective MBR, we'll handle it here.
703 *
704 * We do not create a Linux partition for GPT, but
705 * only for the actual data partitions.
706 * Returns:
707 * -1 if unable to read the partition table
708 *  0 if this isn't our partition table
709 *  1 if successful
710 *
711 */
712int efi_partition(struct parsed_partitions *state)
713{
714	gpt_header *gpt = NULL;
715	gpt_entry *ptes = NULL;
716	u32 i;
717	unsigned ssz = queue_logical_block_size(state->disk->queue) / 512;
718
719	if (!find_valid_gpt(state, &gpt, &ptes) || !gpt || !ptes) {
720		kfree(gpt);
721		kfree(ptes);
722		return 0;
723	}
724
725	pr_debug("GUID Partition Table is valid!  Yea!\n");
726
727	for (i = 0; i < le32_to_cpu(gpt->num_partition_entries) && i < state->limit-1; i++) {
728		struct partition_meta_info *info;
729		unsigned label_max;
730		u64 start = le64_to_cpu(ptes[i].starting_lba);
731		u64 size = le64_to_cpu(ptes[i].ending_lba) -
732			   le64_to_cpu(ptes[i].starting_lba) + 1ULL;
733
734		if (!is_pte_valid(&ptes[i], last_lba(state->disk)))
735			continue;
736
737		put_partition(state, i+1, start * ssz, size * ssz);
738
739		/* If this is a RAID volume, tell md */
740		if (!efi_guidcmp(ptes[i].partition_type_guid, PARTITION_LINUX_RAID_GUID))
741			state->parts[i + 1].flags = ADDPART_FLAG_RAID;
742
743		info = &state->parts[i + 1].info;
744		efi_guid_to_str(&ptes[i].unique_partition_guid, info->uuid);
745
746		/* Naively convert UTF16-LE to 7 bits. */
747		label_max = min(ARRAY_SIZE(info->volname) - 1,
748				ARRAY_SIZE(ptes[i].partition_name));
749		utf16_le_to_7bit(ptes[i].partition_name, label_max, info->volname);
750		state->parts[i + 1].has_info = true;
751	}
752	kfree(ptes);
753	kfree(gpt);
754	strlcat(state->pp_buf, "\n", PAGE_SIZE);
755	return 1;
756}
~/ Read-only file preview.