Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Boot config tool for initrd image
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <sys/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <sys/stat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <fcntl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <endian.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/bootconfig.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) static int xbc_show_value(struct xbc_node *node, bool semicolon)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 	const char *val, *eol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 	char q;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 	int i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 	eol = semicolon ? ";\n" : "\n";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 	xbc_array_for_each_value(node, val) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 		if (strchr(val, '"'))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 			q = '\'';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 			q = '"';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 		printf("%c%s%c%s", q, val, q, xbc_node_is_array(node) ? ", " : eol);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 		i++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	return i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) static void xbc_show_compact_tree(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	struct xbc_node *node, *cnode = NULL, *vnode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	int depth = 0, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	node = xbc_root_node();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	while (node && xbc_node_is_key(node)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 		for (i = 0; i < depth; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 			printf("\t");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 		if (!cnode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 			cnode = xbc_node_get_child(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 		while (cnode && xbc_node_is_key(cnode) && !cnode->next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 			vnode = xbc_node_get_child(cnode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 			 * If @cnode has value and subkeys, this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 			 * should show it as below.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 			 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 			 * key(@node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 			 *      key(@cnode) = value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 			 *      key(@cnode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 			 *          subkeys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 			 *      }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 			 * }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 			if (vnode && xbc_node_is_value(vnode) && vnode->next)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 			printf("%s.", xbc_node_get_data(node));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 			node = cnode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 			cnode = vnode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 		if (cnode && xbc_node_is_key(cnode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 			printf("%s {\n", xbc_node_get_data(node));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 			depth++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 			node = cnode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 			cnode = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 		} else if (cnode && xbc_node_is_value(cnode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 			printf("%s = ", xbc_node_get_data(node));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 			xbc_show_value(cnode, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 			 * If @node has value and subkeys, continue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 			 * looping on subkeys with same node.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 			if (cnode->next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 				cnode = xbc_node_get_next(cnode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 			printf("%s;\n", xbc_node_get_data(node));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 		cnode = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 		if (node->next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 			node = xbc_node_get_next(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 		while (!node->next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 			node = xbc_node_get_parent(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 			if (!node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 				return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 			if (!xbc_node_get_child(node)->next)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 			if (depth) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 				depth--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 				for (i = 0; i < depth; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 					printf("\t");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 				printf("}\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 		node = xbc_node_get_next(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) static void xbc_show_list(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	char key[XBC_KEYLEN_MAX];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	struct xbc_node *leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	const char *val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	xbc_for_each_key_value(leaf, val) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 		ret = xbc_node_compose_key(leaf, key, XBC_KEYLEN_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 		if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 			fprintf(stderr, "Failed to compose key %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 		printf("%s = ", key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 		if (!val || val[0] == '\0') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 			printf("\"\"\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		xbc_show_value(xbc_node_get_child(leaf), false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) #define PAGE_SIZE	4096
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) static int load_xbc_fd(int fd, char **buf, int size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	*buf = malloc(size + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	if (!*buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	ret = read(fd, *buf, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		return -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	(*buf)[size] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) /* Return the read size or -errno */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) static int load_xbc_file(const char *path, char **buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	struct stat stat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	int fd, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	fd = open(path, O_RDONLY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	if (fd < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 		return -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	ret = fstat(fd, &stat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 		return -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	ret = load_xbc_fd(fd, buf, stat.st_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	close(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) static int pr_errno(const char *msg, int err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	pr_err("%s: %d\n", msg, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) static int load_xbc_from_initrd(int fd, char **buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	struct stat stat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	u32 size = 0, csum = 0, rcsum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	char magic[BOOTCONFIG_MAGIC_LEN];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	const char *msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	ret = fstat(fd, &stat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 		return -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	if (stat.st_size < 8 + BOOTCONFIG_MAGIC_LEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	if (lseek(fd, -BOOTCONFIG_MAGIC_LEN, SEEK_END) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 		return pr_errno("Failed to lseek for magic", -errno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	if (read(fd, magic, BOOTCONFIG_MAGIC_LEN) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 		return pr_errno("Failed to read", -errno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	/* Check the bootconfig magic bytes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	if (memcmp(magic, BOOTCONFIG_MAGIC, BOOTCONFIG_MAGIC_LEN) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	if (lseek(fd, -(8 + BOOTCONFIG_MAGIC_LEN), SEEK_END) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 		return pr_errno("Failed to lseek for size", -errno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	if (read(fd, &size, sizeof(u32)) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 		return pr_errno("Failed to read size", -errno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	size = le32toh(size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	if (read(fd, &csum, sizeof(u32)) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 		return pr_errno("Failed to read checksum", -errno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	csum = le32toh(csum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	/* Wrong size error  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	if (stat.st_size < size + 8 + BOOTCONFIG_MAGIC_LEN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 		pr_err("bootconfig size is too big\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 		return -E2BIG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	if (lseek(fd, stat.st_size - (size + 8 + BOOTCONFIG_MAGIC_LEN),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 		  SEEK_SET) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 		return pr_errno("Failed to lseek", -errno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	ret = load_xbc_fd(fd, buf, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	/* Wrong Checksum */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	rcsum = xbc_calc_checksum(*buf, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	if (csum != rcsum) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 		pr_err("checksum error: %d != %d\n", csum, rcsum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	ret = xbc_init(*buf, &msg, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	/* Wrong data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 		pr_err("parse error: %s.\n", msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	return size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) static void show_xbc_error(const char *data, const char *msg, int pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	int lin = 1, col, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	if (pos < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 		pr_err("Error: %s.\n", msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	/* Note that pos starts from 0 but lin and col should start from 1. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	col = pos + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	for (i = 0; i < pos; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 		if (data[i] == '\n') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 			lin++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 			col = pos - i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	pr_err("Parse Error: %s at %d:%d\n", msg, lin, col);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) static int init_xbc_with_error(char *buf, int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	char *copy = strdup(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	const char *msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	int ret, pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	if (!copy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	ret = xbc_init(buf, &msg, &pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 		show_xbc_error(copy, msg, pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	free(copy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) static int show_xbc(const char *path, bool list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	int ret, fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	char *buf = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	struct stat st;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	ret = stat(path, &st);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 		ret = -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 		pr_err("Failed to stat %s: %d\n", path, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	fd = open(path, O_RDONLY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	if (fd < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 		ret = -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 		pr_err("Failed to open initrd %s: %d\n", path, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	ret = load_xbc_from_initrd(fd, &buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	close(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 		pr_err("Failed to load a boot config from initrd: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	/* Assume a bootconfig file if it is enough small */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	if (ret == 0 && st.st_size <= XBC_DATA_MAX) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 		ret = load_xbc_file(path, &buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 		if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 			pr_err("Failed to load a boot config: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 		if (init_xbc_with_error(buf, ret) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	if (list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 		xbc_show_list();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 		xbc_show_compact_tree();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	free(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) static int delete_xbc(const char *path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	struct stat stat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	int ret = 0, fd, size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	char *buf = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	fd = open(path, O_RDWR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	if (fd < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 		ret = -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 		pr_err("Failed to open initrd %s: %d\n", path, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	size = load_xbc_from_initrd(fd, &buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	if (size < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 		ret = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 		pr_err("Failed to load a boot config from initrd: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	} else if (size > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 		ret = fstat(fd, &stat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 		if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 			ret = ftruncate(fd, stat.st_size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 					- size - 8 - BOOTCONFIG_MAGIC_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 			ret = -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	} /* Ignore if there is no boot config in initrd */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	close(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	free(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) static int apply_xbc(const char *path, const char *xbc_path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	char *buf, *data, *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	size_t total_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	struct stat stat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	const char *msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	u32 size, csum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	int pos, pad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 	int ret, fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	ret = load_xbc_file(xbc_path, &buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 		pr_err("Failed to load %s : %d\n", xbc_path, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	size = strlen(buf) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 	csum = xbc_calc_checksum(buf, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 	/* Backup the bootconfig data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	data = calloc(size + BOOTCONFIG_ALIGN +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 		      sizeof(u32) + sizeof(u32) + BOOTCONFIG_MAGIC_LEN, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 	if (!data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	memcpy(data, buf, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	/* Check the data format */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 	ret = xbc_init(buf, &msg, &pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 		show_xbc_error(data, msg, pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 		free(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 		free(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 	printf("Apply %s to %s\n", xbc_path, path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 	printf("\tNumber of nodes: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	printf("\tSize: %u bytes\n", (unsigned int)size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 	printf("\tChecksum: %d\n", (unsigned int)csum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	/* TODO: Check the options by schema */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 	xbc_destroy_all();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 	free(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 	/* Remove old boot config if exists */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 	ret = delete_xbc(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 		pr_err("Failed to delete previous boot config: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 		free(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 	/* Apply new one */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 	fd = open(path, O_RDWR | O_APPEND);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 	if (fd < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 		ret = -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 		pr_err("Failed to open %s: %d\n", path, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 		free(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 	/* TODO: Ensure the @path is initramfs/initrd image */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 	if (fstat(fd, &stat) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 		ret = -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 		pr_err("Failed to get the size of %s\n", path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 	/* To align up the total size to BOOTCONFIG_ALIGN, get padding size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 	total_size = stat.st_size + size + sizeof(u32) * 2 + BOOTCONFIG_MAGIC_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 	pad = ((total_size + BOOTCONFIG_ALIGN - 1) & (~BOOTCONFIG_ALIGN_MASK)) - total_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 	size += pad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 	/* Add a footer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 	p = data + size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 	*(u32 *)p = htole32(size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 	p += sizeof(u32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 	*(u32 *)p = htole32(csum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 	p += sizeof(u32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 	memcpy(p, BOOTCONFIG_MAGIC, BOOTCONFIG_MAGIC_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 	p += BOOTCONFIG_MAGIC_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 	total_size = p - data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 	ret = write(fd, data, total_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 	if (ret < total_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 			ret = -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 		pr_err("Failed to apply a boot config: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 		if (ret >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 			goto out_rollback;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 		ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 	close(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 	free(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) out_rollback:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 	/* Map the partial write to -ENOSPC */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 	if (ret >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 		ret = -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 	if (ftruncate(fd, stat.st_size) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 		ret = -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 		pr_err("Failed to rollback the write error: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 		pr_err("The initrd %s may be corrupted. Recommend to rebuild.\n", path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 	goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) static int usage(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 	printf("Usage: bootconfig [OPTIONS] <INITRD>\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 		"Or     bootconfig <CONFIG>\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 		" Apply, delete or show boot config to initrd.\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 		" Options:\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 		"		-a <config>: Apply boot config to initrd\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 		"		-d : Delete boot config file from initrd\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 		"		-l : list boot config in initrd or file\n\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 		" If no option is given, show the bootconfig in the given file.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 	return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) int main(int argc, char **argv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 	char *path = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 	char *apply = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 	bool delete = false, list = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 	int opt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 	while ((opt = getopt(argc, argv, "hda:l")) != -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 		switch (opt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 		case 'd':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 			delete = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 		case 'a':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 			apply = optarg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 		case 'l':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 			list = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 		case 'h':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 			return usage();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 	if ((apply && delete) || (delete && list) || (apply && list)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 		pr_err("Error: You can give one of -a, -d or -l at once.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 		return usage();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 	if (optind >= argc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 		pr_err("Error: No initrd is specified.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 		return usage();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 	path = argv[optind];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 	if (apply)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 		return apply_xbc(path, apply);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 	else if (delete)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 		return delete_xbc(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 	return show_xbc(path, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) }