summaryrefslogtreecommitdiff
path: root/BeagleBoardPkg/Tools/generate_image.c
blob: c1aebfef575721c28545428da5f89ebd182ec654 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/** @file

  Copyright (c) 2008-2009, Apple Inc. All rights reserved.
  
  All rights reserved. This program and the accompanying materials
  are licensed and made available under the terms and conditions of the BSD License
  which accompanies this distribution.  The full text of the license may be found at
  http://opensource.org/licenses/bsd-license.php

  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.

**/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>



//TOC structure as defined by OMAP35XX TRM.
typedef struct {
  unsigned int  Start;
  unsigned int  Size;
  unsigned int  Reserved1;
  unsigned int  Reserved2;
  unsigned int  Reserved3;
  unsigned char Filename[12];
} TOC_DATA;

//NOTE: OMAP3430 TRM has CHSETTINGS and CHRAM structures.
typedef struct {
  unsigned int   SectionKey;
  unsigned char  Valid;
  unsigned char  Version;
  unsigned short Reserved;
  unsigned int   Flags;
  unsigned int   PRM_CLKSRC_CTRL; 
  unsigned int   PRM_CLKSEL; 
  unsigned int   CM_CLKSEL1_EMU;
  unsigned int   CM_CLKSEL_CORE;
  unsigned int   CM_CLKSEL_WKUP;
  unsigned int   CM_CLKEN_PLL_DPLL3;
  unsigned int   CM_AUTOIDLE_PLL_DPLL3; 
  unsigned int   CM_CLKSEL1_PLL; 
  unsigned int   CM_CLKEN_PLL_DPLL4;
  unsigned int   CM_AUTOIDLE_PLL_DPLL4;
  unsigned int   CM_CLKSEL2_PLL;
  unsigned int   CM_CLKSEL3_PLL; 
  unsigned int   CM_CLKEN_PLL_MPU; 
  unsigned int   CM_AUTOIDLE_PLL_MPU; 
  unsigned int   CM_CLKSEL1_PLL_MPU; 
  unsigned int   CM_CLKSEL2_PLL_MPU; 
  unsigned int   CM_CLKSTCTRL_MPU;
} CHSETTINGS_DATA;

typedef struct {
  unsigned int   SectionKey;
  unsigned char  Valid;
  unsigned char  Reserved1;
  unsigned char  Reserved2;
  unsigned char  Reserved3;
  unsigned short SDRC_SYSCONFIG_LSB;
  unsigned short SDRC_CS_CFG_LSB;
  unsigned short SDRC_SHARING_LSB;
  unsigned short SDRC_ERR_TYPE_LSB;
  unsigned int   SDRC_DLLA_CTRL;
  unsigned short Reserved4;
  unsigned short Reserved5;
  unsigned int   SDRC_POWER;
  unsigned short MEMORY_TYPE_CS0;
  unsigned short Reserved6;
  unsigned int   SDRC_MCFG_0;
  unsigned short SDRC_MR_0_LSB;
  unsigned short SDRC_EMR1_0_LSB;
  unsigned short SDRC_EMR2_0_LSB;
  unsigned short SDRC_EMR3_0_LSB;
  unsigned int   SDRC_ACTIM_CTRLA_0;
  unsigned int   SDRC_ACTIM_CTRLB_0;
  unsigned int   SDRC_RFRCTRL_0;
  unsigned short MEMORY_TYPE_CS1;
  unsigned short Reserved7;
  unsigned int   SDRC_MCFG_1;
  unsigned short SDRC_MR_1_LSB;
  unsigned short SDRC_EMR1_1_LSB;
  unsigned short SDRC_EMR2_1_LSB;
  unsigned short SDRC_EMR3_1_LSB;
  unsigned int   SDRC_ACTIM_CTRLA_1;
  unsigned int   SDRC_ACTIM_CTRLB_1;
  unsigned int   SDRC_RFRCTRL_1;
  unsigned int   Reserved8;
  unsigned short Flags;
  unsigned short Reserved9;
} CHRAM_DATA;

#define CHSETTINGS_START      0xA0
#define CHSETTINGS_SIZE       0x50
#define CHRAM_START           0xF0
#define CHRAM_SIZE            0x5C
#define CLOSING_TOC_ITEM_SIZE 4

unsigned char gConfigurationHeader[512];
unsigned int  gImageExecutionAddress;
char          *gInputImageFile = NULL;
char          *gOutputImageFile = NULL;
char          *gDataFile = NULL;

static
void
PrintUsage (
  void
  )
{
  printf("Usage..\n");
}

