io.c 41 KB

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