summaryrefslogtreecommitdiff
path: root/com32/chain/partiter.c
blob: 1acd1958689ca94261576cf31b777ca6f1ce8e44 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
/* ----------------------------------------------------------------------- *
 *
 *   Copyright 2003-2010 H. Peter Anvin - All Rights Reserved
 *   Copyright 2010 Shao Miller
 *   Copyright 2010 Michal Soltys
 *
 *   Permission is hereby granted, free of charge, to any person
 *   obtaining a copy of this software and associated documentation
 *   files (the "Software"), to deal in the Software without
 *   restriction, including without limitation the rights to use,
 *   copy, modify, merge, publish, distribute, sublicense, and/or
 *   sell copies of the Software, and to permit persons to whom
 *   the Software is furnished to do so, subject to the following
 *   conditions:
 *
 *   The above copyright notice and this permission notice shall
 *   be included in all copies or substantial portions of the Software.
 *
 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 *   OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 *   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 *   HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 *   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 *   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 *   OTHER DEALINGS IN THE SOFTWARE.
 *
 * ----------------------------------------------------------------------- */

/*
 * partiter.c
 *
 * Provides disk / partition iteration.
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <zlib.h>
#include <syslinux/disk.h>
#include "common.h"
#include "partiter.h"
#include "utility.h"

#define ost_is_ext(type) ((type) == 0x05 || (type) == 0x0F || (type) == 0x85)
#define ost_is_nondata(type) (ost_is_ext(type) || (type) == 0x00)
#define sane(s,l) ((s)+(l) > (s))

/* forwards */

static int iter_ctor(struct part_iter *, va_list *);
static int iter_dos_ctor(struct part_iter *, va_list *);
static int iter_gpt_ctor(struct part_iter *, va_list *);
static void iter_dtor(struct part_iter *);
static struct part_iter *pi_dos_next(struct part_iter *);
static struct part_iter *pi_gpt_next(struct part_iter *);
static struct part_iter *pi_raw_next(struct part_iter *);

static struct itertype types[] = {
   [0] = {
	.ctor = &iter_dos_ctor,
	.dtor = &iter_dtor,
	.next = &pi_dos_next,
}, [1] = {
	.ctor = &iter_gpt_ctor,
	.dtor = &iter_dtor,
	.next = &pi_gpt_next,
}, [2] = {
	.ctor = &iter_ctor,
	.dtor = &iter_dtor,
	.next = &pi_raw_next,
}};

const struct itertype * const typedos = types;
const struct itertype * const typegpt = types+1;
const struct itertype * const typeraw = types+2;

#ifdef DEBUG
static int inv_type(const void *type)
{
    int i, cnt = sizeof(types)/sizeof(types[0]);
    for (i = 0; i < cnt; i++) {
	if (type == types + i)
	    return 0;
    }
    return -1;
}
#endif

/**
 * iter_ctor() - common iterator initialization
 * @iter:	iterator pointer
 * @args(0):	disk_info structure used for disk functions
 * @args(1):	stepall modifier
 *
 * Second and further arguments are passed as a pointer to va_list
 **/
static int iter_ctor(struct part_iter *iter, va_list *args)
{
    const struct disk_info *di = va_arg(*args, const struct disk_info *);
    int stepall = va_arg(*args, int);

#ifdef DEBUG
    if (!di)
	return -1;
#endif

    memcpy(&iter->di, di, sizeof(struct disk_info));
    iter->stepall = stepall;
    iter->index0 = -1;
    iter->length = di->lbacnt;

    return 0;
}

/**
 * iter_dtor() - common iterator cleanup
 * @iter:	iterator pointer
 *
 **/
static void iter_dtor(struct part_iter *iter)
{
    free(iter->data);
}

/**
 * iter_dos_ctor() - MBR/EBR iterator specific initialization
 * @iter:	iterator pointer
 * @args(0):	disk_info structure used for disk functions
 * @args(1):	pointer to buffer with loaded valid MBR
 *
 * Second and further arguments are passed as a pointer to va_list.
 * This function only makes rudimentary checks. If user uses
 * pi_new(), he/she is responsible for doing proper sanity checks.
 **/
