Skip to content

Commit fc114e8

Browse files
awojasinskinashif
authored andcommitted
llext: Add filesystem based extension loader
Added loader allowing to load extensions stored in a filesystem. Signed-off-by: Adam Wojasinski <[email protected]>
1 parent 93ffece commit fc114e8

File tree

3 files changed

+139
-0
lines changed

3 files changed

+139
-0
lines changed

include/zephyr/llext/fs_loader.h

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright (c) 2024 BayLibre SAS
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#ifndef ZEPHYR_LLEXT_FS_LOADER_H
8+
#define ZEPHYR_LLEXT_FS_LOADER_H
9+
10+
#include <zephyr/llext/loader.h>
11+
#include <zephyr/fs/fs.h>
12+
13+
#ifdef __cplusplus
14+
extern "C" {
15+
#endif
16+
17+
/**
18+
* @file
19+
* @brief LLEXT filesystem loader implementation.
20+
*
21+
* @addtogroup llext_loader_apis
22+
* @{
23+
*/
24+
25+
/**
26+
* @brief Implementation of @ref llext_loader that reads from a filesystem.
27+
*/
28+
struct llext_fs_loader {
29+
/** Extension loader */
30+
struct llext_loader loader;
31+
32+
/** @cond ignore */
33+
bool is_open;
34+
const char *name;
35+
struct fs_file_t file;
36+
/** @endcond */
37+
};
38+
39+
/** @cond ignore */
40+
int llext_fs_prepare(struct llext_loader *ldr);
41+
int llext_fs_read(struct llext_loader *ldr, void *buf, size_t len);
42+
int llext_fs_seek(struct llext_loader *ldr, size_t pos);
43+
void llext_fs_finalize(struct llext_loader *ldr);
44+
/** @endcond */
45+
46+
/**
47+
* @brief Initializer for an llext_fs_loader structure
48+
*
49+
* @param _filename Absolute path to the extension file.
50+
*/
51+
#define LLEXT_FS_LOADER(_filename) \
52+
{ \
53+
.loader = \
54+
{ \
55+
.prepare = llext_fs_prepare, \
56+
.read = llext_fs_read, \
57+
.seek = llext_fs_seek, \
58+
.peek = NULL, \
59+
.finalize = llext_fs_finalize, \
60+
}, \
61+
.is_open = false, \
62+
.name = (_filename), \
63+
}
64+
65+
/**
66+
* @}
67+
*/
68+
69+
#ifdef __cplusplus
70+
}
71+
#endif
72+
73+
#endif /* ZEPHYR_LLEXT_FS_LOADER_H */

subsys/llext/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ if(CONFIG_LLEXT)
1111
llext_link.c
1212
llext_export.c
1313
buf_loader.c
14+
fs_loader.c
1415
)
1516
zephyr_library_sources_ifdef(CONFIG_LLEXT_SHELL shell.c)
1617
endif()

subsys/llext/fs_loader.c

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright (c) 2024 BayLibre SAS
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
*/
7+
8+
#include <zephyr/llext/fs_loader.h>
9+
#include <zephyr/sys/util.h>
10+
#include <string.h>
11+
12+
#include <zephyr/logging/log.h>
13+
LOG_MODULE_REGISTER(llext_fs_loader, CONFIG_LLEXT_LOG_LEVEL);
14+
15+
int llext_fs_prepare(struct llext_loader *l)
16+
{
17+
int ret = 0;
18+
struct llext_fs_loader *fs_l = CONTAINER_OF(l, struct llext_fs_loader, loader);
19+
20+
fs_file_t_init(&fs_l->file);
21+
22+
ret = fs_open(&fs_l->file, fs_l->name, FS_O_READ);
23+
if (ret != 0) {
24+
LOG_DBG("Failed opening a file: %d", ret);
25+
return ret;
26+
}
27+
28+
fs_l->is_open = true;
29+
return 0;
30+
}
31+
32+
int llext_fs_read(struct llext_loader *l, void *buf, size_t len)
33+
{
34+
int ret = 0;
35+
struct llext_fs_loader *fs_l = CONTAINER_OF(l, struct llext_fs_loader, loader);
36+
37+
if (fs_l->is_open) {
38+
ret = fs_read(&fs_l->file, buf, len);
39+
} else {
40+
ret = -EINVAL;
41+
}
42+
43+
return ret == len ? 0 : -EINVAL;
44+
}
45+
46+
int llext_fs_seek(struct llext_loader *l, size_t pos)
47+
{
48+
struct llext_fs_loader *fs_l = CONTAINER_OF(l, struct llext_fs_loader, loader);
49+
50+
if (fs_l->is_open) {
51+
return fs_seek(&fs_l->file, pos, FS_SEEK_SET);
52+
} else {
53+
return -EINVAL;
54+
}
55+
}
56+
57+
void llext_fs_finalize(struct llext_loader *l)
58+
{
59+
struct llext_fs_loader *fs_l = CONTAINER_OF(l, struct llext_fs_loader, loader);
60+
61+
if (fs_l->is_open) {
62+
fs_close(&fs_l->file);
63+
fs_l->is_open = false;
64+
}
65+
}

0 commit comments

Comments
 (0)