From: Marek Vasut <marex@denx.de>
To: u-boot@lists.denx.de
Cc: Marek Vasut <marex@denx.de>,
Joe Hershberger <joe.hershberger@ni.com>,
Tom Rini <trini@konsulko.com>
Subject: [PATCH] tools: mkenvimage: Add pure shell implementation of mkenvimage
Date: Wed, 5 Feb 2025 15:35:52 +0100 [thread overview]
Message-ID: <20250205143641.65430-1-marex@denx.de> (raw)
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@denx.de>
---
Cc: Joe Hershberger <joe.hershberger@ni.com>
Cc: Tom Rini <trini@konsulko.com>
---
tools/mkenvimage.sh | 126 ++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 126 insertions(+)
create mode 100755 tools/mkenvimage.sh
diff --git a/tools/mkenvimage.sh b/tools/mkenvimage.sh
new file mode 100755
index 00000000000..f83fac92e5d
--- /dev/null
+++ b/tools/mkenvimage.sh
@@ -0,0 +1,126 @@
+#!/bin/sh
+
+print_help() {
+ echo "mkenvimage [-h] [-V] [-r] [-b] [-p <byte>] -s <environment partition size> -o <output> <input file>"
+ echo ""
+ echo "This tool takes a key=value input file (same as would a \`printenv' show) and generates the corresponding environment image, ready to be flashed."
+ echo ""
+ echo " The input file is in format:"
+ echo " key1=value1"
+ echo " key2=value2"
+ echo " ..."
+ echo " Empty lines are skipped, and lines with a # in the first"
+ echo " column are treated as comments (also skipped)."
+ echo " -r : the environment has multiple copies in flash"
+ echo " -b : the target is big endian (default is little endian)"
+ echo " -p <byte> : fill the image with <byte> bytes instead of 0xff bytes"
+ echo " -V : print version information and exit"
+ echo ""
+ echo 'If the input file is "-", data is read from standard input'
+}
+
+INFILE=""
+OUTFILE=""
+
+REDUNDANT=0
+BIGENDIAN=0
+BYTEFILL=$(printf '\377')
+SIZE=0
+
+while getopts "hrbp:s:o:V" opt ; do
+ case "$opt" in
+ r)
+ 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
next reply other threads:[~2025-02-05 14:37 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-02-05 14:35 Marek Vasut [this message]
2025-02-06 12:38 ` [PATCH] tools: mkenvimage: Add pure shell implementation of mkenvimage Simon Glass
2025-02-06 20:38 ` Marek Vasut
2025-02-07 0:49 ` Simon Glass
2025-02-08 21:25 ` Marek Vasut
2025-02-07 7:40 ` Alexander Dahl
2025-02-08 21:26 ` Marek Vasut
2025-02-07 10:31 ` Rasmus Villemoes
2025-02-08 21:16 ` Marek Vasut
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250205143641.65430-1-marex@denx.de \
--to=marex@denx.de \
--cc=joe.hershberger@ni.com \
--cc=trini@konsulko.com \
--cc=u-boot@lists.denx.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.