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,
38 * kexec_load - kexec load helper.
40 static int kexec_load(struct boot_task *boot_task)
45 char *s_initrd = NULL;
49 boot_task->local_initrd_override = NULL;
50 boot_task->local_dtb_override = NULL;
51 boot_task->local_image_override = NULL;
53 if ((result = gpg_validate_boot_files(boot_task))) {
54 if (result == KEXEC_LOAD_DECRYPTION_FALURE) {
55 pb_log("%s: Aborting kexec due to"
56 " decryption failure\n", __func__);
59 if (result == KEXEC_LOAD_SIGNATURE_FAILURE) {
60 pb_log("%s: Aborting kexec due to signature"
61 " verification failure\n", __func__);
66 const char* local_initrd = (boot_task->local_initrd_override) ?
67 boot_task->local_initrd_override : boot_task->local_initrd;
68 const char* local_dtb = (boot_task->local_dtb_override) ?
69 boot_task->local_dtb_override : boot_task->local_dtb;
70 const char* local_image = (boot_task->local_image_override) ?
71 boot_task->local_image_override : boot_task->local_image;
74 *p++ = pb_system_apps.kexec; /* 1 */
78 s_initrd = talloc_asprintf(boot_task, "--initrd=%s",
81 *p++ = s_initrd; /* 3 */
85 s_dtb = talloc_asprintf(boot_task, "--dtb=%s",
91 s_args = talloc_asprintf(boot_task, "--append=%s",
92 boot_task->args ?: "\"\"");
94 *p++ = s_args; /* 5 */
96 *p++ = local_image; /* 6 */
99 result = process_run_simple_argv(boot_task, argv);
102 pb_log("%s: failed: (%d)\n", __func__, result);
105 gpg_validate_boot_files_cleanup(boot_task);
111 * kexec_reboot - Helper to boot the new kernel.
113 * Must only be called after a successful call to kexec_load().
116 static int kexec_reboot(struct boot_task *task)
120 /* First try running shutdown. Init scripts should run 'exec -e' */
121 result = process_run_simple(task, pb_system_apps.shutdown, "-r",
124 /* On error, force a kexec with the -e option */
126 result = process_run_simple(task, pb_system_apps.kexec,
131 pb_log("%s: failed: (%d)\n", __func__, result);
133 /* okay, kexec -e -f */
135 result = process_run_simple(task, pb_system_apps.kexec,
140 pb_log("%s: failed: (%d)\n", __func__, result);
146 static void __attribute__((format(__printf__, 4, 5))) update_status(
147 boot_status_fn fn, void *arg, int type, char *fmt, ...)
149 struct status status;
153 status.message = talloc_vasprintf(NULL, fmt, ap);
158 pb_debug("boot status: [%d] %s\n", type, status.message);
162 talloc_free(status.message);
165 static void boot_hook_update_param(void *ctx, struct boot_task *task,
166 const char *name, const char *value)
171 } *param, params[] = {
172 { "boot_image", &task->local_image },
173 { "boot_initrd", &task->local_initrd },
174 { "boot_dtb", &task->local_dtb },
175 { "boot_args", &task->args },
179 for (param = params; param->name; param++) {
180 if (strcmp(param->name, name))
183 *param->p = talloc_strdup(ctx, value);
188 static void boot_hook_update(struct boot_task *task, const char *hookname,
191 char *line, *name, *val, *sep;
192 char *saveptr = NULL;
194 for (;; buf = NULL) {
196 line = strtok_r(buf, "\n", &saveptr);
200 sep = strchr(line, '=');
208 boot_hook_update_param(task, task, name, val);
210 pb_log("boot hook %s specified %s=%s\n",
211 hookname, name, val);
215 static void boot_hook_setenv(struct boot_task *task)
217 unsetenv("boot_image");
218 unsetenv("boot_initrd");
219 unsetenv("boot_dtb");
220 unsetenv("boot_args");
221 unsetenv("boot_console");
223 setenv("boot_image", task->local_image, 1);
224 if (task->local_initrd)
225 setenv("boot_initrd", task->local_initrd, 1);
227 setenv("boot_dtb", task->local_dtb, 1);
229 setenv("boot_args", task->args, 1);
230 if (task->boot_console)
231 setenv("boot_console", task->boot_console, 1);
234 static int hook_filter(const struct dirent *dirent)
236 return dirent->d_type == DT_REG || dirent->d_type == DT_LNK;
239 static int hook_cmp(const struct dirent **a, const struct dirent **b)
241 return strcmp((*a)->d_name, (*b)->d_name);
244 static void run_boot_hooks(struct boot_task *task)
246 struct dirent **hooks;
249 n = scandir(boot_hook_dir, &hooks, hook_filter, hook_cmp);
253 update_status(task->status_fn, task->status_arg, STATUS_INFO,
254 _("Running boot hooks"));
256 boot_hook_setenv(task);
258 for (i = 0; i < n; i++) {
259 const char *argv[2] = { NULL, NULL };
260 struct process *process;
264 path = join_paths(task, boot_hook_dir, hooks[i]->d_name);
266 if (access(path, X_OK)) {
271 process = process_create(task);
274 process->path = path;
275 process->argv = argv;
276 process->keep_stdout = true;
278 pb_log("running boot hook %s\n", hooks[i]->d_name);
280 rc = process_run_sync(process);
282 pb_log("boot hook exec failed!\n");
284 } else if (WIFEXITED(process->exit_status) &&
285 WEXITSTATUS(process->exit_status)
286 == BOOT_HOOK_EXIT_UPDATE) {
287 /* if the hook returned with BOOT_HOOK_EXIT_UPDATE,
288 * then we process stdout to look for updated params
290 boot_hook_update(task, hooks[i]->d_name,
291 process->stdout_buf);
292 boot_hook_setenv(task);
295 process_release(process);
302 static bool load_pending(struct load_url_result *result)
304 return result && result->status == LOAD_ASYNC;
307 static int check_load(struct boot_task *task, const char *name,
308 struct load_url_result *result)
313 if (result->status != LOAD_ERROR) {
314 update_status(task->status_fn, task->status_arg,
316 _("Loaded %s from %s"), name,
317 pb_url_to_string(result->url));
321 update_status(task->status_fn, task->status_arg,
323 _("Couldn't load %s from %s"), name,
324 pb_url_to_string(result->url));
328 static void cleanup_load(struct load_url_result *result)
332 if (result->status != LOAD_OK)
334 if (!result->cleanup_local)
336 unlink(result->local);
339 static void cleanup_cancellations(struct boot_task *task,
340 struct load_url_result *cur_result)
342 struct load_url_result *result, **results[] = {
343 &task->image, &task->initrd, &task->dtb,
345 bool pending = false;
348 for (i = 0; i < ARRAY_SIZE(results); i++) {
349 result = *results[i];
354 /* We need to cleanup and free any completed loads */
355 if (result == cur_result || result->status == LOAD_OK
356 || result->status == LOAD_ERROR) {
357 cleanup_load(result);
361 /* ... and cancel any pending loads, which we'll free in
362 * the completion callback */
363 } else if (result->status == LOAD_ASYNC) {
364 load_url_async_cancel(result);
367 /* if we're waiting for a cancellation, we still need to
368 * wait for the completion before freeing the boot task */
369 } else if (result->status == LOAD_CANCELLED) {
378 static void boot_process(struct load_url_result *result, void *data)
380 struct boot_task *task = data;
383 if (task->cancelled) {
384 cleanup_cancellations(task, result);
388 if (load_pending(task->image) ||
389 load_pending(task->initrd) ||
390 load_pending(task->dtb))
393 if (check_load(task, "kernel image", task->image) ||
394 check_load(task, "initrd", task->initrd) ||
395 check_load(task, "dtb", task->dtb))
398 if (task->verify_signature) {
399 if (load_pending(task->image_signature) ||
400 load_pending(task->initrd_signature) ||
401 load_pending(task->dtb_signature) ||
402 load_pending(task->cmdline_signature))
405 if (task->decrypt_files) {
406 if (load_pending(task->cmdline_signature))
410 if (task->verify_signature) {
411 if (check_load(task, "kernel image signature",
412 task->image_signature) ||
413 check_load(task, "initrd signature",
414 task->initrd_signature) ||
415 check_load(task, "dtb signature",
416 task->dtb_signature) ||
417 check_load(task, "command line signature",
418 task->cmdline_signature))
421 if (task->decrypt_files) {
422 if (load_pending(task->cmdline_signature))
425 if (check_load(task, "command line signature",
426 task->cmdline_signature))
427 goto no_decrypt_sig_load;
430 /* we make a copy of the local paths, as the boot hooks might update
431 * and/or create these */
432 task->local_image = task->image ? task->image->local : NULL;
433 task->local_initrd = task->initrd ? task->initrd->local : NULL;
434 task->local_dtb = task->dtb ? task->dtb->local : NULL;
436 if (task->verify_signature) {
437 task->local_image_signature = task->image_signature ?
438 task->image_signature->local : NULL;
439 task->local_initrd_signature = task->initrd_signature ?
440 task->initrd_signature->local : NULL;
441 task->local_dtb_signature = task->dtb_signature ?
442 task->dtb_signature->local : NULL;
444 if (task->verify_signature || task->decrypt_files) {
445 task->local_cmdline_signature = task->cmdline_signature ?
446 task->cmdline_signature->local : NULL;
449 run_boot_hooks(task);
451 update_status(task->status_fn, task->status_arg, STATUS_INFO,
452 _("Performing kexec load"));
454 rc = kexec_load(task);
455 if (rc == KEXEC_LOAD_DECRYPTION_FALURE) {
456 update_status(task->status_fn, task->status_arg,
457 STATUS_ERROR, _("Decryption failed"));
459 else if (rc == KEXEC_LOAD_SIGNATURE_FAILURE) {
460 update_status(task->status_fn, task->status_arg,
462 _("Signature verification failed"));
464 else if (rc == KEXEC_LOAD_SIG_SETUP_INVALID) {
465 update_status(task->status_fn, task->status_arg,
467 _("Invalid signature configuration"));
470 update_status(task->status_fn, task->status_arg,
471 STATUS_ERROR, _("kexec load failed"));
475 cleanup_load(task->image_signature);
476 cleanup_load(task->initrd_signature);
477 cleanup_load(task->dtb_signature);
480 cleanup_load(task->cmdline_signature);
483 cleanup_load(task->image);
484 cleanup_load(task->initrd);
485 cleanup_load(task->dtb);
488 update_status(task->status_fn, task->status_arg,
489 STATUS_INFO, _("Performing kexec reboot"));
491 rc = kexec_reboot(task);
493 update_status(task->status_fn, task->status_arg,
495 _("kexec reboot failed"));
500 static int start_url_load(struct boot_task *task, const char *name,
501 struct pb_url *url, struct load_url_result **result)
506 *result = load_url_async(task, url, boot_process, task);
508 update_status(task->status_fn, task->status_arg,
509 STATUS_ERROR, _("Error loading %s"), name);
515 struct boot_task *boot(void *ctx, struct discover_boot_option *opt,
516 struct boot_command *cmd, int dry_run,
517 boot_status_fn status_fn, void *status_arg)
519 struct pb_url *image = NULL, *initrd = NULL, *dtb = NULL;
520 struct pb_url *image_sig = NULL, *initrd_sig = NULL, *dtb_sig = NULL,
522 const struct config *config = config_get();
523 struct boot_task *boot_task;
524 const char *boot_desc;
528 if (opt && opt->option->name)
529 boot_desc = opt->option->name;
530 else if (cmd && cmd->boot_image_file)
531 boot_desc = cmd->boot_image_file;
533 boot_desc = _("(unknown)");
535 update_status(status_fn, status_arg, STATUS_INFO,
536 _("Booting %s"), boot_desc);
538 if (cmd && cmd->boot_image_file) {
539 image = pb_url_parse(opt, cmd->boot_image_file);
540 } else if (opt && opt->boot_image) {
541 image = opt->boot_image->url;
543 pb_log("%s: no image specified\n", __func__);
544 update_status(status_fn, status_arg, STATUS_INFO,
545 _("Boot failed: no image specified"));
549 if (cmd && cmd->initrd_file) {
550 initrd = pb_url_parse(opt, cmd->initrd_file);
551 } else if (opt && opt->initrd) {
552 initrd = opt->initrd->url;
555 if (cmd && cmd->dtb_file) {
556 dtb = pb_url_parse(opt, cmd->dtb_file);
557 } else if (opt && opt->dtb) {
561 boot_task = talloc_zero(ctx, struct boot_task);
562 boot_task->dry_run = dry_run;
563 boot_task->status_fn = status_fn;
564 boot_task->status_arg = status_arg;
566 lockdown_type = lockdown_status();
567 boot_task->verify_signature = (lockdown_type == PB_LOCKDOWN_SIGN);
568 boot_task->decrypt_files = (lockdown_type == PB_LOCKDOWN_DECRYPT);
570 if (cmd && cmd->boot_args) {
571 boot_task->args = talloc_strdup(boot_task, cmd->boot_args);
572 } else if (opt && opt->option->boot_args) {
573 boot_task->args = talloc_strdup(boot_task,
574 opt->option->boot_args);
576 boot_task->args = NULL;
579 if (cmd && cmd->console && !config->manual_console)
580 boot_task->boot_console = talloc_strdup(boot_task, cmd->console);
582 boot_task->boot_console = config ? config->boot_console : NULL;
584 if (boot_task->verify_signature || boot_task->decrypt_files) {
585 if (cmd && cmd->args_sig_file) {
586 cmdline_sig = pb_url_parse(opt, cmd->args_sig_file);
587 } else if (opt && opt->args_sig_file) {
588 cmdline_sig = opt->args_sig_file->url;
590 pb_log("%s: no command line signature file"
591 " specified\n", __func__);
592 update_status(status_fn, status_arg, STATUS_INFO,
593 _("Boot failed: no command line"
594 " signature file specified"));
595 talloc_free(boot_task);
600 /* start async loads for boot resources */
601 rc = start_url_load(boot_task, _("kernel image"),
602 image, &boot_task->image)
603 || start_url_load(boot_task, _("initrd"), initrd, &boot_task->initrd)
604 || start_url_load(boot_task, _("dtb"), dtb, &boot_task->dtb);
606 if (boot_task->verify_signature) {
607 /* Generate names of associated signature files and load */
609 image_sig = gpg_get_signature_url(ctx, image);
610 rc |= start_url_load(boot_task,
611 _("kernel image signature"), image_sig,
612 &boot_task->image_signature);
615 initrd_sig = gpg_get_signature_url(ctx, initrd);
616 rc |= start_url_load(boot_task, _("initrd signature"),
617 initrd_sig, &boot_task->initrd_signature);
620 dtb_sig = gpg_get_signature_url(ctx, dtb);
621 rc |= start_url_load(boot_task, _("dtb signature"),
622 dtb_sig, &boot_task->dtb_signature);
626 if (boot_task->verify_signature || boot_task->decrypt_files) {
627 rc |= start_url_load(boot_task,
628 _("kernel command line signature"), cmdline_sig,
629 &boot_task->cmdline_signature);
632 /* If all URLs are local, we may be done. */
634 /* Don't call boot_cancel() to preserve the status update */
635 boot_task->cancelled = true;
636 cleanup_cancellations(boot_task, NULL);
640 boot_process(NULL, boot_task);
645 void boot_cancel(struct boot_task *task)
647 task->cancelled = true;
649 update_status(task->status_fn, task->status_arg, STATUS_INFO,
650 _("Boot cancelled"));
652 cleanup_cancellations(task, NULL);