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 unsigned int clean_image;
46 unsigned int clean_initrd;
47 unsigned int clean_dtb;
48 struct pb_url *image, *initrd, *dtb;
49 boot_status_fn status_fn;
51 enum boot_process_state state;
56 * kexec_load - kexec load helper.
58 static int kexec_load(struct boot_task *boot_task)
63 char *s_initrd = NULL;
68 *p++ = pb_system_apps.kexec; /* 1 */
71 if (boot_task->local_initrd) {
72 s_initrd = talloc_asprintf(boot_task, "--initrd=%s",
73 boot_task->local_initrd);
75 *p++ = s_initrd; /* 3 */
78 if (boot_task->local_dtb) {
79 s_dtb = talloc_asprintf(boot_task, "--dtb=%s",
80 boot_task->local_dtb);
85 if (boot_task->args) {
86 s_args = talloc_asprintf(boot_task, "--append=%s",
89 *p++ = s_args; /* 5 */
92 *p++ = boot_task->local_image; /* 6 */
95 result = process_run_simple_argv(boot_task, argv);
98 pb_log("%s: failed: (%d)\n", __func__, result);
104 * kexec_reboot - Helper to boot the new kernel.
106 * Must only be called after a successful call to kexec_load().
109 static int kexec_reboot(struct boot_task *task)
113 /* First try running shutdown. Init scripts should run 'exec -e' */
114 result = process_run_simple(task, pb_system_apps.shutdown, "-r",
117 /* On error, force a kexec with the -e option */
119 result = process_run_simple(task, pb_system_apps.kexec,
124 pb_log("%s: failed: (%d)\n", __func__, result);
126 /* okay, kexec -e -f */
128 result = process_run_simple(task, pb_system_apps.kexec,
133 pb_log("%s: failed: (%d)\n", __func__, result);
139 static void __attribute__((format(__printf__, 4, 5))) update_status(
140 boot_status_fn fn, void *arg, int type, char *fmt, ...)
142 struct boot_status status;
146 status.message = talloc_vasprintf(NULL, fmt, ap);
150 status.progress = -1;
151 status.detail = NULL;
155 talloc_free(status.message);
158 static void boot_hook_update_param(void *ctx, struct boot_task *task,
159 const char *name, const char *value)
164 } *param, params[] = {
165 { "boot_image", &task->local_image },
166 { "boot_initrd", &task->local_initrd },
167 { "boot_dtb", &task->local_dtb },
168 { "boot_args", &task->args },
172 for (param = params; param->name; param++) {
173 if (strcmp(param->name, name))
176 *param->p = talloc_strdup(ctx, value);
181 static void boot_hook_update(struct boot_task *task, const char *hookname,
184 char *line, *name, *val, *sep;
187 for (;; buf = NULL) {
189 line = strtok_r(buf, "\n", &saveptr);
193 sep = strchr(line, '=');
201 boot_hook_update_param(task, task, name, val);
203 pb_log("boot hook %s specified %s=%s\n",
204 hookname, name, val);
208 static void boot_hook_setenv(struct boot_task *task)
210 unsetenv("boot_image");
211 unsetenv("boot_initrd");
212 unsetenv("boot_dtb");
213 unsetenv("boot_args");
215 setenv("boot_image", task->local_image, 1);
216 if (task->local_initrd)
217 setenv("boot_initrd", task->local_initrd, 1);
219 setenv("boot_dtb", task->local_dtb, 1);
221 setenv("boot_args", task->args, 1);
224 static int hook_filter(const struct dirent *dirent)
226 return dirent->d_type == DT_REG || dirent->d_type == DT_LNK;
229 static int hook_cmp(const struct dirent **a, const struct dirent **b)
231 return strcmp((*a)->d_name, (*b)->d_name);
234 static void run_boot_hooks(struct boot_task *task)
236 struct dirent **hooks;
239 n = scandir(boot_hook_dir, &hooks, hook_filter, hook_cmp);
243 update_status(task->status_fn, task->status_arg, BOOT_STATUS_INFO,
244 "running boot hooks");
246 boot_hook_setenv(task);
248 for (i = 0; i < n; i++) {
249 const char *argv[2] = { NULL, NULL };
250 struct process *process;
254 path = join_paths(task, boot_hook_dir, hooks[i]->d_name);
256 if (access(path, X_OK)) {
261 process = process_create(task);
264 process->path = path;
265 process->argv = argv;
266 process->keep_stdout = true;
268 pb_log("running boot hook %s\n", hooks[i]->d_name);
270 rc = process_run_sync(process);
272 pb_log("boot hook exec failed!\n");
274 } else if (WIFEXITED(process->exit_status) &&
275 WEXITSTATUS(process->exit_status)
276 == BOOT_HOOK_EXIT_UPDATE) {
277 /* if the hook returned with BOOT_HOOK_EXIT_UPDATE,
278 * then we process stdout to look for updated params
280 if (rc == BOOT_HOOK_EXIT_UPDATE) {
281 boot_hook_update(task, hooks[i]->d_name,
282 process->stdout_buf);
283 boot_hook_setenv(task);
287 process_release(process);
294 static void boot_process(void *ctx, int status)
296 struct boot_task *task = ctx;
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 &task->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 &task->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 &task->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");
392 if (task->clean_image)
393 unlink(task->local_image);
394 if (task->clean_initrd)
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);