io.c 41 KB

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