2004-12-03 00:29:29 +00:00
|
|
|
/*
|
|
|
|
*
|
2013-09-05 12:39:22 +00:00
|
|
|
* (c) 2004-2007 Laurent Vivier <Laurent@Vivier.EU>
|
2004-12-03 00:29:29 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#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>
|
2007-05-06 23:03:55 +00:00
|
|
|
#include <libgen.h>
|
2004-12-03 00:29:29 +00:00
|
|
|
|
2007-09-02 20:31:24 +00:00
|
|
|
#include <libconfig.h>
|
|
|
|
|
2004-12-10 00:28:35 +00:00
|
|
|
#include "libemile.h"
|
2007-10-04 20:55:52 +00:00
|
|
|
#include "libmap.h"
|
2007-10-09 19:29:13 +00:00
|
|
|
#include "device.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);
|
|
|
|
|
2007-09-02 20:31:24 +00:00
|
|
|
static enum {
|
2004-12-27 21:39:19 +00:00
|
|
|
ACTION_NONE = 0x00000000,
|
|
|
|
ACTION_SCANBUS = 0x00000001,
|
|
|
|
ACTION_SET_HFS = 0x00000002,
|
|
|
|
ACTION_RESTORE = 0x00000004,
|
|
|
|
ACTION_BACKUP = 0x00000008,
|
2007-05-11 12:21:58 +00:00
|
|
|
ACTION_TEST = 0x00000010,
|
|
|
|
ACTION_CONFIG = 0x00000020,
|
2008-04-21 19:50:25 +00:00
|
|
|
ACTION_NO_FS = 0x00000040,
|
2007-09-02 20:31:24 +00:00
|
|
|
} action = ACTION_NONE;
|
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-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_HELP = 'h',
|
2007-05-11 12:21:58 +00:00
|
|
|
ARG_CONFIG = 'c',
|
2008-04-21 19:50:25 +00:00
|
|
|
ARG_NO_FS = 'n',
|
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
|
|
|
{"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 },
|
2007-05-11 12:21:58 +00:00
|
|
|
{"verbose", 0, NULL, ARG_VERBOSE },
|
|
|
|
{"help", 0, NULL, ARG_HELP },
|
2004-12-23 23:24:06 +00:00
|
|
|
{"test", 0, NULL, ARG_TEST },
|
2007-05-11 12:21:58 +00:00
|
|
|
{"config", 1, NULL, ARG_CONFIG },
|
2008-04-21 19:50:25 +00:00
|
|
|
{"no-fs", 0, NULL, ARG_NO_FS },
|
2004-12-23 23:24:06 +00:00
|
|
|
{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");
|
2008-07-21 21:54:41 +00:00
|
|
|
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," --restore[=FILE] restore boot block from FILE\n");
|
|
|
|
fprintf(stderr," --backup[=FILE] save current boot block to FILE\n");
|
|
|
|
fprintf(stderr," --set-hfs "
|
|
|
|
"set type of partition DEV to Apple_HFS\n"
|
|
|
|
" (needed to be bootable)\n");
|
|
|
|
fprintf(stderr," -c, --config FILE use config file FILE\n");
|
|
|
|
fprintf(stderr," -n, --no-fs "
|
|
|
|
"don't use EMILE embededed filesystems\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,
|
2007-10-04 20:55:52 +00:00
|
|
|
map_t **map, int *partition)
|
2004-12-22 00:29:25 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
int disk;
|
|
|
|
char disk_name[16];
|
2006-06-01 23:36:57 +00:00
|
|
|
int driver;
|
2007-10-09 19:29:13 +00:00
|
|
|
device_io_t device;
|
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)
|
2006-06-01 23:36:57 +00:00
|
|
|
return -2;
|
2004-12-23 22:35:36 +00:00
|
|
|
|
2006-06-01 23:36:57 +00:00
|
|
|
emile_get_dev_name(disk_name, driver, disk, 0);
|
2004-12-22 00:29:25 +00:00
|
|
|
|
2007-10-09 19:29:13 +00:00
|
|
|
device_sector_size = 512;
|
|
|
|
device.data = (void*)device_open(disk_name, flags);
|
2014-05-31 08:28:11 +00:00
|
|
|
device.write_sector = (stream_write_sector_t)device_write_sector;
|
2007-10-09 19:29:13 +00:00
|
|
|
device.read_sector = (stream_read_sector_t)device_read_sector;
|
|
|
|
device.close = (stream_close_t)device_close;
|
2007-10-10 16:47:03 +00:00
|
|
|
device.get_blocksize = (stream_get_blocksize_t)device_get_blocksize;
|
2007-10-09 19:29:13 +00:00
|
|
|
|
|
|
|
*map = map_open(&device);
|
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)
|
|
|
|
{
|
2007-10-04 20:55:52 +00:00
|
|
|
map_t *map;
|
2004-12-23 22:35:36 +00:00
|
|
|
int partition;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = open_map_of(dev_name, O_RDONLY, &map, &partition);
|
2006-06-01 23:36:57 +00:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2004-12-22 00:29:25 +00:00
|
|
|
|
2007-10-04 20:55:52 +00:00
|
|
|
ret = map_has_apple_driver(map);
|
|
|
|
map_close(map);
|
2004-12-22 00:29:25 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int check_is_hfs(char *dev_name)
|
|
|
|
{
|
2007-10-04 20:55:52 +00:00
|
|
|
map_t *map;
|
2004-12-22 00:29:25 +00:00
|
|
|
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);
|
2006-06-01 23:36:57 +00:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2004-12-22 00:29:25 +00:00
|
|
|
|
2007-10-04 20:55:52 +00:00
|
|
|
ret = map_read(map, partition - 1);
|
2004-12-22 00:29:25 +00:00
|
|
|
if (ret == -1)
|
|
|
|
return -1;
|
|
|
|
|
2007-10-04 20:55:52 +00:00
|
|
|
part_type = map_get_partition_type(map);
|
2004-12-22 00:29:25 +00:00
|
|
|
ret = (strcmp("Apple_HFS", part_type) == 0);
|
|
|
|
|
2007-10-04 20:55:52 +00:00
|
|
|
map_close(map);
|
2004-12-22 00:29:25 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2007-10-04 20:55:52 +00:00
|
|
|
map_t *map;
|
2004-12-22 23:14:07 +00:00
|
|
|
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);
|
2006-06-01 23:36:57 +00:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2004-12-22 23:14:07 +00:00
|
|
|
|
2007-10-04 20:55:52 +00:00
|
|
|
ret = map_read(map, partition - 1);
|
2004-12-22 23:14:07 +00:00
|
|
|
if (ret == -1)
|
|
|
|
return -1;
|
|
|
|
|
2007-10-04 20:55:52 +00:00
|
|
|
ret = map_bootblock_read(map, bootblock);
|
2004-12-22 23:14:07 +00:00
|
|
|
if (ret == -1)
|
|
|
|
return -1;
|
|
|
|
|
2007-10-04 20:55:52 +00:00
|
|
|
bootblock_type = map_bootblock_get_type(bootblock);
|
2004-12-22 23:14:07 +00:00
|
|
|
|
2007-10-04 20:55:52 +00:00
|
|
|
map_close(map);
|
2004-12-22 23:14:07 +00:00
|
|
|
|
|
|
|
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
|
|
|
{
|
2007-10-04 20:55:52 +00:00
|
|
|
map_t *map;
|
2004-12-23 22:35:36 +00:00
|
|
|
int ret;
|
|
|
|
int partition;
|
|
|
|
char bootblock[BOOTBLOCK_SIZE];
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
ret = open_map_of(dev_name, O_RDONLY, &map, &partition);
|
2006-06-01 23:36:57 +00:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2004-12-23 22:35:36 +00:00
|
|
|
|
2007-10-04 20:55:52 +00:00
|
|
|
ret = map_read(map, partition - 1);
|
2004-12-23 22:35:36 +00:00
|
|
|
if (ret == -1)
|
|
|
|
return -1;
|
|
|
|
|
2007-10-04 20:55:52 +00:00
|
|
|
ret = map_bootblock_read(map, bootblock);
|
2004-12-23 22:35:36 +00:00
|
|
|
if (ret == -1)
|
|
|
|
return -1;
|
|
|
|
|
2007-10-04 20:55:52 +00:00
|
|
|
map_close(map);
|
2004-12-23 22:35:36 +00:00
|
|
|
|
|
|
|
/* 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
|
|
|
{
|
2007-10-04 20:55:52 +00:00
|
|
|
map_t *map;
|
2004-12-23 23:24:06 +00:00
|
|
|
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);
|
2006-06-01 23:36:57 +00:00
|
|
|
if (ret < 0)
|
2004-12-23 23:24:06 +00:00
|
|
|
return -1;
|
|
|
|
|
2007-10-04 20:55:52 +00:00
|
|
|
ret = map_read(map, partition - 1);
|
2004-12-23 23:24:06 +00:00
|
|
|
if (ret == -1)
|
|
|
|
return -1;
|
|
|
|
|
2007-10-04 20:55:52 +00:00
|
|
|
ret = map_bootblock_write(map, bootblock);
|
2004-12-23 23:24:06 +00:00
|
|
|
if (ret == -1)
|
|
|
|
return -1;
|
|
|
|
|
2007-10-04 20:55:52 +00:00
|
|
|
map_close(map);
|
2004-12-23 23:24:06 +00:00
|
|
|
|
|
|
|
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
|
|
|
{
|
2007-10-04 20:55:52 +00:00
|
|
|
map_t *map;
|
2004-12-23 23:48:35 +00:00
|
|
|
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);
|
2006-06-01 23:36:57 +00:00
|
|
|
if (ret < 0)
|
2004-12-23 23:48:35 +00:00
|
|
|
return -1;
|
|
|
|
|
2007-10-04 20:55:52 +00:00
|
|
|
ret = map_read(map, partition - 1);
|
2004-12-24 00:10:33 +00:00
|
|
|
if (ret == -1)
|
|
|
|
return -1;
|
|
|
|
|
2007-10-04 20:55:52 +00:00
|
|
|
ret = map_bootblock_write(map, bootblock);
|
2004-12-23 23:48:35 +00:00
|
|
|
if (ret == -1)
|
|
|
|
return -1;
|
|
|
|
|
2007-10-04 20:55:52 +00:00
|
|
|
map_close(map);
|
2004-12-23 23:48:35 +00:00
|
|
|
|
|
|
|
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
|
|
|
{
|
2007-10-04 20:55:52 +00:00
|
|
|
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);
|
2006-06-01 23:36:57 +00:00
|
|
|
if (ret < 0)
|
2004-12-27 21:39:19 +00:00
|
|
|
return -1;
|
|
|
|
|
2007-10-04 20:55:52 +00:00
|
|
|
ret = map_read(map, partition - 1);
|
2004-12-23 23:48:35 +00:00
|
|
|
if (ret == -1)
|
|
|
|
return -1;
|
|
|
|
|
2007-10-04 20:55:52 +00:00
|
|
|
ret = map_set_partition_type(map, "Apple_HFS");
|
2004-12-27 21:39:19 +00:00
|
|
|
if (ret == -1)
|
|
|
|
return -1;
|
2004-12-23 23:48:35 +00:00
|
|
|
|
2007-10-04 20:55:52 +00:00
|
|
|
ret = map_partition_set_bootable(map, 1);
|
2004-12-23 23:48:35 +00:00
|
|
|
if (ret == -1)
|
|
|
|
return -1;
|
|
|
|
|
2007-10-04 20:55:52 +00:00
|
|
|
ret = map_write(map, partition - 1);
|
2004-12-27 21:39:19 +00:00
|
|
|
if (ret == -1)
|
|
|
|
return -1;
|
|
|
|
|
2007-10-04 20:55:52 +00:00
|
|
|
map_close(map);
|
2004-12-27 21:39:19 +00:00
|
|
|
|
2004-12-23 23:48:35 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-09-02 20:31:24 +00:00
|
|
|
static char *get_map_name(char *filename)
|
2004-12-07 00:39:39 +00:00
|
|
|
{
|
2007-09-02 20:31:24 +00:00
|
|
|
char *a, *b;
|
|
|
|
char *base, *dir;
|
|
|
|
char *map_name;
|
|
|
|
|
|
|
|
a = strdup(filename);
|
|
|
|
base = basename(a);
|
|
|
|
|
|
|
|
b = strdup(filename);
|
|
|
|
dir = dirname(b);
|
|
|
|
|
|
|
|
map_name = (char*)malloc(strlen(filename) + 6);
|
|
|
|
if (map_name == NULL)
|
|
|
|
return NULL;
|
|
|
|
sprintf(map_name, "%s/.%s.map", dir, base);
|
|
|
|
free(a);
|
|
|
|
free(b);
|
|
|
|
|
|
|
|
return map_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-09-08 23:09:14 +00:00
|
|
|
static int add_file(int8_t *configuration,
|
2007-09-02 20:31:24 +00:00
|
|
|
char *index, char *property, char *path, char *map_path)
|
|
|
|
{
|
|
|
|
struct emile_container *container;
|
|
|
|
unsigned short unit_id;
|
|
|
|
char map_info[64];
|
|
|
|
|
|
|
|
if (emile_is_url(path))
|
|
|
|
{
|
|
|
|
if (verbose)
|
|
|
|
printf(" %s %s\n", property, path);
|
|
|
|
|
|
|
|
config_set_indexed_property(configuration,
|
|
|
|
"title", index,
|
|
|
|
property, path);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-09-03 19:39:01 +00:00
|
|
|
if ((action & ACTION_TEST) != 0)
|
|
|
|
{
|
|
|
|
map_path = tempnam(NULL, "emile-map_path-");
|
|
|
|
}
|
|
|
|
else if (map_path == NULL)
|
2007-09-02 20:31:24 +00:00
|
|
|
{
|
|
|
|
map_path = get_map_name(path);
|
|
|
|
if (map_path == NULL)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
map_path = strdup(map_path);
|
|
|
|
|
|
|
|
/* get block mapping of kernel in filesystem */
|
|
|
|
|
2007-09-03 19:39:01 +00:00
|
|
|
container = emile_second_create_mapfile(&unit_id, map_path, path);
|
|
|
|
if (container == NULL)
|
2007-09-02 20:31:24 +00:00
|
|
|
{
|
2007-09-03 19:39:01 +00:00
|
|
|
free(map_path);
|
|
|
|
return -1;
|
2007-09-02 20:31:24 +00:00
|
|
|
}
|
|
|
|
|
2007-09-03 19:39:01 +00:00
|
|
|
sprintf(map_info,
|
|
|
|
"container:(sd%d)0x%x,0x%x", unit_id,
|
|
|
|
container->blocks[0].offset,
|
|
|
|
container->blocks[0].count);
|
|
|
|
|
2007-09-02 20:31:24 +00:00
|
|
|
free(container);
|
|
|
|
|
|
|
|
if (verbose)
|
2008-07-28 22:53:26 +00:00
|
|
|
printf(" %s %s (%s = %s)\n", property, path, map_path, map_info);
|
2007-09-02 20:31:24 +00:00
|
|
|
|
|
|
|
config_set_indexed_property(configuration,
|
|
|
|
"title", index,
|
|
|
|
property, map_info);
|
|
|
|
|
2007-09-03 19:39:01 +00:00
|
|
|
if ((action & ACTION_TEST) != 0)
|
2007-09-03 19:55:52 +00:00
|
|
|
fprintf(stderr, "WARNING: test mode, you must remove manually %s\n", map_path);
|
2007-09-03 19:39:01 +00:00
|
|
|
|
2007-09-02 20:31:24 +00:00
|
|
|
free(map_path);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-04-21 19:50:25 +00:00
|
|
|
static int8_t *set_config_no_fs(char *config_path)
|
|
|
|
{
|
|
|
|
int8_t *conffile;
|
|
|
|
int fd;
|
|
|
|
int i;
|
|
|
|
int current;
|
|
|
|
int ret;
|
|
|
|
struct stat st;
|
|
|
|
int8_t *configuration;
|
|
|
|
char property[1024];
|
|
|
|
char property2[1024];
|
|
|
|
char property3[1024];
|
|
|
|
static char *prolog[] = {
|
|
|
|
"gestaltID",
|
|
|
|
"default",
|
|
|
|
"timeout",
|
|
|
|
"vga",
|
|
|
|
"modem",
|
|
|
|
"printer"
|
|
|
|
};
|
|
|
|
|
|
|
|
configuration = malloc(65536);
|
|
|
|
if (configuration == NULL)
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
|
|
|
"ERROR: cannot allocate memory for configuration\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-07-28 22:53:26 +00:00
|
|
|
memset(configuration, 0, 65536);
|
2008-04-21 19:50:25 +00:00
|
|
|
|
|
|
|
/* open configuration file */
|
|
|
|
|
|
|
|
fd = open(config_path, O_RDONLY);
|
|
|
|
if (fd == -1)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "ERROR: cannot open %s\n", config_path);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (fstat(fd, &st) == -1)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "ERROR: cannot fstat %s\n", config_path);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
conffile = (int8_t*)malloc(st.st_size);
|
|
|
|
if (conffile == NULL)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "ERROR: cannot malloc() %s\n", config_path);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (read(fd, conffile, st.st_size) != st.st_size)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "ERROR: cannot read() %s\n", config_path);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
/* copy prolog */
|
|
|
|
|
|
|
|
for (i = 0; i < sizeof(prolog) / sizeof(char*); i++)
|
|
|
|
{
|
|
|
|
if (config_get_property(conffile,
|
|
|
|
prolog[i],
|
|
|
|
property) != -1)
|
|
|
|
config_set_property(configuration, prolog[i], property);
|
|
|
|
}
|
|
|
|
|
|
|
|
current = 0;
|
|
|
|
while (1) {
|
|
|
|
ret = config_get_property(conffile + current,
|
|
|
|
"title", property);
|
|
|
|
if (ret == -1)
|
|
|
|
break;
|
|
|
|
config_add_property(configuration, "title", property);
|
|
|
|
if (verbose)
|
|
|
|
printf("title %s\n", property);
|
|
|
|
|
|
|
|
current += ret;
|
|
|
|
current = config_get_next_property(conffile, current,
|
|
|
|
NULL, NULL);
|
|
|
|
|
|
|
|
ret = config_get_indexed_property(conffile,
|
|
|
|
"title",
|
|
|
|
property,
|
|
|
|
"chainloader",
|
|
|
|
property2);
|
|
|
|
if (ret != -1)
|
|
|
|
{
|
|
|
|
if (emile_is_url(property2))
|
|
|
|
{
|
|
|
|
config_set_indexed_property(configuration,
|
|
|
|
"title", property,
|
|
|
|
"chainloader", property2);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
unsigned short unit_id;
|
|
|
|
struct emile_container *container;
|
|
|
|
struct stat st;
|
|
|
|
char *chainloader;
|
|
|
|
|
|
|
|
fd = open(property2, O_RDONLY);
|
|
|
|
if (fd == -1)
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
|
|
|
"ERROR: cannot open %s\n",
|
|
|
|
property2);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
fstat(fd, &st);
|
|
|
|
|
|
|
|
container = malloc(
|
|
|
|
sizeof(struct emile_container) +
|
|
|
|
sizeof(struct emile_block));
|
|
|
|
if (container == NULL)
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
|
|
|
"ERROR: cannot malloc container"
|
|
|
|
"\n");
|
|
|
|
close(fd);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
ret = emile_scsi_create_container(fd,
|
|
|
|
&unit_id,
|
|
|
|
container,
|
|
|
|
2);
|
|
|
|
close(fd);
|
|
|
|
if (ret == -1)
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
|
|
|
"ERROR: cannot create container"
|
|
|
|
"\n");
|
|
|
|
free(container);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
chainloader = malloc(32);
|
|
|
|
if (chainloader == NULL)
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
|
|
|
"ERROR: cannot malloc chainloader\n");
|
|
|
|
free(container);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
sprintf(chainloader,
|
|
|
|
"block:(sd%d)0x%x,0x%lx", unit_id,
|
|
|
|
container->blocks[0].offset,
|
|
|
|
st.st_size);
|
|
|
|
free(container);
|
|
|
|
config_set_indexed_property(configuration,
|
|
|
|
"title", property,
|
|
|
|
"chainloader", chainloader);
|
|
|
|
free(chainloader);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = config_get_indexed_property(conffile,
|
|
|
|
"title",
|
|
|
|
property,
|
|
|
|
"kernel",
|
|
|
|
property2);
|
|
|
|
if (ret != -1)
|
|
|
|
{
|
|
|
|
ret = config_get_indexed_property(conffile,
|
|
|
|
"title",
|
|
|
|
property,
|
|
|
|
"kernel_map",
|
|
|
|
property3);
|
|
|
|
|
|
|
|
ret = add_file(configuration, property,
|
|
|
|
"kernel", property2,
|
|
|
|
ret == -1 ? NULL : property3);
|
|
|
|
if (ret == -1)
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
|
|
|
"ERROR: cannot add kernel %s\n",
|
|
|
|
property2);
|
|
|
|
free(conffile);
|
|
|
|
free(configuration);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
fprintf(stderr,
|
|
|
|
"WARNING: missing kernel entry for %s\n",
|
|
|
|
property);
|
|
|
|
|
|
|
|
ret = config_get_indexed_property(conffile,
|
|
|
|
"title",
|
|
|
|
property,
|
|
|
|
"initrd",
|
|
|
|
property2);
|
|
|
|
if (ret != -1)
|
|
|
|
{
|
|
|
|
ret = config_get_indexed_property(conffile,
|
|
|
|
"title",
|
|
|
|
property,
|
|
|
|
"initrd_map",
|
|
|
|
property3);
|
|
|
|
|
|
|
|
ret = add_file(configuration, property,
|
|
|
|
"initrd", property2,
|
|
|
|
ret == -1 ? NULL : property3);
|
|
|
|
if (ret == -1)
|
|
|
|
{
|
|
|
|
free(configuration);
|
|
|
|
fprintf(stderr,
|
|
|
|
"ERROR: cannot add initrd %s\n",
|
|
|
|
property3);
|
|
|
|
fprintf(stderr,
|
|
|
|
"ERROR: missing kernel entry for %s\n", property);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = config_get_indexed_property(conffile,
|
|
|
|
"title",
|
|
|
|
property,
|
|
|
|
"args",
|
|
|
|
property2);
|
|
|
|
if (ret != -1)
|
|
|
|
{
|
|
|
|
config_set_indexed_property(configuration,
|
|
|
|
"title", property,
|
|
|
|
"args", property2);
|
|
|
|
if (verbose)
|
|
|
|
printf(" args %s\n", property2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strlen((char*)configuration) > 1023)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
char* bootconfig = "/boot/emile/.bootconfig";
|
|
|
|
|
|
|
|
/* do not fit in second paramstring */
|
|
|
|
|
|
|
|
fd = creat(bootconfig, S_IWUSR);
|
|
|
|
if (fd == -1)
|
|
|
|
{
|
|
|
|
free(configuration);
|
|
|
|
fprintf(stderr,
|
|
|
|
"ERROR: cannot create /boot/emile/.bootconfig\n");
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
write(fd, configuration, strlen((char*)configuration) + 1);
|
|
|
|
close(fd);
|
|
|
|
free(configuration);
|
|
|
|
|
|
|
|
configuration = malloc(1024);
|
|
|
|
if (configuration == NULL)
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
|
|
|
"ERROR: cannot allocate memory for configuration\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
ret = add_file(configuration, NULL,
|
|
|
|
"configuration", bootconfig, NULL);
|
|
|
|
if (ret == -1)
|
|
|
|
{
|
|
|
|
free(configuration);
|
|
|
|
fprintf(stderr,
|
|
|
|
"ERROR: cannot add %s to configuration\n", bootconfig);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return configuration;
|
|
|
|
}
|
|
|
|
|
2008-04-16 18:32:17 +00:00
|
|
|
static int8_t *set_config(char *config_path)
|
2007-09-02 20:31:24 +00:00
|
|
|
{
|
2007-09-08 23:09:14 +00:00
|
|
|
int8_t *configuration;
|
2008-07-29 00:03:47 +00:00
|
|
|
int driver, disk, partition;
|
|
|
|
int fd, ret;
|
|
|
|
|
|
|
|
fd = open(config_path, O_RDONLY);
|
|
|
|
if (fd == -1)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "ERROR: cannot open %s\n", config_path);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
ret = emile_scsi_get_dev(fd, &driver, &disk, &partition);
|
|
|
|
close(fd);
|
|
|
|
if (ret == -1)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "cannot find partition and disk of %s\n",
|
|
|
|
config_path);
|
|
|
|
return NULL;
|
|
|
|
}
|
2007-09-02 20:31:24 +00:00
|
|
|
|
2008-04-16 18:32:17 +00:00
|
|
|
configuration = (int8_t*)malloc(65536);
|
2007-09-02 20:31:24 +00:00
|
|
|
if (configuration == NULL)
|
2007-09-05 12:54:01 +00:00
|
|
|
{
|
|
|
|
fprintf(stderr,
|
|
|
|
"ERROR: cannot allocate memory for configuration\n");
|
2007-09-02 20:31:24 +00:00
|
|
|
return NULL;
|
2007-09-05 12:54:01 +00:00
|
|
|
}
|
2008-07-28 22:53:26 +00:00
|
|
|
memset(configuration, 0, 65536);
|
|
|
|
|
2008-07-29 00:03:47 +00:00
|
|
|
sprintf((char*)configuration, "configuration (sd%d,%d)%s\n",
|
|
|
|
disk, partition - 1, config_path);
|
2008-07-28 22:53:26 +00:00
|
|
|
if (verbose)
|
2008-07-29 00:03:47 +00:00
|
|
|
printf(" configuration (sd%d,%d)%s\n",
|
|
|
|
disk, partition - 1, config_path);
|
2007-09-05 12:54:01 +00:00
|
|
|
|
2007-09-02 20:31:24 +00:00
|
|
|
return configuration;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2008-04-16 18:32:17 +00:00
|
|
|
char property[1024];
|
2007-09-02 20:31:24 +00:00
|
|
|
char *backup_path = PREFIX "/boot/emile/bootblock.backup";
|
|
|
|
char *config_path = PREFIX "/boot/emile/emile.conf";
|
|
|
|
char *first_path;
|
|
|
|
char *second_path;
|
2004-12-22 00:29:25 +00:00
|
|
|
int ret;
|
2004-12-16 23:27:50 +00:00
|
|
|
int c;
|
2008-04-16 18:32:17 +00:00
|
|
|
char *partition;
|
2004-12-16 23:27:50 +00:00
|
|
|
int option_index = 0;
|
2007-09-02 20:31:24 +00:00
|
|
|
int drive, second, size;
|
|
|
|
int fd;
|
2007-09-08 23:09:14 +00:00
|
|
|
int8_t *configuration;
|
2008-04-16 18:32:17 +00:00
|
|
|
int8_t *config;
|
|
|
|
struct stat st;
|
2004-12-16 23:27:50 +00:00
|
|
|
|
|
|
|
while(1)
|
|
|
|
{
|
2007-05-11 12:21:58 +00:00
|
|
|
c = getopt_long(argc, argv, "vhtc:", 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_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;
|
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;
|
|
|
|
break;
|
|
|
|
case ARG_TEST:
|
2004-12-27 21:39:19 +00:00
|
|
|
action |= ACTION_TEST;
|
2004-12-22 00:29:25 +00:00
|
|
|
break;
|
2007-05-11 12:21:58 +00:00
|
|
|
case ARG_CONFIG:
|
|
|
|
action |= ACTION_CONFIG;
|
|
|
|
config_path = optarg;
|
|
|
|
break;
|
2008-04-21 19:50:25 +00:00
|
|
|
case ARG_NO_FS:
|
|
|
|
action |= ACTION_NO_FS;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2007-05-11 12:21:58 +00:00
|
|
|
/* read config file */
|
2004-12-27 21:39:19 +00:00
|
|
|
|
2008-04-16 18:32:17 +00:00
|
|
|
fd = open(config_path, O_RDONLY);
|
|
|
|
if (fd == -1)
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
|
|
|
"ERROR: cannot open config file %s\n", config_path);
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
if (fstat(fd, &st) == -1)
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
|
|
|
"ERROR: cannot fstat file %s\n", config_path);
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
config = (int8_t*)malloc(st.st_size);
|
2007-05-11 12:21:58 +00:00
|
|
|
if (config == NULL)
|
2004-12-22 00:29:25 +00:00
|
|
|
{
|
2008-04-16 18:32:17 +00:00
|
|
|
fprintf(stderr,
|
|
|
|
"ERROR: cannot malloc %s\n", config_path);
|
2007-05-11 12:21:58 +00:00
|
|
|
return 2;
|
2004-12-27 21:39:19 +00:00
|
|
|
}
|
2008-04-16 18:32:17 +00:00
|
|
|
if (read(fd, config, st.st_size) != st.st_size)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "ERROR: cannot read() %s\n", config_path);
|
|
|
|
return 5;
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
/* get partition */
|
2004-12-27 21:39:19 +00:00
|
|
|
|
2008-04-16 18:32:17 +00:00
|
|
|
ret = config_get_property(config, "partition", property);
|
2007-05-11 12:21:58 +00:00
|
|
|
if (ret == -1)
|
2004-12-23 23:24:06 +00:00
|
|
|
{
|
2007-05-11 12:21:58 +00:00
|
|
|
fprintf(stderr, "ERROR: you must specify in %s a partition to set\n"
|
|
|
|
" EMILE bootblock\n", config_path);
|
2004-12-23 23:24:06 +00:00
|
|
|
fprintf(stderr,
|
|
|
|
" you can have the list of available partitions with \"--scanbus\".\n");
|
2008-04-16 18:32:17 +00:00
|
|
|
free(config);
|
2004-12-23 23:24:06 +00:00
|
|
|
return 3;
|
|
|
|
}
|
2008-04-16 18:32:17 +00:00
|
|
|
partition = strdup(property);
|
2004-12-23 23:24:06 +00:00
|
|
|
|
2004-12-27 21:39:19 +00:00
|
|
|
if (action & ACTION_RESTORE)
|
|
|
|
{
|
2007-05-09 19:26:48 +00:00
|
|
|
char* new_name;
|
|
|
|
|
2007-05-11 12:21:58 +00:00
|
|
|
if (action & ~ACTION_RESTORE)
|
2004-12-27 21:39:19 +00:00
|
|
|
{
|
|
|
|
fprintf(stderr,
|
|
|
|
"ERROR: \"--restore\" cannot be used with other arguments\n");
|
2008-04-16 18:32:17 +00:00
|
|
|
free(config);
|
2004-12-27 21:39:19 +00:00
|
|
|
return 13;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = restore_bootblock(partition, backup_path);
|
|
|
|
if (ret == -1)
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
|
|
|
"ERROR: cannot restore bootblock %s from %s\n",
|
|
|
|
partition, backup_path);
|
2008-04-16 18:32:17 +00:00
|
|
|
free(config);
|
2004-12-27 21:39:19 +00:00
|
|
|
return 14;
|
|
|
|
}
|
|
|
|
printf("Bootblock restore successfully done.\n");
|
|
|
|
|
2007-05-09 19:26:48 +00:00
|
|
|
/* rename backup file to .old */
|
|
|
|
|
|
|
|
new_name = (char*)malloc(strlen(backup_path) + 4 + 1);
|
|
|
|
|
|
|
|
sprintf(new_name, "%s.old", new_name);
|
|
|
|
|
|
|
|
unlink(new_name);
|
|
|
|
rename(backup_path, new_name);
|
|
|
|
|
|
|
|
free(new_name);
|
|
|
|
|
2008-04-16 18:32:17 +00:00
|
|
|
free(config);
|
2004-12-27 21:39:19 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-05-11 12:21:58 +00:00
|
|
|
ret = check_has_apple_driver(partition);
|
|
|
|
if (ret != -2)
|
2004-12-23 23:24:06 +00:00
|
|
|
{
|
2007-05-11 12:21:58 +00:00
|
|
|
if (ret == -1)
|
2004-12-27 21:39:19 +00:00
|
|
|
{
|
2007-05-11 12:21:58 +00:00
|
|
|
fprintf(stderr, "ERROR: cannot check if Apple_Driver exists\n");
|
|
|
|
fprintf(stderr, " you should try as root\n");
|
|
|
|
if ((action & ACTION_TEST) == 0)
|
|
|
|
{
|
2008-04-16 18:32:17 +00:00
|
|
|
free(config);
|
2007-05-11 12:21:58 +00:00
|
|
|
return 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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");
|
|
|
|
if ((action & ACTION_TEST) == 0)
|
|
|
|
{
|
2008-04-16 18:32:17 +00:00
|
|
|
free(config);
|
2007-05-11 12:21:58 +00:00
|
|
|
return 5;
|
|
|
|
}
|
2004-12-27 21:39:19 +00:00
|
|
|
}
|
|
|
|
|
2007-05-11 12:21:58 +00:00
|
|
|
ret = check_is_hfs(partition);
|
2004-12-27 21:39:19 +00:00
|
|
|
if (ret == -1)
|
|
|
|
{
|
2007-05-11 12:21:58 +00:00
|
|
|
fprintf(stderr,
|
|
|
|
"ERROR: cannot check if partition is Apple_HFS\n");
|
|
|
|
fprintf(stderr, " you should try as root\n");
|
|
|
|
if ((action & ACTION_TEST) == 0)
|
|
|
|
{
|
2008-04-16 18:32:17 +00:00
|
|
|
free(config);
|
2007-05-11 12:21:58 +00:00
|
|
|
return 6;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( (ret == 0) && !(action & ACTION_SET_HFS) )
|
|
|
|
{
|
|
|
|
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");
|
|
|
|
if ((action & ACTION_TEST) == 0)
|
|
|
|
{
|
2008-04-16 18:32:17 +00:00
|
|
|
free(config);
|
2007-05-11 12:21:58 +00:00
|
|
|
return 7;
|
|
|
|
}
|
2004-12-27 21:39:19 +00:00
|
|
|
}
|
2004-12-22 23:14:07 +00:00
|
|
|
|
2007-05-11 12:21:58 +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");
|
|
|
|
if ((action & ACTION_TEST) == 0)
|
|
|
|
{
|
2008-04-16 18:32:17 +00:00
|
|
|
free(config);
|
2007-05-11 12:21:58 +00:00
|
|
|
return 8;
|
|
|
|
}
|
|
|
|
}
|
2006-06-01 23:36:57 +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)
|
2007-05-11 12:21:58 +00:00
|
|
|
{
|
2008-04-16 18:32:17 +00:00
|
|
|
free(config);
|
2004-12-23 23:24:06 +00:00
|
|
|
return 9;
|
2007-05-11 12:21:58 +00:00
|
|
|
}
|
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");
|
2008-04-16 18:32:17 +00:00
|
|
|
free(config);
|
2004-12-23 23:24:06 +00:00
|
|
|
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);
|
2008-04-16 18:32:17 +00:00
|
|
|
free(config);
|
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
|
|
|
}
|
|
|
|
|
2008-04-16 18:32:17 +00:00
|
|
|
ret = config_get_property(config, "first_level", property);
|
2014-06-01 21:52:03 +00:00
|
|
|
if (ret == -1) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"ERROR: missing first_level in config file\n");
|
2007-05-11 12:21:58 +00:00
|
|
|
return 2;
|
2014-06-01 21:52:03 +00:00
|
|
|
}
|
2008-04-16 18:32:17 +00:00
|
|
|
first_path = strdup(property);
|
2007-05-11 12:21:58 +00:00
|
|
|
|
2008-04-16 18:32:17 +00:00
|
|
|
ret = config_get_property(config, "second_level", property);
|
2014-06-01 21:52:03 +00:00
|
|
|
if (ret == -1) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"ERROR: missing second_level in config file\n");
|
2007-05-11 12:21:58 +00:00
|
|
|
return 2;
|
2014-06-01 21:52:03 +00:00
|
|
|
}
|
2008-04-16 18:32:17 +00:00
|
|
|
second_path = strdup(property);
|
2007-05-11 12:21:58 +00:00
|
|
|
|
2007-09-02 20:31:24 +00:00
|
|
|
fd = open(first_path, O_RDONLY);
|
|
|
|
if (fd == -1)
|
2007-05-11 12:21:58 +00:00
|
|
|
{
|
2007-09-02 20:31:24 +00:00
|
|
|
fprintf(stderr,
|
|
|
|
"ERROR: cannot open \"%s\".\n", first_path);
|
2008-04-16 18:32:17 +00:00
|
|
|
free(config);
|
2007-09-02 20:31:24 +00:00
|
|
|
return 20;
|
2007-05-11 12:21:58 +00:00
|
|
|
}
|
|
|
|
|
2007-09-02 20:31:24 +00:00
|
|
|
ret = emile_first_get_param(fd, &drive, &second, &size);
|
2004-12-13 02:12:27 +00:00
|
|
|
|
2007-09-02 20:31:24 +00:00
|
|
|
close(fd);
|
2004-12-07 00:39:39 +00:00
|
|
|
|
2008-04-21 19:50:25 +00:00
|
|
|
if ((action & ACTION_NO_FS) == 0)
|
|
|
|
configuration = set_config(config_path);
|
2008-07-28 22:53:26 +00:00
|
|
|
else
|
|
|
|
configuration = set_config_no_fs(config_path);
|
2007-09-02 20:31:24 +00:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
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
|
|
|
{
|
2007-09-02 20:31:24 +00:00
|
|
|
/* set configuration in second level */
|
2004-12-13 02:12:27 +00:00
|
|
|
|
2007-09-02 20:31:24 +00:00
|
|
|
fd = open(second_path, O_RDWR);
|
2014-06-01 21:52:03 +00:00
|
|
|
if (fd == -1) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"ERROR: cannot open \"%s\".\n", second_path);
|
|
|
|
return 30;
|
|
|
|
}
|
2007-09-02 20:31:24 +00:00
|
|
|
emile_second_set_configuration(fd, configuration);
|
|
|
|
close(fd);
|
2004-12-13 02:12:27 +00:00
|
|
|
|
2007-09-02 20:31:24 +00:00
|
|
|
/* set second info in first level */
|
2004-12-26 20:12:44 +00:00
|
|
|
|
2007-09-02 20:31:24 +00:00
|
|
|
fd = open(first_path, O_RDWR);
|
2004-12-13 02:12:27 +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);
|
2008-04-16 18:32:17 +00:00
|
|
|
free(first_path);
|
|
|
|
free(second_path);
|
|
|
|
free(config);
|
2008-02-25 21:49:18 +00:00
|
|
|
free(configuration);
|
2005-11-29 23:30:50 +00:00
|
|
|
return 21;
|
2004-12-23 23:24:06 +00:00
|
|
|
}
|
2008-04-16 18:32:17 +00:00
|
|
|
free(second_path);
|
2004-12-23 23:24:06 +00:00
|
|
|
|
2007-09-02 20:31:24 +00:00
|
|
|
close(fd);
|
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));
|
2008-04-16 18:32:17 +00:00
|
|
|
free(first_path);
|
|
|
|
free(config);
|
2008-02-25 21:49:18 +00:00
|
|
|
free(configuration);
|
2005-11-29 23:30:50 +00:00
|
|
|
return 22;
|
2004-12-23 23:24:06 +00:00
|
|
|
}
|
2008-04-16 18:32:17 +00:00
|
|
|
free(first_path);
|
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);
|
2008-04-16 18:32:17 +00:00
|
|
|
free(config);
|
2008-02-25 21:49:18 +00:00
|
|
|
free(configuration);
|
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
|
|
|
|
2008-04-16 18:32:17 +00:00
|
|
|
free(partition);
|
|
|
|
free(config);
|
2008-02-25 21:49:18 +00:00
|
|
|
free(configuration);
|
2004-12-03 00:29:29 +00:00
|
|
|
return 0;
|
|
|
|
}
|