ptgen.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 <ctype.h>
  30. #include <fcntl.h>
  31. #include <stdint.h>
  32. #if __BYTE_ORDER == __BIG_ENDIAN
  33. #define cpu_to_le32(x) bswap_32(x)
  34. #elif __BYTE_ORDER == __LITTLE_ENDIAN
  35. #define cpu_to_le32(x) (x)
  36. #else
  37. #error unknown endianness!
  38. #endif
  39. /* Partition table entry */
  40. struct pte {
  41. uint8_t active;
  42. uint8_t chs_start[3];
  43. uint8_t type;
  44. uint8_t chs_end[3];
  45. uint32_t start;
  46. uint32_t length;
  47. };
  48. struct partinfo {
  49. unsigned long size;
  50. int type;
  51. };
  52. int verbose = 0;
  53. int active = 1;
  54. int heads = -1;
  55. int sectors = -1;
  56. int kb_align = 0;
  57. struct partinfo parts[4];
  58. char *filename = NULL;
  59. /*
  60. * parse the size argument, which is either
  61. * a simple number (K assumed) or
  62. * K, M or G
  63. *
  64. * returns the size in KByte
  65. */
  66. static long to_kbytes(const char *string) {
  67. int exp = 0;
  68. long result;
  69. char *end;
  70. result = strtoul(string, &end, 0);
  71. switch (tolower(*end)) {
  72. case 'k' :
  73. case '\0' : exp = 0; break;
  74. case 'm' : exp = 1; break;
  75. case 'g' : exp = 2; break;
  76. default: return 0;
  77. }
  78. if (*end)
  79. end++;
  80. if (*end) {
  81. fprintf(stderr, "garbage after end of number\n");
  82. return 0;
  83. }
  84. /* result: number + 1024^(exp) */
  85. if (exp == 0)
  86. return result;
  87. return result * (2 << ((10 * exp) - 1));
  88. }
  89. /* convert the sector number into a CHS value for the partition table */
  90. static void to_chs(long sect, unsigned char chs[3]) {
  91. int c,h,s;
  92. s = (sect % sectors) + 1;
  93. sect = sect / sectors;
  94. h = sect % heads;
  95. sect = sect / heads;
  96. c = sect;
  97. chs[0] = h;
  98. chs[1] = s | ((c >> 2) & 0xC0);
  99. chs[2] = c & 0xFF;
  100. return;
  101. }
  102. /* round the sector number up to the next cylinder */
  103. static inline unsigned long round_to_cyl(long sect) {
  104. int cyl_size = heads * sectors;
  105. return sect + cyl_size - (sect % cyl_size);
  106. }
  107. /* round the sector number up to the kb_align boundary */
  108. static inline unsigned long round_to_kb(long sect) {
  109. return ((sect - 1) / kb_align + 1) * kb_align;
  110. }
  111. /* check the partition sizes and write the partition table */
  112. static int gen_ptable(uint32_t signature, int nr)
  113. {
  114. struct pte pte[4];
  115. unsigned long sect = 0;
  116. int i, fd, ret = -1, start, len;
  117. memset(pte, 0, sizeof(struct pte) * 4);
  118. for (i = 0; i < nr; i++) {
  119. if (!parts[i].size) {
  120. fprintf(stderr, "Invalid size in partition %d!\n", i);
  121. return -1;
  122. }
  123. pte[i].active = ((i + 1) == active) ? 0x80 : 0;
  124. pte[i].type = parts[i].type;
  125. start = sect + sectors;
  126. if (kb_align != 0)
  127. start = round_to_kb(start);
  128. pte[i].start = cpu_to_le32(start);
  129. sect = start + parts[i].size * 2;
  130. if (kb_align == 0)
  131. sect = round_to_cyl(sect);
  132. pte[i].length = cpu_to_le32(len = sect - start);
  133. to_chs(start, pte[i].chs_start);
  134. to_chs(start + len - 1, pte[i].chs_end);
  135. if (verbose)
  136. fprintf(stderr, "Partition %d: start=%ld, end=%ld, size=%ld\n", i, (long) start * 512, ((long) start + (long) len) * 512, (long) len * 512);
  137. printf("%ld\n", ((long) start * 512));
  138. printf("%ld\n", ((long) len * 512));
  139. }
  140. if ((fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC, 0644)) < 0) {
  141. fprintf(stderr, "Can't open output file '%s'\n",filename);
  142. return -1;
  143. }
  144. lseek(fd, 440, SEEK_SET);
  145. if (write(fd, &signature, sizeof(signature)) != sizeof(signature)) {
  146. fprintf(stderr, "write failed.\n");
  147. goto fail;
  148. }
  149. lseek(fd, 446, SEEK_SET);
  150. if (write(fd, pte, sizeof(struct pte) * 4) != sizeof(struct pte) * 4) {
  151. fprintf(stderr, "write failed.\n");
  152. goto fail;
  153. }
  154. lseek(fd, 510, SEEK_SET);
  155. if (write(fd, "\x55\xaa", 2) != 2) {
  156. fprintf(stderr, "write failed.\n");
  157. goto fail;
  158. }
  159. ret = 0;
  160. fail:
  161. close(fd);
  162. return ret;
  163. }
  164. static void usage(char *prog)
  165. {
  166. fprintf(stderr, "Usage: %s [-v] -h <heads> -s <sectors> -o <outputfile> [-a 0..4] [-l <align kB>] [[-t <type>] -p <size>...] \n", prog);
  167. exit(1);
  168. }
  169. int main (int argc, char **argv)
  170. {
  171. char type = 0x83;
  172. int ch;
  173. int part = 0;
  174. uint32_t signature = 0x5452574F; /* 'OWRT' */
  175. while ((ch = getopt(argc, argv, "h:s:p:a:t:o:vl:S:")) != -1) {
  176. switch (ch) {
  177. case 'o':
  178. filename = optarg;
  179. break;
  180. case 'v':
  181. verbose++;
  182. break;
  183. case 'h':
  184. heads = (int) strtoul(optarg, NULL, 0);
  185. break;
  186. case 's':
  187. sectors = (int) strtoul(optarg, NULL, 0);
  188. break;
  189. case 'p':
  190. if (part > 3) {
  191. fprintf(stderr, "Too many partitions\n");
  192. exit(1);
  193. }
  194. parts[part].size = to_kbytes(optarg);
  195. parts[part++].type = type;
  196. break;
  197. case 't':
  198. type = (char) strtoul(optarg, NULL, 16);
  199. break;
  200. case 'a':
  201. active = (int) strtoul(optarg, NULL, 0);
  202. if ((active < 0) || (active > 4))
  203. active = 0;
  204. break;
  205. case 'l':
  206. kb_align = (int) strtoul(optarg, NULL, 0) * 2;
  207. break;
  208. case 'S':
  209. signature = strtoul(optarg, NULL, 0);
  210. break;
  211. case '?':
  212. default:
  213. usage(argv[0]);
  214. }
  215. }
  216. argc -= optind;
  217. if (argc || (heads <= 0) || (sectors <= 0) || !filename)
  218. usage(argv[0]);
  219. return gen_ptable(signature, part);
  220. }