5 #include <pb-protocol/pb-protocol.h>
6 #include <system/system.h>
7 #include <talloc/talloc.h>
13 * kexec_load - kexec load helper.
14 * @l_image: The local image file for kexec to execute.
15 * @l_initrd: Optional local initrd file for kexec --initrd, can be NULL.
16 * @args: Optional command line args for kexec --append, can be NULL.
19 static int kexec_load(const char *l_image, const char *l_initrd,
20 const char *args, int dry_run)
25 char *s_initrd = NULL;
29 *p++ = pb_system_apps.kexec; /* 1 */
33 s_initrd = talloc_asprintf(NULL, "--initrd=%s", l_initrd);
35 *p++ = s_initrd; /* 3 */
39 s_args = talloc_asprintf(NULL, "--append=%s", args);
41 *p++ = s_args; /* 4 */
44 *p++ = l_image; /* 5 */
47 result = pb_run_cmd(argv, 1, dry_run);
50 pb_log("%s: failed: (%d)\n", __func__, result);
52 talloc_free(s_initrd);
59 * kexec_reboot - Helper to boot the new kernel.
61 * Must only be called after a successful call to kexec_load().
64 static int kexec_reboot(int dry_run)
70 /* First try running shutdown. Init scripts should run 'exec -e' */
73 *p++ = pb_system_apps.shutdown; /* 1 */
78 result = pb_run_cmd(argv, 1, dry_run);
80 /* On error, force a kexec with the -e option */
84 *p++ = pb_system_apps.kexec; /* 1 */
88 result = pb_run_cmd(argv, 1, 0);
92 pb_log("%s: failed: (%d)\n", __func__, result);
97 int boot(void *ctx, struct boot_option *opt, struct boot_command *cmd,
100 char *local_image, *local_initrd;
101 unsigned int clean_image = 0;
102 unsigned int clean_initrd = 0;
103 char *image, *initrd, *args;
110 if (cmd->boot_image_file) {
111 image = talloc_strdup(ctx, cmd->boot_image_file);
112 } else if (opt && opt->boot_image_file) {
113 image = talloc_strdup(ctx, opt->boot_image_file);
115 pb_log("%s: no image specified", __func__);
119 if (cmd->initrd_file) {
120 initrd = talloc_strdup(ctx, cmd->initrd_file);
121 } else if (opt && opt->initrd_file) {
122 initrd = talloc_strdup(ctx, opt->initrd_file);
125 if (cmd->boot_args) {
126 args = talloc_strdup(ctx, cmd->boot_args);
127 } else if (opt && opt->boot_args) {
128 args = talloc_strdup(ctx, opt->boot_args);
133 local_image = load_file(NULL, image, &clean_image);
139 local_initrd = load_file(NULL, initrd, &clean_initrd);
144 result = kexec_load(local_image, local_initrd, args, dry_run);
150 unlink(local_initrd);
152 talloc_free(local_image);
153 talloc_free(local_initrd);
156 result = kexec_reboot(dry_run);