10 #include <sys/types.h>
13 #include <pb-protocol/pb-protocol.h>
14 #include <process/process.h>
15 #include <system/system.h>
16 #include <talloc/talloc.h>
18 #include <util/util.h>
20 #include "device-handler.h"
25 static const char *boot_hook_dir = PKG_SYSCONF_DIR "/boot.d";
27 BOOT_HOOK_EXIT_OK = 0,
28 BOOT_HOOK_EXIT_UPDATE = 2,
32 struct load_url_result *image;
33 struct load_url_result *initrd;
34 struct load_url_result *dtb;
36 boot_status_fn status_fn;
43 * kexec_load - kexec load helper.
45 static int kexec_load(struct boot_task *boot_task)
50 char *s_initrd = NULL;
55 *p++ = pb_system_apps.kexec; /* 1 */
58 if (boot_task->initrd) {
59 s_initrd = talloc_asprintf(boot_task, "--initrd=%s",
60 boot_task->initrd->local);
62 *p++ = s_initrd; /* 3 */
66 s_dtb = talloc_asprintf(boot_task, "--dtb=%s",
67 boot_task->dtb->local);
72 if (boot_task->args) {
73 s_args = talloc_asprintf(boot_task, "--append=%s",
76 *p++ = s_args; /* 5 */
79 *p++ = boot_task->image->local; /* 6 */
82 result = process_run_simple_argv(boot_task, argv);
85 pb_log("%s: failed: (%d)\n", __func__, result);
91 * kexec_reboot - Helper to boot the new kernel.
93 * Must only be called after a successful call to kexec_load().
96 static int kexec_reboot(struct boot_task *task)
100 /* First try running shutdown. Init scripts should run 'exec -e' */
101 result = process_run_simple(task, pb_system_apps.shutdown, "-r",
104 /* On error, force a kexec with the -e option */
106 result = process_run_simple(task, pb_system_apps.kexec,
111 pb_log("%s: failed: (%d)\n", __func__, result);
113 /* okay, kexec -e -f */
115 result = process_run_simple(task, pb_system_apps.kexec,
120 pb_log("%s: failed: (%d)\n", __func__, result);
126 static void __attribute__((format(__printf__, 4, 5))) update_status(
127 boot_status_fn fn, void *arg, int type, char *fmt, ...)
129 struct boot_status status;
133 status.message = talloc_vasprintf(NULL, fmt, ap);
137 status.progress = -1;
138 status.detail = NULL;
140 pb_debug("boot status: [%d] %s\n", type, status.message);
144 talloc_free(status.message);
147 static void boot_hook_update_param(void *ctx, struct boot_task *task,
148 const char *name, const char *value)
153 } *param, params[] = {
154 { "boot_image", &task->image->local },
155 { "boot_initrd", &task->initrd->local },
156 { "boot_dtb", &task->dtb->local },
157 { "boot_args", &task->args },
161 for (param = params; param->name; param++) {
162 if (strcmp(param->name, name))
165 *param->p = talloc_strdup(ctx, value);
170 static void boot_hook_update(struct boot_task *task, const char *hookname,
173 char *line, *name, *val, *sep;
176 for (;; buf = NULL) {
178 line = strtok_r(buf, "\n", &saveptr);
182 sep = strchr(line, '=');
190 boot_hook_update_param(task, task, name, val);
192 pb_log("boot hook %s specified %s=%s\n",
193 hookname, name, val);
197 static void boot_hook_setenv(struct boot_task *task)
199 unsetenv("boot_image");
200 unsetenv("boot_initrd");
201 unsetenv("boot_dtb");
202 unsetenv("boot_args");
204 setenv("boot_image", task->image->local, 1);
206 setenv("boot_initrd", task->initrd->local, 1);
208 setenv("boot_dtb", task->dtb->local, 1);
210 setenv("boot_args", task->args, 1);
213 static int hook_filter(const struct dirent *dirent)
215 return dirent->d_type == DT_REG || dirent->d_type == DT_LNK;
218 static int hook_cmp(const struct dirent **a, const struct dirent **b)
220 return strcmp((*a)->d_name, (*b)->d_name);
223 static void run_boot_hooks(struct boot_task *task)
225 struct dirent **hooks;
228 n = scandir(boot_hook_dir, &hooks, hook_filter, hook_cmp);
232 update_status(task->status_fn, task->status_arg, BOOT_STATUS_INFO,
233 "running boot hooks");
235 boot_hook_setenv(task);
237 for (i = 0; i < n; i++) {
238 const char *argv[2] = { NULL, NULL };
239 struct process *process;
243 path = join_paths(task, boot_hook_dir, hooks[i]->d_name);
245 if (access(path, X_OK)) {
250 process = process_create(task);
253 process->path = path;
254 process->argv = argv;
255 process->keep_stdout = true;
257 pb_log("running boot hook %s\n", hooks[i]->d_name);
259 rc = process_run_sync(process);
261 pb_log("boot hook exec failed!\n");
263 } else if (WIFEXITED(process->exit_status) &&
264 WEXITSTATUS(process->exit_status)
265 == BOOT_HOOK_EXIT_UPDATE) {
266 /* if the hook returned with BOOT_HOOK_EXIT_UPDATE,
267 * then we process stdout to look for updated params
269 boot_hook_update(task, hooks[i]->d_name,
270 process->stdout_buf);
271 boot_hook_setenv(task);
274 process_release(process);
281 static bool load_pending(struct load_url_result *result)
283 return result && result->status == LOAD_ASYNC;
286 static int check_load(struct boot_task *task, const char *name,
287 struct load_url_result *result)
291 if (result->status != LOAD_ERROR)
294 update_status(task->status_fn, task->status_arg,
296 "Couldn't load %s", name);
300 static void cleanup_load(struct load_url_result *result)
304 if (result->status != LOAD_OK)
306 if (!result->cleanup_local)
308 unlink(result->local);
311 static void cleanup_cancellations(struct boot_task *task,
312 struct load_url_result *cur_result)
314 struct load_url_result *result, **results[] = {
315 &task->image, &task->initrd, &task->dtb,
317 bool pending = false;
320 for (i = 0; i < ARRAY_SIZE(results); i++) {
321 result = *results[i];
326 /* We need to cleanup and free any completed loads */
327 if (result == cur_result || result->status == LOAD_OK
328 || result->status == LOAD_ERROR) {
329 cleanup_load(result);
333 /* ... and cancel any pending loads, which we'll free in
334 * the completion callback */
335 } else if (result->status == LOAD_ASYNC) {
336 load_url_async_cancel(result);
345 static void boot_process(struct load_url_result *result, void *data)
347 struct boot_task *task = data;
350 if (task->cancelled) {
351 cleanup_cancellations(task, result);
355 if (load_pending(task->image) ||
356 load_pending(task->initrd) ||
357 load_pending(task->dtb))
360 if (check_load(task, "kernel image", task->image) ||
361 check_load(task, "initrd", task->initrd) ||
362 check_load(task, "dtb", task->dtb))
365 run_boot_hooks(task);
367 update_status(task->status_fn, task->status_arg, BOOT_STATUS_INFO,
368 "performing kexec_load");
370 rc = kexec_load(task);
372 update_status(task->status_fn, task->status_arg,
373 BOOT_STATUS_ERROR, "kexec load failed");
377 cleanup_load(task->image);
378 cleanup_load(task->initrd);
379 cleanup_load(task->dtb);
382 update_status(task->status_fn, task->status_arg,
384 "performing kexec reboot");
386 rc = kexec_reboot(task);
388 update_status(task->status_fn, task->status_arg,
390 "kexec reboot failed");
395 static int start_url_load(struct boot_task *task, const char *name,
396 struct pb_url *url, struct load_url_result **result)
401 *result = load_url_async(task, url, boot_process, task);
403 update_status(task->status_fn, task->status_arg,
405 "Error loading %s", name);
411 struct boot_task *boot(void *ctx, struct discover_boot_option *opt,
412 struct boot_command *cmd, int dry_run,
413 boot_status_fn status_fn, void *status_arg)
415 struct pb_url *image = NULL, *initrd = NULL, *dtb = NULL;
416 struct boot_task *boot_task;
417 const char *boot_desc;
420 if (opt && opt->option->name)
421 boot_desc = opt->option->name;
422 else if (cmd && cmd->boot_image_file)
423 boot_desc = cmd->boot_image_file;
425 boot_desc = "(unknown)";
427 update_status(status_fn, status_arg, BOOT_STATUS_INFO,
428 "Booting %s.", boot_desc);
430 if (cmd && cmd->boot_image_file) {
431 image = pb_url_parse(opt, cmd->boot_image_file);
432 } else if (opt && opt->boot_image) {
433 image = opt->boot_image->url;
435 pb_log("%s: no image specified\n", __func__);
436 update_status(status_fn, status_arg, BOOT_STATUS_INFO,
437 "Boot failed: no image specified");
441 if (cmd && cmd->initrd_file) {
442 initrd = pb_url_parse(opt, cmd->initrd_file);
443 } else if (opt && opt->initrd) {
444 initrd = opt->initrd->url;
447 if (cmd && cmd->dtb_file) {
448 dtb = pb_url_parse(opt, cmd->dtb_file);
449 } else if (opt && opt->dtb) {
453 boot_task = talloc_zero(ctx, struct boot_task);
454 boot_task->dry_run = dry_run;
455 boot_task->status_fn = status_fn;
456 boot_task->status_arg = status_arg;
458 if (cmd && cmd->boot_args) {
459 boot_task->args = talloc_strdup(boot_task, cmd->boot_args);
460 } else if (opt && opt->option->boot_args) {
461 boot_task->args = talloc_strdup(boot_task,
462 opt->option->boot_args);
464 boot_task->args = NULL;
467 /* start async loads for boot resources */
468 rc = start_url_load(boot_task, "kernel image", image, &boot_task->image)
469 || start_url_load(boot_task, "initrd", initrd, &boot_task->initrd)
470 || start_url_load(boot_task, "dtb", dtb, &boot_task->dtb);
472 /* If all URLs are local, we may be done. */
474 talloc_free(boot_task);
478 boot_process(NULL, boot_task);
483 void boot_cancel(struct boot_task *task)
485 task->cancelled = true;
487 update_status(task->status_fn, task->status_arg, BOOT_STATUS_INFO,
490 cleanup_cancellations(task, NULL);