bintools/bin2coff/bin2coff.cpp
changeset 620 ad8ffc8e1982
parent 600 6d08f4a05d93
equal deleted inserted replaced
585:238f4cb8391f 620:ad8ffc8e1982
       
     1 // Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // Add a simple coff header to a binary file
       
    15 // optionally strip off the first 256 bytes
       
    16 // This is primarily for generating an image
       
    17 // file that can be downloaded via the ether
       
    18 // to cogent using the SmartFirmWare boot rom
       
    19 // 
       
    20 //
       
    21 
       
    22 #include <sys/stat.h>
       
    23 #include <fcntl.h>
       
    24 #include <io.h>
       
    25 #include <stdio.h>
       
    26 #include <string.h>
       
    27 #include <stdlib.h>
       
    28 
       
    29 // little error functionet
       
    30 void error(const char* p)
       
    31 {
       
    32 	fprintf(stderr, "%s\n", p);
       
    33 	exit(1);
       
    34 }
       
    35 
       
    36 // little usage functionet
       
    37 void usage(const char* p)
       
    38 {
       
    39 	fprintf(stderr, "%s\n", p);
       
    40 	exit(EXIT_FAILURE);
       
    41 }
       
    42 
       
    43 
       
    44 int main(int argc, char* argv[])
       
    45 {
       
    46 	// check for usage
       
    47 	if (argc < 3 || argc > 4)
       
    48 		{
       
    49 		usage("Usage: bin2coff infile outfile nostrip\nOption nostrip: don't strip off the first 256 bytes");
       
    50 		}
       
    51 
       
    52 	// make sure we are not operating on only one file
       
    53 	if (!strcmp(argv[1], argv[2]))
       
    54 		{
       
    55 		usage("Usage: bin2coff must specify different input and output files");
       
    56 		}
       
    57 
       
    58 	// try to open the input file
       
    59 	FILE *pInFile;
       
    60 	if((pInFile  = fopen(argv[1], "rb" )) == NULL)
       
    61    		{
       
    62    		error("Cannot open input file");
       
    63    		}
       
    64 
       
    65 	// open output file
       
    66 	FILE *pOutFile;
       
    67 	if((pOutFile = fopen(argv[2], "wb") ) == NULL)
       
    68 		{
       
    69    		error("Cannot open output file");
       
    70 		}
       
    71 
       
    72 	// strip the 256 long header?
       
    73 	// ok, so it's simple, so what
       
    74 	bool stripheader = (argc == 3);
       
    75 
       
    76 	// how big is the input file
       
    77 	struct stat filelength;
       
    78 	stat(argv[1], &filelength);
       
    79 	unsigned long fsize = filelength.st_size;
       
    80 	if (stripheader)
       
    81 		{
       
    82 		fsize -= 256;
       
    83 		}
       
    84 
       
    85 	// write out the coff header to the new file
       
    86 	// somewhere to put a simple coff header
       
    87 	unsigned char coffhead[0x58] = {0};  // zero all the elements
       
    88 
       
    89 	// fill in the constant bits
       
    90 	// this is supposed to be simple, remember
       
    91 	coffhead[1] = 0x0a;
       
    92 	coffhead[2] = 0x01;
       
    93 	coffhead[0x10] = 0x1c;
       
    94 	coffhead[0x12] = 0x0f;
       
    95 	coffhead[0x13] = 0xa1;
       
    96 	coffhead[0x14] = 0x0b;
       
    97 	coffhead[0x15] = 0x01;
       
    98 	coffhead[0x26] = 0x40; // entry point 0x00400000
       
    99 	coffhead[0x2a] = 0x40;
       
   100 	coffhead[0x30] = 0x2e;
       
   101 	coffhead[0x31] = 0x74;
       
   102 	coffhead[0x32] = 0x65;
       
   103 	coffhead[0x33] = 0x78;
       
   104 	coffhead[0x34] = 0x74;
       
   105 	coffhead[0x3a] = 0x40;
       
   106 	coffhead[0x3e] = 0x40;
       
   107 	coffhead[0x44] = 0x58;
       
   108 	coffhead[0x54] = 0x20;
       
   109 
       
   110 	// now fill in the text segment size
       
   111 	*(int *) (&coffhead[0x18]) = fsize;
       
   112 	*(int *) (&coffhead[0x40]) = fsize;
       
   113 
       
   114 	// write out the new file
       
   115 	// first the header
       
   116 	int numwritten=0;
       
   117 	numwritten = fwrite(coffhead, sizeof(char), 0x58, pOutFile);
       
   118 	if (numwritten < 0x58)
       
   119 		{
       
   120    		error("The number of items written is less than 0x58");
       
   121 		}
       
   122 
       
   123 	// now the rest of the file
       
   124 	if (stripheader)
       
   125 		{
       
   126 		fpos_t filePositionIndicator = 0x100;
       
   127 		if (fsetpos(pInFile, &filePositionIndicator) !=0)
       
   128 			{
       
   129 			error("The file is not accessible ");
       
   130 			}
       
   131 		}
       
   132 	
       
   133 	static const int buffsize = 1024;
       
   134 	unsigned char fbuff[buffsize];
       
   135 	while (!feof(pInFile))
       
   136 	        {
       
   137 			int nread = fread(fbuff,sizeof(unsigned char), sizeof(fbuff), pInFile);
       
   138 			int nwritten = fwrite(fbuff, sizeof(unsigned char), nread, pOutFile);
       
   139 			if (nwritten < nread)
       
   140 		        {
       
   141 				error("The number of items written is less than number of items read");
       
   142 				}
       
   143 			}
       
   144    
       
   145 	fclose(pOutFile);
       
   146 	fclose(pInFile);
       
   147 
       
   148 	return 0;
       
   149 }