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