]>
git.ipfire.org Git - people/ms/u-boot.git/blob - tools/imls/imls.c
2 * (C) Copyright 2009 Marco Stornelli
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of
7 * the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
26 #include <sys/types.h>
27 #include <sys/ioctl.h>
34 #include <linux/mtd/mtd.h>
36 #define __user /* nothing */
37 #include <mtd/mtd-user.h>
43 #include <fdt_support.h>
46 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
48 extern unsigned long crc32(unsigned long crc
, const char *buf
, unsigned int len
);
49 static void usage(void);
50 static int image_verify_header(char *ptr
, int fd
);
51 static int flash_bad_block(int fd
, uint8_t mtd_type
, loff_t start
);
56 unsigned int sectorcount
= 0;
58 unsigned int sectoroffset
= 0;
59 unsigned int sectorsize
= 0;
62 int main (int argc
, char **argv
)
64 int fd
= -1, err
= 0, readbyte
= 0, j
;
65 struct mtd_info_user mtdinfo
;
66 char buf
[sizeof(image_header_t
)];
71 while (--argc
> 0 && **++argv
== '-') {
77 sectorcount
= (unsigned int)atoi(*++argv
);
83 sectoroffset
= (unsigned int)atoi(*++argv
);
89 sectorsize
= (unsigned int)atoi(*++argv
);
99 if (argc
!= 1 || cflag
== 0 || sflag
== 0)
104 fd
= open(devicefile
, O_RDONLY
);
106 fprintf (stderr
, "%s: Can't open %s: %s\n",
107 cmdname
, devicefile
, strerror(errno
));
111 err
= ioctl(fd
, MEMGETINFO
, &mtdinfo
);
113 fprintf(stderr
, "%s: Cannot get MTD information: %s\n",cmdname
,
118 if (mtdinfo
.type
!= MTD_NORFLASH
&& mtdinfo
.type
!= MTD_NANDFLASH
) {
119 fprintf(stderr
, "%s: Unsupported flash type %u\n",
120 cmdname
, mtdinfo
.type
);
124 if (sectorsize
* sectorcount
!= mtdinfo
.size
) {
125 fprintf(stderr
, "%s: Partition size (%d) incompatible with "
126 "sector size and count\n", cmdname
, mtdinfo
.size
);
130 if (sectorsize
* sectoroffset
>= mtdinfo
.size
) {
131 fprintf(stderr
, "%s: Partition size (%d) incompatible with "
132 "sector offset given\n", cmdname
, mtdinfo
.size
);
136 if (sectoroffset
> sectorcount
- 1) {
137 fprintf(stderr
, "%s: Sector offset cannot be grater than "
138 "sector count minus one\n", cmdname
);
142 printf("Searching....\n");
144 for (j
= sectoroffset
; j
< sectorcount
; ++j
) {
146 if (lseek(fd
, j
*sectorsize
, SEEK_SET
) != j
*sectorsize
) {
147 fprintf(stderr
, "%s: lseek failure: %s\n",
148 cmdname
, strerror(errno
));
152 err
= flash_bad_block(fd
, mtdinfo
.type
, j
*sectorsize
);
156 continue; /* Skip and jump to next */
158 readbyte
= read(fd
, buf
, sizeof(image_header_t
));
159 if (readbyte
!= sizeof(image_header_t
)) {
160 fprintf(stderr
, "%s: Can't read from device: %s\n",
161 cmdname
, strerror(errno
));
165 if (fdt_check_header(buf
)) {
166 /* old-style image */
167 if (image_verify_header(buf
, fd
)) {
169 image_print_contents((image_header_t
*)buf
);
173 fit_print_contents(buf
);
181 printf("No images found\n");
188 fprintf (stderr
, "Usage:\n"
189 " %s [-o offset] -s size -c count device\n"
190 " -o ==> number of sectors to use as offset\n"
191 " -c ==> number of sectors\n"
192 " -s ==> size of sectors (byte)\n",
198 static int image_verify_header(char *ptr
, int fd
)
203 image_header_t
*hdr
= (image_header_t
*)ptr
;
206 if (image_get_magic(hdr
) != IH_MAGIC
)
210 len
= image_get_header_size();
212 checksum
= image_get_hcrc(hdr
);
213 hdr
->ih_hcrc
= htonl(0); /* clear for re-calculation */
215 if (crc32(0, data
, len
) != checksum
) {
217 "%s: Maybe image found but it has bad header checksum!\n",
222 len
= image_get_size(hdr
);
226 nread
= read(fd
, buf
, MIN(len
,PAGE_SIZE
));
227 if (nread
!= MIN(len
,PAGE_SIZE
)) {
229 "%s: Error while reading: %s\n",
230 cmdname
, strerror(errno
));
233 checksum
= crc32(checksum
, buf
, nread
);
237 if (checksum
!= image_get_dcrc(hdr
)) {
239 "%s: Maybe image found but it has corrupted data!\n",
248 * Test for bad block on NAND, just returns 0 on NOR, on NAND:
251 * < 0 - failed to test
253 static int flash_bad_block(int fd
, uint8_t mtd_type
, loff_t start
)
255 if (mtd_type
== MTD_NANDFLASH
) {
256 int badblock
= ioctl(fd
, MEMGETBADBLOCK
, &start
);
259 fprintf(stderr
,"%s: Cannot read bad block mark: %s\n",
260 cmdname
, strerror(errno
));