io.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255
  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. } else {
  167. ubi_assert(len == read);
  168. if (ubi_dbg_is_bitflip()) {
  169. dbg_msg("bit-flip (emulated)");
  170. err = UBI_IO_BITFLIPS;
  171. }
  172. }
  173. return err;
  174. }
  175. /**
  176. * ubi_io_write - write data to a physical eraseblock.
  177. * @ubi: UBI device description object
  178. * @buf: buffer with the data to write
  179. * @pnum: physical eraseblock number to write to
  180. * @offset: offset within the physical eraseblock where to write
  181. * @len: how many bytes to write
  182. *
  183. * This function writes @len bytes of data from buffer @buf to offset @offset
  184. * of physical eraseblock @pnum. If all the data were successfully written,
  185. * zero is returned. If an error occurred, this function returns a negative
  186. * error code. If %-EIO is returned, the physical eraseblock most probably went
  187. * bad.
  188. *
  189. * Note, in case of an error, it is possible that something was still written
  190. * to the flash media, but may be some garbage.
  191. */
  192. int ubi_io_write(struct ubi_device *ubi, const void *buf, int pnum, int offset,
  193. int len)
  194. {
  195. int err;
  196. size_t written;
  197. loff_t addr;
  198. dbg_io("write %d bytes to PEB %d:%d", len, pnum, offset);
  199. ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
  200. ubi_assert(offset >= 0 && offset + len <= ubi->peb_size);
  201. ubi_assert(offset % ubi->hdrs_min_io_size == 0);
  202. ubi_assert(len > 0 && len % ubi->hdrs_min_io_size == 0);
  203. if (ubi->ro_mode) {
  204. ubi_err("read-only mode");
  205. return -EROFS;
  206. }
  207. /* The below has to be compiled out if paranoid checks are disabled */
  208. err = paranoid_check_not_bad(ubi, pnum);
  209. if (err)
  210. return err > 0 ? -EINVAL : err;
  211. /* The area we are writing to has to contain all 0xFF bytes */
  212. err = paranoid_check_all_ff(ubi, pnum, offset, len);
  213. if (err)
  214. return err > 0 ? -EINVAL : err;
  215. if (offset >= ubi->leb_start) {
  216. /*
  217. * We write to the data area of the physical eraseblock. Make
  218. * sure it has valid EC and VID headers.
  219. */
  220. err = paranoid_check_peb_ec_hdr(ubi, pnum);
  221. if (err)
  222. return err > 0 ? -EINVAL : err;
  223. err = paranoid_check_peb_vid_hdr(ubi, pnum);
  224. if (err)
  225. return err > 0 ? -EINVAL : err;
  226. }
  227. if (ubi_dbg_is_write_failure()) {
  228. dbg_err("cannot write %d bytes to PEB %d:%d "
  229. "(emulated)", len, pnum, offset);
  230. ubi_dbg_dump_stack();
  231. return -EIO;
  232. }
  233. addr = (loff_t)pnum * ubi->peb_size + offset;
  234. err = ubi->mtd->write(ubi->mtd, addr, len, &written, buf);
  235. if (err) {
  236. ubi_err("error %d while writing %d bytes to PEB %d:%d, written"
  237. " %zd bytes", err, len, pnum, offset, written);
  238. ubi_dbg_dump_stack();
  239. } else
  240. ubi_assert(written == len);
  241. return err;
  242. }
  243. /**
  244. * erase_callback - MTD erasure call-back.
  245. * @ei: MTD erase information object.
  246. *
  247. * Note, even though MTD erase interface is asynchronous, all the current
  248. * implementations are synchronous anyway.
  249. */
  250. static void erase_callback(struct erase_info *ei)
  251. {
  252. wake_up_interruptible((wait_queue_head_t *)ei->priv);
  253. }
  254. /**
  255. * do_sync_erase - synchronously erase a physical eraseblock.
  256. * @ubi: UBI device description object
  257. * @pnum: the physical eraseblock number to erase
  258. *
  259. * This function synchronously erases physical eraseblock @pnum and returns
  260. * zero in case of success and a negative error code in case of failure. If
  261. * %-EIO is returned, the physical eraseblock most probably went bad.
  262. */
  263. static int do_sync_erase(struct ubi_device *ubi, int pnum)
  264. {
  265. int err, retries = 0;
  266. struct erase_info ei;
  267. wait_queue_head_t wq;
  268. dbg_io("erase PEB %d", pnum);
  269. retry:
  270. init_waitqueue_head(&wq);
  271. memset(&ei, 0, sizeof(struct erase_info));
  272. ei.mtd = ubi->mtd;
  273. ei.addr = (loff_t)pnum * ubi->peb_size;
  274. ei.len = ubi->peb_size;
  275. ei.callback = erase_callback;
  276. ei.priv = (unsigned long)&wq;
  277. err = ubi->mtd->erase(ubi->mtd, &ei);
  278. if (err) {
  279. if (retries++ < UBI_IO_RETRIES) {
  280. dbg_io("error %d while erasing PEB %d, retry",
  281. err, pnum);
  282. yield();
  283. goto retry;
  284. }
  285. ubi_err("cannot erase PEB %d, error %d", pnum, err);
  286. ubi_dbg_dump_stack();
  287. return err;
  288. }
  289. err = wait_event_interruptible(wq, ei.state == MTD_ERASE_DONE ||
  290. ei.state == MTD_ERASE_FAILED);
  291. if (err) {
  292. ubi_err("interrupted PEB %d erasure", pnum);
  293. return -EINTR;
  294. }
  295. if (ei.state == MTD_ERASE_FAILED) {
  296. if (retries++ < UBI_IO_RETRIES) {
  297. dbg_io("error while erasing PEB %d, retry", pnum);
  298. yield();
  299. goto retry;
  300. }
  301. ubi_err("cannot erase PEB %d", pnum);
  302. ubi_dbg_dump_stack();
  303. return -EIO;
  304. }
  305. err = paranoid_check_all_ff(ubi, pnum, 0, ubi->peb_size);
  306. if (err)
  307. return err > 0 ? -EINVAL : err;
  308. if (ubi_dbg_is_erase_failure() && !err) {
  309. dbg_err("cannot erase PEB %d (emulated)", pnum);
  310. return -EIO;
  311. }
  312. return 0;
  313. }
  314. /**
  315. * check_pattern - check if buffer contains only a certain byte pattern.
  316. * @buf: buffer to check
  317. * @patt: the pattern to check
  318. * @size: buffer size in bytes
  319. *
  320. * This function returns %1 in there are only @patt bytes in @buf, and %0 if
  321. * something else was also found.
  322. */
  323. static int check_pattern(const void *buf, uint8_t patt, int size)
  324. {
  325. int i;
  326. for (i = 0; i < size; i++)
  327. if (((const uint8_t *)buf)[i] != patt)
  328. return 0;
  329. return 1;
  330. }
  331. /* Patterns to write to a physical eraseblock when torturing it */
  332. static uint8_t patterns[] = {0xa5, 0x5a, 0x0};
  333. /**
  334. * torture_peb - test a supposedly bad physical eraseblock.
  335. * @ubi: UBI device description object
  336. * @pnum: the physical eraseblock number to test
  337. *
  338. * This function returns %-EIO if the physical eraseblock did not pass the
  339. * test, a positive number of erase operations done if the test was
  340. * successfully passed, and other negative error codes in case of other errors.
  341. */
  342. static int torture_peb(struct ubi_device *ubi, int pnum)
  343. {
  344. int err, i, patt_count;
  345. patt_count = ARRAY_SIZE(patterns);
  346. ubi_assert(patt_count > 0);
  347. mutex_lock(&ubi->buf_mutex);
  348. for (i = 0; i < patt_count; i++) {
  349. err = do_sync_erase(ubi, pnum);
  350. if (err)
  351. goto out;
  352. /* Make sure the PEB contains only 0xFF bytes */
  353. err = ubi_io_read(ubi, ubi->peb_buf1, pnum, 0, ubi->peb_size);
  354. if (err)
  355. goto out;
  356. err = check_pattern(ubi->peb_buf1, 0xFF, ubi->peb_size);
  357. if (err == 0) {
  358. ubi_err("erased PEB %d, but a non-0xFF byte found",
  359. pnum);
  360. err = -EIO;
  361. goto out;
  362. }
  363. /* Write a pattern and check it */
  364. memset(ubi->peb_buf1, patterns[i], ubi->peb_size);
  365. err = ubi_io_write(ubi, ubi->peb_buf1, pnum, 0, ubi->peb_size);
  366. if (err)
  367. goto out;
  368. memset(ubi->peb_buf1, ~patterns[i], ubi->peb_size);
  369. err = ubi_io_read(ubi, ubi->peb_buf1, pnum, 0, ubi->peb_size);
  370. if (err)
  371. goto out;
  372. err = check_pattern(ubi->peb_buf1, patterns[i], ubi->peb_size);
  373. if (err == 0) {
  374. ubi_err("pattern %x checking failed for PEB %d",
  375. patterns[i], pnum);
  376. err = -EIO;
  377. goto out;
  378. }
  379. }
  380. err = patt_count;
  381. out:
  382. mutex_unlock(&ubi->buf_mutex);
  383. if (err == UBI_IO_BITFLIPS || err == -EBADMSG) {
  384. /*
  385. * If a bit-flip or data integrity error was detected, the test
  386. * has not passed because it happened on a freshly erased
  387. * physical eraseblock which means something is wrong with it.
  388. */
  389. ubi_err("read problems on freshly erased PEB %d, must be bad",
  390. pnum);
  391. err = -EIO;
  392. }
  393. return err;
  394. }
  395. /**
  396. * ubi_io_sync_erase - synchronously erase a physical eraseblock.
  397. * @ubi: UBI device description object
  398. * @pnum: physical eraseblock number to erase
  399. * @torture: if this physical eraseblock has to be tortured
  400. *
  401. * This function synchronously erases physical eraseblock @pnum. If @torture
  402. * flag is not zero, the physical eraseblock is checked by means of writing
  403. * different patterns to it and reading them back. If the torturing is enabled,
  404. * the physical eraseblock is erased more then once.
  405. *
  406. * This function returns the number of erasures made in case of success, %-EIO
  407. * if the erasure failed or the torturing test failed, and other negative error
  408. * codes in case of other errors. Note, %-EIO means that the physical
  409. * eraseblock is bad.
  410. */
  411. int ubi_io_sync_erase(struct ubi_device *ubi, int pnum, int torture)
  412. {
  413. int err, ret = 0;
  414. ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
  415. err = paranoid_check_not_bad(ubi, pnum);
  416. if (err != 0)
  417. return err > 0 ? -EINVAL : err;
  418. if (ubi->ro_mode) {
  419. ubi_err("read-only mode");
  420. return -EROFS;
  421. }
  422. if (torture) {
  423. ret = torture_peb(ubi, pnum);
  424. if (ret < 0)
  425. return ret;
  426. }
  427. err = do_sync_erase(ubi, pnum);
  428. if (err)
  429. return err;
  430. return ret + 1;
  431. }
  432. /**
  433. * ubi_io_is_bad - check if a physical eraseblock is bad.
  434. * @ubi: UBI device description object
  435. * @pnum: the physical eraseblock number to check
  436. *
  437. * This function returns a positive number if the physical eraseblock is bad,
  438. * zero if not, and a negative error code if an error occurred.
  439. */
  440. int ubi_io_is_bad(const struct ubi_device *ubi, int pnum)
  441. {
  442. struct mtd_info *mtd = ubi->mtd;
  443. ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
  444. if (ubi->bad_allowed) {
  445. int ret;
  446. ret = mtd->block_isbad(mtd, (loff_t)pnum * ubi->peb_size);
  447. if (ret < 0)
  448. ubi_err("error %d while checking if PEB %d is bad",
  449. ret, pnum);
  450. else if (ret)
  451. dbg_io("PEB %d is bad", pnum);
  452. return ret;
  453. }
  454. return 0;
  455. }
  456. /**
  457. * ubi_io_mark_bad - mark a physical eraseblock as bad.
  458. * @ubi: UBI device description object
  459. * @pnum: the physical eraseblock number to mark
  460. *
  461. * This function returns zero in case of success and a negative error code in
  462. * case of failure.
  463. */
  464. int ubi_io_mark_bad(const struct ubi_device *ubi, int pnum)
  465. {
  466. int err;
  467. struct mtd_info *mtd = ubi->mtd;
  468. ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
  469. if (ubi->ro_mode) {
  470. ubi_err("read-only mode");
  471. return -EROFS;
  472. }
  473. if (!ubi->bad_allowed)
  474. return 0;
  475. err = mtd->block_markbad(mtd, (loff_t)pnum * ubi->peb_size);
  476. if (err)
  477. ubi_err("cannot mark PEB %d bad, error %d", pnum, err);
  478. return err;
  479. }
  480. /**
  481. * validate_ec_hdr - validate an erase counter header.
  482. * @ubi: UBI device description object
  483. * @ec_hdr: the erase counter header to check
  484. *
  485. * This function returns zero if the erase counter header is OK, and %1 if
  486. * not.
  487. */
  488. static int validate_ec_hdr(const struct ubi_device *ubi,
  489. const struct ubi_ec_hdr *ec_hdr)
  490. {
  491. long long ec;
  492. int vid_hdr_offset, leb_start;
  493. ec = be64_to_cpu(ec_hdr->ec);
  494. vid_hdr_offset = be32_to_cpu(ec_hdr->vid_hdr_offset);
  495. leb_start = be32_to_cpu(ec_hdr->data_offset);
  496. if (ec_hdr->version != UBI_VERSION) {
  497. ubi_err("node with incompatible UBI version found: "
  498. "this UBI version is %d, image version is %d",
  499. UBI_VERSION, (int)ec_hdr->version);
  500. goto bad;
  501. }
  502. if (vid_hdr_offset != ubi->vid_hdr_offset) {
  503. ubi_err("bad VID header offset %d, expected %d",
  504. vid_hdr_offset, ubi->vid_hdr_offset);
  505. goto bad;
  506. }
  507. if (leb_start != ubi->leb_start) {
  508. ubi_err("bad data offset %d, expected %d",
  509. leb_start, ubi->leb_start);
  510. goto bad;
  511. }
  512. if (ec < 0 || ec > UBI_MAX_ERASECOUNTER) {
  513. ubi_err("bad erase counter %lld", ec);
  514. goto bad;
  515. }
  516. return 0;
  517. bad:
  518. ubi_err("bad EC header");
  519. ubi_dbg_dump_ec_hdr(ec_hdr);
  520. ubi_dbg_dump_stack();
  521. return 1;
  522. }
  523. /**
  524. * ubi_io_read_ec_hdr - read and check an erase counter header.
  525. * @ubi: UBI device description object
  526. * @pnum: physical eraseblock to read from
  527. * @ec_hdr: a &struct ubi_ec_hdr object where to store the read erase counter
  528. * header
  529. * @verbose: be verbose if the header is corrupted or was not found
  530. *
  531. * This function reads erase counter header from physical eraseblock @pnum and
  532. * stores it in @ec_hdr. This function also checks CRC checksum of the read
  533. * erase counter header. The following codes may be returned:
  534. *
  535. * o %0 if the CRC checksum is correct and the header was successfully read;
  536. * o %UBI_IO_BITFLIPS if the CRC is correct, but bit-flips were detected
  537. * and corrected by the flash driver; this is harmless but may indicate that
  538. * this eraseblock may become bad soon (but may be not);
  539. * o %UBI_IO_BAD_EC_HDR if the erase counter header is corrupted (a CRC error);
  540. * o %UBI_IO_PEB_EMPTY if the physical eraseblock is empty;
  541. * o a negative error code in case of failure.
  542. */
  543. int ubi_io_read_ec_hdr(struct ubi_device *ubi, int pnum,
  544. struct ubi_ec_hdr *ec_hdr, int verbose)
  545. {
  546. int err, read_err = 0;
  547. uint32_t crc, magic, hdr_crc;
  548. dbg_io("read EC header from PEB %d", pnum);
  549. ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
  550. err = ubi_io_read(ubi, ec_hdr, pnum, 0, UBI_EC_HDR_SIZE);
  551. if (err) {
  552. if (err != UBI_IO_BITFLIPS && err != -EBADMSG)
  553. return err;
  554. /*
  555. * We read all the data, but either a correctable bit-flip
  556. * occurred, or MTD reported about some data integrity error,
  557. * like an ECC error in case of NAND. The former is harmless,
  558. * the later may mean that the read data is corrupted. But we
  559. * have a CRC check-sum and we will detect this. If the EC
  560. * header is still OK, we just report this as there was a
  561. * bit-flip.
  562. */
  563. read_err = err;
  564. }
  565. magic = be32_to_cpu(ec_hdr->magic);
  566. if (magic != UBI_EC_HDR_MAGIC) {
  567. /*
  568. * The magic field is wrong. Let's check if we have read all
  569. * 0xFF. If yes, this physical eraseblock is assumed to be
  570. * empty.
  571. *
  572. * But if there was a read error, we do not test it for all
  573. * 0xFFs. Even if it does contain all 0xFFs, this error
  574. * indicates that something is still wrong with this physical
  575. * eraseblock and we anyway cannot treat it as empty.
  576. */
  577. if (read_err != -EBADMSG &&
  578. check_pattern(ec_hdr, 0xFF, UBI_EC_HDR_SIZE)) {
  579. /* The physical eraseblock is supposedly empty */
  580. /*
  581. * The below is just a paranoid check, it has to be
  582. * compiled out if paranoid checks are disabled.
  583. */
  584. err = paranoid_check_all_ff(ubi, pnum, 0,
  585. ubi->peb_size);
  586. if (err)
  587. return err > 0 ? UBI_IO_BAD_EC_HDR : err;
  588. if (verbose)
  589. ubi_warn("no EC header found at PEB %d, "
  590. "only 0xFF bytes", pnum);
  591. return UBI_IO_PEB_EMPTY;
  592. }
  593. /*
  594. * This is not a valid erase counter header, and these are not
  595. * 0xFF bytes. Report that the header is corrupted.
  596. */
  597. if (verbose) {
  598. ubi_warn("bad magic number at PEB %d: %08x instead of "
  599. "%08x", pnum, magic, UBI_EC_HDR_MAGIC);
  600. ubi_dbg_dump_ec_hdr(ec_hdr);
  601. }
  602. return UBI_IO_BAD_EC_HDR;
  603. }
  604. crc = crc32(UBI_CRC32_INIT, ec_hdr, UBI_EC_HDR_SIZE_CRC);
  605. hdr_crc = be32_to_cpu(ec_hdr->hdr_crc);
  606. if (hdr_crc != crc) {
  607. if (verbose) {
  608. ubi_warn("bad EC header CRC at PEB %d, calculated %#08x,"
  609. " read %#08x", pnum, crc, hdr_crc);
  610. ubi_dbg_dump_ec_hdr(ec_hdr);
  611. }
  612. return UBI_IO_BAD_EC_HDR;
  613. }
  614. /* And of course validate what has just been read from the media */
  615. err = validate_ec_hdr(ubi, ec_hdr);
  616. if (err) {
  617. ubi_err("validation failed for PEB %d", pnum);
  618. return -EINVAL;
  619. }
  620. return read_err ? UBI_IO_BITFLIPS : 0;
  621. }
  622. /**
  623. * ubi_io_write_ec_hdr - write an erase counter header.
  624. * @ubi: UBI device description object
  625. * @pnum: physical eraseblock to write to
  626. * @ec_hdr: the erase counter header to write
  627. *
  628. * This function writes erase counter header described by @ec_hdr to physical
  629. * eraseblock @pnum. It also fills most fields of @ec_hdr before writing, so
  630. * the caller do not have to fill them. Callers must only fill the @ec_hdr->ec
  631. * field.
  632. *
  633. * This function returns zero in case of success and a negative error code in
  634. * case of failure. If %-EIO is returned, the physical eraseblock most probably
  635. * went bad.
  636. */
  637. int ubi_io_write_ec_hdr(struct ubi_device *ubi, int pnum,
  638. struct ubi_ec_hdr *ec_hdr)
  639. {
  640. int err;
  641. uint32_t crc;
  642. dbg_io("write EC header to PEB %d", pnum);
  643. ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
  644. ec_hdr->magic = cpu_to_be32(UBI_EC_HDR_MAGIC);
  645. ec_hdr->version = UBI_VERSION;
  646. ec_hdr->vid_hdr_offset = cpu_to_be32(ubi->vid_hdr_offset);
  647. ec_hdr->data_offset = cpu_to_be32(ubi->leb_start);
  648. crc = crc32(UBI_CRC32_INIT, ec_hdr, UBI_EC_HDR_SIZE_CRC);
  649. ec_hdr->hdr_crc = cpu_to_be32(crc);
  650. err = paranoid_check_ec_hdr(ubi, pnum, ec_hdr);
  651. if (err)
  652. return -EINVAL;
  653. err = ubi_io_write(ubi, ec_hdr, pnum, 0, ubi->ec_hdr_alsize);
  654. return err;
  655. }
  656. /**
  657. * validate_vid_hdr - validate a volume identifier header.
  658. * @ubi: UBI device description object
  659. * @vid_hdr: the volume identifier header to check
  660. *
  661. * This function checks that data stored in the volume identifier header
  662. * @vid_hdr. Returns zero if the VID header is OK and %1 if not.
  663. */
  664. static int validate_vid_hdr(const struct ubi_device *ubi,
  665. const struct ubi_vid_hdr *vid_hdr)
  666. {
  667. int vol_type = vid_hdr->vol_type;
  668. int copy_flag = vid_hdr->copy_flag;
  669. int vol_id = be32_to_cpu(vid_hdr->vol_id);
  670. int lnum = be32_to_cpu(vid_hdr->lnum);
  671. int compat = vid_hdr->compat;
  672. int data_size = be32_to_cpu(vid_hdr->data_size);
  673. int used_ebs = be32_to_cpu(vid_hdr->used_ebs);
  674. int data_pad = be32_to_cpu(vid_hdr->data_pad);
  675. int data_crc = be32_to_cpu(vid_hdr->data_crc);
  676. int usable_leb_size = ubi->leb_size - data_pad;
  677. if (copy_flag != 0 && copy_flag != 1) {
  678. dbg_err("bad copy_flag");
  679. goto bad;
  680. }
  681. if (vol_id < 0 || lnum < 0 || data_size < 0 || used_ebs < 0 ||
  682. data_pad < 0) {
  683. dbg_err("negative values");
  684. goto bad;
  685. }
  686. if (vol_id >= UBI_MAX_VOLUMES && vol_id < UBI_INTERNAL_VOL_START) {
  687. dbg_err("bad vol_id");
  688. goto bad;
  689. }
  690. if (vol_id < UBI_INTERNAL_VOL_START && compat != 0) {
  691. dbg_err("bad compat");
  692. goto bad;
  693. }
  694. if (vol_id >= UBI_INTERNAL_VOL_START && compat != UBI_COMPAT_DELETE &&
  695. compat != UBI_COMPAT_RO && compat != UBI_COMPAT_PRESERVE &&
  696. compat != UBI_COMPAT_REJECT) {
  697. dbg_err("bad compat");
  698. goto bad;
  699. }
  700. if (vol_type != UBI_VID_DYNAMIC && vol_type != UBI_VID_STATIC) {
  701. dbg_err("bad vol_type");
  702. goto bad;
  703. }
  704. if (data_pad >= ubi->leb_size / 2) {
  705. dbg_err("bad data_pad");
  706. goto bad;
  707. }
  708. if (vol_type == UBI_VID_STATIC) {
  709. /*
  710. * Although from high-level point of view static volumes may
  711. * contain zero bytes of data, but no VID headers can contain
  712. * zero at these fields, because they empty volumes do not have
  713. * mapped logical eraseblocks.
  714. */
  715. if (used_ebs == 0) {
  716. dbg_err("zero used_ebs");
  717. goto bad;
  718. }
  719. if (data_size == 0) {
  720. dbg_err("zero data_size");
  721. goto bad;
  722. }
  723. if (lnum < used_ebs - 1) {
  724. if (data_size != usable_leb_size) {
  725. dbg_err("bad data_size");
  726. goto bad;
  727. }
  728. } else if (lnum == used_ebs - 1) {
  729. if (data_size == 0) {
  730. dbg_err("bad data_size at last LEB");
  731. goto bad;
  732. }
  733. } else {
  734. dbg_err("too high lnum");
  735. goto bad;
  736. }
  737. } else {
  738. if (copy_flag == 0) {
  739. if (data_crc != 0) {
  740. dbg_err("non-zero data CRC");
  741. goto bad;
  742. }
  743. if (data_size != 0) {
  744. dbg_err("non-zero data_size");
  745. goto bad;
  746. }
  747. } else {
  748. if (data_size == 0) {
  749. dbg_err("zero data_size of copy");
  750. goto bad;
  751. }
  752. }
  753. if (used_ebs != 0) {
  754. dbg_err("bad used_ebs");
  755. goto bad;
  756. }
  757. }
  758. return 0;
  759. bad:
  760. ubi_err("bad VID header");
  761. ubi_dbg_dump_vid_hdr(vid_hdr);
  762. ubi_dbg_dump_stack();
  763. return 1;
  764. }
  765. /**
  766. * ubi_io_read_vid_hdr - read and check a volume identifier header.
  767. * @ubi: UBI device description object
  768. * @pnum: physical eraseblock number to read from
  769. * @vid_hdr: &struct ubi_vid_hdr object where to store the read volume
  770. * identifier header
  771. * @verbose: be verbose if the header is corrupted or wasn't found
  772. *
  773. * This function reads the volume identifier header from physical eraseblock
  774. * @pnum and stores it in @vid_hdr. It also checks CRC checksum of the read
  775. * volume identifier header. The following codes may be returned:
  776. *
  777. * o %0 if the CRC checksum is correct and the header was successfully read;
  778. * o %UBI_IO_BITFLIPS if the CRC is correct, but bit-flips were detected
  779. * and corrected by the flash driver; this is harmless but may indicate that
  780. * this eraseblock may become bad soon;
  781. * o %UBI_IO_BAD_VID_HRD if the volume identifier header is corrupted (a CRC
  782. * error detected);
  783. * o %UBI_IO_PEB_FREE if the physical eraseblock is free (i.e., there is no VID
  784. * header there);
  785. * o a negative error code in case of failure.
  786. */
  787. int ubi_io_read_vid_hdr(struct ubi_device *ubi, int pnum,
  788. struct ubi_vid_hdr *vid_hdr, int verbose)
  789. {
  790. int err, read_err = 0;
  791. uint32_t crc, magic, hdr_crc;
  792. void *p;
  793. dbg_io("read VID header from PEB %d", pnum);
  794. ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
  795. p = (char *)vid_hdr - ubi->vid_hdr_shift;
  796. err = ubi_io_read(ubi, p, pnum, ubi->vid_hdr_aloffset,
  797. ubi->vid_hdr_alsize);
  798. if (err) {
  799. if (err != UBI_IO_BITFLIPS && err != -EBADMSG)
  800. return err;
  801. /*
  802. * We read all the data, but either a correctable bit-flip
  803. * occurred, or MTD reported about some data integrity error,
  804. * like an ECC error in case of NAND. The former is harmless,
  805. * the later may mean the read data is corrupted. But we have a
  806. * CRC check-sum and we will identify this. If the VID header is
  807. * still OK, we just report this as there was a bit-flip.
  808. */
  809. read_err = err;
  810. }
  811. magic = be32_to_cpu(vid_hdr->magic);
  812. if (magic != UBI_VID_HDR_MAGIC) {
  813. /*
  814. * If we have read all 0xFF bytes, the VID header probably does
  815. * not exist and the physical eraseblock is assumed to be free.
  816. *
  817. * But if there was a read error, we do not test the data for
  818. * 0xFFs. Even if it does contain all 0xFFs, this error
  819. * indicates that something is still wrong with this physical
  820. * eraseblock and it cannot be regarded as free.
  821. */
  822. if (read_err != -EBADMSG &&
  823. check_pattern(vid_hdr, 0xFF, UBI_VID_HDR_SIZE)) {
  824. /* The physical eraseblock is supposedly free */
  825. /*
  826. * The below is just a paranoid check, it has to be
  827. * compiled out if paranoid checks are disabled.
  828. */
  829. err = paranoid_check_all_ff(ubi, pnum, ubi->leb_start,
  830. ubi->leb_size);
  831. if (err)
  832. return err > 0 ? UBI_IO_BAD_VID_HDR : err;
  833. if (verbose)
  834. ubi_warn("no VID header found at PEB %d, "
  835. "only 0xFF bytes", pnum);
  836. return UBI_IO_PEB_FREE;
  837. }
  838. /*
  839. * This is not a valid VID header, and these are not 0xFF
  840. * bytes. Report that the header is corrupted.
  841. */
  842. if (verbose) {
  843. ubi_warn("bad magic number at PEB %d: %08x instead of "
  844. "%08x", pnum, magic, UBI_VID_HDR_MAGIC);
  845. ubi_dbg_dump_vid_hdr(vid_hdr);
  846. }
  847. return UBI_IO_BAD_VID_HDR;
  848. }
  849. crc = crc32(UBI_CRC32_INIT, vid_hdr, UBI_VID_HDR_SIZE_CRC);
  850. hdr_crc = be32_to_cpu(vid_hdr->hdr_crc);
  851. if (hdr_crc != crc) {
  852. if (verbose) {
  853. ubi_warn("bad CRC at PEB %d, calculated %#08x, "
  854. "read %#08x", pnum, crc, hdr_crc);
  855. ubi_dbg_dump_vid_hdr(vid_hdr);
  856. }
  857. return UBI_IO_BAD_VID_HDR;
  858. }
  859. /* Validate the VID header that we have just read */
  860. err = validate_vid_hdr(ubi, vid_hdr);
  861. if (err) {
  862. ubi_err("validation failed for PEB %d", pnum);
  863. return -EINVAL;
  864. }
  865. return read_err ? UBI_IO_BITFLIPS : 0;
  866. }
  867. /**
  868. * ubi_io_write_vid_hdr - write a volume identifier header.
  869. * @ubi: UBI device description object
  870. * @pnum: the physical eraseblock number to write to
  871. * @vid_hdr: the volume identifier header to write
  872. *
  873. * This function writes the volume identifier header described by @vid_hdr to
  874. * physical eraseblock @pnum. This function automatically fills the
  875. * @vid_hdr->magic and the @vid_hdr->version fields, as well as calculates
  876. * header CRC checksum and stores it at vid_hdr->hdr_crc.
  877. *
  878. * This function returns zero in case of success and a negative error code in
  879. * case of failure. If %-EIO is returned, the physical eraseblock probably went
  880. * bad.
  881. */
  882. int ubi_io_write_vid_hdr(struct ubi_device *ubi, int pnum,
  883. struct ubi_vid_hdr *vid_hdr)
  884. {
  885. int err;
  886. uint32_t crc;
  887. void *p;
  888. dbg_io("write VID header to PEB %d", pnum);
  889. ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
  890. err = paranoid_check_peb_ec_hdr(ubi, pnum);
  891. if (err)
  892. return err > 0 ? -EINVAL: err;
  893. vid_hdr->magic = cpu_to_be32(UBI_VID_HDR_MAGIC);
  894. vid_hdr->version = UBI_VERSION;
  895. crc = crc32(UBI_CRC32_INIT, vid_hdr, UBI_VID_HDR_SIZE_CRC);
  896. vid_hdr->hdr_crc = cpu_to_be32(crc);
  897. err = paranoid_check_vid_hdr(ubi, pnum, vid_hdr);
  898. if (err)
  899. return -EINVAL;
  900. p = (char *)vid_hdr - ubi->vid_hdr_shift;
  901. err = ubi_io_write(ubi, p, pnum, ubi->vid_hdr_aloffset,
  902. ubi->vid_hdr_alsize);
  903. return err;
  904. }
  905. #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
  906. /**
  907. * paranoid_check_not_bad - ensure that a physical eraseblock is not bad.
  908. * @ubi: UBI device description object
  909. * @pnum: physical eraseblock number to check
  910. *
  911. * This function returns zero if the physical eraseblock is good, a positive
  912. * number if it is bad and a negative error code if an error occurred.
  913. */
  914. static int paranoid_check_not_bad(const struct ubi_device *ubi, int pnum)
  915. {
  916. int err;
  917. err = ubi_io_is_bad(ubi, pnum);
  918. if (!err)
  919. return err;
  920. ubi_err("paranoid check failed for PEB %d", pnum);
  921. ubi_dbg_dump_stack();
  922. return err;
  923. }
  924. /**
  925. * paranoid_check_ec_hdr - check if an erase counter header is all right.
  926. * @ubi: UBI device description object
  927. * @pnum: physical eraseblock number the erase counter header belongs to
  928. * @ec_hdr: the erase counter header to check
  929. *
  930. * This function returns zero if the erase counter header contains valid
  931. * values, and %1 if not.
  932. */
  933. static int paranoid_check_ec_hdr(const struct ubi_device *ubi, int pnum,
  934. const struct ubi_ec_hdr *ec_hdr)
  935. {
  936. int err;
  937. uint32_t magic;
  938. magic = be32_to_cpu(ec_hdr->magic);
  939. if (magic != UBI_EC_HDR_MAGIC) {
  940. ubi_err("bad magic %#08x, must be %#08x",
  941. magic, UBI_EC_HDR_MAGIC);
  942. goto fail;
  943. }
  944. err = validate_ec_hdr(ubi, ec_hdr);
  945. if (err) {
  946. ubi_err("paranoid check failed for PEB %d", pnum);
  947. goto fail;
  948. }
  949. return 0;
  950. fail:
  951. ubi_dbg_dump_ec_hdr(ec_hdr);
  952. ubi_dbg_dump_stack();
  953. return 1;
  954. }
  955. /**
  956. * paranoid_check_peb_ec_hdr - check that the erase counter header of a
  957. * physical eraseblock is in-place and is all right.
  958. * @ubi: UBI device description object
  959. * @pnum: the physical eraseblock number to check
  960. *
  961. * This function returns zero if the erase counter header is all right, %1 if
  962. * not, and a negative error code if an error occurred.
  963. */
  964. static int paranoid_check_peb_ec_hdr(const struct ubi_device *ubi, int pnum)
  965. {
  966. int err;
  967. uint32_t crc, hdr_crc;
  968. struct ubi_ec_hdr *ec_hdr;
  969. ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_NOFS);
  970. if (!ec_hdr)
  971. return -ENOMEM;
  972. err = ubi_io_read(ubi, ec_hdr, pnum, 0, UBI_EC_HDR_SIZE);
  973. if (err && err != UBI_IO_BITFLIPS && err != -EBADMSG)
  974. goto exit;
  975. crc = crc32(UBI_CRC32_INIT, ec_hdr, UBI_EC_HDR_SIZE_CRC);
  976. hdr_crc = be32_to_cpu(ec_hdr->hdr_crc);
  977. if (hdr_crc != crc) {
  978. ubi_err("bad CRC, calculated %#08x, read %#08x", crc, hdr_crc);
  979. ubi_err("paranoid check failed for PEB %d", pnum);
  980. ubi_dbg_dump_ec_hdr(ec_hdr);
  981. ubi_dbg_dump_stack();
  982. err = 1;
  983. goto exit;
  984. }
  985. err = paranoid_check_ec_hdr(ubi, pnum, ec_hdr);
  986. exit:
  987. kfree(ec_hdr);
  988. return err;
  989. }
  990. /**
  991. * paranoid_check_vid_hdr - check that a volume identifier header is all right.
  992. * @ubi: UBI device description object
  993. * @pnum: physical eraseblock number the volume identifier header belongs to
  994. * @vid_hdr: the volume identifier header to check
  995. *
  996. * This function returns zero if the volume identifier header is all right, and
  997. * %1 if not.
  998. */
  999. static int paranoid_check_vid_hdr(const struct ubi_device *ubi, int pnum,
  1000. const struct ubi_vid_hdr *vid_hdr)
  1001. {
  1002. int err;
  1003. uint32_t magic;
  1004. magic = be32_to_cpu(vid_hdr->magic);
  1005. if (magic != UBI_VID_HDR_MAGIC) {
  1006. ubi_err("bad VID header magic %#08x at PEB %d, must be %#08x",
  1007. magic, pnum, UBI_VID_HDR_MAGIC);
  1008. goto fail;
  1009. }
  1010. err = validate_vid_hdr(ubi, vid_hdr);
  1011. if (err) {
  1012. ubi_err("paranoid check failed for PEB %d", pnum);
  1013. goto fail;
  1014. }
  1015. return err;
  1016. fail:
  1017. ubi_err("paranoid check failed for PEB %d", pnum);
  1018. ubi_dbg_dump_vid_hdr(vid_hdr);
  1019. ubi_dbg_dump_stack();
  1020. return 1;
  1021. }
  1022. /**
  1023. * paranoid_check_peb_vid_hdr - check that the volume identifier header of a
  1024. * physical eraseblock is in-place and is all right.
  1025. * @ubi: UBI device description object
  1026. * @pnum: the physical eraseblock number to check
  1027. *
  1028. * This function returns zero if the volume identifier header is all right,
  1029. * %1 if not, and a negative error code if an error occurred.
  1030. */
  1031. static int paranoid_check_peb_vid_hdr(const struct ubi_device *ubi, int pnum)
  1032. {
  1033. int err;
  1034. uint32_t crc, hdr_crc;
  1035. struct ubi_vid_hdr *vid_hdr;
  1036. void *p;
  1037. vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
  1038. if (!vid_hdr)
  1039. return -ENOMEM;
  1040. p = (char *)vid_hdr - ubi->vid_hdr_shift;
  1041. err = ubi_io_read(ubi, p, pnum, ubi->vid_hdr_aloffset,
  1042. ubi->vid_hdr_alsize);
  1043. if (err && err != UBI_IO_BITFLIPS && err != -EBADMSG)
  1044. goto exit;
  1045. crc = crc32(UBI_CRC32_INIT, vid_hdr, UBI_EC_HDR_SIZE_CRC);
  1046. hdr_crc = be32_to_cpu(vid_hdr->hdr_crc);
  1047. if (hdr_crc != crc) {
  1048. ubi_err("bad VID header CRC at PEB %d, calculated %#08x, "
  1049. "read %#08x", pnum, crc, hdr_crc);
  1050. ubi_err("paranoid check failed for PEB %d", pnum);
  1051. ubi_dbg_dump_vid_hdr(vid_hdr);
  1052. ubi_dbg_dump_stack();
  1053. err = 1;
  1054. goto exit;
  1055. }
  1056. err = paranoid_check_vid_hdr(ubi, pnum, vid_hdr);
  1057. exit:
  1058. ubi_free_vid_hdr(ubi, vid_hdr);
  1059. return err;
  1060. }
  1061. /**
  1062. * paranoid_check_all_ff - check that a region of flash is empty.
  1063. * @ubi: UBI device description object
  1064. * @pnum: the physical eraseblock number to check
  1065. * @offset: the starting offset within the physical eraseblock to check
  1066. * @len: the length of the region to check
  1067. *
  1068. * This function returns zero if only 0xFF bytes are present at offset
  1069. * @offset of the physical eraseblock @pnum, %1 if not, and a negative error
  1070. * code if an error occurred.
  1071. */
  1072. static int paranoid_check_all_ff(struct ubi_device *ubi, int pnum, int offset,
  1073. int len)
  1074. {
  1075. size_t read;
  1076. int err;
  1077. loff_t addr = (loff_t)pnum * ubi->peb_size + offset;
  1078. mutex_lock(&ubi->dbg_buf_mutex);
  1079. err = ubi->mtd->read(ubi->mtd, addr, len, &read, ubi->dbg_peb_buf);
  1080. if (err && err != -EUCLEAN) {
  1081. ubi_err("error %d while reading %d bytes from PEB %d:%d, "
  1082. "read %zd bytes", err, len, pnum, offset, read);
  1083. goto error;
  1084. }
  1085. err = check_pattern(ubi->dbg_peb_buf, 0xFF, len);
  1086. if (err == 0) {
  1087. ubi_err("flash region at PEB %d:%d, length %d does not "
  1088. "contain all 0xFF bytes", pnum, offset, len);
  1089. goto fail;
  1090. }
  1091. mutex_unlock(&ubi->dbg_buf_mutex);
  1092. return 0;
  1093. fail:
  1094. ubi_err("paranoid check failed for PEB %d", pnum);
  1095. dbg_msg("hex dump of the %d-%d region", offset, offset + len);
  1096. print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1,
  1097. ubi->dbg_peb_buf, len, 1);
  1098. err = 1;
  1099. error:
  1100. ubi_dbg_dump_stack();
  1101. mutex_unlock(&ubi->dbg_buf_mutex);
  1102. return err;
  1103. }
  1104. #endif /* CONFIG_MTD_UBI_DEBUG_PARANOID */