History log of /u-boot/tools/mkenvimage.c
Revision Date Author Comments
(<<< Hide modified files)
(Show modified files >>>)
# c246d69f 06-Nov-2022 Marek Vasut <marex@denx.de>

tools: mkenvimage: Drop duplicate crc header include

This header was already included just above version.h,
do not include it twice.

Fixes: 3db71108575 ("crc32: Use the crc.h header for crc functions")
Signed-off-by: Marek Vasut <marex@denx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 3db71108 14-Nov-2019 Simon Glass <sjg@chromium.org>

crc32: Use the crc.h header for crc functions

Drop inclusion of crc.h in common.h and use the correct header directly
instead.

With this we can drop the conflicting definition in fw_env.h and rely on
the crc.h header, which is already included.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Tom Rini <trini@konsulko.com>

# 849f9bef 29-Jun-2019 Andre Przywara <andre.przywara@arm.com>

tools: mkenvimage: Always consider non-regular files

At the moment mkenvimage has two separate read paths: One to read from
a potential pipe, while dynamically increasing the buffer size, and a
second one using mmap(2), using the input file's size. This is
problematic for two reasons:
- The "pipe" path will be chosen if the input filename is missing or
"-". Any named, but non-regular file will use the other path, which
typically will cause mmap() to fail:
$ mkenvimage -s 256 -o out <(echo "foo=bar")
- There is no reason to have *two* ways of reading a file, since the
"pipe way" will always work, even for regular files.

Fix this (and simplify the code on the way) by always using the method
of dynamically resizing the buffer. The existing distinction between
the two cases will merely be used to use the open() syscall or not.

Signed-off-by: Andre Przywara <andre.przywara@arm.com>

# 40e7b3ce 29-Jun-2019 Andre Przywara <andre.przywara@arm.com>

tools: mkenvimage: Fix reading from slow pipe

It is perfectly fine for the read(2) syscall to return with less than
the requested number of bytes read (short read, see the "RETURN VALUE"
section of the man page). This typically happens with slow input
(keyboard, network) or with complex pipes.

So far mkenvimage expects the exact number of requested bytes to be
read, assuming an end-of-file condition otherwise. This wrong behaviour
can be easily shown with:
$ (echo "foo=bar"; sleep 1; echo "bar=baz") | mkenvimage -s 256 -o out -
The second line will be missing from the output.

Correct this by checking for any positive, non-zero return value.

This fixes a problem with a complex pipe in one of my scripts, where
the environment consist of two parts.

Signed-off-by: Andre Przywara <andre.przywara@arm.com>
Acked-by: Alexander Dahl <ada@thorsis.com>

# 83d290c5 06-May-2018 Tom Rini <trini@konsulko.com>

SPDX: Convert all of our single license tags to Linux Kernel style

