f7fefb70e6f2ffbe970e619a608b67cbbe932b01
[platform/upstream/multipath-tools.git] / kpartx / gpt.c
1 /*
2     gpt.[ch]
3
4     Copyright (C) 2000-2001 Dell Computer Corporation <Matt_Domsch@dell.com>
5
6     EFI GUID Partition Table handling
7     Per Intel EFI Specification v1.02
8     http://developer.intel.com/technology/efi/efi.htm
9
10     This program is free software; you can redistribute it and/or modify
11     it under the terms of the GNU General Public License as published by
12     the Free Software Foundation; either version 2 of the License, or
13     (at your option) any later version.
14
15     This program is distributed in the hope that it will be useful,
16     but WITHOUT ANY WARRANTY; without even the implied warranty of
17     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18     GNU General Public License for more details.
19
20     You should have received a copy of the GNU General Public License
21     along with this program.  If not, see <http://www.gnu.org/licenses/>.
22 */
23
24 #define _FILE_OFFSET_BITS 64
25
26 #include "gpt.h"
27 #include <stdio.h>
28 #include <string.h>
29 #include <stdlib.h>
30 #include <inttypes.h>
31 #include <sys/stat.h>
32 #include <sys/ioctl.h>
33 #include <fcntl.h>
34 #include <unistd.h>
35 #include <errno.h>
36 #include <endian.h>
37 #include <byteswap.h>
38 #include <linux/fs.h>
39 #include "crc32.h"
40 #include "kpartx.h"
41
42 #if BYTE_ORDER == LITTLE_ENDIAN
43 #  define __le16_to_cpu(x) (uint16_t)(x)
44 #  define __le32_to_cpu(x) (uint32_t)(x)
45 #  define __le64_to_cpu(x) (uint64_t)(x)
46 #  define __cpu_to_le32(x) (x)
47 #elif BYTE_ORDER == BIG_ENDIAN
48 #  define __le16_to_cpu(x) bswap_16(x)
49 #  define __le32_to_cpu(x) bswap_32(x)
50 #  define __le64_to_cpu(x) bswap_64(x)
51 #  define __cpu_to_le32(x) bswap_32(x)
52 #endif
53
54 #ifndef BLKGETLASTSECT
55 #define BLKGETLASTSECT  _IO(0x12,108)   /* get last sector of block device */
56 #endif
57 #ifndef BLKGETSIZE
58 #define BLKGETSIZE _IO(0x12,96)         /* return device size */
59 #endif
60 #ifndef BLKSSZGET
61 #define BLKSSZGET  _IO(0x12,104)        /* get block device sector size */
62 #endif
63 #ifndef BLKGETSIZE64
64 #define BLKGETSIZE64 _IOR(0x12,114,sizeof(uint64_t))    /* return device size in bytes (u64 *arg) */
65 #endif
66
67 struct blkdev_ioctl_param {
68         unsigned int block;
69         size_t content_length;
70         char * block_contents;
71 };
72
73 /**
74  * efi_crc32() - EFI version of crc32 function
75  * @buf: buffer to calculate crc32 of
76  * @len - length of buf
77  *
78  * Description: Returns EFI-style CRC32 value for @buf
79  *
80  * This function uses the little endian Ethernet polynomial
81  * but seeds the function with ~0, and xor's with ~0 at the end.
82  * Note, the EFI Specification, v1.02, has a reference to
83  * Dr. Dobbs Journal, May 1994 (actually it's in May 1992).
84  */
85 static inline uint32_t
86 efi_crc32(const void *buf, unsigned long len)
87 {
88         return (crc32(~0L, buf, len) ^ ~0L);
89 }
90
91 /**
92  * is_pmbr_valid(): test Protective MBR for validity
93  * @mbr: pointer to a legacy mbr structure
94  *
95  * Description: Returns 1 if PMBR is valid, 0 otherwise.
96  * Validity depends on two things:
97  *  1) MSDOS signature is in the last two bytes of the MBR
98  *  2) One partition of type 0xEE is found
99  */
100 static int
101 is_pmbr_valid(legacy_mbr *mbr)
102 {
103         int i, found = 0, signature = 0;
104         if (!mbr)
105                 return 0;
106         signature = (__le16_to_cpu(mbr->signature) == MSDOS_MBR_SIGNATURE);
107         for (i = 0; signature && i < 4; i++) {
108                 if (mbr->partition[i].sys_type ==
109                     EFI_PMBR_OSTYPE_EFI_GPT) {
110                         found = 1;
111                         break;
112                 }
113         }
114         return (signature && found);
115 }
116
117
118 /************************************************************
119  * _get_num_sectors
120  * Requires:
121  *  - filedes is an open file descriptor, suitable for reading
122  * Modifies: nothing
123  * Returns:
124  *  Last LBA value on success
125  *  0 on error
126  *
127  * Try getting BLKGETSIZE64 and BLKSSZGET first,
128  * then BLKGETSIZE if necessary.
129  *  Kernels 2.4.15-2.4.18 and 2.5.0-2.5.3 have a broken BLKGETSIZE64
130  *  which returns the number of 512-byte sectors, not the size of
131  *  the disk in bytes. Fixed in kernels 2.4.18-pre8 and 2.5.4-pre3.
132  ************************************************************/
133 static uint64_t
134 _get_num_sectors(int filedes)
135 {
136         int rc;
137         uint64_t bytes=0;
138
139         rc = ioctl(filedes, BLKGETSIZE64, &bytes);
140         if (!rc)
141                 return bytes / get_sector_size(filedes);
142
143         return 0;
144 }
145
146 /************************************************************
147  * last_lba(): return number of last logical block of device
148  *
149  * @fd
150  *
151  * Description: returns Last LBA value on success, 0 on error.
152  * Notes: The value st_blocks gives the size of the file
153  *        in 512-byte blocks, which is OK if
154  *        EFI_BLOCK_SIZE_SHIFT == 9.
155  ************************************************************/
156
157 static uint64_t
158 last_lba(int filedes)
159 {
160         int rc;
161         uint64_t sectors = 0;
162         struct stat s;
163         memset(&s, 0, sizeof (s));
164         rc = fstat(filedes, &s);
165         if (rc == -1) {
166                 fprintf(stderr, "last_lba() could not stat: %s\n",
167                         strerror(errno));
168                 return 0;
169         }
170
171         if (S_ISBLK(s.st_mode)) {
172                 sectors = _get_num_sectors(filedes);
173         } else {
174                 fprintf(stderr,
175                         "last_lba(): I don't know how to handle files with mode %x\n",
176                         s.st_mode);
177                 sectors = 1;
178         }
179
180         return sectors ? sectors - 1 : 0;
181 }
182
183
184 static ssize_t
185 read_lastoddsector(int fd, void *buffer, size_t count)
186 {
187         int rc;
188         struct blkdev_ioctl_param ioctl_param;
189
190         if (!buffer) return 0;
191
192         ioctl_param.block = 0; /* read the last sector */
193         ioctl_param.content_length = count;
194         ioctl_param.block_contents = buffer;
195
196         rc = ioctl(fd, BLKGETLASTSECT, &ioctl_param);
197         if (rc == -1) perror("read failed");
198
199         return !rc;
200 }
201
202 static ssize_t
203 read_lba(int fd, uint64_t lba, void *buffer, size_t bytes)
204 {
205         int sector_size = get_sector_size(fd);
206         off_t offset = lba * sector_size;
207         uint64_t lastlba;
208         ssize_t bytesread;
209
210         if (lseek(fd, offset, SEEK_SET) < 0)
211                 return 0;
212         bytesread = read(fd, buffer, bytes);
213
214         lastlba = last_lba(fd);
215         if (!lastlba)
216                 return bytesread;
217
218         /* Kludge.  This is necessary to read/write the last
219            block of an odd-sized disk, until Linux 2.5.x kernel fixes.
220            This is only used by gpt.c, and only to read
221            one sector, so we don't have to be fancy.
222         */
223         if (!bytesread && !(lastlba & 1) && lba == lastlba) {
224                 bytesread = read_lastoddsector(fd, buffer, bytes);
225         }
226         return bytesread;
227 }
228
229 /**
230  * alloc_read_gpt_entries(): reads partition entries from disk
231  * @fd  is an open file descriptor to the whole disk
232  * @gpt is a buffer into which the GPT will be put
233  * Description: Returns ptes on success,  NULL on error.
234  * Allocates space for PTEs based on information found in @gpt.
235  * Notes: remember to free pte when you're done!
236  */
237 static gpt_entry *
238 alloc_read_gpt_entries(int fd, gpt_header * gpt)
239 {
240         gpt_entry *pte;
241         size_t count = __le32_to_cpu(gpt->num_partition_entries) *
242                 __le32_to_cpu(gpt->sizeof_partition_entry);
243
244         if (!count) return NULL;
245
246         if (aligned_malloc((void **)&pte, get_sector_size(fd), &count))
247                 return NULL;
248         memset(pte, 0, count);
249
250         if (!read_lba(fd, __le64_to_cpu(gpt->partition_entry_lba), pte,
251                       count)) {
252                 free(pte);
253                 return NULL;
254         }
255         return pte;
256 }
257
258 /**
259  * alloc_read_gpt_header(): Allocates GPT header, reads into it from disk
260  * @fd  is an open file descriptor to the whole disk
261  * @lba is the Logical Block Address of the partition table
262  *
263  * Description: returns GPT header on success, NULL on error.   Allocates
264  * and fills a GPT header starting at @ from @bdev.
265  * Note: remember to free gpt when finished with it.
266  */
267 static gpt_header *
268 alloc_read_gpt_header(int fd, uint64_t lba)
269 {
270         gpt_header *gpt;
271         size_t size = sizeof (gpt_header);
272         if (aligned_malloc((void **)&gpt, get_sector_size(fd), &size))
273                 return NULL;
274         memset(gpt, 0, size);
275         if (!read_lba(fd, lba, gpt, size)) {
276                 free(gpt);
277                 return NULL;
278         }
279
280         return gpt;
281 }
282
283 /**
284  * is_gpt_valid() - tests one GPT header and PTEs for validity
285  * @fd  is an open file descriptor to the whole disk
286  * @lba is the logical block address of the GPT header to test
287  * @gpt is a GPT header ptr, filled on return.
288  * @ptes is a PTEs ptr, filled on return.
289  *
290  * Description: returns 1 if valid,  0 on error.
291  * If valid, returns pointers to newly allocated GPT header and PTEs.
292  */
293 static int
294 is_gpt_valid(int fd, uint64_t lba,
295              gpt_header ** gpt, gpt_entry ** ptes)
296 {
297         int rc = 0;             /* default to not valid */
298         uint32_t crc, origcrc;
299
300         if (!gpt || !ptes)
301                 return 0;
302         if (!(*gpt = alloc_read_gpt_header(fd, lba)))
303                 return 0;
304
305         /* Check the GUID Partition Table signature */
306         if (__le64_to_cpu((*gpt)->signature) != GPT_HEADER_SIGNATURE) {
307                 /*
308                    printf("GUID Partition Table Header signature is wrong: %" PRIx64" != %" PRIx64 "\n",
309                    __le64_to_cpu((*gpt)->signature), GUID_PT_HEADER_SIGNATURE);
310                  */
311                 free(*gpt);
312                 *gpt = NULL;
313                 return rc;
314         }
315
316         /* Check the GUID Partition Table Header CRC */
317         origcrc = __le32_to_cpu((*gpt)->header_crc32);
318         (*gpt)->header_crc32 = 0;
319         crc = efi_crc32(*gpt, __le32_to_cpu((*gpt)->header_size));
320         if (crc != origcrc) {
321                 // printf( "GPTH CRC check failed, %x != %x.\n", origcrc, crc);
322                 (*gpt)->header_crc32 = __cpu_to_le32(origcrc);
323                 free(*gpt);
324                 *gpt = NULL;
325                 return 0;
326         }
327         (*gpt)->header_crc32 = __cpu_to_le32(origcrc);
328
329         /* Check that the my_lba entry points to the LBA
330          * that contains the GPT we read */
331         if (__le64_to_cpu((*gpt)->my_lba) != lba) {
332                 /*
333                 printf( "my_lba % PRIx64 "x != lba %"PRIx64 "x.\n",
334                                 __le64_to_cpu((*gpt)->my_lba), lba);
335                  */
336                 free(*gpt);
337                 *gpt = NULL;
338                 return 0;
339         }
340
341         /* Check that sizeof_partition_entry has the correct value */
342         if (__le32_to_cpu((*gpt)->sizeof_partition_entry) != sizeof(gpt_entry)) {
343                 // printf("GUID partition entry size check failed.\n");
344                 free(*gpt);
345                 *gpt = NULL;
346                 return 0;
347         }
348
349         if (!(*ptes = alloc_read_gpt_entries(fd, *gpt))) {
350                 free(*gpt);
351                 *gpt = NULL;
352                 return 0;
353         }
354
355         /* Check the GUID Partition Entry Array CRC */
356         crc = efi_crc32(*ptes,
357                         __le32_to_cpu((*gpt)->num_partition_entries) *
358                         __le32_to_cpu((*gpt)->sizeof_partition_entry));
359         if (crc != __le32_to_cpu((*gpt)->partition_entry_array_crc32)) {
360                 // printf("GUID Partitition Entry Array CRC check failed.\n");
361                 free(*gpt);
362                 *gpt = NULL;
363                 free(*ptes);
364                 *ptes = NULL;
365                 return 0;
366         }
367
368         /* We're done, all's well */
369         return 1;
370 }
371 /**
372  * compare_gpts() - Search disk for valid GPT headers and PTEs
373  * @pgpt is the primary GPT header
374  * @agpt is the alternate GPT header
375  * @lastlba is the last LBA number
376  * Description: Returns nothing.  Sanity checks pgpt and agpt fields
377  * and prints warnings on discrepancies.
378  *
379  */
380 static void
381 compare_gpts(gpt_header *pgpt, gpt_header *agpt, uint64_t lastlba)
382 {
383         int error_found = 0;
384         if (!pgpt || !agpt)
385                 return;
386         if (__le64_to_cpu(pgpt->my_lba) != __le64_to_cpu(agpt->alternate_lba)) {
387                 error_found++;
388                 fprintf(stderr,
389                        "GPT:Primary header LBA != Alt. header alternate_lba\n");
390 #ifdef DEBUG
391                 fprintf(stderr,  "GPT:%" PRIx64 " != %" PRIx64 "\n",
392                        __le64_to_cpu(pgpt->my_lba),
393                        __le64_to_cpu(agpt->alternate_lba));
394 #endif
395         }
396         if (__le64_to_cpu(pgpt->alternate_lba) != __le64_to_cpu(agpt->my_lba)) {
397                 error_found++;
398                 fprintf(stderr,
399                        "GPT:Primary header alternate_lba != Alt. header my_lba\n");
400 #ifdef DEBUG
401                 fprintf(stderr,  "GPT:%" PRIx64 " != %" PRIx64 "\n",
402                        __le64_to_cpu(pgpt->alternate_lba),
403                        __le64_to_cpu(agpt->my_lba));
404 #endif
405         }
406         if (__le64_to_cpu(pgpt->first_usable_lba) !=
407             __le64_to_cpu(agpt->first_usable_lba)) {
408                 error_found++;
409                 fprintf(stderr,  "GPT:first_usable_lbas don't match.\n");
410 #ifdef DEBUG
411                 fprintf(stderr,  "GPT:%" PRIx64 " != %" PRIx64 "\n",
412                        __le64_to_cpu(pgpt->first_usable_lba),
413                        __le64_to_cpu(agpt->first_usable_lba));
414 #endif
415         }
416         if (__le64_to_cpu(pgpt->last_usable_lba) !=
417             __le64_to_cpu(agpt->last_usable_lba)) {
418                 error_found++;
419                 fprintf(stderr,  "GPT:last_usable_lbas don't match.\n");
420 #ifdef DEBUG
421                 fprintf(stderr,  "GPT:%" PRIx64 " != %" PRIx64 "\n",
422                        __le64_to_cpu(pgpt->last_usable_lba),
423                        __le64_to_cpu(agpt->last_usable_lba));
424 #endif
425         }
426         if (efi_guidcmp(pgpt->disk_guid, agpt->disk_guid)) {
427                 error_found++;
428                 fprintf(stderr,  "GPT:disk_guids don't match.\n");
429         }
430         if (__le32_to_cpu(pgpt->num_partition_entries) !=
431             __le32_to_cpu(agpt->num_partition_entries)) {
432                 error_found++;
433                 fprintf(stderr,  "GPT:num_partition_entries don't match: "
434                        "0x%x != 0x%x\n",
435                        __le32_to_cpu(pgpt->num_partition_entries),
436                        __le32_to_cpu(agpt->num_partition_entries));
437         }
438         if (__le32_to_cpu(pgpt->sizeof_partition_entry) !=
439             __le32_to_cpu(agpt->sizeof_partition_entry)) {
440                 error_found++;
441                 fprintf(stderr,
442                        "GPT:sizeof_partition_entry values don't match: "
443                        "0x%x != 0x%x\n",
444                        __le32_to_cpu(pgpt->sizeof_partition_entry),
445                        __le32_to_cpu(agpt->sizeof_partition_entry));
446         }
447         if (__le32_to_cpu(pgpt->partition_entry_array_crc32) !=
448             __le32_to_cpu(agpt->partition_entry_array_crc32)) {
449                 error_found++;
450                 fprintf(stderr,
451                        "GPT:partition_entry_array_crc32 values don't match: "
452                        "0x%x != 0x%x\n",
453                        __le32_to_cpu(pgpt->partition_entry_array_crc32),
454                        __le32_to_cpu(agpt->partition_entry_array_crc32));
455         }
456         if (__le64_to_cpu(pgpt->alternate_lba) != lastlba) {
457                 error_found++;
458                 fprintf(stderr,
459                        "GPT:Primary header thinks Alt. header is not at the end of the disk.\n");
460 #ifdef DEBUG
461                 fprintf(stderr,  "GPT:%" PRIx64 " != %" PRIx64 "\n",
462                        __le64_to_cpu(pgpt->alternate_lba), lastlba);
463 #endif
464         }
465
466         if (__le64_to_cpu(agpt->my_lba) != lastlba) {
467                 error_found++;
468                 fprintf(stderr,
469                        "GPT:Alternate GPT header not at the end of the disk.\n");
470 #ifdef DEBUG
471                 fprintf(stderr,  "GPT:%" PRIx64 " != %" PRIx64 "\n",
472                        __le64_to_cpu(agpt->my_lba), lastlba);
473 #endif
474         }
475
476         if (error_found)
477                 fprintf(stderr,
478                        "GPT: Use GNU Parted to correct GPT errors.\n");
479         return;
480 }
481
482 /**
483  * find_valid_gpt() - Search disk for valid GPT headers and PTEs
484  * @fd  is an open file descriptor to the whole disk
485  * @gpt is a GPT header ptr, filled on return.
486  * @ptes is a PTEs ptr, filled on return.
487  * Description: Returns 1 if valid, 0 on error.
488  * If valid, returns pointers to newly allocated GPT header and PTEs.
489  * Validity depends on finding either the Primary GPT header and PTEs valid,
490  * or the Alternate GPT header and PTEs valid, and the PMBR valid.
491  */
492 static int
493 find_valid_gpt(int fd, gpt_header ** gpt, gpt_entry ** ptes)
494 {
495         int good_pgpt = 0, good_agpt = 0, good_pmbr = 0;
496         gpt_header *pgpt = NULL, *agpt = NULL;
497         gpt_entry *pptes = NULL, *aptes = NULL;
498         legacy_mbr *legacymbr = NULL;
499         size_t size = sizeof(legacy_mbr);
500         uint64_t lastlba;
501         if (!gpt || !ptes)
502                 return 0;
503
504         if (!(lastlba = last_lba(fd)))
505                 return 0;
506         good_pgpt = is_gpt_valid(fd, GPT_PRIMARY_PARTITION_TABLE_LBA,
507                                  &pgpt, &pptes);
508         if (good_pgpt) {
509                 good_agpt = is_gpt_valid(fd,
510                                          __le64_to_cpu(pgpt->alternate_lba),
511                                          &agpt, &aptes);
512                 if (!good_agpt) {
513                         good_agpt = is_gpt_valid(fd, lastlba,
514                                                  &agpt, &aptes);
515                 }
516         }
517         else {
518                 good_agpt = is_gpt_valid(fd, lastlba,
519                                          &agpt, &aptes);
520         }
521
522         /* The obviously unsuccessful case */
523         if (!good_pgpt && !good_agpt) {
524                 goto fail;
525         }
526
527         /* This will be added to the EFI Spec. per Intel after v1.02. */
528         if (aligned_malloc((void **)&legacymbr, get_sector_size(fd),
529                            &size) == 0) {
530                 memset(legacymbr, 0, size);
531                 read_lba(fd, 0, (uint8_t *) legacymbr, size);
532                 good_pmbr = is_pmbr_valid(legacymbr);
533                 free(legacymbr);
534                 legacymbr=NULL;
535         }
536
537         /* Failure due to bad PMBR */
538         if ((good_pgpt || good_agpt) && !good_pmbr && !force_gpt) {
539                 fprintf(stderr,
540                        "  Warning: Disk has a valid GPT signature "
541                        "but invalid PMBR.\n"
542                        "  Assuming this disk is *not* a GPT disk anymore.\n"
543                        "  Use gpt kernel option to override.  "
544                        "Use GNU Parted to correct disk.\n");
545                 goto fail;
546         }
547
548         /* Would fail due to bad PMBR, but force GPT anyhow */
549         if ((good_pgpt || good_agpt) && !good_pmbr && force_gpt) {
550                 fprintf(stderr,
551                        "  Warning: Disk has a valid GPT signature but "
552                        "invalid PMBR.\n"
553                        "  Use GNU Parted to correct disk.\n"
554                        "  gpt option taken, disk treated as GPT.\n");
555         }
556
557         compare_gpts(pgpt, agpt, lastlba);
558
559         /* The good cases */
560         if (good_pgpt && (good_pmbr || force_gpt)) {
561                 *gpt  = pgpt;
562                 *ptes = pptes;
563                 if (agpt)  { free(agpt);   agpt = NULL; }
564                 if (aptes) { free(aptes); aptes = NULL; }
565                 if (!good_agpt) {
566                         fprintf(stderr,
567                                "Alternate GPT is invalid, "
568                                "using primary GPT.\n");
569                 }
570                 return 1;
571         }
572         else if (good_agpt && (good_pmbr || force_gpt)) {
573                 *gpt  = agpt;
574                 *ptes = aptes;
575                 if (pgpt)  { free(pgpt);   pgpt = NULL; }
576                 if (pptes) { free(pptes); pptes = NULL; }
577                 fprintf(stderr,
578                        "Primary GPT is invalid, using alternate GPT.\n");
579                 return 1;
580         }
581
582 fail:
583         if (pgpt)  { free(pgpt);   pgpt=NULL; }
584         if (agpt)  { free(agpt);   agpt=NULL; }
585         if (pptes) { free(pptes); pptes=NULL; }
586         if (aptes) { free(aptes); aptes=NULL; }
587         *gpt = NULL;
588         *ptes = NULL;
589         return 0;
590 }
591
592 /**
593  * read_gpt_pt()
594  * @fd
595  * @all - slice with start/size of whole disk
596  *
597  *  0 if this isn't our partition table
598  *  number of partitions if successful
599  *
600  */
601 int
602 read_gpt_pt (int fd, __attribute__((unused)) struct slice all,
603              struct slice *sp, unsigned int ns)
604 {
605         gpt_header *gpt = NULL;
606         gpt_entry *ptes = NULL;
607         unsigned int i;
608         int n = 0;
609         int last_used_index=-1;
610         int sector_size_mul = get_sector_size(fd)/512;
611
612         if (!find_valid_gpt (fd, &gpt, &ptes) || !gpt || !ptes) {
613                 if (gpt)
614                         free (gpt);
615                 if (ptes)
616                         free (ptes);
617                 return 0;
618         }
619
620         for (i = 0; i < __le32_to_cpu(gpt->num_partition_entries) && i < ns; i++) {
621                 if (!efi_guidcmp (NULL_GUID, ptes[i].partition_type_guid)) {
622                         sp[n].start = 0;
623                         sp[n].size = 0;
624                         n++;
625                 } else {
626                         sp[n].start = sector_size_mul *
627                                       __le64_to_cpu(ptes[i].starting_lba);
628                         sp[n].size  = sector_size_mul *
629                                       (__le64_to_cpu(ptes[i].ending_lba) -
630                                        __le64_to_cpu(ptes[i].starting_lba) + 1);
631                         last_used_index=n;
632                         n++;
633                 }
634         }
635         free (ptes);
636         free (gpt);
637         return last_used_index+1;
638 }