From: Wolfgang Denk <wd@denx.de>
To: u-boot@lists.denx.de
Subject: [U-Boot] [PATCHv4] new tool mkenvimage: generates an env image from an arbitrary config file
Date: Tue, 30 Aug 2011 21:34:08 +0200 [thread overview]
Message-ID: <20110830193408.D83DB18C46FE@gemini.denx.de> (raw)
In-Reply-To: <1314619606-1172-1-git-send-email-david.wagner@free-electrons.com>
Dear David Wagner,
In message <1314619606-1172-1-git-send-email-david.wagner@free-electrons.com> you wrote:
> This tool takes a key=value configuration file (same as would a `printenv' show)
> and generates the corresponding environment image, ready to be flashed.
>
> For now, it doesn't work properly if environment variables have embedded
> newlines.
I think this should be added for compatibility with both the printenv
output and the result of "env export -t"
> @Wolgang:
> What is the advantage of using mmap() here ? the file is read entirely and
> sequentially ; does it make much of a difference compared to fread() ?
It's easier to go back in the stream without allocating buffers that
are at least as big as the file.
> PS: Until a proper way is found for replacing only newlines that separate two
> environment variables (and not the ones inside a variable), let's just warn that
> it isn't supported.
What do you mean by "Until a proper way is found"? There is nothing
to be found. Just have a look at the "env import" code which does
exactly that. Alternatively, as you are only dealing with text format
anyway, look if the character immediately preceeding the newline is a
backslash:
=> setenv x 'line 1
> line 2'
=> printenv
...
x=line 1\
line 2
> @@ -69,6 +69,7 @@ BIN_FILES-$(CONFIG_XWAY_SWAP_BYTES) += xway-swap-bytes$(SFX)
> BIN_FILES-y += mkimage$(SFX)
> BIN_FILES-$(CONFIG_NETCONSOLE) += ncb$(SFX)
> BIN_FILES-$(CONFIG_SHA1_CHECK_UB_IMG) += ubsha1$(SFX)
> +BIN_FILES-y += mkenvimage$(SFX)
Please keep list sorted.
> # Source files which exist outside the tools directory
> EXT_OBJ_FILES-$(CONFIG_BUILD_ENVCRC) += common/env_embedded.o
> @@ -94,6 +95,7 @@ OBJ_FILES-$(CONFIG_NETCONSOLE) += ncb.o
> NOPED_OBJ_FILES-y += os_support.o
> OBJ_FILES-$(CONFIG_SHA1_CHECK_UB_IMG) += ubsha1.o
> NOPED_OBJ_FILES-y += ublimage.o
> +NOPED_OBJ_FILES-y += mkenvimage.o
Ditto.
> # Don't build by default
> #ifeq ($(ARCH),ppc)
> @@ -172,6 +174,9 @@ $(obj)bmp_logo$(SFX): $(obj)bmp_logo.o
> $(obj)envcrc$(SFX): $(obj)crc32.o $(obj)env_embedded.o $(obj)envcrc.o $(obj)sha1.o
> $(HOSTCC) $(HOSTCFLAGS) $(HOSTLDFLAGS) -o $@ $^
>
> +$(obj)mkenvimage$(SFX): $(obj)crc32.o $(obj)mkenvimage.o
> + $(HOSTCC) $(HOSTCFLAGS) $(HOSTLDFLAGS) -o $@ $^
> +
> $(obj)gen_eth_addr$(SFX): $(obj)gen_eth_addr.o
> $(HOSTCC) $(HOSTCFLAGS) $(HOSTLDFLAGS) -o $@ $^
> $(HOSTSTRIP) $@
Ditto.
...
> +static void usage(const char *exec_name)
> +{
> + printf("%s [-h] [-r] [-b] [-p <byte>] "
> + "-s <envrionment partition size> -o <output> <input file>\n"
> + "\n"
> + "\tThe input file is in format:\n"
> + "\t\tkey1=value1\n"
> + "\t\tkey2=value2\n"
> + "\t\t...\n"
> + "\t-r : the environment has two copies in flash\n"
Please s/two/multiple/ or s/two/more than one/ - especially on NAND we
can have more than just 2 copies.
> + if (datasize == 0) {
> + fprintf(stderr,
> + "Please specify the size of the envrionnment "
s/envrionnment/environment/
Please fix globally (same error further down below).
> + /* Open the configuration file ... */
> + if (optind >= argc) {
> + fprintf(stderr, "Please specify a configuration filename\n");
> + return EXIT_FAILURE;
> + }
Why don;t you allow reading from stdin? It is good old Unix tradition
that all commands can be used in pipes. Also, input file name '-'
should select stdin.
Um... and why do you call it "configuration file"? It's not
configuration data, it's plain input data, so rather call it "input
file"
> + txt_filename = argv[optind];
> + txt_file = fopen(txt_filename, "rb");
> + if (!txt_file) {
> + fprintf(stderr, "Can't open configuration file \"%s\": %s\n",
> + txt_filename, strerror(errno));
Here it's even better to omit the "configuration file " part
completely.
> + }
> + /* ... and check it */
> + ret = fstat(fileno(txt_file), &txt_file_stat);
> + if (ret == -1) {
> + fprintf(stderr, "Can't stat() on configuration file \"%s\": "
> + " %s\n", txt_filename, strerror(errno));
Same here.
> + return EXIT_FAILURE;
> + }
> + /*
> + * The right test to do is "=>" (not ">") because of the additionnal
> + * ending \0. See below.
> + */
> + if (txt_file_stat.st_size >= envsize) {
> + fprintf(stderr, "The configuration file is larger than the "
> + "envrionnment partition size\n");
See note above.
> + for (i = 0 ; i < txt_file_stat.st_size ; i++)
> + if (envptr[i] == '\n')
> + envptr[i] = '\0';
This needs braces.
> + /*
> + * Make sure there is a final '\0' (necessary if the padding byte isn't
> + * 0x00 or if there wasn't a newline at the end of the configuration
> + * file)
The double '\0' termination is _always_ necessary. Please avoid
constructing special cases where there aren't any.
> + * And do it again on the next byte to mark the end of the environment.
> + */
> + if (i < envsize && envptr[i-1] != '\0') {
> + envptr[i++] = '\0';
> + /*
> + * The text file doesn't have an ending newline. We need to
> + * check the env size again to make sure we have room for two \0
> + */
> + if (i >= envsize) {
> + fprintf(stderr, "The configuration is too large for the"
> + " target environment storage\n");
> + return EXIT_FAILURE;
> + }
If you test this here, you can remove the test above.
> + bin_file = fopen(bin_filename, "wb");
> + if (!bin_file) {
> + fprintf(stderr, "Can't open output file \"%s\": %s\n",
> + bin_filename, strerror(errno));
> + return EXIT_FAILURE;
> + }
> +
> + if (fwrite(dataptr, sizeof(*dataptr), datasize, bin_file) != datasize) {
> + fprintf(stderr, "fwrite() failed: %s\n", strerror(errno));
> + return EXIT_FAILURE;
> + }
> +
> + ret = fclose(bin_file);
Is there any good reason for using stdio functions (fopen(), fread(),
fwrite(), fclose()) instead of plain system calls (open(), read(),
write(), close()) ?
Best regards,
Wolfgang Denk
--
DENX Software Engineering GmbH, MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
What's the sound a name makes when it's dropped?
next prev parent reply other threads:[~2011-08-30 19:34 UTC|newest]
Thread overview: 31+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-08-05 14:49 [U-Boot] [PATCH] new tool mkenvimage: generates an env image from an arbitrary config file David Wagner
2011-08-05 15:23 ` Thomas Petazzoni
2011-08-05 16:23 ` [U-Boot] [PATCHv2] " David Wagner
2011-08-06 11:18 ` Mike Frysinger
2011-08-08 8:16 ` David Wagner
2011-08-08 8:46 ` Albert ARIBAUD
2011-08-08 8:56 ` David Wagner
2011-08-08 18:53 ` Mike Frysinger
2011-08-09 10:31 ` [U-Boot] [PATCH] " David Wagner
2011-08-22 1:09 ` Mike Frysinger
2011-08-24 22:12 ` Wolfgang Denk
2011-08-29 12:06 ` [U-Boot] [PATCHv4] " David Wagner
2011-08-30 19:12 ` Mike Frysinger
2011-08-30 19:34 ` Wolfgang Denk [this message]
2011-09-01 15:57 ` [U-Boot] [PATCHv5] " David Wagner
2011-09-01 17:47 ` Mike Frysinger
2011-09-02 8:47 ` [U-Boot] [PATCH] " David Wagner
2011-09-02 8:48 ` David Wagner
2011-09-02 8:48 ` [U-Boot] [PATCHv6] " David Wagner
2011-09-02 14:35 ` Mike Frysinger
2011-09-26 12:10 ` Thomas Petazzoni
2011-09-26 13:26 ` [U-Boot] [PATCHv7] " David Wagner
2011-09-30 7:31 ` [U-Boot] [PATCHv8] " David Wagner
2011-10-06 21:17 ` Wolfgang Denk
2011-10-13 18:45 ` [U-Boot] [PATCHv9] " David Wagner
2011-10-14 8:21 ` Detlev Zundel
2011-10-14 17:16 ` [U-Boot] [PATCHv10] " David Wagner
2011-10-19 8:22 ` Thomas Petazzoni
2011-10-23 20:44 ` Wolfgang Denk
2011-10-31 0:49 ` Mike Frysinger
2011-11-22 8:48 ` Stefano Babic
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=20110830193408.D83DB18C46FE@gemini.denx.de \
--to=wd@denx.de \
--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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox