10 #include <sys/types.h>
13 #include <pb-protocol/pb-protocol.h>
14 #include <process/process.h>
15 #include <system/system.h>
16 #include <talloc/talloc.h>
18 #include <util/util.h>
20 #include "device-handler.h"
25 static const char *boot_hook_dir = PKG_SYSCONF_DIR "/boot.d";
27 BOOT_HOOK_EXIT_OK = 0,
28 BOOT_HOOK_EXIT_UPDATE = 2,
32 struct load_url_result *image;
33 struct load_url_result *initrd;
34 struct load_url_result *dtb;
35 const char *local_image;
36 const char *local_initrd;
37 const char *local_dtb;
39 boot_status_fn status_fn;
46 * kexec_load - kexec load helper.
48 static int kexec_load(struct boot_task *boot_task)
53 char *s_initrd = NULL;
58 *p++ = pb_system_apps.kexec; /* 1 */
61 if (boot_task->local_initrd) {
62 s_initrd = talloc_asprintf(boot_task, "--initrd=%s",
63 boot_task->local_initrd);
65 *p++ = s_initrd; /* 3 */
68 if (boot_task->local_dtb) {
69 s_dtb = talloc_asprintf(boot_task, "--dtb=%s",
70 boot_task->local_dtb);
75 if (boot_task->args) {
76 s_args = talloc_asprintf(boot_task, "--append=%s",
79 *p++ = s_args; /* 5 */
82 *p++ = boot_task->local_image; /* 6 */
85 result = process_run_simple_argv(boot_task, argv);
88 pb_log("%s: failed: (%d)\n", __func__, result);
94 * kexec_reboot - Helper to boot the new kernel.
96 * Must only be called after a successful call to kexec_load().
99 static int kexec_reboot(struct boot_task *task)
103 /* First try running shutdown. Init scripts should run 'exec -e' */
104 result = process_run_simple(task, pb_system_apps.shutdown, "-r",
107 /* On error, force a kexec with the -e option */
109 result = process_run_simple(task, pb_system_apps.kexec,
114 pb_log("%s: failed: (%d)\n", __func__, result);
116 /* okay, kexec -e -f */
118 result = process_run_simple(task, pb_system_apps.kexec,
123 pb_log("%s: failed: (%d)\n", __func__, result);
129 static void __attribute__((format(__printf__, 4, 5))) update_status(
130 boot_status_fn fn, void *arg, int type, char *fmt, ...)
132 struct boot_status status;
136 status.message = talloc_vasprintf(NULL, fmt, ap);
140 status.progress = -1;
141 status.detail = NULL;
143 pb_debug("boot status: [%d] %s\n", type, status.message);
147 talloc_free(status.message);
150 static void boot_hook_update_param(void *ctx, struct boot_task *task,
151 const char *name, const char *value)
156 } *param, params[] = {
157 { "boot_image", &task->local_image },
158 { "boot_initrd", &task->local_initrd },
159 { "boot_dtb", &task->local_dtb },
160 { "boot_args", &task->args },
164 for (param = params; param->name; param++) {
165 if (strcmp(param->name, name))
168 *param->p = talloc_strdup(ctx, value);
173 static void boot_hook_update(struct boot_task *task, const char *hookname,
176 char *line, *name, *val, *sep;
179 for (;; buf = NULL) {
181 line = strtok_r(buf, "\n", &saveptr);
185 sep = strchr(line, '=');
193 boot_hook_update_param(task, task, name, val);
195 pb_log("boot hook %s specified %s=%s\n",
196 hookname, name, val);
200 static void boot_hook_setenv(struct boot_task *task)
202 unsetenv("boot_image");
203 unsetenv("boot_initrd");
204 unsetenv("boot_dtb");
205 unsetenv("boot_args");
207 setenv("boot_image", task->local_image, 1);
208 if (task->local_initrd)
209 setenv("boot_initrd", task->local_initrd, 1);
211 setenv("boot_dtb", task->local_dtb, 1);
213 setenv("boot_args", task->args, 1);
216 static int hook_filter(const struct dirent *dirent)
218 return dirent->d_type == DT_REG || dirent->d_type == DT_LNK;
221 static int hook_cmp(const struct dirent **a, const struct dirent **b)
223 return strcmp((*a)->d_name, (*b)->d_name);
226 static void run_boot_hooks(struct boot_task *task)
228 struct dirent **hooks;
231 n = scandir(boot_hook_dir, &hooks, hook_filter, hook_cmp);
235 update_status(task->status_fn, task->status_arg, BOOT_STATUS_INFO,
236 "running boot hooks");
238 boot_hook_setenv(task);
240 for (i = 0; i < n; i++) {
241 const char *argv[2] = { NULL, NULL };
242 struct process *process;
246 path = join_paths(task, boot_hook_dir, hooks[i]->d_name);
248 if (access(path, X_OK)) {
253 process = process_create(task);
256 process->path = path;
257 process->argv = argv;
258 process->keep_stdout = true;
260 pb_log("running boot hook %s\n", hooks[i]->d_name);
262 rc = process_run_sync(process);
264 pb_log("boot hook exec failed!\n");
266 } else if (WIFEXITED(process->exit_status) &&
267 WEXITSTATUS(process->exit_status)
268 == BOOT_HOOK_EXIT_UPDATE) {
269 /* if the hook returned with BOOT_HOOK_EXIT_UPDATE,
270 * then we process stdout to look for updated params
272 boot_hook_update(task, hooks[i]->d_name,
273 process->stdout_buf);
274 boot_hook_setenv(task);
277 process_release(process);
284 static bool load_pending(struct load_url_result *result)
286 return result && result->status == LOAD_ASYNC;
289 static int check_load(struct boot_task *task, const char *name,
290 struct load_url_result *result)
294 if (result->status != LOAD_ERROR)
297 update_status(task->status_fn, task->status_arg,
299 "Couldn't load %s", name);
303 static void cleanup_load(struct load_url_result *result)
307 if (result->status != LOAD_OK)
309 if (!result->cleanup_local)
311 unlink(result->local);
314 static void cleanup_cancellations(struct boot_task *task,
315 struct load_url_result *cur_result)
317 struct load_url_result *result, **results[] = {
318 &task->image, &task->initrd, &task->dtb,
320 bool pending = false;
323 for (i = 0; i < ARRAY_SIZE(results); i++) {
324 result = *results[i];
329 /* We need to cleanup and free any completed loads */
330 if (result == cur_result || result->status == LOAD_OK
331 || result->status == LOAD_ERROR) {
332 cleanup_load(result);
336 /* ... and cancel any pending loads, which we'll free in
337 * the completion callback */
338 } else if (result->status == LOAD_ASYNC) {
339 load_url_async_cancel(result);
348 static void boot_process(struct load_url_result *result, void *data)
350 struct boot_task *task = data;
353 if (task->cancelled) {
354 cleanup_cancellations(task, result);
358 if (load_pending(task->image) ||
359 load_pending(task->initrd) ||
360 load_pending(task->dtb))
363 if (check_load(task, "kernel image", task->image) ||
364 check_load(task, "initrd", task->initrd) ||
365 check_load(task, "dtb", task->dtb))
368 /* we make a copy of the local paths, as the boot hooks might update
369 * and/or create these */
370 task->local_image = task->image ? task->image->local : NULL;
371 task->local_initrd = task->initrd ? task->initrd->local : NULL;
372 task->local_dtb = task->dtb ? task->dtb->local : NULL;
374 run_boot_hooks(task);
376 update_status(task->status_fn, task->status_arg, BOOT_STATUS_INFO,
377 "performing kexec_load");
379 rc = kexec_load(task);
381 update_status(task->status_fn, task->status_arg,
382 BOOT_STATUS_ERROR, "kexec load failed");
386 cleanup_load(task->image);
387 cleanup_load(task->initrd);
388 cleanup_load(task->dtb);
391 update_status(task->status_fn, task->status_arg,
393 "performing kexec reboot");
395 rc = kexec_reboot(task);
397 update_status(task->status_fn, task->status_arg,
399 "kexec reboot failed");
404 static int start_url_load(struct boot_task *task, const char *name,
405 struct pb_url *url, struct load_url_result **result)
410 *result = load_url_async(task, url, boot_process, task);
412 update_status(task->status_fn, task->status_arg,
414 "Error loading %s", name);
420 struct boot_task *boot(void *ctx, struct discover_boot_option *opt,
421 struct boot_command *cmd, int dry_run,
422 boot_status_fn status_fn, void *status_arg)
424 struct pb_url *image = NULL, *initrd = NULL, *dtb = NULL;
425 struct boot_task *boot_task;
426 const char *boot_desc;
429 if (opt && opt->option->name)
430 boot_desc = opt->option->name;
431 else if (cmd && cmd->boot_image_file)
432 boot_desc = cmd->boot_image_file;
434 boot_desc = "(unknown)";
436 update_status(status_fn, status_arg, BOOT_STATUS_INFO,
437 "Booting %s.", boot_desc);
439 if (cmd && cmd->boot_image_file) {
440 image = pb_url_parse(opt, cmd->boot_image_file);
441 } else if (opt && opt->boot_image) {
442 image = opt->boot_image->url;
444 pb_log("%s: no image specified\n", __func__);
445 update_status(status_fn, status_arg, BOOT_STATUS_INFO,
446 "Boot failed: no image specified");
450 if (cmd && cmd->initrd_file) {
451 initrd = pb_url_parse(opt, cmd->initrd_file);
452 } else if (opt && opt->initrd) {
453 initrd = opt->initrd->url;
456 if (cmd && cmd->dtb_file) {
457 dtb = pb_url_parse(opt, cmd->dtb_file);
458 } else if (opt && opt->dtb) {
462 boot_task = talloc_zero(ctx, struct boot_task);
463 boot_task->dry_run = dry_run;
464 boot_task->status_fn = status_fn;
465 boot_task->status_arg = status_arg;
467 if (cmd && cmd->boot_args) {
468 boot_task->args = talloc_strdup(boot_task, cmd->boot_args);
469 } else if (opt && opt->option->boot_args) {
470 boot_task->args = talloc_strdup(boot_task,
471 opt->option->boot_args);
473 boot_task->args = NULL;
476 /* start async loads for boot resources */
477 rc = start_url_load(boot_task, "kernel image", image, &boot_task->image)
478 || start_url_load(boot_task, "initrd", initrd, &boot_task->initrd)
479 || start_url_load(boot_task, "dtb", dtb, &boot_task->dtb);
481 /* If all URLs are local, we may be done. */
483 talloc_free(boot_task);
487 boot_process(NULL, boot_task);
492 void boot_cancel(struct boot_task *task)
494 task->cancelled = true;
496 update_status(task->status_fn, task->status_arg, BOOT_STATUS_INFO,
499 cleanup_cancellations(task, NULL);