static int iter_dos_ctor(struct part_iter *iter, va_list *args)
{
    const struct disk_dos_mbr *mbr;

    /* uses args(0) */
    if (iter_ctor(iter, args))
	return -1;

    mbr = va_arg(*args, const struct disk_dos_mbr *);

#ifdef DEBUG
    if (!mbr)
	goto bail;
#endif

    if (!(iter->data = malloc(sizeof(struct disk_dos_mbr))))
	goto bail;

    memcpy(iter->data, mbr, sizeof(struct disk_dos_mbr));

    iter->sub.dos.bebr_index0 = -1;
    iter->sub.dos.disk_sig = mbr->disk_sig;

    return 0;
bail:
    iter->type->dtor(iter);
    return -1;
}

/**
 * iter_gpt_ctor() - GPT iterator specific initialization
 * @iter:	iterator pointer
 * @args(0):	ptr to disk_info structure
 * @args(1):	ptr to buffer with GPT header
 * @args(2):	ptr to buffer with GPT partition list
 *
 * Second and further arguments are passed as a pointer to va_list.
 * This function only makes rudimentary checks. If user uses
 * pi_new(), he/she is responsible for doing proper sanity checks.
 **/
static int iter_gpt_ctor(struct part_iter *iter, va_list *args)
{
    uint64_t siz;
    const struct disk_gpt_header *gpth;
    const struct disk_gpt_part_entry *gptl;

    /* uses args(0) */
    if (iter_ctor(iter, args))
	return -1;

    gpth = va_arg(*args, const struct disk_gpt_header *);
    gptl = va_arg(*args, const struct disk_gpt_part_entry *);

#ifdef DEBUG
    if (!gpth || !gptl)
	goto bail;
#endif

    siz = (uint64_t)gpth->part_count * gpth->part_size;

#ifdef DEBUG
    if (!siz || (siz + iter->di.bps - 1) / iter->di.bps > 255u ||
	    gpth->part_size < sizeof(struct disk_gpt_part_entry)) {
	goto bail;
    }
#endif

    if (!(iter->data = malloc((size_t)siz)))
	goto bail;

    memcpy(iter->data, gptl, (size_t)siz);

    iter->sub.gpt.pe_count = (int)gpth->part_count;
    iter->sub.gpt.pe_size = (int)gpth->part_size;
    iter->sub.gpt.ufirst = gpth->lba_first_usable;
    iter->sub.gpt.ulast = gpth->lba_last_usable;

    memcpy(&iter->sub.gpt.disk_guid, &gpth->disk_guid, sizeof(struct guid));

    return 0;
bail:
    iter->type->dtor(iter);
    return -1;
}

/* Logical partition must be sane, meaning:
 * - must be data or empty
 * - must have non-0 start and length
 * - values must not wrap around 32bit
 * - must be inside current EBR frame
 */

static int notsane_logical(const struct part_iter *iter)
{
    const struct disk_dos_part_entry *dp;
    uint32_t end_log;

    dp = ((struct disk_dos_mbr *)iter->data)->table;

    if (!dp[0].ostype)
	return 0;

    if (ost_is_ext(dp[0].ostype)) {
	error("1st EBR entry must be data or empty.\n");
	return -1;
    }

    end_log = dp[0].start_lba + dp[0].length;

    if (!dp[0].start_lba ||
	!dp[0].length ||
	!sane(dp[0].start_lba, dp[0].length) ||
	end_log > iter->sub.dos.ebr_size) {

	error("Insane logical partition.\n");
	return -1;
    }

    return 0;
}

/* Extended partition must be sane, meaning:
 * - must be extended or empty
 * - must have non-0 start and length
 * - values must not wrap around 32bit
 * - must be inside base EBR frame
 */

static int notsane_extended(const struct part_iter *iter)
{
    const struct disk_dos_part_entry *dp;
    uint32_t end_ebr;

    dp = ((struct disk_dos_mbr *)iter->data)->table;

    if (!dp[1].ostype)
	return 0;

    if (!ost_is_nondata(dp[1].ostype)) {
	error("2nd EBR entry must be extended or empty.\n");
	return -1;
    }

    end_ebr = dp[1].start_lba + dp[1].length;

    if (!dp[1].start_lba ||
	!dp[1].length ||
	!sane(dp[1].start_lba, dp[1].length) ||
	end_ebr > iter->sub.dos.bebr_size) {

	error("Insane extended partition.\n");
	return -1;
    }

    return 0;
}

