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