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;
36 boot_status_fn status_fn;
43 * kexec_load - kexec load helper.
45 static int kexec_load(struct boot_task *boot_task)
50 char *s_initrd = NULL;
55 *p++ = pb_system_apps.kexec; /* 1 */
58 if (boot_task->initrd) {
59 s_initrd = talloc_asprintf(boot_task, "--initrd=%s",
60 boot_task->initrd->local);
62 *p++ = s_initrd; /* 3 */
66 s_dtb = talloc_asprintf(boot_task, "--dtb=%s",
67 boot_task->dtb->local);
72 if (boot_task->args) {
73 s_args = talloc_asprintf(boot_task, "--append=%s",
76 *p++ = s_args; /* 5 */
79 *p++ = boot_task->image->local; /* 6 */
82 result = process_run_simple_argv(boot_task, argv);
85 pb_log("%s: failed: (%d)\n", __func__, result);
91 * kexec_reboot - Helper to boot the new kernel.
93 * Must only be called after a successful call to kexec_load().
96 static int kexec_reboot(struct boot_task *task)
100 /* First try running shutdown. Init scripts should run 'exec -e' */
101 result = process_run_simple(task, pb_system_apps.shutdown, "-r",
104 /* On error, force a kexec with the -e option */
106 result = process_run_simple(task, pb_system_apps.kexec,
111 pb_log("%s: failed: (%d)\n", __func__, result);
113 /* okay, kexec -e -f */
115 result = process_run_simple(task, pb_system_apps.kexec,
120 pb_log("%s: failed: (%d)\n", __func__, result);
126 static void __attribute__((format(__printf__, 4, 5))) update_status(
127 boot_status_fn fn, void *arg, int type, char *fmt, ...)
129 struct boot_status status;
133 status.message = talloc_vasprintf(NULL, fmt, ap);
137 status.progress = -1;
138 status.detail = NULL;
140 pb_debug("boot status: [%d] %s\n", type, status.message);
144 talloc_free(status.message);
147 static void boot_hook_update_param(void *ctx, struct boot_task *task,
148 const char *name, const char *value)
153 } *param, params[] = {
154 { "boot_image", &task->image->local },
155 { "boot_initrd", &task->initrd->local },
156 { "boot_dtb", &task->dtb->local },
157 { "boot_args", &task->args },
161 for (param = params; param->name; param++) {
162 if (strcmp(param->name, name))
165 *param->p = talloc_strdup(ctx, value);
170 static void boot_hook_update(struct boot_task *task, const char *hookname,
173 char *line, *name, *val, *sep;
176 for (;; buf = NULL) {
178 line = strtok_r(buf, "\n", &saveptr);
182 sep = strchr(line, '=');
190 boot_hook_update_param(task, task, name, val);
192 pb_log("boot hook %s specified %s=%s\n",
193 hookname, name, val);
197 static void boot_hook_setenv(struct boot_task *task)
199 unsetenv("boot_image");
200 unsetenv("boot_initrd");
201 unsetenv("boot_dtb");
202 unsetenv("boot_args");
204 setenv("boot_image", task->image->local, 1);
206 setenv("boot_initrd", task->initrd->local, 1);
208 setenv("boot_dtb", task->dtb->local, 1);
210 setenv("boot_args", task->args, 1);
213 static int hook_filter(const struct dirent *dirent)
215 return dirent->d_type == DT_REG || dirent->d_type == DT_LNK;
218 static int hook_cmp(const struct dirent **a, const struct dirent **b)
220 return strcmp((*a)->d_name, (*b)->d_name);
223 static void run_boot_hooks(struct boot_task *task)
225 struct dirent **hooks;
228 n = scandir(boot_hook_dir, &hooks, hook_filter, hook_cmp);
232 update_status(task->status_fn, task->status_arg, BOOT_STATUS_INFO,
233 "running boot hooks");
235 boot_hook_setenv(task);
237 for (i = 0; i < n; i++) {
238 const char *argv[2] = { NULL, NULL };
239 struct process *process;
243 path = join_paths(task, boot_hook_dir, hooks[i]->d_name);
245 if (access(path, X_OK)) {
250 process = process_create(task);
253 process->path = path;
254 process->argv = argv;
255 process->keep_stdout = true;
257 pb_log("running boot hook %s\n", hooks[i]->d_name);
259 rc = process_run_sync(process);
261 pb_log("boot hook exec failed!\n");
263 } else if (WIFEXITED(process->exit_status) &&
264 WEXITSTATUS(process->exit_status)
265 == BOOT_HOOK_EXIT_UPDATE) {
266 /* if the hook returned with BOOT_HOOK_EXIT_UPDATE,
267 * then we process stdout to look for updated params
269 if (rc == BOOT_HOOK_EXIT_UPDATE) {
270 boot_hook_update(task, hooks[i]->d_name,
271 process->stdout_buf);
272 boot_hook_setenv(task);
276 process_release(process);
283 static bool load_pending(struct load_url_result *result)
285 return result && result->status == LOAD_ASYNC;
288 static int check_load(struct boot_task *task, const char *name,
289 struct load_url_result *result)
293 if (result->status != LOAD_ERROR)
296 update_status(task->status_fn, task->status_arg,
298 "Couldn't load %s", name);
302 static void cleanup_load(struct load_url_result *result)
306 if (result->status != LOAD_OK)
308 if (!result->cleanup_local)
310 unlink(result->local);
313 static void cleanup_cancellations(struct boot_task *task,
314 struct load_url_result *cur_result)
316 struct load_url_result *result, **results[] = {
317 &task->image, &task->initrd, &task->dtb,
319 bool pending = false;
322 for (i = 0; i < ARRAY_SIZE(results); i++) {
323 result = *results[i];
328 /* We need to cleanup and free any completed loads */
329 if (result == cur_result || result->status == LOAD_OK
330 || result->status == LOAD_ERROR) {
331 cleanup_load(result);
335 /* ... and cancel any pending loads, which we'll free in
336 * the completion callback */
337 } else if (result->status == LOAD_ASYNC) {
338 load_url_async_cancel(result);
347 static void boot_process(struct load_url_result *result, void *data)
349 struct boot_task *task = data;
352 if (task->cancelled) {
353 cleanup_cancellations(task, result);
357 if (load_pending(task->image) ||
358 load_pending(task->initrd) ||
359 load_pending(task->dtb))
362 if (check_load(task, "kernel image", task->image) ||
363 check_load(task, "initrd", task->initrd) ||
364 check_load(task, "dtb", task->dtb))
367 run_boot_hooks(task);
369 update_status(task->status_fn, task->status_arg, BOOT_STATUS_INFO,
370 "performing kexec_load");
372 rc = kexec_load(task);
374 update_status(task->status_fn, task->status_arg,
375 BOOT_STATUS_ERROR, "kexec load failed");
379 cleanup_load(task->image);
380 cleanup_load(task->initrd);
381 cleanup_load(task->dtb);
384 update_status(task->status_fn, task->status_arg,
386 "performing kexec reboot");
388 rc = kexec_reboot(task);
390 update_status(task->status_fn, task->status_arg,
392 "kexec reboot failed");
397 static int start_url_load(struct boot_task *task, const char *name,
398 struct pb_url *url, struct load_url_result **result)
403 *result = load_url_async(task, url, boot_process, task);
405 update_status(task->status_fn, task->status_arg,
407 "Error loading %s", name);
413 struct boot_task *boot(void *ctx, struct discover_boot_option *opt,
414 struct boot_command *cmd, int dry_run,
415 boot_status_fn status_fn, void *status_arg)
417 struct pb_url *image = NULL, *initrd = NULL, *dtb = NULL;
418 struct boot_task *boot_task;
419 const char *boot_desc;
422 if (opt && opt->option->name)
423 boot_desc = opt->option->name;
424 else if (cmd && cmd->boot_image_file)
425 boot_desc = cmd->boot_image_file;
427 boot_desc = "(unknown)";
429 update_status(status_fn, status_arg, BOOT_STATUS_INFO,
430 "Booting %s.", boot_desc);
432 if (cmd && cmd->boot_image_file) {
433 image = pb_url_parse(opt, cmd->boot_image_file);
434 } else if (opt && opt->boot_image) {
435 image = opt->boot_image->url;
437 pb_log("%s: no image specified\n", __func__);
438 update_status(status_fn, status_arg, BOOT_STATUS_INFO,
439 "Boot failed: no image specified");
443 if (cmd && cmd->initrd_file) {
444 initrd = pb_url_parse(opt, cmd->initrd_file);
445 } else if (opt && opt->initrd) {
446 initrd = opt->initrd->url;
449 if (cmd && cmd->dtb_file) {
450 dtb = pb_url_parse(opt, cmd->dtb_file);
451 } else if (opt && opt->dtb) {
455 boot_task = talloc_zero(ctx, struct boot_task);
456 boot_task->dry_run = dry_run;
457 boot_task->status_fn = status_fn;
458 boot_task->status_arg = status_arg;
460 if (cmd && cmd->boot_args) {
461 boot_task->args = talloc_strdup(boot_task, cmd->boot_args);
462 } else if (opt && opt->option->boot_args) {
463 boot_task->args = talloc_strdup(boot_task,
464 opt->option->boot_args);
466 boot_task->args = NULL;
469 /* start async loads for boot resources */
470 rc = start_url_load(boot_task, "kernel image", image, &boot_task->image)
471 || start_url_load(boot_task, "initrd", initrd, &boot_task->initrd)
472 || start_url_load(boot_task, "dtb", dtb, &boot_task->dtb);
474 /* If all URLs are local, we may be done. */
476 talloc_free(boot_task);
480 boot_process(NULL, boot_task);
485 void boot_cancel(struct boot_task *task)
487 task->cancelled = true;
489 update_status(task->status_fn, task->status_arg, BOOT_STATUS_INFO,
492 cleanup_cancellations(task, NULL);