libscan.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * Copyright (C) 2008 Nokia Corporation
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (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
  12. * the 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., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. *
  18. * Author: Artem Bityutskiy
  19. *
  20. * UBI scanning library.
  21. */
  22. #define PROGRAM_NAME "libscan"
  23. #include <sys/types.h>
  24. #include <sys/stat.h>
  25. #include <stdint.h>
  26. #include <fcntl.h>
  27. #include <unistd.h>
  28. #include <stdlib.h>
  29. #include <mtd_swab.h>
  30. #include <mtd/ubi-media.h>
  31. #include <mtd/mtd-user.h>
  32. #include <libmtd.h>
  33. #include <libscan.h>
  34. #include <crc32.h>
  35. #include "common.h"
  36. static int all_ff(const void *buf, int len)
  37. {
  38. int i;
  39. const uint8_t *p = buf;
  40. for (i = 0; i < len; i++)
  41. if (p[i] != 0xFF)
  42. return 0;
  43. return 1;
  44. }
  45. int ubi_scan(struct mtd_dev_info *mtd, int fd, struct ubi_scan_info **info,
  46. int verbose)
  47. {
  48. int eb, v = (verbose == 2), pr = (verbose == 1);
  49. struct ubi_scan_info *si;
  50. unsigned long long sum = 0;
  51. si = calloc(1, sizeof(struct ubi_scan_info));
  52. if (!si)
  53. return sys_errmsg("cannot allocate %zd bytes of memory",
  54. sizeof(struct ubi_scan_info));
  55. si->ec = calloc(mtd->eb_cnt, sizeof(uint32_t));
  56. if (!si->ec) {
  57. sys_errmsg("cannot allocate %zd bytes of memory",
  58. sizeof(struct ubi_scan_info));
  59. goto out_si;
  60. }
  61. si->vid_hdr_offs = si->data_offs = -1;
  62. verbose(v, "start scanning eraseblocks 0-%d", mtd->eb_cnt);
  63. for (eb = 0; eb < mtd->eb_cnt; eb++) {
  64. int ret;
  65. uint32_t crc;
  66. struct ubi_ec_hdr ech;
  67. unsigned long long ec;
  68. if (v) {
  69. normsg_cont("scanning eraseblock %d", eb);
  70. fflush(stdout);
  71. }
  72. if (pr) {
  73. printf("\r" PROGRAM_NAME ": scanning eraseblock %d -- %2lld %% complete ",
  74. eb, (long long)(eb + 1) * 100 / mtd->eb_cnt);
  75. fflush(stdout);
  76. }
  77. ret = mtd_is_bad(mtd, fd, eb);
  78. if (ret == -1)
  79. goto out_ec;
  80. if (ret) {
  81. si->bad_cnt += 1;
  82. si->ec[eb] = EB_BAD;
  83. if (v)
  84. printf(": bad\n");
  85. continue;
  86. }
  87. ret = mtd_read(mtd, fd, eb, 0, &ech, sizeof(struct ubi_ec_hdr));
  88. if (ret < 0)
  89. goto out_ec;
  90. if (be32_to_cpu(ech.magic) != UBI_EC_HDR_MAGIC) {
  91. if (all_ff(&ech, sizeof(struct ubi_ec_hdr))) {
  92. si->empty_cnt += 1;
  93. si->ec[eb] = EB_EMPTY;
  94. if (v)
  95. printf(": empty\n");
  96. } else {
  97. si->alien_cnt += 1;
  98. si->ec[eb] = EB_ALIEN;
  99. if (v)
  100. printf(": alien\n");
  101. }
  102. continue;
  103. }
  104. crc = mtd_crc32(UBI_CRC32_INIT, &ech, UBI_EC_HDR_SIZE_CRC);
  105. if (be32_to_cpu(ech.hdr_crc) != crc) {
  106. si->corrupted_cnt += 1;
  107. si->ec[eb] = EB_CORRUPTED;
  108. if (v)
  109. printf(": bad CRC %#08x, should be %#08x\n",
  110. crc, be32_to_cpu(ech.hdr_crc));
  111. continue;
  112. }
  113. ec = be64_to_cpu(ech.ec);
  114. if (ec > EC_MAX) {
  115. if (pr)
  116. printf("\n");
  117. errmsg("erase counter in EB %d is %llu, while this "
  118. "program expects them to be less than %u",
  119. eb, ec, EC_MAX);
  120. goto out_ec;
  121. }
  122. if (si->vid_hdr_offs == -1) {
  123. si->vid_hdr_offs = be32_to_cpu(ech.vid_hdr_offset);
  124. si->data_offs = be32_to_cpu(ech.data_offset);
  125. if (si->data_offs % mtd->min_io_size) {
  126. if (pr)
  127. printf("\n");
  128. if (v)
  129. printf(": corrupted because of the below\n");
  130. warnmsg("bad data offset %d at eraseblock %d (n"
  131. "of multiple of min. I/O unit size %d)",
  132. si->data_offs, eb, mtd->min_io_size);
  133. warnmsg("treat eraseblock %d as corrupted", eb);
  134. si->corrupted_cnt += 1;
  135. si->ec[eb] = EB_CORRUPTED;
  136. continue;
  137. }
  138. } else {
  139. if ((int)be32_to_cpu(ech.vid_hdr_offset) != si->vid_hdr_offs) {
  140. if (pr)
  141. printf("\n");
  142. if (v)
  143. printf(": corrupted because of the below\n");
  144. warnmsg("inconsistent VID header offset: was "
  145. "%d, but is %d in eraseblock %d",
  146. si->vid_hdr_offs,
  147. be32_to_cpu(ech.vid_hdr_offset), eb);
  148. warnmsg("treat eraseblock %d as corrupted", eb);
  149. si->corrupted_cnt += 1;
  150. si->ec[eb] = EB_CORRUPTED;
  151. continue;
  152. }
  153. if ((int)be32_to_cpu(ech.data_offset) != si->data_offs) {
  154. if (pr)
  155. printf("\n");
  156. if (v)
  157. printf(": corrupted because of the below\n");
  158. warnmsg("inconsistent data offset: was %d, but"
  159. " is %d in eraseblock %d",
  160. si->data_offs,
  161. be32_to_cpu(ech.data_offset), eb);
  162. warnmsg("treat eraseblock %d as corrupted", eb);
  163. si->corrupted_cnt += 1;
  164. si->ec[eb] = EB_CORRUPTED;
  165. continue;
  166. }
  167. }
  168. si->ok_cnt += 1;
  169. si->ec[eb] = ec;
  170. if (v)
  171. printf(": OK, erase counter %u\n", si->ec[eb]);
  172. }
  173. if (si->ok_cnt != 0) {
  174. /* Calculate mean erase counter */
  175. for (eb = 0; eb < mtd->eb_cnt; eb++) {
  176. if (si->ec[eb] > EC_MAX)
  177. continue;
  178. sum += si->ec[eb];
  179. }
  180. si->mean_ec = sum / si->ok_cnt;
  181. }
  182. si->good_cnt = mtd->eb_cnt - si->bad_cnt;
  183. verbose(v, "finished, mean EC %lld, %d OK, %d corrupted, %d empty, %d "
  184. "alien, bad %d", si->mean_ec, si->ok_cnt, si->corrupted_cnt,
  185. si->empty_cnt, si->alien_cnt, si->bad_cnt);
  186. *info = si;
  187. if (pr)
  188. printf("\n");
  189. return 0;
  190. out_ec:
  191. free(si->ec);
  192. out_si:
  193. free(si);
  194. *info = NULL;
  195. return -1;
  196. }
  197. void ubi_scan_free(struct ubi_scan_info *si)
  198. {
  199. free(si->ec);
  200. free(si);
  201. }