]> git.ozlabs.org Git - petitboot/blob - discover/boot.c
056d02de6869c681aa1d000edad0a8e9e9ef6b5c
[petitboot] / discover / boot.c
1
2 #if defined(HAVE_CONFIG_H)
3 #include "config.h"
4 #endif
5
6 #include <stdbool.h>
7 #include <stdlib.h>
8 #include <assert.h>
9 #include <dirent.h>
10 #include <string.h>
11 #include <fcntl.h>
12 #include <sys/types.h>
13
14 #include <log/log.h>
15 #include <pb-protocol/pb-protocol.h>
16 #include <process/process.h>
17 #include <system/system.h>
18 #include <talloc/talloc.h>
19 #include <url/url.h>
20 #include <util/util.h>
21 #include <i18n/i18n.h>
22
23 #include "device-handler.h"
24 #include "boot.h"
25 #include "paths.h"
26 #include "resource.h"
27 #include "platform.h"
28
29 #include <security/gpg.h>
30
31 static const char *boot_hook_dir = PKG_SYSCONF_DIR "/boot.d";
32 enum {
33         BOOT_HOOK_EXIT_OK       = 0,
34         BOOT_HOOK_EXIT_UPDATE   = 2,
35 };
36
37 /**
38  * kexec_load - kexec load helper.
39  */
40 static int kexec_load(struct boot_task *boot_task)
41 {
42         int result;
43         const char *argv[7];
44         const char **p;
45         char *s_initrd = NULL;
46         char *s_dtb = NULL;
47         char *s_args = NULL;
48
49         boot_task->local_initrd_override = NULL;
50         boot_task->local_dtb_override = NULL;
51         boot_task->local_image_override = NULL;
52
53         if ((result = gpg_validate_boot_files(boot_task))) {
54                 if (result == KEXEC_LOAD_DECRYPTION_FALURE) {
55                         pb_log("%s: Aborting kexec due to"
56                                 " decryption failure\n", __func__);
57                         goto abort_kexec;
58                 }
59                 if (result == KEXEC_LOAD_SIGNATURE_FAILURE) {
60                         pb_log("%s: Aborting kexec due to signature"
61                                 " verification failure\n", __func__);
62                         goto abort_kexec;
63                 }
64         }
65
66         const char* local_initrd = (boot_task->local_initrd_override) ?
67                 boot_task->local_initrd_override : boot_task->local_initrd;
68         const char* local_dtb = (boot_task->local_dtb_override) ?
69                 boot_task->local_dtb_override : boot_task->local_dtb;
70         const char* local_image = (boot_task->local_image_override) ?
71                 boot_task->local_image_override : boot_task->local_image;
72
73         p = argv;
74         *p++ = pb_system_apps.kexec;    /* 1 */
75         *p++ = "-l";                    /* 2 */
76
77         if (local_initrd) {
78                 s_initrd = talloc_asprintf(boot_task, "--initrd=%s",
79                                 local_initrd);
80                 assert(s_initrd);
81                 *p++ = s_initrd;         /* 3 */
82         }
83
84         if (local_dtb) {
85                 s_dtb = talloc_asprintf(boot_task, "--dtb=%s",
86                                                 local_dtb);
87                 assert(s_dtb);
88                 *p++ = s_dtb;            /* 4 */
89         }
90
91         s_args = talloc_asprintf(boot_task, "--append=%s",
92                                 boot_task->args ?: "\"\"");
93         assert(s_args);
94         *p++ = s_args;          /* 5 */
95
96         *p++ = local_image;             /* 6 */
97         *p++ = NULL;                    /* 7 */
98
99         result = process_run_simple_argv(boot_task, argv);
100
101         if (result)
102                 pb_log("%s: failed: (%d)\n", __func__, result);
103
104 abort_kexec:
105         gpg_validate_boot_files_cleanup(boot_task);
106
107         return result;
108 }
109
110 /**
111  * kexec_reboot - Helper to boot the new kernel.
112  *
113  * Must only be called after a successful call to kexec_load().
114  */
115
116 static int kexec_reboot(struct boot_task *task)
117 {
118         int result;
119
120         /* First try running shutdown.  Init scripts should run 'exec -e' */
121         result = process_run_simple(task, pb_system_apps.shutdown, "-r",
122                         "now", NULL);
123
124         /* On error, force a kexec with the -e option */
125         if (result) {
126                 result = process_run_simple(task, pb_system_apps.kexec,
127                                                 "-e", NULL);
128         }
129
130         if (result)
131                 pb_log("%s: failed: (%d)\n", __func__, result);
132
133         /* okay, kexec -e -f */
134         if (result) {
135                 result = process_run_simple(task, pb_system_apps.kexec,
136                                                 "-e", "-f", NULL);
137         }
138
139         if (result)
140                 pb_log("%s: failed: (%d)\n", __func__, result);
141
142
143         return result;
144 }
145
146 static void __attribute__((format(__printf__, 4, 5))) update_status(
147                 boot_status_fn fn, void *arg, int type, char *fmt, ...)
148 {
149         struct status status;
150         va_list ap;
151
152         va_start(ap, fmt);
153         status.message = talloc_vasprintf(NULL, fmt, ap);
154         va_end(ap);
155
156         status.type = type;
157
158         pb_debug("boot status: [%d] %s\n", type, status.message);
159
160         fn(arg, &status);
161
162         talloc_free(status.message);
163 }
164
165 static void boot_hook_update_param(void *ctx, struct boot_task *task,
166                 const char *name, const char *value)
167 {
168         struct p {
169                 const char *name;
170                 const char **p;
171         } *param, params[] = {
172                 { "boot_image",         &task->local_image },
173                 { "boot_initrd",        &task->local_initrd },
174                 { "boot_dtb",           &task->local_dtb },
175                 { "boot_args",          &task->args },
176                 { NULL, NULL },
177         };
178
179         for (param = params; param->name; param++) {
180                 if (strcmp(param->name, name))
181                         continue;
182
183                 *param->p = talloc_strdup(ctx, value);
184                 return;
185         }
186 }
187
188 static void boot_hook_update(struct boot_task *task, const char *hookname,
189                 char *buf)
190 {
191         char *line, *name, *val, *sep;
192         char *saveptr = NULL;
193
194         for (;; buf = NULL) {
195
196                 line = strtok_r(buf, "\n", &saveptr);
197                 if (!line)
198                         break;
199
200                 sep = strchr(line, '=');
201                 if (!sep)
202                         continue;
203
204                 *sep = '\0';
205                 name = line;
206                 val = sep + 1;
207
208                 boot_hook_update_param(task, task, name, val);
209
210                 pb_log("boot hook %s specified %s=%s\n",
211                                 hookname, name, val);
212         }
213 }
214
215 static void boot_hook_setenv(struct boot_task *task)
216 {
217         unsetenv("boot_image");
218         unsetenv("boot_initrd");
219         unsetenv("boot_dtb");
220         unsetenv("boot_args");
221         unsetenv("boot_console");
222
223         setenv("boot_image", task->local_image, 1);
224         if (task->local_initrd)
225                 setenv("boot_initrd", task->local_initrd, 1);
226         if (task->local_dtb)
227                 setenv("boot_dtb", task->local_dtb, 1);
228         if (task->args)
229                 setenv("boot_args", task->args, 1);
230         if (task->boot_console)
231                 setenv("boot_console", task->boot_console, 1);
232 }
233
234 static int hook_filter(const struct dirent *dirent)
235 {
236         return dirent->d_type == DT_REG || dirent->d_type == DT_LNK;
237 }
238
239 static int hook_cmp(const struct dirent **a, const struct dirent **b)
240 {
241         return strcmp((*a)->d_name, (*b)->d_name);
242 }
243
244 static void run_boot_hooks(struct boot_task *task)
245 {
246         struct dirent **hooks;
247         int i, n;
248
249         n = scandir(boot_hook_dir, &hooks, hook_filter, hook_cmp);
250         if (n < 1)
251                 return;
252
253         update_status(task->status_fn, task->status_arg, STATUS_INFO,
254                         _("running boot hooks"));
255
256         boot_hook_setenv(task);
257
258         for (i = 0; i < n; i++) {
259                 const char *argv[2] = { NULL, NULL };
260                 struct process *process;
261                 char *path;
262                 int rc;
263
264                 path = join_paths(task, boot_hook_dir, hooks[i]->d_name);
265
266                 if (access(path, X_OK)) {
267                         talloc_free(path);
268                         continue;
269                 }
270
271                 process = process_create(task);
272
273                 argv[0] = path;
274                 process->path = path;
275                 process->argv = argv;
276                 process->keep_stdout = true;
277
278                 pb_log("running boot hook %s\n", hooks[i]->d_name);
279
280                 rc = process_run_sync(process);
281                 if (rc) {
282                         pb_log("boot hook exec failed!\n");
283
284                 } else if (WIFEXITED(process->exit_status) &&
285                            WEXITSTATUS(process->exit_status)
286                                 == BOOT_HOOK_EXIT_UPDATE) {
287                         /* if the hook returned with BOOT_HOOK_EXIT_UPDATE,
288                          * then we process stdout to look for updated params
289                          */
290                         boot_hook_update(task, hooks[i]->d_name,
291                                         process->stdout_buf);
292                         boot_hook_setenv(task);
293                 }
294
295                 process_release(process);
296                 talloc_free(path);
297         }
298
299         free(hooks);
300 }
301
302 static bool load_pending(struct load_url_result *result)
303 {
304         return result && result->status == LOAD_ASYNC;
305 }
306
307 static int check_load(struct boot_task *task, const char *name,
308                 struct load_url_result *result)
309 {
310         if (!result)
311                 return 0;
312         if (result->status != LOAD_ERROR)
313                 return 0;
314
315         update_status(task->status_fn, task->status_arg,
316                         STATUS_ERROR,
317                         _("Couldn't load %s"), name);
318         return -1;
319 }
320
321 static void cleanup_load(struct load_url_result *result)
322 {
323         if (!result)
324                 return;
325         if (result->status != LOAD_OK)
326                 return;
327         if (!result->cleanup_local)
328                 return;
329         unlink(result->local);
330 }
331
332 static void cleanup_cancellations(struct boot_task *task,
333                 struct load_url_result *cur_result)
334 {
335         struct load_url_result *result, **results[] = {
336                 &task->image, &task->initrd, &task->dtb,
337         };
338         bool pending = false;
339         unsigned int i;
340
341         for (i = 0; i < ARRAY_SIZE(results); i++) {
342                 result = *results[i];
343
344                 if (!result)
345                         continue;
346
347                 /* We need to cleanup and free any completed loads */
348                 if (result == cur_result || result->status == LOAD_OK
349                                 || result->status == LOAD_ERROR) {
350                         cleanup_load(result);
351                         talloc_free(result);
352                         *results[i] = NULL;
353
354                 /* ... and cancel any pending loads, which we'll free in
355                  * the completion callback */
356                 } else if (result->status == LOAD_ASYNC) {
357                         load_url_async_cancel(result);
358                         pending = true;
359
360                 /* if we're waiting for a cancellation, we still need to
361                  * wait for the completion before freeing the boot task */
362                 } else if (result->status == LOAD_CANCELLED) {
363                         pending = true;
364                 }
365         }
366
367         if (!pending)
368                 talloc_free(task);
369 }
370
371 static void boot_process(struct load_url_result *result, void *data)
372 {
373         struct boot_task *task = data;
374         int rc = -1;
375
376         if (task->cancelled) {
377                 cleanup_cancellations(task, result);
378                 return;
379         }
380
381         if (load_pending(task->image) ||
382                         load_pending(task->initrd) ||
383                         load_pending(task->dtb))
384                 return;
385
386         if (check_load(task, "kernel image", task->image) ||
387                         check_load(task, "initrd", task->initrd) ||
388                         check_load(task, "dtb", task->dtb))
389                 goto no_load;
390
391         if (task->verify_signature) {
392                 if (load_pending(task->image_signature) ||
393                                 load_pending(task->initrd_signature) ||
394                                 load_pending(task->dtb_signature) ||
395                                 load_pending(task->cmdline_signature))
396                         return;
397         }
398         if (task->decrypt_files) {
399                 if (load_pending(task->cmdline_signature))
400                         return;
401         }
402
403         if (task->verify_signature) {
404                 if (check_load(task, "kernel image signature",
405                                         task->image_signature) ||
406                                 check_load(task, "initrd signature",
407                                         task->initrd_signature) ||
408                                 check_load(task, "dtb signature",
409                                         task->dtb_signature) ||
410                                 check_load(task, "command line signature",
411                                         task->cmdline_signature))
412                         goto no_sig_load;
413         }
414         if (task->decrypt_files) {
415                 if (load_pending(task->cmdline_signature))
416                         return;
417
418                 if (check_load(task, "command line signature",
419                                         task->cmdline_signature))
420                         goto no_decrypt_sig_load;
421         }
422
423         /* we make a copy of the local paths, as the boot hooks might update
424          * and/or create these */
425         task->local_image = task->image ? task->image->local : NULL;
426         task->local_initrd = task->initrd ? task->initrd->local : NULL;
427         task->local_dtb = task->dtb ? task->dtb->local : NULL;
428
429         if (task->verify_signature) {
430                 task->local_image_signature = task->image_signature ?
431                         task->image_signature->local : NULL;
432                 task->local_initrd_signature = task->initrd_signature ?
433                         task->initrd_signature->local : NULL;
434                 task->local_dtb_signature = task->dtb_signature ?
435                         task->dtb_signature->local : NULL;
436         }
437         if (task->verify_signature || task->decrypt_files) {
438                 task->local_cmdline_signature = task->cmdline_signature ?
439                         task->cmdline_signature->local : NULL;
440         }
441
442         run_boot_hooks(task);
443
444         update_status(task->status_fn, task->status_arg, STATUS_INFO,
445                         _("performing kexec_load"));
446
447         rc = kexec_load(task);
448         if (rc == KEXEC_LOAD_DECRYPTION_FALURE) {
449                 update_status(task->status_fn, task->status_arg,
450                                 STATUS_ERROR, _("decryption failed"));
451         }
452         else if (rc == KEXEC_LOAD_SIGNATURE_FAILURE) {
453                 update_status(task->status_fn, task->status_arg,
454                                 STATUS_ERROR,
455                                 _("signature verification failed"));
456         }
457         else if (rc == KEXEC_LOAD_SIG_SETUP_INVALID) {
458                 update_status(task->status_fn, task->status_arg,
459                                 STATUS_ERROR,
460                                 _("invalid signature configuration"));
461         }
462         else if (rc) {
463                 update_status(task->status_fn, task->status_arg,
464                                 STATUS_ERROR, _("kexec load failed"));
465         }
466
467 no_sig_load:
468         cleanup_load(task->image_signature);
469         cleanup_load(task->initrd_signature);
470         cleanup_load(task->dtb_signature);
471
472 no_decrypt_sig_load:
473         cleanup_load(task->cmdline_signature);
474
475 no_load:
476         cleanup_load(task->image);
477         cleanup_load(task->initrd);
478         cleanup_load(task->dtb);
479
480         if (!rc) {
481                 update_status(task->status_fn, task->status_arg,
482                                 STATUS_INFO, _("performing kexec reboot"));
483
484                 rc = kexec_reboot(task);
485                 if (rc) {
486                         update_status(task->status_fn, task->status_arg,
487                                         STATUS_ERROR,
488                                         _("kexec reboot failed"));
489                 }
490         }
491 }
492
493 static int start_url_load(struct boot_task *task, const char *name,
494                 struct pb_url *url, struct load_url_result **result)
495 {
496         if (!url)
497                 return 0;
498
499         *result = load_url_async(task, url, boot_process, task);
500         if (!*result) {
501                 update_status(task->status_fn, task->status_arg,
502                                 STATUS_ERROR, _("Error loading %s"), name);
503                 return -1;
504         }
505         return 0;
506 }
507
508 struct boot_task *boot(void *ctx, struct discover_boot_option *opt,
509                 struct boot_command *cmd, int dry_run,
510                 boot_status_fn status_fn, void *status_arg)
511 {
512         struct pb_url *image = NULL, *initrd = NULL, *dtb = NULL;
513         struct pb_url *image_sig = NULL, *initrd_sig = NULL, *dtb_sig = NULL,
514                 *cmdline_sig = NULL;
515         const struct config *config = config_get();
516         struct boot_task *boot_task;
517         const char *boot_desc;
518         int rc;
519         int lockdown_type;
520
521         if (opt && opt->option->name)
522                 boot_desc = opt->option->name;
523         else if (cmd && cmd->boot_image_file)
524                 boot_desc = cmd->boot_image_file;
525         else
526                 boot_desc = _("(unknown)");
527
528         update_status(status_fn, status_arg, STATUS_INFO,
529                         _("Booting %s."), boot_desc);
530
531         if (cmd && cmd->boot_image_file) {
532                 image = pb_url_parse(opt, cmd->boot_image_file);
533         } else if (opt && opt->boot_image) {
534                 image = opt->boot_image->url;
535         } else {
536                 pb_log("%s: no image specified\n", __func__);
537                 update_status(status_fn, status_arg, STATUS_INFO,
538                                 _("Boot failed: no image specified"));
539                 return NULL;
540         }
541
542         if (cmd && cmd->initrd_file) {
543                 initrd = pb_url_parse(opt, cmd->initrd_file);
544         } else if (opt && opt->initrd) {
545                 initrd = opt->initrd->url;
546         }
547
548         if (cmd && cmd->dtb_file) {
549                 dtb = pb_url_parse(opt, cmd->dtb_file);
550         } else if (opt && opt->dtb) {
551                 dtb = opt->dtb->url;
552         }
553
554         boot_task = talloc_zero(ctx, struct boot_task);
555         boot_task->dry_run = dry_run;
556         boot_task->status_fn = status_fn;
557         boot_task->status_arg = status_arg;
558
559         lockdown_type = lockdown_status();
560         boot_task->verify_signature = (lockdown_type == PB_LOCKDOWN_SIGN);
561         boot_task->decrypt_files = (lockdown_type == PB_LOCKDOWN_DECRYPT);
562
563         if (cmd && cmd->boot_args) {
564                 boot_task->args = talloc_strdup(boot_task, cmd->boot_args);
565         } else if (opt && opt->option->boot_args) {
566                 boot_task->args = talloc_strdup(boot_task,
567                                                 opt->option->boot_args);
568         } else {
569                 boot_task->args = NULL;
570         }
571
572         if (cmd && cmd->console && !config->manual_console)
573                 boot_task->boot_console = talloc_strdup(boot_task, cmd->console);
574         else
575                 boot_task->boot_console = config ? config->boot_console : NULL;
576
577         if (boot_task->verify_signature || boot_task->decrypt_files) {
578                 if (cmd && cmd->args_sig_file) {
579                         cmdline_sig = pb_url_parse(opt, cmd->args_sig_file);
580                 } else if (opt && opt->args_sig_file) {
581                         cmdline_sig = opt->args_sig_file->url;
582                 } else {
583                         pb_log("%s: no command line signature file"
584                                 " specified\n", __func__);
585                         update_status(status_fn, status_arg, STATUS_INFO,
586                                         _("Boot failed: no command line"
587                                                 " signature file specified"));
588                         talloc_free(boot_task);
589                         return NULL;
590                 }
591         }
592
593         /* start async loads for boot resources */
594         rc = start_url_load(boot_task, "kernel image", image, &boot_task->image)
595           || start_url_load(boot_task, "initrd", initrd, &boot_task->initrd)
596           || start_url_load(boot_task, "dtb", dtb, &boot_task->dtb);
597
598         if (boot_task->verify_signature) {
599                 /* Generate names of associated signature files and load */
600                 if (image) {
601                         image_sig = gpg_get_signature_url(ctx, image);
602                         rc |= start_url_load(boot_task,
603                                 "kernel image signature", image_sig,
604                                 &boot_task->image_signature);
605                 }
606                 if (initrd) {
607                         initrd_sig = gpg_get_signature_url(ctx, initrd);
608                         rc |= start_url_load(boot_task, "initrd signature",
609                                 initrd_sig, &boot_task->initrd_signature);
610                 }
611                 if (dtb) {
612                         dtb_sig = gpg_get_signature_url(ctx, dtb);
613                         rc |= start_url_load(boot_task, "dtb signature",
614                                 dtb_sig, &boot_task->dtb_signature);
615                 }
616         }
617
618         if (boot_task->verify_signature || boot_task->decrypt_files) {
619                 rc |= start_url_load(boot_task,
620                         "kernel command line signature", cmdline_sig,
621                         &boot_task->cmdline_signature);
622         }
623
624         /* If all URLs are local, we may be done. */
625         if (rc) {
626                 /* Don't call boot_cancel() to preserve the status update */
627                 boot_task->cancelled = true;
628                 cleanup_cancellations(boot_task, NULL);
629                 return NULL;
630         }
631
632         boot_process(NULL, boot_task);
633
634         return boot_task;
635 }
636
637 void boot_cancel(struct boot_task *task)
638 {
639         task->cancelled = true;
640
641         update_status(task->status_fn, task->status_arg, STATUS_INFO,
642                         _("Boot cancelled"));
643
644         cleanup_cancellations(task, NULL);
645 }