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
;
112 iter
->length
= di
->lbacnt
;
118 * iter_dtor() - common iterator cleanup
119 * @iter: iterator pointer
122 static void iter_dtor(struct part_iter
*iter
)
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
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.
137 static int iter_dos_ctor(struct part_iter
*iter
, va_list *args
)
139 const struct disk_dos_mbr
*mbr
;
142 if (iter_ctor(iter
, args
))
145 mbr
= va_arg(*args
, const struct disk_dos_mbr
*);
152 if (!(iter
->data
= malloc(sizeof(struct disk_dos_mbr
))))
155 memcpy(iter
->data
, mbr
, sizeof(struct disk_dos_mbr
));
157 iter
->sub
.dos
.bebr_index0
= -1;
158 iter
->sub
.dos
.disk_sig
= mbr
->disk_sig
;
162 iter
->type
->dtor(iter
);
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
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.
177 static int iter_gpt_ctor(struct part_iter
*iter
, va_list *args
)
180 const struct disk_gpt_header
*gpth
;
181 const struct disk_gpt_part_entry
*gptl
;
184 if (iter_ctor(iter
, args
))
187 gpth
= va_arg(*args
, const struct disk_gpt_header
*);
188 gptl
= va_arg(*args
, const struct disk_gpt_part_entry
*);
195 siz
= (uint64_t)gpth
->part_count
* gpth
->part_size
;
198 if (!siz
|| (siz
+ iter
->di
.bps
- 1) / iter
->di
.bps
> 255u ||
199 gpth
->part_size
< sizeof(struct disk_gpt_part_entry
)) {
204 if (!(iter
->data
= malloc((size_t)siz
)))
207 memcpy(iter
->data
, gptl
, (size_t)siz
);
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
;
214 memcpy(&iter
->sub
.gpt
.disk_guid
, &gpth
->disk_guid
, sizeof(struct guid
));
218 iter
->type
->dtor(iter
);
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
229 static int notsane_logical(const struct part_iter
*iter
)
231 const struct disk_dos_part_entry
*dp
;
234 dp
= ((struct disk_dos_mbr
*)iter
->data
)->table
;
239 if (ost_is_ext(dp
[0].ostype
)) {
240 error("1st EBR entry must be data or empty.\n");
244 end_log
= dp
[0].start_lba
+ dp
[0].length
;
246 if (!dp
[0].start_lba
||
248 !sane(dp
[0].start_lba
, dp
[0].length
) ||
249 end_log
> iter
->sub
.dos
.ebr_size
) {
251 error("Insane logical partition.\n");
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
265 static int notsane_extended(const struct part_iter
*iter
)
267 const struct disk_dos_part_entry
*dp
;
270 dp
= ((struct disk_dos_mbr
*)iter
->data
)->table
;
275 if (!ost_is_nondata(dp
[1].ostype
)) {
276 error("2nd EBR entry must be extended or empty.\n");
280 end_ebr
= dp
[1].start_lba
+ dp
[1].length
;
282 if (!dp
[1].start_lba
||
284 !sane(dp
[1].start_lba
, dp
[1].length
) ||
285 end_ebr
> iter
->sub
.dos
.bebr_size
) {
287 error("Insane extended partition.\n");
294 /* Primary partition must be sane, meaning:
295 * - must have non-0 start and length
296 * - values must not wrap around 32bit
299 static int notsane_primary(const struct part_iter
*iter
)
301 const struct disk_dos_part_entry
*dp
;
302 dp
= ((struct disk_dos_mbr
*)iter
->data
)->table
+ iter
->index0
;
307 if (!dp
->start_lba
||
309 !sane(dp
->start_lba
, dp
->length
) ||
310 dp
->start_lba
+ dp
->length
> iter
->di
.lbacnt
) {
311 error("Insane primary (MBR) partition.\n");
318 static int notsane_gpt(const struct part_iter
*iter
)
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
);
324 if (guid_is0(&gp
->type
))
327 if (gp
->lba_first
< iter
->sub
.gpt
.ufirst
||
328 gp
->lba_last
> iter
->sub
.gpt
.ulast
) {
329 error("Insane GPT partition.\n");
336 static int pi_dos_next_mbr(struct part_iter
*iter
, uint32_t *lba
,
337 struct disk_dos_part_entry
**_dp
)
339 struct disk_dos_part_entry
*dp
;
341 while (++iter
->index0
< 4) {
342 dp
= ((struct disk_dos_mbr
*)iter
->data
)->table
+ iter
->index0
;
344 if (notsane_primary(iter
)) {
345 iter
->status
= PI_INSANE
;
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
;
355 /* record base EBR index */
356 iter
->sub
.dos
.bebr_index0
= iter
->index0
;
358 if (!ost_is_nondata(dp
->ostype
) || iter
->stepall
) {
359 *lba
= dp
->start_lba
;
370 static int prep_base_ebr(struct part_iter
*iter
)
372 struct disk_dos_part_entry
*dp
;
374 if (iter
->sub
.dos
.bebr_index0
< 0) /* if we don't have base extended partition at all */
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
;
379 iter
->sub
.dos
.bebr_start
= dp
->start_lba
;
380 iter
->sub
.dos
.bebr_size
= dp
->length
;
382 iter
->sub
.dos
.ebr_start
= 0;
383 iter
->sub
.dos
.ebr_size
= iter
->sub
.dos
.bebr_size
;
385 iter
->sub
.dos
.cebr_lba
= 0;
386 iter
->sub
.dos
.nebr_lba
= iter
->sub
.dos
.bebr_start
;
393 static int pi_dos_next_ebr(struct part_iter
*iter
, uint32_t *lba
,
394 struct disk_dos_part_entry
**_dp
)
396 struct disk_dos_part_entry
*dp
;
398 if (prep_base_ebr(iter
)) {
399 iter
->status
= PI_DONE
;
403 while (++iter
->index0
< 1024 && iter
->sub
.dos
.nebr_lba
) {
406 disk_read_sectors(&iter
->di
, iter
->sub
.dos
.nebr_lba
, 1))) {
407 error("Couldn't load EBR.\n");
408 iter
->status
= PI_ERRLOAD
;
412 if (notsane_logical(iter
) || notsane_extended(iter
)) {
413 iter
->status
= PI_INSANE
;
417 dp
= ((struct disk_dos_mbr
*)iter
->data
)->table
;
419 iter
->sub
.dos
.cebr_lba
= iter
->sub
.dos
.nebr_lba
;
421 /* setup next frame values */
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
;
427 iter
->sub
.dos
.ebr_start
= 0;
428 iter
->sub
.dos
.ebr_size
= 0;
429 iter
->sub
.dos
.nebr_lba
= 0;
433 iter
->sub
.dos
.skipcnt
++;
435 if (dp
[0].ostype
|| iter
->stepall
) {
436 *lba
= iter
->sub
.dos
.cebr_lba
+ dp
[0].start_lba
;
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
448 iter
->status
= PI_DONE
;
452 static struct part_iter
*pi_dos_next(struct part_iter
*iter
)
454 uint32_t start_lba
= 0;
455 struct disk_dos_part_entry
*dos_part
= NULL
;
460 /* look for primary partitions */
461 if (iter
->index0
< 4 &&
462 pi_dos_next_mbr(iter
, &start_lba
, &dos_part
))
465 /* look for logical partitions */
466 if (iter
->index0
>= 4 &&
467 pi_dos_next_ebr(iter
, &start_lba
, &dos_part
))
471 * note special index handling, if we have stepall set -
472 * this is made to keep index consistent with non-stepall
476 if (iter
->index0
>= 4 && !dos_part
->ostype
)
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
;
486 disk_dos_part_dump(dos_part
);
494 static void gpt_conv_label(struct part_iter
*iter
)
496 const struct disk_gpt_part_entry
*gp
;
497 const int16_t *orig_lab
;
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
;
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
];
507 iter
->sub
.gpt
.part_label
[PI_GPTLABSIZE
/2] = 0;
510 static struct part_iter
*pi_gpt_next(struct part_iter
*iter
)
512 const struct disk_gpt_part_entry
*gpt_part
= NULL
;
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
);
521 if (notsane_gpt(iter
)) {
522 iter
->status
= PI_INSANE
;
526 if (!guid_is0(&gpt_part
->type
) || iter
->stepall
)
529 /* no more partitions ? */
530 if (iter
->index0
== iter
->sub
.gpt
.pe_count
) {
531 iter
->status
= PI_DONE
;
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
);
544 disk_gpt_part_dump(gpt_part
);
552 static struct part_iter
*pi_raw_next(struct part_iter
*iter
)
554 iter
->status
= PI_DONE
;
558 static int check_crc(uint32_t crc_match
, const uint8_t *buf
, unsigned int siz
)
562 crc
= crc32(0, NULL
, 0);
563 crc
= crc32(crc
, buf
, siz
);
565 return crc_match
!= crc
;
568 static int gpt_check_hdr_crc(const struct disk_info
* const diskinfo
, struct disk_gpt_header
**_gh
)
570 struct disk_gpt_header
*gh
= *_gh
;
574 hold_crc32
= gh
->chksum
;
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
;
581 if (!(gh
= *_gh
= disk_read_sectors(diskinfo
, lba_alt
, 1))) {
582 error("Couldn't read backup GPT header.\n");
585 hold_crc32
= gh
->chksum
;
587 if (check_crc(hold_crc32
, (const uint8_t *)gh
, gh
->hdr_size
)) {
588 error("Secondary GPT header checksum invalid.\n");
592 /* restore old checksum */
593 gh
->chksum
= hold_crc32
;
599 * ----------------------------------------------------------------------------
600 * Following functions are for users to call.
601 * ----------------------------------------------------------------------------
605 int pi_next(struct part_iter
**_iter
)
607 struct part_iter
*iter
;
609 if(!_iter
|| !*_iter
)
613 if (inv_type(iter
->type
)) {
614 error("This is not a valid iterator.\n");
618 if ((iter
= iter
->type
->next(iter
))) {
621 return (*_iter
)->status
;
625 * pi_new() - get new iterator
626 * @itertype: iterator type
627 * @...: variable arguments passed to ctors
629 * Variable arguments depend on the type. Please see functions:
630 * iter_gpt_ctor() and iter_dos_ctor() for details.
632 struct part_iter
*pi_new(const struct itertype
*type
, ...)
635 struct part_iter
*iter
= NULL
;
641 if (inv_type(type
)) {
642 error("Unknown iterator requested.\n");
647 if (!(iter
= malloc(sizeof(struct part_iter
)))) {
648 error("Couldn't allocate memory for the iterator.\n");
652 memset(iter
, 0, sizeof(struct part_iter
));
655 if (type
->ctor(iter
, &ap
)) {
657 error("Cannot initialize the iterator.\n");
671 * pi_del() - delete iterator
672 * @iter: iterator double pointer
676 void pi_del(struct part_iter
**_iter
)
678 struct part_iter
*iter
;
680 if(!_iter
|| !*_iter
)
685 if (inv_type(iter
->type
)) {
686 error("This is not a valid iterator.\n");
691 iter
->type
->dtor(iter
);
697 * pi_begin() - check disk, validate, and get proper iterator
698 * @di: diskinfo struct pointer
700 * This function checks the disk for GPT or legacy partition table and allocates
701 * an appropriate iterator.
703 struct part_iter
*pi_begin(const struct disk_info
*di
, int stepall
)
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
;
712 if (!(mbr
= disk_read_sectors(di
, 0, 1))) {
713 error("Couldn't read first disk sector.\n");
719 /* Check for MBR magic*/
720 if (mbr
->sig
!= disk_mbr_sig_magic
) {
721 error("No MBR magic.\n");
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");
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 */
740 puts("Looks like a GPT v1.0 disk.");
741 disk_gpt_header_dump(gpth
);
743 /* Verify checksum, fallback to backup, then bail if invalid */
744 if (gpt_check_hdr_crc(di
, &gpth
))
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
;
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.
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");
767 if (!(gptl
= disk_read_sectors(di
, gpt_loff
, (uint8_t)gpt_lcnt
))) {
768 error("Couldn't read GPT partition list.\n");
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");
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");
780 if (check_crc(gpth
->table_chksum
, (const uint8_t *)gptl
, (unsigned int)gpt_lsiz
)) {
781 error("Backup GPT partition list checksum invalid.\n");
785 /* allocate iterator and exit */
786 iter
= pi_new(typegpt
, di
, stepall
, gpth
, gptl
);
789 iter
= pi_new(typedos
, di
, stepall
, mbr
);
795 error("WARNING: treating disk as raw.\n");
796 iter
= pi_new(typeraw
, di
, stepall
);
805 /* vim: set ts=8 sts=4 sw=4 noet: */