Merge remote-tracking branch 'origin/master' into chaindev
[profile/ivi/syslinux.git] / com32 / chain / partiter.c
1 /* ----------------------------------------------------------------------- *
2  *
3  *   Copyright 2003-2010 H. Peter Anvin - All Rights Reserved
4  *   Copyright 2010 Shao Miller
5  *   Copyright 2010 Michal Soltys
6  *
7  *   Permission is hereby granted, free of charge, to any person
8  *   obtaining a copy of this software and associated documentation
9  *   files (the "Software"), to deal in the Software without
10  *   restriction, including without limitation the rights to use,
11  *   copy, modify, merge, publish, distribute, sublicense, and/or
12  *   sell copies of the Software, and to permit persons to whom
13  *   the Software is furnished to do so, subject to the following
14  *   conditions:
15  *
16  *   The above copyright notice and this permission notice shall
17  *   be included in all copies or substantial portions of the Software.
18  *
19  *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20  *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
21  *   OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22  *   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
23  *   HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
24  *   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25  *   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
26  *   OTHER DEALINGS IN THE SOFTWARE.
27  *
28  * ----------------------------------------------------------------------- */
29
30 /*
31  * partiter.c
32  *
33  * Provides disk / partition iteration.
34  */
35
36 #include <stdlib.h>
37 #include <stdio.h>
38 #include <string.h>
39 #include <stdarg.h>
40 #include <zlib.h>
41 #include <syslinux/disk.h>
42 #include "common.h"
43 #include "partiter.h"
44 #include "utility.h"
45
46 #define ost_is_ext(type) ((type) == 0x05 || (type) == 0x0F || (type) == 0x85)
47 #define ost_is_nondata(type) (ost_is_ext(type) || (type) == 0x00)
48 #define sane(s,l) ((s)+(l) > (s))
49
50 /* forwards */
51
52 static int iter_ctor(struct part_iter *, va_list *);
53 static int iter_dos_ctor(struct part_iter *, va_list *);
54 static int iter_gpt_ctor(struct part_iter *, va_list *);
55 static void iter_dtor(struct part_iter *);
56 static struct part_iter *pi_dos_next(struct part_iter *);
57 static struct part_iter *pi_gpt_next(struct part_iter *);
58 static struct part_iter *pi_raw_next(struct part_iter *);
59
60 static struct itertype types[] = {
61    [0] = {
62         .ctor = &iter_dos_ctor,
63         .dtor = &iter_dtor,
64         .next = &pi_dos_next,
65 }, [1] = {
66         .ctor = &iter_gpt_ctor,
67         .dtor = &iter_dtor,
68         .next = &pi_gpt_next,
69 }, [2] = {
70         .ctor = &iter_ctor,
71         .dtor = &iter_dtor,
72         .next = &pi_raw_next,
73 }};
74
75 const struct itertype * const typedos = types;
76 const struct itertype * const typegpt = types+1;
77 const struct itertype * const typeraw = types+2;
78
79 #ifdef DEBUG
80 static int inv_type(const void *type)
81 {
82     int i, cnt = sizeof(types)/sizeof(types[0]);
83     for (i = 0; i < cnt; i++) {
84         if (type == types + i)
85             return 0;
86     }
87     return -1;
88 }
89 #endif
90
91 /**
92  * iter_ctor() - common iterator initialization
93  * @iter:       iterator pointer
94  * @args(0):    disk_info structure used for disk functions
95  * @args(1):    stepall modifier
96  *
97  * Second and further arguments are passed as a pointer to va_list
98  **/
99 static int iter_ctor(struct part_iter *iter, va_list *args)
100 {
101     const struct disk_info *di = va_arg(*args, const struct disk_info *);
102     int stepall = va_arg(*args, int);
103
104 #ifdef DEBUG
105     if (!di)
106         return -1;
107 #endif
108
109     memcpy(&iter->di, di, sizeof(struct disk_info));
110     iter->stepall = stepall;
111     iter->index0 = -1;
112     iter->length = di->lbacnt;
113
114     return 0;
115 }
116
117 /**
118  * iter_dtor() - common iterator cleanup
119  * @iter:       iterator pointer
120  *
121  **/
122 static void iter_dtor(struct part_iter *iter)
123 {
124     free(iter->data);
125 }
126
127 /**
128  * iter_dos_ctor() - MBR/EBR iterator specific initialization
129  * @iter:       iterator pointer
130  * @args(0):    disk_info structure used for disk functions
131  * @args(1):    pointer to buffer with loaded valid MBR
132  *
133  * Second and further arguments are passed as a pointer to va_list.
134  * This function only makes rudimentary checks. If user uses
135  * pi_new(), he/she is responsible for doing proper sanity checks.
136  **/
137 static int iter_dos_ctor(struct part_iter *iter, va_list *args)
138 {
139     const struct disk_dos_mbr *mbr;
140
141     /* uses args(0) */
142     if (iter_ctor(iter, args))
143         return -1;
144
145     mbr = va_arg(*args, const struct disk_dos_mbr *);
146
147 #ifdef DEBUG
148     if (!mbr)
149         goto bail;
150 #endif
151
152     if (!(iter->data = malloc(sizeof(struct disk_dos_mbr))))
153         goto bail;
154
155     memcpy(iter->data, mbr, sizeof(struct disk_dos_mbr));
156
157     iter->sub.dos.bebr_index0 = -1;
158     iter->sub.dos.disk_sig = mbr->disk_sig;
159
160     return 0;
161 bail:
162     iter->type->dtor(iter);
163     return -1;
164 }
165
166 /**
167  * iter_gpt_ctor() - GPT iterator specific initialization
168  * @iter:       iterator pointer
169  * @args(0):    ptr to disk_info structure
170  * @args(1):    ptr to buffer with GPT header
171  * @args(2):    ptr to buffer with GPT partition list
172  *
173  * Second and further arguments are passed as a pointer to va_list.
174  * This function only makes rudimentary checks. If user uses
175  * pi_new(), he/she is responsible for doing proper sanity checks.
176  **/
177 static int iter_gpt_ctor(struct part_iter *iter, va_list *args)
178 {
179     uint64_t siz;
180     const struct disk_gpt_header *gpth;
181     const struct disk_gpt_part_entry *gptl;
182
183     /* uses args(0) */
184     if (iter_ctor(iter, args))
185         return -1;
186
187     gpth = va_arg(*args, const struct disk_gpt_header *);
188     gptl = va_arg(*args, const struct disk_gpt_part_entry *);
189
190 #ifdef DEBUG
191     if (!gpth || !gptl)
192         goto bail;
193 #endif
194
195     siz = (uint64_t)gpth->part_count * gpth->part_size;
196
197 #ifdef DEBUG
198     if (!siz || (siz + iter->di.bps - 1) / iter->di.bps > 255u ||
199             gpth->part_size < sizeof(struct disk_gpt_part_entry)) {
200         goto bail;
201     }
202 #endif
203
204     if (!(iter->data = malloc((size_t)siz)))
205         goto bail;
206
207     memcpy(iter->data, gptl, (size_t)siz);
208
209     iter->sub.gpt.pe_count = (int)gpth->part_count;
210     iter->sub.gpt.pe_size = (int)gpth->part_size;
211     iter->sub.gpt.ufirst = gpth->lba_first_usable;
212     iter->sub.gpt.ulast = gpth->lba_last_usable;
213
214     memcpy(&iter->sub.gpt.disk_guid, &gpth->disk_guid, sizeof(struct guid));
215
216     return 0;
217 bail:
218     iter->type->dtor(iter);
219     return -1;
220 }
221
222 /* Logical partition must be sane, meaning:
223  * - must be data or empty
224  * - must have non-0 start and length
225  * - values must not wrap around 32bit
226  * - must be inside current EBR frame
227  */
228
229 static int notsane_logical(const struct part_iter *iter)
230 {
231     const struct disk_dos_part_entry *dp;
232     uint32_t end_log;
233
234     dp = ((struct disk_dos_mbr *)iter->data)->table;
235
236     if (!dp[0].ostype)
237         return 0;
238
239     if (ost_is_ext(dp[0].ostype)) {
240         error("1st EBR entry must be data or empty.\n");
241         return -1;
242     }
243
244     end_log = dp[0].start_lba + dp[0].length;
245
246     if (!dp[0].start_lba ||
247         !dp[0].length ||
248         !sane(dp[0].start_lba, dp[0].length) ||
249         end_log > iter->sub.dos.ebr_size) {
250
251         error("Insane logical partition.\n");
252         return -1;
253     }
254
255     return 0;
256 }
257
258 /* Extended partition must be sane, meaning:
259  * - must be extended or empty
260  * - must have non-0 start and length
261  * - values must not wrap around 32bit
262  * - must be inside base EBR frame
263  */
264
265 static int notsane_extended(const struct part_iter *iter)
266 {
267     const struct disk_dos_part_entry *dp;
268     uint32_t end_ebr;
269
270     dp = ((struct disk_dos_mbr *)iter->data)->table;
271
272     if (!dp[1].ostype)
273         return 0;
274
275     if (!ost_is_nondata(dp[1].ostype)) {
276         error("2nd EBR entry must be extended or empty.\n");
277         return -1;
278     }
279
280     end_ebr = dp[1].start_lba + dp[1].length;
281
282     if (!dp[1].start_lba ||
283         !dp[1].length ||
284         !sane(dp[1].start_lba, dp[1].length) ||
285         end_ebr > iter->sub.dos.bebr_size) {
286
287         error("Insane extended partition.\n");
288         return -1;
289     }
290
291     return 0;
292 }
293
294 /* Primary partition must be sane, meaning:
295  * - must have non-0 start and length
296  * - values must not wrap around 32bit
297  */
298
299 static int notsane_primary(const struct part_iter *iter)
300 {
301     const struct disk_dos_part_entry *dp;
302     dp = ((struct disk_dos_mbr *)iter->data)->table + iter->index0;
303
304     if (!dp->ostype)
305         return 0;
306
307     if (!dp->start_lba ||
308         !dp->length ||
309         !sane(dp->start_lba, dp->length) ||
310         dp->start_lba + dp->length > iter->di.lbacnt) {
311         error("Insane primary (MBR) partition.\n");
312         return -1;
313     }
314
315     return 0;
316 }
317
318 static int notsane_gpt(const struct part_iter *iter)
319 {
320     const struct disk_gpt_part_entry *gp;
321     gp = (const struct disk_gpt_part_entry *)
322         (iter->data + iter->index0 * iter->sub.gpt.pe_size);
323
324     if (guid_is0(&gp->type))
325         return 0;
326
327     if (gp->lba_first < iter->sub.gpt.ufirst ||
328         gp->lba_last > iter->sub.gpt.ulast) {
329         error("Insane GPT partition.\n");
330         return -1;
331     }
332
333     return 0;
334 }
335
336 static int pi_dos_next_mbr(struct part_iter *iter, uint32_t *lba,
337                             struct disk_dos_part_entry **_dp)
338 {
339     struct disk_dos_part_entry *dp;
340
341     while (++iter->index0 < 4) {
342         dp = ((struct disk_dos_mbr *)iter->data)->table + iter->index0;
343
344         if (notsane_primary(iter)) {
345             iter->status = PI_INSANE;
346             goto bail;
347         }
348
349         if (ost_is_ext(dp->ostype)) {
350             if (iter->sub.dos.bebr_index0 >= 0) {
351                 error("You have more than 1 extended partition.\n");
352                 iter->status = PI_INSANE;
353                 goto bail;
354             }
355             /* record base EBR index */
356             iter->sub.dos.bebr_index0 = iter->index0;
357         }
358         if (!ost_is_nondata(dp->ostype) || iter->stepall) {
359             *lba = dp->start_lba;
360             *_dp = dp;
361             break;
362         }
363     }
364
365     return 0;
366 bail:
367     return -1;
368 }
369
370 static int prep_base_ebr(struct part_iter *iter)
371 {
372     struct disk_dos_part_entry *dp;
373
374     if (iter->sub.dos.bebr_index0 < 0)  /* if we don't have base extended partition at all */
375         return -1;
376     else if (!iter->sub.dos.bebr_start) { /* if not initialized yet */
377         dp = ((struct disk_dos_mbr *)iter->data)->table + iter->sub.dos.bebr_index0;
378
379         iter->sub.dos.bebr_start = dp->start_lba;
380         iter->sub.dos.bebr_size = dp->length;
381
382         iter->sub.dos.ebr_start = 0;
383         iter->sub.dos.ebr_size = iter->sub.dos.bebr_size;
384
385         iter->sub.dos.cebr_lba = 0;
386         iter->sub.dos.nebr_lba = iter->sub.dos.bebr_start;
387
388         iter->index0--;
389     }
390     return 0;
391 }
392
393 static int pi_dos_next_ebr(struct part_iter *iter, uint32_t *lba,
394                             struct disk_dos_part_entry **_dp)
395 {
396     struct disk_dos_part_entry *dp;
397
398     if (prep_base_ebr(iter)) {
399         iter->status = PI_DONE;
400         return -1;
401     }
402
403     while (++iter->index0 < 1024 && iter->sub.dos.nebr_lba) {
404         free(iter->data);
405         if (!(iter->data =
406                     disk_read_sectors(&iter->di, iter->sub.dos.nebr_lba, 1))) {
407             error("Couldn't load EBR.\n");
408             iter->status = PI_ERRLOAD;
409             return -1;
410         }
411
412         if (notsane_logical(iter) || notsane_extended(iter)) {
413             iter->status = PI_INSANE;
414             return -1;
415         }
416
417         dp = ((struct disk_dos_mbr *)iter->data)->table;
418
419         iter->sub.dos.cebr_lba = iter->sub.dos.nebr_lba;
420
421         /* setup next frame values */
422         if (dp[1].ostype) {
423             iter->sub.dos.ebr_start = dp[1].start_lba;
424             iter->sub.dos.ebr_size = dp[1].length;
425             iter->sub.dos.nebr_lba = iter->sub.dos.bebr_start + dp[1].start_lba;
426         } else {
427             iter->sub.dos.ebr_start = 0;
428             iter->sub.dos.ebr_size = 0;
429             iter->sub.dos.nebr_lba = 0;
430         }
431
432         if (!dp[0].ostype)
433             iter->sub.dos.skipcnt++;
434
435         if (dp[0].ostype || iter->stepall) {
436             *lba = iter->sub.dos.cebr_lba + dp[0].start_lba;
437             *_dp = dp;
438             return 0;
439         }
440         /*
441          * This way it's possible to continue, if some crazy soft left a "hole"
442          * - EBR with a valid extended partition without a logical one. In
443          * such case, linux will not reserve a number for such hole - so we
444          * don't increase index0. If stepall flag is set, we will never reach
445          * this place.
446          */
447     }
448     iter->status = PI_DONE;
449     return -1;
450 }
451
452 static struct part_iter *pi_dos_next(struct part_iter *iter)
453 {
454     uint32_t start_lba = 0;
455     struct disk_dos_part_entry *dos_part = NULL;
456
457     if (iter->status)
458         goto bail;
459
460     /* look for primary partitions */
461     if (iter->index0 < 4 &&
462             pi_dos_next_mbr(iter, &start_lba, &dos_part))
463         goto bail;
464
465     /* look for logical partitions */
466     if (iter->index0 >= 4 &&
467             pi_dos_next_ebr(iter, &start_lba, &dos_part))
468         goto bail;
469
470     /*
471      * note special index handling, if we have stepall set -
472      * this is made to keep index consistent with non-stepall
473      * iterators
474      */
475
476     if (iter->index0 >= 4 && !dos_part->ostype)
477         iter->index = -1;
478     else
479         iter->index = iter->index0 - iter->sub.dos.skipcnt + 1;
480     iter->rawindex = iter->index0 + 1;
481     iter->start_lba = start_lba;
482     iter->length = dos_part->length;
483     iter->record = (char *)dos_part;
484
485 #ifdef DEBUG
486     disk_dos_part_dump(dos_part);
487 #endif
488
489     return iter;
490 bail:
491     return NULL;
492 }
493
494 static void gpt_conv_label(struct part_iter *iter)
495 {
496     const struct disk_gpt_part_entry *gp;
497     const int16_t *orig_lab;
498
499     gp = (const struct disk_gpt_part_entry *)
500         (iter->data + iter->index0 * iter->sub.gpt.pe_size);
501     orig_lab = (const int16_t *)gp->name;
502
503     /* caveat: this is very crude conversion */
504     for (int i = 0; i < PI_GPTLABSIZE/2; i++) {
505         iter->sub.gpt.part_label[i] = (char)orig_lab[i];
506     }
507     iter->sub.gpt.part_label[PI_GPTLABSIZE/2] = 0;
508 }
509
510 static struct part_iter *pi_gpt_next(struct part_iter *iter)
511 {
512     const struct disk_gpt_part_entry *gpt_part = NULL;
513
514     if (iter->status)
515         goto bail;
516
517     while (++iter->index0 < iter->sub.gpt.pe_count) {
518         gpt_part = (const struct disk_gpt_part_entry *)
519             (iter->data + iter->index0 * iter->sub.gpt.pe_size);
520
521         if (notsane_gpt(iter)) {
522             iter->status = PI_INSANE;
523             goto bail;
524         }
525
526         if (!guid_is0(&gpt_part->type) || iter->stepall)
527             break;
528     }
529     /* no more partitions ? */
530     if (iter->index0 == iter->sub.gpt.pe_count) {
531         iter->status = PI_DONE;
532         goto bail;
533     }
534     /* gpt_part is guaranteed to be valid here */
535     iter->index = iter->index0 + 1;
536     iter->rawindex = iter->index0 + 1;
537     iter->start_lba = gpt_part->lba_first;
538     iter->length = gpt_part->lba_last - gpt_part->lba_first + 1;
539     iter->record = (char *)gpt_part;
540     memcpy(&iter->sub.gpt.part_guid, &gpt_part->uid, sizeof(struct guid));
541     gpt_conv_label(iter);
542
543 #ifdef DEBUG
544     disk_gpt_part_dump(gpt_part);
545 #endif
546
547     return iter;
548 bail:
549     return NULL;
550 }
551
552 static struct part_iter *pi_raw_next(struct part_iter *iter)
553 {
554     iter->status = PI_DONE;
555     return NULL;
556 }
557
558 static int check_crc(uint32_t crc_match, const uint8_t *buf, unsigned int siz)
559 {
560     uint32_t crc;
561
562     crc = crc32(0, NULL, 0);
563     crc = crc32(crc, buf, siz);
564
565     return crc_match != crc;
566 }
567
568 static int gpt_check_hdr_crc(const struct disk_info * const diskinfo, struct disk_gpt_header **_gh)
569 {
570     struct disk_gpt_header *gh = *_gh;
571     uint64_t lba_alt;
572     uint32_t hold_crc32;
573
574     hold_crc32 = gh->chksum;
575     gh->chksum = 0;
576     if (check_crc(hold_crc32, (const uint8_t *)gh, gh->hdr_size)) {
577         error("WARNING: Primary GPT header checksum invalid.\n");
578         /* retry with backup */
579         lba_alt = gh->lba_alt;
580         free(gh);
581         if (!(gh = *_gh = disk_read_sectors(diskinfo, lba_alt, 1))) {
582             error("Couldn't read backup GPT header.\n");
583             return -1;
584         }
585         hold_crc32 = gh->chksum;
586         gh->chksum = 0;
587         if (check_crc(hold_crc32, (const uint8_t *)gh, gh->hdr_size)) {
588             error("Secondary GPT header checksum invalid.\n");
589             return -1;
590         }
591     }
592     /* restore old checksum */
593     gh->chksum = hold_crc32;
594
595     return 0;
596 }
597
598 /*
599  * ----------------------------------------------------------------------------
600  * Following functions are for users to call.
601  * ----------------------------------------------------------------------------
602  */
603
604
605 int pi_next(struct part_iter **_iter)
606 {
607     struct part_iter *iter;
608
609     if(!_iter || !*_iter)
610         return 0;
611     iter = *_iter;
612 #ifdef DEBUG
613     if (inv_type(iter->type)) {
614         error("This is not a valid iterator.\n");
615         return 0;
616     }
617 #endif
618     if ((iter = iter->type->next(iter))) {
619         *_iter = iter;
620     }
621     return (*_iter)->status;
622 }
623
624 /**
625  * pi_new() - get new iterator
626  * @itertype:   iterator type
627  * @...:        variable arguments passed to ctors
628  *
629  * Variable arguments depend on the type. Please see functions:
630  * iter_gpt_ctor() and iter_dos_ctor() for details.
631  **/
632 struct part_iter *pi_new(const struct itertype *type, ...)
633 {
634     int badctor = 0;
635     struct part_iter *iter = NULL;
636     va_list ap;
637
638     va_start(ap, type);
639
640 #ifdef DEBUG
641     if (inv_type(type)) {
642         error("Unknown iterator requested.\n");
643         goto bail;
644     }
645 #endif
646
647     if (!(iter = malloc(sizeof(struct part_iter)))) {
648         error("Couldn't allocate memory for the iterator.\n");
649         goto bail;
650     }
651
652     memset(iter, 0, sizeof(struct part_iter));
653     iter->type = type;
654
655     if (type->ctor(iter, &ap)) {
656         badctor = -1;
657         error("Cannot initialize the iterator.\n");
658         goto bail;
659     }
660
661 bail:
662     va_end(ap);
663     if (badctor) {
664         free(iter);
665         iter = NULL;
666     }
667     return iter;
668 }
669
670 /**
671  * pi_del() - delete iterator
672  * @iter:       iterator double pointer
673  *
674  **/
675
676 void pi_del(struct part_iter **_iter)
677 {
678     struct part_iter *iter;
679
680     if(!_iter || !*_iter)
681         return;
682     iter = *_iter;
683
684 #ifdef DEBUG
685     if (inv_type(iter->type)) {
686         error("This is not a valid iterator.\n");
687         return;
688     }
689 #endif
690
691     iter->type->dtor(iter);
692     free(iter);
693     *_iter = NULL;
694 }
695
696 /**
697  * pi_begin() - check disk, validate, and get proper iterator
698  * @di:     diskinfo struct pointer
699  *
700  * This function checks the disk for GPT or legacy partition table and allocates
701  * an appropriate iterator.
702  **/
703 struct part_iter *pi_begin(const struct disk_info *di, int stepall)
704 {
705     int setraw = 0;
706     struct part_iter *iter = NULL;
707     struct disk_dos_mbr *mbr = NULL;
708     struct disk_gpt_header *gpth = NULL;
709     struct disk_gpt_part_entry *gptl = NULL;
710
711     /* Read MBR */
712     if (!(mbr = disk_read_sectors(di, 0, 1))) {
713         error("Couldn't read first disk sector.\n");
714         goto bail;
715     }
716
717     setraw = -1;
718
719     /* Check for MBR magic*/
720     if (mbr->sig != disk_mbr_sig_magic) {
721         error("No MBR magic.\n");
722         goto bail;
723     }
724
725     /* Check for GPT protective MBR */
726     if (mbr->table[0].ostype == 0xEE) {
727         if (!(gpth = disk_read_sectors(di, 1, 1))) {
728             error("Couldn't read potential GPT header.\n");
729             goto bail;
730         }
731     }
732
733     if (gpth && gpth->rev.uint32 == 0x00010000 &&
734             !memcmp(gpth->sig, disk_gpt_sig_magic, sizeof(disk_gpt_sig_magic))) {
735         /* looks like GPT v1.0 */
736         uint64_t gpt_loff;          /* offset to GPT partition list in sectors */
737         uint64_t gpt_lsiz;          /* size of GPT partition list in bytes */
738         uint64_t gpt_lcnt;          /* size of GPT partition in sectors */
739 #ifdef DEBUG
740         puts("Looks like a GPT v1.0 disk.");
741         disk_gpt_header_dump(gpth);
742 #endif
743         /* Verify checksum, fallback to backup, then bail if invalid */
744         if (gpt_check_hdr_crc(di, &gpth))
745             goto bail;
746
747         gpt_loff = gpth->lba_table;
748         gpt_lsiz = (uint64_t)gpth->part_size * gpth->part_count;
749         gpt_lcnt = (gpt_lsiz + di->bps - 1) / di->bps;
750
751         /*
752          * disk_read_sectors allows reading of max 255 sectors, so we use
753          * it as a sanity check base. EFI doesn't specify max (AFAIK).
754          * Apart from that, some extensive sanity checks.
755          */
756         if (!gpt_loff || !gpt_lsiz || gpt_lcnt > 255u ||
757                 gpth->lba_first_usable > gpth->lba_last_usable ||
758                 !sane(gpt_loff, gpt_lcnt) ||
759                 gpt_loff + gpt_lcnt > gpth->lba_first_usable ||
760                 !sane(gpth->lba_last_usable, gpt_lcnt) ||
761                 gpth->lba_last_usable + gpt_lcnt >= gpth->lba_alt ||
762                 gpth->lba_alt >= di->lbacnt ||
763                 gpth->part_size < sizeof(struct disk_gpt_part_entry)) {
764             error("Invalid GPT header's values.\n");
765             goto bail;
766         }
767         if (!(gptl = disk_read_sectors(di, gpt_loff, (uint8_t)gpt_lcnt))) {
768             error("Couldn't read GPT partition list.\n");
769             goto bail;
770         }
771         /* Check array checksum(s). */
772         if (check_crc(gpth->table_chksum, (const uint8_t *)gptl, (unsigned int)gpt_lsiz)) {
773             error("WARNING: GPT partition list checksum invalid, trying backup.\n");
774             free(gptl);
775             /* secondary array directly precedes secondary header */
776             if (!(gptl = disk_read_sectors(di, gpth->lba_alt - gpt_lcnt, (uint8_t)gpt_lcnt))) {
777                 error("Couldn't read backup GPT partition list.\n");
778                 goto bail;
779             }
780             if (check_crc(gpth->table_chksum, (const uint8_t *)gptl, (unsigned int)gpt_lsiz)) {
781                 error("Backup GPT partition list checksum invalid.\n");
782                 goto bail;
783             }
784         }
785         /* allocate iterator and exit */
786         iter = pi_new(typegpt, di, stepall, gpth, gptl);
787     } else {
788         /* looks like MBR */
789         iter = pi_new(typedos, di, stepall, mbr);
790     }
791
792     setraw = 0;
793 bail:
794     if (setraw) {
795         error("WARNING: treating disk as raw.\n");
796         iter = pi_new(typeraw, di, stepall);
797     }
798     free(mbr);
799     free(gpth);
800     free(gptl);
801
802     return iter;
803 }
804
805 /* vim: set ts=8 sts=4 sw=4 noet: */