From 9e05beb9e57d90ac87bfdb883e7143a381745f96 Mon Sep 17 00:00:00 2001 From: AmadeusGhost <42570690+AmadeusGhost@users.noreply.github.com> Date: Wed, 3 Mar 2021 12:08:03 +0800 Subject: [PATCH] firmware-utils/ptgen: sync upstream source code --- tools/firmware-utils/src/ptgen.c | 142 +++++++++++++++++++++++++------ 1 file changed, 114 insertions(+), 28 deletions(-) diff --git a/tools/firmware-utils/src/ptgen.c b/tools/firmware-utils/src/ptgen.c index 223ee29561..1eed21393d 100644 --- a/tools/firmware-utils/src/ptgen.c +++ b/tools/firmware-utils/src/ptgen.c @@ -52,6 +52,8 @@ #define swap(a, b) \ do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0) +#define BIT(_x) (1UL << (_x)) + typedef struct { uint8_t b[16]; } guid_t; @@ -80,10 +82,23 @@ typedef struct { GUID_INIT( 0x21686148, 0x6449, 0x6E6F, \ 0x74, 0x4E, 0x65, 0x65, 0x64, 0x45, 0x46, 0x49) +#define GUID_PARTITION_LINUX_FIT_GUID \ + GUID_INIT( 0xcae9be83, 0xb15f, 0x49cc, \ + 0x86, 0x3f, 0x08, 0x1b, 0x74, 0x4a, 0x2d, 0x93) + +#define GUID_PARTITION_LINUX_FS_GUID \ + GUID_INIT( 0x0fc63daf, 0x8483, 0x4772, \ + 0x8e, 0x79, 0x3d, 0x69, 0xd8, 0x47, 0x7d, 0xe4) + #define GPT_HEADER_SIZE 92 #define GPT_ENTRY_SIZE 128 #define GPT_ENTRY_MAX 128 #define GPT_ENTRY_NAME_SIZE 72 +#define GPT_SIZE GPT_ENTRY_SIZE * GPT_ENTRY_MAX / DISK_SECTOR_SIZE + +#define GPT_ATTR_PLAT_REQUIRED BIT(0) +#define GPT_ATTR_EFI_IGNORE BIT(1) +#define GPT_ATTR_LEGACY_BOOT BIT(2) #define GPT_HEADER_SECTOR 1 #define GPT_FIRST_ENTRY_SECTOR 2 @@ -106,9 +121,14 @@ struct pte { }; struct partinfo { + unsigned long actual_start; unsigned long start; unsigned long size; int type; + int hybrid; + char *name; + short int required; + guid_t guid; }; /* GPT Partition table header */ @@ -352,7 +372,7 @@ fail: /* check the partition sizes and write the guid partition table */ static int gen_gptable(uint32_t signature, guid_t guid, unsigned nr) { - struct pte pte; + struct pte pte[MBR_ENTRY_MAX]; struct gpth gpth = { .signature = cpu_to_le64(GPT_SIGNATURE), .revision = cpu_to_le32(GPT_REVISION), @@ -365,10 +385,12 @@ static int gen_gptable(uint32_t signature, guid_t guid, unsigned nr) .entry_size = cpu_to_le32(GPT_ENTRY_SIZE), }; struct gpte gpte[GPT_ENTRY_MAX]; - uint64_t start, end, sect = 0; + uint64_t start, end; + uint64_t sect = GPT_SIZE + GPT_FIRST_ENTRY_SECTOR; int fd, ret = -1; - unsigned i; + unsigned i, pmbr = 1; + memset(pte, 0, sizeof(struct pte) * MBR_ENTRY_MAX); memset(gpte, 0, GPT_ENTRY_SIZE * GPT_ENTRY_MAX); for (i = 0; i < nr; i++) { if (!parts[i].size) { @@ -377,7 +399,7 @@ static int gen_gptable(uint32_t signature, guid_t guid, unsigned nr) fprintf(stderr, "Invalid size in partition %d!\n", i); return ret; } - start = sect + sectors; + start = sect; if (parts[i].start != 0) { if (parts[i].start * 2 < start) { fprintf(stderr, "Invalid start %ld for partition %d!\n", @@ -388,21 +410,34 @@ static int gen_gptable(uint32_t signature, guid_t guid, unsigned nr) } else if (kb_align != 0) { start = round_to_kb(start); } + parts[i].actual_start = start; gpte[i].start = cpu_to_le64(start); sect = start + parts[i].size * 2; - if (kb_align == 0) - sect = round_to_cyl(sect); gpte[i].end = cpu_to_le64(sect -1); gpte[i].guid = guid; gpte[i].guid.b[sizeof(guid_t) -1] += i + 1; - if (parts[i].type == 0xEF || (i + 1) == (unsigned)active) { - gpte[i].type = GUID_PARTITION_SYSTEM; - init_utf16("EFI System Partition", (uint16_t *)gpte[i].name, GPT_ENTRY_NAME_SIZE / sizeof(uint16_t)); - } else { - gpte[i].type = GUID_PARTITION_BASIC_DATA; + gpte[i].type = parts[i].guid; + + if (parts[i].hybrid && pmbr < MBR_ENTRY_MAX) { + pte[pmbr].active = ((i + 1) == active) ? 0x80 : 0; + pte[pmbr].type = parts[i].type; + pte[pmbr].start = cpu_to_le32(start); + pte[pmbr].length = cpu_to_le32(sect - start); + to_chs(start, pte[1].chs_start); + to_chs(sect - 1, pte[1].chs_end); + pmbr++; } + if (parts[i].name) + init_utf16(parts[i].name, (uint16_t *)gpte[i].name, GPT_ENTRY_NAME_SIZE / sizeof(uint16_t)); + + if ((i + 1) == (unsigned)active) + gpte[i].attr |= GPT_ATTR_LEGACY_BOOT; + + if (parts[i].required) + gpte[i].attr |= GPT_ATTR_PLAT_REQUIRED; + if (verbose) fprintf(stderr, "Partition %d: start=%" PRIu64 ", end=%" PRIu64 ", size=%" PRIu64 "\n", i, @@ -412,21 +447,23 @@ static int gen_gptable(uint32_t signature, guid_t guid, unsigned nr) printf("%" PRIu64 "\n", (sect - start) * DISK_SECTOR_SIZE); } - gpte[GPT_ENTRY_MAX - 1].start = cpu_to_le64(GPT_FIRST_ENTRY_SECTOR + GPT_ENTRY_SIZE * GPT_ENTRY_MAX / DISK_SECTOR_SIZE); - gpte[GPT_ENTRY_MAX - 1].end = cpu_to_le64((kb_align ? round_to_kb(sectors) : (unsigned long)sectors) - 1); - gpte[GPT_ENTRY_MAX - 1].type = GUID_PARTITION_BIOS_BOOT; - gpte[GPT_ENTRY_MAX - 1].guid = guid; - gpte[GPT_ENTRY_MAX - 1].guid.b[sizeof(guid_t) -1] += GPT_ENTRY_MAX; + if (parts[0].actual_start > GPT_FIRST_ENTRY_SECTOR + GPT_SIZE) { + gpte[GPT_ENTRY_MAX - 1].start = cpu_to_le64(GPT_FIRST_ENTRY_SECTOR + GPT_SIZE); + gpte[GPT_ENTRY_MAX - 1].end = cpu_to_le64(parts[0].actual_start - 1); + gpte[GPT_ENTRY_MAX - 1].type = GUID_PARTITION_BIOS_BOOT; + gpte[GPT_ENTRY_MAX - 1].guid = guid; + gpte[GPT_ENTRY_MAX - 1].guid.b[sizeof(guid_t) -1] += GPT_ENTRY_MAX; + } - end = sect + sectors - 1; + end = sect + GPT_SIZE; - pte.type = 0xEE; - pte.start = cpu_to_le32(GPT_HEADER_SECTOR); - pte.length = cpu_to_le32(end); - to_chs(GPT_HEADER_SECTOR, pte.chs_start); - to_chs(end, pte.chs_end); + pte[0].type = 0xEE; + pte[0].start = cpu_to_le32(GPT_HEADER_SECTOR); + pte[0].length = cpu_to_le32(end - GPT_HEADER_SECTOR); + to_chs(GPT_HEADER_SECTOR, pte[0].chs_start); + to_chs(end, pte[0].chs_end); - gpth.last_usable = cpu_to_le64(end - GPT_ENTRY_SIZE * GPT_ENTRY_MAX / DISK_SECTOR_SIZE - 1); + gpth.last_usable = cpu_to_le64(end - GPT_SIZE - 1); gpth.alternate = cpu_to_le64(end); gpth.entry_crc32 = cpu_to_le32(gpt_crc32(gpte, GPT_ENTRY_SIZE * GPT_ENTRY_MAX)); gpth.crc32 = cpu_to_le32(gpt_crc32((char *)&gpth, GPT_HEADER_SIZE)); @@ -443,7 +480,7 @@ static int gen_gptable(uint32_t signature, guid_t guid, unsigned nr) } lseek(fd, MBR_PARTITION_ENTRY_OFFSET, SEEK_SET); - if (write(fd, &pte, sizeof(struct pte)) != sizeof(struct pte)) { + if (write(fd, pte, sizeof(struct pte) * MBR_ENTRY_MAX) != sizeof(struct pte) * MBR_ENTRY_MAX) { fputs("write failed.\n", stderr); goto fail; } @@ -498,21 +535,45 @@ fail: static void usage(char *prog) { - fprintf(stderr, "Usage: %s [-v] [-n] [-g] -h -s -o [-a 0..4] [-l ] [-G ] [[-t ] -p [@]...] \n", prog); + fprintf(stderr, "Usage: %s [-v] [-n] [-g] -h -s -o [-a 0..4] [-l ] [-G ] [[-t ] [-r] [-N ] -p [@]...] \n", prog); exit(EXIT_FAILURE); } +static guid_t type_to_guid_and_name(unsigned char type, char **name) +{ + guid_t guid = GUID_PARTITION_BASIC_DATA; + + switch (type) { + case 0xef: + if(*name == NULL) + *name = "EFI System Partition"; + guid = GUID_PARTITION_SYSTEM; + break; + case 0x83: + guid = GUID_PARTITION_LINUX_FS_GUID; + break; + case 0x2e: + guid = GUID_PARTITION_LINUX_FIT_GUID; + break; + } + + return guid; +} + int main (int argc, char **argv) { unsigned char type = 0x83; char *p; int ch; int part = 0; + char *name = NULL; + unsigned short int hybrid = 0, required = 0; uint32_t signature = 0x5452574F; /* 'OWRT' */ guid_t guid = GUID_INIT( signature, 0x2211, 0x4433, \ 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0x00); + guid_t part_guid = GUID_PARTITION_BASIC_DATA; - while ((ch = getopt(argc, argv, "h:s:p:a:t:o:vngl:S:G:")) != -1) { + while ((ch = getopt(argc, argv, "h:s:p:a:t:o:vnHN:gl:rS:G:")) != -1) { switch (ch) { case 'o': filename = optarg; @@ -526,6 +587,9 @@ int main (int argc, char **argv) case 'g': use_guid_partition_table = 1; break; + case 'H': + hybrid = 1; + break; case 'h': heads = (int)strtoul(optarg, NULL, 0); break; @@ -542,12 +606,31 @@ int main (int argc, char **argv) *(p++) = 0; parts[part].start = to_kbytes(p); } + part_guid = type_to_guid_and_name(type, &name); parts[part].size = to_kbytes(optarg); + parts[part].required = required; + parts[part].name = name; + parts[part].hybrid = hybrid; + parts[part].guid = part_guid; fprintf(stderr, "part %ld %ld\n", parts[part].start, parts[part].size); parts[part++].type = type; + /* + * reset 'name','required' and 'hybrid' + * 'type' is deliberately inherited from the previous delcaration + */ + name = NULL; + required = 0; + hybrid = 0; + break; + case 'N': + name = optarg; + break; + case 'r': + required = 1; break; case 't': type = (char)strtoul(optarg, NULL, 16); + part_guid = type_to_guid_and_name(type, &name); break; case 'a': active = (int)strtoul(optarg, NULL, 0); @@ -572,11 +655,14 @@ int main (int argc, char **argv) } } argc -= optind; - if (argc || (heads <= 0) || (sectors <= 0) || !filename) + if (argc || (!use_guid_partition_table && ((heads <= 0) || (sectors <= 0))) || !filename) usage(argv[0]); - if (use_guid_partition_table) + if (use_guid_partition_table) { + heads = 254; + sectors = 63; return gen_gptable(signature, guid, part) ? EXIT_FAILURE : EXIT_SUCCESS; + } return gen_ptable(signature, part) ? EXIT_FAILURE : EXIT_SUCCESS; }