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,
41 struct load_url_result *image;
42 struct load_url_result *initrd;
43 struct load_url_result *dtb;
45 struct pb_url *image_url, *initrd_url, *dtb_url;
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->initrd) {
69 s_initrd = talloc_asprintf(boot_task, "--initrd=%s",
70 boot_task->initrd->local);
72 *p++ = s_initrd; /* 3 */
76 s_dtb = talloc_asprintf(boot_task, "--dtb=%s",
77 boot_task->dtb->local);
82 if (boot_task->args) {
83 s_args = talloc_asprintf(boot_task, "--append=%s",
86 *p++ = s_args; /* 5 */
89 *p++ = boot_task->image->local; /* 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->image->local },
163 { "boot_initrd", &task->initrd->local },
164 { "boot_dtb", &task->dtb->local },
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->image->local, 1);
214 setenv("boot_initrd", task->initrd->local, 1);
216 setenv("boot_dtb", task->dtb->local, 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 cleanup_load(struct load_url_result *result)
295 if (result->status != LOAD_OK)
297 if (!result->cleanup_local)
299 unlink(result->local);
302 static void boot_process(struct load_url_result *result, void *data)
304 struct boot_task *task = data;
307 if (task->state == BOOT_STATE_INITIAL) {
308 update_status(task->status_fn, task->status_arg,
309 BOOT_STATUS_INFO, "loading kernel");
310 task->image = load_url_async(task, task->image_url,
313 update_status(task->status_fn, task->status_arg,
315 "Couldn't load kernel image");
318 task->state = BOOT_STATE_IMAGE_LOADING;
322 if (task->state == BOOT_STATE_IMAGE_LOADING) {
323 if (result->status == LOAD_ERROR) {
324 update_status(task->status_fn, task->status_arg,
326 "Error loading kernel image");
329 task->state = BOOT_STATE_INITRD_LOADING;
331 if (task->initrd_url) {
332 update_status(task->status_fn, task->status_arg,
333 BOOT_STATUS_INFO, "loading initrd");
334 task->initrd = load_url_async(task, task->initrd_url,
337 update_status(task->status_fn, task->status_arg,
339 "Couldn't load initrd image");
346 if (task->state == BOOT_STATE_INITRD_LOADING) {
347 if (result->status) {
348 update_status(task->status_fn, task->status_arg,
350 "Error loading initrd");
353 task->state = BOOT_STATE_DTB_LOADING;
356 update_status(task->status_fn, task->status_arg,
358 "loading device tree");
359 task->dtb = load_url_async(task, task->dtb_url,
362 update_status(task->status_fn, task->status_arg,
364 "Couldn't load device tree");
371 if (task->state == BOOT_STATE_DTB_LOADING) {
372 if (result->status) {
373 update_status(task->status_fn, task->status_arg,
375 "Error loading dtb");
378 task->state = BOOT_STATE_FINISH;
382 if (task->state != BOOT_STATE_FINISH) {
383 task->state = BOOT_STATE_UNKNOWN;
387 run_boot_hooks(task);
389 update_status(task->status_fn, task->status_arg, BOOT_STATUS_INFO,
390 "performing kexec_load");
392 rc = kexec_load(task);
394 update_status(task->status_fn, task->status_arg,
395 BOOT_STATUS_ERROR, "kexec load failed");
399 cleanup_load(task->image);
400 cleanup_load(task->initrd);
401 cleanup_load(task->dtb);
404 update_status(task->status_fn, task->status_arg,
406 "performing kexec reboot");
408 rc = kexec_reboot(task);
410 update_status(task->status_fn, task->status_arg,
412 "kexec reboot failed");
419 int boot(void *ctx, struct discover_boot_option *opt, struct boot_command *cmd,
420 int dry_run, boot_status_fn status_fn, void *status_arg)
422 struct boot_task *boot_task;
423 struct pb_url *image = NULL;
424 const char *boot_desc;
426 if (opt && opt->option->name)
427 boot_desc = opt->option->name;
428 else if (cmd && cmd->boot_image_file)
429 boot_desc = cmd->boot_image_file;
431 boot_desc = "(unknown)";
433 update_status(status_fn, status_arg, BOOT_STATUS_INFO,
434 "Booting %s.", boot_desc);
436 if (cmd && cmd->boot_image_file) {
437 image = pb_url_parse(opt, cmd->boot_image_file);
438 } else if (opt && opt->boot_image) {
439 image = opt->boot_image->url;
441 pb_log("%s: no image specified\n", __func__);
442 update_status(status_fn, status_arg, BOOT_STATUS_INFO,
443 "Boot failed: no image specified");
447 boot_task = talloc_zero(ctx, struct boot_task);
448 boot_task->image_url = image;
449 boot_task->dry_run = dry_run;
450 boot_task->status_fn = status_fn;
451 boot_task->status_arg = status_arg;
452 boot_task->state = BOOT_STATE_INITIAL;
454 if (cmd && cmd->initrd_file) {
455 boot_task->initrd_url = pb_url_parse(opt, cmd->initrd_file);
456 } else if (opt && opt->initrd) {
457 boot_task->initrd_url = opt->initrd->url;
460 if (cmd && cmd->dtb_file) {
461 boot_task->dtb_url = pb_url_parse(opt, cmd->dtb_file);
462 } else if (opt && opt->dtb) {
463 boot_task->dtb_url = opt->dtb->url;
466 if (cmd && cmd->boot_args) {
467 boot_task->args = talloc_strdup(boot_task, cmd->boot_args);
468 } else if (opt && opt->option->boot_args) {
469 boot_task->args = talloc_strdup(boot_task,
470 opt->option->boot_args);
472 boot_task->args = NULL;
475 boot_process(NULL, boot_task);