5 #include <pb-protocol/pb-protocol.h>
6 #include <system/system.h>
7 #include <talloc/talloc.h>
10 #include "device-handler.h"
16 * kexec_load - kexec load helper.
17 * @l_image: The local image file for kexec to execute.
18 * @l_initrd: Optional local initrd file for kexec --initrd, can be NULL.
19 * @args: Optional command line args for kexec --append, can be NULL.
22 static int kexec_load(const char *l_image, const char *l_initrd,
23 const char *args, int dry_run)
28 char *s_initrd = NULL;
32 *p++ = pb_system_apps.kexec; /* 1 */
36 s_initrd = talloc_asprintf(NULL, "--initrd=%s", l_initrd);
38 *p++ = s_initrd; /* 3 */
42 s_args = talloc_asprintf(NULL, "--append=%s", args);
44 *p++ = s_args; /* 4 */
47 *p++ = l_image; /* 5 */
50 result = pb_run_cmd(argv, 1, dry_run);
53 pb_log("%s: failed: (%d)\n", __func__, result);
55 talloc_free(s_initrd);
62 * kexec_reboot - Helper to boot the new kernel.
64 * Must only be called after a successful call to kexec_load().
67 static int kexec_reboot(int dry_run)
73 /* First try running shutdown. Init scripts should run 'exec -e' */
76 *p++ = pb_system_apps.shutdown; /* 1 */
81 result = pb_run_cmd(argv, 1, dry_run);
83 /* On error, force a kexec with the -e option */
87 *p++ = pb_system_apps.kexec; /* 1 */
91 result = pb_run_cmd(argv, 1, 0);
95 pb_log("%s: failed: (%d)\n", __func__, result);
100 static void update_status(boot_status_fn fn, void *arg, int type,
103 struct boot_status status;
106 status.message = message;
107 status.progress = -1;
108 status.detail = NULL;
113 int boot(void *ctx, struct discover_boot_option *opt, struct boot_command *cmd,
114 int dry_run, boot_status_fn status_fn, void *status_arg)
116 char *local_image, *local_initrd;
117 unsigned int clean_image = 0;
118 unsigned int clean_initrd = 0;
119 struct pb_url *image, *initrd;
128 if (cmd->boot_image_file) {
129 image = pb_url_parse(opt, cmd->boot_image_file);
130 } else if (opt && opt->boot_image) {
131 image = opt->boot_image->url;
133 pb_log("%s: no image specified", __func__);
137 if (cmd->initrd_file) {
138 initrd = pb_url_parse(opt, cmd->initrd_file);
139 } else if (opt && opt->initrd) {
140 initrd = opt->initrd->url;
143 if (cmd->boot_args) {
144 args = talloc_strdup(ctx, cmd->boot_args);
145 } else if (opt && opt->option->boot_args) {
146 args = talloc_strdup(ctx, opt->option->boot_args);
151 update_status(status_fn, status_arg, BOOT_STATUS_INFO,
153 local_image = load_url(NULL, image, &clean_image);
155 update_status(status_fn, status_arg, BOOT_STATUS_ERROR,
156 "Couldn't load kernel image");
161 update_status(status_fn, status_arg, BOOT_STATUS_INFO,
163 local_initrd = load_url(NULL, initrd, &clean_initrd);
165 update_status(status_fn, status_arg, BOOT_STATUS_ERROR,
166 "Couldn't load initrd image");
171 update_status(status_fn, status_arg, BOOT_STATUS_INFO,
172 "performing kexec_load");
174 result = kexec_load(local_image, local_initrd, args, dry_run);
177 update_status(status_fn, status_arg, BOOT_STATUS_ERROR,
178 "kexec load failed");
185 unlink(local_initrd);
187 talloc_free(local_image);
188 talloc_free(local_initrd);
191 update_status(status_fn, status_arg, BOOT_STATUS_INFO,
192 "performing kexec reboot");
194 result = kexec_reboot(dry_run);
197 update_status(status_fn, status_arg, BOOT_STATUS_ERROR,
198 "kexec reboot failed");