imls.c 6.0 KB

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