io.c 41 KB

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