ptgen.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * ptgen - partition table generator
  4. * Copyright (C) 2006 by Felix Fietkau <[email protected]>
  5. *
  6. * uses parts of afdisk
  7. * Copyright (C) 2002 by David Roetzel <[email protected]>
  8. *
  9. * UUID/GUID definition stolen from kernel/include/uapi/linux/uuid.h
  10. * Copyright (C) 2010, Intel Corp. Huang Ying <[email protected]>
  11. */
  12. #include <sys/types.h>
  13. #include <sys/stat.h>
  14. #include <string.h>
  15. #include <unistd.h>
  16. #include <stdlib.h>
  17. #include <stdio.h>
  18. #include <stdint.h>
  19. #include <stdbool.h>
  20. #include <ctype.h>
  21. #include <inttypes.h>
  22. #include <fcntl.h>
  23. #include <stdint.h>
  24. #include "cyg_crc.h"
  25. #if __BYTE_ORDER == __BIG_ENDIAN
  26. #define cpu_to_le16(x) bswap_16(x)
  27. #define cpu_to_le32(x) bswap_32(x)
  28. #define cpu_to_le64(x) bswap_64(x)
  29. #elif __BYTE_ORDER == __LITTLE_ENDIAN
  30. #define cpu_to_le16(x) (x)
  31. #define cpu_to_le32(x) (x)
  32. #define cpu_to_le64(x) (x)
  33. #else
  34. #error unknown endianness!
  35. #endif
  36. #define swap(a, b) \
  37. do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
  38. #define BIT(_x) (1UL << (_x))
  39. typedef struct {
  40. uint8_t b[16];
  41. } guid_t;
  42. #define GUID_INIT(a, b, c, d0, d1, d2, d3, d4, d5, d6, d7) \
  43. ((guid_t) \
  44. {{ (a) & 0xff, ((a) >> 8) & 0xff, ((a) >> 16) & 0xff, ((a) >> 24) & 0xff, \
  45. (b) & 0xff, ((b) >> 8) & 0xff, \
  46. (c) & 0xff, ((c) >> 8) & 0xff, \
  47. (d0), (d1), (d2), (d3), (d4), (d5), (d6), (d7) }})
  48. #define GUID_STRING_LENGTH 36
  49. #define GPT_SIGNATURE 0x5452415020494645ULL
  50. #define GPT_REVISION 0x00010000
  51. #define GUID_PARTITION_SYSTEM \
  52. GUID_INIT( 0xC12A7328, 0xF81F, 0x11d2, \
  53. 0xBA, 0x4B, 0x00, 0xA0, 0xC9, 0x3E, 0xC9, 0x3B)
  54. #define GUID_PARTITION_BASIC_DATA \
  55. GUID_INIT( 0xEBD0A0A2, 0xB9E5, 0x4433, \
  56. 0x87, 0xC0, 0x68, 0xB6, 0xB7, 0x26, 0x99, 0xC7)
  57. #define GUID_PARTITION_BIOS_BOOT \
  58. GUID_INIT( 0x21686148, 0x6449, 0x6E6F, \
  59. 0x74, 0x4E, 0x65, 0x65, 0x64, 0x45, 0x46, 0x49)
  60. #define GUID_PARTITION_LINUX_FIT_GUID \
  61. GUID_INIT( 0xcae9be83, 0xb15f, 0x49cc, \
  62. 0x86, 0x3f, 0x08, 0x1b, 0x74, 0x4a, 0x2d, 0x93)
  63. #define GUID_PARTITION_LINUX_FS_GUID \
  64. GUID_INIT( 0x0fc63daf, 0x8483, 0x4772, \
  65. 0x8e, 0x79, 0x3d, 0x69, 0xd8, 0x47, 0x7d, 0xe4)
  66. #define GPT_HEADER_SIZE 92
  67. #define GPT_ENTRY_SIZE 128
  68. #define GPT_ENTRY_MAX 128
  69. #define GPT_ENTRY_NAME_SIZE 72
  70. #define GPT_SIZE GPT_ENTRY_SIZE * GPT_ENTRY_MAX / DISK_SECTOR_SIZE
  71. #define GPT_ATTR_PLAT_REQUIRED BIT(0)
  72. #define GPT_ATTR_EFI_IGNORE BIT(1)
  73. #define GPT_ATTR_LEGACY_BOOT BIT(2)
  74. #define GPT_HEADER_SECTOR 1
  75. #define GPT_FIRST_ENTRY_SECTOR 2
  76. #define MBR_ENTRY_MAX 4
  77. #define MBR_DISK_SIGNATURE_OFFSET 440
  78. #define MBR_PARTITION_ENTRY_OFFSET 446
  79. #define MBR_BOOT_SIGNATURE_OFFSET 510
  80. #define DISK_SECTOR_SIZE 512
  81. /* Partition table entry */
  82. struct pte {
  83. uint8_t active;
  84. uint8_t chs_start[3];
  85. uint8_t type;
  86. uint8_t chs_end[3];
  87. uint32_t start;
  88. uint32_t length;
  89. };
  90. struct partinfo {
  91. unsigned long actual_start;
  92. unsigned long start;
  93. unsigned long size;
  94. int type;
  95. int hybrid;
  96. char *name;
  97. short int required;
  98. guid_t guid;
  99. };
  100. /* GPT Partition table header */
  101. struct gpth {
  102. uint64_t signature;
  103. uint32_t revision;
  104. uint32_t size;
  105. uint32_t crc32;
  106. uint32_t reserved;
  107. uint64_t self;
  108. uint64_t alternate;
  109. uint64_t first_usable;
  110. uint64_t last_usable;
  111. guid_t disk_guid;
  112. uint64_t first_entry;
  113. uint32_t entry_num;
  114. uint32_t entry_size;
  115. uint32_t entry_crc32;
  116. } __attribute__((packed));
  117. /* GPT Partition table entry */
  118. struct gpte {
  119. guid_t type;
  120. guid_t guid;
  121. uint64_t start;
  122. uint64_t end;
  123. uint64_t attr;
  124. char name[GPT_ENTRY_NAME_SIZE];
  125. } __attribute__((packed));
  126. int verbose = 0;
  127. int active = 1;
  128. int heads = -1;
  129. int sectors = -1;
  130. int kb_align = 0;
  131. bool ignore_null_sized_partition = false;
  132. bool use_guid_partition_table = false;
  133. struct partinfo parts[GPT_ENTRY_MAX];
  134. char *filename = NULL;
  135. /*
  136. * parse the size argument, which is either
  137. * a simple number (K assumed) or
  138. * K, M or G
  139. *
  140. * returns the size in KByte
  141. */
  142. static long to_kbytes(const char *string)
  143. {
  144. int exp = 0;
  145. long result;
  146. char *end;
  147. result = strtoul(string, &end, 0);
  148. switch (tolower(*end)) {
  149. case 'k' :
  150. case '\0' : exp = 0; break;
  151. case 'm' : exp = 1; break;
  152. case 'g' : exp = 2; break;
  153. default: return 0;
  154. }
  155. if (*end)
  156. end++;
  157. if (*end) {
  158. fputs("garbage after end of number\n", stderr);
  159. return 0;
  160. }
  161. /* result: number + 1024^(exp) */
  162. if (exp == 0)
  163. return result;
  164. return result * (2 << ((10 * exp) - 1));
  165. }
  166. /* convert the sector number into a CHS value for the partition table */
  167. static void to_chs(long sect, unsigned char chs[3])
  168. {
  169. int c,h,s;
  170. s = (sect % sectors) + 1;
  171. sect = sect / sectors;
  172. h = sect % heads;
  173. sect = sect / heads;
  174. c = sect;
  175. chs[0] = h;
  176. chs[1] = s | ((c >> 2) & 0xC0);
  177. chs[2] = c & 0xFF;
  178. return;
  179. }
  180. /* round the sector number up to the next cylinder */
  181. static inline unsigned long round_to_cyl(long sect)
  182. {
  183. int cyl_size = heads * sectors;
  184. return sect + cyl_size - (sect % cyl_size);
  185. }
  186. /* round the sector number up to the kb_align boundary */
  187. static inline unsigned long round_to_kb(long sect) {
  188. return ((sect - 1) / kb_align + 1) * kb_align;
  189. }
  190. /* Compute a CRC for guid partition table */
  191. static inline unsigned long gpt_crc32(void *buf, unsigned long len)
  192. {
  193. return cyg_crc32_accumulate(~0L, buf, len) ^ ~0L;
  194. }
  195. /* Parse a guid string to guid_t struct */
  196. static inline int guid_parse(char *buf, guid_t *guid)
  197. {
  198. char b[4] = {0};
  199. char *p = buf;
  200. unsigned i = 0;
  201. if (strnlen(buf, GUID_STRING_LENGTH) != GUID_STRING_LENGTH)
  202. return -1;
  203. for (i = 0; i < sizeof(guid_t); i++) {
  204. if (*p == '-')
  205. p++;
  206. if (*p == '\0')
  207. return -1;
  208. memcpy(b, p, 2);
  209. guid->b[i] = strtol(b, 0, 16);
  210. p += 2;
  211. }
  212. swap(guid->b[0], guid->b[3]);
  213. swap(guid->b[1], guid->b[2]);
  214. swap(guid->b[4], guid->b[5]);
  215. swap(guid->b[6], guid->b[7]);
  216. return 0;
  217. }
  218. /* init an utf-16 string from utf-8 string */
  219. static inline void init_utf16(char *str, uint16_t *buf, unsigned bufsize)
  220. {
  221. unsigned i, n = 0;
  222. for (i = 0; i < bufsize; i++) {
  223. if (str[n] == 0x00) {
  224. buf[i] = 0x00;
  225. return ;
  226. } else if ((str[n] & 0x80) == 0x00) {//0xxxxxxx
  227. buf[i] = cpu_to_le16(str[n++]);
  228. } else if ((str[n] & 0xE0) == 0xC0) {//110xxxxx
  229. buf[i] = cpu_to_le16((str[n] & 0x1F) << 6 | (str[n + 1] & 0x3F));
  230. n += 2;
  231. } else if ((str[n] & 0xF0) == 0xE0) {//1110xxxx
  232. buf[i] = cpu_to_le16((str[n] & 0x0F) << 12 | (str[n + 1] & 0x3F) << 6 | (str[n + 2] & 0x3F));
  233. n += 3;
  234. } else {
  235. buf[i] = cpu_to_le16('?');
  236. n++;
  237. }
  238. }
  239. }
  240. /* check the partition sizes and write the partition table */
  241. static int gen_ptable(uint32_t signature, int nr)
  242. {
  243. struct pte pte[MBR_ENTRY_MAX];
  244. unsigned long start, len, sect = 0;
  245. int i, fd, ret = -1;
  246. memset(pte, 0, sizeof(struct pte) * MBR_ENTRY_MAX);
  247. for (i = 0; i < nr; i++) {
  248. if (!parts[i].size) {
  249. if (ignore_null_sized_partition)
  250. continue;
  251. fprintf(stderr, "Invalid size in partition %d!\n", i);
  252. return ret;
  253. }
  254. pte[i].active = ((i + 1) == active) ? 0x80 : 0;
  255. pte[i].type = parts[i].type;
  256. start = sect + sectors;
  257. if (parts[i].start != 0) {
  258. if (parts[i].start * 2 < start) {
  259. fprintf(stderr, "Invalid start %ld for partition %d!\n",
  260. parts[i].start, i);
  261. return ret;
  262. }
  263. start = parts[i].start * 2;
  264. } else if (kb_align != 0) {
  265. start = round_to_kb(start);
  266. }
  267. pte[i].start = cpu_to_le32(start);
  268. sect = start + parts[i].size * 2;
  269. if (kb_align == 0)
  270. sect = round_to_cyl(sect);
  271. pte[i].length = cpu_to_le32(len = sect - start);
  272. to_chs(start, pte[i].chs_start);
  273. to_chs(start + len - 1, pte[i].chs_end);
  274. if (verbose)
  275. fprintf(stderr, "Partition %d: start=%ld, end=%ld, size=%ld\n",
  276. i,
  277. (long)start * DISK_SECTOR_SIZE,
  278. (long)(start + len) * DISK_SECTOR_SIZE,
  279. (long)len * DISK_SECTOR_SIZE);
  280. printf("%ld\n", (long)start * DISK_SECTOR_SIZE);
  281. printf("%ld\n", (long)len * DISK_SECTOR_SIZE);
  282. }
  283. if ((fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC, 0644)) < 0) {
  284. fprintf(stderr, "Can't open output file '%s'\n",filename);
  285. return ret;
  286. }
  287. lseek(fd, MBR_DISK_SIGNATURE_OFFSET, SEEK_SET);
  288. if (write(fd, &signature, sizeof(signature)) != sizeof(signature)) {
  289. fputs("write failed.\n", stderr);
  290. goto fail;
  291. }
  292. lseek(fd, MBR_PARTITION_ENTRY_OFFSET, SEEK_SET);
  293. if (write(fd, pte, sizeof(struct pte) * MBR_ENTRY_MAX) != sizeof(struct pte) * MBR_ENTRY_MAX) {
  294. fputs("write failed.\n", stderr);
  295. goto fail;
  296. }
  297. lseek(fd, MBR_BOOT_SIGNATURE_OFFSET, SEEK_SET);
  298. if (write(fd, "\x55\xaa", 2) != 2) {
  299. fputs("write failed.\n", stderr);
  300. goto fail;
  301. }
  302. ret = 0;
  303. fail:
  304. close(fd);
  305. return ret;
  306. }
  307. /* check the partition sizes and write the guid partition table */
  308. static int gen_gptable(uint32_t signature, guid_t guid, unsigned nr)
  309. {
  310. struct pte pte[MBR_ENTRY_MAX];
  311. struct gpth gpth = {
  312. .signature = cpu_to_le64(GPT_SIGNATURE),
  313. .revision = cpu_to_le32(GPT_REVISION),
  314. .size = cpu_to_le32(GPT_HEADER_SIZE),
  315. .self = cpu_to_le64(GPT_HEADER_SECTOR),
  316. .first_usable = cpu_to_le64(GPT_FIRST_ENTRY_SECTOR + GPT_ENTRY_SIZE * GPT_ENTRY_MAX / DISK_SECTOR_SIZE),
  317. .first_entry = cpu_to_le64(GPT_FIRST_ENTRY_SECTOR),
  318. .disk_guid = guid,
  319. .entry_num = cpu_to_le32(GPT_ENTRY_MAX),
  320. .entry_size = cpu_to_le32(GPT_ENTRY_SIZE),
  321. };
  322. struct gpte gpte[GPT_ENTRY_MAX];
  323. uint64_t start, end;
  324. uint64_t sect = GPT_SIZE + GPT_FIRST_ENTRY_SECTOR;
  325. int fd, ret = -1;
  326. unsigned i, pmbr = 1;
  327. memset(pte, 0, sizeof(struct pte) * MBR_ENTRY_MAX);
  328. memset(gpte, 0, GPT_ENTRY_SIZE * GPT_ENTRY_MAX);
  329. for (i = 0; i < nr; i++) {
  330. if (!parts[i].size) {
  331. if (ignore_null_sized_partition)
  332. continue;
  333. fprintf(stderr, "Invalid size in partition %d!\n", i);
  334. return ret;
  335. }
  336. start = sect;
  337. if (parts[i].start != 0) {
  338. if (parts[i].start * 2 < start) {
  339. fprintf(stderr, "Invalid start %ld for partition %d!\n",
  340. parts[i].start, i);
  341. return ret;
  342. }
  343. start = parts[i].start * 2;
  344. } else if (kb_align != 0) {
  345. start = round_to_kb(start);
  346. }
  347. parts[i].actual_start = start;
  348. gpte[i].start = cpu_to_le64(start);
  349. sect = start + parts[i].size * 2;
  350. gpte[i].end = cpu_to_le64(sect -1);
  351. gpte[i].guid = guid;
  352. gpte[i].guid.b[sizeof(guid_t) -1] += i + 1;
  353. gpte[i].type = parts[i].guid;
  354. if (parts[i].hybrid && pmbr < MBR_ENTRY_MAX) {
  355. pte[pmbr].active = ((i + 1) == active) ? 0x80 : 0;
  356. pte[pmbr].type = parts[i].type;
  357. pte[pmbr].start = cpu_to_le32(start);
  358. pte[pmbr].length = cpu_to_le32(sect - start);
  359. to_chs(start, pte[1].chs_start);
  360. to_chs(sect - 1, pte[1].chs_end);
  361. pmbr++;
  362. }
  363. if (parts[i].name)
  364. init_utf16(parts[i].name, (uint16_t *)gpte[i].name, GPT_ENTRY_NAME_SIZE / sizeof(uint16_t));
  365. if ((i + 1) == (unsigned)active)
  366. gpte[i].attr |= GPT_ATTR_LEGACY_BOOT;
  367. if (parts[i].required)
  368. gpte[i].attr |= GPT_ATTR_PLAT_REQUIRED;
  369. if (verbose)
  370. fprintf(stderr, "Partition %d: start=%" PRIu64 ", end=%" PRIu64 ", size=%" PRIu64 "\n",
  371. i,
  372. start * DISK_SECTOR_SIZE, sect * DISK_SECTOR_SIZE,
  373. (sect - start) * DISK_SECTOR_SIZE);
  374. printf("%" PRIu64 "\n", start * DISK_SECTOR_SIZE);
  375. printf("%" PRIu64 "\n", (sect - start) * DISK_SECTOR_SIZE);
  376. }
  377. if (parts[0].actual_start > GPT_FIRST_ENTRY_SECTOR + GPT_SIZE) {
  378. gpte[GPT_ENTRY_MAX - 1].start = cpu_to_le64(GPT_FIRST_ENTRY_SECTOR + GPT_SIZE);
  379. gpte[GPT_ENTRY_MAX - 1].end = cpu_to_le64(parts[0].actual_start - 1);
  380. gpte[GPT_ENTRY_MAX - 1].type = GUID_PARTITION_BIOS_BOOT;
  381. gpte[GPT_ENTRY_MAX - 1].guid = guid;
  382. gpte[GPT_ENTRY_MAX - 1].guid.b[sizeof(guid_t) -1] += GPT_ENTRY_MAX;
  383. }
  384. end = sect + GPT_SIZE;
  385. pte[0].type = 0xEE;
  386. pte[0].start = cpu_to_le32(GPT_HEADER_SECTOR);
  387. pte[0].length = cpu_to_le32(end - GPT_HEADER_SECTOR);
  388. to_chs(GPT_HEADER_SECTOR, pte[0].chs_start);
  389. to_chs(end, pte[0].chs_end);
  390. gpth.last_usable = cpu_to_le64(end - GPT_SIZE - 1);
  391. gpth.alternate = cpu_to_le64(end);
  392. gpth.entry_crc32 = cpu_to_le32(gpt_crc32(gpte, GPT_ENTRY_SIZE * GPT_ENTRY_MAX));
  393. gpth.crc32 = cpu_to_le32(gpt_crc32((char *)&gpth, GPT_HEADER_SIZE));
  394. if ((fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC, 0644)) < 0) {
  395. fprintf(stderr, "Can't open output file '%s'\n",filename);
  396. return ret;
  397. }
  398. lseek(fd, MBR_DISK_SIGNATURE_OFFSET, SEEK_SET);
  399. if (write(fd, &signature, sizeof(signature)) != sizeof(signature)) {
  400. fputs("write failed.\n", stderr);
  401. goto fail;
  402. }
  403. lseek(fd, MBR_PARTITION_ENTRY_OFFSET, SEEK_SET);
  404. if (write(fd, pte, sizeof(struct pte) * MBR_ENTRY_MAX) != sizeof(struct pte) * MBR_ENTRY_MAX) {
  405. fputs("write failed.\n", stderr);
  406. goto fail;
  407. }
  408. lseek(fd, MBR_BOOT_SIGNATURE_OFFSET, SEEK_SET);
  409. if (write(fd, "\x55\xaa", 2) != 2) {
  410. fputs("write failed.\n", stderr);
  411. goto fail;
  412. }
  413. if (write(fd, &gpth, GPT_HEADER_SIZE) != GPT_HEADER_SIZE) {
  414. fputs("write failed.\n", stderr);
  415. goto fail;
  416. }
  417. lseek(fd, GPT_FIRST_ENTRY_SECTOR * DISK_SECTOR_SIZE, SEEK_SET);
  418. if (write(fd, &gpte, GPT_ENTRY_SIZE * GPT_ENTRY_MAX) != GPT_ENTRY_SIZE * GPT_ENTRY_MAX) {
  419. fputs("write failed.\n", stderr);
  420. goto fail;
  421. }
  422. #ifdef WANT_ALTERNATE_PTABLE
  423. /* The alternate partition table (We omit it by default) */
  424. swap(gpth.self, gpth.alternate);
  425. gpth.first_entry = cpu_to_le64(end - GPT_ENTRY_SIZE * GPT_ENTRY_MAX / DISK_SECTOR_SIZE),
  426. gpth.crc32 = 0;
  427. gpth.crc32 = cpu_to_le32(gpt_crc32(&gpth, GPT_HEADER_SIZE));
  428. lseek(fd, end * DISK_SECTOR_SIZE - GPT_ENTRY_SIZE * GPT_ENTRY_MAX, SEEK_SET);
  429. if (write(fd, &gpte, GPT_ENTRY_SIZE * GPT_ENTRY_MAX) != GPT_ENTRY_SIZE * GPT_ENTRY_MAX) {
  430. fputs("write failed.\n", stderr);
  431. goto fail;
  432. }
  433. lseek(fd, end * DISK_SECTOR_SIZE, SEEK_SET);
  434. if (write(fd, &gpth, GPT_HEADER_SIZE) != GPT_HEADER_SIZE) {
  435. fputs("write failed.\n", stderr);
  436. goto fail;
  437. }
  438. lseek(fd, (end + 1) * DISK_SECTOR_SIZE -1, SEEK_SET);
  439. if (write(fd, "\x00", 1) != 1) {
  440. fputs("write failed.\n", stderr);
  441. goto fail;
  442. }
  443. #endif
  444. ret = 0;
  445. fail:
  446. close(fd);
  447. return ret;
  448. }
  449. static void usage(char *prog)
  450. {
  451. fprintf(stderr, "Usage: %s [-v] [-n] [-g] -h <heads> -s <sectors> -o <outputfile> [-a 0..4] [-l <align kB>] [-G <guid>] [[-t <type>] [-r] [-N <name>] -p <size>[@<start>]...] \n", prog);
  452. exit(EXIT_FAILURE);
  453. }
  454. static guid_t type_to_guid_and_name(unsigned char type, char **name)
  455. {
  456. guid_t guid = GUID_PARTITION_BASIC_DATA;
  457. switch (type) {
  458. case 0xef:
  459. if(*name == NULL)
  460. *name = "EFI System Partition";
  461. guid = GUID_PARTITION_SYSTEM;
  462. break;
  463. case 0x83:
  464. guid = GUID_PARTITION_LINUX_FS_GUID;
  465. break;
  466. case 0x2e:
  467. guid = GUID_PARTITION_LINUX_FIT_GUID;
  468. break;
  469. }
  470. return guid;
  471. }
  472. int main (int argc, char **argv)
  473. {
  474. unsigned char type = 0x83;
  475. char *p;
  476. int ch;
  477. int part = 0;
  478. char *name = NULL;
  479. unsigned short int hybrid = 0, required = 0;
  480. uint32_t signature = 0x5452574F; /* 'OWRT' */
  481. guid_t guid = GUID_INIT( signature, 0x2211, 0x4433, \
  482. 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0x00);
  483. guid_t part_guid = GUID_PARTITION_BASIC_DATA;
  484. while ((ch = getopt(argc, argv, "h:s:p:a:t:o:vnHN:gl:rS:G:")) != -1) {
  485. switch (ch) {
  486. case 'o':
  487. filename = optarg;
  488. break;
  489. case 'v':
  490. verbose++;
  491. break;
  492. case 'n':
  493. ignore_null_sized_partition = true;
  494. break;
  495. case 'g':
  496. use_guid_partition_table = 1;
  497. break;
  498. case 'H':
  499. hybrid = 1;
  500. break;
  501. case 'h':
  502. heads = (int)strtoul(optarg, NULL, 0);
  503. break;
  504. case 's':
  505. sectors = (int)strtoul(optarg, NULL, 0);
  506. break;
  507. case 'p':
  508. if (part > GPT_ENTRY_MAX - 1 || (!use_guid_partition_table && part > 3)) {
  509. fputs("Too many partitions\n", stderr);
  510. exit(EXIT_FAILURE);
  511. }
  512. p = strchr(optarg, '@');
  513. if (p) {
  514. *(p++) = 0;
  515. parts[part].start = to_kbytes(p);
  516. }
  517. part_guid = type_to_guid_and_name(type, &name);
  518. parts[part].size = to_kbytes(optarg);
  519. parts[part].required = required;
  520. parts[part].name = name;
  521. parts[part].hybrid = hybrid;
  522. parts[part].guid = part_guid;
  523. fprintf(stderr, "part %ld %ld\n", parts[part].start, parts[part].size);
  524. parts[part++].type = type;
  525. /*
  526. * reset 'name','required' and 'hybrid'
  527. * 'type' is deliberately inherited from the previous delcaration
  528. */
  529. name = NULL;
  530. required = 0;
  531. hybrid = 0;
  532. break;
  533. case 'N':
  534. name = optarg;
  535. break;
  536. case 'r':
  537. required = 1;
  538. break;
  539. case 't':
  540. type = (char)strtoul(optarg, NULL, 16);
  541. break;
  542. case 'a':
  543. active = (int)strtoul(optarg, NULL, 0);
  544. if ((active < 0) || (active > 4))
  545. active = 0;
  546. break;
  547. case 'l':
  548. kb_align = (int)strtoul(optarg, NULL, 0) * 2;
  549. break;
  550. case 'S':
  551. signature = strtoul(optarg, NULL, 0);
  552. break;
  553. case 'G':
  554. if (guid_parse(optarg, &guid)) {
  555. fputs("Invalid guid string\n", stderr);
  556. exit(EXIT_FAILURE);
  557. }
  558. break;
  559. case '?':
  560. default:
  561. usage(argv[0]);
  562. }
  563. }
  564. argc -= optind;
  565. if (argc || (!use_guid_partition_table && ((heads <= 0) || (sectors <= 0))) || !filename)
  566. usage(argv[0]);
  567. if (use_guid_partition_table) {
  568. heads = 254;
  569. sectors = 63;
  570. return gen_gptable(signature, guid, part) ? EXIT_FAILURE : EXIT_SUCCESS;
  571. }
  572. return gen_ptable(signature, part) ? EXIT_FAILURE : EXIT_SUCCESS;
  573. }