pt.c 5.7 KB

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