io.c 42 KB

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