~ubuntu-branches/ubuntu/trusty/util-linux/trusty-proposed

« back to all changes in this revision

Viewing changes to shlibs/blkid/src/partitions/gpt.c

  • Committer: Package Import Robot
  • Author(s): LaMont Jones
  • Date: 2011-11-03 15:38:23 UTC
  • mto: (4.5.5 sid) (1.6.4)
  • mto: This revision was merged to the branch mainline in revision 85.
  • Revision ID: package-import@ubuntu.com-20111103153823-10sx16jprzxlhkqf
ImportĀ upstreamĀ versionĀ 2.20.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * EFI GPT partition parsing code
3
 
 *
4
 
 * Copyright (C) 2009 Karel Zak <kzak@redhat.com>
5
 
 *
6
 
 * This file may be redistributed under the terms of the
7
 
 * GNU Lesser General Public License.
8
 
 *
9
 
 * This code is not copy & past from any other implementation.
10
 
 *
11
 
 * For more information about GPT start your study at:
12
 
 * http://en.wikipedia.org/wiki/GUID_Partition_Table
13
 
 * http://technet.microsoft.com/en-us/library/cc739412(WS.10).aspx
14
 
 */
15
 
#include <stdio.h>
16
 
#include <string.h>
17
 
#include <stdlib.h>
18
 
#include <stdint.h>
19
 
#include <stddef.h>
20
 
 
21
 
#include "partitions.h"
22
 
#include "crc32.h"
23
 
#include "dos.h"
24
 
 
25
 
#define GPT_PRIMARY_LBA 1
26
 
 
27
 
/* Signature - ā€œEFI PARTā€ */
28
 
#define GPT_HEADER_SIGNATURE 0x5452415020494645ULL
29
 
 
30
 
/* basic types */
31
 
typedef uint16_t efi_char16_t;
32
 
 
33
 
/* UUID */
34
 
typedef struct {
35
 
        uint32_t time_low;
36
 
        uint16_t time_mid;
37
 
        uint16_t time_hi_and_version;
38
 
        uint8_t clock_seq_hi;
39
 
        uint8_t clock_seq_low;
40
 
        uint8_t node[6];
41
 
} efi_guid_t;
42
 
 
43
 
 
44
 
#define GPT_UNUSED_ENTRY_GUID \
45
 
            ((efi_guid_t) { 0x00000000, 0x0000, 0x0000, 0x00, 0x00, \
46
 
                            { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }})
47
 
struct gpt_header {
48
 
        uint64_t        signature;              /* "EFI PART" */
49
 
        uint32_t        revision;
50
 
        uint32_t        header_size;            /* usualy 92 bytes */
51
 
        uint32_t        header_crc32;           /* checksum of header with this
52
 
                                                 * field zeroed during calculation */
53
 
        uint32_t        reserved1;
54
 
 
55
 
        uint64_t        my_lba;                 /* location of this header copy */
56
 
        uint64_t        alternate_lba;          /* location of the other header copy */
57
 
        uint64_t        first_usable_lba;       /* lirst usable LBA for partitions */
58
 
        uint64_t        last_usable_lba;        /* last usable LBA for partitions */
59
 
 
60
 
        efi_guid_t      disk_guid;              /* disk UUID */
61
 
 
62
 
        uint64_t        partition_entries_lba;  /* always 2 in primary header copy */
63
 
        uint32_t        num_partition_entries;
64
 
        uint32_t        sizeof_partition_entry;
65
 
        uint32_t        partition_entry_array_crc32;
66
 
 
67
 
        /*
68
 
         * The rest of the block is reserved by UEFI and must be zero. EFI
69
 
         * standard handles this by:
70
 
         *
71
 
         * uint8_t              reserved2[ BLKSSZGET - 92 ];
72
 
         *
73
 
         * This definition is useless in practice. It is necessary to read
74
 
         * whole block from the device rather than sizeof(struct gpt_header)
75
 
         * only.
76
 
         */
77
 
} __attribute__ ((packed));
78
 
 
79
 
/*** not used
80
 
struct gpt_entry_attributes {
81
 
        uint64_t        required_to_function:1;
82
 
        uint64_t        reserved:47;
83
 
        uint64_t        type_guid_specific:16;
84
 
} __attribute__ ((packed));
85
 
***/
86
 
 
87
 
struct gpt_entry {
88
 
        efi_guid_t      partition_type_guid;    /* type UUID */
89
 
        efi_guid_t      unique_partition_guid;  /* partition UUID */
90
 
        uint64_t        starting_lba;
91
 
