EMILE/tools/emile-install.c

548 lines
12 KiB
C
Raw Normal View History

2004-06-01 21:56:28 +00:00
/*
*
2008-04-16 18:32:17 +00:00
* (c) 2004-2008 Laurent Vivier <Laurent@lvivier.info>
2004-06-01 21:56:28 +00:00
*
*/
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
2004-06-01 21:56:28 +00:00
2004-12-10 00:28:35 +00:00
#include "libemile.h"
2007-08-26 19:46:39 +00:00
#include "libconfig.h"
2004-06-01 21:56:28 +00:00
enum {
ARG_NONE = 0,
ARG_HELP ='h',
ARG_VERBOSE = 'v',
ARG_FIRST = 'f',
ARG_SECOND = 's',
ARG_KERNEL = 'k',
ARG_RAMDISK = 'r',
2004-12-25 00:48:29 +00:00
ARG_GETINFO = 'g',
2007-08-26 19:46:39 +00:00
ARG_CONFIG = 'c',
};
static struct option long_options[] =
{
{"help", 0, NULL, ARG_HELP },
{"verbose", 0, NULL, ARG_VERBOSE },
{"first", 1, NULL, ARG_FIRST },
{"second", 1, NULL, ARG_SECOND },
{"kernel", 1, NULL, ARG_KERNEL },
{"ramdisk", 1, NULL, ARG_RAMDISK },
2004-12-25 00:48:29 +00:00
{"getinfo", 1, NULL, ARG_GETINFO },
2007-08-26 19:46:39 +00:00
{"config", 1, NULL, ARG_CONFIG },
{NULL, 0, NULL, 0 },
};
2004-06-01 21:56:28 +00:00
static void usage(int argc, char** argv)
{
fprintf(stderr, "Usage: %s [OPTION] <image>\n", argv[0]);
fprintf(stderr, "Create an EMILE bootable floppy disk\n");
fprintf(stderr, "EMILE allows you to boot linux from a floppy disk\n");
2008-07-21 22:01:39 +00:00
fprintf(stderr, " -h, --help display this text\n");
fprintf(stderr, " -v, --verbose verbose mode\n");
fprintf(stderr, " -f, --first=PATH "
"first level to copy to floppy\n");
fprintf(stderr, " -s, --second=PATH "
"second level to copy to floppy\n");
fprintf(stderr, " -k, --kernel=PATH kernel to copy to floppy\n");
fprintf(stderr, " -r, --ramdisk=PATH ramdisk to copy to floppy\n");
fprintf(stderr, " -g, --getinfo get information from <image>\n");
fprintf(stderr, " -c, --config=FILE "
2007-09-02 12:57:37 +00:00
"set configuration according to a config file\n");
2004-06-03 11:09:28 +00:00
fprintf(stderr, "\nbuild: \n%s\n", SIGNATURE);
2004-06-01 21:56:28 +00:00
}
2007-09-02 12:46:37 +00:00
static int get_info(char *image, int verbose)
{
int fd;
int drive_num;
int second_offset;
int second_size;
2007-09-08 23:09:14 +00:00
int8_t * configuration;
2007-09-02 12:46:37 +00:00
char property[1024];
char title[1024];
int index;
char *known_properties[] = {
"root",
2007-09-02 12:46:37 +00:00
"kernel",
"initrd",
"args",
"chainloader",
2007-09-02 12:46:37 +00:00
NULL
};
int i;
int current;
int res;
int ret;
fd = open(image, O_RDONLY);
if (fd == -1)
{
fprintf(stderr, "ERROR: cannot open \"%s\"\n",
image);
return 2;
}
/* first level info */
ret = emile_first_get_param(fd, &drive_num, &second_offset,
&second_size);
if (ret != 0)
{
printf("EMILE is not installed in this bootblock\n");
close(fd);
return 0;
}
printf("EMILE boot block identified\n\n");
printf("Drive number: %d\n", drive_num);
printf("Second level offset: %d\n", second_offset);
printf("Second level size: %d\n", second_size);
/* second level info */
printf("EMILE second level information\n");
configuration = emile_second_get_configuration(fd);
if (configuration == NULL)
{
fprintf(stderr, "ERROR: cannot read second level\n");
return 3;
}
if (verbose)
{
printf("%s\n", configuration);
return 0;
}
2007-09-02 12:46:37 +00:00
if (config_get_property(configuration,
"gestaltID", property) != -1)
printf("User forces gestalt ID to %ld\n",
strtol(property, NULL, 0));
if (config_get_property(configuration,
"default", property) != -1)
printf("default %ld\n", strtol(property, NULL, 0));
if (config_get_property(configuration,
"timeout", property) != -1)
printf("timeout %ld\n", strtol(property, NULL, 0));
if (config_get_property(configuration,
"vga", property) != -1)
printf("vga %s\n", property);
if (config_get_property(configuration,
"modem", property) != -1)
printf("modem %s\n", property);
if (config_get_property(configuration,
"printer", property) != -1)
printf("printer %s\n", property);
2007-09-02 12:46:37 +00:00
current = 0;
for (index = 0; index < 20; index++)
{
res = config_get_property(configuration + current,
"title", title);
if (res == -1)
{
if (index)
break;
}
else {
printf("title %s\n", title);
current += res;
current = config_get_next_property(
configuration,
current,
NULL, NULL);
}
for (i = 0; known_properties[i] != NULL; i++)
{
if (config_get_indexed_property(
configuration,
(res == -1) ? NULL : "title",
title,
known_properties[i],
property) != -1)
printf( " %s %s\n",
known_properties[i],
property);
}
}
free(configuration);
close(fd);
return 0;
}
2007-09-02 12:57:37 +00:00
static int set_config(char *image, int verbose, char *config_path,
char *first_level, char *second_level)
{
2008-04-16 18:32:17 +00:00
char property[1024];
char property2[1024];
2007-09-02 12:57:37 +00:00
char *ramdisk_ondisk, *kernel_ondisk;
2007-09-08 23:09:14 +00:00
int8_t *configuration;
2008-04-16 18:32:17 +00:00
int8_t *conffile;
2007-09-02 12:57:37 +00:00
int fd;
2008-04-16 18:32:17 +00:00
struct stat st;
int i;
int current;
int res;
int has_root;
2008-04-16 18:32:17 +00:00
static char *prolog[] = {
"gestaltID",
"default",
"timeout",
"vga",
"modem",
"printer"
};
static char *known_properties[] ={
"root",
2008-04-16 18:32:17 +00:00
"kernel",
"initrd",
2008-04-21 19:49:13 +00:00
"args",
2008-04-16 18:32:17 +00:00
"chainloader"
};
2007-09-02 12:57:37 +00:00
2008-04-16 18:32:17 +00:00
/* open configuration file */
fd = open(config_path, O_RDONLY);
if (fd == -1)
2007-09-02 12:57:37 +00:00
{
2008-04-16 18:32:17 +00:00
fprintf(stderr, "ERROR: cannot open %s\n", config_path);
return 5;
2007-09-02 12:57:37 +00:00
}
2008-04-16 18:32:17 +00:00
if (fstat(fd, &st) == -1)
2007-09-02 12:57:37 +00:00
{
2008-04-16 18:32:17 +00:00
fprintf(stderr, "ERROR: cannot fstat %s\n", config_path);
return 5;
}
conffile = (int8_t*)malloc(st.st_size);
if (conffile == NULL)
{
fprintf(stderr, "ERROR: cannot malloc() %s\n", config_path);
2007-09-02 12:57:37 +00:00
return 5;
}
2008-04-16 18:32:17 +00:00
if (read(fd, conffile, st.st_size) != st.st_size)
{
fprintf(stderr, "ERROR: cannot read() %s\n", config_path);
return 5;
}
close(fd);
2007-09-02 12:57:37 +00:00
2008-04-16 18:32:17 +00:00
/* extract properties */
if (first_level == NULL) {
if (config_get_property(conffile,
"first_level", property) == -1)
first_level = PREFIX "/lib/emile/first_floppy";
else
first_level = property;
}
if (second_level == NULL) {
if (config_get_property(conffile,
"second_level", property2) == -1)
second_level = PREFIX "/lib/emile/second_floppy";
else
second_level = property2;
}
/* create floppy, set first and second level position */
2007-09-02 12:57:37 +00:00
fd = emile_floppy_create(image, first_level, second_level);
if (fd < 0)
{
fprintf(stderr, "ERROR: cannot create %s\n", image);
2008-04-16 18:32:17 +00:00
free(conffile);
2007-09-02 12:57:37 +00:00
return 6;
}
2008-04-16 18:32:17 +00:00
/* create configuration information of the floppy image */
2007-09-02 21:00:05 +00:00
configuration = malloc(65536);
2007-09-02 12:57:37 +00:00
if (configuration == NULL)
{
fprintf(stderr, "ERROR: cannot initalize configuration\n");
2008-04-16 18:32:17 +00:00
free(conffile);
2007-09-02 12:57:37 +00:00
return 7;
}
2007-10-21 01:16:11 +00:00
memset(configuration, 0, 65536);
2007-09-02 12:57:37 +00:00
2008-04-16 18:32:17 +00:00
/* copy prolog */
2007-09-02 12:57:37 +00:00
2008-04-16 18:32:17 +00:00
for (i = 0; i < sizeof(prolog) / sizeof(char*); i++)
2007-09-02 12:57:37 +00:00
{
2008-04-16 18:32:17 +00:00
if (config_get_property(conffile,
prolog[i], property) != -1)
2008-04-21 19:49:13 +00:00
config_set_property(configuration, prolog[i], property);
2007-09-02 12:57:37 +00:00
}
2008-04-16 18:32:17 +00:00
/* get kernel properties */
kernel_ondisk = NULL;
ramdisk_ondisk = NULL;
current = 0;
while(1)
2007-09-02 12:57:37 +00:00
{
2008-04-16 18:32:17 +00:00
res = config_get_property(conffile + current,
"title", property);
if (res == -1)
break;
2007-09-02 12:57:37 +00:00
2008-04-16 18:32:17 +00:00
config_add_property(configuration, "title", property);
2008-04-16 18:32:17 +00:00
if (verbose)
printf("title %s\n", property);
2008-04-16 18:32:17 +00:00
current += res;
current = config_get_next_property(conffile, current,
NULL, NULL);
has_root = 0;
2008-04-16 18:32:17 +00:00
for (i = 0; i < sizeof(known_properties) / sizeof(char*); i++)
2007-09-02 12:57:37 +00:00
{
2008-04-16 18:32:17 +00:00
res = config_get_indexed_property(conffile,
"title",
property,
known_properties[i],
property2);
if (res == -1)
continue;
if (strcmp(known_properties[i], "root") == 0)
{
has_root = 1;
if (verbose)
printf(" %s %s\n",
known_properties[i], property2);
config_set_indexed_property(configuration,
"title", property,
known_properties[i],
property2);
} else if (has_root || emile_is_url(property2))
2007-09-02 12:57:37 +00:00
{
if (verbose)
2008-04-16 18:32:17 +00:00
printf(" %s %s\n",
known_properties[i], property2);
2007-09-02 12:57:37 +00:00
config_set_indexed_property(configuration,
2008-04-16 18:32:17 +00:00
"title", property,
known_properties[i],
property2);
2007-09-02 12:57:37 +00:00
}
else
{
2008-04-16 18:32:17 +00:00
char *url;
if (strcmp(known_properties[i], "kernel") == 0)
{
if (kernel_ondisk == NULL)
kernel_ondisk =
emile_floppy_add(fd,
property2);
url = kernel_ondisk;
} else if (strcmp(known_properties[i], "initrd") == 0)
2008-04-16 18:32:17 +00:00
{
if (ramdisk_ondisk == NULL)
ramdisk_ondisk =
emile_floppy_add(fd,
property2);
url = ramdisk_ondisk;
} else
url = property2;
2007-09-02 12:57:37 +00:00
config_set_indexed_property(configuration,
2008-04-16 18:32:17 +00:00
"title", property,
known_properties[i], url);
2007-09-02 12:57:37 +00:00
if (verbose)
2008-04-16 18:32:17 +00:00
printf(" %s %s (%s)\n",
known_properties[i],
property2, url);
2007-09-02 12:57:37 +00:00
}
}
2008-04-16 18:32:17 +00:00
}
2007-09-02 12:57:37 +00:00
if (ramdisk_ondisk != NULL)
free(ramdisk_ondisk);
if (kernel_ondisk != NULL)
free(kernel_ondisk);
2007-09-08 23:09:14 +00:00
if (strlen((char*)configuration) > 1023)
2007-09-02 21:00:05 +00:00
{
int tmpfd;
char *conf;
char *tmpfile = strdup("/tmp/emile-install-XXXXXX");
fprintf(stderr, "WARNING: configuration doesn't fit in second level\n"
" will store it at end of floppy\n");
tmpfd = mkstemp(tmpfile);
2007-09-08 23:09:14 +00:00
write(tmpfd, configuration, strlen((char*)configuration) + 1);
2007-09-02 21:00:05 +00:00
close(tmpfd);
conf = emile_floppy_add(fd, tmpfile);
free(configuration);
unlink(tmpfile);
free(tmpfile);
configuration = malloc(1024);
memset(configuration, 0, 1024);
2007-09-02 21:00:05 +00:00
config_set_property(configuration, "configuration", conf);
if (verbose)
printf("configuration %s\n", conf);
}
2007-09-02 12:57:37 +00:00
emile_second_set_configuration(fd, configuration);
emile_floppy_close(fd);
2008-04-16 18:32:17 +00:00
free(conffile);
2007-09-02 12:57:37 +00:00
free(configuration);
return 0;
}
2004-06-01 21:56:28 +00:00
int main(int argc, char** argv)
{
int verbose = 0;
int option_index = 0;
2004-06-01 21:56:28 +00:00
char* first_level = NULL;
char* second_level = NULL;
char* kernel_image = NULL;
char* ramdisk = NULL;
char* image = NULL;
2007-08-26 19:46:39 +00:00
char* config_path = NULL;
2004-12-25 00:48:29 +00:00
int action_getinfo = 0;
int c;
2004-06-01 21:56:28 +00:00
int ret;
while(1)
{
2007-08-26 19:46:39 +00:00
c = getopt_long(argc, argv, "hvf:s:k:r:gc:", long_options,
&option_index);
if (c == -1)
break;
switch(c)
{
case ARG_HELP:
usage(argc, argv);
return 0;
case ARG_VERBOSE:
verbose = 1;
break;
case ARG_FIRST:
first_level = optarg;
break;
case ARG_SECOND:
second_level = optarg;
break;
case ARG_KERNEL:
kernel_image = optarg;
break;
case ARG_RAMDISK:
ramdisk = optarg;
break;
2004-12-25 00:48:29 +00:00
case ARG_GETINFO:
action_getinfo = 1;
break;
2007-08-26 19:46:39 +00:00
case ARG_CONFIG:
config_path = optarg;
break;
}
}
if (optind < argc)
image = argv[optind];
if (image == NULL)
2004-06-01 21:56:28 +00:00
{
fprintf(stderr,
"ERROR: you must provide an image file or a block device.\n");
2004-06-01 21:56:28 +00:00
usage(argc, argv);
return 1;
}
2004-12-25 00:48:29 +00:00
if (action_getinfo)
2007-09-02 12:46:37 +00:00
return get_info(image, verbose);
2004-12-25 00:48:29 +00:00
2007-08-26 19:46:39 +00:00
if (config_path != NULL)
2008-04-16 18:32:17 +00:00
{
if (kernel_image || ramdisk)
{
fprintf(stderr, "ERROR: don't use --kernel"
" or --ramdisk with --config\n");
return 4;
}
2007-09-02 12:57:37 +00:00
return set_config(image, verbose, config_path,
first_level, second_level);
2008-04-16 18:32:17 +00:00
}
2007-08-26 19:46:39 +00:00
if (first_level == NULL)
first_level = PREFIX "/lib/emile/first_floppy";
if (second_level == NULL)
second_level = PREFIX "/lib/emile/second_floppy";
if (kernel_image == NULL)
kernel_image = PREFIX "/boot/vmlinuz";
if (verbose)
{
printf("first: %s\n", first_level);
printf("second: %s\n", second_level);
printf("kernel: %s\n", kernel_image);
printf("ramdisk: %s\n", ramdisk);
printf("image: %s\n", image);
}
2004-12-10 00:28:35 +00:00
ret = emile_floppy_create_image(first_level, second_level,
2005-11-28 00:19:54 +00:00
kernel_image, ramdisk, image);
switch(ret)
{
case 0:
break;
case EEMILE_CANNOT_WRITE_FIRST:
fprintf(stderr, "ERROR: cannot write first\n");
break;
case EEMILE_CANNOT_WRITE_SECOND:
fprintf(stderr, "ERROR: cannot write second\n");
break;
case EEMILE_CANNOT_WRITE_KERNEL:
fprintf(stderr, "ERROR: cannot write kernel\n");
break;
case EEMILE_CANNOT_WRITE_RAMDISK:
fprintf(stderr, "ERROR: cannot write ramdisk\n");
break;
case EEMILE_CANNOT_WRITE_PAD:
fprintf(stderr, "ERROR: cannot write padding\n");
break;
case EEMILE_CANNOT_CREATE_IMAGE:
fprintf(stderr, "ERROR: cannot create image\n");
break;
case EEMILE_CANNOT_OPEN_FILE:
fprintf(stderr, "ERROR: cannot open one of provided files\n");
break;
default:
fprintf(stderr, "ERROR: unknown error :-P\n");
break;
}
2004-06-01 21:56:28 +00:00
return ret;
}