xref: /openbmc/linux/tools/bootconfig/main.c (revision e306220cb7b7c2948f191414ab06851e143b54c1)
1950313ebSMasami Hiramatsu // SPDX-License-Identifier: GPL-2.0
2950313ebSMasami Hiramatsu /*
3950313ebSMasami Hiramatsu  * Boot config tool for initrd image
4950313ebSMasami Hiramatsu  */
5950313ebSMasami Hiramatsu #include <stdio.h>
6950313ebSMasami Hiramatsu #include <stdlib.h>
7950313ebSMasami Hiramatsu #include <sys/types.h>
8950313ebSMasami Hiramatsu #include <sys/stat.h>
9950313ebSMasami Hiramatsu #include <fcntl.h>
10950313ebSMasami Hiramatsu #include <unistd.h>
11950313ebSMasami Hiramatsu #include <string.h>
12950313ebSMasami Hiramatsu #include <errno.h>
13e8684358SMasami Hiramatsu #include <endian.h>
14950313ebSMasami Hiramatsu 
15950313ebSMasami Hiramatsu #include <linux/kernel.h>
16950313ebSMasami Hiramatsu #include <linux/bootconfig.h>
17950313ebSMasami Hiramatsu 
18e4f70b7bSMasami Hiramatsu static int xbc_show_value(struct xbc_node *node, bool semicolon)
19950313ebSMasami Hiramatsu {
20e4f70b7bSMasami Hiramatsu 	const char *val, *eol;
21272da327SMasami Hiramatsu 	char q;
22950313ebSMasami Hiramatsu 	int i = 0;
23950313ebSMasami Hiramatsu 
24e4f70b7bSMasami Hiramatsu 	eol = semicolon ? ";\n" : "\n";
25950313ebSMasami Hiramatsu 	xbc_array_for_each_value(node, val) {
26272da327SMasami Hiramatsu 		if (strchr(val, '"'))
27272da327SMasami Hiramatsu 			q = '\'';
28272da327SMasami Hiramatsu 		else
29272da327SMasami Hiramatsu 			q = '"';
30ca24306dSMasami Hiramatsu 		printf("%c%s%c%s", q, val, q, xbc_node_is_array(node) ? ", " : eol);
31950313ebSMasami Hiramatsu 		i++;
32950313ebSMasami Hiramatsu 	}
33950313ebSMasami Hiramatsu 	return i;
34950313ebSMasami Hiramatsu }
35950313ebSMasami Hiramatsu 
36950313ebSMasami Hiramatsu static void xbc_show_compact_tree(void)
37950313ebSMasami Hiramatsu {
38e5efaeb8SMasami Hiramatsu 	struct xbc_node *node, *cnode = NULL, *vnode;
39950313ebSMasami Hiramatsu 	int depth = 0, i;
40950313ebSMasami Hiramatsu 
41950313ebSMasami Hiramatsu 	node = xbc_root_node();
42950313ebSMasami Hiramatsu 	while (node && xbc_node_is_key(node)) {
43950313ebSMasami Hiramatsu 		for (i = 0; i < depth; i++)
44950313ebSMasami Hiramatsu 			printf("\t");
45e5efaeb8SMasami Hiramatsu 		if (!cnode)
46950313ebSMasami Hiramatsu 			cnode = xbc_node_get_child(node);
47950313ebSMasami Hiramatsu 		while (cnode && xbc_node_is_key(cnode) && !cnode->next) {
48e5efaeb8SMasami Hiramatsu 			vnode = xbc_node_get_child(cnode);
49e5efaeb8SMasami Hiramatsu 			/*
50e5efaeb8SMasami Hiramatsu 			 * If @cnode has value and subkeys, this
51e5efaeb8SMasami Hiramatsu 			 * should show it as below.
52e5efaeb8SMasami Hiramatsu 			 *
53e5efaeb8SMasami Hiramatsu 			 * key(@node) {
54e5efaeb8SMasami Hiramatsu 			 *      key(@cnode) = value;
55e5efaeb8SMasami Hiramatsu 			 *      key(@cnode) {
56e5efaeb8SMasami Hiramatsu 			 *          subkeys;
57e5efaeb8SMasami Hiramatsu 			 *      }
58e5efaeb8SMasami Hiramatsu 			 * }
59e5efaeb8SMasami Hiramatsu 			 */
60e5efaeb8SMasami Hiramatsu 			if (vnode && xbc_node_is_value(vnode) && vnode->next)
61e5efaeb8SMasami Hiramatsu 				break;
62950313ebSMasami Hiramatsu 			printf("%s.", xbc_node_get_data(node));
63950313ebSMasami Hiramatsu 			node = cnode;
64e5efaeb8SMasami Hiramatsu 			cnode = vnode;
65950313ebSMasami Hiramatsu 		}
66950313ebSMasami Hiramatsu 		if (cnode && xbc_node_is_key(cnode)) {
67950313ebSMasami Hiramatsu 			printf("%s {\n", xbc_node_get_data(node));
68950313ebSMasami Hiramatsu 			depth++;
69950313ebSMasami Hiramatsu 			node = cnode;
70e5efaeb8SMasami Hiramatsu 			cnode = NULL;
71950313ebSMasami Hiramatsu 			continue;
72950313ebSMasami Hiramatsu 		} else if (cnode && xbc_node_is_value(cnode)) {
73950313ebSMasami Hiramatsu 			printf("%s = ", xbc_node_get_data(node));
74e4f70b7bSMasami Hiramatsu 			xbc_show_value(cnode, true);
75e5efaeb8SMasami Hiramatsu 			/*
76e5efaeb8SMasami Hiramatsu 			 * If @node has value and subkeys, continue
77e5efaeb8SMasami Hiramatsu 			 * looping on subkeys with same node.
78e5efaeb8SMasami Hiramatsu 			 */
79e5efaeb8SMasami Hiramatsu 			if (cnode->next) {
80e5efaeb8SMasami Hiramatsu 				cnode = xbc_node_get_next(cnode);
81e5efaeb8SMasami Hiramatsu 				continue;
82e5efaeb8SMasami Hiramatsu 			}
83950313ebSMasami Hiramatsu 		} else {
84950313ebSMasami Hiramatsu 			printf("%s;\n", xbc_node_get_data(node));
85950313ebSMasami Hiramatsu 		}
86e5efaeb8SMasami Hiramatsu 		cnode = NULL;
87950313ebSMasami Hiramatsu 
88950313ebSMasami Hiramatsu 		if (node->next) {
89950313ebSMasami Hiramatsu 			node = xbc_node_get_next(node);
90950313ebSMasami Hiramatsu 			continue;
91950313ebSMasami Hiramatsu 		}
92950313ebSMasami Hiramatsu 		while (!node->next) {
93950313ebSMasami Hiramatsu 			node = xbc_node_get_parent(node);
94950313ebSMasami Hiramatsu 			if (!node)
95950313ebSMasami Hiramatsu 				return;
96950313ebSMasami Hiramatsu 			if (!xbc_node_get_child(node)->next)
97950313ebSMasami Hiramatsu 				continue;
98e5efaeb8SMasami Hiramatsu 			if (depth) {
99950313ebSMasami Hiramatsu 				depth--;
100950313ebSMasami Hiramatsu 				for (i = 0; i < depth; i++)
101950313ebSMasami Hiramatsu 					printf("\t");
102950313ebSMasami Hiramatsu 				printf("}\n");
103950313ebSMasami Hiramatsu 			}
104e5efaeb8SMasami Hiramatsu 		}
105950313ebSMasami Hiramatsu 		node = xbc_node_get_next(node);
106950313ebSMasami Hiramatsu 	}
107950313ebSMasami Hiramatsu }
108950313ebSMasami Hiramatsu 
109e4f70b7bSMasami Hiramatsu static void xbc_show_list(void)
110e4f70b7bSMasami Hiramatsu {
111e4f70b7bSMasami Hiramatsu 	char key[XBC_KEYLEN_MAX];
112e4f70b7bSMasami Hiramatsu 	struct xbc_node *leaf;
113e4f70b7bSMasami Hiramatsu 	const char *val;
114903bd067SJulio Faracco 	int ret;
115e4f70b7bSMasami Hiramatsu 
116e4f70b7bSMasami Hiramatsu 	xbc_for_each_key_value(leaf, val) {
117903bd067SJulio Faracco 		ret = xbc_node_compose_key(leaf, key, XBC_KEYLEN_MAX);
118903bd067SJulio Faracco 		if (ret < 0) {
119e5efaeb8SMasami Hiramatsu 			fprintf(stderr, "Failed to compose key %d\n", ret);
120e4f70b7bSMasami Hiramatsu 			break;
121e5efaeb8SMasami Hiramatsu 		}
122e4f70b7bSMasami Hiramatsu 		printf("%s = ", key);
123e4f70b7bSMasami Hiramatsu 		if (!val || val[0] == '\0') {
124e4f70b7bSMasami Hiramatsu 			printf("\"\"\n");
125e4f70b7bSMasami Hiramatsu 			continue;
126e4f70b7bSMasami Hiramatsu 		}
127e4f70b7bSMasami Hiramatsu 		xbc_show_value(xbc_node_get_child(leaf), false);
128e4f70b7bSMasami Hiramatsu 	}
129e4f70b7bSMasami Hiramatsu }
130e4f70b7bSMasami Hiramatsu 
131950313ebSMasami Hiramatsu #define PAGE_SIZE	4096
132950313ebSMasami Hiramatsu 
133483ce670SMasami Hiramatsu static int load_xbc_fd(int fd, char **buf, int size)
134950313ebSMasami Hiramatsu {
135950313ebSMasami Hiramatsu 	int ret;
136950313ebSMasami Hiramatsu 
137950313ebSMasami Hiramatsu 	*buf = malloc(size + 1);
138950313ebSMasami Hiramatsu 	if (!*buf)
139950313ebSMasami Hiramatsu 		return -ENOMEM;
140950313ebSMasami Hiramatsu 
141950313ebSMasami Hiramatsu 	ret = read(fd, *buf, size);
142950313ebSMasami Hiramatsu 	if (ret < 0)
143950313ebSMasami Hiramatsu 		return -errno;
144950313ebSMasami Hiramatsu 	(*buf)[size] = '\0';
145950313ebSMasami Hiramatsu 
146950313ebSMasami Hiramatsu 	return ret;
147950313ebSMasami Hiramatsu }
148950313ebSMasami Hiramatsu 
149950313ebSMasami Hiramatsu /* Return the read size or -errno */
150483ce670SMasami Hiramatsu static int load_xbc_file(const char *path, char **buf)
151950313ebSMasami Hiramatsu {
152950313ebSMasami Hiramatsu 	struct stat stat;
153950313ebSMasami Hiramatsu 	int fd, ret;
154950313ebSMasami Hiramatsu 
155950313ebSMasami Hiramatsu 	fd = open(path, O_RDONLY);
156950313ebSMasami Hiramatsu 	if (fd < 0)
157950313ebSMasami Hiramatsu 		return -errno;
158950313ebSMasami Hiramatsu 	ret = fstat(fd, &stat);
159950313ebSMasami Hiramatsu 	if (ret < 0)
160950313ebSMasami Hiramatsu 		return -errno;
161950313ebSMasami Hiramatsu 
162950313ebSMasami Hiramatsu 	ret = load_xbc_fd(fd, buf, stat.st_size);
163950313ebSMasami Hiramatsu 
164950313ebSMasami Hiramatsu 	close(fd);
165950313ebSMasami Hiramatsu 
166950313ebSMasami Hiramatsu 	return ret;
167950313ebSMasami Hiramatsu }
168950313ebSMasami Hiramatsu 
169a61ea637SMasami Hiramatsu static int pr_errno(const char *msg, int err)
170a61ea637SMasami Hiramatsu {
171a61ea637SMasami Hiramatsu 	pr_err("%s: %d\n", msg, err);
172a61ea637SMasami Hiramatsu 	return err;
173a61ea637SMasami Hiramatsu }
174a61ea637SMasami Hiramatsu 
175483ce670SMasami Hiramatsu static int load_xbc_from_initrd(int fd, char **buf)
176950313ebSMasami Hiramatsu {
177950313ebSMasami Hiramatsu 	struct stat stat;
178950313ebSMasami Hiramatsu 	int ret;
179950313ebSMasami Hiramatsu 	u32 size = 0, csum = 0, rcsum;
18085c46b78SMasami Hiramatsu 	char magic[BOOTCONFIG_MAGIC_LEN];
18189b74cacSMasami Hiramatsu 	const char *msg;
182950313ebSMasami Hiramatsu 
183950313ebSMasami Hiramatsu 	ret = fstat(fd, &stat);
184950313ebSMasami Hiramatsu 	if (ret < 0)
185950313ebSMasami Hiramatsu 		return -errno;
186950313ebSMasami Hiramatsu 
18785c46b78SMasami Hiramatsu 	if (stat.st_size < 8 + BOOTCONFIG_MAGIC_LEN)
188950313ebSMasami Hiramatsu 		return 0;
189950313ebSMasami Hiramatsu 
190a61ea637SMasami Hiramatsu 	if (lseek(fd, -BOOTCONFIG_MAGIC_LEN, SEEK_END) < 0)
191a61ea637SMasami Hiramatsu 		return pr_errno("Failed to lseek for magic", -errno);
192a61ea637SMasami Hiramatsu 
19385c46b78SMasami Hiramatsu 	if (read(fd, magic, BOOTCONFIG_MAGIC_LEN) < 0)
194a61ea637SMasami Hiramatsu 		return pr_errno("Failed to read", -errno);
195a61ea637SMasami Hiramatsu 
19685c46b78SMasami Hiramatsu 	/* Check the bootconfig magic bytes */
19785c46b78SMasami Hiramatsu 	if (memcmp(magic, BOOTCONFIG_MAGIC, BOOTCONFIG_MAGIC_LEN) != 0)
19885c46b78SMasami Hiramatsu 		return 0;
19985c46b78SMasami Hiramatsu 
200a61ea637SMasami Hiramatsu 	if (lseek(fd, -(8 + BOOTCONFIG_MAGIC_LEN), SEEK_END) < 0)
201a61ea637SMasami Hiramatsu 		return pr_errno("Failed to lseek for size", -errno);
202950313ebSMasami Hiramatsu 
203950313ebSMasami Hiramatsu 	if (read(fd, &size, sizeof(u32)) < 0)
204a61ea637SMasami Hiramatsu 		return pr_errno("Failed to read size", -errno);
205e8684358SMasami Hiramatsu 	size = le32toh(size);
206950313ebSMasami Hiramatsu 
207950313ebSMasami Hiramatsu 	if (read(fd, &csum, sizeof(u32)) < 0)
208a61ea637SMasami Hiramatsu 		return pr_errno("Failed to read checksum", -errno);
209e8684358SMasami Hiramatsu 	csum = le32toh(csum);
210950313ebSMasami Hiramatsu 
21185c46b78SMasami Hiramatsu 	/* Wrong size error  */
21285c46b78SMasami Hiramatsu 	if (stat.st_size < size + 8 + BOOTCONFIG_MAGIC_LEN) {
21385c46b78SMasami Hiramatsu 		pr_err("bootconfig size is too big\n");
21485c46b78SMasami Hiramatsu 		return -E2BIG;
21585c46b78SMasami Hiramatsu 	}
216950313ebSMasami Hiramatsu 
21785c46b78SMasami Hiramatsu 	if (lseek(fd, stat.st_size - (size + 8 + BOOTCONFIG_MAGIC_LEN),
218a61ea637SMasami Hiramatsu 		  SEEK_SET) < 0)
219a61ea637SMasami Hiramatsu 		return pr_errno("Failed to lseek", -errno);
220950313ebSMasami Hiramatsu 
221950313ebSMasami Hiramatsu 	ret = load_xbc_fd(fd, buf, size);
222950313ebSMasami Hiramatsu 	if (ret < 0)
223950313ebSMasami Hiramatsu 		return ret;
224950313ebSMasami Hiramatsu 
22585c46b78SMasami Hiramatsu 	/* Wrong Checksum */
22699f4f5d6SMasami Hiramatsu 	rcsum = xbc_calc_checksum(*buf, size);
227950313ebSMasami Hiramatsu 	if (csum != rcsum) {
22897378001SMasami Hiramatsu 		pr_err("checksum error: %d != %d\n", csum, rcsum);
22985c46b78SMasami Hiramatsu 		return -EINVAL;
230950313ebSMasami Hiramatsu 	}
231950313ebSMasami Hiramatsu 
232bdac5c2bSMasami Hiramatsu 	ret = xbc_init(*buf, size, &msg, NULL);
23385c46b78SMasami Hiramatsu 	/* Wrong data */
23489b74cacSMasami Hiramatsu 	if (ret < 0) {
23589b74cacSMasami Hiramatsu 		pr_err("parse error: %s.\n", msg);
23685c46b78SMasami Hiramatsu 		return ret;
23789b74cacSMasami Hiramatsu 	}
238950313ebSMasami Hiramatsu 
239950313ebSMasami Hiramatsu 	return size;
240950313ebSMasami Hiramatsu }
241950313ebSMasami Hiramatsu 
242d052e1c6SMasami Hiramatsu static void show_xbc_error(const char *data, const char *msg, int pos)
243d052e1c6SMasami Hiramatsu {
244d052e1c6SMasami Hiramatsu 	int lin = 1, col, i;
245d052e1c6SMasami Hiramatsu 
246d052e1c6SMasami Hiramatsu 	if (pos < 0) {
247d052e1c6SMasami Hiramatsu 		pr_err("Error: %s.\n", msg);
248d052e1c6SMasami Hiramatsu 		return;
249d052e1c6SMasami Hiramatsu 	}
250d052e1c6SMasami Hiramatsu 
251d052e1c6SMasami Hiramatsu 	/* Note that pos starts from 0 but lin and col should start from 1. */
252d052e1c6SMasami Hiramatsu 	col = pos + 1;
253d052e1c6SMasami Hiramatsu 	for (i = 0; i < pos; i++) {
254d052e1c6SMasami Hiramatsu 		if (data[i] == '\n') {
255d052e1c6SMasami Hiramatsu 			lin++;
256d052e1c6SMasami Hiramatsu 			col = pos - i;
257d052e1c6SMasami Hiramatsu 		}
258d052e1c6SMasami Hiramatsu 	}
259d052e1c6SMasami Hiramatsu 	pr_err("Parse Error: %s at %d:%d\n", msg, lin, col);
260d052e1c6SMasami Hiramatsu 
261d052e1c6SMasami Hiramatsu }
262d052e1c6SMasami Hiramatsu 
263d052e1c6SMasami Hiramatsu static int init_xbc_with_error(char *buf, int len)
264d052e1c6SMasami Hiramatsu {
265d052e1c6SMasami Hiramatsu 	char *copy = strdup(buf);
266d052e1c6SMasami Hiramatsu 	const char *msg;
267d052e1c6SMasami Hiramatsu 	int ret, pos;
268d052e1c6SMasami Hiramatsu 
269d052e1c6SMasami Hiramatsu 	if (!copy)
270d052e1c6SMasami Hiramatsu 		return -ENOMEM;
271d052e1c6SMasami Hiramatsu 
272bdac5c2bSMasami Hiramatsu 	ret = xbc_init(buf, len, &msg, &pos);
273d052e1c6SMasami Hiramatsu 	if (ret < 0)
274d052e1c6SMasami Hiramatsu 		show_xbc_error(copy, msg, pos);
275d052e1c6SMasami Hiramatsu 	free(copy);
276d052e1c6SMasami Hiramatsu 
277d052e1c6SMasami Hiramatsu 	return ret;
278d052e1c6SMasami Hiramatsu }
279d052e1c6SMasami Hiramatsu 
280483ce670SMasami Hiramatsu static int show_xbc(const char *path, bool list)
281950313ebSMasami Hiramatsu {
282950313ebSMasami Hiramatsu 	int ret, fd;
283950313ebSMasami Hiramatsu 	char *buf = NULL;
284d052e1c6SMasami Hiramatsu 	struct stat st;
285d052e1c6SMasami Hiramatsu 
286d052e1c6SMasami Hiramatsu 	ret = stat(path, &st);
287d052e1c6SMasami Hiramatsu 	if (ret < 0) {
288a61ea637SMasami Hiramatsu 		ret = -errno;
289a61ea637SMasami Hiramatsu 		pr_err("Failed to stat %s: %d\n", path, ret);
290a61ea637SMasami Hiramatsu 		return ret;
291d052e1c6SMasami Hiramatsu 	}
292950313ebSMasami Hiramatsu 
293950313ebSMasami Hiramatsu 	fd = open(path, O_RDONLY);
294950313ebSMasami Hiramatsu 	if (fd < 0) {
295a61ea637SMasami Hiramatsu 		ret = -errno;
296a61ea637SMasami Hiramatsu 		pr_err("Failed to open initrd %s: %d\n", path, ret);
297a61ea637SMasami Hiramatsu 		return ret;
298950313ebSMasami Hiramatsu 	}
299950313ebSMasami Hiramatsu 
300950313ebSMasami Hiramatsu 	ret = load_xbc_from_initrd(fd, &buf);
301d052e1c6SMasami Hiramatsu 	close(fd);
302f91cb5b7SMasami Hiramatsu 	if (ret < 0) {
30397378001SMasami Hiramatsu 		pr_err("Failed to load a boot config from initrd: %d\n", ret);
304f91cb5b7SMasami Hiramatsu 		goto out;
305f91cb5b7SMasami Hiramatsu 	}
306d052e1c6SMasami Hiramatsu 	/* Assume a bootconfig file if it is enough small */
307d052e1c6SMasami Hiramatsu 	if (ret == 0 && st.st_size <= XBC_DATA_MAX) {
308d052e1c6SMasami Hiramatsu 		ret = load_xbc_file(path, &buf);
309d052e1c6SMasami Hiramatsu 		if (ret < 0) {
310d052e1c6SMasami Hiramatsu 			pr_err("Failed to load a boot config: %d\n", ret);
311d052e1c6SMasami Hiramatsu 			goto out;
312d052e1c6SMasami Hiramatsu 		}
313d052e1c6SMasami Hiramatsu 		if (init_xbc_with_error(buf, ret) < 0)
314d052e1c6SMasami Hiramatsu 			goto out;
315d052e1c6SMasami Hiramatsu 	}
316e4f70b7bSMasami Hiramatsu 	if (list)
317e4f70b7bSMasami Hiramatsu 		xbc_show_list();
318e4f70b7bSMasami Hiramatsu 	else
319950313ebSMasami Hiramatsu 		xbc_show_compact_tree();
320f91cb5b7SMasami Hiramatsu 	ret = 0;
321f91cb5b7SMasami Hiramatsu out:
322950313ebSMasami Hiramatsu 	free(buf);
323950313ebSMasami Hiramatsu 
324950313ebSMasami Hiramatsu 	return ret;
325950313ebSMasami Hiramatsu }
326950313ebSMasami Hiramatsu 
327483ce670SMasami Hiramatsu static int delete_xbc(const char *path)
328950313ebSMasami Hiramatsu {
329950313ebSMasami Hiramatsu 	struct stat stat;
330950313ebSMasami Hiramatsu 	int ret = 0, fd, size;
331950313ebSMasami Hiramatsu 	char *buf = NULL;
332950313ebSMasami Hiramatsu 
333950313ebSMasami Hiramatsu 	fd = open(path, O_RDWR);
334950313ebSMasami Hiramatsu 	if (fd < 0) {
335a61ea637SMasami Hiramatsu 		ret = -errno;
336a61ea637SMasami Hiramatsu 		pr_err("Failed to open initrd %s: %d\n", path, ret);
337a61ea637SMasami Hiramatsu 		return ret;
338950313ebSMasami Hiramatsu 	}
339950313ebSMasami Hiramatsu 
340950313ebSMasami Hiramatsu 	size = load_xbc_from_initrd(fd, &buf);
341950313ebSMasami Hiramatsu 	if (size < 0) {
342950313ebSMasami Hiramatsu 		ret = size;
34397378001SMasami Hiramatsu 		pr_err("Failed to load a boot config from initrd: %d\n", ret);
344950313ebSMasami Hiramatsu 	} else if (size > 0) {
345950313ebSMasami Hiramatsu 		ret = fstat(fd, &stat);
346950313ebSMasami Hiramatsu 		if (!ret)
34785c46b78SMasami Hiramatsu 			ret = ftruncate(fd, stat.st_size
34885c46b78SMasami Hiramatsu 					- size - 8 - BOOTCONFIG_MAGIC_LEN);
349950313ebSMasami Hiramatsu 		if (ret)
350950313ebSMasami Hiramatsu 			ret = -errno;
351950313ebSMasami Hiramatsu 	} /* Ignore if there is no boot config in initrd */
352950313ebSMasami Hiramatsu 
353950313ebSMasami Hiramatsu 	close(fd);
354950313ebSMasami Hiramatsu 	free(buf);
355950313ebSMasami Hiramatsu 
356950313ebSMasami Hiramatsu 	return ret;
357950313ebSMasami Hiramatsu }
358950313ebSMasami Hiramatsu 
359483ce670SMasami Hiramatsu static int apply_xbc(const char *path, const char *xbc_path)
360950313ebSMasami Hiramatsu {
361e1cef2d4SMasami Hiramatsu 	char *buf, *data, *p;
362e1cef2d4SMasami Hiramatsu 	size_t total_size;
363a995e6bcSMasami Hiramatsu 	struct stat stat;
36489b74cacSMasami Hiramatsu 	const char *msg;
365e1cef2d4SMasami Hiramatsu 	u32 size, csum;
366e1cef2d4SMasami Hiramatsu 	int pos, pad;
367e1cef2d4SMasami Hiramatsu 	int ret, fd;
368950313ebSMasami Hiramatsu 
369950313ebSMasami Hiramatsu 	ret = load_xbc_file(xbc_path, &buf);
370950313ebSMasami Hiramatsu 	if (ret < 0) {
37197378001SMasami Hiramatsu 		pr_err("Failed to load %s : %d\n", xbc_path, ret);
372950313ebSMasami Hiramatsu 		return ret;
373950313ebSMasami Hiramatsu 	}
374950313ebSMasami Hiramatsu 	size = strlen(buf) + 1;
37599f4f5d6SMasami Hiramatsu 	csum = xbc_calc_checksum(buf, size);
376950313ebSMasami Hiramatsu 
377e1cef2d4SMasami Hiramatsu 	/* Backup the bootconfig data */
378e1cef2d4SMasami Hiramatsu 	data = calloc(size + BOOTCONFIG_ALIGN +
379e1cef2d4SMasami Hiramatsu 		      sizeof(u32) + sizeof(u32) + BOOTCONFIG_MAGIC_LEN, 1);
380950313ebSMasami Hiramatsu 	if (!data)
381950313ebSMasami Hiramatsu 		return -ENOMEM;
382e1cef2d4SMasami Hiramatsu 	memcpy(data, buf, size);
383950313ebSMasami Hiramatsu 
384950313ebSMasami Hiramatsu 	/* Check the data format */
385bdac5c2bSMasami Hiramatsu 	ret = xbc_init(buf, size, &msg, &pos);
386950313ebSMasami Hiramatsu 	if (ret < 0) {
38789b74cacSMasami Hiramatsu 		show_xbc_error(data, msg, pos);
388950313ebSMasami Hiramatsu 		free(data);
389950313ebSMasami Hiramatsu 		free(buf);
39089b74cacSMasami Hiramatsu 
391950313ebSMasami Hiramatsu 		return ret;
392950313ebSMasami Hiramatsu 	}
393950313ebSMasami Hiramatsu 	printf("Apply %s to %s\n", xbc_path, path);
394*e306220cSMasami Hiramatsu 	xbc_get_info(&ret, NULL);
3950f0d0a77SMasami Hiramatsu 	printf("\tNumber of nodes: %d\n", ret);
396950313ebSMasami Hiramatsu 	printf("\tSize: %u bytes\n", (unsigned int)size);
397950313ebSMasami Hiramatsu 	printf("\tChecksum: %d\n", (unsigned int)csum);
398950313ebSMasami Hiramatsu 
399950313ebSMasami Hiramatsu 	/* TODO: Check the options by schema */
400950313ebSMasami Hiramatsu 	xbc_destroy_all();
401950313ebSMasami Hiramatsu 	free(buf);
402950313ebSMasami Hiramatsu 
403950313ebSMasami Hiramatsu 	/* Remove old boot config if exists */
404950313ebSMasami Hiramatsu 	ret = delete_xbc(path);
405950313ebSMasami Hiramatsu 	if (ret < 0) {
40697378001SMasami Hiramatsu 		pr_err("Failed to delete previous boot config: %d\n", ret);
40788426044SYunfeng Ye 		free(data);
408950313ebSMasami Hiramatsu 		return ret;
409950313ebSMasami Hiramatsu 	}
410950313ebSMasami Hiramatsu 
411950313ebSMasami Hiramatsu 	/* Apply new one */
412950313ebSMasami Hiramatsu 	fd = open(path, O_RDWR | O_APPEND);
413950313ebSMasami Hiramatsu 	if (fd < 0) {
414a61ea637SMasami Hiramatsu 		ret = -errno;
415a61ea637SMasami Hiramatsu 		pr_err("Failed to open %s: %d\n", path, ret);
41688426044SYunfeng Ye 		free(data);
417a61ea637SMasami Hiramatsu 		return ret;
418950313ebSMasami Hiramatsu 	}
419950313ebSMasami Hiramatsu 	/* TODO: Ensure the @path is initramfs/initrd image */
420a995e6bcSMasami Hiramatsu 	if (fstat(fd, &stat) < 0) {
421e8ba0b2bSZhen Lei 		ret = -errno;
422a995e6bcSMasami Hiramatsu 		pr_err("Failed to get the size of %s\n", path);
42388426044SYunfeng Ye 		goto out;
424950313ebSMasami Hiramatsu 	}
425e1cef2d4SMasami Hiramatsu 
426e1cef2d4SMasami Hiramatsu 	/* To align up the total size to BOOTCONFIG_ALIGN, get padding size */
427e1cef2d4SMasami Hiramatsu 	total_size = stat.st_size + size + sizeof(u32) * 2 + BOOTCONFIG_MAGIC_LEN;
428e1cef2d4SMasami Hiramatsu 	pad = ((total_size + BOOTCONFIG_ALIGN - 1) & (~BOOTCONFIG_ALIGN_MASK)) - total_size;
429e1cef2d4SMasami Hiramatsu 	size += pad;
430e1cef2d4SMasami Hiramatsu 
431e1cef2d4SMasami Hiramatsu 	/* Add a footer */
432e1cef2d4SMasami Hiramatsu 	p = data + size;
433e8684358SMasami Hiramatsu 	*(u32 *)p = htole32(size);
434e1cef2d4SMasami Hiramatsu 	p += sizeof(u32);
435e1cef2d4SMasami Hiramatsu 
436e8684358SMasami Hiramatsu 	*(u32 *)p = htole32(csum);
437e1cef2d4SMasami Hiramatsu 	p += sizeof(u32);
438e1cef2d4SMasami Hiramatsu 
439e1cef2d4SMasami Hiramatsu 	memcpy(p, BOOTCONFIG_MAGIC, BOOTCONFIG_MAGIC_LEN);
440e1cef2d4SMasami Hiramatsu 	p += BOOTCONFIG_MAGIC_LEN;
441e1cef2d4SMasami Hiramatsu 
442e1cef2d4SMasami Hiramatsu 	total_size = p - data;
443e1cef2d4SMasami Hiramatsu 
444e1cef2d4SMasami Hiramatsu 	ret = write(fd, data, total_size);
445e1cef2d4SMasami Hiramatsu 	if (ret < total_size) {
446a995e6bcSMasami Hiramatsu 		if (ret < 0)
447a995e6bcSMasami Hiramatsu 			ret = -errno;
448a995e6bcSMasami Hiramatsu 		pr_err("Failed to apply a boot config: %d\n", ret);
449e1cef2d4SMasami Hiramatsu 		if (ret >= 0)
450a995e6bcSMasami Hiramatsu 			goto out_rollback;
451e1cef2d4SMasami Hiramatsu 	} else
4529d82ccdaSSteven Rostedt (VMware) 		ret = 0;
453e1cef2d4SMasami Hiramatsu 
45488426044SYunfeng Ye out:
455950313ebSMasami Hiramatsu 	close(fd);
456950313ebSMasami Hiramatsu 	free(data);
457950313ebSMasami Hiramatsu 
45888426044SYunfeng Ye 	return ret;
459a995e6bcSMasami Hiramatsu 
460a995e6bcSMasami Hiramatsu out_rollback:
461a995e6bcSMasami Hiramatsu 	/* Map the partial write to -ENOSPC */
462a995e6bcSMasami Hiramatsu 	if (ret >= 0)
463a995e6bcSMasami Hiramatsu 		ret = -ENOSPC;
464a995e6bcSMasami Hiramatsu 	if (ftruncate(fd, stat.st_size) < 0) {
465a995e6bcSMasami Hiramatsu 		ret = -errno;
466a995e6bcSMasami Hiramatsu 		pr_err("Failed to rollback the write error: %d\n", ret);
467a995e6bcSMasami Hiramatsu 		pr_err("The initrd %s may be corrupted. Recommend to rebuild.\n", path);
468a995e6bcSMasami Hiramatsu 	}
469a995e6bcSMasami Hiramatsu 	goto out;
470950313ebSMasami Hiramatsu }
471950313ebSMasami Hiramatsu 
472483ce670SMasami Hiramatsu static int usage(void)
473950313ebSMasami Hiramatsu {
474950313ebSMasami Hiramatsu 	printf("Usage: bootconfig [OPTIONS] <INITRD>\n"
475d052e1c6SMasami Hiramatsu 		"Or     bootconfig <CONFIG>\n"
476950313ebSMasami Hiramatsu 		" Apply, delete or show boot config to initrd.\n"
477950313ebSMasami Hiramatsu 		" Options:\n"
478950313ebSMasami Hiramatsu 		"		-a <config>: Apply boot config to initrd\n"
479e4f70b7bSMasami Hiramatsu 		"		-d : Delete boot config file from initrd\n"
480e4f70b7bSMasami Hiramatsu 		"		-l : list boot config in initrd or file\n\n"
481d052e1c6SMasami Hiramatsu 		" If no option is given, show the bootconfig in the given file.\n");
482950313ebSMasami Hiramatsu 	return -1;
483950313ebSMasami Hiramatsu }
484950313ebSMasami Hiramatsu 
485950313ebSMasami Hiramatsu int main(int argc, char **argv)
486950313ebSMasami Hiramatsu {
487950313ebSMasami Hiramatsu 	char *path = NULL;
488950313ebSMasami Hiramatsu 	char *apply = NULL;
489e4f70b7bSMasami Hiramatsu 	bool delete = false, list = false;
490950313ebSMasami Hiramatsu 	int opt;
491950313ebSMasami Hiramatsu 
492e4f70b7bSMasami Hiramatsu 	while ((opt = getopt(argc, argv, "hda:l")) != -1) {
493950313ebSMasami Hiramatsu 		switch (opt) {
494950313ebSMasami Hiramatsu 		case 'd':
495950313ebSMasami Hiramatsu 			delete = true;
496950313ebSMasami Hiramatsu 			break;
497950313ebSMasami Hiramatsu 		case 'a':
498950313ebSMasami Hiramatsu 			apply = optarg;
499950313ebSMasami Hiramatsu 			break;
500e4f70b7bSMasami Hiramatsu 		case 'l':
501e4f70b7bSMasami Hiramatsu 			list = true;
502e4f70b7bSMasami Hiramatsu 			break;
503950313ebSMasami Hiramatsu 		case 'h':
504950313ebSMasami Hiramatsu 		default:
505950313ebSMasami Hiramatsu 			return usage();
506950313ebSMasami Hiramatsu 		}
507950313ebSMasami Hiramatsu 	}
508950313ebSMasami Hiramatsu 
509e4f70b7bSMasami Hiramatsu 	if ((apply && delete) || (delete && list) || (apply && list)) {
510e4f70b7bSMasami Hiramatsu 		pr_err("Error: You can give one of -a, -d or -l at once.\n");
511950313ebSMasami Hiramatsu 		return usage();
512950313ebSMasami Hiramatsu 	}
513950313ebSMasami Hiramatsu 
514950313ebSMasami Hiramatsu 	if (optind >= argc) {
51597378001SMasami Hiramatsu 		pr_err("Error: No initrd is specified.\n");
516950313ebSMasami Hiramatsu 		return usage();
517950313ebSMasami Hiramatsu 	}
518950313ebSMasami Hiramatsu 
519950313ebSMasami Hiramatsu 	path = argv[optind];
520950313ebSMasami Hiramatsu 
521950313ebSMasami Hiramatsu 	if (apply)
522950313ebSMasami Hiramatsu 		return apply_xbc(path, apply);
523950313ebSMasami Hiramatsu 	else if (delete)
524950313ebSMasami Hiramatsu 		return delete_xbc(path);
525950313ebSMasami Hiramatsu 
526e4f70b7bSMasami Hiramatsu 	return show_xbc(path, list);
527950313ebSMasami Hiramatsu }
528