generate_image.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /** @file
  2. The data structures in this code come from:
  3. OMAP35x Applications Processor Technical Reference Manual chapter 25
  4. OMAP34xx Multimedia Device Technical Reference Manual chapter 26.4.8.
  5. You should use the OMAP35x manual when possible. Some things, like SectionKey,
  6. are not defined in the OMAP35x manual and you have to use the OMAP34xx manual
  7. to find the data.
  8. Copyright (c) 2008 - 2010, Apple Inc. All rights reserved.<BR>
  9. SPDX-License-Identifier: BSD-2-Clause-Patent
  10. **/
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <errno.h>
  15. #include <sys/types.h>
  16. #include <sys/stat.h>
  17. //TOC structure as defined by OMAP35XX TRM.
  18. typedef struct {
  19. unsigned int Start;
  20. unsigned int Size;
  21. unsigned int Reserved1;
  22. unsigned int Reserved2;
  23. unsigned int Reserved3;
  24. unsigned char Filename[12];
  25. } TOC_DATA;
  26. //NOTE: OMAP3430 TRM has CHSETTINGS and CHRAM structures.
  27. typedef struct {
  28. unsigned int SectionKey;
  29. unsigned char Valid;
  30. unsigned char Version;
  31. unsigned short Reserved;
  32. unsigned int Flags;
  33. unsigned int PRM_CLKSRC_CTRL;
  34. unsigned int PRM_CLKSEL;
  35. unsigned int CM_CLKSEL1_EMU;
  36. unsigned int CM_CLKSEL_CORE;
  37. unsigned int CM_CLKSEL_WKUP;
  38. unsigned int CM_CLKEN_PLL_DPLL3;
  39. unsigned int CM_AUTOIDLE_PLL_DPLL3;
  40. unsigned int CM_CLKSEL1_PLL;
  41. unsigned int CM_CLKEN_PLL_DPLL4;
  42. unsigned int CM_AUTOIDLE_PLL_DPLL4;
  43. unsigned int CM_CLKSEL2_PLL;
  44. unsigned int CM_CLKSEL3_PLL;
  45. unsigned int CM_CLKEN_PLL_MPU;
  46. unsigned int CM_AUTOIDLE_PLL_MPU;
  47. unsigned int CM_CLKSEL1_PLL_MPU;
  48. unsigned int CM_CLKSEL2_PLL_MPU;
  49. unsigned int CM_CLKSTCTRL_MPU;
  50. } CHSETTINGS_DATA;
  51. typedef struct {
  52. unsigned int SectionKey;
  53. unsigned char Valid;
  54. unsigned char Reserved1;
  55. unsigned char Reserved2;
  56. unsigned char Reserved3;
  57. unsigned short SDRC_SYSCONFIG_LSB;
  58. unsigned short SDRC_CS_CFG_LSB;
  59. unsigned short SDRC_SHARING_LSB;
  60. unsigned short SDRC_ERR_TYPE_LSB;
  61. unsigned int SDRC_DLLA_CTRL;
  62. unsigned short Reserved4;
  63. unsigned short Reserved5;
  64. unsigned int SDRC_POWER;
  65. unsigned short MEMORY_TYPE_CS0;
  66. unsigned short Reserved6;
  67. unsigned int SDRC_MCFG_0;
  68. unsigned short SDRC_MR_0_LSB;
  69. unsigned short SDRC_EMR1_0_LSB;
  70. unsigned short SDRC_EMR2_0_LSB;
  71. unsigned short SDRC_EMR3_0_LSB;
  72. unsigned int SDRC_ACTIM_CTRLA_0;
  73. unsigned int SDRC_ACTIM_CTRLB_0;
  74. unsigned int SDRC_RFRCTRL_0;
  75. unsigned short MEMORY_TYPE_CS1;
  76. unsigned short Reserved7;
  77. unsigned int SDRC_MCFG_1;
  78. unsigned short SDRC_MR_1_LSB;
  79. unsigned short SDRC_EMR1_1_LSB;
  80. unsigned short SDRC_EMR2_1_LSB;
  81. unsigned short SDRC_EMR3_1_LSB;
  82. unsigned int SDRC_ACTIM_CTRLA_1;
  83. unsigned int SDRC_ACTIM_CTRLB_1;
  84. unsigned int SDRC_RFRCTRL_1;
  85. unsigned int Reserved8;
  86. unsigned short Flags;
  87. unsigned short Reserved9;
  88. } CHRAM_DATA;
  89. #define CHSETTINGS_START 0xA0
  90. #define CHSETTINGS_SIZE 0x50
  91. #define CHRAM_START 0xF0
  92. #define CHRAM_SIZE 0x5C
  93. #define CLOSING_TOC_ITEM_SIZE 4
  94. unsigned char gConfigurationHeader[512];
  95. unsigned int gImageExecutionAddress;
  96. char *gInputImageFile = NULL;
  97. char *gOutputImageFile = NULL;
  98. char *gDataFile = NULL;
  99. static
  100. void
  101. PrintUsage (
  102. void
  103. )
  104. {
  105. printf("Usage..\n");
  106. }
  107. static
  108. void
  109. PopulateCHSETTINGSData (
  110. FILE *DataFile,
  111. CHSETTINGS_DATA *CHSETTINGSData
  112. )
  113. {
  114. unsigned int Value;
  115. CHSETTINGSData->SectionKey = 0xC0C0C0C1;
  116. CHSETTINGSData->Valid = 0x1;
  117. CHSETTINGSData->Version = 0x1;
  118. CHSETTINGSData->Reserved = 0x00;
  119. CHSETTINGSData->Flags = 0x050001FD;
  120. //General clock settings.
  121. fscanf(DataFile, "PRM_CLKSRC_CTRL=0x%08x\n", &Value);
  122. CHSETTINGSData->PRM_CLKSRC_CTRL = Value;
  123. fscanf(DataFile, "PRM_CLKSEL=0x%08x\n", &Value);
  124. CHSETTINGSData->PRM_CLKSEL = Value;
  125. fscanf(DataFile, "CM_CLKSEL1_EMU=0x%08x\n", &Value);
  126. CHSETTINGSData->CM_CLKSEL1_EMU = Value;
  127. //Clock configuration
  128. fscanf(DataFile, "CM_CLKSEL_CORE=0x%08x\n", &Value);
  129. CHSETTINGSData->CM_CLKSEL_CORE = Value;
  130. fscanf(DataFile, "CM_CLKSEL_WKUP=0x%08x\n", &Value);
  131. CHSETTINGSData->CM_CLKSEL_WKUP = Value;
  132. //DPLL3 (Core) settings
  133. fscanf(DataFile, "CM_CLKEN_PLL_DPLL3=0x%08x\n", &Value);
  134. CHSETTINGSData->CM_CLKEN_PLL_DPLL3 = Value;
  135. fscanf(DataFile, "CM_AUTOIDLE_PLL_DPLL3=0x%08x\n", &Value);
  136. CHSETTINGSData->CM_AUTOIDLE_PLL_DPLL3 = Value;
  137. fscanf(DataFile, "CM_CLKSEL1_PLL=0x%08x\n", &Value);
  138. CHSETTINGSData->CM_CLKSEL1_PLL = Value;
  139. //DPLL4 (Peripheral) settings
  140. fscanf(DataFile, "CM_CLKEN_PLL_DPLL4=0x%08x\n", &Value);
  141. CHSETTINGSData->CM_CLKEN_PLL_DPLL4 = Value;
  142. fscanf(DataFile, "CM_AUTOIDLE_PLL_DPLL4=0x%08x\n", &Value);
  143. CHSETTINGSData->CM_AUTOIDLE_PLL_DPLL4 = Value;
  144. fscanf(DataFile, "CM_CLKSEL2_PLL=0x%08x\n", &Value);
  145. CHSETTINGSData->CM_CLKSEL2_PLL = Value;
  146. fscanf(DataFile, "CM_CLKSEL3_PLL=0x%08x\n", &Value);
  147. CHSETTINGSData->CM_CLKSEL3_PLL = Value;
  148. //DPLL1 (MPU) settings
  149. fscanf(DataFile, "CM_CLKEN_PLL_MPU=0x%08x\n", &Value);
  150. CHSETTINGSData->CM_CLKEN_PLL_MPU = Value;
  151. fscanf(DataFile, "CM_AUTOIDLE_PLL_MPU=0x%08x\n", &Value);
  152. CHSETTINGSData->CM_AUTOIDLE_PLL_MPU = Value;
  153. fscanf(DataFile, "CM_CLKSEL1_PLL_MPU=0x%08x\n", &Value);
  154. CHSETTINGSData->CM_CLKSEL1_PLL_MPU = Value;
  155. fscanf(DataFile, "CM_CLKSEL2_PLL_MPU=0x%08x\n", &Value);
  156. CHSETTINGSData->CM_CLKSEL2_PLL_MPU = Value;
  157. fscanf(DataFile, "CM_CLKSTCTRL_MPU=0x%08x\n", &Value);
  158. CHSETTINGSData->CM_CLKSTCTRL_MPU = Value;
  159. }
  160. static
  161. void
  162. PopulateCHRAMData (
  163. FILE *DataFile,
  164. CHRAM_DATA *CHRAMData
  165. )
  166. {
  167. unsigned int Value;
  168. CHRAMData->SectionKey = 0xC0C0C0C2;
  169. CHRAMData->Valid = 0x1;
  170. fscanf(DataFile, "SDRC_SYSCONFIG_LSB=0x%04x\n", &Value);
  171. CHRAMData->SDRC_SYSCONFIG_LSB = Value;
  172. fscanf(DataFile, "SDRC_CS_CFG_LSB=0x%04x\n", &Value);
  173. CHRAMData->SDRC_CS_CFG_LSB = Value;
  174. fscanf(DataFile, "SDRC_SHARING_LSB=0x%04x\n", &Value);
  175. CHRAMData->SDRC_SHARING_LSB = Value;
  176. fscanf(DataFile, "SDRC_ERR_TYPE_LSB=0x%04x\n", &Value);
  177. CHRAMData->SDRC_ERR_TYPE_LSB = Value;
  178. fscanf(DataFile, "SDRC_DLLA_CTRL=0x%08x\n", &Value);
  179. CHRAMData->SDRC_DLLA_CTRL = Value;
  180. fscanf(DataFile, "SDRC_POWER=0x%08x\n", &Value);
  181. CHRAMData->SDRC_POWER = Value;
  182. fscanf(DataFile, "MEMORY_TYPE_CS0=0x%04x\n", &Value);
  183. CHRAMData->MEMORY_TYPE_CS0 = Value;
  184. fscanf(DataFile, "SDRC_MCFG_0=0x%08x\n", &Value);
  185. CHRAMData->SDRC_MCFG_0 = Value;
  186. fscanf(DataFile, "SDRC_MR_0_LSB=0x%04x\n", &Value);
  187. CHRAMData->SDRC_MR_0_LSB = Value;
  188. fscanf(DataFile, "SDRC_EMR1_0_LSB=0x%04x\n", &Value);
  189. CHRAMData->SDRC_EMR1_0_LSB = Value;
  190. fscanf(DataFile, "SDRC_EMR2_0_LSB=0x%04x\n", &Value);
  191. CHRAMData->SDRC_EMR2_0_LSB = Value;
  192. fscanf(DataFile, "SDRC_EMR3_0_LSB=0x%04x\n", &Value);
  193. CHRAMData->SDRC_EMR3_0_LSB = Value;
  194. fscanf(DataFile, "SDRC_ACTIM_CTRLA_0=0x%08x\n", &Value);
  195. CHRAMData->SDRC_ACTIM_CTRLA_0 = Value;
  196. fscanf(DataFile, "SDRC_ACTIM_CTRLB_0=0x%08x\n", &Value);
  197. CHRAMData->SDRC_ACTIM_CTRLB_0 = Value;
  198. fscanf(DataFile, "SDRC_RFRCTRL_0=0x%08x\n", &Value);
  199. CHRAMData->SDRC_RFRCTRL_0 = Value;
  200. fscanf(DataFile, "MEMORY_TYPE_CS1=0x%04x\n", &Value);
  201. CHRAMData->MEMORY_TYPE_CS1 = Value;
  202. fscanf(DataFile, "SDRC_MCFG_1=0x%08x\n", &Value);
  203. CHRAMData->SDRC_MCFG_1 = Value;
  204. fscanf(DataFile, "SDRC_MR_1_LSB=0x%04x\n", &Value);
  205. CHRAMData->SDRC_MR_1_LSB = Value;
  206. fscanf(DataFile, "SDRC_EMR1_1_LSB=0x%04x\n", &Value);
  207. CHRAMData->SDRC_EMR1_1_LSB = Value;
  208. fscanf(DataFile, "SDRC_EMR2_1_LSB=0x%04x\n", &Value);
  209. CHRAMData->SDRC_EMR2_1_LSB = Value;
  210. fscanf(DataFile, "SDRC_EMR3_1_LSB=0x%04x\n", &Value);
  211. CHRAMData->SDRC_EMR3_1_LSB = Value;
  212. fscanf(DataFile, "SDRC_ACTIM_CTRLA_1=0x%08x\n", &Value);
  213. CHRAMData->SDRC_ACTIM_CTRLA_1 = Value;
  214. fscanf(DataFile, "SDRC_ACTIM_CTRLB_1=0x%08x\n", &Value);
  215. CHRAMData->SDRC_ACTIM_CTRLB_1 = Value;
  216. fscanf(DataFile, "SDRC_RFRCTRL_1=0x%08x\n", &Value);
  217. CHRAMData->SDRC_RFRCTRL_1 = Value;
  218. CHRAMData->Flags = 0x0003;
  219. }
  220. static
  221. void
  222. PrepareConfigurationHeader (
  223. void
  224. )
  225. {
  226. TOC_DATA Toc;
  227. CHSETTINGS_DATA CHSETTINGSData;
  228. CHRAM_DATA CHRAMData;
  229. unsigned int ConfigurationHdrOffset = 0;
  230. FILE *DataFile;
  231. // Open data file
  232. DataFile = fopen(gDataFile, "rb");
  233. if (DataFile == NULL) {
  234. fprintf(stderr, "Can't open data file %s.\n", gDataFile);
  235. exit(1);
  236. }
  237. //Initialize configuration header.
  238. memset(gConfigurationHeader, 0x00, sizeof(gConfigurationHeader));
  239. //CHSETTINGS TOC
  240. memset(&Toc, 0x00, sizeof(TOC_DATA));
  241. Toc.Start = CHSETTINGS_START;
  242. Toc.Size = CHSETTINGS_SIZE;
  243. strcpy((char *)Toc.Filename, (const char *)"CHSETTINGS");
  244. memcpy(gConfigurationHeader + ConfigurationHdrOffset, &Toc, sizeof(TOC_DATA));
  245. //Populate CHSETTINGS Data
  246. memset(&CHSETTINGSData, 0x00, sizeof(CHSETTINGS_DATA));
  247. PopulateCHSETTINGSData(DataFile, &CHSETTINGSData);
  248. memcpy(gConfigurationHeader + Toc.Start, &CHSETTINGSData, Toc.Size);
  249. //Adjust ConfigurationHdrOffset to point to next TOC
  250. ConfigurationHdrOffset += sizeof(TOC_DATA);
  251. //CHRAM TOC
  252. memset(&Toc, 0x00, sizeof(TOC_DATA));
  253. Toc.Start = CHRAM_START;
  254. Toc.Size = CHRAM_SIZE;
  255. strcpy((char *)Toc.Filename, (const char *)"CHRAM");
  256. memcpy(gConfigurationHeader + ConfigurationHdrOffset, &Toc, sizeof(TOC_DATA));
  257. //Populate CHRAM Data
  258. memset(&CHRAMData, 0x00, sizeof(CHRAM_DATA));
  259. PopulateCHRAMData(DataFile, &CHRAMData);
  260. memcpy(gConfigurationHeader + Toc.Start, &CHRAMData, Toc.Size);
  261. //Adjust ConfigurationHdrOffset to point to next TOC
  262. ConfigurationHdrOffset += sizeof(TOC_DATA);
  263. //Closing TOC item
  264. memset(gConfigurationHeader + ConfigurationHdrOffset, 0xFF, CLOSING_TOC_ITEM_SIZE);
  265. ConfigurationHdrOffset += CLOSING_TOC_ITEM_SIZE;
  266. // Close data file
  267. fclose(DataFile);
  268. }
  269. static
  270. void
  271. ConstructImage (
  272. void
  273. )
  274. {
  275. FILE *InputFile;
  276. FILE *OutputFile;
  277. unsigned int InputImageFileSize;
  278. struct stat FileStat;
  279. char Ch;
  280. unsigned int i;
  281. InputFile = fopen(gInputImageFile, "rb");
  282. if (InputFile == NULL) {
  283. fprintf(stderr, "Can't open input file.\n");
  284. exit(0);
  285. }
  286. // Get the size of the input image.
  287. fstat(fileno(InputFile), &FileStat);
  288. InputImageFileSize = FileStat.st_size;
  289. OutputFile = fopen(gOutputImageFile, "wb");
  290. if (OutputFile == NULL) {
  291. fprintf(stderr, "Can't open output file %s.\n", gOutputImageFile);
  292. exit(0);
  293. }
  294. // Write Configuration header
  295. fwrite(gConfigurationHeader, 1, sizeof(gConfigurationHeader), OutputFile);
  296. // Write image header (Input image size, execution address)
  297. fwrite(&InputImageFileSize, 1, 4, OutputFile);
  298. fwrite(&gImageExecutionAddress, 1, 4, OutputFile);
  299. // Copy input image to the output file.
  300. for (i = 0; i < InputImageFileSize; i++) {
  301. fread(&Ch, 1, 1, InputFile);
  302. fwrite(&Ch, 1, 1, OutputFile);
  303. }
  304. fclose(InputFile);
  305. fclose(OutputFile);
  306. }
  307. int
  308. main (
  309. int argc,
  310. char** argv
  311. )
  312. {
  313. char Ch;
  314. unsigned char *ptr;
  315. int i;
  316. int TwoArg;
  317. if (argc == 1) {
  318. PrintUsage ();
  319. exit(1);
  320. }
  321. for (i=1; i < argc; i++) {
  322. if (argv[i][0] == '-') {
  323. // TwoArg TRUE -E 0x123, FALSE -E0x1234
  324. TwoArg = (argv[i][2] != ' ');
  325. switch (argv[i][1]) {
  326. case 'E': /* Image execution address */
  327. gImageExecutionAddress = strtoul (TwoArg ? argv[i+1] : &argv[i][2], (char **)&ptr, 16);
  328. break;
  329. case 'I': /* Input image file */
  330. gInputImageFile = TwoArg ? argv[i+1] : &argv[i][2];
  331. break;
  332. case 'O': /* Output image file */
  333. gOutputImageFile = TwoArg ? argv[i+1] : &argv[i][2];
  334. break;
  335. case 'D': /* Data file */
  336. gDataFile = TwoArg ? argv[i+1] : &argv[i][2];
  337. break;
  338. default:
  339. abort ();
  340. }
  341. }
  342. }
  343. //Prepare configuration header
  344. PrepareConfigurationHeader ();
  345. //Build image with configuration header + image header + image
  346. ConstructImage ();
  347. return 0;
  348. }