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