/*
* ptgen - partition table generator
- * Copyright (C) 2006 by Felix Fietkau <nbd@openwrt.org>
+ * Copyright (C) 2006 by Felix Fietkau <nbd@nbd.name>
*
* uses parts of afdisk
* Copyright (C) 2002 by David Roetzel <david@roetzel.de>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
+#include <stdint.h>
#include <ctype.h>
#include <fcntl.h>
+#include <stdint.h>
#if __BYTE_ORDER == __BIG_ENDIAN
-#define cpu_to_le16(x) bswap_16(x)
+#define cpu_to_le32(x) bswap_32(x)
#elif __BYTE_ORDER == __LITTLE_ENDIAN
-#define cpu_to_le16(x) (x)
+#define cpu_to_le32(x) (x)
#else
#error unknown endianness!
#endif
/* Partition table entry */
-struct pte {
- unsigned char active;
- unsigned char chs_start[3];
- unsigned char type;
- unsigned char chs_end[3];
- unsigned int start;
- unsigned int length;
+struct pte {
+ uint8_t active;
+ uint8_t chs_start[3];
+ uint8_t type;
+ uint8_t chs_end[3];
+ uint32_t start;
+ uint32_t length;
};
struct partinfo {
int active = 1;
int heads = -1;
int sectors = -1;
+int kb_align = 0;
struct partinfo parts[4];
char *filename = NULL;
}
/* result: number + 1024^(exp) */
- return result * ((2 << ((10 * exp) - 1)) ?: 1);
+ if (exp == 0)
+ return result;
+ return result * (2 << ((10 * exp) - 1));
}
/* convert the sector number into a CHS value for the partition table */
return sect + cyl_size - (sect % cyl_size);
}
+/* round the sector number up to the kb_align boundary */
+static inline unsigned long round_to_kb(long sect) {
+ return ((sect - 1) / kb_align + 1) * kb_align;
+}
+
/* check the partition sizes and write the partition table */
-static int gen_ptable(int nr)
+static int gen_ptable(uint32_t signature, int nr)
{
struct pte pte[4];
unsigned long sect = 0;
}
pte[i].active = ((i + 1) == active) ? 0x80 : 0;
pte[i].type = parts[i].type;
- pte[i].start = cpu_to_le16(start = sect + sectors);
- sect = round_to_cyl(start + parts[i].size * 2);
- pte[i].length = cpu_to_le16(len = sect - start);
+ start = sect + sectors;
+ if (kb_align != 0)
+ start = round_to_kb(start);
+ pte[i].start = cpu_to_le32(start);
+ sect = start + parts[i].size * 2;
+ if (kb_align == 0)
+ sect = round_to_cyl(sect);
+ pte[i].length = cpu_to_le32(len = sect - start);
to_chs(start, pte[i].chs_start);
to_chs(start + len - 1, pte[i].chs_end);
if (verbose)
fprintf(stderr, "Partition %d: start=%ld, end=%ld, size=%ld\n", i, (long) start * 512, ((long) start + (long) len) * 512, (long) len * 512);
printf("%ld\n", ((long) start * 512));
+ printf("%ld\n", ((long) len * 512));
}
- if ((fd = open(filename, O_WRONLY|O_CREAT, 0644)) < 0) {
+ if ((fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC, 0644)) < 0) {
fprintf(stderr, "Can't open output file '%s'\n",filename);
return -1;
}
+ lseek(fd, 440, SEEK_SET);
+ if (write(fd, &signature, sizeof(signature)) != sizeof(signature)) {
+ fprintf(stderr, "write failed.\n");
+ goto fail;
+ }
+
lseek(fd, 446, SEEK_SET);
if (write(fd, pte, sizeof(struct pte) * 4) != sizeof(struct pte) * 4) {
fprintf(stderr, "write failed.\n");
static void usage(char *prog)
{
- fprintf(stderr, "Usage: %s [-v] -h <heads> -s <sectors> -o <outputfile> [-a 0..4] [[-t <type>] -p <size>...] \n", prog);
+ fprintf(stderr, "Usage: %s [-v] -h <heads> -s <sectors> -o <outputfile> [-a 0..4] [-l <align kB>] [[-t <type>] -p <size>...] \n", prog);
exit(1);
}
char type = 0x83;
int ch;
int part = 0;
+ uint32_t signature = 0x5452574F; /* 'OWRT' */
- while ((ch = getopt(argc, argv, "h:s:p:a:t:o:v")) != -1) {
+ while ((ch = getopt(argc, argv, "h:s:p:a:t:o:vl:S:")) != -1) {
switch (ch) {
case 'o':
filename = optarg;
if ((active < 0) || (active > 4))
active = 0;
break;
+ case 'l':
+ kb_align = (int) strtoul(optarg, NULL, 0) * 2;
+ break;
+ case 'S':
+ signature = strtoul(optarg, NULL, 0);
+ break;
case '?':
default:
usage(argv[0]);
argc -= optind;
if (argc || (heads <= 0) || (sectors <= 0) || !filename)
usage(argv[0]);
-
- return gen_ptable(part);
+
+ return gen_ptable(signature, part);
}