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