ptgen.c 5.7 KB

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