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(boot_task, "--initrd=%s",
57 boot_task->local_initrd);
59 *p++ = s_initrd; /* 3 */
62 if (boot_task->local_dtb) {
63 s_dtb = talloc_asprintf(boot_task, "--dtb=%s",
64 boot_task->local_dtb);
69 if (boot_task->args) {
70 s_args = talloc_asprintf(boot_task, "--append=%s",
73 *p++ = s_args; /* 5 */
76 *p++ = boot_task->local_image; /* 6 */
79 result = pb_run_cmd(argv, 1, boot_task->dry_run);
82 pb_log("%s: failed: (%d)\n", __func__, result);
88 * kexec_reboot - Helper to boot the new kernel.
90 * Must only be called after a successful call to kexec_load().
93 static int kexec_reboot(bool dry_run)
99 /* First try running shutdown. Init scripts should run 'exec -e' */
102 *p++ = pb_system_apps.shutdown; /* 1 */
104 *p++ = "now"; /* 3 */
107 result = pb_run_cmd(argv, 1, dry_run);
109 /* On error, force a kexec with the -e option */
113 *p++ = pb_system_apps.kexec; /* 1 */
117 result = pb_run_cmd(argv, 1, 0);
121 pb_log("%s: failed: (%d)\n", __func__, result);
123 /* okay, kexec -e -f */
126 *p++ = pb_system_apps.kexec; /* 1 */
131 result = pb_run_cmd(argv, 1, 0);
135 pb_log("%s: failed: (%d)\n", __func__, result);
141 static void update_status(boot_status_fn fn, void *arg, int type,
144 struct boot_status status;
147 status.message = message;
148 status.progress = -1;
149 status.detail = NULL;
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, boot_status_fn status_fn,
233 struct dirent **hooks;
236 n = scandir(boot_hook_dir, &hooks, hook_filter, hook_cmp);
240 update_status(status_fn, status_arg, BOOT_STATUS_INFO,
241 "running boot hooks");
243 boot_hook_setenv(task);
245 for (i = 0; i < n; i++) {
246 const char *argv[2] = { NULL, NULL };
250 path = join_paths(task, boot_hook_dir, hooks[i]->d_name);
252 if (access(path, X_OK))
255 pb_log("running boot hook %s\n", hooks[i]->d_name);
258 rc = pb_run_cmd_pipe(argv, 1, task->dry_run, task,
261 /* if the hook returned with BOOT_HOOK_EXIT_UPDATE,
262 * then we process stdout to look for updated params
264 if (rc == BOOT_HOOK_EXIT_UPDATE) {
265 boot_hook_update(task, hooks[i]->d_name, buf);
266 boot_hook_setenv(task);
276 int boot(void *ctx, struct discover_boot_option *opt, struct boot_command *cmd,
277 int dry_run, boot_status_fn status_fn, void *status_arg)
279 struct boot_task *boot_task;
280 struct pb_url *image, *initrd, *dtb;
281 unsigned int clean_image = 0;
282 unsigned int clean_initrd = 0;
283 unsigned int clean_dtb = 0;
290 if (cmd && cmd->boot_image_file) {
291 image = pb_url_parse(opt, cmd->boot_image_file);
292 } else if (opt && opt->boot_image) {
293 image = opt->boot_image->url;
295 pb_log("%s: no image specified", __func__);
299 boot_task = talloc_zero(ctx, struct boot_task);
300 boot_task->dry_run = dry_run;
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(boot_task, cmd->boot_args);
316 } else if (opt && opt->option->boot_args) {
317 boot_task->args = talloc_strdup(boot_task,
318 opt->option->boot_args);
320 boot_task->args = NULL;
325 update_status(status_fn, status_arg, BOOT_STATUS_INFO,
327 boot_task->local_image = load_url(NULL, image, &clean_image);
328 if (!boot_task->local_image) {
329 update_status(status_fn, status_arg, BOOT_STATUS_ERROR,
330 "Couldn't load kernel image");
335 update_status(status_fn, status_arg, BOOT_STATUS_INFO,
337 boot_task->local_initrd = load_url(NULL, initrd, &clean_initrd);
338 if (!boot_task->local_initrd) {
339 update_status(status_fn, status_arg, BOOT_STATUS_ERROR,
340 "Couldn't load initrd image");
346 update_status(status_fn, status_arg, BOOT_STATUS_INFO,
347 "loading device tree");
348 boot_task->local_dtb = load_url(NULL, dtb, &clean_dtb);
349 if (!boot_task->local_dtb) {
350 update_status(status_fn, status_arg, BOOT_STATUS_ERROR,
351 "Couldn't load device tree");
356 run_boot_hooks(boot_task, status_fn, status_arg);
358 update_status(status_fn, status_arg, BOOT_STATUS_INFO,
359 "performing kexec_load");
361 result = kexec_load(boot_task);
364 update_status(status_fn, status_arg, BOOT_STATUS_ERROR,
365 "kexec load failed");
370 unlink(boot_task->local_image);
372 unlink(boot_task->local_initrd);
374 unlink(boot_task->local_dtb);
376 talloc_free(boot_task);
379 update_status(status_fn, status_arg, BOOT_STATUS_INFO,
380 "performing kexec reboot");
382 result = kexec_reboot(boot_task->dry_run);
385 update_status(status_fn, status_arg, BOOT_STATUS_ERROR,
386 "kexec reboot failed");