/* Primary partition must be sane, meaning:
 * - must have non-0 start and length
 * - values must not wrap around 32bit
 */

static int notsane_primary(const struct part_iter *iter)
{
    const struct disk_dos_part_entry *dp;
    dp = ((struct disk_dos_mbr *)iter->data)->table + iter->index0;

    if (!dp->ostype)
	return 0;

    if (!dp->start_lba ||
	!dp->length ||
	!sane(dp->start_lba, dp->length) ||
	dp->start_lba + dp->length > iter->di.lbacnt) {
	error("Insane primary (MBR) partition.\n");
	return -1;
    }

    return 0;
}

static int notsane_gpt(const struct part_iter *iter)
{
    const struct disk_gpt_part_entry *gp;
    gp = (const struct disk_gpt_part_entry *)
	(iter->data + iter->index0 * iter->sub.gpt.pe_size);

    if (guid_is0(&gp->type))
	return 0;

    if (gp->lba_first < iter->sub.gpt.ufirst ||
	gp->lba_last > iter->sub.gpt.ulast) {
	error("Insane GPT partition.\n");
	return -1;
    }

    return 0;
}

static int pi_dos_next_mbr(struct part_iter *iter, uint32_t *lba,
			    struct disk_dos_part_entry **_dp)
{
    struct disk_dos_part_entry *dp;

    while (++iter->index0 < 4) {
	dp = ((struct disk_dos_mbr *)iter->data)->table + iter->index0;

	if (notsane_primary(iter)) {
	    iter->status = PI_INSANE;
	    goto bail;
	}

	if (ost_is_ext(dp->ostype)) {
	    if (iter->sub.dos.bebr_index0 >= 0) {
		error("You have more than 1 extended partition.\n");
		iter->status = PI_INSANE;
		goto bail;
	    }
	    /* record base EBR index */
	    iter->sub.dos.bebr_index0 = iter->index0;
	}
	if (!ost_is_nondata(dp->ostype) || iter->stepall) {
	    *lba = dp->start_lba;
	    *_dp = dp;
	    break;
	}
    }

    return 0;
bail:
    return -1;
}

static int prep_base_ebr(struct part_iter *iter)
{
    struct disk_dos_part_entry *dp;

    if (iter->sub.dos.bebr_index0 < 0)	/* if we don't have base extended partition at all */
	return -1;
    else if (!iter->sub.dos.bebr_start) { /* if not initialized yet */
	dp = ((struct disk_dos_mbr *)iter->data)->table + iter->sub.dos.bebr_index0;

	iter->sub.dos.bebr_start = dp->start_lba;
	iter->sub.dos.bebr_size = dp->length;

	iter->sub.dos.ebr_start = 0;
	iter->sub.dos.ebr_size = iter->sub.dos.bebr_size;

	iter->sub.dos.cebr_lba = 0;
	iter->sub.dos.nebr_lba = iter->sub.dos.bebr_start;

	iter->index0--;
    }
    return 0;
}

static int pi_dos_next_ebr(struct part_iter *iter, uint32_t *lba,
			    struct disk_dos_part_entry **_dp)
{
    struct disk_dos_part_entry *dp;

    if (prep_base_ebr(iter)) {
	iter->status = PI_DONE;
	return -1;
    }

