io.c 41 KB

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