pt.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * pt - partition table utility
  3. * Copyright (C) 2010 by Waldemar Brodkorb <wbx@openadk.org>
  4. *
  5. * just adds some required code to ptgen - partition table generator
  6. * Copyright (C) 2006 by Felix Fietkau <nbd@openwrt.org>
  7. *
  8. * uses parts of afdisk
  9. * Copyright (C) 2002 by David Roetzel <david@roetzel.de>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  24. */
  25. #include <sys/types.h>
  26. #include <sys/stat.h>
  27. #include <string.h>
  28. #include <unistd.h>
  29. #include <stdlib.h>
  30. #include <stdio.h>
  31. #include <ctype.h>
  32. #include <fcntl.h>
  33. #include <sys/ioctl.h>
  34. #if defined(__linux__)
  35. #include <linux/fs.h>
  36. #endif
  37. #if defined(__APPLE__)
  38. #include <sys/disk.h>
  39. #define BLKGETSIZE DKIOCGETBLOCKCOUNT
  40. #endif
  41. /* Partition table entry */
  42. struct pte {
  43. unsigned char active;
  44. unsigned char chs_start[3];
  45. unsigned char type;
  46. unsigned char chs_end[3];
  47. unsigned int start;
  48. unsigned int length;
  49. };
  50. struct partinfo {
  51. unsigned long size;
  52. int type;
  53. };
  54. int verbose = 0;
  55. int active = 1;
  56. int heads = -1;
  57. int sectors = -1;
  58. struct partinfo parts[4];
  59. char *filename = NULL;
  60. /*
  61. * get the sector size of the block device
  62. *
  63. * print the sector size
  64. */
  65. static void getmaxsize(char *device) {
  66. int fd;
  67. unsigned long maxsectors=0;
  68. fd = open(device, O_RDONLY);
  69. ioctl(fd, BLKGETSIZE, &maxsectors);
  70. printf("%lu\n", maxsectors);
  71. close(fd);
  72. }
  73. /*
  74. * parse the size argument, which is either
  75. * a simple number (K assumed) or
  76. * K, M or G
  77. *
  78. * returns the size in KByte
  79. */
  80. static long to_kbytes(const char *string) {
  81. int exp = 0;
  82. long result;
  83. char *end;
  84. result = strtoul(string, &end, 0);
  85. switch (tolower(*end)) {
  86. case 'k' :
  87. case '\0' : exp = 0; break;
  88. case 'm' : exp = 1; break;
  89. case 'g' : exp = 2; break;
  90. default: return 0;
  91. }
  92. if (*end)
  93. end++;
  94. if (*end) {
  95. fprintf(stderr, "garbage after end of number\n");
  96. return 0;
  97. }
  98. /* result: number + 1024^(exp) */
  99. return result * ((2 << ((10 * exp) - 1)) ?: 1);
  100. }
  101. /* convert the sector number into a CHS value for the partition table */
  102. static void to_chs(long sect, unsigned char chs[3]) {
  103. int c,h,s;
  104. s = (sect % sectors) + 1;
  105. sect = sect / sectors;
  106. h = sect % heads;
  107. sect = sect / heads;
  108. c = sect;
  109. chs[0] = h;
  110. chs[1] = s | ((c >> 2) & 0xC0);
  111. chs[2] = c & 0xFF;
  112. return;
  113. }
  114. /* round the sector number up to the next cylinder */
  115. static inline unsigned long round_to_cyl(long sect) {
  116. int cyl_size = heads * sectors;
  117. return sect + cyl_size - (sect % cyl_size);
  118. }
  119. /* check the partition sizes and write the partition table */
  120. static int gen_ptable(int nr)
  121. {
  122. struct pte pte[4];
  123. unsigned long sect = 0;
  124. unsigned int start, len;
  125. int i, fd, ret = -1;
  126. memset(pte, 0, sizeof(struct pte) * 4);
  127. for (i = 0; i < nr; i++) {
  128. if (!parts[i].size) {
  129. fprintf(stderr, "Invalid size in partition %d!\n", i);
  130. return -1;
  131. }
  132. pte[i].active = ((i + 1) == active) ? 0x80 : 0;
  133. pte[i].type = parts[i].type;
  134. pte[i].start = start = sect + sectors;
  135. sect = round_to_cyl(start + parts[i].size * 2);
  136. pte[i].length = len = sect - start;
  137. to_chs(start, pte[i].chs_start);
  138. to_chs(start + len - 1, pte[i].chs_end);
  139. if (verbose)
  140. fprintf(stderr, "Partition %d: start=%u, end=%u, size=%u\n", i, start * 512, (start + len) * 512, len * 512);
  141. }
  142. if ((fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC, 0644)) < 0) {
  143. fprintf(stderr, "Can't open output file '%s'\n",filename);
  144. return -1;
  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] [[-t <type>] -p <size>...] \n", prog);
  164. fprintf(stderr, "Usage: %s -g <device>\n", prog);
  165. exit(1);
  166. }
  167. int main (int argc, char **argv)
  168. {
  169. char type = 0x83;
  170. int ch;
  171. int part = 0;
  172. while ((ch = getopt(argc, argv, "h:s:p:a:t:o:vg:")) != -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 'g':
  203. getmaxsize(optarg);
  204. exit(0);
  205. case '?':
  206. default:
  207. usage(argv[0]);
  208. }
  209. }
  210. argc -= optind;
  211. if (argc || (heads <= 0) || (sectors <= 0) || !filename)
  212. usage(argv[0]);
  213. return gen_ptable(part);
  214. }