static
void
PopulateCHSETTINGSData (
  FILE            *DataFile,
  CHSETTINGS_DATA *CHSETTINGSData
  )
{
  unsigned int Value;
  
  CHSETTINGSData->SectionKey            = 0xC0C0C0C1;
  CHSETTINGSData->Valid                 = 0x1;
  CHSETTINGSData->Version               = 0x1;
  CHSETTINGSData->Reserved              = 0x00;
  CHSETTINGSData->Flags                 = 0x050001FD;

  //General clock settings.
  fscanf(DataFile, "PRM_CLKSRC_CTRL=0x%08x\n", &Value);
  CHSETTINGSData->PRM_CLKSRC_CTRL = Value;
  fscanf(DataFile, "PRM_CLKSEL=0x%08x\n", &Value);
  CHSETTINGSData->PRM_CLKSEL = Value;
  fscanf(DataFile, "CM_CLKSEL1_EMU=0x%08x\n", &Value);
  CHSETTINGSData->CM_CLKSEL1_EMU = Value;

  //Clock configuration
  fscanf(DataFile, "CM_CLKSEL_CORE=0x%08x\n", &Value);
  CHSETTINGSData->CM_CLKSEL_CORE = Value;
  fscanf(DataFile, "CM_CLKSEL_WKUP=0x%08x\n", &Value);
  CHSETTINGSData->CM_CLKSEL_WKUP = Value;

  //DPLL3 (Core) settings
  fscanf(DataFile, "CM_CLKEN_PLL_DPLL3=0x%08x\n", &Value);
  CHSETTINGSData->CM_CLKEN_PLL_DPLL3 = Value;
  fscanf(DataFile, "CM_AUTOIDLE_PLL_DPLL3=0x%08x\n", &Value);
  CHSETTINGSData->CM_AUTOIDLE_PLL_DPLL3 = Value;
  fscanf(DataFile, "CM_CLKSEL1_PLL=0x%08x\n", &Value);
  CHSETTINGSData->CM_CLKSEL1_PLL = Value;

  //DPLL4 (Peripheral) settings
  fscanf(DataFile, "CM_CLKEN_PLL_DPLL4=0x%08x\n", &Value);
  CHSETTINGSData->CM_CLKEN_PLL_DPLL4 = Value;
  fscanf(DataFile, "CM_AUTOIDLE_PLL_DPLL4=0x%08x\n", &Value);
  CHSETTINGSData->CM_AUTOIDLE_PLL_DPLL4 = Value;
  fscanf(DataFile, "CM_CLKSEL2_PLL=0x%08x\n", &Value);
  CHSETTINGSData->CM_CLKSEL2_PLL = Value;
  fscanf(DataFile, "CM_CLKSEL3_PLL=0x%08x\n", &Value);
  CHSETTINGSData->CM_CLKSEL3_PLL = Value;

  //DPLL1 (MPU) settings
  fscanf(DataFile, "CM_CLKEN_PLL_MPU=0x%08x\n", &Value);
  CHSETTINGSData->CM_CLKEN_PLL_MPU = Value;
  fscanf(DataFile, "CM_AUTOIDLE_PLL_MPU=0x%08x\n", &Value);
  CHSETTINGSData->CM_AUTOIDLE_PLL_MPU = Value;
  fscanf(DataFile, "CM_CLKSEL1_PLL_MPU=0x%08x\n", &Value);
  CHSETTINGSData->CM_CLKSEL1_PLL_MPU = Value;
  fscanf(DataFile, "CM_CLKSEL2_PLL_MPU=0x%08x\n", &Value);
  CHSETTINGSData->CM_CLKSEL2_PLL_MPU = Value;
  fscanf(DataFile, "CM_CLKSTCTRL_MPU=0x%08x\n", &Value);
  CHSETTINGSData->CM_CLKSTCTRL_MPU = Value;
}

