io.c 37 KB

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