This source file includes following definitions.
- dump_section
- print_offs
- get_flat_string
- main
1
2
3
4 #include <stddef.h>
5 #include <string.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <fcntl.h>
9
10 #include "myio.h"
11 #define USE_INT32_FOR_PTRS
12 #include "flt.h"
13
14 flat_hdr* flat;
15 unsigned char* flat_buf;
16 char* filename_elf="";
17 int FLAG_VERBOSE=0;
18 int FLAG_DUMP_FLAT=0;
19
20
21 void dump_section(char* name, unsigned char *ptr, int size )
22 {
23 printf("\n\nDump %s",name);
24
25 int i;
26 for(i=0;i<size;i++)
27 {
28 if ((i % 16)==0 ) {printf("\n%06x: ",i);}
29 if ((i % 16)==8 ) {printf("| ");}
30 printf("%02x ",ptr[i]);
31 }
32 printf("\n");
33 }
34
35 static
36 void print_offs(char *prefix, int offs,char* postfix)
37 {
38 int secoffs = 0;
39 char* sect="unkn";
40
41 if ( !offs ) {
42 printf("%s 0x00000000\n",prefix);
43 return;
44 }
45
46 if ( offs >=flat->entry && offs<flat->data_start )
47 { sect="text"; secoffs=flat->entry;}
48 else if ( offs >=flat->data_start && offs<flat->reloc_start )
49 { sect="data"; secoffs=flat->data_start;}
50 else if ( offs >=flat->reloc_start && offs<(flat->reloc_start+flat->bss_size) )
51 { sect="bss"; secoffs=flat->reloc_start;}
52 printf("%s 0x%08x (%s+0x%08x)%s",prefix, offs,sect,offs-secoffs,postfix);
53 }
54
55 char* get_flat_string( int32_t offs )
56 {
57 static char buf[200];
58
59 if ( offs <0 )
60 {
61 sprintf(buf," LANGID %d",-offs);
62 return buf;
63 }
64
65 if ( offs >=flat->reloc_start || offs<flat->data_start )
66 return "";
67
68 strncpy( buf, (char*)flat_buf+offs, sizeof(buf)-1);
69 buf[sizeof(buf)-1]=0;
70 return buf;
71 }
72
73 int main(int argc, char **argv)
74 {
75
76 if ( argc < 2 )
77 {
78 printf("fltdump.exe filename.flt [-f]\n -f = dump content of FLAT sections\n");
79 return 1;
80 }
81
82 if ( argc > 2 && argv[2][0]=='-' && argv[2][1]=='f' )
83 { FLAG_DUMP_FLAT=1;}
84
85 char* filename_flt = argv[1];
86
87 int rv;
88 if ( (rv=b_file_preload(filename_flt)) <= 0 )
89 {
90 fprintf(stderr, "Error load file '%s': loaded %d\n",filename_flt,rv);
91 return 1;
92 }
93
94
95 flat = (flat_hdr*) b_get_buf();
96 flat_buf = (unsigned char*)b_get_buf();
97
98 char magic[5];
99 memcpy(magic,&flat->magic,4);
100 magic[4]=0;
101
102 printf("\nFLT Headers:\n");
103 printf("->magic %s (flat rev.%d)\n", magic, flat->rev );
104
105 if (flat->magic != FLAT_MAGIC_NUMBER)
106 {
107 printf("This is not CHDK-FLAT!\n");
108 return 1;
109 }
110
111 if ( flat->rev != FLAT_VERSION )
112 {
113 printf("Bad FLAT revision! It is %d while should be %d\n", flat->rev, FLAT_VERSION);
114 }
115
116 int flat_reloc_count;
117 flat_reloc_count = (flat->import_start-flat->reloc_start)/sizeof(uint32_t);
118 int flat_import_count;
119 flat_import_count = (flat->import_size)/sizeof(uint32_t);
120
121
122 printf("->entry(.text) 0x%x (size %d)\n", flat->entry, flat->data_start - flat->entry );
123 printf("->data_start 0x%x (size %d)\n", flat->data_start, flat->reloc_start - flat->data_start );
124 printf("->bss_start 0x%x (size %d)\n", flat->reloc_start, flat->bss_size );
125 printf("->reloc_start 0x%x (size %d)\n", flat->reloc_start, flat->import_start - flat->reloc_start );
126 printf("->import_start 0x%x (size %d)\n", flat->import_start, flat->import_size );
127 printf("\n");
128
129 if ( flat->rev == FLAT_VERSION )
130 {
131 ModuleInfo* _module_info = (ModuleInfo*)(flat_buf + flat->_module_info_offset);
132 if ( _module_info->magicnum != MODULEINFO_V1_MAGICNUM )
133 {
134 printf("Malformed module info - bad magicnum!\n");
135 return 1;
136 }
137 if ( _module_info->sizeof_struct != sizeof(ModuleInfo) )
138 {
139 printf("Malformed module info - bad sizeof!\n");
140 return 1;
141 }
142
143
144 printf("\nModule info:\n");
145 printf("->Module Name: %s\n", get_flat_string(_module_info->moduleName) );
146 printf("->Module Ver: %d.%d\n", _module_info->module_version.major, _module_info->module_version.minor );
147
148 char* branches_str[] = {"any branch","CHDK", "CHDK_DE", "CHDK_SDM", "PRIVATEBUILD"};
149 int branch = (_module_info->chdk_required_branch>REQUIRE_CHDK_PRIVATEBUILD) ?
150 REQUIRE_CHDK_PRIVATEBUILD : _module_info->chdk_required_branch;
151 printf("->Require: %s-build%d. ", branches_str[branch], _module_info->chdk_required_ver );
152 if ( _module_info->chdk_required_platfid == 0 )
153 printf("Any platform.\n");
154 else
155 printf(" Platform #%d only.\n", _module_info->chdk_required_platfid );
156
157 print_offs("->lib = ", (int)_module_info->lib,"\n");
158
159
160
161
162 }
163
164 if ( !FLAG_DUMP_FLAT )
165 return 0;
166
167 dump_section( "FLT_header", flat_buf, sizeof(flat_hdr) );
168 dump_section( "FLT_text", flat_buf+flat->entry, flat->data_start-flat->entry );
169 dump_section( "FLT_data", flat_buf+flat->data_start, flat->reloc_start-flat->data_start);
170
171
172 int i;
173 printf("\nDump relocations 0x%x (size=%d):\n",flat->reloc_start,flat_reloc_count*sizeof(uint32_t));
174 for( i = 0; i< flat_reloc_count; i++)
175 print_offs("Offs: ",*(int*)(flat_buf+flat->reloc_start+i*sizeof(uint32_t)),"\n");
176
177 printf("\nDump imports 0x%x (size=%d):\n",flat->import_start,flat_import_count*sizeof(uint32_t));
178 uint32_t *new_import_buf = (uint32_t*)(flat_buf+flat->import_start);
179 for( i = 0; i< flat_import_count; i++)
180 {
181 uint32_t idx = new_import_buf[i++];
182 int cnt = new_import_buf[i] >> 24;
183 int j;
184 for (j=0; j<cnt; j++)
185 {
186 uint32_t offs = new_import_buf[i++] & 0x00FFFFFF;
187 print_offs((j==0)?"Offs: ":" ",offs,"");
188 int addend = *(uint32_t*)(flat_buf+offs);
189 printf(" = sym_%08x[%s]+0x%x\n",idx,get_import_symbol(idx),addend);
190 }
191 }
192
193 return 0;
194
195 }
196
197