cmd_nandbcb.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551
  1. /*
  2. * i.MX nand boot control block(bcb).
  3. *
  4. * Based on the common/imx-bbu-nand-fcb.c from barebox and imx kobs-ng
  5. *
  6. * Copyright (C) 2017 Jagan Teki <jagan@amarulasolutions.com>
  7. * Copyright (C) 2016 Sergey Kubushyn <ksi@koi8.net>
  8. *
  9. * Reconstucted by Han Xu <han.xu@nxp.com>
  10. *
  11. * SPDX-License-Identifier: GPL-2.0+
  12. */
  13. #include <common.h>
  14. #include <command.h>
  15. #include <log.h>
  16. #include <malloc.h>
  17. #include <nand.h>
  18. #include <dm/devres.h>
  19. #include <linux/bug.h>
  20. #include <asm/io.h>
  21. #include <jffs2/jffs2.h>
  22. #include <linux/bch.h>
  23. #include <linux/mtd/mtd.h>
  24. #include <asm/arch/sys_proto.h>
  25. #include <asm/mach-imx/imx-nandbcb.h>
  26. #include <asm/mach-imx/imximage.cfg>
  27. #include <mxs_nand.h>
  28. #include <linux/mtd/mtd.h>
  29. #include <nand.h>
  30. #include <fuse.h>
  31. #include "../../../cmd/legacy-mtd-utils.h"
  32. /* FCB related flags */
  33. /* FCB layout with leading 12B reserved */
  34. #define FCB_LAYOUT_RESV_12B BIT(0)
  35. /* FCB layout with leading 32B meta data */
  36. #define FCB_LAYOUT_META_32B BIT(1)
  37. /* FCB encrypted by Hamming code */
  38. #define FCB_ENCODE_HAMMING BIT(2)
  39. /* FCB encrypted by 40bit BCH */
  40. #define FCB_ENCODE_BCH_40b BIT(3)
  41. /* FCB encrypted by 62bit BCH */
  42. #define FCB_ENCODE_BCH_62b BIT(4)
  43. /* FCB encrypted by BCH */
  44. #define FCB_ENCODE_BCH (FCB_ENCODE_BCH_40b | FCB_ENCODE_BCH_62b)
  45. /* FCB data was randomized */
  46. #define FCB_RANDON_ENABLED BIT(5)
  47. /* Firmware related flags */
  48. /* No 1K padding */
  49. #define FIRMWARE_NEED_PADDING BIT(8)
  50. /* Extra firmware*/
  51. #define FIRMWARE_EXTRA_ONE BIT(9)
  52. /* Secondary firmware on fixed address */
  53. #define FIRMWARE_SECONDARY_FIXED_ADDR BIT(10)
  54. /* Boot search related flags */
  55. #define BT_SEARCH_CNT_FROM_FUSE BIT(16)
  56. struct platform_config {
  57. int misc_flags;
  58. };
  59. static struct platform_config plat_config;
  60. /* imx6q/dl/solo */
  61. static struct platform_config imx6qdl_plat_config = {
  62. .misc_flags = FCB_LAYOUT_RESV_12B |
  63. FCB_ENCODE_HAMMING |
  64. FIRMWARE_NEED_PADDING,
  65. };
  66. static struct platform_config imx6sx_plat_config = {
  67. .misc_flags = FCB_LAYOUT_META_32B |
  68. FCB_ENCODE_BCH_62b |
  69. FIRMWARE_NEED_PADDING |
  70. FCB_RANDON_ENABLED,
  71. };
  72. static struct platform_config imx7d_plat_config = {
  73. .misc_flags = FCB_LAYOUT_META_32B |
  74. FCB_ENCODE_BCH_62b |
  75. FIRMWARE_NEED_PADDING |
  76. FCB_RANDON_ENABLED,
  77. };
  78. /* imx6ul/ull/ulz */
  79. static struct platform_config imx6ul_plat_config = {
  80. .misc_flags = FCB_LAYOUT_META_32B |
  81. FCB_ENCODE_BCH_40b |
  82. FIRMWARE_NEED_PADDING,
  83. };
  84. static struct platform_config imx8mq_plat_config = {
  85. .misc_flags = FCB_LAYOUT_META_32B |
  86. FCB_ENCODE_BCH_62b |
  87. FIRMWARE_NEED_PADDING |
  88. FCB_RANDON_ENABLED |
  89. FIRMWARE_EXTRA_ONE,
  90. };
  91. /* all other imx8mm */
  92. static struct platform_config imx8mm_plat_config = {
  93. .misc_flags = FCB_LAYOUT_META_32B |
  94. FCB_ENCODE_BCH_62b |
  95. FIRMWARE_NEED_PADDING |
  96. FCB_RANDON_ENABLED,
  97. };
  98. /* imx8mn */
  99. static struct platform_config imx8mn_plat_config = {
  100. .misc_flags = FCB_LAYOUT_META_32B |
  101. FCB_ENCODE_BCH_62b |
  102. FCB_RANDON_ENABLED |
  103. FIRMWARE_SECONDARY_FIXED_ADDR |
  104. BT_SEARCH_CNT_FROM_FUSE,
  105. };
  106. /* imx8qx/qm */
  107. static struct platform_config imx8q_plat_config = {
  108. .misc_flags = FCB_LAYOUT_META_32B |
  109. FCB_ENCODE_BCH_62b |
  110. FCB_RANDON_ENABLED |
  111. FIRMWARE_SECONDARY_FIXED_ADDR |
  112. BT_SEARCH_CNT_FROM_FUSE,
  113. };
  114. /* boot search related variables and definitions */
  115. static int g_boot_search_count = 4;
  116. static int g_boot_search_stride;
  117. static int g_pages_per_stride;
  118. /* mtd config structure */
  119. struct boot_config {
  120. int dev;
  121. struct mtd_info *mtd;
  122. loff_t maxsize;
  123. loff_t input_size;
  124. loff_t offset;
  125. loff_t boot_stream1_address;
  126. loff_t boot_stream2_address;
  127. size_t boot_stream1_size;
  128. size_t boot_stream2_size;
  129. size_t max_boot_stream_size;
  130. int stride_size_in_byte;
  131. int search_area_size_in_bytes;
  132. int search_area_size_in_pages;
  133. int secondary_boot_stream_off_in_MB;
  134. };
  135. /* boot_stream config structure */
  136. struct boot_stream_config {
  137. char bs_label[32];
  138. loff_t bs_addr;
  139. size_t bs_size;
  140. void *bs_buf;
  141. loff_t next_bs_addr;
  142. bool need_padding;
  143. };
  144. /* FW index */
  145. #define FW1_ONLY 1
  146. #define FW2_ONLY 2
  147. #define FW_ALL FW1_ONLY | FW2_ONLY
  148. #define FW_INX(x) (1 << (x))
  149. /* NAND convert macros */
  150. #define CONV_TO_PAGES(x) ((u32)(x) / (u32)(mtd->writesize))
  151. #define CONV_TO_BLOCKS(x) ((u32)(x) / (u32)(mtd->erasesize))
  152. #define GETBIT(v, n) (((v) >> (n)) & 0x1)
  153. #define IMX8MQ_SPL_SZ 0x3e000
  154. #define IMX8MQ_HDMI_FW_SZ 0x19c00
  155. static int nandbcb_get_info(int argc, char * const argv[],
  156. struct boot_config *boot_cfg)
  157. {
  158. int dev;
  159. struct mtd_info *mtd;
  160. dev = nand_curr_device;
  161. if (dev < 0) {
  162. printf("failed to get nand_curr_device, run nand device\n");
  163. return CMD_RET_FAILURE;
  164. }
  165. mtd = get_nand_dev_by_index(dev);
  166. if (!mtd) {
  167. printf("failed to get mtd info\n");
  168. return CMD_RET_FAILURE;
  169. }
  170. boot_cfg->dev = dev;
  171. boot_cfg->mtd = mtd;
  172. return CMD_RET_SUCCESS;
  173. }
  174. static int nandbcb_get_size(int argc, char * const argv[], int num,
  175. struct boot_config *boot_cfg)
  176. {
  177. int dev;
  178. loff_t offset, size, maxsize;
  179. struct mtd_info *mtd;
  180. dev = boot_cfg->dev;
  181. mtd = boot_cfg->mtd;
  182. size = 0;
  183. if (mtd_arg_off_size(argc - num, argv + num, &dev, &offset, &size,
  184. &maxsize, MTD_DEV_TYPE_NAND, mtd->size))
  185. return CMD_RET_FAILURE;
  186. boot_cfg->maxsize = maxsize;
  187. boot_cfg->offset = offset;
  188. debug("max: %llx, offset: %llx\n", maxsize, offset);
  189. if (size && size != maxsize)
  190. boot_cfg->input_size = size;
  191. return CMD_RET_SUCCESS;
  192. }
  193. static int nandbcb_set_boot_config(int argc, char * const argv[],
  194. struct boot_config *boot_cfg)
  195. {
  196. struct mtd_info *mtd;
  197. loff_t maxsize;
  198. loff_t boot_stream1_address, boot_stream2_address, max_boot_stream_size;
  199. if (!boot_cfg->mtd) {
  200. printf("Didn't get the mtd info, quit\n");
  201. return CMD_RET_FAILURE;
  202. }
  203. mtd = boot_cfg->mtd;
  204. /*
  205. * By default
  206. * set the search count as 4
  207. * set each FCB/DBBT/Firmware offset at the beginning of blocks
  208. * customers may change the value as needed
  209. */
  210. /* if need more compact layout, change these values */
  211. /* g_boot_search_count was set as 4 at the definition*/
  212. /* g_pages_per_stride was set as block size */
  213. g_pages_per_stride = mtd->erasesize / mtd->writesize;
  214. g_boot_search_stride = mtd->writesize * g_pages_per_stride;
  215. boot_cfg->stride_size_in_byte = g_boot_search_stride * mtd->writesize;
  216. boot_cfg->search_area_size_in_bytes =
  217. g_boot_search_count * g_boot_search_stride;
  218. boot_cfg->search_area_size_in_pages =
  219. boot_cfg->search_area_size_in_bytes / mtd->writesize;
  220. /* after FCB/DBBT, split the rest of area for two Firmwares */
  221. if (!boot_cfg->maxsize) {
  222. printf("Didn't get the maxsize, quit\n");
  223. return CMD_RET_FAILURE;
  224. }
  225. maxsize = boot_cfg->maxsize;
  226. /* align to page boundary */
  227. maxsize = ((u32)(maxsize + mtd->writesize - 1)) / (u32)mtd->writesize
  228. * mtd->writesize;
  229. boot_stream1_address = 2 * boot_cfg->search_area_size_in_bytes;
  230. boot_stream2_address = ((maxsize - boot_stream1_address) / 2 +
  231. boot_stream1_address);
  232. if (boot_cfg->secondary_boot_stream_off_in_MB)
  233. boot_stream2_address =
  234. (loff_t)boot_cfg->secondary_boot_stream_off_in_MB * 1024 * 1024;
  235. max_boot_stream_size = boot_stream2_address - boot_stream1_address;
  236. /* sanity check */
  237. if (max_boot_stream_size <= 0) {
  238. debug("st1_addr: %llx, st2_addr: %llx, max: %llx\n",
  239. boot_stream1_address, boot_stream2_address,
  240. max_boot_stream_size);
  241. printf("something wrong with firmware address settings\n");
  242. return CMD_RET_FAILURE;
  243. }
  244. boot_cfg->boot_stream1_address = boot_stream1_address;
  245. boot_cfg->boot_stream2_address = boot_stream2_address;
  246. boot_cfg->max_boot_stream_size = max_boot_stream_size;
  247. /* set the boot_stream size as the input size now */
  248. if (boot_cfg->input_size) {
  249. boot_cfg->boot_stream1_size = boot_cfg->input_size;
  250. boot_cfg->boot_stream2_size = boot_cfg->input_size;
  251. }
  252. return CMD_RET_SUCCESS;
  253. }
  254. static int nandbcb_check_space(struct boot_config *boot_cfg)
  255. {
  256. size_t maxsize = boot_cfg->maxsize;
  257. size_t max_boot_stream_size = boot_cfg->max_boot_stream_size;
  258. loff_t boot_stream2_address = boot_cfg->boot_stream2_address;
  259. if (boot_cfg->boot_stream1_size &&
  260. boot_cfg->boot_stream1_size > max_boot_stream_size) {
  261. printf("boot stream1 doesn't fit, check partition size or settings\n");
  262. return CMD_RET_FAILURE;
  263. }
  264. if (boot_cfg->boot_stream2_size &&
  265. boot_cfg->boot_stream2_size > maxsize - boot_stream2_address) {
  266. printf("boot stream2 doesn't fit, check partition size or settings\n");
  267. return CMD_RET_FAILURE;
  268. }
  269. return CMD_RET_SUCCESS;
  270. }
  271. #if defined(CONFIG_MX6UL) || defined(CONFIG_MX6ULL)
  272. static uint8_t reverse_bit(uint8_t b)
  273. {
  274. b = (b & 0xf0) >> 4 | (b & 0x0f) << 4;
  275. b = (b & 0xcc) >> 2 | (b & 0x33) << 2;
  276. b = (b & 0xaa) >> 1 | (b & 0x55) << 1;
  277. return b;
  278. }
  279. static void encode_bch_ecc(void *buf, struct fcb_block *fcb, int eccbits)
  280. {
  281. int i, j, m = 13;
  282. int blocksize = 128;
  283. int numblocks = 8;
  284. int ecc_buf_size = (m * eccbits + 7) / 8;
  285. struct bch_control *bch = init_bch(m, eccbits, 0);
  286. u8 *ecc_buf = kzalloc(ecc_buf_size, GFP_KERNEL);
  287. u8 *tmp_buf = kzalloc(blocksize * numblocks, GFP_KERNEL);
  288. u8 *psrc, *pdst;
  289. /*
  290. * The blocks here are bit aligned. If eccbits is a multiple of 8,
  291. * we just can copy bytes. Otherwiese we must move the blocks to
  292. * the next free bit position.
  293. */
  294. WARN_ON(eccbits % 8);
  295. memcpy(tmp_buf, fcb, sizeof(*fcb));
  296. for (i = 0; i < numblocks; i++) {
  297. memset(ecc_buf, 0, ecc_buf_size);
  298. psrc = tmp_buf + i * blocksize;
  299. pdst = buf + i * (blocksize + ecc_buf_size);
  300. /* copy data byte aligned to destination buf */
  301. memcpy(pdst, psrc, blocksize);
  302. /*
  303. * imx-kobs use a modified encode_bch which reverse the
  304. * bit order of the data before calculating bch.
  305. * Do this in the buffer and use the bch lib here.
  306. */
  307. for (j = 0; j < blocksize; j++)
  308. psrc[j] = reverse_bit(psrc[j]);
  309. encode_bch(bch, psrc, blocksize, ecc_buf);
  310. /* reverse ecc bit */
  311. for (j = 0; j < ecc_buf_size; j++)
  312. ecc_buf[j] = reverse_bit(ecc_buf[j]);
  313. /* Here eccbuf is byte aligned and we can just copy it */
  314. memcpy(pdst + blocksize, ecc_buf, ecc_buf_size);
  315. }
  316. kfree(ecc_buf);
  317. kfree(tmp_buf);
  318. free_bch(bch);
  319. }
  320. #else
  321. static u8 calculate_parity_13_8(u8 d)
  322. {
  323. u8 p = 0;
  324. p |= (GETBIT(d, 6) ^ GETBIT(d, 5) ^ GETBIT(d, 3) ^ GETBIT(d, 2)) << 0;
  325. p |= (GETBIT(d, 7) ^ GETBIT(d, 5) ^ GETBIT(d, 4) ^ GETBIT(d, 2) ^
  326. GETBIT(d, 1)) << 1;
  327. p |= (GETBIT(d, 7) ^ GETBIT(d, 6) ^ GETBIT(d, 5) ^ GETBIT(d, 1) ^
  328. GETBIT(d, 0)) << 2;
  329. p |= (GETBIT(d, 7) ^ GETBIT(d, 4) ^ GETBIT(d, 3) ^ GETBIT(d, 0)) << 3;
  330. p |= (GETBIT(d, 6) ^ GETBIT(d, 4) ^ GETBIT(d, 3) ^ GETBIT(d, 2) ^
  331. GETBIT(d, 1) ^ GETBIT(d, 0)) << 4;
  332. return p;
  333. }
  334. static void encode_hamming_13_8(void *_src, void *_ecc, size_t size)
  335. {
  336. int i;
  337. u8 *src = _src;
  338. u8 *ecc = _ecc;
  339. for (i = 0; i < size; i++)
  340. ecc[i] = calculate_parity_13_8(src[i]);
  341. }
  342. #endif
  343. static u32 calc_chksum(void *buf, size_t size)
  344. {
  345. u32 chksum = 0;
  346. u8 *bp = buf;
  347. size_t i;
  348. for (i = 0; i < size; i++)
  349. chksum += bp[i];
  350. return ~chksum;
  351. }
  352. static void fill_fcb(struct fcb_block *fcb, struct boot_config *boot_cfg)
  353. {
  354. struct mtd_info *mtd = boot_cfg->mtd;
  355. struct nand_chip *chip = mtd_to_nand(mtd);
  356. struct mxs_nand_info *nand_info = nand_get_controller_data(chip);
  357. struct mxs_nand_layout l;
  358. mxs_nand_get_layout(mtd, &l);
  359. fcb->fingerprint = FCB_FINGERPRINT;
  360. fcb->version = FCB_VERSION_1;
  361. fcb->datasetup = 80;
  362. fcb->datahold = 60;
  363. fcb->addr_setup = 25;
  364. fcb->dsample_time = 6;
  365. fcb->pagesize = mtd->writesize;
  366. fcb->oob_pagesize = mtd->writesize + mtd->oobsize;
  367. fcb->sectors = mtd->erasesize / mtd->writesize;
  368. fcb->meta_size = l.meta_size;
  369. fcb->nr_blocks = l.nblocks;
  370. fcb->ecc_nr = l.data0_size;
  371. fcb->ecc_level = l.ecc0;
  372. fcb->ecc_size = l.datan_size;
  373. fcb->ecc_type = l.eccn;
  374. fcb->bchtype = l.gf_len;
  375. /* DBBT search area starts from the next block after all FCB */
  376. fcb->dbbt_start = boot_cfg->search_area_size_in_pages;
  377. fcb->bb_byte = nand_info->bch_geometry.block_mark_byte_offset;
  378. fcb->bb_start_bit = nand_info->bch_geometry.block_mark_bit_offset;
  379. fcb->phy_offset = mtd->writesize;
  380. fcb->disbbm = 0;
  381. fcb->fw1_start = CONV_TO_PAGES(boot_cfg->boot_stream1_address);
  382. fcb->fw2_start = CONV_TO_PAGES(boot_cfg->boot_stream2_address);
  383. fcb->fw1_pages = CONV_TO_PAGES(boot_cfg->boot_stream1_size);
  384. fcb->fw2_pages = CONV_TO_PAGES(boot_cfg->boot_stream2_size);
  385. fcb->checksum = calc_chksum((void *)fcb + 4, sizeof(*fcb) - 4);
  386. }
  387. static int fill_dbbt_data(struct mtd_info *mtd, void *buf, int num_blocks)
  388. {
  389. int n, n_bad_blocks = 0;
  390. u32 *bb = buf + 0x8;
  391. u32 *n_bad_blocksp = buf + 0x4;
  392. for (n = 0; n < num_blocks; n++) {
  393. loff_t offset = (loff_t)n * mtd->erasesize;
  394. if (mtd_block_isbad(mtd, offset)) {
  395. n_bad_blocks++;
  396. *bb = n;
  397. bb++;
  398. }
  399. }
  400. *n_bad_blocksp = n_bad_blocks;
  401. return n_bad_blocks;
  402. }
  403. /*
  404. * return 1 - bad block
  405. * return 0 - read successfully
  406. * return < 0 - read failed
  407. */
  408. static int read_fcb(struct boot_config *boot_cfg, struct fcb_block *fcb,
  409. loff_t off)
  410. {
  411. struct mtd_info *mtd;
  412. void *fcb_raw_page;
  413. size_t size;
  414. int ret = 0;
  415. mtd = boot_cfg->mtd;
  416. if (mtd_block_isbad(mtd, off)) {
  417. printf("Block %d is bad, skipped\n", (int)CONV_TO_BLOCKS(off));
  418. return 1;
  419. }
  420. fcb_raw_page = kzalloc(mtd->writesize + mtd->oobsize, GFP_KERNEL);
  421. if (!fcb_raw_page) {
  422. debug("failed to allocate fcb_raw_page\n");
  423. ret = -ENOMEM;
  424. return ret;
  425. }
  426. /*
  427. * User BCH hardware to decode ECC for FCB
  428. */
  429. if (plat_config.misc_flags & FCB_ENCODE_BCH) {
  430. size = sizeof(struct fcb_block);
  431. /* switch nand BCH to FCB compatible settings */
  432. if (plat_config.misc_flags & FCB_ENCODE_BCH_62b)
  433. mxs_nand_mode_fcb_62bit(mtd);
  434. else if (plat_config.misc_flags & FCB_ENCODE_BCH_40b)
  435. mxs_nand_mode_fcb_40bit(mtd);
  436. ret = nand_read(mtd, off, &size, (u_char *)fcb);
  437. /* switch BCH back */
  438. mxs_nand_mode_normal(mtd);
  439. printf("NAND FCB read from 0x%llx offset 0x%zx read: %s\n",
  440. off, size, ret ? "ERROR" : "OK");
  441. } else if (plat_config.misc_flags & FCB_ENCODE_HAMMING) {
  442. /* raw read*/
  443. mtd_oob_ops_t ops = {
  444. .datbuf = (u8 *)fcb_raw_page,
  445. .oobbuf = ((u8 *)fcb_raw_page) + mtd->writesize,
  446. .len = mtd->writesize,
  447. .ooblen = mtd->oobsize,
  448. .mode = MTD_OPS_RAW
  449. };
  450. ret = mtd_read_oob(mtd, off, &ops);
  451. printf("NAND FCB read from 0x%llx offset 0x%zx read: %s\n",
  452. off, ops.len, ret ? "ERROR" : "OK");
  453. }
  454. if (ret)
  455. goto fcb_raw_page_err;
  456. if ((plat_config.misc_flags & FCB_ENCODE_HAMMING) &&
  457. (plat_config.misc_flags & FCB_LAYOUT_RESV_12B))
  458. memcpy(fcb, fcb_raw_page + 12, sizeof(struct fcb_block));
  459. /* TODO: check if it can pass Hamming check */
  460. fcb_raw_page_err:
  461. kfree(fcb_raw_page);
  462. return ret;
  463. }
  464. static int write_fcb(struct boot_config *boot_cfg, struct fcb_block *fcb)
  465. {
  466. struct mtd_info *mtd;
  467. void *fcb_raw_page = NULL;
  468. int i, ret = 0;
  469. loff_t off;
  470. size_t size;
  471. mtd = boot_cfg->mtd;
  472. /*
  473. * We prepare raw page only for i.MX6, for i.MX7 we
  474. * leverage BCH hw module instead
  475. */
  476. if ((plat_config.misc_flags & FCB_ENCODE_HAMMING) &&
  477. (plat_config.misc_flags & FCB_LAYOUT_RESV_12B)) {
  478. fcb_raw_page = kzalloc(mtd->writesize + mtd->oobsize,
  479. GFP_KERNEL);
  480. if (!fcb_raw_page) {
  481. debug("failed to allocate fcb_raw_page\n");
  482. ret = -ENOMEM;
  483. return ret;
  484. }
  485. #if defined(CONFIG_MX6UL) || defined(CONFIG_MX6ULL)
  486. /* 40 bit BCH, for i.MX6UL(L) */
  487. encode_bch_ecc(fcb_raw_page + 32, fcb, 40);
  488. #else
  489. memcpy(fcb_raw_page + 12, fcb, sizeof(struct fcb_block));
  490. encode_hamming_13_8(fcb_raw_page + 12,
  491. fcb_raw_page + 12 + 512, 512);
  492. #endif
  493. /*
  494. * Set the first and second byte of OOB data to 0xFF,
  495. * not 0x00. These bytes are used as the Manufacturers Bad
  496. * Block Marker (MBBM). Since the FCB is mostly written to
  497. * the first page in a block, a scan for
  498. * factory bad blocks will detect these blocks as bad, e.g.
  499. * when function nand_scan_bbt() is executed to build a new
  500. * bad block table.
  501. */
  502. memset(fcb_raw_page + mtd->writesize, 0xFF, 2);
  503. }
  504. /* start writing FCB from the very beginning */
  505. off = 0;
  506. for (i = 0; i < g_boot_search_count; i++) {
  507. if (mtd_block_isbad(mtd, off)) {
  508. printf("Block %d is bad, skipped\n", i);
  509. continue;
  510. }
  511. /*
  512. * User BCH hardware module to generate ECC for FCB
  513. */
  514. if (plat_config.misc_flags & FCB_ENCODE_BCH) {
  515. size = sizeof(struct fcb_block);
  516. /* switch nand BCH to FCB compatible settings */
  517. if (plat_config.misc_flags & FCB_ENCODE_BCH_62b)
  518. mxs_nand_mode_fcb_62bit(mtd);
  519. else if (plat_config.misc_flags & FCB_ENCODE_BCH_40b)
  520. mxs_nand_mode_fcb_40bit(mtd);
  521. ret = nand_write(mtd, off, &size, (u_char *)fcb);
  522. /* switch BCH back */
  523. mxs_nand_mode_normal(mtd);
  524. printf("NAND FCB write to 0x%zx offset 0x%llx written: %s\n",
  525. size, off, ret ? "ERROR" : "OK");
  526. } else if (plat_config.misc_flags & FCB_ENCODE_HAMMING) {
  527. /* raw write */
  528. mtd_oob_ops_t ops = {
  529. .datbuf = (u8 *)fcb_raw_page,
  530. .oobbuf = ((u8 *)fcb_raw_page) +
  531. mtd->writesize,
  532. .len = mtd->writesize,
  533. .ooblen = mtd->oobsize,
  534. .mode = MTD_OPS_RAW
  535. };
  536. ret = mtd_write_oob(mtd, off, &ops);
  537. printf("NAND FCB write to 0x%llxx offset 0x%zx written: %s\n", off, ops.len, ret ? "ERROR" : "OK");
  538. }
  539. if (ret)
  540. goto fcb_raw_page_err;
  541. /* next writing location */
  542. off += g_boot_search_stride;
  543. }
  544. fcb_raw_page_err:
  545. kfree(fcb_raw_page);
  546. return ret;
  547. }
  548. /*
  549. * return 1 - bad block
  550. * return 0 - read successfully
  551. * return < 0 - read failed
  552. */
  553. static int read_dbbt(struct boot_config *boot_cfg, struct dbbt_block *dbbt,
  554. void *dbbt_data_page, loff_t off)
  555. {
  556. size_t size;
  557. struct mtd_info *mtd;
  558. loff_t to;
  559. int ret;
  560. mtd = boot_cfg->mtd;
  561. if (mtd_block_isbad(mtd, off)) {
  562. printf("Block %d is bad, skipped\n",
  563. (int)CONV_TO_BLOCKS(off));
  564. return 1;
  565. }
  566. size = sizeof(struct dbbt_block);
  567. ret = nand_read(mtd, off, &size, (u_char *)dbbt);
  568. printf("NAND DBBT read from 0x%llx offset 0x%zx read: %s\n",
  569. off, size, ret ? "ERROR" : "OK");
  570. if (ret)
  571. return ret;
  572. /* dbbtpages == 0 if no bad blocks */
  573. if (dbbt->dbbtpages > 0) {
  574. to = off + 4 * mtd->writesize;
  575. size = mtd->writesize;
  576. ret = nand_read(mtd, to, &size, dbbt_data_page);
  577. printf("DBBT data read from 0x%llx offset 0x%zx read: %s\n",
  578. to, size, ret ? "ERROR" : "OK");
  579. if (ret)
  580. return ret;
  581. }
  582. return 0;
  583. }
  584. static int write_dbbt(struct boot_config *boot_cfg, struct dbbt_block *dbbt,
  585. void *dbbt_data_page)
  586. {
  587. int i;
  588. loff_t off, to;
  589. size_t size;
  590. struct mtd_info *mtd;
  591. int ret;
  592. mtd = boot_cfg->mtd;
  593. /* start writing DBBT after all FCBs */
  594. off = boot_cfg->search_area_size_in_bytes;
  595. size = mtd->writesize;
  596. for (i = 0; i < g_boot_search_count; i++) {
  597. if (mtd_block_isbad(mtd, off)) {
  598. printf("Block %d is bad, skipped\n",
  599. (int)(i + CONV_TO_BLOCKS(off)));
  600. continue;
  601. }
  602. ret = nand_write(mtd, off, &size, (u_char *)dbbt);
  603. printf("NAND DBBT write to 0x%llx offset 0x%zx written: %s\n",
  604. off, size, ret ? "ERROR" : "OK");
  605. if (ret)
  606. return ret;
  607. /* dbbtpages == 0 if no bad blocks */
  608. if (dbbt->dbbtpages > 0) {
  609. to = off + 4 * mtd->writesize;
  610. ret = nand_write(mtd, to, &size, dbbt_data_page);
  611. printf("DBBT data write to 0x%llx offset 0x%zx written: %s\n",
  612. to, size, ret ? "ERROR" : "OK");
  613. if (ret)
  614. return ret;
  615. }
  616. /* next writing location */
  617. off += g_boot_search_stride;
  618. }
  619. return 0;
  620. }
  621. /* reuse the check_skip_len from nand_util.c with minor change*/
  622. static int check_skip_length(struct boot_config *boot_cfg, loff_t offset,
  623. size_t length, size_t *used)
  624. {
  625. struct mtd_info *mtd = boot_cfg->mtd;
  626. size_t maxsize = boot_cfg->maxsize;
  627. size_t len_excl_bad = 0;
  628. int ret = 0;
  629. while (len_excl_bad < length) {
  630. size_t block_len, block_off;
  631. loff_t block_start;
  632. if (offset >= maxsize)
  633. return -1;
  634. block_start = offset & ~(loff_t)(mtd->erasesize - 1);
  635. block_off = offset & (mtd->erasesize - 1);
  636. block_len = mtd->erasesize - block_off;
  637. if (!nand_block_isbad(mtd, block_start))
  638. len_excl_bad += block_len;
  639. else
  640. ret = 1;
  641. offset += block_len;
  642. *used += block_len;
  643. }
  644. /* If the length is not a multiple of block_len, adjust. */
  645. if (len_excl_bad > length)
  646. *used -= (len_excl_bad - length);
  647. return ret;
  648. }
  649. static int nandbcb_get_next_good_blk_addr(struct boot_config *boot_cfg,
  650. struct boot_stream_config *bs_cfg)
  651. {
  652. struct mtd_info *mtd = boot_cfg->mtd;
  653. loff_t offset = bs_cfg->bs_addr;
  654. size_t length = bs_cfg->bs_size;
  655. size_t used = 0;
  656. int ret;
  657. ret = check_skip_length(boot_cfg, offset, length, &used);
  658. if (ret < 0)
  659. return ret;
  660. /* get next image address */
  661. bs_cfg->next_bs_addr = (u32)(offset + used + mtd->erasesize - 1)
  662. / (u32)mtd->erasesize * mtd->erasesize;
  663. return ret;
  664. }
  665. static int nandbcb_write_bs_skip_bad(struct boot_config *boot_cfg,
  666. struct boot_stream_config *bs_cfg)
  667. {
  668. struct mtd_info *mtd;
  669. void *buf;
  670. loff_t offset, maxsize;
  671. size_t size;
  672. size_t length;
  673. int ret;
  674. bool padding_flag = false;
  675. mtd = boot_cfg->mtd;
  676. offset = bs_cfg->bs_addr;
  677. maxsize = boot_cfg->maxsize;
  678. size = bs_cfg->bs_size;
  679. /* some boot images may need leading offset */
  680. if (bs_cfg->need_padding &&
  681. (plat_config.misc_flags & FIRMWARE_NEED_PADDING))
  682. padding_flag = 1;
  683. if (padding_flag)
  684. length = ALIGN(size + FLASH_OFFSET_STANDARD, mtd->writesize);
  685. else
  686. length = ALIGN(size, mtd->writesize);
  687. buf = kzalloc(length, GFP_KERNEL);
  688. if (!buf) {
  689. printf("failed to allocate buffer for firmware\n");
  690. ret = -ENOMEM;
  691. return ret;
  692. }
  693. if (padding_flag)
  694. memcpy(buf + FLASH_OFFSET_STANDARD, bs_cfg->bs_buf, size);
  695. else
  696. memcpy(buf, bs_cfg->bs_buf, size);
  697. ret = nand_write_skip_bad(mtd, offset, &length, NULL, maxsize,
  698. (u_char *)buf, WITH_WR_VERIFY);
  699. printf("Write %s @0x%llx offset, 0x%zx bytes written: %s\n",
  700. bs_cfg->bs_label, offset, length, ret ? "ERROR" : "OK");
  701. if (ret)
  702. /* write image failed, quit */
  703. goto err;
  704. /* get next good blk address if needed */
  705. if (bs_cfg->need_padding) {
  706. ret = nandbcb_get_next_good_blk_addr(boot_cfg, bs_cfg);
  707. if (ret < 0) {
  708. printf("Next image cannot fit in NAND partition\n");
  709. goto err;
  710. }
  711. }
  712. /* now we know how the exact image size written to NAND */
  713. bs_cfg->bs_size = length;
  714. return 0;
  715. err:
  716. kfree(buf);
  717. return ret;
  718. }
  719. static int nandbcb_write_fw(struct boot_config *boot_cfg, u_char *buf,
  720. int index)
  721. {
  722. int i;
  723. loff_t offset;
  724. size_t size;
  725. loff_t next_bs_addr;
  726. struct boot_stream_config bs_cfg;
  727. int ret;
  728. for (i = 0; i < 2; ++i) {
  729. if (!(FW_INX(i) & index))
  730. continue;
  731. if (i == 0) {
  732. offset = boot_cfg->boot_stream1_address;
  733. size = boot_cfg->boot_stream1_size;
  734. } else {
  735. offset = boot_cfg->boot_stream2_address;
  736. size = boot_cfg->boot_stream2_size;
  737. }
  738. /* write Firmware*/
  739. if (!(plat_config.misc_flags & FIRMWARE_EXTRA_ONE)) {
  740. memset(&bs_cfg, 0, sizeof(struct boot_stream_config));
  741. sprintf(bs_cfg.bs_label, "firmware%d", i);
  742. bs_cfg.bs_addr = offset;
  743. bs_cfg.bs_size = size;
  744. bs_cfg.bs_buf = buf;
  745. bs_cfg.need_padding = 1;
  746. ret = nandbcb_write_bs_skip_bad(boot_cfg, &bs_cfg);
  747. if (ret)
  748. return ret;
  749. /* update the boot stream size */
  750. if (i == 0)
  751. boot_cfg->boot_stream1_size = bs_cfg.bs_size;
  752. else
  753. boot_cfg->boot_stream2_size = bs_cfg.bs_size;
  754. } else {
  755. /* some platforms need extra firmware */
  756. memset(&bs_cfg, 0, sizeof(struct boot_stream_config));
  757. sprintf(bs_cfg.bs_label, "fw%d_part%d", i, 1);
  758. bs_cfg.bs_addr = offset;
  759. bs_cfg.bs_size = IMX8MQ_HDMI_FW_SZ;
  760. bs_cfg.bs_buf = buf;
  761. bs_cfg.need_padding = 1;
  762. ret = nandbcb_write_bs_skip_bad(boot_cfg, &bs_cfg);
  763. if (ret)
  764. return ret;
  765. /* update the boot stream size */
  766. if (i == 0)
  767. boot_cfg->boot_stream1_size = bs_cfg.bs_size;
  768. else
  769. boot_cfg->boot_stream2_size = bs_cfg.bs_size;
  770. /* get next image address */
  771. next_bs_addr = bs_cfg.next_bs_addr;
  772. memset(&bs_cfg, 0, sizeof(struct boot_stream_config));
  773. sprintf(bs_cfg.bs_label, "fw%d_part%d", i, 2);
  774. bs_cfg.bs_addr = next_bs_addr;
  775. bs_cfg.bs_size = IMX8MQ_SPL_SZ;
  776. bs_cfg.bs_buf = (u_char *)(buf + IMX8MQ_HDMI_FW_SZ);
  777. bs_cfg.need_padding = 0;
  778. ret = nandbcb_write_bs_skip_bad(boot_cfg, &bs_cfg);
  779. if (ret)
  780. return ret;
  781. }
  782. }
  783. return 0;
  784. }
  785. static int nandbcb_init(struct boot_config *boot_cfg, u_char *buf)
  786. {
  787. struct mtd_info *mtd;
  788. nand_erase_options_t opts;
  789. struct fcb_block *fcb;
  790. struct dbbt_block *dbbt;
  791. void *dbbt_page, *dbbt_data_page;
  792. int ret;
  793. loff_t maxsize, off;
  794. mtd = boot_cfg->mtd;
  795. maxsize = boot_cfg->maxsize;
  796. off = boot_cfg->offset;
  797. /* erase */
  798. memset(&opts, 0, sizeof(opts));
  799. opts.offset = off;
  800. opts.length = maxsize - 1;
  801. ret = nand_erase_opts(mtd, &opts);
  802. if (ret) {
  803. printf("%s: erase failed (ret = %d)\n", __func__, ret);
  804. return ret;
  805. }
  806. /*
  807. * Reference documentation from i.MX6DQRM section 8.5.2.2
  808. *
  809. * Nand Boot Control Block(BCB) contains two data structures,
  810. * - Firmware Configuration Block(FCB)
  811. * - Discovered Bad Block Table(DBBT)
  812. *
  813. * FCB contains,
  814. * - nand timings
  815. * - DBBT search page address,
  816. * - start page address of primary firmware
  817. * - start page address of secondary firmware
  818. *
  819. * setup fcb:
  820. * - number of blocks = mtd partition size / mtd erasesize
  821. * - two firmware blocks, primary and secondary
  822. * - first 4 block for FCB/DBBT
  823. * - rest split in half for primary and secondary firmware
  824. * - same firmware write twice
  825. */
  826. /* write Firmware*/
  827. ret = nandbcb_write_fw(boot_cfg, buf, FW_ALL);
  828. if (ret)
  829. goto err;
  830. /* fill fcb */
  831. fcb = kzalloc(sizeof(*fcb), GFP_KERNEL);
  832. if (!fcb) {
  833. debug("failed to allocate fcb\n");
  834. ret = -ENOMEM;
  835. return ret;
  836. }
  837. fill_fcb(fcb, boot_cfg);
  838. ret = write_fcb(boot_cfg, fcb);
  839. /* fill dbbt */
  840. dbbt_page = kzalloc(mtd->writesize, GFP_KERNEL);
  841. if (!dbbt_page) {
  842. debug("failed to allocate dbbt_page\n");
  843. ret = -ENOMEM;
  844. goto fcb_err;
  845. }
  846. dbbt_data_page = kzalloc(mtd->writesize, GFP_KERNEL);
  847. if (!dbbt_data_page) {
  848. debug("failed to allocate dbbt_data_page\n");
  849. ret = -ENOMEM;
  850. goto dbbt_page_err;
  851. }
  852. dbbt = dbbt_page;
  853. dbbt->checksum = 0;
  854. dbbt->fingerprint = DBBT_FINGERPRINT;
  855. dbbt->version = DBBT_VERSION_1;
  856. ret = fill_dbbt_data(mtd, dbbt_data_page, CONV_TO_BLOCKS(maxsize));
  857. if (ret < 0)
  858. goto dbbt_data_page_err;
  859. else if (ret > 0)
  860. dbbt->dbbtpages = 1;
  861. /* write dbbt */
  862. ret = write_dbbt(boot_cfg, dbbt, dbbt_data_page);
  863. if (ret < 0)
  864. printf("failed to write FCB/DBBT\n");
  865. dbbt_data_page_err:
  866. kfree(dbbt_data_page);
  867. dbbt_page_err:
  868. kfree(dbbt_page);
  869. fcb_err:
  870. kfree(fcb);
  871. err:
  872. return ret;
  873. }
  874. static int do_nandbcb_bcbonly(int argc, char *const argv[])
  875. {
  876. struct fcb_block *fcb;
  877. struct dbbt_block *dbbt;
  878. struct mtd_info *mtd;
  879. nand_erase_options_t opts;
  880. size_t maxsize;
  881. loff_t off;
  882. void *dbbt_page, *dbbt_data_page;
  883. int ret;
  884. struct boot_config cfg;
  885. if (argc < 4)
  886. return CMD_RET_USAGE;
  887. memset(&cfg, 0, sizeof(struct boot_config));
  888. if (nandbcb_get_info(argc, argv, &cfg))
  889. return CMD_RET_FAILURE;
  890. /* only get the partition info */
  891. if (nandbcb_get_size(2, argv, 1, &cfg))
  892. return CMD_RET_FAILURE;
  893. if (nandbcb_set_boot_config(argc, argv, &cfg))
  894. return CMD_RET_FAILURE;
  895. mtd = cfg.mtd;
  896. cfg.boot_stream1_address = hextoul(argv[2], NULL);
  897. cfg.boot_stream1_size = hextoul(argv[3], NULL);
  898. cfg.boot_stream1_size = ALIGN(cfg.boot_stream1_size, mtd->writesize);
  899. if (argc > 5) {
  900. cfg.boot_stream2_address = hextoul(argv[4], NULL);
  901. cfg.boot_stream2_size = hextoul(argv[5], NULL);
  902. cfg.boot_stream2_size = ALIGN(cfg.boot_stream2_size,
  903. mtd->writesize);
  904. }
  905. /* sanity check */
  906. nandbcb_check_space(&cfg);
  907. maxsize = cfg.maxsize;
  908. off = cfg.offset;
  909. /* erase the previous FCB/DBBT */
  910. memset(&opts, 0, sizeof(opts));
  911. opts.offset = off;
  912. opts.length = g_boot_search_stride * 2;
  913. ret = nand_erase_opts(mtd, &opts);
  914. if (ret) {
  915. printf("%s: erase failed (ret = %d)\n", __func__, ret);
  916. return CMD_RET_FAILURE;
  917. }
  918. /* fill fcb */
  919. fcb = kzalloc(sizeof(*fcb), GFP_KERNEL);
  920. if (!fcb) {
  921. printf("failed to allocate fcb\n");
  922. ret = -ENOMEM;
  923. return CMD_RET_FAILURE;
  924. }
  925. fill_fcb(fcb, &cfg);
  926. /* write fcb */
  927. ret = write_fcb(&cfg, fcb);
  928. /* fill dbbt */
  929. dbbt_page = kzalloc(mtd->writesize, GFP_KERNEL);
  930. if (!dbbt_page) {
  931. printf("failed to allocate dbbt_page\n");
  932. ret = -ENOMEM;
  933. goto fcb_err;
  934. }
  935. dbbt_data_page = kzalloc(mtd->writesize, GFP_KERNEL);
  936. if (!dbbt_data_page) {
  937. printf("failed to allocate dbbt_data_page\n");
  938. ret = -ENOMEM;
  939. goto dbbt_page_err;
  940. }
  941. dbbt = dbbt_page;
  942. dbbt->checksum = 0;
  943. dbbt->fingerprint = DBBT_FINGERPRINT;
  944. dbbt->version = DBBT_VERSION_1;
  945. ret = fill_dbbt_data(mtd, dbbt_data_page, CONV_TO_BLOCKS(maxsize));
  946. if (ret < 0)
  947. goto dbbt_data_page_err;
  948. else if (ret > 0)
  949. dbbt->dbbtpages = 1;
  950. /* write dbbt */
  951. ret = write_dbbt(&cfg, dbbt, dbbt_data_page);
  952. dbbt_data_page_err:
  953. kfree(dbbt_data_page);
  954. dbbt_page_err:
  955. kfree(dbbt_page);
  956. fcb_err:
  957. kfree(fcb);
  958. if (ret < 0) {
  959. printf("failed to write FCB/DBBT\n");
  960. return CMD_RET_FAILURE;
  961. }
  962. return CMD_RET_SUCCESS;
  963. }
  964. /* dump data which is read from NAND chip */
  965. void dump_structure(struct boot_config *boot_cfg, struct fcb_block *fcb,
  966. struct dbbt_block *dbbt, void *dbbt_data_page)
  967. {
  968. int i;
  969. struct mtd_info *mtd = boot_cfg->mtd;
  970. #define P1(x) printf(" %s = 0x%08x\n", #x, fcb->x)
  971. printf("FCB\n");
  972. P1(checksum);
  973. P1(fingerprint);
  974. P1(version);
  975. #undef P1
  976. #define P1(x) printf(" %s = %d\n", #x, fcb->x)
  977. P1(datasetup);
  978. P1(datahold);
  979. P1(addr_setup);
  980. P1(dsample_time);
  981. P1(pagesize);
  982. P1(oob_pagesize);
  983. P1(sectors);
  984. P1(nr_nand);
  985. P1(nr_die);
  986. P1(celltype);
  987. P1(ecc_type);
  988. P1(ecc_nr);
  989. P1(ecc_size);
  990. P1(ecc_level);
  991. P1(meta_size);
  992. P1(nr_blocks);
  993. P1(ecc_type_sdk);
  994. P1(ecc_nr_sdk);
  995. P1(ecc_size_sdk);
  996. P1(ecc_level_sdk);
  997. P1(nr_blocks_sdk);
  998. P1(meta_size_sdk);
  999. P1(erase_th);
  1000. P1(bootpatch);
  1001. P1(patch_size);
  1002. P1(fw1_start);
  1003. P1(fw2_start);
  1004. P1(fw1_pages);
  1005. P1(fw2_pages);
  1006. P1(dbbt_start);
  1007. P1(bb_byte);
  1008. P1(bb_start_bit);
  1009. P1(phy_offset);
  1010. P1(bchtype);
  1011. P1(readlatency);
  1012. P1(predelay);
  1013. P1(cedelay);
  1014. P1(postdelay);
  1015. P1(cmdaddpause);
  1016. P1(datapause);
  1017. P1(tmspeed);
  1018. P1(busytimeout);
  1019. P1(disbbm);
  1020. P1(spare_offset);
  1021. #if !defined(CONFIG_MX6) || defined(CONFIG_MX6SX) || \
  1022. defined(CONFIG_MX6UL) || defined(CONFIG_MX6ULL)
  1023. P1(onfi_sync_enable);
  1024. P1(onfi_sync_speed);
  1025. P1(onfi_sync_nand_data);
  1026. P1(disbbm_search);
  1027. P1(disbbm_search_limit);
  1028. P1(read_retry_enable);
  1029. #endif
  1030. #undef P1
  1031. #define P1(x) printf(" %s = 0x%08x\n", #x, dbbt->x)
  1032. printf("DBBT :\n");
  1033. P1(checksum);
  1034. P1(fingerprint);
  1035. P1(version);
  1036. #undef P1
  1037. #define P1(x) printf(" %s = %d\n", #x, dbbt->x)
  1038. P1(dbbtpages);
  1039. #undef P1
  1040. for (i = 0; i < dbbt->dbbtpages; ++i)
  1041. printf("%d ", *((u32 *)(dbbt_data_page + i)));
  1042. if (!(plat_config.misc_flags & FIRMWARE_EXTRA_ONE)) {
  1043. printf("Firmware: image #0 @ 0x%x size 0x%x\n",
  1044. fcb->fw1_start, fcb->fw1_pages * mtd->writesize);
  1045. printf("Firmware: image #1 @ 0x%x size 0x%x\n",
  1046. fcb->fw2_start, fcb->fw2_pages * mtd->writesize);
  1047. } else {
  1048. printf("Firmware: image #0 @ 0x%x size 0x%x\n",
  1049. fcb->fw1_start, fcb->fw1_pages * mtd->writesize);
  1050. printf("Firmware: image #1 @ 0x%x size 0x%x\n",
  1051. fcb->fw2_start, fcb->fw2_pages * mtd->writesize);
  1052. /* TODO: Add extra image information */
  1053. }
  1054. }
  1055. static bool check_fingerprint(void *data, int fingerprint)
  1056. {
  1057. int off = 4;
  1058. return (*(int *)(data + off) == fingerprint);
  1059. }
  1060. static int fuse_to_search_count(u32 bank, u32 word, u32 mask, u32 off)
  1061. {
  1062. int err;
  1063. u32 val;
  1064. int ret;
  1065. /* by default, the boot search count from fuse should be 2 */
  1066. err = fuse_read(bank, word, &val);
  1067. if (err)
  1068. return 2;
  1069. val = (val & mask) >> off;
  1070. switch (val) {
  1071. case 0:
  1072. ret = 2;
  1073. break;
  1074. case 1:
  1075. case 2:
  1076. case 3:
  1077. ret = 1 << val;
  1078. break;
  1079. default:
  1080. ret = 2;
  1081. }
  1082. return ret;
  1083. }
  1084. static int nandbcb_dump(struct boot_config *boot_cfg)
  1085. {
  1086. int i;
  1087. loff_t off;
  1088. struct mtd_info *mtd = boot_cfg->mtd;
  1089. struct fcb_block fcb, fcb_copy;
  1090. struct dbbt_block dbbt, dbbt_copy;
  1091. void *dbbt_data_page, *dbbt_data_page_copy;
  1092. bool fcb_not_found, dbbt_not_found;
  1093. int ret = 0;
  1094. dbbt_data_page = kzalloc(mtd->writesize, GFP_KERNEL);
  1095. if (!dbbt_data_page) {
  1096. printf("failed to allocate dbbt_data_page\n");
  1097. ret = -ENOMEM;
  1098. return ret;
  1099. }
  1100. dbbt_data_page_copy = kzalloc(mtd->writesize, GFP_KERNEL);
  1101. if (!dbbt_data_page_copy) {
  1102. printf("failed to allocate dbbt_data_page\n");
  1103. ret = -ENOMEM;
  1104. goto dbbt_page_err;
  1105. }
  1106. /* read fcb */
  1107. fcb_not_found = 1;
  1108. off = 0;
  1109. for (i = 0; i < g_boot_search_count; ++i) {
  1110. if (fcb_not_found) {
  1111. ret = read_fcb(boot_cfg, &fcb, off);
  1112. if (ret < 0)
  1113. goto dbbt_page_copy_err;
  1114. else if (ret == 1)
  1115. continue;
  1116. else if (ret == 0)
  1117. if (check_fingerprint(&fcb, FCB_FINGERPRINT))
  1118. fcb_not_found = 0;
  1119. } else {
  1120. ret = read_fcb(boot_cfg, &fcb_copy, off);
  1121. if (ret < 0)
  1122. goto dbbt_page_copy_err;
  1123. if (memcmp(&fcb, &fcb_copy,
  1124. sizeof(struct fcb_block))) {
  1125. printf("FCB copies are not identical\n");
  1126. ret = -EINVAL;
  1127. goto dbbt_page_copy_err;
  1128. }
  1129. }
  1130. /* next read location */
  1131. off += g_boot_search_stride;
  1132. }
  1133. /* read dbbt*/
  1134. dbbt_not_found = 1;
  1135. off = boot_cfg->search_area_size_in_bytes;
  1136. for (i = 0; i < g_boot_search_count; ++i) {
  1137. if (dbbt_not_found) {
  1138. ret = read_dbbt(boot_cfg, &dbbt, dbbt_data_page, off);
  1139. if (ret < 0)
  1140. goto dbbt_page_copy_err;
  1141. else if (ret == 1)
  1142. continue;
  1143. else if (ret == 0)
  1144. if (check_fingerprint(&dbbt, DBBT_FINGERPRINT))
  1145. dbbt_not_found = 0;
  1146. } else {
  1147. ret = read_dbbt(boot_cfg, &dbbt_copy,
  1148. dbbt_data_page_copy, off);
  1149. if (ret < 0)
  1150. goto dbbt_page_copy_err;
  1151. if (memcmp(&dbbt, &dbbt_copy,
  1152. sizeof(struct dbbt_block))) {
  1153. printf("DBBT copies are not identical\n");
  1154. ret = -EINVAL;
  1155. goto dbbt_page_copy_err;
  1156. }
  1157. if (dbbt.dbbtpages > 0 &&
  1158. memcmp(dbbt_data_page, dbbt_data_page_copy,
  1159. mtd->writesize)) {
  1160. printf("DBBT data copies are not identical\n");
  1161. ret = -EINVAL;
  1162. goto dbbt_page_copy_err;
  1163. }
  1164. }
  1165. /* next read location */
  1166. off += g_boot_search_stride;
  1167. }
  1168. dump_structure(boot_cfg, &fcb, &dbbt, dbbt_data_page);
  1169. dbbt_page_copy_err:
  1170. kfree(dbbt_data_page_copy);
  1171. dbbt_page_err:
  1172. kfree(dbbt_data_page);
  1173. return ret;
  1174. }
  1175. static int do_nandbcb_dump(int argc, char * const argv[])
  1176. {
  1177. struct boot_config cfg;
  1178. int ret;
  1179. if (argc != 2)
  1180. return CMD_RET_USAGE;
  1181. memset(&cfg, 0, sizeof(struct boot_config));
  1182. if (nandbcb_get_info(argc, argv, &cfg))
  1183. return CMD_RET_FAILURE;
  1184. if (nandbcb_get_size(argc, argv, 1, &cfg))
  1185. return CMD_RET_FAILURE;
  1186. if (nandbcb_set_boot_config(argc, argv, &cfg))
  1187. return CMD_RET_FAILURE;
  1188. ret = nandbcb_dump(&cfg);
  1189. if (ret)
  1190. return ret;
  1191. return ret;
  1192. }
  1193. static int do_nandbcb_init(int argc, char * const argv[])
  1194. {
  1195. u_char *buf;
  1196. size_t size;
  1197. loff_t addr;
  1198. char *endp;
  1199. int ret;
  1200. struct boot_config cfg;
  1201. if (argc != 4)
  1202. return CMD_RET_USAGE;
  1203. memset(&cfg, 0, sizeof(struct boot_config));
  1204. if (nandbcb_get_info(argc, argv, &cfg))
  1205. return CMD_RET_FAILURE;
  1206. if (nandbcb_get_size(argc, argv, 2, &cfg))
  1207. return CMD_RET_FAILURE;
  1208. size = cfg.boot_stream1_size;
  1209. if (nandbcb_set_boot_config(argc, argv, &cfg))
  1210. return CMD_RET_FAILURE;
  1211. addr = hextoul(argv[1], &endp);
  1212. if (*argv[1] == 0 || *endp != 0)
  1213. return CMD_RET_FAILURE;
  1214. buf = map_physmem(addr, size, MAP_WRBACK);
  1215. if (!buf) {
  1216. puts("failed to map physical memory\n");
  1217. return CMD_RET_FAILURE;
  1218. }
  1219. ret = nandbcb_init(&cfg, buf);
  1220. return ret == 0 ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
  1221. }
  1222. static int do_nandbcb(struct cmd_tbl *cmdtp, int flag, int argc,
  1223. char *const argv[])
  1224. {
  1225. const char *cmd;
  1226. int ret = 0;
  1227. if (argc < 3)
  1228. goto usage;
  1229. /* check the platform config first */
  1230. if (is_mx6sx()) {
  1231. plat_config = imx6sx_plat_config;
  1232. } else if (is_mx7()) {
  1233. plat_config = imx7d_plat_config;
  1234. } else if (is_mx6ul() || is_mx6ull()) {
  1235. plat_config = imx6ul_plat_config;
  1236. } else if (is_mx6() && !is_mx6sx() && !is_mx6ul() && !is_mx6ull()) {
  1237. plat_config = imx6qdl_plat_config;
  1238. } else if (is_imx8mq()) {
  1239. plat_config = imx8mq_plat_config;
  1240. } else if (is_imx8mm()) {
  1241. plat_config = imx8mm_plat_config;
  1242. } else if (is_imx8mn() || is_imx8mp()) {
  1243. plat_config = imx8mn_plat_config;
  1244. } else if (is_imx8qm() || is_imx8qxp()) {
  1245. plat_config = imx8q_plat_config;
  1246. } else {
  1247. printf("ERROR: Unknown platform\n");
  1248. return CMD_RET_FAILURE;
  1249. }
  1250. if ((plat_config.misc_flags) & BT_SEARCH_CNT_FROM_FUSE) {
  1251. if (is_imx8qxp())
  1252. g_boot_search_count = fuse_to_search_count(0, 720, 0xc0, 6);
  1253. if (is_imx8mn() || is_imx8mp())
  1254. g_boot_search_count = fuse_to_search_count(2, 2, 0x6000, 13);
  1255. printf("search count set to %d from fuse\n",
  1256. g_boot_search_count);
  1257. }
  1258. cmd = argv[1];
  1259. --argc;
  1260. ++argv;
  1261. if (strcmp(cmd, "init") == 0) {
  1262. ret = do_nandbcb_init(argc, argv);
  1263. goto done;
  1264. }
  1265. if (strcmp(cmd, "dump") == 0) {
  1266. ret = do_nandbcb_dump(argc, argv);
  1267. goto done;
  1268. }
  1269. if (strcmp(cmd, "bcbonly") == 0) {
  1270. ret = do_nandbcb_bcbonly(argc, argv);
  1271. goto done;
  1272. }
  1273. done:
  1274. if (ret != -1)
  1275. return ret;
  1276. usage:
  1277. return CMD_RET_USAGE;
  1278. }
  1279. #ifdef CONFIG_SYS_LONGHELP
  1280. static char nandbcb_help_text[] =
  1281. "init addr off|partition len - update 'len' bytes starting at\n"
  1282. " 'off|part' to memory address 'addr', skipping bad blocks\n"
  1283. "nandbcb bcbonly off|partition fw1-off fw1-size [fw2-off fw2-size]\n"
  1284. " - write BCB only (FCB and DBBT)\n"
  1285. " where `fwx-size` is fw sizes in bytes, `fw1-off`\n"
  1286. " and `fw2-off` - firmware offsets\n"
  1287. " FIY, BCB isn't erased automatically, so mtd erase should\n"
  1288. " be called in advance before writing new BCB:\n"
  1289. " > mtd erase mx7-bcb\n"
  1290. "nandbcb dump off|partition - dump/verify boot structures\n";
  1291. #endif
  1292. U_BOOT_CMD(nandbcb, 7, 1, do_nandbcb,
  1293. "i.MX NAND Boot Control Blocks write",
  1294. nandbcb_help_text
  1295. );