]>
git.ipfire.org Git - thirdparty/u-boot.git/blob - tools/imls/imls.c
2 * (C) Copyright 2009 Marco Stornelli
4 * SPDX-License-Identifier: GPL-2.0+
13 #include <sys/types.h>
14 #include <sys/ioctl.h>
21 #include <linux/mtd/mtd.h>
23 #define __user /* nothing */
24 #include <mtd/mtd-user.h>
29 #include <fdt_support.h>
32 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
34 extern unsigned long crc32(unsigned long crc
, const char *buf
, unsigned int len
);
35 static void usage(void);
36 static int image_verify_header(char *ptr
, int fd
);
37 static int flash_bad_block(int fd
, uint8_t mtd_type
, loff_t start
);
42 unsigned int sectorcount
= 0;
44 unsigned int sectoroffset
= 0;
45 unsigned int sectorsize
= 0;
48 int main (int argc
, char **argv
)
50 int fd
= -1, err
= 0, readbyte
= 0, j
;
51 struct mtd_info_user mtdinfo
;
52 char buf
[sizeof(image_header_t
)];
57 while (--argc
> 0 && **++argv
== '-') {
63 sectorcount
= (unsigned int)atoi(*++argv
);
69 sectoroffset
= (unsigned int)atoi(*++argv
);
75 sectorsize
= (unsigned int)atoi(*++argv
);
85 if (argc
!= 1 || cflag
== 0 || sflag
== 0)
90 fd
= open(devicefile
, O_RDONLY
);
92 fprintf (stderr
, "%s: Can't open %s: %s\n",
93 cmdname
, devicefile
, strerror(errno
));
97 err
= ioctl(fd
, MEMGETINFO
, &mtdinfo
);
99 fprintf(stderr
, "%s: Cannot get MTD information: %s\n",cmdname
,
104 if (mtdinfo
.type
!= MTD_NORFLASH
&& mtdinfo
.type
!= MTD_NANDFLASH
) {
105 fprintf(stderr
, "%s: Unsupported flash type %u\n",
106 cmdname
, mtdinfo
.type
);
110 if (sectorsize
* sectorcount
!= mtdinfo
.size
) {
111 fprintf(stderr
, "%s: Partition size (%d) incompatible with "
112 "sector size and count\n", cmdname
, mtdinfo
.size
);
116 if (sectorsize
* sectoroffset
>= mtdinfo
.size
) {
117 fprintf(stderr
, "%s: Partition size (%d) incompatible with "
118 "sector offset given\n", cmdname
, mtdinfo
.size
);
122 if (sectoroffset
> sectorcount
- 1) {
123 fprintf(stderr
, "%s: Sector offset cannot be grater than "
124 "sector count minus one\n", cmdname
);
128 printf("Searching....\n");
130 for (j
= sectoroffset
; j
< sectorcount
; ++j
) {
132 if (lseek(fd
, j
*sectorsize
, SEEK_SET
) != j
*sectorsize
) {
133 fprintf(stderr
, "%s: lseek failure: %s\n",
134 cmdname
, strerror(errno
));
138 err
= flash_bad_block(fd
, mtdinfo
.type
, j
*sectorsize
);
142 continue; /* Skip and jump to next */
144 readbyte
= read(fd
, buf
, sizeof(image_header_t
));
145 if (readbyte
!= sizeof(image_header_t
)) {
146 fprintf(stderr
, "%s: Can't read from device: %s\n",
147 cmdname
, strerror(errno
));
151 if (fdt_check_header(buf
)) {
152 /* old-style image */
153 if (image_verify_header(buf
, fd
)) {
155 image_print_contents((image_header_t
*)buf
);
159 fit_print_contents(buf
);
167 printf("No images found\n");
174 fprintf (stderr
, "Usage:\n"
175 " %s [-o offset] -s size -c count device\n"
176 " -o ==> number of sectors to use as offset\n"
177 " -c ==> number of sectors\n"
178 " -s ==> size of sectors (byte)\n",
184 static int image_verify_header(char *ptr
, int fd
)
189 image_header_t
*hdr
= (image_header_t
*)ptr
;
192 if (image_get_magic(hdr
) != IH_MAGIC
)
196 len
= image_get_header_size();
198 checksum
= image_get_hcrc(hdr
);
199 hdr
->ih_hcrc
= htonl(0); /* clear for re-calculation */
201 if (crc32(0, data
, len
) != checksum
) {
203 "%s: Maybe image found but it has bad header checksum!\n",
208 len
= image_get_size(hdr
);
212 nread
= read(fd
, buf
, MIN(len
,PAGE_SIZE
));
213 if (nread
!= MIN(len
,PAGE_SIZE
)) {
215 "%s: Error while reading: %s\n",
216 cmdname
, strerror(errno
));
219 checksum
= crc32(checksum
, buf
, nread
);
223 if (checksum
!= image_get_dcrc(hdr
)) {
225 "%s: Maybe image found but it has corrupted data!\n",
234 * Test for bad block on NAND, just returns 0 on NOR, on NAND:
237 * < 0 - failed to test
239 static int flash_bad_block(int fd
, uint8_t mtd_type
, loff_t start
)
241 if (mtd_type
== MTD_NANDFLASH
) {
242 int badblock
= ioctl(fd
, MEMGETBADBLOCK
, &start
);
245 fprintf(stderr
,"%s: Cannot read bad block mark: %s\n",
246 cmdname
, strerror(errno
));