]> git.ozlabs.org Git - petitboot/blob - discover/boot.c
discover/boot: Put all boot params into a struct boot_task
[petitboot] / discover / boot.c
1
2 #include <stdbool.h>
3 #include <assert.h>
4
5 #include <log/log.h>
6 #include <pb-protocol/pb-protocol.h>
7 #include <system/system.h>
8 #include <talloc/talloc.h>
9 #include <url/url.h>
10
11 #include "device-handler.h"
12 #include "boot.h"
13 #include "paths.h"
14 #include "resource.h"
15
16 struct boot_task {
17         char *local_image;
18         char *local_initrd;
19         char *local_dtb;
20         const char *args;
21
22         bool dry_run;
23 };
24
25 /**
26  * kexec_load - kexec load helper.
27  */
28 static int kexec_load(struct boot_task *boot_task)
29 {
30         int result;
31         const char *argv[7];
32         const char **p;
33         char *s_initrd = NULL;
34         char *s_dtb = NULL;
35         char *s_args = NULL;
36
37         p = argv;
38         *p++ = pb_system_apps.kexec;    /* 1 */
39         *p++ = "-l";                    /* 2 */
40
41         if (boot_task->local_initrd) {
42                 s_initrd = talloc_asprintf(NULL, "--initrd=%s",
43                                 boot_task->local_initrd);
44                 assert(s_initrd);
45                 *p++ = s_initrd;         /* 3 */
46         }
47
48         if (boot_task->local_dtb) {
49                 s_dtb = talloc_asprintf(NULL, "--dtb=%s", boot_task->local_dtb);
50                 assert(s_dtb);
51                 *p++ = s_dtb;            /* 4 */
52         }
53
54         if (boot_task->args) {
55                 s_args = talloc_asprintf(NULL, "--append=%s", boot_task->args);
56                 assert(s_args);
57                 *p++ = s_args;          /* 5 */
58         }
59
60         *p++ = boot_task->local_image;  /* 6 */
61         *p++ = NULL;                    /* 7 */
62
63         result = pb_run_cmd(argv, 1, boot_task->dry_run);
64
65         if (result)
66                 pb_log("%s: failed: (%d)\n", __func__, result);
67
68         talloc_free(s_initrd);
69         talloc_free(s_dtb);
70         talloc_free(s_args);
71
72         return result;
73 }
74
75 /**
76  * kexec_reboot - Helper to boot the new kernel.
77  *
78  * Must only be called after a successful call to kexec_load().
79  */
80
81 static int kexec_reboot(bool dry_run)
82 {
83         int result = 0;
84         const char *argv[4];
85         const char **p;
86
87         /* First try running shutdown.  Init scripts should run 'exec -e' */
88
89         p = argv;
90         *p++ = pb_system_apps.shutdown; /* 1 */
91         *p++ =  "-r";                   /* 2 */
92         *p++ =  "now";                  /* 3 */
93         *p++ =  NULL;                   /* 4 */
94
95         result = pb_run_cmd(argv, 1, dry_run);
96
97         /* On error, force a kexec with the -e option */
98
99         if (result) {
100                 p = argv;
101                 *p++ = pb_system_apps.kexec;    /* 1 */
102                 *p++ = "-e";                    /* 2 */
103                 *p++ = NULL;                    /* 3 */
104
105                 result = pb_run_cmd(argv, 1, 0);
106         }
107
108         if (result)
109                 pb_log("%s: failed: (%d)\n", __func__, result);
110
111         /* okay, kexec -e -f */
112         if (result) {
113                 p = argv;
114                 *p++ = pb_system_apps.kexec;    /* 1 */
115                 *p++ = "-e";                    /* 2 */
116                 *p++ = "-f";                    /* 3 */
117                 *p++ = NULL;                    /* 4 */
118
119                 result = pb_run_cmd(argv, 1, 0);
120         }
121
122         if (result)
123                 pb_log("%s: failed: (%d)\n", __func__, result);
124
125
126         return result;
127 }
128
129 static void update_status(boot_status_fn fn, void *arg, int type,
130                 char *message)
131 {
132         struct boot_status status;
133
134         status.type = type;
135         status.message = message;
136         status.progress = -1;
137         status.detail = NULL;
138
139         fn(arg, &status);
140 }
141
142 int boot(void *ctx, struct discover_boot_option *opt, struct boot_command *cmd,
143                 int dry_run, boot_status_fn status_fn, void *status_arg)
144 {
145         struct boot_task boot_task;
146         struct pb_url *image, *initrd, *dtb;
147         unsigned int clean_image = 0;
148         unsigned int clean_initrd = 0;
149         unsigned int clean_dtb = 0;
150         int result;
151
152         image = NULL;
153         initrd = NULL;
154         dtb = NULL;
155         boot_task.dry_run = dry_run;
156
157         if (cmd && cmd->boot_image_file) {
158                 image = pb_url_parse(opt, cmd->boot_image_file);
159         } else if (opt && opt->boot_image) {
160                 image = opt->boot_image->url;
161         } else {
162                 pb_log("%s: no image specified", __func__);
163                 return -1;
164         }
165
166         if (cmd && cmd->initrd_file) {
167                 initrd = pb_url_parse(opt, cmd->initrd_file);
168         } else if (opt && opt->initrd) {
169                 initrd = opt->initrd->url;
170         }
171
172         if (cmd && cmd->dtb_file) {
173                 dtb = pb_url_parse(opt, cmd->dtb_file);
174         } else if (opt && opt->dtb) {
175                 dtb = opt->dtb->url;
176         }
177
178         if (cmd && cmd->boot_args) {
179                 boot_task.args = talloc_strdup(ctx, cmd->boot_args);
180         } else if (opt && opt->option->boot_args) {
181                 boot_task.args = talloc_strdup(ctx, opt->option->boot_args);
182         } else {
183                 boot_task.args = NULL;
184         }
185
186         result = -1;
187
188         update_status(status_fn, status_arg, BOOT_STATUS_INFO,
189                         "loading kernel");
190         boot_task.local_image = load_url(NULL, image, &clean_image);
191         if (!boot_task.local_image) {
192                 update_status(status_fn, status_arg, BOOT_STATUS_ERROR,
193                                 "Couldn't load kernel image");
194                 goto no_load;
195         }
196
197         boot_task.local_initrd = NULL;
198         if (initrd) {
199                 update_status(status_fn, status_arg, BOOT_STATUS_INFO,
200                                 "loading initrd");
201                 boot_task.local_initrd = load_url(NULL, initrd, &clean_initrd);
202                 if (!boot_task.local_initrd) {
203                         update_status(status_fn, status_arg, BOOT_STATUS_ERROR,
204                                         "Couldn't load initrd image");
205                         goto no_load;
206                 }
207         }
208
209         boot_task.local_dtb = NULL;
210         if (dtb) {
211                 update_status(status_fn, status_arg, BOOT_STATUS_INFO,
212                                 "loading device tree");
213                 boot_task.local_dtb = load_url(NULL, dtb, &clean_dtb);
214                 if (!boot_task.local_dtb) {
215                         update_status(status_fn, status_arg, BOOT_STATUS_ERROR,
216                                         "Couldn't load device tree");
217                         goto no_load;
218                 }
219         }
220
221         update_status(status_fn, status_arg, BOOT_STATUS_INFO,
222                         "performing kexec_load");
223
224         result = kexec_load(&boot_task);
225
226         if (result) {
227                 update_status(status_fn, status_arg, BOOT_STATUS_ERROR,
228                                 "kexec load failed");
229         }
230
231 no_load:
232         if (clean_image)
233                 unlink(boot_task.local_image);
234         if (clean_initrd)
235                 unlink(boot_task.local_initrd);
236         if (clean_dtb)
237                 unlink(boot_task.local_dtb);
238
239         talloc_free(boot_task.local_image);
240         talloc_free(boot_task.local_initrd);
241         talloc_free(boot_task.local_dtb);
242
243         if (!result) {
244                 update_status(status_fn, status_arg, BOOT_STATUS_INFO,
245                                 "performing kexec reboot");
246
247                 result = kexec_reboot(boot_task.dry_run);
248
249                 if (result) {
250                         update_status(status_fn, status_arg, BOOT_STATUS_ERROR,
251                                         "kexec reboot failed");
252                 }
253         }
254
255         return result;
256 }