io.c 39 KB

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