imgtools/romtools/rombuild/r_srec.cpp
changeset 2 39c28ec933dd
equal deleted inserted replaced
1:820b22e13ff1 2:39c28ec933dd
       
     1 /*
       
     2 * Copyright (c) 1996-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the License "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "r_rom.h"
       
    20 #include "r_obey.h"
       
    21 
       
    22 #include <e32std.h>
       
    23 #include <e32std_private.h>
       
    24 
       
    25 #if defined(__MSVCDOTNET__) || defined(__TOOLS2__)
       
    26 #include <iostream>
       
    27 #include <fstream>
       
    28 #else //!__MSVCDOTNET__
       
    29 #include <iostream.h>
       
    30 #include <fstream.h>
       
    31 #endif //__MSVCDOTNET__
       
    32 
       
    33 #include <string.h>
       
    34 
       
    35 const TInt KSRecBytesPerLine=32;		// max line = 1+1+2+8 + (32*2) + 2 = 78
       
    36 
       
    37 LOCAL_C TInt enchex(TUint nibble)
       
    38 //
       
    39 // Return ascii hex character corresponding to nibble
       
    40 //
       
    41     {
       
    42 
       
    43     nibble&=0xf;
       
    44     return(nibble<=9 ? '0'+nibble : 'A'-10+nibble);
       
    45     }
       
    46 
       
    47 GLDEF_C TUint putbhx(TUint8 *buf,TUint byte)
       
    48 //
       
    49 // Write byte to buffer as two hex digits
       
    50 //
       
    51     {
       
    52 
       
    53     *buf++ = (TUint8)enchex(byte>>4);
       
    54     *buf++ = (TUint8)enchex(byte);
       
    55 	return byte&0xff;
       
    56     }
       
    57 
       
    58 GLDEF_C TInt putmot(TUint8 *mcode, TUint8 *mdata,TUint mlen,TUint addr)
       
    59 //
       
    60 // Write SREC or S19 format to buffer at mcode from mlen bytes of binary data
       
    61 // stored in buffer mdata. The code is given address addr.
       
    62 // Returns the number of bytes written to mcode.
       
    63 //
       
    64     {
       
    65     TUint8 *p,*q,*qend;
       
    66     TUint  sum,byte;
       
    67 
       
    68     p=mcode;
       
    69     *p='S';
       
    70 #ifdef ALLOW_S_RECORD_THREE_BYTE_ADDRESSES
       
    71 	// This is an optimisation which is useful for S-Record downloads over serial cable
       
    72 	// but some S-Record tools don't support it, so it's off by default.
       
    73 	if ((TUint)((addr>>24)&0xff) == 0)
       
    74 		{
       
    75 		*(p+1)='2';		// 3-byte address field
       
    76 		sum=putbhx(p+=2,3+mlen+1);
       
    77  		}
       
    78 	else
       
    79 #endif
       
    80 		{
       
    81 		*(p+1)='3';		// 4-byte address field
       
    82 		sum=putbhx(p+=2,4+mlen+1);
       
    83 		sum+=putbhx(p+=2,(TUint)((addr>>24)&0xff));
       
    84 		}
       
    85     sum+=putbhx(p+=2,(TUint)((addr>>16)&0xff));
       
    86     sum+=putbhx(p+=2,(TUint)((addr>>8)&0xff));
       
    87     sum+=putbhx(p+=2,(TUint)addr);
       
    88     q=mdata;
       
    89     qend=mdata+mlen;
       
    90     for (q=mdata;q<qend;q++)
       
    91 		{
       
    92 		byte=(*q);
       
    93 		sum+=putbhx(p+=2,byte);
       
    94 		}
       
    95 	putbhx(p+=2,~sum);
       
    96     return((TUint)(p-mcode+2));
       
    97     }
       
    98 
       
    99 void E32Rom::WriteSRecord(ofstream &of)
       
   100 //
       
   101 // Write the rom to a file in S record format and return its check sum.
       
   102 //
       
   103 	{
       
   104 
       
   105 	TInt i;
       
   106 	TUint8 sBuf[256];
       
   107 	of << "S00600004844521B\n";
       
   108 	TInt size=iObey->iRomSize;
       
   109 	TUint8 *ptr=(TUint8 *)iHeader;
       
   110 	for (i=0; i<size; i+=KSRecBytesPerLine)
       
   111 		{
       
   112 		TInt len;
       
   113 		if ((i+KSRecBytesPerLine)>size)
       
   114 			len=size-i;
       
   115 		else
       
   116 			len=KSRecBytesPerLine;
       
   117 		TUint8 *pS=ptr+i;
       
   118 		TInt l=putmot(sBuf,pS,len,i+iObey->iSRecordBase);
       
   119 		of.write(reinterpret_cast<char *>(sBuf), l);
       
   120 		of<<endl;
       
   121 		}
       
   122 	of << "S70500000000FA\n";		// Fixed address! - would need to compute the checksum
       
   123 	}
       
   124