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
llext_fs_prepare(struct llext_loader * l)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
llext_fs_read(struct llext_loader * l,void * buf,size_t len)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
llext_fs_seek(struct llext_loader * l,size_t pos)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
llext_fs_finalize(struct llext_loader * l)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 }
66