]> git.ozlabs.org Git - petitboot/blob - discover/boot.c
5ca69b9f135f3cdbc032ccd7168311685e3adf5a
[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 #include <url/url.h>
9
10 #include "device-handler.h"
11 #include "boot.h"
12 #include "paths.h"
13 #include "resource.h"
14
15 /**
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.
20  */
21
22 static int kexec_load(const char *l_image, const char *l_initrd,
23         const char *args, int dry_run)
24 {
25         int result;
26         const char *argv[6];
27         const char **p;
28         char *s_initrd = NULL;
29         char *s_args = NULL;
30
31         p = argv;
32         *p++ = pb_system_apps.kexec;    /* 1 */
33         *p++ = "-l";                    /* 2 */
34
35         if (l_initrd) {
36                 s_initrd = talloc_asprintf(NULL, "--initrd=%s", l_initrd);
37                 assert(s_initrd);
38                 *p++ = s_initrd;         /* 3 */
39         }
40
41         if (args) {
42                 s_args = talloc_asprintf(NULL, "--append=%s", args);
43                 assert(s_args);
44                 *p++ = s_args;          /* 4 */
45         }
46
47         *p++ = l_image;                 /* 5 */
48         *p++ = NULL;                    /* 6 */
49
50         result = pb_run_cmd(argv, 1, dry_run);
51
52         if (result)
53                 pb_log("%s: failed: (%d)\n", __func__, result);
54
55         talloc_free(s_initrd);
56         talloc_free(s_args);
57
58         return result;
59 }
60
61 /**
62  * kexec_reboot - Helper to boot the new kernel.
63  *
64  * Must only be called after a successful call to kexec_load().
65  */
66
67 static int kexec_reboot(int dry_run)
68 {
69         int result = 0;
70         const char *argv[4];
71         const char **p;
72
73         /* First try running shutdown.  Init scripts should run 'exec -e' */
74
75         p = argv;
76         *p++ = pb_system_apps.shutdown; /* 1 */
77         *p++ =  "-r";                   /* 2 */
78         *p++ =  "now";                  /* 3 */
79         *p++ =  NULL;                   /* 4 */
80
81         result = pb_run_cmd(argv, 1, dry_run);
82
83         /* On error, force a kexec with the -e option */
84
85         if (result) {
86                 p = argv;
87                 *p++ = pb_system_apps.kexec;    /* 1 */
88                 *p++ = "-e";                    /* 2 */
89                 *p++ = NULL;                    /* 3 */
90
91                 result = pb_run_cmd(argv, 1, 0);
92         }
93
94         if (result)
95                 pb_log("%s: failed: (%d)\n", __func__, result);
96
97         /* okay, kexec -e -f */
98         if (result) {
99                 p = argv;
100                 *p++ = pb_system_apps.kexec;    /* 1 */
101                 *p++ = "-e";                    /* 2 */
102                 *p++ = "-f";                    /* 3 */
103                 *p++ = NULL;                    /* 4 */
104
105                 result = pb_run_cmd(argv, 1, 0);
106         }
107
108         if (result)
109                 pb_log("%s: failed: (%d)\n", __func__, result);
110
111
112         return result;
113 }
114
115 static void update_status(boot_status_fn fn, void *arg, int type,
116                 char *message)
117 {
118         struct boot_status status;
119
120         status.type = type;
121         status.message = message;
122         status.progress = -1;
123         status.detail = NULL;
124
125         fn(arg, &status);
126 }
127
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)
130 {
131         char *local_image, *local_initrd;
132         unsigned int clean_image = 0;
133         unsigned int clean_initrd = 0;
134         struct pb_url *image, *initrd;
135         char *args;
136         int result;
137
138         local_initrd = NULL;
139         image = NULL;
140         initrd = NULL;
141         args = NULL;
142
143         if (cmd && 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;
147         } else {
148                 pb_log("%s: no image specified", __func__);
149                 return -1;
150         }
151
152         if (cmd && cmd->initrd_file) {
153                 initrd = pb_url_parse(opt, cmd->initrd_file);
154         } else if (opt && opt->initrd) {
155                 initrd = opt->initrd->url;
156         }
157
158         if (cmd && 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);
162         }
163
164         result = -1;
165
166         update_status(status_fn, status_arg, BOOT_STATUS_INFO,
167                         "loading kernel");
168         local_image = load_url(NULL, image, &clean_image);
169         if (!local_image) {
170                 update_status(status_fn, status_arg, BOOT_STATUS_ERROR,
171                                 "Couldn't load kernel image");
172                 goto no_load;
173         }
174
175         if (initrd) {
176                 update_status(status_fn, status_arg, BOOT_STATUS_INFO,
177                                 "loading initrd");
178                 local_initrd = load_url(NULL, initrd, &clean_initrd);
179                 if (!local_initrd) {
180                         update_status(status_fn, status_arg, BOOT_STATUS_ERROR,
181                                         "Couldn't load initrd image");
182                         goto no_load;
183                 }
184         }
185
186         update_status(status_fn, status_arg, BOOT_STATUS_INFO,
187                         "performing kexec_load");
188
189         result = kexec_load(local_image, local_initrd, args, dry_run);
190
191         if (result) {
192                 update_status(status_fn, status_arg, BOOT_STATUS_ERROR,
193                                 "kexec load failed");
194         }
195
196 no_load:
197         if (clean_image)
198                 unlink(local_image);
199         if (clean_initrd)
200                 unlink(local_initrd);
201
202         talloc_free(local_image);
203         talloc_free(local_initrd);
204
205         if (!result) {
206                 update_status(status_fn, status_arg, BOOT_STATUS_INFO,
207                                 "performing kexec reboot");
208
209                 result = kexec_reboot(dry_run);
210
211                 if (result) {
212                         update_status(status_fn, status_arg, BOOT_STATUS_ERROR,
213                                         "kexec reboot failed");
214                 }
215         }
216
217         return result;
218 }