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,
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->local_initrd) {
57 s_initrd = talloc_asprintf(boot_task, "--initrd=%s",
58 boot_task->local_initrd);
60 *p++ = s_initrd; /* 3 */
63 if (boot_task->local_dtb) {
64 s_dtb = talloc_asprintf(boot_task, "--dtb=%s",
65 boot_task->local_dtb);
70 if (boot_task->args) {
71 s_args = talloc_asprintf(boot_task, "--append=%s",
74 *p++ = s_args; /* 5 */
77 *p++ = boot_task->local_image; /* 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->local_image },
151 { "boot_initrd", &task->local_initrd },
152 { "boot_dtb", &task->local_dtb },
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->local_image, 1);
201 if (task->local_initrd)
202 setenv("boot_initrd", task->local_initrd, 1);
204 setenv("boot_dtb", task->local_dtb, 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, boot_status_fn status_fn,
222 struct dirent **hooks;
225 n = scandir(boot_hook_dir, &hooks, hook_filter, hook_cmp);
229 update_status(status_fn, status_arg, BOOT_STATUS_INFO,
230 "running boot hooks");
232 boot_hook_setenv(task);
234 for (i = 0; i < n; i++) {
235 const char *argv[2] = { NULL, NULL };
236 struct process *process;
240 path = join_paths(task, boot_hook_dir, hooks[i]->d_name);
242 if (access(path, X_OK)) {
247 process = process_create(task);
250 process->path = path;
251 process->argv = argv;
252 process->keep_stdout = true;
254 pb_log("running boot hook %s\n", hooks[i]->d_name);
256 rc = process_run_sync(process);
258 pb_log("boot hook exec failed!\n");
260 } else if (WIFEXITED(process->exit_status) &&
261 WEXITSTATUS(process->exit_status)
262 == BOOT_HOOK_EXIT_UPDATE) {
263 /* if the hook returned with BOOT_HOOK_EXIT_UPDATE,
264 * then we process stdout to look for updated params
266 if (rc == BOOT_HOOK_EXIT_UPDATE) {
267 boot_hook_update(task, hooks[i]->d_name,
268 process->stdout_buf);
269 boot_hook_setenv(task);
273 process_release(process);
280 int boot(void *ctx, struct discover_boot_option *opt, struct boot_command *cmd,
281 int dry_run, boot_status_fn status_fn, void *status_arg)
283 struct boot_task *boot_task;
284 struct pb_url *image, *initrd, *dtb;
285 unsigned int clean_image = 0;
286 unsigned int clean_initrd = 0;
287 unsigned int clean_dtb = 0;
288 const char *boot_desc;
295 if (opt && opt->option->name)
296 boot_desc = opt->option->name;
297 else if (cmd && cmd->boot_image_file)
298 boot_desc = cmd->boot_image_file;
300 boot_desc = "(unknown)";
302 update_status(status_fn, status_arg, BOOT_STATUS_INFO,
303 "Booting %s.", boot_desc);
305 if (cmd && cmd->boot_image_file) {
306 image = pb_url_parse(opt, cmd->boot_image_file);
307 } else if (opt && opt->boot_image) {
308 image = opt->boot_image->url;
310 pb_log("%s: no image specified\n", __func__);
311 update_status(status_fn, status_arg, BOOT_STATUS_INFO,
312 "Boot failed: no image specified");
316 boot_task = talloc_zero(ctx, struct boot_task);
317 boot_task->dry_run = dry_run;
319 if (cmd && cmd->initrd_file) {
320 initrd = pb_url_parse(opt, cmd->initrd_file);
321 } else if (opt && opt->initrd) {
322 initrd = opt->initrd->url;
325 if (cmd && cmd->dtb_file) {
326 dtb = pb_url_parse(opt, cmd->dtb_file);
327 } else if (opt && opt->dtb) {
331 if (cmd && cmd->boot_args) {
332 boot_task->args = talloc_strdup(boot_task, cmd->boot_args);
333 } else if (opt && opt->option->boot_args) {
334 boot_task->args = talloc_strdup(boot_task,
335 opt->option->boot_args);
337 boot_task->args = NULL;
342 update_status(status_fn, status_arg, BOOT_STATUS_INFO,
344 boot_task->local_image = load_url(NULL, image, &clean_image);
345 if (!boot_task->local_image) {
346 update_status(status_fn, status_arg, BOOT_STATUS_ERROR,
347 "Couldn't load kernel image");
352 update_status(status_fn, status_arg, BOOT_STATUS_INFO,
354 boot_task->local_initrd = load_url(NULL, initrd, &clean_initrd);
355 if (!boot_task->local_initrd) {
356 update_status(status_fn, status_arg, BOOT_STATUS_ERROR,
357 "Couldn't load initrd image");
363 update_status(status_fn, status_arg, BOOT_STATUS_INFO,
364 "loading device tree");
365 boot_task->local_dtb = load_url(NULL, dtb, &clean_dtb);
366 if (!boot_task->local_dtb) {
367 update_status(status_fn, status_arg, BOOT_STATUS_ERROR,
368 "Couldn't load device tree");
373 run_boot_hooks(boot_task, status_fn, status_arg);
375 update_status(status_fn, status_arg, BOOT_STATUS_INFO,
376 "performing kexec_load");
378 result = kexec_load(boot_task);
381 update_status(status_fn, status_arg, BOOT_STATUS_ERROR,
382 "kexec load failed");
387 unlink(boot_task->local_image);
389 unlink(boot_task->local_initrd);
391 unlink(boot_task->local_dtb);
394 update_status(status_fn, status_arg, BOOT_STATUS_INFO,
395 "performing kexec reboot");
397 result = kexec_reboot(boot_task);
400 update_status(status_fn, status_arg, BOOT_STATUS_ERROR,
401 "kexec reboot failed");
405 talloc_free(boot_task);