OpenCores
URL https://opencores.org/ocsvn/plasma/plasma/trunk

Subversion Repositories plasma

[/] [plasma/] [trunk/] [tools/] [convert.c] - Blame information for rev 27

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 rhoads
//convert.c by Steve Rhoads 4/26/01
2 27 rhoads
//set $gp and zero .sbss and .bss
3 2 rhoads
#include <stdio.h>
4
#include <stdlib.h>
5
 
6
#define BUF_SIZE (1024*1024)
7 27 rhoads
#define ntohl(A) ((A>>24)|((A&0x00ff0000)>>8)|((A&0xff00)<<8)|(A<<24))
8 2 rhoads
 
9 27 rhoads
enum {
10
   TEXT_OFFSET,  TEXT_LENGTH,
11
   RDATA_OFFSET, RDATA_LENGTH,
12
   DATA_OFFSET,  DATA_LENGTH,
13
   SDATA_OFFSET, SDATA_LENGTH,
14
   SBSS_OFFSET,  SBSS_LENGTH,
15
   BSS_OFFSET,   BSS_LENGTH
16
};
17
 
18
long code_start_offset=0x60;
19
unsigned long map[12];
20
long offset[]={
21
   0x58, 0x5c,  //.text offset,length
22
   0x80, 0x84,  //.rdata offset,length
23
   0xa8, 0xac,  //.data offset,length
24
   0xd0, 0xd4,  //.sdata offset,length
25
   0xf8, 0xfc,  //.sbss offset,length
26
   0x120,0x124  //.bss offset,length
27
};
28
 
29
unsigned long load(char *ptr,unsigned long address)
30
{
31
   unsigned long value;
32
   value=*(unsigned long*)(ptr+address);
33
   value=ntohl(value);
34
   return value;
35
}
36
 
37
void set_low(char *ptr,unsigned long address,unsigned long value)
38
{
39
   unsigned long opcode;
40
   opcode=*(unsigned long*)(ptr+address);
41
   opcode=ntohl(opcode);
42
   opcode=(opcode&0xffff0000)|(value&0xffff);
43
   opcode=ntohl(opcode);
44
   *(unsigned long*)(ptr+address)=opcode;
45
}
46
 
47 2 rhoads
int main(int argc,char *argv[])
48
{
49 26 rhoads
   FILE *infile,*outfile,*txtfile;
50 27 rhoads
   unsigned char *buf,*code;
51
   long size,code_offset;
52
   unsigned long i,d,gp_ptr;
53
 
54
   printf("test.exe -> code.txt & test2.exe\n");
55 2 rhoads
   infile=fopen("test.exe","rb");
56
   if(infile==NULL) {
57
      printf("Can't open test.exe");
58
      return 0;
59
   }
60
   buf=(unsigned char*)malloc(BUF_SIZE);
61
   size=fread(buf,1,BUF_SIZE,infile);
62
   fclose(infile);
63 27 rhoads
 
64
   code_offset=load(buf,code_start_offset);
65
   printf("code_offset=0x%x\n",code_offset);
66
   code=buf+code_offset;
67
   /*load all of the segment offsets and lengths*/
68
   for(i=0;i<12;++i) {
69
      map[i]=load(buf,offset[i]);
70
   }
71
   if(code_offset<0x120) {
72
      printf("no .sdata\n");
73
      for(i=6;i<12;i+=2) {
74
         map[i]=map[4];
75
         map[i+1]=map[5];
76
      }
77
   } else if(code_offset<0x140) {
78
      printf("no .rdata\n");
79
      for(i=11;i>4;--i) {
80
         map[i]=map[i-2];
81
      }
82
   }
83
   for(i=0;i<12;i+=2) {
84
      printf("0x%x 0x%x\n",map[i],map[i+1]);
85
   }
86
 
87
   /*Initialize the $gp register for sdata and sbss*/
88
   gp_ptr=map[SDATA_OFFSET]+0x8000;
89
   printf("gp_ptr=0x%x\n",gp_ptr);
90
   /*modify the first opcodes in boot.asm*/
91
   /*modify the lui opcode*/
92
   set_low(code,0,gp_ptr>>16);
93
   /*modify the ori opcode*/
94
   set_low(code,4,gp_ptr&0xffff);
95
 
96
   /*Clear .sbss and .bss*/
97
   printf(".sbss=0x%x .bss_end=0x%x\n",
98
      map[SBSS_OFFSET],map[BSS_OFFSET]+map[BSS_LENGTH]);
99
   set_low(code,8,map[SBSS_OFFSET]);
100
   set_low(code,12,map[BSS_OFFSET]+map[BSS_LENGTH]);
101
 
102
   /*write out code.txt*/
103 26 rhoads
   outfile=fopen("test2.exe","wb");
104
   txtfile=fopen("code.txt","w");
105 27 rhoads
   for(i=0;i<=map[SDATA_OFFSET]+map[SDATA_LENGTH];i+=4) {
106
      d=load(code,i);
107 26 rhoads
      fprintf(txtfile,"%8.8x\n",d);
108 27 rhoads
      fwrite(code+i,4,1,outfile);
109 2 rhoads
   }
110
   fclose(outfile);
111 26 rhoads
   fclose(txtfile);
112 2 rhoads
   free(buf);
113 27 rhoads
 
114 2 rhoads
   return 0;
115
}
116
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.