[PATCH] tools: mkenvimage: Add pure shell implementation of mkenvimage
Simon Glass
sjg at chromium.org
Thu Feb 6 13:38:41 CET 2025
Hi Marek,
On Wed, 5 Feb 2025 at 07:37, Marek Vasut <marex at denx.de> wrote:
>
> Add implementation of mkenvimage written purely in bourne shell.
>
> This is not a replacement for mkenvimage tool, but rather a simple
> implementation which can be used in environments where mkenvimage
> itself cannot be deployed due to various constraints, like hardware
> manufacturing plants, but where bourne shell and basic tool are
> already available.
>
> The external dependencies which are not shell built-ins are gzip
> and grep.
>
> All mkenvimage parameters are implemented and compatible with the
> C implementation of mkenvimage.
>
> Signed-off-by: Marek Vasut <marex at denx.de>
> ---
> Cc: Joe Hershberger <joe.hershberger at ni.com>
> Cc: Tom Rini <trini at konsulko.com>
> ---
> tools/mkenvimage.sh | 126 ++++++++++++++++++++++++++++++++++++++++++++
> 1 file changed, 126 insertions(+)
> create mode 100755 tools/mkenvimage.sh
Would it be worth adding a simple test for this?
Regards,
SImon
> + REDUNDANT=1
> + ;;
> + b)
> + # Swap CRC endianness
> + BIGENDIAN=1
> + ;;
> + p)
> + BYTEFILL="$(printf "%b" "\0$(printf "%o" $((OPTARG)))")"
> + ;;
> + s)
> + SIZE="$((OPTARG))"
> + ;;
> + o)
> + OUTFILE="${OPTARG}"
> + ;;
> + V)
> + echo "mkenvimage version SHELL"
> + exit 0
> + ;;
> + h|*)
> + print_help
> + exit 0
> + ;;
> + esac
> +done
> +
> +shift $((OPTIND - 1))
> +if test -n "${1}" ; then
> + INFILE=${1}
> +fi
> +
> +if test -z "${INFILE}" ; then
> + echo "Input file not specified"
> + print_help
> + exit 1
> +fi
> +
> +if test -z "${OUTFILE}" ; then
> + echo "Output file not specified"
> + print_help
> + exit 1
> +fi
> +
> +if test "${SIZE}" -eq 0 ; then
> + echo "Size not specified"
> + print_help
> + exit 1
> +fi
> +
> +crctmpfile=$(mktemp)
> +outtmpfile=$(mktemp)
> +
> +(
> +# Read input environment file, make sure there is a trailing newline,
> +# sort the result and remove empty and commented out lines.
> +( cat "${INFILE}" ; echo ) | sort -u "${INFILE}" | grep -v '^#' | grep -v '^[ \t]*$' | tr '\n' '\0'
> +# Insert one more trailing zero at the end of environment.
> +printf '\0'
> +# Fill the rest of the environment with BYTEFILL bytes.
> +tr '\0' "${BYTEFILL}" < /dev/zero
> +# Clip the environment block to correct size. Header is 4 bytes CRC32
> +# for regular environment and 5 bytes CRC32 and flags for redundant
> +# environment.
> +) | dd count="$((SIZE - 4 - REDUNDANT))" bs=1 of="${outtmpfile}" 2>/dev/null
> +
> +gzip -ck1 "${outtmpfile}" | tail -c8 | head -c4 > "${crctmpfile}"
> +
> +# Reverse crc32 for big-endian systems
> +if test "$BIGENDIAN" -eq 1 ; then
> + # Note that 'rev' is from util-linux and may not be available,
> + # 'xxd' is from vim and may not be available, and 'tac -rs .'
> + # does not handle unterminated strings and newlines well, so
> + # use plain 'dd' to reverse the 4 bytes reliably and using
> + # minimum dependencies:
> + crcbetmpfile=$(mktemp)
> + (
> + dd if="${crctmpfile}" bs=1 count=1 skip=3 ;
> + dd if="${crctmpfile}" bs=1 count=1 skip=2 ;
> + dd if="${crctmpfile}" bs=1 count=1 skip=1 ;
> + dd if="${crctmpfile}" bs=1 count=1 skip=0
> + ) >> "${crcbetmpfile}" 2>/dev/null
> + mv "${crcbetmpfile}" "${crctmpfile}"
> +fi
> +
> +# Add 5th byte set to 0x01 to indicate redundant environment image.
> +if test "$REDUNDANT" -eq 1 ; then
> + printf '\001' >> "${crctmpfile}"
> +fi
> +
> +# Concatenate the CRC32 and environment block into the final output file.
> +cat "${crctmpfile}" "${outtmpfile}" > "${OUTFILE}"
> +
> +# Clean up the temporary files
> +rm -f "${crctmpfile}" "${outtmpfile}"
> --
> 2.47.2
>
More information about the U-Boot
mailing list