From: Thibaut VARÈNE Date: Fri, 27 Oct 2017 18:12:15 +0000 (+0200) Subject: tools/firmware-utils: mktplinkfw* fix rootfs offset X-Git-Url: http://git.lede-project.org./?a=commitdiff_plain;h=050fb3b55cde4348f4ed12ab98f451c4c56a695b;p=openwrt%2Fstaging%2Fneocturne.git tools/firmware-utils: mktplinkfw* fix rootfs offset With '-a' specified on the command line, the current code: - computes an aligned _kernel length_ instead of an aligned _rootfs offset_. - does not update the rootfs offset after computing the new kernel length, and instead retains the layout default. When the kernel length exceeds the available space left with this fixed offset, the resulting image header contains invalid data, with the recorded rootfs offset overlapping the kernel area. This patch ensures that rootfs offset is correctly computed and reflected in the final image. Furthermore, the build_fw() function special cases the rootfs_align option because of the above invalid logic. This is also fixed and the computed (or command-line provided, or layout-provided) rootfs_ofs value is used in all cases. There seems to be no valid reason to extend the kernel length beyond the actual length of the kernel itself (OFW images don't do it) so this part of the existing behavior is dropped. Example image before the patch: Kernel data offset : 0x00000200 / 512 bytes Kernel data length : 0x00158438 / 1410104 bytes Kernel load address : 0x00000080 Kernel entry point : 0x00000080 Rootfs data offset : 0x00140000 / 1310720 bytes Rootfs data length : 0x001e4f7e / 1986430 bytes Example image after the patch: Kernel data offset : 0x00000200 / 512 bytes Kernel data length : 0x001583fe / 1410046 bytes Kernel load address : 0x00000080 Kernel entry point : 0x00000080 Rootfs data offset : 0x00158600 / 1410560 bytes Rootfs data length : 0x001e4e22 / 1986082 bytes Tested-by: Mathias Kresin Tested-by: Stefan Lippers-Hollmann Tested-by: Daniel Engberg Tested-by: Henryk Heisig Signed-off-by: Thibaut VARÈNE --- diff --git a/tools/firmware-utils/src/mktplinkfw-lib.c b/tools/firmware-utils/src/mktplinkfw-lib.c index 18da13dd04..b2b6de26db 100644 --- a/tools/firmware-utils/src/mktplinkfw-lib.c +++ b/tools/firmware-utils/src/mktplinkfw-lib.c @@ -236,19 +236,13 @@ int build_fw(size_t header_size) goto out_free_buf; if (!combined) { - if (rootfs_align) - p = buf + writelen; - else - p = buf + rootfs_ofs; + p = buf + rootfs_ofs; ret = read_to_buf(&rootfs_info, p); if (ret) goto out_free_buf; - if (rootfs_align) - writelen += rootfs_info.file_size; - else - writelen = rootfs_ofs + rootfs_info.file_size; + writelen = rootfs_ofs + rootfs_info.file_size; if (add_jffs2_eof) writelen = pad_jffs2(buf, writelen, layout->fw_max_len); diff --git a/tools/firmware-utils/src/mktplinkfw.c b/tools/firmware-utils/src/mktplinkfw.c index 9bc112fe6c..ef19e10529 100644 --- a/tools/firmware-utils/src/mktplinkfw.c +++ b/tools/firmware-utils/src/mktplinkfw.c @@ -307,10 +307,10 @@ static int check_options(void) if (rootfs_align) { kernel_len += sizeof(struct fw_header); - kernel_len = ALIGN(kernel_len, rootfs_align); + rootfs_ofs = ALIGN(kernel_len, rootfs_align); kernel_len -= sizeof(struct fw_header); - DBG("kernel length aligned to %u", kernel_len); + DBG("rootfs offset aligned to 0x%u", rootfs_ofs); exceed_bytes = kernel_len + rootfs_info.file_size - (fw_max_len - sizeof(struct fw_header)); if (exceed_bytes > 0) { diff --git a/tools/firmware-utils/src/mktplinkfw2.c b/tools/firmware-utils/src/mktplinkfw2.c index 892c93f109..b6caf8a0e5 100644 --- a/tools/firmware-utils/src/mktplinkfw2.c +++ b/tools/firmware-utils/src/mktplinkfw2.c @@ -267,10 +267,10 @@ static int check_options(void) if (rootfs_align) { kernel_len += sizeof(struct fw_header); - kernel_len = ALIGN(kernel_len, rootfs_align); + rootfs_ofs = ALIGN(kernel_len, rootfs_align); kernel_len -= sizeof(struct fw_header); - DBG("kernel length aligned to %u", kernel_len); + DBG("rootfs offset aligned to 0x%u", rootfs_ofs); if (kernel_len + rootfs_info.file_size > layout->fw_max_len - sizeof(struct fw_header)) {