static
void
PopulateCHRAMData (
  FILE       *DataFile,
  CHRAM_DATA *CHRAMData
  )
{
  unsigned int Value;
  
  CHRAMData->SectionKey         = 0xC0C0C0C2;
  CHRAMData->Valid              = 0x1;
  
  fscanf(DataFile, "SDRC_SYSCONFIG_LSB=0x%04x\n", &Value);
  CHRAMData->SDRC_SYSCONFIG_LSB = Value;
  fscanf(DataFile, "SDRC_CS_CFG_LSB=0x%04x\n", &Value);
  CHRAMData->SDRC_CS_CFG_LSB    = Value;
  fscanf(DataFile, "SDRC_SHARING_LSB=0x%04x\n", &Value);
  CHRAMData->SDRC_SHARING_LSB   = Value;
  fscanf(DataFile, "SDRC_ERR_TYPE_LSB=0x%04x\n", &Value);
  CHRAMData->SDRC_ERR_TYPE_LSB  = Value;
  fscanf(DataFile, "SDRC_DLLA_CTRL=0x%08x\n", &Value);
  CHRAMData->SDRC_DLLA_CTRL     = Value;
  fscanf(DataFile, "SDRC_POWER=0x%08x\n", &Value);
  CHRAMData->SDRC_POWER         = Value;
  fscanf(DataFile, "MEMORY_TYPE_CS0=0x%04x\n", &Value);
  CHRAMData->MEMORY_TYPE_CS0    = Value;
  fscanf(DataFile, "SDRC_MCFG_0=0x%08x\n", &Value);
  CHRAMData->SDRC_MCFG_0        = Value;
  fscanf(DataFile, "SDRC_MR_0_LSB=0x%04x\n", &Value);
  CHRAMData->SDRC_MR_0_LSB      = Value;
  fscanf(DataFile, "SDRC_EMR1_0_LSB=0x%04x\n", &Value);
  CHRAMData->SDRC_EMR1_0_LSB    = Value;
  fscanf(DataFile, "SDRC_EMR2_0_LSB=0x%04x\n", &Value);
  CHRAMData->SDRC_EMR2_0_LSB    = Value;
  fscanf(DataFile, "SDRC_EMR3_0_LSB=0x%04x\n", &Value);
  CHRAMData->SDRC_EMR3_0_LSB    = Value;
  fscanf(DataFile, "SDRC_ACTIM_CTRLA_0=0x%08x\n", &Value);
  CHRAMData->SDRC_ACTIM_CTRLA_0 = Value;
  fscanf(DataFile, "SDRC_ACTIM_CTRLB_0=0x%08x\n", &Value);
  CHRAMData->SDRC_ACTIM_CTRLB_0 = Value;
  fscanf(DataFile, "SDRC_RFRCTRL_0=0x%08x\n", &Value);
  CHRAMData->SDRC_RFRCTRL_0     = Value;
  fscanf(DataFile, "MEMORY_TYPE_CS1=0x%04x\n", &Value);
  CHRAMData->MEMORY_TYPE_CS1    = Value;
  fscanf(DataFile, "SDRC_MCFG_1=0x%08x\n", &Value);
  CHRAMData->SDRC_MCFG_1        = Value;
  fscanf(DataFile, "SDRC_MR_1_LSB=0x%04x\n", &Value);
  CHRAMData->SDRC_MR_1_LSB      = Value;
  fscanf(DataFile, "SDRC_EMR1_1_LSB=0x%04x\n", &Value);
  CHRAMData->SDRC_EMR1_1_LSB    = Value;
  fscanf(DataFile, "SDRC_EMR2_1_LSB=0x%04x\n", &Value);
  CHRAMData->SDRC_EMR2_1_LSB    = Value;
  fscanf(DataFile, "SDRC_EMR3_1_LSB=0x%04x\n", &Value);
  CHRAMData->SDRC_EMR3_1_LSB    = Value;
  fscanf(DataFile, "SDRC_ACTIM_CTRLA_1=0x%08x\n", &Value);
  CHRAMData->SDRC_ACTIM_CTRLA_1 = Value;
  fscanf(DataFile, "SDRC_ACTIM_CTRLB_1=0x%08x\n", &Value);
  CHRAMData->SDRC_ACTIM_CTRLB_1 = Value;
  fscanf(DataFile, "SDRC_RFRCTRL_1=0x%08x\n", &Value);
  CHRAMData->SDRC_RFRCTRL_1     = Value;

  CHRAMData->Flags              = 0x0003;
}

