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 * @l_dtb: Optional local dtb file for kexec --dtb, can be NULL.
20 * @args: Optional command line args for kexec --append, can be NULL.
23 static int kexec_load(const char *l_image, const char *l_initrd,
24 const char *l_dtb, const char *args, int dry_run)
29 char *s_initrd = NULL;
34 *p++ = pb_system_apps.kexec; /* 1 */
38 s_initrd = talloc_asprintf(NULL, "--initrd=%s", l_initrd);
40 *p++ = s_initrd; /* 3 */
44 s_dtb = talloc_asprintf(NULL, "--dtb=%s", l_dtb);
50 s_args = talloc_asprintf(NULL, "--append=%s", args);
52 *p++ = s_args; /* 5 */
55 *p++ = l_image; /* 6*/
58 result = pb_run_cmd(argv, 1, dry_run);
61 pb_log("%s: failed: (%d)\n", __func__, result);
63 talloc_free(s_initrd);
71 * kexec_reboot - Helper to boot the new kernel.
73 * Must only be called after a successful call to kexec_load().
76 static int kexec_reboot(int dry_run)
82 /* First try running shutdown. Init scripts should run 'exec -e' */
85 *p++ = pb_system_apps.shutdown; /* 1 */
90 result = pb_run_cmd(argv, 1, dry_run);
92 /* On error, force a kexec with the -e option */
96 *p++ = pb_system_apps.kexec; /* 1 */
100 result = pb_run_cmd(argv, 1, 0);
104 pb_log("%s: failed: (%d)\n", __func__, result);
106 /* okay, kexec -e -f */
109 *p++ = pb_system_apps.kexec; /* 1 */
114 result = pb_run_cmd(argv, 1, 0);
118 pb_log("%s: failed: (%d)\n", __func__, result);
124 static void update_status(boot_status_fn fn, void *arg, int type,
127 struct boot_status status;
130 status.message = message;
131 status.progress = -1;
132 status.detail = NULL;
137 int boot(void *ctx, struct discover_boot_option *opt, struct boot_command *cmd,
138 int dry_run, boot_status_fn status_fn, void *status_arg)
140 char *local_image, *local_initrd, *local_dtb;
141 struct pb_url *image, *initrd, *dtb;
142 unsigned int clean_image = 0;
143 unsigned int clean_initrd = 0;
144 unsigned int clean_dtb = 0;
154 if (cmd && cmd->boot_image_file) {
155 image = pb_url_parse(opt, cmd->boot_image_file);
156 } else if (opt && opt->boot_image) {
157 image = opt->boot_image->url;
159 pb_log("%s: no image specified", __func__);
163 if (cmd && cmd->initrd_file) {
164 initrd = pb_url_parse(opt, cmd->initrd_file);
165 } else if (opt && opt->initrd) {
166 initrd = opt->initrd->url;
169 if (cmd && cmd->dtb_file) {
170 dtb = pb_url_parse(opt, cmd->dtb_file);
171 } else if (opt && opt->dtb) {
175 if (cmd && cmd->boot_args) {
176 args = talloc_strdup(ctx, cmd->boot_args);
177 } else if (opt && opt->option->boot_args) {
178 args = talloc_strdup(ctx, opt->option->boot_args);
183 update_status(status_fn, status_arg, BOOT_STATUS_INFO,
185 local_image = load_url(NULL, image, &clean_image);
187 update_status(status_fn, status_arg, BOOT_STATUS_ERROR,
188 "Couldn't load kernel image");
193 update_status(status_fn, status_arg, BOOT_STATUS_INFO,
195 local_initrd = load_url(NULL, initrd, &clean_initrd);
197 update_status(status_fn, status_arg, BOOT_STATUS_ERROR,
198 "Couldn't load initrd image");
205 update_status(status_fn, status_arg, BOOT_STATUS_INFO,
206 "loading device tree");
207 local_dtb = load_url(NULL, dtb, &clean_dtb);
209 update_status(status_fn, status_arg, BOOT_STATUS_ERROR,
210 "Couldn't load device tree");
215 update_status(status_fn, status_arg, BOOT_STATUS_INFO,
216 "performing kexec_load");
218 result = kexec_load(local_image, local_initrd, local_dtb,
222 update_status(status_fn, status_arg, BOOT_STATUS_ERROR,
223 "kexec load failed");
230 unlink(local_initrd);
234 talloc_free(local_image);
235 talloc_free(local_initrd);
236 talloc_free(local_dtb);
239 update_status(status_fn, status_arg, BOOT_STATUS_INFO,
240 "performing kexec reboot");
242 result = kexec_reboot(dry_run);
245 update_status(status_fn, status_arg, BOOT_STATUS_ERROR,
246 "kexec reboot failed");