[PATCH 0/2] mkimage: fix using long pathnames

When running an buildbot with OpenWrt-CI I got the following error:
mkimage -f /var/lib/buildbot/slaves/slave/ipq40xx-generic/build/firmware/openwrt/ build_dir/target-arm_cortex-a7+neon-vfpv4_musl_eabi/linux-ipq40xx_generic/tmp/ freifunk-berlin-dev-daily-1907-f66973b-ipq40xx-generic-asus_map-ac2200-initramfs-fit-uImage.itb.its /var/lib/buildbot/slaves/slave/ipq40xx-generic/build/firmware/openwrt/build_dir/ target-arm_cortex-a7+neon-vfpv4_musl_eabi/linux-ipq40xx_generic/tmp/ freifunk-berlin-dev-daily-1907-f66973b-ipq40xx-generic-asus_map-ac2200-initramfs-fit-uImage.itb.new sh: 1: Syntax error: Unterminated quoted string
The syntax error is reported by the system()-call of fit_image.c as the cmd- variable get truncated when using such long pathnames for teh output and the FIT-file. It's fixed by redefining the MKIMAGE_MAX_DTC_CMDLINE_LEN based on the size. of MKIMAGE_MAX_TMPFILE_LEN. Also a Warning will be printed when detecting that the cmd-variable has reached its maximal size.
Sven Roederer (2): tools/mkimage: fix handling long filenames tools/fit-image: print a warning when cmd-line for dtc might be truncated
tools/fit_image.c | 4 ++++ tools/mkimage.h | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-)

The cmdline for calling the dtc was cut-off when using long filenames (e.g. 245 bytes) for output-file and datafile of "-f" parameter. For FIT-images cmd[MKIMAGE_MAX_DTC_CMDLINE_LEN] is declared (hardcoded 512 bytes), and contains some static values, the path of a tmpfile and a datafile. tmpfile is max MKIMAGE_MAX_TMPFILE_LEN (256) and datafile might be also this size. Having two very long pathname results in a truncation os the executed shell command, as the truncated datafile path will not be found. Redefine MKIMAGE_MAX_DTC_CMDLINE_LEN to "2 * MKIMAGE_MAX_TMPFILE_LEN + 35 for the parameters. This likely applies to the "-d" parameter, too.
Signed-off-by: Sven Roederer devel-sven@geroedel.de --- tools/mkimage.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/tools/mkimage.h b/tools/mkimage.h index 0254af59fb..9de94dbe99 100644 --- a/tools/mkimage.h +++ b/tools/mkimage.h @@ -42,6 +42,7 @@ static inline ulong map_to_sysmem(void *ptr) #define MKIMAGE_TMPFILE_SUFFIX ".tmp" #define MKIMAGE_MAX_TMPFILE_LEN 256 #define MKIMAGE_DEFAULT_DTC_OPTIONS "-I dts -O dtb -p 500" -#define MKIMAGE_MAX_DTC_CMDLINE_LEN 512 +#define MKIMAGE_MAX_DTC_CMDLINE_LEN 2 * MKIMAGE_MAX_TMPFILE_LEN + 35 /* 35 bytes for quotes, spaces, "-o", len(MKIMAGE_DEFAULT_DTC_OPTIONS), len(MKIMAGE_TMPFILE_SUFFIX) */
#endif /* _MKIIMAGE_H_ */

On Mon, Apr 27, 2020 at 02:08:38AM +0200, Sven Roederer wrote:
The cmdline for calling the dtc was cut-off when using long filenames (e.g. 245 bytes) for output-file and datafile of "-f" parameter. For FIT-images cmd[MKIMAGE_MAX_DTC_CMDLINE_LEN] is declared (hardcoded 512 bytes), and contains some static values, the path of a tmpfile and a datafile. tmpfile is max MKIMAGE_MAX_TMPFILE_LEN (256) and datafile might be also this size. Having two very long pathname results in a truncation os the executed shell command, as the truncated datafile path will not be found. Redefine MKIMAGE_MAX_DTC_CMDLINE_LEN to "2 * MKIMAGE_MAX_TMPFILE_LEN + 35 for the parameters. This likely applies to the "-d" parameter, too.
Signed-off-by: Sven Roederer devel-sven@geroedel.de
Applied to u-boot/master, thanks!

Signed-off-by: Sven Roederer devel-sven@geroedel.de --- tools/fit_image.c | 4 ++++ 1 file changed, 4 insertions(+)
diff --git a/tools/fit_image.c b/tools/fit_image.c index 4aeabbcfe9..88ff093d05 100644 --- a/tools/fit_image.c +++ b/tools/fit_image.c @@ -17,6 +17,7 @@ #include "fit_common.h" #include "mkimage.h" #include <image.h> +#include <string.h> #include <stdarg.h> #include <version.h> #include <u-boot/crc.h> @@ -744,6 +745,9 @@ static int fit_handle_file(struct image_tool_params *params) snprintf(cmd, sizeof(cmd), "cp "%s" "%s"", params->imagefile, tmpfile); } + if (strlen(cmd) >= MKIMAGE_MAX_DTC_CMDLINE_LEN - 1) { + fprintf(stderr, "WARNING: command-line for FIT creation might be truncated and will probably fail.\n"); + }
if (*cmd && system(cmd) == -1) { fprintf (stderr, "%s: system(%s) failed: %s\n",

On Mon, Apr 27, 2020 at 02:08:39AM +0200, Sven Roederer wrote:
Signed-off-by: Sven Roederer devel-sven@geroedel.de
Applied to u-boot/master, thanks!
participants (2)
-
Sven Roederer
-
Tom Rini