9
0
Fork 0

Merge branch 'for-next/bootspec'

This commit is contained in:
Sascha Hauer 2013-11-07 08:31:47 +01:00
commit 73ef216f0d
5 changed files with 344 additions and 229 deletions

View File

@ -21,6 +21,7 @@
#include <blspec.h>
#include <libgen.h>
#include <malloc.h>
#include <clock.h>
#include <boot.h>
#include <menu.h>
#include <fs.h>
@ -28,132 +29,9 @@
#include <linux/stat.h>
static int boot_script(char *path);
static int verbose;
static int dryrun;
static void bootsource_action(struct menu *m, struct menu_entry *me)
{
struct blspec_entry *be = container_of(me, struct blspec_entry, me);
int ret;
if (be->scriptpath) {
ret = boot_script(be->scriptpath);
} else {
if (IS_ENABLED(CONFIG_BLSPEC))
ret = blspec_boot(be, 0, 0);
else
ret = -ENOSYS;
}
if (ret)
printf("Booting failed with: %s\n", strerror(-ret));
printf("Press any key to continue\n");
read_key();
}
static int bootsources_menu_env_entries(struct blspec *blspec)
{
const char *path = "/env/boot";
DIR *dir;
struct dirent *d;
struct blspec_entry *be;
dir = opendir(path);
if (!dir)
return -errno;
while ((d = readdir(dir))) {
if (*d->d_name == '.')
continue;
be = blspec_entry_alloc(blspec);
be->me.type = MENU_ENTRY_NORMAL;
be->scriptpath = asprintf("/env/boot/%s", d->d_name);
be->me.display = xstrdup(d->d_name);
}
closedir(dir);
return 0;
}
static struct blspec *bootentries_collect(void)
{
struct blspec *blspec;
blspec = blspec_alloc();
blspec->menu->display = asprintf("boot");
bootsources_menu_env_entries(blspec);
if (IS_ENABLED(CONFIG_BLSPEC))
blspec_scan_devices(blspec);
return blspec;
}
static void bootsources_menu(void)
{
struct blspec *blspec = NULL;
struct blspec_entry *entry;
struct menu_entry *back_entry;
if (!IS_ENABLED(CONFIG_MENU)) {
printf("no menu support available\n");
return;
}
blspec = bootentries_collect();
blspec_for_each_entry(blspec, entry) {
entry->me.action = bootsource_action;
menu_add_entry(blspec->menu, &entry->me);
}
back_entry = xzalloc(sizeof(*back_entry));
back_entry->display = "back";
back_entry->type = MENU_ENTRY_NORMAL;
back_entry->non_re_ent = 1;
menu_add_entry(blspec->menu, back_entry);
menu_show(blspec->menu);
free(back_entry);
blspec_free(blspec);
}
static void bootsources_list(void)
{
struct blspec *blspec;
struct blspec_entry *entry;
blspec = bootentries_collect();
printf("\nBootscripts:\n\n");
printf("%-40s %-20s\n", "name", "title");
printf("%-40s %-20s\n", "----", "-----");
blspec_for_each_entry(blspec, entry) {
if (entry->scriptpath)
printf("%-40s %s\n", basename(entry->scriptpath), entry->me.display);
}
if (!IS_ENABLED(CONFIG_BLSPEC))
return;
printf("\nBootloader spec entries:\n\n");
printf("%-20s %-20s %s\n", "device", "hwdevice", "title");
printf("%-20s %-20s %s\n", "------", "--------", "-----");
blspec_for_each_entry(blspec, entry)
if (!entry->scriptpath)
printf("%s\n", entry->me.display);
blspec_free(blspec);
}
static int timeout;
/*
* Start a single boot script. 'path' is a full path to a boot script.
@ -166,8 +44,6 @@ static int boot_script(char *path)
.initrd_address = UIMAGE_SOME_ADDRESS,
};
printf("booting %s...\n", basename(path));
globalvar_set_match("linux.bootargs.dyn.", "");
globalvar_set_match("bootm.", "");
@ -194,79 +70,261 @@ out:
return ret;
}
/*
* boot a script. 'name' can either be a filename under /env/boot/,
* a full path to a boot script or a path to a directory. This function
* returns a negative error on failure, or 0 on a successful dryrun boot.
*/
static int boot(const char *name)
static int boot_entry(struct blspec_entry *be)
{
char *path;
DIR *dir;
struct dirent *d;
struct stat s;
int ret;
if (*name == '/')
path = xstrdup(name);
else
path = asprintf("/env/boot/%s", name);
ret = stat(path, &s);
if (ret) {
if (!IS_ENABLED(CONFIG_BLSPEC)) {
pr_err("%s: %s\n", path, strerror(-ret));
goto out;
}
ret = blspec_boot_hwdevice(name, verbose, dryrun);
pr_err("%s: %s\n", name, strerror(-ret));
goto out;
if (be->scriptpath) {
ret = boot_script(be->scriptpath);
} else {
if (IS_ENABLED(CONFIG_BLSPEC))
ret = blspec_boot(be, verbose, dryrun);
else
ret = -ENOSYS;
}
if (S_ISREG(s.st_mode)) {
ret = boot_script(path);
goto out;
return ret;
}
static void bootsource_action(struct menu *m, struct menu_entry *me)
{
struct blspec_entry *be = container_of(me, struct blspec_entry, me);
int ret;
ret = boot_entry(be);
if (ret)
printf("Booting failed with: %s\n", strerror(-ret));
printf("Press any key to continue\n");
read_key();
}
/*
* bootscript_create_entry - create a boot entry from a script name
*/
static int bootscript_create_entry(struct blspec *blspec, const char *name)
{
struct blspec_entry *be;
be = blspec_entry_alloc(blspec);
be->me.type = MENU_ENTRY_NORMAL;
be->scriptpath = xstrdup(name);
be->me.display = xstrdup(basename(be->scriptpath));
return 0;
}
/*
* bootscript_scan_path - create boot entries from a path
*
* path can either be a full path to a bootscript or a full path to a diretory
* containing bootscripts.
*/
static int bootscript_scan_path(struct blspec *blspec, const char *path)
{
struct stat s;
DIR *dir;
struct dirent *d;
int ret;
int found = 0;
ret = stat(path, &s);
if (ret)
return ret;
if (!S_ISDIR(s.st_mode)) {
ret = bootscript_create_entry(blspec, path);
if (ret)
return ret;
return 1;
}
dir = opendir(path);
if (!dir) {
ret = -errno;
printf("cannot open %s: %s\n", path, strerror(-errno));
goto out;
}
if (!dir)
return -errno;
while ((d = readdir(dir))) {
char *file;
struct stat s;
char *bootscript_path;
if (*d->d_name == '.')
continue;
file = asprintf("%s/%s", path, d->d_name);
ret = stat(file, &s);
if (ret) {
free(file);
continue;
}
if (!S_ISREG(s.st_mode)) {
free(file);
continue;
}
ret = boot_script(file);
free(file);
if (!ret)
break;
bootscript_path = asprintf("%s/%s", path, d->d_name);
bootscript_create_entry(blspec, bootscript_path);
found++;
free(bootscript_path);
}
ret = found;
closedir(dir);
out:
free(path);
return ret;
}
/*
* bootentry_parse_one - create boot entries from a name
*
* name can be:
* - a name of a boot script under /env/boot
* - a full path of a boot script
* - a device name
* - a cdev name
* - a full path of a directory containing bootloader spec entries
* - a full path of a directory containing bootscripts
*
* Returns the number of entries found or a negative error code.
*/
static int bootentry_parse_one(struct blspec *blspec, const char *name)
{
int found = 0, ret;
if (IS_ENABLED(CONFIG_BLSPEC)) {
ret = blspec_scan_devicename(blspec, name);
if (ret > 0)
found += ret;
ret = blspec_scan_directory(blspec, name);
if (ret > 0)
found += ret;
}
if (!found) {
char *path;
if (*name != '/')
path = asprintf("/env/boot/%s", name);
else
path = xstrdup(name);
ret = bootscript_scan_path(blspec, path);
if (ret > 0)
found += ret;
free(path);
}
return found;
}
/*
* bootentries_collect - collect bootentries from an array of names
*/
static struct blspec *bootentries_collect(char *entries[], int num_entries)
{
struct blspec *blspec;
int i;
blspec = blspec_alloc();
blspec->menu->display = asprintf("boot");
if (!num_entries)
bootscript_scan_path(blspec, "/env/boot");
if (IS_ENABLED(CONFIG_BLSPEC) && !num_entries)
blspec_scan_devices(blspec);
for (i = 0; i < num_entries; i++)
bootentry_parse_one(blspec, entries[i]);
return blspec;
}
/*
* bootsources_menu - show a menu from an array of names
*/
static void bootsources_menu(char *entries[], int num_entries)
{
struct blspec *blspec = NULL;
struct blspec_entry *entry;
struct menu_entry *back_entry;
if (!IS_ENABLED(CONFIG_MENU)) {
printf("no menu support available\n");
return;
}
blspec = bootentries_collect(entries, num_entries);
blspec_for_each_entry(blspec, entry) {
entry->me.action = bootsource_action;
menu_add_entry(blspec->menu, &entry->me);
}
back_entry = xzalloc(sizeof(*back_entry));
back_entry->display = "back";
back_entry->type = MENU_ENTRY_NORMAL;
back_entry->non_re_ent = 1;
menu_add_entry(blspec->menu, back_entry);
if (timeout >= 0)
blspec->menu->auto_select = timeout;
menu_show(blspec->menu);
free(back_entry);
blspec_free(blspec);
}
/*
* bootsources_list - list boot entries from an array of names
*/
static void bootsources_list(char *entries[], int num_entries)
{
struct blspec *blspec;
struct blspec_entry *entry;
blspec = bootentries_collect(entries, num_entries);
printf("%-20s %-20s %s\n", "device", "hwdevice", "title");
printf("%-20s %-20s %s\n", "------", "--------", "-----");
blspec_for_each_entry(blspec, entry) {
if (entry->scriptpath)
printf("%-40s %s\n", basename(entry->scriptpath), entry->me.display);
else
printf("%s\n", entry->me.display);
}
blspec_free(blspec);
}
/*
* boot a script or a bootspec entry. 'name' can be:
* - a filename under /env/boot/
* - a full path to a boot script
* - a device name
* - a partition name under /dev/
* - a full path to a directory which
* - contains boot scripts, or
* - contains a loader/entries/ directory containing bootspec entries
*
* Returns a negative error on failure, or 0 on a successful dryrun boot.
*/
static int boot(const char *name)
{
struct blspec *blspec;
struct blspec_entry *entry;
int ret = -ENOENT;
blspec = blspec_alloc();
ret = bootentry_parse_one(blspec, name);
if (ret < 0)
return ret;
if (!ret) {
printf("Nothing bootable found on %s\n", name);
return -ENOENT;
}
blspec_for_each_entry(blspec, entry) {
printf("booting %s\n", entry->me.display);
ret = boot_entry(entry);
if (!ret)
break;
printf("booting %s failed: %s\n", entry->me.display, strerror(-ret));
}
return ret;
}
@ -279,8 +337,9 @@ static int do_boot(int argc, char *argv[])
verbose = 0;
dryrun = 0;
timeout = -1;
while ((opt = getopt(argc, argv, "vldm")) > 0) {
while ((opt = getopt(argc, argv, "vldmt:")) > 0) {
switch (opt) {
case 'v':
verbose++;
@ -294,16 +353,19 @@ static int do_boot(int argc, char *argv[])
case 'm':
do_menu = 1;
break;
case 't':
timeout = simple_strtoul(optarg, NULL, 0);
break;
}
}
if (do_list) {
bootsources_list();
bootsources_list(&argv[optind], argc - optind);
return 0;
}
if (do_menu) {
bootsources_menu();
bootsources_menu(&argv[optind], argc - optind);
return 0;
}
@ -347,10 +409,14 @@ BAREBOX_CMD_HELP_START(boot)
BAREBOX_CMD_HELP_USAGE("boot [OPTIONS] [BOOTSRC...]\n")
BAREBOX_CMD_HELP_SHORT("Boot an operating system.\n")
BAREBOX_CMD_HELP_SHORT("[BOOTSRC...] can be:\n")
BAREBOX_CMD_HELP_SHORT("- a filename from /env/boot/\n")
BAREBOX_CMD_HELP_SHORT("- a full path to a file\n")
BAREBOX_CMD_HELP_SHORT("- a path to a directory. All files in this directory are treated\n")
BAREBOX_CMD_HELP_SHORT(" as boot scripts.\n")
BAREBOX_CMD_HELP_SHORT("- a filename under /env/boot/\n")
BAREBOX_CMD_HELP_SHORT("- a full path to a boot script\n")
BAREBOX_CMD_HELP_SHORT("- a device name\n")
BAREBOX_CMD_HELP_SHORT("- a partition name under /dev/\n")
BAREBOX_CMD_HELP_SHORT("- a full path to a directory which\n")
BAREBOX_CMD_HELP_SHORT(" - contains boot scripts, or\n")
BAREBOX_CMD_HELP_SHORT(" - contains a loader/entries/ directory containing bootspec entries\n")
BAREBOX_CMD_HELP_SHORT("\n")
BAREBOX_CMD_HELP_SHORT("Multiple bootsources may be given which are probed in order until\n")
BAREBOX_CMD_HELP_SHORT("one succeeds.\n")
BAREBOX_CMD_HELP_SHORT("\nOptions:\n")
@ -358,6 +424,7 @@ BAREBOX_CMD_HELP_OPT ("-v","Increase verbosity\n")
BAREBOX_CMD_HELP_OPT ("-d","Dryrun. See what happens but do no actually boot\n")
BAREBOX_CMD_HELP_OPT ("-l","List available boot sources\n")
BAREBOX_CMD_HELP_OPT ("-m","Show a menu with boot options\n")
BAREBOX_CMD_HELP_OPT ("-t <timeout>","specify timeout for the menu\n")
BAREBOX_CMD_HELP_END
BAREBOX_CMD_START(boot)

View File

@ -138,8 +138,7 @@ static int blspec_have_entry(struct blspec *blspec, const char *path)
* returns 0 if at least one entry could be successfully loaded, negative
* error value otherwise.
*/
static int blspec_scan_directory(struct blspec *blspec, const char *root,
struct cdev *cdev)
int blspec_scan_directory(struct blspec *blspec, const char *root)
{
struct blspec_entry *entry;
DIR *dir;
@ -207,11 +206,11 @@ static int blspec_scan_directory(struct blspec *blspec, const char *root,
continue;
}
found = 1;
found++;
entry->rootpath = xstrdup(root);
entry->configpath = configname;
entry->cdev = cdev;
entry->cdev = get_cdev_by_mountpath(root);
name = asprintf("%s/%s", dirname, d->d_name);
if (entry_default && !strcmp(name, entry_default))
@ -220,19 +219,24 @@ static int blspec_scan_directory(struct blspec *blspec, const char *root,
entry->boot_once = true;
free(name);
devname = xstrdup(dev_name(entry->cdev->dev));
if (entry->cdev->dev->parent)
hwdevname = xstrdup(dev_name(entry->cdev->dev->parent));
if (entry->cdev) {
devname = xstrdup(dev_name(entry->cdev->dev));
if (entry->cdev->dev->parent)
hwdevname = xstrdup(dev_name(entry->cdev->dev->parent));
}
entry->me.display = asprintf("%-20s %-20s %s", devname, hwdevname,
entry->me.display = asprintf("%-20s %-20s %s",
devname ? devname : "",
hwdevname ? hwdevname : "",
blspec_entry_var_get(entry, "title"));
free(devname);
free(hwdevname);
entry->me.type = MENU_ENTRY_NORMAL;
}
ret = found ? 0 : -ENOENT;
ret = found;
closedir(dir);
err_out:
@ -249,8 +253,8 @@ err_out:
* Given a cdev this function mounts the filesystem and collects all blspec
* entries found under /blspec/entries/.
*
* returns 0 if at least one entry could be successfully loaded, negative
* error value otherwise.
* returns the number of entries found or a negative error code if some unexpected
* error occured.
*/
static int blspec_scan_cdev(struct blspec *blspec, struct cdev *cdev)
{
@ -277,20 +281,21 @@ static int blspec_scan_cdev(struct blspec *blspec, struct cdev *cdev)
if (IS_ERR(rootpath))
return PTR_ERR(rootpath);
ret = blspec_scan_directory(blspec, rootpath, cdev);
return ret;
return blspec_scan_directory(blspec, rootpath);
}
/*
* blspec_scan_devices - scan all devices for child cdevs
*
* Iterate over all devices and collect child their cdevs.
* Returns the number of entries found or a negative error code if some unexpected
* error occured.
*/
void blspec_scan_devices(struct blspec *blspec)
int blspec_scan_devices(struct blspec *blspec)
{
struct device_d *dev;
struct block_device *bdev;
int ret, found = 0;
for_each_device(dev)
device_detect(dev);
@ -298,9 +303,14 @@ void blspec_scan_devices(struct blspec *blspec)
for_each_block_device(bdev) {
struct cdev *cdev = &bdev->cdev;
list_for_each_entry(cdev, &bdev->dev->cdevs, devices_list)
blspec_scan_cdev(blspec, cdev);
list_for_each_entry(cdev, &bdev->dev->cdevs, devices_list) {
ret = blspec_scan_cdev(blspec, cdev);
if (ret > 0)
found += ret;
}
}
return found;
}
/*
@ -308,15 +318,19 @@ void blspec_scan_devices(struct blspec *blspec)
*
* Given a device this functions scans over all child cdevs looking
* for blspec entries.
* Returns the number of entries found or a negative error code if some unexpected
* error occured.
*/
int blspec_scan_device(struct blspec *blspec, struct device_d *dev)
{
struct device_d *child;
struct cdev *cdev;
int ret;
int ret, found = 0;
pr_debug("%s: %s\n", __func__, dev_name(dev));
device_detect(dev);
list_for_each_entry(cdev, &dev->cdevs, devices_list) {
/*
* If the OS is installed on a disk with MBR disk label, and a
@ -324,8 +338,11 @@ int blspec_scan_device(struct blspec *blspec, struct device_d *dev)
* should be used as $BOOT
*/
if (cdev->dos_partition_type == 0xea) {
blspec_scan_cdev(blspec, cdev);
return 0;
ret = blspec_scan_cdev(blspec, cdev);
if (ret == 0)
ret = -ENOENT;
return ret;
}
/*
@ -341,8 +358,8 @@ int blspec_scan_device(struct blspec *blspec, struct device_d *dev)
/* Try child devices */
device_for_each_child(dev, child) {
ret = blspec_scan_device(blspec, child);
if (!ret)
return 0;
if (ret > 0)
return ret;
}
/*
@ -351,34 +368,49 @@ int blspec_scan_device(struct blspec *blspec, struct device_d *dev)
*/
list_for_each_entry(cdev, &dev->cdevs, devices_list) {
ret = blspec_scan_cdev(blspec, cdev);
if (!ret)
return 0;
if (ret > 0)
found += ret;
}
return -ENODEV;
return found;
}
/*
* blspec_scan_hwdevice - scan a hardware device for child cdevs
* blspec_scan_devicename - scan a hardware device for child cdevs
*
* Given a name of a hardware device this functions scans over all child
* cdevs looking for blspec entries.
* Returns the number of entries found or a negative error code if some unexpected
* error occured.
*/
int blspec_scan_hwdevice(struct blspec *blspec, const char *devname)
int blspec_scan_devicename(struct blspec *blspec, const char *devname)
{
struct device_d *dev;
struct cdev *cdev;
const char *colon;
pr_debug("%s: %s\n", __func__, devname);
colon = strchr(devname, '.');
if (colon) {
char *name = xstrdup(devname);
*strchr(name, '.') = 0;
device_detect_by_name(name);
free(name);
}
cdev = cdev_by_name(devname);
if (cdev) {
int ret = blspec_scan_cdev(blspec, cdev);
if (ret > 0)
return ret;
}
dev = get_device_by_name(devname);
if (!dev)
return -ENODEV;
device_detect(dev);
blspec_scan_device(blspec, dev);
return 0;
return blspec_scan_device(blspec, dev);
}
/*
@ -431,7 +463,7 @@ int blspec_boot(struct blspec_entry *entry, int verbose, int dryrun)
globalvar_add_simple("linux.bootargs.blspec", options);
pr_info("booting %s from %s\n", blspec_entry_var_get(entry, "title"),
dev_name(entry->cdev->dev));
entry->cdev ? dev_name(entry->cdev->dev) : "none");
if (entry->boot_once) {
char *s = asprintf("%s/once", abspath);
@ -488,10 +520,10 @@ struct blspec_entry *blspec_entry_default(struct blspec *l)
}
/*
* blspec_boot_hwdevice - scan hardware device for blspec entries and
* blspec_boot_devicename - scan hardware device for blspec entries and
* start the best one.
*/
int blspec_boot_hwdevice(const char *devname, int verbose, int dryrun)
int blspec_boot_devicename(const char *devname, int verbose, int dryrun)
{
struct blspec *blspec;
struct blspec_entry *e;
@ -499,13 +531,13 @@ int blspec_boot_hwdevice(const char *devname, int verbose, int dryrun)
blspec = blspec_alloc();
ret = blspec_scan_hwdevice(blspec, devname);
ret = blspec_scan_devicename(blspec, devname);
if (ret)
return ret;
e = blspec_entry_default(blspec);
if (!e) {
printf("Nothing found on %s\n", devname);
printf("No bootspec entry found on %s\n", devname);
ret = -ENOENT;
goto out;
}

13
fs/fs.c
View File

@ -283,6 +283,19 @@ static struct fs_device_d *get_fsdevice_by_path(const char *path)
return fs_dev_root;
}
/*
* get_cdev_by_mountpath - return the cdev the given path
* is mounted on
*/
struct cdev *get_cdev_by_mountpath(const char *path)
{
struct fs_device_d *fsdev;
fsdev = get_fsdevice_by_path(path);
return fsdev->cdev;
}
char *get_mounted_path(const char *path)
{
struct fs_device_d *fdev;

View File

@ -31,12 +31,13 @@ int blspec_entry_save(struct blspec_entry *entry, const char *path);
int blspec_boot(struct blspec_entry *entry, int verbose, int dryrun);
int blspec_boot_hwdevice(const char *devname, int verbose, int dryrun);
int blspec_boot_devicename(const char *devname, int verbose, int dryrun);
void blspec_scan_devices(struct blspec *blspec);
int blspec_scan_devices(struct blspec *blspec);
struct blspec_entry *blspec_entry_default(struct blspec *l);
int blspec_scan_hwdevice(struct blspec *blspec, const char *devname);
int blspec_scan_devicename(struct blspec *blspec, const char *devname);
int blspec_scan_directory(struct blspec *blspec, const char *root);
#define blspec_for_each_entry(blspec, entry) \
list_for_each_entry(entry, &blspec->entries, list)

View File

@ -184,6 +184,8 @@ char *normalise_link(const char *pathname, const char* symlink);
char *get_mounted_path(const char *path);
struct cdev *get_cdev_by_mountpath(const char *path);
/* Register a new filesystem driver */
int register_fs_driver(struct fs_driver_d *fsdrv);