Re: [U-Boot] [PATCHv10] new tool mkenvimage: generates an env image from an arbitrary config file
On 14/10/2011 19:16, David Wagner 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. use case: flash the environment with an external tool Signed-off-by: David Wagner david.wag...@free-electrons.com Acked-by: Mike Frysinger vap...@gentoo.org Tested-by: Thomas Petazzoni thomas.petazz...@free-electrons.com Applied to u-boot-staging, sba...@denx.de, thanks. Best regards, Stefano Babic -- = DENX Software Engineering GmbH, MD: Wolfgang Denk Detlev Zundel HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany Phone: +49-8142-66989-0 Fax: +49-8142-66989-80 Email: off...@denx.de = ___ U-Boot mailing list U-Boot@lists.denx.de http://lists.denx.de/mailman/listinfo/u-boot
Re: [U-Boot] [PATCHv10] new tool mkenvimage: generates an env image from an arbitrary config file
On Sunday 23 October 2011 16:44:38 Wolfgang Denk wrote: David Wagner 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. use case: flash the environment with an external tool This patch fails to build when I try to run a plain make tools/mkenvimage: tools/mkenvimage.c:35:22: fatal error: compiler.h: No such file or directory tools/mkenvimage.c:39:24: fatal error: u-boot/crc.h: No such file or directory i think that's expected. if you do `make tools`, does it build correctly ? -mike signature.asc Description: This is a digitally signed message part. ___ U-Boot mailing list U-Boot@lists.denx.de http://lists.denx.de/mailman/listinfo/u-boot
Re: [U-Boot] [PATCHv10] new tool mkenvimage: generates an env image from an arbitrary config file
Dear David Wagner, In message 1318612616-16799-1-git-send-email-david.wag...@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. use case: flash the environment with an external tool This patch fails to build when I try to run a plain make tools/mkenvimage: tools/mkenvimage.c:35:22: fatal error: compiler.h: No such file or directory tools/mkenvimage.c:39:24: fatal error: u-boot/crc.h: No such file or directory ... + /* Parse the cmdline */ + while ((option = getopt(argc, argv, s:o:rbp:h)) != -1) { + switch (option) { + case 's': + datasize = strtol(optarg, NULL, 0); ... + padbyte = strtol(optarg, NULL, 0); Please add error checking for invalid input formats here. + if (datasize == 0) { + fprintf(stderr, + Please specify the size of the envrionnment + partition.\n); Please don't split that string, and fix the envrionment typo. + dataptr = malloc(datasize * sizeof(*dataptr)); + if (!dataptr) { + fprintf(stderr, Can't alloc dataptr.\n); It might be helpful to know how many bytes you were trying to allocate. + /* + * envptr points to the beginning of the actual environment (after the + * crc and possible `redundant' bit s/bit/byte/ + /* Open the input file ... */ + if (optind = argc) { + fprintf(stderr, Please specify an input filename\n); + return EXIT_FAILURE; + } Please also allow to use stdin as input when no input file arg is given. + int readlen = sizeof(*envptr) * 2048; + txt_fd = STDIN_FILENO; + + do { + filebuf = realloc(filebuf, readlen); + readbytes = read(txt_fd, filebuf + filesize, readlen); + filesize += readbytes; + } while (readbytes == readlen); This is pretty much inefficient. Consider a size increment of something like min(readlen, 4096). Also, realloc() can fail - add error handling. And read() can fail, too - add error handling. + filesize = txt_file_stat.st_size; + /* Read the raw input file and transform it */ Why don't you just use mmap() here? + if (filesize = envsize) { + fprintf(stderr, The input file is larger than the + envrionnment partition size\n); Please don't split such strings. See CodingStyle: However, never break user-visible strings such as printk messages, because that breaks the ability to grep for them. Please fix globally. + } else if (filebuf[fp] == '#') { + if (fp != 0 filebuf[fp-1] == '\n') { + /* This line is a comment, let's skip it */ + while (fp txt_file_stat.st_size +filebuf[fp] != '\n') + fp++; + } else { + envptr[ep++] = filebuf[fp]; + } printenv output does not contain any such comments. And - aren't you also catching embedded hashes here, like in serial# for example? ... + + /* Computes the CRC and put it at the beginning of the data */ + crc = crc32(0, envptr, envsize); + targetendian_crc = bigendian ? cpu_to_be32(crc) : cpu_to_le32(crc); + + memcpy(dataptr, targetendian_crc, sizeof(uint32_t)); I fail to see where you set the redundant flag? 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: w...@denx.de I am more bored than you could ever possibly be. Go back to work. ___ U-Boot mailing list U-Boot@lists.denx.de http://lists.denx.de/mailman/listinfo/u-boot
Re: [U-Boot] [PATCHv10] new tool mkenvimage: generates an env image from an arbitrary config file
Hello, Le Fri, 14 Oct 2011 19:16:56 +0200, David Wagner david.wag...@free-electrons.com a écrit : 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.wag...@free-electrons.com Acked-by: Mike Frysinger vap...@gentoo.org Tested-by: Thomas Petazzoni thomas.petazz...@free-electrons.com Unless there are further comments, would it be possible to get this merged for the upcoming U-Boot, or is the merge window already closed? Thanks! Thomas -- Thomas Petazzoni, Free Electrons Kernel, drivers, real-time and embedded Linux development, consulting, training and support. http://free-electrons.com ___ U-Boot mailing list U-Boot@lists.denx.de http://lists.denx.de/mailman/listinfo/u-boot
[U-Boot] [PATCHv10] 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.wag...@free-electrons.com Acked-by: Mike Frysinger vap...@gentoo.org Tested-by: Thomas Petazzoni thomas.petazz...@free-electrons.com --- change since v9: * fix a typo in the commit message spotted by Detlev Zundel tools/Makefile |5 + tools/mkenvimage.c | 272 2 files changed, 277 insertions(+), 0 deletions(-) create mode 100644 tools/mkenvimage.c diff --git a/tools/Makefile b/tools/Makefile index fc741d3..da7caf0 100644 --- a/tools/Makefile +++ b/tools/Makefile @@ -66,6 +66,7 @@ BIN_FILES-$(CONFIG_BUILD_ENVCRC) += envcrc$(SFX) BIN_FILES-$(CONFIG_CMD_NET) += gen_eth_addr$(SFX) BIN_FILES-$(CONFIG_CMD_LOADS) += img2srec$(SFX) BIN_FILES-$(CONFIG_XWAY_SWAP_BYTES) += xway-swap-bytes$(SFX) +BIN_FILES-y += mkenvimage$(SFX) BIN_FILES-y += mkimage$(SFX) BIN_FILES-$(CONFIG_NETCONSOLE) += ncb$(SFX) BIN_FILES-$(CONFIG_SHA1_CHECK_UB_IMG) += ubsha1$(SFX) @@ -89,6 +90,7 @@ OBJ_FILES-$(CONFIG_XWAY_SWAP_BYTES) += xway-swap-bytes.o NOPED_OBJ_FILES-y += kwbimage.o NOPED_OBJ_FILES-y += imximage.o NOPED_OBJ_FILES-y += omapimage.o +NOPED_OBJ_FILES-y += mkenvimage.o NOPED_OBJ_FILES-y += mkimage.o OBJ_FILES-$(CONFIG_NETCONSOLE) += ncb.o NOPED_OBJ_FILES-y += os_support.o @@ -184,6 +186,9 @@ $(obj)xway-swap-bytes$(SFX):$(obj)xway-swap-bytes.o $(HOSTCC) $(HOSTCFLAGS) $(HOSTLDFLAGS) -o $@ $^ $(HOSTSTRIP) $@ +$(obj)mkenvimage$(SFX):$(obj)crc32.o $(obj)mkenvimage.o + $(HOSTCC) $(HOSTCFLAGS) $(HOSTLDFLAGS) -o $@ $^ + $(obj)mkimage$(SFX): $(obj)crc32.o \ $(obj)default_image.o \ $(obj)fit_image.o \ diff --git a/tools/mkenvimage.c b/tools/mkenvimage.c new file mode 100644 index 000..2c7fbe7 --- /dev/null +++ b/tools/mkenvimage.c @@ -0,0 +1,272 @@ +/* + * (C) Copyright 2011 Free Electrons + * David Wagner david.wag...@free-electrons.com + * + * Inspired from envcrc.c: + * (C) Copyright 2001 + * Paolo Scaffardi, AIRVENT SAM s.p.a - RIMINI(ITALY), arse...@tin.it + * + * See file CREDITS for list of people who contributed to this + * project. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, + * MA 02111-1307 USA + */ + +#include errno.h +#include fcntl.h +#include stdio.h +#include stdlib.h +#include stdint.h +#include string.h +#include unistd.h +#include compiler.h +#include sys/types.h +#include sys/stat.h + +#include u-boot/crc.h + +#define CRC_SIZE sizeof(uint32_t) + +static void usage(const char *exec_name) +{ + fprintf(stderr, %s [-h] [-r] [-b] [-p byte] + -s environment partition size -o output input file\n + \n + This tool takes a key=value input file (same as would a + `printenv' show) and generates the corresponding environment + image, ready to be flashed.\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 multiple copies in flash\n + \t-b : the target is big endian (default is little endian)\n + \t-p byte : fill the image with byte bytes instead of + 0xff bytes\n + \n + If the input file is \-\, data is read from standard input\n, + exec_name); +} + +int main(int argc, char **argv) +{ + uint32_t crc, targetendian_crc; + const char *txt_filename = NULL, *bin_filename = NULL; + int txt_fd, bin_fd; + unsigned char *dataptr, *envptr; + unsigned char *filebuf = NULL; + unsigned int filesize = 0, envsize = 0, datasize = 0; + int bigendian = 0; + int redundant = 0; + unsigned char padbyte = 0xff; + + int option; + int ret = EXIT_SUCCESS; + + struct stat txt_file_stat; + + int fp, ep; + + /* Parse the cmdline */ + while ((option = getopt(argc, argv, s:o:rbp:h)) != -1) { + switch (option) { + case 's': + datasize