io.c 40 KB

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