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>
22 #include "device-handler.h"
27 static const char *boot_hook_dir = PKG_SYSCONF_DIR "/boot.d";
29 BOOT_HOOK_EXIT_OK = 0,
30 BOOT_HOOK_EXIT_UPDATE = 2,
34 struct load_url_result *image;
35 struct load_url_result *initrd;
36 struct load_url_result *dtb;
37 const char *local_image;
38 const char *local_initrd;
39 const char *local_dtb;
41 boot_status_fn status_fn;
48 * kexec_load - kexec load helper.
50 static int kexec_load(struct boot_task *boot_task)
55 char *s_initrd = NULL;
60 *p++ = pb_system_apps.kexec; /* 1 */
63 if (boot_task->local_initrd) {
64 s_initrd = talloc_asprintf(boot_task, "--initrd=%s",
65 boot_task->local_initrd);
67 *p++ = s_initrd; /* 3 */
70 if (boot_task->local_dtb) {
71 s_dtb = talloc_asprintf(boot_task, "--dtb=%s",
72 boot_task->local_dtb);
77 if (boot_task->args) {
78 s_args = talloc_asprintf(boot_task, "--append=%s",
81 *p++ = s_args; /* 5 */
84 *p++ = boot_task->local_image; /* 6 */
87 result = process_run_simple_argv(boot_task, argv);
90 pb_log("%s: failed: (%d)\n", __func__, result);
96 * kexec_reboot - Helper to boot the new kernel.
98 * Must only be called after a successful call to kexec_load().
101 static int kexec_reboot(struct boot_task *task)
105 /* First try running shutdown. Init scripts should run 'exec -e' */
106 result = process_run_simple(task, pb_system_apps.shutdown, "-r",
109 /* On error, force a kexec with the -e option */
111 result = process_run_simple(task, pb_system_apps.kexec,
116 pb_log("%s: failed: (%d)\n", __func__, result);
118 /* okay, kexec -e -f */
120 result = process_run_simple(task, pb_system_apps.kexec,
125 pb_log("%s: failed: (%d)\n", __func__, result);
131 static void __attribute__((format(__printf__, 4, 5))) update_status(
132 boot_status_fn fn, void *arg, int type, char *fmt, ...)
134 struct boot_status status;
138 status.message = talloc_vasprintf(NULL, fmt, ap);
142 status.progress = -1;
143 status.detail = NULL;
145 pb_debug("boot status: [%d] %s\n", type, status.message);
149 talloc_free(status.message);
152 static void boot_hook_update_param(void *ctx, struct boot_task *task,
153 const char *name, const char *value)
158 } *param, params[] = {
159 { "boot_image", &task->local_image },
160 { "boot_initrd", &task->local_initrd },
161 { "boot_dtb", &task->local_dtb },
162 { "boot_args", &task->args },
166 for (param = params; param->name; param++) {
167 if (strcmp(param->name, name))
170 *param->p = talloc_strdup(ctx, value);
175 static void boot_hook_update(struct boot_task *task, const char *hookname,
178 char *line, *name, *val, *sep;
181 for (;; buf = NULL) {
183 line = strtok_r(buf, "\n", &saveptr);
187 sep = strchr(line, '=');
195 boot_hook_update_param(task, task, name, val);
197 pb_log("boot hook %s specified %s=%s\n",
198 hookname, name, val);
202 static void boot_hook_setenv(struct boot_task *task)
204 unsetenv("boot_image");
205 unsetenv("boot_initrd");
206 unsetenv("boot_dtb");
207 unsetenv("boot_args");
209 setenv("boot_image", task->local_image, 1);
210 if (task->local_initrd)
211 setenv("boot_initrd", task->local_initrd, 1);
213 setenv("boot_dtb", task->local_dtb, 1);
215 setenv("boot_args", task->args, 1);
218 static int hook_filter(const struct dirent *dirent)
220 return dirent->d_type == DT_REG || dirent->d_type == DT_LNK;
223 static int hook_cmp(const struct dirent **a, const struct dirent **b)
225 return strcmp((*a)->d_name, (*b)->d_name);
228 static void run_boot_hooks(struct boot_task *task)
230 struct dirent **hooks;
233 n = scandir(boot_hook_dir, &hooks, hook_filter, hook_cmp);
237 update_status(task->status_fn, task->status_arg, BOOT_STATUS_INFO,
238 "running boot hooks");
240 boot_hook_setenv(task);
242 for (i = 0; i < n; i++) {
243 const char *argv[2] = { NULL, NULL };
244 struct process *process;
248 path = join_paths(task, boot_hook_dir, hooks[i]->d_name);
250 if (access(path, X_OK)) {
255 process = process_create(task);
258 process->path = path;
259 process->argv = argv;
260 process->keep_stdout = true;
262 pb_log("running boot hook %s\n", hooks[i]->d_name);
264 rc = process_run_sync(process);
266 pb_log("boot hook exec failed!\n");
268 } else if (WIFEXITED(process->exit_status) &&
269 WEXITSTATUS(process->exit_status)
270 == BOOT_HOOK_EXIT_UPDATE) {
271 /* if the hook returned with BOOT_HOOK_EXIT_UPDATE,
272 * then we process stdout to look for updated params
274 boot_hook_update(task, hooks[i]->d_name,
275 process->stdout_buf);
276 boot_hook_setenv(task);
279 process_release(process);
286 static bool load_pending(struct load_url_result *result)
288 return result && result->status == LOAD_ASYNC;
291 static int check_load(struct boot_task *task, const char *name,
292 struct load_url_result *result)
296 if (result->status != LOAD_ERROR)
299 update_status(task->status_fn, task->status_arg,
301 "Couldn't load %s", name);
305 static void cleanup_load(struct load_url_result *result)
309 if (result->status != LOAD_OK)
311 if (!result->cleanup_local)
313 unlink(result->local);
316 static void cleanup_cancellations(struct boot_task *task,
317 struct load_url_result *cur_result)
319 struct load_url_result *result, **results[] = {
320 &task->image, &task->initrd, &task->dtb,
322 bool pending = false;
325 for (i = 0; i < ARRAY_SIZE(results); i++) {
326 result = *results[i];
331 /* We need to cleanup and free any completed loads */
332 if (result == cur_result || result->status == LOAD_OK
333 || result->status == LOAD_ERROR) {
334 cleanup_load(result);
338 /* ... and cancel any pending loads, which we'll free in
339 * the completion callback */
340 } else if (result->status == LOAD_ASYNC) {
341 load_url_async_cancel(result);
350 static void boot_process(struct load_url_result *result, void *data)
352 struct boot_task *task = data;
355 if (task->cancelled) {
356 cleanup_cancellations(task, result);
360 if (load_pending(task->image) ||
361 load_pending(task->initrd) ||
362 load_pending(task->dtb))
365 if (check_load(task, "kernel image", task->image) ||
366 check_load(task, "initrd", task->initrd) ||
367 check_load(task, "dtb", task->dtb))
370 /* we make a copy of the local paths, as the boot hooks might update
371 * and/or create these */
372 task->local_image = task->image ? task->image->local : NULL;
373 task->local_initrd = task->initrd ? task->initrd->local : NULL;
374 task->local_dtb = task->dtb ? task->dtb->local : NULL;
376 run_boot_hooks(task);
378 update_status(task->status_fn, task->status_arg, BOOT_STATUS_INFO,
379 "performing kexec_load");
381 rc = kexec_load(task);
383 update_status(task->status_fn, task->status_arg,
384 BOOT_STATUS_ERROR, "kexec load failed");
388 cleanup_load(task->image);
389 cleanup_load(task->initrd);
390 cleanup_load(task->dtb);
393 update_status(task->status_fn, task->status_arg,
395 "performing kexec reboot");
397 rc = kexec_reboot(task);
399 update_status(task->status_fn, task->status_arg,
401 "kexec reboot failed");
406 static int start_url_load(struct boot_task *task, const char *name,
407 struct pb_url *url, struct load_url_result **result)
412 *result = load_url_async(task, url, boot_process, task);
414 update_status(task->status_fn, task->status_arg,
416 "Error loading %s", name);
422 struct boot_task *boot(void *ctx, struct discover_boot_option *opt,
423 struct boot_command *cmd, int dry_run,
424 boot_status_fn status_fn, void *status_arg)
426 struct pb_url *image = NULL, *initrd = NULL, *dtb = NULL;
427 struct boot_task *boot_task;
428 const char *boot_desc;
431 if (opt && opt->option->name)
432 boot_desc = opt->option->name;
433 else if (cmd && cmd->boot_image_file)
434 boot_desc = cmd->boot_image_file;
436 boot_desc = "(unknown)";
438 update_status(status_fn, status_arg, BOOT_STATUS_INFO,
439 "Booting %s.", boot_desc);
441 if (cmd && cmd->boot_image_file) {
442 image = pb_url_parse(opt, cmd->boot_image_file);
443 } else if (opt && opt->boot_image) {
444 image = opt->boot_image->url;
446 pb_log("%s: no image specified\n", __func__);
447 update_status(status_fn, status_arg, BOOT_STATUS_INFO,
448 "Boot failed: no image specified");
452 if (cmd && cmd->initrd_file) {
453 initrd = pb_url_parse(opt, cmd->initrd_file);
454 } else if (opt && opt->initrd) {
455 initrd = opt->initrd->url;
458 if (cmd && cmd->dtb_file) {
459 dtb = pb_url_parse(opt, cmd->dtb_file);
460 } else if (opt && opt->dtb) {
464 boot_task = talloc_zero(ctx, struct boot_task);
465 boot_task->dry_run = dry_run;
466 boot_task->status_fn = status_fn;
467 boot_task->status_arg = status_arg;
469 if (cmd && cmd->boot_args) {
470 boot_task->args = talloc_strdup(boot_task, cmd->boot_args);
471 } else if (opt && opt->option->boot_args) {
472 boot_task->args = talloc_strdup(boot_task,
473 opt->option->boot_args);
475 boot_task->args = NULL;
478 /* start async loads for boot resources */
479 rc = start_url_load(boot_task, "kernel image", image, &boot_task->image)
480 || start_url_load(boot_task, "initrd", initrd, &boot_task->initrd)
481 || start_url_load(boot_task, "dtb", dtb, &boot_task->dtb);
483 /* If all URLs are local, we may be done. */
485 talloc_free(boot_task);
489 boot_process(NULL, boot_task);
494 void boot_cancel(struct boot_task *task)
496 task->cancelled = true;
498 update_status(task->status_fn, task->status_arg, BOOT_STATUS_INFO,
501 cleanup_cancellations(task, NULL);