ubispl.c 22 KB

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