static 
void 
PrepareConfigurationHeader (
  void
  )
{
  TOC_DATA        Toc;
  CHSETTINGS_DATA CHSETTINGSData;
  CHRAM_DATA      CHRAMData;
  unsigned int    ConfigurationHdrOffset = 0;
  FILE            *DataFile;
  
  // Open data file
  DataFile = fopen(gDataFile, "rb");
  if (DataFile == NULL) {
    fprintf(stderr, "Can't open data file %s.\n", gOutputImageFile);
    exit(1);
  }
  
  //Initialize configuration header.
  memset(gConfigurationHeader, 0x00, sizeof(gConfigurationHeader));

  //CHSETTINGS TOC
  memset(&Toc, 0x00, sizeof(TOC_DATA));
  Toc.Start = CHSETTINGS_START;
  Toc.Size = CHSETTINGS_SIZE;
  strcpy((char *)Toc.Filename, (const char *)"CHSETTINGS");
  memcpy(gConfigurationHeader + ConfigurationHdrOffset, &Toc, sizeof(TOC_DATA));

  //Populate CHSETTINGS Data
  memset(&CHSETTINGSData, 0x00, sizeof(CHSETTINGS_DATA));
  PopulateCHSETTINGSData(DataFile, &CHSETTINGSData);
  memcpy(gConfigurationHeader + Toc.Start, &CHSETTINGSData, Toc.Size);

  //Adjust ConfigurationHdrOffset to point to next TOC
  ConfigurationHdrOffset += sizeof(TOC_DATA);

  //CHRAM TOC
  memset(&Toc, 0x00, sizeof(TOC_DATA));
  Toc.Start = CHRAM_START;
  Toc.Size = CHRAM_SIZE;
  strcpy((char *)Toc.Filename, (const char *)"CHRAM");
  memcpy(gConfigurationHeader + ConfigurationHdrOffset, &Toc, sizeof(TOC_DATA));

  //Populate CHRAM Data
  memset(&CHRAMData, 0x00, sizeof(CHRAM_DATA));
  PopulateCHRAMData(DataFile, &CHRAMData);
  memcpy(gConfigurationHeader + Toc.Start, &CHRAMData, Toc.Size);
  
  //Adjust ConfigurationHdrOffset to point to next TOC
  ConfigurationHdrOffset += sizeof(TOC_DATA);

  //Closing TOC item
  memset(gConfigurationHeader + ConfigurationHdrOffset, 0xFF, CLOSING_TOC_ITEM_SIZE);
  ConfigurationHdrOffset += CLOSING_TOC_ITEM_SIZE;
  
  // Close data file
  fclose(DataFile);
}

static 
void
ConstructImage (
  void
  )
{
  FILE         *InputFile;
  FILE         *OutputFile;
  unsigned int InputImageFileSize;
  unsigned int NewImageFileSize;
  struct       stat FileStat;
  char         Ch;
  unsigned int i;

  InputFile = fopen(gInputImageFile, "rb");
  if (InputFile == NULL) {
    fprintf(stderr, "Can't open input file.\n");
    exit(0);
  } 

  //Get the size of the input image.
  fstat(fileno(InputFile), &FileStat);
  InputImageFileSize = FileStat.st_size;
  
  //Calculate new file size
  NewImageFileSize = InputImageFileSize - 520;

  OutputFile = fopen(gOutputImageFile, "wb");
  if (OutputFile == NULL) {
    fprintf(stderr, "Can't open output file %s.\n", gOutputImageFile);
    exit(0);
  }

  //Write Configuration header 
  fwrite(gConfigurationHeader, 1, sizeof(gConfigurationHeader), OutputFile);

  //Write image header (Input image size, execution address)
  fwrite(&NewImageFileSize, 1, 4, OutputFile);
  fwrite(&gImageExecutionAddress, 1, 4, OutputFile);

  //Skip first 0x207 bytes
  fseek(InputFile, 520, SEEK_SET);

  //Copy input image to the output file.
  for (i = 0; i < NewImageFileSize; i++) {
    fread(&Ch, 1, 1, InputFile);
    fwrite(&Ch, 1, 1, OutputFile);
  }

  fclose(InputFile);
  fclose(OutputFile);
}


int 
main (
  int    argc, 
  char** argv
  )
{
  char          Ch;
  unsigned char *ptr;
  int           i;
  int           TwoArg;  

  if (argc == 1) {
    PrintUsage ();
    exit(1);
  }

  for (i=1; i < argc; i++) {
    if (argv[i][0] == '-') {
      // TwoArg TRUE -E 0x123, FALSE -E0x1234
      TwoArg = (argv[i][2] != ' ');
      switch (argv[i][1]) {
        case 'E': /* Image execution address */
          gImageExecutionAddress = strtoul (TwoArg ? argv[i+1] : &argv[i][2], (char **)&ptr, 16);
          break;
        
        case 'I': /* Input image file */
          gInputImageFile = TwoArg ? argv[i+1] : &argv[i][2];
          break;

        case 'O': /* Output image file */
          gOutputImageFile = TwoArg ? argv[i+1] : &argv[i][2];
          break;
        
        case 'D': /* Data file */
          gDataFile = TwoArg ? argv[i+1] : &argv[i][2];
          break;

        default:
          abort ();
      }
      printf ("\n%d(%x) - %s %s", i, TwoArg, argv[i], TwoArg ? argv[i+1] : &argv[i][2]); 
    }
  }

 
  //Prepare configuration header
  PrepareConfigurationHeader ();

  //Build image with configuration header + image header + image
  ConstructImage ();

  return 0;
}