0b27ca9bdc6f3b83c668ea3d580b2ae1b4ec7072
1 /*-----------------------------------------------------------------------------*/
3 /* Copyright (C) 1999-2000 by Texas Instruments, Inc. All rights reserved. */
5 /* IMPORTANT - READ CAREFULLY BEFORE PROCEEDING TO USE SOFTWARE. */
7 /* This document is displayed for you to read prior to using the software */
8 /* and documentation. By using the software and documentation, or opening */
9 /* the sealed packet containing the software, or proceeding to download the */
10 /* software from a Bulletin Board System(BBS) or a WEB Server, you agree to */
11 /* abide by the following Texas Instruments License Agreement. If you choose */
12 /* not to agree with these provisions, promptly discontinue use of the */
13 /* software and documentation and return the material to the place you */
16 /* Texas Instruments License Agreement */
18 /* 1. License - Texas Instruments (hereinafter "TI"), grants you a license */
19 /* to use the software program and documentation in this package ("Licensed */
20 /* Materials") for Texas Instruments broadband products. */
22 /* 2. Restrictions - You may not reverse-assemble or reverse-compile the */
23 /* Licensed Materials provided in object code or executable format. You may */
24 /* not sublicense, transfer, assign, rent, or lease the Licensed Materials */
25 /* or this Agreement without written permission from TI. */
27 /* 3. Copyright - The Licensed Materials are copyrighted. Accordingly, you */
28 /* may either make one copy of the Licensed Materials for backup and/or */
29 /* archival purposes or copy the Licensed Materials to another medium and */
30 /* keep the original Licensed Materials for backup and/or archival purposes. */
32 /* 4. Runtime and Applications Software - You may create modified or */
33 /* derivative programs of software identified as Runtime Libraries or */
34 /* Applications Software, which, in source code form, remain subject to this */
35 /* Agreement, but object code versions of such derivative programs are not */
36 /* subject to this Agreement. */
38 /* 5. Warranty - TI warrants the media to be free from defects in material */
39 /* and workmanship and that the software will substantially conform to the */
40 /* related documentation for a period of ninety (90) days after the date of */
41 /* your purchase. TI does not warrant that the Licensed Materials will be */
42 /* free from error or will meet your specific requirements. */
44 /* 6. Remedies - If you find defects in the media or that the software does */
45 /* not conform to the enclosed documentation, you may return the Licensed */
46 /* Materials along with the purchase receipt, postage prepaid, to the */
47 /* following address within the warranty period and receive a refund. */
49 /* TEXAS INSTRUMENTS */
50 /* Application Specific Products, MS 8650 */
51 /* c/o ADAM2 Application Manager */
52 /* 12500 TI Boulevard */
53 /* Dallas, TX 75243 - U.S.A. */
55 /* 7. Limitations - TI makes no warranty or condition, either expressed or */
56 /* implied, including, but not limited to, any implied warranties of */
57 /* merchantability and fitness for a particular purpose, regarding the */
58 /* licensed materials. */
60 /* Neither TI nor any applicable licensor will be liable for any indirect, */
61 /* incidental or consequential damages, including but not limited to loss of */
64 /* 8. Term - The license is effective until terminated. You may terminate */
65 /* it at any other time by destroying the program together with all copies, */
66 /* modifications and merged portions in any form. It also will terminate if */
67 /* you fail to comply with any term or condition of this Agreement. */
69 /* 9. Export Control - The re-export of United States origin software and */
70 /* documentation is subject to the U.S. Export Administration Regulations or */
71 /* your equivalent local regulations. Compliance with such regulations is */
72 /* your responsibility. */
74 /* *** IMPORTANT NOTICE *** */
76 /* Texas Instruments (TI) reserves the right to make changes to or to */
77 /* discontinue any semiconductor product or service identified in this */
78 /* publication without notice. TI advises its customers to obtain the latest */
79 /* version of the relevant information to verify, before placing orders, */
80 /* that the information being relied upon is current. */
82 /* TI warrants performance of its semiconductor products and related */
83 /* software to current specifications in accordance with TI's standard */
84 /* warranty. Testing and other quality control techniques are utilized to */
85 /* the extent TI deems necessary to support this warranty. Unless mandated */
86 /* by government requirements, specific testing of all parameters of each */
87 /* device is not necessarily performed. */
89 /* Please be aware that Texas Instruments products are not intended for use */
90 /* in life-support appliances, devices, or systems. Use of a TI product in */
91 /* such applications without the written approval of the appropriate TI */
92 /* officer is prohibited. Certain applications using semiconductor devices */
93 /* may involve potential risks of injury, property damage, or loss of life. */
94 /* In order to minimize these risks, adequate design and operating */
95 /* safeguards should be provided by the customer to minimize inherent or */
96 /* procedural hazards. Inclusion of TI products in such applications is */
97 /* understood to be fully at the risk of the customer using TI devices or */
100 /* TI assumes no liability for TI applications assistance, customer product */
101 /* design, software performance, or infringement of patents or services */
102 /* described herein. Nor does TI warrant or represent that license, either */
103 /* expressed or implied, is granted under any patent right, copyright, mask */
104 /* work right, or other intellectual property right of TI covering or */
105 /* relating to any combination, machine, or process in which such */
106 /* semiconductor products or services might be or are used. */
108 /* All company and/or product names are trademarks and/or registered */
109 /* trademarks of their respective manaufacturers. */
111 /*-----------------------------------------------------------------------------*/
115 // #include <string.h>
120 // 8 Jan 2001 MJH Added code to write data to Binary file
121 // note: outputfile is name.bin, where name is first part
122 // of input file. ie tmp.rec -> tmp.bin
124 // srec2bin <input SREC file> <Output Binary File> <If Present, Big Endian>
127 // bit32u TAG_BIG = 0xDEADBE42;
128 // bit32u TAG_LITTLE = 0xFEEDFA42;
135 // Data Records Structure
137 // LENGTH : 32 Bits <- Length of DATA, excludes ADDRESS and CHECKSUM
139 // DATA : 8 Bits * LENGTH
140 // CHECKSUM: 32 Bits <- 0 - (Sum of Length --> End of Data)
142 // Note : If Length == 0, Address will be Program Start
153 #define EndianSwitch(x) ((x >> 24) | (x << 24) | ((x << 8) & (0x00FF0000)) | ((x >> 8) & (0x0000FF00)) )
155 typedef unsigned char bit8u
;
156 typedef unsigned int bit32u
;
160 #define TRUE (!FALSE)
168 FILE *OpenOutputFile( char *Name
);
172 bit32u AddressCurrent
;
174 bit32u
gh(char *cp
,int nibs
);
180 // char buf[16*1024];
194 void dumpfTell(char *s
, bit32u Value
)
197 Length
= (int) RecLength
;
199 printf("[%s ] ftell()[0x%08lX] Length[0x%4X] Length[%4d] Value[0x%08x]\n",
200 s
, ftell(fOut
), Length
, Length
, Value
);
203 void DispHex(bit32u Hex
)
205 // printf("%X", Hex);
208 void WaitDisplay(void)
212 char iline
[]={"-\\|/"};
218 printf("%c%c",iline
[Index
++],8);
224 void binOut32 ( bit32u Data
)
226 // On UNIX machine all 32bit writes need ENDIAN switched
227 // Data = EndianSwitch(Data);
228 // fwrite( &Data, sizeof(bit32u), 1, fOut);
234 sdat
[i
]=(char)(Data
>>(i
*8));
235 fwrite( sdat
, 1, 4, fOut
);
236 dumpfTell("Out32" , Data
);
239 // Only update RecLength on Byte Writes
240 // All 32 bit writes will be for Length etc
242 void binOut8 ( bit8u Data
)
245 dumpfTell("B4Data" , (bit32u
) (Data
& 0xFF) );
246 n
= fwrite( &Data
, sizeof(bit8u
), 1, fOut
);
248 printf("Error in writing %X for Address 0x%8X\n", Data
, AddressCurrent
);
252 // Currently ONLY used for outputting Program Start
254 void binRecStart(bit32u Address
)
261 printf("[RecStart] CheckSum[0x%08X] Length[%4d] Address[0x%08X]\n",
262 CheckSum
, RecLength
, Address
);
265 dumpfTell("RecLength", RecLength
);
266 binOut32( RecLength
);
267 dumpfTell("Address", Address
);
275 if (!RecStart
) // if no record started, do not end it
283 RecEnd
= ftell(fOut
); // Save Current position
286 printf("[RecEnd ] CheckSum[0x%08X] Length[%4d] Length[0x%X] RecEnd[0x%08lX]\n",
287 CheckSum
, RecLength
, RecLength
, RecEnd
);
289 fseek( fOut
, -(RecLength
), SEEK_CUR
); // move back Start Of Data
291 dumpfTell("Data ", -1);
293 fseek( fOut
, -4, SEEK_CUR
); // move back Start Of Address
295 dumpfTell("Address ", -1);
297 fseek( fOut
, -4, SEEK_CUR
); // move back Start Of Length
299 dumpfTell("Length ", -1);
301 binOut32( RecLength
);
303 fseek( fOut
, RecEnd
, SEEK_SET
); // move to end of Record
305 CheckSum
+= RecLength
;
307 CheckSum
= ~CheckSum
+ 1; // Two's complement
309 binOut32( CheckSum
);
312 printf("[Created Record of %d Bytes with CheckSum [0x%8X]\n", RecLength
, CheckSum
);
315 void binRecOutProgramStart(bit32u Address
)
317 if (Address
!= (AddressCurrent
+1))
320 binRecStart(Address
);
322 AddressCurrent
= Address
;
324 void binRecOutByte(bit32u Address
, bit8u Data
)
326 // If Address is one after Current Address, output Byte
327 // If not, close out last record, update Length, write checksum
328 // Then Start New Record, updating Current Address
330 if (Address
!= (AddressCurrent
+1))
333 binRecStart(Address
);
335 AddressCurrent
= Address
;
340 //=============================================================================
342 //=============================================================================
343 int readline(FILE *fil
,char *buf
,int len
)
348 if (len
==0) return(0);
353 cur_len
=fread(buffer
, 1, sizeof(buffer
), fil
);
376 if ((len
>1)&&(*cur_ptr
!='\r'))
399 int SRLerrorout(char *c1
,char *c2
)
401 printf("\nERROR: %s - '%s'.",c1
,c2
);
406 int checksum(char *cp
,int count
)
415 if (!isxdigit(*scp
++))
416 return(SRLerrorout("Invalid hex digits",cp
));
431 // printf("\nCk:%02x",cksum);
432 return(cksum
==0x0ff);
435 bit32u
gh(char *cp
,int nibs
)
445 if ((*cp
>='a')&&(*cp
<='z')) *cp
&= 0x5f;
446 if ((*cp
>='0')&&(*cp
<='9'))
449 if ((*cp
>='A')&&(*cp
<='F'))
452 SRLerrorout("Bad Hex char", cp
);
459 //=============================================================================
461 //=============================================================================
463 int srecLine(char *pSrecLine
)
468 static bit32u RecordCounter
=0;
474 return(SRLerrorout("Not an Srecord file",scp
));
476 if (strlen(pSrecLine
)<4)
477 return(SRLerrorout("Srecord too short",scp
));
481 count
=gh(pSrecLine
,2);
486 // printf("count %d, strlen(pSrecLine) = %d, pSrecLine =[%s]\n", count, strlen(pSrecLine), pSrecLine);
488 DispHex(RecordCounter
);
490 if ((count
*2) != strlen(pSrecLine
)) return(SRLerrorout("Count field larger than record",scp
));
492 if (!checksum(pSrecLine
, count
)) return(SRLerrorout("Bad Checksum",scp
));
496 case '0': if (count
<3) return(SRLerrorout("Invalid Srecord count field",scp
));
497 itmp
=gh(pSrecLine
,4); pSrecLine
+=4; count
-=2;
498 if (itmp
) return(SRLerrorout("Srecord 1 address not zero",scp
));
500 case '1': if (count
<3) return(SRLerrorout("Invalid Srecord count field",scp
));
501 return(SRLerrorout("Srecord Not valid for MIPS",scp
));
503 case '2': if (count
<4) return(SRLerrorout("Invalid Srecord count field",scp
));
504 return(SRLerrorout("Srecord Not valid for MIPS",scp
));
506 case '3': if (count
<5) return(SRLerrorout("Invalid Srecord count field",scp
));
507 adr
=gh(pSrecLine
,8); pSrecLine
+=8; count
-=4;
511 dat
=gh(pSrecLine
,2); pSrecLine
+=2; count
--;
512 binRecOutByte(adr
, (char) (dat
& 0xFF));
517 case '4': return(SRLerrorout("Invalid Srecord type",scp
));
519 case '5': if (count
<3) return(SRLerrorout("Invalid Srecord count field",scp
));
520 itmp
=gh(pSrecLine
,4); pSrecLine
+=4; count
-=2;
521 if (itmp
|=s1s2s3_total
) return(SRLerrorout("Incorrect number of S3 Record processed",scp
));
523 case '6': return(SRLerrorout("Invalid Srecord type",scp
));
525 case '7': // PROGRAM START
526 if (count
<5) return(SRLerrorout("Invalid Srecord count field",scp
));
527 adr
=gh(pSrecLine
,8); pSrecLine
+=8; count
-=4;
528 if (count
!=1) return(SRLerrorout("Invalid Srecord count field",scp
));
529 binRecOutProgramStart(adr
);
531 case '8': if (count
<4) return(SRLerrorout("Invalid Srecord count field",scp
));
532 return(SRLerrorout("Srecord Not valid for MIPS",scp
));
534 case '9': if (count
<3) return(SRLerrorout("Invalid Srecord count field",scp
));
535 return(SRLerrorout("Srecord Not valid for MIPS",scp
));
544 //=============================================================================
545 // MAIN LOGIC, READS IN LINE AND OUTPUTS BINARY
546 //=============================================================================
548 int srec2bin(int argc
,char *argv
[],int verbose
)
554 bit32u TAG_BIG
= 0xDEADBE42;
555 bit32u TAG_LITTLE
= 0xFEEDFA42;
562 printf("\nError: <srec2bin <srec input file> <bin output file>\n\n");
566 if (argc
> 3) BigEndian
=TRUE
; else BigEndian
=FALSE
;
574 printf("\nEndian: %s, Tag is 0x%8X\n",(BigEndian
)?"BIG":"LITTLE", Tag
);
576 fp
= fopen(argv
[1],"rt");
580 printf("\nError: Opening input file, %s.", argv
[1]);
584 fOut
= fopen( argv
[2], "wb");
588 printf("\nError: Opening Output file, %s.", argv
[2]);
595 AddressCurrent
= 0xFFFFFFFFL
;
599 dumpfTell("Tag", Tag
);
607 rlen
= readline(fp
,buff
,sizeof buff
);
609 while( (sts
) && (rlen
!= -1))
613 sts
&= srecLine(buff
);
616 rlen
= readline(fp
,buff
,sizeof buff
);
620 // printf("PC: 0x%08X, Length 0x%08X, Tag 0x%08X\n", ProgramStart, RecLength, TAG_LITTLE);
625 if(fOut
) fclose(fOut
);
630 main(int argc
, char *argv
[])
635 srec2bin(argc
,argv
,verbose
);
This page took 0.084768 seconds and 3 git commands to generate.