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