3 // NOTE: THIS MAY NEED TO BE ADJUSTED
4 // change to reflect the total amount of ram in your system
7 return 0x10000000; // 256 MB
11 define /r GetWord(Addr)
15 if( (Addr & 0x2) == 0 )
19 //$printf "getword data is %x\n", data$;
24 data = dword(Addr & 0xfffffffc);
26 data = data / 0x10000;
27 //$printf "getword data is %x (1)\n", data$;
33 define /r ProcessPE32(imgstart)
34 unsigned long imgstart;
36 unsigned long filehdrstart;
37 unsigned long debugdirentryrva;
38 unsigned long debugtype;
39 unsigned long debugrva;
40 unsigned long dwarfsig;
41 unsigned long baseofcode;
42 unsigned long baseofdata;
43 unsigned long elfbase;
46 $printf "PE32 image found at %x",imgstart$;
48 //$printf "PE file hdr offset %x",dword(imgstart+0x3C)$;
50 // offset from dos hdr to PE file hdr
51 filehdrstart = imgstart + dword(imgstart+0x3C);
53 // offset to debug dir in PE hdrs
54 //$printf "debug dir is at %x",(filehdrstart+0xA8)$;
55 debugdirentryrva = dword(filehdrstart + 0xA8);
56 if(debugdirentryrva == 0)
58 $printf "no debug dir for image at %x",imgstart$;
62 //$printf "debug dir entry rva is %x",debugdirentryrva$;
64 debugtype = dword(imgstart + debugdirentryrva + 0xc);
65 if( (debugtype != 0xdf) && (debugtype != 0x2) )
67 $printf "debug type is not dwarf for image at %x",imgstart$;
68 $printf "debug type is %x",debugtype$;
72 debugrva = dword(imgstart + debugdirentryrva + 0x14);
73 dwarfsig = dword(imgstart + debugrva);
74 if(dwarfsig != 0x66727764)
76 $printf "dwarf debug signature not found for image at %x",imgstart$;
80 elfpath = (char *)(imgstart + debugrva + 0xc);
82 baseofcode = imgstart + dword(filehdrstart + 0x28);
83 baseofdata = imgstart + dword(filehdrstart + 0x2c);
85 if( (baseofcode < baseofdata) && (baseofcode != 0) )
94 $printf "found path %s",elfpath$;
95 $fprintf 50, "load /ni /np /a %s &0x%x\n",elfpath,elfbase$;
99 define /r ProcessTE(imgstart)
100 unsigned long imgstart;
102 unsigned long strippedsize;
103 unsigned long debugdirentryrva;
104 unsigned long debugtype;
105 unsigned long debugrva;
106 unsigned long dwarfsig;
107 unsigned long elfbase;
110 $printf "TE image found at %x",imgstart$;
112 // determine pe header bytes removed to account for in rva references
113 //strippedsize = word(imgstart + 0x6);
114 //strippedsize = (dword(imgstart + 0x4) & 0xffff0000) >> 16;
115 strippedsize = (dword(imgstart + 0x4) & 0xffff0000) / 0x10000;
116 strippedsize = strippedsize - 0x28;
118 debugdirentryrva = dword(imgstart + 0x20);
119 if(debugdirentryrva == 0)
121 $printf "no debug dir for image at %x",imgstart$;
124 debugdirentryrva = debugdirentryrva - strippedsize;
126 //$printf "debug dir entry rva is %x",debugdirentryrva$;
128 debugtype = dword(imgstart + debugdirentryrva + 0xc);
129 if( (debugtype != 0xdf) && (debugtype != 0x2) )
131 $printf "debug type is not dwarf for image at %x",imgstart$;
132 $printf "debug type is %x",debugtype$;
136 debugrva = dword(imgstart + debugdirentryrva + 0x14);
137 debugrva = debugrva - strippedsize;
138 dwarfsig = dword(imgstart + debugrva);
139 if( (dwarfsig != 0x66727764) && (dwarfsig != 0x3031424e) )
141 $printf "dwarf debug signature not found for image at %x",imgstart$;
142 $printf "found %x", dwarfsig$;
146 if( dwarfsig == 0x66727764 )
148 elfpath = (char *)(imgstart + debugrva + 0xc);
149 $printf "looking for elf path at 0x%x", elfpath$;
153 elfpath = (char *)(imgstart + debugrva + 0x10);
154 $printf "looking for elf path at 0x%x", elfpath$;
157 // elf base is baseofcode (we hope that for TE images it's not baseofdata)
158 elfbase = imgstart + dword(imgstart + 0xc) - strippedsize;
160 $printf "found path %s",elfpath$;
161 $fprintf 50, "load /ni /np /a %s &0x%x\n",elfpath,elfbase$;
165 define /r ProcessFvSection(secstart)
166 unsigned long secstart;
168 unsigned long sectionsize;
169 unsigned char sectiontype;
171 sectionsize = dword(secstart);
172 //sectiontype = (sectionsize & 0xff000000) >> 24;
173 sectiontype = (sectionsize & 0xff000000) / 0x1000000;
174 sectionsize = sectionsize & 0x00ffffff;
176 $printf "fv section at %x size %x type %x",secstart,sectionsize,sectiontype$;
178 if(sectiontype == 0x10) // PE32
180 ProcessPE32(secstart+0x4);
182 else if(sectiontype == 0x12) // TE
184 ProcessTE(secstart+0x4);
189 define /r ProcessFfsFile(ffsfilestart)
190 unsigned long ffsfilestart;
192 unsigned long ffsfilesize;
193 unsigned long ffsfiletype;
194 unsigned long secoffset;
195 unsigned long secsize;
197 //ffsfiletype = byte(ffsfilestart + 0x12);
198 ffsfilesize = dword(ffsfilestart + 0x14);
199 //ffsfiletype = (ffsfilesize & 0xff000000) >> 24;
200 ffsfiletype = (ffsfilesize & 0xff000000) / 0x1000000;
201 ffsfilesize = ffsfilesize & 0x00ffffff;
203 if(ffsfiletype == 0xff) return;
205 $printf "ffs file at %x size %x type %x",ffsfilestart,ffsfilesize,ffsfiletype$;
207 secoffset = ffsfilestart + 0x18;
209 // loop through sections in file
210 while(secoffset < (ffsfilestart + ffsfilesize))
212 // process fv section and increment section offset by size
213 secsize = dword(secoffset) & 0x00ffffff;
214 ProcessFvSection(secoffset);
215 secoffset = secoffset + secsize;
217 // align to next 4 byte boundary
218 if( (secoffset & 0x3) != 0 )
220 secoffset = secoffset + (0x4 - (secoffset & 0x3));
222 } // end section loop
226 define /r LoadPeiSec()
228 unsigned long fvbase;
231 unsigned long ffsoffset;
232 unsigned long ffsfilesize;
235 $printf "fvbase %x",fvbase$;
237 // get fv signature field
238 fvsig = dword(fvbase + 0x28);
239 if(fvsig != 0x4856465F)
241 $printf "FV does not have proper signature, exiting"$;
245 $printf "FV signature found"$;
247 $fopen 50, 'C:\loadfiles.inc'$;
249 fvlen = dword(fvbase + 0x20);
251 // first ffs file is after fv header, use headerlength field
252 //ffsoffset = (dword(fvbase + 0x30) & 0xffff0000) >> 16;
253 ffsoffset = (dword(fvbase + 0x30) & 0xffff0000) / 0x10000;
254 ffsoffset = fvbase + GetWord(fvbase + 0x30);
256 // loop through ffs files
257 while(ffsoffset < (fvbase+fvlen))
259 // process ffs file and increment by ffs file size field
260 ProcessFfsFile(ffsoffset);
261 ffsfilesize = (dword(ffsoffset + 0x14) & 0x00ffffff);
266 ffsoffset = ffsoffset + ffsfilesize;
269 // align to next 8 byte boundary
270 if( (ffsoffset & 0x7) != 0 )
272 ffsoffset = ffsoffset + (0x8 - (ffsoffset & 0x7));
282 define /r FindSystemTable(TopOfRam)
283 unsigned long TopOfRam;
285 unsigned long offset;
287 $printf "FindSystemTable"$;
288 $printf "top of mem is %x",TopOfRam$;
292 // align to highest 4MB boundary
293 offset = offset & 0xFFC00000;
295 // start at top and look on 4MB boundaries for system table ptr structure
298 //$printf "checking %x",offset$;
299 //$printf "value is %x",dword(offset)$;
301 // low signature match
302 if(dword(offset) == 0x20494249)
304 // high signature match
305 if(dword(offset+4) == 0x54535953)
308 if(dword(offset+0x0c) == 0)
310 // less than top of ram?
311 if(dword(offset+8) < TopOfRam)
313 return(dword(offset+8));
320 if(offset < 0x400000) break;
321 offset = offset - 0x400000;
328 define /r ProcessImage(ImageBase)
329 unsigned long ImageBase;
331 $printf "ProcessImage %x", ImageBase$;
335 define /r FindDebugInfo(SystemTable)
336 unsigned long SystemTable;
338 unsigned long CfgTableEntries;
339 unsigned long ConfigTable;
341 unsigned long offset;
342 unsigned long dbghdr;
343 unsigned long dbgentries;
344 unsigned long dbgptr;
345 unsigned long dbginfo;
346 unsigned long loadedimg;
348 $printf "FindDebugInfo"$;
351 CfgTableEntries = dword(SystemTable + 0x40);
352 ConfigTable = dword(SystemTable + 0x44);
354 $printf "config table is at %x (%d entries)", ConfigTable, CfgTableEntries$;
356 // now search for debug info entry with guid 49152E77-1ADA-4764-B7A2-7AFEFED95E8B
357 // 0x49152E77 0x47641ADA 0xFE7AA2B7 0x8B5ED9FE
358 for(i=0; i<CfgTableEntries; i++)
360 offset = ConfigTable + (i*0x14);
361 if(dword(offset) == 0x49152E77)
363 if(dword(offset+4) == 0x47641ADA)
365 if(dword(offset+8) == 0xFE7AA2B7)
367 if(dword(offset+0xc) == 0x8B5ED9FE)
369 dbghdr = dword(offset+0x10);
370 dbgentries = dword(dbghdr + 4);
371 dbgptr = dword(dbghdr + 8);
380 $printf "no debug entries found"$;
384 $printf "debug table at %x (%d entries)", dbgptr, dbgentries$;
386 for(i=0; i<dbgentries; i++)
388 dbginfo = dword(dbgptr + (i*4));
391 if(dword(dbginfo) == 1) // normal debug info type
393 loadedimg = dword(dbginfo + 4);
394 ProcessPE32(dword(loadedimg + 0x20));
403 unsigned long maxmem;
404 unsigned long systbl;
408 $fopen 50, 'C:\loadfiles.inc'$;
410 maxmem = GetMaxMem();
411 systbl = FindSystemTable(maxmem);
414 $printf "found system table at %x",systbl$;
415 FindDebugInfo(systbl);
422 define /r LoadRuntimeDxe()
425 unsigned long maxmem;
426 unsigned long SystemTable;
427 unsigned long CfgTableEntries;
428 unsigned long ConfigTable;
430 unsigned long offset;
431 unsigned long numentries;
432 unsigned long RuntimeDebugInfo;
433 unsigned long DebugInfoOffset;
434 unsigned long imgbase;
436 $printf "LoadRuntimeDxe"$;
438 $fopen 50, 'C:\loadfiles.inc'$;
440 RuntimeDebugInfo = 0x80000010;
442 if(RuntimeDebugInfo != 0)
444 numentries = dword(RuntimeDebugInfo);
446 $printf "runtime debug info is at %x (%d entries)", RuntimeDebugInfo, numentries$;
448 DebugInfoOffset = RuntimeDebugInfo + 0x4;
449 for(i=0; i<numentries; i++)
451 imgbase = dword(DebugInfoOffset);
454 $printf "found image at %x",imgbase$;
455 ProcessPE32(imgbase);
457 DebugInfoOffset = DebugInfoOffset + 0x4;