EMILE/tools/emile-set-cmdline.c

170 lines
3.2 KiB
C
Raw Normal View History

2004-05-19 00:09:58 +00:00
/*
*
* (c) 2004 Laurent Vivier <Laurent@lvivier.info>
2004-05-19 00:09:58 +00:00
*
*/
#include <stdio.h>
#include <unistd.h>
2006-06-02 11:45:10 +00:00
#include <getopt.h>
2004-05-19 00:09:58 +00:00
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
2004-05-21 10:42:54 +00:00
#include <stdlib.h>
2004-05-26 23:05:35 +00:00
#include <string.h>
2004-05-19 00:09:58 +00:00
2004-12-10 00:28:35 +00:00
#include "libemile.h"
2004-05-19 00:09:58 +00:00
2006-06-02 11:45:10 +00:00
enum {
ARG_NONE = 0,
ARG_HELP ='h',
ARG_KERNEL = 'k',
ARG_RAMDISK = 'r',
ARG_APPEND = 'a',
ARG_GETINFO = 'g',
};
static struct option long_options[] =
{
{"help", 0, NULL, ARG_HELP },
{"kernel", 1, NULL, ARG_KERNEL },
{"ramdisk", 1, NULL, ARG_RAMDISK },
{"append", 1, NULL, ARG_APPEND },
{"getinfo", 1, NULL, ARG_GETINFO },
{NULL, 0, NULL, 0 },
};
2004-05-19 00:09:58 +00:00
static void usage(int argc, char** argv)
{
2006-06-02 11:45:10 +00:00
fprintf(stderr, "Usage: %s [OPTION] <file>\n", argv[0]);
fprintf(stderr, "Configure second level to load kernel and ramdisk\n");
fprintf(stderr, " <file> can be a file or a device (/dev/fd0)\n");
fprintf(stderr, " -h, --help display this text\n");
fprintf(stderr, " -k, --kernel set kernel path\n");
fprintf(stderr, " -r, --ramdisk set ramdisk path\n");
fprintf(stderr, " -a, --append set kernel command line\n");
fprintf(stderr, " -g, --getinfo get second level information\n");
2004-06-03 11:09:28 +00:00
fprintf(stderr, "\nbuild: \n%s\n", SIGNATURE);
2004-05-19 00:09:58 +00:00
}
2006-06-02 11:45:10 +00:00
static int set_cmdline(char* image, char *kernel, char* cmdline, char *ramdisk)
2004-05-19 00:09:58 +00:00
{
int fd;
int ret;
int drive, second, size;
2004-05-21 10:42:54 +00:00
2004-12-10 00:28:35 +00:00
fd = open(image, O_RDWR);
2004-05-19 00:09:58 +00:00
if (fd == -1)
{
2004-05-21 10:42:54 +00:00
perror("Cannot open image file");
2004-05-19 00:09:58 +00:00
return 2;
}
ret = emile_first_get_param(fd, &drive, &second, &size);
if (ret == EEMILE_UNKNOWN_FIRST)
return -1;
2006-06-02 11:45:10 +00:00
ret = emile_second_set_param(fd, kernel, cmdline, ramdisk);
2004-12-10 00:28:35 +00:00
close(fd);
2005-11-28 20:56:06 +00:00
return 0;
2004-12-10 00:28:35 +00:00
}
int get_cmdline(char* image)
2004-12-10 00:28:35 +00:00
{
int fd;
int ret;
2006-06-02 11:45:10 +00:00
char kernel[255];
2004-12-10 00:28:35 +00:00
char cmdline[255];
2006-06-02 11:45:10 +00:00
char ramdisk[255];
2004-12-10 00:28:35 +00:00
fd = open(image, O_RDONLY);
if (fd == -1)
2004-05-19 00:09:58 +00:00
{
2004-12-10 00:28:35 +00:00
perror("Cannot open image file");
return 2;
2004-05-21 10:42:54 +00:00
}
2004-05-26 23:05:35 +00:00
2006-06-02 11:45:10 +00:00
ret = emile_second_get_param(fd, kernel, cmdline, ramdisk);
2006-06-02 11:45:10 +00:00
if (kernel[0])
printf("kernel path: \"%s\"\n", kernel);
else
fprintf(stderr, "No kernel path found\n");
if (cmdline[0])
2006-06-02 11:45:10 +00:00
printf("command line: \"%s\"\n", cmdline);
else
fprintf(stderr, "No command line found\n");
2006-06-02 11:45:10 +00:00
if (ramdisk[0])
printf("ramdisk path: \"%s\"\n", ramdisk);
else
fprintf(stderr, "No ramdisk path found\n");
2004-12-10 00:28:35 +00:00
2004-05-19 00:09:58 +00:00
close(fd);
2004-05-26 23:05:35 +00:00
return 0;
2004-05-19 00:09:58 +00:00
}
int main(int argc, char** argv)
{
int ret;
2006-06-02 11:45:10 +00:00
char *kernel = NULL;
char *cmdline = NULL;
char *ramdisk = NULL;
char *file = NULL;
int action_getinfo = 0;
int option_index;
int c;
while(1)
{
c = getopt_long(argc, argv, "ha:k:r:g", long_options,
&option_index);
if (c == -1)
break;
switch(c)
{
case ARG_HELP:
usage(argc, argv);
return 0;
case ARG_KERNEL:
kernel = optarg;
break;
case ARG_APPEND:
cmdline = optarg;
break;
case ARG_RAMDISK:
ramdisk = optarg;
break;
case ARG_GETINFO:
action_getinfo = 1;
break;
}
}
if (optind < argc)
file = argv[optind];
if (file == NULL)
{
fprintf(stderr,
"ERROR: you must provide an image file or a block device.\n");
usage(argc, argv);
return 1;
}
if ( action_getinfo && ( kernel || cmdline || ramdisk) )
{
fprintf(stderr, "Incompatible parameters\n");
return 1;
}
if (action_getinfo)
ret = get_cmdline(file);
2004-05-21 10:42:54 +00:00
else
2006-06-02 11:45:10 +00:00
ret = set_cmdline(file, kernel, cmdline, ramdisk);
2004-05-19 00:09:58 +00:00
return ret;
}