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/security.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;
49 status.boot_active = type == STATUS_INFO;
51 pb_debug("boot status: [%d] %s\n", type, status.message);
55 talloc_free(status.message);
59 * kexec_load - kexec load helper.
61 static int kexec_load(struct boot_task *boot_task)
63 struct process *process;
64 char *s_initrd = NULL;
72 boot_task->local_initrd_override = NULL;
73 boot_task->local_dtb_override = NULL;
74 boot_task->local_image_override = NULL;
76 if ((result = validate_boot_files(boot_task))) {
77 if (result == KEXEC_LOAD_DECRYPTION_FALURE) {
78 pb_log("%s: Aborting kexec due to"
79 " 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__);
89 const char* local_initrd = (boot_task->local_initrd_override) ?
90 boot_task->local_initrd_override : boot_task->local_initrd;
91 const char* local_dtb = (boot_task->local_dtb_override) ?
92 boot_task->local_dtb_override : boot_task->local_dtb;
93 const char* local_image = (boot_task->local_image_override) ?
94 boot_task->local_image_override : boot_task->local_image;
96 process = process_create(boot_task);
98 pb_log_fn("failed to create process\n");
102 process->path = pb_system_apps.kexec;
103 process->argv = argv;
104 process->keep_stdout = true;
105 process->add_stderr = true;
108 *p++ = pb_system_apps.kexec; /* 1 */
111 if (pb_log_get_debug()) {
112 *p++ = "--debug"; /* 3 */
116 s_initrd = talloc_asprintf(boot_task, "--initrd=%s",
119 *p++ = s_initrd; /* 4 */
123 s_dtb = talloc_asprintf(boot_task, "--dtb=%s",
126 *p++ = s_dtb; /* 5 */
129 s_args = talloc_asprintf(boot_task, "--append=%s",
130 boot_task->args ?: "\"\"");
132 *p++ = s_args; /* 6 */
134 *p++ = local_image; /* 7 */
137 result = process_run_sync(process);
139 pb_log_fn("failed to run process\n");
143 result = process->exit_status;
146 pb_log_fn("failed: (%d)\n", result);
147 update_status(boot_task->status_fn, boot_task->status_arg,
148 STATUS_ERROR, "%s", process->stdout_buf);
152 validate_boot_files_cleanup(boot_task);
158 * kexec_reboot - Helper to boot the new kernel.
160 * Must only be called after a successful call to kexec_load().
163 static int kexec_reboot(struct boot_task *task)
167 /* First try running shutdown. Init scripts should run 'exec -e' */
168 result = process_run_simple(task, pb_system_apps.shutdown, "-r",
171 /* On error, force a kexec with the -e option */
173 result = process_run_simple(task, pb_system_apps.kexec,
178 pb_log_fn("failed: (%d)\n", result);
180 /* okay, kexec -e -f */
182 result = process_run_simple(task, pb_system_apps.kexec,
187 pb_log_fn("failed: (%d)\n", result);
193 static void boot_hook_update_param(void *ctx, struct boot_task *task,
194 const char *name, const char *value)
199 } *param, params[] = {
200 { "boot_image", &task->local_image },
201 { "boot_initrd", &task->local_initrd },
202 { "boot_dtb", &task->local_dtb },
203 { "boot_args", &task->args },
207 for (param = params; param->name; param++) {
208 if (strcmp(param->name, name))
211 *param->p = talloc_strdup(ctx, value);
216 static void boot_hook_update(struct boot_task *task, const char *hookname,
219 char *line, *name, *val, *sep;
220 char *saveptr = NULL;
222 for (;; buf = NULL) {
224 line = strtok_r(buf, "\n", &saveptr);
228 sep = strchr(line, '=');
236 boot_hook_update_param(task, task, name, val);
238 pb_log("boot hook %s specified %s=%s\n",
239 hookname, name, val);
243 static void boot_hook_setenv(struct boot_task *task)
245 unsetenv("boot_image");
246 unsetenv("boot_initrd");
247 unsetenv("boot_dtb");
248 unsetenv("boot_args");
249 unsetenv("boot_console");
251 setenv("boot_image", task->local_image, 1);
252 if (task->local_initrd)
253 setenv("boot_initrd", task->local_initrd, 1);
255 setenv("boot_dtb", task->local_dtb, 1);
257 setenv("boot_args", task->args, 1);
258 if (task->boot_console)
259 setenv("boot_console", task->boot_console, 1);
262 static int hook_filter(const struct dirent *dirent)
264 return dirent->d_type == DT_REG || dirent->d_type == DT_LNK;
267 static int hook_cmp(const struct dirent **a, const struct dirent **b)
269 return strcmp((*a)->d_name, (*b)->d_name);
272 static void run_boot_hooks(struct boot_task *task)
274 struct dirent **hooks;
277 n = scandir(boot_hook_dir, &hooks, hook_filter, hook_cmp);
281 update_status(task->status_fn, task->status_arg, STATUS_INFO,
282 _("Running boot hooks"));
284 boot_hook_setenv(task);
286 for (i = 0; i < n; i++) {
287 const char *argv[2] = { NULL, NULL };
288 struct process *process;
292 path = join_paths(task, boot_hook_dir, hooks[i]->d_name);
294 if (access(path, X_OK)) {
299 process = process_create(task);
302 process->path = path;
303 process->argv = argv;
304 process->keep_stdout = true;
306 pb_log("running boot hook %s\n", hooks[i]->d_name);
308 rc = process_run_sync(process);
310 pb_log("boot hook exec failed!\n");
312 } else if (WIFEXITED(process->exit_status) &&
313 WEXITSTATUS(process->exit_status)
314 == BOOT_HOOK_EXIT_UPDATE) {
315 /* if the hook returned with BOOT_HOOK_EXIT_UPDATE,
316 * then we process stdout to look for updated params
318 boot_hook_update(task, hooks[i]->d_name,
319 process->stdout_buf);
320 boot_hook_setenv(task);
323 process_release(process);
330 static bool load_pending(struct load_url_result *result)
332 return !result || result->status == LOAD_ASYNC;
335 static int check_load(struct boot_task *task, const char *name,
336 struct load_url_result *result)
341 if (result->status != LOAD_ERROR) {
342 update_status(task->status_fn, task->status_arg,
344 _("Loaded %s from %s"), name,
345 pb_url_to_string(result->url));
349 pb_log("Failed to load %s from %s\n", name,
350 pb_url_to_string(result->url));
351 update_status(task->status_fn, task->status_arg,
353 _("Couldn't load %s from %s"), name,
354 pb_url_to_string(result->url));
358 static void cleanup_load(struct load_url_result *result)
362 if (result->status != LOAD_OK)
364 if (!result->cleanup_local)
366 unlink(result->local);
369 static void cleanup_cancellations(struct boot_task *task,
370 struct load_url_result *cur_result)
372 struct boot_resource *resource;
373 struct load_url_result *result;
374 bool pending = false;
376 list_for_each_entry(&task->resources, resource, list) {
377 result = resource->result;
379 /* Nothing to do if a load hasn't actually started yet */
383 /* We need to cleanup and free any completed loads */
384 if (result == cur_result || result->status == LOAD_OK
385 || result->status == LOAD_ERROR) {
386 cleanup_load(result);
387 /* ... and cancel any pending loads, which we'll free in
388 * the completion callback */
389 } else if (result->status == LOAD_ASYNC) {
390 load_url_async_cancel(result);
393 /* if we're waiting for a cancellation, we still need to
394 * wait for the completion before freeing the boot task */
395 } else if (result->status == LOAD_CANCELLED) {
404 static void boot_process(struct load_url_result *result, void *data)
406 struct boot_task *task = data;
407 struct boot_resource *resource;
410 if (task->cancelled) {
411 cleanup_cancellations(task, result);
415 list_for_each_entry(&task->resources, resource, list)
416 if (load_pending(resource->result))
419 list_for_each_entry(&task->resources, resource, list) {
420 if (check_load(task, resource->name, resource->result))
422 *resource->local_path = resource->result->local;
425 run_boot_hooks(task);
427 update_status(task->status_fn, task->status_arg, STATUS_INFO,
428 _("Performing kexec load"));
430 rc = kexec_load(task);
431 pb_log_fn("kexec_load returned %d\n", rc);
432 if (rc == KEXEC_LOAD_DECRYPTION_FALURE) {
433 update_status(task->status_fn, task->status_arg,
434 STATUS_ERROR, _("Decryption failed"));
436 else if (rc == KEXEC_LOAD_SIGNATURE_FAILURE) {
437 update_status(task->status_fn, task->status_arg,
439 _("Signature verification failed"));
441 else if (rc == KEXEC_LOAD_SIG_SETUP_INVALID) {
442 update_status(task->status_fn, task->status_arg,
444 _("Invalid signature configuration"));
448 list_for_each_entry(&task->resources, resource, list)
449 cleanup_load(resource->result);
452 update_status(task->status_fn, task->status_arg,
453 STATUS_INFO, _("Performing kexec reboot"));
455 rc = kexec_reboot(task);
457 update_status(task->status_fn, task->status_arg,
459 _("kexec reboot failed"));
462 pb_log("Failed to load all boot resources\n");
466 static int start_url_load(struct boot_task *task, struct boot_resource *res)
471 res->result = load_url_async(task, res->url, boot_process,
472 task, NULL, task->status_arg);
474 pb_log("Error starting load for %s at %s\n",
475 res->name, pb_url_to_string(res->url));
476 update_status(task->status_fn, task->status_arg,
477 STATUS_ERROR, _("Error loading %s"),
484 static struct boot_resource *add_boot_resource(struct boot_task *task,
485 const char *name, struct pb_url *url,
486 const char **local_path)
488 struct boot_resource *res;
493 res = talloc_zero(task, struct boot_resource);
497 res->name = talloc_strdup(res, name);
498 res->url = pb_url_copy(res, url);
499 res->local_path = local_path;
501 list_add(&task->resources, &res->list);
505 struct boot_task *boot(void *ctx, struct discover_boot_option *opt,
506 struct boot_command *cmd, int dry_run,
507 boot_status_fn status_fn, void *status_arg)
509 struct pb_url *image = NULL, *initrd = NULL, *dtb = NULL;
510 struct pb_url *image_sig = NULL, *initrd_sig = NULL, *dtb_sig = NULL,
512 struct boot_resource *image_res, *initrd_res, *dtb_res, *tmp;
513 const struct config *config = config_get();
514 struct boot_task *boot_task;
515 const char *boot_desc;
519 if (opt && opt->option->name)
520 boot_desc = opt->option->name;
521 else if (cmd && cmd->boot_image_file)
522 boot_desc = cmd->boot_image_file;
524 boot_desc = _("(unknown)");
526 update_status(status_fn, status_arg, STATUS_INFO,
527 _("Booting %s"), boot_desc);
529 boot_task = talloc_zero(ctx, struct boot_task);
530 boot_task->dry_run = dry_run;
531 boot_task->status_fn = status_fn;
532 boot_task->status_arg = status_arg;
534 if (cmd && cmd->boot_image_file) {
535 image = pb_url_parse(boot_task, cmd->boot_image_file);
536 } else if (opt && opt->boot_image) {
537 image = opt->boot_image->url;
539 pb_log_fn("no image specified\n");
540 update_status(status_fn, status_arg, STATUS_ERROR,
541 _("Boot failed: no image specified"));
542 talloc_free(boot_task);
546 if (cmd && cmd->initrd_file) {
547 initrd = pb_url_parse(boot_task, cmd->initrd_file);
548 } else if (opt && opt->initrd) {
549 initrd = opt->initrd->url;
552 if (cmd && cmd->dtb_file) {
553 dtb = pb_url_parse(boot_task, cmd->dtb_file);
554 } else if (opt && opt->dtb) {
558 if (opt && opt->proxy) {
559 setenv("http_proxy", opt->proxy, 1);
560 setenv("https_proxy", opt->proxy, 1);
563 list_init(&boot_task->resources);
565 lockdown_type = lockdown_status();
566 boot_task->verify_signature = (lockdown_type == PB_LOCKDOWN_SIGN);
567 boot_task->decrypt_files = (lockdown_type == PB_LOCKDOWN_DECRYPT);
569 if (cmd && cmd->boot_args) {
570 boot_task->args = talloc_strdup(boot_task, cmd->boot_args);
571 } else if (opt && opt->option->boot_args) {
572 boot_task->args = talloc_strdup(boot_task,
573 opt->option->boot_args);
575 boot_task->args = NULL;
578 if (cmd && cmd->console && !config->manual_console)
579 boot_task->boot_console = talloc_strdup(boot_task, cmd->console);
581 boot_task->boot_console = config ? config->boot_console : NULL;
583 if (boot_task->verify_signature || boot_task->decrypt_files) {
584 if (cmd && cmd->args_sig_file) {
585 cmdline_sig = pb_url_parse(opt, cmd->args_sig_file);
586 } else if (opt && opt->args_sig_file) {
587 cmdline_sig = opt->args_sig_file->url;
589 pb_log("%s: no command line signature file"
590 " specified\n", __func__);
591 update_status(status_fn, status_arg, STATUS_ERROR,
592 _("Boot failed: no command line"
593 " signature file specified"));
594 talloc_free(boot_task);
599 image_res = add_boot_resource(boot_task, _("kernel image"), image,
600 &boot_task->local_image);
601 initrd_res = add_boot_resource(boot_task, _("initrd"), initrd,
602 &boot_task->local_initrd);
603 dtb_res = add_boot_resource(boot_task, _("dtb"), dtb,
604 &boot_task->local_dtb);
606 /* start async loads for boot resources */
607 rc = start_url_load(boot_task, image_res)
608 || start_url_load(boot_task, initrd_res)
609 || start_url_load(boot_task, dtb_res);
611 if (boot_task->verify_signature) {
612 /* Generate names of associated signature files and load */
614 image_sig = get_signature_url(ctx, image);
615 tmp = add_boot_resource(boot_task,
616 _("kernel image signature"), image_sig,
617 &boot_task->local_image_signature);
618 rc |= start_url_load(boot_task, tmp);
621 initrd_sig = get_signature_url(ctx, initrd);
622 tmp = add_boot_resource(boot_task,
623 _("initrd signature"), initrd_sig,
624 &boot_task->local_initrd_signature);
625 rc |= start_url_load(boot_task, tmp);
628 dtb_sig = get_signature_url(ctx, dtb);
629 tmp = add_boot_resource(boot_task,
630 _("dtb signature"), dtb_sig,
631 &boot_task->local_dtb_signature);
632 rc |= start_url_load(boot_task, tmp);
636 if (boot_task->verify_signature || boot_task->decrypt_files) {
637 tmp = add_boot_resource(boot_task,
638 _("kernel command line signature"), cmdline_sig,
639 &boot_task->local_cmdline_signature);
640 rc |= start_url_load(boot_task, tmp);
643 /* If all URLs are local, we may be done. */
645 /* Don't call boot_cancel() to preserve the status update */
646 boot_task->cancelled = true;
647 cleanup_cancellations(boot_task, NULL);
651 boot_process(NULL, boot_task);
656 void boot_cancel(struct boot_task *task)
658 task->cancelled = true;
660 update_status(task->status_fn, task->status_arg, STATUS_ERROR,
661 _("Boot cancelled"));
663 cleanup_cancellations(task, NULL);