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>
19 #include "device-handler.h"
24 static const char *boot_hook_dir = PKG_SYSCONF_DIR "/boot.d";
26 BOOT_HOOK_EXIT_OK = 0,
27 BOOT_HOOK_EXIT_UPDATE = 2,
30 enum boot_process_state {
32 BOOT_STATE_IMAGE_LOADING,
33 BOOT_STATE_INITRD_LOADING,
34 BOOT_STATE_DTB_LOADING,
45 struct pb_url *image, *initrd, *dtb;
46 boot_status_fn status_fn;
48 enum boot_process_state state;
53 * kexec_load - kexec load helper.
55 static int kexec_load(struct boot_task *boot_task)
60 char *s_initrd = NULL;
65 *p++ = pb_system_apps.kexec; /* 1 */
68 if (boot_task->local_initrd) {
69 s_initrd = talloc_asprintf(boot_task, "--initrd=%s",
70 boot_task->local_initrd);
72 *p++ = s_initrd; /* 3 */
75 if (boot_task->local_dtb) {
76 s_dtb = talloc_asprintf(boot_task, "--dtb=%s",
77 boot_task->local_dtb);
82 if (boot_task->args) {
83 s_args = talloc_asprintf(boot_task, "--append=%s",
86 *p++ = s_args; /* 5 */
89 *p++ = boot_task->local_image; /* 6 */
92 result = process_run_simple_argv(boot_task, argv);
95 pb_log("%s: failed: (%d)\n", __func__, result);
101 * kexec_reboot - Helper to boot the new kernel.
103 * Must only be called after a successful call to kexec_load().
106 static int kexec_reboot(struct boot_task *task)
110 /* First try running shutdown. Init scripts should run 'exec -e' */
111 result = process_run_simple(task, pb_system_apps.shutdown, "-r",
114 /* On error, force a kexec with the -e option */
116 result = process_run_simple(task, pb_system_apps.kexec,
121 pb_log("%s: failed: (%d)\n", __func__, result);
123 /* okay, kexec -e -f */
125 result = process_run_simple(task, pb_system_apps.kexec,
130 pb_log("%s: failed: (%d)\n", __func__, result);
136 static void __attribute__((format(__printf__, 4, 5))) update_status(
137 boot_status_fn fn, void *arg, int type, char *fmt, ...)
139 struct boot_status status;
143 status.message = talloc_vasprintf(NULL, fmt, ap);
147 status.progress = -1;
148 status.detail = NULL;
152 talloc_free(status.message);
155 static void boot_hook_update_param(void *ctx, struct boot_task *task,
156 const char *name, const char *value)
161 } *param, params[] = {
162 { "boot_image", &task->local_image },
163 { "boot_initrd", &task->local_initrd },
164 { "boot_dtb", &task->local_dtb },
165 { "boot_args", &task->args },
169 for (param = params; param->name; param++) {
170 if (strcmp(param->name, name))
173 *param->p = talloc_strdup(ctx, value);
178 static void boot_hook_update(struct boot_task *task, const char *hookname,
181 char *line, *name, *val, *sep;
184 for (;; buf = NULL) {
186 line = strtok_r(buf, "\n", &saveptr);
190 sep = strchr(line, '=');
198 boot_hook_update_param(task, task, name, val);
200 pb_log("boot hook %s specified %s=%s\n",
201 hookname, name, val);
205 static void boot_hook_setenv(struct boot_task *task)
207 unsetenv("boot_image");
208 unsetenv("boot_initrd");
209 unsetenv("boot_dtb");
210 unsetenv("boot_args");
212 setenv("boot_image", task->local_image, 1);
213 if (task->local_initrd)
214 setenv("boot_initrd", task->local_initrd, 1);
216 setenv("boot_dtb", task->local_dtb, 1);
218 setenv("boot_args", task->args, 1);
221 static int hook_filter(const struct dirent *dirent)
223 return dirent->d_type == DT_REG || dirent->d_type == DT_LNK;
226 static int hook_cmp(const struct dirent **a, const struct dirent **b)
228 return strcmp((*a)->d_name, (*b)->d_name);
231 static void run_boot_hooks(struct boot_task *task)
233 struct dirent **hooks;
236 n = scandir(boot_hook_dir, &hooks, hook_filter, hook_cmp);
240 update_status(task->status_fn, task->status_arg, BOOT_STATUS_INFO,
241 "running boot hooks");
243 boot_hook_setenv(task);
245 for (i = 0; i < n; i++) {
246 const char *argv[2] = { NULL, NULL };
247 struct process *process;
251 path = join_paths(task, boot_hook_dir, hooks[i]->d_name);
253 if (access(path, X_OK)) {
258 process = process_create(task);
261 process->path = path;
262 process->argv = argv;
263 process->keep_stdout = true;
265 pb_log("running boot hook %s\n", hooks[i]->d_name);
267 rc = process_run_sync(process);
269 pb_log("boot hook exec failed!\n");
271 } else if (WIFEXITED(process->exit_status) &&
272 WEXITSTATUS(process->exit_status)
273 == BOOT_HOOK_EXIT_UPDATE) {
274 /* if the hook returned with BOOT_HOOK_EXIT_UPDATE,
275 * then we process stdout to look for updated params
277 if (rc == BOOT_HOOK_EXIT_UPDATE) {
278 boot_hook_update(task, hooks[i]->d_name,
279 process->stdout_buf);
280 boot_hook_setenv(task);
284 process_release(process);
291 static void boot_process(void *ctx, int status)
293 struct boot_task *task = ctx;
294 unsigned int clean_image = 0;
295 unsigned int clean_initrd = 0;
296 unsigned int clean_dtb = 0;
299 if (task->state == BOOT_STATE_INITIAL) {
300 update_status(task->status_fn, task->status_arg,
301 BOOT_STATUS_INFO, "loading kernel");
302 task->local_image = load_url_async(task, task->image,
303 &clean_image, boot_process);
304 if (!task->local_image) {
305 update_status(task->status_fn, task->status_arg,
307 "Couldn't load kernel image");
310 task->state = BOOT_STATE_IMAGE_LOADING;
314 if (task->state == BOOT_STATE_IMAGE_LOADING) {
316 update_status(task->status_fn, task->status_arg,
318 "Error loading kernel image");
321 task->state = BOOT_STATE_INITRD_LOADING;
324 update_status(task->status_fn, task->status_arg,
325 BOOT_STATUS_INFO, "loading initrd");
326 task->local_initrd = load_url_async(task, task->initrd,
327 &clean_initrd, boot_process);
328 if (!task->local_initrd) {
329 update_status(task->status_fn, task->status_arg,
331 "Couldn't load initrd image");
338 if (task->state == BOOT_STATE_INITRD_LOADING) {
340 update_status(task->status_fn, task->status_arg,
342 "Error loading initrd");
345 task->state = BOOT_STATE_DTB_LOADING;
348 update_status(task->status_fn, task->status_arg,
350 "loading device tree");
351 task->local_dtb = load_url_async(task, task->dtb,
352 &clean_dtb, boot_process);
353 if (!task->local_dtb) {
354 update_status(task->status_fn, task->status_arg,
356 "Couldn't load device tree");
363 if (task->state == BOOT_STATE_DTB_LOADING) {
365 update_status(task->status_fn, task->status_arg,
367 "Error loading dtb");
370 task->state = BOOT_STATE_FINISH;
374 if (task->state != BOOT_STATE_FINISH) {
375 task->state = BOOT_STATE_UNKNOWN;
379 run_boot_hooks(task);
381 update_status(task->status_fn, task->status_arg, BOOT_STATUS_INFO,
382 "performing kexec_load");
384 result = kexec_load(task);
387 update_status(task->status_fn, task->status_arg,
388 BOOT_STATUS_ERROR, "kexec load failed");
393 unlink(task->local_image);
395 unlink(task->local_initrd);
397 unlink(task->local_dtb);
400 update_status(task->status_fn, task->status_arg,
402 "performing kexec reboot");
404 result = kexec_reboot(task);
407 update_status(task->status_fn, task->status_arg,
409 "kexec reboot failed");
416 int boot(void *ctx, struct discover_boot_option *opt, struct boot_command *cmd,
417 int dry_run, boot_status_fn status_fn, void *status_arg)
419 struct boot_task *boot_task;
420 struct pb_url *image = NULL;
421 const char *boot_desc;
423 if (opt && opt->option->name)
424 boot_desc = opt->option->name;
425 else if (cmd && cmd->boot_image_file)
426 boot_desc = cmd->boot_image_file;
428 boot_desc = "(unknown)";
430 update_status(status_fn, status_arg, BOOT_STATUS_INFO,
431 "Booting %s.", boot_desc);
433 if (cmd && cmd->boot_image_file) {
434 image = pb_url_parse(opt, cmd->boot_image_file);
435 } else if (opt && opt->boot_image) {
436 image = opt->boot_image->url;
438 pb_log("%s: no image specified\n", __func__);
439 update_status(status_fn, status_arg, BOOT_STATUS_INFO,
440 "Boot failed: no image specified");
444 boot_task = talloc_zero(ctx, struct boot_task);
445 boot_task->image = image;
446 boot_task->dry_run = dry_run;
447 boot_task->status_fn = status_fn;
448 boot_task->status_arg = status_arg;
449 boot_task->state = BOOT_STATE_INITIAL;
451 if (cmd && cmd->initrd_file) {
452 boot_task->initrd = pb_url_parse(opt, cmd->initrd_file);
453 } else if (opt && opt->initrd) {
454 boot_task->initrd = opt->initrd->url;
457 if (cmd && cmd->dtb_file) {
458 boot_task->dtb = pb_url_parse(opt, cmd->dtb_file);
459 } else if (opt && opt->dtb) {
460 boot_task->dtb = opt->dtb->url;
463 if (cmd && cmd->boot_args) {
464 boot_task->args = talloc_strdup(boot_task, cmd->boot_args);
465 } else if (opt && opt->option->boot_args) {
466 boot_task->args = talloc_strdup(boot_task,
467 opt->option->boot_args);
469 boot_task->args = NULL;
472 boot_process(boot_task, 0);