io.c 42 KB

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