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 static const char *boot_hook_dir = PKG_SYSCONF_DIR "/boot.d";
31 BOOT_HOOK_EXIT_OK = 0,
32 BOOT_HOOK_EXIT_UPDATE = 2,
36 struct load_url_result *image;
37 struct load_url_result *initrd;
38 struct load_url_result *dtb;
39 const char *local_image;
40 const char *local_initrd;
41 const char *local_dtb;
44 boot_status_fn status_fn;
51 * kexec_load - kexec load helper.
53 static int kexec_load(struct boot_task *boot_task)
58 char *s_initrd = NULL;
63 *p++ = pb_system_apps.kexec; /* 1 */
66 if (boot_task->local_initrd) {
67 s_initrd = talloc_asprintf(boot_task, "--initrd=%s",
68 boot_task->local_initrd);
70 *p++ = s_initrd; /* 3 */
73 if (boot_task->local_dtb) {
74 s_dtb = talloc_asprintf(boot_task, "--dtb=%s",
75 boot_task->local_dtb);
80 s_args = talloc_asprintf(boot_task, "--append=%s",
81 boot_task->args ?: "\"\"");
83 *p++ = s_args; /* 5 */
85 *p++ = boot_task->local_image; /* 6 */
88 result = process_run_simple_argv(boot_task, argv);
91 pb_log("%s: failed: (%d)\n", __func__, result);
97 * kexec_reboot - Helper to boot the new kernel.
99 * Must only be called after a successful call to kexec_load().
102 static int kexec_reboot(struct boot_task *task)
106 /* First try running shutdown. Init scripts should run 'exec -e' */
107 result = process_run_simple(task, pb_system_apps.shutdown, "-r",
110 /* On error, force a kexec with the -e option */
112 result = process_run_simple(task, pb_system_apps.kexec,
117 pb_log("%s: failed: (%d)\n", __func__, result);
119 /* okay, kexec -e -f */
121 result = process_run_simple(task, pb_system_apps.kexec,
126 pb_log("%s: failed: (%d)\n", __func__, result);
132 static void __attribute__((format(__printf__, 4, 5))) update_status(
133 boot_status_fn fn, void *arg, int type, char *fmt, ...)
135 struct boot_status status;
139 status.message = talloc_vasprintf(NULL, fmt, ap);
143 status.progress = -1;
144 status.detail = NULL;
146 pb_debug("boot status: [%d] %s\n", type, status.message);
150 talloc_free(status.message);
153 static void boot_hook_update_param(void *ctx, struct boot_task *task,
154 const char *name, const char *value)
159 } *param, params[] = {
160 { "boot_image", &task->local_image },
161 { "boot_initrd", &task->local_initrd },
162 { "boot_dtb", &task->local_dtb },
163 { "boot_args", &task->args },
167 for (param = params; param->name; param++) {
168 if (strcmp(param->name, name))
171 *param->p = talloc_strdup(ctx, value);
176 static void boot_hook_update(struct boot_task *task, const char *hookname,
179 char *line, *name, *val, *sep;
180 char *saveptr = NULL;
182 for (;; buf = NULL) {
184 line = strtok_r(buf, "\n", &saveptr);
188 sep = strchr(line, '=');
196 boot_hook_update_param(task, task, name, val);
198 pb_log("boot hook %s specified %s=%s\n",
199 hookname, name, val);
203 static void boot_hook_setenv(struct boot_task *task)
205 unsetenv("boot_image");
206 unsetenv("boot_initrd");
207 unsetenv("boot_dtb");
208 unsetenv("boot_args");
209 unsetenv("boot_tty");
211 setenv("boot_image", task->local_image, 1);
212 if (task->local_initrd)
213 setenv("boot_initrd", task->local_initrd, 1);
215 setenv("boot_dtb", task->local_dtb, 1);
217 setenv("boot_args", task->args, 1);
219 setenv("boot_tty", task->boot_tty, 1);
222 static int hook_filter(const struct dirent *dirent)
224 return dirent->d_type == DT_REG || dirent->d_type == DT_LNK;
227 static int hook_cmp(const struct dirent **a, const struct dirent **b)
229 return strcmp((*a)->d_name, (*b)->d_name);
232 static void run_boot_hooks(struct boot_task *task)
234 struct dirent **hooks;
237 n = scandir(boot_hook_dir, &hooks, hook_filter, hook_cmp);
241 update_status(task->status_fn, task->status_arg, BOOT_STATUS_INFO,
242 _("running boot hooks"));
244 boot_hook_setenv(task);
246 for (i = 0; i < n; i++) {
247 const char *argv[2] = { NULL, NULL };
248 struct process *process;
252 path = join_paths(task, boot_hook_dir, hooks[i]->d_name);
254 if (access(path, X_OK)) {
259 process = process_create(task);
262 process->path = path;
263 process->argv = argv;
264 process->keep_stdout = true;
266 pb_log("running boot hook %s\n", hooks[i]->d_name);
268 rc = process_run_sync(process);
270 pb_log("boot hook exec failed!\n");
272 } else if (WIFEXITED(process->exit_status) &&
273 WEXITSTATUS(process->exit_status)
274 == BOOT_HOOK_EXIT_UPDATE) {
275 /* if the hook returned with BOOT_HOOK_EXIT_UPDATE,
276 * then we process stdout to look for updated params
278 boot_hook_update(task, hooks[i]->d_name,
279 process->stdout_buf);
280 boot_hook_setenv(task);
283 process_release(process);
290 static bool load_pending(struct load_url_result *result)
292 return result && result->status == LOAD_ASYNC;
295 static int check_load(struct boot_task *task, const char *name,
296 struct load_url_result *result)
300 if (result->status != LOAD_ERROR)
303 update_status(task->status_fn, task->status_arg,
305 _("Couldn't load %s"), name);
309 static void cleanup_load(struct load_url_result *result)
313 if (result->status != LOAD_OK)
315 if (!result->cleanup_local)
317 unlink(result->local);
320 static void cleanup_cancellations(struct boot_task *task,
321 struct load_url_result *cur_result)
323 struct load_url_result *result, **results[] = {
324 &task->image, &task->initrd, &task->dtb,
326 bool pending = false;
329 for (i = 0; i < ARRAY_SIZE(results); i++) {
330 result = *results[i];
335 /* We need to cleanup and free any completed loads */
336 if (result == cur_result || result->status == LOAD_OK
337 || result->status == LOAD_ERROR) {
338 cleanup_load(result);
342 /* ... and cancel any pending loads, which we'll free in
343 * the completion callback */
344 } else if (result->status == LOAD_ASYNC) {
345 load_url_async_cancel(result);
348 /* if we're waiting for a cancellation, we still need to
349 * wait for the completion before freeing the boot task */
350 } else if (result->status == LOAD_CANCELLED) {
359 static void boot_process(struct load_url_result *result, void *data)
361 struct boot_task *task = data;
364 if (task->cancelled) {
365 cleanup_cancellations(task, result);
369 if (load_pending(task->image) ||
370 load_pending(task->initrd) ||
371 load_pending(task->dtb))
374 if (check_load(task, "kernel image", task->image) ||
375 check_load(task, "initrd", task->initrd) ||
376 check_load(task, "dtb", task->dtb))
379 /* we make a copy of the local paths, as the boot hooks might update
380 * and/or create these */
381 task->local_image = task->image ? task->image->local : NULL;
382 task->local_initrd = task->initrd ? task->initrd->local : NULL;
383 task->local_dtb = task->dtb ? task->dtb->local : NULL;
385 run_boot_hooks(task);
387 update_status(task->status_fn, task->status_arg, BOOT_STATUS_INFO,
388 _("performing kexec_load"));
390 rc = kexec_load(task);
392 update_status(task->status_fn, task->status_arg,
393 BOOT_STATUS_ERROR, _("kexec load failed"));
397 cleanup_load(task->image);
398 cleanup_load(task->initrd);
399 cleanup_load(task->dtb);
402 update_status(task->status_fn, task->status_arg,
404 _("performing kexec reboot"));
406 rc = kexec_reboot(task);
408 update_status(task->status_fn, task->status_arg,
410 _("kexec reboot failed"));
415 static int start_url_load(struct boot_task *task, const char *name,
416 struct pb_url *url, struct load_url_result **result)
421 *result = load_url_async(task, url, boot_process, task);
423 update_status(task->status_fn, task->status_arg,
425 _("Error loading %s"), name);
431 struct boot_task *boot(void *ctx, struct discover_boot_option *opt,
432 struct boot_command *cmd, int dry_run,
433 boot_status_fn status_fn, void *status_arg)
435 struct pb_url *image = NULL, *initrd = NULL, *dtb = NULL;
436 const struct config *config;
437 struct boot_task *boot_task;
438 const char *boot_desc;
441 if (opt && opt->option->name)
442 boot_desc = opt->option->name;
443 else if (cmd && cmd->boot_image_file)
444 boot_desc = cmd->boot_image_file;
446 boot_desc = _("(unknown)");
448 update_status(status_fn, status_arg, BOOT_STATUS_INFO,
449 _("Booting %s."), boot_desc);
451 if (cmd && cmd->boot_image_file) {
452 image = pb_url_parse(opt, cmd->boot_image_file);
453 } else if (opt && opt->boot_image) {
454 image = opt->boot_image->url;
456 pb_log("%s: no image specified\n", __func__);
457 update_status(status_fn, status_arg, BOOT_STATUS_INFO,
458 _("Boot failed: no image specified"));
462 if (cmd && cmd->initrd_file) {
463 initrd = pb_url_parse(opt, cmd->initrd_file);
464 } else if (opt && opt->initrd) {
465 initrd = opt->initrd->url;
468 if (cmd && cmd->dtb_file) {
469 dtb = pb_url_parse(opt, cmd->dtb_file);
470 } else if (opt && opt->dtb) {
474 boot_task = talloc_zero(ctx, struct boot_task);
475 boot_task->dry_run = dry_run;
476 boot_task->status_fn = status_fn;
477 boot_task->status_arg = status_arg;
479 if (cmd && cmd->boot_args) {
480 boot_task->args = talloc_strdup(boot_task, cmd->boot_args);
481 } else if (opt && opt->option->boot_args) {
482 boot_task->args = talloc_strdup(boot_task,
483 opt->option->boot_args);
485 boot_task->args = NULL;
489 boot_task->boot_tty = talloc_strdup(boot_task, cmd->tty);
491 config = config_get();
492 boot_task->boot_tty = config ? config->boot_tty : NULL;
495 /* start async loads for boot resources */
496 rc = start_url_load(boot_task, "kernel image", image, &boot_task->image)
497 || start_url_load(boot_task, "initrd", initrd, &boot_task->initrd)
498 || start_url_load(boot_task, "dtb", dtb, &boot_task->dtb);
501 /* Don't call boot_cancel() to preserve the status update */
502 boot_task->cancelled = true;
503 cleanup_cancellations(boot_task, NULL);
507 boot_process(NULL, boot_task);
512 void boot_cancel(struct boot_task *task)
514 task->cancelled = true;
516 update_status(task->status_fn, task->status_arg, BOOT_STATUS_INFO,
517 _("Boot cancelled"));
519 cleanup_cancellations(task, NULL);