ubispl.c 27 KB

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