ubispl.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139
  1. // SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
  2. /*
  3. * Copyright (c) Thomas Gleixner <tglx@linutronix.de>
  4. *
  5. * The parts taken from the kernel implementation are:
  6. *
  7. * Copyright (c) International Business Machines Corp., 2006
  8. */
  9. #include <common.h>
  10. #include <errno.h>
  11. #include <linux/bug.h>
  12. #include <u-boot/crc.h>
  13. #include <ubispl.h>
  14. #include <linux/bitops.h>
  15. #include <linux/crc32.h>
  16. #include "ubispl.h"
  17. /**
  18. * ubi_calc_fm_size - calculates the fastmap size in bytes for an UBI device.
  19. * @ubi: UBI device description object
  20. */
  21. static size_t ubi_calc_fm_size(struct ubi_scan_info *ubi)
  22. {
  23. size_t size;
  24. size = sizeof(struct ubi_fm_sb) +
  25. sizeof(struct ubi_fm_hdr) +
  26. sizeof(struct ubi_fm_scan_pool) +
  27. sizeof(struct ubi_fm_scan_pool) +
  28. (ubi->peb_count * sizeof(struct ubi_fm_ec)) +
  29. (sizeof(struct ubi_fm_eba) +
  30. (ubi->peb_count * sizeof(__be32))) +
  31. sizeof(struct ubi_fm_volhdr) * UBI_MAX_VOLUMES;
  32. return roundup(size, ubi->leb_size);
  33. }
  34. static int ubi_io_read(struct ubi_scan_info *ubi, void *buf, int pnum,
  35. unsigned long from, unsigned long len)
  36. {
  37. return ubi->read(pnum + ubi->peb_offset, from, len, buf);
  38. }
  39. static int ubi_io_is_bad(struct ubi_scan_info *ubi, int peb)
  40. {
  41. return peb >= ubi->peb_count || peb < 0;
  42. }
  43. #ifdef CONFIG_SPL_UBI_LOAD_BY_VOLNAME
  44. /**
  45. * ubi_dump_vtbl_record - dump a &struct ubi_vtbl_record object.
  46. * @r: the object to dump
  47. * @idx: volume table index
  48. */
  49. void ubi_dump_vtbl_record(const struct ubi_vtbl_record *r, int idx)
  50. {
  51. int name_len = be16_to_cpu(r->name_len);
  52. ubi_dbg("Volume table record %d dump: size: %d",
  53. idx, sizeof(struct ubi_vtbl_record));
  54. ubi_dbg("\treserved_pebs %d", be32_to_cpu(r->reserved_pebs));
  55. ubi_dbg("\talignment %d", be32_to_cpu(r->alignment));
  56. ubi_dbg("\tdata_pad %d", be32_to_cpu(r->data_pad));
  57. ubi_dbg("\tvol_type %d", (int)r->vol_type);
  58. ubi_dbg("\tupd_marker %d", (int)r->upd_marker);
  59. ubi_dbg("\tname_len %d", name_len);
  60. if (r->name[0] == '\0') {
  61. ubi_dbg("\tname NULL");
  62. return;
  63. }
  64. if (name_len <= UBI_VOL_NAME_MAX &&
  65. strnlen(&r->name[0], name_len + 1) == name_len) {
  66. ubi_dbg("\tname %s", &r->name[0]);
  67. } else {
  68. ubi_dbg("\t1st 5 characters of name: %c%c%c%c%c",
  69. r->name[0], r->name[1], r->name[2], r->name[3],
  70. r->name[4]);
  71. }
  72. ubi_dbg("\tcrc %#08x", be32_to_cpu(r->crc));
  73. }
  74. /* Empty volume table record */
  75. static struct ubi_vtbl_record empty_vtbl_record;
  76. /**
  77. * vtbl_check - check if volume table is not corrupted and sensible.
  78. * @ubi: UBI device description object
  79. * @vtbl: volume table
  80. *
  81. * This function returns zero if @vtbl is all right, %1 if CRC is incorrect,
  82. * and %-EINVAL if it contains inconsistent data.
  83. */
  84. static int vtbl_check(struct ubi_scan_info *ubi,
  85. struct ubi_vtbl_record *vtbl)
  86. {
  87. int i, n, reserved_pebs, alignment, data_pad, vol_type, name_len;
  88. int upd_marker, err;
  89. uint32_t crc;
  90. const char *name;
  91. for (i = 0; i < UBI_SPL_VOL_IDS; i++) {
  92. reserved_pebs = be32_to_cpu(vtbl[i].reserved_pebs);
  93. alignment = be32_to_cpu(vtbl[i].alignment);
  94. data_pad = be32_to_cpu(vtbl[i].data_pad);
  95. upd_marker = vtbl[i].upd_marker;
  96. vol_type = vtbl[i].vol_type;
  97. name_len = be16_to_cpu(vtbl[i].name_len);
  98. name = &vtbl[i].name[0];
  99. crc = crc32(UBI_CRC32_INIT, &vtbl[i], UBI_VTBL_RECORD_SIZE_CRC);
  100. if (be32_to_cpu(vtbl[i].crc) != crc) {
  101. ubi_err("bad CRC at record %u: %#08x, not %#08x",
  102. i, crc, be32_to_cpu(vtbl[i].crc));
  103. ubi_dump_vtbl_record(&vtbl[i], i);
  104. return 1;
  105. }
  106. if (reserved_pebs == 0) {
  107. if (memcmp(&vtbl[i], &empty_vtbl_record,
  108. UBI_VTBL_RECORD_SIZE)) {
  109. err = 2;
  110. goto bad;
  111. }
  112. continue;
  113. }
  114. if (reserved_pebs < 0 || alignment < 0 || data_pad < 0 ||
  115. name_len < 0) {
  116. err = 3;
  117. goto bad;
  118. }
  119. if (alignment > ubi->leb_size || alignment == 0) {
  120. err = 4;
  121. goto bad;
  122. }
  123. n = alignment & (CONFIG_SPL_UBI_VID_OFFSET - 1);
  124. if (alignment != 1 && n) {
  125. err = 5;
  126. goto bad;
  127. }
  128. n = ubi->leb_size % alignment;
  129. if (data_pad != n) {
  130. ubi_err("bad data_pad, has to be %d", n);
  131. err = 6;
  132. goto bad;
  133. }
  134. if (vol_type != UBI_VID_DYNAMIC && vol_type != UBI_VID_STATIC) {
  135. err = 7;
  136. goto bad;
  137. }
  138. if (upd_marker != 0 && upd_marker != 1) {
  139. err = 8;
  140. goto bad;
  141. }
  142. if (name_len > UBI_VOL_NAME_MAX) {
  143. err = 10;
  144. goto bad;
  145. }
  146. if (name[0] == '\0') {
  147. err = 11;
  148. goto bad;
  149. }
  150. if (name_len != strnlen(name, name_len + 1)) {
  151. err = 12;
  152. goto bad;
  153. }
  154. ubi_dump_vtbl_record(&vtbl[i], i);
  155. }
  156. /* Checks that all names are unique */
  157. for (i = 0; i < UBI_SPL_VOL_IDS - 1; i++) {
  158. for (n = i + 1; n < UBI_SPL_VOL_IDS; n++) {
  159. int len1 = be16_to_cpu(vtbl[i].name_len);
  160. int len2 = be16_to_cpu(vtbl[n].name_len);
  161. if (len1 > 0 && len1 == len2 &&
  162. !strncmp(vtbl[i].name, vtbl[n].name, len1)) {
  163. ubi_err("volumes %d and %d have the same name \"%s\"",
  164. i, n, vtbl[i].name);
  165. ubi_dump_vtbl_record(&vtbl[i], i);
  166. ubi_dump_vtbl_record(&vtbl[n], n);
  167. return -EINVAL;
  168. }
  169. }
  170. }
  171. return 0;
  172. bad:
  173. ubi_err("volume table check failed: record %d, error %d", i, err);
  174. ubi_dump_vtbl_record(&vtbl[i], i);
  175. return -EINVAL;
  176. }
  177. static int ubi_read_volume_table(struct ubi_scan_info *ubi, u32 pnum)
  178. {
  179. int err = -EINVAL;
  180. empty_vtbl_record.crc = cpu_to_be32(0xf116c36b);
  181. err = ubi_io_read(ubi, &ubi->vtbl, pnum, ubi->leb_start,
  182. sizeof(struct ubi_vtbl_record) * UBI_SPL_VOL_IDS);
  183. if (err && err != UBI_IO_BITFLIPS) {
  184. ubi_err("unable to read volume table");
  185. goto out;
  186. }
  187. if (!vtbl_check(ubi, ubi->vtbl)) {
  188. ubi->vtbl_valid = 1;
  189. err = 0;
  190. }
  191. out:
  192. return err;
  193. }
  194. #endif /* CONFIG_SPL_UBI_LOAD_BY_VOLNAME */
  195. static int ubi_io_read_vid_hdr(struct ubi_scan_info *ubi, int pnum,
  196. struct ubi_vid_hdr *vh, int unused)
  197. {
  198. u32 magic;
  199. int res;
  200. /* No point in rescanning a corrupt block */
  201. if (test_bit(pnum, ubi->corrupt))
  202. return UBI_IO_BAD_HDR;
  203. /*
  204. * If the block has been scanned already, no need to rescan
  205. */
  206. if (test_and_set_bit(pnum, ubi->scanned))
  207. return 0;
  208. res = ubi_io_read(ubi, vh, pnum, ubi->vid_offset, sizeof(*vh));
  209. /*
  210. * Bad block, unrecoverable ECC error, skip the block
  211. */
  212. if (res) {
  213. ubi_dbg("Skipping bad or unreadable block %d", pnum);
  214. vh->magic = 0;
  215. generic_set_bit(pnum, ubi->corrupt);
  216. return res;
  217. }
  218. /* Magic number available ? */
  219. magic = be32_to_cpu(vh->magic);
  220. if (magic != UBI_VID_HDR_MAGIC) {
  221. generic_set_bit(pnum, ubi->corrupt);
  222. if (magic == 0xffffffff)
  223. return UBI_IO_FF;
  224. ubi_msg("Bad magic in block 0%d %08x", pnum, magic);
  225. return UBI_IO_BAD_HDR;
  226. }
  227. /* Header CRC correct ? */
  228. if (crc32(UBI_CRC32_INIT, vh, UBI_VID_HDR_SIZE_CRC) !=
  229. be32_to_cpu(vh->hdr_crc)) {
  230. ubi_msg("Bad CRC in block 0%d", pnum);
  231. generic_set_bit(pnum, ubi->corrupt);
  232. return UBI_IO_BAD_HDR;
  233. }
  234. ubi_dbg("RV: pnum: %i sqnum %llu", pnum, be64_to_cpu(vh->sqnum));
  235. return 0;
  236. }
  237. static int ubi_rescan_fm_vid_hdr(struct ubi_scan_info *ubi,
  238. struct ubi_vid_hdr *vh,
  239. u32 fm_pnum, u32 fm_vol_id, u32 fm_lnum)
  240. {
  241. int res;
  242. if (ubi_io_is_bad(ubi, fm_pnum))
  243. return -EINVAL;
  244. res = ubi_io_read_vid_hdr(ubi, fm_pnum, vh, 0);
  245. if (!res) {
  246. /* Check volume id, volume type and lnum */
  247. if (be32_to_cpu(vh->vol_id) == fm_vol_id &&
  248. vh->vol_type == UBI_VID_STATIC &&
  249. be32_to_cpu(vh->lnum) == fm_lnum)
  250. return 0;
  251. ubi_dbg("RS: PEB %u vol: %u : %u typ %u lnum %u %u",
  252. fm_pnum, fm_vol_id, vh->vol_type,
  253. be32_to_cpu(vh->vol_id),
  254. fm_lnum, be32_to_cpu(vh->lnum));
  255. }
  256. return res;
  257. }
  258. /* Insert the logic block into the volume info */
  259. static int ubi_add_peb_to_vol(struct ubi_scan_info *ubi,
  260. struct ubi_vid_hdr *vh, u32 vol_id,
  261. u32 pnum, u32 lnum)
  262. {
  263. struct ubi_vol_info *vi = ubi->volinfo + vol_id;
  264. u32 *ltp;
  265. /*
  266. * If the volume is larger than expected, yell and give up :(
  267. */
  268. if (lnum >= UBI_MAX_VOL_LEBS) {
  269. ubi_warn("Vol: %u LEB %d > %d", vol_id, lnum, UBI_MAX_VOL_LEBS);
  270. return -EINVAL;
  271. }
  272. ubi_dbg("SC: Add PEB %u to Vol %u as LEB %u fnd %d sc %d",
  273. pnum, vol_id, lnum, !!test_bit(lnum, vi->found),
  274. !!test_bit(pnum, ubi->scanned));
  275. /* Points to the translation entry */
  276. ltp = vi->lebs_to_pebs + lnum;
  277. /* If the block is already assigned, check sqnum */
  278. if (__test_and_set_bit(lnum, vi->found)) {
  279. u32 cur_pnum = *ltp;
  280. struct ubi_vid_hdr *cur = ubi->blockinfo + cur_pnum;
  281. /*
  282. * If the current block hase not yet been scanned, we
  283. * need to do that. The other block might be stale or
  284. * the current block corrupted and the FM not yet
  285. * updated.
  286. */
  287. if (!test_bit(cur_pnum, ubi->scanned)) {
  288. /*
  289. * If the scan fails, we use the valid block
  290. */
  291. if (ubi_rescan_fm_vid_hdr(ubi, cur, cur_pnum, vol_id,
  292. lnum)) {
  293. *ltp = pnum;
  294. return 0;
  295. }
  296. }
  297. /*
  298. * Should not happen ....
  299. */
  300. if (test_bit(cur_pnum, ubi->corrupt)) {
  301. *ltp = pnum;
  302. return 0;
  303. }
  304. ubi_dbg("Vol %u LEB %u PEB %u->sqnum %llu NPEB %u->sqnum %llu",
  305. vol_id, lnum, cur_pnum, be64_to_cpu(cur->sqnum), pnum,
  306. be64_to_cpu(vh->sqnum));
  307. /*
  308. * Compare sqnum and take the newer one
  309. */
  310. if (be64_to_cpu(cur->sqnum) < be64_to_cpu(vh->sqnum))
  311. *ltp = pnum;
  312. } else {
  313. *ltp = pnum;
  314. if (lnum > vi->last_block)
  315. vi->last_block = lnum;
  316. }
  317. return 0;
  318. }
  319. static int ubi_scan_vid_hdr(struct ubi_scan_info *ubi, struct ubi_vid_hdr *vh,
  320. u32 pnum)
  321. {
  322. u32 vol_id, lnum;
  323. int res;
  324. if (ubi_io_is_bad(ubi, pnum))
  325. return -EINVAL;
  326. res = ubi_io_read_vid_hdr(ubi, pnum, vh, 0);
  327. if (res)
  328. return res;
  329. /* Get volume id */
  330. vol_id = be32_to_cpu(vh->vol_id);
  331. /* If this is the fastmap anchor, return right away */
  332. if (vol_id == UBI_FM_SB_VOLUME_ID)
  333. return ubi->fm_enabled ? UBI_FASTMAP_ANCHOR : 0;
  334. #ifdef CONFIG_SPL_UBI_LOAD_BY_VOLNAME
  335. /* If this is a UBI volume table, read it and return */
  336. if (vol_id == UBI_LAYOUT_VOLUME_ID && !ubi->vtbl_valid) {
  337. res = ubi_read_volume_table(ubi, pnum);
  338. return res;
  339. }
  340. #endif
  341. /* We only care about static volumes with an id < UBI_SPL_VOL_IDS */
  342. if (vol_id >= UBI_SPL_VOL_IDS || vh->vol_type != UBI_VID_STATIC)
  343. return 0;
  344. #ifndef CONFIG_SPL_UBI_LOAD_BY_VOLNAME
  345. /* We are only interested in the volumes to load */
  346. if (!test_bit(vol_id, ubi->toload))
  347. return 0;
  348. #endif
  349. lnum = be32_to_cpu(vh->lnum);
  350. return ubi_add_peb_to_vol(ubi, vh, vol_id, pnum, lnum);
  351. }
  352. static int assign_aeb_to_av(struct ubi_scan_info *ubi, u32 pnum, u32 lnum,
  353. u32 vol_id, u32 vol_type, u32 used)
  354. {
  355. struct ubi_vid_hdr *vh;
  356. if (ubi_io_is_bad(ubi, pnum))
  357. return -EINVAL;
  358. ubi->fastmap_pebs++;
  359. #ifndef CONFIG_SPL_UBI_LOAD_BY_VOLNAME
  360. if (vol_id >= UBI_SPL_VOL_IDS || vol_type != UBI_STATIC_VOLUME)
  361. return 0;
  362. /* We are only interested in the volumes to load */
  363. if (!test_bit(vol_id, ubi->toload))
  364. return 0;
  365. #endif
  366. vh = ubi->blockinfo + pnum;
  367. return ubi_scan_vid_hdr(ubi, vh, pnum);
  368. }
  369. static int scan_pool(struct ubi_scan_info *ubi, __be32 *pebs, int pool_size)
  370. {
  371. struct ubi_vid_hdr *vh;
  372. u32 pnum;
  373. int i;
  374. ubi_dbg("Scanning pool size: %d", pool_size);
  375. for (i = 0; i < pool_size; i++) {
  376. pnum = be32_to_cpu(pebs[i]);
  377. if (ubi_io_is_bad(ubi, pnum)) {
  378. ubi_err("FM: Bad PEB in fastmap pool! %u", pnum);
  379. return UBI_BAD_FASTMAP;
  380. }
  381. vh = ubi->blockinfo + pnum;
  382. /*
  383. * We allow the scan to fail here. The loader will notice
  384. * and look for a replacement.
  385. */
  386. ubi_scan_vid_hdr(ubi, vh, pnum);
  387. }
  388. return 0;
  389. }
  390. /*
  391. * Fastmap code is stolen from Linux kernel and this stub structure is used
  392. * to make it happy.
  393. */
  394. struct ubi_attach_info {
  395. int i;
  396. };
  397. static int ubi_attach_fastmap(struct ubi_scan_info *ubi,
  398. struct ubi_attach_info *ai,
  399. struct ubi_fastmap_layout *fm)
  400. {
  401. struct ubi_fm_hdr *fmhdr;
  402. struct ubi_fm_scan_pool *fmpl1, *fmpl2;
  403. struct ubi_fm_ec *fmec;
  404. struct ubi_fm_volhdr *fmvhdr;
  405. struct ubi_fm_eba *fm_eba;
  406. int ret, i, j, pool_size, wl_pool_size;
  407. size_t fm_pos = 0, fm_size = ubi->fm_size;
  408. void *fm_raw = ubi->fm_buf;
  409. memset(ubi->fm_used, 0, sizeof(ubi->fm_used));
  410. fm_pos += sizeof(struct ubi_fm_sb);
  411. if (fm_pos >= fm_size)
  412. goto fail_bad;
  413. fmhdr = (struct ubi_fm_hdr *)(fm_raw + fm_pos);
  414. fm_pos += sizeof(*fmhdr);
  415. if (fm_pos >= fm_size)
  416. goto fail_bad;
  417. if (be32_to_cpu(fmhdr->magic) != UBI_FM_HDR_MAGIC) {
  418. ubi_err("bad fastmap header magic: 0x%x, expected: 0x%x",
  419. be32_to_cpu(fmhdr->magic), UBI_FM_HDR_MAGIC);
  420. goto fail_bad;
  421. }
  422. fmpl1 = (struct ubi_fm_scan_pool *)(fm_raw + fm_pos);
  423. fm_pos += sizeof(*fmpl1);
  424. if (fm_pos >= fm_size)
  425. goto fail_bad;
  426. if (be32_to_cpu(fmpl1->magic) != UBI_FM_POOL_MAGIC) {
  427. ubi_err("bad fastmap pool magic: 0x%x, expected: 0x%x",
  428. be32_to_cpu(fmpl1->magic), UBI_FM_POOL_MAGIC);
  429. goto fail_bad;
  430. }
  431. fmpl2 = (struct ubi_fm_scan_pool *)(fm_raw + fm_pos);
  432. fm_pos += sizeof(*fmpl2);
  433. if (fm_pos >= fm_size)
  434. goto fail_bad;
  435. if (be32_to_cpu(fmpl2->magic) != UBI_FM_POOL_MAGIC) {
  436. ubi_err("bad fastmap pool magic: 0x%x, expected: 0x%x",
  437. be32_to_cpu(fmpl2->magic), UBI_FM_POOL_MAGIC);
  438. goto fail_bad;
  439. }
  440. pool_size = be16_to_cpu(fmpl1->size);
  441. wl_pool_size = be16_to_cpu(fmpl2->size);
  442. fm->max_pool_size = be16_to_cpu(fmpl1->max_size);
  443. fm->max_wl_pool_size = be16_to_cpu(fmpl2->max_size);
  444. if (pool_size > UBI_FM_MAX_POOL_SIZE || pool_size < 0) {
  445. ubi_err("bad pool size: %i", pool_size);
  446. goto fail_bad;
  447. }
  448. if (wl_pool_size > UBI_FM_MAX_POOL_SIZE || wl_pool_size < 0) {
  449. ubi_err("bad WL pool size: %i", wl_pool_size);
  450. goto fail_bad;
  451. }
  452. if (fm->max_pool_size > UBI_FM_MAX_POOL_SIZE ||
  453. fm->max_pool_size < 0) {
  454. ubi_err("bad maximal pool size: %i", fm->max_pool_size);
  455. goto fail_bad;
  456. }
  457. if (fm->max_wl_pool_size > UBI_FM_MAX_POOL_SIZE ||
  458. fm->max_wl_pool_size < 0) {
  459. ubi_err("bad maximal WL pool size: %i", fm->max_wl_pool_size);
  460. goto fail_bad;
  461. }
  462. /* read EC values from free list */
  463. for (i = 0; i < be32_to_cpu(fmhdr->free_peb_count); i++) {
  464. fmec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
  465. fm_pos += sizeof(*fmec);
  466. if (fm_pos >= fm_size)
  467. goto fail_bad;
  468. }
  469. /* read EC values from used list */
  470. for (i = 0; i < be32_to_cpu(fmhdr->used_peb_count); i++) {
  471. fmec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
  472. fm_pos += sizeof(*fmec);
  473. if (fm_pos >= fm_size)
  474. goto fail_bad;
  475. generic_set_bit(be32_to_cpu(fmec->pnum), ubi->fm_used);
  476. }
  477. /* read EC values from scrub list */
  478. for (i = 0; i < be32_to_cpu(fmhdr->scrub_peb_count); i++) {
  479. fmec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
  480. fm_pos += sizeof(*fmec);
  481. if (fm_pos >= fm_size)
  482. goto fail_bad;
  483. }
  484. /* read EC values from erase list */
  485. for (i = 0; i < be32_to_cpu(fmhdr->erase_peb_count); i++) {
  486. fmec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
  487. fm_pos += sizeof(*fmec);
  488. if (fm_pos >= fm_size)
  489. goto fail_bad;
  490. }
  491. /* Iterate over all volumes and read their EBA table */
  492. for (i = 0; i < be32_to_cpu(fmhdr->vol_count); i++) {
  493. u32 vol_id, vol_type, used, reserved;
  494. fmvhdr = (struct ubi_fm_volhdr *)(fm_raw + fm_pos);
  495. fm_pos += sizeof(*fmvhdr);
  496. if (fm_pos >= fm_size)
  497. goto fail_bad;
  498. if (be32_to_cpu(fmvhdr->magic) != UBI_FM_VHDR_MAGIC) {
  499. ubi_err("bad fastmap vol header magic: 0x%x, " \
  500. "expected: 0x%x",
  501. be32_to_cpu(fmvhdr->magic), UBI_FM_VHDR_MAGIC);
  502. goto fail_bad;
  503. }
  504. vol_id = be32_to_cpu(fmvhdr->vol_id);
  505. vol_type = fmvhdr->vol_type;
  506. used = be32_to_cpu(fmvhdr->used_ebs);
  507. fm_eba = (struct ubi_fm_eba *)(fm_raw + fm_pos);
  508. fm_pos += sizeof(*fm_eba);
  509. fm_pos += (sizeof(__be32) * be32_to_cpu(fm_eba->reserved_pebs));
  510. if (fm_pos >= fm_size)
  511. goto fail_bad;
  512. if (be32_to_cpu(fm_eba->magic) != UBI_FM_EBA_MAGIC) {
  513. ubi_err("bad fastmap EBA header magic: 0x%x, " \
  514. "expected: 0x%x",
  515. be32_to_cpu(fm_eba->magic), UBI_FM_EBA_MAGIC);
  516. goto fail_bad;
  517. }
  518. reserved = be32_to_cpu(fm_eba->reserved_pebs);
  519. ubi_dbg("FA: vol %u used %u res: %u", vol_id, used, reserved);
  520. for (j = 0; j < reserved; j++) {
  521. int pnum = be32_to_cpu(fm_eba->pnum[j]);
  522. if ((int)be32_to_cpu(fm_eba->pnum[j]) < 0)
  523. continue;
  524. if (!__test_and_clear_bit(pnum, ubi->fm_used))
  525. continue;
  526. /*
  527. * We only handle static volumes so used_ebs
  528. * needs to be handed in. And we do not assign
  529. * the reserved blocks
  530. */
  531. if (j >= used)
  532. continue;
  533. ret = assign_aeb_to_av(ubi, pnum, j, vol_id,
  534. vol_type, used);
  535. if (!ret)
  536. continue;
  537. /*
  538. * Nasty: The fastmap claims that the volume
  539. * has one block more than it, but that block
  540. * is always empty and the other blocks have
  541. * the correct number of total LEBs in the
  542. * headers. Deal with it.
  543. */
  544. if (ret != UBI_IO_FF && j != used - 1)
  545. goto fail_bad;
  546. ubi_dbg("FA: Vol: %u Ignoring empty LEB %d of %d",
  547. vol_id, j, used);
  548. }
  549. }
  550. ret = scan_pool(ubi, fmpl1->pebs, pool_size);
  551. if (ret)
  552. goto fail;
  553. ret = scan_pool(ubi, fmpl2->pebs, wl_pool_size);
  554. if (ret)
  555. goto fail;
  556. #ifdef CHECKME
  557. /*
  558. * If fastmap is leaking PEBs (must not happen), raise a
  559. * fat warning and fall back to scanning mode.
  560. * We do this here because in ubi_wl_init() it's too late
  561. * and we cannot fall back to scanning.
  562. */
  563. if (WARN_ON(count_fastmap_pebs(ai) != ubi->peb_count -
  564. ai->bad_peb_count - fm->used_blocks))
  565. goto fail_bad;
  566. #endif
  567. return 0;
  568. fail_bad:
  569. ret = UBI_BAD_FASTMAP;
  570. fail:
  571. return ret;
  572. }
  573. static int ubi_scan_fastmap(struct ubi_scan_info *ubi,
  574. struct ubi_attach_info *ai,
  575. int fm_anchor)
  576. {
  577. struct ubi_fm_sb *fmsb, *fmsb2;
  578. struct ubi_vid_hdr *vh;
  579. struct ubi_fastmap_layout *fm;
  580. int i, used_blocks, pnum, ret = 0;
  581. size_t fm_size;
  582. __be32 crc, tmp_crc;
  583. unsigned long long sqnum = 0;
  584. fmsb = &ubi->fm_sb;
  585. fm = &ubi->fm_layout;
  586. ret = ubi_io_read(ubi, fmsb, fm_anchor, ubi->leb_start, sizeof(*fmsb));
  587. if (ret && ret != UBI_IO_BITFLIPS)
  588. goto free_fm_sb;
  589. else if (ret == UBI_IO_BITFLIPS)
  590. fm->to_be_tortured[0] = 1;
  591. if (be32_to_cpu(fmsb->magic) != UBI_FM_SB_MAGIC) {
  592. ubi_err("bad super block magic: 0x%x, expected: 0x%x",
  593. be32_to_cpu(fmsb->magic), UBI_FM_SB_MAGIC);
  594. ret = UBI_BAD_FASTMAP;
  595. goto free_fm_sb;
  596. }
  597. if (fmsb->version != UBI_FM_FMT_VERSION) {
  598. ubi_err("bad fastmap version: %i, expected: %i",
  599. fmsb->version, UBI_FM_FMT_VERSION);
  600. ret = UBI_BAD_FASTMAP;
  601. goto free_fm_sb;
  602. }
  603. used_blocks = be32_to_cpu(fmsb->used_blocks);
  604. if (used_blocks > UBI_FM_MAX_BLOCKS || used_blocks < 1) {
  605. ubi_err("number of fastmap blocks is invalid: %i", used_blocks);
  606. ret = UBI_BAD_FASTMAP;
  607. goto free_fm_sb;
  608. }
  609. fm_size = ubi->leb_size * used_blocks;
  610. if (fm_size != ubi->fm_size) {
  611. ubi_err("bad fastmap size: %zi, expected: %zi", fm_size,
  612. ubi->fm_size);
  613. ret = UBI_BAD_FASTMAP;
  614. goto free_fm_sb;
  615. }
  616. vh = &ubi->fm_vh;
  617. for (i = 0; i < used_blocks; i++) {
  618. pnum = be32_to_cpu(fmsb->block_loc[i]);
  619. if (ubi_io_is_bad(ubi, pnum)) {
  620. ret = UBI_BAD_FASTMAP;
  621. goto free_hdr;
  622. }
  623. #ifdef LATER
  624. int image_seq;
  625. ret = ubi_io_read_ec_hdr(ubi, pnum, ech, 0);
  626. if (ret && ret != UBI_IO_BITFLIPS) {
  627. ubi_err("unable to read fastmap block# %i EC (PEB: %i)",
  628. i, pnum);
  629. if (ret > 0)
  630. ret = UBI_BAD_FASTMAP;
  631. goto free_hdr;
  632. } else if (ret == UBI_IO_BITFLIPS)
  633. fm->to_be_tortured[i] = 1;
  634. image_seq = be32_to_cpu(ech->image_seq);
  635. if (!ubi->image_seq)
  636. ubi->image_seq = image_seq;
  637. /*
  638. * Older UBI implementations have image_seq set to zero, so
  639. * we shouldn't fail if image_seq == 0.
  640. */
  641. if (image_seq && (image_seq != ubi->image_seq)) {
  642. ubi_err("wrong image seq:%d instead of %d",
  643. be32_to_cpu(ech->image_seq), ubi->image_seq);
  644. ret = UBI_BAD_FASTMAP;
  645. goto free_hdr;
  646. }
  647. #endif
  648. ret = ubi_io_read_vid_hdr(ubi, pnum, vh, 0);
  649. if (ret && ret != UBI_IO_BITFLIPS) {
  650. ubi_err("unable to read fastmap block# %i (PEB: %i)",
  651. i, pnum);
  652. goto free_hdr;
  653. }
  654. /*
  655. * Mainline code rescans the anchor header. We've done
  656. * that already so we merily copy it over.
  657. */
  658. if (pnum == fm_anchor)
  659. memcpy(vh, ubi->blockinfo + pnum, sizeof(*fm));
  660. if (i == 0) {
  661. if (be32_to_cpu(vh->vol_id) != UBI_FM_SB_VOLUME_ID) {
  662. ubi_err("bad fastmap anchor vol_id: 0x%x," \
  663. " expected: 0x%x",
  664. be32_to_cpu(vh->vol_id),
  665. UBI_FM_SB_VOLUME_ID);
  666. ret = UBI_BAD_FASTMAP;
  667. goto free_hdr;
  668. }
  669. } else {
  670. if (be32_to_cpu(vh->vol_id) != UBI_FM_DATA_VOLUME_ID) {
  671. ubi_err("bad fastmap data vol_id: 0x%x," \
  672. " expected: 0x%x",
  673. be32_to_cpu(vh->vol_id),
  674. UBI_FM_DATA_VOLUME_ID);
  675. ret = UBI_BAD_FASTMAP;
  676. goto free_hdr;
  677. }
  678. }
  679. if (sqnum < be64_to_cpu(vh->sqnum))
  680. sqnum = be64_to_cpu(vh->sqnum);
  681. ret = ubi_io_read(ubi, ubi->fm_buf + (ubi->leb_size * i), pnum,
  682. ubi->leb_start, ubi->leb_size);
  683. if (ret && ret != UBI_IO_BITFLIPS) {
  684. ubi_err("unable to read fastmap block# %i (PEB: %i, " \
  685. "err: %i)", i, pnum, ret);
  686. goto free_hdr;
  687. }
  688. }
  689. fmsb2 = (struct ubi_fm_sb *)(ubi->fm_buf);
  690. tmp_crc = be32_to_cpu(fmsb2->data_crc);
  691. fmsb2->data_crc = 0;
  692. crc = crc32(UBI_CRC32_INIT, ubi->fm_buf, fm_size);
  693. if (crc != tmp_crc) {
  694. ubi_err("fastmap data CRC is invalid");
  695. ubi_err("CRC should be: 0x%x, calc: 0x%x", tmp_crc, crc);
  696. ret = UBI_BAD_FASTMAP;
  697. goto free_hdr;
  698. }
  699. fmsb2->sqnum = sqnum;
  700. fm->used_blocks = used_blocks;
  701. ret = ubi_attach_fastmap(ubi, ai, fm);
  702. if (ret) {
  703. if (ret > 0)
  704. ret = UBI_BAD_FASTMAP;
  705. goto free_hdr;
  706. }
  707. ubi->fm = fm;
  708. ubi->fm_pool.max_size = ubi->fm->max_pool_size;
  709. ubi->fm_wl_pool.max_size = ubi->fm->max_wl_pool_size;
  710. ubi_msg("attached by fastmap %uMB %u blocks",
  711. ubi->fsize_mb, ubi->peb_count);
  712. ubi_dbg("fastmap pool size: %d", ubi->fm_pool.max_size);
  713. ubi_dbg("fastmap WL pool size: %d", ubi->fm_wl_pool.max_size);
  714. out:
  715. if (ret)
  716. ubi_err("Attach by fastmap failed, doing a full scan!");
  717. return ret;
  718. free_hdr:
  719. free_fm_sb:
  720. goto out;
  721. }
  722. /*
  723. * Scan the flash and attempt to attach via fastmap
  724. */
  725. static void ipl_scan(struct ubi_scan_info *ubi)
  726. {
  727. unsigned int pnum;
  728. int res;
  729. /*
  730. * Scan first for the fastmap super block
  731. */
  732. for (pnum = 0; pnum < UBI_FM_MAX_START; pnum++) {
  733. res = ubi_scan_vid_hdr(ubi, ubi->blockinfo + pnum, pnum);
  734. /*
  735. * We ignore errors here as we are meriliy scanning
  736. * the headers.
  737. */
  738. if (res != UBI_FASTMAP_ANCHOR)
  739. continue;
  740. /*
  741. * If fastmap is disabled, continue scanning. This
  742. * might happen because the previous attempt failed or
  743. * the caller disabled it right away.
  744. */
  745. if (!ubi->fm_enabled)
  746. continue;
  747. /*
  748. * Try to attach the fastmap, if that fails continue
  749. * scanning.
  750. */
  751. if (!ubi_scan_fastmap(ubi, NULL, pnum))
  752. return;
  753. /*
  754. * Fastmap failed. Clear everything we have and start
  755. * over. We are paranoid and do not trust anything.
  756. */
  757. memset(ubi->volinfo, 0, sizeof(ubi->volinfo));
  758. pnum = 0;
  759. break;
  760. }
  761. /*
  762. * Continue scanning, ignore errors, we might find what we are
  763. * looking for,
  764. */
  765. for (; pnum < ubi->peb_count; pnum++)
  766. ubi_scan_vid_hdr(ubi, ubi->blockinfo + pnum, pnum);
  767. }
  768. /*
  769. * Load a logical block of a volume into memory
  770. */
  771. static int ubi_load_block(struct ubi_scan_info *ubi, uint8_t *laddr,
  772. struct ubi_vol_info *vi, u32 vol_id, u32 lnum,
  773. u32 last)
  774. {
  775. struct ubi_vid_hdr *vh, *vrepl;
  776. u32 pnum, crc, dlen;
  777. retry:
  778. /*
  779. * If this is a fastmap run, we try to rescan full, otherwise
  780. * we simply give up.
  781. */
  782. if (!test_bit(lnum, vi->found)) {
  783. ubi_warn("LEB %d of %d is missing", lnum, last);
  784. return -EINVAL;
  785. }
  786. pnum = vi->lebs_to_pebs[lnum];
  787. ubi_dbg("Load vol %u LEB %u PEB %u", vol_id, lnum, pnum);
  788. if (ubi_io_is_bad(ubi, pnum)) {
  789. ubi_warn("Corrupted mapping block %d PB %d\n", lnum, pnum);
  790. return -EINVAL;
  791. }
  792. if (test_bit(pnum, ubi->corrupt))
  793. goto find_other;
  794. /*
  795. * Lets try to read that block
  796. */
  797. vh = ubi->blockinfo + pnum;
  798. if (!test_bit(pnum, ubi->scanned)) {
  799. ubi_warn("Vol: %u LEB %u PEB %u not yet scanned", vol_id,
  800. lnum, pnum);
  801. if (ubi_rescan_fm_vid_hdr(ubi, vh, pnum, vol_id, lnum))
  802. goto find_other;
  803. }
  804. /*
  805. * Check, if the total number of blocks is correct
  806. */
  807. if (be32_to_cpu(vh->used_ebs) != last) {
  808. ubi_dbg("Block count missmatch.");
  809. ubi_dbg("vh->used_ebs: %d nrblocks: %d",
  810. be32_to_cpu(vh->used_ebs), last);
  811. generic_set_bit(pnum, ubi->corrupt);
  812. goto find_other;
  813. }
  814. /*
  815. * Get the data length of this block.
  816. */
  817. dlen = be32_to_cpu(vh->data_size);
  818. /*
  819. * Read the data into RAM. We ignore the return value
  820. * here as the only thing which might go wrong are
  821. * bitflips. Try nevertheless.
  822. */
  823. ubi_io_read(ubi, laddr, pnum, ubi->leb_start, dlen);
  824. /* Calculate CRC over the data */
  825. crc = crc32(UBI_CRC32_INIT, laddr, dlen);
  826. if (crc != be32_to_cpu(vh->data_crc)) {
  827. ubi_warn("Vol: %u LEB %u PEB %u data CRC failure", vol_id,
  828. lnum, pnum);
  829. generic_set_bit(pnum, ubi->corrupt);
  830. goto find_other;
  831. }
  832. /* We are good. Return the data length we read */
  833. return dlen;
  834. find_other:
  835. ubi_dbg("Find replacement for LEB %u PEB %u", lnum, pnum);
  836. generic_clear_bit(lnum, vi->found);
  837. vrepl = NULL;
  838. for (pnum = 0; pnum < ubi->peb_count; pnum++) {
  839. struct ubi_vid_hdr *tmp = ubi->blockinfo + pnum;
  840. u32 t_vol_id = be32_to_cpu(tmp->vol_id);
  841. u32 t_lnum = be32_to_cpu(tmp->lnum);
  842. if (test_bit(pnum, ubi->corrupt))
  843. continue;
  844. if (t_vol_id != vol_id || t_lnum != lnum)
  845. continue;
  846. if (!test_bit(pnum, ubi->scanned)) {
  847. ubi_warn("Vol: %u LEB %u PEB %u not yet scanned",
  848. vol_id, lnum, pnum);
  849. if (ubi_rescan_fm_vid_hdr(ubi, tmp, pnum, vol_id, lnum))
  850. continue;
  851. }
  852. /*
  853. * We found one. If its the first, assign it otherwise
  854. * compare the sqnum
  855. */
  856. generic_set_bit(lnum, vi->found);
  857. if (!vrepl) {
  858. vrepl = tmp;
  859. continue;
  860. }
  861. if (be64_to_cpu(vrepl->sqnum) < be64_to_cpu(tmp->sqnum))
  862. vrepl = tmp;
  863. }
  864. if (vrepl) {
  865. /* Update the vi table */
  866. pnum = vrepl - ubi->blockinfo;
  867. vi->lebs_to_pebs[lnum] = pnum;
  868. ubi_dbg("Trying PEB %u for LEB %u", pnum, lnum);
  869. vh = vrepl;
  870. }
  871. goto retry;
  872. }
  873. /*
  874. * Load a volume into RAM
  875. */
  876. static int ipl_load(struct ubi_scan_info *ubi, const u32 vol_id, uint8_t *laddr)
  877. {
  878. struct ubi_vol_info *vi;
  879. u32 lnum, last, len;
  880. if (vol_id >= UBI_SPL_VOL_IDS)
  881. return -EINVAL;
  882. len = 0;
  883. vi = ubi->volinfo + vol_id;
  884. last = vi->last_block + 1;
  885. /* Read the blocks to RAM, check CRC */
  886. for (lnum = 0 ; lnum < last; lnum++) {
  887. int res = ubi_load_block(ubi, laddr, vi, vol_id, lnum, last);
  888. if (res < 0) {
  889. ubi_warn("Failed to load volume %u", vol_id);
  890. return res;
  891. }
  892. /* res is the data length of the read block */
  893. laddr += res;
  894. len += res;
  895. }
  896. return len;
  897. }
  898. int ubispl_load_volumes(struct ubispl_info *info, struct ubispl_load *lvols,
  899. int nrvols)
  900. {
  901. struct ubi_scan_info *ubi = info->ubi;
  902. int res, i, fastmap = info->fastmap;
  903. u32 fsize;
  904. retry:
  905. /*
  906. * We do a partial initializiation of @ubi. Cleaning fm_buf is
  907. * not necessary.
  908. */
  909. memset(ubi, 0, offsetof(struct ubi_scan_info, fm_buf));
  910. ubi->read = info->read;
  911. /* Precalculate the offsets */
  912. ubi->vid_offset = info->vid_offset;
  913. ubi->leb_start = info->leb_start;
  914. ubi->leb_size = info->peb_size - ubi->leb_start;
  915. ubi->peb_count = info->peb_count;
  916. ubi->peb_offset = info->peb_offset;
  917. #ifdef CONFIG_SPL_UBI_LOAD_BY_VOLNAME
  918. ubi->vtbl_valid = 0;
  919. #endif
  920. fsize = info->peb_size * info->peb_count;
  921. ubi->fsize_mb = fsize >> 20;
  922. /* Fastmap init */
  923. ubi->fm_size = ubi_calc_fm_size(ubi);
  924. ubi->fm_enabled = fastmap;
  925. for (i = 0; i < nrvols; i++) {
  926. struct ubispl_load *lv = lvols + i;
  927. generic_set_bit(lv->vol_id, ubi->toload);
  928. }
  929. ipl_scan(ubi);
  930. for (i = 0; i < nrvols; i++) {
  931. struct ubispl_load *lv = lvols + i;
  932. #ifdef CONFIG_SPL_UBI_LOAD_BY_VOLNAME
  933. if (lv->vol_id == -1) {
  934. for (int j = 0; j < UBI_SPL_VOL_IDS; j++) {
  935. int len = be16_to_cpu(ubi->vtbl[j].name_len);
  936. if (strncmp(lv->name,
  937. ubi->vtbl[j].name,
  938. len) == 0) {
  939. lv->vol_id = j;
  940. break;
  941. }
  942. }
  943. }
  944. ubi_msg("Loading VolName %s (VolId #%d)", lv->name, lv->vol_id);
  945. #else
  946. ubi_msg("Loading VolId #%d", lv->vol_id);
  947. #endif
  948. res = ipl_load(ubi, lv->vol_id, lv->load_addr);
  949. if (res < 0) {
  950. if (fastmap) {
  951. fastmap = 0;
  952. goto retry;
  953. }
  954. ubi_warn("Failed");
  955. return res;
  956. }
  957. }
  958. return 0;
  959. }