2004-12-03 00:29:29 +00:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* (c) 2004 Laurent Vivier <LaurentVivier@wanadoo.fr>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
2004-12-16 23:27:50 +00:00
|
|
|
#include <getopt.h>
|
2004-12-03 00:29:29 +00:00
|
|
|
|
2004-12-10 00:28:35 +00:00
|
|
|
#include "libemile.h"
|
2004-12-07 00:39:39 +00:00
|
|
|
|
2004-12-16 23:27:50 +00:00
|
|
|
int verbose = 0;
|
|
|
|
|
2004-12-22 00:29:25 +00:00
|
|
|
extern void scanbus(void);
|
|
|
|
|
2004-12-16 23:27:50 +00:00
|
|
|
static char *first_path = PREFIX "/boot/emile/first_scsi";
|
|
|
|
static char *second_path = PREFIX "/boot/emile/second_scsi";
|
|
|
|
static char *kernel_path = PREFIX "/boot/vmlinuz";
|
2005-11-29 23:30:50 +00:00
|
|
|
static char *map_path = NULL;
|
2004-12-22 23:14:07 +00:00
|
|
|
static char *backup_path = NULL;
|
2004-12-16 23:27:50 +00:00
|
|
|
static char *partition = NULL;
|
2004-12-23 23:24:06 +00:00
|
|
|
static char *append_string = NULL;
|
2004-12-16 23:27:50 +00:00
|
|
|
static int buffer_size = 0;
|
|
|
|
|
2004-12-27 21:39:19 +00:00
|
|
|
enum {
|
|
|
|
ACTION_NONE = 0x00000000,
|
|
|
|
ACTION_SCANBUS = 0x00000001,
|
|
|
|
ACTION_SET_HFS = 0x00000002,
|
|
|
|
ACTION_RESTORE = 0x00000004,
|
|
|
|
ACTION_BACKUP = 0x00000008,
|
2005-11-29 00:15:30 +00:00
|
|
|
ACTION_APPEND = 0x00000010,
|
2004-12-27 21:39:19 +00:00
|
|
|
ACTION_TEST = 0x00000020,
|
|
|
|
ACTION_FIRST = 0x00000040,
|
|
|
|
ACTION_SECOND = 0x00000080,
|
|
|
|
ACTION_KERNEL = 0x00000100,
|
|
|
|
ACTION_PARTITION = 0x00000200,
|
2005-11-29 23:30:50 +00:00
|
|
|
ACTION_MAP = 0x00000400,
|
2004-12-27 21:39:19 +00:00
|
|
|
};
|
|
|
|
|
2004-12-22 00:29:25 +00:00
|
|
|
enum {
|
2004-12-23 23:24:06 +00:00
|
|
|
ARG_NONE = 0,
|
|
|
|
ARG_SCANBUS,
|
|
|
|
ARG_SET_HFS,
|
2004-12-27 21:39:19 +00:00
|
|
|
ARG_RESTORE,
|
2004-12-23 23:24:06 +00:00
|
|
|
ARG_BACKUP,
|
2004-12-24 00:10:33 +00:00
|
|
|
ARG_APPEND = 'a',
|
2004-12-22 00:29:25 +00:00
|
|
|
ARG_VERBOSE ='v',
|
2004-12-24 00:10:33 +00:00
|
|
|
ARG_TEST = 't',
|
2004-12-22 00:29:25 +00:00
|
|
|
ARG_FIRST = 'f',
|
|
|
|
ARG_SECOND = 's',
|
|
|
|
ARG_KERNEL = 'k',
|
|
|
|
ARG_PARTITION = 'p',
|
|
|
|
ARG_HELP = 'h',
|
2005-11-29 23:30:50 +00:00
|
|
|
ARG_MAP = 'm',
|
2004-12-22 00:29:25 +00:00
|
|
|
};
|
|
|
|
|
2004-12-16 23:27:50 +00:00
|
|
|
static struct option long_options[] =
|
|
|
|
{
|
2004-12-23 23:24:06 +00:00
|
|
|
{"verbose", 0, NULL, ARG_VERBOSE },
|
|
|
|
{"first", 1, NULL, ARG_FIRST },
|
|
|
|
{"second", 1, NULL, ARG_SECOND },
|
|
|
|
{"kernel", 1, NULL, ARG_KERNEL },
|
2005-11-29 23:30:50 +00:00
|
|
|
{"map", 1, NULL, ARG_MAP },
|
2004-12-23 23:24:06 +00:00
|
|
|
{"partition", 1, NULL, ARG_PARTITION },
|
|
|
|
{"help", 0, NULL, ARG_HELP },
|
|
|
|
{"scanbus", 0, NULL, ARG_SCANBUS },
|
2004-12-27 01:32:03 +00:00
|
|
|
{"set-hfs", 0, NULL, ARG_SET_HFS },
|
2004-12-27 21:39:19 +00:00
|
|
|
{"restore", 2, NULL, ARG_RESTORE },
|
2004-12-23 23:24:06 +00:00
|
|
|
{"backup", 2, NULL, ARG_BACKUP },
|
|
|
|
{"test", 0, NULL, ARG_TEST },
|
|
|
|
{"append", 0, NULL, ARG_APPEND },
|
|
|
|
{NULL, 0, NULL, 0 }
|
2004-12-16 23:27:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static void usage(int argc, char** argv)
|
|
|
|
{
|
2004-12-24 00:10:33 +00:00
|
|
|
fprintf(stderr, "Usage: %s [OPTION]\n", argv[0]);
|
|
|
|
fprintf(stderr, "Update and install EMILE stuff on your SCSI disk.\n");
|
|
|
|
fprintf(stderr, "EMILE allows to boot linux directly from linux partition\n");
|
|
|
|
fprintf(stderr," -h, --help display this text\n");
|
|
|
|
fprintf(stderr," -v, --verbose active verbose mode\n");
|
|
|
|
fprintf(stderr," -t, --test active test mode (don't write to disk)\n");
|
|
|
|
fprintf(stderr," --scanbus display information about all disks and partitions\n");
|
|
|
|
fprintf(stderr," -f, --first PATH set path of EMILE first level\n");
|
|
|
|
fprintf(stderr," -s, --second PATH set path of EMILE second level\n");
|
|
|
|
fprintf(stderr," -k, --kernel PATH set path of kernel\n");
|
2005-11-29 23:44:48 +00:00
|
|
|
fprintf(stderr," -m, --map PATH set path to the EMILE kernel map file (generated)\n");
|
2004-12-24 00:10:33 +00:00
|
|
|
fprintf(stderr," -a, --append ARG set kernel command line\n");
|
|
|
|
fprintf(stderr," -p, --partition DEV define device where to install boot block\n");
|
2004-12-27 21:39:19 +00:00
|
|
|
fprintf(stderr," --restore[=FILE] save current boot block from FILE\n");
|
2004-12-24 00:10:33 +00:00
|
|
|
fprintf(stderr," --backup[=FILE] save current boot block to FILE\n");
|
|
|
|
fprintf(stderr," --set-hfs set type of partition DEV to Apple_HFS (needed to be bootable)\n");
|
|
|
|
fprintf(stderr, "\nUse \"--test\" to see default values\n");
|
|
|
|
fprintf(stderr, "!!! USE WITH CAUTION AND AT YOUR OWN RISK !!!\n");
|
|
|
|
|
2004-12-16 23:27:50 +00:00
|
|
|
fprintf(stderr, "\nbuild: \n%s\n", SIGNATURE);
|
|
|
|
}
|
|
|
|
|
2004-12-23 22:35:36 +00:00
|
|
|
static int open_map_of( char *dev_name, int flags,
|
|
|
|
emile_map_t **map, int *partition)
|
2004-12-22 00:29:25 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
int disk;
|
|
|
|
char disk_name[16];
|
2004-12-27 21:39:19 +00:00
|
|
|
char *driver;
|
2004-12-22 00:29:25 +00:00
|
|
|
|
2004-12-27 21:39:19 +00:00
|
|
|
ret = emile_scsi_get_rdev(dev_name, &driver, &disk, partition);
|
2004-12-23 22:35:36 +00:00
|
|
|
if (ret == -1)
|
|
|
|
return -1;
|
|
|
|
|
2004-12-27 21:39:19 +00:00
|
|
|
sprintf(disk_name, "%s%c", driver, 'a' + disk);
|
2004-12-22 00:29:25 +00:00
|
|
|
|
2004-12-23 22:35:36 +00:00
|
|
|
*map = emile_map_open(disk_name, flags);
|
2004-12-27 22:32:18 +00:00
|
|
|
if (*map == NULL)
|
|
|
|
return -1;
|
2004-12-23 22:35:36 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int check_has_apple_driver(char *dev_name)
|
|
|
|
{
|
|
|
|
emile_map_t *map;
|
|
|
|
int partition;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = open_map_of(dev_name, O_RDONLY, &map, &partition);
|
|
|
|
if (ret == -1)
|
2004-12-22 00:29:25 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
ret = emile_map_has_apple_driver(map);
|
|
|
|
emile_map_close(map);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int check_is_hfs(char *dev_name)
|
|
|
|
{
|
|
|
|
emile_map_t *map;
|
|
|
|
int ret;
|
|
|
|
int partition;
|
|
|
|
char *part_type;
|
|
|
|
|
2004-12-23 22:35:36 +00:00
|
|
|
ret = open_map_of(dev_name, O_RDONLY, &map, &partition);
|
|
|
|
if (ret == -1)
|
2004-12-22 00:29:25 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
ret = emile_map_read(map, partition - 1);
|
|
|
|
if (ret == -1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
part_type = emile_map_get_partition_type(map);
|
|
|
|
ret = (strcmp("Apple_HFS", part_type) == 0);
|
|
|
|
|
|
|
|
emile_map_close(map);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2004-12-13 02:12:27 +00:00
|
|
|
|
2004-12-22 23:14:07 +00:00
|
|
|
static int check_is_EMILE_bootblock(char *dev_name)
|
|
|
|
{
|
|
|
|
emile_map_t *map;
|
|
|
|
int ret;
|
|
|
|
int partition;
|
|
|
|
char bootblock[BOOTBLOCK_SIZE];
|
|
|
|
int bootblock_type;
|
|
|
|
|
2004-12-23 22:35:36 +00:00
|
|
|
ret = open_map_of(dev_name, O_RDONLY, &map, &partition);
|
|
|
|
if (ret == -1)
|
2004-12-22 23:14:07 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
ret = emile_map_read(map, partition - 1);
|
|
|
|
if (ret == -1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
ret = emile_map_bootblock_read(map, bootblock);
|
|
|
|
if (ret == -1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
bootblock_type = emile_map_bootblock_get_type(bootblock);
|
|
|
|
|
|
|
|
emile_map_close(map);
|
|
|
|
|
|
|
|
return EMILE_BOOTBLOCK == bootblock_type;
|
|
|
|
}
|
|
|
|
|
2004-12-23 22:35:36 +00:00
|
|
|
static int backup_bootblock(char *dev_name, char *filename)
|
2004-12-22 23:14:07 +00:00
|
|
|
{
|
2004-12-23 22:35:36 +00:00
|
|
|
emile_map_t *map;
|
|
|
|
int ret;
|
|
|
|
int partition;
|
|
|
|
char bootblock[BOOTBLOCK_SIZE];
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
ret = open_map_of(dev_name, O_RDONLY, &map, &partition);
|
|
|
|
if (ret == -1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
ret = emile_map_read(map, partition - 1);
|
|
|
|
if (ret == -1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
ret = emile_map_bootblock_read(map, bootblock);
|
|
|
|
if (ret == -1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
emile_map_close(map);
|
|
|
|
|
|
|
|
/* save bootblock */
|
|
|
|
|
|
|
|
fd = open(filename, O_CREAT | O_EXCL | O_WRONLY, S_IRUSR | S_IWUSR);
|
|
|
|
if (fd == -1)
|
|
|
|
{
|
|
|
|
if (errno == EEXIST)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "ERROR: \"%s\" already exists.\n",
|
|
|
|
filename);
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = write(fd, bootblock, BOOTBLOCK_SIZE);
|
|
|
|
if (ret != BOOTBLOCK_SIZE)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
return 0;
|
2004-12-22 23:14:07 +00:00
|
|
|
}
|
|
|
|
|
2004-12-27 21:39:19 +00:00
|
|
|
static int restore_bootblock(char *dev_name, char *filename)
|
2004-12-23 23:24:06 +00:00
|
|
|
{
|
|
|
|
emile_map_t *map;
|
|
|
|
int ret;
|
|
|
|
int partition;
|
|
|
|
char bootblock[BOOTBLOCK_SIZE];
|
|
|
|
int fd;
|
|
|
|
|
2004-12-27 21:39:19 +00:00
|
|
|
if (!check_is_EMILE_bootblock(dev_name))
|
|
|
|
{
|
|
|
|
fprintf(stderr, "ERROR: cannot restore bootblock over non-EMILE bootblock\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2004-12-23 23:24:06 +00:00
|
|
|
|
2004-12-27 21:39:19 +00:00
|
|
|
/* read bootblock */
|
|
|
|
|
|
|
|
fd = open(filename, O_RDONLY);
|
2004-12-23 23:24:06 +00:00
|
|
|
if (fd == -1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
ret = read(fd, bootblock, BOOTBLOCK_SIZE);
|
|
|
|
if (ret != BOOTBLOCK_SIZE)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
|
2004-12-27 21:39:19 +00:00
|
|
|
/* write bootblock */
|
2004-12-23 23:24:06 +00:00
|
|
|
|
2004-12-27 22:32:18 +00:00
|
|
|
ret = open_map_of(dev_name, O_RDWR, &map, &partition);
|
2004-12-23 23:24:06 +00:00
|
|
|
if (ret == -1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
ret = emile_map_read(map, partition - 1);
|
|
|
|
if (ret == -1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
ret = emile_map_bootblock_write(map, bootblock);
|
|
|
|
if (ret == -1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
emile_map_close(map);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2004-12-23 23:48:35 +00:00
|
|
|
|
2004-12-27 21:39:19 +00:00
|
|
|
static int copy_file_to_bootblock(char* first_path, char* dev_name)
|
2004-12-23 23:48:35 +00:00
|
|
|
{
|
|
|
|
emile_map_t *map;
|
|
|
|
int ret;
|
|
|
|
int partition;
|
2004-12-27 21:39:19 +00:00
|
|
|
char bootblock[BOOTBLOCK_SIZE];
|
|
|
|
int fd;
|
2004-12-23 23:48:35 +00:00
|
|
|
|
2004-12-27 21:39:19 +00:00
|
|
|
/* read first level */
|
|
|
|
|
|
|
|
fd = open(first_path, O_RDONLY);
|
|
|
|
if (fd == -1)
|
2004-12-23 23:48:35 +00:00
|
|
|
return -1;
|
|
|
|
|
2004-12-27 21:39:19 +00:00
|
|
|
ret = read(fd, bootblock, BOOTBLOCK_SIZE);
|
|
|
|
if (ret != BOOTBLOCK_SIZE)
|
2004-12-23 23:48:35 +00:00
|
|
|
return -1;
|
|
|
|
|
2004-12-27 21:39:19 +00:00
|
|
|
close(fd);
|
|
|
|
|
|
|
|
/* write bootblock to partition */
|
|
|
|
|
|
|
|
ret = open_map_of(dev_name, O_RDWR, &map, &partition);
|
2004-12-23 23:48:35 +00:00
|
|
|
if (ret == -1)
|
|
|
|
return -1;
|
|
|
|
|
2004-12-27 21:39:19 +00:00
|
|
|
ret = emile_map_read(map, partition - 1);
|
2004-12-24 00:10:33 +00:00
|
|
|
if (ret == -1)
|
|
|
|
return -1;
|
|
|
|
|
2004-12-27 21:39:19 +00:00
|
|
|
ret = emile_map_bootblock_write(map, bootblock);
|
2004-12-23 23:48:35 +00:00
|
|
|
if (ret == -1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
emile_map_close(map);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-12-27 21:39:19 +00:00
|
|
|
static int set_HFS(char *dev_name)
|
2004-12-23 23:48:35 +00:00
|
|
|
{
|
2004-12-27 21:39:19 +00:00
|
|
|
emile_map_t *map;
|
2004-12-23 23:48:35 +00:00
|
|
|
int ret;
|
|
|
|
int partition;
|
|
|
|
|
2004-12-27 21:39:19 +00:00
|
|
|
ret = open_map_of(dev_name, O_RDWR, &map, &partition);
|
|
|
|
if (ret == -1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
ret = emile_map_read(map, partition - 1);
|
2004-12-23 23:48:35 +00:00
|
|
|
if (ret == -1)
|
|
|
|
return -1;
|
|
|
|
|
2004-12-27 21:39:19 +00:00
|
|
|
ret = emile_map_set_partition_type(map, "Apple_HFS");
|
|
|
|
if (ret == -1)
|
|
|
|
return -1;
|
2004-12-23 23:48:35 +00:00
|
|
|
|
2004-12-27 21:39:19 +00:00
|
|
|
ret = emile_map_partition_set_bootable(map, 1);
|
2004-12-23 23:48:35 +00:00
|
|
|
if (ret == -1)
|
|
|
|
return -1;
|
|
|
|
|
2004-12-27 21:39:19 +00:00
|
|
|
ret = emile_map_write(map, partition - 1);
|
|
|
|
if (ret == -1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
emile_map_close(map);
|
|
|
|
|
2004-12-23 23:48:35 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-12-07 00:39:39 +00:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2004-12-22 00:29:25 +00:00
|
|
|
int ret;
|
2004-12-16 23:27:50 +00:00
|
|
|
int c;
|
|
|
|
int option_index = 0;
|
2004-12-22 00:29:25 +00:00
|
|
|
char tmp_partition[16];
|
2004-12-23 23:24:06 +00:00
|
|
|
char tmp_append[512];
|
|
|
|
int fd;
|
2004-12-27 21:39:19 +00:00
|
|
|
int action = ACTION_NONE;
|
2004-12-16 23:27:50 +00:00
|
|
|
|
|
|
|
while(1)
|
|
|
|
{
|
2004-12-24 00:10:33 +00:00
|
|
|
c = getopt_long(argc, argv, "vhtf:a:s:k:b:", long_options, &option_index);
|
2004-12-16 23:27:50 +00:00
|
|
|
if (c == -1)
|
|
|
|
break;
|
|
|
|
switch(c)
|
|
|
|
{
|
2004-12-22 00:29:25 +00:00
|
|
|
case ARG_VERBOSE:
|
2004-12-16 23:27:50 +00:00
|
|
|
verbose++;
|
|
|
|
break;
|
2004-12-22 00:29:25 +00:00
|
|
|
case ARG_HELP:
|
2004-12-16 23:27:50 +00:00
|
|
|
usage(argc, argv);
|
|
|
|
return 0;
|
2004-12-27 21:39:19 +00:00
|
|
|
case ARG_SCANBUS:
|
|
|
|
action |= ACTION_SCANBUS;
|
|
|
|
break;
|
2004-12-22 00:29:25 +00:00
|
|
|
case ARG_FIRST:
|
2004-12-27 21:39:19 +00:00
|
|
|
action |= ACTION_FIRST;
|
2004-12-16 23:27:50 +00:00
|
|
|
first_path = optarg;
|
|
|
|
break;
|
2004-12-22 00:29:25 +00:00
|
|
|
case ARG_SECOND:
|
2004-12-27 21:39:19 +00:00
|
|
|
action |= ACTION_SECOND;
|
2004-12-16 23:27:50 +00:00
|
|
|
second_path = optarg;
|
|
|
|
break;
|
2004-12-22 00:29:25 +00:00
|
|
|
case ARG_KERNEL:
|
2004-12-27 21:39:19 +00:00
|
|
|
action |= ACTION_KERNEL;
|
2004-12-16 23:27:50 +00:00
|
|
|
kernel_path = optarg;
|
|
|
|
break;
|
2005-11-29 23:30:50 +00:00
|
|
|
case ARG_MAP:
|
|
|
|
action |= ACTION_MAP;
|
|
|
|
map_path = optarg;
|
|
|
|
break;
|
2004-12-22 00:29:25 +00:00
|
|
|
case ARG_PARTITION:
|
2004-12-27 21:39:19 +00:00
|
|
|
action |= ACTION_PARTITION;
|
2004-12-22 00:29:25 +00:00
|
|
|
partition = optarg;
|
|
|
|
break;
|
|
|
|
case ARG_SET_HFS:
|
2004-12-27 21:39:19 +00:00
|
|
|
action |= ACTION_SET_HFS;
|
2004-12-22 00:29:25 +00:00
|
|
|
break;
|
2004-12-27 21:39:19 +00:00
|
|
|
case ARG_RESTORE:
|
|
|
|
action |= ACTION_RESTORE;
|
|
|
|
if (optarg != NULL)
|
|
|
|
backup_path = optarg;
|
|
|
|
else
|
|
|
|
backup_path = PREFIX "/boot/emile/bootblock.backup";
|
2004-12-22 00:29:25 +00:00
|
|
|
break;
|
2004-12-23 23:24:06 +00:00
|
|
|
case ARG_BACKUP:
|
2004-12-27 21:39:19 +00:00
|
|
|
action |= ACTION_BACKUP;
|
2004-12-22 23:14:07 +00:00
|
|
|
if (optarg != NULL)
|
|
|
|
backup_path = optarg;
|
|
|
|
else
|
|
|
|
backup_path = PREFIX "/boot/emile/bootblock.backup";
|
|
|
|
break;
|
2004-12-23 23:24:06 +00:00
|
|
|
case ARG_APPEND:
|
2004-12-27 21:39:19 +00:00
|
|
|
action |= ACTION_APPEND;
|
2004-12-23 23:24:06 +00:00
|
|
|
append_string = optarg;
|
|
|
|
break;
|
2004-12-22 23:14:07 +00:00
|
|
|
case ARG_TEST:
|
2004-12-27 21:39:19 +00:00
|
|
|
action |= ACTION_TEST;
|
2004-12-22 00:29:25 +00:00
|
|
|
break;
|
2004-12-27 01:32:03 +00:00
|
|
|
default:
|
|
|
|
fprintf(stderr, "ERROR: unknown option %s (%d, %c)\n",
|
|
|
|
argv[optind], c, c);
|
|
|
|
return 1;
|
2004-12-16 23:27:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-12-27 21:39:19 +00:00
|
|
|
if (action & ACTION_SCANBUS) {
|
2004-12-23 23:24:06 +00:00
|
|
|
|
2004-12-27 21:39:19 +00:00
|
|
|
if (action & ~ACTION_SCANBUS) {
|
2004-12-23 23:24:06 +00:00
|
|
|
fprintf(stderr,
|
2004-12-27 21:39:19 +00:00
|
|
|
"ERROR: \"--scanbus\" cannot be used with other arguments\n");
|
2004-12-23 23:24:06 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2004-12-16 23:27:50 +00:00
|
|
|
scanbus();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-12-27 21:39:19 +00:00
|
|
|
/* seek first HFS partition */
|
|
|
|
|
2004-12-22 00:29:25 +00:00
|
|
|
if (partition == NULL)
|
|
|
|
{
|
2004-12-23 23:24:06 +00:00
|
|
|
int fd;
|
2004-12-27 21:39:19 +00:00
|
|
|
char dev_name[16];
|
|
|
|
emile_map_t* map;
|
|
|
|
char *part_type;
|
|
|
|
int i;
|
|
|
|
char *driver;
|
|
|
|
int disk;
|
|
|
|
int partnb;
|
|
|
|
|
|
|
|
fd = open(second_path, O_RDONLY);
|
|
|
|
if (fd == -1)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "ERROR: cannot open \"%s\"\n",
|
|
|
|
second_path);
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = emile_scsi_get_dev(fd, &driver, &disk, &partnb);
|
|
|
|
if (ret == -1)
|
|
|
|
{
|
|
|
|
fprintf(stderr,"ERROR: cannot find device of \"%s\"\n",
|
|
|
|
second_path);
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
|
2004-12-27 22:32:18 +00:00
|
|
|
sprintf(dev_name, "%s%c", driver, disk + 'a');
|
2004-12-27 21:39:19 +00:00
|
|
|
|
|
|
|
/* ROM boots on the first HFS partition it finds */
|
|
|
|
|
|
|
|
map = emile_map_open(dev_name, O_RDONLY);
|
|
|
|
if (map == NULL)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "ERROR: cannot open partition map\n");
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < emile_map_get_number(map); i++)
|
|
|
|
{
|
|
|
|
ret = emile_map_read(map, i);
|
|
|
|
if (ret == -1)
|
|
|
|
break;
|
|
|
|
|
|
|
|
part_type = emile_map_get_partition_type(map);
|
|
|
|
if (strcmp(part_type, "Apple_HFS") == 0)
|
|
|
|
{
|
|
|
|
sprintf(tmp_partition, "%s%d", dev_name, i + 1);
|
|
|
|
partition = tmp_partition;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
emile_map_close(map);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if there is no HFS partition, we'll use the partition
|
|
|
|
* where there is second_scsi (/boot or /)
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (partition == NULL)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
char *driver;
|
|
|
|
int disk;
|
|
|
|
int partnb;
|
|
|
|
|
2004-12-23 23:24:06 +00:00
|
|
|
fd = open(second_path, O_RDONLY);
|
|
|
|
if (fd == -1)
|
|
|
|
return 2;
|
2004-12-27 21:39:19 +00:00
|
|
|
ret = emile_scsi_get_dev(fd, &driver, &disk, &partnb);
|
|
|
|
if (ret == 0)
|
|
|
|
{
|
|
|
|
sprintf(tmp_partition,
|
|
|
|
"%s%c%d", driver, disk + 'a', partnb);
|
2004-12-23 23:24:06 +00:00
|
|
|
partition = tmp_partition;
|
2004-12-27 21:39:19 +00:00
|
|
|
}
|
2004-12-23 23:24:06 +00:00
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (partition == NULL)
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
|
|
|
"ERROR: you must specify a partition to set EMILE bootblock\n");
|
|
|
|
fprintf(stderr,
|
|
|
|
" you can have the list of available partitions with \"--scanbus\".\n");
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
2004-12-27 21:39:19 +00:00
|
|
|
if (action & ACTION_RESTORE)
|
|
|
|
{
|
2004-12-27 22:32:18 +00:00
|
|
|
if (action & ~(ACTION_RESTORE | ACTION_PARTITION))
|
2004-12-27 21:39:19 +00:00
|
|
|
{
|
|
|
|
fprintf(stderr,
|
|
|
|
"ERROR: \"--restore\" cannot be used with other arguments\n");
|
|
|
|
return 13;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = restore_bootblock(partition, backup_path);
|
|
|
|
if (ret == -1)
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
|
|
|
"ERROR: cannot restore bootblock %s from %s\n",
|
|
|
|
partition, backup_path);
|
|
|
|
return 14;
|
|
|
|
}
|
|
|
|
printf("Bootblock restore successfully done.\n");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-12-23 23:24:06 +00:00
|
|
|
if (append_string == NULL)
|
|
|
|
{
|
2004-12-27 21:39:19 +00:00
|
|
|
char *driver;
|
|
|
|
int disk;
|
|
|
|
int partnb;
|
|
|
|
|
|
|
|
fd = open(second_path, O_RDONLY);
|
|
|
|
if (fd == -1)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "ERROR: cannot open \"%s\"\n",
|
|
|
|
second_path);
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = emile_scsi_get_dev(fd, &driver, &disk, &partnb);
|
|
|
|
if (ret == -1)
|
|
|
|
{
|
|
|
|
fprintf(stderr,"ERROR: cannot find device of \"%s\"\n",
|
|
|
|
second_path);
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
sprintf(tmp_append, "root=%s%c%d", driver, disk + 'a', partnb);
|
2004-12-23 23:24:06 +00:00
|
|
|
append_string = tmp_append;
|
2004-12-22 00:29:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = check_has_apple_driver(partition);
|
|
|
|
if (ret == -1)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "ERROR: cannot check if Apple_Driver exists\n");
|
2004-12-22 23:14:07 +00:00
|
|
|
fprintf(stderr, " you should try as root\n");
|
2004-12-27 21:39:19 +00:00
|
|
|
if ((action & ACTION_TEST) == 0)
|
2004-12-23 23:24:06 +00:00
|
|
|
return 4;
|
2004-12-22 00:29:25 +00:00
|
|
|
}
|
|
|
|
if (ret == 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
|
|
|
"ERROR: to be bootable a disk must have an Apple Driver on it\n");
|
|
|
|
fprintf(stderr,
|
|
|
|
" You must partition this disk with Apple Disk utility\n");
|
|
|
|
fprintf(stderr,
|
|
|
|
" or wait a release of EMILE allowing you to add this driver\n");
|
2004-12-27 21:39:19 +00:00
|
|
|
if ((action & ACTION_TEST) == 0)
|
2004-12-23 23:24:06 +00:00
|
|
|
return 5;
|
2004-12-22 00:29:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = check_is_hfs(partition);
|
|
|
|
if (ret == -1)
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
|
|
|
"ERROR: cannot check if partition is Apple_HFS\n");
|
2004-12-22 23:14:07 +00:00
|
|
|
fprintf(stderr, " you should try as root\n");
|
2004-12-27 21:39:19 +00:00
|
|
|
if ((action & ACTION_TEST) == 0)
|
2004-12-23 23:24:06 +00:00
|
|
|
return 6;
|
2004-12-22 00:29:25 +00:00
|
|
|
}
|
2004-12-27 21:39:19 +00:00
|
|
|
if ( (ret == 0) && (action & ACTION_SET_HFS) )
|
2004-12-22 00:29:25 +00:00
|
|
|
{
|
|
|
|
fprintf(stderr,
|
|
|
|
"ERROR: to be bootable a partition must be of type Apple_HFS\n");
|
|
|
|
fprintf(stderr,
|
|
|
|
" you can change it to Apple_HFS using \"--set-hfs\" argument\n");
|
2004-12-27 21:39:19 +00:00
|
|
|
if ((action & ACTION_TEST) == 0)
|
2004-12-23 23:24:06 +00:00
|
|
|
return 7;
|
2004-12-22 23:14:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = check_is_EMILE_bootblock(partition);
|
|
|
|
if (ret == -1)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "ERROR: cannot check bootblock type\n");
|
|
|
|
fprintf(stderr, " you should try as root\n");
|
2004-12-27 21:39:19 +00:00
|
|
|
if ((action & ACTION_TEST) == 0)
|
2004-12-23 23:24:06 +00:00
|
|
|
return 8;
|
2004-12-22 23:14:07 +00:00
|
|
|
}
|
2004-12-27 22:32:18 +00:00
|
|
|
if ( (ret == 0) && ((action & ACTION_BACKUP) == 0) )
|
2004-12-22 23:14:07 +00:00
|
|
|
{
|
|
|
|
fprintf(stderr,
|
|
|
|
"ERROR: there is already a bootblock on \"%s\"\n", partition);
|
|
|
|
fprintf(stderr,
|
2004-12-23 23:24:06 +00:00
|
|
|
" you must use \"--backup\" to save it\n");
|
2004-12-27 21:39:19 +00:00
|
|
|
if ((action & ACTION_TEST) == 0)
|
2004-12-23 23:24:06 +00:00
|
|
|
return 9;
|
2004-12-22 23:14:07 +00:00
|
|
|
}
|
|
|
|
|
2004-12-27 21:39:19 +00:00
|
|
|
if (action & ACTION_BACKUP)
|
2004-12-22 23:14:07 +00:00
|
|
|
{
|
2004-12-27 21:39:19 +00:00
|
|
|
if (action & ACTION_TEST)
|
2004-12-22 23:14:07 +00:00
|
|
|
{
|
|
|
|
fprintf(stderr,
|
2004-12-23 23:24:06 +00:00
|
|
|
"ERROR: \"--backup\" cannot be used with \"--test\"\n");
|
|
|
|
return 13;
|
2004-12-22 23:14:07 +00:00
|
|
|
}
|
|
|
|
|
2004-12-23 22:35:36 +00:00
|
|
|
ret = backup_bootblock(partition, backup_path);
|
2004-12-22 23:14:07 +00:00
|
|
|
if (ret == -1)
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
|
|
|
"ERROR: cannot backup bootblock %s to %s\n",
|
|
|
|
partition, backup_path);
|
2004-12-23 23:24:06 +00:00
|
|
|
return 14;
|
2004-12-22 23:14:07 +00:00
|
|
|
}
|
|
|
|
printf("Bootblock backup successfully done.\n");
|
2004-12-22 00:29:25 +00:00
|
|
|
}
|
|
|
|
|
2005-11-29 23:30:50 +00:00
|
|
|
if (map_path == NULL)
|
|
|
|
{
|
|
|
|
map_path = (char*)malloc(strlen(kernel_path) + 5);
|
|
|
|
if (map_path == NULL)
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
|
|
|
"ERROR: cannot allocate memory\n");
|
|
|
|
return 15;
|
|
|
|
}
|
|
|
|
sprintf(map_path, "%s.map", kernel_path);
|
|
|
|
}
|
|
|
|
|
2004-12-23 23:24:06 +00:00
|
|
|
printf("partition: %s\n", partition);
|
2004-12-16 23:27:50 +00:00
|
|
|
printf("first: %s\n", first_path);
|
|
|
|
printf("second: %s\n", second_path);
|
|
|
|
printf("kernel: %s\n", kernel_path);
|
2005-11-29 23:30:50 +00:00
|
|
|
printf("map file: %s\n", map_path);
|
2004-12-23 23:24:06 +00:00
|
|
|
printf("append: %s\n", append_string);
|
2004-12-16 23:27:50 +00:00
|
|
|
printf("buffer size: %d\n", buffer_size);
|
2004-12-13 02:12:27 +00:00
|
|
|
|
2004-12-26 20:12:44 +00:00
|
|
|
/* set kernel info into second level */
|
2004-12-07 00:39:39 +00:00
|
|
|
|
2004-12-26 20:12:44 +00:00
|
|
|
fd = open(second_path, O_RDWR);
|
|
|
|
if (fd == -1)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "ERROR: cannot open \"%s\"\n",
|
|
|
|
second_path);
|
2005-11-29 23:30:50 +00:00
|
|
|
return 16;
|
2004-12-26 20:12:44 +00:00
|
|
|
}
|
2004-12-13 02:12:27 +00:00
|
|
|
|
2004-12-27 21:39:19 +00:00
|
|
|
if ((action & ACTION_TEST) == 0)
|
2004-12-26 20:12:44 +00:00
|
|
|
{
|
2005-11-29 00:15:30 +00:00
|
|
|
char *configuration;
|
2005-11-29 23:30:50 +00:00
|
|
|
struct emile_container *container;
|
2005-12-01 22:16:06 +00:00
|
|
|
short unit_id;
|
2005-11-29 23:30:50 +00:00
|
|
|
char map_info[64];
|
2005-11-29 00:15:30 +00:00
|
|
|
|
2005-12-01 22:16:06 +00:00
|
|
|
container = emile_second_create_mapfile(&unit_id, map_path, kernel_path);
|
2005-11-29 23:30:50 +00:00
|
|
|
if (container == NULL)
|
2004-12-23 23:24:06 +00:00
|
|
|
{
|
|
|
|
fprintf(stderr,
|
|
|
|
"ERROR: cannot set \"%s\" information in \"%s\".\n",
|
2005-11-29 23:30:50 +00:00
|
|
|
kernel_path, map_path);
|
|
|
|
return 17;
|
2004-12-23 23:24:06 +00:00
|
|
|
}
|
2004-12-13 02:12:27 +00:00
|
|
|
|
2005-11-29 23:30:50 +00:00
|
|
|
/* set second configuration */
|
2004-12-13 02:12:27 +00:00
|
|
|
|
2004-12-23 23:24:06 +00:00
|
|
|
lseek(fd, 0, SEEK_SET);
|
2005-11-29 00:15:30 +00:00
|
|
|
configuration = emile_second_get_configuration(fd);
|
2004-12-13 02:12:27 +00:00
|
|
|
|
2005-11-29 23:30:50 +00:00
|
|
|
/* set kernel info */
|
|
|
|
|
2005-12-01 22:16:06 +00:00
|
|
|
sprintf(map_info, "container:(sd%d)0x%x,0x%x", unit_id,
|
2005-11-29 23:30:50 +00:00
|
|
|
container->blocks[0].offset, container->blocks[0].count);
|
|
|
|
emile_second_set_property(configuration, "kernel", map_info);
|
|
|
|
|
|
|
|
/* set cmdline */
|
|
|
|
|
2005-11-29 00:15:30 +00:00
|
|
|
emile_second_set_property(configuration, "parameters", append_string);
|
2004-12-03 00:29:29 +00:00
|
|
|
|
2005-11-29 23:30:50 +00:00
|
|
|
/* save configuration */
|
|
|
|
|
2004-12-23 23:24:06 +00:00
|
|
|
lseek(fd, 0, SEEK_SET);
|
2005-11-29 00:15:30 +00:00
|
|
|
ret = emile_second_set_configuration(fd, configuration);
|
|
|
|
if (ret != 0)
|
2004-12-23 23:24:06 +00:00
|
|
|
{
|
2005-11-29 00:15:30 +00:00
|
|
|
free(configuration);
|
2004-12-23 23:24:06 +00:00
|
|
|
fprintf(stderr,
|
2005-11-29 23:30:50 +00:00
|
|
|
"ERROR: cannot set configuration in %s\n", second_path);
|
|
|
|
return 19;
|
2004-12-23 23:24:06 +00:00
|
|
|
}
|
2005-11-29 00:15:30 +00:00
|
|
|
free(configuration);
|
2004-12-26 20:12:44 +00:00
|
|
|
}
|
2004-12-13 02:12:27 +00:00
|
|
|
|
2004-12-26 20:12:44 +00:00
|
|
|
close(fd);
|
2004-12-13 02:12:27 +00:00
|
|
|
|
2004-12-26 20:12:44 +00:00
|
|
|
/* set second info in first level */
|
|
|
|
|
|
|
|
fd = open(first_path, O_RDWR);
|
|
|
|
if (fd == -1)
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
|
|
|
"ERROR: cannot open \"%s\".\n", first_path);
|
2005-11-29 23:30:50 +00:00
|
|
|
return 20;
|
2004-12-26 20:12:44 +00:00
|
|
|
}
|
2004-12-13 02:12:27 +00:00
|
|
|
|
2004-12-27 21:39:19 +00:00
|
|
|
if ((action & ACTION_TEST) == 0)
|
2004-12-26 20:12:44 +00:00
|
|
|
{
|
2004-12-23 23:24:06 +00:00
|
|
|
ret = emile_first_set_param_scsi(fd, second_path);
|
|
|
|
if (ret == -1)
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
|
|
|
"ERROR: cannot set \"%s\" information into \"%s\".\n",
|
|
|
|
second_path, first_path);
|
2005-11-29 23:30:50 +00:00
|
|
|
return 21;
|
2004-12-23 23:24:06 +00:00
|
|
|
}
|
2004-12-26 20:12:44 +00:00
|
|
|
}
|
2004-12-23 23:24:06 +00:00
|
|
|
|
2004-12-26 20:12:44 +00:00
|
|
|
close(fd);
|
2004-12-23 23:24:06 +00:00
|
|
|
|
2004-12-27 21:39:19 +00:00
|
|
|
if ((action & ACTION_TEST) == 0)
|
2004-12-26 20:12:44 +00:00
|
|
|
{
|
2004-12-23 23:24:06 +00:00
|
|
|
/* copy first level to boot block */
|
|
|
|
|
|
|
|
ret = copy_file_to_bootblock(first_path, partition);
|
|
|
|
if (ret == -1)
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
|
|
|
"ERROR: cannot write \"%s\" to bootblock of \"%s\".\n",
|
|
|
|
first_path, partition);
|
|
|
|
fprintf(stderr,
|
|
|
|
" %s\n", strerror(errno));
|
2005-11-29 23:30:50 +00:00
|
|
|
return 22;
|
2004-12-23 23:24:06 +00:00
|
|
|
}
|
2004-12-23 23:48:35 +00:00
|
|
|
|
|
|
|
/* set HFS if needed */
|
|
|
|
|
2004-12-27 21:39:19 +00:00
|
|
|
if (action & ACTION_SET_HFS)
|
2004-12-23 23:48:35 +00:00
|
|
|
{
|
|
|
|
ret = set_HFS(partition);
|
|
|
|
if (ret == -1)
|
|
|
|
{
|
|
|
|
fprintf( stderr,
|
|
|
|
"ERROR: cannot set partition type of \"%s\" to Apple_HFS.\n"
|
|
|
|
, partition);
|
2005-11-29 23:30:50 +00:00
|
|
|
return 23;
|
2004-12-23 23:48:35 +00:00
|
|
|
}
|
|
|
|
}
|
2004-12-23 23:24:06 +00:00
|
|
|
}
|
2004-12-13 02:12:27 +00:00
|
|
|
|
2004-12-03 00:29:29 +00:00
|
|
|
return 0;
|
|
|
|
}
|