ptgen.c 5.0 KB

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