diff options
author | Peter H. Froehlich <peter.hans.froehlich@gmail.com> | 2021-04-25 11:24:47 +0200 |
---|---|---|
committer | Peter H. Froehlich <peter.hans.froehlich@gmail.com> | 2021-04-25 11:24:47 +0200 |
commit | d817b0bfa285f3aff0f0a8a3b7d6a7fc1ab83590 (patch) | |
tree | 7fb22db2af3892d598aadd945390bb3ffed4e7a2 | |
parent | 9e51cfbfd2ab013cb5e8474e74a499827f73318f (diff) | |
download | mkgpt-d817b0bfa285f3aff0f0a8a3b7d6a7fc1ab83590.tar.gz |
Just a bit of cleanup.
-rw-r--r-- | README.md | 14 | ||||
-rw-r--r-- | part_ids.c | 83 |
2 files changed, 56 insertions, 41 deletions
diff --git a/README.md b/README.md index 8f8eabb..8bd3b71 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ if you have a full musl toolchain) followed by `make install` instead. - `--minimum-image-size <size>` minimum size of the image in sectors (defaults to 2048) - `--disk-guid <guid>` - GUID of the entire disk (see GUID format below - defaults to random) + GUID of the entire disk (see GUID format below, defaults to random) - `--part <file> <options>` begin a partition entry containing the specified image as its data and options as below @@ -31,19 +31,19 @@ if you have a full musl toolchain) followed by `make install` instead. - `--name <name>` set the name of the entry in the GPT - `--type <type>` - set the type of the entry, either a GUID, MBR partition ID _in decimal_ or - one of the known paritition types + set the type of the entry, either a GUID, numeric MBR-style partition ID, or + one of the known partition types - `--uuid <guid>` - specify the UUID of the partition in the GPT (defaults to a random uuid) + specify the UUID of the partition in the GPT (defaults to a random UUID) ### Known partition types -- `system` EFI sytem partition -- `bios` BIOS boot partition +- EFI system partition: `system` +- BIOS boot partition: `bios` - FAT types: `fat12`, `fat16`, `fat16b`, `fat32`, `fat16x`, `fat32x`, `fat16+`, `fat32+` - NTFS types: `ntfs` -- Linux types: `linux` +- Linux types: `linux`, `swap` ### GUID format diff --git a/part_ids.c b/part_ids.c index d678598..84f4189 100644 --- a/part_ids.c +++ b/part_ids.c @@ -1,3 +1,5 @@ +/* SPDX-License-Identifier: MIT */ + #include "part_ids.h" #include <ctype.h> @@ -5,32 +7,27 @@ #include <stdlib.h> #include <errno.h> -#define MBR_SWAP 0x82 -#define MBR_LINUX 0x83 -#define EFI_SYSTEM 0x100 -#define BIOS_BOOT 0x101 - #define GUID_TABLE \ - X(SWAP, "0657FD6D-A4AB-43C4-84E5-0933C84B4F4F") \ - X(LINUX, "0FC63DAF-8483-4772-8E79-3D69D8477DE4") \ - X(SYSTEM, "C12A7328-F81F-11D2-BA4B-00A0C93EC93B") \ - X(BDP, "EBD0A0A2-B9E5-4433-87C0-68B6B72699C7") \ - X(BIOS, "21686148-6449-6E6F-744E-656564454649") + X(LINUX_SWAP, "0657FD6D-A4AB-43C4-84E5-0933C84B4F4F") \ + X(LINUX_FS, "0FC63DAF-8483-4772-8E79-3D69D8477DE4") \ + X(EFI_SYSTEM, "C12A7328-F81F-11D2-BA4B-00A0C93EC93B") \ + X(MS_BASIC_DATA, "EBD0A0A2-B9E5-4433-87C0-68B6B72699C7") \ + X(BIOS_BOOT, "21686148-6449-6E6F-744E-656564454649") #define ALIAS_TABLE \ - X("fat12", 0x01, BDP) \ - X("fat16", 0x04, BDP) \ - X("fat16b", 0x06, BDP) \ - X("ntfs", 0x07, BDP) \ - X("fat32", 0x0b, BDP) \ - X("fat32x", 0x0c, BDP) \ - X("fat16x", 0x0e, BDP) \ - X("fat16+", 0x28, BDP) \ - X("fat32+", 0x29, BDP) \ - X("swap", MBR_SWAP, SWAP) \ - X("linux", MBR_LINUX, LINUX) \ - X("system", EFI_SYSTEM, SYSTEM) \ - X("bios", BIOS_BOOT, BIOS) + X("fat12", 0x01, MS_BASIC_DATA) \ + X("fat16", 0x04, MS_BASIC_DATA) \ + X("fat16b", 0x06, MS_BASIC_DATA) \ + X("ntfs", 0x07, MS_BASIC_DATA) \ + X("fat32", 0x0b, MS_BASIC_DATA) \ + X("fat32x", 0x0c, MS_BASIC_DATA) \ + X("fat16x", 0x0e, MS_BASIC_DATA) \ + X("fat16+", 0x28, MS_BASIC_DATA) \ + X("fat32+", 0x29, MS_BASIC_DATA) \ + X("swap", 0x82, LINUX_SWAP) \ + X("linux", 0x83, LINUX_FS) \ + X("system", 0x100, EFI_SYSTEM) \ + X("bios", 0x101, BIOS_BOOT) /* First we generate compact indices for each GUID using an enum. */ enum GUID_INDEX { @@ -45,7 +42,7 @@ enum GUID_INDEX { * that there are no pointers involved and no NUL terminators at the "end" of * a GUID, so don't try to `printf` them directly! */ -static char guids[NUM_GUIDS][GUID_STRLEN] = { +static const char guids[NUM_GUIDS][GUID_STRLEN] = { #define X(index, guid) [GUID_ ## index] = guid, GUID_TABLE #undef X @@ -56,7 +53,7 @@ static char guids[NUM_GUIDS][GUID_STRLEN] = { * sorted, but unless we have thousands of names, a linear search should be * all we need. A `key` of `NULL` marks the end. */ -static struct {char *key; int value;} name_to_guid[] = { +static const struct {char *key; int value;} name_to_guid[] = { #define X(name, id, index) {name, GUID_ ## index}, ALIAS_TABLE #undef X @@ -66,7 +63,7 @@ static struct {char *key; int value;} name_to_guid[] = { /* * The same for the "short" MBR-style ids. A `key` of `-1` marks the end. */ -static struct {int key; int value;} mbr_to_guid[] = { +static const struct {int key; int value;} mbr_to_guid[] = { #define X(name, id, index) {id, GUID_ ## index}, ALIAS_TABLE #undef X @@ -106,6 +103,28 @@ valid_string_guid(const char str[GUID_STRLEN]) return 1; } +static int +find_by_name(const char *str, GUID *guid) +{ + for (int i = 0; name_to_guid[i].key != NULL; i++) { + if (!strcmp(str, name_to_guid[i].key)) { + return string_to_guid(guid, guids[name_to_guid[i].value]); + } + } + return -1; +} + +static int +find_by_id(const int id, GUID *guid) +{ + for (int i = 0; mbr_to_guid[i].key != -1; i++) { + if (mbr_to_guid[i].key == id) { + return string_to_guid(guid, guids[mbr_to_guid[i].value]); + } + } + return -1; +} + /* * Parse GUID/UUID notation used on the command line. * Returns 0 on success. @@ -119,10 +138,8 @@ parse_guid(const char *str, GUID *guid) } /* detect by name */ - for (int i = 0; name_to_guid[i].key != NULL; i++) { - if (!strcmp(str, name_to_guid[i].key)) { - return string_to_guid(guid, guids[name_to_guid[i].value]); - } + if (find_by_name(str, guid) == 0) { + return 0; } /* try and parse as guid */ @@ -136,10 +153,8 @@ parse_guid(const char *str, GUID *guid) if (errno != 0) { return -1; } - for (int i = 0; mbr_to_guid[i].key != -1; i++) { - if (mbr_to_guid[i].key == mbr_id) { - return string_to_guid(guid, guids[mbr_to_guid[i].value]); - } + if (find_by_id(mbr_id, guid) == 0) { + return 0; } return -1; |