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