io.c 40 KB

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