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>
42 #include <fdt_support.h>
45 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
47 extern unsigned long crc32(unsigned long crc, const char *buf, unsigned int len);
48 static void usage(void);
49 static int image_verify_header(char *ptr, int fd);
50 static int flash_bad_block(int fd, uint8_t mtd_type, loff_t start);
55 unsigned int sectorcount = 0;
57 unsigned int sectoroffset = 0;
58 unsigned int sectorsize = 0;
61 int main (int argc, char **argv)
63 int fd = -1, err = 0, readbyte = 0, j;
64 struct mtd_info_user mtdinfo;
65 char buf[sizeof(image_header_t)];
70 while (--argc > 0 && **++argv == '-') {
76 sectorcount = (unsigned int)atoi(*++argv);
82 sectoroffset = (unsigned int)atoi(*++argv);
88 sectorsize = (unsigned int)atoi(*++argv);
98 if (argc != 1 || cflag == 0 || sflag == 0)
103 fd = open(devicefile, O_RDONLY);
105 fprintf (stderr, "%s: Can't open %s: %s\n",
106 cmdname, devicefile, strerror(errno));
110 err = ioctl(fd, MEMGETINFO, &mtdinfo);
112 fprintf(stderr, "%s: Cannot get MTD information: %s\n",cmdname,
117 if (mtdinfo.type != MTD_NORFLASH && mtdinfo.type != MTD_NANDFLASH) {
118 fprintf(stderr, "%s: Unsupported flash type %u\n",
119 cmdname, mtdinfo.type);
123 if (sectorsize * sectorcount != mtdinfo.size) {
124 fprintf(stderr, "%s: Partition size (%d) incompatible with "
125 "sector size and count\n", cmdname, mtdinfo.size);
129 if (sectorsize * sectoroffset >= mtdinfo.size) {
130 fprintf(stderr, "%s: Partition size (%d) incompatible with "
131 "sector offset given\n", cmdname, mtdinfo.size);
135 if (sectoroffset > sectorcount - 1) {
136 fprintf(stderr, "%s: Sector offset cannot be grater than "
137 "sector count minus one\n", cmdname);
141 printf("Searching....\n");
143 for (j = sectoroffset; j < sectorcount; ++j) {
145 if (lseek(fd, j*sectorsize, SEEK_SET) != j*sectorsize) {
146 fprintf(stderr, "%s: lseek failure: %s\n",
147 cmdname, strerror(errno));
151 err = flash_bad_block(fd, mtdinfo.type, j*sectorsize);
155 continue; /* Skip and jump to next */
157 readbyte = read(fd, buf, sizeof(image_header_t));
158 if (readbyte != sizeof(image_header_t)) {
159 fprintf(stderr, "%s: Can't read from device: %s\n",
160 cmdname, strerror(errno));
164 if (fdt_check_header(buf)) {
165 /* old-style image */
166 if (image_verify_header(buf, fd)) {
168 image_print_contents((image_header_t *)buf);
172 fit_print_contents(buf);
180 printf("No images found\n");
187 fprintf (stderr, "Usage:\n"
188 " %s [-o offset] -s size -c count device\n"
189 " -o ==> number of sectors to use as offset\n"
190 " -c ==> number of sectors\n"
191 " -s ==> size of sectors (byte)\n",
197 static int image_verify_header(char *ptr, int fd)
202 image_header_t *hdr = (image_header_t *)ptr;
205 if (image_get_magic(hdr) != IH_MAGIC)
209 len = image_get_header_size();
211 checksum = image_get_hcrc(hdr);
212 hdr->ih_hcrc = htonl(0); /* clear for re-calculation */
214 if (crc32(0, data, len) != checksum) {
216 "%s: Maybe image found but it has bad header checksum!\n",
221 len = image_get_size(hdr);
225 nread = read(fd, buf, MIN(len,PAGE_SIZE));
226 if (nread != MIN(len,PAGE_SIZE)) {
228 "%s: Error while reading: %s\n",
229 cmdname, strerror(errno));
232 checksum = crc32(checksum, buf, nread);
236 if (checksum != image_get_dcrc(hdr)) {
238 "%s: Maybe image found but it has corrupted data!\n",
247 * Test for bad block on NAND, just returns 0 on NOR, on NAND:
250 * < 0 - failed to test
252 static int flash_bad_block(int fd, uint8_t mtd_type, loff_t start)
254 if (mtd_type == MTD_NANDFLASH) {
255 int badblock = ioctl(fd, MEMGETBADBLOCK, &start);
258 fprintf(stderr,"%s: Cannot read bad block mark: %s\n",
259 cmdname, strerror(errno));