xref: /openbmc/linux/tools/bootconfig/main.c (revision 160321b2602ff8e42ea77a09f2e3f1b35e3acfaf)
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 
18*160321b2SMasami Hiramatsu #define pr_err(fmt, ...) fprintf(stderr, fmt, ##__VA_ARGS__)
19*160321b2SMasami Hiramatsu 
20e4f70b7bSMasami Hiramatsu static int xbc_show_value(struct xbc_node *node, bool semicolon)
21950313ebSMasami Hiramatsu {
22e4f70b7bSMasami Hiramatsu 	const char *val, *eol;
23272da327SMasami Hiramatsu 	char q;
24950313ebSMasami Hiramatsu 	int i = 0;
25950313ebSMasami Hiramatsu 
26e4f70b7bSMasami Hiramatsu 	eol = semicolon ? ";\n" : "\n";
27950313ebSMasami Hiramatsu 	xbc_array_for_each_value(node, val) {
28272da327SMasami Hiramatsu 		if (strchr(val, '"'))
29272da327SMasami Hiramatsu 			q = '\'';
30272da327SMasami Hiramatsu 		else
31272da327SMasami Hiramatsu 			q = '"';
32ca24306dSMasami Hiramatsu 		printf("%c%s%c%s", q, val, q, xbc_node_is_array(node) ? ", " : eol);
33950313ebSMasami Hiramatsu 		i++;
34950313ebSMasami Hiramatsu 	}
35950313ebSMasami Hiramatsu 	return i;
36950313ebSMasami Hiramatsu }
37950313ebSMasami Hiramatsu 
38950313ebSMasami Hiramatsu static void xbc_show_compact_tree(void)
39950313ebSMasami Hiramatsu {
40e5efaeb8SMasami Hiramatsu 	struct xbc_node *node, *cnode = NULL, *vnode;
41950313ebSMasami Hiramatsu 	int depth = 0, i;
42950313ebSMasami Hiramatsu 
43950313ebSMasami Hiramatsu 	node = xbc_root_node();
44950313ebSMasami Hiramatsu 	while (node && xbc_node_is_key(node)) {
45950313ebSMasami Hiramatsu 		for (i = 0; i < depth; i++)
46950313ebSMasami Hiramatsu 			printf("\t");
47e5efaeb8SMasami Hiramatsu 		if (!cnode)
48950313ebSMasami Hiramatsu 			cnode = xbc_node_get_child(node);
49950313ebSMasami Hiramatsu 		while (cnode && xbc_node_is_key(cnode) && !cnode->next) {
50e5efaeb8SMasami Hiramatsu 			vnode = xbc_node_get_child(cnode);
51e5efaeb8SMasami Hiramatsu 			/*
52e5efaeb8SMasami Hiramatsu 			 * If @cnode has value and subkeys, this
53e5efaeb8SMasami Hiramatsu 			 * should show it as below.
54e5efaeb8SMasami Hiramatsu 			 *
55e5efaeb8SMasami Hiramatsu 			 * key(@node) {
56e5efaeb8SMasami Hiramatsu 			 *      key(@cnode) = value;
57e5efaeb8SMasami Hiramatsu 			 *      key(@cnode) {
58e5efaeb8SMasami Hiramatsu 			 *          subkeys;
59e5efaeb8SMasami Hiramatsu 			 *      }
60e5efaeb8SMasami Hiramatsu 			 * }
61e5efaeb8SMasami Hiramatsu 			 */
62e5efaeb8SMasami Hiramatsu 			if (vnode && xbc_node_is_value(vnode) && vnode->next)
63e5efaeb8SMasami Hiramatsu 				break;
64950313ebSMasami Hiramatsu 			printf("%s.", xbc_node_get_data(node));
65950313ebSMasami Hiramatsu 			node = cnode;
66e5efaeb8SMasami Hiramatsu 			cnode = vnode;
67950313ebSMasami Hiramatsu 		}
68950313ebSMasami Hiramatsu 		if (cnode && xbc_node_is_key(cnode)) {
69950313ebSMasami Hiramatsu 			printf("%s {\n", xbc_node_get_data(node));
70950313ebSMasami Hiramatsu 			depth++;
71950313ebSMasami Hiramatsu 			node = cnode;
72e5efaeb8SMasami Hiramatsu 			cnode = NULL;
73950313ebSMasami Hiramatsu 			continue;
74950313ebSMasami Hiramatsu 		} else if (cnode && xbc_node_is_value(cnode)) {
75950313ebSMasami Hiramatsu 			printf("%s = ", xbc_node_get_data(node));
76e4f70b7bSMasami Hiramatsu 			xbc_show_value(cnode, true);
77e5efaeb8SMasami Hiramatsu 			/*
78e5efaeb8SMasami Hiramatsu 			 * If @node has value and subkeys, continue
79e5efaeb8SMasami Hiramatsu 			 * looping on subkeys with same node.
80e5efaeb8SMasami Hiramatsu 			 */
81e5efaeb8SMasami Hiramatsu 			if (cnode->next) {
82e5efaeb8SMasami Hiramatsu 				cnode = xbc_node_get_next(cnode);
83e5efaeb8SMasami Hiramatsu 				continue;
84e5efaeb8SMasami Hiramatsu 			}
85950313ebSMasami Hiramatsu 		} else {
86950313ebSMasami Hiramatsu 			printf("%s;\n", xbc_node_get_data(node));
87950313ebSMasami Hiramatsu 		}
88e5efaeb8SMasami Hiramatsu 		cnode = NULL;
89950313ebSMasami Hiramatsu 
90950313ebSMasami Hiramatsu 		if (node->next) {
91950313ebSMasami Hiramatsu 			node = xbc_node_get_next(node);
92950313ebSMasami Hiramatsu 			continue;
93950313ebSMasami Hiramatsu 		}
94950313ebSMasami Hiramatsu 		while (!node->next) {
95950313ebSMasami Hiramatsu 			node = xbc_node_get_parent(node);
96950313ebSMasami Hiramatsu 			if (!node)
97950313ebSMasami Hiramatsu 				return;
98950313ebSMasami Hiramatsu 			if (!xbc_node_get_child(node)->next)
99950313ebSMasami Hiramatsu 				continue;
100e5efaeb8SMasami Hiramatsu 			if (depth) {
101950313ebSMasami Hiramatsu 				depth--;
102950313ebSMasami Hiramatsu 				for (i = 0; i < depth; i++)
103950313ebSMasami Hiramatsu 					printf("\t");
104950313ebSMasami Hiramatsu 				printf("}\n");
105950313ebSMasami Hiramatsu 			}
106e5efaeb8SMasami Hiramatsu 		}
107950313ebSMasami Hiramatsu 		node = xbc_node_get_next(node);
108950313ebSMasami Hiramatsu 	}
109950313ebSMasami Hiramatsu }
110950313ebSMasami Hiramatsu 
111e4f70b7bSMasami Hiramatsu static void xbc_show_list(void)
112e4f70b7bSMasami Hiramatsu {
113e4f70b7bSMasami Hiramatsu 	char key[XBC_KEYLEN_MAX];
114e4f70b7bSMasami Hiramatsu 	struct xbc_node *leaf;
115e4f70b7bSMasami Hiramatsu 	const char *val;
116903bd067SJulio Faracco 	int ret;
117e4f70b7bSMasami Hiramatsu 
118e4f70b7bSMasami Hiramatsu 	xbc_for_each_key_value(leaf, val) {
119903bd067SJulio Faracco 		ret = xbc_node_compose_key(leaf, key, XBC_KEYLEN_MAX);
120903bd067SJulio Faracco 		if (ret < 0) {
121e5efaeb8SMasami Hiramatsu 			fprintf(stderr, "Failed to compose key %d\n", ret);
122e4f70b7bSMasami Hiramatsu 			break;
123e5efaeb8SMasami Hiramatsu 		}
124e4f70b7bSMasami Hiramatsu 		printf("%s = ", key);
125e4f70b7bSMasami Hiramatsu 		if (!val || val[0] == '\0') {
126e4f70b7bSMasami Hiramatsu 			printf("\"\"\n");
127e4f70b7bSMasami Hiramatsu 			continue;
128e4f70b7bSMasami Hiramatsu 		}
129e4f70b7bSMasami Hiramatsu 		xbc_show_value(xbc_node_get_child(leaf), false);
130e4f70b7bSMasami Hiramatsu 	}
131e4f70b7bSMasami Hiramatsu }
132e4f70b7bSMasami Hiramatsu 
133950313ebSMasami Hiramatsu #define PAGE_SIZE	4096
134950313ebSMasami Hiramatsu 
135483ce670SMasami Hiramatsu static int load_xbc_fd(int fd, char **buf, int size)
136950313ebSMasami Hiramatsu {
137950313ebSMasami Hiramatsu 	int ret;
138950313ebSMasami Hiramatsu 
139950313ebSMasami Hiramatsu 	*buf = malloc(size + 1);
140950313ebSMasami Hiramatsu 	if (!*buf)
141950313ebSMasami Hiramatsu 		return -ENOMEM;
142950313ebSMasami Hiramatsu 
143950313ebSMasami Hiramatsu 	ret = read(fd, *buf, size);
144950313ebSMasami Hiramatsu 	if (ret < 0)
145950313ebSMasami Hiramatsu 		return -errno;
146950313ebSMasami Hiramatsu 	(*buf)[size] = '\0';
147950313ebSMasami Hiramatsu 
148950313ebSMasami Hiramatsu 	return ret;
149950313ebSMasami Hiramatsu }
150950313ebSMasami Hiramatsu 
151950313ebSMasami Hiramatsu /* Return the read size or -errno */
152483ce670SMasami Hiramatsu static int load_xbc_file(const char *path, char **buf)
153950313ebSMasami Hiramatsu {
154950313ebSMasami Hiramatsu 	struct stat stat;
155950313ebSMasami Hiramatsu 	int fd, ret;
156950313ebSMasami Hiramatsu 
157950313ebSMasami Hiramatsu 	fd = open(path, O_RDONLY);
158950313ebSMasami Hiramatsu 	if (fd < 0)
159950313ebSMasami Hiramatsu 		return -errno;
160950313ebSMasami Hiramatsu 	ret = fstat(fd, &stat);
161950313ebSMasami Hiramatsu 	if (ret < 0)
162950313ebSMasami Hiramatsu 		return -errno;
163950313ebSMasami Hiramatsu 
164950313ebSMasami Hiramatsu 	ret = load_xbc_fd(fd, buf, stat.st_size);
165950313ebSMasami Hiramatsu 
166950313ebSMasami Hiramatsu 	close(fd);
167950313ebSMasami Hiramatsu 
168950313ebSMasami Hiramatsu 	return ret;
169950313ebSMasami Hiramatsu }
170950313ebSMasami Hiramatsu 
171a61ea637SMasami Hiramatsu static int pr_errno(const char *msg, int err)
172a61ea637SMasami Hiramatsu {
173a61ea637SMasami Hiramatsu 	pr_err("%s: %d\n", msg, err);
174a61ea637SMasami Hiramatsu 	return err;
175a61ea637SMasami Hiramatsu }
176a61ea637SMasami Hiramatsu 
177483ce670SMasami Hiramatsu static int load_xbc_from_initrd(int fd, char **buf)
178950313ebSMasami Hiramatsu {
179950313ebSMasami Hiramatsu 	struct stat stat;
180950313ebSMasami Hiramatsu 	int ret;
181950313ebSMasami Hiramatsu 	u32 size = 0, csum = 0, rcsum;
18285c46b78SMasami Hiramatsu 	char magic[BOOTCONFIG_MAGIC_LEN];
18389b74cacSMasami Hiramatsu 	const char *msg;
184950313ebSMasami Hiramatsu 
185950313ebSMasami Hiramatsu 	ret = fstat(fd, &stat);
186950313ebSMasami Hiramatsu 	if (ret < 0)
187950313ebSMasami Hiramatsu 		return -errno;
188950313ebSMasami Hiramatsu 
18985c46b78SMasami Hiramatsu 	if (stat.st_size < 8 + BOOTCONFIG_MAGIC_LEN)
190950313ebSMasami Hiramatsu 		return 0;
191950313ebSMasami Hiramatsu 
192a61ea637SMasami Hiramatsu 	if (lseek(fd, -BOOTCONFIG_MAGIC_LEN, SEEK_END) < 0)
193a61ea637SMasami Hiramatsu 		return pr_errno("Failed to lseek for magic", -errno);
194a61ea637SMasami Hiramatsu 
19585c46b78SMasami Hiramatsu 	if (read(fd, magic, BOOTCONFIG_MAGIC_LEN) < 0)
196a61ea637SMasami Hiramatsu 		return pr_errno("Failed to read", -errno);
197a61ea637SMasami Hiramatsu 
19885c46b78SMasami Hiramatsu 	/* Check the bootconfig magic bytes */
19985c46b78SMasami Hiramatsu 	if (memcmp(magic, BOOTCONFIG_MAGIC, BOOTCONFIG_MAGIC_LEN) != 0)
20085c46b78SMasami Hiramatsu 		return 0;
20185c46b78SMasami Hiramatsu 
202a61ea637SMasami Hiramatsu 	if (lseek(fd, -(8 + BOOTCONFIG_MAGIC_LEN), SEEK_END) < 0)
203a61ea637SMasami Hiramatsu 		return pr_errno("Failed to lseek for size", -errno);
204950313ebSMasami Hiramatsu 
205950313ebSMasami Hiramatsu 	if (read(fd, &size, sizeof(u32)) < 0)
206a61ea637SMasami Hiramatsu 		return pr_errno("Failed to read size", -errno);
207e8684358SMasami Hiramatsu 	size = le32toh(size);
208950313ebSMasami Hiramatsu 
209950313ebSMasami Hiramatsu 	if (read(fd, &csum, sizeof(u32)) < 0)
210a61ea637SMasami Hiramatsu 		return pr_errno("Failed to read checksum", -errno);
211e8684358SMasami Hiramatsu 	csum = le32toh(csum);
212950313ebSMasami Hiramatsu 
21385c46b78SMasami Hiramatsu 	/* Wrong size error  */
21485c46b78SMasami Hiramatsu 	if (stat.st_size < size + 8 + BOOTCONFIG_MAGIC_LEN) {
21585c46b78SMasami Hiramatsu 		pr_err("bootconfig size is too big\n");
21685c46b78SMasami Hiramatsu 		return -E2BIG;
21785c46b78SMasami Hiramatsu 	}
218950313ebSMasami Hiramatsu 
21985c46b78SMasami Hiramatsu 	if (lseek(fd, stat.st_size - (size + 8 + BOOTCONFIG_MAGIC_LEN),
220a61ea637SMasami Hiramatsu 		  SEEK_SET) < 0)
221a61ea637SMasami Hiramatsu 		return pr_errno("Failed to lseek", -errno);
222950313ebSMasami Hiramatsu 
223950313ebSMasami Hiramatsu 	ret = load_xbc_fd(fd, buf, size);
224950313ebSMasami Hiramatsu 	if (ret < 0)
225950313ebSMasami Hiramatsu 		return ret;
226950313ebSMasami Hiramatsu 
22785c46b78SMasami Hiramatsu 	/* Wrong Checksum */
22899f4f5d6SMasami Hiramatsu 	rcsum = xbc_calc_checksum(*buf, size);
229950313ebSMasami Hiramatsu 	if (csum != rcsum) {
23097378001SMasami Hiramatsu 		pr_err("checksum error: %d != %d\n", csum, rcsum);
23185c46b78SMasami Hiramatsu 		return -EINVAL;
232950313ebSMasami Hiramatsu 	}
233950313ebSMasami Hiramatsu 
234bdac5c2bSMasami Hiramatsu 	ret = xbc_init(*buf, size, &msg, NULL);
23585c46b78SMasami Hiramatsu 	/* Wrong data */
23689b74cacSMasami Hiramatsu 	if (ret < 0) {
23789b74cacSMasami Hiramatsu 		pr_err("parse error: %s.\n", msg);
23885c46b78SMasami Hiramatsu 		return ret;
23989b74cacSMasami Hiramatsu 	}
240950313ebSMasami Hiramatsu 
241950313ebSMasami Hiramatsu 	return size;
242950313ebSMasami Hiramatsu }
243950313ebSMasami Hiramatsu 
244d052e1c6SMasami Hiramatsu static void show_xbc_error(const char *data, const char *msg, int pos)
245d052e1c6SMasami Hiramatsu {
246d052e1c6SMasami Hiramatsu 	int lin = 1, col, i;
247d052e1c6SMasami Hiramatsu 
248d052e1c6SMasami Hiramatsu 	if (pos < 0) {
249d052e1c6SMasami Hiramatsu 		pr_err("Error: %s.\n", msg);
250d052e1c6SMasami Hiramatsu 		return;
251d052e1c6SMasami Hiramatsu 	}
252d052e1c6SMasami Hiramatsu 
253d052e1c6SMasami Hiramatsu 	/* Note that pos starts from 0 but lin and col should start from 1. */
254d052e1c6SMasami Hiramatsu 	col = pos + 1;
255d052e1c6SMasami Hiramatsu 	for (i = 0; i < pos; i++) {
256d052e1c6SMasami Hiramatsu 		if (data[i] == '\n') {
257d052e1c6SMasami Hiramatsu 			lin++;
258d052e1c6SMasami Hiramatsu 			col = pos - i;
259d052e1c6SMasami Hiramatsu 		}
260d052e1c6SMasami Hiramatsu 	}
261d052e1c6SMasami Hiramatsu 	pr_err("Parse Error: %s at %d:%d\n", msg, lin, col);
262d052e1c6SMasami Hiramatsu 
263d052e1c6SMasami Hiramatsu }
264d052e1c6SMasami Hiramatsu 
265d052e1c6SMasami Hiramatsu static int init_xbc_with_error(char *buf, int len)
266d052e1c6SMasami Hiramatsu {
267d052e1c6SMasami Hiramatsu 	char *copy = strdup(buf);
268d052e1c6SMasami Hiramatsu 	const char *msg;
269d052e1c6SMasami Hiramatsu 	int ret, pos;
270d052e1c6SMasami Hiramatsu 
271d052e1c6SMasami Hiramatsu 	if (!copy)
272d052e1c6SMasami Hiramatsu 		return -ENOMEM;
273d052e1c6SMasami Hiramatsu 
274bdac5c2bSMasami Hiramatsu 	ret = xbc_init(buf, len, &msg, &pos);
275d052e1c6SMasami Hiramatsu 	if (ret < 0)
276d052e1c6SMasami Hiramatsu 		show_xbc_error(copy, msg, pos);
277d052e1c6SMasami Hiramatsu 	free(copy);
278d052e1c6SMasami Hiramatsu 
279d052e1c6SMasami Hiramatsu 	return ret;
280d052e1c6SMasami Hiramatsu }
281d052e1c6SMasami Hiramatsu 
282483ce670SMasami Hiramatsu static int show_xbc(const char *path, bool list)
283950313ebSMasami Hiramatsu {
284950313ebSMasami Hiramatsu 	int ret, fd;
285950313ebSMasami Hiramatsu 	char *buf = NULL;
286d052e1c6SMasami Hiramatsu 	struct stat st;
287d052e1c6SMasami Hiramatsu 
288d052e1c6SMasami Hiramatsu 	ret = stat(path, &st);
289d052e1c6SMasami Hiramatsu 	if (ret < 0) {
290a61ea637SMasami Hiramatsu 		ret = -errno;
291a61ea637SMasami Hiramatsu 		pr_err("Failed to stat %s: %d\n", path, ret);
292a61ea637SMasami Hiramatsu 		return ret;
293d052e1c6SMasami Hiramatsu 	}
294950313ebSMasami Hiramatsu 
295950313ebSMasami Hiramatsu 	fd = open(path, O_RDONLY);
296950313ebSMasami Hiramatsu 	if (fd < 0) {
297a61ea637SMasami Hiramatsu 		ret = -errno;
298a61ea637SMasami Hiramatsu 		pr_err("Failed to open initrd %s: %d\n", path, ret);
299a61ea637SMasami Hiramatsu 		return ret;
300950313ebSMasami Hiramatsu 	}
301950313ebSMasami Hiramatsu 
302950313ebSMasami Hiramatsu 	ret = load_xbc_from_initrd(fd, &buf);
303d052e1c6SMasami Hiramatsu 	close(fd);
304f91cb5b7SMasami Hiramatsu 	if (ret < 0) {
30597378001SMasami Hiramatsu 		pr_err("Failed to load a boot config from initrd: %d\n", ret);
306f91cb5b7SMasami Hiramatsu 		goto out;
307f91cb5b7SMasami Hiramatsu 	}
308d052e1c6SMasami Hiramatsu 	/* Assume a bootconfig file if it is enough small */
309d052e1c6SMasami Hiramatsu 	if (ret == 0 && st.st_size <= XBC_DATA_MAX) {
310d052e1c6SMasami Hiramatsu 		ret = load_xbc_file(path, &buf);
311d052e1c6SMasami Hiramatsu 		if (ret < 0) {
312d052e1c6SMasami Hiramatsu 			pr_err("Failed to load a boot config: %d\n", ret);
313d052e1c6SMasami Hiramatsu 			goto out;
314d052e1c6SMasami Hiramatsu 		}
315d052e1c6SMasami Hiramatsu 		if (init_xbc_with_error(buf, ret) < 0)
316d052e1c6SMasami Hiramatsu 			goto out;
317d052e1c6SMasami Hiramatsu 	}
318e4f70b7bSMasami Hiramatsu 	if (list)
319e4f70b7bSMasami Hiramatsu 		xbc_show_list();
320e4f70b7bSMasami Hiramatsu 	else
321950313ebSMasami Hiramatsu 		xbc_show_compact_tree();
322f91cb5b7SMasami Hiramatsu 	ret = 0;
323f91cb5b7SMasami Hiramatsu out:
324950313ebSMasami Hiramatsu 	free(buf);
325950313ebSMasami Hiramatsu 
326950313ebSMasami Hiramatsu 	return ret;
327950313ebSMasami Hiramatsu }
328950313ebSMasami Hiramatsu 
329483ce670SMasami Hiramatsu static int delete_xbc(const char *path)
330950313ebSMasami Hiramatsu {
331950313ebSMasami Hiramatsu 	struct stat stat;
332950313ebSMasami Hiramatsu 	int ret = 0, fd, size;
333950313ebSMasami Hiramatsu 	char *buf = NULL;
334950313ebSMasami Hiramatsu 
335950313ebSMasami Hiramatsu 	fd = open(path, O_RDWR);
336950313ebSMasami Hiramatsu 	if (fd < 0) {
337a61ea637SMasami Hiramatsu 		ret = -errno;
338a61ea637SMasami Hiramatsu 		pr_err("Failed to open initrd %s: %d\n", path, ret);
339a61ea637SMasami Hiramatsu 		return ret;
340950313ebSMasami Hiramatsu 	}
341950313ebSMasami Hiramatsu 
342950313ebSMasami Hiramatsu 	size = load_xbc_from_initrd(fd, &buf);
343950313ebSMasami Hiramatsu 	if (size < 0) {
344950313ebSMasami Hiramatsu 		ret = size;
34597378001SMasami Hiramatsu 		pr_err("Failed to load a boot config from initrd: %d\n", ret);
346950313ebSMasami Hiramatsu 	} else if (size > 0) {
347950313ebSMasami Hiramatsu 		ret = fstat(fd, &stat);
348950313ebSMasami Hiramatsu 		if (!ret)
34985c46b78SMasami Hiramatsu 			ret = ftruncate(fd, stat.st_size
35085c46b78SMasami Hiramatsu 					- size - 8 - BOOTCONFIG_MAGIC_LEN);
351950313ebSMasami Hiramatsu 		if (ret)
352950313ebSMasami Hiramatsu 			ret = -errno;
353950313ebSMasami Hiramatsu 	} /* Ignore if there is no boot config in initrd */
354950313ebSMasami Hiramatsu 
355950313ebSMasami Hiramatsu 	close(fd);
356950313ebSMasami Hiramatsu 	free(buf);
357950313ebSMasami Hiramatsu 
358950313ebSMasami Hiramatsu 	return ret;
359950313ebSMasami Hiramatsu }
360950313ebSMasami Hiramatsu 
361483ce670SMasami Hiramatsu static int apply_xbc(const char *path, const char *xbc_path)
362950313ebSMasami Hiramatsu {
363e1cef2d4SMasami Hiramatsu 	char *buf, *data, *p;
364e1cef2d4SMasami Hiramatsu 	size_t total_size;
365a995e6bcSMasami Hiramatsu 	struct stat stat;
36689b74cacSMasami Hiramatsu 	const char *msg;
367e1cef2d4SMasami Hiramatsu 	u32 size, csum;
368e1cef2d4SMasami Hiramatsu 	int pos, pad;
369e1cef2d4SMasami Hiramatsu 	int ret, fd;
370950313ebSMasami Hiramatsu 
371950313ebSMasami Hiramatsu 	ret = load_xbc_file(xbc_path, &buf);
372950313ebSMasami Hiramatsu 	if (ret < 0) {
37397378001SMasami Hiramatsu 		pr_err("Failed to load %s : %d\n", xbc_path, ret);
374950313ebSMasami Hiramatsu 		return ret;
375950313ebSMasami Hiramatsu 	}
376950313ebSMasami Hiramatsu 	size = strlen(buf) + 1;
37799f4f5d6SMasami Hiramatsu 	csum = xbc_calc_checksum(buf, size);
378950313ebSMasami Hiramatsu 
379e1cef2d4SMasami Hiramatsu 	/* Backup the bootconfig data */
380e1cef2d4SMasami Hiramatsu 	data = calloc(size + BOOTCONFIG_ALIGN +
381e1cef2d4SMasami Hiramatsu 		      sizeof(u32) + sizeof(u32) + BOOTCONFIG_MAGIC_LEN, 1);
382950313ebSMasami Hiramatsu 	if (!data)
383950313ebSMasami Hiramatsu 		return -ENOMEM;
384e1cef2d4SMasami Hiramatsu 	memcpy(data, buf, size);
385950313ebSMasami Hiramatsu 
386950313ebSMasami Hiramatsu 	/* Check the data format */
387bdac5c2bSMasami Hiramatsu 	ret = xbc_init(buf, size, &msg, &pos);
388950313ebSMasami Hiramatsu 	if (ret < 0) {
38989b74cacSMasami Hiramatsu 		show_xbc_error(data, msg, pos);
390950313ebSMasami Hiramatsu 		free(data);
391950313ebSMasami Hiramatsu 		free(buf);
39289b74cacSMasami Hiramatsu 
393950313ebSMasami Hiramatsu 		return ret;
394950313ebSMasami Hiramatsu 	}
395950313ebSMasami Hiramatsu 	printf("Apply %s to %s\n", xbc_path, path);
396e306220cSMasami Hiramatsu 	xbc_get_info(&ret, NULL);
3970f0d0a77SMasami Hiramatsu 	printf("\tNumber of nodes: %d\n", ret);
398950313ebSMasami Hiramatsu 	printf("\tSize: %u bytes\n", (unsigned int)size);
399950313ebSMasami Hiramatsu 	printf("\tChecksum: %d\n", (unsigned int)csum);
400950313ebSMasami Hiramatsu 
401950313ebSMasami Hiramatsu 	/* TODO: Check the options by schema */
402115d4d08SMasami Hiramatsu 	xbc_exit();
403950313ebSMasami Hiramatsu 	free(buf);
404950313ebSMasami Hiramatsu 
405950313ebSMasami Hiramatsu 	/* Remove old boot config if exists */
406950313ebSMasami Hiramatsu 	ret = delete_xbc(path);
407950313ebSMasami Hiramatsu 	if (ret < 0) {
40897378001SMasami Hiramatsu 		pr_err("Failed to delete previous boot config: %d\n", ret);
40988426044SYunfeng Ye 		free(data);
410950313ebSMasami Hiramatsu 		return ret;
411950313ebSMasami Hiramatsu 	}
412950313ebSMasami Hiramatsu 
413950313ebSMasami Hiramatsu 	/* Apply new one */
414950313ebSMasami Hiramatsu 	fd = open(path, O_RDWR | O_APPEND);
415950313ebSMasami Hiramatsu 	if (fd < 0) {
416a61ea637SMasami Hiramatsu 		ret = -errno;
417a61ea637SMasami Hiramatsu 		pr_err("Failed to open %s: %d\n", path, ret);
41888426044SYunfeng Ye 		free(data);
419a61ea637SMasami Hiramatsu 		return ret;
420950313ebSMasami Hiramatsu 	}
421950313ebSMasami Hiramatsu 	/* TODO: Ensure the @path is initramfs/initrd image */
422a995e6bcSMasami Hiramatsu 	if (fstat(fd, &stat) < 0) {
423e8ba0b2bSZhen Lei 		ret = -errno;
424a995e6bcSMasami Hiramatsu 		pr_err("Failed to get the size of %s\n", path);
42588426044SYunfeng Ye 		goto out;
426950313ebSMasami Hiramatsu 	}
427e1cef2d4SMasami Hiramatsu 
428e1cef2d4SMasami Hiramatsu 	/* To align up the total size to BOOTCONFIG_ALIGN, get padding size */
429e1cef2d4SMasami Hiramatsu 	total_size = stat.st_size + size + sizeof(u32) * 2 + BOOTCONFIG_MAGIC_LEN;
430e1cef2d4SMasami Hiramatsu 	pad = ((total_size + BOOTCONFIG_ALIGN - 1) & (~BOOTCONFIG_ALIGN_MASK)) - total_size;
431e1cef2d4SMasami Hiramatsu 	size += pad;
432e1cef2d4SMasami Hiramatsu 
433e1cef2d4SMasami Hiramatsu 	/* Add a footer */
434e1cef2d4SMasami Hiramatsu 	p = data + size;
435e8684358SMasami Hiramatsu 	*(u32 *)p = htole32(size);
436e1cef2d4SMasami Hiramatsu 	p += sizeof(u32);
437e1cef2d4SMasami Hiramatsu 
438e8684358SMasami Hiramatsu 	*(u32 *)p = htole32(csum);
439e1cef2d4SMasami Hiramatsu 	p += sizeof(u32);
440e1cef2d4SMasami Hiramatsu 
441e1cef2d4SMasami Hiramatsu 	memcpy(p, BOOTCONFIG_MAGIC, BOOTCONFIG_MAGIC_LEN);
442e1cef2d4SMasami Hiramatsu 	p += BOOTCONFIG_MAGIC_LEN;
443e1cef2d4SMasami Hiramatsu 
444e1cef2d4SMasami Hiramatsu 	total_size = p - data;
445e1cef2d4SMasami Hiramatsu 
446e1cef2d4SMasami Hiramatsu 	ret = write(fd, data, total_size);
447e1cef2d4SMasami Hiramatsu 	if (ret < total_size) {
448a995e6bcSMasami Hiramatsu 		if (ret < 0)
449a995e6bcSMasami Hiramatsu 			ret = -errno;
450a995e6bcSMasami Hiramatsu 		pr_err("Failed to apply a boot config: %d\n", ret);
451e1cef2d4SMasami Hiramatsu 		if (ret >= 0)
452a995e6bcSMasami Hiramatsu 			goto out_rollback;
453e1cef2d4SMasami Hiramatsu 	} else
4549d82ccdaSSteven Rostedt (VMware) 		ret = 0;
455e1cef2d4SMasami Hiramatsu 
45688426044SYunfeng Ye out:
457950313ebSMasami Hiramatsu 	close(fd);
458950313ebSMasami Hiramatsu 	free(data);
459950313ebSMasami Hiramatsu 
46088426044SYunfeng Ye 	return ret;
461a995e6bcSMasami Hiramatsu 
462a995e6bcSMasami Hiramatsu out_rollback:
463a995e6bcSMasami Hiramatsu 	/* Map the partial write to -ENOSPC */
464a995e6bcSMasami Hiramatsu 	if (ret >= 0)
465a995e6bcSMasami Hiramatsu 		ret = -ENOSPC;
466a995e6bcSMasami Hiramatsu 	if (ftruncate(fd, stat.st_size) < 0) {
467a995e6bcSMasami Hiramatsu 		ret = -errno;
468a995e6bcSMasami Hiramatsu 		pr_err("Failed to rollback the write error: %d\n", ret);
469a995e6bcSMasami Hiramatsu 		pr_err("The initrd %s may be corrupted. Recommend to rebuild.\n", path);
470a995e6bcSMasami Hiramatsu 	}
471a995e6bcSMasami Hiramatsu 	goto out;
472950313ebSMasami Hiramatsu }
473950313ebSMasami Hiramatsu 
474483ce670SMasami Hiramatsu static int usage(void)
475950313ebSMasami Hiramatsu {
476950313ebSMasami Hiramatsu 	printf("Usage: bootconfig [OPTIONS] <INITRD>\n"
477d052e1c6SMasami Hiramatsu 		"Or     bootconfig <CONFIG>\n"
478950313ebSMasami Hiramatsu 		" Apply, delete or show boot config to initrd.\n"
479950313ebSMasami Hiramatsu 		" Options:\n"
480950313ebSMasami Hiramatsu 		"		-a <config>: Apply boot config to initrd\n"
481e4f70b7bSMasami Hiramatsu 		"		-d : Delete boot config file from initrd\n"
482e4f70b7bSMasami Hiramatsu 		"		-l : list boot config in initrd or file\n\n"
483d052e1c6SMasami Hiramatsu 		" If no option is given, show the bootconfig in the given file.\n");
484950313ebSMasami Hiramatsu 	return -1;
485950313ebSMasami Hiramatsu }
486950313ebSMasami Hiramatsu 
487950313ebSMasami Hiramatsu int main(int argc, char **argv)
488950313ebSMasami Hiramatsu {
489950313ebSMasami Hiramatsu 	char *path = NULL;
490950313ebSMasami Hiramatsu 	char *apply = NULL;
491e4f70b7bSMasami Hiramatsu 	bool delete = false, list = false;
492950313ebSMasami Hiramatsu 	int opt;
493950313ebSMasami Hiramatsu 
494e4f70b7bSMasami Hiramatsu 	while ((opt = getopt(argc, argv, "hda:l")) != -1) {
495950313ebSMasami Hiramatsu 		switch (opt) {
496950313ebSMasami Hiramatsu 		case 'd':
497950313ebSMasami Hiramatsu 			delete = true;
498950313ebSMasami Hiramatsu 			break;
499950313ebSMasami Hiramatsu 		case 'a':
500950313ebSMasami Hiramatsu 			apply = optarg;
501950313ebSMasami Hiramatsu 			break;
502e4f70b7bSMasami Hiramatsu 		case 'l':
503e4f70b7bSMasami Hiramatsu 			list = true;
504e4f70b7bSMasami Hiramatsu 			break;
505950313ebSMasami Hiramatsu 		case 'h':
506950313ebSMasami Hiramatsu 		default:
507950313ebSMasami Hiramatsu 			return usage();
508950313ebSMasami Hiramatsu 		}
509950313ebSMasami Hiramatsu 	}
510950313ebSMasami Hiramatsu 
511e4f70b7bSMasami Hiramatsu 	if ((apply && delete) || (delete && list) || (apply && list)) {
512e4f70b7bSMasami Hiramatsu 		pr_err("Error: You can give one of -a, -d or -l at once.\n");
513950313ebSMasami Hiramatsu 		return usage();
514950313ebSMasami Hiramatsu 	}
515950313ebSMasami Hiramatsu 
516950313ebSMasami Hiramatsu 	if (optind >= argc) {
51797378001SMasami Hiramatsu 		pr_err("Error: No initrd is specified.\n");
518950313ebSMasami Hiramatsu 		return usage();
519950313ebSMasami Hiramatsu 	}
520950313ebSMasami Hiramatsu 
521950313ebSMasami Hiramatsu 	path = argv[optind];
522950313ebSMasami Hiramatsu 
523950313ebSMasami Hiramatsu 	if (apply)
524950313ebSMasami Hiramatsu 		return apply_xbc(path, apply);
525950313ebSMasami Hiramatsu 	else if (delete)
526950313ebSMasami Hiramatsu 		return delete_xbc(path);
527950313ebSMasami Hiramatsu 
528e4f70b7bSMasami Hiramatsu 	return show_xbc(path, list);
529950313ebSMasami Hiramatsu }
530