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