Ken Van Hoeylandt f8172446fd
App loading improvements and more (#675)
- External apps can opt into automatic cleanup when they exit, including tracked tasks, threads, files, directories, and memory.
- App-relative file paths resolve against the app’s working directory, with `.` and `..` normalized.
- The shell identifies ELF binaries that cannot be executed instead of treating them as scripts.
- Shared icon fonts now offer distinct default and large sizes, with updated icons across the interface.
- ESP32 apps can load additional ELF formats when address mirroring is enabled.
- Terminal colors render clearly on monochrome displays, and toolbar buttons have cleaner styling.
- GPS settings safely refresh their device list when their window is hidden or its widgets are destroyed.
2026-10-06 07:45:58 +02:00

60 lines
1.6 KiB
C++

// SPDX-License-Identifier: Apache-2.0
#include <app/elf_check.h>
#include <cstdio>
#include <cstring>
namespace {
constexpr size_t ELF_HEADER_PREFIX_SIZE = 20; // e_ident[16] + e_type(2) + e_machine(2)
constexpr uint8_t ELF_MAGIC[4] = { 0x7f, 'E', 'L', 'F' };
uint16_t read_le16(const uint8_t* bytes) {
return static_cast<uint16_t>(bytes[0] | (bytes[1] << 8));
}
} // namespace
bool elf_check_file(const char* path, const struct ElfRequirements* requirements) {
FILE* file = fopen(path, "rb");
if (file == nullptr) {
return false;
}
uint8_t header[ELF_HEADER_PREFIX_SIZE];
size_t read = fread(header, 1, sizeof(header), file);
fclose(file);
if (read != sizeof(header)) {
return false;
}
if (memcmp(header, ELF_MAGIC, sizeof(ELF_MAGIC)) != 0) {
return false;
}
uint8_t elf_class = header[4]; // e_ident[EI_CLASS]
uint8_t data = header[5]; // e_ident[EI_DATA]
uint16_t type = read_le16(header + 16); // e_type
uint16_t machine = read_le16(header + 18); // e_machine
return elf_class == requirements->elf_class
&& data == requirements->data
&& type < 32
&& (requirements->types & ELF_TYPE_MASK(type)) != 0
&& machine == requirements->machine;
}
bool elf_has_magic(const char* path) {
FILE* file = fopen(path, "rb");
if (file == nullptr) {
return false;
}
uint8_t magic[sizeof(ELF_MAGIC)];
size_t read = fread(magic, 1, sizeof(magic), file);
fclose(file);
return read == sizeof(magic) && memcmp(magic, ELF_MAGIC, sizeof(ELF_MAGIC)) == 0;
}