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 #include <security/gpg.h>
31 static const char *boot_hook_dir = PKG_SYSCONF_DIR "/boot.d";
33 BOOT_HOOK_EXIT_OK = 0,
34 BOOT_HOOK_EXIT_UPDATE = 2,
37 static void __attribute__((format(__printf__, 4, 5))) update_status(
38 boot_status_fn fn, void *arg, int type, char *fmt, ...)
44 status.message = talloc_vasprintf(NULL, fmt, ap);
48 status.backlog = false;
50 pb_debug("boot status: [%d] %s\n", type, status.message);
54 talloc_free(status.message);
58 * kexec_load - kexec load helper.
60 static int kexec_load(struct boot_task *boot_task)
62 struct process *process;
63 char *s_initrd = NULL;
71 boot_task->local_initrd_override = NULL;
72 boot_task->local_dtb_override = NULL;
73 boot_task->local_image_override = NULL;
75 if ((result = gpg_validate_boot_files(boot_task))) {
76 if (result == KEXEC_LOAD_DECRYPTION_FALURE) {
77 pb_log("%s: Aborting kexec due to"
78 " decryption failure\n", __func__);
81 if (result == KEXEC_LOAD_SIGNATURE_FAILURE) {
82 pb_log("%s: Aborting kexec due to signature"
83 " verification failure\n", __func__);
88 const char* local_initrd = (boot_task->local_initrd_override) ?
89 boot_task->local_initrd_override : boot_task->local_initrd;
90 const char* local_dtb = (boot_task->local_dtb_override) ?
91 boot_task->local_dtb_override : boot_task->local_dtb;
92 const char* local_image = (boot_task->local_image_override) ?
93 boot_task->local_image_override : boot_task->local_image;
95 process = process_create(boot_task);
97 pb_log("%s: failed to create process\n", __func__);
101 process->path = pb_system_apps.kexec;
102 process->argv = argv;
103 process->keep_stdout = true;
104 process->add_stderr = true;
107 *p++ = pb_system_apps.kexec; /* 1 */
111 s_initrd = talloc_asprintf(boot_task, "--initrd=%s",
114 *p++ = s_initrd; /* 3 */
118 s_dtb = talloc_asprintf(boot_task, "--dtb=%s",
121 *p++ = s_dtb; /* 4 */
124 s_args = talloc_asprintf(boot_task, "--append=%s",
125 boot_task->args ?: "\"\"");
127 *p++ = s_args; /* 5 */
129 *p++ = local_image; /* 6 */
132 result = process_run_sync(process);
134 pb_log("%s: failed to run process\n", __func__);
138 result = process->exit_status;
141 pb_log("%s: failed: (%d)\n", __func__, result);
142 update_status(boot_task->status_fn, boot_task->status_arg,
143 STATUS_ERROR, "%s", process->stdout_buf);
147 gpg_validate_boot_files_cleanup(boot_task);
153 * kexec_reboot - Helper to boot the new kernel.
155 * Must only be called after a successful call to kexec_load().
158 static int kexec_reboot(struct boot_task *task)
162 /* First try running shutdown. Init scripts should run 'exec -e' */
163 result = process_run_simple(task, pb_system_apps.shutdown, "-r",
166 /* On error, force a kexec with the -e option */
168 result = process_run_simple(task, pb_system_apps.kexec,
173 pb_log("%s: failed: (%d)\n", __func__, result);
175 /* okay, kexec -e -f */
177 result = process_run_simple(task, pb_system_apps.kexec,
182 pb_log("%s: failed: (%d)\n", __func__, result);
188 static void boot_hook_update_param(void *ctx, struct boot_task *task,
189 const char *name, const char *value)
194 } *param, params[] = {
195 { "boot_image", &task->local_image },
196 { "boot_initrd", &task->local_initrd },
197 { "boot_dtb", &task->local_dtb },
198 { "boot_args", &task->args },
202 for (param = params; param->name; param++) {
203 if (strcmp(param->name, name))
206 *param->p = talloc_strdup(ctx, value);
211 static void boot_hook_update(struct boot_task *task, const char *hookname,
214 char *line, *name, *val, *sep;
215 char *saveptr = NULL;
217 for (;; buf = NULL) {
219 line = strtok_r(buf, "\n", &saveptr);
223 sep = strchr(line, '=');
231 boot_hook_update_param(task, task, name, val);
233 pb_log("boot hook %s specified %s=%s\n",
234 hookname, name, val);
238 static void boot_hook_setenv(struct boot_task *task)
240 unsetenv("boot_image");
241 unsetenv("boot_initrd");
242 unsetenv("boot_dtb");
243 unsetenv("boot_args");
244 unsetenv("boot_console");
246 setenv("boot_image", task->local_image, 1);
247 if (task->local_initrd)
248 setenv("boot_initrd", task->local_initrd, 1);
250 setenv("boot_dtb", task->local_dtb, 1);
252 setenv("boot_args", task->args, 1);
253 if (task->boot_console)
254 setenv("boot_console", task->boot_console, 1);
257 static int hook_filter(const struct dirent *dirent)
259 return dirent->d_type == DT_REG || dirent->d_type == DT_LNK;
262 static int hook_cmp(const struct dirent **a, const struct dirent **b)
264 return strcmp((*a)->d_name, (*b)->d_name);
267 static void run_boot_hooks(struct boot_task *task)
269 struct dirent **hooks;
272 n = scandir(boot_hook_dir, &hooks, hook_filter, hook_cmp);
276 update_status(task->status_fn, task->status_arg, STATUS_INFO,
277 _("Running boot hooks"));
279 boot_hook_setenv(task);
281 for (i = 0; i < n; i++) {
282 const char *argv[2] = { NULL, NULL };
283 struct process *process;
287 path = join_paths(task, boot_hook_dir, hooks[i]->d_name);
289 if (access(path, X_OK)) {
294 process = process_create(task);
297 process->path = path;
298 process->argv = argv;
299 process->keep_stdout = true;
301 pb_log("running boot hook %s\n", hooks[i]->d_name);
303 rc = process_run_sync(process);
305 pb_log("boot hook exec failed!\n");
307 } else if (WIFEXITED(process->exit_status) &&
308 WEXITSTATUS(process->exit_status)
309 == BOOT_HOOK_EXIT_UPDATE) {
310 /* if the hook returned with BOOT_HOOK_EXIT_UPDATE,
311 * then we process stdout to look for updated params
313 boot_hook_update(task, hooks[i]->d_name,
314 process->stdout_buf);
315 boot_hook_setenv(task);
318 process_release(process);
325 static bool load_pending(struct load_url_result *result)
327 return result && result->status == LOAD_ASYNC;
330 static int check_load(struct boot_task *task, const char *name,
331 struct load_url_result *result)
336 if (result->status != LOAD_ERROR) {
337 update_status(task->status_fn, task->status_arg,
339 _("Loaded %s from %s"), name,
340 pb_url_to_string(result->url));
344 update_status(task->status_fn, task->status_arg,
346 _("Couldn't load %s from %s"), name,
347 pb_url_to_string(result->url));
351 static void cleanup_load(struct load_url_result *result)
355 if (result->status != LOAD_OK)
357 if (!result->cleanup_local)
359 unlink(result->local);
362 static void cleanup_cancellations(struct boot_task *task,
363 struct load_url_result *cur_result)
365 struct load_url_result *result, **results[] = {
366 &task->image, &task->initrd, &task->dtb,
368 bool pending = false;
371 for (i = 0; i < ARRAY_SIZE(results); i++) {
372 result = *results[i];
377 /* We need to cleanup and free any completed loads */
378 if (result == cur_result || result->status == LOAD_OK
379 || result->status == LOAD_ERROR) {
380 cleanup_load(result);
384 /* ... and cancel any pending loads, which we'll free in
385 * the completion callback */
386 } else if (result->status == LOAD_ASYNC) {
387 load_url_async_cancel(result);
390 /* if we're waiting for a cancellation, we still need to
391 * wait for the completion before freeing the boot task */
392 } else if (result->status == LOAD_CANCELLED) {
401 static void boot_process(struct load_url_result *result, void *data)
403 struct boot_task *task = data;
406 if (task->cancelled) {
407 cleanup_cancellations(task, result);
411 if (load_pending(task->image) ||
412 load_pending(task->initrd) ||
413 load_pending(task->dtb))
416 if (check_load(task, "kernel image", task->image) ||
417 check_load(task, "initrd", task->initrd) ||
418 check_load(task, "dtb", task->dtb))
421 if (task->verify_signature) {
422 if (load_pending(task->image_signature) ||
423 load_pending(task->initrd_signature) ||
424 load_pending(task->dtb_signature) ||
425 load_pending(task->cmdline_signature))
428 if (task->decrypt_files) {
429 if (load_pending(task->cmdline_signature))
433 if (task->verify_signature) {
434 if (check_load(task, "kernel image signature",
435 task->image_signature) ||
436 check_load(task, "initrd signature",
437 task->initrd_signature) ||
438 check_load(task, "dtb signature",
439 task->dtb_signature) ||
440 check_load(task, "command line signature",
441 task->cmdline_signature))
444 if (task->decrypt_files) {
445 if (load_pending(task->cmdline_signature))
448 if (check_load(task, "command line signature",
449 task->cmdline_signature))
450 goto no_decrypt_sig_load;
453 /* we make a copy of the local paths, as the boot hooks might update
454 * and/or create these */
455 task->local_image = task->image ? task->image->local : NULL;
456 task->local_initrd = task->initrd ? task->initrd->local : NULL;
457 task->local_dtb = task->dtb ? task->dtb->local : NULL;
459 if (task->verify_signature) {
460 task->local_image_signature = task->image_signature ?
461 task->image_signature->local : NULL;
462 task->local_initrd_signature = task->initrd_signature ?
463 task->initrd_signature->local : NULL;
464 task->local_dtb_signature = task->dtb_signature ?
465 task->dtb_signature->local : NULL;
467 if (task->verify_signature || task->decrypt_files) {
468 task->local_cmdline_signature = task->cmdline_signature ?
469 task->cmdline_signature->local : NULL;
472 run_boot_hooks(task);
474 update_status(task->status_fn, task->status_arg, STATUS_INFO,
475 _("Performing kexec load"));
477 rc = kexec_load(task);
478 pb_log("%s: kexec_load returned %d\n", __func__, rc);
479 if (rc == KEXEC_LOAD_DECRYPTION_FALURE) {
480 update_status(task->status_fn, task->status_arg,
481 STATUS_ERROR, _("Decryption failed"));
483 else if (rc == KEXEC_LOAD_SIGNATURE_FAILURE) {
484 update_status(task->status_fn, task->status_arg,
486 _("Signature verification failed"));
488 else if (rc == KEXEC_LOAD_SIG_SETUP_INVALID) {
489 update_status(task->status_fn, task->status_arg,
491 _("Invalid signature configuration"));
495 cleanup_load(task->image_signature);
496 cleanup_load(task->initrd_signature);
497 cleanup_load(task->dtb_signature);
500 cleanup_load(task->cmdline_signature);
503 cleanup_load(task->image);
504 cleanup_load(task->initrd);
505 cleanup_load(task->dtb);
508 update_status(task->status_fn, task->status_arg,
509 STATUS_INFO, _("Performing kexec reboot"));
511 rc = kexec_reboot(task);
513 update_status(task->status_fn, task->status_arg,
515 _("kexec reboot failed"));
520 static int start_url_load(struct boot_task *task, const char *name,
521 struct pb_url *url, struct load_url_result **result)
526 *result = load_url_async(task, url, boot_process, task, NULL,
529 update_status(task->status_fn, task->status_arg,
530 STATUS_ERROR, _("Error loading %s"), name);
536 struct boot_task *boot(void *ctx, struct discover_boot_option *opt,
537 struct boot_command *cmd, int dry_run,
538 boot_status_fn status_fn, void *status_arg)
540 struct pb_url *image = NULL, *initrd = NULL, *dtb = NULL;
541 struct pb_url *image_sig = NULL, *initrd_sig = NULL, *dtb_sig = NULL,
543 const struct config *config = config_get();
544 struct boot_task *boot_task;
545 const char *boot_desc;
549 if (opt && opt->option->name)
550 boot_desc = opt->option->name;
551 else if (cmd && cmd->boot_image_file)
552 boot_desc = cmd->boot_image_file;
554 boot_desc = _("(unknown)");
556 update_status(status_fn, status_arg, STATUS_INFO,
557 _("Booting %s"), boot_desc);
559 if (cmd && cmd->boot_image_file) {
560 image = pb_url_parse(opt, cmd->boot_image_file);
561 } else if (opt && opt->boot_image) {
562 image = opt->boot_image->url;
564 pb_log("%s: no image specified\n", __func__);
565 update_status(status_fn, status_arg, STATUS_INFO,
566 _("Boot failed: no image specified"));
570 if (cmd && cmd->initrd_file) {
571 initrd = pb_url_parse(opt, cmd->initrd_file);
572 } else if (opt && opt->initrd) {
573 initrd = opt->initrd->url;
576 if (cmd && cmd->dtb_file) {
577 dtb = pb_url_parse(opt, cmd->dtb_file);
578 } else if (opt && opt->dtb) {
582 if (opt && opt->proxy) {
583 setenv("http_proxy", opt->proxy, 1);
584 setenv("https_proxy", opt->proxy, 1);
587 boot_task = talloc_zero(ctx, struct boot_task);
588 boot_task->dry_run = dry_run;
589 boot_task->status_fn = status_fn;
590 boot_task->status_arg = status_arg;
592 lockdown_type = lockdown_status();
593 boot_task->verify_signature = (lockdown_type == PB_LOCKDOWN_SIGN);
594 boot_task->decrypt_files = (lockdown_type == PB_LOCKDOWN_DECRYPT);
596 if (cmd && cmd->boot_args) {
597 boot_task->args = talloc_strdup(boot_task, cmd->boot_args);
598 } else if (opt && opt->option->boot_args) {
599 boot_task->args = talloc_strdup(boot_task,
600 opt->option->boot_args);
602 boot_task->args = NULL;
605 if (cmd && cmd->console && !config->manual_console)
606 boot_task->boot_console = talloc_strdup(boot_task, cmd->console);
608 boot_task->boot_console = config ? config->boot_console : NULL;
610 if (boot_task->verify_signature || boot_task->decrypt_files) {
611 if (cmd && cmd->args_sig_file) {
612 cmdline_sig = pb_url_parse(opt, cmd->args_sig_file);
613 } else if (opt && opt->args_sig_file) {
614 cmdline_sig = opt->args_sig_file->url;
616 pb_log("%s: no command line signature file"
617 " specified\n", __func__);
618 update_status(status_fn, status_arg, STATUS_INFO,
619 _("Boot failed: no command line"
620 " signature file specified"));
621 talloc_free(boot_task);
626 /* start async loads for boot resources */
627 rc = start_url_load(boot_task, _("kernel image"),
628 image, &boot_task->image)
629 || start_url_load(boot_task, _("initrd"), initrd, &boot_task->initrd)
630 || start_url_load(boot_task, _("dtb"), dtb, &boot_task->dtb);
632 if (boot_task->verify_signature) {
633 /* Generate names of associated signature files and load */
635 image_sig = gpg_get_signature_url(ctx, image);
636 rc |= start_url_load(boot_task,
637 _("kernel image signature"), image_sig,
638 &boot_task->image_signature);
641 initrd_sig = gpg_get_signature_url(ctx, initrd);
642 rc |= start_url_load(boot_task, _("initrd signature"),
643 initrd_sig, &boot_task->initrd_signature);
646 dtb_sig = gpg_get_signature_url(ctx, dtb);
647 rc |= start_url_load(boot_task, _("dtb signature"),
648 dtb_sig, &boot_task->dtb_signature);
652 if (boot_task->verify_signature || boot_task->decrypt_files) {
653 rc |= start_url_load(boot_task,
654 _("kernel command line signature"), cmdline_sig,
655 &boot_task->cmdline_signature);
658 /* If all URLs are local, we may be done. */
660 /* Don't call boot_cancel() to preserve the status update */
661 boot_task->cancelled = true;
662 cleanup_cancellations(boot_task, NULL);
666 boot_process(NULL, boot_task);
671 void boot_cancel(struct boot_task *task)
673 task->cancelled = true;
675 update_status(task->status_fn, task->status_arg, STATUS_INFO,
676 _("Boot cancelled"));
678 cleanup_cancellations(task, NULL);