+ rc = process_run_sync(process);
+ if (rc) {
+ pb_log("boot hook exec failed!\n");
+
+ } else if (WIFEXITED(process->exit_status) &&
+ WEXITSTATUS(process->exit_status)
+ == BOOT_HOOK_EXIT_UPDATE) {
+ /* if the hook returned with BOOT_HOOK_EXIT_UPDATE,
+ * then we process stdout to look for updated params
+ */
+ boot_hook_update(task, hooks[i]->d_name,
+ process->stdout_buf);
+ boot_hook_setenv(task);
+ }
+
+ process_release(process);
+ talloc_free(path);
+ }
+
+ free(hooks);
+}
+
+static bool load_pending(struct load_url_result *result)
+{
+ return !result || result->status == LOAD_ASYNC;
+}
+
+static int check_load(struct boot_task *task, const char *name,
+ struct load_url_result *result)
+{
+ if (!result)
+ return 0;
+
+ if (result->status != LOAD_ERROR) {
+ update_status(task->status_fn, task->status_arg,
+ STATUS_ERROR,
+ _("Loaded %s from %s"), name,
+ pb_url_to_string(result->url));
+ return 0;
+ }
+
+ pb_log("Failed to load %s from %s\n", name,
+ pb_url_to_string(result->url));
+ update_status(task->status_fn, task->status_arg,
+ STATUS_ERROR,
+ _("Couldn't load %s from %s"), name,
+ pb_url_to_string(result->url));
+ return -1;
+}
+
+static void cleanup_load(struct load_url_result *result)
+{
+ if (!result)
+ return;
+ if (result->status != LOAD_OK)
+ return;
+ if (!result->cleanup_local)
+ return;
+ unlink(result->local);
+}
+
+static void cleanup_cancellations(struct boot_task *task,
+ struct load_url_result *cur_result)
+{
+ struct boot_resource *resource;
+ struct load_url_result *result;
+ bool pending = false;
+
+ list_for_each_entry(&task->resources, resource, list) {
+ result = resource->result;
+
+ /* Nothing to do if a load hasn't actually started yet */
+ if (!result)
+ continue;
+
+ /* We need to cleanup and free any completed loads */
+ if (result == cur_result || result->status == LOAD_OK
+ || result->status == LOAD_ERROR) {
+ cleanup_load(result);
+ /* ... and cancel any pending loads, which we'll free in
+ * the completion callback */
+ } else if (result->status == LOAD_ASYNC) {
+ load_url_async_cancel(result);
+ pending = true;
+
+ /* if we're waiting for a cancellation, we still need to
+ * wait for the completion before freeing the boot task */
+ } else if (result->status == LOAD_CANCELLED) {
+ pending = true;
+ }
+ }
+
+ if (!pending)
+ talloc_free(task);
+}
+
+static void boot_process(struct load_url_result *result, void *data)
+{
+ struct boot_task *task = data;
+ struct boot_resource *resource;
+ int rc = -1;
+
+ if (task->cancelled) {
+ cleanup_cancellations(task, result);
+ return;
+ }
+
+ list_for_each_entry(&task->resources, resource, list)
+ if (load_pending(resource->result))
+ return;
+
+ list_for_each_entry(&task->resources, resource, list) {
+ if (check_load(task, resource->name, resource->result))
+ goto no_load;
+ *resource->local_path = resource->result->local;
+ }
+
+ run_boot_hooks(task);
+
+ update_status(task->status_fn, task->status_arg, STATUS_INFO,
+ _("Performing kexec load"));
+
+ rc = kexec_load(task);
+ pb_log_fn("kexec_load returned %d\n", rc);
+ if (rc == KEXEC_LOAD_DECRYPTION_FALURE) {
+ update_status(task->status_fn, task->status_arg,
+ STATUS_ERROR, _("Decryption failed"));
+ }
+ else if (rc == KEXEC_LOAD_SIGNATURE_FAILURE) {
+ update_status(task->status_fn, task->status_arg,
+ STATUS_ERROR,
+ _("Signature verification failed"));
+ }
+ else if (rc == KEXEC_LOAD_SIG_SETUP_INVALID) {
+ update_status(task->status_fn, task->status_arg,
+ STATUS_ERROR,
+ _("Invalid signature configuration"));
+ }
+
+no_load:
+ list_for_each_entry(&task->resources, resource, list)
+ cleanup_load(resource->result);
+
+ if (!rc) {
+ update_status(task->status_fn, task->status_arg,
+ STATUS_INFO, _("Performing kexec reboot"));
+
+ rc = kexec_reboot(task);
+ if (rc) {
+ update_status(task->status_fn, task->status_arg,
+ STATUS_ERROR,
+ _("kexec reboot failed"));
+ }
+ } else {
+ pb_log("Failed to load all boot resources\n");
+ }
+}
+
+static int start_url_load(struct boot_task *task, struct boot_resource *res)
+{
+ if (!res)
+ return 0;
+
+ res->result = load_url_async(task, res->url, boot_process,
+ task, NULL, task->status_arg);
+ if (!res->result) {
+ pb_log("Error starting load for %s at %s\n",
+ res->name, pb_url_to_string(res->url));
+ update_status(task->status_fn, task->status_arg,
+ STATUS_ERROR, _("Error loading %s"),
+ res->name);
+ return -1;
+ }
+ return 0;
+}
+
+static struct boot_resource *add_boot_resource(struct boot_task *task,
+ const char *name, struct pb_url *url,
+ const char **local_path)
+{
+ struct boot_resource *res;
+
+ if (!url)
+ return NULL;
+
+ res = talloc_zero(task, struct boot_resource);
+ if (!res)
+ return NULL;
+
+ res->name = talloc_strdup(res, name);
+ res->url = pb_url_copy(res, url);
+ res->local_path = local_path;
+
+ list_add(&task->resources, &res->list);
+ return res;
+}
+
+struct boot_task *boot(void *ctx, struct discover_boot_option *opt,
+ struct boot_command *cmd, int dry_run,
+ boot_status_fn status_fn, void *status_arg)
+{
+ struct pb_url *image = NULL, *initrd = NULL, *dtb = NULL;
+ struct pb_url *image_sig = NULL, *initrd_sig = NULL, *dtb_sig = NULL,
+ *cmdline_sig = NULL;
+ struct boot_resource *image_res, *initrd_res, *dtb_res, *tmp;
+ const struct config *config = config_get();
+ struct boot_task *boot_task;
+ const char *boot_desc;
+ int rc;
+ int lockdown_type;
+
+ if (opt && opt->option->name)
+ boot_desc = opt->option->name;
+ else if (cmd && cmd->boot_image_file)
+ boot_desc = cmd->boot_image_file;
+ else
+ boot_desc = _("(unknown)");
+
+ update_status(status_fn, status_arg, STATUS_INFO,
+ _("Booting %s"), boot_desc);