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