    while (++iter->index0 < 1024 && iter->sub.dos.nebr_lba) {
	free(iter->data);
	if (!(iter->data =
		    disk_read_sectors(&iter->di, iter->sub.dos.nebr_lba, 1))) {
	    error("Couldn't load EBR.\n");
	    iter->status = PI_ERRLOAD;
	    return -1;
	}

	if (notsane_logical(iter) || notsane_extended(iter)) {
	    iter->status = PI_INSANE;
	    return -1;
	}

	dp = ((struct disk_dos_mbr *)iter->data)->table;

	iter->sub.dos.cebr_lba = iter->sub.dos.nebr_lba;

	/* setup next frame values */
	if (dp[1].ostype) {
	    iter->sub.dos.ebr_start = dp[1].start_lba;
	    iter->sub.dos.ebr_size = dp[1].length;
	    iter->sub.dos.nebr_lba = iter->sub.dos.bebr_start + dp[1].start_lba;
	} else {
	    iter->sub.dos.ebr_start = 0;
	    iter->sub.dos.ebr_size = 0;
	    iter->sub.dos.nebr_lba = 0;
	}

	if (!dp[0].ostype)
	    iter->sub.dos.skipcnt++;

	if (dp[0].ostype || iter->stepall) {
	    *lba = iter->sub.dos.cebr_lba + dp[0].start_lba;
	    *_dp = dp;
	    return 0;
	}
	/*
	 * This way it's possible to continue, if some crazy soft left a "hole"
	 * - EBR with a valid extended partition without a logical one. In
	 * such case, linux will not reserve a number for such hole - so we
	 * don't increase index0. If stepall flag is set, we will never reach
	 * this place.
	 */
    }
    iter->status = PI_DONE;
    return -1;
}

static struct part_iter *pi_dos_next(struct part_iter *iter)
{
    uint32_t start_lba = 0;
    struct disk_dos_part_entry *dos_part = NULL;

    if (iter->status)
	goto bail;

    /* look for primary partitions */
    if (iter->index0 < 4 &&
	    pi_dos_next_mbr(iter, &start_lba, &dos_part))
	goto bail;

    /* look for logical partitions */
    if (iter->index0 >= 4 &&
	    pi_dos_next_ebr(iter, &start_lba, &dos_part))
	goto bail;

    /*
     * note special index handling, if we have stepall set -
     * this is made to keep index consistent with non-stepall
     * iterators
     */

    if (iter->index0 >= 4 && !dos_part->ostype)
	iter->index = -1;
    else
	iter->index = iter->index0 - iter->sub.dos.skipcnt + 1;
    iter->rawindex = iter->index0 + 1;
    iter->start_lba = start_lba;
    iter->length = dos_part->length;
    iter->record = (char *)dos_part;

#ifdef DEBUG
    disk_dos_part_dump(dos_part);
#endif

    return iter;
bail:
    return NULL;
}

static void gpt_conv_label(struct part_iter *iter)
{
    const struct disk_gpt_part_entry *gp;
    const int16_t *orig_lab;

    gp = (const struct disk_gpt_part_entry *)
	(iter->data + iter->index0 * iter->sub.gpt.pe_size);
    orig_lab = (const int16_t *)gp->name;

    /* caveat: this is very crude conversion */
    for (int i = 0; i < PI_GPTLABSIZE/2; i++) {
	iter->sub.gpt.part_label[i] = (char)orig_lab[i];
    }
    iter->sub.gpt.part_label[PI_GPTLABSIZE/2] = 0;
}

static struct part_iter *pi_gpt_next(struct part_iter *iter)
{
    const struct disk_gpt_part_entry *gpt_part = NULL;

    if (iter->status)
	goto bail;

    while (++iter->index0 < iter->sub.gpt.pe_count) {
	gpt_part = (const struct disk_gpt_part_entry *)
	    (iter->data + iter->index0 * iter->sub.gpt.pe_size);

	if (notsane_gpt(iter)) {
	    iter->status = PI_INSANE;
	    goto bail;
	}

	if (!guid_is0(&gpt_part->type) || iter->stepall)
	    break;
    }
    /* no more partitions ? */
    if (iter->index0 == iter->sub.gpt.pe_count) {
	iter->status = PI_DONE;
	goto bail;
    }
    /* gpt_part is guaranteed to be valid here */
    iter->index = iter->index0 + 1;
    iter->rawindex = iter->index0 + 1;
    iter->start_lba = gpt_part->lba_first;
    iter->length = gpt_part->lba_last - gpt_part->lba_first + 1;
    iter->record = (char *)gpt_part;
    memcpy(&iter->sub.gpt.part_guid, &gpt_part->uid, sizeof(struct guid));
    gpt_conv_label(iter);

#ifdef DEBUG
    disk_gpt_part_dump(gpt_part);
#endif

