Home | History | Annotate | Line # | Download | only in sysinst
partitions.c revision 1.1.2.4
      1  1.1.2.4  msaitoh /*	$NetBSD: partitions.c,v 1.1.2.4 2019/12/17 09:44:50 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.2.3  msaitoh bool
     69  1.1.2.3  msaitoh generic_adapt_foreign_part_info(const struct disk_partitions *myself,
     70  1.1.2.3  msaitoh     struct disk_part_info *dest,
     71  1.1.2.3  msaitoh     const struct disk_partitioning_scheme *src_scheme,
     72  1.1.2.3  msaitoh     const struct disk_part_info *src)
     73  1.1.2.3  msaitoh {
     74  1.1.2.3  msaitoh 	*dest = *src;
     75  1.1.2.3  msaitoh 	if (myself->pscheme == src_scheme)
     76  1.1.2.3  msaitoh 		return true;	/* no conversion needed */
     77  1.1.2.3  msaitoh 
     78  1.1.2.3  msaitoh 	if (src->nat_type == NULL)
     79  1.1.2.3  msaitoh 		return false;
     80  1.1.2.3  msaitoh 
     81  1.1.2.3  msaitoh 	/* slightly simplistic, enhance when needed */
     82  1.1.2.4  msaitoh 	dest->nat_type = myself->pscheme->get_fs_part_type(
     83  1.1.2.4  msaitoh 	    dest->nat_type ? dest->nat_type->generic_ptype : PT_root,
     84  1.1.2.4  msaitoh 	    dest->fs_type,
     85  1.1.2.3  msaitoh 	    dest->fs_sub_type);
     86  1.1.2.3  msaitoh 	if (dest->nat_type == NULL)
     87  1.1.2.3  msaitoh 		dest->nat_type = myself->pscheme->get_generic_part_type(
     88  1.1.2.3  msaitoh 		    src->nat_type->generic_ptype);
     89  1.1.2.3  msaitoh 	if (dest->nat_type == NULL)
     90  1.1.2.3  msaitoh 		dest->nat_type = myself->pscheme->create_unknown_part_type();
     91  1.1.2.3  msaitoh 	if (dest->nat_type == NULL)
     92  1.1.2.3  msaitoh 		dest->nat_type = myself->pscheme->get_generic_part_type(
     93  1.1.2.3  msaitoh 		    PT_unknown);
     94  1.1.2.3  msaitoh 
     95  1.1.2.3  msaitoh 	return true;
     96  1.1.2.3  msaitoh }
     97  1.1.2.3  msaitoh 
     98      1.1   martin /*************** global init ****************************************/
     99      1.1   martin /*
    100      1.1   martin  * Helper structure to fill our global list of available partitioning
    101      1.1   martin  * schemes.
    102      1.1   martin  */
    103      1.1   martin struct part_scheme_desc {
    104      1.1   martin 	bool (*is_available)(void);
    105      1.1   martin 	const struct disk_partitioning_scheme *ps;
    106      1.1   martin };
    107      1.1   martin 
    108      1.1   martin #ifdef HAVE_GPT
    109      1.1   martin bool gpt_parts_check(void);
    110      1.1   martin extern const struct disk_partitioning_scheme gpt_parts;
    111      1.1   martin #endif
    112      1.1   martin #ifdef HAVE_MBR
    113      1.1   martin extern const struct disk_partitioning_scheme mbr_parts;
    114      1.1   martin #endif
    115  1.1.2.1  msaitoh 
    116  1.1.2.1  msaitoh extern const struct disk_partitioning_scheme disklabel_parts;
    117  1.1.2.1  msaitoh #if RAW_PART != 2
    118  1.1.2.1  msaitoh static struct disk_partitioning_scheme only_disklabel_parts;
    119  1.1.2.1  msaitoh 
    120  1.1.2.1  msaitoh /*
    121  1.1.2.1  msaitoh  * If not overriden by MD code, we can not boot from plain
    122  1.1.2.1  msaitoh  * disklabel disks (w/o MBR).
    123  1.1.2.1  msaitoh  */
    124  1.1.2.1  msaitoh static bool have_only_disklabel_boot_support(const char *disk)
    125  1.1.2.1  msaitoh {
    126  1.1.2.1  msaitoh #ifdef HAVE_PLAIN_DISKLABEL_BOOT
    127  1.1.2.1  msaitoh 	return HAVE_PLAIN_DISKLABEL_BOOT(disk);
    128  1.1.2.1  msaitoh #else
    129  1.1.2.1  msaitoh 	return false;
    130  1.1.2.1  msaitoh #endif
    131  1.1.2.1  msaitoh }
    132      1.1   martin #endif
    133      1.1   martin 
    134      1.1   martin /*
    135      1.1   martin  * One time initialization
    136      1.1   martin  */
    137      1.1   martin void
    138      1.1   martin partitions_init(void)
    139      1.1   martin {
    140      1.1   martin 	/*
    141      1.1   martin 	 * List of partitioning schemes.
    142      1.1   martin 	 * Order is important, the selection menu is created from start
    143      1.1   martin 	 * to end. Keep good defaults early. Most architectures will
    144      1.1   martin 	 * only offer very few entries.
    145      1.1   martin 	 */
    146      1.1   martin static const struct part_scheme_desc all_descs[] = {
    147      1.1   martin #if RAW_PART == 2	/* only available as primary on some architectures */
    148      1.1   martin 		{ NULL, &disklabel_parts },
    149      1.1   martin #endif
    150      1.1   martin #ifdef HAVE_GPT
    151      1.1   martin 		{ gpt_parts_check, &gpt_parts },
    152      1.1   martin #endif
    153      1.1   martin #ifdef HAVE_MBR
    154      1.1   martin 		{ NULL, &mbr_parts },
    155      1.1   martin #endif
    156  1.1.2.1  msaitoh #if RAW_PART != 2	/* "whole disk NetBSD" disklabel variant */
    157  1.1.2.1  msaitoh 		{ NULL, &only_disklabel_parts },
    158  1.1.2.1  msaitoh #endif
    159      1.1   martin 	};
    160      1.1   martin 
    161      1.1   martin 	size_t i, avail;
    162      1.1   martin 	const struct disk_partitioning_scheme **out;
    163      1.1   martin 	bool *is_available;
    164      1.1   martin 	static const size_t all_cnt = __arraycount(all_descs);
    165      1.1   martin 
    166      1.1   martin 	check_available_binaries();
    167      1.1   martin 
    168  1.1.2.1  msaitoh #if RAW_PART != 2
    169  1.1.2.1  msaitoh 	/* generate a variant of disklabel w/o parent scheme */
    170  1.1.2.1  msaitoh 	only_disklabel_parts = disklabel_parts;
    171  1.1.2.1  msaitoh 	only_disklabel_parts.name = MSG_parttype_only_disklabel;
    172  1.1.2.1  msaitoh 	only_disklabel_parts.have_boot_support =
    173  1.1.2.1  msaitoh 	    have_only_disklabel_boot_support;
    174  1.1.2.1  msaitoh #endif
    175  1.1.2.1  msaitoh 
    176  1.1.2.1  msaitoh 
    177      1.1   martin 	is_available = malloc(all_cnt);
    178      1.1   martin 
    179      1.1   martin 	for (avail = i = 0; i < all_cnt; i++) {
    180      1.1   martin 		is_available[i] = all_descs[i].is_available == NULL
    181      1.1   martin 				|| all_descs[i].is_available();
    182      1.1   martin 		if (is_available[i])
    183      1.1   martin 			avail++;
    184      1.1   martin 	}
    185      1.1   martin 
    186      1.1   martin 	if (avail == 0)
    187      1.1   martin 		return;
    188      1.1   martin 
    189      1.1   martin 	num_available_part_schemes = avail;
    190      1.1   martin 	available_part_schemes = malloc(sizeof(*available_part_schemes)
    191      1.1   martin 	    * (avail+1));
    192      1.1   martin 	if (available_part_schemes == NULL)
    193      1.1   martin 		return;
    194      1.1   martin 
    195      1.1   martin 	for (out = available_part_schemes, i = 0; i < all_cnt; i++) {
    196      1.1   martin 		if (!is_available[i])
    197      1.1   martin 			continue;
    198      1.1   martin 		*out++ = all_descs[i].ps;
    199      1.1   martin 	}
    200      1.1   martin 	*out = NULL;
    201      1.1   martin 
    202      1.1   martin 	free(is_available);
    203      1.1   martin }
    204  1.1.2.3  msaitoh 
    205  1.1.2.3  msaitoh /*
    206  1.1.2.3  msaitoh  * Final cleanup
    207  1.1.2.3  msaitoh  */
    208  1.1.2.3  msaitoh void
    209  1.1.2.3  msaitoh partitions_cleanup(void)
    210  1.1.2.3  msaitoh {
    211  1.1.2.3  msaitoh 	for (size_t i = 0; i < num_available_part_schemes; i++)
    212  1.1.2.3  msaitoh 		if (available_part_schemes[i]->cleanup != NULL)
    213  1.1.2.3  msaitoh 			available_part_schemes[i]->cleanup();
    214  1.1.2.3  msaitoh 	free(available_part_schemes);
    215  1.1.2.3  msaitoh }
    216