io.c 43 KB

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