io.c 43 KB

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