+
+ talloc_free(status.message);
+}
+
+static void boot_hook_update_param(void *ctx, struct boot_task *task,
+ const char *name, const char *value)
+{
+ struct p {
+ const char *name;
+ const char **p;
+ } *param, params[] = {
+ { "boot_image", &task->image->local },
+ { "boot_initrd", &task->initrd->local },
+ { "boot_dtb", &task->dtb->local },
+ { "boot_args", &task->args },
+ { NULL, NULL },
+ };
+
+ for (param = params; param->name; param++) {
+ if (strcmp(param->name, name))
+ continue;
+
+ *param->p = talloc_strdup(ctx, value);
+ return;
+ }
+}
+
+static void boot_hook_update(struct boot_task *task, const char *hookname,
+ char *buf)
+{
+ char *line, *name, *val, *sep;
+ char *saveptr;
+
+ for (;; buf = NULL) {
+
+ line = strtok_r(buf, "\n", &saveptr);
+ if (!line)
+ break;
+
+ sep = strchr(line, '=');
+ if (!sep)
+ continue;
+
+ *sep = '\0';
+ name = line;
+ val = sep + 1;
+
+ boot_hook_update_param(task, task, name, val);
+
+ pb_log("boot hook %s specified %s=%s\n",
+ hookname, name, val);
+ }
+}
+
+static void boot_hook_setenv(struct boot_task *task)
+{
+ unsetenv("boot_image");
+ unsetenv("boot_initrd");
+ unsetenv("boot_dtb");
+ unsetenv("boot_args");
+
+ setenv("boot_image", task->image->local, 1);
+ if (task->initrd)
+ setenv("boot_initrd", task->initrd->local, 1);
+ if (task->dtb)
+ setenv("boot_dtb", task->dtb->local, 1);
+ if (task->args)
+ setenv("boot_args", task->args, 1);
+}
+
+static int hook_filter(const struct dirent *dirent)
+{
+ return dirent->d_type == DT_REG || dirent->d_type == DT_LNK;
+}
+
+static int hook_cmp(const struct dirent **a, const struct dirent **b)
+{
+ return strcmp((*a)->d_name, (*b)->d_name);
+}
+
+static void run_boot_hooks(struct boot_task *task)
+{
+ struct dirent **hooks;
+ int i, n;
+
+ n = scandir(boot_hook_dir, &hooks, hook_filter, hook_cmp);
+ if (n < 1)
+ return;
+
+ update_status(task->status_fn, task->status_arg, BOOT_STATUS_INFO,
+ "running boot hooks");
+
+ boot_hook_setenv(task);
+
+ for (i = 0; i < n; i++) {
+ const char *argv[2] = { NULL, NULL };
+ struct process *process;
+ char *path;
+ int rc;
+
+ path = join_paths(task, boot_hook_dir, hooks[i]->d_name);
+
+ if (access(path, X_OK)) {
+ talloc_free(path);
+ continue;
+ }
+
+ process = process_create(task);
+
+ argv[0] = path;
+ process->path = path;
+ process->argv = argv;
+ process->keep_stdout = true;
+
+ pb_log("running boot hook %s\n", hooks[i]->d_name);
+
+ 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
+ */
+ if (rc == BOOT_HOOK_EXIT_UPDATE) {
+ 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)
+ return 0;
+
+ update_status(task->status_fn, task->status_arg,
+ BOOT_STATUS_ERROR,
+ "Couldn't load %s", name);
+ 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 boot_process(struct load_url_result *result __attribute__((unused)),
+ void *data)
+{
+ struct boot_task *task = data;
+ int rc = -1;
+
+ if (load_pending(task->image) ||
+ load_pending(task->initrd) ||
+ load_pending(task->dtb))
+ return;
+
+ if (check_load(task, "kernel image", task->image) ||
+ check_load(task, "initrd", task->initrd) ||
+ check_load(task, "dtb", task->dtb))
+ goto no_load;
+
+ run_boot_hooks(task);
+
+ update_status(task->status_fn, task->status_arg, BOOT_STATUS_INFO,
+ "performing kexec_load");
+
+ rc = kexec_load(task);
+ if (rc) {
+ update_status(task->status_fn, task->status_arg,
+ BOOT_STATUS_ERROR, "kexec load failed");
+ }
+
+no_load:
+ cleanup_load(task->image);
+ cleanup_load(task->initrd);
+ cleanup_load(task->dtb);
+
+ if (!rc) {
+ update_status(task->status_fn, task->status_arg,
+ BOOT_STATUS_INFO,
+ "performing kexec reboot");
+
+ rc = kexec_reboot(task);
+ if (rc) {
+ update_status(task->status_fn, task->status_arg,
+ BOOT_STATUS_ERROR,
+ "kexec reboot failed");
+ }
+ }
+
+ talloc_free(task);
+}
+
+static int start_url_load(struct boot_task *task, const char *name,
+ struct pb_url *url, struct load_url_result **result)
+{
+ if (!url)
+ return 0;
+
+ *result = load_url_async(task, url, boot_process, task);
+ if (!*result) {
+ update_status(task->status_fn, task->status_arg,
+ BOOT_STATUS_ERROR,
+ "Error loading %s", name);
+ return -1;
+ }
+ return 0;