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