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 boot_status status;
153 status.message = talloc_vasprintf(NULL, fmt, ap);
157 status.progress = -1;
158 status.detail = NULL;
160 pb_debug("boot status: [%d] %s\n", type, status.message);
164 talloc_free(status.message);
167 static void boot_hook_update_param(void *ctx, struct boot_task *task,
168 const char *name, const char *value)
173 } *param, params[] = {
174 { "boot_image", &task->local_image },
175 { "boot_initrd", &task->local_initrd },
176 { "boot_dtb", &task->local_dtb },
177 { "boot_args", &task->args },
181 for (param = params; param->name; param++) {
182 if (strcmp(param->name, name))
185 *param->p = talloc_strdup(ctx, value);
190 static void boot_hook_update(struct boot_task *task, const char *hookname,
193 char *line, *name, *val, *sep;
194 char *saveptr = NULL;
196 for (;; buf = NULL) {
198 line = strtok_r(buf, "\n", &saveptr);
202 sep = strchr(line, '=');
210 boot_hook_update_param(task, task, name, val);
212 pb_log("boot hook %s specified %s=%s\n",
213 hookname, name, val);
217 static void boot_hook_setenv(struct boot_task *task)
219 unsetenv("boot_image");
220 unsetenv("boot_initrd");
221 unsetenv("boot_dtb");
222 unsetenv("boot_args");
223 unsetenv("boot_console");
225 setenv("boot_image", task->local_image, 1);
226 if (task->local_initrd)
227 setenv("boot_initrd", task->local_initrd, 1);
229 setenv("boot_dtb", task->local_dtb, 1);
231 setenv("boot_args", task->args, 1);
232 if (task->boot_console)
233 setenv("boot_console", task->boot_console, 1);
236 static int hook_filter(const struct dirent *dirent)
238 return dirent->d_type == DT_REG || dirent->d_type == DT_LNK;
241 static int hook_cmp(const struct dirent **a, const struct dirent **b)
243 return strcmp((*a)->d_name, (*b)->d_name);
246 static void run_boot_hooks(struct boot_task *task)
248 struct dirent **hooks;
251 n = scandir(boot_hook_dir, &hooks, hook_filter, hook_cmp);
255 update_status(task->status_fn, task->status_arg, BOOT_STATUS_INFO,
256 _("running boot hooks"));
258 boot_hook_setenv(task);
260 for (i = 0; i < n; i++) {
261 const char *argv[2] = { NULL, NULL };
262 struct process *process;
266 path = join_paths(task, boot_hook_dir, hooks[i]->d_name);
268 if (access(path, X_OK)) {
273 process = process_create(task);
276 process->path = path;
277 process->argv = argv;
278 process->keep_stdout = true;
280 pb_log("running boot hook %s\n", hooks[i]->d_name);
282 rc = process_run_sync(process);
284 pb_log("boot hook exec failed!\n");
286 } else if (WIFEXITED(process->exit_status) &&
287 WEXITSTATUS(process->exit_status)
288 == BOOT_HOOK_EXIT_UPDATE) {
289 /* if the hook returned with BOOT_HOOK_EXIT_UPDATE,
290 * then we process stdout to look for updated params
292 boot_hook_update(task, hooks[i]->d_name,
293 process->stdout_buf);
294 boot_hook_setenv(task);
297 process_release(process);
304 static bool load_pending(struct load_url_result *result)
306 return result && result->status == LOAD_ASYNC;
309 static int check_load(struct boot_task *task, const char *name,
310 struct load_url_result *result)
314 if (result->status != LOAD_ERROR)
317 update_status(task->status_fn, task->status_arg,
319 _("Couldn't load %s"), name);
323 static void cleanup_load(struct load_url_result *result)
327 if (result->status != LOAD_OK)
329 if (!result->cleanup_local)
331 unlink(result->local);
334 static void cleanup_cancellations(struct boot_task *task,
335 struct load_url_result *cur_result)
337 struct load_url_result *result, **results[] = {
338 &task->image, &task->initrd, &task->dtb,
340 bool pending = false;
343 for (i = 0; i < ARRAY_SIZE(results); i++) {
344 result = *results[i];
349 /* We need to cleanup and free any completed loads */
350 if (result == cur_result || result->status == LOAD_OK
351 || result->status == LOAD_ERROR) {
352 cleanup_load(result);
356 /* ... and cancel any pending loads, which we'll free in
357 * the completion callback */
358 } else if (result->status == LOAD_ASYNC) {
359 load_url_async_cancel(result);
362 /* if we're waiting for a cancellation, we still need to
363 * wait for the completion before freeing the boot task */
364 } else if (result->status == LOAD_CANCELLED) {
373 static void boot_process(struct load_url_result *result, void *data)
375 struct boot_task *task = data;
378 if (task->cancelled) {
379 cleanup_cancellations(task, result);
383 if (load_pending(task->image) ||
384 load_pending(task->initrd) ||
385 load_pending(task->dtb))
388 if (check_load(task, "kernel image", task->image) ||
389 check_load(task, "initrd", task->initrd) ||
390 check_load(task, "dtb", task->dtb))
393 if (task->verify_signature) {
394 if (load_pending(task->image_signature) ||
395 load_pending(task->initrd_signature) ||
396 load_pending(task->dtb_signature) ||
397 load_pending(task->cmdline_signature))
400 if (task->decrypt_files) {
401 if (load_pending(task->cmdline_signature))
405 if (task->verify_signature) {
406 if (check_load(task, "kernel image signature",
407 task->image_signature) ||
408 check_load(task, "initrd signature",
409 task->initrd_signature) ||
410 check_load(task, "dtb signature",
411 task->dtb_signature) ||
412 check_load(task, "command line signature",
413 task->cmdline_signature))
416 if (task->decrypt_files) {
417 if (load_pending(task->cmdline_signature))
420 if (check_load(task, "command line signature",
421 task->cmdline_signature))
422 goto no_decrypt_sig_load;
425 /* we make a copy of the local paths, as the boot hooks might update
426 * and/or create these */
427 task->local_image = task->image ? task->image->local : NULL;
428 task->local_initrd = task->initrd ? task->initrd->local : NULL;
429 task->local_dtb = task->dtb ? task->dtb->local : NULL;
431 if (task->verify_signature) {
432 task->local_image_signature = task->image_signature ?
433 task->image_signature->local : NULL;
434 task->local_initrd_signature = task->initrd_signature ?
435 task->initrd_signature->local : NULL;
436 task->local_dtb_signature = task->dtb_signature ?
437 task->dtb_signature->local : NULL;
439 if (task->verify_signature || task->decrypt_files) {
440 task->local_cmdline_signature = task->cmdline_signature ?
441 task->cmdline_signature->local : NULL;
444 run_boot_hooks(task);
446 update_status(task->status_fn, task->status_arg, BOOT_STATUS_INFO,
447 _("performing kexec_load"));
449 rc = kexec_load(task);
450 if (rc == KEXEC_LOAD_DECRYPTION_FALURE) {
451 update_status(task->status_fn, task->status_arg,
452 BOOT_STATUS_ERROR, _("decryption failed"));
454 else if (rc == KEXEC_LOAD_SIGNATURE_FAILURE) {
455 update_status(task->status_fn, task->status_arg,
457 _("signature verification failed"));
459 else if (rc == KEXEC_LOAD_SIG_SETUP_INVALID) {
460 update_status(task->status_fn, task->status_arg,
462 _("invalid signature configuration"));
465 update_status(task->status_fn, task->status_arg,
467 _("kexec load failed"));
471 cleanup_load(task->image_signature);
472 cleanup_load(task->initrd_signature);
473 cleanup_load(task->dtb_signature);
476 cleanup_load(task->cmdline_signature);
479 cleanup_load(task->image);
480 cleanup_load(task->initrd);
481 cleanup_load(task->dtb);
484 update_status(task->status_fn, task->status_arg,
486 _("performing kexec reboot"));
488 rc = kexec_reboot(task);
490 update_status(task->status_fn, task->status_arg,
492 _("kexec reboot failed"));
497 static int start_url_load(struct boot_task *task, const char *name,
498 struct pb_url *url, struct load_url_result **result)
503 *result = load_url_async(task, url, boot_process, task);
505 update_status(task->status_fn, task->status_arg,
507 _("Error loading %s"), name);
513 struct boot_task *boot(void *ctx, struct discover_boot_option *opt,
514 struct boot_command *cmd, int dry_run,
515 boot_status_fn status_fn, void *status_arg)
517 struct pb_url *image = NULL, *initrd = NULL, *dtb = NULL;
518 struct pb_url *image_sig = NULL, *initrd_sig = NULL, *dtb_sig = NULL,
520 const struct config *config;
521 struct boot_task *boot_task;
522 const char *boot_desc;
526 if (opt && opt->option->name)
527 boot_desc = opt->option->name;
528 else if (cmd && cmd->boot_image_file)
529 boot_desc = cmd->boot_image_file;
531 boot_desc = _("(unknown)");
533 update_status(status_fn, status_arg, BOOT_STATUS_INFO,
534 _("Booting %s."), boot_desc);
536 if (cmd && cmd->boot_image_file) {
537 image = pb_url_parse(opt, cmd->boot_image_file);
538 } else if (opt && opt->boot_image) {
539 image = opt->boot_image->url;
541 pb_log("%s: no image specified\n", __func__);
542 update_status(status_fn, status_arg, BOOT_STATUS_INFO,
543 _("Boot failed: no image specified"));
547 if (cmd && cmd->initrd_file) {
548 initrd = pb_url_parse(opt, cmd->initrd_file);
549 } else if (opt && opt->initrd) {
550 initrd = opt->initrd->url;
553 if (cmd && cmd->dtb_file) {
554 dtb = pb_url_parse(opt, cmd->dtb_file);
555 } else if (opt && opt->dtb) {
559 boot_task = talloc_zero(ctx, struct boot_task);
560 boot_task->dry_run = dry_run;
561 boot_task->status_fn = status_fn;
562 boot_task->status_arg = status_arg;
564 lockdown_type = lockdown_status();
565 boot_task->verify_signature = (lockdown_type == PB_LOCKDOWN_SIGN);
566 boot_task->decrypt_files = (lockdown_type == PB_LOCKDOWN_DECRYPT);
568 if (cmd && cmd->boot_args) {
569 boot_task->args = talloc_strdup(boot_task, cmd->boot_args);
570 } else if (opt && opt->option->boot_args) {
571 boot_task->args = talloc_strdup(boot_task,
572 opt->option->boot_args);
574 boot_task->args = NULL;
577 if (cmd && cmd->console)
578 boot_task->boot_console = talloc_strdup(boot_task, cmd->console);
580 config = config_get();
581 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, BOOT_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", image, &boot_task->image)
602 || start_url_load(boot_task, "initrd", initrd, &boot_task->initrd)
603 || start_url_load(boot_task, "dtb", dtb, &boot_task->dtb);
605 if (boot_task->verify_signature) {
606 /* Generate names of associated signature files and load */
608 image_sig = gpg_get_signature_url(ctx, image);
609 rc |= start_url_load(boot_task,
610 "kernel image signature", image_sig,
611 &boot_task->image_signature);
614 initrd_sig = gpg_get_signature_url(ctx, initrd);
615 rc |= start_url_load(boot_task, "initrd signature",
616 initrd_sig, &boot_task->initrd_signature);
619 dtb_sig = gpg_get_signature_url(ctx, dtb);
620 rc |= start_url_load(boot_task, "dtb signature",
621 dtb_sig, &boot_task->dtb_signature);
625 if (boot_task->verify_signature || boot_task->decrypt_files) {
626 rc |= start_url_load(boot_task,
627 "kernel command line signature", cmdline_sig,
628 &boot_task->cmdline_signature);
631 /* If all URLs are local, we may be done. */
633 /* Don't call boot_cancel() to preserve the status update */
634 boot_task->cancelled = true;
635 cleanup_cancellations(boot_task, NULL);
639 boot_process(NULL, boot_task);
644 void boot_cancel(struct boot_task *task)
646 task->cancelled = true;
648 update_status(task->status_fn, task->status_arg, BOOT_STATUS_INFO,
649 _("Boot cancelled"));
651 cleanup_cancellations(task, NULL);