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