1 /* ----------------------------------------------------------------------- *
3 * Copyright 2003-2010 H. Peter Anvin - All Rights Reserved
4 * Copyright 2010 Shao Miller
5 * Copyright 2010 Michal Soltys
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
16 * The above copyright notice and this permission notice shall
17 * be included in all copies or substantial portions of the Software.
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.
28 * ----------------------------------------------------------------------- */
33 * Provides disk / partition iteration.
41 #include <syslinux/disk.h>
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))
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
*);
60 static struct itertype types
[] = {
62 .ctor
= &iter_dos_ctor
,
66 .ctor
= &iter_gpt_ctor
,
75 const struct itertype
* const typedos
= types
;
76 const struct itertype
* const typegpt
= types
+1;
77 const struct itertype
* const typeraw
= types
+2;
80 static int inv_type(const void *type
)
82 int i
, cnt
= sizeof(types
)/sizeof(types
[0]);
83 for (i
= 0; i
< cnt
; i
++) {
84 if (type
== types
+ i
)
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
97 * Second and further arguments are passed as a pointer to va_list
99 static int iter_ctor(struct part_iter
*iter
, va_list *args
)
101 const struct disk_info
*di
= va_arg(*args
, const struct disk_info
*);
102 int stepall
= va_arg(*args
, int);
109 memcpy(&iter
->di
, di
, sizeof(struct disk_info
));
110 iter
->stepall
= stepall
;
117 * iter_dtor() - common iterator cleanup
118 * @iter: iterator pointer
121 static void iter_dtor(struct part_iter
*iter
)
127 * iter_dos_ctor() - MBR/EBR iterator specific initialization
128 * @iter: iterator pointer
129 * @args(0): disk_info structure used for disk functions
130 * @args(1): pointer to buffer with loaded valid MBR
132 * Second and further arguments are passed as a pointer to va_list.
133 * This function only makes rudimentary checks. If user uses
134 * pi_new(), he/she is responsible for doing proper sanity checks.
136 static int iter_dos_ctor(struct part_iter
*iter
, va_list *args
)
138 const struct disk_dos_mbr
*mbr
;
141 if (iter_ctor(iter
, args
))
144 mbr
= va_arg(*args
, const struct disk_dos_mbr
*);
151 if (!(iter
->data
= malloc(sizeof(struct disk_dos_mbr
))))
154 memcpy(iter
->data
, mbr
, sizeof(struct disk_dos_mbr
));
156 iter
->sub
.dos
.bebr_index0
= -1;
157 iter
->sub
.dos
.disk_sig
= mbr
->disk_sig
;
161 iter
->type
->dtor(iter
);
166 * iter_gpt_ctor() - GPT iterator specific initialization
167 * @iter: iterator pointer
168 * @args(0): ptr to disk_info structure
169 * @args(1): ptr to buffer with GPT header
170 * @args(2): ptr to buffer with GPT partition list
172 * Second and further arguments are passed as a pointer to va_list.
173 * This function only makes rudimentary checks. If user uses
174 * pi_new(), he/she is responsible for doing proper sanity checks.
176 static int iter_gpt_ctor(struct part_iter
*iter
, va_list *args
)
179 const struct disk_gpt_header
*gpth
;
180 const struct disk_gpt_part_entry
*gptl
;
183 if (iter_ctor(iter
, args
))
186 gpth
= va_arg(*args
, const struct disk_gpt_header
*);
187 gptl
= va_arg(*args
, const struct disk_gpt_part_entry
*);
194 siz
= (uint64_t)gpth
->part_count
* gpth
->part_size
;
197 if (!siz
|| (siz
+ iter
->di
.bps
- 1) / iter
->di
.bps
> 255u ||
198 gpth
->part_size
< sizeof(struct disk_gpt_part_entry
)) {
203 if (!(iter
->data
= malloc((size_t)siz
)))
206 memcpy(iter
->data
, gptl
, (size_t)siz
);
208 iter
->sub
.gpt
.pe_count
= (int)gpth
->part_count
;
209 iter
->sub
.gpt
.pe_size
= (int)gpth
->part_size
;
210 iter
->sub
.gpt
.ufirst
= gpth
->lba_first_usable
;
211 iter
->sub
.gpt
.ulast
= gpth
->lba_last_usable
;
213 memcpy(&iter
->sub
.gpt
.disk_guid
, &gpth
->disk_guid
, sizeof(struct guid
));
217 iter
->type
->dtor(iter
);
221 /* Logical partition must be sane, meaning:
222 * - must be data or empty
223 * - must have non-0 start and length
224 * - values must not wrap around 32bit
225 * - must be inside current EBR frame
228 static int notsane_logical(const struct part_iter
*iter
)
230 const struct disk_dos_part_entry
*dp
;
233 dp
= ((struct disk_dos_mbr
*)iter
->data
)->table
;
238 if (ost_is_ext(dp
[0].ostype
)) {
239 error("1st EBR entry must be data or empty.\n");
243 end_log
= dp
[0].start_lba
+ dp
[0].length
;
245 if (!dp
[0].start_lba
||
247 !sane(dp
[0].start_lba
, dp
[0].length
) ||
248 end_log
> iter
->sub
.dos
.ebr_size
) {
250 error("Insane logical partition.\n");
257 /* Extended partition must be sane, meaning:
258 * - must be extended or empty
259 * - must have non-0 start and length
260 * - values must not wrap around 32bit
261 * - must be inside base EBR frame
264 static int notsane_extended(const struct part_iter
*iter
)
266 const struct disk_dos_part_entry
*dp
;
269 dp
= ((struct disk_dos_mbr
*)iter
->data
)->table
;
274 if (!ost_is_nondata(dp
[1].ostype
)) {
275 error("2nd EBR entry must be extended or empty.\n");
279 end_ebr
= dp
[1].start_lba
+ dp
[1].length
;
281 if (!dp
[1].start_lba
||
283 !sane(dp
[1].start_lba
, dp
[1].length
) ||
284 end_ebr
> iter
->sub
.dos
.bebr_size
) {
286 error("Insane extended partition.\n");
293 /* Primary partition must be sane, meaning:
294 * - must have non-0 start and length
295 * - values must not wrap around 32bit
298 static int notsane_primary(const struct part_iter
*iter
)
300 const struct disk_dos_part_entry
*dp
;
301 dp
= ((struct disk_dos_mbr
*)iter
->data
)->table
+ iter
->index0
;
306 if (!dp
->start_lba
||
308 !sane(dp
->start_lba
, dp
->length
) ||
309 dp
->start_lba
+ dp
->length
> iter
->di
.lbacnt
) {
310 error("Insane primary (MBR) partition.\n");
317 static int notsane_gpt(const struct part_iter
*iter
)
319 const struct disk_gpt_part_entry
*gp
;
320 gp
= (const struct disk_gpt_part_entry
*)
321 (iter
->data
+ iter
->index0
* iter
->sub
.gpt
.pe_size
);
323 if (guid_is0(&gp
->type
))
326 if (gp
->lba_first
< iter
->sub
.gpt
.ufirst
||
327 gp
->lba_last
> iter
->sub
.gpt
.ulast
) {
328 error("Insane GPT partition.\n");
335 static int pi_dos_next_mbr(struct part_iter
*iter
, uint32_t *lba
,
336 struct disk_dos_part_entry
**_dp
)
338 struct disk_dos_part_entry
*dp
;
340 while (++iter
->index0
< 4) {
341 dp
= ((struct disk_dos_mbr
*)iter
->data
)->table
+ iter
->index0
;
343 if (notsane_primary(iter
)) {
344 iter
->status
= PI_INSANE
;
348 if (ost_is_ext(dp
->ostype
)) {
349 if (iter
->sub
.dos
.bebr_index0
>= 0) {
350 error("You have more than 1 extended partition.\n");
351 iter
->status
= PI_INSANE
;
354 /* record base EBR index */
355 iter
->sub
.dos
.bebr_index0
= iter
->index0
;
357 if (!ost_is_nondata(dp
->ostype
) || iter
->stepall
) {
358 *lba
= dp
->start_lba
;
369 static int prep_base_ebr(struct part_iter
*iter
)
371 struct disk_dos_part_entry
*dp
;
373 if (iter
->sub
.dos
.bebr_index0
< 0) /* if we don't have base extended partition at all */
375 else if (!iter
->sub
.dos
.bebr_start
) { /* if not initialized yet */
376 dp
= ((struct disk_dos_mbr
*)iter
->data
)->table
+ iter
->sub
.dos
.bebr_index0
;
378 iter
->sub
.dos
.bebr_start
= dp
->start_lba
;
379 iter
->sub
.dos
.bebr_size
= dp
->length
;
381 iter
->sub
.dos
.ebr_start
= 0;
382 iter
->sub
.dos
.ebr_size
= iter
->sub
.dos
.bebr_size
;
384 iter
->sub
.dos
.cebr_lba
= 0;
385 iter
->sub
.dos
.nebr_lba
= iter
->sub
.dos
.bebr_start
;
392 static int pi_dos_next_ebr(struct part_iter
*iter
, uint32_t *lba
,
393 struct disk_dos_part_entry
**_dp
)
395 struct disk_dos_part_entry
*dp
;
397 if (prep_base_ebr(iter
)) {
398 iter
->status
= PI_DONE
;
402 while (++iter
->index0
< 1024 && iter
->sub
.dos
.nebr_lba
) {
405 disk_read_sectors(&iter
->di
, iter
->sub
.dos
.nebr_lba
, 1))) {
406 error("Couldn't load EBR.\n");
407 iter
->status
= PI_ERRLOAD
;
411 if (notsane_logical(iter
) || notsane_extended(iter
)) {
412 iter
->status
= PI_INSANE
;
416 dp
= ((struct disk_dos_mbr
*)iter
->data
)->table
;
418 iter
->sub
.dos
.cebr_lba
= iter
->sub
.dos
.nebr_lba
;
420 /* setup next frame values */
422 iter
->sub
.dos
.ebr_start
= dp
[1].start_lba
;
423 iter
->sub
.dos
.ebr_size
= dp
[1].length
;
424 iter
->sub
.dos
.nebr_lba
= iter
->sub
.dos
.bebr_start
+ dp
[1].start_lba
;
426 iter
->sub
.dos
.ebr_start
= 0;
427 iter
->sub
.dos
.ebr_size
= 0;
428 iter
->sub
.dos
.nebr_lba
= 0;
432 iter
->sub
.dos
.skipcnt
++;
434 if (dp
[0].ostype
|| iter
->stepall
) {
435 *lba
= iter
->sub
.dos
.cebr_lba
+ dp
[0].start_lba
;
440 * This way it's possible to continue, if some crazy soft left a "hole"
441 * - EBR with a valid extended partition without a logical one. In
442 * such case, linux will not reserve a number for such hole - so we
443 * don't increase index0. If stepall flag is set, we will never reach
447 iter
->status
= PI_DONE
;
451 static struct part_iter
*pi_dos_next(struct part_iter
*iter
)
453 uint32_t start_lba
= 0;
454 struct disk_dos_part_entry
*dos_part
= NULL
;
459 /* look for primary partitions */
460 if (iter
->index0
< 4 &&
461 pi_dos_next_mbr(iter
, &start_lba
, &dos_part
))
464 /* look for logical partitions */
465 if (iter
->index0
>= 4 &&
466 pi_dos_next_ebr(iter
, &start_lba
, &dos_part
))
470 * note special index handling, if we have stepall set -
471 * this is made to keep index consistent with non-stepall
475 if (iter
->index0
>= 4 && !dos_part
->ostype
)
478 iter
->index
= iter
->index0
- iter
->sub
.dos
.skipcnt
+ 1;
479 iter
->rawindex
= iter
->index0
+ 1;
480 iter
->start_lba
= start_lba
;
481 iter
->record
= (char *)dos_part
;
484 disk_dos_part_dump(dos_part
);
492 static void gpt_conv_label(struct part_iter
*iter
)
494 const struct disk_gpt_part_entry
*gp
;
495 const int16_t *orig_lab
;
497 gp
= (const struct disk_gpt_part_entry
*)
498 (iter
->data
+ iter
->index0
* iter
->sub
.gpt
.pe_size
);
499 orig_lab
= (const int16_t *)gp
->name
;
501 /* caveat: this is very crude conversion */
502 for (int i
= 0; i
< PI_GPTLABSIZE
/2; i
++) {
503 iter
->sub
.gpt
.part_label
[i
] = (char)orig_lab
[i
];
505 iter
->sub
.gpt
.part_label
[PI_GPTLABSIZE
/2] = 0;
508 static struct part_iter
*pi_gpt_next(struct part_iter
*iter
)
510 const struct disk_gpt_part_entry
*gpt_part
= NULL
;
515 while (++iter
->index0
< iter
->sub
.gpt
.pe_count
) {
516 gpt_part
= (const struct disk_gpt_part_entry
*)
517 (iter
->data
+ iter
->index0
* iter
->sub
.gpt
.pe_size
);
519 if (notsane_gpt(iter
)) {
520 iter
->status
= PI_INSANE
;
524 if (!guid_is0(&gpt_part
->type
) || iter
->stepall
)
527 /* no more partitions ? */
528 if (iter
->index0
== iter
->sub
.gpt
.pe_count
) {
529 iter
->status
= PI_DONE
;
532 /* gpt_part is guaranteed to be valid here */
533 iter
->index
= iter
->index0
+ 1;
534 iter
->rawindex
= iter
->index0
+ 1;
535 iter
->start_lba
= gpt_part
->lba_first
;
536 iter
->record
= (char *)gpt_part
;
537 memcpy(&iter
->sub
.gpt
.part_guid
, &gpt_part
->uid
, sizeof(struct guid
));
538 gpt_conv_label(iter
);
541 disk_gpt_part_dump(gpt_part
);
549 static struct part_iter
*pi_raw_next(struct part_iter
*iter
)
551 iter
->status
= PI_DONE
;
555 static int check_crc(uint32_t crc_match
, const uint8_t *buf
, unsigned int siz
)
559 crc
= crc32(0, NULL
, 0);
560 crc
= crc32(crc
, buf
, siz
);
562 return crc_match
!= crc
;
565 static int gpt_check_hdr_crc(const struct disk_info
* const diskinfo
, struct disk_gpt_header
**_gh
)
567 struct disk_gpt_header
*gh
= *_gh
;
571 hold_crc32
= gh
->chksum
;
573 if (check_crc(hold_crc32
, (const uint8_t *)gh
, gh
->hdr_size
)) {
574 error("WARNING: Primary GPT header checksum invalid.\n");
575 /* retry with backup */
576 lba_alt
= gh
->lba_alt
;
578 if (!(gh
= *_gh
= disk_read_sectors(diskinfo
, lba_alt
, 1))) {
579 error("Couldn't read backup GPT header.\n");
582 hold_crc32
= gh
->chksum
;
584 if (check_crc(hold_crc32
, (const uint8_t *)gh
, gh
->hdr_size
)) {
585 error("Secondary GPT header checksum invalid.\n");
589 /* restore old checksum */
590 gh
->chksum
= hold_crc32
;
596 * ----------------------------------------------------------------------------
597 * Following functions are for users to call.
598 * ----------------------------------------------------------------------------
602 int pi_next(struct part_iter
**_iter
)
604 struct part_iter
*iter
;
606 if(!_iter
|| !*_iter
)
610 if (inv_type(iter
->type
)) {
611 error("This is not a valid iterator.\n");
615 if ((iter
= iter
->type
->next(iter
))) {
618 return (*_iter
)->status
;
622 * pi_new() - get new iterator
623 * @itertype: iterator type
624 * @...: variable arguments passed to ctors
626 * Variable arguments depend on the type. Please see functions:
627 * iter_gpt_ctor() and iter_dos_ctor() for details.
629 struct part_iter
*pi_new(const struct itertype
*type
, ...)
632 struct part_iter
*iter
= NULL
;
638 if (inv_type(type
)) {
639 error("Unknown iterator requested.\n");
644 if (!(iter
= malloc(sizeof(struct part_iter
)))) {
645 error("Couldn't allocate memory for the iterator.\n");
649 memset(iter
, 0, sizeof(struct part_iter
));
652 if (type
->ctor(iter
, &ap
)) {
654 error("Cannot initialize the iterator.\n");
668 * pi_del() - delete iterator
669 * @iter: iterator double pointer
673 void pi_del(struct part_iter
**_iter
)
675 struct part_iter
*iter
;
677 if(!_iter
|| !*_iter
)
682 if (inv_type(iter
->type
)) {
683 error("This is not a valid iterator.\n");
688 iter
->type
->dtor(iter
);
694 * pi_begin() - check disk, validate, and get proper iterator
695 * @di: diskinfo struct pointer
697 * This function checks the disk for GPT or legacy partition table and allocates
698 * an appropriate iterator.
700 struct part_iter
*pi_begin(const struct disk_info
*di
, int stepall
)
703 struct part_iter
*iter
= NULL
;
704 struct disk_dos_mbr
*mbr
= NULL
;
705 struct disk_gpt_header
*gpth
= NULL
;
706 struct disk_gpt_part_entry
*gptl
= NULL
;
709 if (!(mbr
= disk_read_sectors(di
, 0, 1))) {
710 error("Couldn't read first disk sector.\n");
716 /* Check for MBR magic*/
717 if (mbr
->sig
!= disk_mbr_sig_magic
) {
718 error("No MBR magic.\n");
722 /* Check for GPT protective MBR */
723 if (mbr
->table
[0].ostype
== 0xEE) {
724 if (!(gpth
= disk_read_sectors(di
, 1, 1))) {
725 error("Couldn't read potential GPT header.\n");
730 if (gpth
&& gpth
->rev
.uint32
== 0x00010000 &&
731 !memcmp(gpth
->sig
, disk_gpt_sig_magic
, sizeof(disk_gpt_sig_magic
))) {
732 /* looks like GPT v1.0 */
733 uint64_t gpt_loff
; /* offset to GPT partition list in sectors */
734 uint64_t gpt_lsiz
; /* size of GPT partition list in bytes */
735 uint64_t gpt_lcnt
; /* size of GPT partition in sectors */
737 puts("Looks like a GPT v1.0 disk.");
738 disk_gpt_header_dump(gpth
);
740 /* Verify checksum, fallback to backup, then bail if invalid */
741 if (gpt_check_hdr_crc(di
, &gpth
))
744 gpt_loff
= gpth
->lba_table
;
745 gpt_lsiz
= (uint64_t)gpth
->part_size
* gpth
->part_count
;
746 gpt_lcnt
= (gpt_lsiz
+ di
->bps
- 1) / di
->bps
;
749 * disk_read_sectors allows reading of max 255 sectors, so we use
750 * it as a sanity check base. EFI doesn't specify max (AFAIK).
751 * Apart from that, some extensive sanity checks.
753 if (!gpt_loff
|| !gpt_lsiz
|| gpt_lcnt
> 255u ||
754 gpth
->lba_first_usable
> gpth
->lba_last_usable
||
755 !sane(gpt_loff
, gpt_lcnt
) ||
756 gpt_loff
+ gpt_lcnt
> gpth
->lba_first_usable
||
757 !sane(gpth
->lba_last_usable
, gpt_lcnt
) ||
758 gpth
->lba_last_usable
+ gpt_lcnt
>= gpth
->lba_alt
||
759 gpth
->lba_alt
>= di
->lbacnt
||
760 gpth
->part_size
< sizeof(struct disk_gpt_part_entry
)) {
761 error("Invalid GPT header's values.\n");
764 if (!(gptl
= disk_read_sectors(di
, gpt_loff
, (uint8_t)gpt_lcnt
))) {
765 error("Couldn't read GPT partition list.\n");
768 /* Check array checksum(s). */
769 if (check_crc(gpth
->table_chksum
, (const uint8_t *)gptl
, (unsigned int)gpt_lsiz
)) {
770 error("WARNING: GPT partition list checksum invalid, trying backup.\n");
772 /* secondary array directly precedes secondary header */
773 if (!(gptl
= disk_read_sectors(di
, gpth
->lba_alt
- gpt_lcnt
, (uint8_t)gpt_lcnt
))) {
774 error("Couldn't read backup GPT partition list.\n");
777 if (check_crc(gpth
->table_chksum
, (const uint8_t *)gptl
, (unsigned int)gpt_lsiz
)) {
778 error("Backup GPT partition list checksum invalid.\n");
782 /* allocate iterator and exit */
783 iter
= pi_new(typegpt
, di
, stepall
, gpth
, gptl
);
786 iter
= pi_new(typedos
, di
, stepall
, mbr
);
792 error("WARNING: treating disk as raw.\n");
793 iter
= pi_new(typeraw
, di
, stepall
);
802 /* vim: set ts=8 sts=4 sw=4 noet: */