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 <dev@kresin.me>
Tested-by: Stefan Lippers-Hollmann <s.l-h@gmx.de>
Tested-by: Daniel Engberg <daniel.engberg.lists@pyret.net>
Tested-by: Henryk Heisig <hyniu@o2.pl>
Signed-off-by: Thibaut VARÈNE <hacks@slashdirt.org>
This commit is contained in:
Thibaut VARÈNE 2017-10-27 20:12:15 +02:00 committed by Mathias Kresin
parent dea3bad84e
commit 050fb3b55c
3 changed files with 6 additions and 12 deletions

View File

@ -236,18 +236,12 @@ int build_fw(size_t header_size)
goto out_free_buf;
if (!combined) {
if (rootfs_align)
p = buf + writelen;
else
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;
if (add_jffs2_eof)

View File

@ -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) {

View File

@ -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)) {