Home | History | Annotate | Line # | Download | only in sysinst
partitions.c revision 1.1.2.1
      1  1.1.2.1  msaitoh /*	$NetBSD: partitions.c,v 1.1.2.1 2019/08/18 13:21:40 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   martin partitions_read_disk(const char *dev, daddr_t disk_size)
     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   martin 		struct disk_partitions *parts =
     57  1.1.2.1  msaitoh 		    (*ps)->read_from_disk(dev, 0, disk_size, *ps);
     58      1.1   martin 		if (parts)
     59      1.1   martin 			return parts;
     60      1.1   martin 	}
     61      1.1   martin 	return NULL;
     62      1.1   martin }
     63      1.1   martin 
     64      1.1   martin /*************** global init ****************************************/
     65      1.1   martin /*
     66      1.1   martin  * Helper structure to fill our global list of available partitioning
     67      1.1   martin  * schemes.
     68      1.1   martin  */
     69      1.1   martin struct part_scheme_desc {
     70      1.1   martin 	bool (*is_available)(void);
     71      1.1   martin 	const struct disk_partitioning_scheme *ps;
     72      1.1   martin };
     73      1.1   martin 
     74      1.1   martin #ifdef HAVE_GPT
     75      1.1   martin bool gpt_parts_check(void);
     76      1.1   martin extern const struct disk_partitioning_scheme gpt_parts;
     77      1.1   martin #endif
     78      1.1   martin #ifdef HAVE_MBR
     79      1.1   martin extern const struct disk_partitioning_scheme mbr_parts;
     80      1.1   martin #endif
     81  1.1.2.1  msaitoh 
     82  1.1.2.1  msaitoh extern const struct disk_partitioning_scheme disklabel_parts;
     83  1.1.2.1  msaitoh #if RAW_PART != 2
     84  1.1.2.1  msaitoh static struct disk_partitioning_scheme only_disklabel_parts;
     85  1.1.2.1  msaitoh 
     86  1.1.2.1  msaitoh /*
     87  1.1.2.1  msaitoh  * If not overriden by MD code, we can not boot from plain
     88  1.1.2.1  msaitoh  * disklabel disks (w/o MBR).
     89  1.1.2.1  msaitoh  */
     90  1.1.2.1  msaitoh static bool have_only_disklabel_boot_support(const char *disk)
     91  1.1.2.1  msaitoh {
     92  1.1.2.1  msaitoh #ifdef HAVE_PLAIN_DISKLABEL_BOOT
     93  1.1.2.1  msaitoh 	return HAVE_PLAIN_DISKLABEL_BOOT(disk);
     94  1.1.2.1  msaitoh #else
     95  1.1.2.1  msaitoh 	return false;
     96  1.1.2.1  msaitoh #endif
     97  1.1.2.1  msaitoh }
     98      1.1   martin #endif
     99      1.1   martin 
    100      1.1   martin /*
    101      1.1   martin  * One time initialization
    102      1.1   martin  */
    103      1.1   martin void
    104      1.1   martin partitions_init(void)
    105      1.1   martin {
    106      1.1   martin 	/*
    107      1.1   martin 	 * List of partitioning schemes.
    108      1.1   martin 	 * Order is important, the selection menu is created from start
    109      1.1   martin 	 * to end. Keep good defaults early. Most architectures will
    110      1.1   martin 	 * only offer very few entries.
    111      1.1   martin 	 */
    112      1.1   martin static const struct part_scheme_desc all_descs[] = {
    113      1.1   martin #if RAW_PART == 2	/* only available as primary on some architectures */
    114      1.1   martin 		{ NULL, &disklabel_parts },
    115      1.1   martin #endif
    116      1.1   martin #ifdef HAVE_GPT
    117      1.1   martin 		{ gpt_parts_check, &gpt_parts },
    118      1.1   martin #endif
    119      1.1   martin #ifdef HAVE_MBR
    120      1.1   martin 		{ NULL, &mbr_parts },
    121      1.1   martin #endif
    122  1.1.2.1  msaitoh #if RAW_PART != 2	/* "whole disk NetBSD" disklabel variant */
    123  1.1.2.1  msaitoh 		{ NULL, &only_disklabel_parts },
    124  1.1.2.1  msaitoh #endif
    125      1.1   martin 	};
    126      1.1   martin 
    127      1.1   martin 	size_t i, avail;
    128      1.1   martin 	const struct disk_partitioning_scheme **out;
    129      1.1   martin 	bool *is_available;
    130      1.1   martin 	static const size_t all_cnt = __arraycount(all_descs);
    131      1.1   martin 
    132      1.1   martin 	check_available_binaries();
    133      1.1   martin 
    134  1.1.2.1  msaitoh #if RAW_PART != 2
    135  1.1.2.1  msaitoh 	/* generate a variant of disklabel w/o parent scheme */
    136  1.1.2.1  msaitoh 	only_disklabel_parts = disklabel_parts;
    137  1.1.2.1  msaitoh 	only_disklabel_parts.name = MSG_parttype_only_disklabel;
    138  1.1.2.1  msaitoh 	only_disklabel_parts.have_boot_support =
    139  1.1.2.1  msaitoh 	    have_only_disklabel_boot_support;
    140  1.1.2.1  msaitoh #endif
    141  1.1.2.1  msaitoh 
    142  1.1.2.1  msaitoh 
    143      1.1   martin 	is_available = malloc(all_cnt);
    144      1.1   martin 
    145      1.1   martin 	for (avail = i = 0; i < all_cnt; i++) {
    146      1.1   martin 		is_available[i] = all_descs[i].is_available == NULL
    147      1.1   martin 				|| all_descs[i].is_available();
    148      1.1   martin 		if (is_available[i])
    149      1.1   martin 			avail++;
    150      1.1   martin 	}
    151      1.1   martin 
    152      1.1   martin 	if (avail == 0)
    153      1.1   martin 		return;
    154      1.1   martin 
    155      1.1   martin 	num_available_part_schemes = avail;
    156      1.1   martin 	available_part_schemes = malloc(sizeof(*available_part_schemes)
    157      1.1   martin 	    * (avail+1));
    158      1.1   martin 	if (available_part_schemes == NULL)
    159      1.1   martin 		return;
    160      1.1   martin 
    161      1.1   martin 	for (out = available_part_schemes, i = 0; i < all_cnt; i++) {
    162      1.1   martin 		if (!is_available[i])
    163      1.1   martin 			continue;
    164      1.1   martin 		*out++ = all_descs[i].ps;
    165      1.1   martin 	}
    166      1.1   martin 	*out = NULL;
    167      1.1   martin 
    168      1.1   martin 	free(is_available);
    169      1.1   martin }
    170