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;
140 talloc_free(status.message);
143 static void boot_hook_update_param(void *ctx, struct boot_task *task,
144 const char *name, const char *value)
149 } *param, params[] = {
150 { "boot_image", &task->image->local },
151 { "boot_initrd", &task->initrd->local },
152 { "boot_dtb", &task->dtb->local },
153 { "boot_args", &task->args },
157 for (param = params; param->name; param++) {
158 if (strcmp(param->name, name))
161 *param->p = talloc_strdup(ctx, value);
166 static void boot_hook_update(struct boot_task *task, const char *hookname,
169 char *line, *name, *val, *sep;
172 for (;; buf = NULL) {
174 line = strtok_r(buf, "\n", &saveptr);
178 sep = strchr(line, '=');
186 boot_hook_update_param(task, task, name, val);
188 pb_log("boot hook %s specified %s=%s\n",
189 hookname, name, val);
193 static void boot_hook_setenv(struct boot_task *task)
195 unsetenv("boot_image");
196 unsetenv("boot_initrd");
197 unsetenv("boot_dtb");
198 unsetenv("boot_args");
200 setenv("boot_image", task->image->local, 1);
202 setenv("boot_initrd", task->initrd->local, 1);
204 setenv("boot_dtb", task->dtb->local, 1);
206 setenv("boot_args", task->args, 1);
209 static int hook_filter(const struct dirent *dirent)
211 return dirent->d_type == DT_REG || dirent->d_type == DT_LNK;
214 static int hook_cmp(const struct dirent **a, const struct dirent **b)
216 return strcmp((*a)->d_name, (*b)->d_name);
219 static void run_boot_hooks(struct boot_task *task)
221 struct dirent **hooks;
224 n = scandir(boot_hook_dir, &hooks, hook_filter, hook_cmp);
228 update_status(task->status_fn, task->status_arg, BOOT_STATUS_INFO,
229 "running boot hooks");
231 boot_hook_setenv(task);
233 for (i = 0; i < n; i++) {
234 const char *argv[2] = { NULL, NULL };
235 struct process *process;
239 path = join_paths(task, boot_hook_dir, hooks[i]->d_name);
241 if (access(path, X_OK)) {
246 process = process_create(task);
249 process->path = path;
250 process->argv = argv;
251 process->keep_stdout = true;
253 pb_log("running boot hook %s\n", hooks[i]->d_name);
255 rc = process_run_sync(process);
257 pb_log("boot hook exec failed!\n");
259 } else if (WIFEXITED(process->exit_status) &&
260 WEXITSTATUS(process->exit_status)
261 == BOOT_HOOK_EXIT_UPDATE) {
262 /* if the hook returned with BOOT_HOOK_EXIT_UPDATE,
263 * then we process stdout to look for updated params
265 if (rc == BOOT_HOOK_EXIT_UPDATE) {
266 boot_hook_update(task, hooks[i]->d_name,
267 process->stdout_buf);
268 boot_hook_setenv(task);
272 process_release(process);
279 static bool load_pending(struct load_url_result *result)
281 return result && result->status == LOAD_ASYNC;
284 static int check_load(struct boot_task *task, const char *name,
285 struct load_url_result *result)
289 if (result->status != LOAD_ERROR)
292 update_status(task->status_fn, task->status_arg,
294 "Couldn't load %s", name);
298 static void cleanup_load(struct load_url_result *result)
302 if (result->status != LOAD_OK)
304 if (!result->cleanup_local)
306 unlink(result->local);
309 static void boot_process(struct load_url_result *result __attribute__((unused)),
312 struct boot_task *task = data;
315 if (load_pending(task->image) ||
316 load_pending(task->initrd) ||
317 load_pending(task->dtb))
320 if (check_load(task, "kernel image", task->image) ||
321 check_load(task, "initrd", task->initrd) ||
322 check_load(task, "dtb", task->dtb))
325 run_boot_hooks(task);
327 update_status(task->status_fn, task->status_arg, BOOT_STATUS_INFO,
328 "performing kexec_load");
330 rc = kexec_load(task);
332 update_status(task->status_fn, task->status_arg,
333 BOOT_STATUS_ERROR, "kexec load failed");
337 cleanup_load(task->image);
338 cleanup_load(task->initrd);
339 cleanup_load(task->dtb);
342 update_status(task->status_fn, task->status_arg,
344 "performing kexec reboot");
346 rc = kexec_reboot(task);
348 update_status(task->status_fn, task->status_arg,
350 "kexec reboot failed");
357 static int start_url_load(struct boot_task *task, const char *name,
358 struct pb_url *url, struct load_url_result **result)
363 *result = load_url_async(task, url, boot_process, task);
365 update_status(task->status_fn, task->status_arg,
367 "Error loading %s", name);
373 int boot(void *ctx, struct discover_boot_option *opt, struct boot_command *cmd,
374 int dry_run, boot_status_fn status_fn, void *status_arg)
376 struct pb_url *image = NULL, *initrd = NULL, *dtb = NULL;
377 struct boot_task *boot_task;
378 const char *boot_desc;
381 if (opt && opt->option->name)
382 boot_desc = opt->option->name;
383 else if (cmd && cmd->boot_image_file)
384 boot_desc = cmd->boot_image_file;
386 boot_desc = "(unknown)";
388 update_status(status_fn, status_arg, BOOT_STATUS_INFO,
389 "Booting %s.", boot_desc);
391 if (cmd && cmd->boot_image_file) {
392 image = pb_url_parse(opt, cmd->boot_image_file);
393 } else if (opt && opt->boot_image) {
394 image = opt->boot_image->url;
396 pb_log("%s: no image specified\n", __func__);
397 update_status(status_fn, status_arg, BOOT_STATUS_INFO,
398 "Boot failed: no image specified");
402 if (cmd && cmd->initrd_file) {
403 initrd = pb_url_parse(opt, cmd->initrd_file);
404 } else if (opt && opt->initrd) {
405 initrd = opt->initrd->url;
408 if (cmd && cmd->dtb_file) {
409 dtb = pb_url_parse(opt, cmd->dtb_file);
410 } else if (opt && opt->dtb) {
414 boot_task = talloc_zero(ctx, struct boot_task);
415 boot_task->dry_run = dry_run;
416 boot_task->status_fn = status_fn;
417 boot_task->status_arg = status_arg;
419 if (cmd && cmd->boot_args) {
420 boot_task->args = talloc_strdup(boot_task, cmd->boot_args);
421 } else if (opt && opt->option->boot_args) {
422 boot_task->args = talloc_strdup(boot_task,
423 opt->option->boot_args);
425 boot_task->args = NULL;
428 /* start async loads for boot resources */
429 rc = start_url_load(boot_task, "kernel image", image, &boot_task->image)
430 || start_url_load(boot_task, "initrd", initrd, &boot_task->initrd)
431 || start_url_load(boot_task, "dtb", dtb, &boot_task->dtb);
433 /* If all URLs are local, we may be done. */
435 boot_process(NULL, boot_task);