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 int boot(void *ctx, struct discover_boot_option *opt, struct boot_command *cmd,
103 char *local_image, *local_initrd;
104 unsigned int clean_image = 0;
105 unsigned int clean_initrd = 0;
106 struct pb_url *image, *initrd;
115 if (cmd->boot_image_file) {
116 image = pb_url_parse(opt, cmd->boot_image_file);
117 } else if (opt && opt->boot_image) {
118 image = opt->boot_image->url;
120 pb_log("%s: no image specified", __func__);
124 if (cmd->initrd_file) {
125 initrd = pb_url_parse(opt, cmd->initrd_file);
126 } else if (opt && opt->initrd) {
127 initrd = opt->initrd->url;
130 if (cmd->boot_args) {
131 args = talloc_strdup(ctx, cmd->boot_args);
132 } else if (opt && opt->option->boot_args) {
133 args = talloc_strdup(ctx, opt->option->boot_args);
138 local_image = load_url(NULL, image, &clean_image);
143 local_initrd = load_url(NULL, initrd, &clean_initrd);
148 result = kexec_load(local_image, local_initrd, args, dry_run);
154 unlink(local_initrd);
156 talloc_free(local_image);
157 talloc_free(local_initrd);
160 result = kexec_reboot(dry_run);