When U-Boot started using SPDX tags we were among the early adopters and
there weren't a lot of other examples to borrow from. So we picked the
area of the file that usually had a full license text and replaced it
with an appropriate SPDX-License-Identifier: entry. Since then, the
Linux Kernel has adopted SPDX tags and they place it as the very first
line in a file (except where shebangs are used, then it's second line)
and with slightly different comment styles than us.

In part due to community overlap, in part due to better tag visibility
and in part for other minor reasons, switch over to that style.

This commit changes all instances where we have a single declared
license in the tag as both the before and after are identical in tag
contents. There's also a few places where I found we did not have a tag
and have introduced one.

Signed-off-by: Tom Rini <trini@konsulko.com>

# c3b115f4 20-Apr-2018 Alexander Dahl <ada@thorsis.com>

tools: mkenvimage: Fix possible segfault on stdin input

The size of 'filebuf' was not increased as more and more bytes are read
from stdin, but 'filebuf' was always reallocated to the same fix size.
This works as long as only less bytes than the initial buffer size come
in, for more input this will segfault. (It actually does, I tested
that.) So for each loop cycle the buffer size has to be increased by the
number of bytes we want to read.

Signed-off-by: Alexander Dahl <ada@thorsis.com>

# 3559028c 20-Apr-2018 Alexander Dahl <ada@thorsis.com>

tools: mkenvimage: Fix read() stdin error handling

On success read() returns the number of bytes read or zero for EOF. On
error -1 is returned and errno is set, so the right way to test if read
had failed is to test the return value instead of errno.

Signed-off-by: Alexander Dahl <ada@thorsis.com>

# 80ee0196 12-Mar-2015 Brian McFarland <mcfarlandjb@gmail.com>

Patch to mkenvimage to handle text files with length that exceed env size

The current head revision of mkenvimage
(e72be8947e129f5ab274c0a9f235d2cc0014b2ea) will prevent you from creating
an env image from a text file that is larger than the env length specified
by the '-s' option. That doesn't make sense given that the tool now allows
comments and blank lines. This patch removes that limitation and allows
longer text files to be used.

I don't have time / desire at the moment to figure out "patman" and could
really care less if this is adopted up stream. Just figured I would share
in case anybody else finds it useful enough to take time to do a proper
patch.

>From 39ff30190c2bf687861f4b4b33230f1944fb64f9 Mon Sep 17 00:00:00 2001
From: Brian McFarland <bmcfarland@rldrake.com>
Date: Thu, 12 Mar 2015 11:37:19 -0400
Subject: [PATCH] In mkenvimage, removed the check that prevented using a
source text file larger than the output environment image. Instead, the main
parsing loop checks to see if the environment buffer is full, and quits if it
is. After the main parse loop, a second loop swallows comments and
whitespace until either the EOF is reached or more env vars are found, in
which case an error will be thrown.

# e72be894 27-Aug-2014 Dominik Muth <dominik.muth@bkvibro.com>

Added support for comments in input to mkenvimage.

This patch adds support for comments in the input to mkenvimage, i.e. in
the environment source: All lines starting with a # in the firs column
will be ignored.

Additionally empty lines will also be ignored.

Signed-off-by: Dominik Muth <dominik.muth@bkvibro.com>

# 2db1c3fc 30-Jul-2013 York Sun <yorksun@freescale.com>

tools/Makefile: Move _GNU_SOURCE to Makefile

Commit 669dfc2e adds libfdt_env.h to HOSTCPPFLAGS. It causes stdio.h
to be included before _GNU_SOURCE is defined in C files. On some old hosts
some prototypes are protected by #ifdef __USE_GNU, which is set when
_GNU_SOURCE is defined.

Signed-off-by: York Sun <yorksun@freescale.com>
Acked-by: Simon Glass <sjg@chromium.org>

# 1a459660 08-Jul-2013 Wolfgang Denk <wd@denx.de>

Add GPL-2.0+ SPDX-License-Identifier to source files

Signed-off-by: Wolfgang Denk <wd@denx.de>
[trini: Fixup common/cmd_io.c]
Signed-off-by: Tom Rini <trini@ti.com>

# 4f7136e7 18-Jul-2012 Mike Frysinger <vapier@gentoo.org>

tools: clean up mingw ifdefs

We have a header file specifically for mingw cruft, so keep it there
to avoid crap spreading into the main tools. This lets our devs just
worry about *nix systems.

Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# 8b6a4952 07-Jul-2012 Vladimir Yakovlev <nagos@inbox.ru>

tools: Fix mingw tools build

mkenvimage does not build due to missed os_support.o and unsupported
file modes S_IRGRP S_IWGRP.
Tested with mingw 4.2.1 on ubuntu 12.04.

Signed-off-by: Vladimir Yakovlev <nagos@inbox.ru>

# 558cd995 28-Jun-2012 Andreas Bießmann <andreas.devel@googlemail.com>

tools/mkenvimage.c: fix basename(3) usage

Use the POSIX variant of basename due to BSD systems (e.g. OS X) do not provide
GNU version of basename(3). It is save to use the POSIX variant here cause we do
never use argv[0] later on which may be modified by the basename(3) POSIX
variant.
On systems providing GNU variant the GNU variant should be used since string.h
is included before libgen.h. Therefore let the _GNU_SOURCE as is.

This patch fixes following warning (on OS X):
---8<---
mkenvimage.c: In function ‘main’:
mkenvimage.c:105: warning: implicit declaration of function ‘basename’
mkenvimage.c:105: warning: assignment makes pointer from integer without a cast
--->8---

Signed-off-by: Andreas Bießmann <andreas.devel@googlemail.com>
cc: Keith Mok <ek9852@gmail.com>

# 1ebff63f 05-Apr-2012 Dirk Behme <dirk.behme@googlemail.com>

mkenvimage: Fix compiler warning

Fix the compiler warning

mkenvimage.c: In function ‘main’:
mkenvimage.c:218: warning: format ‘%ld’ expects type ‘long int’, but argument 3 has type ‘unsigned int’
mkenvimage.c:226: warning: format ‘%ld’ expects type ‘long int’, but argument 3 has type ‘unsigned int’

introduced with the commit

mkenvimage: Use mmap() when reading from a regular file
6ee39f8055680654f9cc97b98dcce9588f1ab71e

Signed-off-by: Dirk Behme <dirk.behme@googlemail.com>
CC: David Wagner <david.wagner@free-electrons.com>
CC: Anatolij Gustschin <agust@denx.de>
CC: Mike Frysinger <vapier@gentoo.org>
Acked-by: Mike Frysinger <vapier@gentoo.org>
Acked-by: David Wagner <deubeuliou@gmail.com>

# d8d26599 13-Jan-2012 David Wagner <david.wagner@free-electrons.com>

mkenvimage: Really set the redundant byte when applicable

Signed-off-by: David Wagner <david.wagner@free-electrons.com>
Acked-by: Mike Frysinger <vapier@gentoo.org>

# dc6449a9 13-Jan-2012 David Wagner <david.wagner@free-electrons.com>

mkenvimage: Don't try to detect comments in the input file

Remove this feature since it seems impossible to reliably detect them.

Signed-off-by: David Wagner <david.wagner@free-electrons.com>
Acked-by: Mike Frysinger <vapier@gentoo.org>

# 6ee39f80 13-Jan-2012 David Wagner <david.wagner@free-electrons.com>

mkenvimage: Use mmap() when reading from a regular file

Fall back to read() if it fails.

Signed-off-by: David Wagner <david.wagner@free-electrons.com>
Acked-by: Mike Frysinger <vapier@gentoo.org>

# 48995b5a 13-Jan-2012 David Wagner <david.wagner@free-electrons.com>

mkenvimage: Read/Write from/to stdin/out by default or if the filename is "-"

Signed-off-by: David Wagner <david.wagner@free-electrons.com>
Acked-by: Mike Frysinger <vapier@gentoo.org>

# 3d0f9bd0 13-Jan-2012 David Wagner <david.wagner@free-electrons.com>

mkenvimage: More error handling

Verbosly fail if the target environment size or the padding byte are badly
formated.

Verbosly fail if something bad happens when reading from standard input.

Signed-off-by: David Wagner <david.wagner@free-electrons.com>
Acked-by: Mike Frysinger <vapier@gentoo.org>

# d1acdae9 13-Jan-2012 David Wagner <david.wagner@free-electrons.com>

mkenvimage: Correct an include and add a missing one

compiler.h needs to be included from U-Boot's headers.
Also, group U-Boot-specific includes together

stdlib.h was missing.

Signed-off-by: David Wagner <david.wagner@free-electrons.com>
Acked-by: Mike Frysinger <vapier@gentoo.org>

# 8a1b8fc7 13-Jan-2012 David Wagner <david.wagner@free-electrons.com>

mkenvimage: correct and clarify comments and error messages

Also, don't split error messages over several lines as per a coding style
exception making them easier to grep.

Signed-off-by: David Wagner <david.wagner@free-electrons.com>
Acked-by: Mike Frysinger <vapier@gentoo.org>

# e758a5c4 01-Mar-2012 Wolfgang Denk <wd@denx.de>

mkenvimage: fix usage message

Don't use argv[0] for usage() because it may or may not be clobbered
by the previous call to basename(). Use "prg" instead as it is done
in the rest of the code.

Signed-off-by: Wolfgang Denk <wd@denx.de>
Acked-by: Mike Frysinger <vapier@gentoo.org>

# dbee61db 20-Dec-2011 David Wagner <david.wagner@free-electrons.com>

Correctly handle input files beginning with several newlines

Also, fix some comments (minor)

Signed-off-by: David Wagner <david.wagner@free-electrons.com>

# 72ebafbe 23-Dec-2011 Horst Kronstorfer <hkronsto@frequentis.com>

tools/mkenvimage.c: Fix a merge issue

With bfcc40bb09b05c90cc3b1496abb270eb8aa72134 'optopt' was reverted.

Signed-off-by: Horst Kronstorfer <hkronsto@frequentis.com>

# bfcc40bb 23-Dec-2011 Wolfgang Denk <wd@denx.de>

Merge branch 'next' of ../next

* 'next' of ../next:
mkenvimage: Add version info switch (-V)
mkenvimage: Fix getopt() error handling
mkenvimage: Fix some typos
phy: add Micrel KS8721BL phy definition
net: introduce per device index
mvgbe: remove setting of ethaddr within the driver
x86: Add support for specifying an initrd with the zboot command
x86: Refactor the zboot innards so they can be reused with a vboot image
x86: Add infrastructure to extract an e820 table from the coreboot tables
x86: Add support for booting Linux using the 32 bit boot protocol
x86: Clean up the x86 zimage code in preparation to extend it
x86: Import code from coreboot's libpayload to parse the coreboot table
x86: Initial commit for running as a coreboot payload
CHECKPATCH: ./board/esd/hh405/logo_320_240_8bpp.c
CHECKPATCH: ./board/esd/hh405/logo_1024_768_8bpp.c
CHECKPATCH: ./board/esd/hh405/logo_320_240_4bpp.c
CHECKPATCH: ./board/esd/hh405/logo_640_480_24bpp.c
CHECKPATCH: ./board/esd/apc405/logo_640_480_24bpp.c
CHECKPATCH: ./board/esd/voh405/logo_320_240_4bpp.c
CHECKPATCH: ./board/esd/voh405/logo_640_480_24bpp.c
CHECKPATCH: ./board/esd/hh405/fpgadata.c
CHECKPATCH: ./board/esd/pci405/fpgadata.c
CHECKPATCH: ./board/esd/tasreg/fpgadata.c
CHECKPATCH: ./board/esd/apc405/fpgadata.c
CHECKPATCH: ./board/esd/voh405/fpgadata.c
CHECKPATCH: ./board/esd/ash405/fpgadata.c
CHECKPATCH: ./board/esd/dasa_sim/fpgadata.c
CHECKPATCH: ./board/esd/ar405/fpgadata_xl30.c
CHECKPATCH: ./board/esd/ar405/fpgadata.c
CHECKPATCH: ./board/esd/plu405/fpgadata.c
CHECKPATCH: ./board/esd/wuh405/fpgadata.c
CHECKPATCH: ./board/esd/cpci405/fpgadata_cpci405.c
CHECKPATCH: ./board/esd/cpci405/fpgadata_cpci405ab.c
CHECKPATCH: ./board/esd/cpci405/fpgadata_cpci4052.c
CHECKPATCH: ./board/esd/canbt/fpgadata.c
CHECKPATCH: ./board/esd/du405/fpgadata.c
CHECKPATCH: ./board/esd/cpciiser4/fpgadata.c
CHECKPATCH: ./board/dave/PPChameleonEVB/fpgadata.c
avr32:mmu.c: fix printf() length modifier
fat.c: fix printf() length modifier
cmd_sf.c: fix printf() length modifier
Make printf and vprintf safe from buffer overruns
vsprintf: Move function documentation into header file
Add safe vsnprintf and snprintf library functions
Move vsprintf functions into their own header

Conflicts:
tools/mkenvimage.c

Signed-off-by: Wolfgang Denk <wd@denx.de>


# af44f4b2 21-Dec-2011 Horst Kronstorfer <hkronsto@frequentis.com>

Print program basename instead of whole path in usage()

Signed-off-by: Horst Kronstorfer <hkronsto@frequentis.com>

# 1895420b 04-Dec-2011 Horst Kronstorfer <hkronsto@frequentis.com>

mkenvimage: Add version info switch (-V)

Signed-off-by: Horst Kronstorfer <hkronsto@frequentis.com>

# 5e0c63e2 04-Dec-2011 Horst Kronstorfer <hkronsto@frequentis.com>

mkenvimage: Fix getopt() error handling

Since the original implementation indicates explicit error handling
we turn off getopt()'s internal error messaging to avoid duplicate
error messages. Additionally we add ':' (missing option argument)
error handling.

Signed-off-by: Horst Kronstorfer <hkronsto@frequentis.com>

# d30011bf 04-Dec-2011 Horst Kronstorfer <hkronsto@frequentis.com>

mkenvimage: Fix some typos

Signed-off-by: Horst Kronstorfer <hkronsto@frequentis.com>

# a6337e6f 25-Sep-2011 David Wagner <david.wagner@free-electrons.com>

new tool mkenvimage: generates an env image from an arbitrary config file

This tool takes a key=value configuration file (same as would a `printenv' show)
and generates the corresponding environment image, ready to be flashed.

use case: flash the environment with an external tool

Signed-off-by: David Wagner <david.wagner@free-electrons.com>
Acked-by; Mike Frysinger <vapier@gentoo.org>
Tested-by: Thomas Petazzoni <thomas.petazzoni@free-electrons.com>

# 3db71108 14-Nov-2019 Simon Glass <sjg@chromium.org>

crc32: Use the crc.h header for crc functions

Drop inclusion of crc.h in common.h and use the correct header directly
instead.

With this we can drop the conflicting definition in fw_env.h and rely on
the crc.h header, which is already included.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Tom Rini <trini@konsulko.com>

# 849f9bef 29-Jun-2019 Andre Przywara <andre.przywara@arm.com>

tools: mkenvimage: Always consider non-regular files

At the moment mkenvimage has two separate read paths: One to read from
a potential pipe, while dynamically increasing the buffer size, and a
second one using mmap(2), using the input file's size. This is
problematic for two reasons:
- The "pipe" path will be chosen if the input filename is missing or
"-". Any named, but non-regular file will use the other path, which
typically will cause mmap() to fail:
$ mkenvimage -s 256 -o out <(echo "foo=bar")
- There is no reason to have *two* ways of reading a file, since the
"pipe way" will always work, even for regular files.

Fix this (and simplify the code on the way) by always using the method
of dynamically resizing the buffer. The existing distinction between
the two cases will merely be used to use the open() syscall or not.

Signed-off-by: Andre Przywara <andre.przywara@arm.com>

# 40e7b3ce 29-Jun-2019 Andre Przywara <andre.przywara@arm.com>

tools: mkenvimage: Fix reading from slow pipe

It is perfectly fine for the read(2) syscall to return with less than
the requested number of bytes read (short read, see the "RETURN VALUE"
section of the man page). This typically happens with slow input
(keyboard, network) or with complex pipes.

So far mkenvimage expects the exact number of requested bytes to be
read, assuming an end-of-file condition otherwise. This wrong behaviour
can be easily shown with:
$ (echo "foo=bar"; sleep 1; echo "bar=baz") | mkenvimage -s 256 -o out -
The second line will be missing from the output.

Correct this by checking for any positive, non-zero return value.

This fixes a problem with a complex pipe in one of my scripts, where
the environment consist of two parts.

Signed-off-by: Andre Przywara <andre.przywara@arm.com>
Acked-by: Alexander Dahl <ada@thorsis.com>

# 83d290c5 06-May-2018 Tom Rini <trini@konsulko.com>

SPDX: Convert all of our single license tags to Linux Kernel style

When U-Boot started using SPDX tags we were among the early adopters and
there weren't a lot of other examples to borrow from. So we picked the
area of the file that usually had a full license text and replaced it
with an appropriate SPDX-License-Identifier: entry. Since then, the
Linux Kernel has adopted SPDX tags and they place it as the very first
line in a file (except where shebangs are used, then it's second line)
and with slightly different comment styles than us.

In part due to community overlap, in part due to better tag visibility
and in part for other minor reasons, switch over to that style.

This commit changes all instances where we have a single declared
license in the tag as both the before and after are identical in tag
contents. There's also a few places where I found we did not have a tag
and have introduced one.

Signed-off-by: Tom Rini <trini@konsulko.com>

# c3b115f4 20-Apr-2018 Alexander Dahl <ada@thorsis.com>

tools: mkenvimage: Fix possible segfault on stdin input

The size of 'filebuf' was not increased as more and more bytes are read
from stdin, but 'filebuf' was always reallocated to the same fix size.
This works as long as only less bytes than the initial buffer size come
in, for more input this will segfault. (It actually does, I tested
that.) So for each loop cycle the buffer size has to be increased by the
number of bytes we want to read.

Signed-off-by: Alexander Dahl <ada@thorsis.com>

# 3559028c 20-Apr-2018 Alexander Dahl <ada@thorsis.com>

tools: mkenvimage: Fix read() stdin error handling

On success read() returns the number of bytes read or zero for EOF. On
error -1 is returned and errno is set, so the right way to test if read
had failed is to test the return value instead of errno.

Signed-off-by: Alexander Dahl <ada@thorsis.com>

# 80ee0196 12-Mar-2015 Brian McFarland <mcfarlandjb@gmail.com>

Patch to mkenvimage to handle text files with length that exceed env size

The current head revision of mkenvimage
(e72be8947e129f5ab274c0a9f235d2cc0014b2ea) will prevent you from creating
an env image from a text file that is larger than the env length specified
by the '-s' option. That doesn't make sense given that the tool now allows
comments and blank lines. This patch removes that limitation and allows
longer text files to be used.

I don't have time / desire at the moment to figure out "patman" and could
really care less if this is adopted up stream. Just figured I would share
in case anybody else finds it useful enough to take time to do a proper
patch.

>From 39ff30190c2bf687861f4b4b33230f1944fb64f9 Mon Sep 17 00:00:00 2001
From: Brian McFarland <bmcfarland@rldrake.com>
Date: Thu, 12 Mar 2015 11:37:19 -0400
Subject: [PATCH] In mkenvimage, removed the check that prevented using a
source text file larger than the output environment image. Instead, the main
parsing loop checks to see if the environment buffer is full, and quits if it
is. After the main parse loop, a second loop swallows comments and
whitespace until either the EOF is reached or more env vars are found, in
which case an error will be thrown.

# e72be894 27-Aug-2014 Dominik Muth <dominik.muth@bkvibro.com>

Added support for comments in input to mkenvimage.

This patch adds support for comments in the input to mkenvimage, i.e. in
the environment source: All lines starting with a # in the firs column
will be ignored.

Additionally empty lines will also be ignored.

Signed-off-by: Dominik Muth <dominik.muth@bkvibro.com>

# 2db1c3fc 30-Jul-2013 York Sun <yorksun@freescale.com>

tools/Makefile: Move _GNU_SOURCE to Makefile

Commit 669dfc2e adds libfdt_env.h to HOSTCPPFLAGS. It causes stdio.h
to be included before _GNU_SOURCE is defined in C files. On some old hosts
some prototypes are protected by #ifdef __USE_GNU, which is set when
_GNU_SOURCE is defined.

Signed-off-by: York Sun <yorksun@freescale.com>
Acked-by: Simon Glass <sjg@chromium.org>

# 1a459660 08-Jul-2013 Wolfgang Denk <wd@denx.de>

Add GPL-2.0+ SPDX-License-Identifier to source files

Signed-off-by: Wolfgang Denk <wd@denx.de>
[trini: Fixup common/cmd_io.c]
Signed-off-by: Tom Rini <trini@ti.com>

# 4f7136e7 18-Jul-2012 Mike Frysinger <vapier@gentoo.org>

tools: clean up mingw ifdefs

We have a header file specifically for mingw cruft, so keep it there
to avoid crap spreading into the main tools. This lets our devs just
worry about *nix systems.

Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# 8b6a4952 07-Jul-2012 Vladimir Yakovlev <nagos@inbox.ru>

tools: Fix mingw tools build

mkenvimage does not build due to missed os_support.o and unsupported
file modes S_IRGRP S_IWGRP.
Tested with mingw 4.2.1 on ubuntu 12.04.

Signed-off-by: Vladimir Yakovlev <nagos@inbox.ru>

# 558cd995 28-Jun-2012 Andreas Bießmann <andreas.devel@googlemail.com>

tools/mkenvimage.c: fix basename(3) usage

Use the POSIX variant of basename due to BSD systems (e.g. OS X) do not provide
GNU version of basename(3). It is save to use the POSIX variant here cause we do
never use argv[0] later on which may be modified by the basename(3) POSIX
variant.
On systems providing GNU variant the GNU variant should be used since string.h
is included before libgen.h. Therefore let the _GNU_SOURCE as is.

This patch fixes following warning (on OS X):
---8<---
mkenvimage.c: In function ‘main’:
mkenvimage.c:105: warning: implicit declaration of function ‘basename’
mkenvimage.c:105: warning: assignment makes pointer from integer without a cast
--->8---

Signed-off-by: Andreas Bießmann <andreas.devel@googlemail.com>
cc: Keith Mok <ek9852@gmail.com>

# 1ebff63f 05-Apr-2012 Dirk Behme <dirk.behme@googlemail.com>

mkenvimage: Fix compiler warning

Fix the compiler warning

mkenvimage.c: In function ‘main’:
mkenvimage.c:218: warning: format ‘%ld’ expects type ‘long int’, but argument 3 has type ‘unsigned int’
mkenvimage.c:226: warning: format ‘%ld’ expects type ‘long int’, but argument 3 has type ‘unsigned int’

introduced with the commit

mkenvimage: Use mmap() when reading from a regular file
6ee39f8055680654f9cc97b98dcce9588f1ab71e

Signed-off-by: Dirk Behme <dirk.behme@googlemail.com>
CC: David Wagner <david.wagner@free-electrons.com>
CC: Anatolij Gustschin <agust@denx.de>
CC: Mike Frysinger <vapier@gentoo.org>
Acked-by: Mike Frysinger <vapier@gentoo.org>
Acked-by: David Wagner <deubeuliou@gmail.com>

# d8d26599 13-Jan-2012 David Wagner <david.wagner@free-electrons.com>

mkenvimage: Really set the redundant byte when applicable

Signed-off-by: David Wagner <david.wagner@free-electrons.com>
Acked-by: Mike Frysinger <vapier@gentoo.org>

# dc6449a9 13-Jan-2012 David Wagner <david.wagner@free-electrons.com>

mkenvimage: Don't try to detect comments in the input file

Remove this feature since it seems impossible to reliably detect them.

Signed-off-by: David Wagner <david.wagner@free-electrons.com>
Acked-by: Mike Frysinger <vapier@gentoo.org>

# 6ee39f80 13-Jan-2012 David Wagner <david.wagner@free-electrons.com>

mkenvimage: Use mmap() when reading from a regular file

Fall back to read() if it fails.

Signed-off-by: David Wagner <david.wagner@free-electrons.com>
Acked-by: Mike Frysinger <vapier@gentoo.org>

# 48995b5a 13-Jan-2012 David Wagner <david.wagner@free-electrons.com>

mkenvimage: Read/Write from/to stdin/out by default or if the filename is "-"

Signed-off-by: David Wagner <david.wagner@free-electrons.com>
Acked-by: Mike Frysinger <vapier@gentoo.org>

# 3d0f9bd0 13-Jan-2012 David Wagner <david.wagner@free-electrons.com>

mkenvimage: More error handling

Verbosly fail if the target environment size or the padding byte are badly
formated.

Verbosly fail if something bad happens when reading from standard input.

Signed-off-by: David Wagner <david.wagner@free-electrons.com>
Acked-by: Mike Frysinger <vapier@gentoo.org>

# d1acdae9 13-Jan-2012 David Wagner <david.wagner@free-electrons.com>

mkenvimage: Correct an include and add a missing one

compiler.h needs to be included from U-Boot's headers.
Also, group U-Boot-specific includes together

stdlib.h was missing.

Signed-off-by: David Wagner <david.wagner@free-electrons.com>
Acked-by: Mike Frysinger <vapier@gentoo.org>

# 8a1b8fc7 13-Jan-2012 David Wagner <david.wagner@free-electrons.com>

mkenvimage: correct and clarify comments and error messages

Also, don't split error messages over several lines as per a coding style
exception making them easier to grep.

Signed-off-by: David Wagner <david.wagner@free-electrons.com>
Acked-by: Mike Frysinger <vapier@gentoo.org>

# e758a5c4 01-Mar-2012 Wolfgang Denk <wd@denx.de>

mkenvimage: fix usage message

Don't use argv[0] for usage() because it may or may not be clobbered
by the previous call to basename(). Use "prg" instead as it is done
in the rest of the code.

Signed-off-by: Wolfgang Denk <wd@denx.de>
Acked-by: Mike Frysinger <vapier@gentoo.org>

# dbee61db 20-Dec-2011 David Wagner <david.wagner@free-electrons.com>

Correctly handle input files beginning with several newlines

Also, fix some comments (minor)

Signed-off-by: David Wagner <david.wagner@free-electrons.com>

# 72ebafbe 23-Dec-2011 Horst Kronstorfer <hkronsto@frequentis.com>

tools/mkenvimage.c: Fix a merge issue

With bfcc40bb09b05c90cc3b1496abb270eb8aa72134 'optopt' was reverted.

Signed-off-by: Horst Kronstorfer <hkronsto@frequentis.com>

# bfcc40bb 23-Dec-2011 Wolfgang Denk <wd@denx.de>

Merge branch 'next' of ../next

* 'next' of ../next:
mkenvimage: Add version info switch (-V)
mkenvimage: Fix getopt() error handling
mkenvimage: Fix some typos
phy: add Micrel KS8721BL phy definition
net: introduce per device index
mvgbe: remove setting of ethaddr within the driver
x86: Add support for specifying an initrd with the zboot command
x86: Refactor the zboot innards so they can be reused with a vboot image
x86: Add infrastructure to extract an e820 table from the coreboot tables
x86: Add support for booting Linux using the 32 bit boot protocol
x86: Clean up the x86 zimage code in preparation to extend it
x86: Import code from coreboot's libpayload to parse the coreboot table
x86: Initial commit for running as a coreboot payload
CHECKPATCH: ./board/esd/hh405/logo_320_240_8bpp.c
CHECKPATCH: ./board/esd/hh405/logo_1024_768_8bpp.c
CHECKPATCH: ./board/esd/hh405/logo_320_240_4bpp.c
CHECKPATCH: ./board/esd/hh405/logo_640_480_24bpp.c
CHECKPATCH: ./board/esd/apc405/logo_640_480_24bpp.c
CHECKPATCH: ./board/esd/voh405/logo_320_240_4bpp.c
CHECKPATCH: ./board/esd/voh405/logo_640_480_24bpp.c
CHECKPATCH: ./board/esd/hh405/fpgadata.c
CHECKPATCH: ./board/esd/pci405/fpgadata.c
CHECKPATCH: ./board/esd/tasreg/fpgadata.c
CHECKPATCH: ./board/esd/apc405/fpgadata.c
CHECKPATCH: ./board/esd/voh405/fpgadata.c
CHECKPATCH: ./board/esd/ash405/fpgadata.c
CHECKPATCH: ./board/esd/dasa_sim/fpgadata.c
CHECKPATCH: ./board/esd/ar405/fpgadata_xl30.c
CHECKPATCH: ./board/esd/ar405/fpgadata.c
CHECKPATCH: ./board/esd/plu405/fpgadata.c
CHECKPATCH: ./board/esd/wuh405/fpgadata.c
CHECKPATCH: ./board/esd/cpci405/fpgadata_cpci405.c
CHECKPATCH: ./board/esd/cpci405/fpgadata_cpci405ab.c
CHECKPATCH: ./board/esd/cpci405/fpgadata_cpci4052.c
CHECKPATCH: ./board/esd/canbt/fpgadata.c
CHECKPATCH: ./board/esd/du405/fpgadata.c
CHECKPATCH: ./board/esd/cpciiser4/fpgadata.c
CHECKPATCH: ./board/dave/PPChameleonEVB/fpgadata.c
avr32:mmu.c: fix printf() length modifier
fat.c: fix printf() length modifier
cmd_sf.c: fix printf() length modifier
Make printf and vprintf safe from buffer overruns
vsprintf: Move function documentation into header file
Add safe vsnprintf and snprintf library functions
Move vsprintf functions into their own header

Conflicts:
tools/mkenvimage.c

Signed-off-by: Wolfgang Denk <wd@denx.de>


# af44f4b2 21-Dec-2011 Horst Kronstorfer <hkronsto@frequentis.com>

Print program basename instead of whole path in usage()

Signed-off-by: Horst Kronstorfer <hkronsto@frequentis.com>

# 1895420b 04-Dec-2011 Horst Kronstorfer <hkronsto@frequentis.com>

mkenvimage: Add version info switch (-V)

Signed-off-by: Horst Kronstorfer <hkronsto@frequentis.com>

# 5e0c63e2 04-Dec-2011 Horst Kronstorfer <hkronsto@frequentis.com>

mkenvimage: Fix getopt() error handling

Since the original implementation indicates explicit error handling
we turn off getopt()'s internal error messaging to avoid duplicate
error messages. Additionally we add ':' (missing option argument)
error handling.

Signed-off-by: Horst Kronstorfer <hkronsto@frequentis.com>

# d30011bf 04-Dec-2011 Horst Kronstorfer <hkronsto@frequentis.com>

mkenvimage: Fix some typos

Signed-off-by: Horst Kronstorfer <hkronsto@frequentis.com>

# a6337e6f 25-Sep-2011 David Wagner <david.wagner@free-electrons.com>

new tool mkenvimage: generates an env image from an arbitrary config file

This tool takes a key=value configuration file (same as would a `printenv' show)
and generates the corresponding environment image, ready to be flashed.

use case: flash the environment with an external tool

Signed-off-by: David Wagner <david.wagner@free-electrons.com>
Acked-by; Mike Frysinger <vapier@gentoo.org>
Tested-by: Thomas Petazzoni <thomas.petazzoni@free-electrons.com>

# 849f9bef 29-Jun-2019 Andre Przywara <andre.przywara@arm.com>

tools: mkenvimage: Always consider non-regular files

At the moment mkenvimage has two separate read paths: One to read from
a potential pipe, while dynamically increasing the buffer size, and a
second one using mmap(2), using the input file's size. This is
problematic for two reasons:
- The "pipe" path will be chosen if the input filename is missing or
"-". Any named, but non-regular file will use the other path, which
typically will cause mmap() to fail:
$ mkenvimage -s 256 -o out <(echo "foo=bar")
- There is no reason to have *two* ways of reading a file, since the
"pipe way" will always work, even for regular files.

Fix this (and simplify the code on the way) by always using the method
of dynamically resizing the buffer. The existing distinction between
the two cases will merely be used to use the open() syscall or not.

Signed-off-by: Andre Przywara <andre.przywara@arm.com>

# 40e7b3ce 29-Jun-2019 Andre Przywara <andre.przywara@arm.com>

tools: mkenvimage: Fix reading from slow pipe

It is perfectly fine for the read(2) syscall to return with less than
the requested number of bytes read (short read, see the "RETURN VALUE"
section of the man page). This typically happens with slow input
(keyboard, network) or with complex pipes.

So far mkenvimage expects the exact number of requested bytes to be
read, assuming an end-of-file condition otherwise. This wrong behaviour
can be easily shown with:
$ (echo "foo=bar"; sleep 1; echo "bar=baz") | mkenvimage -s 256 -o out -
The second line will be missing from the output.

Correct this by checking for any positive, non-zero return value.

This fixes a problem with a complex pipe in one of my scripts, where
the environment consist of two parts.

Signed-off-by: Andre Przywara <andre.przywara@arm.com>
Acked-by: Alexander Dahl <ada@thorsis.com>

# 83d290c5 06-May-2018 Tom Rini <trini@konsulko.com>

SPDX: Convert all of our single license tags to Linux Kernel style

When U-Boot started using SPDX tags we were among the early adopters and
there weren't a lot of other examples to borrow from. So we picked the
area of the file that usually had a full license text and replaced it
with an appropriate SPDX-License-Identifier: entry. Since then, the
Linux Kernel has adopted SPDX tags and they place it as the very first
line in a file (except where shebangs are used, then it's second line)
and with slightly different comment styles than us.

In part due to community overlap, in part due to better tag visibility
and in part for other minor reasons, switch over to that style.

This commit changes all instances where we have a single declared
license in the tag as both the before and after are identical in tag
contents. There's also a few places where I found we did not have a tag
and have introduced one.

Signed-off-by: Tom Rini <trini@konsulko.com>

# c3b115f4 20-Apr-2018 Alexander Dahl <ada@thorsis.com>

tools: mkenvimage: Fix possible segfault on stdin input

The size of 'filebuf' was not increased as more and more bytes are read
from stdin, but 'filebuf' was always reallocated to the same fix size.
This works as long as only less bytes than the initial buffer size come
in, for more input this will segfault. (It actually does, I tested
that.) So for each loop cycle the buffer size has to be increased by the
number of bytes we want to read.

Signed-off-by: Alexander Dahl <ada@thorsis.com>

# 3559028c 20-Apr-2018 Alexander Dahl <ada@thorsis.com>

tools: mkenvimage: Fix read() stdin error handling

On success read() returns the number of bytes read or zero for EOF. On
error -1 is returned and errno is set, so the right way to test if read
had failed is to test the return value instead of errno.

Signed-off-by: Alexander Dahl <ada@thorsis.com>

# 80ee0196 12-Mar-2015 Brian McFarland <mcfarlandjb@gmail.com>

Patch to mkenvimage to handle text files with length that exceed env size

The current head revision of mkenvimage
(e72be8947e129f5ab274c0a9f235d2cc0014b2ea) will prevent you from creating
an env image from a text file that is larger than the env length specified
by the '-s' option. That doesn't make sense given that the tool now allows
comments and blank lines. This patch removes that limitation and allows
longer text files to be used.

I don't have time / desire at the moment to figure out "patman" and could
really care less if this is adopted up stream. Just figured I would share
in case anybody else finds it useful enough to take time to do a proper
patch.

>From 39ff30190c2bf687861f4b4b33230f1944fb64f9 Mon Sep 17 00:00:00 2001
From: Brian McFarland <bmcfarland@rldrake.com>
Date: Thu, 12 Mar 2015 11:37:19 -0400
Subject: [PATCH] In mkenvimage, removed the check that prevented using a
source text file larger than the output environment image. Instead, the main
parsing loop checks to see if the environment buffer is full, and quits if it
is. After the main parse loop, a second loop swallows comments and
whitespace until either the EOF is reached or more env vars are found, in
which case an error will be thrown.

# e72be894 27-Aug-2014 Dominik Muth <dominik.muth@bkvibro.com>

Added support for comments in input to mkenvimage.

This patch adds support for comments in the input to mkenvimage, i.e. in
the environment source: All lines starting with a # in the firs column
will be ignored.

Additionally empty lines will also be ignored.

Signed-off-by: Dominik Muth <dominik.muth@bkvibro.com>

# 2db1c3fc 30-Jul-2013 York Sun <yorksun@freescale.com>

tools/Makefile: Move _GNU_SOURCE to Makefile

Commit 669dfc2e adds libfdt_env.h to HOSTCPPFLAGS. It causes stdio.h
to be included before _GNU_SOURCE is defined in C files. On some old hosts
some prototypes are protected by #ifdef __USE_GNU, which is set when
_GNU_SOURCE is defined.

Signed-off-by: York Sun <yorksun@freescale.com>
Acked-by: Simon Glass <sjg@chromium.org>

# 1a459660 08-Jul-2013 Wolfgang Denk <wd@denx.de>

Add GPL-2.0+ SPDX-License-Identifier to source files

Signed-off-by: Wolfgang Denk <wd@denx.de>
[trini: Fixup common/cmd_io.c]
Signed-off-by: Tom Rini <trini@ti.com>

# 4f7136e7 18-Jul-2012 Mike Frysinger <vapier@gentoo.org>

tools: clean up mingw ifdefs

We have a header file specifically for mingw cruft, so keep it there
to avoid crap spreading into the main tools. This lets our devs just
worry about *nix systems.

Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# 8b6a4952 07-Jul-2012 Vladimir Yakovlev <nagos@inbox.ru>

tools: Fix mingw tools build

mkenvimage does not build due to missed os_support.o and unsupported
file modes S_IRGRP S_IWGRP.
Tested with mingw 4.2.1 on ubuntu 12.04.

Signed-off-by: Vladimir Yakovlev <nagos@inbox.ru>

# 558cd995 28-Jun-2012 Andreas Bießmann <andreas.devel@googlemail.com>

tools/mkenvimage.c: fix basename(3) usage

Use the POSIX variant of basename due to BSD systems (e.g. OS X) do not provide
GNU version of basename(3). It is save to use the POSIX variant here cause we do
never use argv[0] later on which may be modified by the basename(3) POSIX
variant.
On systems providing GNU variant the GNU variant should be used since string.h
is included before libgen.h. Therefore let the _GNU_SOURCE as is.

This patch fixes following warning (on OS X):
---8<---
mkenvimage.c: In function ‘main’:
mkenvimage.c:105: warning: implicit declaration of function ‘basename’
mkenvimage.c:105: warning: assignment makes pointer from integer without a cast
--->8---

Signed-off-by: Andreas Bießmann <andreas.devel@googlemail.com>
cc: Keith Mok <ek9852@gmail.com>

# 1ebff63f 05-Apr-2012 Dirk Behme <dirk.behme@googlemail.com>

mkenvimage: Fix compiler warning

Fix the compiler warning

mkenvimage.c: In function ‘main’:
mkenvimage.c:218: warning: format ‘%ld’ expects type ‘long int’, but argument 3 has type ‘unsigned int’
mkenvimage.c:226: warning: format ‘%ld’ expects type ‘long int’, but argument 3 has type ‘unsigned int’

introduced with the commit

mkenvimage: Use mmap() when reading from a regular file
6ee39f8055680654f9cc97b98dcce9588f1ab71e

Signed-off-by: Dirk Behme <dirk.behme@googlemail.com>
CC: David Wagner <david.wagner@free-electrons.com>
CC: Anatolij Gustschin <agust@denx.de>
CC: Mike Frysinger <vapier@gentoo.org>
Acked-by: Mike Frysinger <vapier@gentoo.org>
Acked-by: David Wagner <deubeuliou@gmail.com>

# d8d26599 13-Jan-2012 David Wagner <david.wagner@free-electrons.com>

mkenvimage: Really set the redundant byte when applicable

Signed-off-by: David Wagner <david.wagner@free-electrons.com>
Acked-by: Mike Frysinger <vapier@gentoo.org>

# dc6449a9 13-Jan-2012 David Wagner <david.wagner@free-electrons.com>

mkenvimage: Don't try to detect comments in the input file

Remove this feature since it seems impossible to reliably detect them.

Signed-off-by: David Wagner <david.wagner@free-electrons.com>
Acked-by: Mike Frysinger <vapier@gentoo.org>

# 6ee39f80 13-Jan-2012 David Wagner <david.wagner@free-electrons.com>

mkenvimage: Use mmap() when reading from a regular file

Fall back to read() if it fails.

Signed-off-by: David Wagner <david.wagner@free-electrons.com>
Acked-by: Mike Frysinger <vapier@gentoo.org>

# 48995b5a 13-Jan-2012 David Wagner <david.wagner@free-electrons.com>

mkenvimage: Read/Write from/to stdin/out by default or if the filename is "-"

Signed-off-by: David Wagner <david.wagner@free-electrons.com>
Acked-by: Mike Frysinger <vapier@gentoo.org>

# 3d0f9bd0 13-Jan-2012 David Wagner <david.wagner@free-electrons.com>

mkenvimage: More error handling

Verbosly fail if the target environment size or the padding byte are badly
formated.

Verbosly fail if something bad happens when reading from standard input.

Signed-off-by: David Wagner <david.wagner@free-electrons.com>
Acked-by: Mike Frysinger <vapier@gentoo.org>

# d1acdae9 13-Jan-2012 David Wagner <david.wagner@free-electrons.com>

mkenvimage: Correct an include and add a missing one

compiler.h needs to be included from U-Boot's headers.
Also, group U-Boot-specific includes together

stdlib.h was missing.

Signed-off-by: David Wagner <david.wagner@free-electrons.com>
Acked-by: Mike Frysinger <vapier@gentoo.org>

# 8a1b8fc7 13-Jan-2012 David Wagner <david.wagner@free-electrons.com>

mkenvimage: correct and clarify comments and error messages

Also, don't split error messages over several lines as per a coding style
exception making them easier to grep.

Signed-off-by: David Wagner <david.wagner@free-electrons.com>
Acked-by: Mike Frysinger <vapier@gentoo.org>

# e758a5c4 01-Mar-2012 Wolfgang Denk <wd@denx.de>

mkenvimage: fix usage message

Don't use argv[0] for usage() because it may or may not be clobbered
by the previous call to basename(). Use "prg" instead as it is done
in the rest of the code.

Signed-off-by: Wolfgang Denk <wd@denx.de>
Acked-by: Mike Frysinger <vapier@gentoo.org>

# dbee61db 20-Dec-2011 David Wagner <david.wagner@free-electrons.com>

Correctly handle input files beginning with several newlines

Also, fix some comments (minor)

Signed-off-by: David Wagner <david.wagner@free-electrons.com>

# 72ebafbe 23-Dec-2011 Horst Kronstorfer <hkronsto@frequentis.com>

tools/mkenvimage.c: Fix a merge issue

With bfcc40bb09b05c90cc3b1496abb270eb8aa72134 'optopt' was reverted.

Signed-off-by: Horst Kronstorfer <hkronsto@frequentis.com>

# bfcc40bb 23-Dec-2011 Wolfgang Denk <wd@denx.de>

Merge branch 'next' of ../next

* 'next' of ../next:
mkenvimage: Add version info switch (-V)
mkenvimage: Fix getopt() error handling
mkenvimage: Fix some typos
phy: add Micrel KS8721BL phy definition
net: introduce per device index
mvgbe: remove setting of ethaddr within the driver
x86: Add support for specifying an initrd with the zboot command
x86: Refactor the zboot innards so they can be reused with a vboot image
x86: Add infrastructure to extract an e820 table from the coreboot tables
x86: Add support for booting Linux using the 32 bit boot protocol
x86: Clean up the x86 zimage code in preparation to extend it
x86: Import code from coreboot's libpayload to parse the coreboot table
x86: Initial commit for running as a coreboot payload
CHECKPATCH: ./board/esd/hh405/logo_320_240_8bpp.c
CHECKPATCH: ./board/esd/hh405/logo_1024_768_8bpp.c
CHECKPATCH: ./board/esd/hh405/logo_320_240_4bpp.c
CHECKPATCH: ./board/esd/hh405/logo_640_480_24bpp.c
CHECKPATCH: ./board/esd/apc405/logo_640_480_24bpp.c
CHECKPATCH: ./board/esd/voh405/logo_320_240_4bpp.c
CHECKPATCH: ./board/esd/voh405/logo_640_480_24bpp.c
CHECKPATCH: ./board/esd/hh405/fpgadata.c
CHECKPATCH: ./board/esd/pci405/fpgadata.c
CHECKPATCH: ./board/esd/tasreg/fpgadata.c
CHECKPATCH: ./board/esd/apc405/fpgadata.c
CHECKPATCH: ./board/esd/voh405/fpgadata.c
CHECKPATCH: ./board/esd/ash405/fpgadata.c
CHECKPATCH: ./board/esd/dasa_sim/fpgadata.c
CHECKPATCH: ./board/esd/ar405/fpgadata_xl30.c
CHECKPATCH: ./board/esd/ar405/fpgadata.c
CHECKPATCH: ./board/esd/plu405/fpgadata.c
CHECKPATCH: ./board/esd/wuh405/fpgadata.c
CHECKPATCH: ./board/esd/cpci405/fpgadata_cpci405.c
CHECKPATCH: ./board/esd/cpci405/fpgadata_cpci405ab.c
CHECKPATCH: ./board/esd/cpci405/fpgadata_cpci4052.c
CHECKPATCH: ./board/esd/canbt/fpgadata.c
CHECKPATCH: ./board/esd/du405/fpgadata.c
CHECKPATCH: ./board/esd/cpciiser4/fpgadata.c
CHECKPATCH: ./board/dave/PPChameleonEVB/fpgadata.c
avr32:mmu.c: fix printf() length modifier
fat.c: fix printf() length modifier
cmd_sf.c: fix printf() length modifier
Make printf and vprintf safe from buffer overruns
vsprintf: Move function documentation into header file
Add safe vsnprintf and snprintf library functions
Move vsprintf functions into their own header

Conflicts:
tools/mkenvimage.c

Signed-off-by: Wolfgang Denk <wd@denx.de>


# af44f4b2 21-Dec-2011 Horst Kronstorfer <hkronsto@frequentis.com>

Print program basename instead of whole path in usage()

Signed-off-by: Horst Kronstorfer <hkronsto@frequentis.com>

# 1895420b 04-Dec-2011 Horst Kronstorfer <hkronsto@frequentis.com>

mkenvimage: Add version info switch (-V)

Signed-off-by: Horst Kronstorfer <hkronsto@frequentis.com>

# 5e0c63e2 04-Dec-2011 Horst Kronstorfer <hkronsto@frequentis.com>

mkenvimage: Fix getopt() error handling

Since the original implementation indicates explicit error handling
we turn off getopt()'s internal error messaging to avoid duplicate
error messages. Additionally we add ':' (missing option argument)
error handling.

Signed-off-by: Horst Kronstorfer <hkronsto@frequentis.com>

# d30011bf 04-Dec-2011 Horst Kronstorfer <hkronsto@frequentis.com>

mkenvimage: Fix some typos

Signed-off-by: Horst Kronstorfer <hkronsto@frequentis.com>

# a6337e6f 25-Sep-2011 David Wagner <david.wagner@free-electrons.com>

new tool mkenvimage: generates an env image from an arbitrary config file

This tool takes a key=value configuration file (same as would a `printenv' show)
and generates the corresponding environment image, ready to be flashed.

use case: flash the environment with an external tool

Signed-off-by: David Wagner <david.wagner@free-electrons.com>
Acked-by; Mike Frysinger <vapier@gentoo.org>
Tested-by: Thomas Petazzoni <thomas.petazzoni@free-electrons.com>

# 83d290c5 06-May-2018 Tom Rini <trini@konsulko.com>

SPDX: Convert all of our single license tags to Linux Kernel style

When U-Boot started using SPDX tags we were among the early adopters and
there weren't a lot of other examples to borrow from. So we picked the
area of the file that usually had a full license text and replaced it
with an appropriate SPDX-License-Identifier: entry. Since then, the
Linux Kernel has adopted SPDX tags and they place it as the very first
line in a file (except where shebangs are used, then it's second line)
and with slightly different comment styles than us.

In part due to community overlap, in part due to better tag visibility
and in part for other minor reasons, switch over to that style.

This commit changes all instances where we have a single declared
license in the tag as both the before and after are identical in tag
contents. There's also a few places where I found we did not have a tag
and have introduced one.

Signed-off-by: Tom Rini <trini@konsulko.com>


# c3b115f4 20-Apr-2018 Alexander Dahl <ada@thorsis.com>

tools: mkenvimage: Fix possible segfault on stdin input

The size of 'filebuf' was not increased as more and more bytes are read
from stdin, but 'filebuf' was always reallocated to the same fix size.
This works as long as only less bytes than the initial buffer size come
in, for more input this will segfault. (It actually does, I tested
that.) So for each loop cycle the buffer size has to be increased by the
number of bytes we want to read.

Signed-off-by: Alexander Dahl <ada@thorsis.com>


# 3559028c 20-Apr-2018 Alexander Dahl <ada@thorsis.com>

tools: mkenvimage: Fix read() stdin error handling

On success read() returns the number of bytes read or zero for EOF. On
error -1 is returned and errno is set, so the right way to test if read
had failed is to test the return value instead of errno.

Signed-off-by: Alexander Dahl <ada@thorsis.com>


# 80ee0196 12-Mar-2015 Brian McFarland <mcfarlandjb@gmail.com>

Patch to mkenvimage to handle text files with length that exceed env size

The current head revision of mkenvimage
(e72be8947e129f5ab274c0a9f235d2cc0014b2ea) will prevent you from creating
an env image from a text file that is larger than the env length specified
by the '-s' option. That doesn't make sense given that the tool now allows
comments and blank lines. This patch removes that limitation and allows
longer text files to be used.

I don't have time / desire at the moment to figure out "patman" and could
really care less if this is adopted up stream. Just figured I would share
in case anybody else finds it useful enough to take time to do a proper
patch.

>From 39ff30190c2bf687861f4b4b33230f1944fb64f9 Mon Sep 17 00:00:00 2001
From: Brian McFarland <bmcfarland@rldrake.com>
Date: Thu, 12 Mar 2015 11:37:19 -0400
Subject: [PATCH] In mkenvimage, removed the check that prevented using a
source text file larger than the output environment image. Instead, the main
parsing loop checks to see if the environment buffer is full, and quits if it
is. After the main parse loop, a second loop swallows comments and
whitespace until either the EOF is reached or more env vars are found, in
which case an error will be thrown.


# e72be894 27-Aug-2014 Dominik Muth <dominik.muth@bkvibro.com>

Added support for comments in input to mkenvimage.

This patch adds support for comments in the input to mkenvimage, i.e. in
the environment source: All lines starting with a # in the firs column
will be ignored.

Additionally empty lines will also be ignored.

Signed-off-by: Dominik Muth <dominik.muth@bkvibro.com>


# 2db1c3fc 30-Jul-2013 York Sun <yorksun@freescale.com>

tools/Makefile: Move _GNU_SOURCE to Makefile

Commit 669dfc2e adds libfdt_env.h to HOSTCPPFLAGS. It causes stdio.h
to be included before _GNU_SOURCE is defined in C files. On some old hosts
some prototypes are protected by #ifdef __USE_GNU, which is set when
_GNU_SOURCE is defined.

Signed-off-by: York Sun <yorksun@freescale.com>
Acked-by: Simon Glass <sjg@chromium.org>


# 1a459660 08-Jul-2013 Wolfgang Denk <wd@denx.de>

Add GPL-2.0+ SPDX-License-Identifier to source files

Signed-off-by: Wolfgang Denk <wd@denx.de>
[trini: Fixup common/cmd_io.c]
Signed-off-by: Tom Rini <trini@ti.com>


# 4f7136e7 18-Jul-2012 Mike Frysinger <vapier@gentoo.org>

tools: clean up mingw ifdefs

We have a header file specifically for mingw cruft, so keep it there
to avoid crap spreading into the main tools. This lets our devs just
worry about *nix systems.

Signed-off-by: Mike Frysinger <vapier@gentoo.org>


# 8b6a4952 07-Jul-2012 Vladimir Yakovlev <nagos@inbox.ru>

tools: Fix mingw tools build

mkenvimage does not build due to missed os_support.o and unsupported
file modes S_IRGRP S_IWGRP.
Tested with mingw 4.2.1 on ubuntu 12.04.

Signed-off-by: Vladimir Yakovlev <nagos@inbox.ru>


# 558cd995 28-Jun-2012 Andreas Bießmann <andreas.devel@googlemail.com>

tools/mkenvimage.c: fix basename(3) usage

Use the POSIX variant of basename due to BSD systems (e.g. OS X) do not provide
GNU version of basename(3). It is save to use the POSIX variant here cause we do
never use argv[0] later on which may be modified by the basename(3) POSIX
variant.
On systems providing GNU variant the GNU variant should be used since string.h
is included before libgen.h. Therefore let the _GNU_SOURCE as is.

This patch fixes following warning (on OS X):
---8<---
mkenvimage.c: In function ‘main’:
mkenvimage.c:105: warning: implicit declaration of function ‘basename’
mkenvimage.c:105: warning: assignment makes pointer from integer without a cast
--->8---

Signed-off-by: Andreas Bießmann <andreas.devel@googlemail.com>
cc: Keith Mok <ek9852@gmail.com>


# 1ebff63f 05-Apr-2012 Dirk Behme <dirk.behme@googlemail.com>

mkenvimage: Fix compiler warning

Fix the compiler warning

mkenvimage.c: In function ‘main’:
mkenvimage.c:218: warning: format ‘%ld’ expects type ‘long int’, but argument 3 has type ‘unsigned int’
mkenvimage.c:226: warning: format ‘%ld’ expects type ‘long int’, but argument 3 has type ‘unsigned int’

introduced with the commit

mkenvimage: Use mmap() when reading from a regular file
6ee39f8055680654f9cc97b98dcce9588f1ab71e

Signed-off-by: Dirk Behme <dirk.behme@googlemail.com>
CC: David Wagner <david.wagner@free-electrons.com>
CC: Anatolij Gustschin <agust@denx.de>
CC: Mike Frysinger <vapier@gentoo.org>
Acked-by: Mike Frysinger <vapier@gentoo.org>
Acked-by: David Wagner <deubeuliou@gmail.com>


# d8d26599 13-Jan-2012 David Wagner <david.wagner@free-electrons.com>

mkenvimage: Really set the redundant byte when applicable

Signed-off-by: David Wagner <david.wagner@free-electrons.com>
Acked-by: Mike Frysinger <vapier@gentoo.org>


# dc6449a9 13-Jan-2012 David Wagner <david.wagner@free-electrons.com>

mkenvimage: Don't try to detect comments in the input file

Remove this feature since it seems impossible to reliably detect them.

Signed-off-by: David Wagner <david.wagner@free-electrons.com>
Acked-by: Mike Frysinger <vapier@gentoo.org>


# 6ee39f80 13-Jan-2012 David Wagner <david.wagner@free-electrons.com>

mkenvimage: Use mmap() when reading from a regular file

Fall back to read() if it fails.

Signed-off-by: David Wagner <david.wagner@free-electrons.com>
Acked-by: Mike Frysinger <vapier@gentoo.org>


# 48995b5a 13-Jan-2012 David Wagner <david.wagner@free-electrons.com>

mkenvimage: Read/Write from/to stdin/out by default or if the filename is "-"

Signed-off-by: David Wagner <david.wagner@free-electrons.com>
Acked-by: Mike Frysinger <vapier@gentoo.org>


# 3d0f9bd0 13-Jan-2012 David Wagner <david.wagner@free-electrons.com>

mkenvimage: More error handling

Verbosly fail if the target environment size or the padding byte are badly
formated.

Verbosly fail if something bad happens when reading from standard input.

Signed-off-by: David Wagner <david.wagner@free-electrons.com>
Acked-by: Mike Frysinger <vapier@gentoo.org>


# d1acdae9 13-Jan-2012 David Wagner <david.wagner@free-electrons.com>

mkenvimage: Correct an include and add a missing one

compiler.h needs to be included from U-Boot's headers.
Also, group U-Boot-specific includes together

stdlib.h was missing.

Signed-off-by: David Wagner <david.wagner@free-electrons.com>
Acked-by: Mike Frysinger <vapier@gentoo.org>


# 8a1b8fc7 13-Jan-2012 David Wagner <david.wagner@free-electrons.com>

mkenvimage: correct and clarify comments and error messages

Also, don't split error messages over several lines as per a coding style
exception making them easier to grep.

Signed-off-by: David Wagner <david.wagner@free-electrons.com>
Acked-by: Mike Frysinger <vapier@gentoo.org>


# e758a5c4 01-Mar-2012 Wolfgang Denk <wd@denx.de>

mkenvimage: fix usage message

Don't use argv[0] for usage() because it may or may not be clobbered
by the previous call to basename(). Use "prg" instead as it is done
in the rest of the code.

Signed-off-by: Wolfgang Denk <wd@denx.de>
Acked-by: Mike Frysinger <vapier@gentoo.org>


# dbee61db 20-Dec-2011 David Wagner <david.wagner@free-electrons.com>

Correctly handle input files beginning with several newlines

Also, fix some comments (minor)

Signed-off-by: David Wagner <david.wagner@free-electrons.com>


# 72ebafbe 23-Dec-2011 Horst Kronstorfer <hkronsto@frequentis.com>

tools/mkenvimage.c: Fix a merge issue

With bfcc40bb09b05c90cc3b1496abb270eb8aa72134 'optopt' was reverted.

Signed-off-by: Horst Kronstorfer <hkronsto@frequentis.com>


# af44f4b2 21-Dec-2011 Horst Kronstorfer <hkronsto@frequentis.com>

Print program basename instead of whole path in usage()

Signed-off-by: Horst Kronstorfer <hkronsto@frequentis.com>


# 1895420b 04-Dec-2011 Horst Kronstorfer <hkronsto@frequentis.com>

mkenvimage: Add version info switch (-V)

Signed-off-by: Horst Kronstorfer <hkronsto@frequentis.com>


# 5e0c63e2 04-Dec-2011 Horst Kronstorfer <hkronsto@frequentis.com>

mkenvimage: Fix getopt() error handling

Since the original implementation indicates explicit error handling
we turn off getopt()'s internal error messaging to avoid duplicate
error messages. Additionally we add ':' (missing option argument)
error handling.

Signed-off-by: Horst Kronstorfer <hkronsto@frequentis.com>


# d30011bf 04-Dec-2011 Horst Kronstorfer <hkronsto@frequentis.com>

mkenvimage: Fix some typos

Signed-off-by: Horst Kronstorfer <hkronsto@frequentis.com>


# a6337e6f 25-Sep-2011 David Wagner <david.wagner@free-electrons.com>

new tool mkenvimage: generates an env image from an arbitrary config file

This tool takes a key=value configuration file (same as would a `printenv' show)
and generates the corresponding environment image, ready to be flashed.

use case: flash the environment with an external tool

Signed-off-by: David Wagner <david.wagner@free-electrons.com>
Acked-by; Mike Frysinger <vapier@gentoo.org>
Tested-by: Thomas Petazzoni <thomas.petazzoni@free-electrons.com>