io.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402
  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 sub-system.
  23. *
  24. * This sub-system provides a uniform way to work with all kinds of the
  25. * underlying MTD devices. It also implements handy functions for reading and
  26. * writing UBI 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
  30. * sub-system 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 sub-system does the following trick in order to avoid this extra
  82. * copy. It always allocates a @ubi->vid_hdr_alsize bytes buffer for the VID
  83. * header and returns a pointer to offset @ubi->vid_hdr_shift of this buffer.
  84. * When the VID header is being written out, it shifts the VID header pointer
  85. * back and writes the whole sub-page.
  86. */
  87. #include <linux/crc32.h>
  88. #include <linux/err.h>
  89. #include <linux/slab.h>
  90. #include "ubi.h"
  91. #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
  92. static int paranoid_check_not_bad(const struct ubi_device *ubi, int pnum);
  93. static int paranoid_check_peb_ec_hdr(const struct ubi_device *ubi, int pnum);
  94. static int paranoid_check_ec_hdr(const struct ubi_device *ubi, int pnum,
  95. const struct ubi_ec_hdr *ec_hdr);
  96. static int paranoid_check_peb_vid_hdr(const struct ubi_device *ubi, int pnum);
  97. static int paranoid_check_vid_hdr(const struct ubi_device *ubi, int pnum,
  98. const struct ubi_vid_hdr *vid_hdr);
  99. #else
  100. #define paranoid_check_not_bad(ubi, pnum) 0
  101. #define paranoid_check_peb_ec_hdr(ubi, pnum) 0
  102. #define paranoid_check_ec_hdr(ubi, pnum, ec_hdr) 0
  103. #define paranoid_check_peb_vid_hdr(ubi, pnum) 0
  104. #define paranoid_check_vid_hdr(ubi, pnum, vid_hdr) 0
  105. #endif
  106. /**
  107. * ubi_io_read - read data from a physical eraseblock.
  108. * @ubi: UBI device description object
  109. * @buf: buffer where to store the read data
  110. * @pnum: physical eraseblock number to read from
  111. * @offset: offset within the physical eraseblock from where to read
  112. * @len: how many bytes to read
  113. *
  114. * This function reads data from offset @offset of physical eraseblock @pnum
  115. * and stores the read data in the @buf buffer. The following return codes are
  116. * possible:
  117. *
  118. * o %0 if all the requested data were successfully read;
  119. * o %UBI_IO_BITFLIPS if all the requested data were successfully read, but
  120. * correctable bit-flips were detected; this is harmless but may indicate
  121. * that this eraseblock may become bad soon (but do not have to);
  122. * o %-EBADMSG if the MTD subsystem reported about data integrity problems, for
  123. * example it can be an ECC error in case of NAND; this most probably means
  124. * that the data is corrupted;
  125. * o %-EIO if some I/O error occurred;
  126. * o other negative error codes in case of other errors.
  127. */
  128. int ubi_io_read(const struct ubi_device *ubi, void *buf, int pnum, int offset,
  129. int len)
  130. {
  131. int err, retries = 0;
  132. size_t read;
  133. loff_t addr;
  134. dbg_io("read %d bytes from PEB %d:%d", len, pnum, offset);
  135. ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
  136. ubi_assert(offset >= 0 && offset + len <= ubi->peb_size);
  137. ubi_assert(len > 0);
  138. err = paranoid_check_not_bad(ubi, pnum);
  139. if (err)
  140. return err;
  141. addr = (loff_t)pnum * ubi->peb_size + offset;
  142. retry:
  143. err = ubi->mtd->read(ubi->mtd, addr, len, &read, buf);
  144. if (err) {
  145. if (err == -EUCLEAN) {
  146. /*
  147. * -EUCLEAN is reported if there was a bit-flip which
  148. * was corrected, so this is harmless.
  149. *
  150. * We do not report about it here unless debugging is
  151. * enabled. A corresponding message will be printed
  152. * later, when it is has been scrubbed.
  153. */
  154. dbg_msg("fixable bit-flip detected at PEB %d", pnum);
  155. ubi_assert(len == read);
  156. return UBI_IO_BITFLIPS;
  157. }
  158. if (read != len && retries++ < UBI_IO_RETRIES) {
  159. dbg_io("error %d while reading %d bytes from PEB %d:%d,"
  160. " read only %zd bytes, retry",
  161. err, len, pnum, offset, read);
  162. yield();
  163. goto retry;
  164. }
  165. ubi_err("error %d while reading %d bytes from PEB %d:%d, "
  166. "read %zd bytes", err, len, pnum, offset, read);
  167. ubi_dbg_dump_stack();
  168. /*
  169. * The driver should never return -EBADMSG if it failed to read
  170. * all the requested data. But some buggy drivers might do
  171. * this, so we change it to -EIO.
  172. */
  173. if (read != len && err == -EBADMSG) {
  174. ubi_assert(0);
  175. err = -EIO;
  176. }
  177. } else {
  178. ubi_assert(len == read);
  179. if (ubi_dbg_is_bitflip()) {
  180. dbg_gen("bit-flip (emulated)");
  181. err = UBI_IO_BITFLIPS;
  182. }
  183. }
  184. return err;
  185. }
  186. /**
  187. * ubi_io_write - write data to a physical eraseblock.
  188. * @ubi: UBI device description object
  189. * @buf: buffer with the data to write
  190. * @pnum: physical eraseblock number to write to
  191. * @offset: offset within the physical eraseblock where to write
  192. * @len: how many bytes to write
  193. *
  194. * This function writes @len bytes of data from buffer @buf to offset @offset
  195. * of physical eraseblock @pnum. If all the data were successfully written,
  196. * zero is returned. If an error occurred, this function returns a negative
  197. * error code. If %-EIO is returned, the physical eraseblock most probably went
  198. * bad.
  199. *
  200. * Note, in case of an error, it is possible that something was still written
  201. * to the flash media, but may be some garbage.
  202. */
  203. int ubi_io_write(struct ubi_device *ubi, const void *buf, int pnum, int offset,
  204. int len)
  205. {
  206. int err;
  207. size_t written;
  208. loff_t addr;
  209. dbg_io("write %d bytes to PEB %d:%d", len, pnum, offset);
  210. ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
  211. ubi_assert(offset >= 0 && offset + len <= ubi->peb_size);
  212. ubi_assert(offset % ubi->hdrs_min_io_size == 0);
  213. ubi_assert(len > 0 && len % ubi->hdrs_min_io_size == 0);
  214. if (ubi->ro_mode) {
  215. ubi_err("read-only mode");
  216. return -EROFS;
  217. }
  218. /* The below has to be compiled out if paranoid checks are disabled */
  219. err = paranoid_check_not_bad(ubi, pnum);
  220. if (err)
  221. return err;
  222. /* The area we are writing to has to contain all 0xFF bytes */
  223. err = ubi_dbg_check_all_ff(ubi, pnum, offset, len);
  224. if (err)
  225. return err;
  226. if (offset >= ubi->leb_start) {
  227. /*
  228. * We write to the data area of the physical eraseblock. Make
  229. * sure it has valid EC and VID headers.
  230. */
  231. err = paranoid_check_peb_ec_hdr(ubi, pnum);
  232. if (err)
  233. return err;
  234. err = paranoid_check_peb_vid_hdr(ubi, pnum);
  235. if (err)
  236. return err;
  237. }
  238. if (ubi_dbg_is_write_failure()) {
  239. dbg_err("cannot write %d bytes to PEB %d:%d "
  240. "(emulated)", len, pnum, offset);
  241. ubi_dbg_dump_stack();
  242. return -EIO;
  243. }
  244. addr = (loff_t)pnum * ubi->peb_size + offset;
  245. err = ubi->mtd->write(ubi->mtd, addr, len, &written, buf);
  246. if (err) {
  247. ubi_err("error %d while writing %d bytes to PEB %d:%d, written "
  248. "%zd bytes", err, len, pnum, offset, written);
  249. ubi_dbg_dump_stack();
  250. ubi_dbg_dump_flash(ubi, pnum, offset, len);
  251. } else
  252. ubi_assert(written == len);
  253. if (!err) {
  254. err = ubi_dbg_check_write(ubi, buf, pnum, offset, len);
  255. if (err)
  256. return err;
  257. /*
  258. * Since we always write sequentially, the rest of the PEB has
  259. * to contain only 0xFF bytes.
  260. */
  261. offset += len;
  262. len = ubi->peb_size - offset;
  263. if (len)
  264. err = ubi_dbg_check_all_ff(ubi, pnum, offset, len);
  265. }
  266. return err;
  267. }
  268. /**
  269. * erase_callback - MTD erasure call-back.
  270. * @ei: MTD erase information object.
  271. *
  272. * Note, even though MTD erase interface is asynchronous, all the current
  273. * implementations are synchronous anyway.
  274. */
  275. static void erase_callback(struct erase_info *ei)
  276. {
  277. wake_up_interruptible((wait_queue_head_t *)ei->priv);
  278. }
  279. /**
  280. * do_sync_erase - synchronously erase a physical eraseblock.
  281. * @ubi: UBI device description object
  282. * @pnum: the physical eraseblock number to erase
  283. *
  284. * This function synchronously erases physical eraseblock @pnum and returns
  285. * zero in case of success and a negative error code in case of failure. If
  286. * %-EIO is returned, the physical eraseblock most probably went bad.
  287. */
  288. static int do_sync_erase(struct ubi_device *ubi, int pnum)
  289. {
  290. int err, retries = 0;
  291. struct erase_info ei;
  292. wait_queue_head_t wq;
  293. dbg_io("erase PEB %d", pnum);
  294. retry:
  295. init_waitqueue_head(&wq);
  296. memset(&ei, 0, sizeof(struct erase_info));
  297. ei.mtd = ubi->mtd;
  298. ei.addr = (loff_t)pnum * ubi->peb_size;
  299. ei.len = ubi->peb_size;
  300. ei.callback = erase_callback;
  301. ei.priv = (unsigned long)&wq;
  302. err = ubi->mtd->erase(ubi->mtd, &ei);
  303. if (err) {
  304. if (retries++ < UBI_IO_RETRIES) {
  305. dbg_io("error %d while erasing PEB %d, retry",
  306. err, pnum);
  307. yield();
  308. goto retry;
  309. }
  310. ubi_err("cannot erase PEB %d, error %d", pnum, err);
  311. ubi_dbg_dump_stack();
  312. return err;
  313. }
  314. err = wait_event_interruptible(wq, ei.state == MTD_ERASE_DONE ||
  315. ei.state == MTD_ERASE_FAILED);
  316. if (err) {
  317. ubi_err("interrupted PEB %d erasure", pnum);
  318. return -EINTR;
  319. }
  320. if (ei.state == MTD_ERASE_FAILED) {
  321. if (retries++ < UBI_IO_RETRIES) {
  322. dbg_io("error while erasing PEB %d, retry", pnum);
  323. yield();
  324. goto retry;
  325. }
  326. ubi_err("cannot erase PEB %d", pnum);
  327. ubi_dbg_dump_stack();
  328. return -EIO;
  329. }
  330. err = ubi_dbg_check_all_ff(ubi, pnum, 0, ubi->peb_size);
  331. if (err)
  332. return err;
  333. if (ubi_dbg_is_erase_failure() && !err) {
  334. dbg_err("cannot erase PEB %d (emulated)", pnum);
  335. return -EIO;
  336. }
  337. return 0;
  338. }
  339. /**
  340. * check_pattern - check if buffer contains only a certain byte pattern.
  341. * @buf: buffer to check
  342. * @patt: the pattern to check
  343. * @size: buffer size in bytes
  344. *
  345. * This function returns %1 in there are only @patt bytes in @buf, and %0 if
  346. * something else was also found.
  347. */
  348. static int check_pattern(const void *buf, uint8_t patt, int size)
  349. {
  350. int i;
  351. for (i = 0; i < size; i++)
  352. if (((const uint8_t *)buf)[i] != patt)
  353. return 0;
  354. return 1;
  355. }
  356. /* Patterns to write to a physical eraseblock when torturing it */
  357. static uint8_t patterns[] = {0xa5, 0x5a, 0x0};
  358. /**
  359. * torture_peb - test a supposedly bad physical eraseblock.
  360. * @ubi: UBI device description object
  361. * @pnum: the physical eraseblock number to test
  362. *
  363. * This function returns %-EIO if the physical eraseblock did not pass the
  364. * test, a positive number of erase operations done if the test was
  365. * successfully passed, and other negative error codes in case of other errors.
  366. */
  367. static int torture_peb(struct ubi_device *ubi, int pnum)
  368. {
  369. int err, i, patt_count;
  370. ubi_msg("run torture test for PEB %d", pnum);
  371. patt_count = ARRAY_SIZE(patterns);
  372. ubi_assert(patt_count > 0);
  373. mutex_lock(&ubi->buf_mutex);
  374. for (i = 0; i < patt_count; i++) {
  375. err = do_sync_erase(ubi, pnum);
  376. if (err)
  377. goto out;
  378. /* Make sure the PEB contains only 0xFF bytes */
  379. err = ubi_io_read(ubi, ubi->peb_buf1, pnum, 0, ubi->peb_size);
  380. if (err)
  381. goto out;
  382. err = check_pattern(ubi->peb_buf1, 0xFF, ubi->peb_size);
  383. if (err == 0) {
  384. ubi_err("erased PEB %d, but a non-0xFF byte found",
  385. pnum);
  386. err = -EIO;
  387. goto out;
  388. }
  389. /* Write a pattern and check it */
  390. memset(ubi->peb_buf1, patterns[i], ubi->peb_size);
  391. err = ubi_io_write(ubi, ubi->peb_buf1, pnum, 0, ubi->peb_size);
  392. if (err)
  393. goto out;
  394. memset(ubi->peb_buf1, ~patterns[i], ubi->peb_size);
  395. err = ubi_io_read(ubi, ubi->peb_buf1, pnum, 0, ubi->peb_size);
  396. if (err)
  397. goto out;
  398. err = check_pattern(ubi->peb_buf1, patterns[i], ubi->peb_size);
  399. if (err == 0) {
  400. ubi_err("pattern %x checking failed for PEB %d",
  401. patterns[i], pnum);
  402. err = -EIO;
  403. goto out;
  404. }
  405. }
  406. err = patt_count;
  407. ubi_msg("PEB %d passed torture test, do not mark it a bad", pnum);
  408. out:
  409. mutex_unlock(&ubi->buf_mutex);
  410. if (err == UBI_IO_BITFLIPS || err == -EBADMSG) {
  411. /*
  412. * If a bit-flip or data integrity error was detected, the test
  413. * has not passed because it happened on a freshly erased
  414. * physical eraseblock which means something is wrong with it.
  415. */
  416. ubi_err("read problems on freshly erased PEB %d, must be bad",
  417. pnum);
  418. err = -EIO;
  419. }
  420. return err;
  421. }
  422. /**
  423. * nor_erase_prepare - prepare a NOR flash PEB for erasure.
  424. * @ubi: UBI device description object
  425. * @pnum: physical eraseblock number to prepare
  426. *
  427. * NOR flash, or at least some of them, have peculiar embedded PEB erasure
  428. * algorithm: the PEB is first filled with zeroes, then it is erased. And
  429. * filling with zeroes starts from the end of the PEB. This was observed with
  430. * Spansion S29GL512N NOR flash.
  431. *
  432. * This means that in case of a power cut we may end up with intact data at the
  433. * beginning of the PEB, and all zeroes at the end of PEB. In other words, the
  434. * EC and VID headers are OK, but a large chunk of data at the end of PEB is
  435. * zeroed. This makes UBI mistakenly treat this PEB as used and associate it
  436. * with an LEB, which leads to subsequent failures (e.g., UBIFS fails).
  437. *
  438. * This function is called before erasing NOR PEBs and it zeroes out EC and VID
  439. * magic numbers in order to invalidate them and prevent the failures. Returns
  440. * zero in case of success and a negative error code in case of failure.
  441. */
  442. static int nor_erase_prepare(struct ubi_device *ubi, int pnum)
  443. {
  444. int err, err1;
  445. size_t written;
  446. loff_t addr;
  447. uint32_t data = 0;
  448. struct ubi_vid_hdr vid_hdr;
  449. addr = (loff_t)pnum * ubi->peb_size + ubi->vid_hdr_aloffset;
  450. err = ubi->mtd->write(ubi->mtd, addr, 4, &written, (void *)&data);
  451. if (!err) {
  452. addr -= ubi->vid_hdr_aloffset;
  453. err = ubi->mtd->write(ubi->mtd, addr, 4, &written,
  454. (void *)&data);
  455. if (!err)
  456. return 0;
  457. }
  458. /*
  459. * We failed to write to the media. This was observed with Spansion
  460. * S29GL512N NOR flash. Most probably the eraseblock erasure was
  461. * interrupted at a very inappropriate moment, so it became unwritable.
  462. * In this case we probably anyway have garbage in this PEB.
  463. */
  464. err1 = ubi_io_read_vid_hdr(ubi, pnum, &vid_hdr, 0);
  465. if (err1 == UBI_IO_BAD_VID_HDR)
  466. /*
  467. * The VID header is corrupted, so we can safely erase this
  468. * PEB and not afraid that it will be treated as a valid PEB in
  469. * case of an unclean reboot.
  470. */
  471. return 0;
  472. /*
  473. * The PEB contains a valid VID header, but we cannot invalidate it.
  474. * Supposedly the flash media or the driver is screwed up, so return an
  475. * error.
  476. */
  477. ubi_err("cannot invalidate PEB %d, write returned %d read returned %d",
  478. pnum, err, err1);
  479. ubi_dbg_dump_flash(ubi, pnum, 0, ubi->peb_size);
  480. return -EIO;
  481. }
  482. /**
  483. * ubi_io_sync_erase - synchronously erase a physical eraseblock.
  484. * @ubi: UBI device description object
  485. * @pnum: physical eraseblock number to erase
  486. * @torture: if this physical eraseblock has to be tortured
  487. *
  488. * This function synchronously erases physical eraseblock @pnum. If @torture
  489. * flag is not zero, the physical eraseblock is checked by means of writing
  490. * different patterns to it and reading them back. If the torturing is enabled,
  491. * the physical eraseblock is erased more than once.
  492. *
  493. * This function returns the number of erasures made in case of success, %-EIO
  494. * if the erasure failed or the torturing test failed, and other negative error
  495. * codes in case of other errors. Note, %-EIO means that the physical
  496. * eraseblock is bad.
  497. */
  498. int ubi_io_sync_erase(struct ubi_device *ubi, int pnum, int torture)
  499. {
  500. int err, ret = 0;
  501. ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
  502. err = paranoid_check_not_bad(ubi, pnum);
  503. if (err != 0)
  504. return err;
  505. if (ubi->ro_mode) {
  506. ubi_err("read-only mode");
  507. return -EROFS;
  508. }
  509. if (ubi->nor_flash) {
  510. err = nor_erase_prepare(ubi, pnum);
  511. if (err)
  512. return err;
  513. }
  514. if (torture) {
  515. ret = torture_peb(ubi, pnum);
  516. if (ret < 0)
  517. return ret;
  518. }
  519. err = do_sync_erase(ubi, pnum);
  520. if (err)
  521. return err;
  522. return ret + 1;
  523. }
  524. /**
  525. * ubi_io_is_bad - check if a physical eraseblock is bad.
  526. * @ubi: UBI device description object
  527. * @pnum: the physical eraseblock number to check
  528. *
  529. * This function returns a positive number if the physical eraseblock is bad,
  530. * zero if not, and a negative error code if an error occurred.
  531. */
  532. int ubi_io_is_bad(const struct ubi_device *ubi, int pnum)
  533. {
  534. struct mtd_info *mtd = ubi->mtd;
  535. ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
  536. if (ubi->bad_allowed) {
  537. int ret;
  538. ret = mtd->block_isbad(mtd, (loff_t)pnum * ubi->peb_size);
  539. if (ret < 0)
  540. ubi_err("error %d while checking if PEB %d is bad",
  541. ret, pnum);
  542. else if (ret)
  543. dbg_io("PEB %d is bad", pnum);
  544. return ret;
  545. }
  546. return 0;
  547. }
  548. /**
  549. * ubi_io_mark_bad - mark a physical eraseblock as bad.
  550. * @ubi: UBI device description object
  551. * @pnum: the physical eraseblock number to mark
  552. *
  553. * This function returns zero in case of success and a negative error code in
  554. * case of failure.
  555. */
  556. int ubi_io_mark_bad(const struct ubi_device *ubi, int pnum)
  557. {
  558. int err;
  559. struct mtd_info *mtd = ubi->mtd;
  560. ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
  561. if (ubi->ro_mode) {
  562. ubi_err("read-only mode");
  563. return -EROFS;
  564. }
  565. if (!ubi->bad_allowed)
  566. return 0;
  567. err = mtd->block_markbad(mtd, (loff_t)pnum * ubi->peb_size);
  568. if (err)
  569. ubi_err("cannot mark PEB %d bad, error %d", pnum, err);
  570. return err;
  571. }
  572. /**
  573. * validate_ec_hdr - validate an erase counter header.
  574. * @ubi: UBI device description object
  575. * @ec_hdr: the erase counter header to check
  576. *
  577. * This function returns zero if the erase counter header is OK, and %1 if
  578. * not.
  579. */
  580. static int validate_ec_hdr(const struct ubi_device *ubi,
  581. const struct ubi_ec_hdr *ec_hdr)
  582. {
  583. long long ec;
  584. int vid_hdr_offset, leb_start;
  585. ec = be64_to_cpu(ec_hdr->ec);
  586. vid_hdr_offset = be32_to_cpu(ec_hdr->vid_hdr_offset);
  587. leb_start = be32_to_cpu(ec_hdr->data_offset);
  588. if (ec_hdr->version != UBI_VERSION) {
  589. ubi_err("node with incompatible UBI version found: "
  590. "this UBI version is %d, image version is %d",
  591. UBI_VERSION, (int)ec_hdr->version);
  592. goto bad;
  593. }
  594. if (vid_hdr_offset != ubi->vid_hdr_offset) {
  595. ubi_err("bad VID header offset %d, expected %d",
  596. vid_hdr_offset, ubi->vid_hdr_offset);
  597. goto bad;
  598. }
  599. if (leb_start != ubi->leb_start) {
  600. ubi_err("bad data offset %d, expected %d",
  601. leb_start, ubi->leb_start);
  602. goto bad;
  603. }
  604. if (ec < 0 || ec > UBI_MAX_ERASECOUNTER) {
  605. ubi_err("bad erase counter %lld", ec);
  606. goto bad;
  607. }
  608. return 0;
  609. bad:
  610. ubi_err("bad EC header");
  611. ubi_dbg_dump_ec_hdr(ec_hdr);
  612. ubi_dbg_dump_stack();
  613. return 1;
  614. }
  615. /**
  616. * ubi_io_read_ec_hdr - read and check an erase counter header.
  617. * @ubi: UBI device description object
  618. * @pnum: physical eraseblock to read from
  619. * @ec_hdr: a &struct ubi_ec_hdr object where to store the read erase counter
  620. * header
  621. * @verbose: be verbose if the header is corrupted or was not found
  622. *
  623. * This function reads erase counter header from physical eraseblock @pnum and
  624. * stores it in @ec_hdr. This function also checks CRC checksum of the read
  625. * erase counter header. The following codes may be returned:
  626. *
  627. * o %0 if the CRC checksum is correct and the header was successfully read;
  628. * o %UBI_IO_BITFLIPS if the CRC is correct, but bit-flips were detected
  629. * and corrected by the flash driver; this is harmless but may indicate that
  630. * this eraseblock may become bad soon (but may be not);
  631. * o %UBI_IO_BAD_EC_HDR if the erase counter header is corrupted (a CRC error);
  632. * o %UBI_IO_PEB_EMPTY if the physical eraseblock is empty;
  633. * o a negative error code in case of failure.
  634. */
  635. int ubi_io_read_ec_hdr(struct ubi_device *ubi, int pnum,
  636. struct ubi_ec_hdr *ec_hdr, int verbose)
  637. {
  638. int err, read_err = 0;
  639. uint32_t crc, magic, hdr_crc;
  640. dbg_io("read EC header from PEB %d", pnum);
  641. ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
  642. err = ubi_io_read(ubi, ec_hdr, pnum, 0, UBI_EC_HDR_SIZE);
  643. if (err) {
  644. if (err != UBI_IO_BITFLIPS && err != -EBADMSG)
  645. return err;
  646. /*
  647. * We read all the data, but either a correctable bit-flip
  648. * occurred, or MTD reported about some data integrity error,
  649. * like an ECC error in case of NAND. The former is harmless,
  650. * the later may mean that the read data is corrupted. But we
  651. * have a CRC check-sum and we will detect this. If the EC
  652. * header is still OK, we just report this as there was a
  653. * bit-flip.
  654. */
  655. read_err = err;
  656. }
  657. magic = be32_to_cpu(ec_hdr->magic);
  658. if (magic != UBI_EC_HDR_MAGIC) {
  659. /*
  660. * The magic field is wrong. Let's check if we have read all
  661. * 0xFF. If yes, this physical eraseblock is assumed to be
  662. * empty.
  663. *
  664. * But if there was a read error, we do not test it for all
  665. * 0xFFs. Even if it does contain all 0xFFs, this error
  666. * indicates that something is still wrong with this physical
  667. * eraseblock and we anyway cannot treat it as empty.
  668. */
  669. if (read_err != -EBADMSG &&
  670. check_pattern(ec_hdr, 0xFF, UBI_EC_HDR_SIZE)) {
  671. /* The physical eraseblock is supposedly empty */
  672. if (verbose)
  673. ubi_warn("no EC header found at PEB %d, "
  674. "only 0xFF bytes", pnum);
  675. else if (UBI_IO_DEBUG)
  676. dbg_msg("no EC header found at PEB %d, "
  677. "only 0xFF bytes", pnum);
  678. return UBI_IO_PEB_EMPTY;
  679. }
  680. /*
  681. * This is not a valid erase counter header, and these are not
  682. * 0xFF bytes. Report that the header is corrupted.
  683. */
  684. if (verbose) {
  685. ubi_warn("bad magic number at PEB %d: %08x instead of "
  686. "%08x", pnum, magic, UBI_EC_HDR_MAGIC);
  687. ubi_dbg_dump_ec_hdr(ec_hdr);
  688. } else if (UBI_IO_DEBUG)
  689. dbg_msg("bad magic number at PEB %d: %08x instead of "
  690. "%08x", pnum, magic, UBI_EC_HDR_MAGIC);
  691. return UBI_IO_BAD_EC_HDR;
  692. }
  693. crc = crc32(UBI_CRC32_INIT, ec_hdr, UBI_EC_HDR_SIZE_CRC);
  694. hdr_crc = be32_to_cpu(ec_hdr->hdr_crc);
  695. if (hdr_crc != crc) {
  696. if (verbose) {
  697. ubi_warn("bad EC header CRC at PEB %d, calculated "
  698. "%#08x, read %#08x", pnum, crc, hdr_crc);
  699. ubi_dbg_dump_ec_hdr(ec_hdr);
  700. } else if (UBI_IO_DEBUG)
  701. dbg_msg("bad EC header CRC at PEB %d, calculated "
  702. "%#08x, read %#08x", pnum, crc, hdr_crc);
  703. return UBI_IO_BAD_EC_HDR;
  704. }
  705. /* And of course validate what has just been read from the media */
  706. err = validate_ec_hdr(ubi, ec_hdr);
  707. if (err) {
  708. ubi_err("validation failed for PEB %d", pnum);
  709. return -EINVAL;
  710. }
  711. return read_err ? UBI_IO_BITFLIPS : 0;
  712. }
  713. /**
  714. * ubi_io_write_ec_hdr - write an erase counter header.
  715. * @ubi: UBI device description object
  716. * @pnum: physical eraseblock to write to
  717. * @ec_hdr: the erase counter header to write
  718. *
  719. * This function writes erase counter header described by @ec_hdr to physical
  720. * eraseblock @pnum. It also fills most fields of @ec_hdr before writing, so
  721. * the caller do not have to fill them. Callers must only fill the @ec_hdr->ec
  722. * field.
  723. *
  724. * This function returns zero in case of success and a negative error code in
  725. * case of failure. If %-EIO is returned, the physical eraseblock most probably
  726. * went bad.
  727. */
  728. int ubi_io_write_ec_hdr(struct ubi_device *ubi, int pnum,
  729. struct ubi_ec_hdr *ec_hdr)
  730. {
  731. int err;
  732. uint32_t crc;
  733. dbg_io("write EC header to PEB %d", pnum);
  734. ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
  735. ec_hdr->magic = cpu_to_be32(UBI_EC_HDR_MAGIC);
  736. ec_hdr->version = UBI_VERSION;
  737. ec_hdr->vid_hdr_offset = cpu_to_be32(ubi->vid_hdr_offset);
  738. ec_hdr->data_offset = cpu_to_be32(ubi->leb_start);
  739. ec_hdr->image_seq = cpu_to_be32(ubi->image_seq);
  740. crc = crc32(UBI_CRC32_INIT, ec_hdr, UBI_EC_HDR_SIZE_CRC);
  741. ec_hdr->hdr_crc = cpu_to_be32(crc);
  742. err = paranoid_check_ec_hdr(ubi, pnum, ec_hdr);
  743. if (err)
  744. return err;
  745. err = ubi_io_write(ubi, ec_hdr, pnum, 0, ubi->ec_hdr_alsize);
  746. return err;
  747. }
  748. /**
  749. * validate_vid_hdr - validate a volume identifier header.
  750. * @ubi: UBI device description object
  751. * @vid_hdr: the volume identifier header to check
  752. *
  753. * This function checks that data stored in the volume identifier header
  754. * @vid_hdr. Returns zero if the VID header is OK and %1 if not.
  755. */
  756. static int validate_vid_hdr(const struct ubi_device *ubi,
  757. const struct ubi_vid_hdr *vid_hdr)
  758. {
  759. int vol_type = vid_hdr->vol_type;
  760. int copy_flag = vid_hdr->copy_flag;
  761. int vol_id = be32_to_cpu(vid_hdr->vol_id);
  762. int lnum = be32_to_cpu(vid_hdr->lnum);
  763. int compat = vid_hdr->compat;
  764. int data_size = be32_to_cpu(vid_hdr->data_size);
  765. int used_ebs = be32_to_cpu(vid_hdr->used_ebs);
  766. int data_pad = be32_to_cpu(vid_hdr->data_pad);
  767. int data_crc = be32_to_cpu(vid_hdr->data_crc);
  768. int usable_leb_size = ubi->leb_size - data_pad;
  769. if (copy_flag != 0 && copy_flag != 1) {
  770. dbg_err("bad copy_flag");
  771. goto bad;
  772. }
  773. if (vol_id < 0 || lnum < 0 || data_size < 0 || used_ebs < 0 ||
  774. data_pad < 0) {
  775. dbg_err("negative values");
  776. goto bad;
  777. }
  778. if (vol_id >= UBI_MAX_VOLUMES && vol_id < UBI_INTERNAL_VOL_START) {
  779. dbg_err("bad vol_id");
  780. goto bad;
  781. }
  782. if (vol_id < UBI_INTERNAL_VOL_START && compat != 0) {
  783. dbg_err("bad compat");
  784. goto bad;
  785. }
  786. if (vol_id >= UBI_INTERNAL_VOL_START && compat != UBI_COMPAT_DELETE &&
  787. compat != UBI_COMPAT_RO && compat != UBI_COMPAT_PRESERVE &&
  788. compat != UBI_COMPAT_REJECT) {
  789. dbg_err("bad compat");
  790. goto bad;
  791. }
  792. if (vol_type != UBI_VID_DYNAMIC && vol_type != UBI_VID_STATIC) {
  793. dbg_err("bad vol_type");
  794. goto bad;
  795. }
  796. if (data_pad >= ubi->leb_size / 2) {
  797. dbg_err("bad data_pad");
  798. goto bad;
  799. }
  800. if (vol_type == UBI_VID_STATIC) {
  801. /*
  802. * Although from high-level point of view static volumes may
  803. * contain zero bytes of data, but no VID headers can contain
  804. * zero at these fields, because they empty volumes do not have
  805. * mapped logical eraseblocks.
  806. */
  807. if (used_ebs == 0) {
  808. dbg_err("zero used_ebs");
  809. goto bad;
  810. }
  811. if (data_size == 0) {
  812. dbg_err("zero data_size");
  813. goto bad;
  814. }
  815. if (lnum < used_ebs - 1) {
  816. if (data_size != usable_leb_size) {
  817. dbg_err("bad data_size");
  818. goto bad;
  819. }
  820. } else if (lnum == used_ebs - 1) {
  821. if (data_size == 0) {
  822. dbg_err("bad data_size at last LEB");
  823. goto bad;
  824. }
  825. } else {
  826. dbg_err("too high lnum");
  827. goto bad;
  828. }
  829. } else {
  830. if (copy_flag == 0) {
  831. if (data_crc != 0) {
  832. dbg_err("non-zero data CRC");
  833. goto bad;
  834. }
  835. if (data_size != 0) {
  836. dbg_err("non-zero data_size");
  837. goto bad;
  838. }
  839. } else {
  840. if (data_size == 0) {
  841. dbg_err("zero data_size of copy");
  842. goto bad;
  843. }
  844. }
  845. if (used_ebs != 0) {
  846. dbg_err("bad used_ebs");
  847. goto bad;
  848. }
  849. }
  850. return 0;
  851. bad:
  852. ubi_err("bad VID header");
  853. ubi_dbg_dump_vid_hdr(vid_hdr);
  854. ubi_dbg_dump_stack();
  855. return 1;
  856. }
  857. /**
  858. * ubi_io_read_vid_hdr - read and check a volume identifier header.
  859. * @ubi: UBI device description object
  860. * @pnum: physical eraseblock number to read from
  861. * @vid_hdr: &struct ubi_vid_hdr object where to store the read volume
  862. * identifier header
  863. * @verbose: be verbose if the header is corrupted or wasn't found
  864. *
  865. * This function reads the volume identifier header from physical eraseblock
  866. * @pnum and stores it in @vid_hdr. It also checks CRC checksum of the read
  867. * volume identifier header. The following codes may be returned:
  868. *
  869. * o %0 if the CRC checksum is correct and the header was successfully read;
  870. * o %UBI_IO_BITFLIPS if the CRC is correct, but bit-flips were detected
  871. * and corrected by the flash driver; this is harmless but may indicate that
  872. * this eraseblock may become bad soon;
  873. * o %UBI_IO_BAD_VID_HDR if the volume identifier header is corrupted (a CRC
  874. * error detected);
  875. * o %UBI_IO_PEB_FREE if the physical eraseblock is free (i.e., there is no VID
  876. * header there);
  877. * o a negative error code in case of failure.
  878. */
  879. int ubi_io_read_vid_hdr(struct ubi_device *ubi, int pnum,
  880. struct ubi_vid_hdr *vid_hdr, int verbose)
  881. {
  882. int err, read_err = 0;
  883. uint32_t crc, magic, hdr_crc;
  884. void *p;
  885. dbg_io("read VID header from PEB %d", pnum);
  886. ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
  887. p = (char *)vid_hdr - ubi->vid_hdr_shift;
  888. err = ubi_io_read(ubi, p, pnum, ubi->vid_hdr_aloffset,
  889. ubi->vid_hdr_alsize);
  890. if (err) {
  891. if (err != UBI_IO_BITFLIPS && err != -EBADMSG)
  892. return err;
  893. /*
  894. * We read all the data, but either a correctable bit-flip
  895. * occurred, or MTD reported about some data integrity error,
  896. * like an ECC error in case of NAND. The former is harmless,
  897. * the later may mean the read data is corrupted. But we have a
  898. * CRC check-sum and we will identify this. If the VID header is
  899. * still OK, we just report this as there was a bit-flip.
  900. */
  901. read_err = err;
  902. }
  903. magic = be32_to_cpu(vid_hdr->magic);
  904. if (magic != UBI_VID_HDR_MAGIC) {
  905. /*
  906. * If we have read all 0xFF bytes, the VID header probably does
  907. * not exist and the physical eraseblock is assumed to be free.
  908. *
  909. * But if there was a read error, we do not test the data for
  910. * 0xFFs. Even if it does contain all 0xFFs, this error
  911. * indicates that something is still wrong with this physical
  912. * eraseblock and it cannot be regarded as free.
  913. */
  914. if (read_err != -EBADMSG &&
  915. check_pattern(vid_hdr, 0xFF, UBI_VID_HDR_SIZE)) {
  916. /* The physical eraseblock is supposedly free */
  917. if (verbose)
  918. ubi_warn("no VID header found at PEB %d, "
  919. "only 0xFF bytes", pnum);
  920. else if (UBI_IO_DEBUG)
  921. dbg_msg("no VID header found at PEB %d, "
  922. "only 0xFF bytes", pnum);
  923. return UBI_IO_PEB_FREE;
  924. }
  925. /*
  926. * This is not a valid VID header, and these are not 0xFF
  927. * bytes. Report that the header is corrupted.
  928. */
  929. if (verbose) {
  930. ubi_warn("bad magic number at PEB %d: %08x instead of "
  931. "%08x", pnum, magic, UBI_VID_HDR_MAGIC);
  932. ubi_dbg_dump_vid_hdr(vid_hdr);
  933. } else if (UBI_IO_DEBUG)
  934. dbg_msg("bad magic number at PEB %d: %08x instead of "
  935. "%08x", pnum, magic, UBI_VID_HDR_MAGIC);
  936. return UBI_IO_BAD_VID_HDR;
  937. }
  938. crc = crc32(UBI_CRC32_INIT, vid_hdr, UBI_VID_HDR_SIZE_CRC);
  939. hdr_crc = be32_to_cpu(vid_hdr->hdr_crc);
  940. if (hdr_crc != crc) {
  941. if (verbose) {
  942. ubi_warn("bad CRC at PEB %d, calculated %#08x, "
  943. "read %#08x", pnum, crc, hdr_crc);
  944. ubi_dbg_dump_vid_hdr(vid_hdr);
  945. } else if (UBI_IO_DEBUG)
  946. dbg_msg("bad CRC at PEB %d, calculated %#08x, "
  947. "read %#08x", pnum, crc, hdr_crc);
  948. return UBI_IO_BAD_VID_HDR;
  949. }
  950. /* Validate the VID header that we have just read */
  951. err = validate_vid_hdr(ubi, vid_hdr);
  952. if (err) {
  953. ubi_err("validation failed for PEB %d", pnum);
  954. return -EINVAL;
  955. }
  956. return read_err ? UBI_IO_BITFLIPS : 0;
  957. }
  958. /**
  959. * ubi_io_write_vid_hdr - write a volume identifier header.
  960. * @ubi: UBI device description object
  961. * @pnum: the physical eraseblock number to write to
  962. * @vid_hdr: the volume identifier header to write
  963. *
  964. * This function writes the volume identifier header described by @vid_hdr to
  965. * physical eraseblock @pnum. This function automatically fills the
  966. * @vid_hdr->magic and the @vid_hdr->version fields, as well as calculates
  967. * header CRC checksum and stores it at vid_hdr->hdr_crc.
  968. *
  969. * This function returns zero in case of success and a negative error code in
  970. * case of failure. If %-EIO is returned, the physical eraseblock probably went
  971. * bad.
  972. */
  973. int ubi_io_write_vid_hdr(struct ubi_device *ubi, int pnum,
  974. struct ubi_vid_hdr *vid_hdr)
  975. {
  976. int err;
  977. uint32_t crc;
  978. void *p;
  979. dbg_io("write VID header to PEB %d", pnum);
  980. ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
  981. err = paranoid_check_peb_ec_hdr(ubi, pnum);
  982. if (err)
  983. return err;
  984. vid_hdr->magic = cpu_to_be32(UBI_VID_HDR_MAGIC);
  985. vid_hdr->version = UBI_VERSION;
  986. crc = crc32(UBI_CRC32_INIT, vid_hdr, UBI_VID_HDR_SIZE_CRC);
  987. vid_hdr->hdr_crc = cpu_to_be32(crc);
  988. err = paranoid_check_vid_hdr(ubi, pnum, vid_hdr);
  989. if (err)
  990. return err;
  991. p = (char *)vid_hdr - ubi->vid_hdr_shift;
  992. err = ubi_io_write(ubi, p, pnum, ubi->vid_hdr_aloffset,
  993. ubi->vid_hdr_alsize);
  994. return err;
  995. }
  996. #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
  997. /**
  998. * paranoid_check_not_bad - ensure that a physical eraseblock is not bad.
  999. * @ubi: UBI device description object
  1000. * @pnum: physical eraseblock number to check
  1001. *
  1002. * This function returns zero if the physical eraseblock is good, %-EINVAL if
  1003. * it is bad and a negative error code if an error occurred.
  1004. */
  1005. static int paranoid_check_not_bad(const struct ubi_device *ubi, int pnum)
  1006. {
  1007. int err;
  1008. err = ubi_io_is_bad(ubi, pnum);
  1009. if (!err)
  1010. return err;
  1011. ubi_err("paranoid check failed for PEB %d", pnum);
  1012. ubi_dbg_dump_stack();
  1013. return err > 0 ? -EINVAL : err;
  1014. }
  1015. /**
  1016. * paranoid_check_ec_hdr - check if an erase counter header is all right.
  1017. * @ubi: UBI device description object
  1018. * @pnum: physical eraseblock number the erase counter header belongs to
  1019. * @ec_hdr: the erase counter header to check
  1020. *
  1021. * This function returns zero if the erase counter header contains valid
  1022. * values, and %-EINVAL if not.
  1023. */
  1024. static int paranoid_check_ec_hdr(const struct ubi_device *ubi, int pnum,
  1025. const struct ubi_ec_hdr *ec_hdr)
  1026. {
  1027. int err;
  1028. uint32_t magic;
  1029. magic = be32_to_cpu(ec_hdr->magic);
  1030. if (magic != UBI_EC_HDR_MAGIC) {
  1031. ubi_err("bad magic %#08x, must be %#08x",
  1032. magic, UBI_EC_HDR_MAGIC);
  1033. goto fail;
  1034. }
  1035. err = validate_ec_hdr(ubi, ec_hdr);
  1036. if (err) {
  1037. ubi_err("paranoid check failed for PEB %d", pnum);
  1038. goto fail;
  1039. }
  1040. return 0;
  1041. fail:
  1042. ubi_dbg_dump_ec_hdr(ec_hdr);
  1043. ubi_dbg_dump_stack();
  1044. return -EINVAL;
  1045. }
  1046. /**
  1047. * paranoid_check_peb_ec_hdr - check erase counter header.
  1048. * @ubi: UBI device description object
  1049. * @pnum: the physical eraseblock number to check
  1050. *
  1051. * This function returns zero if the erase counter header is all right and and
  1052. * a negative error code if not or if an error occurred.
  1053. */
  1054. static int paranoid_check_peb_ec_hdr(const struct ubi_device *ubi, int pnum)
  1055. {
  1056. int err;
  1057. uint32_t crc, hdr_crc;
  1058. struct ubi_ec_hdr *ec_hdr;
  1059. ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_NOFS);
  1060. if (!ec_hdr)
  1061. return -ENOMEM;
  1062. err = ubi_io_read(ubi, ec_hdr, pnum, 0, UBI_EC_HDR_SIZE);
  1063. if (err && err != UBI_IO_BITFLIPS && err != -EBADMSG)
  1064. goto exit;
  1065. crc = crc32(UBI_CRC32_INIT, ec_hdr, UBI_EC_HDR_SIZE_CRC);
  1066. hdr_crc = be32_to_cpu(ec_hdr->hdr_crc);
  1067. if (hdr_crc != crc) {
  1068. ubi_err("bad CRC, calculated %#08x, read %#08x", crc, hdr_crc);
  1069. ubi_err("paranoid check failed for PEB %d", pnum);
  1070. ubi_dbg_dump_ec_hdr(ec_hdr);
  1071. ubi_dbg_dump_stack();
  1072. err = -EINVAL;
  1073. goto exit;
  1074. }
  1075. err = paranoid_check_ec_hdr(ubi, pnum, ec_hdr);
  1076. exit:
  1077. kfree(ec_hdr);
  1078. return err;
  1079. }
  1080. /**
  1081. * paranoid_check_vid_hdr - check that a volume identifier header is all right.
  1082. * @ubi: UBI device description object
  1083. * @pnum: physical eraseblock number the volume identifier header belongs to
  1084. * @vid_hdr: the volume identifier header to check
  1085. *
  1086. * This function returns zero if the volume identifier header is all right, and
  1087. * %-EINVAL if not.
  1088. */
  1089. static int paranoid_check_vid_hdr(const struct ubi_device *ubi, int pnum,
  1090. const struct ubi_vid_hdr *vid_hdr)
  1091. {
  1092. int err;
  1093. uint32_t magic;
  1094. magic = be32_to_cpu(vid_hdr->magic);
  1095. if (magic != UBI_VID_HDR_MAGIC) {
  1096. ubi_err("bad VID header magic %#08x at PEB %d, must be %#08x",
  1097. magic, pnum, UBI_VID_HDR_MAGIC);
  1098. goto fail;
  1099. }
  1100. err = validate_vid_hdr(ubi, vid_hdr);
  1101. if (err) {
  1102. ubi_err("paranoid check failed for PEB %d", pnum);
  1103. goto fail;
  1104. }
  1105. return err;
  1106. fail:
  1107. ubi_err("paranoid check failed for PEB %d", pnum);
  1108. ubi_dbg_dump_vid_hdr(vid_hdr);
  1109. ubi_dbg_dump_stack();
  1110. return -EINVAL;
  1111. }
  1112. /**
  1113. * paranoid_check_peb_vid_hdr - check volume identifier header.
  1114. * @ubi: UBI device description object
  1115. * @pnum: the physical eraseblock number to check
  1116. *
  1117. * This function returns zero if the volume identifier header is all right,
  1118. * and a negative error code if not or if an error occurred.
  1119. */
  1120. static int paranoid_check_peb_vid_hdr(const struct ubi_device *ubi, int pnum)
  1121. {
  1122. int err;
  1123. uint32_t crc, hdr_crc;
  1124. struct ubi_vid_hdr *vid_hdr;
  1125. void *p;
  1126. vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
  1127. if (!vid_hdr)
  1128. return -ENOMEM;
  1129. p = (char *)vid_hdr - ubi->vid_hdr_shift;
  1130. err = ubi_io_read(ubi, p, pnum, ubi->vid_hdr_aloffset,
  1131. ubi->vid_hdr_alsize);
  1132. if (err && err != UBI_IO_BITFLIPS && err != -EBADMSG)
  1133. goto exit;
  1134. crc = crc32(UBI_CRC32_INIT, vid_hdr, UBI_EC_HDR_SIZE_CRC);
  1135. hdr_crc = be32_to_cpu(vid_hdr->hdr_crc);
  1136. if (hdr_crc != crc) {
  1137. ubi_err("bad VID header CRC at PEB %d, calculated %#08x, "
  1138. "read %#08x", pnum, crc, hdr_crc);
  1139. ubi_err("paranoid check failed for PEB %d", pnum);
  1140. ubi_dbg_dump_vid_hdr(vid_hdr);
  1141. ubi_dbg_dump_stack();
  1142. err = -EINVAL;
  1143. goto exit;
  1144. }
  1145. err = paranoid_check_vid_hdr(ubi, pnum, vid_hdr);
  1146. exit:
  1147. ubi_free_vid_hdr(ubi, vid_hdr);
  1148. return err;
  1149. }
  1150. /**
  1151. * ubi_dbg_check_write - make sure write succeeded.
  1152. * @ubi: UBI device description object
  1153. * @buf: buffer with data which were written
  1154. * @pnum: physical eraseblock number the data were written to
  1155. * @offset: offset within the physical eraseblock the data were written to
  1156. * @len: how many bytes were written
  1157. *
  1158. * This functions reads data which were recently written and compares it with
  1159. * the original data buffer - the data have to match. Returns zero if the data
  1160. * match and a negative error code if not or in case of failure.
  1161. */
  1162. int ubi_dbg_check_write(struct ubi_device *ubi, const void *buf, int pnum,
  1163. int offset, int len)
  1164. {
  1165. int err, i;
  1166. mutex_lock(&ubi->dbg_buf_mutex);
  1167. err = ubi_io_read(ubi, ubi->dbg_peb_buf, pnum, offset, len);
  1168. if (err)
  1169. goto out_unlock;
  1170. for (i = 0; i < len; i++) {
  1171. uint8_t c = ((uint8_t *)buf)[i];
  1172. uint8_t c1 = ((uint8_t *)ubi->dbg_peb_buf)[i];
  1173. int dump_len;
  1174. if (c == c1)
  1175. continue;
  1176. ubi_err("paranoid check failed for PEB %d:%d, len %d",
  1177. pnum, offset, len);
  1178. ubi_msg("data differ at position %d", i);
  1179. dump_len = max_t(int, 128, len - i);
  1180. ubi_msg("hex dump of the original buffer from %d to %d",
  1181. i, i + dump_len);
  1182. print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1,
  1183. buf + i, dump_len, 1);
  1184. ubi_msg("hex dump of the read buffer from %d to %d",
  1185. i, i + dump_len);
  1186. print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1,
  1187. ubi->dbg_peb_buf + i, dump_len, 1);
  1188. ubi_dbg_dump_stack();
  1189. err = -EINVAL;
  1190. goto out_unlock;
  1191. }
  1192. mutex_unlock(&ubi->dbg_buf_mutex);
  1193. return 0;
  1194. out_unlock:
  1195. mutex_unlock(&ubi->dbg_buf_mutex);
  1196. return err;
  1197. }
  1198. /**
  1199. * ubi_dbg_check_all_ff - check that a region of flash is empty.
  1200. * @ubi: UBI device description object
  1201. * @pnum: the physical eraseblock number to check
  1202. * @offset: the starting offset within the physical eraseblock to check
  1203. * @len: the length of the region to check
  1204. *
  1205. * This function returns zero if only 0xFF bytes are present at offset
  1206. * @offset of the physical eraseblock @pnum, and a negative error code if not
  1207. * or if an error occurred.
  1208. */
  1209. int ubi_dbg_check_all_ff(struct ubi_device *ubi, int pnum, int offset, int len)
  1210. {
  1211. size_t read;
  1212. int err;
  1213. loff_t addr = (loff_t)pnum * ubi->peb_size + offset;
  1214. mutex_lock(&ubi->dbg_buf_mutex);
  1215. err = ubi->mtd->read(ubi->mtd, addr, len, &read, ubi->dbg_peb_buf);
  1216. if (err && err != -EUCLEAN) {
  1217. ubi_err("error %d while reading %d bytes from PEB %d:%d, "
  1218. "read %zd bytes", err, len, pnum, offset, read);
  1219. goto error;
  1220. }
  1221. err = check_pattern(ubi->dbg_peb_buf, 0xFF, len);
  1222. if (err == 0) {
  1223. ubi_err("flash region at PEB %d:%d, length %d does not "
  1224. "contain all 0xFF bytes", pnum, offset, len);
  1225. goto fail;
  1226. }
  1227. mutex_unlock(&ubi->dbg_buf_mutex);
  1228. return 0;
  1229. fail:
  1230. ubi_err("paranoid check failed for PEB %d", pnum);
  1231. ubi_msg("hex dump of the %d-%d region", offset, offset + len);
  1232. print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1,
  1233. ubi->dbg_peb_buf, len, 1);
  1234. err = -EINVAL;
  1235. error:
  1236. ubi_dbg_dump_stack();
  1237. mutex_unlock(&ubi->dbg_buf_mutex);
  1238. return err;
  1239. }
  1240. #endif /* CONFIG_MTD_UBI_DEBUG_PARANOID */