]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - keystone-rtos/mcsdk-tools.git/blob - boot_loader/examples/emac/Utilities/mergebtbl.c
Added factory images directory
[keystone-rtos/mcsdk-tools.git] / boot_loader / examples / emac / Utilities / mergebtbl.c
1 /**************************************************************************************
2  * FILE PURPOSE: Merge multiple boot tables into one boot table
3  **************************************************************************************
4  * FILE NAME: mergebtbl.c
5  *
6  *   Usage: mergebtbl inputfile1 inputfile2 ... outputfile
7  *          Note: may not support single line boot table(hardly possible)
8  *
9  **************************************************************************************/
10 #include <stdio.h>
11 #include <string.h>
14 int asciiByte (unsigned char c)
15 {
16   if ((c >= '0') && (c <= '9'))
17     return (1);
19   if ((c >= 'A') && (c <= 'F'))
20     return (1);
22   return (0);
23 }
26 int main (int argc, char *argv[])
27 {
28   FILE *strin;
29   FILE *strout;
30   int i,j,k;
31   int line;
32   int start;
33  
34   char iline[132];
35   char iline2nd[132];
37   /* Verify the number of args */
38    if (argc < 3)  {
39     printf ("usage: %s inpfile[1] ... infile[n] outfile\n", argv[0]);
40     return (-1);
41   }
43   strout = fopen (argv[argc-1], "w");
44   if (strout == NULL)  {
45     printf ("could not open file %s to write\n",argv[argc-1]);
46     return (-1);
47   }
49   fputs("first line\n",strout);
50   fputs("second line\n",strout);
52   for(i=1; i<argc-1;i++) {
53         
54         strin = fopen (argv[i], "r");
55                 if (strin == NULL)  {
56                         printf ("could not open input file %s\n", argv[i]);
57                         return (-1);
58                 }
60                 line = 0;
62                 /* skip the next two lines */
63                 fgets (iline, 132, strin);
64                 fgets (iline, 132, strin);
66                 if(fgets(iline2nd, 132, strin)==NULL) {
67                         printf("error in boot table\n");
68                 }
70                 while(1) {
71                         fgets(iline, 132, strin);
72                         if(!asciiByte(iline[0])) break;
73                         line++;
75                         if(i!=1 && line==1) { /* if not the first file, get rid of entry point */
76                                 for(j=12; j<132; j++) {
77                                         if(iline2nd[j]=='\0') break;
78                                         fputc(iline2nd[j],strout);
79                                 }
80                         } else {
81                                 fputs(iline2nd,strout);
82                         }
83                 strcpy(iline2nd,iline);
84                 } /* while */
85                 
86                 if(i!=argc-2) { /* If not the last file, get rid of ending 00 00 00 00 */
87                         for(j=0,start=0;j<132;j+=2){
88                                 if((iline2nd[j]=='0')&&(iline2nd[j+1]=='0')) {
89                                         if(start) k++;
90                                         else if((j-j/4*4)==0) {
91                                                 start = 1; /* not count from any 00 */
92                                                 k=1;
93                                         }
94                                 }
95                                 else { 
96                                         start = 0;
97                                         k=0;
98                                 }
99                 
100                                 if(k==4) {  // when 00 00 00 00 reached
101                                         if(asciiByte(iline2nd[j+3])) { //if the next char is still ASCII
102                                                 start = 0;
103                                         } else break; /* Ending 00 00 00 00 reach */
104                                 }
105                                 j++;            /* skip the space */
106                         }
108                         if((k==4) && (j>10)) {
109                         for(k=0;k<j-10;k++){
110                                 fputc(iline2nd[k],strout);
111                                 }
112                                 fputc('\n',strout);
113                         }
114                 } else {
115                         fputs(iline2nd,strout);
116                 }
118                 fclose(strin);
119   } /* for */
121   fputc(0x04,strout); /* any non-ASCII character */
123   fclose(strout);
125   return (0);