]> git.ozlabs.org Git - petitboot/blob - discover/boot.c
config: Split interface configuration from network configuration
[petitboot] / discover / boot.c
1
2 #define _GNU_SOURCE
3
4 #include <stdbool.h>
5 #include <stdlib.h>
6 #include <assert.h>
7 #include <dirent.h>
8 #include <string.h>
9 #include <fcntl.h>
10 #include <sys/types.h>
11
12 #include <log/log.h>
13 #include <pb-protocol/pb-protocol.h>
14 #include <system/system.h>
15 #include <talloc/talloc.h>
16 #include <url/url.h>
17
18 #include "device-handler.h"
19 #include "boot.h"
20 #include "paths.h"
21 #include "resource.h"
22
23 static const char *boot_hook_dir = PKG_SYSCONF_DIR "/boot.d";
24 enum {
25         BOOT_HOOK_EXIT_OK       = 0,
26         BOOT_HOOK_EXIT_UPDATE   = 2,
27 };
28
29
30 struct boot_task {
31         char *local_image;
32         char *local_initrd;
33         char *local_dtb;
34         char *args;
35
36         bool dry_run;
37 };
38
39 /**
40  * kexec_load - kexec load helper.
41  */
42 static int kexec_load(struct boot_task *boot_task)
43 {
44         int result;
45         const char *argv[7];
46         const char **p;
47         char *s_initrd = NULL;
48         char *s_dtb = NULL;
49         char *s_args = NULL;
50
51         p = argv;
52         *p++ = pb_system_apps.kexec;    /* 1 */
53         *p++ = "-l";                    /* 2 */
54
55         if (boot_task->local_initrd) {
56                 s_initrd = talloc_asprintf(NULL, "--initrd=%s",
57                                 boot_task->local_initrd);
58                 assert(s_initrd);
59                 *p++ = s_initrd;         /* 3 */
60         }
61
62         if (boot_task->local_dtb) {
63                 s_dtb = talloc_asprintf(NULL, "--dtb=%s", boot_task->local_dtb);
64                 assert(s_dtb);
65                 *p++ = s_dtb;            /* 4 */
66         }
67
68         if (boot_task->args) {
69                 s_args = talloc_asprintf(NULL, "--append=%s", boot_task->args);
70                 assert(s_args);
71                 *p++ = s_args;          /* 5 */
72         }
73
74         *p++ = boot_task->local_image;  /* 6 */
75         *p++ = NULL;                    /* 7 */
76
77         result = pb_run_cmd(argv, 1, boot_task->dry_run);
78
79         if (result)
80                 pb_log("%s: failed: (%d)\n", __func__, result);
81
82         talloc_free(s_initrd);
83         talloc_free(s_dtb);
84         talloc_free(s_args);
85
86         return result;
87 }
88
89 /**
90  * kexec_reboot - Helper to boot the new kernel.
91  *
92  * Must only be called after a successful call to kexec_load().
93  */
94
95 static int kexec_reboot(bool dry_run)
96 {
97         int result = 0;
98         const char *argv[4];
99         const char **p;
100
101         /* First try running shutdown.  Init scripts should run 'exec -e' */
102
103         p = argv;
104         *p++ = pb_system_apps.shutdown; /* 1 */
105         *p++ =  "-r";                   /* 2 */
106         *p++ =  "now";                  /* 3 */
107         *p++ =  NULL;                   /* 4 */
108
109         result = pb_run_cmd(argv, 1, dry_run);
110
111         /* On error, force a kexec with the -e option */
112
113         if (result) {
114                 p = argv;
115                 *p++ = pb_system_apps.kexec;    /* 1 */
116                 *p++ = "-e";                    /* 2 */
117                 *p++ = NULL;                    /* 3 */
118
119                 result = pb_run_cmd(argv, 1, 0);
120         }
121
122         if (result)
123                 pb_log("%s: failed: (%d)\n", __func__, result);
124
125         /* okay, kexec -e -f */
126         if (result) {
127                 p = argv;
128                 *p++ = pb_system_apps.kexec;    /* 1 */
129                 *p++ = "-e";                    /* 2 */
130                 *p++ = "-f";                    /* 3 */
131                 *p++ = NULL;                    /* 4 */
132
133                 result = pb_run_cmd(argv, 1, 0);
134         }
135
136         if (result)
137                 pb_log("%s: failed: (%d)\n", __func__, result);
138
139
140         return result;
141 }
142
143 static void update_status(boot_status_fn fn, void *arg, int type,
144                 char *message)
145 {
146         struct boot_status status;
147
148         status.type = type;
149         status.message = message;
150         status.progress = -1;
151         status.detail = NULL;
152
153         fn(arg, &status);
154 }
155
156 static void boot_hook_update_param(void *ctx, struct boot_task *task,
157                 const char *name, const char *value)
158 {
159         struct p {
160                 const char *name;
161                 char **p;
162         } *param, params[] = {
163                 { "boot_image",         &task->local_image },
164                 { "boot_initrd",        &task->local_initrd },
165                 { "boot_dtb",           &task->local_dtb },
166                 { "boot_args",          &task->args },
167                 { NULL, NULL },
168         };
169
170         for (param = params; param->name; param++) {
171                 if (strcmp(param->name, name))
172                         continue;
173
174                 *param->p = talloc_strdup(ctx, value);
175                 return;
176         }
177 }
178
179 static void boot_hook_update(void *ctx, const char *hookname,
180                 struct boot_task *task, char *buf)
181 {
182         char *line, *name, *val, *sep;
183         char *saveptr;
184
185         for (;; buf = NULL) {
186
187                 line = strtok_r(buf, "\n", &saveptr);
188                 if (!line)
189                         break;
190
191                 sep = strchr(line, '=');
192                 if (!sep)
193                         continue;
194
195                 *sep = '\0';
196                 name = line;
197                 val = sep + 1;
198
199                 boot_hook_update_param(ctx, task, name, val);
200
201                 pb_log("boot hook %s specified %s=%s\n",
202                                 hookname, name, val);
203         }
204 }
205
206 static void boot_hook_setenv(struct boot_task *task)
207 {
208         unsetenv("boot_image");
209         unsetenv("boot_initrd");
210         unsetenv("boot_dtb");
211         unsetenv("boot_args");
212
213         setenv("boot_image", task->local_image, 1);
214         if (task->local_initrd)
215                 setenv("boot_initrd", task->local_initrd, 1);
216         if (task->local_dtb)
217                 setenv("boot_dtb", task->local_dtb, 1);
218         if (task->args)
219                 setenv("boot_args", task->args, 1);
220 }
221
222 static int hook_filter(const struct dirent *dirent)
223 {
224         return dirent->d_type == DT_REG || dirent->d_type == DT_LNK;
225 }
226
227 static int hook_cmp(const struct dirent **a, const struct dirent **b)
228 {
229         return strcmp((*a)->d_name, (*b)->d_name);
230 }
231
232 static void run_boot_hooks(void *ctx, struct boot_task *task,
233                 boot_status_fn status_fn, void *status_arg)
234 {
235         struct dirent **hooks;
236         int i, n;
237
238         n = scandir(boot_hook_dir, &hooks, hook_filter, hook_cmp);
239         if (n < 1)
240                 return;
241
242         update_status(status_fn, status_arg, BOOT_STATUS_INFO,
243                         "running boot hooks");
244
245         boot_hook_setenv(task);
246
247         for (i = 0; i < n; i++) {
248                 const char *argv[2] = { NULL, NULL };
249                 char *path, *buf;
250                 int buf_len, rc;
251
252                 path = join_paths(ctx, boot_hook_dir, hooks[i]->d_name);
253
254                 if (access(path, X_OK))
255                         continue;
256
257                 pb_log("running boot hook %s\n", hooks[i]->d_name);
258
259                 argv[0] = path;
260                 rc = pb_run_cmd_pipe(argv, 1, task->dry_run, ctx,
261                                 &buf, &buf_len);
262
263                 /* if the hook returned with BOOT_HOOK_EXIT_UPDATE,
264                  * then we process stdout to look for updated params
265                  */
266                 if (rc == BOOT_HOOK_EXIT_UPDATE) {
267                         boot_hook_update(ctx, hooks[i]->d_name, task, buf);
268                         boot_hook_setenv(task);
269                 }
270
271                 talloc_free(buf);
272                 talloc_free(path);
273         }
274
275         free(hooks);
276 }
277
278 int boot(void *ctx, struct discover_boot_option *opt, struct boot_command *cmd,
279                 int dry_run, boot_status_fn status_fn, void *status_arg)
280 {
281         struct boot_task boot_task;
282         struct pb_url *image, *initrd, *dtb;
283         unsigned int clean_image = 0;
284         unsigned int clean_initrd = 0;
285         unsigned int clean_dtb = 0;
286         int result;
287
288         image = NULL;
289         initrd = NULL;
290         dtb = NULL;
291         boot_task.dry_run = dry_run;
292
293         if (cmd && cmd->boot_image_file) {
294                 image = pb_url_parse(opt, cmd->boot_image_file);
295         } else if (opt && opt->boot_image) {
296                 image = opt->boot_image->url;
297         } else {
298                 pb_log("%s: no image specified", __func__);
299                 return -1;
300         }
301
302         if (cmd && cmd->initrd_file) {
303                 initrd = pb_url_parse(opt, cmd->initrd_file);
304         } else if (opt && opt->initrd) {
305                 initrd = opt->initrd->url;
306         }
307
308         if (cmd && cmd->dtb_file) {
309                 dtb = pb_url_parse(opt, cmd->dtb_file);
310         } else if (opt && opt->dtb) {
311                 dtb = opt->dtb->url;
312         }
313
314         if (cmd && cmd->boot_args) {
315                 boot_task.args = talloc_strdup(ctx, cmd->boot_args);
316         } else if (opt && opt->option->boot_args) {
317                 boot_task.args = talloc_strdup(ctx, opt->option->boot_args);
318         } else {
319                 boot_task.args = NULL;
320         }
321
322         result = -1;
323
324         boot_task.local_initrd = NULL;
325         boot_task.local_dtb = NULL;
326
327         update_status(status_fn, status_arg, BOOT_STATUS_INFO,
328                         "loading kernel");
329         boot_task.local_image = load_url(NULL, image, &clean_image);
330         if (!boot_task.local_image) {
331                 update_status(status_fn, status_arg, BOOT_STATUS_ERROR,
332                                 "Couldn't load kernel image");
333                 goto no_load;
334         }
335
336         if (initrd) {
337                 update_status(status_fn, status_arg, BOOT_STATUS_INFO,
338                                 "loading initrd");
339                 boot_task.local_initrd = load_url(NULL, initrd, &clean_initrd);
340                 if (!boot_task.local_initrd) {
341                         update_status(status_fn, status_arg, BOOT_STATUS_ERROR,
342                                         "Couldn't load initrd image");
343                         goto no_load;
344                 }
345         }
346
347         if (dtb) {
348                 update_status(status_fn, status_arg, BOOT_STATUS_INFO,
349                                 "loading device tree");
350                 boot_task.local_dtb = load_url(NULL, dtb, &clean_dtb);
351                 if (!boot_task.local_dtb) {
352                         update_status(status_fn, status_arg, BOOT_STATUS_ERROR,
353                                         "Couldn't load device tree");
354                         goto no_load;
355                 }
356         }
357
358         run_boot_hooks(ctx, &boot_task, status_fn, status_arg);
359
360         update_status(status_fn, status_arg, BOOT_STATUS_INFO,
361                         "performing kexec_load");
362
363         result = kexec_load(&boot_task);
364
365         if (result) {
366                 update_status(status_fn, status_arg, BOOT_STATUS_ERROR,
367                                 "kexec load failed");
368         }
369
370 no_load:
371         if (clean_image)
372                 unlink(boot_task.local_image);
373         if (clean_initrd)
374                 unlink(boot_task.local_initrd);
375         if (clean_dtb)
376                 unlink(boot_task.local_dtb);
377
378         talloc_free(boot_task.local_image);
379         talloc_free(boot_task.local_initrd);
380         talloc_free(boot_task.local_dtb);
381
382         if (!result) {
383                 update_status(status_fn, status_arg, BOOT_STATUS_INFO,
384                                 "performing kexec reboot");
385
386                 result = kexec_reboot(boot_task.dry_run);
387
388                 if (result) {
389                         update_status(status_fn, status_arg, BOOT_STATUS_ERROR,
390                                         "kexec reboot failed");
391                 }
392         }
393
394         return result;
395 }