Skip to content

Commit 699049f

Browse files
authored
Add support for extra security bit (#273)
* Add support for extra_security bits * Only print in info -m
1 parent fa5f639 commit 699049f

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

bintool/metadata.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@
1616
#define DEBUG_LOG(...) ((void)0)
1717
#endif
1818

19+
// Support for SDK 2.1.0 & SDK 2.1.1 -----
20+
#ifndef PICOBIN_IMAGE_TYPE_EXE_EXTRA_SECURITY_BITS
21+
#define PICOBIN_IMAGE_TYPE_EXE_EXTRA_SECURITY_BITS _u(0x0800)
22+
#endif
23+
// ------
24+
1925
struct item;
2026

2127
template<typename InputIterator> std::vector<uint32_t> lsb_bytes_to_words(InputIterator begin, InputIterator end) {
@@ -176,6 +182,7 @@ struct image_type_item : public single_byte_size_item {
176182
image_type_exe_cpu cpu() const { return static_cast<image_type_exe_cpu>((flags & PICOBIN_IMAGE_TYPE_EXE_CPU_BITS) >> PICOBIN_IMAGE_TYPE_EXE_CPU_LSB); }
177183
image_type_exe_chip chip() const { return static_cast<image_type_exe_chip>((flags & PICOBIN_IMAGE_TYPE_EXE_CHIP_BITS) >> PICOBIN_IMAGE_TYPE_EXE_CHIP_LSB); }
178184
bool tbyb() const { return flags & PICOBIN_IMAGE_TYPE_EXE_TBYB_BITS; }
185+
bool extra_security() const { return flags & PICOBIN_IMAGE_TYPE_EXE_EXTRA_SECURITY_BITS; }
179186

180187
uint16_t flags;
181188
};

main.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3369,6 +3369,10 @@ void info_guts(memory_access &raw_access, void *con) {
33693369
if (image_def->tbyb()) {
33703370
info_pair("tbyb", "not bought");
33713371
}
3372+
3373+
if (verbose_metadata) {
3374+
info_pair("extra security", image_def->extra_security() ? "enabled" : "not enabled");
3375+
}
33723376
}
33733377

33743378
// Partition Table
@@ -5086,9 +5090,11 @@ void sign_guts_elf(elf_file* elf, private_t private_key, public_t public_key) {
50865090
new_block.items.push_back(version);
50875091
}
50885092

5089-
// Add entry point when signing Arm images
5093+
// Add entry point and vector table when signing Arm images, and set PICOBIN_IMAGE_TYPE_EXE_EXTRA_SECURITY_BITS
50905094
std::shared_ptr<image_type_item> image_type = new_block.get_item<image_type_item>();
50915095
if (settings.seal.sign && image_type != nullptr && image_type->image_type() == type_exe && image_type->cpu() == cpu_arm) {
5096+
// Set PICOBIN_IMAGE_TYPE_EXE_EXTRA_SECURITY_BITS
5097+
image_type->flags |= PICOBIN_IMAGE_TYPE_EXE_EXTRA_SECURITY_BITS;
50925098
std::shared_ptr<entry_point_item> entry_point = new_block.get_item<entry_point_item>();
50935099
if (entry_point == nullptr) {
50945100
std::shared_ptr<vector_table_item> vtor = new_block.get_item<vector_table_item>();
@@ -5107,6 +5113,9 @@ void sign_guts_elf(elf_file* elf, private_t private_key, public_t public_key) {
51075113
vtor_loc += rwd->addr;
51085114
}
51095115
}
5116+
5117+
vtor = std::make_shared<vector_table_item>(vtor_loc);
5118+
new_block.items.push_back(vtor);
51105119
}
51115120
auto segment = elf->segment_from_virtual_address(vtor_loc);
51125121
if (segment == nullptr) {
@@ -5167,15 +5176,20 @@ vector<uint8_t> sign_guts_bin(iostream_memory_access in, private_t private_key,
51675176
new_block.items.push_back(version);
51685177
}
51695178

5170-
// Add entry point when signing Arm images
5179+
// Add entry point and vector table when signing Arm images, and set PICOBIN_IMAGE_TYPE_EXE_EXTRA_SECURITY_BITS
51715180
std::shared_ptr<image_type_item> image_type = new_block.get_item<image_type_item>();
51725181
if (settings.seal.sign && image_type != nullptr && image_type->image_type() == type_exe && image_type->cpu() == cpu_arm) {
5182+
// Set PICOBIN_IMAGE_TYPE_EXE_EXTRA_SECURITY_BITS
5183+
image_type->flags |= PICOBIN_IMAGE_TYPE_EXE_EXTRA_SECURITY_BITS;
51735184
std::shared_ptr<entry_point_item> entry_point = new_block.get_item<entry_point_item>();
51745185
if (entry_point == nullptr) {
51755186
std::shared_ptr<vector_table_item> vtor = new_block.get_item<vector_table_item>();
51765187
uint32_t vtor_loc = bin_start;
51775188
if (vtor != nullptr) {
51785189
vtor_loc = vtor->addr;
5190+
} else {
5191+
vtor = std::make_shared<vector_table_item>(vtor_loc);
5192+
new_block.items.push_back(vtor);
51795193
}
51805194
auto offset = vtor_loc - bin_start;
51815195
uint32_t ep;

0 commit comments

Comments
 (0)