discover: Let 'boot' user-event boot by name
authorSamuel Mendoza-Jonas <sam@mendozajonas.com>
Mon, 20 Aug 2018 06:09:58 +0000 (16:09 +1000)
committerSamuel Mendoza-Jonas <sam@mendozajonas.com>
Tue, 28 Aug 2018 03:46:03 +0000 (13:46 +1000)
If a 'name' parameter is used for a boot user event, search existing
boot options for one that matches that name on the given device.
This allows a pb-event user to boot based on name rather than having to
specify the exact boot arguments.

Signed-off-by: Samuel Mendoza-Jonas <sam@mendozajonas.com>
discover/device-handler.c
discover/device-handler.h
discover/user-event.c

index 9c050293df98cf446a44aa560ad3941dbfa4aa89..cf379e78ca7f7ecc1f52adbca0fedc2e22903fa8 100644 (file)
@@ -1404,6 +1404,28 @@ int device_handler_dhcp(struct device_handler *handler,
        return 0;
 }
 
+struct discover_boot_option *device_handler_find_option_by_name(
+               struct device_handler *handler, const char *device,
+               const char *name)
+{
+       size_t len = strlen(name);
+       unsigned int i;
+
+       for (i = 0; i < handler->n_devices; i++) {
+               struct discover_device *dev = handler->devices[i];
+               struct discover_boot_option *opt;
+
+               list_for_each_entry(&dev->boot_options, opt, list)
+                       /* Match exactly, partial matches can be quite common */
+                       if (strlen(opt->option->name) == len &&
+                                       !strcmp(opt->option->name, name))
+                               if (!dev || !strcmp(opt->option->device_id, device))
+                                       return opt;
+       }
+
+       return NULL;
+}
+
 static struct discover_boot_option *find_boot_option_by_id(
                struct device_handler *handler, const char *id)
 {
index 3ca88e4e07d9adbd968f36c8d5867ca2d93ff9b9..9696ec064cd369d7833cd2cd86d4ae1a1d92d9d8 100644 (file)
@@ -157,6 +157,9 @@ void discover_device_set_param(struct discover_device *device,
 const char *discover_device_get_param(struct discover_device *device,
                const char *name);
 
+struct discover_boot_option *device_handler_find_option_by_name(
+               struct device_handler *handler, const char *device,
+               const char *name);
 void device_handler_boot(struct device_handler *handler,
                struct boot_command *cmd);
 void device_handler_cancel_default(struct device_handler *handler);
index 1257796494e5211f01d210a11565c9539a6bffca..734f77b3014a6905635b73e110dd2e38706e3a28 100644 (file)
@@ -482,13 +482,30 @@ static int user_event_url(struct user_event *uev, struct event *event)
 static int user_event_boot(struct user_event *uev, struct event *event)
 {
        struct device_handler *handler = uev->handler;
-       struct boot_command *cmd = talloc(handler, struct boot_command);
+       struct boot_command *cmd = talloc_zero(handler, struct boot_command);
+       struct discover_boot_option *opt;
+       const char *name;
+
+       name = event_get_param(event, "name");
+       if (name) {
+               pb_log("Finding boot option %s @ %s\n", name, event->device);
+               opt = device_handler_find_option_by_name(handler,
+                               event->device, name);
+               if (!opt) {
+                       pb_log("No option with name %s\n", name);
+                       return -1;
+               }
 
-       cmd->option_id = talloc_strdup(cmd, event_get_param(event, "id"));
-       cmd->boot_image_file = talloc_strdup(cmd, event_get_param(event, "image"));
-       cmd->initrd_file = talloc_strdup(cmd, event_get_param(event, "initrd"));
-       cmd->dtb_file = talloc_strdup(cmd, event_get_param(event, "dtb"));
-       cmd->boot_args = talloc_strdup(cmd, event_get_param(event, "args"));
+               pb_log("Found option with id %s!\n", opt->option->id);
+               cmd->option_id = talloc_strdup(cmd, opt->option->id);
+       } else {
+               pb_log("Booting based on full boot command\n");
+               cmd->option_id = talloc_strdup(cmd, event_get_param(event, "id"));
+               cmd->boot_image_file = talloc_strdup(cmd, event_get_param(event, "image"));
+               cmd->initrd_file = talloc_strdup(cmd, event_get_param(event, "initrd"));
+               cmd->dtb_file = talloc_strdup(cmd, event_get_param(event, "dtb"));
+               cmd->boot_args = talloc_strdup(cmd, event_get_param(event, "args"));
+       }
 
        device_handler_boot(handler, cmd);