imls.c 6.0 KB

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