ptgen.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*
  2. * ptgen - partition table generator
  3. * Copyright (C) 2006 by Felix Fietkau <nbd@nbd.name>
  4. *
  5. * uses parts of afdisk
  6. * Copyright (C) 2002 by David Roetzel <david@roetzel.de>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include <sys/types.h>
  23. #include <sys/stat.h>
  24. #include <string.h>
  25. #include <unistd.h>
  26. #include <stdlib.h>
  27. #include <stdio.h>
  28. #include <stdint.h>
  29. #include <stdbool.h>
  30. #include <ctype.h>
  31. #include <fcntl.h>
  32. #include <stdint.h>
  33. #if __BYTE_ORDER == __BIG_ENDIAN
  34. #define cpu_to_le32(x) bswap_32(x)
  35. #elif __BYTE_ORDER == __LITTLE_ENDIAN
  36. #define cpu_to_le32(x) (x)
  37. #else
  38. #error unknown endianness!
  39. #endif
  40. /* Partition table entry */
  41. struct pte {
  42. uint8_t active;
  43. uint8_t chs_start[3];
  44. uint8_t type;
  45. uint8_t chs_end[3];
  46. uint32_t start;
  47. uint32_t length;
  48. };
  49. struct partinfo {
  50. unsigned long size;
  51. int type;
  52. };
  53. int verbose = 0;
  54. int active = 1;
  55. int heads = -1;
  56. int sectors = -1;
  57. int kb_align = 0;
  58. bool ignore_null_sized_partition = false;
  59. struct partinfo parts[4];
  60. char *filename = NULL;
  61. /*
  62. * parse the size argument, which is either
  63. * a simple number (K assumed) or
  64. * K, M or G
  65. *
  66. * returns the size in KByte
  67. */
  68. static long to_kbytes(const char *string)
  69. {
  70. int exp = 0;
  71. long result;
  72. char *end;
  73. result = strtoul(string, &end, 0);
  74. switch (tolower(*end)) {
  75. case 'k' :
  76. case '\0' : exp = 0; break;
  77. case 'm' : exp = 1; break;
  78. case 'g' : exp = 2; break;
  79. default: return 0;
  80. }
  81. if (*end)
  82. end++;
  83. if (*end) {
  84. fprintf(stderr, "garbage after end of number\n");
  85. return 0;
  86. }
  87. /* result: number + 1024^(exp) */
  88. if (exp == 0)
  89. return result;
  90. return result * (2 << ((10 * exp) - 1));
  91. }
  92. /* convert the sector number into a CHS value for the partition table */
  93. static void to_chs(long sect, unsigned char chs[3])
  94. {
  95. int c,h,s;
  96. s = (sect % sectors) + 1;
  97. sect = sect / sectors;
  98. h = sect % heads;
  99. sect = sect / heads;
  100. c = sect;
  101. chs[0] = h;
  102. chs[1] = s | ((c >> 2) & 0xC0);
  103. chs[2] = c & 0xFF;
  104. return;
  105. }
  106. /* round the sector number up to the next cylinder */
  107. static inline unsigned long round_to_cyl(long sect)
  108. {
  109. int cyl_size = heads * sectors;
  110. return sect + cyl_size - (sect % cyl_size);
  111. }
  112. /* round the sector number up to the kb_align boundary */
  113. static inline unsigned long round_to_kb(long sect) {
  114. return ((sect - 1) / kb_align + 1) * kb_align;
  115. }
  116. /* check the partition sizes and write the partition table */
  117. static int gen_ptable(uint32_t signature, int nr)
  118. {
  119. struct pte pte[4];
  120. unsigned long sect = 0;
  121. int i, fd, ret = -1, start, len;
  122. memset(pte, 0, sizeof(struct pte) * 4);
  123. for (i = 0; i < nr; i++) {
  124. if (!parts[i].size) {
  125. if (ignore_null_sized_partition)
  126. continue;
  127. fprintf(stderr, "Invalid size in partition %d!\n", i);
  128. return -1;
  129. }
  130. pte[i].active = ((i + 1) == active) ? 0x80 : 0;
  131. pte[i].type = parts[i].type;
  132. start = sect + sectors;
  133. if (kb_align != 0)
  134. start = round_to_kb(start);
  135. pte[i].start = cpu_to_le32(start);
  136. sect = start + parts[i].size * 2;
  137. if (kb_align == 0)
  138. sect = round_to_cyl(sect);
  139. pte[i].length = cpu_to_le32(len = sect - start);
  140. to_chs(start, pte[i].chs_start);
  141. to_chs(start + len - 1, pte[i].chs_end);
  142. if (verbose)
  143. fprintf(stderr, "Partition %d: start=%ld, end=%ld, size=%ld\n", i, (long)start * 512, ((long)start + (long)len) * 512, (long)len * 512);
  144. printf("%ld\n", (long)start * 512);
  145. printf("%ld\n", (long)len * 512);
  146. }
  147. if ((fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC, 0644)) < 0) {
  148. fprintf(stderr, "Can't open output file '%s'\n",filename);
  149. return -1;
  150. }
  151. lseek(fd, 440, SEEK_SET);
  152. if (write(fd, &signature, sizeof(signature)) != sizeof(signature)) {
  153. fprintf(stderr, "write failed.\n");
  154. goto fail;
  155. }
  156. lseek(fd, 446, SEEK_SET);
  157. if (write(fd, pte, sizeof(struct pte) * 4) != sizeof(struct pte) * 4) {
  158. fprintf(stderr, "write failed.\n");
  159. goto fail;
  160. }
  161. lseek(fd, 510, SEEK_SET);
  162. if (write(fd, "\x55\xaa", 2) != 2) {
  163. fprintf(stderr, "write failed.\n");
  164. goto fail;
  165. }
  166. ret = 0;
  167. fail:
  168. close(fd);
  169. return ret;
  170. }
  171. static void usage(char *prog)
  172. {
  173. fprintf(stderr, "Usage: %s [-v] [-n] -h <heads> -s <sectors> -o <outputfile> [-a 0..4] [-l <align kB>] [[-t <type>] -p <size>...] \n", prog);
  174. exit(EXIT_FAILURE);
  175. }
  176. int main (int argc, char **argv)
  177. {
  178. char type = 0x83;
  179. int ch;
  180. int part = 0;
  181. uint32_t signature = 0x5452574F; /* 'OWRT' */
  182. while ((ch = getopt(argc, argv, "h:s:p:a:t:o:vnl:S:")) != -1) {
  183. switch (ch) {
  184. case 'o':
  185. filename = optarg;
  186. break;
  187. case 'v':
  188. verbose++;
  189. break;
  190. case 'n':
  191. ignore_null_sized_partition = true;
  192. break;
  193. case 'h':
  194. heads = (int)strtoul(optarg, NULL, 0);
  195. break;
  196. case 's':
  197. sectors = (int)strtoul(optarg, NULL, 0);
  198. break;
  199. case 'p':
  200. if (part > 3) {
  201. fprintf(stderr, "Too many partitions\n");
  202. exit(EXIT_FAILURE);
  203. }
  204. parts[part].size = to_kbytes(optarg);
  205. parts[part++].type = type;
  206. break;
  207. case 't':
  208. type = (char)strtoul(optarg, NULL, 16);
  209. break;
  210. case 'a':
  211. active = (int)strtoul(optarg, NULL, 0);
  212. if ((active < 0) || (active > 4))
  213. active = 0;
  214. break;
  215. case 'l':
  216. kb_align = (int)strtoul(optarg, NULL, 0) * 2;
  217. break;
  218. case 'S':
  219. signature = strtoul(optarg, NULL, 0);
  220. break;
  221. case '?':
  222. default:
  223. usage(argv[0]);
  224. }
  225. }
  226. argc -= optind;
  227. if (argc || (heads <= 0) || (sectors <= 0) || !filename)
  228. usage(argv[0]);
  229. return gen_ptable(signature, part) ? EXIT_FAILURE : EXIT_SUCCESS;
  230. }