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