📄 coff_both.c
字号:
/* $Id:$
* $Source:$
*------------------------------------------------------------------
*
* FILE: coff_both.c
* DATE: 03/25/98
* (modification of coff_new.c from 12/17/97)
* Authors: Arun Chhabra and Ramesh Iyer
* Texas Instruments, Inc.
*
* DESCRIPTION:
* Modified from the original file "coff.c" (3/29/97) by
* Chao-Li Tarng of Cisco Systems, Inc.
* This file is intended to parse a 16-bit COFF2 format
* executable file [where the executable has been created using
* the TI C54x Tools (Version 1.2)] and print out the extracted
* hex code. The code has also been modified so that coff_new.exe
* utility is designed to run on a PC platform running Windows 95.
* The current implementation is an extension of the file
* coff.c by Chao-Li Tarng, which performed the same operation but
* for a COFF1 format executable file, created using TI C54x Tools
* (Version 1.16). Also in this earlier case, the coff.exe utility
* was intended to run on a SunOS platform.
*
* PLEASE NOTE: In this current implementation, the following routines
* are not called upon:
* - wordswap()
* - swapulong()
* - swap_fileheader()
* - swap_sheader()
* However, they have been commented out and left in the
* code. This is so that if the need to port the code back
* to a SunOS platform arises, then those routines should
* prove to be useful.
*
* IMPORTANT: To invoke this utility on a TI Tools (ver 1.2) generated
* executable file (say, foo.out), type the following on the
* DOS prompt command line: "coff_new -out foo.out"
* The output generated containing the extracted code will be
* entitled "foo.out.c"
*
* All rights reserved.
*------------------------------------------------------------------
*
* $Log:$
*------------------------------------------------------------------
* $Endlog$
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include "coff.h"
#include <errno.h>
int out = 0;
ushort
swapushort (ushort data)
{
ushort tmp;
tmp = (data & 0x00FF);
tmp|= (data & 0xFF00);
return tmp;
}
ushort
xor (ushort a, ushort b)
{
return (~(a|b)^(~a|~b));
}
void
DownLoad (uint src_addr, uint length, uint dest_addr,
int space, struct buffer buf, FILE *fp)
{
int i;
int wrap = 0;
ushort checksum = 0;
if (out) {
fprintf(fp, "src_addr = 0x%X \n", src_addr);
fprintf(fp, "length = 0x%X (%d) \n", length, length);
fprintf(fp, "dest_addr = 0x%X\n", dest_addr);
fprintf(fp, "space = %d\n", space);
fprintf(fp, " \n");
for (i = 0; i < length; i++) {
if (4 <= wrap) {
wrap = 0;
fprintf(fp, "\n ");
}
fprintf(fp, "0x%04X, ", swapushort(buf.data[i]));
checksum = xor(checksum, swapushort(buf.data[i]));
/* prints the output in the
specified format and performs
the checksum */
wrap++;
}
fprintf(fp, "\n\n");
fprintf(fp, "checksum = 0x%4X\n\n", checksum);
}
return;
}
/*
ulong
swapulong (ulong data)
{
ushort tmp1;
ushort tmp2;
ulong tmp;
// Intel Sun/Moto
// 0x78563412 0x12345678
tmp = (data & 0x000000FF) << 24;
tmp |= (data & 0x0000FF00) << 8;
tmp |= (data & 0x00FF0000) >> 8;
tmp |= (data & 0xFF000000) >> 24;
return tmp;
}
#define SWAPUSHORT(us) us = swapushort(us)
#define SWAPULONG(ul) ul = swapulong(ul);
void
swap_fileheader (struct fheader *fh)
{
fh->version = swapushort(fh->version);
fh->sections = swapushort(fh->sections);
fh->stamp = swapulong(fh->stamp);
fh->symptr = swapulong(fh->symptr);
fh->symbols = swapulong(fh->symbols);
fh->optbytes = swapushort(fh->optbytes);
fh->flags = swapushort(fh->flags);
fh->magic = swapushort(fh->magic);
}
*/
/*
void
swap_sheader (struct sheader *sh)
{
sh->phyaddr = swapulong(sh->phyaddr);
sh->virtaddr = swapulong(sh->virtaddr);
sh->sectsize = swapulong(sh->sectsize);
sh->rawdataptr = swapulong(sh->rawdataptr);
sh->relocationptr = swapulong(sh->relocationptr);
sh->linnumptr = swapulong(sh->linnumptr);
sh->relocationents = swapushort(sh->relocationents);
sh->linnums = swapushort(sh->linnums);
sh->flags = swapushort(sh->flags);
}
void
wordswap (struct buffer *buf, int size)
{
int i;
short tmp;
for (i = 0; i < size; i++) {
tmp = (buf->data[i] & 0x00ff);
tmp |= (buf->data[i] & 0xff00);
buf->data[i] = tmp;
}
}
*/
int
main (int argc, char *argv[])
{
char *targetfile;
char sectionname[9] = "";
long entryaddr = 0;
long tempentry;
FILE* infile;
long sectlen = 0;
long remaining = 0;
double headersize;
unsigned char *file2struct_ptr;
struct fheader fileheader;
/*struct sheader sectheader;*/
struct sheader1 sectheader1;
struct sheader2 sectheader2;
struct buffer inbuff;
long currentloc = 0;
int i, value;
char ofname[256], line[2];
FILE *ofp;
if (argc > 2) { /* Check if number of items (entries) on
the command line are greater than 2 */
if (0 == strcmp(argv[1], "-out")) {
/* if the second entry on the
command line is "-out" */
out = 1; /* variable out = true */
targetfile = argv[2]; /* 'targetfile' points to the third entry
on command line which is the input to
the "coff_new" routine ;
in this case, it is the executable
generated by TI tools as a .out file */
} else {
printf("usage: coff [-out] <coff_image>\n");
exit(1); /* if "-out" condition not satisfied,
then print error in usage message */
}
} else if (argc > 1) { /* if number of entries on command line
are greater than 1 */
targetfile = argv[1]; /* make 'targetfile' point to the second
entry on the command line */
} else {
printf("usage: coff [-out] <coff_image>\n");
/* else print error in usage message */
exit(1);
}
if (out) { /* if second entry on command line
= "-out" */
sprintf(ofname, "%s.c", targetfile);
/* prints out the filename pointed to by
targetfile into a string "ofname" */
if (NULL == (ofp = fopen(ofname, "w"))) {
/* file pointer "ofp" is assigned a
pointer to the file "ofname". If
"ofname" doesn't exist then, print
error message given below */
printf("cannot open file %s. errno = %d\n", ofname, errno);
return;
}
}
printf("Which type of COFF output file format are you using?\n");
printf("For v1.1 tools default format = COFF 1\n");
printf("For v1.2 tools default format = COFF 2\n");
printf("Enter '2' if using COFF2, enter '1' if using COFF1...followed by 'Enter'\n\n");
fgets(line, sizeof(line), stdin);
sscanf(line, "%d", &value);
if (value==1) {
/*
* Get File header.
*/
file2struct_ptr = (unsigned char *)&fileheader;
/* assigns the address of 'fileheader' to
file2struct_ptr ;
'fileheader' is a structure of type
'struct fheader' */
headersize = sizeof(fileheader);
/* headersize is assigned the size of
structure 'fileheader' */
if((infile = fopen(targetfile, "rb")) == NULL) {
/* if 'targetfile' does not exist then,
print error */
printf("Cannot open %s file\n", targetfile);
return(-1);
}
fread(file2struct_ptr, headersize,1,infile);
/* the 'file2struct_ptr' will read 1 entry
of 'headersize' bytes from the file
'infile' ;*/
// swap_fileheader(&fileheader);
printf("App File: %s %s", targetfile, ctime(&fileheader.stamp));
/* prompt info while invoking utility */
/*
* Skip optional header, if any.
*/
printf("Optional header = %d \n", fileheader.optbytes);
if(fileheader.optbytes) {
fseek(infile,fileheader.optbytes-2, SEEK_CUR);
/* Set the file position for "infile" to
optbytes-2 characters from where it
was left off by the previous fread
statement */
printf("COFF (.out) file\n");
}
/*
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -