tools/bootconfig: Align the bootconfig applied initrd image size to 4
Align the bootconfig applied initrd image size to 4. To pad the data,
bootconfig will use space (0x20) in front of the bootconfig data,
and expands its size and update checksum.
Signed-off-by: Masami Hiramatsu <mhiramat@kernel.org>
diff --git a/include/linux/bootconfig.h b/include/linux/bootconfig.h
index 9903088..461f621 100644
--- a/include/linux/bootconfig.h
+++ b/include/linux/bootconfig.h
@@ -12,6 +12,7 @@
#define BOOTCONFIG_MAGIC "#BOOTCONFIG\n"
#define BOOTCONFIG_MAGIC_LEN 12
+#define BOOTCONFIG_ALIGN 4
/* XBC tree node */
struct xbc_node {
diff --git a/tools/bootconfig/main.c b/tools/bootconfig/main.c
index eb92027..86fa450 100644
--- a/tools/bootconfig/main.c
+++ b/tools/bootconfig/main.c
@@ -336,7 +336,8 @@
char *buf, *data;
int ret, fd;
const char *msg;
- int pos;
+ struct stat st;
+ int pos, pad;
ret = load_xbc_file(xbc_path, &buf);
if (ret < 0) {
@@ -365,9 +366,6 @@
}
printf("Apply %s to %s\n", xbc_path, path);
printf("\tNumber of nodes: %d\n", ret);
- printf("\tSize: %u bytes\n", (unsigned int)size);
- printf("\tChecksum: %d\n", (unsigned int)csum);
-
/* TODO: Check the options by schema */
xbc_destroy_all();
free(buf);
@@ -387,6 +385,30 @@
free(data);
return fd;
}
+
+ /* To algin up the total size to BOOTCONFIG_ALIGN, get the padding size */
+ ret = fstat(fd, &st);
+ if (ret < 0) {
+ pr_err("Failed to get the stat of %s\n", path);
+ free(data);
+ return ret;
+ }
+ pad = BOOTCONFIG_ALIGN - (st.st_size + size) % BOOTCONFIG_ALIGN;
+ if (pad != BOOTCONFIG_ALIGN) {
+ /* Update size and checksum with padding spaces */
+ *(u32 *)(data + size) += pad;
+ *(u32 *)(data + size + 4) += (int)' ' * pad;
+
+ /* Write padding spaces */
+ ret = write(fd, " ", pad);
+ if (ret < 0) {
+ pr_err("Failed to write padding spaces: %d\n", ret);
+ goto out;
+ }
+ }
+ printf("\tSize: %u bytes\n", (unsigned int)*(u32 *)(data + size));
+ printf("\tChecksum: %d\n", (unsigned int)*(u32 *)(data + size + 4));
+
/* TODO: Ensure the @path is initramfs/initrd image */
ret = write(fd, data, size + 8);
if (ret < 0) {