Revert "tty: hvc: Fix data abort due to race in hvc_open"
[linux/fpc-iii.git] / tools / bootconfig / main.c
blobe0878f5f74b1b8cdaae27fe366b2a38ebe2ab097
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>
14 #include <linux/kernel.h>
15 #include <linux/bootconfig.h>
17 static int xbc_show_value(struct xbc_node *node)
19 const char *val;
20 char q;
21 int i = 0;
23 xbc_array_for_each_value(node, val) {
24 if (strchr(val, '"'))
25 q = '\'';
26 else
27 q = '"';
28 printf("%c%s%c%s", q, val, q, node->next ? ", " : ";\n");
29 i++;
31 return i;
34 static void xbc_show_compact_tree(void)
36 struct xbc_node *node, *cnode;
37 int depth = 0, i;
39 node = xbc_root_node();
40 while (node && xbc_node_is_key(node)) {
41 for (i = 0; i < depth; i++)
42 printf("\t");
43 cnode = xbc_node_get_child(node);
44 while (cnode && xbc_node_is_key(cnode) && !cnode->next) {
45 printf("%s.", xbc_node_get_data(node));
46 node = cnode;
47 cnode = xbc_node_get_child(node);
49 if (cnode && xbc_node_is_key(cnode)) {
50 printf("%s {\n", xbc_node_get_data(node));
51 depth++;
52 node = cnode;
53 continue;
54 } else if (cnode && xbc_node_is_value(cnode)) {
55 printf("%s = ", xbc_node_get_data(node));
56 xbc_show_value(cnode);
57 } else {
58 printf("%s;\n", xbc_node_get_data(node));
61 if (node->next) {
62 node = xbc_node_get_next(node);
63 continue;
65 while (!node->next) {
66 node = xbc_node_get_parent(node);
67 if (!node)
68 return;
69 if (!xbc_node_get_child(node)->next)
70 continue;
71 depth--;
72 for (i = 0; i < depth; i++)
73 printf("\t");
74 printf("}\n");
76 node = xbc_node_get_next(node);
80 /* Simple real checksum */
81 int checksum(unsigned char *buf, int len)
83 int i, sum = 0;
85 for (i = 0; i < len; i++)
86 sum += buf[i];
88 return sum;
91 #define PAGE_SIZE 4096
93 int load_xbc_fd(int fd, char **buf, int size)
95 int ret;
97 *buf = malloc(size + 1);
98 if (!*buf)
99 return -ENOMEM;
101 ret = read(fd, *buf, size);
102 if (ret < 0)
103 return -errno;
104 (*buf)[size] = '\0';
106 return ret;
109 /* Return the read size or -errno */
110 int load_xbc_file(const char *path, char **buf)
112 struct stat stat;
113 int fd, ret;
115 fd = open(path, O_RDONLY);
116 if (fd < 0)
117 return -errno;
118 ret = fstat(fd, &stat);
119 if (ret < 0)
120 return -errno;
122 ret = load_xbc_fd(fd, buf, stat.st_size);
124 close(fd);
126 return ret;
129 int load_xbc_from_initrd(int fd, char **buf)
131 struct stat stat;
132 int ret;
133 u32 size = 0, csum = 0, rcsum;
134 char magic[BOOTCONFIG_MAGIC_LEN];
135 const char *msg;
137 ret = fstat(fd, &stat);
138 if (ret < 0)
139 return -errno;
141 if (stat.st_size < 8 + BOOTCONFIG_MAGIC_LEN)
142 return 0;
144 if (lseek(fd, -BOOTCONFIG_MAGIC_LEN, SEEK_END) < 0) {
145 pr_err("Failed to lseek: %d\n", -errno);
146 return -errno;
148 if (read(fd, magic, BOOTCONFIG_MAGIC_LEN) < 0)
149 return -errno;
150 /* Check the bootconfig magic bytes */
151 if (memcmp(magic, BOOTCONFIG_MAGIC, BOOTCONFIG_MAGIC_LEN) != 0)
152 return 0;
154 if (lseek(fd, -(8 + BOOTCONFIG_MAGIC_LEN), SEEK_END) < 0) {
155 pr_err("Failed to lseek: %d\n", -errno);
156 return -errno;
159 if (read(fd, &size, sizeof(u32)) < 0)
160 return -errno;
162 if (read(fd, &csum, sizeof(u32)) < 0)
163 return -errno;
165 /* Wrong size error */
166 if (stat.st_size < size + 8 + BOOTCONFIG_MAGIC_LEN) {
167 pr_err("bootconfig size is too big\n");
168 return -E2BIG;
171 if (lseek(fd, stat.st_size - (size + 8 + BOOTCONFIG_MAGIC_LEN),
172 SEEK_SET) < 0) {
173 pr_err("Failed to lseek: %d\n", -errno);
174 return -errno;
177 ret = load_xbc_fd(fd, buf, size);
178 if (ret < 0)
179 return ret;
181 /* Wrong Checksum */
182 rcsum = checksum((unsigned char *)*buf, size);
183 if (csum != rcsum) {
184 pr_err("checksum error: %d != %d\n", csum, rcsum);
185 return -EINVAL;
188 ret = xbc_init(*buf, &msg, NULL);
189 /* Wrong data */
190 if (ret < 0) {
191 pr_err("parse error: %s.\n", msg);
192 return ret;
195 return size;
198 int show_xbc(const char *path)
200 int ret, fd;
201 char *buf = NULL;
203 fd = open(path, O_RDONLY);
204 if (fd < 0) {
205 pr_err("Failed to open initrd %s: %d\n", path, fd);
206 return -errno;
209 ret = load_xbc_from_initrd(fd, &buf);
210 if (ret < 0) {
211 pr_err("Failed to load a boot config from initrd: %d\n", ret);
212 goto out;
214 xbc_show_compact_tree();
215 ret = 0;
216 out:
217 close(fd);
218 free(buf);
220 return ret;
223 int delete_xbc(const char *path)
225 struct stat stat;
226 int ret = 0, fd, size;
227 char *buf = NULL;
229 fd = open(path, O_RDWR);
230 if (fd < 0) {
231 pr_err("Failed to open initrd %s: %d\n", path, fd);
232 return -errno;
235 size = load_xbc_from_initrd(fd, &buf);
236 if (size < 0) {
237 ret = size;
238 pr_err("Failed to load a boot config from initrd: %d\n", ret);
239 } else if (size > 0) {
240 ret = fstat(fd, &stat);
241 if (!ret)
242 ret = ftruncate(fd, stat.st_size
243 - size - 8 - BOOTCONFIG_MAGIC_LEN);
244 if (ret)
245 ret = -errno;
246 } /* Ignore if there is no boot config in initrd */
248 close(fd);
249 free(buf);
251 return ret;
254 static void show_xbc_error(const char *data, const char *msg, int pos)
256 int lin = 1, col, i;
258 if (pos < 0) {
259 pr_err("Error: %s.\n", msg);
260 return;
263 /* Note that pos starts from 0 but lin and col should start from 1. */
264 col = pos + 1;
265 for (i = 0; i < pos; i++) {
266 if (data[i] == '\n') {
267 lin++;
268 col = pos - i;
271 pr_err("Parse Error: %s at %d:%d\n", msg, lin, col);
275 int apply_xbc(const char *path, const char *xbc_path)
277 u32 size, csum;
278 char *buf, *data;
279 int ret, fd;
280 const char *msg;
281 int pos;
283 ret = load_xbc_file(xbc_path, &buf);
284 if (ret < 0) {
285 pr_err("Failed to load %s : %d\n", xbc_path, ret);
286 return ret;
288 size = strlen(buf) + 1;
289 csum = checksum((unsigned char *)buf, size);
291 /* Prepare xbc_path data */
292 data = malloc(size + 8);
293 if (!data)
294 return -ENOMEM;
295 strcpy(data, buf);
296 *(u32 *)(data + size) = size;
297 *(u32 *)(data + size + 4) = csum;
299 /* Check the data format */
300 ret = xbc_init(buf, &msg, &pos);
301 if (ret < 0) {
302 show_xbc_error(data, msg, pos);
303 free(data);
304 free(buf);
306 return ret;
308 printf("Apply %s to %s\n", xbc_path, path);
309 printf("\tNumber of nodes: %d\n", ret);
310 printf("\tSize: %u bytes\n", (unsigned int)size);
311 printf("\tChecksum: %d\n", (unsigned int)csum);
313 /* TODO: Check the options by schema */
314 xbc_destroy_all();
315 free(buf);
317 /* Remove old boot config if exists */
318 ret = delete_xbc(path);
319 if (ret < 0) {
320 pr_err("Failed to delete previous boot config: %d\n", ret);
321 free(data);
322 return ret;
325 /* Apply new one */
326 fd = open(path, O_RDWR | O_APPEND);
327 if (fd < 0) {
328 pr_err("Failed to open %s: %d\n", path, fd);
329 free(data);
330 return fd;
332 /* TODO: Ensure the @path is initramfs/initrd image */
333 ret = write(fd, data, size + 8);
334 if (ret < 0) {
335 pr_err("Failed to apply a boot config: %d\n", ret);
336 goto out;
338 /* Write a magic word of the bootconfig */
339 ret = write(fd, BOOTCONFIG_MAGIC, BOOTCONFIG_MAGIC_LEN);
340 if (ret < 0) {
341 pr_err("Failed to apply a boot config magic: %d\n", ret);
342 goto out;
344 ret = 0;
345 out:
346 close(fd);
347 free(data);
349 return ret;
352 int usage(void)
354 printf("Usage: bootconfig [OPTIONS] <INITRD>\n"
355 " Apply, delete or show boot config to initrd.\n"
356 " Options:\n"
357 " -a <config>: Apply boot config to initrd\n"
358 " -d : Delete boot config file from initrd\n\n"
359 " If no option is given, show current applied boot config.\n");
360 return -1;
363 int main(int argc, char **argv)
365 char *path = NULL;
366 char *apply = NULL;
367 bool delete = false;
368 int opt;
370 while ((opt = getopt(argc, argv, "hda:")) != -1) {
371 switch (opt) {
372 case 'd':
373 delete = true;
374 break;
375 case 'a':
376 apply = optarg;
377 break;
378 case 'h':
379 default:
380 return usage();
384 if (apply && delete) {
385 pr_err("Error: You can not specify both -a and -d at once.\n");
386 return usage();
389 if (optind >= argc) {
390 pr_err("Error: No initrd is specified.\n");
391 return usage();
394 path = argv[optind];
396 if (apply)
397 return apply_xbc(path, apply);
398 else if (delete)
399 return delete_xbc(path);
401 return show_xbc(path);