Skip to content

Commit

Permalink
Merge pull request #91 from binarly-io/save_module_type_as_json
Browse files Browse the repository at this point in the history
Dump UEFI module name and type in json
  • Loading branch information
yeggor authored Oct 5, 2024
2 parents 89f9648 + 6fe16b1 commit 6fde36f
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 21 deletions.
25 changes: 14 additions & 11 deletions efiXloader/uefitool.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ void efiloader::Uefitool::get_image_guid(qstring &image_guid,
}

std::vector<std::string>
efiloader::Uefitool::parseDepexSectionBody(const UModelIndex &index,
UString &parsed) {
efiloader::Uefitool::parse_depex_section_body(const UModelIndex &index,
UString &parsed) {
// Adopted from FfsParser::parseDepexSectionBody
std::vector<std::string> res;

Expand Down Expand Up @@ -180,7 +180,7 @@ efiloader::Uefitool::parseDepexSectionBody(const UModelIndex &index,
}

std::vector<std::string>
efiloader::Uefitool::parseAprioriRawSection(const UModelIndex &index) {
efiloader::Uefitool::parse_apriori_raw_section(const UModelIndex &index) {
// Adopted from FfsParser::parseDepexSectionBody
std::vector<std::string> res;

Expand Down Expand Up @@ -270,7 +270,8 @@ void efiloader::Uefitool::dump(const UModelIndex &index, uint8_t el_type,
if (images_guids[guid.c_str()]
.is_null()) { // check if GUID already exists
get_unique_name(module_name);
images_guids[guid.c_str()] = module_name.c_str();
images_guids[guid.c_str()] = {{"name", module_name.c_str()},
{"kind", get_kind(index)}};
file->qname.swap(module_name);
file->write();
files.push_back(file);
Expand Down Expand Up @@ -305,7 +306,8 @@ void efiloader::Uefitool::dump(const UModelIndex &index, uint8_t el_type,
files.push_back(file);
if (module_name.size()) {
// save image to the images_guids
images_guids[module_name.c_str()] = module_name.c_str();
images_guids[module_name.c_str()] = {{"name", module_name.c_str()},
{"kind", get_kind(index)}};
}
}
break;
Expand Down Expand Up @@ -340,7 +342,7 @@ void efiloader::Uefitool::get_deps(UModelIndex index, std::string key) {
qstring image_guid("");

get_image_guid(image_guid, index);
deps = parseDepexSectionBody(index, parsed);
deps = parse_depex_section_body(index, parsed);
if (deps.size()) {
msg("[efiXloader] dependency section for image with GUID %s: %s\n",
image_guid.c_str(), parsed.data);
Expand All @@ -352,22 +354,23 @@ void efiloader::Uefitool::get_apriori(UModelIndex index, std::string key) {
if (all_deps.contains(key)) {
return;
}
std::vector<std::string> deps = parseAprioriRawSection(index);
auto deps = parse_apriori_raw_section(index);
if (deps.empty()) {
return;
}
all_deps[key] = deps;
}

void efiloader::Uefitool::dump_jsons() {
// Dump deps
// dump JSON with DEPEX and GUIDs information for each image

std::filesystem::path out;
out /= get_path(PATH_TYPE_IDB);
out.replace_extension(".deps.json");
std::ofstream out_deps(out);
out_deps << std::setw(4) << all_deps << std::endl;
// Dump images
out_deps << std::setw(2) << all_deps << std::endl;

out.replace_extension("").replace_extension(".images.json");
std::ofstream out_guids(out);
out_guids << std::setw(4) << images_guids << std::endl;
out_guids << std::setw(2) << images_guids << std::endl;
}
21 changes: 14 additions & 7 deletions efiXloader/uefitool.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,15 +130,17 @@ class Uefitool {
}
void get_unique_name(qstring &image_name);
void get_image_guid(qstring &image_guid, UModelIndex index);
std::vector<std::string> parseDepexSectionBody(const UModelIndex &index,
UString &parsed);
std::vector<std::string> parseAprioriRawSection(const UModelIndex &index);
std::vector<std::string> parse_depex_section_body(const UModelIndex &index,
UString &parsed);
std::vector<std::string> parse_apriori_raw_section(const UModelIndex &index);
void get_deps(UModelIndex index, std::string key);
void get_apriori(UModelIndex index, std::string key);
void
dump_jsons(); // dump JSON with DEPEX and GUIDs information for each image
json all_deps; // DEPEX information for each image
json images_guids; // matching the modules to the parent's GUIDs
void dump_jsons();

// DEPEX information for each image
json all_deps;

json images_guids;
TreeModel model;
const char *buffer;
uint32_t buffer_size;
Expand All @@ -149,5 +151,10 @@ class Uefitool {
void set_machine_type(UByteArray pe_body);
uint16_t machine_type = 0xffff;
bool machine_type_detected = false;

private:
std::string get_kind(const UModelIndex &index) {
return fileTypeToUString(model.subtype(index.parent())).toLocal8Bit();
}
};
} // namespace efiloader
4 changes: 2 additions & 2 deletions efiXplorer/efi_deps.cc
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ json efi_deps_t::get_module_info(std::string module) {
// can not get name for module
continue;
}
std::string dmodule_name = m_modules_guids[dmodule_guid];
std::string dmodule_name = m_modules_guids[dmodule_guid]["name"];
if (dmodule_name == module) {
deps_protocols = element.value();
found = true;
Expand All @@ -268,7 +268,7 @@ string_set_t efi_deps_t::get_apriori_modules() {
for (auto file : files) {
auto modules = m_uefitool_deps[file];
for (auto &mguid : modules) {
std::string module = m_modules_guids[mguid];
std::string module = m_modules_guids[mguid]["name"];
apriori_modules.insert(module);
efi_utils::log("module from %s: %s\n", file.c_str(), module.c_str());
}
Expand Down
8 changes: 7 additions & 1 deletion efiXplorer/efi_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,13 @@ void attach_action_protocols_deps() {
// action handler for showing the sequence of modules execution
struct modules_seq_handler_t : public action_handler_t {
virtual int idaapi activate(action_activation_ctx_t *ctx) {
g_deps.build_modules_sequence();
try {
g_deps.build_modules_sequence();
} catch (std::exception &e) {
efi_utils::log("failed to build modules sequence: %s\n", e.what());
return -1;
}

std::string s = g_deps.m_modules_sequence.dump(2);
efi_utils::log("sequence of modules execution: %s\n", s.c_str());

Expand Down

0 comments on commit 6fde36f

Please sign in to comment.