Add imls utility command
PATCH v3 1/1 Add imls utility command
Fixed a memory leak in image_verify_header function. Used the functions image_get_xxxx() from image.h. Fixed two sector boundary misalignment while reading.
---
tools/imls.c +229 -0
@@ -0,0 +1,229 @@ +/* +* (C) Copyright 2009 Marco Stornelli +* +* 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 "imls.h" + +extern unsigned long crc32 (unsigned long crc,const char *buf,unsigned int len); +static void usage (void ); +static int image_verify_header (char *ptr,int fd); + +char *cmdname; +char *devicefile; + +unsigned int sectorcount =0 ; +int sflag =0 ; +unsigned int sectoroffset =0 ; +unsigned int sectorsize =0 ; +int cflag =0 ; + +int main (int argc,char **argv) +{ +int fd =-1 , err =0 , readbyte =0 , j; +uint32_t mtdsize, dim, diff; +struct mtd_info_user mtdinfo; +char *buf; +int found =0 ; + + cmdname = *argv; + +while (--argc >0 && **++argv =='-' ) { +while (*++*argv) { +switch (**argv) { +case 's' : +if (--argc <=0 ) +usage (); + sectorcount = (unsigned int )atoi (*++argv); + sflag =1 ; + goto NXTARG; +case 'o' : +if (--argc <=0 ) +usage (); + sectoroffset = (unsigned int )atoi (*++argv); + goto NXTARG; + +case 'c' : +if (--argc <=0 ) +usage (); + sectorsize = (unsigned int )atoi (*++argv); + cflag =1 ; + goto NXTARG; +default : +usage (); + } + } +NXTARG: ; + } + +if (argc !=1 || cflag ==0 || sflag ==0 ) +usage (); + + devicefile = *argv; + + fd =open (devicefile,O_RDONLY ); +if (fd <0 ) { +fprintf (stderr,"%s: Can't open %s: %s\n" , + cmdname, devicefile,strerror (errno)); +exit (EXIT_FAILURE ); + } + + err =ioctl (fd,MEMGETINFO , &mtdinfo); +if (err <0 ) { +fprintf (stderr,"%s: Cannot get MTD information: %s\n" ,cmdname,strerror (errno)); +exit (EXIT_FAILURE ); + } + + mtdsize = mtdinfo.size; + +if (sectorsize * sectorcount != mtdsize) { +fprintf (stderr,"%s: Partition size (%d) incompatible with sector size and count\n" , cmdname, mtdsize); +exit (EXIT_FAILURE ); + } + +if (sectorsize * sectoroffset >= mtdsize) { +fprintf (stderr,"%s: Partition size (%d) incompatible with sector offset given\n" , cmdname, mtdsize); +exit (EXIT_FAILURE ); + } + +if (sectoroffset > sectorcount -1 ) { +fprintf (stderr,"%s: Sector offset cannot be grater than sector count minus one\n" , cmdname); +exit (EXIT_FAILURE ); + } + + dim =image_get_header_size (); + +if (dim > sectorsize) { +fprintf (stderr,"%s: Image header bigger than sector size\n" , cmdname); +exit (EXIT_FAILURE ); + } + + diff = sectorsize - dim; + + buf =malloc (sizeof (char )*dim); +if (!buf) { +fprintf (stderr,"%s: Can't allocate memory: %s\n" , + cmdname,strerror (errno)); +exit (EXIT_FAILURE ); + } + +if (sectoroffset !=0 ) +lseek (fd, sectoroffset*sectorsize,SEEK_SET ); + +printf ("Searching....\n" ); + +for (j = sectoroffset; j < sectorcount; ++j) { + + readbyte =read (fd, buf, dim); +if (readbyte != dim) { +fprintf (stderr,"%s: Can't read from device: %s\n" , + cmdname,strerror (errno)); +exit (EXIT_FAILURE ); + } + +if (fdt_check_header (buf)) { +/* old-style image */ +if (image_verify_header (buf, fd)) { + found =1 ; +image_print_contents ((image_header_t *)buf); + } + }else { +/* FIT image */ +fit_print_contents (buf); + } + +lseek (fd, diff,SEEK_CUR );/* Align to next sector boundary */ + + } + +free (buf); + +close (fd); + +if (!found) +printf ("No images found\n" ); + +exit (EXIT_SUCCESS ); +} + +void usage () +{ +fprintf (stderr,"Usage:\n" +" %s [-o offset] -s count -c size \n" +" -o ==> number of sectors to use as offset\n" +" -s ==> number of sectors\n" +" -c ==> size of sectors\n" , + cmdname); + +exit (EXIT_FAILURE ); +} + +static int image_verify_header (char *ptr,int fd) +{ +int len, n; +char *data; +uint32_t checksum; +image_header_t *hdr = (image_header_t *)ptr; + +if (image_get_magic (hdr) !=IH_MAGIC ) { +return 0 ; + } + + data = (char *)hdr; + len =image_get_header_size (); + + checksum =image_get_hcrc (hdr); + hdr->ih_hcrc =htonl (0 );/* clear for re-calculation */ + +if (crc32 (0 , data, len) != checksum) { +fprintf (stderr, +"%s: Maybe image found but it has bad header checksum!\n" , + cmdname); +return 0 ; + } + + len =image_get_size (hdr); + data =malloc (len *sizeof (char )); +if (!data) { +fprintf (stderr,"%s: Can't allocate memory: %s\n" , + cmdname,strerror (errno)); +exit (EXIT_FAILURE ); + } + + n =read (fd, data, len); +if (n != len) { +fprintf (stderr, +"%s: Error while reading: %s\n" , + cmdname,strerror (errno)); +exit (EXIT_FAILURE ); + } + +lseek (fd, -len,SEEK_CUR ); + +if (crc32 (0 , data, len) !=image_get_dcrc (hdr)) { +fprintf (stderr, +"%s: Maybe image found but it has corrupted data!\n" , + cmdname); +free (data); +return 0 ; + } + +free (data); + +return 1 ; +} +
tools/imls.h +41 -0
41 lines not on this page · raw message
tools/Makefile +9 -2
@@ -21,10 +21,10 @@ # MA 02111-1307 USA # -BIN_FILES = img2srec$(SFX) mkimage$(SFX) envcrc$(SFX) ubsha1$(SFX) gen_eth_addr$(SFX) bmp_logo$(SFX) +BIN_FILES = img2srec$(SFX) mkimage$(SFX) imls$(SFX) envcrc$(SFX) ubsha1$(SFX) gen_eth_addr$(SFX) bmp_logo$(SFX) OBJ_LINKS = env_embedded.o crc32.o md5.o sha1.o image.o -OBJ_FILES = img2srec.o mkimage.o envcrc.o ubsha1.o gen_eth_addr.o bmp_logo.o +OBJ_FILES = img2srec.o mkimage.o imls.o envcrc.o ubsha1.o gen_eth_addr.o bmp_logo.o ifeq ($(ARCH),mips) BIN_FILES += inca-swap-bytes$(SFX)
@@ -151,6 +151,10 @@ $(obj)mkimage$(SFX): $(obj)mkimage.o $(o $(CC) $(CFLAGS) $(HOST_LDFLAGS) -o $@ $^ $(STRIP) $@ +$(obj)imls$(SFX): $(obj)imls.o $(obj)crc32.o $(obj)image.o $(obj)md5.o $(obj)sha1.o $(LIBFDT_OBJ_FILES) + $(CC) $(CFLAGS) $(HOST_LDFLAGS) -o $@ $^ + $(STRIP) $@ + $(obj)ncb$(SFX): $(obj)ncb.o $(CC) $(CFLAGS) $(HOST_LDFLAGS) -o $@ $^ $(STRIP) $@
@@ -196,6 +200,9 @@ $(obj)image.o: $(obj)image.c $(obj)mkimage.o: $(src)mkimage.c $(CC) -g $(FIT_CFLAGS) -c -o $@ $< +$(obj)imls.o: $(src)imls.c + $(CC) -g $(FIT_CFLAGS) -c -o $@ $< + $(obj)ncb.o: $(src)ncb.c $(CC) -g $(CFLAGS) -c -o $@ $<
PATCH v3 1/1 Add imls utility command
Dear Marco,
Quoted 3 lines · In message <[email protected]> you wrote:
Fixed a memory leak in
image_verify_headerfunction. Used the functionsimage_get_xxxx()fromimage.h. Fixed two sector boundary misalignment while reading.
...
tools/imls.c line 87
Quoted 3 lines
tools/imls.c +3
-0+ err =ioctl (fd,MEMGETINFO , &mtdinfo); +if (err <0 ) { +fprintf (stderr,"%s: Cannot get MTD information: %s\n" ,cmdname,strerror (errno));
Line too long (check also rest of file).
Review
Quoted 2 lines
(quoted diff) +2
-0+ exit(EXIT_FAILURE); + }
You probably want to check for unsupported flash types, too - see example in "tools/env/fw_env.c"
tools/imls.c line 96
Quoted 6 lines
tools/imls.c +6
-0+ mtdsize = mtdinfo.size; + +if (sectorsize * sectorcount != mtdsize) { +fprintf (stderr,"%s: Partition size (%d) incompatible with sector size and count\n" , cmdname, mtdsize); +exit (EXIT_FAILURE ); + }
Hmm... what happens if an image starts in one of the small sectors in a bottom boot type flash?
...
tools/imls.c line 117
Quoted 1 line
tools/imls.c +1
-0+ buf =malloc (sizeof (char )*dim);
Why not just use a dynamic array on the stack? Get rid of all this malloc() and free() stuff...
tools/imls.c line 125
Quoted 8 lines
tools/imls.c +8
-0+if (!buf) { +fprintf (stderr,"%s: Can't allocate memory: %s\n" , + cmdname,strerror (errno)); +exit (EXIT_FAILURE ); + } + +if (sectoroffset !=0 ) +lseek (fd, sectoroffset*sectorsize,SEEK_SET );
Delete these two lines and mode them into the loop...
tools/imls.c line 129
Quoted 3 lines
tools/imls.c +3
-0+printf ("Searching....\n" ); + +for (j = sectoroffset; j < sectorcount; ++j) {
i. e. add
if (lseek(fd, j*sectorsize, SEEK_SET) != j*sectorsize) {
error handling goes here
}Quoted 9 lines
tools/imls.c +9
-0+ readbyte =read (fd, buf, dim); +if (readbyte != dim) { +fprintf (stderr,"%s: Can't read from device: %s\n" , + cmdname,strerror (errno)); +exit (EXIT_FAILURE ); + } + +if (fdt_check_header (buf)) { +/* old-style image */
tools/imls.c line 149
Quoted 10 lines
tools/imls.c +10
-0+if (image_verify_header (buf, fd)) { + found =1 ; +image_print_contents ((image_header_t *)buf); + } + }else { +/* FIT image */ +fit_print_contents (buf); + } + +lseek (fd, diff,SEEK_CUR );/* Align to next sector boundary */
Delete this line then, too.
tools/imls.c line 170
Quoted 8 lines
tools/imls.c +8
-0+void usage () +{ +fprintf (stderr,"Usage:\n" +" %s [-o offset] -s count -c size \n" +" -o ==> number of sectors to use as offset\n" +" -s ==> number of sectors\n" +" -c ==> size of sectors\n" , + cmdname);
Usage message is not correct - the device argument is missing.
Quoted 19 lines
tools/imls.c +19
-0+exit (EXIT_FAILURE ); +} + +static int image_verify_header (char *ptr,int fd) +{ +int len, n; +char *data; +uint32_t checksum; +image_header_t *hdr = (image_header_t *)ptr; + +if (image_get_magic (hdr) !=IH_MAGIC ) { +return 0 ; + } + + data = (char *)hdr; + len =image_get_header_size (); + + checksum =image_get_hcrc (hdr); + hdr->ih_hcrc =htonl (0 );/* clear for re-calculation */
tools/imls.c line 200
Quoted 10 lines
tools/imls.c +10
-0+ +if (crc32 (0 , data, len) != checksum) { +fprintf (stderr, +"%s: Maybe image found but it has bad header checksum!\n" , + cmdname); +return 0 ; + } + + len =image_get_size (hdr); + data =malloc (len *sizeof (char ));
Come on. sizeof(char) ? You must be joking.
tools/imls.c line 205
Quoted 5 lines
tools/imls.c +5
-0+if (!data) { +fprintf (stderr,"%s: Can't allocate memory: %s\n" , + cmdname,strerror (errno)); +exit (EXIT_FAILURE ); + }
Get rid of this malloc(), too.
tools/imls.c line 215
Quoted 9 lines
tools/imls.c +9
-0+ n =read (fd, data, len); +if (n != len) { +fprintf (stderr, +"%s: Error while reading: %s\n" , + cmdname,strerror (errno)); +exit (EXIT_FAILURE ); + } + +lseek (fd, -len,SEEK_CUR );
Why would this lseek() be needed? [And ifit ws needed, you should check it's return value.]
Quoted 4 lines
diff -uprN
u-boot-2009.03-orig/tools/imls.hu-boot-2009.03/tools/imls.htools/imls.h +0
-0@@ -0,0 +1,41 @@
...
Quoted 12 lines
tools/imls.h +12
-0+#include <errno.h> +#include <fcntl.h> +#include <stdio.h> +#include <stdlib.h> +#include <stddef.h> +#include <string.h> +#include <sys/types.h> +#include <sys/ioctl.h> +#include <sys/stat.h> +#include <unistd.h> + +#ifdef MTD_OLD
tools/imls.h line 41
Quoted 10 lines
tools/imls.h +10
-0+# include <stdint.h> +# include <linux/mtd/mtd.h> +#else +# define __user/* nothing */ +# include <mtd/mtd-user.h> +#endif + +#include <sha1.h> +#include "fdt_host.h" +#include <image.h>
Such a header file makes no sense, especially with just a single user. Dump it.
tools/Makefile
Quoted 3 lines
diff -uprN u-boot-2009.03-orig/tools/Makefile u-boot-2009.03/tools/Makefile
tools/Makefile +0
-0
Please also rebase against current top of tree.
Best regards,
Wolfgang Denk
Signature · 4 lines
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 Swap read error. You lose your mind.
PATCH v3 1/1 Add imls utility command
Hi Wolfgang,
first of all thank you for your comments.
Wolfgang Denk ha scritto:
Quoted 6 lines
Dear Marco,
Quoted 3 lines · In message <[email protected]> you wrote:
Fixed a memory leak in
image_verify_headerfunction. Used the functionsimage_get_xxxx()fromimage.h. Fixed two sector boundary misalignment while reading.
Quoted 7 lines
...
tools/imls.c line 87
Quoted 3 lines
tools/imls.c +3
-0+ err =ioctl (fd,MEMGETINFO , &mtdinfo); +if (err <0 ) { +fprintf (stderr,"%s: Cannot get MTD information: %s\n" ,cmdname,strerror (errno));Line too long (check also rest of file).
Ok. I see other files where some lines exceed 80 columns, is the limit of 80? I'll fix it.
Quoted 5 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ exit(EXIT_FAILURE); + }You probably want to check for unsupported flash types, too - see example in "
tools/env/fw_env.c"
Ok.
Quoted 10 lines
tools/imls.c line 96
Quoted 7 lines
tools/imls.c +6
-0+ mtdsize = mtdinfo.size; + +if (sectorsize * sectorcount != mtdsize) { +fprintf (stderr,"%s: Partition size (%d) incompatible with sector size and count\n" , cmdname, mtdsize); +exit (EXIT_FAILURE ); + }Hmm... what happens if an image starts in one of the small sectors in a bottom boot type flash?
Do you mean with an asymmetric flash layout? My first idea was manage only homogeneous situation. However you could use sectorsize equals to the smallest size and then use the offset parameter to start the searching at the bottom of the flash.
Quoted 6 lines
...
tools/imls.c line 117
Quoted 1 line
tools/imls.c +1
-0+ buf =malloc (sizeof (char )*dim);Why not just use a dynamic array on the stack? Get rid of all this
malloc()andfree()stuff...
Do you mean a thing like this: char buf[sizeof(image_header_t)]; ? Ok no problem.
Quoted 18 lines
tools/imls.c line 125
Quoted 9 lines
tools/imls.c +8
-0+if (!buf) { +fprintf (stderr,"%s: Can't allocate memory: %s\n" , + cmdname,strerror (errno)); +exit (EXIT_FAILURE ); + } + +if (sectoroffset !=0 ) +lseek (fd, sectoroffset*sectorsize,SEEK_SET );Delete these two lines and mode them into the loop...
tools/imls.c line 129
Quoted 3 lines
tools/imls.c +3
-0+printf ("Searching....\n" ); + +for (j = sectoroffset; j < sectorcount; ++j) {i. e. add
Quoted 3 lines
if (lseek(fd, j*sectorsize, SEEK_SET) != j*sectorsize) {
error handling goes here
}Mmm...I don't well understand why. For example if a sector is 128k and the header size is for example of 100 byte, after the read below, at the beginning of the cycle, the offset will be 100 byte (we say that sectoroffset is 0) and for example we don't find any image. At next cycle if I do an lseek of 1*128K, I'll get (1*128K)+100 byte as offset, at next (2*128k) + 200 byte and so on. To do this I should do an lseek after the read as lseek(fd,-readbyte,SEEK_CUR).
Quoted 23 lines
Quoted 10 lines
tools/imls.c +9
-0+ readbyte =read (fd, buf, dim); +if (readbyte != dim) { +fprintf (stderr,"%s: Can't read from device: %s\n" , + cmdname,strerror (errno)); +exit (EXIT_FAILURE ); + } + +if (fdt_check_header (buf)) { +/* old-style image */ tools/imls.c line 149
Quoted 10 lines
tools/imls.c +10
-0+if (image_verify_header (buf, fd)) { + found =1 ; +image_print_contents ((image_header_t *)buf); + } + }else { +/* FIT image */ +fit_print_contents (buf); + } + +lseek (fd, diff,SEEK_CUR );/* Align to next sector boundary */ Delete this line then, too.
Quoted 10 lines
tools/imls.c line 170
Quoted 8 lines
tools/imls.c +8
-0+void usage () +{ +fprintf (stderr,"Usage:\n" +" %s [-o offset] -s count -c size \n" +" -o ==> number of sectors to use as offset\n" +" -s ==> number of sectors\n" +" -c ==> size of sectors\n" , + cmdname);Usage message is not correct - the device argument is missing.
Oops my fault. Ok.
Quoted 33 lines
Quoted 20 lines
tools/imls.c +19
-0+exit (EXIT_FAILURE ); +} + +static int image_verify_header (char *ptr,int fd) +{ +int len, n; +char *data; +uint32_t checksum; +image_header_t *hdr = (image_header_t *)ptr; + +if (image_get_magic (hdr) !=IH_MAGIC ) { +return 0 ; + } + + data = (char *)hdr; + len =image_get_header_size (); + + checksum =image_get_hcrc (hdr); + hdr->ih_hcrc =htonl (0 );/* clear for re-calculation */ tools/imls.c line 200
Quoted 10 lines
tools/imls.c +10
-0+ +if (crc32 (0 , data, len) != checksum) { +fprintf (stderr, +"%s: Maybe image found but it has bad header checksum!\n" , + cmdname); +return 0 ; + } + + len =image_get_size (hdr); + data =malloc (len *sizeof (char ));Come on. sizeof(char) ? You must be joking.
Quoted 7 lines
tools/imls.c line 205
Quoted 5 lines
tools/imls.c +5
-0+if (!data) { +fprintf (stderr,"%s: Can't allocate memory: %s\n" , + cmdname,strerror (errno)); +exit (EXIT_FAILURE ); + }Get rid of this
malloc(), too.
I can't know in this case the size of the data, to remove the malloc I have to allocate a fixed buffer of X byte, I think in this case a malloc is better.
Quoted 13 lines
tools/imls.c line 215
Quoted 10 lines
tools/imls.c +9
-0+ n =read (fd, data, len); +if (n != len) { +fprintf (stderr, +"%s: Error while reading: %s\n" , + cmdname,strerror (errno)); +exit (EXIT_FAILURE ); + } + +lseek (fd, -len,SEEK_CUR );Why would this
lseek()be needed? [And ifit ws needed, you should check it's return value.]
It was needed to be aligned to sector boundary and set the file offset as before to call this function (see my comment above).
Quoted 23 lines
Quoted 5 lines
diff -uprN
u-boot-2009.03-orig/tools/imls.hu-boot-2009.03/tools/imls.htools/imls.h +0
-0@@ -0,0 +1,41 @@...
Quoted 17 lines
tools/imls.h +17
-0+#include <errno.h> +#include <fcntl.h> +#include <stdio.h> +#include <stdlib.h> +#include <stddef.h> +#include <string.h> +#include <sys/types.h> +#include <sys/ioctl.h> +#include <sys/stat.h> +#include <unistd.h> + +#ifdef MTD_OLD +# include <stdint.h> +# include <linux/mtd/mtd.h> +#else +# define __user/* nothing */ +# include <mtd/mtd-user.h>
Quoted 8 lines
tools/imls.h line 41
Quoted 5 lines
tools/imls.h +5
-0+#endif + +#include <sha1.h> +#include "fdt_host.h" +#include <image.h> Such a header file makes no sense, especially with just a single user. Dump it.
Ok.
Quoted 6 lines
tools/Makefile
Quoted 4 lines
diff -uprN u-boot-2009.03-orig/tools/Makefile u-boot-2009.03/tools/Makefile
tools/Makefile +0
-0Please also rebase against current top of tree.
Ok.
Quoted 6 lines
Best regards,
Wolfgang Denk
Regards,
Marco
PATCH v3 1/1 Add imls utility command
Dear Marco,
Quoted 9 lines · In message <[email protected]> you wrote:
Quoted 8 lines
tools/imls.c line 129
Quoted 2 lines
tools/imls.c +1
-0+for (j = sectoroffset; j < sectorcount; ++j) {i. e. add
if (lseek(fd, j*sectorsize, SEEK_SET) != j*sectorsize) { error handling goes here }
Quoted 7 lines
Mmm...I don't well understand why. For example if a sector is 128k and the header size is for example of 100 byte, after the read below, at the beginning of the cycle, the offset will be 100 byte (we say that sectoroffset is 0) and for example we don't find any image. At next cycle if I do an lseek of 1*128K, I'll get (1*128K)+100 byte as offset, at next (2*128k) + 200 byte and so on. To do this I should do an lseek after the read as
lseek(fd,-readbyte,SEEK_CUR).
Please think about this again. We use a single lseek() before each read() to position the read pointer at the next sector boundary, i.e. where we need it. We don't care whe reit is after the read().
...
Quoted 12 lines
Quoted 5 lines
tools/imls.c line 200
Quoted 2 lines
tools/imls.c +2
-0+ len =image_get_size (hdr); + data =malloc (len *sizeof (char ));Come on. sizeof(char) ? You must be joking.
Quoted 7 lines
tools/imls.c line 205
Quoted 5 lines
tools/imls.c +5
-0+if (!data) { +fprintf (stderr,"%s: Can't allocate memory: %s\n" , + cmdname,strerror (errno)); +exit (EXIT_FAILURE ); + }Get rid of this
malloc(), too.
Quoted 3 lines
I can't know in this case the size of the data, to remove the malloc I have to allocate a fixed buffer of X byte, I think in this case a malloc is better.
No. Who claims you must read the whole image at once? Use a fixed buffer of a convenient size (say PAGE_SIZE), and read the data sequentially. No need to allocate zillions of megabytes of memory at once.
Quoted 7 lines
Quoted 4 lines
tools/imls.c line 215
Quoted 1 line
tools/imls.c +1
-0+lseek (fd, -len,SEEK_CUR );Why would this
lseek()be needed? [And ifit ws needed, you should check it's return value.]It was needed to be aligned to sector boundary and set the file offset as before to call this function (see my comment above).
Your thinking is inverted. Don't try to fix the read pointer after each read(). Set it as needed before the read() when necessary.
Best regards,
Wolfgang Denk
Signature · 5 lines
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 A memorandum is written not to inform the reader, but to protect the writer. -- Dean Acheson
PATCH v3 1/1 Add imls utility command
Hi Wolfgang,
2009/4/5 Wolfgang Denk <[email protected]>:
Quoted 20 lines
Dear Marco,
Quoted 9 lines · In message <[email protected]> you wrote:
Quoted 8 lines
Quoted 2 lines
+ ?for (j = sectoroffset; j < sectorcount; ++j) {
i. e. add
? ? ? ? ? ? if (
lseek(fd, j*sectorsize, SEEK_SET)!= j*sectorsize) { ? ? ? ? ? ? ? ? ? ? error handling goes here
? ? ? ? ? ? }Quoted 7 lines
Mmm...I don't well understand why. For example if a sector is 128k and the header size is for example of 100 byte, after the read below, at the beginning of the cycle, the offset will be 100 byte (we say that sectoroffset is 0) and for example we don't find any image. At next cycle if I do an lseek of 1*128K, I'll get (1*128K)+100 byte as offset, at next (2*128k) + 200 byte and so on. To do this I should do an lseek after the read as
lseek(fd,-readbyte,SEEK_CUR).
Quoted 4 lines
Please think about this again. We use a single
lseek()before eachread()to position the read pointer at the next sector boundary, i.e. where we need it. We don't care whe reit is after theread().
Oops sorry, I've read SEEK_CUR instead of SEEK_SET, ok in this way works.
Quoted 13 lines
...
Quoted 12 lines
Quoted 12 lines
Quoted 2 lines
+ ?len =
image_get_size(hdr);
+ ?data =malloc(len * sizeof(char));Come on. sizeof(char) ? You must be joking.
Quoted 5 lines
+ ?if (!data) {
+ ? ? ? ? ?fprintf (stderr, "%s: Can't allocate memory: %s\n",
+ ? ? ? ? ?cmdname, strerror(errno));
+ ? ? ? ? ?exit(EXIT_FAILURE);
+ ?}Get rid of this
malloc(), too.
Quoted 9 lines
Quoted 3 lines
I can't know in this case the size of the data, to remove the malloc I have to allocate a fixed buffer of X byte, I think in this case a malloc is better.
No. Who claims you must read the whole image at once? Use a fixed buffer of a convenient size (say
PAGE_SIZE), and read the data sequentially. No need to allocate zillions of megabytes of memory at once.
Ok, I'll try to do that sequentially.
Quoted 13 lines
Quoted 7 lines
Quoted 4 lines
Quoted 1 line
+ ?lseek(fd, -len,
SEEK_CUR);Why would this
lseek()be needed? [And ifit ws needed, you should check it's return value.]It was needed to be aligned to sector boundary and set the file offset as before to call this function (see my comment above).
Your thinking is inverted. Don't try to fix the read pointer after each
read(). Set it as needed before theread()when necessary.Best regards,
Quoted 9 lines
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 A memorandum is written not to inform the reader, but to protect ?the writer. ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? -- Dean Acheson
I'll send a new patch with the modification as soon as