ec6f6e579eb6cb31f8929c1ab140accc9be99b35
[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 struct boot_task {
31         struct load_url_result *image;
32         struct load_url_result *initrd;
33         struct load_url_result *dtb;
34         const char *args;
35         boot_status_fn status_fn;
36         void *status_arg;
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->initrd) {
57                 s_initrd = talloc_asprintf(boot_task, "--initrd=%s",
58                                 boot_task->initrd->local);
59                 assert(s_initrd);
60                 *p++ = s_initrd;         /* 3 */
61         }
62
63         if (boot_task->dtb) {
64                 s_dtb = talloc_asprintf(boot_task, "--dtb=%s",
65                                                 boot_task->dtb->local);
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->image->local; /* 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 __attribute__((format(__printf__, 4, 5))) update_status(
125                 boot_status_fn fn, void *arg, int type, char *fmt, ...)
126 {
127         struct boot_status status;
128         va_list ap;
129
130         va_start(ap, fmt);
131         status.message = talloc_vasprintf(NULL, fmt, ap);
132         va_end(ap);
133
134         status.type = type;
135         status.progress = -1;
136         status.detail = NULL;
137
138         pb_debug("boot status: [%d] %s\n", type, status.message);
139
140         fn(arg, &status);
141
142         talloc_free(status.message);
143 }
144
145 static void boot_hook_update_param(void *ctx, struct boot_task *task,
146                 const char *name, const char *value)
147 {
148         struct p {
149                 const char *name;
150                 const char **p;
151         } *param, params[] = {
152                 { "boot_image",         &task->image->local },
153                 { "boot_initrd",        &task->initrd->local },
154                 { "boot_dtb",           &task->dtb->local },
155                 { "boot_args",          &task->args },
156                 { NULL, NULL },
157         };
158
159         for (param = params; param->name; param++) {
160                 if (strcmp(param->name, name))
161                         continue;
162
163                 *param->p = talloc_strdup(ctx, value);
164                 return;
165         }
166 }
167
168 static void boot_hook_update(struct boot_task *task, const char *hookname,
169                 char *buf)
170 {
171         char *line, *name, *val, *sep;
172         char *saveptr;
173
174         for (;; buf = NULL) {
175
176                 line = strtok_r(buf, "\n", &saveptr);
177                 if (!line)
178                         break;
179
180                 sep = strchr(line, '=');
181                 if (!sep)
182                         continue;
183
184                 *sep = '\0';
185                 name = line;
186                 val = sep + 1;
187
188                 boot_hook_update_param(task, task, name, val);
189
190                 pb_log("boot hook %s specified %s=%s\n",
191                                 hookname, name, val);
192         }
193 }
194
195 static void boot_hook_setenv(struct boot_task *task)
196 {
197         unsetenv("boot_image");
198         unsetenv("boot_initrd");
199         unsetenv("boot_dtb");
200         unsetenv("boot_args");
201
202         setenv("boot_image", task->image->local, 1);
203         if (task->initrd)
204                 setenv("boot_initrd", task->initrd->local, 1);
205         if (task->dtb)
206                 setenv("boot_dtb", task->dtb->local, 1);
207         if (task->args)
208                 setenv("boot_args", task->args, 1);
209 }
210
211 static int hook_filter(const struct dirent *dirent)
212 {
213         return dirent->d_type == DT_REG || dirent->d_type == DT_LNK;
214 }
215
216 static int hook_cmp(const struct dirent **a, const struct dirent **b)
217 {
218         return strcmp((*a)->d_name, (*b)->d_name);
219 }
220
221 static void run_boot_hooks(struct boot_task *task)
222 {
223         struct dirent **hooks;
224         int i, n;
225
226         n = scandir(boot_hook_dir, &hooks, hook_filter, hook_cmp);
227         if (n < 1)
228                 return;
229
230         update_status(task->status_fn, task->status_arg, BOOT_STATUS_INFO,
231                         "running boot hooks");
232
233         boot_hook_setenv(task);
234
235         for (i = 0; i < n; i++) {
236                 const char *argv[2] = { NULL, NULL };
237                 struct process *process;
238                 char *path;
239                 int rc;
240
241                 path = join_paths(task, boot_hook_dir, hooks[i]->d_name);
242
243                 if (access(path, X_OK)) {
244                         talloc_free(path);
245                         continue;
246                 }
247
248                 process = process_create(task);
249
250                 argv[0] = path;
251                 process->path = path;
252                 process->argv = argv;
253                 process->keep_stdout = true;
254
255                 pb_log("running boot hook %s\n", hooks[i]->d_name);
256
257                 rc = process_run_sync(process);
258                 if (rc) {
259                         pb_log("boot hook exec failed!\n");
260
261                 } else if (WIFEXITED(process->exit_status) &&
262                            WEXITSTATUS(process->exit_status)
263                                 == BOOT_HOOK_EXIT_UPDATE) {
264                         /* if the hook returned with BOOT_HOOK_EXIT_UPDATE,
265                          * then we process stdout to look for updated params
266                          */
267                         if (rc == BOOT_HOOK_EXIT_UPDATE) {
268                                 boot_hook_update(task, hooks[i]->d_name,
269                                                 process->stdout_buf);
270                                 boot_hook_setenv(task);
271                         }
272                 }
273
274                 process_release(process);
275                 talloc_free(path);
276         }
277
278         free(hooks);
279 }
280
281 static bool load_pending(struct load_url_result *result)
282 {
283         return result && result->status == LOAD_ASYNC;
284 }
285
286 static int check_load(struct boot_task *task, const char *name,
287                 struct load_url_result *result)
288 {
289         if (!result)
290                 return 0;
291         if (result->status != LOAD_ERROR)
292                 return 0;
293
294         update_status(task->status_fn, task->status_arg,
295                         BOOT_STATUS_ERROR,
296                         "Couldn't load %s", name);
297         return -1;
298 }
299
300 static void cleanup_load(struct load_url_result *result)
301 {
302         if (!result)
303                 return;
304         if (result->status != LOAD_OK)
305                 return;
306         if (!result->cleanup_local)
307                 return;
308         unlink(result->local);
309 }
310
311 static void boot_process(struct load_url_result *result __attribute__((unused)),
312                 void *data)
313 {
314         struct boot_task *task = data;
315         int rc = -1;
316
317         if (load_pending(task->image) ||
318                         load_pending(task->initrd) ||
319                         load_pending(task->dtb))
320                 return;
321
322         if (check_load(task, "kernel image", task->image) ||
323                         check_load(task, "initrd", task->initrd) ||
324                         check_load(task, "dtb", task->dtb))
325                 goto no_load;
326
327         run_boot_hooks(task);
328
329         update_status(task->status_fn, task->status_arg, BOOT_STATUS_INFO,
330                         "performing kexec_load");
331
332         rc = kexec_load(task);
333         if (rc) {
334                 update_status(task->status_fn, task->status_arg,
335                                 BOOT_STATUS_ERROR, "kexec load failed");
336         }
337
338 no_load:
339         cleanup_load(task->image);
340         cleanup_load(task->initrd);
341         cleanup_load(task->dtb);
342
343         if (!rc) {
344                 update_status(task->status_fn, task->status_arg,
345                                 BOOT_STATUS_INFO,
346                                 "performing kexec reboot");
347
348                 rc = kexec_reboot(task);
349                 if (rc) {
350                         update_status(task->status_fn, task->status_arg,
351                                         BOOT_STATUS_ERROR,
352                                         "kexec reboot failed");
353                 }
354         }
355
356         talloc_free(task);
357 }
358
359 static int start_url_load(struct boot_task *task, const char *name,
360                 struct pb_url *url, struct load_url_result **result)
361 {
362         if (!url)
363                 return 0;
364
365         *result = load_url_async(task, url, boot_process, task);
366         if (!*result) {
367                 update_status(task->status_fn, task->status_arg,
368                                 BOOT_STATUS_ERROR,
369                                 "Error loading %s", name);
370                 return -1;
371         }
372         return 0;
373 }
374
375 int boot(void *ctx, struct discover_boot_option *opt, struct boot_command *cmd,
376                 int dry_run, boot_status_fn status_fn, void *status_arg)
377 {
378         struct pb_url *image = NULL, *initrd = NULL, *dtb = NULL;
379         struct boot_task *boot_task;
380         const char *boot_desc;
381         int rc;
382
383         if (opt && opt->option->name)
384                 boot_desc = opt->option->name;
385         else if (cmd && cmd->boot_image_file)
386                 boot_desc = cmd->boot_image_file;
387         else
388                 boot_desc = "(unknown)";
389
390         update_status(status_fn, status_arg, BOOT_STATUS_INFO,
391                         "Booting %s.", boot_desc);
392
393         if (cmd && cmd->boot_image_file) {
394                 image = pb_url_parse(opt, cmd->boot_image_file);
395         } else if (opt && opt->boot_image) {
396                 image = opt->boot_image->url;
397         } else {
398                 pb_log("%s: no image specified\n", __func__);
399                 update_status(status_fn, status_arg, BOOT_STATUS_INFO,
400                                 "Boot failed: no image specified");
401                 return -1;
402         }
403
404         if (cmd && cmd->initrd_file) {
405                 initrd = pb_url_parse(opt, cmd->initrd_file);
406         } else if (opt && opt->initrd) {
407                 initrd = opt->initrd->url;
408         }
409
410         if (cmd && cmd->dtb_file) {
411                 dtb = pb_url_parse(opt, cmd->dtb_file);
412         } else if (opt && opt->dtb) {
413                 dtb = opt->dtb->url;
414         }
415
416         boot_task = talloc_zero(ctx, struct boot_task);
417         boot_task->dry_run = dry_run;
418         boot_task->status_fn = status_fn;
419         boot_task->status_arg = status_arg;
420
421         if (cmd && cmd->boot_args) {
422                 boot_task->args = talloc_strdup(boot_task, cmd->boot_args);
423         } else if (opt && opt->option->boot_args) {
424                 boot_task->args = talloc_strdup(boot_task,
425                                                 opt->option->boot_args);
426         } else {
427                 boot_task->args = NULL;
428         }
429
430         /* start async loads for boot resources */
431         rc = start_url_load(boot_task, "kernel image", image, &boot_task->image)
432           || start_url_load(boot_task, "initrd", initrd, &boot_task->initrd)
433           || start_url_load(boot_task, "dtb", dtb, &boot_task->dtb);
434
435         /* If all URLs are local, we may be done. */
436         if (!rc)
437                 boot_process(NULL, boot_task);
438
439         return rc;
440 }