Debian Codegraph @

doc/contrib_howtos/board_enable/reverse_engineering.rst

raw
1 ===========================================
2 Finding Board Enable by Reverse Engineering
3 ===========================================
4
5 **Note the document below was migrated from old website and the content >10yrs old**
6 Patches to add/update documentation are very much appreciated.
7
8 Finding the board enable code inside the flash rom seems like looking for a needle in a haystack,
9 but using the right search method might be as easy as finding a magnetic needle using a strong magnet.
10 This page explains approaches to find the board enable code in different vendor BIOSes.
11
12 Useful Tools
13 ============
14
15 For free/open source reverse engineering tools, take a look at objdump and ndisasm.
16
17 For objdump, use::
18
19 objdump -b binary -m i386 -M i8086,intel --disassemble-all datafile.bin
20
21 (you might want to leave off the "intel" option if you prefer the AT&T assembler syntax)
22
23 But as all free tools we know of are not comparable to the commercial tool `IDA Pro <http://www.hex-rays.com/idapro/>`_
24 which has free-as-in-beer version for non-commercial use `IDA 4.9 Freeware <http://www.hex-rays.com/idapro/idadownfreeware.htm>`_
25 which has all features needed for BIOS analysis, it should be mentioned here, too.
26
27 General Hints
28 =============
29
30 * Get an lspci of that board before you start. Best way is to request it from the board owner,
31 but you might find it on Google. Everest Reports of systems include an hexdump of config space at the end.
32
33 * Get used to the binary PCI address specification: Device/function ID is combined into one byte,
34 the device ID (0..31) is multiplied by eight and added/ored with the function ID (0..7).
35 Most PCI addresses in the BIOS have the resulting byte hardcoded.
36 So if this byte is 0xF9 for example, it is 1f.1 in lspci syntax.
37
38 * In/Out to fixed I/O addresses above 0x400 is usually accessing GPIO pins.
39 The base address can usually be found in PCI config space, but many BIOS vendors hard-code
40 the address as they know what address their own BIOS configures the GPIO address to.
41 Typical base values are 0x400 or 0x800 on Intel ICH and 0x4400 on nVidia MCP.
42 Check the GPIO setting function for the chipset on the board early to recognize patterns
43 in the assembly code (you *did* get the lspci before you started,
44 so you can easily look up which chipset and which GPIO method is used, didn't you?)
45
46 Vendor-specific Hints
47 =====================
48
49 Award BIOS
50 ----------
51
52 First, you need the runtime BIOS, this is the 128KB thats available at the addresses E0000-FFFFF
53 when the system is running. You can either obtain it by dumping from a running system,
54 or by running "lha x bios.bin" on a BIOS image as it gets written to the chip.
55
56 From the 128KB you only need the second half (the f-segment). In this segment, look for the text "AWDFLASH".
57 This signature is followed by eleven 16-bit procedure offsets (all these procedures reside in the segment F000).
58 The second procedure offset points to the board/chipset enable function.
59 The third procedure offset points to the board/chipset disable function.
60
61 Useful hints:
62
63 * PCI register manipulation is common. If you find a procedure that outputs something to port CF8,
64 it accesses PCI configuration space. If a second out instruction follows, it is a PCI config write,
65 if an in instruction follows, it's a PCI config read. In AWARD BIOS code,
66 the Device/Function ID is passed in CH, the config space address in CL. Bus number
67 (if used at all, check the procedure called) in BH or BL. Data is exchanged via AL/AX/EAX
68
69 AMI BIOS
70 --------
71
72 Get the runtime F-segment of the BIOS. Currently there is no automated extraction method
73 available than dumping from a running machine. Flashing is handled through Int 16
74 (entry point at F000:E82E, look for AH=E0, usually catched in the very beginning),
75 but can also be found in a different way. For each similar group of flash chips,
76 there is a call table with three 16 bit offsets for identifying, erasing and writing the chip.
77 In most AMI biosses, this table is quite closely followed by a pointer to the flash chip name
78 and a list of strings containing all flash chip names in this group. The identification procedure
79 adjusts the pointer to point to the right string. Finding flashing procedures can be done by searching
80 for magic constants like 0x2AA, 0xAAA, 0x2AAA and looking at the code. All low-level-flashing code
81 for one group is quite close together, so poking around to find the main entry points is feasible.
82
83 There is a table at some other place that (amongs other data) contains the offset
84 of the call table for each group. The offset of this table is referenced in the generic flashing code
85 that tends to be shortly before the group-specific code. Usually, on detection the address of one group
86 call table is stored in a "current group" pointer, which in the running system points to one
87 of the group entries, too. There will be a function that references this current group pointer
88 and calls the erase function [bx+2] (maybe via a looping helper function) and the write function
89 [bx+4] in quick succession. The function called before the erase function is the flash enable function.
90
91 If you go the forward route, trace Int 16, AX=E025 which should call the erase procedure.
92
93 Useful hints:
94
95 * PCI access in AMI commonly uses a bit fiddling function. This function takes the bus number in DH,
96 the device/function number in DL, a bit mask in BH, bits to be set in BL, and the config space address in AH.
97 It clears all bits set in BH and sets the bit set in BL. It returns the old value (all bits) in AL,
98 and sets the zero flag if all bits in BH were already clear. This function is wrapped by a generic bit setting
99 and a generic bit clearing function. These functions take a bit mask (usually only one bit set) in AL.
100 The bit setting function calls the bit manipulation function with BH=0 and BL=AL,
101 the bit clearing function calls the bit manipulation function with BH=AL and BL=0.
102
103 FOSDEM presentation
104 ===================
105
106 There was a talk at FOSDEM 2010 about reverse engineering board enables.
107 The `slides are here <http://people.freedesktop.org/~libv/flash_enable_bios_reverse_engineering_(FOSDEM2010_-_slides).pdf>`_