io.c 41 KB

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