Home | History | Annotate | Line # | Download | only in sysinst
partitions.c revision 1.1.2.2
      1  1.1.2.2  msaitoh /*	$NetBSD: partitions.c,v 1.1.2.2 2019/10/28 02:53:17 msaitoh Exp $	*/
      2      1.1   martin 
      3      1.1   martin /*
      4      1.1   martin  * Copyright 2018 The NetBSD Foundation, Inc.
      5      1.1   martin  * All rights reserved.
      6      1.1   martin  *
      7      1.1   martin  * Redistribution and use in source and binary forms, with or without
      8      1.1   martin  * modification, are permitted provided that the following conditions
      9      1.1   martin  * are met:
     10      1.1   martin  * 1. Redistributions of source code must retain the above copyright
     11      1.1   martin  *    notice, this list of conditions and the following disclaimer.
     12      1.1   martin  * 2. Redistributions in binary form must reproduce the above copyright
     13      1.1   martin  *    notice, this list of conditions and the following disclaimer in the
     14      1.1   martin  *    documentation and/or other materials provided with the distribution.
     15      1.1   martin  *
     16      1.1   martin  * THIS SOFTWARE IS PROVIDED BY PIERMONT INFORMATION SYSTEMS INC. ``AS IS''
     17      1.1   martin  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18      1.1   martin  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19      1.1   martin  * ARE DISCLAIMED. IN NO EVENT SHALL PIERMONT INFORMATION SYSTEMS INC. BE
     20      1.1   martin  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21      1.1   martin  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22      1.1   martin  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23      1.1   martin  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24      1.1   martin  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25      1.1   martin  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
     26      1.1   martin  * THE POSSIBILITY OF SUCH DAMAGE.
     27      1.1   martin  *
     28      1.1   martin  */
     29      1.1   martin 
     30      1.1   martin #include "defs.h"
     31      1.1   martin #include "mbr.h"
     32      1.1   martin #include <assert.h>
     33      1.1   martin 
     34      1.1   martin /*
     35      1.1   martin  * A list of partitioning schemes, so we can iterate over everything
     36      1.1   martin  * supported (e.g. when partitioning a new disk). NULL terminated.
     37      1.1   martin  */
     38      1.1   martin const struct disk_partitioning_scheme **available_part_schemes;
     39      1.1   martin /*
     40      1.1   martin  * The number of valid entries on above list
     41      1.1   martin  */
     42      1.1   martin size_t num_available_part_schemes;
     43      1.1   martin 
     44      1.1   martin /*
     45      1.1   martin  * Generic reader - query a disk device and read all partitions from it
     46      1.1   martin  */
     47      1.1   martin struct disk_partitions *
     48  1.1.2.2  msaitoh partitions_read_disk(const char *dev, daddr_t disk_size, bool no_mbr)
     49      1.1   martin {
     50      1.1   martin 	const struct disk_partitioning_scheme **ps;
     51      1.1   martin 
     52      1.1   martin 	if (!available_part_schemes)
     53      1.1   martin 		return NULL;
     54      1.1   martin 
     55      1.1   martin 	for (ps = available_part_schemes; *ps; ps++) {
     56  1.1.2.2  msaitoh #ifdef HAVE_MBR
     57  1.1.2.2  msaitoh 		if (no_mbr && (*ps)->name == MSG_parttype_mbr)
     58  1.1.2.2  msaitoh 			continue;
     59  1.1.2.2  msaitoh #endif
     60      1.1   martin 		struct disk_partitions *parts =
     61  1.1.2.1  msaitoh 		    (*ps)->read_from_disk(dev, 0, disk_size, *ps);
     62      1.1   martin 		if (parts)
     63      1.1   martin 			return parts;
     64      1.1   martin 	}
     65      1.1   martin 	return NULL;
     66      1.1   martin }
     67      1.1   martin 
     68      1.1   martin /*************** global init ****************************************/
     69      1.1   martin /*
     70      1.1   martin  * Helper structure to fill our global list of available partitioning
     71      1.1   martin  * schemes.
     72      1.1   martin  */
     73      1.1   martin struct part_scheme_desc {
     74      1.1   martin 	bool (*is_available)(void);
     75      1.1   martin 	const struct disk_partitioning_scheme *ps;
     76      1.1   martin };
     77      1.1   martin 
     78      1.1   martin #ifdef HAVE_GPT
     79      1.1   martin bool gpt_parts_check(void);
     80      1.1   martin extern const struct disk_partitioning_scheme gpt_parts;
     81      1.1   martin #endif
     82      1.1   martin #ifdef HAVE_MBR
     83      1.1   martin extern const struct disk_partitioning_scheme mbr_parts;
     84      1.1   martin #endif
     85  1.1.2.1  msaitoh 
     86  1.1.2.1  msaitoh extern const struct disk_partitioning_scheme disklabel_parts;
     87  1.1.2.1  msaitoh #if RAW_PART != 2
     88  1.1.2.1  msaitoh static struct disk_partitioning_scheme only_disklabel_parts;
     89  1.1.2.1  msaitoh 
     90  1.1.2.1  msaitoh /*
     91  1.1.2.1  msaitoh  * If not overriden by MD code, we can not boot from plain
     92  1.1.2.1  msaitoh  * disklabel disks (w/o MBR).
     93  1.1.2.1  msaitoh  */
     94  1.1.2.1  msaitoh static bool have_only_disklabel_boot_support(const char *disk)
     95  1.1.2.1  msaitoh {
     96  1.1.2.1  msaitoh #ifdef HAVE_PLAIN_DISKLABEL_BOOT
     97  1.1.2.1  msaitoh 	return HAVE_PLAIN_DISKLABEL_BOOT(disk);
     98  1.1.2.1  msaitoh #else
     99  1.1.2.1  msaitoh 	return false;
    100  1.1.2.1  msaitoh #endif
    101  1.1.2.1  msaitoh }
    102      1.1   martin #endif
    103      1.1   martin 
    104      1.1   martin /*
    105      1.1   martin  * One time initialization
    106      1.1   martin  */
    107      1.1   martin void
    108      1.1   martin partitions_init(void)
    109      1.1   martin {
    110      1.1   martin 	/*
    111      1.1   martin 	 * List of partitioning schemes.
    112      1.1   martin 	 * Order is important, the selection menu is created from start
    113      1.1   martin 	 * to end. Keep good defaults early. Most architectures will
    114      1.1   martin 	 * only offer very few entries.
    115      1.1   martin 	 */
    116      1.1   martin static const struct part_scheme_desc all_descs[] = {
    117      1.1   martin #if RAW_PART == 2	/* only available as primary on some architectures */
    118      1.1   martin 		{ NULL, &disklabel_parts },
    119      1.1   martin #endif
    120      1.1   martin #ifdef HAVE_GPT
    121      1.1   martin 		{ gpt_parts_check, &gpt_parts },
    122      1.1   martin #endif
    123      1.1   martin #ifdef HAVE_MBR
    124      1.1   martin 		{ NULL, &mbr_parts },
    125      1.1   martin #endif
    126  1.1.2.1  msaitoh #if RAW_PART != 2	/* "whole disk NetBSD" disklabel variant */
    127  1.1.2.1  msaitoh 		{ NULL, &only_disklabel_parts },
    128  1.1.2.1  msaitoh #endif
    129      1.1   martin 	};
    130      1.1   martin 
    131      1.1   martin 	size_t i, avail;
    132      1.1   martin 	const struct disk_partitioning_scheme **out;
    133      1.1   martin 	bool *is_available;
    134      1.1   martin 	static const size_t all_cnt = __arraycount(all_descs);
    135      1.1   martin 
    136      1.1   martin 	check_available_binaries();
    137      1.1   martin 
    138  1.1.2.1  msaitoh #if RAW_PART != 2
    139  1.1.2.1  msaitoh 	/* generate a variant of disklabel w/o parent scheme */
    140  1.1.2.1  msaitoh 	only_disklabel_parts = disklabel_parts;
    141  1.1.2.1  msaitoh 	only_disklabel_parts.name = MSG_parttype_only_disklabel;
    142  1.1.2.1  msaitoh 	only_disklabel_parts.have_boot_support =
    143  1.1.2.1  msaitoh 	    have_only_disklabel_boot_support;
    144  1.1.2.1  msaitoh #endif
    145  1.1.2.1  msaitoh 
    146  1.1.2.1  msaitoh 
    147      1.1   martin 	is_available = malloc(all_cnt);
    148      1.1   martin 
    149      1.1   martin 	for (avail = i = 0; i < all_cnt; i++) {
    150      1.1   martin 		is_available[i] = all_descs[i].is_available == NULL
    151      1.1   martin 				|| all_descs[i].is_available();
    152      1.1   martin 		if (is_available[i])
    153      1.1   martin 			avail++;
    154      1.1   martin 	}
    155      1.1   martin 
    156      1.1   martin 	if (avail == 0)
    157      1.1   martin 		return;
    158      1.1   martin 
    159      1.1   martin 	num_available_part_schemes = avail;
    160      1.1   martin 	available_part_schemes = malloc(sizeof(*available_part_schemes)
    161      1.1   martin 	    * (avail+1));
    162      1.1   martin 	if (available_part_schemes == NULL)
    163      1.1   martin 		return;
    164      1.1   martin 
    165      1.1   martin 	for (out = available_part_schemes, i = 0; i < all_cnt; i++) {
    166      1.1   martin 		if (!is_available[i])
    167      1.1   martin 			continue;
    168      1.1   martin 		*out++ = all_descs[i].ps;
    169      1.1   martin 	}
    170      1.1   martin 	*out = NULL;
    171      1.1   martin 
    172      1.1   martin 	free(is_available);
    173      1.1   martin }
    174