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