        uint64_t        ending_lba;
92
 
 
93
 
        /*struct gpt_entry_attributes   attributes;*/
94
 
 
95
 
        uint64_t        attributes;
96
 
 
97
 
        efi_char16_t    partition_name[72 / sizeof(efi_char16_t)]; /* UTF-16LE string*/
98
 
} __attribute__ ((packed));
99
 
 
100
 
 
101
 
/*
102
 
 * EFI uses crc32 with ~0 seed and xor's with ~0 at the end.
103
 
 */
104
 
static inline uint32_t count_crc32(const unsigned char *buf, size_t len)
105
 
{
106
 
        return (crc32(~0L, buf, len) ^ ~0L);
107
 
}
108
 
 
109
 
static inline unsigned char *get_lba_buffer(blkid_probe pr,
110
 
                                        uint64_t lba, size_t bytes)
111
 
{
112
 
        return blkid_probe_get_buffer(pr,
113
 
                        blkid_probe_get_sectorsize(pr) * lba, bytes);
114
 
}
115
 
 
116
 
static inline int guidcmp(efi_guid_t left, efi_guid_t right)
117
 
{
118
 
        return memcmp(&left, &right, sizeof (efi_guid_t));
119
 
}
120
 
 
121
 
/*
122
 
 * UUID is traditionaly 16 byte big-endian array, except Intel EFI
123
 
 * specification where the UUID is a structure of little-endian fields.
124
 
 */
125
 
static void swap_efi_guid(efi_guid_t *uid)
126
 
{
127
 
        uid->time_low = swab32(uid->time_low);
128
 
        uid->time_mid = swab16(uid->time_mid);
129
 
        uid->time_hi_and_version = swab16(uid->time_hi_and_version);
130
 
}
131
 
 
132
 
static int last_lba(blkid_probe pr, uint64_t *lba)
133
 
{
134
 
        blkid_loff_t sz = blkid_probe_get_size(pr);
135
 
        if (sz < blkid_probe_get_sectorsize(pr))
136
 
                return -1;
137
 
 
138
 
        *lba = (sz >> 9) - 1;
139
 
        return 0;
140
 
}
141
 
 
142
 
/*
143
 
 * Protective (legacy) MBR.
144
 
 *
145
 
 * This MBR contains standard DOS partition table with a single partition, type
146
 
 * of 0xEE.  The partition usually encompassing the entire GPT drive - or 2TiB
147
 
 * for large disks.
148
 
 *
149
 
 * Note that Apple uses GPT/MBR hybrid disks, where the DOS partition table is
150
 
 * synchronized with GPT. This synchronization has many restriction of course
151
 
 * (due DOS PT limitations).
152
 
 *
153
 
 * Note that the PMBR detection is optional (enabled by default) and could be
154
 
 * disabled by BLKID_PARTS_FOPCE_GPT flag (see also blkid_paertitions_set_flags()).
155
 
 */
156
 
static int is_pmbr_valid(blkid_probe pr)
157
 
{
158
 
        int flags = blkid_partitions_get_flags(pr);
159
 
        unsigned char *data;
160
 
        struct dos_partition *p;
161
 
        int i;
162
 
 
163
 
        if (flags & BLKID_PARTS_FORCE_GPT)
164
 
                goto ok;                        /* skip PMBR check */
165
 
 
166
 
        data = blkid_probe_get_sector(pr, 0);
167
 
        if (!data)
168
 
                goto failed;
169
 
 
170
 
        if (!is_valid_mbr_signature(data))
171
 
                goto failed;
172
 
 
173
 
        p = (struct dos_partition *) (data + BLKID_MSDOS_PT_OFFSET);
174
 
 
175
 
        for (i = 0; i < 4; i++, p++) {
176
 
                if (p->sys_type == BLKID_GPT_PARTITION)
177
 
                        goto ok;
178
 
        }
179
 
failed:
180
 
        return 0;
181
 
ok:
182
 
        return 1;
183
 
}
184
 
 
185
 
/*
186
 
 * Reads GPT header to @hdr and returns a pointer to @hdr or NULL in case of
187
 
 * error. The function also returns GPT entries in @ents.
188
 
 *
189
 
 * Note, this function does not allocate any memory. The GPT header has fixed
190
 
 * size so we use stack, and @ents returns memory from libblkid buffer (so the
191
 
 * next blkid_probe_get_buffer() will overwrite this buffer).
192
 
 *
193
 
 * This function checks validity of header and entries array. A corrupted
194
 
 * header is not returned.
195
 
 */
196
 
