]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blob - tools/bootconfig/main.c
Merge tag 'asoc-v5.7' of https://git.kernel.org/pub/scm/linux/kernel/git/broonie...
[mirror_ubuntu-hirsute-kernel.git] / tools / bootconfig / main.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Boot config tool for initrd image
4 */
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <sys/types.h>
8 #include <sys/stat.h>
9 #include <fcntl.h>
10 #include <unistd.h>
11 #include <string.h>
12 #include <errno.h>
13
14 #include <linux/kernel.h>
15 #include <linux/bootconfig.h>
16
17 static int xbc_show_array(struct xbc_node *node)
18 {
19 const char *val;
20 int i = 0;
21
22 xbc_array_for_each_value(node, val) {
23 printf("\"%s\"%s", val, node->next ? ", " : ";\n");
24 i++;
25 }
26 return i;
27 }
28
29 static void xbc_show_compact_tree(void)
30 {
31 struct xbc_node *node, *cnode;
32 int depth = 0, i;
33
34 node = xbc_root_node();
35 while (node && xbc_node_is_key(node)) {
36 for (i = 0; i < depth; i++)
37 printf("\t");
38 cnode = xbc_node_get_child(node);
39 while (cnode && xbc_node_is_key(cnode) && !cnode->next) {
40 printf("%s.", xbc_node_get_data(node));
41 node = cnode;
42 cnode = xbc_node_get_child(node);
43 }
44 if (cnode && xbc_node_is_key(cnode)) {
45 printf("%s {\n", xbc_node_get_data(node));
46 depth++;
47 node = cnode;
48 continue;
49 } else if (cnode && xbc_node_is_value(cnode)) {
50 printf("%s = ", xbc_node_get_data(node));
51 if (cnode->next)
52 xbc_show_array(cnode);
53 else
54 printf("\"%s\";\n", xbc_node_get_data(cnode));
55 } else {
56 printf("%s;\n", xbc_node_get_data(node));
57 }
58
59 if (node->next) {
60 node = xbc_node_get_next(node);
61 continue;
62 }
63 while (!node->next) {
64 node = xbc_node_get_parent(node);
65 if (!node)
66 return;
67 if (!xbc_node_get_child(node)->next)
68 continue;
69 depth--;
70 for (i = 0; i < depth; i++)
71 printf("\t");
72 printf("}\n");
73 }
74 node = xbc_node_get_next(node);
75 }
76 }
77
78 /* Simple real checksum */
79 int checksum(unsigned char *buf, int len)
80 {
81 int i, sum = 0;
82
83 for (i = 0; i < len; i++)
84 sum += buf[i];
85
86 return sum;
87 }
88
89 #define PAGE_SIZE 4096
90
91 int load_xbc_fd(int fd, char **buf, int size)
92 {
93 int ret;
94
95 *buf = malloc(size + 1);
96 if (!*buf)
97 return -ENOMEM;
98
99 ret = read(fd, *buf, size);
100 if (ret < 0)
101 return -errno;
102 (*buf)[size] = '\0';
103
104 return ret;
105 }
106
107 /* Return the read size or -errno */
108 int load_xbc_file(const char *path, char **buf)
109 {
110 struct stat stat;
111 int fd, ret;
112
113 fd = open(path, O_RDONLY);
114 if (fd < 0)
115 return -errno;
116 ret = fstat(fd, &stat);
117 if (ret < 0)
118 return -errno;
119
120 ret = load_xbc_fd(fd, buf, stat.st_size);
121
122 close(fd);
123
124 return ret;
125 }
126
127 int load_xbc_from_initrd(int fd, char **buf)
128 {
129 struct stat stat;
130 int ret;
131 u32 size = 0, csum = 0, rcsum;
132 char magic[BOOTCONFIG_MAGIC_LEN];
133
134 ret = fstat(fd, &stat);
135 if (ret < 0)
136 return -errno;
137
138 if (stat.st_size < 8 + BOOTCONFIG_MAGIC_LEN)
139 return 0;
140
141 if (lseek(fd, -BOOTCONFIG_MAGIC_LEN, SEEK_END) < 0) {
142 pr_err("Failed to lseek: %d\n", -errno);
143 return -errno;
144 }
145 if (read(fd, magic, BOOTCONFIG_MAGIC_LEN) < 0)
146 return -errno;
147 /* Check the bootconfig magic bytes */
148 if (memcmp(magic, BOOTCONFIG_MAGIC, BOOTCONFIG_MAGIC_LEN) != 0)
149 return 0;
150
151 if (lseek(fd, -(8 + BOOTCONFIG_MAGIC_LEN), SEEK_END) < 0) {
152 pr_err("Failed to lseek: %d\n", -errno);
153 return -errno;
154 }
155
156 if (read(fd, &size, sizeof(u32)) < 0)
157 return -errno;
158
159 if (read(fd, &csum, sizeof(u32)) < 0)
160 return -errno;
161
162 /* Wrong size error */
163 if (stat.st_size < size + 8 + BOOTCONFIG_MAGIC_LEN) {
164 pr_err("bootconfig size is too big\n");
165 return -E2BIG;
166 }
167
168 if (lseek(fd, stat.st_size - (size + 8 + BOOTCONFIG_MAGIC_LEN),
169 SEEK_SET) < 0) {
170 pr_err("Failed to lseek: %d\n", -errno);
171 return -errno;
172 }
173
174 ret = load_xbc_fd(fd, buf, size);
175 if (ret < 0)
176 return ret;
177
178 /* Wrong Checksum */
179 rcsum = checksum((unsigned char *)*buf, size);
180 if (csum != rcsum) {
181 pr_err("checksum error: %d != %d\n", csum, rcsum);
182 return -EINVAL;
183 }
184
185 ret = xbc_init(*buf);
186 /* Wrong data */
187 if (ret < 0)
188 return ret;
189
190 return size;
191 }
192
193 int show_xbc(const char *path)
194 {
195 int ret, fd;
196 char *buf = NULL;
197
198 fd = open(path, O_RDONLY);
199 if (fd < 0) {
200 pr_err("Failed to open initrd %s: %d\n", path, fd);
201 return -errno;
202 }
203
204 ret = load_xbc_from_initrd(fd, &buf);
205 if (ret < 0)
206 pr_err("Failed to load a boot config from initrd: %d\n", ret);
207 else
208 xbc_show_compact_tree();
209
210 close(fd);
211 free(buf);
212
213 return ret;
214 }
215
216 int delete_xbc(const char *path)
217 {
218 struct stat stat;
219 int ret = 0, fd, size;
220 char *buf = NULL;
221
222 fd = open(path, O_RDWR);
223 if (fd < 0) {
224 pr_err("Failed to open initrd %s: %d\n", path, fd);
225 return -errno;
226 }
227
228 size = load_xbc_from_initrd(fd, &buf);
229 if (size < 0) {
230 ret = size;
231 pr_err("Failed to load a boot config from initrd: %d\n", ret);
232 } else if (size > 0) {
233 ret = fstat(fd, &stat);
234 if (!ret)
235 ret = ftruncate(fd, stat.st_size
236 - size - 8 - BOOTCONFIG_MAGIC_LEN);
237 if (ret)
238 ret = -errno;
239 } /* Ignore if there is no boot config in initrd */
240
241 close(fd);
242 free(buf);
243
244 return ret;
245 }
246
247 int apply_xbc(const char *path, const char *xbc_path)
248 {
249 u32 size, csum;
250 char *buf, *data;
251 int ret, fd;
252
253 ret = load_xbc_file(xbc_path, &buf);
254 if (ret < 0) {
255 pr_err("Failed to load %s : %d\n", xbc_path, ret);
256 return ret;
257 }
258 size = strlen(buf) + 1;
259 csum = checksum((unsigned char *)buf, size);
260
261 /* Prepare xbc_path data */
262 data = malloc(size + 8);
263 if (!data)
264 return -ENOMEM;
265 strcpy(data, buf);
266 *(u32 *)(data + size) = size;
267 *(u32 *)(data + size + 4) = csum;
268
269 /* Check the data format */
270 ret = xbc_init(buf);
271 if (ret < 0) {
272 pr_err("Failed to parse %s: %d\n", xbc_path, ret);
273 free(data);
274 free(buf);
275 return ret;
276 }
277 printf("Apply %s to %s\n", xbc_path, path);
278 printf("\tNumber of nodes: %d\n", ret);
279 printf("\tSize: %u bytes\n", (unsigned int)size);
280 printf("\tChecksum: %d\n", (unsigned int)csum);
281
282 /* TODO: Check the options by schema */
283 xbc_destroy_all();
284 free(buf);
285
286 /* Remove old boot config if exists */
287 ret = delete_xbc(path);
288 if (ret < 0) {
289 pr_err("Failed to delete previous boot config: %d\n", ret);
290 return ret;
291 }
292
293 /* Apply new one */
294 fd = open(path, O_RDWR | O_APPEND);
295 if (fd < 0) {
296 pr_err("Failed to open %s: %d\n", path, fd);
297 return fd;
298 }
299 /* TODO: Ensure the @path is initramfs/initrd image */
300 ret = write(fd, data, size + 8);
301 if (ret < 0) {
302 pr_err("Failed to apply a boot config: %d\n", ret);
303 return ret;
304 }
305 /* Write a magic word of the bootconfig */
306 ret = write(fd, BOOTCONFIG_MAGIC, BOOTCONFIG_MAGIC_LEN);
307 if (ret < 0) {
308 pr_err("Failed to apply a boot config magic: %d\n", ret);
309 return ret;
310 }
311 close(fd);
312 free(data);
313
314 return 0;
315 }
316
317 int usage(void)
318 {
319 printf("Usage: bootconfig [OPTIONS] <INITRD>\n"
320 " Apply, delete or show boot config to initrd.\n"
321 " Options:\n"
322 " -a <config>: Apply boot config to initrd\n"
323 " -d : Delete boot config file from initrd\n\n"
324 " If no option is given, show current applied boot config.\n");
325 return -1;
326 }
327
328 int main(int argc, char **argv)
329 {
330 char *path = NULL;
331 char *apply = NULL;
332 bool delete = false;
333 int opt;
334
335 while ((opt = getopt(argc, argv, "hda:")) != -1) {
336 switch (opt) {
337 case 'd':
338 delete = true;
339 break;
340 case 'a':
341 apply = optarg;
342 break;
343 case 'h':
344 default:
345 return usage();
346 }
347 }
348
349 if (apply && delete) {
350 pr_err("Error: You can not specify both -a and -d at once.\n");
351 return usage();
352 }
353
354 if (optind >= argc) {
355 pr_err("Error: No initrd is specified.\n");
356 return usage();
357 }
358
359 path = argv[optind];
360
361 if (apply)
362 return apply_xbc(path, apply);
363 else if (delete)
364 return delete_xbc(path);
365
366 return show_xbc(path);
367 }