2 #if defined(HAVE_CONFIG_H)
12 #include <sys/types.h>
15 #include <pb-protocol/pb-protocol.h>
16 #include <process/process.h>
17 #include <system/system.h>
18 #include <talloc/talloc.h>
20 #include <util/util.h>
21 #include <i18n/i18n.h>
23 #include "device-handler.h"
29 static const char *boot_hook_dir = PKG_SYSCONF_DIR "/boot.d";
31 BOOT_HOOK_EXIT_OK = 0,
32 BOOT_HOOK_EXIT_UPDATE = 2,
36 struct load_url_result *image;
37 struct load_url_result *initrd;
38 struct load_url_result *dtb;
39 const char *local_image;
40 const char *local_initrd;
41 const char *local_dtb;
44 boot_status_fn status_fn;
51 * kexec_load - kexec load helper.
53 static int kexec_load(struct boot_task *boot_task)
58 char *s_initrd = NULL;
63 *p++ = pb_system_apps.kexec; /* 1 */
66 if (boot_task->local_initrd) {
67 s_initrd = talloc_asprintf(boot_task, "--initrd=%s",
68 boot_task->local_initrd);
70 *p++ = s_initrd; /* 3 */
73 if (boot_task->local_dtb) {
74 s_dtb = talloc_asprintf(boot_task, "--dtb=%s",
75 boot_task->local_dtb);
80 if (boot_task->args) {
81 s_args = talloc_asprintf(boot_task, "--append=%s",
84 *p++ = s_args; /* 5 */
87 *p++ = boot_task->local_image; /* 6 */
90 result = process_run_simple_argv(boot_task, argv);
93 pb_log("%s: failed: (%d)\n", __func__, result);
99 * kexec_reboot - Helper to boot the new kernel.
101 * Must only be called after a successful call to kexec_load().
104 static int kexec_reboot(struct boot_task *task)
108 /* First try running shutdown. Init scripts should run 'exec -e' */
109 result = process_run_simple(task, pb_system_apps.shutdown, "-r",
112 /* On error, force a kexec with the -e option */
114 result = process_run_simple(task, pb_system_apps.kexec,
119 pb_log("%s: failed: (%d)\n", __func__, result);
121 /* okay, kexec -e -f */
123 result = process_run_simple(task, pb_system_apps.kexec,
128 pb_log("%s: failed: (%d)\n", __func__, result);
134 static void __attribute__((format(__printf__, 4, 5))) update_status(
135 boot_status_fn fn, void *arg, int type, char *fmt, ...)
137 struct boot_status status;
141 status.message = talloc_vasprintf(NULL, fmt, ap);
145 status.progress = -1;
146 status.detail = NULL;
148 pb_debug("boot status: [%d] %s\n", type, status.message);
152 talloc_free(status.message);
155 static void boot_hook_update_param(void *ctx, struct boot_task *task,
156 const char *name, const char *value)
161 } *param, params[] = {
162 { "boot_image", &task->local_image },
163 { "boot_initrd", &task->local_initrd },
164 { "boot_dtb", &task->local_dtb },
165 { "boot_args", &task->args },
169 for (param = params; param->name; param++) {
170 if (strcmp(param->name, name))
173 *param->p = talloc_strdup(ctx, value);
178 static void boot_hook_update(struct boot_task *task, const char *hookname,
181 char *line, *name, *val, *sep;
182 char *saveptr = NULL;
184 for (;; buf = NULL) {
186 line = strtok_r(buf, "\n", &saveptr);
190 sep = strchr(line, '=');
198 boot_hook_update_param(task, task, name, val);
200 pb_log("boot hook %s specified %s=%s\n",
201 hookname, name, val);
205 static void boot_hook_setenv(struct boot_task *task)
207 unsetenv("boot_image");
208 unsetenv("boot_initrd");
209 unsetenv("boot_dtb");
210 unsetenv("boot_args");
211 unsetenv("boot_tty");
213 setenv("boot_image", task->local_image, 1);
214 if (task->local_initrd)
215 setenv("boot_initrd", task->local_initrd, 1);
217 setenv("boot_dtb", task->local_dtb, 1);
219 setenv("boot_args", task->args, 1);
221 setenv("boot_tty", task->boot_tty, 1);
224 static int hook_filter(const struct dirent *dirent)
226 return dirent->d_type == DT_REG || dirent->d_type == DT_LNK;
229 static int hook_cmp(const struct dirent **a, const struct dirent **b)
231 return strcmp((*a)->d_name, (*b)->d_name);
234 static void run_boot_hooks(struct boot_task *task)
236 struct dirent **hooks;
239 n = scandir(boot_hook_dir, &hooks, hook_filter, hook_cmp);
243 update_status(task->status_fn, task->status_arg, BOOT_STATUS_INFO,
244 _("running boot hooks"));
246 boot_hook_setenv(task);
248 for (i = 0; i < n; i++) {
249 const char *argv[2] = { NULL, NULL };
250 struct process *process;
254 path = join_paths(task, boot_hook_dir, hooks[i]->d_name);
256 if (access(path, X_OK)) {
261 process = process_create(task);
264 process->path = path;
265 process->argv = argv;
266 process->keep_stdout = true;
268 pb_log("running boot hook %s\n", hooks[i]->d_name);
270 rc = process_run_sync(process);
272 pb_log("boot hook exec failed!\n");
274 } else if (WIFEXITED(process->exit_status) &&
275 WEXITSTATUS(process->exit_status)
276 == BOOT_HOOK_EXIT_UPDATE) {
277 /* if the hook returned with BOOT_HOOK_EXIT_UPDATE,
278 * then we process stdout to look for updated params
280 boot_hook_update(task, hooks[i]->d_name,
281 process->stdout_buf);
282 boot_hook_setenv(task);
285 process_release(process);
292 static bool load_pending(struct load_url_result *result)
294 return result && result->status == LOAD_ASYNC;
297 static int check_load(struct boot_task *task, const char *name,
298 struct load_url_result *result)
302 if (result->status != LOAD_ERROR)
305 update_status(task->status_fn, task->status_arg,
307 _("Couldn't load %s"), name);
311 static void cleanup_load(struct load_url_result *result)
315 if (result->status != LOAD_OK)
317 if (!result->cleanup_local)
319 unlink(result->local);
322 static void cleanup_cancellations(struct boot_task *task,
323 struct load_url_result *cur_result)
325 struct load_url_result *result, **results[] = {
326 &task->image, &task->initrd, &task->dtb,
328 bool pending = false;
331 for (i = 0; i < ARRAY_SIZE(results); i++) {
332 result = *results[i];
337 /* We need to cleanup and free any completed loads */
338 if (result == cur_result || result->status == LOAD_OK
339 || result->status == LOAD_ERROR) {
340 cleanup_load(result);
344 /* ... and cancel any pending loads, which we'll free in
345 * the completion callback */
346 } else if (result->status == LOAD_ASYNC) {
347 load_url_async_cancel(result);
350 /* if we're waiting for a cancellation, we still need to
351 * wait for the completion before freeing the boot task */
352 } else if (result->status == LOAD_CANCELLED) {
361 static void boot_process(struct load_url_result *result, void *data)
363 struct boot_task *task = data;
366 if (task->cancelled) {
367 cleanup_cancellations(task, result);
371 if (load_pending(task->image) ||
372 load_pending(task->initrd) ||
373 load_pending(task->dtb))
376 if (check_load(task, "kernel image", task->image) ||
377 check_load(task, "initrd", task->initrd) ||
378 check_load(task, "dtb", task->dtb))
381 /* we make a copy of the local paths, as the boot hooks might update
382 * and/or create these */
383 task->local_image = task->image ? task->image->local : NULL;
384 task->local_initrd = task->initrd ? task->initrd->local : NULL;
385 task->local_dtb = task->dtb ? task->dtb->local : NULL;
387 run_boot_hooks(task);
389 update_status(task->status_fn, task->status_arg, BOOT_STATUS_INFO,
390 _("performing kexec_load"));
392 rc = kexec_load(task);
394 update_status(task->status_fn, task->status_arg,
395 BOOT_STATUS_ERROR, _("kexec load failed"));
399 cleanup_load(task->image);
400 cleanup_load(task->initrd);
401 cleanup_load(task->dtb);
404 update_status(task->status_fn, task->status_arg,
406 _("performing kexec reboot"));
408 rc = kexec_reboot(task);
410 update_status(task->status_fn, task->status_arg,
412 _("kexec reboot failed"));
417 static int start_url_load(struct boot_task *task, const char *name,
418 struct pb_url *url, struct load_url_result **result)
423 *result = load_url_async(task, url, boot_process, task);
425 update_status(task->status_fn, task->status_arg,
427 _("Error loading %s"), name);
433 struct boot_task *boot(void *ctx, struct discover_boot_option *opt,
434 struct boot_command *cmd, int dry_run,
435 boot_status_fn status_fn, void *status_arg)
437 struct pb_url *image = NULL, *initrd = NULL, *dtb = NULL;
438 const struct config *config;
439 struct boot_task *boot_task;
440 const char *boot_desc;
443 if (opt && opt->option->name)
444 boot_desc = opt->option->name;
445 else if (cmd && cmd->boot_image_file)
446 boot_desc = cmd->boot_image_file;
448 boot_desc = _("(unknown)");
450 update_status(status_fn, status_arg, BOOT_STATUS_INFO,
451 _("Booting %s."), boot_desc);
453 if (cmd && cmd->boot_image_file) {
454 image = pb_url_parse(opt, cmd->boot_image_file);
455 } else if (opt && opt->boot_image) {
456 image = opt->boot_image->url;
458 pb_log("%s: no image specified\n", __func__);
459 update_status(status_fn, status_arg, BOOT_STATUS_INFO,
460 _("Boot failed: no image specified"));
464 if (cmd && cmd->initrd_file) {
465 initrd = pb_url_parse(opt, cmd->initrd_file);
466 } else if (opt && opt->initrd) {
467 initrd = opt->initrd->url;
470 if (cmd && cmd->dtb_file) {
471 dtb = pb_url_parse(opt, cmd->dtb_file);
472 } else if (opt && opt->dtb) {
476 boot_task = talloc_zero(ctx, struct boot_task);
477 boot_task->dry_run = dry_run;
478 boot_task->status_fn = status_fn;
479 boot_task->status_arg = status_arg;
481 if (cmd && cmd->boot_args) {
482 boot_task->args = talloc_strdup(boot_task, cmd->boot_args);
483 } else if (opt && opt->option->boot_args) {
484 boot_task->args = talloc_strdup(boot_task,
485 opt->option->boot_args);
487 boot_task->args = NULL;
491 boot_task->boot_tty = talloc_strdup(boot_task, cmd->tty);
493 config = config_get();
494 boot_task->boot_tty = config ? config->boot_tty : NULL;
497 /* start async loads for boot resources */
498 rc = start_url_load(boot_task, "kernel image", image, &boot_task->image)
499 || start_url_load(boot_task, "initrd", initrd, &boot_task->initrd)
500 || start_url_load(boot_task, "dtb", dtb, &boot_task->dtb);
503 /* Don't call boot_cancel() to preserve the status update */
504 boot_task->cancelled = true;
505 cleanup_cancellations(boot_task, NULL);
509 boot_process(NULL, boot_task);
514 void boot_cancel(struct boot_task *task)
516 task->cancelled = true;
518 update_status(task->status_fn, task->status_arg, BOOT_STATUS_INFO,
519 _("Boot cancelled"));
521 cleanup_cancellations(task, NULL);