]> git.ozlabs.org Git - petitboot/blob - discover/boot.c
6109562491db79855655a4bf8431e07c20bd1e0f
[petitboot] / discover / boot.c
1
2 #include <assert.h>
3
4 #include <log/log.h>
5 #include <pb-protocol/pb-protocol.h>
6 #include <system/system.h>
7 #include <talloc/talloc.h>
8
9 #include "boot.h"
10 #include "paths.h"
11
12 /**
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.
17  */
18
19 static int kexec_load(const char *l_image, const char *l_initrd,
20         const char *args, int dry_run)
21 {
22         int result;
23         const char *argv[6];
24         const char **p;
25         char *s_initrd = NULL;
26         char *s_args = NULL;
27
28         p = argv;
29         *p++ = pb_system_apps.kexec;    /* 1 */
30         *p++ = "-l";                    /* 2 */
31
32         if (l_initrd) {
33                 s_initrd = talloc_asprintf(NULL, "--initrd=%s", l_initrd);
34                 assert(s_initrd);
35                 *p++ = s_initrd;         /* 3 */
36         }
37
38         if (args) {
39                 s_args = talloc_asprintf(NULL, "--append=%s", args);
40                 assert(s_args);
41                 *p++ = s_args;          /* 4 */
42         }
43
44         *p++ = l_image;                 /* 5 */
45         *p++ = NULL;                    /* 6 */
46
47         result = pb_run_cmd(argv, 1, dry_run);
48
49         if (result)
50                 pb_log("%s: failed: (%d)\n", __func__, result);
51
52         talloc_free(s_initrd);
53         talloc_free(s_args);
54
55         return result;
56 }
57
58 /**
59  * kexec_reboot - Helper to boot the new kernel.
60  *
61  * Must only be called after a successful call to kexec_load().
62  */
63
64 static int kexec_reboot(int dry_run)
65 {
66         int result = 0;
67         const char *argv[4];
68         const char **p;
69
70         /* First try running shutdown.  Init scripts should run 'exec -e' */
71
72         p = argv;
73         *p++ = pb_system_apps.shutdown; /* 1 */
74         *p++ =  "-r";                   /* 2 */
75         *p++ =  "now";                  /* 3 */
76         *p++ =  NULL;                   /* 4 */
77
78         result = pb_run_cmd(argv, 1, dry_run);
79
80         /* On error, force a kexec with the -e option */
81
82         if (result) {
83                 p = argv;
84                 *p++ = pb_system_apps.kexec;    /* 1 */
85                 *p++ = "-e";                    /* 2 */
86                 *p++ = NULL;                    /* 3 */
87
88                 result = pb_run_cmd(argv, 1, 0);
89         }
90
91         if (result)
92                 pb_log("%s: failed: (%d)\n", __func__, result);
93
94         return result;
95 }
96
97 int boot(void *ctx, struct boot_option *opt, struct boot_command *cmd,
98                 int dry_run)
99 {
100         char *local_image, *local_initrd;
101         unsigned int clean_image = 0;
102         unsigned int clean_initrd = 0;
103         char *image, *initrd, *args;
104         int result;
105
106         image = NULL;
107         initrd = NULL;
108         args = NULL;
109
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);
114         } else {
115                 pb_log("%s: no image specified", __func__);
116                 return -1;
117         }
118
119         if (cmd->initrd_file) {
120                 image = talloc_strdup(ctx, cmd->initrd_file);
121         } else if (opt && opt->initrd_file) {
122                 image = talloc_strdup(ctx, opt->initrd_file);
123         }
124
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);
129         }
130
131         result = -1;
132
133         local_image = load_file(NULL, image, &clean_image);
134         if (!local_image)
135                 goto no_load;
136
137         local_initrd = NULL;
138         if (initrd) {
139                 local_initrd = load_file(NULL, initrd, &clean_initrd);
140                 if (!local_initrd)
141                         goto no_load;
142         }
143
144         result = kexec_load(local_image, local_initrd, args, dry_run);
145
146 no_load:
147         if (clean_image)
148                 unlink(local_image);
149         if (clean_initrd)
150                 unlink(local_initrd);
151
152         talloc_free(local_image);
153         talloc_free(local_initrd);
154
155         if (!result)
156                 result = kexec_reboot(dry_run);
157
158         return result;
159 }