io.c 37 KB

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