10 #include <sys/types.h>
13 #include <pb-protocol/pb-protocol.h>
14 #include <system/system.h>
15 #include <talloc/talloc.h>
18 #include "device-handler.h"
23 static const char *boot_hook_dir = PKG_SYSCONF_DIR "/boot.d";
25 BOOT_HOOK_EXIT_OK = 0,
26 BOOT_HOOK_EXIT_UPDATE = 2,
40 * kexec_load - kexec load helper.
42 static int kexec_load(struct boot_task *boot_task)
47 char *s_initrd = NULL;
52 *p++ = pb_system_apps.kexec; /* 1 */
55 if (boot_task->local_initrd) {
56 s_initrd = talloc_asprintf(NULL, "--initrd=%s",
57 boot_task->local_initrd);
59 *p++ = s_initrd; /* 3 */
62 if (boot_task->local_dtb) {
63 s_dtb = talloc_asprintf(NULL, "--dtb=%s", boot_task->local_dtb);
68 if (boot_task->args) {
69 s_args = talloc_asprintf(NULL, "--append=%s", boot_task->args);
71 *p++ = s_args; /* 5 */
74 *p++ = boot_task->local_image; /* 6 */
77 result = pb_run_cmd(argv, 1, boot_task->dry_run);
80 pb_log("%s: failed: (%d)\n", __func__, result);
82 talloc_free(s_initrd);
90 * kexec_reboot - Helper to boot the new kernel.
92 * Must only be called after a successful call to kexec_load().
95 static int kexec_reboot(bool dry_run)
101 /* First try running shutdown. Init scripts should run 'exec -e' */
104 *p++ = pb_system_apps.shutdown; /* 1 */
106 *p++ = "now"; /* 3 */
109 result = pb_run_cmd(argv, 1, dry_run);
111 /* On error, force a kexec with the -e option */
115 *p++ = pb_system_apps.kexec; /* 1 */
119 result = pb_run_cmd(argv, 1, 0);
123 pb_log("%s: failed: (%d)\n", __func__, result);
125 /* okay, kexec -e -f */
128 *p++ = pb_system_apps.kexec; /* 1 */
133 result = pb_run_cmd(argv, 1, 0);
137 pb_log("%s: failed: (%d)\n", __func__, result);
143 static void update_status(boot_status_fn fn, void *arg, int type,
146 struct boot_status status;
149 status.message = message;
150 status.progress = -1;
151 status.detail = NULL;
156 static void boot_hook_update_param(void *ctx, struct boot_task *task,
157 const char *name, const char *value)
162 } *param, params[] = {
163 { "boot_image", &task->local_image },
164 { "boot_initrd", &task->local_initrd },
165 { "boot_dtb", &task->local_dtb },
166 { "boot_args", &task->args },
170 for (param = params; param->name; param++) {
171 if (strcmp(param->name, name))
174 *param->p = talloc_strdup(ctx, value);
179 static void boot_hook_update(void *ctx, const char *hookname,
180 struct boot_task *task, char *buf)
182 char *line, *name, *val, *sep;
185 for (;; buf = NULL) {
187 line = strtok_r(buf, "\n", &saveptr);
191 sep = strchr(line, '=');
199 boot_hook_update_param(ctx, task, name, val);
201 pb_log("boot hook %s specified %s=%s\n",
202 hookname, name, val);
206 static void boot_hook_setenv(struct boot_task *task)
208 unsetenv("boot_image");
209 unsetenv("boot_initrd");
210 unsetenv("boot_dtb");
211 unsetenv("boot_args");
213 setenv("boot_image", task->local_image, 1);
214 if (task->local_initrd)
215 setenv("boot_initrd", task->local_initrd, 1);
217 setenv("boot_dtb", task->local_dtb, 1);
219 setenv("boot_args", task->args, 1);
222 static int hook_filter(const struct dirent *dirent)
224 return dirent->d_type == DT_REG || dirent->d_type == DT_LNK;
227 static int hook_cmp(const struct dirent **a, const struct dirent **b)
229 return strcmp((*a)->d_name, (*b)->d_name);
232 static void run_boot_hooks(void *ctx, struct boot_task *task,
233 boot_status_fn status_fn, void *status_arg)
235 struct dirent **hooks;
238 n = scandir(boot_hook_dir, &hooks, hook_filter, hook_cmp);
242 update_status(status_fn, status_arg, BOOT_STATUS_INFO,
243 "running boot hooks");
245 boot_hook_setenv(task);
247 for (i = 0; i < n; i++) {
248 const char *argv[2] = { NULL, NULL };
252 path = join_paths(ctx, boot_hook_dir, hooks[i]->d_name);
254 if (access(path, X_OK))
257 pb_log("running boot hook %s\n", hooks[i]->d_name);
260 rc = pb_run_cmd_pipe(argv, 1, task->dry_run, ctx,
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(ctx, hooks[i]->d_name, task, buf);
268 boot_hook_setenv(task);
278 int boot(void *ctx, struct discover_boot_option *opt, struct boot_command *cmd,
279 int dry_run, boot_status_fn status_fn, void *status_arg)
281 struct boot_task boot_task;
282 struct pb_url *image, *initrd, *dtb;
283 unsigned int clean_image = 0;
284 unsigned int clean_initrd = 0;
285 unsigned int clean_dtb = 0;
291 boot_task.dry_run = dry_run;
293 if (cmd && cmd->boot_image_file) {
294 image = pb_url_parse(opt, cmd->boot_image_file);
295 } else if (opt && opt->boot_image) {
296 image = opt->boot_image->url;
298 pb_log("%s: no image specified", __func__);
302 if (cmd && cmd->initrd_file) {
303 initrd = pb_url_parse(opt, cmd->initrd_file);
304 } else if (opt && opt->initrd) {
305 initrd = opt->initrd->url;
308 if (cmd && cmd->dtb_file) {
309 dtb = pb_url_parse(opt, cmd->dtb_file);
310 } else if (opt && opt->dtb) {
314 if (cmd && cmd->boot_args) {
315 boot_task.args = talloc_strdup(ctx, cmd->boot_args);
316 } else if (opt && opt->option->boot_args) {
317 boot_task.args = talloc_strdup(ctx, opt->option->boot_args);
319 boot_task.args = NULL;
324 boot_task.local_initrd = NULL;
325 boot_task.local_dtb = NULL;
327 update_status(status_fn, status_arg, BOOT_STATUS_INFO,
329 boot_task.local_image = load_url(NULL, image, &clean_image);
330 if (!boot_task.local_image) {
331 update_status(status_fn, status_arg, BOOT_STATUS_ERROR,
332 "Couldn't load kernel image");
337 update_status(status_fn, status_arg, BOOT_STATUS_INFO,
339 boot_task.local_initrd = load_url(NULL, initrd, &clean_initrd);
340 if (!boot_task.local_initrd) {
341 update_status(status_fn, status_arg, BOOT_STATUS_ERROR,
342 "Couldn't load initrd image");
348 update_status(status_fn, status_arg, BOOT_STATUS_INFO,
349 "loading device tree");
350 boot_task.local_dtb = load_url(NULL, dtb, &clean_dtb);
351 if (!boot_task.local_dtb) {
352 update_status(status_fn, status_arg, BOOT_STATUS_ERROR,
353 "Couldn't load device tree");
358 run_boot_hooks(ctx, &boot_task, status_fn, status_arg);
360 update_status(status_fn, status_arg, BOOT_STATUS_INFO,
361 "performing kexec_load");
363 result = kexec_load(&boot_task);
366 update_status(status_fn, status_arg, BOOT_STATUS_ERROR,
367 "kexec load failed");
372 unlink(boot_task.local_image);
374 unlink(boot_task.local_initrd);
376 unlink(boot_task.local_dtb);
378 talloc_free(boot_task.local_image);
379 talloc_free(boot_task.local_initrd);
380 talloc_free(boot_task.local_dtb);
383 update_status(status_fn, status_arg, BOOT_STATUS_INFO,
384 "performing kexec reboot");
386 result = kexec_reboot(boot_task.dry_run);
389 update_status(status_fn, status_arg, BOOT_STATUS_ERROR,
390 "kexec reboot failed");