io.c 42 KB

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