Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an entry point when signing Arm images #163

Merged
merged 5 commits into from
Nov 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4607,6 +4607,41 @@ void sign_guts_elf(elf_file* elf, private_t private_key, public_t public_key) {
new_block.items.push_back(version);
}

// Add entry point when signing Arm images
std::shared_ptr<image_type_item> image_type = new_block.get_item<image_type_item>();
if (settings.seal.sign && image_type != nullptr && image_type->image_type() == type_exe && image_type->cpu() == cpu_arm) {
std::shared_ptr<entry_point_item> entry_point = new_block.get_item<entry_point_item>();
if (entry_point == nullptr) {
std::shared_ptr<vector_table_item> vtor = new_block.get_item<vector_table_item>();
uint32_t vtor_loc = 0x10000000;
kilograham marked this conversation as resolved.
Show resolved Hide resolved
if (vtor != nullptr) {
vtor_loc = vtor->addr;
} else {
if (elf->header().entry >= SRAM_START) {
vtor_loc = 0x20000000;
} else if (elf->header().entry >= XIP_SRAM_START_RP2350) {
vtor_loc = 0x13ffc000;
} else {
vtor_loc = 0x10000000;
std::shared_ptr<rolling_window_delta_item> rwd = new_block.get_item<rolling_window_delta_item>();
if (rwd != nullptr) {
vtor_loc += rwd->addr;
}
}
}
auto segment = elf->segment_from_physical_address(vtor_loc);
auto content = elf->content(*segment);
auto offset = vtor_loc - segment->physical_address();
uint32_t ep;
memcpy(&ep, content.data() + offset + 4, sizeof(ep));
uint32_t sp;
memcpy(&sp, content.data() + offset, sizeof(sp));
DEBUG_LOG("Adding entry_point_item: ep %08x, sp %08x\n", ep, sp);
entry_point = std::make_shared<entry_point_item>(ep, sp);
new_block.items.push_back(entry_point);
}
}

hash_andor_sign(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Screenshot from 2024-11-19 23-27-37

😉

elf, &new_block, public_key, private_key,
settings.seal.hash, settings.seal.sign,
Expand Down Expand Up @@ -4643,6 +4678,27 @@ vector<uint8_t> sign_guts_bin(iostream_memory_access in, private_t private_key,
new_block.items.push_back(version);
}

// Add entry point when signing Arm images
will-v-pi marked this conversation as resolved.
Show resolved Hide resolved
std::shared_ptr<image_type_item> image_type = new_block.get_item<image_type_item>();
if (settings.seal.sign && image_type != nullptr && image_type->image_type() == type_exe && image_type->cpu() == cpu_arm) {
std::shared_ptr<entry_point_item> entry_point = new_block.get_item<entry_point_item>();
if (entry_point == nullptr) {
std::shared_ptr<vector_table_item> vtor = new_block.get_item<vector_table_item>();
uint32_t vtor_loc = bin_start;
if (vtor != nullptr) {
vtor_loc = vtor->addr;
}
auto offset = vtor_loc - bin_start;
uint32_t ep;
memcpy(&ep, bin.data() + offset + 4, sizeof(ep));
uint32_t sp;
memcpy(&sp, bin.data() + offset, sizeof(sp));
DEBUG_LOG("Adding entry_point_item: ep %08x, sp %08x\n", ep, sp);
entry_point = std::make_shared<entry_point_item>(ep, sp);
new_block.items.push_back(entry_point);
}
}

auto sig_data = hash_andor_sign(
bin, bin_start, bin_start,
&new_block, public_key, private_key,
Expand Down
Loading