static struct gpt_header *get_gpt_header(
197
 
                                blkid_probe pr, struct gpt_header *hdr,
198
 
                                struct gpt_entry **ents, uint64_t lba,
199
 
                                uint64_t lastlba)
200
 
{
201
 
        struct gpt_header *h;
202
 
        uint32_t crc, orgcrc;
203
 
        uint64_t lu, fu;
204
 
        size_t esz;
205
 
        uint32_t hsz, ssz;
206
 
 
207
 
        ssz = blkid_probe_get_sectorsize(pr);
208
 
 
209
 
        /* whole sector is allocated for GPT header */
210
 
        h = (struct gpt_header *) get_lba_buffer(pr, lba, ssz);
211
 
        if (!h)
212
 
                return NULL;
213
 
 
214
 
        if (le64_to_cpu(h->signature) != GPT_HEADER_SIGNATURE)
215
 
                return NULL;
216
 
 
217
 
        hsz = le32_to_cpu(h->header_size);
218
 
 
219
 
        /* EFI: The HeaderSize must be greater than 92 and must be less
220
 
         *      than or equal to the logical block size.
221
 
         */
222
 
        if (hsz > ssz || hsz < sizeof(*h))
223
 
                return NULL;
224
 
 
225
 
        /* Header has to be verified when header_crc32 is zero */
226
 
        orgcrc = le32_to_cpu(h->header_crc32);
227
 
        h->header_crc32 = 0;
228
 
 
229
 
        crc = count_crc32((unsigned char *) h, hsz);
230
 
        if (crc != orgcrc) {
231
 
                DBG(DEBUG_LOWPROBE, printf("GPT header corrupted\n"));
232
 
                return NULL;
233
 
        }
234
 
        h->header_crc32 = cpu_to_le32(orgcrc);
235
 
 
236
 
        /* Valid header has to be at MyLBA */
237
 
        if (le64_to_cpu(h->my_lba) != lba) {
238
 
                DBG(DEBUG_LOWPROBE, printf(
239
 
                        "GPT->MyLBA mismatch with real position\n"));
240
 
                return NULL;
241
 
        }
242
 
 
243
 
        fu = le64_to_cpu(h->first_usable_lba);
244
 
        lu = le64_to_cpu(h->last_usable_lba);
245
 
 
246
 
        /* Check if First and Last usable LBA makes sense */
247
 
        if (lu < fu || fu > lastlba || lu > lastlba) {
248
 
                DBG(DEBUG_LOWPROBE, printf(
249
 
                        "GPT->{First,Last}UsableLBA out of range\n"));
250
 
                return NULL;
251
 
        }
252
 
 
253
 
        /* The header has to be outside usable range */
254
 
        if (fu < lba && lba < lu) {
255
 
                DBG(DEBUG_LOWPROBE, printf("GPT header is inside usable area\n"));
256
 
                return NULL;
257
 
        }
258
 
 
259
 
        /* Size of blocks with GPT entries */
260
 
        esz = le32_to_cpu(h->num_partition_entries) *
261
 
                        le32_to_cpu(h->sizeof_partition_entry);
262
 
        if (!esz) {
263
 
                DBG(DEBUG_LOWPROBE, printf("GPT entries undefined\n"));
264
 
                return NULL;
265
 
        }
266
 
 
267
 
        /* The header seems valid, save it
268
 
         * (we don't care about zeros in hdr->reserved2 area) */
269
 
        memcpy(hdr, h, sizeof(*h));
270
 
        h = hdr;
271
 
 
272
 
        /* Read GPT entries */
273
 
        *ents = (struct gpt_entry *) get_lba_buffer(pr,
274
 
                                le64_to_cpu(h->partition_entries_lba), esz);
275
 
        if (!*ents) {
276
 
                DBG(DEBUG_LOWPROBE, printf("GPT entries unreadable\n"));
277
 
                return NULL;
278
 
        }
279
 
 
280
 
        /* Validate entries */
281
 
        crc = count_crc32((unsigned char *) *ents, esz);
282
 
        if (crc != le32_to_cpu(h->partition_entry_array_crc32)) {
283
 
                DBG(DEBUG_LOWPROBE, printf("GPT entries corrupted\n"));
284
 
                return NULL;
285
 
        }
286
 
 
287
 
        return h;
288
 
}
289
 
 
290
 
static int probe_gpt_pt(blkid_probe pr, const struct blkid_idmag *mag)
291
 
