io.c 42 KB

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