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