summaryrefslogtreecommitdiffstats
blob: fe986f77391a87a46416f72ed4887c78da7de91b (plain) (blame)
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
/**************************************************************************************
 * FILE PURPOSE: Merge I2C boot parameter and boot table into one final .ccs file
 **************************************************************************************
 * FILE NAME: myparser.c
 *
 *   Usage: myparser input.ccs input.txt output.ccs
 *
 *   Note: this code can be improved to support read boot table not from offset 0x20 but anywhere
 *
 **************************************************************************************/
#include <stdio.h>
#include <string.h>


/***************************************************************************************
 * FUNCTION PURPOSE: The main program
 ***************************************************************************************
 * DESCRIPTION: Performs the conversion from ccs format to qt format
 ***************************************************************************************/
int main (int argc, char *argv[])
{
  FILE *strin1;
  FILE *strin2;
  FILE *strout;

  int a,b,c,d,n1,n;
  int i, table;
  int num_tables;
  int table_size;
  int rem_lines;
  long int temp;

  char iline[132];
  char iline2[132];

  /* Verify the number of args */
  if (argc != 4)  {
    printf ("usage: %s input.ccs input.txt output.ccs\n", argv[0]);
    return (-1);
  }

  /* Open all the files */
  strin1 = fopen (argv[1], "r");
  if (strin1 == NULL)  {
    printf ("could not open input file %s\n", argv[1]);
    return (-1);
  }

  strin2 = fopen (argv[2], "r");
  if (strin2 == NULL)  {
    printf ("could not open input file %s\n", argv[2]);
    return (-1);
  }

  strout = fopen (argv[3], "w");
  if (strout == NULL)  {
    printf ("could not open file %s to write\n",argv[3]);
    return (-1);
  }

  /* Read the header */
  fgets (iline, 132, strin1);
  sscanf (iline, "%x %x %x %x %x", &a, &b, &c, &d, &n1);

  /* read the first config line - get number of tables in the file */
  fgets(iline,132,strin2);
  sscanf (iline, "%x", &num_tables);

  if (num_tables == 0)
  {
    printf("Error in I2C configure file, num tables = 0.\n");
    return(-1);
  }
  printf("Reading %d tables from I2C configure file\n", num_tables);

  n = n1 + (32 * num_tables) + 2;

  /* Write the CCS header */
  fprintf (strout, "1651 1 10890000 1 %x\n", n);
  fprintf (strout, "0x%08x\n",n*4-8);
  fprintf (strout, "0x%08x\n",0);


  /* Prime the loop: read the table size from the first line */
  if (fgets(iline,132,strin2)==NULL)
  {
    fclose(strin1);
    fclose(strin2);
    fclose(strout);
    printf("Error in I2C configure file\n");
    return(-1);
  }

  for (table = 0; table < num_tables; table++)
  {
    strncpy(iline2, iline, 6); //get the first 16 bits (0xffff)
    iline2[6] = 0;             //make sure it's null terminated
    sscanf (iline2, "%x", &table_size);
    fprintf(strout, "%s", iline);

    printf("  Table %d size %d\n", table+1, table_size);

    /* There must be table_size / 4 lines in the file. */
    for (i = 1; i < (table_size / 4); i++)
    {
      if (fgets(iline,132,strin2)==NULL)
      {
        fclose(strin1);
        fclose(strin2);
        fclose(strout);
        printf("Error in I2C configure file\n");
        return(-1);
      }

      fprintf (strout, "%s",iline);
    }

    rem_lines = (128 - table_size) / 4;

    /* Read remaining lines, or encounter the next header early. */
    for (i = 0; i < rem_lines; i++)
    {
      if (fgets(iline,132,strin2)==NULL) break;

      strncpy(iline2, iline, 10); //get just the hex number
      iline2[10] = 0;             //make sure it's null terminated
      sscanf (iline2, "%x", &temp);
      if (temp != 0) break;
      fprintf (strout, "%s",iline);
    }

    if (i < rem_lines) //encountered next header early, add remaining lines
    {
      rem_lines -= i;

      for (i = 0; i < rem_lines; i++)
        fprintf (strout, "0x%08x\n",0);
    }
    else if (table < (num_tables - 1))
    {
      fgets(iline,132,strin2);  //read the next header
    }
  }

  fclose(strin2);

  for (i=0; i < n1; i++)
  {
    fgets(iline,132,strin1);
    fprintf (strout, "%s",iline);
  }

  fclose(strin1);
  fclose(strout);

  return (0);
}