Merge branch 'master' of git://git.denx.de/u-boot-arm
[oweals/u-boot.git] / tools / imls / imls.c
1 /*
2  * (C) Copyright 2009 Marco Stornelli
3  *
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.
8  *
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.
13  *
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,
17  * MA 02111-1307 USA
18  */
19
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <stddef.h>
25 #include <string.h>
26 #include <sys/types.h>
27 #include <sys/ioctl.h>
28 #include <sys/stat.h>
29 #include <unistd.h>
30 #include <asm/page.h>
31
32 #ifdef MTD_OLD
33 #include <stdint.h>
34 #include <linux/mtd/mtd.h>
35 #else
36 #define  __user /* nothing */
37 #include <mtd/mtd-user.h>
38 #endif
39
40 #include <sha1.h>
41 #include <libfdt.h>
42 #include <fdt_support.h>
43 #include <image.h>
44
45 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
46
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);
51
52 char    *cmdname;
53 char    *devicefile;
54
55 unsigned int sectorcount = 0;
56 int sflag = 0;
57 unsigned int sectoroffset = 0;
58 unsigned int sectorsize = 0;
59 int cflag = 0;
60
61 int main (int argc, char **argv)
62 {
63         int fd = -1, err = 0, readbyte = 0, j;
64         struct mtd_info_user mtdinfo;
65         char buf[sizeof(image_header_t)];
66         int found = 0;
67
68         cmdname = *argv;
69
70         while (--argc > 0 && **++argv == '-') {
71                 while (*++*argv) {
72                         switch (**argv) {
73                         case 'c':
74                                 if (--argc <= 0)
75                                         usage ();
76                                 sectorcount = (unsigned int)atoi(*++argv);
77                                 cflag = 1;
78                                 goto NXTARG;
79                         case 'o':
80                                 if (--argc <= 0)
81                                         usage ();
82                                 sectoroffset = (unsigned int)atoi(*++argv);
83                                 goto NXTARG;
84
85                         case 's':
86                                 if (--argc <= 0)
87                                         usage ();
88                                 sectorsize = (unsigned int)atoi(*++argv);
89                                 sflag = 1;
90                                 goto NXTARG;
91                         default:
92                                 usage ();
93                         }
94                 }
95 NXTARG:         ;
96         }
97
98         if (argc != 1 || cflag == 0 || sflag == 0)
99                 usage();
100
101         devicefile = *argv;
102
103         fd = open(devicefile, O_RDONLY);
104         if (fd < 0) {
105                 fprintf (stderr, "%s: Can't open %s: %s\n",
106                          cmdname, devicefile, strerror(errno));
107                 exit(EXIT_FAILURE);
108         }
109
110         err = ioctl(fd, MEMGETINFO, &mtdinfo);
111         if (err < 0) {
112                 fprintf(stderr, "%s: Cannot get MTD information: %s\n",cmdname,
113                         strerror(errno));
114                 exit(EXIT_FAILURE);
115         }
116
117         if (mtdinfo.type != MTD_NORFLASH && mtdinfo.type != MTD_NANDFLASH) {
118                 fprintf(stderr, "%s: Unsupported flash type %u\n",
119                         cmdname, mtdinfo.type);
120                 exit(EXIT_FAILURE);
121         }
122
123         if (sectorsize * sectorcount != mtdinfo.size) {
124                 fprintf(stderr, "%s: Partition size (%d) incompatible with "
125                         "sector size and count\n", cmdname, mtdinfo.size);
126                 exit(EXIT_FAILURE);
127         }
128
129         if (sectorsize * sectoroffset >= mtdinfo.size) {
130                 fprintf(stderr, "%s: Partition size (%d) incompatible with "
131                         "sector offset given\n", cmdname, mtdinfo.size);
132                 exit(EXIT_FAILURE);
133         }
134
135         if (sectoroffset > sectorcount - 1) {
136                 fprintf(stderr, "%s: Sector offset cannot be grater than "
137                         "sector count minus one\n", cmdname);
138                 exit(EXIT_FAILURE);
139         }
140
141         printf("Searching....\n");
142
143         for (j = sectoroffset; j < sectorcount; ++j) {
144
145                 if (lseek(fd, j*sectorsize, SEEK_SET) != j*sectorsize) {
146                         fprintf(stderr, "%s: lseek failure: %s\n",
147                         cmdname, strerror(errno));
148                         exit(EXIT_FAILURE);
149                 }
150
151                 err = flash_bad_block(fd, mtdinfo.type, j*sectorsize);
152                 if (err < 0)
153                         exit(EXIT_FAILURE);
154                 if (err)
155                         continue; /* Skip and jump to next */
156
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));
161                         exit(EXIT_FAILURE);
162                 }
163
164                 if (fdt_check_header(buf)) {
165                         /* old-style image */
166                         if (image_verify_header(buf, fd)) {
167                                 found = 1;
168                                 image_print_contents((image_header_t *)buf);
169                         }
170                 } else {
171                         /* FIT image */
172                         fit_print_contents(buf);
173                 }
174
175         }
176
177         close(fd);
178
179         if(!found)
180                 printf("No images found\n");
181
182         exit(EXIT_SUCCESS);
183 }
184
185 void usage()
186 {
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",
192                 cmdname);
193
194         exit(EXIT_FAILURE);
195 }
196
197 static int image_verify_header(char *ptr, int fd)
198 {
199         int len, nread;
200         char *data;
201         uint32_t checksum;
202         image_header_t *hdr = (image_header_t *)ptr;
203         char buf[PAGE_SIZE];
204
205         if (image_get_magic(hdr) != IH_MAGIC)
206                 return 0;
207
208         data = (char *)hdr;
209         len  = image_get_header_size();
210
211         checksum = image_get_hcrc(hdr);
212         hdr->ih_hcrc = htonl(0);        /* clear for re-calculation */
213
214         if (crc32(0, data, len) != checksum) {
215                 fprintf(stderr,
216                       "%s: Maybe image found but it has bad header checksum!\n",
217                       cmdname);
218                 return 0;
219         }
220
221         len = image_get_size(hdr);
222         checksum = 0;
223
224         while (len > 0) {
225                 nread = read(fd, buf, MIN(len,PAGE_SIZE));
226                 if (nread != MIN(len,PAGE_SIZE)) {
227                         fprintf(stderr,
228                                 "%s: Error while reading: %s\n",
229                                 cmdname, strerror(errno));
230                         exit(EXIT_FAILURE);
231                 }
232                 checksum = crc32(checksum, buf, nread);
233                 len -= nread;
234         }
235
236         if (checksum != image_get_dcrc(hdr)) {
237                 fprintf (stderr,
238                         "%s: Maybe image found but it has corrupted data!\n",
239                         cmdname);
240                 return 0;
241         }
242
243         return 1;
244 }
245
246 /*
247  * Test for bad block on NAND, just returns 0 on NOR, on NAND:
248  * 0    - block is good
249  * > 0  - block is bad
250  * < 0  - failed to test
251  */
252 static int flash_bad_block(int fd, uint8_t mtd_type, loff_t start)
253 {
254         if (mtd_type == MTD_NANDFLASH) {
255                 int badblock = ioctl(fd, MEMGETBADBLOCK, &start);
256
257                 if (badblock < 0) {
258                         fprintf(stderr,"%s: Cannot read bad block mark: %s\n",
259                                 cmdname, strerror(errno));
260                         return badblock;
261                 }
262
263                 if (badblock) {
264                         return badblock;
265                 }
266         }
267
268         return 0;
269 }