{
292
 
        uint64_t lastlba = 0, lba;
293
 
        struct gpt_header hdr, *h;
294
 
        struct gpt_entry *e;
295
 
        blkid_parttable tab = NULL;
296
 
        blkid_partlist ls;
297
 
        int i;
298
 
        uint64_t fu, lu;
299
 
        uint32_t ssf;
300
 
 
301
 
 
302
 
        if (last_lba(pr, &lastlba))
303
 
                goto nothing;
304
 
 
305
 
        if (!is_pmbr_valid(pr))
306
 
                goto nothing;
307
 
 
308
 
        h = get_gpt_header(pr, &hdr, &e, (lba = GPT_PRIMARY_LBA), lastlba);
309
 
        if (!h)
310
 
                h = get_gpt_header(pr, &hdr, &e, (lba = lastlba), lastlba);
311
 
 
312
 
        if (!h)
313
 
                goto nothing;
314
 
 
315
 
        blkid_probe_use_wiper(pr, lba * blkid_probe_get_size(pr), 8);
316
 
 
317
 
        if (blkid_partitions_need_typeonly(pr))
318
 
                /* caller does not ask for details about partitions */
319
 
                return 0;
320
 
 
321
 
        ls = blkid_probe_get_partlist(pr);
322
 
        if (!ls)
323
 
                goto err;
324
 
 
325
 
        tab = blkid_partlist_new_parttable(ls, "gpt", lba << 9);
326
 
        if (!tab)
327
 
                goto err;
328
 
 
329
 
        ssf = blkid_probe_get_sectorsize(pr) / 512;
330
 
 
331
 
        fu = le64_to_cpu(h->first_usable_lba);
332
 
        lu = le64_to_cpu(h->last_usable_lba);
333
 
 
334
 
        for (i = 0; i < le32_to_cpu(h->num_partition_entries); i++, e++) {
335
 
 
336
 
                blkid_partition par;
337
 
                uint64_t start = le64_to_cpu(e->starting_lba);
338
 
                uint64_t size = le64_to_cpu(e->ending_lba) -
339
 
                                        le64_to_cpu(e->starting_lba) + 1ULL;
340
 
 
341
 
                /* 00000000-0000-0000-0000-000000000000 entry */
342
 
                if (!guidcmp(e->partition_type_guid, GPT_UNUSED_ENTRY_GUID)) {
343
 
                        blkid_partlist_increment_partno(ls);
344
 
                        continue;
345
 
                }
346
 
                /* the partition has to inside usable range */
347
 
                if (start < fu || start + size - 1 > lu) {
348
 
                        DBG(DEBUG_LOWPROBE, printf(
349
 
                                "GPT entry[%d] overflows usable area - ignore\n",
350
 
                                i));
351
 
                        blkid_partlist_increment_partno(ls);
352
 
                        continue;
353
 
                }
354
 
 
355
 
                par = blkid_partlist_add_partition(ls, tab,
356
 
                                        start * ssf, size * ssf);
357
 
                if (!par)
358
 
                        goto err;
359
 
 
360
 
                blkid_partition_set_utf8name(par,
361
 
                        (unsigned char *) e->partition_name,
362
 
                        sizeof(e->partition_name), BLKID_ENC_UTF16LE);
363
 
 
364
 
                swap_efi_guid(&e->unique_partition_guid);
365
 
                swap_efi_guid(&e->partition_type_guid);
366
 
 
367
 
                blkid_partition_set_uuid(par,
368
 
                        (const unsigned char *) &e->unique_partition_guid);
369
 
 
370
 
                blkid_partition_set_type_uuid(par,
371
 
                        (const unsigned char *) &e->partition_type_guid);
372
 
 
373
 
                blkid_partition_set_flags(par, e->attributes);
374
 
        }
375
 
 
376
 
        return 0;
377
 
 
378
 
nothing:
379
 
        return 1;
380
 
err:
381
 
        return -1;
382
 
}
383
 
 
384
 
 
385
 
const struct blkid_idinfo gpt_pt_idinfo =
386
 
{
387
 
        .name           = "gpt",
388
 
        .probefunc      = probe_gpt_pt,
389
 
        .minsz          = 1024 * 1440 + 1,      /* ignore floppies */
390
 
 
391
 
        /*
392
 
         * It would be possible to check for DOS signature (0xAA55), but
393
 
         * unfortunately almost all EFI GPT implemenations allow to optionaly
394
 
         * skip the legacy MBR. We follows this behavior and MBR is optional.
395
 
         * See is_valid_pmbr().
396
 
         *
397
 
         * It means we have to always call probe_gpt_pt().
398
 
         */
399
 
        .magics         = BLKID_NONE_MAGIC
400
 
};
401