create_pbl.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  1. /*
  2. * Copyright 2021-2022 NXP
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. *
  6. */
  7. #include <stdbool.h>
  8. #include <stdint.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <getopt.h>
  13. #include <unistd.h>
  14. #define NUM_MEM_BLOCK 1
  15. #define FOUR_BYTE_ALIGN 4
  16. #define EIGHT_BYTE_ALIGN 8
  17. #define SIZE_TWO_PBL_CMD 24
  18. /* Define for add_boot_ptr_cmd() */
  19. #define BOOTPTR_ADDR 0x09570604
  20. #define CSF_ADDR_SB 0x09ee0200
  21. /* CCSR write command to address 0x1e00400 i.e BOOTLOCPTR */
  22. #define BOOTPTR_ADDR_CH3 0x31e00400
  23. /* Load CSF header command */
  24. #define CSF_ADDR_SB_CH3 0x80220000
  25. #define MAND_ARG_MASK 0xFFF3
  26. #define ARG_INIT_MASK 0xFF00
  27. #define RCW_FILE_NAME_ARG_MASK 0x0080
  28. #define IN_FILE_NAME_ARG_MASK 0x0040
  29. #define CHASSIS_ARG_MASK 0x0020
  30. #define BOOT_SRC_ARG_MASK 0x0010
  31. #define ENTRY_POINT_ADDR_ARG_MASK 0x0008
  32. #define BL2_BIN_STRG_LOC_BOOT_SRC_ARG_MASK 0x0004
  33. #define BL2_BIN_CPY_DEST_ADDR_ARG_MASK 0x0002
  34. #define OP_FILE_NAME_ARG_MASK 0x0001
  35. /* Define for add_cpy_cmd() */
  36. #define OFFSET_MASK 0x00ffffff
  37. #define WRITE_CMD_BASE 0x81000000
  38. #define MAX_PBI_DATA_LEN_BYTE 64
  39. /* 140 Bytes = Preamble + LOAD RCW command + RCW (128 bytes) + Checksum */
  40. #define CHS3_CRC_PAYLOAD_START_OFFSET 140
  41. #define PBI_CRC_POLYNOMIAL 0x04c11db7
  42. typedef enum {
  43. CHASSIS_UNKNOWN,
  44. CHASSIS_2,
  45. CHASSIS_3,
  46. CHASSIS_3_2,
  47. CHASSIS_MAX /* must be last item in list */
  48. } chassis_t;
  49. typedef enum {
  50. UNKNOWN_BOOT = 0,
  51. IFC_NOR_BOOT,
  52. IFC_NAND_BOOT,
  53. QSPI_BOOT,
  54. SD_BOOT,
  55. EMMC_BOOT,
  56. FLXSPI_NOR_BOOT,
  57. FLXSPI_NAND_BOOT,
  58. FLXSPI_NAND4K_BOOT,
  59. MAX_BOOT /* must be last item in list */
  60. } boot_src_t;
  61. /* Base Addresses where PBL image is copied depending on the boot source.
  62. * Boot address map varies as per Chassis architecture.
  63. */
  64. #define BASE_ADDR_UNDEFINED 0xFFFFFFFF
  65. #define BASE_ADDR_QSPI 0x20000000
  66. #define BASE_ADDR_SD 0x00001000
  67. #define BASE_ADDR_IFC_NOR 0x30000000
  68. #define BASE_ADDR_EMMC 0x00001000
  69. #define BASE_ADDR_FLX_NOR 0x20000000
  70. #define BASE_ADDR_NAND 0x20000000
  71. uint32_t base_addr_ch3[MAX_BOOT] = {
  72. BASE_ADDR_UNDEFINED,
  73. BASE_ADDR_IFC_NOR,
  74. BASE_ADDR_UNDEFINED, /*IFC NAND */
  75. BASE_ADDR_QSPI,
  76. BASE_ADDR_SD,
  77. BASE_ADDR_EMMC,
  78. BASE_ADDR_UNDEFINED, /*FLXSPI NOR */
  79. BASE_ADDR_UNDEFINED, /*FLXSPI NAND 2K */
  80. BASE_ADDR_UNDEFINED /*FLXSPI NAND 4K */
  81. };
  82. uint32_t base_addr_ch32[MAX_BOOT] = {
  83. BASE_ADDR_UNDEFINED,
  84. BASE_ADDR_UNDEFINED, /* IFC NOR */
  85. BASE_ADDR_UNDEFINED, /* IFC NAND */
  86. BASE_ADDR_UNDEFINED, /* QSPI */
  87. BASE_ADDR_SD,
  88. BASE_ADDR_EMMC,
  89. BASE_ADDR_FLX_NOR,
  90. BASE_ADDR_UNDEFINED, /*FLXSPI NAND 2K */
  91. BASE_ADDR_UNDEFINED /*FLXSPI NAND 4K */
  92. };
  93. /* for Chassis 3 */
  94. uint32_t blk_cpy_hdr_map_ch3[] = {
  95. 0, /* Unknown Boot Source */
  96. 0x80000020, /* NOR_BOOT */
  97. 0x0, /* NAND_BOOT */
  98. 0x80000062, /* QSPI_BOOT */
  99. 0x80000040, /* SD_BOOT */
  100. 0x80000041, /* EMMC_BOOT */
  101. 0x0, /* FLEXSPI NOR_BOOT */
  102. 0x0, /* FLEX SPI NAND2K BOOT */
  103. 0x0, /* CHASIS3_2_NAND4K_BOOT */
  104. };
  105. uint32_t blk_cpy_hdr_map_ch32[] = {
  106. 0, /* Unknown Boot Source */
  107. 0x0, /* NOR_BOOT */
  108. 0x0, /* NAND_BOOT */
  109. 0x0, /* QSPI_BOOT */
  110. 0x80000008, /* SD_BOOT */
  111. 0x80000009, /* EMMC_BOOT */
  112. 0x8000000F, /* FLEXSPI NOR_BOOT */
  113. 0x8000000C, /* FLEX SPI NAND2K BOOT */
  114. 0x8000000D, /* CHASIS3_2_NAND4K_BOOT */
  115. };
  116. char *boot_src_string[] = {
  117. "UNKNOWN_BOOT",
  118. "IFC_NOR_BOOT",
  119. "IFC_NAND_BOOT",
  120. "QSPI_BOOT",
  121. "SD_BOOT",
  122. "EMMC_BOOT",
  123. "FLXSPI_NOR_BOOT",
  124. "FLXSPI_NAND_BOOT",
  125. "FLXSPI_NAND4K_BOOT",
  126. };
  127. enum stop_command {
  128. STOP_COMMAND = 0,
  129. CRC_STOP_COMMAND
  130. };
  131. /* Structure will get populated in the main function
  132. * as part of parsing the command line arguments.
  133. * All member parameters are mandatory except:
  134. * -ep
  135. * -src_addr
  136. */
  137. struct pbl_image {
  138. char *rcw_nm; /* Input RCW File */
  139. char *sec_imgnm; /* Input BL2 binary */
  140. char *imagefile; /* Generated output file */
  141. boot_src_t boot_src; /* Boot Source - QSPI, SD, NOR, NAND etc */
  142. uint32_t src_addr; /* Source Address */
  143. uint32_t addr; /* Load address */
  144. uint32_t ep; /* Entry point <opt> default is load address */
  145. chassis_t chassis; /* Chassis type */
  146. } pblimg;
  147. #define SUCCESS 0
  148. #define FAILURE -1
  149. #define CRC_STOP_CMD_ARM 0x08610040
  150. #define CRC_STOP_CMD_ARM_CH3 0x808f0000
  151. #define STOP_CMD_ARM_CH3 0x80ff0000
  152. #define BYTE_SWAP_32(word) ((((word) & 0xff000000) >> 24)| \
  153. (((word) & 0x00ff0000) >> 8) | \
  154. (((word) & 0x0000ff00) << 8) | \
  155. (((word) & 0x000000ff) << 24))
  156. #define PBI_LEN_MASK 0xFFF00000
  157. #define PBI_LEN_SHIFT 20
  158. #define NUM_RCW_WORD 35
  159. #define PBI_LEN_ADD 6
  160. #define MAX_CRC_ENTRIES 256
  161. /* SoC numeric identifier */
  162. #define SOC_LS1012 1012
  163. #define SOC_LS1023 1023
  164. #define SOC_LS1026 1026
  165. #define SOC_LS1028 1028
  166. #define SOC_LS1043 1043
  167. #define SOC_LS1046 1046
  168. #define SOC_LS1088 1088
  169. #define SOC_LS2080 2080
  170. #define SOC_LS2088 2088
  171. #define SOC_LX2160 2160
  172. static uint32_t pbl_size;
  173. bool sb_flag;
  174. /***************************************************************************
  175. * Description : CRC32 Lookup Table
  176. ***************************************************************************/
  177. static uint32_t crc32_lookup[] = {
  178. 0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA,
  179. 0x076DC419, 0x706AF48F, 0xE963A535, 0x9E6495A3,
  180. 0x0EDB8832, 0x79DCB8A4, 0xE0D5E91E, 0x97D2D988,
  181. 0x09B64C2B, 0x7EB17CBD, 0xE7B82D07, 0x90BF1D91,
  182. 0x1DB71064, 0x6AB020F2, 0xF3B97148, 0x84BE41DE,
  183. 0x1ADAD47D, 0x6DDDE4EB, 0xF4D4B551, 0x83D385C7,
  184. 0x136C9856, 0x646BA8C0, 0xFD62F97A, 0x8A65C9EC,
  185. 0x14015C4F, 0x63066CD9, 0xFA0F3D63, 0x8D080DF5,
  186. 0x3B6E20C8, 0x4C69105E, 0xD56041E4, 0xA2677172,
  187. 0x3C03E4D1, 0x4B04D447, 0xD20D85FD, 0xA50AB56B,
  188. 0x35B5A8FA, 0x42B2986C, 0xDBBBC9D6, 0xACBCF940,
  189. 0x32D86CE3, 0x45DF5C75, 0xDCD60DCF, 0xABD13D59,
  190. 0x26D930AC, 0x51DE003A, 0xC8D75180, 0xBFD06116,
  191. 0x21B4F4B5, 0x56B3C423, 0xCFBA9599, 0xB8BDA50F,
  192. 0x2802B89E, 0x5F058808, 0xC60CD9B2, 0xB10BE924,
  193. 0x2F6F7C87, 0x58684C11, 0xC1611DAB, 0xB6662D3D,
  194. 0x76DC4190, 0x01DB7106, 0x98D220BC, 0xEFD5102A,
  195. 0x71B18589, 0x06B6B51F, 0x9FBFE4A5, 0xE8B8D433,
  196. 0x7807C9A2, 0x0F00F934, 0x9609A88E, 0xE10E9818,
  197. 0x7F6A0DBB, 0x086D3D2D, 0x91646C97, 0xE6635C01,
  198. 0x6B6B51F4, 0x1C6C6162, 0x856530D8, 0xF262004E,
  199. 0x6C0695ED, 0x1B01A57B, 0x8208F4C1, 0xF50FC457,
  200. 0x65B0D9C6, 0x12B7E950, 0x8BBEB8EA, 0xFCB9887C,
  201. 0x62DD1DDF, 0x15DA2D49, 0x8CD37CF3, 0xFBD44C65,
  202. 0x4DB26158, 0x3AB551CE, 0xA3BC0074, 0xD4BB30E2,
  203. 0x4ADFA541, 0x3DD895D7, 0xA4D1C46D, 0xD3D6F4FB,
  204. 0x4369E96A, 0x346ED9FC, 0xAD678846, 0xDA60B8D0,
  205. 0x44042D73, 0x33031DE5, 0xAA0A4C5F, 0xDD0D7CC9,
  206. 0x5005713C, 0x270241AA, 0xBE0B1010, 0xC90C2086,
  207. 0x5768B525, 0x206F85B3, 0xB966D409, 0xCE61E49F,
  208. 0x5EDEF90E, 0x29D9C998, 0xB0D09822, 0xC7D7A8B4,
  209. 0x59B33D17, 0x2EB40D81, 0xB7BD5C3B, 0xC0BA6CAD,
  210. 0xEDB88320, 0x9ABFB3B6, 0x03B6E20C, 0x74B1D29A,
  211. 0xEAD54739, 0x9DD277AF, 0x04DB2615, 0x73DC1683,
  212. 0xE3630B12, 0x94643B84, 0x0D6D6A3E, 0x7A6A5AA8,
  213. 0xE40ECF0B, 0x9309FF9D, 0x0A00AE27, 0x7D079EB1,
  214. 0xF00F9344, 0x8708A3D2, 0x1E01F268, 0x6906C2FE,
  215. 0xF762575D, 0x806567CB, 0x196C3671, 0x6E6B06E7,
  216. 0xFED41B76, 0x89D32BE0, 0x10DA7A5A, 0x67DD4ACC,
  217. 0xF9B9DF6F, 0x8EBEEFF9, 0x17B7BE43, 0x60B08ED5,
  218. 0xD6D6A3E8, 0xA1D1937E, 0x38D8C2C4, 0x4FDFF252,
  219. 0xD1BB67F1, 0xA6BC5767, 0x3FB506DD, 0x48B2364B,
  220. 0xD80D2BDA, 0xAF0A1B4C, 0x36034AF6, 0x41047A60,
  221. 0xDF60EFC3, 0xA867DF55, 0x316E8EEF, 0x4669BE79,
  222. 0xCB61B38C, 0xBC66831A, 0x256FD2A0, 0x5268E236,
  223. 0xCC0C7795, 0xBB0B4703, 0x220216B9, 0x5505262F,
  224. 0xC5BA3BBE, 0xB2BD0B28, 0x2BB45A92, 0x5CB36A04,
  225. 0xC2D7FFA7, 0xB5D0CF31, 0x2CD99E8B, 0x5BDEAE1D,
  226. 0x9B64C2B0, 0xEC63F226, 0x756AA39C, 0x026D930A,
  227. 0x9C0906A9, 0xEB0E363F, 0x72076785, 0x05005713,
  228. 0x95BF4A82, 0xE2B87A14, 0x7BB12BAE, 0x0CB61B38,
  229. 0x92D28E9B, 0xE5D5BE0D, 0x7CDCEFB7, 0x0BDBDF21,
  230. 0x86D3D2D4, 0xF1D4E242, 0x68DDB3F8, 0x1FDA836E,
  231. 0x81BE16CD, 0xF6B9265B, 0x6FB077E1, 0x18B74777,
  232. 0x88085AE6, 0xFF0F6A70, 0x66063BCA, 0x11010B5C,
  233. 0x8F659EFF, 0xF862AE69, 0x616BFFD3, 0x166CCF45,
  234. 0xA00AE278, 0xD70DD2EE, 0x4E048354, 0x3903B3C2,
  235. 0xA7672661, 0xD06016F7, 0x4969474D, 0x3E6E77DB,
  236. 0xAED16A4A, 0xD9D65ADC, 0x40DF0B66, 0x37D83BF0,
  237. 0xA9BCAE53, 0xDEBB9EC5, 0x47B2CF7F, 0x30B5FFE9,
  238. 0xBDBDF21C, 0xCABAC28A, 0x53B39330, 0x24B4A3A6,
  239. 0xBAD03605, 0xCDD70693, 0x54DE5729, 0x23D967BF,
  240. 0xB3667A2E, 0xC4614AB8, 0x5D681B02, 0x2A6F2B94,
  241. 0xB40BBE37, 0xC30C8EA1, 0x5A05DF1B, 0x2D02EF8D
  242. };
  243. static void print_usage(void)
  244. {
  245. printf("\nCorrect Usage of Tool is:\n");
  246. printf("\n ./create_pbl [options] (mentioned below):\n\n");
  247. printf("\t-r <RCW file-name> - name of RCW binary file.\n");
  248. printf("\t-i <BL2 Bin file-name> - file to be added to rcw file.\n");
  249. printf("\t-c <Number> - Chassis Architecture (=2 or =3\n");
  250. printf("\t or =4 for 3.2).\n");
  251. printf("\t-b <qspi/nor/nand/sd> - Boot source.\n");
  252. printf("\t-d <Address> - Destination address where BL2\n");
  253. printf("\t image is to be copied\n");
  254. printf("\t-o <output filename> - Name of PBL image generated\n");
  255. printf("\t as an output of the tool.\n");
  256. printf("\t-f <Address> - BL2 image Src Offset\n");
  257. printf("\t on Boot Source for block copy.\n");
  258. printf("\t command for chassis >=3.)\n");
  259. printf("\t-e <Address> - [Optional] Entry Point Address\n");
  260. printf("\t of the BL2.bin\n");
  261. printf("\t-s Secure Boot.\n");
  262. printf("\t-h Help.\n");
  263. printf("\n\n");
  264. exit(0);
  265. }
  266. /***************************************************************************
  267. * Function : crypto_calculate_checksum()
  268. * Arguments : data - Pointer to FILE
  269. * num - Number of 32 bit words for checksum
  270. * Return : Checksum Value
  271. * Description : Calculate Checksum over the data
  272. ***************************************************************************/
  273. uint32_t crypto_calculate_checksum(FILE *fp_rcw_pbi_op, uint32_t num)
  274. {
  275. uint32_t i;
  276. uint64_t sum = 0;
  277. uint32_t word;
  278. fseek(fp_rcw_pbi_op, 0L, SEEK_SET);
  279. for (i = 0; i < num ; i++) {
  280. if ((fread(&word, sizeof(word), NUM_MEM_BLOCK, fp_rcw_pbi_op))
  281. < NUM_MEM_BLOCK) {
  282. printf("%s: Error reading word.\n", __func__);
  283. return FAILURE;
  284. }
  285. sum = sum + word;
  286. sum = sum & 0xFFFFFFFF;
  287. }
  288. return (uint32_t)sum;
  289. }
  290. /***************************************************************************
  291. * Function : add_pbi_stop_cmd
  292. * Arguments : fp_rcw_pbi_op - output rcw_pbi file pointer
  293. * Return : SUCCESS or FAILURE
  294. * Description : This function insert pbi stop command.
  295. ***************************************************************************/
  296. int add_pbi_stop_cmd(FILE *fp_rcw_pbi_op, enum stop_command flag)
  297. {
  298. int ret = FAILURE;
  299. int32_t pbi_stop_cmd;
  300. uint32_t pbi_crc = 0xffffffff, i, j, c;
  301. uint32_t crc_table[MAX_CRC_ENTRIES];
  302. uint8_t data;
  303. switch (pblimg.chassis) {
  304. case CHASSIS_2:
  305. pbi_stop_cmd = BYTE_SWAP_32(CRC_STOP_CMD_ARM);
  306. break;
  307. case CHASSIS_3:
  308. case CHASSIS_3_2:
  309. /* Based on flag add the corresponsding cmd
  310. * -- stop cmd or stop with CRC cmd
  311. */
  312. if (flag == CRC_STOP_COMMAND) {
  313. pbi_stop_cmd = CRC_STOP_CMD_ARM_CH3;
  314. } else {
  315. pbi_stop_cmd = STOP_CMD_ARM_CH3;
  316. }
  317. break;
  318. case CHASSIS_UNKNOWN:
  319. case CHASSIS_MAX:
  320. default:
  321. printf("Internal Error: Invalid Chassis val = %d.\n",
  322. pblimg.chassis);
  323. goto pbi_stop_err;
  324. }
  325. if (fwrite(&pbi_stop_cmd, sizeof(pbi_stop_cmd), NUM_MEM_BLOCK,
  326. fp_rcw_pbi_op) != NUM_MEM_BLOCK) {
  327. printf("%s: Error in Writing PBI STOP CMD\n", __func__);
  328. goto pbi_stop_err;
  329. }
  330. if (flag == CRC_STOP_COMMAND) {
  331. for (i = 0; i < MAX_CRC_ENTRIES; i++) {
  332. c = i << 24;
  333. for (j = 0; j < 8; j++) {
  334. c = (c & 0x80000000) ?
  335. PBI_CRC_POLYNOMIAL ^ (c << 1) : c << 1;
  336. }
  337. crc_table[i] = c;
  338. }
  339. }
  340. switch (pblimg.chassis) {
  341. case CHASSIS_2:
  342. /* Chassis 2: CRC is calculated on RCW + PBL cmd.*/
  343. fseek(fp_rcw_pbi_op, 0L, SEEK_SET);
  344. break;
  345. case CHASSIS_3:
  346. case CHASSIS_3_2:
  347. /* Chassis 3: CRC is calculated on PBL cmd only. */
  348. fseek(fp_rcw_pbi_op, CHS3_CRC_PAYLOAD_START_OFFSET, SEEK_SET);
  349. break;
  350. case CHASSIS_UNKNOWN:
  351. case CHASSIS_MAX:
  352. printf("%s: Unknown Chassis.\n", __func__);
  353. goto pbi_stop_err;
  354. }
  355. while ((fread(&data, sizeof(data), NUM_MEM_BLOCK, fp_rcw_pbi_op))
  356. == NUM_MEM_BLOCK) {
  357. if (flag == CRC_STOP_COMMAND) {
  358. if (pblimg.chassis == CHASSIS_2) {
  359. pbi_crc = crc_table
  360. [((pbi_crc >> 24) ^ (data)) & 0xff] ^
  361. (pbi_crc << 8);
  362. } else {
  363. pbi_crc = (pbi_crc >> 8) ^
  364. crc32_lookup[((pbi_crc) ^
  365. (data)) & 0xff];
  366. }
  367. }
  368. }
  369. switch (pblimg.chassis) {
  370. case CHASSIS_2:
  371. pbi_crc = BYTE_SWAP_32(pbi_crc);
  372. break;
  373. case CHASSIS_3:
  374. case CHASSIS_3_2:
  375. if (flag == CRC_STOP_COMMAND) {
  376. pbi_crc = pbi_crc ^ 0xFFFFFFFF;
  377. } else {
  378. pbi_crc = 0x00000000;
  379. }
  380. break;
  381. case CHASSIS_UNKNOWN:
  382. case CHASSIS_MAX:
  383. printf("%s: Unknown Chassis.\n", __func__);
  384. goto pbi_stop_err;
  385. }
  386. if (fwrite(&pbi_crc, sizeof(pbi_crc), NUM_MEM_BLOCK, fp_rcw_pbi_op)
  387. != NUM_MEM_BLOCK) {
  388. printf("%s: Error in Writing PBI PBI CRC\n", __func__);
  389. goto pbi_stop_err;
  390. }
  391. ret = SUCCESS;
  392. pbi_stop_err:
  393. return ret;
  394. }
  395. /*
  396. * Returns:
  397. * File size in bytes, on Success.
  398. * FAILURE, on failure.
  399. */
  400. int get_filesize(const char *c)
  401. {
  402. FILE *fp;
  403. int ret = FAILURE;
  404. fp = fopen(c, "rb");
  405. if (fp == NULL) {
  406. fprintf(stderr, "%s: Error in opening the file: %s\n",
  407. __func__, c);
  408. goto filesize_err;
  409. }
  410. fseek(fp, 0L, SEEK_END);
  411. ret = ftell(fp);
  412. fclose(fp);
  413. filesize_err:
  414. return ret;
  415. }
  416. /***************************************************************************
  417. * Function : get_bootptr
  418. * Arguments : fp_rcw_pbi_op - Pointer to output file
  419. * Return : SUCCESS or FAILURE
  420. * Description : Add bootptr pbi command to output file
  421. ***************************************************************************/
  422. int add_boot_ptr_cmd(FILE *fp_rcw_pbi_op)
  423. {
  424. uint32_t bootptr_addr;
  425. int ret = FAILURE;
  426. switch (pblimg.chassis) {
  427. case CHASSIS_2:
  428. if (sb_flag == true)
  429. bootptr_addr = BYTE_SWAP_32(CSF_ADDR_SB);
  430. else
  431. bootptr_addr = BYTE_SWAP_32(BOOTPTR_ADDR);
  432. pblimg.ep = BYTE_SWAP_32(pblimg.ep);
  433. break;
  434. case CHASSIS_3:
  435. case CHASSIS_3_2:
  436. if (sb_flag == true)
  437. bootptr_addr = CSF_ADDR_SB_CH3;
  438. else
  439. bootptr_addr = BOOTPTR_ADDR_CH3;
  440. break;
  441. case CHASSIS_UNKNOWN:
  442. case CHASSIS_MAX:
  443. default:
  444. printf("Internal Error: Invalid Chassis val = %d.\n",
  445. pblimg.chassis);
  446. goto bootptr_err;
  447. }
  448. if (fwrite(&bootptr_addr, sizeof(bootptr_addr), NUM_MEM_BLOCK,
  449. fp_rcw_pbi_op) != NUM_MEM_BLOCK) {
  450. printf("%s: Error in Writing PBI Words:[%d].\n",
  451. __func__, ret);
  452. goto bootptr_err;
  453. }
  454. if (pblimg.ep != 0) {
  455. if (fwrite(&pblimg.ep, sizeof(pblimg.ep), NUM_MEM_BLOCK,
  456. fp_rcw_pbi_op) != NUM_MEM_BLOCK) {
  457. printf("%s: Error in Writing PBI Words\n", __func__);
  458. goto bootptr_err;
  459. }
  460. }
  461. printf("\nBoot Location Pointer= 0x%x\n",
  462. pblimg.chassis == CHASSIS_2 ? BYTE_SWAP_32(pblimg.ep) :
  463. pblimg.ep);
  464. ret = SUCCESS;
  465. bootptr_err:
  466. return ret;
  467. }
  468. /***************************************************************************
  469. * Function : add_blk_cpy_cmd
  470. * Arguments : pbi_word - pointer to pbi commands
  471. * args - Command line args flag.
  472. * Return : SUCCESS or FAILURE
  473. * Description : Add pbi commands for block copy cmd in pbi_words
  474. ***************************************************************************/
  475. int add_blk_cpy_cmd(FILE *fp_rcw_pbi_op, uint16_t args)
  476. {
  477. uint32_t blk_cpy_hdr;
  478. uint32_t file_size, new_file_size;
  479. uint32_t align = 4;
  480. int ret = FAILURE;
  481. int num_pad_bytes = 0;
  482. if ((args & BL2_BIN_STRG_LOC_BOOT_SRC_ARG_MASK) == 0) {
  483. printf("ERROR: Offset not specified for Block Copy Cmd.\n");
  484. printf("\tSee Usage and use -f option\n");
  485. goto blk_copy_err;
  486. }
  487. switch (pblimg.chassis) {
  488. case CHASSIS_3:
  489. /* Block copy command */
  490. blk_cpy_hdr = blk_cpy_hdr_map_ch3[pblimg.boot_src];
  491. pblimg.src_addr += base_addr_ch3[pblimg.boot_src];
  492. break;
  493. case CHASSIS_3_2:
  494. /* Block copy command */
  495. blk_cpy_hdr = blk_cpy_hdr_map_ch32[pblimg.boot_src];
  496. pblimg.src_addr += base_addr_ch32[pblimg.boot_src];
  497. break;
  498. default:
  499. printf("%s: Error invalid chassis type for this command.\n",
  500. __func__);
  501. goto blk_copy_err;
  502. }
  503. file_size = get_filesize(pblimg.sec_imgnm);
  504. if (file_size > 0) {
  505. new_file_size = (file_size + (file_size % align));
  506. /* Add Block copy command */
  507. if (fwrite(&blk_cpy_hdr, sizeof(blk_cpy_hdr), NUM_MEM_BLOCK,
  508. fp_rcw_pbi_op) != NUM_MEM_BLOCK) {
  509. printf("%s: Error writing blk_cpy_hdr to the file.\n",
  510. __func__);
  511. goto blk_copy_err;
  512. }
  513. if ((args & BL2_BIN_STRG_LOC_BOOT_SRC_ARG_MASK) == 0)
  514. num_pad_bytes = pblimg.src_addr % 4;
  515. /* Add Src address word */
  516. if (fwrite(&pblimg.src_addr + num_pad_bytes,
  517. sizeof(pblimg.src_addr), NUM_MEM_BLOCK,
  518. fp_rcw_pbi_op) != NUM_MEM_BLOCK) {
  519. printf("%s: Error writing BLK SRC Addr to the file.\n",
  520. __func__);
  521. goto blk_copy_err;
  522. }
  523. /* Add Dest address word */
  524. if (fwrite(&pblimg.addr, sizeof(pblimg.addr),
  525. NUM_MEM_BLOCK, fp_rcw_pbi_op) != NUM_MEM_BLOCK) {
  526. printf("%s: Error writing DST Addr to the file.\n",
  527. __func__);
  528. goto blk_copy_err;
  529. }
  530. /* Add size */
  531. if (fwrite(&new_file_size, sizeof(new_file_size),
  532. NUM_MEM_BLOCK, fp_rcw_pbi_op) != NUM_MEM_BLOCK) {
  533. printf("%s: Error writing size to the file.\n",
  534. __func__);
  535. goto blk_copy_err;
  536. }
  537. }
  538. ret = SUCCESS;
  539. blk_copy_err:
  540. return ret;
  541. }
  542. /***************************************************************************
  543. * Function : add_cpy_cmd
  544. * Arguments : pbi_word - pointer to pbi commands
  545. * Return : SUCCESS or FAILURE
  546. * Description : Append pbi commands for copying BL2 image to the
  547. * load address stored in pbl_image.addr
  548. ***************************************************************************/
  549. int add_cpy_cmd(FILE *fp_rcw_pbi_op)
  550. {
  551. uint32_t ALTCBAR_ADDRESS = BYTE_SWAP_32(0x09570158);
  552. uint32_t WAIT_CMD_WRITE_ADDRESS = BYTE_SWAP_32(0x096100c0);
  553. uint32_t WAIT_CMD = BYTE_SWAP_32(0x000FFFFF);
  554. int file_size;
  555. uint32_t pbi_cmd, altcbar;
  556. uint8_t pbi_data[MAX_PBI_DATA_LEN_BYTE];
  557. uint32_t dst_offset;
  558. FILE *fp_img = NULL;
  559. int ret = FAILURE;
  560. altcbar = pblimg.addr;
  561. dst_offset = pblimg.addr;
  562. fp_img = fopen(pblimg.sec_imgnm, "rb");
  563. if (fp_img == NULL) {
  564. printf("%s: Error in opening the file: %s\n", __func__,
  565. pblimg.sec_imgnm);
  566. goto add_cpy_err;
  567. }
  568. file_size = get_filesize(pblimg.sec_imgnm);
  569. altcbar = 0xfff00000 & altcbar;
  570. altcbar = BYTE_SWAP_32(altcbar >> 16);
  571. if (fwrite(&ALTCBAR_ADDRESS, sizeof(ALTCBAR_ADDRESS), NUM_MEM_BLOCK,
  572. fp_rcw_pbi_op) != NUM_MEM_BLOCK) {
  573. printf("%s: Error in writing address of ALTCFG CMD.\n",
  574. __func__);
  575. goto add_cpy_err;
  576. }
  577. if (fwrite(&altcbar, sizeof(altcbar), NUM_MEM_BLOCK, fp_rcw_pbi_op)
  578. != NUM_MEM_BLOCK) {
  579. printf("%s: Error in writing ALTCFG CMD.\n", __func__);
  580. goto add_cpy_err;
  581. }
  582. if (fwrite(&WAIT_CMD_WRITE_ADDRESS, sizeof(WAIT_CMD_WRITE_ADDRESS),
  583. NUM_MEM_BLOCK, fp_rcw_pbi_op) != NUM_MEM_BLOCK) {
  584. printf("%s: Error in writing address of WAIT_CMD.\n",
  585. __func__);
  586. goto add_cpy_err;
  587. }
  588. if (fwrite(&WAIT_CMD, sizeof(WAIT_CMD), NUM_MEM_BLOCK, fp_rcw_pbi_op)
  589. != NUM_MEM_BLOCK) {
  590. printf("%s: Error in writing WAIT_CMD.\n", __func__);
  591. goto add_cpy_err;
  592. }
  593. do {
  594. memset(pbi_data, 0, MAX_PBI_DATA_LEN_BYTE);
  595. ret = fread(&pbi_data, MAX_PBI_DATA_LEN_BYTE,
  596. NUM_MEM_BLOCK, fp_img);
  597. if ((ret != NUM_MEM_BLOCK) && (!feof(fp_img))) {
  598. printf("%s: Error writing ALTCFG Word: [%d].\n",
  599. __func__, ret);
  600. goto add_cpy_err;
  601. }
  602. dst_offset &= OFFSET_MASK;
  603. pbi_cmd = WRITE_CMD_BASE | dst_offset;
  604. pbi_cmd = BYTE_SWAP_32(pbi_cmd);
  605. if (fwrite(&pbi_cmd, sizeof(pbi_cmd), NUM_MEM_BLOCK,
  606. fp_rcw_pbi_op) != NUM_MEM_BLOCK) {
  607. printf("%s: Error writing ALTCFG Word write cmd.\n",
  608. __func__);
  609. goto add_cpy_err;
  610. }
  611. if (fwrite(&pbi_data, MAX_PBI_DATA_LEN_BYTE, NUM_MEM_BLOCK,
  612. fp_rcw_pbi_op) != NUM_MEM_BLOCK) {
  613. printf("%s: Error writing ALTCFG_Word.\n", __func__);
  614. goto add_cpy_err;
  615. }
  616. dst_offset += MAX_PBI_DATA_LEN_BYTE;
  617. file_size -= MAX_PBI_DATA_LEN_BYTE;
  618. } while (!feof(fp_img));
  619. ret = SUCCESS;
  620. add_cpy_err:
  621. if (fp_img != NULL) {
  622. fclose(fp_img);
  623. }
  624. return ret;
  625. }
  626. int main(int argc, char **argv)
  627. {
  628. FILE *file = NULL;
  629. char *ptr;
  630. int opt;
  631. int tmp;
  632. uint16_t args = ARG_INIT_MASK;
  633. FILE *fp_rcw_pbi_ip = NULL, *fp_rcw_pbi_op = NULL;
  634. uint32_t word, word_1;
  635. int ret = FAILURE;
  636. bool bootptr_flag = false;
  637. enum stop_command flag_stop_cmd = CRC_STOP_COMMAND;
  638. /* Initializing the global structure to zero. */
  639. memset(&pblimg, 0x0, sizeof(struct pbl_image));
  640. while ((opt = getopt(argc, argv,
  641. ":b:f:r:i:e:d:c:o:h:s")) != -1) {
  642. switch (opt) {
  643. case 'd':
  644. pblimg.addr = strtoull(optarg, &ptr, 16);
  645. if (*ptr != 0) {
  646. fprintf(stderr, "CMD Error: invalid load or destination address %s\n", optarg);
  647. goto exit_main;
  648. }
  649. args |= BL2_BIN_CPY_DEST_ADDR_ARG_MASK;
  650. break;
  651. case 'r':
  652. pblimg.rcw_nm = optarg;
  653. file = fopen(pblimg.rcw_nm, "r");
  654. if (file == NULL) {
  655. printf("CMD Error: Opening the RCW File.\n");
  656. goto exit_main;
  657. } else {
  658. args |= RCW_FILE_NAME_ARG_MASK;
  659. fclose(file);
  660. }
  661. break;
  662. case 'e':
  663. bootptr_flag = true;
  664. pblimg.ep = strtoull(optarg, &ptr, 16);
  665. if (*ptr != 0) {
  666. fprintf(stderr,
  667. "CMD Error: Invalid entry point %s\n", optarg);
  668. goto exit_main;
  669. }
  670. break;
  671. case 'h':
  672. print_usage();
  673. break;
  674. case 'i':
  675. pblimg.sec_imgnm = optarg;
  676. file = fopen(pblimg.sec_imgnm, "r");
  677. if (file == NULL) {
  678. printf("CMD Error: Opening Input file.\n");
  679. goto exit_main;
  680. } else {
  681. args |= IN_FILE_NAME_ARG_MASK;
  682. fclose(file);
  683. }
  684. break;
  685. case 'c':
  686. tmp = atoi(optarg);
  687. switch (tmp) {
  688. case SOC_LS1012:
  689. case SOC_LS1023:
  690. case SOC_LS1026:
  691. case SOC_LS1043:
  692. case SOC_LS1046:
  693. pblimg.chassis = CHASSIS_2;
  694. break;
  695. case SOC_LS1088:
  696. case SOC_LS2080:
  697. case SOC_LS2088:
  698. pblimg.chassis = CHASSIS_3;
  699. break;
  700. case SOC_LS1028:
  701. case SOC_LX2160:
  702. pblimg.chassis = CHASSIS_3_2;
  703. break;
  704. default:
  705. printf("CMD Error: Invalid SoC Val = %d.\n", tmp);
  706. goto exit_main;
  707. }
  708. args |= CHASSIS_ARG_MASK;
  709. break;
  710. case 'o':
  711. pblimg.imagefile = optarg;
  712. args |= OP_FILE_NAME_ARG_MASK;
  713. break;
  714. case 's':
  715. sb_flag = true;
  716. break;
  717. case 'b':
  718. if (strcmp(optarg, "qspi") == 0) {
  719. pblimg.boot_src = QSPI_BOOT;
  720. } else if (strcmp(optarg, "nor") == 0) {
  721. pblimg.boot_src = IFC_NOR_BOOT;
  722. } else if (strcmp(optarg, "nand") == 0) {
  723. pblimg.boot_src = IFC_NAND_BOOT;
  724. } else if (strcmp(optarg, "sd") == 0) {
  725. pblimg.boot_src = SD_BOOT;
  726. } else if (strcmp(optarg, "emmc") == 0) {
  727. pblimg.boot_src = EMMC_BOOT;
  728. } else if (strcmp(optarg, "flexspi_nor") == 0) {
  729. pblimg.boot_src = FLXSPI_NOR_BOOT;
  730. } else if (strcmp(optarg, "flexspi_nand") == 0) {
  731. pblimg.boot_src = FLXSPI_NAND_BOOT;
  732. } else if (strcmp(optarg, "flexspi_nand2k") == 0) {
  733. pblimg.boot_src = FLXSPI_NAND4K_BOOT;
  734. } else {
  735. printf("CMD Error: Invalid boot source.\n");
  736. goto exit_main;
  737. }
  738. args |= BOOT_SRC_ARG_MASK;
  739. break;
  740. case 'f':
  741. pblimg.src_addr = strtoull(optarg, &ptr, 16);
  742. if (*ptr != 0) {
  743. fprintf(stderr,
  744. "CMD Error: Invalid src offset %s\n", optarg);
  745. goto exit_main;
  746. }
  747. args |= BL2_BIN_STRG_LOC_BOOT_SRC_ARG_MASK;
  748. break;
  749. default:
  750. /* issue a warning and skip the unknown arg */
  751. printf("Cmd Warning: Invalid Arg = %c.\n", opt);
  752. }
  753. }
  754. if ((args & MAND_ARG_MASK) != MAND_ARG_MASK
  755. || pblimg.rcw_nm == NULL
  756. || pblimg.imagefile == NULL) {
  757. print_usage();
  758. }
  759. fp_rcw_pbi_ip = fopen(pblimg.rcw_nm, "rb");
  760. if (fp_rcw_pbi_ip == NULL) {
  761. printf("%s: Error in opening the rcw file: %s\n",
  762. __func__, pblimg.rcw_nm);
  763. goto exit_main;
  764. }
  765. fp_rcw_pbi_op = fopen(pblimg.imagefile, "wb+");
  766. if (fp_rcw_pbi_op == NULL) {
  767. printf("%s: Error opening the input file: %s\n",
  768. __func__, pblimg.imagefile);
  769. goto exit_main;
  770. }
  771. printf("\nInput Boot Source: %s\n", boot_src_string[pblimg.boot_src]);
  772. printf("Input RCW File: %s\n", pblimg.rcw_nm);
  773. printf("Input BL2 Binary File: %s\n", pblimg.sec_imgnm);
  774. printf("Input load address for BL2 Binary File: 0x%x\n", pblimg.addr);
  775. printf("Chassis Type: %d\n", pblimg.chassis);
  776. switch (pblimg.chassis) {
  777. case CHASSIS_2:
  778. if (fread(&word, sizeof(word), NUM_MEM_BLOCK, fp_rcw_pbi_ip)
  779. != NUM_MEM_BLOCK) {
  780. printf("%s: Error in reading word from the rcw file.\n",
  781. __func__);
  782. goto exit_main;
  783. }
  784. while (BYTE_SWAP_32(word) != 0x08610040) {
  785. if (BYTE_SWAP_32(word) == 0x09550000
  786. || BYTE_SWAP_32(word) == 0x000f400c) {
  787. break;
  788. }
  789. if (fwrite(&word, sizeof(word), NUM_MEM_BLOCK,
  790. fp_rcw_pbi_op) != NUM_MEM_BLOCK) {
  791. printf("%s: [CH2] Error in Writing PBI Words\n",
  792. __func__);
  793. goto exit_main;
  794. }
  795. if (fread(&word, sizeof(word), NUM_MEM_BLOCK,
  796. fp_rcw_pbi_ip) != NUM_MEM_BLOCK) {
  797. printf("%s: [CH2] Error in Reading PBI Words\n",
  798. __func__);
  799. goto exit_main;
  800. }
  801. }
  802. if (bootptr_flag == true) {
  803. /* Add command to set boot_loc ptr */
  804. ret = add_boot_ptr_cmd(fp_rcw_pbi_op);
  805. if (ret != SUCCESS) {
  806. goto exit_main;
  807. }
  808. }
  809. /* Write acs write commands to output file */
  810. ret = add_cpy_cmd(fp_rcw_pbi_op);
  811. if (ret != SUCCESS) {
  812. goto exit_main;
  813. }
  814. /* Add stop command after adding pbi commands
  815. * For Chasis 2.0 platforms it is always CRC &
  816. * Stop command
  817. */
  818. flag_stop_cmd = CRC_STOP_COMMAND;
  819. ret = add_pbi_stop_cmd(fp_rcw_pbi_op, flag_stop_cmd);
  820. if (ret != SUCCESS) {
  821. goto exit_main;
  822. }
  823. break;
  824. case CHASSIS_3:
  825. case CHASSIS_3_2:
  826. if (fread(&word, sizeof(word), NUM_MEM_BLOCK, fp_rcw_pbi_ip)
  827. != NUM_MEM_BLOCK) {
  828. printf("%s: Error reading PBI Cmd.\n", __func__);
  829. goto exit_main;
  830. }
  831. while (word != 0x808f0000 && word != 0x80ff0000) {
  832. pbl_size++;
  833. /* 11th words in RCW has PBL length. Update it
  834. * with new length. 2 commands get added
  835. * Block copy + CCSR Write/CSF header write
  836. */
  837. if (pbl_size == 11) {
  838. word_1 = (word & PBI_LEN_MASK)
  839. + (PBI_LEN_ADD << 20);
  840. word = word & ~PBI_LEN_MASK;
  841. word = word | word_1;
  842. }
  843. /* Update the CRC command */
  844. /* Check load command..
  845. * add a check if command is Stop with CRC
  846. * or stop without checksum
  847. */
  848. if (pbl_size == 35) {
  849. word = crypto_calculate_checksum(fp_rcw_pbi_op,
  850. NUM_RCW_WORD - 1);
  851. if (word == FAILURE) {
  852. goto exit_main;
  853. }
  854. }
  855. if (fwrite(&word, sizeof(word), NUM_MEM_BLOCK,
  856. fp_rcw_pbi_op) != NUM_MEM_BLOCK) {
  857. printf("%s: [CH3] Error in Writing PBI Words\n",
  858. __func__);
  859. goto exit_main;
  860. }
  861. if (fread(&word, sizeof(word), NUM_MEM_BLOCK,
  862. fp_rcw_pbi_ip) != NUM_MEM_BLOCK) {
  863. printf("%s: [CH3] Error in Reading PBI Words\n",
  864. __func__);
  865. goto exit_main;
  866. }
  867. if (word == CRC_STOP_CMD_ARM_CH3) {
  868. flag_stop_cmd = CRC_STOP_COMMAND;
  869. } else if (word == STOP_CMD_ARM_CH3) {
  870. flag_stop_cmd = STOP_COMMAND;
  871. }
  872. }
  873. if (bootptr_flag == true) {
  874. /* Add command to set boot_loc ptr */
  875. ret = add_boot_ptr_cmd(fp_rcw_pbi_op);
  876. if (ret != SUCCESS) {
  877. printf("%s: add_boot_ptr_cmd return failure.\n",
  878. __func__);
  879. goto exit_main;
  880. }
  881. }
  882. /* Write acs write commands to output file */
  883. ret = add_blk_cpy_cmd(fp_rcw_pbi_op, args);
  884. if (ret != SUCCESS) {
  885. printf("%s: Function add_blk_cpy_cmd return failure.\n",
  886. __func__);
  887. goto exit_main;
  888. }
  889. /* Add stop command after adding pbi commands */
  890. ret = add_pbi_stop_cmd(fp_rcw_pbi_op, flag_stop_cmd);
  891. if (ret != SUCCESS) {
  892. goto exit_main;
  893. }
  894. break;
  895. default:
  896. printf("%s: Unknown chassis type.\n",
  897. __func__);
  898. }
  899. if (ret == SUCCESS) {
  900. printf("Output file successfully created with name: %s\n\n",
  901. pblimg.imagefile);
  902. }
  903. exit_main:
  904. if (fp_rcw_pbi_op != NULL) {
  905. fclose(fp_rcw_pbi_op);
  906. }
  907. if (fp_rcw_pbi_ip != NULL) {
  908. fclose(fp_rcw_pbi_ip);
  909. }
  910. return ret;
  911. }