cmd_nandbcb.c 37 KB

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