|
| 1 | +# EFI Resolver |
| 2 | +Author: **Vector 35 Inc** |
| 3 | + |
| 4 | +_A Binary Ninja built-in plugin that automatically resolves type information for EFI protocol usage._ |
| 5 | + |
| 6 | +This repository contains C++ version of EFI Resolver, which is bundled with Binary Ninja. For the original Python |
| 7 | +version, please refer to https://github.com/vector35/efi-resolver/tree/main |
| 8 | + |
| 9 | +## Description: |
| 10 | + |
| 11 | +EFI Resolver is a Binary Ninja plugin that automates the task of resolving EFI protocol type information. It supports both DXE files and PEI files. It propagates parameter pointers from entry points to system table, MM system table, boot services, and runtime services to any global variables where they are stored. For PEI files, it also supports identifying [processor-specific mechanisms](https://uefi.org/specs/PI/1.8/V1_PEI_Foundation.html#pei-services-table-retrieval) for retrieving PEI services pointers. The plugin also identifies references to the boot services, MM protocol functions and PEI services, and applies type information according to the GUID passed to these functions. The plugin supports the core UEFI specification, and allows users to provide custom vendor protocols. |
| 12 | + |
| 13 | +## Build Instructions |
| 14 | + |
| 15 | +```bash |
| 16 | +git clone https://github.com/Vector35/binaryninja-api.git |
| 17 | +git clone https://github.com/Vector35/efi-resolver.git && cd efi-resolver |
| 18 | +export BN_API_PATH=../binaryninja-api # Or specifying the path to api repo |
| 19 | +cmake -S . -B build -GNinja |
| 20 | +cmake --build build -t install |
| 21 | +``` |
| 22 | + |
| 23 | +## License |
| 24 | + |
| 25 | +This plugin is released under an Apache-2.0 license. |
| 26 | + |
| 27 | +## Supplying Custom UEFI Protocol GUIDs and Types |
| 28 | + |
| 29 | +By default, EFI Resolver propagates types and GUIDs using Binary Ninja's native platform types for EFI. Many UEFI |
| 30 | +firmware binaries include types (and GUIDs) for proprietary protocols. This section describes how users can supply |
| 31 | +custom UEFI types and GUIDs for use with EFI Resolver type propagation. |
| 32 | + |
| 33 | +### User-supplied EFI GUIDs |
| 34 | + |
| 35 | +EFI Resolver uses a JSON file to associate user-supplied EFI GUIDs with types for propagation. GUIDs for proprietary |
| 36 | +protocol types can be used with EFI Resolver by creating a file at `<user folder>/types/efi-guids.json` containing JSON |
| 37 | +entries in the following format: |
| 38 | + |
| 39 | +```json |
| 40 | +{ |
| 41 | + "EFI_EXAMPLE_CUSTOM_PROTOCOL_GUID": [ |
| 42 | + 19088743, |
| 43 | + 35243, |
| 44 | + 52719, |
| 45 | + 1, |
| 46 | + 35, |
| 47 | + 69, |
| 48 | + 103, |
| 49 | + 137, |
| 50 | + 171, |
| 51 | + 205, |
| 52 | + 239 |
| 53 | + ] |
| 54 | +} |
| 55 | +``` |
| 56 | + |
| 57 | +In this example, the protocol type of `EFI_EXAMPLE_CUSTOM_PROTOCOL` is mapped to the |
| 58 | +`{0x01234567,0x89ab,0xcdef,{0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef}}` GUID (named `EFI_EXAMPLE_CUSTOM_PROTOCOL_GUID`). |
| 59 | +To test that the file is a valid JSON file, run `python -m json.tool < efi-guids.json`. |
| 60 | + |
| 61 | +__Note: user-supplied proprietary GUIDs from `efi-guids.json` are used to name variables regardless of whether or not an associated platform type has been loaded. If EFI Resolver fails to query the type for an EFI protocol interface, it will set the variable type for the protocol interface pointer to `VOID*`.__ |
| 62 | + |
| 63 | +### User-supplied EFI Platform Types |
| 64 | + |
| 65 | +Types and structures for proprietary protocols are to be imported using Binary Ninja's standard mechanism for loading |
| 66 | +user-supplied platform types. Instructions on adding custom platform types can be found [here](https://docs.binary.ninja/guide/types/platformtypes.html). Available EFI platform names include: |
| 67 | +- `efi-x86` |
| 68 | +- `efi-x86_64` |
| 69 | +- `efi-thumb2` |
| 70 | +- `efi-armv7` |
| 71 | +- `efi-aarch64` |
| 72 | +- `efi-windows-aarch64` |
| 73 | +- `efi-windows-x86` |
| 74 | +- `efi-windows-x86_64` |
| 75 | + |
| 76 | +To avoid having to add duplicate types in each platform-specific `*.c` file, it is recommended to add common types |
| 77 | +to a top-level `efi.c` file and `#include` the file in the platform-specific `*.c` files. For example: |
| 78 | + |
| 79 | +```C |
| 80 | +// <user folder>/types/platform/efi-x86_64.c including <user folder>/types/efi.c |
| 81 | +#include "../efi.c" |
| 82 | +``` |
| 83 | + |
| 84 | +To test that C source files containing custom EFI platform types are in the correct format, use the `bv.platform.parse_types_from_source_file` API. |
| 85 | + |
| 86 | +Alternatively, user types can be supplied manually from type libraries, header files, or any other mechanism supported |
| 87 | +by Binary Ninja. Just ensure that the name for types associated with GUIDs match what is in `efi-guids.json`. Protocol |
| 88 | +GUID names in `efi-guids.json` should end with `_PROTOCOL_GUID` and the prefix must be identical to the associated |
| 89 | +protocol type name. For example, if the GUID is named `EFI_EXAMPLE_PROTOCOL_GUID`, EFI Resolver will attempt to |
| 90 | +look up a type named `EFI_EXAMPLE_PROTOCOL`. |
| 91 | + |
| 92 | +### Full Example |
| 93 | + |
| 94 | +In summary, including a custom platform type of `EFI_EXAMPLE_CUSTOM_PROTOCOL` for the `efi-x86` platform and associating |
| 95 | +it with a GUID named `EFI_EXAMPLE_CUSTOM_PROTOCOL_GUID` requires two steps: |
| 96 | + |
| 97 | +1. Create the `<user folder>/types/platform/efi-x86.c` header file: |
| 98 | + |
| 99 | +```C |
| 100 | +struct EFI_EXAMPLE_CUSTOM_PROTOCOL |
| 101 | +{ |
| 102 | + uint32_t length; |
| 103 | +} |
| 104 | +``` |
| 105 | + |
| 106 | +2. Create the `<user folder>/types/efi-guids.json` file: |
| 107 | + |
| 108 | +```json |
| 109 | +{ |
| 110 | + "EFI_EXAMPLE_CUSTOM_PROTOCOL_GUID": [ |
| 111 | + 19088743, |
| 112 | + 35243, |
| 113 | + 52719, |
| 114 | + 1, |
| 115 | + 35, |
| 116 | + 69, |
| 117 | + 103, |
| 118 | + 137, |
| 119 | + 171, |
| 120 | + 205, |
| 121 | + 239 |
| 122 | + ] |
| 123 | +} |
| 124 | +``` |
| 125 | + |
| 126 | +After a Binary Ninja restart, when a binary is loaded with the `efi-x86` platform, the `EFI_EXAMPLE_CUSTOM_PROTOCOL` |
| 127 | +type will be imported. When EFI Resolver runs, it will detect uses of `EFI_EXAMPLE_CUSTOM_PROTOCOL_GUID` and propagate |
| 128 | +the `EFI_EXAMPLE_CUSTOM_PROTOCOL` type. |
0 commit comments