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);
49 pb_debug("boot status: [%d] %s\n", type, status.message);
53 talloc_free(status.message);
57 * kexec_load - kexec load helper.
59 static int kexec_load(struct boot_task *boot_task)
61 struct process *process;
62 char *s_initrd = NULL;
70 boot_task->local_initrd_override = NULL;
71 boot_task->local_dtb_override = NULL;
72 boot_task->local_image_override = NULL;
74 if ((result = gpg_validate_boot_files(boot_task))) {
75 if (result == KEXEC_LOAD_DECRYPTION_FALURE) {
76 pb_log("%s: Aborting kexec due to"
77 " decryption failure\n", __func__);
80 if (result == KEXEC_LOAD_SIGNATURE_FAILURE) {
81 pb_log("%s: Aborting kexec due to signature"
82 " verification failure\n", __func__);
87 const char* local_initrd = (boot_task->local_initrd_override) ?
88 boot_task->local_initrd_override : boot_task->local_initrd;
89 const char* local_dtb = (boot_task->local_dtb_override) ?
90 boot_task->local_dtb_override : boot_task->local_dtb;
91 const char* local_image = (boot_task->local_image_override) ?
92 boot_task->local_image_override : boot_task->local_image;
94 process = process_create(boot_task);
96 pb_log("%s: failed to create process\n", __func__);
100 process->path = pb_system_apps.kexec;
101 process->argv = argv;
102 process->keep_stdout = true;
103 process->add_stderr = true;
106 *p++ = pb_system_apps.kexec; /* 1 */
110 s_initrd = talloc_asprintf(boot_task, "--initrd=%s",
113 *p++ = s_initrd; /* 3 */
117 s_dtb = talloc_asprintf(boot_task, "--dtb=%s",
120 *p++ = s_dtb; /* 4 */
123 s_args = talloc_asprintf(boot_task, "--append=%s",
124 boot_task->args ?: "\"\"");
126 *p++ = s_args; /* 5 */
128 *p++ = local_image; /* 6 */
131 result = process_run_sync(process);
133 pb_log("%s: failed to run process\n", __func__);
137 result = process->exit_status;
140 pb_log("%s: failed: (%d)\n", __func__, result);
141 update_status(boot_task->status_fn, boot_task->status_arg,
142 STATUS_ERROR, "%s", process->stdout_buf);
146 gpg_validate_boot_files_cleanup(boot_task);
152 * kexec_reboot - Helper to boot the new kernel.
154 * Must only be called after a successful call to kexec_load().
157 static int kexec_reboot(struct boot_task *task)
161 /* First try running shutdown. Init scripts should run 'exec -e' */
162 result = process_run_simple(task, pb_system_apps.shutdown, "-r",
165 /* On error, force a kexec with the -e option */
167 result = process_run_simple(task, pb_system_apps.kexec,
172 pb_log("%s: failed: (%d)\n", __func__, result);
174 /* okay, kexec -e -f */
176 result = process_run_simple(task, pb_system_apps.kexec,
181 pb_log("%s: failed: (%d)\n", __func__, result);
187 static void boot_hook_update_param(void *ctx, struct boot_task *task,
188 const char *name, const char *value)
193 } *param, params[] = {
194 { "boot_image", &task->local_image },
195 { "boot_initrd", &task->local_initrd },
196 { "boot_dtb", &task->local_dtb },
197 { "boot_args", &task->args },
201 for (param = params; param->name; param++) {
202 if (strcmp(param->name, name))
205 *param->p = talloc_strdup(ctx, value);
210 static void boot_hook_update(struct boot_task *task, const char *hookname,
213 char *line, *name, *val, *sep;
214 char *saveptr = NULL;
216 for (;; buf = NULL) {
218 line = strtok_r(buf, "\n", &saveptr);
222 sep = strchr(line, '=');
230 boot_hook_update_param(task, task, name, val);
232 pb_log("boot hook %s specified %s=%s\n",
233 hookname, name, val);
237 static void boot_hook_setenv(struct boot_task *task)
239 unsetenv("boot_image");
240 unsetenv("boot_initrd");
241 unsetenv("boot_dtb");
242 unsetenv("boot_args");
243 unsetenv("boot_console");
245 setenv("boot_image", task->local_image, 1);
246 if (task->local_initrd)
247 setenv("boot_initrd", task->local_initrd, 1);
249 setenv("boot_dtb", task->local_dtb, 1);
251 setenv("boot_args", task->args, 1);
252 if (task->boot_console)
253 setenv("boot_console", task->boot_console, 1);
256 static int hook_filter(const struct dirent *dirent)
258 return dirent->d_type == DT_REG || dirent->d_type == DT_LNK;
261 static int hook_cmp(const struct dirent **a, const struct dirent **b)
263 return strcmp((*a)->d_name, (*b)->d_name);
266 static void run_boot_hooks(struct boot_task *task)
268 struct dirent **hooks;
271 n = scandir(boot_hook_dir, &hooks, hook_filter, hook_cmp);
275 update_status(task->status_fn, task->status_arg, STATUS_INFO,
276 _("Running boot hooks"));
278 boot_hook_setenv(task);
280 for (i = 0; i < n; i++) {
281 const char *argv[2] = { NULL, NULL };
282 struct process *process;
286 path = join_paths(task, boot_hook_dir, hooks[i]->d_name);
288 if (access(path, X_OK)) {
293 process = process_create(task);
296 process->path = path;
297 process->argv = argv;
298 process->keep_stdout = true;
300 pb_log("running boot hook %s\n", hooks[i]->d_name);
302 rc = process_run_sync(process);
304 pb_log("boot hook exec failed!\n");
306 } else if (WIFEXITED(process->exit_status) &&
307 WEXITSTATUS(process->exit_status)
308 == BOOT_HOOK_EXIT_UPDATE) {
309 /* if the hook returned with BOOT_HOOK_EXIT_UPDATE,
310 * then we process stdout to look for updated params
312 boot_hook_update(task, hooks[i]->d_name,
313 process->stdout_buf);
314 boot_hook_setenv(task);
317 process_release(process);
324 static bool load_pending(struct load_url_result *result)
326 return result && result->status == LOAD_ASYNC;
329 static int check_load(struct boot_task *task, const char *name,
330 struct load_url_result *result)
335 if (result->status != LOAD_ERROR) {
336 update_status(task->status_fn, task->status_arg,
338 _("Loaded %s from %s"), name,
339 pb_url_to_string(result->url));
343 update_status(task->status_fn, task->status_arg,
345 _("Couldn't load %s from %s"), name,
346 pb_url_to_string(result->url));
350 static void cleanup_load(struct load_url_result *result)
354 if (result->status != LOAD_OK)
356 if (!result->cleanup_local)
358 unlink(result->local);
361 static void cleanup_cancellations(struct boot_task *task,
362 struct load_url_result *cur_result)
364 struct load_url_result *result, **results[] = {
365 &task->image, &task->initrd, &task->dtb,
367 bool pending = false;
370 for (i = 0; i < ARRAY_SIZE(results); i++) {
371 result = *results[i];
376 /* We need to cleanup and free any completed loads */
377 if (result == cur_result || result->status == LOAD_OK
378 || result->status == LOAD_ERROR) {
379 cleanup_load(result);
383 /* ... and cancel any pending loads, which we'll free in
384 * the completion callback */
385 } else if (result->status == LOAD_ASYNC) {
386 load_url_async_cancel(result);
389 /* if we're waiting for a cancellation, we still need to
390 * wait for the completion before freeing the boot task */
391 } else if (result->status == LOAD_CANCELLED) {
400 static void boot_process(struct load_url_result *result, void *data)
402 struct boot_task *task = data;
405 if (task->cancelled) {
406 cleanup_cancellations(task, result);
410 if (load_pending(task->image) ||
411 load_pending(task->initrd) ||
412 load_pending(task->dtb))
415 if (check_load(task, "kernel image", task->image) ||
416 check_load(task, "initrd", task->initrd) ||
417 check_load(task, "dtb", task->dtb))
420 if (task->verify_signature) {
421 if (load_pending(task->image_signature) ||
422 load_pending(task->initrd_signature) ||
423 load_pending(task->dtb_signature) ||
424 load_pending(task->cmdline_signature))
427 if (task->decrypt_files) {
428 if (load_pending(task->cmdline_signature))
432 if (task->verify_signature) {
433 if (check_load(task, "kernel image signature",
434 task->image_signature) ||
435 check_load(task, "initrd signature",
436 task->initrd_signature) ||
437 check_load(task, "dtb signature",
438 task->dtb_signature) ||
439 check_load(task, "command line signature",
440 task->cmdline_signature))
443 if (task->decrypt_files) {
444 if (load_pending(task->cmdline_signature))
447 if (check_load(task, "command line signature",
448 task->cmdline_signature))
449 goto no_decrypt_sig_load;
452 /* we make a copy of the local paths, as the boot hooks might update
453 * and/or create these */
454 task->local_image = task->image ? task->image->local : NULL;
455 task->local_initrd = task->initrd ? task->initrd->local : NULL;
456 task->local_dtb = task->dtb ? task->dtb->local : NULL;
458 if (task->verify_signature) {
459 task->local_image_signature = task->image_signature ?
460 task->image_signature->local : NULL;
461 task->local_initrd_signature = task->initrd_signature ?
462 task->initrd_signature->local : NULL;
463 task->local_dtb_signature = task->dtb_signature ?
464 task->dtb_signature->local : NULL;
466 if (task->verify_signature || task->decrypt_files) {
467 task->local_cmdline_signature = task->cmdline_signature ?
468 task->cmdline_signature->local : NULL;
471 run_boot_hooks(task);
473 update_status(task->status_fn, task->status_arg, STATUS_INFO,
474 _("Performing kexec load"));
476 rc = kexec_load(task);
477 pb_log("%s: kexec_load returned %d\n", __func__, rc);
478 if (rc == KEXEC_LOAD_DECRYPTION_FALURE) {
479 update_status(task->status_fn, task->status_arg,
480 STATUS_ERROR, _("Decryption failed"));
482 else if (rc == KEXEC_LOAD_SIGNATURE_FAILURE) {
483 update_status(task->status_fn, task->status_arg,
485 _("Signature verification failed"));
487 else if (rc == KEXEC_LOAD_SIG_SETUP_INVALID) {
488 update_status(task->status_fn, task->status_arg,
490 _("Invalid signature configuration"));
494 cleanup_load(task->image_signature);
495 cleanup_load(task->initrd_signature);
496 cleanup_load(task->dtb_signature);
499 cleanup_load(task->cmdline_signature);
502 cleanup_load(task->image);
503 cleanup_load(task->initrd);
504 cleanup_load(task->dtb);
507 update_status(task->status_fn, task->status_arg,
508 STATUS_INFO, _("Performing kexec reboot"));
510 rc = kexec_reboot(task);
512 update_status(task->status_fn, task->status_arg,
514 _("kexec reboot failed"));
519 static int start_url_load(struct boot_task *task, const char *name,
520 struct pb_url *url, struct load_url_result **result)
525 *result = load_url_async(task, url, boot_process, task, NULL,
528 update_status(task->status_fn, task->status_arg,
529 STATUS_ERROR, _("Error loading %s"), name);
535 struct boot_task *boot(void *ctx, struct discover_boot_option *opt,
536 struct boot_command *cmd, int dry_run,
537 boot_status_fn status_fn, void *status_arg)
539 struct pb_url *image = NULL, *initrd = NULL, *dtb = NULL;
540 struct pb_url *image_sig = NULL, *initrd_sig = NULL, *dtb_sig = NULL,
542 const struct config *config = config_get();
543 struct boot_task *boot_task;
544 const char *boot_desc;
548 if (opt && opt->option->name)
549 boot_desc = opt->option->name;
550 else if (cmd && cmd->boot_image_file)
551 boot_desc = cmd->boot_image_file;
553 boot_desc = _("(unknown)");
555 update_status(status_fn, status_arg, STATUS_INFO,
556 _("Booting %s"), boot_desc);
558 if (cmd && cmd->boot_image_file) {
559 image = pb_url_parse(opt, cmd->boot_image_file);
560 } else if (opt && opt->boot_image) {
561 image = opt->boot_image->url;
563 pb_log("%s: no image specified\n", __func__);
564 update_status(status_fn, status_arg, STATUS_INFO,
565 _("Boot failed: no image specified"));
569 if (cmd && cmd->initrd_file) {
570 initrd = pb_url_parse(opt, cmd->initrd_file);
571 } else if (opt && opt->initrd) {
572 initrd = opt->initrd->url;
575 if (cmd && cmd->dtb_file) {
576 dtb = pb_url_parse(opt, cmd->dtb_file);
577 } else if (opt && opt->dtb) {
581 boot_task = talloc_zero(ctx, struct boot_task);
582 boot_task->dry_run = dry_run;
583 boot_task->status_fn = status_fn;
584 boot_task->status_arg = status_arg;
586 lockdown_type = lockdown_status();
587 boot_task->verify_signature = (lockdown_type == PB_LOCKDOWN_SIGN);
588 boot_task->decrypt_files = (lockdown_type == PB_LOCKDOWN_DECRYPT);
590 if (cmd && cmd->boot_args) {
591 boot_task->args = talloc_strdup(boot_task, cmd->boot_args);
592 } else if (opt && opt->option->boot_args) {
593 boot_task->args = talloc_strdup(boot_task,
594 opt->option->boot_args);
596 boot_task->args = NULL;
599 if (cmd && cmd->console && !config->manual_console)
600 boot_task->boot_console = talloc_strdup(boot_task, cmd->console);
602 boot_task->boot_console = config ? config->boot_console : NULL;
604 if (boot_task->verify_signature || boot_task->decrypt_files) {
605 if (cmd && cmd->args_sig_file) {
606 cmdline_sig = pb_url_parse(opt, cmd->args_sig_file);
607 } else if (opt && opt->args_sig_file) {
608 cmdline_sig = opt->args_sig_file->url;
610 pb_log("%s: no command line signature file"
611 " specified\n", __func__);
612 update_status(status_fn, status_arg, STATUS_INFO,
613 _("Boot failed: no command line"
614 " signature file specified"));
615 talloc_free(boot_task);
620 /* start async loads for boot resources */
621 rc = start_url_load(boot_task, _("kernel image"),
622 image, &boot_task->image)
623 || start_url_load(boot_task, _("initrd"), initrd, &boot_task->initrd)
624 || start_url_load(boot_task, _("dtb"), dtb, &boot_task->dtb);
626 if (boot_task->verify_signature) {
627 /* Generate names of associated signature files and load */
629 image_sig = gpg_get_signature_url(ctx, image);
630 rc |= start_url_load(boot_task,
631 _("kernel image signature"), image_sig,
632 &boot_task->image_signature);
635 initrd_sig = gpg_get_signature_url(ctx, initrd);
636 rc |= start_url_load(boot_task, _("initrd signature"),
637 initrd_sig, &boot_task->initrd_signature);
640 dtb_sig = gpg_get_signature_url(ctx, dtb);
641 rc |= start_url_load(boot_task, _("dtb signature"),
642 dtb_sig, &boot_task->dtb_signature);
646 if (boot_task->verify_signature || boot_task->decrypt_files) {
647 rc |= start_url_load(boot_task,
648 _("kernel command line signature"), cmdline_sig,
649 &boot_task->cmdline_signature);
652 /* If all URLs are local, we may be done. */
654 /* Don't call boot_cancel() to preserve the status update */
655 boot_task->cancelled = true;
656 cleanup_cancellations(boot_task, NULL);
660 boot_process(NULL, boot_task);
665 void boot_cancel(struct boot_task *task)
667 task->cancelled = true;
669 update_status(task->status_fn, task->status_arg, STATUS_INFO,
670 _("Boot cancelled"));
672 cleanup_cancellations(task, NULL);