io.c 41 KB

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