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"
28 static const char *boot_hook_dir = PKG_SYSCONF_DIR "/boot.d";
30 BOOT_HOOK_EXIT_OK = 0,
31 BOOT_HOOK_EXIT_UPDATE = 2,
35 struct load_url_result *image;
36 struct load_url_result *initrd;
37 struct load_url_result *dtb;
38 const char *local_image;
39 const char *local_initrd;
40 const char *local_dtb;
42 boot_status_fn status_fn;
49 * kexec_load - kexec load helper.
51 static int kexec_load(struct boot_task *boot_task)
56 char *s_initrd = NULL;
61 *p++ = pb_system_apps.kexec; /* 1 */
64 if (boot_task->local_initrd) {
65 s_initrd = talloc_asprintf(boot_task, "--initrd=%s",
66 boot_task->local_initrd);
68 *p++ = s_initrd; /* 3 */
71 if (boot_task->local_dtb) {
72 s_dtb = talloc_asprintf(boot_task, "--dtb=%s",
73 boot_task->local_dtb);
78 if (boot_task->args) {
79 s_args = talloc_asprintf(boot_task, "--append=%s",
82 *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");
210 setenv("boot_image", task->local_image, 1);
211 if (task->local_initrd)
212 setenv("boot_initrd", task->local_initrd, 1);
214 setenv("boot_dtb", task->local_dtb, 1);
216 setenv("boot_args", task->args, 1);
219 static int hook_filter(const struct dirent *dirent)
221 return dirent->d_type == DT_REG || dirent->d_type == DT_LNK;
224 static int hook_cmp(const struct dirent **a, const struct dirent **b)
226 return strcmp((*a)->d_name, (*b)->d_name);
229 static void run_boot_hooks(struct boot_task *task)
231 struct dirent **hooks;
234 n = scandir(boot_hook_dir, &hooks, hook_filter, hook_cmp);
238 update_status(task->status_fn, task->status_arg, BOOT_STATUS_INFO,
239 _("running boot hooks"));
241 boot_hook_setenv(task);
243 for (i = 0; i < n; i++) {
244 const char *argv[2] = { NULL, NULL };
245 struct process *process;
249 path = join_paths(task, boot_hook_dir, hooks[i]->d_name);
251 if (access(path, X_OK)) {
256 process = process_create(task);
259 process->path = path;
260 process->argv = argv;
261 process->keep_stdout = true;
263 pb_log("running boot hook %s\n", hooks[i]->d_name);
265 rc = process_run_sync(process);
267 pb_log("boot hook exec failed!\n");
269 } else if (WIFEXITED(process->exit_status) &&
270 WEXITSTATUS(process->exit_status)
271 == BOOT_HOOK_EXIT_UPDATE) {
272 /* if the hook returned with BOOT_HOOK_EXIT_UPDATE,
273 * then we process stdout to look for updated params
275 boot_hook_update(task, hooks[i]->d_name,
276 process->stdout_buf);
277 boot_hook_setenv(task);
280 process_release(process);
287 static bool load_pending(struct load_url_result *result)
289 return result && result->status == LOAD_ASYNC;
292 static int check_load(struct boot_task *task, const char *name,
293 struct load_url_result *result)
297 if (result->status != LOAD_ERROR)
300 update_status(task->status_fn, task->status_arg,
302 _("Couldn't load %s"), name);
306 static void cleanup_load(struct load_url_result *result)
310 if (result->status != LOAD_OK)
312 if (!result->cleanup_local)
314 unlink(result->local);
317 static void cleanup_cancellations(struct boot_task *task,
318 struct load_url_result *cur_result)
320 struct load_url_result *result, **results[] = {
321 &task->image, &task->initrd, &task->dtb,
323 bool pending = false;
326 for (i = 0; i < ARRAY_SIZE(results); i++) {
327 result = *results[i];
332 /* We need to cleanup and free any completed loads */
333 if (result == cur_result || result->status == LOAD_OK
334 || result->status == LOAD_ERROR) {
335 cleanup_load(result);
339 /* ... and cancel any pending loads, which we'll free in
340 * the completion callback */
341 } else if (result->status == LOAD_ASYNC) {
342 load_url_async_cancel(result);
345 /* if we're waiting for a cancellation, we still need to
346 * wait for the completion before freeing the boot task */
347 } else if (result->status == LOAD_CANCELLED) {
356 static void boot_process(struct load_url_result *result, void *data)
358 struct boot_task *task = data;
361 if (task->cancelled) {
362 cleanup_cancellations(task, result);
366 if (load_pending(task->image) ||
367 load_pending(task->initrd) ||
368 load_pending(task->dtb))
371 if (check_load(task, "kernel image", task->image) ||
372 check_load(task, "initrd", task->initrd) ||
373 check_load(task, "dtb", task->dtb))
376 /* we make a copy of the local paths, as the boot hooks might update
377 * and/or create these */
378 task->local_image = task->image ? task->image->local : NULL;
379 task->local_initrd = task->initrd ? task->initrd->local : NULL;
380 task->local_dtb = task->dtb ? task->dtb->local : NULL;
382 run_boot_hooks(task);
384 update_status(task->status_fn, task->status_arg, BOOT_STATUS_INFO,
385 _("performing kexec_load"));
387 rc = kexec_load(task);
389 update_status(task->status_fn, task->status_arg,
390 BOOT_STATUS_ERROR, _("kexec load failed"));
394 cleanup_load(task->image);
395 cleanup_load(task->initrd);
396 cleanup_load(task->dtb);
399 update_status(task->status_fn, task->status_arg,
401 _("performing kexec reboot"));
403 rc = kexec_reboot(task);
405 update_status(task->status_fn, task->status_arg,
407 _("kexec reboot failed"));
412 static int start_url_load(struct boot_task *task, const char *name,
413 struct pb_url *url, struct load_url_result **result)
418 *result = load_url_async(task, url, boot_process, task);
420 update_status(task->status_fn, task->status_arg,
422 _("Error loading %s"), name);
428 struct boot_task *boot(void *ctx, struct discover_boot_option *opt,
429 struct boot_command *cmd, int dry_run,
430 boot_status_fn status_fn, void *status_arg)
432 struct pb_url *image = NULL, *initrd = NULL, *dtb = NULL;
433 struct boot_task *boot_task;
434 const char *boot_desc;
437 if (opt && opt->option->name)
438 boot_desc = opt->option->name;
439 else if (cmd && cmd->boot_image_file)
440 boot_desc = cmd->boot_image_file;
442 boot_desc = _("(unknown)");
444 update_status(status_fn, status_arg, BOOT_STATUS_INFO,
445 _("Booting %s."), boot_desc);
447 if (cmd && cmd->boot_image_file) {
448 image = pb_url_parse(opt, cmd->boot_image_file);
449 } else if (opt && opt->boot_image) {
450 image = opt->boot_image->url;
452 pb_log("%s: no image specified\n", __func__);
453 update_status(status_fn, status_arg, BOOT_STATUS_INFO,
454 _("Boot failed: no image specified"));
458 if (cmd && cmd->initrd_file) {
459 initrd = pb_url_parse(opt, cmd->initrd_file);
460 } else if (opt && opt->initrd) {
461 initrd = opt->initrd->url;
464 if (cmd && cmd->dtb_file) {
465 dtb = pb_url_parse(opt, cmd->dtb_file);
466 } else if (opt && opt->dtb) {
470 boot_task = talloc_zero(ctx, struct boot_task);
471 boot_task->dry_run = dry_run;
472 boot_task->status_fn = status_fn;
473 boot_task->status_arg = status_arg;
475 if (cmd && cmd->boot_args) {
476 boot_task->args = talloc_strdup(boot_task, cmd->boot_args);
477 } else if (opt && opt->option->boot_args) {
478 boot_task->args = talloc_strdup(boot_task,
479 opt->option->boot_args);
481 boot_task->args = NULL;
484 /* start async loads for boot resources */
485 rc = start_url_load(boot_task, "kernel image", image, &boot_task->image)
486 || start_url_load(boot_task, "initrd", initrd, &boot_task->initrd)
487 || start_url_load(boot_task, "dtb", dtb, &boot_task->dtb);
489 /* If all URLs are local, we may be done. */
491 talloc_free(boot_task);
495 boot_process(NULL, boot_task);
500 void boot_cancel(struct boot_task *task)
502 task->cancelled = true;
504 update_status(task->status_fn, task->status_arg, BOOT_STATUS_INFO,
505 _("Boot cancelled"));
507 cleanup_cancellations(task, NULL);