io.c 41 KB

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