Home | History | Annotate | Line # | Download | only in sysinst
partitions.h revision 1.15.4.2
      1  1.15.4.2  martin /*	$NetBSD: partitions.h,v 1.15.4.2 2020/04/13 08:06:00 martin Exp $	*/
      2  1.15.4.2  martin 
      3  1.15.4.2  martin /*
      4  1.15.4.2  martin  * Copyright 2018 The NetBSD Foundation, Inc.
      5  1.15.4.2  martin  * All rights reserved.
      6  1.15.4.2  martin  *
      7  1.15.4.2  martin  * Redistribution and use in source and binary forms, with or without
      8  1.15.4.2  martin  * modification, are permitted provided that the following conditions
      9  1.15.4.2  martin  * are met:
     10  1.15.4.2  martin  * 1. Redistributions of source code must retain the above copyright
     11  1.15.4.2  martin  *    notice, this list of conditions and the following disclaimer.
     12  1.15.4.2  martin  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.15.4.2  martin  *    notice, this list of conditions and the following disclaimer in the
     14  1.15.4.2  martin  *    documentation and/or other materials provided with the distribution.
     15  1.15.4.2  martin  *
     16  1.15.4.2  martin  * THIS SOFTWARE IS PROVIDED BY PIERMONT INFORMATION SYSTEMS INC. ``AS IS''
     17  1.15.4.2  martin  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  1.15.4.2  martin  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  1.15.4.2  martin  * ARE DISCLAIMED. IN NO EVENT SHALL PIERMONT INFORMATION SYSTEMS INC. BE
     20  1.15.4.2  martin  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  1.15.4.2  martin  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  1.15.4.2  martin  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  1.15.4.2  martin  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  1.15.4.2  martin  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  1.15.4.2  martin  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
     26  1.15.4.2  martin  * THE POSSIBILITY OF SUCH DAMAGE.
     27  1.15.4.2  martin  *
     28  1.15.4.2  martin  */
     29  1.15.4.2  martin 
     30  1.15.4.2  martin /*
     31  1.15.4.2  martin  * Abstract interface to access arbitrary disk partitioning schemes and
     32  1.15.4.2  martin  * keep Sysinst proper independent of the implementation / on-disk
     33  1.15.4.2  martin  * details.
     34  1.15.4.2  martin  *
     35  1.15.4.2  martin  * NOTE:
     36  1.15.4.2  martin  *  - all sector numbers, alignement and sizes are in units of the
     37  1.15.4.2  martin  *    disks physical sector size (not necessarily 512 bytes)!
     38  1.15.4.2  martin  *  - some interfaces pass the disks sector size (when it is easily
     39  1.15.4.2  martin  *    available at typical callers), but the backends can always
     40  1.15.4.2  martin  *    assume it to be equal to the real physical sector size. If
     41  1.15.4.2  martin  *    no value is passed, the backend can query the disk data
     42  1.15.4.2  martin  *    via get_disk_geom().
     43  1.15.4.2  martin  *  - single exception: disk_partitioning_scheme::size_limit is in 512
     44  1.15.4.2  martin  *    byte sectors (as it is not associated with a concrete disk)
     45  1.15.4.2  martin  */
     46  1.15.4.2  martin 
     47  1.15.4.2  martin #include <sys/types.h>
     48  1.15.4.2  martin #include <stdbool.h>
     49  1.15.4.2  martin #include "msg_defs.h"
     50  1.15.4.2  martin 
     51  1.15.4.2  martin /*
     52  1.15.4.2  martin  * Import all the file system types, as enum fs_type.
     53  1.15.4.2  martin  */
     54  1.15.4.2  martin #define FSTYPE_ENUMNAME	fs_type
     55  1.15.4.2  martin #define FSTYPENAMES
     56  1.15.4.2  martin #include <sys/disklabel.h>
     57  1.15.4.2  martin #undef FSTYPE_ENUMNAME
     58  1.15.4.2  martin 
     59  1.15.4.2  martin #ifndef	FS_TMPFS
     60  1.15.4.2  martin #define	FS_TMPFS	256	/* random value (outside uint8_t range) */
     61  1.15.4.2  martin #endif
     62  1.15.4.2  martin #ifndef	FS_MFS
     63  1.15.4.2  martin #define	FS_MFS		257	/* another random (out of range) value */
     64  1.15.4.2  martin #endif
     65  1.15.4.2  martin 
     66  1.15.4.2  martin #define	MAX_LABEL_LEN		128	/* max. length of a partition label */
     67  1.15.4.2  martin #define	MAX_SHORTCUT_LEN	8	/* max. length of a shortcut ("a:") */
     68  1.15.4.2  martin 
     69  1.15.4.2  martin /*
     70  1.15.4.2  martin  * A partition index / handle, identifies a singlepartition within
     71  1.15.4.2  martin  * a struct disk_partitions. This is just an iterator/index - whenever
     72  1.15.4.2  martin  * changes to the set of partitions are done, partitions may get a new
     73  1.15.4.2  martin  * part_id.
     74  1.15.4.2  martin  * We assume that partitioning schemes keep partitions sorted (with
     75  1.15.4.2  martin  * key = start address, some schemes will have overlapping partitions,
     76  1.15.4.2  martin  * like MBR extended partitions).
     77  1.15.4.2  martin  */
     78  1.15.4.2  martin typedef size_t part_id;
     79  1.15.4.2  martin 
     80  1.15.4.2  martin /*
     81  1.15.4.2  martin  * An invalid value for a partition index / handle
     82  1.15.4.2  martin  */
     83  1.15.4.2  martin #define	NO_PART		((part_id)~0U)
     84  1.15.4.2  martin 
     85  1.15.4.2  martin /*
     86  1.15.4.2  martin  * Intended usage for a partition
     87  1.15.4.2  martin  */
     88  1.15.4.2  martin enum part_type {
     89  1.15.4.2  martin 	PT_undef,		/* invalid value */
     90  1.15.4.2  martin 	PT_unknown,		/* anything we can not map to one of these */
     91  1.15.4.2  martin 	PT_root,		/* the NetBSD / partition (bootable) */
     92  1.15.4.2  martin 	PT_swap,		/* the NetBSD swap partition */
     93  1.15.4.2  martin 	PT_FAT,			/* boot partition (e.g. for u-boot) */
     94  1.15.4.2  martin 	PT_EFI_SYSTEM,		/* (U)EFI boot partition */
     95  1.15.4.2  martin };
     96  1.15.4.2  martin 
     97  1.15.4.2  martin /*
     98  1.15.4.2  martin  * A generic structure describing partition types for menu/user interface
     99  1.15.4.2  martin  * purposes. The internal details may be richer and the *pointer* value
    100  1.15.4.2  martin  * is the unique token - that is: the partitioning scheme will hand out
    101  1.15.4.2  martin  * pointers to internal data and recognize the exact partition type details
    102  1.15.4.2  martin  * by pointer comparision.
    103  1.15.4.2  martin  */
    104  1.15.4.2  martin struct part_type_desc {
    105  1.15.4.2  martin 	enum part_type generic_ptype;	/* what this maps to in generic terms */
    106  1.15.4.2  martin 	const char *short_desc;		/* short type description */
    107  1.15.4.2  martin 	const char *description;	/* full description */
    108  1.15.4.2  martin };
    109  1.15.4.2  martin 
    110  1.15.4.2  martin /* Bits for disk_part_info.flags: */
    111  1.15.4.2  martin #define	PTI_SEC_CONTAINER	1		/* this covers our secondary
    112  1.15.4.2  martin 						   partitions */
    113  1.15.4.2  martin #define	PTI_WHOLE_DISK		2		/* all of the NetBSD disk */
    114  1.15.4.2  martin #define	PTI_BOOT		4		/* required for booting */
    115  1.15.4.2  martin #define	PTI_PSCHEME_INTERNAL	8		/* no user partition, e.g.
    116  1.15.4.2  martin 						   MBRs extend partition */
    117  1.15.4.2  martin #define	PTI_RAW_PART		16		/* total disk */
    118  1.15.4.2  martin 
    119  1.15.4.2  martin /* A single partition */
    120  1.15.4.2  martin struct disk_part_info {
    121  1.15.4.2  martin 	daddr_t start, size;			/* start and size on disk */
    122  1.15.4.2  martin 	uint32_t flags;				/* active PTI_ flags */
    123  1.15.4.2  martin 	const struct part_type_desc *nat_type;	/* native partition type */
    124  1.15.4.2  martin 	/*
    125  1.15.4.2  martin 	 * The following will only be available
    126  1.15.4.2  martin 	 *  a) for a small subset of file system types
    127  1.15.4.2  martin 	 *  b) if the partition (in this state) has already been
    128  1.15.4.2  martin 	 *     used before
    129  1.15.4.2  martin 	 * It is OK to leave all these zeroed / NULL when setting
    130  1.15.4.2  martin 	 * partition data - or leave them at the last values a get operation
    131  1.15.4.2  martin 	 * returned. Backends can not rely on them to be valid.
    132  1.15.4.2  martin 	 */
    133  1.15.4.2  martin 	const char *last_mounted;		/* last mount point or NULL */
    134  1.15.4.2  martin 	unsigned int fs_type, fs_sub_type;	/* FS_* type of filesystem
    135  1.15.4.2  martin 						 * and for some FS a sub
    136  1.15.4.2  martin 						 * type (e.g. FFSv1 vs. FFSv2)
    137  1.15.4.2  martin 						 */
    138  1.15.4.2  martin };
    139  1.15.4.2  martin 
    140  1.15.4.2  martin /* An unused area that may be used for new partitions */
    141  1.15.4.2  martin struct disk_part_free_space {
    142  1.15.4.2  martin 	daddr_t start, size;
    143  1.15.4.2  martin };
    144  1.15.4.2  martin 
    145  1.15.4.2  martin /*
    146  1.15.4.2  martin  * Some partition schemes define additional data that needs to be edited.
    147  1.15.4.2  martin  * These attributes are described in this structure and referenced by
    148  1.15.4.2  martin  * their index into the fixed list of available attributes.
    149  1.15.4.2  martin  */
    150  1.15.4.2  martin enum custom_attr_type { pet_bool, pet_cardinal, pet_str };
    151  1.15.4.2  martin struct disk_part_custom_attribute {
    152  1.15.4.2  martin 	msg label;			/* Name, like "active partition" */
    153  1.15.4.2  martin 	enum custom_attr_type type;	/* bool, long, char* */
    154  1.15.4.2  martin 	size_t strlen;			/* maximum length if pet_str */
    155  1.15.4.2  martin };
    156  1.15.4.2  martin 
    157  1.15.4.2  martin /*
    158  1.15.4.2  martin  * When displaying a partition editor, we have standard colums, but
    159  1.15.4.2  martin  * partitioning schemes add custom columns to the table as well.
    160  1.15.4.2  martin  * There is a fixed number of columns and they are described by this
    161  1.15.4.2  martin  * structure:
    162  1.15.4.2  martin  */
    163  1.15.4.2  martin struct disk_part_edit_column_desc {
    164  1.15.4.2  martin 	msg title;
    165  1.15.4.2  martin 	unsigned int width;
    166  1.15.4.2  martin };
    167  1.15.4.2  martin 
    168  1.15.4.2  martin struct disk_partitions;	/* in-memory represenation of a set of partitions */
    169  1.15.4.2  martin 
    170  1.15.4.2  martin /*
    171  1.15.4.2  martin  * When querying partition "device" names, we may ask for:
    172  1.15.4.2  martin  */
    173  1.15.4.2  martin enum dev_name_usage {
    174  1.15.4.2  martin 	parent_device_only,	/* wd0 instead of wd0i, no path */
    175  1.15.4.2  martin 	logical_name,		/* NAME=my-root instead of dk7 */
    176  1.15.4.2  martin 	plain_name,		/* e.g. /dev/wd0i or /dev/dk7 */
    177  1.15.4.2  martin 	raw_dev_name,		/* e.g. /dev/rwd0i or /dev/rdk7 */
    178  1.15.4.2  martin };
    179  1.15.4.2  martin 
    180  1.15.4.2  martin /*
    181  1.15.4.2  martin  * A scheme how to store partitions on-disk, and methods to read/write
    182  1.15.4.2  martin  * them to/from our abstract internal presentation.
    183  1.15.4.2  martin  */
    184  1.15.4.2  martin struct disk_partitioning_scheme {
    185  1.15.4.2  martin 	/* name of the on-disk scheme, retrieved via msg_string */
    186  1.15.4.2  martin 	msg name, short_name;
    187  1.15.4.2  martin 
    188  1.15.4.2  martin 	/* prompt shown when creating custom partition types */
    189  1.15.4.2  martin 	msg new_type_prompt;
    190  1.15.4.2  martin 
    191  1.15.4.2  martin 	/* description of scheme specific partition flags */
    192  1.15.4.2  martin 	msg part_flag_desc;
    193  1.15.4.2  martin 
    194  1.15.4.2  martin 	/*
    195  1.15.4.2  martin 	 * size restrictions for this partitioning scheme (number
    196  1.15.4.2  martin 	 * of 512 byte sectors max)
    197  1.15.4.2  martin 	 */
    198  1.15.4.2  martin 	daddr_t size_limit;	/* 0 if not limited */
    199  1.15.4.2  martin 
    200  1.15.4.2  martin 	/*
    201  1.15.4.2  martin 	 * If this scheme allows sub-partitions (i.e. MBR -> disklabel),
    202  1.15.4.2  martin 	 * this is a pointer to the (potential/optional) secondary
    203  1.15.4.2  martin 	 * scheme. Depending on partitioning details it may not be
    204  1.15.4.2  martin 	 * used in the end.
    205  1.15.4.2  martin 	 * This link is only here for better help messages.
    206  1.15.4.2  martin 	 * See *secondary_partitions further below for actually accesing
    207  1.15.4.2  martin 	 * secondary partitions.
    208  1.15.4.2  martin 	 */
    209  1.15.4.2  martin 	const struct disk_partitioning_scheme *secondary_scheme;
    210  1.15.4.2  martin 
    211  1.15.4.2  martin 	/*
    212  1.15.4.2  martin 	 * Partition editor colum descriptions for whatever the scheme
    213  1.15.4.2  martin 	 * needs to display (see format_partition_table_str below).
    214  1.15.4.2  martin 	 */
    215  1.15.4.2  martin 	size_t edit_columns_count;
    216  1.15.4.2  martin 	const struct disk_part_edit_column_desc *edit_columns;
    217  1.15.4.2  martin 
    218  1.15.4.2  martin 	/*
    219  1.15.4.2  martin 	 * Custom attributes editable by the partitioning scheme (but of
    220  1.15.4.2  martin 	 * no particular meaning for sysinst)
    221  1.15.4.2  martin 	 */
    222  1.15.4.2  martin 	size_t custom_attribute_count;
    223  1.15.4.2  martin 	const struct disk_part_custom_attribute *custom_attributes;
    224  1.15.4.2  martin 
    225  1.15.4.2  martin 	/*
    226  1.15.4.2  martin 	 * Partition types supported by this scheme,
    227  1.15.4.2  martin 	 * first function gets the number, second queries single elements
    228  1.15.4.2  martin 	 */
    229  1.15.4.2  martin 	size_t (*get_part_types_count)(void);
    230  1.15.4.2  martin 	const struct part_type_desc * (*get_part_type)(size_t ndx);
    231  1.15.4.2  martin 	/*
    232  1.15.4.2  martin 	 * Get the prefered native representation for a generic partition type
    233  1.15.4.2  martin 	 */
    234  1.15.4.2  martin 	const struct part_type_desc * (*get_generic_part_type)(enum part_type);
    235  1.15.4.2  martin 	/*
    236  1.15.4.2  martin 	 * Get the prefered native partition type for a specific file system
    237  1.15.4.2  martin 	 * type (FS_*) and subtype (fs specific value)
    238  1.15.4.2  martin 	 */
    239  1.15.4.2  martin 	const struct part_type_desc * (*get_fs_part_type)(
    240  1.15.4.2  martin 	    enum part_type, unsigned, unsigned);
    241  1.15.4.2  martin 	/*
    242  1.15.4.2  martin 	 * Optional: inverse to above: given a part_type_desc, set default
    243  1.15.4.2  martin 	 * fstype and subtype.
    244  1.15.4.2  martin 	 */
    245  1.15.4.2  martin 	bool (*get_default_fstype)(const struct part_type_desc *,
    246  1.15.4.2  martin 	    unsigned *fstype, unsigned *fs_sub_type);
    247  1.15.4.2  martin 	/*
    248  1.15.4.2  martin 	 * Create a custom partition type. If the type already exists
    249  1.15.4.2  martin 	 * (or there is a collision), the old existing type will be
    250  1.15.4.2  martin 	 * returned and no new type created. This is not considered
    251  1.15.4.2  martin 	 * an error (to keep the user interface simple).
    252  1.15.4.2  martin 	 * On failure NULL is returned and (if passed != NULL)
    253  1.15.4.2  martin 	 * *err_msg is set to a message describing the error.
    254  1.15.4.2  martin 	 */
    255  1.15.4.2  martin 	const struct part_type_desc * (*create_custom_part_type)
    256  1.15.4.2  martin 	    (const char *custom, const char **err_msg);
    257  1.15.4.2  martin 	/*
    258  1.15.4.2  martin 	 * Return a usable internal partition type representation
    259  1.15.4.2  martin 	 * for types that are not otherwise mappable.
    260  1.15.4.2  martin 	 * This could be FS_OTHER for disklabel, or a randomly
    261  1.15.4.2  martin 	 * created type guid for GPT. This type may or may not be
    262  1.15.4.2  martin 	 * in the regular type list. If not, it needs to behave like a
    263  1.15.4.2  martin 	 * custom type.
    264  1.15.4.2  martin 	 */
    265  1.15.4.2  martin 	const struct part_type_desc * (*create_unknown_part_type)(void);
    266  1.15.4.2  martin 
    267  1.15.4.2  martin 	/*
    268  1.15.4.2  martin 	 * Global attributes
    269  1.15.4.2  martin 	 */
    270  1.15.4.2  martin 	/*
    271  1.15.4.2  martin 	 * Get partition alignment suggestion. The schemen may enforce
    272  1.15.4.2  martin 	 * additional/different alignment for some partitions.
    273  1.15.4.2  martin 	 */
    274  1.15.4.2  martin 	daddr_t (*get_part_alignment)(const struct disk_partitions*);
    275  1.15.4.2  martin 
    276  1.15.4.2  martin 	/*
    277  1.15.4.2  martin 	 * Methods to manipulate the in-memory abstract representation
    278  1.15.4.2  martin 	 */
    279  1.15.4.2  martin 
    280  1.15.4.2  martin 	/* Retrieve data about a single partition, identified by the part_id.
    281  1.15.4.2  martin 	 * Fill the disk_part_info structure
    282  1.15.4.2  martin 	 */
    283  1.15.4.2  martin 	bool (*get_part_info)(const struct disk_partitions*, part_id,
    284  1.15.4.2  martin 	    struct disk_part_info*);
    285  1.15.4.2  martin 
    286  1.15.4.2  martin 	/* Optional: fill a atribute string describing the given partition */
    287  1.15.4.2  martin 	bool (*get_part_attr_str)(const struct disk_partitions*, part_id,
    288  1.15.4.2  martin 	    char *str, size_t avail_space);
    289  1.15.4.2  martin 	/* Format a partition editor element for the "col" column in
    290  1.15.4.2  martin 	 * edit_columns. Used e.g. with MBR to set "active" flags.
    291  1.15.4.2  martin 	 */
    292  1.15.4.2  martin 	bool (*format_partition_table_str)(const struct disk_partitions*,
    293  1.15.4.2  martin 	    part_id, size_t col, char *outstr, size_t outspace);
    294  1.15.4.2  martin 
    295  1.15.4.2  martin 	/* is the type of this partition changable? */
    296  1.15.4.2  martin 	bool (*part_type_can_change)(const struct disk_partitions*,
    297  1.15.4.2  martin 	    part_id);
    298  1.15.4.2  martin 
    299  1.15.4.2  martin 	/* can we add further partitions? */
    300  1.15.4.2  martin 	bool (*can_add_partition)(const struct disk_partitions*);
    301  1.15.4.2  martin 
    302  1.15.4.2  martin 	/* is the custom attribut changable? */
    303  1.15.4.2  martin 	bool (*custom_attribute_writable)(const struct disk_partitions*,
    304  1.15.4.2  martin 	    part_id, size_t attr_no);
    305  1.15.4.2  martin 	/*
    306  1.15.4.2  martin 	 * Output formatting for custom attributes.
    307  1.15.4.2  martin 	 * If "info" is != NULL, use (where it makes sense)
    308  1.15.4.2  martin 	 * values from that structure, as if a call to set_part_info
    309  1.15.4.2  martin 	 * would have been done before this call.
    310  1.15.4.2  martin 	 */
    311  1.15.4.2  martin 	bool (*format_custom_attribute)(const struct disk_partitions*,
    312  1.15.4.2  martin 	    part_id, size_t attr_no, const struct disk_part_info *info,
    313  1.15.4.2  martin 	    char *out, size_t out_space);
    314  1.15.4.2  martin 	/* value setter functions for custom attributes */
    315  1.15.4.2  martin 	/* pet_bool: */
    316  1.15.4.2  martin 	bool (*custom_attribute_toggle)(struct disk_partitions*,
    317  1.15.4.2  martin 	    part_id, size_t attr_no);
    318  1.15.4.2  martin 	/* pet_cardinal: */
    319  1.15.4.2  martin 	bool (*custom_attribute_set_card)(struct disk_partitions*,
    320  1.15.4.2  martin 	    part_id, size_t attr_no, long new_val);
    321  1.15.4.2  martin 	/* pet_str or pet_cardinal: */
    322  1.15.4.2  martin 	bool (*custom_attribute_set_str)(struct disk_partitions*,
    323  1.15.4.2  martin 	    part_id, size_t attr_no, const char *new_val);
    324  1.15.4.2  martin 
    325  1.15.4.2  martin 	/*
    326  1.15.4.2  martin 	 * Optional: additional user information when showing the size
    327  1.15.4.2  martin 	 * editor (especially for existing unknown partitions)
    328  1.15.4.2  martin 	 */
    329  1.15.4.2  martin 	const char * (*other_partition_identifier)(const struct
    330  1.15.4.2  martin 	    disk_partitions*, part_id);
    331  1.15.4.2  martin 
    332  1.15.4.2  martin 
    333  1.15.4.2  martin 	/* Retrieve device and partition names, e.g. for checking
    334  1.15.4.2  martin 	 * against kern.root_device or invoking newfs.
    335  1.15.4.2  martin 	 * For disklabel partitions, "part" will be set to the partition
    336  1.15.4.2  martin 	 * index (a = 0, b = 1, ...), for others it will get set to -1.
    337  1.15.4.2  martin 	 * If dev_name_usage is parent_device_only, the device name will
    338  1.15.4.2  martin 	 * not include a partition letter - obviously this only makes a
    339  1.15.4.2  martin 	 * difference with disklabel partitions.
    340  1.15.4.2  martin 	 * If dev_name_usage is logical_name instead of a device name
    341  1.15.4.2  martin 	 * a given name may be returned in NAME= syntax.
    342  1.15.4.2  martin 	 * If with_path is true (and the returned value is a device
    343  1.15.4.2  martin 	 * node), include the /dev/ prefix in the result string
    344  1.15.4.2  martin 	 * (this is ignored when returning NAME= syntax for /etc/fstab).
    345  1.15.4.2  martin 	 * If life is true, the device must be made available under
    346  1.15.4.2  martin 	 * that name (only makes a difference for NAME=syntax if
    347  1.15.4.2  martin 	 * no wedge has been created yet,) - implied for all variants
    348  1.15.4.2  martin 	 * where dev_name_usage != logical_name.
    349  1.15.4.2  martin 	 */
    350  1.15.4.2  martin 	bool (*get_part_device)(const struct disk_partitions*,
    351  1.15.4.2  martin 	    part_id, char *devname, size_t max_devname_len, int *part,
    352  1.15.4.2  martin 	    enum dev_name_usage, bool with_path, bool life);
    353  1.15.4.2  martin 
    354  1.15.4.2  martin 	/*
    355  1.15.4.2  martin 	 * How big could we resize the given position (start of existing
    356  1.15.4.2  martin 	 * partition or free space)
    357  1.15.4.2  martin 	 */
    358  1.15.4.2  martin 	daddr_t (*max_free_space_at)(const struct disk_partitions*, daddr_t);
    359  1.15.4.2  martin 
    360  1.15.4.2  martin 	/*
    361  1.15.4.2  martin 	 * Provide a list of free spaces usable for further partitioning,
    362  1.15.4.2  martin 	 * assuming the given partition alignment.
    363  1.15.4.2  martin 	 * If start is > 0 no space with lower sector numbers will
    364  1.15.4.2  martin 	 * be found.
    365  1.15.4.2  martin 	 * If ignore is > 0, any partition starting at that sector will
    366  1.15.4.2  martin 	 * be considered "free", this is used e.g. when moving an existing
    367  1.15.4.2  martin 	 * partition around.
    368  1.15.4.2  martin 	 */
    369  1.15.4.2  martin 	size_t (*get_free_spaces)(const struct disk_partitions*,
    370  1.15.4.2  martin 	    struct disk_part_free_space *result, size_t max_num_result,
    371  1.15.4.2  martin 	    daddr_t min_space_size, daddr_t align, daddr_t start,
    372  1.15.4.2  martin 	    daddr_t ignore /* -1 */);
    373  1.15.4.2  martin 
    374  1.15.4.2  martin 	/*
    375  1.15.4.2  martin 	 * Translate a partition description from a foreign partitioning
    376  1.15.4.2  martin 	 * scheme as close as possible to what we can handle in add_partition.
    377  1.15.4.2  martin 	 * This mostly adjusts flags and partition type pointers (using
    378  1.15.4.2  martin 	 * more lose matching than add_partition would do).
    379  1.15.4.2  martin 	 */
    380  1.15.4.2  martin 	bool (*adapt_foreign_part_info)(
    381  1.15.4.2  martin 	    const struct disk_partitions *myself, struct disk_part_info *dest,
    382  1.15.4.2  martin 	    const struct disk_partitioning_scheme *src_scheme,
    383  1.15.4.2  martin 	    const struct disk_part_info *src);
    384  1.15.4.2  martin 
    385  1.15.4.2  martin 	/*
    386  1.15.4.2  martin 	 * Update data for an existing partition
    387  1.15.4.2  martin 	 */
    388  1.15.4.2  martin 	bool (*set_part_info)(struct disk_partitions*, part_id,
    389  1.15.4.2  martin 	    const struct disk_part_info*, const char **err_msg);
    390  1.15.4.2  martin 
    391  1.15.4.2  martin 	/* Add a new partition and return its part_id. */
    392  1.15.4.2  martin 	part_id (*add_partition)(struct disk_partitions*,
    393  1.15.4.2  martin 	    const struct disk_part_info*, const char **err_msg);
    394  1.15.4.2  martin 
    395  1.15.4.2  martin 	/*
    396  1.15.4.2  martin 	 * Optional: add a partition from an outer scheme, accept all
    397  1.15.4.2  martin 	 * details w/o verification as best as possible.
    398  1.15.4.2  martin 	 */
    399  1.15.4.2  martin 	part_id (*add_outer_partition)(struct disk_partitions*,
    400  1.15.4.2  martin 	    const struct disk_part_info*, const char **err_msg);
    401  1.15.4.2  martin 
    402  1.15.4.2  martin 	/* Delete all partitions */
    403  1.15.4.2  martin 	bool (*delete_all_partitions)(struct disk_partitions*);
    404  1.15.4.2  martin 
    405  1.15.4.2  martin 	/* Optional: delete any partitions inside the given range */
    406  1.15.4.2  martin 	bool (*delete_partitions_in_range)(struct disk_partitions*,
    407  1.15.4.2  martin 	    daddr_t start, daddr_t size);
    408  1.15.4.2  martin 
    409  1.15.4.2  martin 	/* Delete the specified partition */
    410  1.15.4.2  martin 	bool (*delete_partition)(struct disk_partitions*, part_id,
    411  1.15.4.2  martin 	    const char **err_msg);
    412  1.15.4.2  martin 
    413  1.15.4.2  martin 	/*
    414  1.15.4.2  martin 	 * Methods for the whole set of partitions
    415  1.15.4.2  martin 	 */
    416  1.15.4.2  martin 	/*
    417  1.15.4.2  martin 	 * If this scheme only creates a singly NetBSD partition, which
    418  1.15.4.2  martin 	 * then is sub-partitioned (usually by disklabel), this returns a
    419  1.15.4.2  martin 	 * pointer to the secondary partition set.
    420  1.15.4.2  martin 	 * Otherwise NULL is returned, e.g. when there is no
    421  1.15.4.2  martin 	 * NetBSD partition defined (so this might change over time).
    422  1.15.4.2  martin 	 * Schemes that NEVER use a secondary scheme set this
    423  1.15.4.2  martin 	 * function pointer to NULL.
    424  1.15.4.2  martin 	 *
    425  1.15.4.2  martin 	 * If force_empty = true, ignore all on-disk contents and just
    426  1.15.4.2  martin 	 * create a new disk_partitons structure for the secondary scheme
    427  1.15.4.2  martin 	 * (this is used after deleting all partitions and setting up
    428  1.15.4.2  martin 	 * things for "use whole disk").
    429  1.15.4.2  martin 	 *
    430  1.15.4.2  martin 	 * The returned pointer is always owned by the primary partitions,
    431  1.15.4.2  martin 	 * caller MUST never free it, but otherwise can manipulate it
    432  1.15.4.2  martin 	 * arbitrarily.
    433  1.15.4.2  martin 	 */
    434  1.15.4.2  martin 	struct disk_partitions *
    435  1.15.4.2  martin 	    (*secondary_partitions)(struct disk_partitions *, daddr_t start,
    436  1.15.4.2  martin 	        bool force_empty);
    437  1.15.4.2  martin 
    438  1.15.4.2  martin 	/*
    439  1.15.4.2  martin 	 * Write the whole set (in new_state) back to disk.
    440  1.15.4.2  martin 	 */
    441  1.15.4.2  martin 	bool (*write_to_disk)(struct disk_partitions *new_state);
    442  1.15.4.2  martin 
    443  1.15.4.2  martin 	/*
    444  1.15.4.2  martin 	 * Try to read partitions from a disk, return NULL if this is not
    445  1.15.4.2  martin 	 * the partitioning scheme in use on that device.
    446  1.15.4.2  martin 	 * Usually start and len are 0 (and ignored).
    447  1.15.4.2  martin 	 * If this is about a part of a disk (like only the NetBSD
    448  1.15.4.2  martin 	 * MBR partition, start and len are the valid part of the
    449  1.15.4.2  martin 	 * disk.
    450  1.15.4.2  martin 	 */
    451  1.15.4.2  martin 	struct disk_partitions * (*read_from_disk)(const char *,
    452  1.15.4.2  martin 	    daddr_t start, daddr_t len, size_t bytes_per_sec,
    453  1.15.4.2  martin 	    const struct disk_partitioning_scheme *);
    454  1.15.4.2  martin 
    455  1.15.4.2  martin 	/*
    456  1.15.4.2  martin 	 * Set up all internal data for a new disk.
    457  1.15.4.2  martin 	 */
    458  1.15.4.2  martin 	struct disk_partitions * (*create_new_for_disk)(const char *,
    459  1.15.4.2  martin 	    daddr_t start, daddr_t len, bool is_boot_drive,
    460  1.15.4.2  martin 	    struct disk_partitions *parent);
    461  1.15.4.2  martin 
    462  1.15.4.2  martin 	/*
    463  1.15.4.2  martin 	 * Optional: this scheme may be used to boot from the given disk
    464  1.15.4.2  martin 	 */
    465  1.15.4.2  martin 	bool (*have_boot_support)(const char *disk);
    466  1.15.4.2  martin 
    467  1.15.4.2  martin 	/*
    468  1.15.4.2  martin 	 * Optional: try to guess disk geometry from the partition information
    469  1.15.4.2  martin 	 */
    470  1.15.4.2  martin 	int (*guess_disk_geom)(struct disk_partitions *,
    471  1.15.4.2  martin 	    int *cyl, int *head, int *sec);
    472  1.15.4.2  martin 
    473  1.15.4.2  martin 	/*
    474  1.15.4.2  martin 	 * Return a "cylinder size" (in number of blocks) - whatever that
    475  1.15.4.2  martin 	 * means to a particular partitioning scheme.
    476  1.15.4.2  martin 	 */
    477  1.15.4.2  martin 	size_t (*get_cylinder_size)(const struct disk_partitions *);
    478  1.15.4.2  martin 
    479  1.15.4.2  martin 	/*
    480  1.15.4.2  martin 	 * Optional: change used geometry info and update internal state
    481  1.15.4.2  martin 	 */
    482  1.15.4.2  martin 	bool (*change_disk_geom)(struct disk_partitions *,
    483  1.15.4.2  martin 	    int cyl, int head, int sec);
    484  1.15.4.2  martin 
    485  1.15.4.2  martin 	/*
    486  1.15.4.2  martin 	 * Optional:
    487  1.15.4.2  martin 	 * Get or set a name for the whole disk (most partitioning
    488  1.15.4.2  martin 	 * schemes do not provide this). Used for disklabel "pack names",
    489  1.15.4.2  martin 	 * which then may be used for aut-discovery of wedges, so it
    490  1.15.4.2  martin 	 * makes sense for the user to edit them.
    491  1.15.4.2  martin 	 */
    492  1.15.4.2  martin 	bool (*get_disk_pack_name)(const struct disk_partitions *,
    493  1.15.4.2  martin 	    char *, size_t);
    494  1.15.4.2  martin 	bool (*set_disk_pack_name)(struct disk_partitions *, const char *);
    495  1.15.4.2  martin 
    496  1.15.4.2  martin 	/*
    497  1.15.4.2  martin 	 * Optional:
    498  1.15.4.2  martin 	 * Find a partition by name (as used in /etc/fstab NAME= entries)
    499  1.15.4.2  martin 	 */
    500  1.15.4.2  martin 	part_id (*find_by_name)(struct disk_partitions *, const char *name);
    501  1.15.4.2  martin 
    502  1.15.4.2  martin 	/*
    503  1.15.4.2  martin 	 * Optional:
    504  1.15.4.2  martin 	 * Try to guess install target partition from internal data,
    505  1.15.4.2  martin 	 * returns true if a safe match was found and sets start/size
    506  1.15.4.2  martin 	 * to the target partition.
    507  1.15.4.2  martin 	 */
    508  1.15.4.2  martin 	bool (*guess_install_target)(const struct disk_partitions *,
    509  1.15.4.2  martin 		daddr_t *start, daddr_t *size);
    510  1.15.4.2  martin 
    511  1.15.4.2  martin 	/*
    512  1.15.4.2  martin 	 * Optional: verify that the whole set of partitions would be bootable,
    513  1.15.4.2  martin 	 * fix up any issues (with user interaction) where needed.
    514  1.15.4.2  martin 	 * If "quiet" is true, fix up everything silently if possible
    515  1.15.4.2  martin 	 * and never return 1.
    516  1.15.4.2  martin 	 * Returns:
    517  1.15.4.2  martin 	 *  0: abort install
    518  1.15.4.2  martin 	 *  1: re-edit partitions
    519  1.15.4.2  martin 	 *  2: use anyway (continue)
    520  1.15.4.2  martin 	 */
    521  1.15.4.2  martin 	int (*post_edit_verify)(struct disk_partitions *, bool quiet);
    522  1.15.4.2  martin 
    523  1.15.4.2  martin 	/*
    524  1.15.4.2  martin 	 * Optional: called during updates, before mounting the target disk(s),
    525  1.15.4.2  martin 	 * before md_pre_update() is called. Can be used to fixup
    526  1.15.4.2  martin 	 * partition info for historic errors (e.g. i386 changing MBR
    527  1.15.4.2  martin 	 * partition type from 165 to 169), similar to post_edit_verify.
    528  1.15.4.2  martin 	 * Returns:
    529  1.15.4.2  martin 	 *   true if the partition info has changed (write back required)
    530  1.15.4.2  martin 	 *   false if nothing further needs to be done.
    531  1.15.4.2  martin 	 */
    532  1.15.4.2  martin 	bool (*pre_update_verify)(struct disk_partitions *);
    533  1.15.4.2  martin 
    534  1.15.4.2  martin 	/* Free all the data */
    535  1.15.4.2  martin 	void (*free)(struct disk_partitions*);
    536  1.15.4.2  martin 
    537  1.15.4.2  martin 	/* Scheme global cleanup */
    538  1.15.4.2  martin 	void (*cleanup)(void);
    539  1.15.4.2  martin };
    540  1.15.4.2  martin 
    541  1.15.4.2  martin /*
    542  1.15.4.2  martin  * The in-memory representation of all partitions on a concrete disk,
    543  1.15.4.2  martin  * tied to the partitioning scheme in use.
    544  1.15.4.2  martin  *
    545  1.15.4.2  martin  * Concrete schemes will derive from the abstract disk_partitions
    546  1.15.4.2  martin  * structure (by aggregation), but consumers of the API will only
    547  1.15.4.2  martin  * ever see this public part.
    548  1.15.4.2  martin  */
    549  1.15.4.2  martin struct disk_partitions {
    550  1.15.4.2  martin 	/* which partitioning scheme is in use */
    551  1.15.4.2  martin 	const struct disk_partitioning_scheme *pscheme;
    552  1.15.4.2  martin 
    553  1.15.4.2  martin 	/* the disk device this came from (or should go to) */
    554  1.15.4.2  martin 	const char *disk;
    555  1.15.4.2  martin 
    556  1.15.4.2  martin 	/* global/public disk data */
    557  1.15.4.2  martin 
    558  1.15.4.2  martin 	/*
    559  1.15.4.2  martin 	 * The basic unit of size used for this disk (all "start",
    560  1.15.4.2  martin 	 * "size" and "align" values are in this unit).
    561  1.15.4.2  martin 	 */
    562  1.15.4.2  martin 	size_t bytes_per_sector;	/* must be 2^n and >= 512 */
    563  1.15.4.2  martin 
    564  1.15.4.2  martin 	/*
    565  1.15.4.2  martin 	 * Valid partitions may have IDs in the range 0 .. num_part (excl.)
    566  1.15.4.2  martin 	 */
    567  1.15.4.2  martin 	part_id num_part;
    568  1.15.4.2  martin 
    569  1.15.4.2  martin 	/*
    570  1.15.4.2  martin 	 * If this is a sub-partitioning, the start of the "disk" is
    571  1.15.4.2  martin 	 * some arbitrary partition in the parent. Sometimes we need
    572  1.15.4.2  martin 	 * to be able to calculate absoluted offsets.
    573  1.15.4.2  martin 	 */
    574  1.15.4.2  martin 	daddr_t disk_start;
    575  1.15.4.2  martin 	/*
    576  1.15.4.2  martin 	 * Total size of the disk (usable for partitioning)
    577  1.15.4.2  martin 	 */
    578  1.15.4.2  martin 	daddr_t disk_size;
    579  1.15.4.2  martin 
    580  1.15.4.2  martin 	/*
    581  1.15.4.2  martin 	 * Space not yet allocated
    582  1.15.4.2  martin 	 */
    583  1.15.4.2  martin 	daddr_t free_space;
    584  1.15.4.2  martin 
    585  1.15.4.2  martin 	/*
    586  1.15.4.2  martin 	 * If this is the secondary partitioning scheme, pointer to
    587  1.15.4.2  martin 	 * the outer one. Otherwise NULL.
    588  1.15.4.2  martin 	 */
    589  1.15.4.2  martin 	struct disk_partitions *parent;
    590  1.15.4.2  martin };
    591  1.15.4.2  martin 
    592  1.15.4.2  martin /*
    593  1.15.4.2  martin  * A list of partitioning schemes, so we can iterate over everything
    594  1.15.4.2  martin  * supported (e.g. when partitioning a new disk). NULL terminated.
    595  1.15.4.2  martin  */
    596  1.15.4.2  martin extern const struct disk_partitioning_scheme **available_part_schemes;
    597  1.15.4.2  martin extern size_t num_available_part_schemes;
    598  1.15.4.2  martin 
    599  1.15.4.2  martin /*
    600  1.15.4.2  martin  * Generic reader - query a disk device and read all partitions from it
    601  1.15.4.2  martin  */
    602  1.15.4.2  martin struct disk_partitions *
    603  1.15.4.2  martin partitions_read_disk(const char *, daddr_t disk_size,
    604  1.15.4.2  martin     size_t bytes_per_sector, bool no_mbr);
    605  1.15.4.2  martin 
    606  1.15.4.2  martin /*
    607  1.15.4.2  martin  * Generic part info adaption, may be overriden by individual partitionin
    608  1.15.4.2  martin  * schemes
    609  1.15.4.2  martin  */
    610  1.15.4.2  martin bool generic_adapt_foreign_part_info(
    611  1.15.4.2  martin     const struct disk_partitions *myself, struct disk_part_info *dest,
    612  1.15.4.2  martin     const struct disk_partitioning_scheme *src_scheme,
    613  1.15.4.2  martin     const struct disk_part_info *src);
    614  1.15.4.2  martin 
    615  1.15.4.2  martin /*
    616  1.15.4.2  martin  * One time initialization and clenaup
    617  1.15.4.2  martin  */
    618  1.15.4.2  martin void partitions_init(void);
    619  1.15.4.2  martin void partitions_cleanup(void);
    620  1.15.4.2  martin 
    621