emile-mktable: force the size of the disk in the table

Signed-off-by: Laurent Vivier <laurent@vivier.eu>
This commit is contained in:
Laurent Vivier 2015-11-11 18:42:51 +01:00
parent 3be63e7001
commit 53062ff0bf

View File

@ -18,6 +18,7 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <fcntl.h> #include <fcntl.h>
#include <errno.h> #include <errno.h>
#include <limits.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <sys/mount.h> #include <sys/mount.h>
@ -42,12 +43,14 @@ enum {
ARG_NONE = 0, ARG_NONE = 0,
ARG_HELP ='h', ARG_HELP ='h',
ARG_EMILEDRIVER = 'e', ARG_EMILEDRIVER = 'e',
ARG_FORCE_SIZE = 'f',
}; };
static struct option long_options[] = static struct option long_options[] =
{ {
{"help", 0, NULL, ARG_HELP }, {"help", 0, NULL, ARG_HELP },
{"emiledriver", 1, NULL, ARG_EMILEDRIVER }, {"emiledriver", 1, NULL, ARG_EMILEDRIVER },
{"force-size", 1, NULL, ARG_FORCE_SIZE },
{NULL, 0, NULL, 0 }, {NULL, 0, NULL, 0 },
}; };
@ -56,15 +59,17 @@ static void usage(int argc, char** argv)
fprintf(stderr, "Usage %s [FLAGS] disk\n", fprintf(stderr, "Usage %s [FLAGS] disk\n",
argv[0]); argv[0]);
fprintf(stderr, "Create and EMILE bootable disk\n"); fprintf(stderr, "Create and EMILE bootable disk\n");
fprintf(stderr, " -h, --help display this text\n"); fprintf(stderr, " -h, --help display this text\n");
fprintf(stderr, " -e, --emiledriver=FILE " fprintf(stderr, " -f, --force-size=SECTORS "
"force the size of the disk in the table\n");
fprintf(stderr, " -e, --emiledriver=FILE "
"emiledriver to copy to disk\n"); "emiledriver to copy to disk\n");
fprintf(stderr, "\nbuild: \n%s\n", SIGNATURE); fprintf(stderr, "\nbuild: \n%s\n", SIGNATURE);
} }
#define BLOCKSIZE (512) #define BLOCKSIZE (512)
static int emile_mktable(char *filename, char *appledriver) static int emile_mktable(char *filename, char *appledriver, unsigned long force_size)
{ {
struct DriverDescriptor block0; struct DriverDescriptor block0;
struct Partition *map512; struct Partition *map512;
@ -87,9 +92,13 @@ static int emile_mktable(char *filename, char *appledriver)
filename); filename);
return -1; return -1;
} }
if (ioctl(fd, BLKGETSIZE, &disk_block_count) == -1) { if (force_size == 0) {
fstat(fd, &st); if (ioctl(fd, BLKGETSIZE, &disk_block_count) == -1) {
disk_block_count = (st.st_size + 512 - 1) / 512; fstat(fd, &st);
disk_block_count = (st.st_size + 512 - 1) / 512;
}
} else {
disk_block_count = force_size;
} }
/* read apple driver */ /* read apple driver */
@ -242,11 +251,12 @@ int main(int argc, char** argv)
int option_index = 0; int option_index = 0;
char* image = NULL; char* image = NULL;
char* emiledriver = NULL; char* emiledriver = NULL;
unsigned long force_size = 0;
int c; int c;
while(1) while(1)
{ {
c = getopt_long(argc, argv, "he:c:", c = getopt_long(argc, argv, "he:f:",
long_options, &option_index); long_options, &option_index);
if (c == -1) if (c == -1)
break; break;
@ -258,6 +268,13 @@ int main(int argc, char** argv)
case ARG_EMILEDRIVER: case ARG_EMILEDRIVER:
emiledriver = optarg; emiledriver = optarg;
break; break;
case ARG_FORCE_SIZE:
force_size = strtoull(optarg, NULL, 0);
if (force_size == ULONG_MAX && errno == ERANGE) {
fprintf(stderr, "ERROR: invalid size\n");
return 1;
}
break;
} }
} }
@ -275,7 +292,7 @@ int main(int argc, char** argv)
emiledriver = DRIVER_PATH; emiledriver = DRIVER_PATH;
if (emile_mktable(image, emiledriver)) if (emile_mktable(image, emiledriver, force_size))
return 1; return 1;
return 0; return 0;