    return iter;
bail:
    return NULL;
}

static struct part_iter *pi_raw_next(struct part_iter *iter)
{
    iter->status = PI_DONE;
    return NULL;
}

static int check_crc(uint32_t crc_match, const uint8_t *buf, unsigned int siz)
{
    uint32_t crc;

    crc = crc32(0, NULL, 0);
    crc = crc32(crc, buf, siz);

    return crc_match != crc;
}

static int gpt_check_hdr_crc(const struct disk_info * const diskinfo, struct disk_gpt_header **_gh)
{
    struct disk_gpt_header *gh = *_gh;
    uint64_t lba_alt;
    uint32_t hold_crc32;

    hold_crc32 = gh->chksum;
    gh->chksum = 0;
    if (check_crc(hold_crc32, (const uint8_t *)gh, gh->hdr_size)) {
	error("WARNING: Primary GPT header checksum invalid.\n");
	/* retry with backup */
	lba_alt = gh->lba_alt;
	free(gh);
	if (!(gh = *_gh = disk_read_sectors(diskinfo, lba_alt, 1))) {
	    error("Couldn't read backup GPT header.\n");
	    return -1;
	}
	hold_crc32 = gh->chksum;
	gh->chksum = 0;
	if (check_crc(hold_crc32, (const uint8_t *)gh, gh->hdr_size)) {
	    error("Secondary GPT header checksum invalid.\n");
	    return -1;
	}
    }
    /* restore old checksum */
    gh->chksum = hold_crc32;

    return 0;
}

/*
 * ----------------------------------------------------------------------------
 * Following functions are for users to call.
 * ----------------------------------------------------------------------------
 */


int pi_next(struct part_iter **_iter)
{
    struct part_iter *iter;

    if(!_iter || !*_iter)
	return 0;
    iter = *_iter;
#ifdef DEBUG
    if (inv_type(iter->type)) {
	error("This is not a valid iterator.\n");
	return 0;
    }
#endif
    if ((iter = iter->type->next(iter))) {
	*_iter = iter;
    }
    return (*_iter)->status;
}

/**
 * pi_new() - get new iterator
 * @itertype:	iterator type
 * @...:	variable arguments passed to ctors
 *
 * Variable arguments depend on the type. Please see functions:
 * iter_gpt_ctor() and iter_dos_ctor() for details.
 **/
struct part_iter *pi_new(const struct itertype *type, ...)
{
    int badctor = 0;
    struct part_iter *iter = NULL;
    va_list ap;

    va_start(ap, type);

#ifdef DEBUG
    if (inv_type(type)) {
	error("Unknown iterator requested.\n");
	goto bail;
    }
#endif

    if (!(iter = malloc(sizeof(struct part_iter)))) {
	error("Couldn't allocate memory for the iterator.\n");
	goto bail;
    }

    memset(iter, 0, sizeof(struct part_iter));
    iter->type = type;

    if (type->ctor(iter, &ap)) {
	badctor = -1;
	error("Cannot initialize the iterator.\n");
	goto bail;
    }

bail:
    va_end(ap);
    if (badctor) {
	free(iter);
	iter = NULL;
    }
    return iter;
}

/**
 * pi_del() - delete iterator
 * @iter:       iterator double pointer
 *
 **/

void pi_del(struct part_iter **_iter)
{
    struct part_iter *iter;

    if(!_iter || !*_iter)
	return;
    iter = *_iter;

#ifdef DEBUG
    if (inv_type(iter->type)) {
	error("This is not a valid iterator.\n");
	return;
    }
#endif

    iter->type->dtor(iter);
    free(iter);
    *_iter = NULL;
}

/**
 * pi_begin() - check disk, validate, and get proper iterator
 * @di:	    diskinfo struct pointer
 *
 * This function checks the disk for GPT or legacy partition table and allocates
 * an appropriate iterator.
 **/
