tools: otrx: allow own magic
[project/firmware-utils.git] / src / ptgen.c
index d50c12b71432bd3ce9282af2f77ac1d1b4e7e919..d911b9d1f01ee90084e5120a8d4e33d27245d1ba 100644 (file)
@@ -94,6 +94,7 @@ typedef struct {
 #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)
@@ -120,9 +121,11 @@ 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;
@@ -369,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),
@@ -382,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) {
@@ -394,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",
@@ -405,16 +410,25 @@ 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;
                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));
 
@@ -433,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));
@@ -464,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;
        }
@@ -551,13 +567,13 @@ int main (int argc, char **argv)
        int ch;
        int part = 0;
        char *name = NULL;
-       unsigned short int required = 0;
+       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:vnN:gl:rS:G:")) != -1) {
+       while ((ch = getopt(argc, argv, "h:s:p:a:t:o:vnHN:gl:rS:G:")) != -1) {
                switch (ch) {
                case 'o':
                        filename = optarg;
@@ -571,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;
@@ -591,15 +610,17 @@ int main (int argc, char **argv)
                        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' and 'required'
+                        * reset 'name','required' and 'hybrid'
                         * 'type' is deliberately inherited from the previous delcaration
                         */
                        name = NULL;
                        required = 0;
+                       hybrid = 0;
                        break;
                case 'N':
                        name = optarg;
@@ -609,7 +630,6 @@ int main (int argc, char **argv)
                        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);
@@ -634,11 +654,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;
 }