label.h (2956B)
1 /* see LICENSE file for copyright and license details */ 2 #ifndef ARUU_LIBDISK_LABEL_H 3 #define ARUU_LIBDISK_LABEL_H 4 5 /* a disk's partition table as a list of partitions: read it, change it in 6 * memory, write it. nothing is written to the disk until label_write, so a 7 * program can offer to undo every change. GPT and MBR (four primary 8 * partitions); the byte layout of each is gpt.c's and mbr in label.c's. 9 * fdisk edits the tables directly, a graphical partitioner or an installer 10 * goes through this */ 11 12 #include "../diskutil.h" 13 14 #include <stdint.h> 15 16 #define LABEL_SLOTS 128 17 #define LABEL_ALIGN 2048 /* sectors: partitions start on 1 MiB at 512 byte sectors */ 18 19 enum label_kind { LABEL_NONE, LABEL_GPT, LABEL_MBR }; 20 21 struct label_part { 22 int used; 23 uint64_t start, end; /* sectors, the last one included */ 24 char type[8]; /* gdisk's four hex digits, "" when unknown */ 25 uint8_t type_guid[16]; 26 uint8_t guid[16]; /* the partition's own, GPT */ 27 uint8_t mbr_type; 28 char name[40]; 29 int active; /* MBR's boot flag */ 30 }; 31 32 struct label { 33 enum label_kind kind; 34 uint64_t sectors; /* the disk */ 35 size_t sec_size; 36 uint64_t first, last; /* usable */ 37 int slots; /* 128 for GPT, 4 for MBR */ 38 struct label_part part[LABEL_SLOTS]; 39 /* what a rewrite keeps of the table that was there */ 40 uint8_t disk_guid[16]; 41 uint8_t mbr_code[446]; 42 uint32_t mbr_id; 43 }; 44 45 /* the table on dev: kind LABEL_NONE when it has none. -1 when it cannot be read */ 46 int label_read(struct BlockDev *dev, struct label *l); 47 /* an empty table of kind for a disk of dev's size, in memory only. -1 for a disk too small */ 48 int label_init(struct label *l, const struct BlockDev *dev, enum label_kind kind); 49 /* a partition of sectors (0: all of the biggest gap) at start (0: the first gap it fits), of type 50 * (a code of label_types) and name. its slot, a negative errno: -ENOSPC no gap, -EINVAL a start 51 * that overlaps, -EEXIST all slots used */ 52 int 53 label_add(struct label *l, uint64_t start, uint64_t sectors, const char *type, const char *name); 54 int label_delete(struct label *l, int slot); 55 int label_set_type(struct label *l, int slot, const char *type); 56 int label_set_name(struct label *l, int slot, const char *name); 57 58 /* the free stretches between partitions, aligned and at least LABEL_ALIGN 59 * sectors long: how many, at most max */ 60 struct label_gap { 61 uint64_t start, end; 62 }; 63 int label_gaps(const struct label *l, struct label_gap *out, int max); 64 65 /* the table written to dev, backup copy and protective MBR included. 0, or -1 */ 66 int label_write(struct BlockDev *dev, const struct label *l); 67 68 /* the partition types, for a list to choose from and to name one */ 69 struct label_type { 70 const char *code, *name; 71 int gpt_only; /* MBR has no byte for it */ 72 }; 73 int label_types(const struct label_type **out); 74 const char *label_type_name(const struct label_part *p); 75 76 #endif