5 // 8 Jan 2001 MJH Added code to write data to Binary file
6 // note: outputfile is name.bin, where name is first part
7 // of input file. ie tmp.rec -> tmp.bin
9 // srec2bin <input SREC file> <Output Binary File> <If Present, Big Endian>
12 // bit32u TAG_BIG = 0xDEADBE42;
13 // bit32u TAG_LITTLE = 0xFEEDFA42;
20 // Data Records Structure
22 // LENGTH : 32 Bits <- Length of DATA, excludes ADDRESS and CHECKSUM
24 // DATA : 8 Bits * LENGTH
25 // CHECKSUM: 32 Bits <- 0 - (Sum of Length --> End of Data)
27 // Note : If Length == 0, Address will be Program Start
38 #define EndianSwitch(x) ((x >> 24) | (x << 24) | ((x << 8) & (0x00FF0000)) | ((x >> 8) & (0x0000FF00)) )
40 typedef unsigned char bit8u
;
41 typedef unsigned int bit32u
;
53 FILE *OpenOutputFile( char *Name
);
57 bit32u AddressCurrent
;
59 bit32u
gh(char *cp
,int nibs
);
79 void dumpfTell(char *s
, bit32u Value
)
82 Length
= (int) RecLength
;
84 printf("[%s ] ftell()[0x%08lX] Length[0x%4X] Length[%4d] Value[0x%08x]\n",
85 s
, ftell(fOut
), Length
, Length
, Value
);
88 void DispHex(bit32u Hex
)
93 void WaitDisplay(void)
97 char iline
[]={"-\\|/"};
103 printf("%c%c",iline
[Index
++],8);
109 void binOut32 ( bit32u Data
)
111 // On UNIX machine all 32bit writes need ENDIAN switched
112 // Data = EndianSwitch(Data);
113 // fwrite( &Data, sizeof(bit32u), 1, fOut);
119 sdat
[i
]=(char)(Data
>>(i
*8));
120 fwrite( sdat
, 1, 4, fOut
);
121 dumpfTell("Out32" , Data
);
124 // Only update RecLength on Byte Writes
125 // All 32 bit writes will be for Length etc
127 void binOut8 ( bit8u Data
)
130 dumpfTell("B4Data" , (bit32u
) (Data
& 0xFF) );
131 n
= fwrite( &Data
, sizeof(bit8u
), 1, fOut
);
133 printf("Error in writing %X for Address 0x%8X\n", Data
, AddressCurrent
);
137 // Currently ONLY used for outputting Program Start
139 void binRecStart(bit32u Address
)
146 printf("[RecStart] CheckSum[0x%08X] Length[%4d] Address[0x%08X]\n",
147 CheckSum
, RecLength
, Address
);
150 dumpfTell("RecLength", RecLength
);
151 binOut32( RecLength
);
152 dumpfTell("Address", Address
);
160 if (!RecStart
) // if no record started, do not end it
168 RecEnd
= ftell(fOut
); // Save Current position
171 printf("[RecEnd ] CheckSum[0x%08X] Length[%4d] Length[0x%X] RecEnd[0x%08lX]\n",
172 CheckSum
, RecLength
, RecLength
, RecEnd
);
174 fseek( fOut
, -((long) RecLength
), SEEK_CUR
); // move back Start Of Data
176 dumpfTell("Data ", -1);
178 fseek( fOut
, -4, SEEK_CUR
); // move back Start Of Address
180 dumpfTell("Address ", -1);
182 fseek( fOut
, -4, SEEK_CUR
); // move back Start Of Length
184 dumpfTell("Length ", -1);
186 binOut32( RecLength
);
188 fseek( fOut
, RecEnd
, SEEK_SET
); // move to end of Record
190 CheckSum
+= RecLength
;
192 CheckSum
= ~CheckSum
+ 1; // Two's complement
194 binOut32( CheckSum
);
197 printf("[Created Record of %d Bytes with CheckSum [0x%8X]\n", RecLength
, CheckSum
);
200 void binRecOutProgramStart(bit32u Address
)
202 if (Address
!= (AddressCurrent
+1))
205 binRecStart(Address
);
207 AddressCurrent
= Address
;
209 void binRecOutByte(bit32u Address
, bit8u Data
)
211 // If Address is one after Current Address, output Byte
212 // If not, close out last record, update Length, write checksum
213 // Then Start New Record, updating Current Address
215 if (Address
!= (AddressCurrent
+1))
218 binRecStart(Address
);
220 AddressCurrent
= Address
;
225 //=============================================================================
227 //=============================================================================
228 int readline(FILE *fil
,char *buf
,int len
)
233 if (len
==0) return(0);
238 cur_len
=fread(buffer
, 1, sizeof(buffer
), fil
);
261 if ((len
>1)&&(*cur_ptr
!='\r'))
284 int SRLerrorout(char *c1
,char *c2
)
286 printf("\nERROR: %s - '%s'.",c1
,c2
);
291 int checksum(char *cp
,int count
)
300 if (!isxdigit(*scp
++))
301 return(SRLerrorout("Invalid hex digits",cp
));
316 // printf("\nCk:%02x",cksum);
317 return(cksum
==0x0ff);
320 bit32u
gh(char *cp
,int nibs
)
330 if ((*cp
>='a')&&(*cp
<='z')) *cp
&= 0x5f;
331 if ((*cp
>='0')&&(*cp
<='9'))
334 if ((*cp
>='A')&&(*cp
<='F'))
337 SRLerrorout("Bad Hex char", cp
);
344 //=============================================================================
346 //=============================================================================
348 int srecLine(char *pSrecLine
)
353 static bit32u RecordCounter
=0;
359 return(SRLerrorout("Not an Srecord file",scp
));
361 if (strlen(pSrecLine
)<4)
362 return(SRLerrorout("Srecord too short",scp
));
366 count
=gh(pSrecLine
,2);
371 // printf("count %d, strlen(pSrecLine) = %d, pSrecLine =[%s]\n", count, strlen(pSrecLine), pSrecLine);
373 DispHex(RecordCounter
);
375 if ((count
*2) != strlen(pSrecLine
)) return(SRLerrorout("Count field larger than record",scp
));
377 if (!checksum(pSrecLine
, count
)) return(SRLerrorout("Bad Checksum",scp
));
381 case '0': if (count
<3) return(SRLerrorout("Invalid Srecord count field",scp
));
382 itmp
=gh(pSrecLine
,4); pSrecLine
+=4; count
-=2;
383 if (itmp
) return(SRLerrorout("Srecord 1 address not zero",scp
));
385 case '1': if (count
<3) return(SRLerrorout("Invalid Srecord count field",scp
));
386 return(SRLerrorout("Srecord Not valid for MIPS",scp
));
388 case '2': if (count
<4) return(SRLerrorout("Invalid Srecord count field",scp
));
389 return(SRLerrorout("Srecord Not valid for MIPS",scp
));
391 case '3': if (count
<5) return(SRLerrorout("Invalid Srecord count field",scp
));
392 adr
=gh(pSrecLine
,8); pSrecLine
+=8; count
-=4;
396 dat
=gh(pSrecLine
,2); pSrecLine
+=2; count
--;
397 binRecOutByte(adr
, (char) (dat
& 0xFF));
402 case '4': return(SRLerrorout("Invalid Srecord type",scp
));
404 case '5': if (count
<3) return(SRLerrorout("Invalid Srecord count field",scp
));
405 itmp
=gh(pSrecLine
,4); pSrecLine
+=4; count
-=2;
406 if (itmp
|=s1s2s3_total
) return(SRLerrorout("Incorrect number of S3 Record processed",scp
));
408 case '6': return(SRLerrorout("Invalid Srecord type",scp
));
410 case '7': // PROGRAM START
411 if (count
<5) return(SRLerrorout("Invalid Srecord count field",scp
));
412 adr
=gh(pSrecLine
,8); pSrecLine
+=8; count
-=4;
413 if (count
!=1) return(SRLerrorout("Invalid Srecord count field",scp
));
414 binRecOutProgramStart(adr
);
416 case '8': if (count
<4) return(SRLerrorout("Invalid Srecord count field",scp
));
417 return(SRLerrorout("Srecord Not valid for MIPS",scp
));
419 case '9': if (count
<3) return(SRLerrorout("Invalid Srecord count field",scp
));
420 return(SRLerrorout("Srecord Not valid for MIPS",scp
));
429 //=============================================================================
430 // MAIN LOGIC, READS IN LINE AND OUTPUTS BINARY
431 //=============================================================================
433 int srec2bin(int argc
,char *argv
[],int verbose
)
439 bit32u TAG_BIG
= 0xDEADBE42;
440 bit32u TAG_LITTLE
= 0xFEEDFA42;
447 printf("\nError: <srec2bin <srec input file> <bin output file>\n\n");
451 if (argc
> 3) BigEndian
=TRUE
; else BigEndian
=FALSE
;
459 printf("\nEndian: %s, Tag is 0x%8X\n",(BigEndian
)?"BIG":"LITTLE", Tag
);
461 fp
= fopen(argv
[1],"rt");
465 printf("\nError: Opening input file, %s.", argv
[1]);
469 fOut
= fopen( argv
[2], "wb");
473 printf("\nError: Opening Output file, %s.", argv
[2]);
480 AddressCurrent
= 0xFFFFFFFFL
;
484 dumpfTell("Tag", Tag
);
492 rlen
= readline(fp
,buff
,sizeof buff
);
494 while( (sts
) && (rlen
!= -1))
498 sts
&= srecLine(buff
);
501 rlen
= readline(fp
,buff
,sizeof buff
);
505 // printf("PC: 0x%08X, Length 0x%08X, Tag 0x%08X\n", ProgramStart, RecLength, TAG_LITTLE);
510 if(fOut
) fclose(fOut
);
515 main(int argc
, char *argv
[])
520 srec2bin(argc
,argv
,verbose
);
This page took 0.063617 seconds and 5 git commands to generate.