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