📄 coff2b.c
字号:
//*************************************************************************
// FILENAME: Coff2b.c
// DESCRIPTION: Convert a COFF file (versions 1 and 2) to an equivalent
// data dump in binary
// ARGUMENTS:
// a) Input: TI executable .out file (e.g. "foo.out")
// b) Output: "foo.bin"
//*************************************************************************
#include <windows.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "coff2b.h"
void PrintUsage(void)
{
printf ("usage: coff2b <input (*.out)> <output (*.bin)> <DDR Configuration word in hex><DLL Configuraton word In hex>\n");
printf (" Example: \n coff2c monitor.out monitor.bin 0x0317675D 0xF2954A\n");
}
void PrintTI(void)
{
printf ("COFF1/COFF2 File Conversion Utility Version 1.5\n");
printf ("Copyright (c) 2005 Texas Instruments Incorporated\n\n");
}
int FileWriteHeader (FILE *ofp, ulong ddrConfig)
{
ulong lbuf;
// write exception vectors 8x32bit
lbuf = 0x0;
fwrite (&lbuf, 1, sizeof (ulong), ofp);
fwrite (&lbuf, 1, sizeof (ulong), ofp);
fwrite (&lbuf, 1, sizeof (ulong), ofp);
fwrite (&lbuf, 1, sizeof (ulong), ofp);
fwrite (&lbuf, 1, sizeof (ulong), ofp);
fwrite (&lbuf, 1, sizeof (ulong), ofp);
fwrite (&lbuf, 1, sizeof (ulong), ofp);
fwrite (&lbuf, 1, sizeof (ulong), ofp);
// write magic words 5x32bit
lbuf = 0x204a4342;
fwrite (&lbuf, 1, sizeof (ulong), ofp);
lbuf = 0x20545343;
fwrite (&lbuf, 1, sizeof (ulong), ofp);
lbuf = 0x20555343;
fwrite (&lbuf, 1, sizeof (ulong), ofp);
lbuf = 0x20565343;
fwrite (&lbuf, 1, sizeof (ulong), ofp);
lbuf = 0x20575343;
fwrite (&lbuf, 1, sizeof (ulong), ofp);
// write DDR configuration word 1x32bit
lbuf = BYTE_SWAP32(ddrConfig);
fwrite (&lbuf, 1, sizeof (ulong), ofp);
// write EBI configuration words 5x32bit
lbuf = 0xffffffff;
fwrite (&lbuf, 1, sizeof (ulong), ofp);
lbuf = 0x0;
fwrite (&lbuf, 1, sizeof (ulong), ofp);
fwrite (&lbuf, 1, sizeof (ulong), ofp);
fwrite (&lbuf, 1, sizeof (ulong), ofp);
fwrite (&lbuf, 1, sizeof (ulong), ofp);
fwrite (&lbuf, 1, sizeof (ulong), ofp);
// Execution vector
fwrite (&lbuf, 1, sizeof (ulong), ofp);
// total 84 bytes
return 84;
}
int FileWriteDllSection (FILE *ofp, ulong dllConfig)
{
ulong lbuf;
// write DLL configuration word
lbuf = 0x04000000;
fwrite (&lbuf, 1, sizeof (ulong), ofp);
lbuf = 0x04080200;
fwrite (&lbuf, 1, sizeof (ulong), ofp);
lbuf = BYTE_SWAP32(dllConfig);
fwrite (&lbuf, 1, sizeof (ulong), ofp);
// total 12 bytes
return 12;
}
int SectionWrite( ushort coff_type,
SECTHEADER *header,
unsigned char *buffer,
FILE *fp)
{
unsigned char *dataPtr, pad;
int adjustedSectSize, bytecount;
ulong lbuf, i;
adjustedSectSize = header->sectsize;
// Adjust to next largest word size (divisible by 4)
if (adjustedSectSize % 4)
adjustedSectSize += (4 - (adjustedSectSize % 4));
// write section size
dataPtr = (unsigned char *)&(adjustedSectSize);
lbuf = (*(dataPtr)<<24) | (*(dataPtr+1)<<16) | (*(dataPtr+2)<<8) | *(dataPtr+3);
fwrite (&lbuf, 1, sizeof(ulong), fp);
bytecount = 4;
// write section physical address
dataPtr = (unsigned char *)&(header->phyaddr);
lbuf = (*(dataPtr)<<24) | (*(dataPtr+1)<<16) | (*(dataPtr+2)<<8) | *(dataPtr+3);
fwrite (&lbuf, 1, sizeof(ulong), fp);
bytecount += 4;
// write section data
dataPtr = buffer;
for (i = 0; i < header->sectsize; i++)
{
fwrite(dataPtr++, 1, 1, fp);
bytecount++;
}
// Check if padding is required
pad = 0;
switch (header->sectsize % 4)
{
case 0:
break; // No padding needed
case 1:
fwrite(&pad, 1, 1, fp);
fwrite(&pad, 1, 1, fp);
fwrite(&pad, 1, 1, fp);
bytecount += 3;
break;
case 2:
fwrite(&pad, 1, 1, fp);
fwrite(&pad, 1, 1, fp);
bytecount += 2;
break;
case 3:
fwrite(&pad, 1, 1, fp);
bytecount += 1;
break;
}
return bytecount;
}
ulong SectionHeaderRead (FILE *fp,
ushort coff_type,
SECTHEADER *header)
{
SHEADER2 temp2;
SHEADER1 temp1;
ulong headersize;
if (coff_type == 0x00c1)
{
headersize = sizeof (SHEADER1);
fread ((char *)&temp1, headersize, 1, fp);
strncpy(header->name, temp1.name, 8);
header->phyaddr = temp1.phyaddr;
header->virtaddr = temp1.virtaddr;
header->sectsize = temp1.sectsize;
header->rawdataptr = temp1.rawdataptr;
header->relocationptr = temp1.relocationptr;
header->linnumptr = temp1.linnumptr;
header->relocationents = temp1.relocationents;
header->linnums = temp1.linnums;
header->flags = temp1.flags;
header->resv = temp1.resv;
header->page = temp1.page;
} else if (coff_type == 0x00c2)
{
headersize = sizeof (SHEADER2);
fread ((char *)&temp2, headersize, 1, fp);
strncpy(header->name, temp2.name, 8);
header->phyaddr = temp2.phyaddr;
header->virtaddr = temp2.virtaddr;
header->sectsize = temp2.sectsize;
header->rawdataptr = temp2.rawdataptr;
header->relocationptr = temp2.relocationptr;
header->linnumptr = temp2.linnumptr;
header->relocationents = temp2.relocationents;
header->linnums = temp2.linnums;
header->flags = temp2.flags;
header->resv = temp2.resv;
header->page = temp2.page;
} else
{
return 1;
}
if (header->rawdataptr == 0x0)
{
// determining section size
header->sectsize = 0;
}
return 0;
}
//***************************************************************************
// FUNCTION NAME: coff2b()
// Input:
// ifname - input file name, should be a CCS executable
// ofname - output file name, should be a flash image in binary
// ddrconfig - DDR configuartion in hex
// dllconfig - DLL configuartion in hex
// Output:
// size - pointer to a INTEGER for output file size
// return - error return
//***************************************************************************
int coff2b(char *ifname, char *ofname, unsigned long ddrconfig, unsigned long dllconfig, int *size)
{
char sectionname[9] = "";
unsigned long entryaddr = 0;
unsigned long baseaddr = 0;
FILE *infile, *ofp;
long sectlen = 0;
long remaining = 0;
SIZE_T headersize;
unsigned char *file2struct_ptr;
struct fheader fileheader;
SECTHEADER secthead, next_secthead;
struct inbuff;
long currentloc = 0;
char *buffer;
int i;
ushort coff_type;
SECTHEADER *secthead_buf, *data_ptr;
ulong lbuf;
// file pointer "ofp" is assigned a pointer to the file "ofname".
// if cannot open "ofname", then return error
if (NULL == (ofp = fopen(ofname, "wb")))
{
return COFF2B_OPEN_OUTPUT_ERR;
}
// if cannot open "ifname", then return error
if ((infile = fopen(ifname, "rb")) == NULL)
{
fclose(ofp);
return COFF2B_OPEN_INPUT_ERR;
}
// Get File header and assign the address of 'fileheader' to
// file2struct_ptr
// 'fileheader' is a structure of type 'struct fheader'
file2struct_ptr = (unsigned char *)&fileheader;
// headersize is assigned the size of structure 'fileheader'
// the executable generated by TI tools as a .out file
headersize = sizeof(fileheader);
// the 'file2struct_ptr' will read 1 entry of 'headersize' bytes from
// the file 'infile'
fread(file2struct_ptr, headersize, 1, infile);
// Skip optional header, if any
if (fileheader.optbytes)
{
fseek(infile, 38, SEEK_SET); // Move read pointer to entry address
fread(&entryaddr, 4, 1, infile); // Read entry address
// Set the file position for "infile" to optbytes-2 characters from
// where it was left off by the previous fread statement
fseek (infile, 50, SEEK_SET); // set readpointer to first section header;
}
coff_type = fileheader.version;
// allocate buffer for section headers
secthead_buf = (SECTHEADER *)malloc((fileheader.sections)*sizeof(SECTHEADER));
data_ptr = secthead_buf;
// read section headers from input file
for (i=0; i<fileheader.sections; i++)
{
if (SectionHeaderRead(infile, coff_type, data_ptr++))
{
fclose(ofp);
fclose(infile);
return COFF2B_FORMAT_ERR;
}
}
baseaddr = 0xffffffff;
*size += FileWriteHeader(ofp, ddrconfig);
*size += FileWriteDllSection(ofp, dllconfig);
// for the total number of sections in the input file do the following
for (i=0; i<fileheader.sections; i++)
{
secthead = secthead_buf[i];
if (i < fileheader.sections - 1)
next_secthead = secthead_buf[i+1];
if (((secthead.flags & 0x93) == 0) && (secthead.sectsize > 0))
{
if (secthead.phyaddr < baseaddr)
baseaddr = secthead.phyaddr;
fseek (infile, secthead.rawdataptr, SEEK_SET);
// allocate buffer for section data
buffer = (char *)malloc(secthead.sectsize);
// read section data from input file
fread (buffer, secthead.sectsize, 1, infile);
*size += SectionWrite(fileheader.version,
§head,
buffer,
ofp);
// release section data buffer
free(buffer);
}
}
// write ending
//fprintf (ofp, " 0xff, 0xff, 0xff, 0xff\n");
lbuf = 0xffffffff;
fwrite(&lbuf, 1, sizeof(ulong), ofp);
*size += 4;
fclose (infile);
fclose (ofp);
return COFF2B_OK;
}
//***************************************************************************
// FUNCTION NAME: Main()
// Input: int argc, char *argv[]
//***************************************************************************
int main(int argc, char *argv[])
{
unsigned long ddrConfig, dllConfig;
int size, res;
PrintTI();
// Check if number of items (entries) on the command line
// are greater than 4
if (argc > 4)
{
// get DDR configuration word
sscanf (argv[3],"%x", &ddrConfig);
// get DLL configuration
sscanf (argv[4],"%x", &dllConfig);
} else
{
PrintUsage();
return 0;
}
// call coff2b
if (res = coff2b(argv[1], argv[2], ddrConfig, dllConfig, &size))
{
switch (res)
{
case COFF2B_OPEN_OUTPUT_ERR:
printf("Cannot open output file %s\n", argv[1]);
break;
case COFF2B_OPEN_INPUT_ERR:
printf("Cannot open input file %s\n", argv[2]);
break;
case COFF2B_FORMAT_ERR:
printf("Illegal COFF file\n");
}
} else
{
printf("The conversion is completed sucessfully\n");
printf("%d bytes has been output to %s\n", size, argv[2]);
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -