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);
97 /* okay, kexec -e -f */
100 *p++ = pb_system_apps.kexec; /* 1 */
105 result = pb_run_cmd(argv, 1, 0);
109 pb_log("%s: failed: (%d)\n", __func__, result);
115 static void update_status(boot_status_fn fn, void *arg, int type,
118 struct boot_status status;
121 status.message = message;
122 status.progress = -1;
123 status.detail = NULL;
128 int boot(void *ctx, struct discover_boot_option *opt, struct boot_command *cmd,
129 int dry_run, boot_status_fn status_fn, void *status_arg)
131 char *local_image, *local_initrd;
132 unsigned int clean_image = 0;
133 unsigned int clean_initrd = 0;
134 struct pb_url *image, *initrd;
143 if (cmd->boot_image_file) {
144 image = pb_url_parse(opt, cmd->boot_image_file);
145 } else if (opt && opt->boot_image) {
146 image = opt->boot_image->url;
148 pb_log("%s: no image specified", __func__);
152 if (cmd->initrd_file) {
153 initrd = pb_url_parse(opt, cmd->initrd_file);
154 } else if (opt && opt->initrd) {
155 initrd = opt->initrd->url;
158 if (cmd->boot_args) {
159 args = talloc_strdup(ctx, cmd->boot_args);
160 } else if (opt && opt->option->boot_args) {
161 args = talloc_strdup(ctx, opt->option->boot_args);
166 update_status(status_fn, status_arg, BOOT_STATUS_INFO,
168 local_image = load_url(NULL, image, &clean_image);
170 update_status(status_fn, status_arg, BOOT_STATUS_ERROR,
171 "Couldn't load kernel image");
176 update_status(status_fn, status_arg, BOOT_STATUS_INFO,
178 local_initrd = load_url(NULL, initrd, &clean_initrd);
180 update_status(status_fn, status_arg, BOOT_STATUS_ERROR,
181 "Couldn't load initrd image");
186 update_status(status_fn, status_arg, BOOT_STATUS_INFO,
187 "performing kexec_load");
189 result = kexec_load(local_image, local_initrd, args, dry_run);
192 update_status(status_fn, status_arg, BOOT_STATUS_ERROR,
193 "kexec load failed");
200 unlink(local_initrd);
202 talloc_free(local_image);
203 talloc_free(local_initrd);
206 update_status(status_fn, status_arg, BOOT_STATUS_INFO,
207 "performing kexec reboot");
209 result = kexec_reboot(dry_run);
212 update_status(status_fn, status_arg, BOOT_STATUS_ERROR,
213 "kexec reboot failed");