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,
31 struct load_url_result *image;
32 struct load_url_result *initrd;
33 struct load_url_result *dtb;
35 boot_status_fn status_fn;
41 * kexec_load - kexec load helper.
43 static int kexec_load(struct boot_task *boot_task)
48 char *s_initrd = NULL;
53 *p++ = pb_system_apps.kexec; /* 1 */
56 if (boot_task->initrd) {
57 s_initrd = talloc_asprintf(boot_task, "--initrd=%s",
58 boot_task->initrd->local);
60 *p++ = s_initrd; /* 3 */
64 s_dtb = talloc_asprintf(boot_task, "--dtb=%s",
65 boot_task->dtb->local);
70 if (boot_task->args) {
71 s_args = talloc_asprintf(boot_task, "--append=%s",
74 *p++ = s_args; /* 5 */
77 *p++ = boot_task->image->local; /* 6 */
80 result = process_run_simple_argv(boot_task, argv);
83 pb_log("%s: failed: (%d)\n", __func__, result);
89 * kexec_reboot - Helper to boot the new kernel.
91 * Must only be called after a successful call to kexec_load().
94 static int kexec_reboot(struct boot_task *task)
98 /* First try running shutdown. Init scripts should run 'exec -e' */
99 result = process_run_simple(task, pb_system_apps.shutdown, "-r",
102 /* On error, force a kexec with the -e option */
104 result = process_run_simple(task, pb_system_apps.kexec,
109 pb_log("%s: failed: (%d)\n", __func__, result);
111 /* okay, kexec -e -f */
113 result = process_run_simple(task, pb_system_apps.kexec,
118 pb_log("%s: failed: (%d)\n", __func__, result);
124 static void __attribute__((format(__printf__, 4, 5))) update_status(
125 boot_status_fn fn, void *arg, int type, char *fmt, ...)
127 struct boot_status status;
131 status.message = talloc_vasprintf(NULL, fmt, ap);
135 status.progress = -1;
136 status.detail = NULL;
138 pb_debug("boot status: [%d] %s\n", type, status.message);
142 talloc_free(status.message);
145 static void boot_hook_update_param(void *ctx, struct boot_task *task,
146 const char *name, const char *value)
151 } *param, params[] = {
152 { "boot_image", &task->image->local },
153 { "boot_initrd", &task->initrd->local },
154 { "boot_dtb", &task->dtb->local },
155 { "boot_args", &task->args },
159 for (param = params; param->name; param++) {
160 if (strcmp(param->name, name))
163 *param->p = talloc_strdup(ctx, value);
168 static void boot_hook_update(struct boot_task *task, const char *hookname,
171 char *line, *name, *val, *sep;
174 for (;; buf = NULL) {
176 line = strtok_r(buf, "\n", &saveptr);
180 sep = strchr(line, '=');
188 boot_hook_update_param(task, task, name, val);
190 pb_log("boot hook %s specified %s=%s\n",
191 hookname, name, val);
195 static void boot_hook_setenv(struct boot_task *task)
197 unsetenv("boot_image");
198 unsetenv("boot_initrd");
199 unsetenv("boot_dtb");
200 unsetenv("boot_args");
202 setenv("boot_image", task->image->local, 1);
204 setenv("boot_initrd", task->initrd->local, 1);
206 setenv("boot_dtb", task->dtb->local, 1);
208 setenv("boot_args", task->args, 1);
211 static int hook_filter(const struct dirent *dirent)
213 return dirent->d_type == DT_REG || dirent->d_type == DT_LNK;
216 static int hook_cmp(const struct dirent **a, const struct dirent **b)
218 return strcmp((*a)->d_name, (*b)->d_name);
221 static void run_boot_hooks(struct boot_task *task)
223 struct dirent **hooks;
226 n = scandir(boot_hook_dir, &hooks, hook_filter, hook_cmp);
230 update_status(task->status_fn, task->status_arg, BOOT_STATUS_INFO,
231 "running boot hooks");
233 boot_hook_setenv(task);
235 for (i = 0; i < n; i++) {
236 const char *argv[2] = { NULL, NULL };
237 struct process *process;
241 path = join_paths(task, boot_hook_dir, hooks[i]->d_name);
243 if (access(path, X_OK)) {
248 process = process_create(task);
251 process->path = path;
252 process->argv = argv;
253 process->keep_stdout = true;
255 pb_log("running boot hook %s\n", hooks[i]->d_name);
257 rc = process_run_sync(process);
259 pb_log("boot hook exec failed!\n");
261 } else if (WIFEXITED(process->exit_status) &&
262 WEXITSTATUS(process->exit_status)
263 == BOOT_HOOK_EXIT_UPDATE) {
264 /* if the hook returned with BOOT_HOOK_EXIT_UPDATE,
265 * then we process stdout to look for updated params
267 if (rc == BOOT_HOOK_EXIT_UPDATE) {
268 boot_hook_update(task, hooks[i]->d_name,
269 process->stdout_buf);
270 boot_hook_setenv(task);
274 process_release(process);
281 static bool load_pending(struct load_url_result *result)
283 return result && result->status == LOAD_ASYNC;
286 static int check_load(struct boot_task *task, const char *name,
287 struct load_url_result *result)
291 if (result->status != LOAD_ERROR)
294 update_status(task->status_fn, task->status_arg,
296 "Couldn't load %s", name);
300 static void cleanup_load(struct load_url_result *result)
304 if (result->status != LOAD_OK)
306 if (!result->cleanup_local)
308 unlink(result->local);
311 static void boot_process(struct load_url_result *result __attribute__((unused)),
314 struct boot_task *task = data;
317 if (load_pending(task->image) ||
318 load_pending(task->initrd) ||
319 load_pending(task->dtb))
322 if (check_load(task, "kernel image", task->image) ||
323 check_load(task, "initrd", task->initrd) ||
324 check_load(task, "dtb", task->dtb))
327 run_boot_hooks(task);
329 update_status(task->status_fn, task->status_arg, BOOT_STATUS_INFO,
330 "performing kexec_load");
332 rc = kexec_load(task);
334 update_status(task->status_fn, task->status_arg,
335 BOOT_STATUS_ERROR, "kexec load failed");
339 cleanup_load(task->image);
340 cleanup_load(task->initrd);
341 cleanup_load(task->dtb);
344 update_status(task->status_fn, task->status_arg,
346 "performing kexec reboot");
348 rc = kexec_reboot(task);
350 update_status(task->status_fn, task->status_arg,
352 "kexec reboot failed");
359 static int start_url_load(struct boot_task *task, const char *name,
360 struct pb_url *url, struct load_url_result **result)
365 *result = load_url_async(task, url, boot_process, task);
367 update_status(task->status_fn, task->status_arg,
369 "Error loading %s", name);
375 int boot(void *ctx, struct discover_boot_option *opt, struct boot_command *cmd,
376 int dry_run, boot_status_fn status_fn, void *status_arg)
378 struct pb_url *image = NULL, *initrd = NULL, *dtb = NULL;
379 struct boot_task *boot_task;
380 const char *boot_desc;
383 if (opt && opt->option->name)
384 boot_desc = opt->option->name;
385 else if (cmd && cmd->boot_image_file)
386 boot_desc = cmd->boot_image_file;
388 boot_desc = "(unknown)";
390 update_status(status_fn, status_arg, BOOT_STATUS_INFO,
391 "Booting %s.", boot_desc);
393 if (cmd && cmd->boot_image_file) {
394 image = pb_url_parse(opt, cmd->boot_image_file);
395 } else if (opt && opt->boot_image) {
396 image = opt->boot_image->url;
398 pb_log("%s: no image specified\n", __func__);
399 update_status(status_fn, status_arg, BOOT_STATUS_INFO,
400 "Boot failed: no image specified");
404 if (cmd && cmd->initrd_file) {
405 initrd = pb_url_parse(opt, cmd->initrd_file);
406 } else if (opt && opt->initrd) {
407 initrd = opt->initrd->url;
410 if (cmd && cmd->dtb_file) {
411 dtb = pb_url_parse(opt, cmd->dtb_file);
412 } else if (opt && opt->dtb) {
416 boot_task = talloc_zero(ctx, struct boot_task);
417 boot_task->dry_run = dry_run;
418 boot_task->status_fn = status_fn;
419 boot_task->status_arg = status_arg;
421 if (cmd && cmd->boot_args) {
422 boot_task->args = talloc_strdup(boot_task, cmd->boot_args);
423 } else if (opt && opt->option->boot_args) {
424 boot_task->args = talloc_strdup(boot_task,
425 opt->option->boot_args);
427 boot_task->args = NULL;
430 /* start async loads for boot resources */
431 rc = start_url_load(boot_task, "kernel image", image, &boot_task->image)
432 || start_url_load(boot_task, "initrd", initrd, &boot_task->initrd)
433 || start_url_load(boot_task, "dtb", dtb, &boot_task->dtb);
435 /* If all URLs are local, we may be done. */
437 boot_process(NULL, boot_task);