io.c 37 KB

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