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)
312 if (result->status != LOAD_ERROR)
315 update_status(task->status_fn, task->status_arg,
317 _("Couldn't load %s"), name);
321 static void cleanup_load(struct load_url_result *result)
325 if (result->status != LOAD_OK)
327 if (!result->cleanup_local)
329 unlink(result->local);
332 static void cleanup_cancellations(struct boot_task *task,
333 struct load_url_result *cur_result)
335 struct load_url_result *result, **results[] = {
336 &task->image, &task->initrd, &task->dtb,
338 bool pending = false;
341 for (i = 0; i < ARRAY_SIZE(results); i++) {
342 result = *results[i];
347 /* We need to cleanup and free any completed loads */
348 if (result == cur_result || result->status == LOAD_OK
349 || result->status == LOAD_ERROR) {
350 cleanup_load(result);
354 /* ... and cancel any pending loads, which we'll free in
355 * the completion callback */
356 } else if (result->status == LOAD_ASYNC) {
357 load_url_async_cancel(result);
360 /* if we're waiting for a cancellation, we still need to
361 * wait for the completion before freeing the boot task */
362 } else if (result->status == LOAD_CANCELLED) {
371 static void boot_process(struct load_url_result *result, void *data)
373 struct boot_task *task = data;
376 if (task->cancelled) {
377 cleanup_cancellations(task, result);
381 if (load_pending(task->image) ||
382 load_pending(task->initrd) ||
383 load_pending(task->dtb))
386 if (check_load(task, "kernel image", task->image) ||
387 check_load(task, "initrd", task->initrd) ||
388 check_load(task, "dtb", task->dtb))
391 if (task->verify_signature) {
392 if (load_pending(task->image_signature) ||
393 load_pending(task->initrd_signature) ||
394 load_pending(task->dtb_signature) ||
395 load_pending(task->cmdline_signature))
398 if (task->decrypt_files) {
399 if (load_pending(task->cmdline_signature))
403 if (task->verify_signature) {
404 if (check_load(task, "kernel image signature",
405 task->image_signature) ||
406 check_load(task, "initrd signature",
407 task->initrd_signature) ||
408 check_load(task, "dtb signature",
409 task->dtb_signature) ||
410 check_load(task, "command line signature",
411 task->cmdline_signature))
414 if (task->decrypt_files) {
415 if (load_pending(task->cmdline_signature))
418 if (check_load(task, "command line signature",
419 task->cmdline_signature))
420 goto no_decrypt_sig_load;
423 /* we make a copy of the local paths, as the boot hooks might update
424 * and/or create these */
425 task->local_image = task->image ? task->image->local : NULL;
426 task->local_initrd = task->initrd ? task->initrd->local : NULL;
427 task->local_dtb = task->dtb ? task->dtb->local : NULL;
429 if (task->verify_signature) {
430 task->local_image_signature = task->image_signature ?
431 task->image_signature->local : NULL;
432 task->local_initrd_signature = task->initrd_signature ?
433 task->initrd_signature->local : NULL;
434 task->local_dtb_signature = task->dtb_signature ?
435 task->dtb_signature->local : NULL;
437 if (task->verify_signature || task->decrypt_files) {
438 task->local_cmdline_signature = task->cmdline_signature ?
439 task->cmdline_signature->local : NULL;
442 run_boot_hooks(task);
444 update_status(task->status_fn, task->status_arg, STATUS_INFO,
445 _("performing kexec_load"));
447 rc = kexec_load(task);
448 if (rc == KEXEC_LOAD_DECRYPTION_FALURE) {
449 update_status(task->status_fn, task->status_arg,
450 STATUS_ERROR, _("decryption failed"));
452 else if (rc == KEXEC_LOAD_SIGNATURE_FAILURE) {
453 update_status(task->status_fn, task->status_arg,
455 _("signature verification failed"));
457 else if (rc == KEXEC_LOAD_SIG_SETUP_INVALID) {
458 update_status(task->status_fn, task->status_arg,
460 _("invalid signature configuration"));
463 update_status(task->status_fn, task->status_arg,
464 STATUS_ERROR, _("kexec load failed"));
468 cleanup_load(task->image_signature);
469 cleanup_load(task->initrd_signature);
470 cleanup_load(task->dtb_signature);
473 cleanup_load(task->cmdline_signature);
476 cleanup_load(task->image);
477 cleanup_load(task->initrd);
478 cleanup_load(task->dtb);
481 update_status(task->status_fn, task->status_arg,
482 STATUS_INFO, _("performing kexec reboot"));
484 rc = kexec_reboot(task);
486 update_status(task->status_fn, task->status_arg,
488 _("kexec reboot failed"));
493 static int start_url_load(struct boot_task *task, const char *name,
494 struct pb_url *url, struct load_url_result **result)
499 *result = load_url_async(task, url, boot_process, task);
501 update_status(task->status_fn, task->status_arg,
502 STATUS_ERROR, _("Error loading %s"), name);
508 struct boot_task *boot(void *ctx, struct discover_boot_option *opt,
509 struct boot_command *cmd, int dry_run,
510 boot_status_fn status_fn, void *status_arg)
512 struct pb_url *image = NULL, *initrd = NULL, *dtb = NULL;
513 struct pb_url *image_sig = NULL, *initrd_sig = NULL, *dtb_sig = NULL,
515 const struct config *config = config_get();
516 struct boot_task *boot_task;
517 const char *boot_desc;
521 if (opt && opt->option->name)
522 boot_desc = opt->option->name;
523 else if (cmd && cmd->boot_image_file)
524 boot_desc = cmd->boot_image_file;
526 boot_desc = _("(unknown)");
528 update_status(status_fn, status_arg, STATUS_INFO,
529 _("Booting %s."), boot_desc);
531 if (cmd && cmd->boot_image_file) {
532 image = pb_url_parse(opt, cmd->boot_image_file);
533 } else if (opt && opt->boot_image) {
534 image = opt->boot_image->url;
536 pb_log("%s: no image specified\n", __func__);
537 update_status(status_fn, status_arg, STATUS_INFO,
538 _("Boot failed: no image specified"));
542 if (cmd && cmd->initrd_file) {
543 initrd = pb_url_parse(opt, cmd->initrd_file);
544 } else if (opt && opt->initrd) {
545 initrd = opt->initrd->url;
548 if (cmd && cmd->dtb_file) {
549 dtb = pb_url_parse(opt, cmd->dtb_file);
550 } else if (opt && opt->dtb) {
554 boot_task = talloc_zero(ctx, struct boot_task);
555 boot_task->dry_run = dry_run;
556 boot_task->status_fn = status_fn;
557 boot_task->status_arg = status_arg;
559 lockdown_type = lockdown_status();
560 boot_task->verify_signature = (lockdown_type == PB_LOCKDOWN_SIGN);
561 boot_task->decrypt_files = (lockdown_type == PB_LOCKDOWN_DECRYPT);
563 if (cmd && cmd->boot_args) {
564 boot_task->args = talloc_strdup(boot_task, cmd->boot_args);
565 } else if (opt && opt->option->boot_args) {
566 boot_task->args = talloc_strdup(boot_task,
567 opt->option->boot_args);
569 boot_task->args = NULL;
572 if (cmd && cmd->console && !config->manual_console)
573 boot_task->boot_console = talloc_strdup(boot_task, cmd->console);
575 boot_task->boot_console = config ? config->boot_console : NULL;
577 if (boot_task->verify_signature || boot_task->decrypt_files) {
578 if (cmd && cmd->args_sig_file) {
579 cmdline_sig = pb_url_parse(opt, cmd->args_sig_file);
580 } else if (opt && opt->args_sig_file) {
581 cmdline_sig = opt->args_sig_file->url;
583 pb_log("%s: no command line signature file"
584 " specified\n", __func__);
585 update_status(status_fn, status_arg, STATUS_INFO,
586 _("Boot failed: no command line"
587 " signature file specified"));
588 talloc_free(boot_task);
593 /* start async loads for boot resources */
594 rc = start_url_load(boot_task, "kernel image", image, &boot_task->image)
595 || start_url_load(boot_task, "initrd", initrd, &boot_task->initrd)
596 || start_url_load(boot_task, "dtb", dtb, &boot_task->dtb);
598 if (boot_task->verify_signature) {
599 /* Generate names of associated signature files and load */
601 image_sig = gpg_get_signature_url(ctx, image);
602 rc |= start_url_load(boot_task,
603 "kernel image signature", image_sig,
604 &boot_task->image_signature);
607 initrd_sig = gpg_get_signature_url(ctx, initrd);
608 rc |= start_url_load(boot_task, "initrd signature",
609 initrd_sig, &boot_task->initrd_signature);
612 dtb_sig = gpg_get_signature_url(ctx, dtb);
613 rc |= start_url_load(boot_task, "dtb signature",
614 dtb_sig, &boot_task->dtb_signature);
618 if (boot_task->verify_signature || boot_task->decrypt_files) {
619 rc |= start_url_load(boot_task,
620 "kernel command line signature", cmdline_sig,
621 &boot_task->cmdline_signature);
624 /* If all URLs are local, we may be done. */
626 /* Don't call boot_cancel() to preserve the status update */
627 boot_task->cancelled = true;
628 cleanup_cancellations(boot_task, NULL);
632 boot_process(NULL, boot_task);
637 void boot_cancel(struct boot_task *task)
639 task->cancelled = true;
641 update_status(task->status_fn, task->status_arg, STATUS_INFO,
642 _("Boot cancelled"));
644 cleanup_cancellations(task, NULL);