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