io.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269
  1. /*
  2. * Copyright (c) International Business Machines Corp., 2006
  3. * Copyright (c) Nokia Corporation, 2006, 2007
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  13. * the GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. * Author: Artem Bityutskiy (Битюцкий Артём)
  20. */
  21. /*
  22. * UBI input/output unit.
  23. *
  24. * This unit provides a uniform way to work with all kinds of the underlying
  25. * MTD devices. It also implements handy functions for reading and writing UBI
  26. * headers.
  27. *
  28. * We are trying to have a paranoid mindset and not to trust to what we read
  29. * from the flash media in order to be more secure and robust. So this unit
  30. * validates every single header it reads from the flash media.
  31. *
  32. * Some words about how the eraseblock headers are stored.
  33. *
  34. * The erase counter header is always stored at offset zero. By default, the
  35. * VID header is stored after the EC header at the closest aligned offset
  36. * (i.e. aligned to the minimum I/O unit size). Data starts next to the VID
  37. * header at the closest aligned offset. But this default layout may be
  38. * changed. For example, for different reasons (e.g., optimization) UBI may be
  39. * asked to put the VID header at further offset, and even at an unaligned
  40. * offset. Of course, if the offset of the VID header is unaligned, UBI adds
  41. * proper padding in front of it. Data offset may also be changed but it has to
  42. * be aligned.
  43. *
  44. * About minimal I/O units. In general, UBI assumes flash device model where
  45. * there is only one minimal I/O unit size. E.g., in case of NOR flash it is 1,
  46. * in case of NAND flash it is a NAND page, etc. This is reported by MTD in the
  47. * @ubi->mtd->writesize field. But as an exception, UBI admits of using another
  48. * (smaller) minimal I/O unit size for EC and VID headers to make it possible
  49. * to do different optimizations.
  50. *
  51. * This is extremely useful in case of NAND flashes which admit of several
  52. * write operations to one NAND page. In this case UBI can fit EC and VID
  53. * headers at one NAND page. Thus, UBI may use "sub-page" size as the minimal
  54. * I/O unit for the headers (the @ubi->hdrs_min_io_size field). But it still
  55. * reports NAND page size (@ubi->min_io_size) as a minimal I/O unit for the UBI
  56. * users.
  57. *
  58. * Example: some Samsung NANDs with 2KiB pages allow 4x 512-byte writes, so
  59. * although the minimal I/O unit is 2K, UBI uses 512 bytes for EC and VID
  60. * headers.
  61. *
  62. * Q: why not just to treat sub-page as a minimal I/O unit of this flash
  63. * device, e.g., make @ubi->min_io_size = 512 in the example above?
  64. *
  65. * A: because when writing a sub-page, MTD still writes a full 2K page but the
  66. * bytes which are no relevant to the sub-page are 0xFF. So, basically, writing
  67. * 4x512 sub-pages is 4 times slower then writing one 2KiB NAND page. Thus, we
  68. * prefer to use sub-pages only for EV and VID headers.
  69. *
  70. * As it was noted above, the VID header may start at a non-aligned offset.
  71. * For example, in case of a 2KiB page NAND flash with a 512 bytes sub-page,
  72. * the VID header may reside at offset 1984 which is the last 64 bytes of the
  73. * last sub-page (EC header is always at offset zero). This causes some
  74. * difficulties when reading and writing VID headers.
  75. *
  76. * Suppose we have a 64-byte buffer and we read a VID header at it. We change
  77. * the data and want to write this VID header out. As we can only write in
  78. * 512-byte chunks, we have to allocate one more buffer and copy our VID header
  79. * to offset 448 of this buffer.
  80. *
  81. * The I/O unit does the following trick in order to avoid this extra copy.
  82. * It always allocates a @ubi->vid_hdr_alsize bytes buffer for the VID header
  83. * and returns a pointer to offset @ubi->vid_hdr_shift of this buffer. When the
  84. * VID header is being written out, it shifts the VID header pointer back and
  85. * writes the whole sub-page.
  86. */
  87. #include <linux/crc32.h>
  88. #include <linux/err.h>
  89. #include "ubi.h"
  90. #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
  91. static int paranoid_check_not_bad(const struct ubi_device *ubi, int pnum);
  92. static int paranoid_check_peb_ec_hdr(const struct ubi_device *ubi, int pnum);
  93. static int paranoid_check_ec_hdr(const struct ubi_device *ubi, int pnum,
  94. const struct ubi_ec_hdr *ec_hdr);
  95. static int paranoid_check_peb_vid_hdr(const struct ubi_device *ubi, int pnum);
  96. static int paranoid_check_vid_hdr(const struct ubi_device *ubi, int pnum,
  97. const struct ubi_vid_hdr *vid_hdr);
  98. static int paranoid_check_all_ff(struct ubi_device *ubi, int pnum, int offset,
  99. int len);
  100. #else
  101. #define paranoid_check_not_bad(ubi, pnum) 0
  102. #define paranoid_check_peb_ec_hdr(ubi, pnum) 0
  103. #define paranoid_check_ec_hdr(ubi, pnum, ec_hdr) 0
  104. #define paranoid_check_peb_vid_hdr(ubi, pnum) 0
  105. #define paranoid_check_vid_hdr(ubi, pnum, vid_hdr) 0
  106. #define paranoid_check_all_ff(ubi, pnum, offset, len) 0
  107. #endif
  108. /**
  109. * ubi_io_read - read data from a physical eraseblock.
  110. * @ubi: UBI device description object
  111. * @buf: buffer where to store the read data
  112. * @pnum: physical eraseblock number to read from
  113. * @offset: offset within the physical eraseblock from where to read
  114. * @len: how many bytes to read
  115. *
  116. * This function reads data from offset @offset of physical eraseblock @pnum
  117. * and stores the read data in the @buf buffer. The following return codes are
  118. * possible:
  119. *
  120. * o %0 if all the requested data were successfully read;
  121. * o %UBI_IO_BITFLIPS if all the requested data were successfully read, but
  122. * correctable bit-flips were detected; this is harmless but may indicate
  123. * that this eraseblock may become bad soon (but do not have to);
  124. * o %-EBADMSG if the MTD subsystem reported about data integrity problems, for
  125. * example it can be an ECC error in case of NAND; this most probably means
  126. * that the data is corrupted;
  127. * o %-EIO if some I/O error occurred;
  128. * o other negative error codes in case of other errors.
  129. */
  130. int ubi_io_read(const struct ubi_device *ubi, void *buf, int pnum, int offset,
  131. int len)
  132. {
  133. int err, retries = 0;
  134. size_t read;
  135. loff_t addr;
  136. dbg_io("read %d bytes from PEB %d:%d", len, pnum, offset);
  137. ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
  138. ubi_assert(offset >= 0 && offset + len <= ubi->peb_size);
  139. ubi_assert(len > 0);
  140. err = paranoid_check_not_bad(ubi, pnum);
  141. if (err)
  142. return err > 0 ? -EINVAL : err;
  143. addr = (loff_t)pnum * ubi->peb_size + offset;
  144. retry:
  145. err = ubi->mtd->read(ubi->mtd, addr, len, &read, buf);
  146. if (err) {
  147. if (err == -EUCLEAN) {
  148. /*
  149. * -EUCLEAN is reported if there was a bit-flip which
  150. * was corrected, so this is harmless.
  151. */
  152. ubi_msg("fixable bit-flip detected at PEB %d", pnum);
  153. ubi_assert(len == read);
  154. return UBI_IO_BITFLIPS;
  155. }
  156. if (read != len && retries++ < UBI_IO_RETRIES) {
  157. dbg_io("error %d while reading %d bytes from PEB %d:%d, "
  158. "read only %zd bytes, retry",
  159. err, len, pnum, offset, read);
  160. yield();
  161. goto retry;
  162. }
  163. ubi_err("error %d while reading %d bytes from PEB %d:%d, "
  164. "read %zd bytes", err, len, pnum, offset, read);
  165. ubi_dbg_dump_stack();
  166. /*
  167. * The driver should never return -EBADMSG if it failed to read
  168. * all the requested data. But some buggy drivers might do
  169. * this, so we change it to -EIO.
  170. */
  171. if (read != len && err == -EBADMSG) {
  172. ubi_assert(0);
  173. err = -EIO;
  174. }
  175. } else {
  176. ubi_assert(len == read);
  177. if (ubi_dbg_is_bitflip()) {
  178. dbg_msg("bit-flip (emulated)");
  179. err = UBI_IO_BITFLIPS;
  180. }
  181. }
  182. return err;
  183. }
  184. /**
  185. * ubi_io_write - write data to a physical eraseblock.
  186. * @ubi: UBI device description object
  187. * @buf: buffer with the data to write
  188. * @pnum: physical eraseblock number to write to
  189. * @offset: offset within the physical eraseblock where to write
  190. * @len: how many bytes to write
  191. *
  192. * This function writes @len bytes of data from buffer @buf to offset @offset
  193. * of physical eraseblock @pnum. If all the data were successfully written,
  194. * zero is returned. If an error occurred, this function returns a negative
  195. * error code. If %-EIO is returned, the physical eraseblock most probably went
  196. * bad.
  197. *
  198. * Note, in case of an error, it is possible that something was still written
  199. * to the flash media, but may be some garbage.
  200. */
  201. int ubi_io_write(struct ubi_device *ubi, const void *buf, int pnum, int offset,
  202. int len)
  203. {
  204. int err;
  205. size_t written;
  206. loff_t addr;
  207. dbg_io("write %d bytes to PEB %d:%d", len, pnum, offset);
  208. ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
  209. ubi_assert(offset >= 0 && offset + len <= ubi->peb_size);
  210. ubi_assert(offset % ubi->hdrs_min_io_size == 0);
  211. ubi_assert(len > 0 && len % ubi->hdrs_min_io_size == 0);
  212. if (ubi->ro_mode) {
  213. ubi_err("read-only mode");
  214. return -EROFS;
  215. }
  216. /* The below has to be compiled out if paranoid checks are disabled */
  217. err = paranoid_check_not_bad(ubi, pnum);
  218. if (err)
  219. return err > 0 ? -EINVAL : err;
  220. /* The area we are writing to has to contain all 0xFF bytes */
  221. err = paranoid_check_all_ff(ubi, pnum, offset, len);
  222. if (err)
  223. return err > 0 ? -EINVAL : err;
  224. if (offset >= ubi->leb_start) {
  225. /*
  226. * We write to the data area of the physical eraseblock. Make
  227. * sure it has valid EC and VID headers.
  228. */
  229. err = paranoid_check_peb_ec_hdr(ubi, pnum);
  230. if (err)
  231. return err > 0 ? -EINVAL : err;
  232. err = paranoid_check_peb_vid_hdr(ubi, pnum);
  233. if (err)
  234. return err > 0 ? -EINVAL : err;
  235. }
  236. if (ubi_dbg_is_write_failure()) {
  237. dbg_err("cannot write %d bytes to PEB %d:%d "
  238. "(emulated)", len, pnum, offset);
  239. ubi_dbg_dump_stack();
  240. return -EIO;
  241. }
  242. addr = (loff_t)pnum * ubi->peb_size + offset;
  243. err = ubi->mtd->write(ubi->mtd, addr, len, &written, buf);
  244. if (err) {
  245. ubi_err("error %d while writing %d bytes to PEB %d:%d, written"
  246. " %zd bytes", err, len, pnum, offset, written);
  247. ubi_dbg_dump_stack();
  248. } else
  249. ubi_assert(written == len);
  250. return err;
  251. }
  252. /**
  253. * erase_callback - MTD erasure call-back.
  254. * @ei: MTD erase information object.
  255. *
  256. * Note, even though MTD erase interface is asynchronous, all the current
  257. * implementations are synchronous anyway.
  258. */
  259. static void erase_callback(struct erase_info *ei)
  260. {
  261. wake_up_interruptible((wait_queue_head_t *)ei->priv);
  262. }
  263. /**
  264. * do_sync_erase - synchronously erase a physical eraseblock.
  265. * @ubi: UBI device description object
  266. * @pnum: the physical eraseblock number to erase
  267. *
  268. * This function synchronously erases physical eraseblock @pnum and returns
  269. * zero in case of success and a negative error code in case of failure. If
  270. * %-EIO is returned, the physical eraseblock most probably went bad.
  271. */
  272. static int do_sync_erase(struct ubi_device *ubi, int pnum)
  273. {
  274. int err, retries = 0;
  275. struct erase_info ei;
  276. wait_queue_head_t wq;
  277. dbg_io("erase PEB %d", pnum);
  278. retry:
  279. init_waitqueue_head(&wq);
  280. memset(&ei, 0, sizeof(struct erase_info));
  281. ei.mtd = ubi->mtd;
  282. ei.addr = (loff_t)pnum * ubi->peb_size;
  283. ei.len = ubi->peb_size;
  284. ei.callback = erase_callback;
  285. ei.priv = (unsigned long)&wq;
  286. err = ubi->mtd->erase(ubi->mtd, &ei);
  287. if (err) {
  288. if (retries++ < UBI_IO_RETRIES) {
  289. dbg_io("error %d while erasing PEB %d, retry",
  290. err, pnum);
  291. yield();
  292. goto retry;
  293. }
  294. ubi_err("cannot erase PEB %d, error %d", pnum, err);
  295. ubi_dbg_dump_stack();
  296. return err;
  297. }
  298. err = wait_event_interruptible(wq, ei.state == MTD_ERASE_DONE ||
  299. ei.state == MTD_ERASE_FAILED);
  300. if (err) {
  301. ubi_err("interrupted PEB %d erasure", pnum);
  302. return -EINTR;
  303. }
  304. if (ei.state == MTD_ERASE_FAILED) {
  305. if (retries++ < UBI_IO_RETRIES) {
  306. dbg_io("error while erasing PEB %d, retry", pnum);
  307. yield();
  308. goto retry;
  309. }
  310. ubi_err("cannot erase PEB %d", pnum);
  311. ubi_dbg_dump_stack();
  312. return -EIO;
  313. }
  314. err = paranoid_check_all_ff(ubi, pnum, 0, ubi->peb_size);
  315. if (err)
  316. return err > 0 ? -EINVAL : err;
  317. if (ubi_dbg_is_erase_failure() && !err) {
  318. dbg_err("cannot erase PEB %d (emulated)", pnum);
  319. return -EIO;
  320. }
  321. return 0;
  322. }
  323. /**
  324. * check_pattern - check if buffer contains only a certain byte pattern.
  325. * @buf: buffer to check
  326. * @patt: the pattern to check
  327. * @size: buffer size in bytes
  328. *
  329. * This function returns %1 in there are only @patt bytes in @buf, and %0 if
  330. * something else was also found.
  331. */
  332. static int check_pattern(const void *buf, uint8_t patt, int size)
  333. {
  334. int i;
  335. for (i = 0; i < size; i++)
  336. if (((const uint8_t *)buf)[i] != patt)
  337. return 0;
  338. return 1;
  339. }
  340. /* Patterns to write to a physical eraseblock when torturing it */
  341. static uint8_t patterns[] = {0xa5, 0x5a, 0x0};
  342. /**
  343. * torture_peb - test a supposedly bad physical eraseblock.
  344. * @ubi: UBI device description object
  345. * @pnum: the physical eraseblock number to test
  346. *
  347. * This function returns %-EIO if the physical eraseblock did not pass the
  348. * test, a positive number of erase operations done if the test was
  349. * successfully passed, and other negative error codes in case of other errors.
  350. */
  351. static int torture_peb(struct ubi_device *ubi, int pnum)
  352. {
  353. int err, i, patt_count;
  354. patt_count = ARRAY_SIZE(patterns);
  355. ubi_assert(patt_count > 0);
  356. mutex_lock(&ubi->buf_mutex);
  357. for (i = 0; i < patt_count; i++) {
  358. err = do_sync_erase(ubi, pnum);
  359. if (err)
  360. goto out;
  361. /* Make sure the PEB contains only 0xFF bytes */
  362. err = ubi_io_read(ubi, ubi->peb_buf1, pnum, 0, ubi->peb_size);
  363. if (err)
  364. goto out;
  365. err = check_pattern(ubi->peb_buf1, 0xFF, ubi->peb_size);
  366. if (err == 0) {
  367. ubi_err("erased PEB %d, but a non-0xFF byte found",
  368. pnum);
  369. err = -EIO;
  370. goto out;
  371. }
  372. /* Write a pattern and check it */
  373. memset(ubi->peb_buf1, patterns[i], ubi->peb_size);
  374. err = ubi_io_write(ubi, ubi->peb_buf1, pnum, 0, ubi->peb_size);
  375. if (err)
  376. goto out;
  377. memset(ubi->peb_buf1, ~patterns[i], ubi->peb_size);
  378. err = ubi_io_read(ubi, ubi->peb_buf1, pnum, 0, ubi->peb_size);
  379. if (err)
  380. goto out;
  381. err = check_pattern(ubi->peb_buf1, patterns[i], ubi->peb_size);
  382. if (err == 0) {
  383. ubi_err("pattern %x checking failed for PEB %d",
  384. patterns[i], pnum);
  385. err = -EIO;
  386. goto out;
  387. }
  388. }
  389. err = patt_count;
  390. out:
  391. mutex_unlock(&ubi->buf_mutex);
  392. if (err == UBI_IO_BITFLIPS || err == -EBADMSG) {
  393. /*
  394. * If a bit-flip or data integrity error was detected, the test
  395. * has not passed because it happened on a freshly erased
  396. * physical eraseblock which means something is wrong with it.
  397. */
  398. ubi_err("read problems on freshly erased PEB %d, must be bad",
  399. pnum);
  400. err = -EIO;
  401. }
  402. return err;
  403. }
  404. /**
  405. * ubi_io_sync_erase - synchronously erase a physical eraseblock.
  406. * @ubi: UBI device description object
  407. * @pnum: physical eraseblock number to erase
  408. * @torture: if this physical eraseblock has to be tortured
  409. *
  410. * This function synchronously erases physical eraseblock @pnum. If @torture
  411. * flag is not zero, the physical eraseblock is checked by means of writing
  412. * different patterns to it and reading them back. If the torturing is enabled,
  413. * the physical eraseblock is erased more then once.
  414. *
  415. * This function returns the number of erasures made in case of success, %-EIO
  416. * if the erasure failed or the torturing test failed, and other negative error
  417. * codes in case of other errors. Note, %-EIO means that the physical
  418. * eraseblock is bad.
  419. */
  420. int ubi_io_sync_erase(struct ubi_device *ubi, int pnum, int torture)
  421. {
  422. int err, ret = 0;
  423. ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
  424. err = paranoid_check_not_bad(ubi, pnum);
  425. if (err != 0)
  426. return err > 0 ? -EINVAL : err;
  427. if (ubi->ro_mode) {
  428. ubi_err("read-only mode");
  429. return -EROFS;
  430. }
  431. if (torture) {
  432. ret = torture_peb(ubi, pnum);
  433. if (ret < 0)
  434. return ret;
  435. }
  436. err = do_sync_erase(ubi, pnum);
  437. if (err)
  438. return err;
  439. return ret + 1;
  440. }
  441. /**
  442. * ubi_io_is_bad - check if a physical eraseblock is bad.
  443. * @ubi: UBI device description object
  444. * @pnum: the physical eraseblock number to check
  445. *
  446. * This function returns a positive number if the physical eraseblock is bad,
  447. * zero if not, and a negative error code if an error occurred.
  448. */
  449. int ubi_io_is_bad(const struct ubi_device *ubi, int pnum)
  450. {
  451. struct mtd_info *mtd = ubi->mtd;
  452. ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
  453. if (ubi->bad_allowed) {
  454. int ret;
  455. ret = mtd->block_isbad(mtd, (loff_t)pnum * ubi->peb_size);
  456. if (ret < 0)
  457. ubi_err("error %d while checking if PEB %d is bad",
  458. ret, pnum);
  459. else if (ret)
  460. dbg_io("PEB %d is bad", pnum);
  461. return ret;
  462. }
  463. return 0;
  464. }
  465. /**
  466. * ubi_io_mark_bad - mark a physical eraseblock as bad.
  467. * @ubi: UBI device description object
  468. * @pnum: the physical eraseblock number to mark
  469. *
  470. * This function returns zero in case of success and a negative error code in
  471. * case of failure.
  472. */
  473. int ubi_io_mark_bad(const struct ubi_device *ubi, int pnum)
  474. {
  475. int err;
  476. struct mtd_info *mtd = ubi->mtd;
  477. ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
  478. if (ubi->ro_mode) {
  479. ubi_err("read-only mode");
  480. return -EROFS;
  481. }
  482. if (!ubi->bad_allowed)
  483. return 0;
  484. err = mtd->block_markbad(mtd, (loff_t)pnum * ubi->peb_size);
  485. if (err)
  486. ubi_err("cannot mark PEB %d bad, error %d", pnum, err);
  487. return err;
  488. }
  489. /**
  490. * validate_ec_hdr - validate an erase counter header.
  491. * @ubi: UBI device description object
  492. * @ec_hdr: the erase counter header to check
  493. *
  494. * This function returns zero if the erase counter header is OK, and %1 if
  495. * not.
  496. */
  497. static int validate_ec_hdr(const struct ubi_device *ubi,
  498. const struct ubi_ec_hdr *ec_hdr)
  499. {
  500. long long ec;
  501. int vid_hdr_offset, leb_start;
  502. ec = be64_to_cpu(ec_hdr->ec);
  503. vid_hdr_offset = be32_to_cpu(ec_hdr->vid_hdr_offset);
  504. leb_start = be32_to_cpu(ec_hdr->data_offset);
  505. if (ec_hdr->version != UBI_VERSION) {
  506. ubi_err("node with incompatible UBI version found: "
  507. "this UBI version is %d, image version is %d",
  508. UBI_VERSION, (int)ec_hdr->version);
  509. goto bad;
  510. }
  511. if (vid_hdr_offset != ubi->vid_hdr_offset) {
  512. ubi_err("bad VID header offset %d, expected %d",
  513. vid_hdr_offset, ubi->vid_hdr_offset);
  514. goto bad;
  515. }
  516. if (leb_start != ubi->leb_start) {
  517. ubi_err("bad data offset %d, expected %d",
  518. leb_start, ubi->leb_start);
  519. goto bad;
  520. }
  521. if (ec < 0 || ec > UBI_MAX_ERASECOUNTER) {
  522. ubi_err("bad erase counter %lld", ec);
  523. goto bad;
  524. }
  525. return 0;
  526. bad:
  527. ubi_err("bad EC header");
  528. ubi_dbg_dump_ec_hdr(ec_hdr);
  529. ubi_dbg_dump_stack();
  530. return 1;
  531. }
  532. /**
  533. * ubi_io_read_ec_hdr - read and check an erase counter header.
  534. * @ubi: UBI device description object
  535. * @pnum: physical eraseblock to read from
  536. * @ec_hdr: a &struct ubi_ec_hdr object where to store the read erase counter
  537. * header
  538. * @verbose: be verbose if the header is corrupted or was not found
  539. *
  540. * This function reads erase counter header from physical eraseblock @pnum and
  541. * stores it in @ec_hdr. This function also checks CRC checksum of the read
  542. * erase counter header. The following codes may be returned:
  543. *
  544. * o %0 if the CRC checksum is correct and the header was successfully read;
  545. * o %UBI_IO_BITFLIPS if the CRC is correct, but bit-flips were detected
  546. * and corrected by the flash driver; this is harmless but may indicate that
  547. * this eraseblock may become bad soon (but may be not);
  548. * o %UBI_IO_BAD_EC_HDR if the erase counter header is corrupted (a CRC error);
  549. * o %UBI_IO_PEB_EMPTY if the physical eraseblock is empty;
  550. * o a negative error code in case of failure.
  551. */
  552. int ubi_io_read_ec_hdr(struct ubi_device *ubi, int pnum,
  553. struct ubi_ec_hdr *ec_hdr, int verbose)
  554. {
  555. int err, read_err = 0;
  556. uint32_t crc, magic, hdr_crc;
  557. dbg_io("read EC header from PEB %d", pnum);
  558. ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
  559. if (UBI_IO_DEBUG)
  560. verbose = 1;
  561. err = ubi_io_read(ubi, ec_hdr, pnum, 0, UBI_EC_HDR_SIZE);
  562. if (err) {
  563. if (err != UBI_IO_BITFLIPS && err != -EBADMSG)
  564. return err;
  565. /*
  566. * We read all the data, but either a correctable bit-flip
  567. * occurred, or MTD reported about some data integrity error,
  568. * like an ECC error in case of NAND. The former is harmless,
  569. * the later may mean that the read data is corrupted. But we
  570. * have a CRC check-sum and we will detect this. If the EC
  571. * header is still OK, we just report this as there was a
  572. * bit-flip.
  573. */
  574. read_err = err;
  575. }
  576. magic = be32_to_cpu(ec_hdr->magic);
  577. if (magic != UBI_EC_HDR_MAGIC) {
  578. /*
  579. * The magic field is wrong. Let's check if we have read all
  580. * 0xFF. If yes, this physical eraseblock is assumed to be
  581. * empty.
  582. *
  583. * But if there was a read error, we do not test it for all
  584. * 0xFFs. Even if it does contain all 0xFFs, this error
  585. * indicates that something is still wrong with this physical
  586. * eraseblock and we anyway cannot treat it as empty.
  587. */
  588. if (read_err != -EBADMSG &&
  589. check_pattern(ec_hdr, 0xFF, UBI_EC_HDR_SIZE)) {
  590. /* The physical eraseblock is supposedly empty */
  591. /*
  592. * The below is just a paranoid check, it has to be
  593. * compiled out if paranoid checks are disabled.
  594. */
  595. err = paranoid_check_all_ff(ubi, pnum, 0,
  596. ubi->peb_size);
  597. if (err)
  598. return err > 0 ? UBI_IO_BAD_EC_HDR : err;
  599. if (verbose)
  600. ubi_warn("no EC header found at PEB %d, "
  601. "only 0xFF bytes", pnum);
  602. return UBI_IO_PEB_EMPTY;
  603. }
  604. /*
  605. * This is not a valid erase counter header, and these are not
  606. * 0xFF bytes. Report that the header is corrupted.
  607. */
  608. if (verbose) {
  609. ubi_warn("bad magic number at PEB %d: %08x instead of "
  610. "%08x", pnum, magic, UBI_EC_HDR_MAGIC);
  611. ubi_dbg_dump_ec_hdr(ec_hdr);
  612. }
  613. return UBI_IO_BAD_EC_HDR;
  614. }
  615. crc = crc32(UBI_CRC32_INIT, ec_hdr, UBI_EC_HDR_SIZE_CRC);
  616. hdr_crc = be32_to_cpu(ec_hdr->hdr_crc);
  617. if (hdr_crc != crc) {
  618. if (verbose) {
  619. ubi_warn("bad EC header CRC at PEB %d, calculated %#08x,"
  620. " read %#08x", pnum, crc, hdr_crc);
  621. ubi_dbg_dump_ec_hdr(ec_hdr);
  622. }
  623. return UBI_IO_BAD_EC_HDR;
  624. }
  625. /* And of course validate what has just been read from the media */
  626. err = validate_ec_hdr(ubi, ec_hdr);
  627. if (err) {
  628. ubi_err("validation failed for PEB %d", pnum);
  629. return -EINVAL;
  630. }
  631. return read_err ? UBI_IO_BITFLIPS : 0;
  632. }
  633. /**
  634. * ubi_io_write_ec_hdr - write an erase counter header.
  635. * @ubi: UBI device description object
  636. * @pnum: physical eraseblock to write to
  637. * @ec_hdr: the erase counter header to write
  638. *
  639. * This function writes erase counter header described by @ec_hdr to physical
  640. * eraseblock @pnum. It also fills most fields of @ec_hdr before writing, so
  641. * the caller do not have to fill them. Callers must only fill the @ec_hdr->ec
  642. * field.
  643. *
  644. * This function returns zero in case of success and a negative error code in
  645. * case of failure. If %-EIO is returned, the physical eraseblock most probably
  646. * went bad.
  647. */
  648. int ubi_io_write_ec_hdr(struct ubi_device *ubi, int pnum,
  649. struct ubi_ec_hdr *ec_hdr)
  650. {
  651. int err;
  652. uint32_t crc;
  653. dbg_io("write EC header to PEB %d", pnum);
  654. ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
  655. ec_hdr->magic = cpu_to_be32(UBI_EC_HDR_MAGIC);
  656. ec_hdr->version = UBI_VERSION;
  657. ec_hdr->vid_hdr_offset = cpu_to_be32(ubi->vid_hdr_offset);
  658. ec_hdr->data_offset = cpu_to_be32(ubi->leb_start);
  659. crc = crc32(UBI_CRC32_INIT, ec_hdr, UBI_EC_HDR_SIZE_CRC);
  660. ec_hdr->hdr_crc = cpu_to_be32(crc);
  661. err = paranoid_check_ec_hdr(ubi, pnum, ec_hdr);
  662. if (err)
  663. return -EINVAL;
  664. err = ubi_io_write(ubi, ec_hdr, pnum, 0, ubi->ec_hdr_alsize);
  665. return err;
  666. }
  667. /**
  668. * validate_vid_hdr - validate a volume identifier header.
  669. * @ubi: UBI device description object
  670. * @vid_hdr: the volume identifier header to check
  671. *
  672. * This function checks that data stored in the volume identifier header
  673. * @vid_hdr. Returns zero if the VID header is OK and %1 if not.
  674. */
  675. static int validate_vid_hdr(const struct ubi_device *ubi,
  676. const struct ubi_vid_hdr *vid_hdr)
  677. {
  678. int vol_type = vid_hdr->vol_type;
  679. int copy_flag = vid_hdr->copy_flag;
  680. int vol_id = be32_to_cpu(vid_hdr->vol_id);
  681. int lnum = be32_to_cpu(vid_hdr->lnum);
  682. int compat = vid_hdr->compat;
  683. int data_size = be32_to_cpu(vid_hdr->data_size);
  684. int used_ebs = be32_to_cpu(vid_hdr->used_ebs);
  685. int data_pad = be32_to_cpu(vid_hdr->data_pad);
  686. int data_crc = be32_to_cpu(vid_hdr->data_crc);
  687. int usable_leb_size = ubi->leb_size - data_pad;
  688. if (copy_flag != 0 && copy_flag != 1) {
  689. dbg_err("bad copy_flag");
  690. goto bad;
  691. }
  692. if (vol_id < 0 || lnum < 0 || data_size < 0 || used_ebs < 0 ||
  693. data_pad < 0) {
  694. dbg_err("negative values");
  695. goto bad;
  696. }
  697. if (vol_id >= UBI_MAX_VOLUMES && vol_id < UBI_INTERNAL_VOL_START) {
  698. dbg_err("bad vol_id");
  699. goto bad;
  700. }
  701. if (vol_id < UBI_INTERNAL_VOL_START && compat != 0) {
  702. dbg_err("bad compat");
  703. goto bad;
  704. }
  705. if (vol_id >= UBI_INTERNAL_VOL_START && compat != UBI_COMPAT_DELETE &&
  706. compat != UBI_COMPAT_RO && compat != UBI_COMPAT_PRESERVE &&
  707. compat != UBI_COMPAT_REJECT) {
  708. dbg_err("bad compat");
  709. goto bad;
  710. }
  711. if (vol_type != UBI_VID_DYNAMIC && vol_type != UBI_VID_STATIC) {
  712. dbg_err("bad vol_type");
  713. goto bad;
  714. }
  715. if (data_pad >= ubi->leb_size / 2) {
  716. dbg_err("bad data_pad");
  717. goto bad;
  718. }
  719. if (vol_type == UBI_VID_STATIC) {
  720. /*
  721. * Although from high-level point of view static volumes may
  722. * contain zero bytes of data, but no VID headers can contain
  723. * zero at these fields, because they empty volumes do not have
  724. * mapped logical eraseblocks.
  725. */
  726. if (used_ebs == 0) {
  727. dbg_err("zero used_ebs");
  728. goto bad;
  729. }
  730. if (data_size == 0) {
  731. dbg_err("zero data_size");
  732. goto bad;
  733. }
  734. if (lnum < used_ebs - 1) {
  735. if (data_size != usable_leb_size) {
  736. dbg_err("bad data_size");
  737. goto bad;
  738. }
  739. } else if (lnum == used_ebs - 1) {
  740. if (data_size == 0) {
  741. dbg_err("bad data_size at last LEB");
  742. goto bad;
  743. }
  744. } else {
  745. dbg_err("too high lnum");
  746. goto bad;
  747. }
  748. } else {
  749. if (copy_flag == 0) {
  750. if (data_crc != 0) {
  751. dbg_err("non-zero data CRC");
  752. goto bad;
  753. }
  754. if (data_size != 0) {
  755. dbg_err("non-zero data_size");
  756. goto bad;
  757. }
  758. } else {
  759. if (data_size == 0) {
  760. dbg_err("zero data_size of copy");
  761. goto bad;
  762. }
  763. }
  764. if (used_ebs != 0) {
  765. dbg_err("bad used_ebs");
  766. goto bad;
  767. }
  768. }
  769. return 0;
  770. bad:
  771. ubi_err("bad VID header");
  772. ubi_dbg_dump_vid_hdr(vid_hdr);
  773. ubi_dbg_dump_stack();
  774. return 1;
  775. }
  776. /**
  777. * ubi_io_read_vid_hdr - read and check a volume identifier header.
  778. * @ubi: UBI device description object
  779. * @pnum: physical eraseblock number to read from
  780. * @vid_hdr: &struct ubi_vid_hdr object where to store the read volume
  781. * identifier header
  782. * @verbose: be verbose if the header is corrupted or wasn't found
  783. *
  784. * This function reads the volume identifier header from physical eraseblock
  785. * @pnum and stores it in @vid_hdr. It also checks CRC checksum of the read
  786. * volume identifier header. The following codes may be returned:
  787. *
  788. * o %0 if the CRC checksum is correct and the header was successfully read;
  789. * o %UBI_IO_BITFLIPS if the CRC is correct, but bit-flips were detected
  790. * and corrected by the flash driver; this is harmless but may indicate that
  791. * this eraseblock may become bad soon;
  792. * o %UBI_IO_BAD_VID_HRD if the volume identifier header is corrupted (a CRC
  793. * error detected);
  794. * o %UBI_IO_PEB_FREE if the physical eraseblock is free (i.e., there is no VID
  795. * header there);
  796. * o a negative error code in case of failure.
  797. */
  798. int ubi_io_read_vid_hdr(struct ubi_device *ubi, int pnum,
  799. struct ubi_vid_hdr *vid_hdr, int verbose)
  800. {
  801. int err, read_err = 0;
  802. uint32_t crc, magic, hdr_crc;
  803. void *p;
  804. dbg_io("read VID header from PEB %d", pnum);
  805. ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
  806. if (UBI_IO_DEBUG)
  807. verbose = 1;
  808. p = (char *)vid_hdr - ubi->vid_hdr_shift;
  809. err = ubi_io_read(ubi, p, pnum, ubi->vid_hdr_aloffset,
  810. ubi->vid_hdr_alsize);
  811. if (err) {
  812. if (err != UBI_IO_BITFLIPS && err != -EBADMSG)
  813. return err;
  814. /*
  815. * We read all the data, but either a correctable bit-flip
  816. * occurred, or MTD reported about some data integrity error,
  817. * like an ECC error in case of NAND. The former is harmless,
  818. * the later may mean the read data is corrupted. But we have a
  819. * CRC check-sum and we will identify this. If the VID header is
  820. * still OK, we just report this as there was a bit-flip.
  821. */
  822. read_err = err;
  823. }
  824. magic = be32_to_cpu(vid_hdr->magic);
  825. if (magic != UBI_VID_HDR_MAGIC) {
  826. /*
  827. * If we have read all 0xFF bytes, the VID header probably does
  828. * not exist and the physical eraseblock is assumed to be free.
  829. *
  830. * But if there was a read error, we do not test the data for
  831. * 0xFFs. Even if it does contain all 0xFFs, this error
  832. * indicates that something is still wrong with this physical
  833. * eraseblock and it cannot be regarded as free.
  834. */
  835. if (read_err != -EBADMSG &&
  836. check_pattern(vid_hdr, 0xFF, UBI_VID_HDR_SIZE)) {
  837. /* The physical eraseblock is supposedly free */
  838. /*
  839. * The below is just a paranoid check, it has to be
  840. * compiled out if paranoid checks are disabled.
  841. */
  842. err = paranoid_check_all_ff(ubi, pnum, ubi->leb_start,
  843. ubi->leb_size);
  844. if (err)
  845. return err > 0 ? UBI_IO_BAD_VID_HDR : err;
  846. if (verbose)
  847. ubi_warn("no VID header found at PEB %d, "
  848. "only 0xFF bytes", pnum);
  849. return UBI_IO_PEB_FREE;
  850. }
  851. /*
  852. * This is not a valid VID header, and these are not 0xFF
  853. * bytes. Report that the header is corrupted.
  854. */
  855. if (verbose) {
  856. ubi_warn("bad magic number at PEB %d: %08x instead of "
  857. "%08x", pnum, magic, UBI_VID_HDR_MAGIC);
  858. ubi_dbg_dump_vid_hdr(vid_hdr);
  859. }
  860. return UBI_IO_BAD_VID_HDR;
  861. }
  862. crc = crc32(UBI_CRC32_INIT, vid_hdr, UBI_VID_HDR_SIZE_CRC);
  863. hdr_crc = be32_to_cpu(vid_hdr->hdr_crc);
  864. if (hdr_crc != crc) {
  865. if (verbose) {
  866. ubi_warn("bad CRC at PEB %d, calculated %#08x, "
  867. "read %#08x", pnum, crc, hdr_crc);
  868. ubi_dbg_dump_vid_hdr(vid_hdr);
  869. }
  870. return UBI_IO_BAD_VID_HDR;
  871. }
  872. /* Validate the VID header that we have just read */
  873. err = validate_vid_hdr(ubi, vid_hdr);
  874. if (err) {
  875. ubi_err("validation failed for PEB %d", pnum);
  876. return -EINVAL;
  877. }
  878. return read_err ? UBI_IO_BITFLIPS : 0;
  879. }
  880. /**
  881. * ubi_io_write_vid_hdr - write a volume identifier header.
  882. * @ubi: UBI device description object
  883. * @pnum: the physical eraseblock number to write to
  884. * @vid_hdr: the volume identifier header to write
  885. *
  886. * This function writes the volume identifier header described by @vid_hdr to
  887. * physical eraseblock @pnum. This function automatically fills the
  888. * @vid_hdr->magic and the @vid_hdr->version fields, as well as calculates
  889. * header CRC checksum and stores it at vid_hdr->hdr_crc.
  890. *
  891. * This function returns zero in case of success and a negative error code in
  892. * case of failure. If %-EIO is returned, the physical eraseblock probably went
  893. * bad.
  894. */
  895. int ubi_io_write_vid_hdr(struct ubi_device *ubi, int pnum,
  896. struct ubi_vid_hdr *vid_hdr)
  897. {
  898. int err;
  899. uint32_t crc;
  900. void *p;
  901. dbg_io("write VID header to PEB %d", pnum);
  902. ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
  903. err = paranoid_check_peb_ec_hdr(ubi, pnum);
  904. if (err)
  905. return err > 0 ? -EINVAL: err;
  906. vid_hdr->magic = cpu_to_be32(UBI_VID_HDR_MAGIC);
  907. vid_hdr->version = UBI_VERSION;
  908. crc = crc32(UBI_CRC32_INIT, vid_hdr, UBI_VID_HDR_SIZE_CRC);
  909. vid_hdr->hdr_crc = cpu_to_be32(crc);
  910. err = paranoid_check_vid_hdr(ubi, pnum, vid_hdr);
  911. if (err)
  912. return -EINVAL;
  913. p = (char *)vid_hdr - ubi->vid_hdr_shift;
  914. err = ubi_io_write(ubi, p, pnum, ubi->vid_hdr_aloffset,
  915. ubi->vid_hdr_alsize);
  916. return err;
  917. }
  918. #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
  919. /**
  920. * paranoid_check_not_bad - ensure that a physical eraseblock is not bad.
  921. * @ubi: UBI device description object
  922. * @pnum: physical eraseblock number to check
  923. *
  924. * This function returns zero if the physical eraseblock is good, a positive
  925. * number if it is bad and a negative error code if an error occurred.
  926. */
  927. static int paranoid_check_not_bad(const struct ubi_device *ubi, int pnum)
  928. {
  929. int err;
  930. err = ubi_io_is_bad(ubi, pnum);
  931. if (!err)
  932. return err;
  933. ubi_err("paranoid check failed for PEB %d", pnum);
  934. ubi_dbg_dump_stack();
  935. return err;
  936. }
  937. /**
  938. * paranoid_check_ec_hdr - check if an erase counter header is all right.
  939. * @ubi: UBI device description object
  940. * @pnum: physical eraseblock number the erase counter header belongs to
  941. * @ec_hdr: the erase counter header to check
  942. *
  943. * This function returns zero if the erase counter header contains valid
  944. * values, and %1 if not.
  945. */
  946. static int paranoid_check_ec_hdr(const struct ubi_device *ubi, int pnum,
  947. const struct ubi_ec_hdr *ec_hdr)
  948. {
  949. int err;
  950. uint32_t magic;
  951. magic = be32_to_cpu(ec_hdr->magic);
  952. if (magic != UBI_EC_HDR_MAGIC) {
  953. ubi_err("bad magic %#08x, must be %#08x",
  954. magic, UBI_EC_HDR_MAGIC);
  955. goto fail;
  956. }
  957. err = validate_ec_hdr(ubi, ec_hdr);
  958. if (err) {
  959. ubi_err("paranoid check failed for PEB %d", pnum);
  960. goto fail;
  961. }
  962. return 0;
  963. fail:
  964. ubi_dbg_dump_ec_hdr(ec_hdr);
  965. ubi_dbg_dump_stack();
  966. return 1;
  967. }
  968. /**
  969. * paranoid_check_peb_ec_hdr - check that the erase counter header of a
  970. * physical eraseblock is in-place and is all right.
  971. * @ubi: UBI device description object
  972. * @pnum: the physical eraseblock number to check
  973. *
  974. * This function returns zero if the erase counter header is all right, %1 if
  975. * not, and a negative error code if an error occurred.
  976. */
  977. static int paranoid_check_peb_ec_hdr(const struct ubi_device *ubi, int pnum)
  978. {
  979. int err;
  980. uint32_t crc, hdr_crc;
  981. struct ubi_ec_hdr *ec_hdr;
  982. ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_NOFS);
  983. if (!ec_hdr)
  984. return -ENOMEM;
  985. err = ubi_io_read(ubi, ec_hdr, pnum, 0, UBI_EC_HDR_SIZE);
  986. if (err && err != UBI_IO_BITFLIPS && err != -EBADMSG)
  987. goto exit;
  988. crc = crc32(UBI_CRC32_INIT, ec_hdr, UBI_EC_HDR_SIZE_CRC);
  989. hdr_crc = be32_to_cpu(ec_hdr->hdr_crc);
  990. if (hdr_crc != crc) {
  991. ubi_err("bad CRC, calculated %#08x, read %#08x", crc, hdr_crc);
  992. ubi_err("paranoid check failed for PEB %d", pnum);
  993. ubi_dbg_dump_ec_hdr(ec_hdr);
  994. ubi_dbg_dump_stack();
  995. err = 1;
  996. goto exit;
  997. }
  998. err = paranoid_check_ec_hdr(ubi, pnum, ec_hdr);
  999. exit:
  1000. kfree(ec_hdr);
  1001. return err;
  1002. }
  1003. /**
  1004. * paranoid_check_vid_hdr - check that a volume identifier header is all right.
  1005. * @ubi: UBI device description object
  1006. * @pnum: physical eraseblock number the volume identifier header belongs to
  1007. * @vid_hdr: the volume identifier header to check
  1008. *
  1009. * This function returns zero if the volume identifier header is all right, and
  1010. * %1 if not.
  1011. */
  1012. static int paranoid_check_vid_hdr(const struct ubi_device *ubi, int pnum,
  1013. const struct ubi_vid_hdr *vid_hdr)
  1014. {
  1015. int err;
  1016. uint32_t magic;
  1017. magic = be32_to_cpu(vid_hdr->magic);
  1018. if (magic != UBI_VID_HDR_MAGIC) {
  1019. ubi_err("bad VID header magic %#08x at PEB %d, must be %#08x",
  1020. magic, pnum, UBI_VID_HDR_MAGIC);
  1021. goto fail;
  1022. }
  1023. err = validate_vid_hdr(ubi, vid_hdr);
  1024. if (err) {
  1025. ubi_err("paranoid check failed for PEB %d", pnum);
  1026. goto fail;
  1027. }
  1028. return err;
  1029. fail:
  1030. ubi_err("paranoid check failed for PEB %d", pnum);
  1031. ubi_dbg_dump_vid_hdr(vid_hdr);
  1032. ubi_dbg_dump_stack();
  1033. return 1;
  1034. }
  1035. /**
  1036. * paranoid_check_peb_vid_hdr - check that the volume identifier header of a
  1037. * physical eraseblock is in-place and is all right.
  1038. * @ubi: UBI device description object
  1039. * @pnum: the physical eraseblock number to check
  1040. *
  1041. * This function returns zero if the volume identifier header is all right,
  1042. * %1 if not, and a negative error code if an error occurred.
  1043. */
  1044. static int paranoid_check_peb_vid_hdr(const struct ubi_device *ubi, int pnum)
  1045. {
  1046. int err;
  1047. uint32_t crc, hdr_crc;
  1048. struct ubi_vid_hdr *vid_hdr;
  1049. void *p;
  1050. vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
  1051. if (!vid_hdr)
  1052. return -ENOMEM;
  1053. p = (char *)vid_hdr - ubi->vid_hdr_shift;
  1054. err = ubi_io_read(ubi, p, pnum, ubi->vid_hdr_aloffset,
  1055. ubi->vid_hdr_alsize);
  1056. if (err && err != UBI_IO_BITFLIPS && err != -EBADMSG)
  1057. goto exit;
  1058. crc = crc32(UBI_CRC32_INIT, vid_hdr, UBI_EC_HDR_SIZE_CRC);
  1059. hdr_crc = be32_to_cpu(vid_hdr->hdr_crc);
  1060. if (hdr_crc != crc) {
  1061. ubi_err("bad VID header CRC at PEB %d, calculated %#08x, "
  1062. "read %#08x", pnum, crc, hdr_crc);
  1063. ubi_err("paranoid check failed for PEB %d", pnum);
  1064. ubi_dbg_dump_vid_hdr(vid_hdr);
  1065. ubi_dbg_dump_stack();
  1066. err = 1;
  1067. goto exit;
  1068. }
  1069. err = paranoid_check_vid_hdr(ubi, pnum, vid_hdr);
  1070. exit:
  1071. ubi_free_vid_hdr(ubi, vid_hdr);
  1072. return err;
  1073. }
  1074. /**
  1075. * paranoid_check_all_ff - check that a region of flash is empty.
  1076. * @ubi: UBI device description object
  1077. * @pnum: the physical eraseblock number to check
  1078. * @offset: the starting offset within the physical eraseblock to check
  1079. * @len: the length of the region to check
  1080. *
  1081. * This function returns zero if only 0xFF bytes are present at offset
  1082. * @offset of the physical eraseblock @pnum, %1 if not, and a negative error
  1083. * code if an error occurred.
  1084. */
  1085. static int paranoid_check_all_ff(struct ubi_device *ubi, int pnum, int offset,
  1086. int len)
  1087. {
  1088. size_t read;
  1089. int err;
  1090. loff_t addr = (loff_t)pnum * ubi->peb_size + offset;
  1091. mutex_lock(&ubi->dbg_buf_mutex);
  1092. err = ubi->mtd->read(ubi->mtd, addr, len, &read, ubi->dbg_peb_buf);
  1093. if (err && err != -EUCLEAN) {
  1094. ubi_err("error %d while reading %d bytes from PEB %d:%d, "
  1095. "read %zd bytes", err, len, pnum, offset, read);
  1096. goto error;
  1097. }
  1098. err = check_pattern(ubi->dbg_peb_buf, 0xFF, len);
  1099. if (err == 0) {
  1100. ubi_err("flash region at PEB %d:%d, length %d does not "
  1101. "contain all 0xFF bytes", pnum, offset, len);
  1102. goto fail;
  1103. }
  1104. mutex_unlock(&ubi->dbg_buf_mutex);
  1105. return 0;
  1106. fail:
  1107. ubi_err("paranoid check failed for PEB %d", pnum);
  1108. dbg_msg("hex dump of the %d-%d region", offset, offset + len);
  1109. print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1,
  1110. ubi->dbg_peb_buf, len, 1);
  1111. err = 1;
  1112. error:
  1113. ubi_dbg_dump_stack();
  1114. mutex_unlock(&ubi->dbg_buf_mutex);
  1115. return err;
  1116. }
  1117. #endif /* CONFIG_MTD_UBI_DEBUG_PARANOID */