Skip to content

Commit 64120d6

Browse files
committed
Update avmpack API to return end section
Signed-off-by: Paul Guyot <pguyot@kallisys.net>
1 parent 86878f3 commit 64120d6

File tree

8 files changed

+15
-9
lines changed

8 files changed

+15
-9
lines changed

src/libAtomVM/avmpack.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ static inline int pad(int size)
3535

3636
bool avmpack_is_valid(const void *avmpack_binary, uint32_t size)
3737
{
38+
// "#!/usr/bin/env AtomVM"
3839
const unsigned char pack_header[AVMPACK_SIZE] = {
3940
0x23, 0x21, 0x2f, 0x75,
4041
0x73, 0x72, 0x2f, 0x62,
@@ -51,7 +52,7 @@ bool avmpack_is_valid(const void *avmpack_binary, uint32_t size)
5152
return memcmp(avmpack_binary, pack_header, AVMPACK_SIZE) == 0;
5253
}
5354

54-
int avmpack_find_section_by_flag(const void *avmpack_binary, uint32_t flags_mask, const void **ptr, uint32_t *size, const char **name)
55+
int avmpack_find_section_by_flag(const void *avmpack_binary, uint32_t flags_mask, uint32_t flags_val, const void **ptr, uint32_t *size, const char **name)
5556
{
5657
int offset = AVMPACK_SIZE;
5758
const uint32_t *flags;
@@ -60,7 +61,7 @@ int avmpack_find_section_by_flag(const void *avmpack_binary, uint32_t flags_mask
6061
const uint32_t *sizes = ((const uint32_t *) (avmpack_binary)) + offset / sizeof(uint32_t);
6162
flags = ((const uint32_t *) (avmpack_binary)) + 1 + offset / sizeof(uint32_t);
6263

63-
if ((ENDIAN_SWAP_32(*flags) & flags_mask) == flags_mask) {
64+
if ((ENDIAN_SWAP_32(*flags) & flags_mask) == flags_val) {
6465
const char *found_section_name = (const char *) (sizes + 3);
6566
int section_name_len = pad(strlen(found_section_name) + 1);
6667

src/libAtomVM/avmpack.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ extern "C" {
3939
#define END_OF_FILE 0
4040
#define BEAM_START_FLAG 1
4141
#define BEAM_CODE_FLAG 2
42+
#define END_OF_FILE_MASK 255
4243

4344
struct AVMPackData;
4445

@@ -104,12 +105,13 @@ typedef void *(*avmpack_fold_fun)(void *accum, const void *section_ptr, uint32_t
104105
* @details Finds an AVM Pack section that has certain flags set and returns a pointer to it, its size and its name.
105106
* @param avmpack_binary a pointer to valid AVM Pack file data.
106107
* @param flags_mask that will be matched against file sections.
108+
* @param flags_value that will be matched against file sections.
107109
* @param ptr will point to the found file section.
108110
* @param size will be set to the file section size that has been found, if the section has not been found it will not be updated.
109111
* @param name the section name, as defined in the module header.
110112
* @returns 1 if the file section has been found, 0 otherwise.
111113
*/
112-
int avmpack_find_section_by_flag(const void *avmpack_binary, uint32_t flags_mask, const void **ptr, uint32_t *size, const char **name);
114+
int avmpack_find_section_by_flag(const void *avmpack_binary, uint32_t flags_mask, uint32_t flags_value, const void **ptr, uint32_t *size, const char **name);
113115

114116
/**
115117
* @brief Finds an AVM Pack section that has certain name.

src/libAtomVM/nifs.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4806,7 +4806,7 @@ static term nif_atomvm_get_start_beam(Context *ctx, int argc, term argv[])
48064806
uint32_t size;
48074807
const void *beam;
48084808
const char *module_name;
4809-
if (!avmpack_find_section_by_flag(avmpack_data->data, BEAM_START_FLAG, &beam, &size, &module_name)) {
4809+
if (!avmpack_find_section_by_flag(avmpack_data->data, BEAM_START_FLAG, BEAM_START_FLAG, &beam, &size, &module_name)) {
48104810
synclist_unlock(&ctx->global->avmpack_data);
48114811
if (UNLIKELY(memory_ensure_free(ctx, TUPLE_SIZE(2)) != MEMORY_GC_OK)) {
48124812
RAISE_ERROR(OUT_OF_MEMORY_ATOM);

src/platforms/emscripten/src/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ static int load_module(const char *path)
5959
const void *startup_beam = NULL;
6060
uint32_t startup_beam_size;
6161
const char *startup_module_name;
62-
avmpack_find_section_by_flag(avmpack_data->data, 1, &startup_beam, &startup_beam_size, &startup_module_name);
62+
avmpack_find_section_by_flag(avmpack_data->data, BEAM_START_FLAG, BEAM_START_FLAG, &startup_beam, &startup_beam_size, &startup_module_name);
6363
if (startup_beam) {
6464
avmpack_data->in_use = true;
6565
main_module = module_new_from_iff_binary(global, startup_beam, startup_beam_size);

src/platforms/esp32/main/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ void app_main()
9898
ESP_LOGE(TAG, "Invalid startup avmpack. size=%u", size);
9999
AVM_ABORT();
100100
}
101-
if (!avmpack_find_section_by_flag(startup_avm, BEAM_START_FLAG, &startup_beam, &startup_beam_size, &startup_module_name)) {
101+
if (!avmpack_find_section_by_flag(startup_avm, BEAM_START_FLAG, BEAM_START_FLAG, &startup_beam, &startup_beam_size, &startup_module_name)) {
102102
ESP_LOGE(TAG, "Error: Failed to locate start module in startup partition. (Did you flash a library by mistake?)");
103103
AVM_ABORT();
104104
}

src/platforms/generic_unix/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ int main(int argc, char **argv)
104104
const void *startup_beam = NULL;
105105
const char *startup_module_name;
106106
uint32_t startup_beam_size;
107-
avmpack_find_section_by_flag(avmpack_data->data, 1, &startup_beam, &startup_beam_size, &startup_module_name);
107+
avmpack_find_section_by_flag(avmpack_data->data, BEAM_START_FLAG, BEAM_START_FLAG, &startup_beam, &startup_beam_size, &startup_module_name);
108108

109109
if (startup_beam) {
110110
avmpack_data->in_use = true;

src/platforms/rp2/src/main.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,12 @@ static int app_main()
8787
if (!avmpack_is_valid(MAIN_AVM, XIP_SRAM_BASE - (uintptr_t) MAIN_AVM)) {
8888
sleep_ms(5000);
8989
fprintf(stderr, "Fatal error: invalid main.avm packbeam\n");
90+
if (avmpack_is_valid(LIB_AVM, (uintptr_t) MAIN_AVM - (uintptr_t) LIB_AVM)) {
91+
fprintf(stderr, "Lib avm packbeam is valid, though\n");
92+
}
9093
AVM_ABORT();
9194
}
92-
if (!avmpack_find_section_by_flag(MAIN_AVM, BEAM_START_FLAG, &startup_beam, &startup_beam_size, &startup_module_name)) {
95+
if (!avmpack_find_section_by_flag(MAIN_AVM, BEAM_START_FLAG, BEAM_START_FLAG, &startup_beam, &startup_beam_size, &startup_module_name)) {
9396
sleep_ms(5000);
9497
fprintf(stderr, "Fatal error: Failed to locate start module in main.avm packbeam. (Did you flash a library by mistake?)");
9598
AVM_ABORT();

src/platforms/stm32/src/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ int main()
248248
port_driver_init_all(glb);
249249
nif_collection_init_all(glb);
250250

251-
if (!avmpack_is_valid(flashed_avm, size) || !avmpack_find_section_by_flag(flashed_avm, BEAM_START_FLAG, &startup_beam, &startup_beam_size, &startup_module_name)) {
251+
if (!avmpack_is_valid(flashed_avm, size) || !avmpack_find_section_by_flag(flashed_avm, BEAM_START_FLAG, BEAM_START_FLAG, &startup_beam, &startup_beam_size, &startup_module_name)) {
252252
AVM_LOGE(TAG, "Invalid AVM Pack");
253253
AVM_ABORT();
254254
}

0 commit comments

Comments
 (0)