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,
44 struct pb_url *image, *initrd, *dtb;
45 boot_status_fn status_fn;
47 enum boot_process_state state;
52 * kexec_load - kexec load helper.
54 static int kexec_load(struct boot_task *boot_task)
59 char *s_initrd = NULL;
64 *p++ = pb_system_apps.kexec; /* 1 */
67 if (boot_task->local_initrd) {
68 s_initrd = talloc_asprintf(boot_task, "--initrd=%s",
69 boot_task->local_initrd);
71 *p++ = s_initrd; /* 3 */
74 if (boot_task->local_dtb) {
75 s_dtb = talloc_asprintf(boot_task, "--dtb=%s",
76 boot_task->local_dtb);
81 if (boot_task->args) {
82 s_args = talloc_asprintf(boot_task, "--append=%s",
85 *p++ = s_args; /* 5 */
88 *p++ = boot_task->local_image; /* 6 */
91 result = process_run_simple_argv(boot_task, argv);
94 pb_log("%s: failed: (%d)\n", __func__, result);
100 * kexec_reboot - Helper to boot the new kernel.
102 * Must only be called after a successful call to kexec_load().
105 static int kexec_reboot(struct boot_task *task)
109 /* First try running shutdown. Init scripts should run 'exec -e' */
110 result = process_run_simple(task, pb_system_apps.shutdown, "-r",
113 /* On error, force a kexec with the -e option */
115 result = process_run_simple(task, pb_system_apps.kexec,
120 pb_log("%s: failed: (%d)\n", __func__, result);
122 /* okay, kexec -e -f */
124 result = process_run_simple(task, pb_system_apps.kexec,
129 pb_log("%s: failed: (%d)\n", __func__, result);
135 static void __attribute__((format(__printf__, 4, 5))) update_status(
136 boot_status_fn fn, void *arg, int type, char *fmt, ...)
138 struct boot_status status;
142 status.message = talloc_vasprintf(NULL, fmt, ap);
146 status.progress = -1;
147 status.detail = NULL;
151 talloc_free(status.message);
154 static void boot_hook_update_param(void *ctx, struct boot_task *task,
155 const char *name, const char *value)
160 } *param, params[] = {
161 { "boot_image", &task->local_image },
162 { "boot_initrd", &task->local_initrd },
163 { "boot_dtb", &task->local_dtb },
164 { "boot_args", &task->args },
168 for (param = params; param->name; param++) {
169 if (strcmp(param->name, name))
172 *param->p = talloc_strdup(ctx, value);
177 static void boot_hook_update(struct boot_task *task, const char *hookname,
180 char *line, *name, *val, *sep;
183 for (;; buf = NULL) {
185 line = strtok_r(buf, "\n", &saveptr);
189 sep = strchr(line, '=');
197 boot_hook_update_param(task, task, name, val);
199 pb_log("boot hook %s specified %s=%s\n",
200 hookname, name, val);
204 static void boot_hook_setenv(struct boot_task *task)
206 unsetenv("boot_image");
207 unsetenv("boot_initrd");
208 unsetenv("boot_dtb");
209 unsetenv("boot_args");
211 setenv("boot_image", task->local_image, 1);
212 if (task->local_initrd)
213 setenv("boot_initrd", task->local_initrd, 1);
215 setenv("boot_dtb", task->local_dtb, 1);
217 setenv("boot_args", task->args, 1);
220 static int hook_filter(const struct dirent *dirent)
222 return dirent->d_type == DT_REG || dirent->d_type == DT_LNK;
225 static int hook_cmp(const struct dirent **a, const struct dirent **b)
227 return strcmp((*a)->d_name, (*b)->d_name);
230 static void run_boot_hooks(struct boot_task *task)
232 struct dirent **hooks;
235 n = scandir(boot_hook_dir, &hooks, hook_filter, hook_cmp);
239 update_status(task->status_fn, task->status_arg, BOOT_STATUS_INFO,
240 "running boot hooks");
242 boot_hook_setenv(task);
244 for (i = 0; i < n; i++) {
245 const char *argv[2] = { NULL, NULL };
246 struct process *process;
250 path = join_paths(task, boot_hook_dir, hooks[i]->d_name);
252 if (access(path, X_OK)) {
257 process = process_create(task);
260 process->path = path;
261 process->argv = argv;
262 process->keep_stdout = true;
264 pb_log("running boot hook %s\n", hooks[i]->d_name);
266 rc = process_run_sync(process);
268 pb_log("boot hook exec failed!\n");
270 } else if (WIFEXITED(process->exit_status) &&
271 WEXITSTATUS(process->exit_status)
272 == BOOT_HOOK_EXIT_UPDATE) {
273 /* if the hook returned with BOOT_HOOK_EXIT_UPDATE,
274 * then we process stdout to look for updated params
276 if (rc == BOOT_HOOK_EXIT_UPDATE) {
277 boot_hook_update(task, hooks[i]->d_name,
278 process->stdout_buf);
279 boot_hook_setenv(task);
283 process_release(process);
290 static void boot_process(void *ctx, int status __attribute__((unused)))
292 struct boot_task *task = ctx;
293 unsigned int clean_image = 0;
294 unsigned int clean_initrd = 0;
295 unsigned int clean_dtb = 0;
298 if (task->state == BOOT_STATE_INITIAL) {
299 update_status(task->status_fn, task->status_arg,
300 BOOT_STATUS_INFO, "loading kernel");
301 task->local_image = load_url_async(task, task->image,
302 &clean_image, boot_process);
303 if (!task->local_image) {
304 update_status(task->status_fn, task->status_arg,
306 "Couldn't load kernel image");
309 task->state = BOOT_STATE_IMAGE_LOADING;
314 if (task->state == BOOT_STATE_IMAGE_LOADING) {
316 update_status(task->status_fn, task->status_arg,
317 BOOT_STATUS_INFO, "loading initrd");
318 task->local_initrd = load_url_async(task, task->initrd,
319 &clean_initrd, boot_process);
320 if (!task->local_initrd) {
321 update_status(task->status_fn, task->status_arg,
323 "Couldn't load initrd image");
326 task->state = BOOT_STATE_INITRD_LOADING;
330 task->state = BOOT_STATE_INITRD_LOADING;
334 if (task->state == BOOT_STATE_INITRD_LOADING) {
336 update_status(task->status_fn, task->status_arg,
338 "loading device tree");
339 task->local_dtb = load_url_async(task, task->dtb,
340 &clean_dtb, boot_process);
341 if (!task->local_dtb) {
342 update_status(task->status_fn, task->status_arg,
344 "Couldn't load device tree");
347 task->state = BOOT_STATE_FINISH;
351 task->state = BOOT_STATE_FINISH;
355 if (task->state != BOOT_STATE_FINISH) {
356 task->state = BOOT_STATE_UNKNOWN;
360 run_boot_hooks(task);
362 update_status(task->status_fn, task->status_arg, BOOT_STATUS_INFO,
363 "performing kexec_load");
365 result = kexec_load(task);
368 update_status(task->status_fn, task->status_arg,
369 BOOT_STATUS_ERROR, "kexec load failed");
374 unlink(task->local_image);
376 unlink(task->local_initrd);
378 unlink(task->local_dtb);
381 update_status(task->status_fn, task->status_arg,
383 "performing kexec reboot");
385 result = kexec_reboot(task);
388 update_status(task->status_fn, task->status_arg,
390 "kexec reboot failed");
397 int boot(void *ctx, struct discover_boot_option *opt, struct boot_command *cmd,
398 int dry_run, boot_status_fn status_fn, void *status_arg)
400 struct boot_task *boot_task;
401 struct pb_url *image = NULL;
402 const char *boot_desc;
404 if (opt && opt->option->name)
405 boot_desc = opt->option->name;
406 else if (cmd && cmd->boot_image_file)
407 boot_desc = cmd->boot_image_file;
409 boot_desc = "(unknown)";
411 update_status(status_fn, status_arg, BOOT_STATUS_INFO,
412 "Booting %s.", boot_desc);
414 if (cmd && cmd->boot_image_file) {
415 image = pb_url_parse(opt, cmd->boot_image_file);
416 } else if (opt && opt->boot_image) {
417 image = opt->boot_image->url;
419 pb_log("%s: no image specified\n", __func__);
420 update_status(status_fn, status_arg, BOOT_STATUS_INFO,
421 "Boot failed: no image specified");
425 boot_task = talloc_zero(ctx, struct boot_task);
426 boot_task->image = image;
427 boot_task->dry_run = dry_run;
428 boot_task->status_fn = status_fn;
429 boot_task->status_arg = status_arg;
430 boot_task->state = BOOT_STATE_INITIAL;
432 if (cmd && cmd->initrd_file) {
433 boot_task->initrd = pb_url_parse(opt, cmd->initrd_file);
434 } else if (opt && opt->initrd) {
435 boot_task->initrd = opt->initrd->url;
438 if (cmd && cmd->dtb_file) {
439 boot_task->dtb = pb_url_parse(opt, cmd->dtb_file);
440 } else if (opt && opt->dtb) {
441 boot_task->dtb = opt->dtb->url;
444 if (cmd && cmd->boot_args) {
445 boot_task->args = talloc_strdup(boot_task, cmd->boot_args);
446 } else if (opt && opt->option->boot_args) {
447 boot_task->args = talloc_strdup(boot_task,
448 opt->option->boot_args);
450 boot_task->args = NULL;
453 boot_process(boot_task, 0);