struct part_iter *pi_begin(const struct disk_info *di, int stepall)
{
    int setraw = 0;
    struct part_iter *iter = NULL;
    struct disk_dos_mbr *mbr = NULL;
    struct disk_gpt_header *gpth = NULL;
    struct disk_gpt_part_entry *gptl = NULL;

    /* Read MBR */
    if (!(mbr = disk_read_sectors(di, 0, 1))) {
	error("Couldn't read first disk sector.\n");
	goto bail;
    }

    setraw = -1;

    /* Check for MBR magic*/
    if (mbr->sig != disk_mbr_sig_magic) {
	error("No MBR magic.\n");
	goto bail;
    }

    /* Check for GPT protective MBR */
    if (mbr->table[0].ostype == 0xEE) {
	if (!(gpth = disk_read_sectors(di, 1, 1))) {
	    error("Couldn't read potential GPT header.\n");
	    goto bail;
	}
    }

    if (gpth && gpth->rev.uint32 == 0x00010000 &&
	    !memcmp(gpth->sig, disk_gpt_sig_magic, sizeof(disk_gpt_sig_magic))) {
	/* looks like GPT v1.0 */
	uint64_t gpt_loff;	    /* offset to GPT partition list in sectors */
	uint64_t gpt_lsiz;	    /* size of GPT partition list in bytes */
	uint64_t gpt_lcnt;	    /* size of GPT partition in sectors */
#ifdef DEBUG
	puts("Looks like a GPT v1.0 disk.");
	disk_gpt_header_dump(gpth);
#endif
	/* Verify checksum, fallback to backup, then bail if invalid */
	if (gpt_check_hdr_crc(di, &gpth))
	    goto bail;

	gpt_loff = gpth->lba_table;
	gpt_lsiz = (uint64_t)gpth->part_size * gpth->part_count;
	gpt_lcnt = (gpt_lsiz + di->bps - 1) / di->bps;

	/*
	 * disk_read_sectors allows reading of max 255 sectors, so we use
	 * it as a sanity check base. EFI doesn't specify max (AFAIK).
	 * Apart from that, some extensive sanity checks.
	 */
	if (!gpt_loff || !gpt_lsiz || gpt_lcnt > 255u ||
		gpth->lba_first_usable > gpth->lba_last_usable ||
		!sane(gpt_loff, gpt_lcnt) ||
		gpt_loff + gpt_lcnt > gpth->lba_first_usable ||
		!sane(gpth->lba_last_usable, gpt_lcnt) ||
		gpth->lba_last_usable + gpt_lcnt >= gpth->lba_alt ||
		gpth->lba_alt >= di->lbacnt ||
		gpth->part_size < sizeof(struct disk_gpt_part_entry)) {
	    error("Invalid GPT header's values.\n");
	    goto bail;
	}
	if (!(gptl = disk_read_sectors(di, gpt_loff, (uint8_t)gpt_lcnt))) {
	    error("Couldn't read GPT partition list.\n");
	    goto bail;
	}
	/* Check array checksum(s). */
	if (check_crc(gpth->table_chksum, (const uint8_t *)gptl, (unsigned int)gpt_lsiz)) {
	    error("WARNING: GPT partition list checksum invalid, trying backup.\n");
	    free(gptl);
	    /* secondary array directly precedes secondary header */
	    if (!(gptl = disk_read_sectors(di, gpth->lba_alt - gpt_lcnt, (uint8_t)gpt_lcnt))) {
		error("Couldn't read backup GPT partition list.\n");
		goto bail;
	    }
	    if (check_crc(gpth->table_chksum, (const uint8_t *)gptl, (unsigned int)gpt_lsiz)) {
		error("Backup GPT partition list checksum invalid.\n");
		goto bail;
	    }
	}
	/* allocate iterator and exit */
	iter = pi_new(typegpt, di, stepall, gpth, gptl);
    } else {
	/* looks like MBR */
	iter = pi_new(typedos, di, stepall, mbr);
    }

    setraw = 0;
bail:
    if (setraw) {
	error("WARNING: treating disk as raw.\n");
	iter = pi_new(typeraw, di, stepall);
    }
    free(mbr);
    free(gpth);
    free(gptl);

    return iter;
}

/* vim: set ts=8 sts=4 sw=4 noet: */