Line data Source code
1 1 : /*
2 : * Copyright (c) 2023 Intel Corporation
3 : *
4 : * SPDX-License-Identifier: Apache-2.0
5 : */
6 :
7 : #ifndef ZEPHYR_LLEXT_BUF_LOADER_H
8 : #define ZEPHYR_LLEXT_BUF_LOADER_H
9 :
10 : #include <zephyr/llext/loader.h>
11 :
12 : #ifdef __cplusplus
13 : extern "C" {
14 : #endif
15 :
16 : /**
17 : * @file
18 : * @brief LLEXT buffer loader implementation.
19 : *
20 : * @addtogroup llext_loader_apis
21 : * @{
22 : */
23 :
24 : /**
25 : * @brief Implementation of @ref llext_loader that reads from a memory buffer.
26 : */
27 1 : struct llext_buf_loader {
28 : /** Extension loader */
29 1 : struct llext_loader loader;
30 :
31 : /** @cond ignore */
32 : const uint8_t *buf;
33 : size_t len;
34 : size_t pos;
35 : /** @endcond */
36 : };
37 :
38 : /** @cond ignore */
39 : int llext_buf_read(struct llext_loader *ldr, void *buf, size_t len);
40 : int llext_buf_seek(struct llext_loader *ldr, size_t pos);
41 : void *llext_buf_peek(struct llext_loader *ldr, size_t pos);
42 :
43 : #define Z_LLEXT_BUF_LOADER(_buf, _buf_len, _storage) \
44 : { \
45 : .loader = \
46 : { \
47 : .prepare = NULL, \
48 : .read = llext_buf_read, \
49 : .seek = llext_buf_seek, \
50 : .peek = llext_buf_peek, \
51 : .finalize = NULL, \
52 : .storage = _storage, \
53 : }, \
54 : .buf = (_buf), \
55 : .len = (_buf_len), \
56 : .pos = 0, \
57 : }
58 : /** @endcond */
59 :
60 : /**
61 : * @brief Initializer for an llext_buf_loader structure
62 : *
63 : * The storage type for the provided buffer depends on the value of the
64 : * @kconfig{CONFIG_LLEXT_STORAGE_WRITABLE} option: if it is defined, the
65 : * buffer is assumed to be writable; otherwise it is assumed to be persistent.
66 : *
67 : * Consider using one of the alternative macros instead.
68 : *
69 : * @see LLEXT_TEMPORARY_BUF_LOADER
70 : * @see LLEXT_PERSISTENT_BUF_LOADER
71 : * @see LLEXT_WRITABLE_BUF_LOADER
72 : *
73 : * @param _buf Buffer containing the ELF binary
74 : * @param _buf_len Buffer length in bytes
75 : */
76 1 : #define LLEXT_BUF_LOADER(_buf, _buf_len) \
77 : Z_LLEXT_BUF_LOADER(_buf, _buf_len, \
78 : IS_ENABLED(CONFIG_LLEXT_STORAGE_WRITABLE) ? \
79 : LLEXT_STORAGE_WRITABLE : LLEXT_STORAGE_PERSISTENT)
80 :
81 : /**
82 : * @brief Initialize an llext_buf_loader structure for a temporary buffer
83 : *
84 : * ELF data from the specified buffer can only be used during llext_load().
85 : * The LLEXT subsystem will copy all necessary data to internal buffers at load
86 : * time.
87 : *
88 : * @param _buf Buffer containing the ELF binary
89 : * @param _buf_len Buffer length in bytes
90 : */
91 1 : #define LLEXT_TEMPORARY_BUF_LOADER(_buf, _buf_len) \
92 : Z_LLEXT_BUF_LOADER(_buf, _buf_len, LLEXT_STORAGE_TEMPORARY)
93 :
94 : /**
95 : * @brief Initialize an llext_buf_loader structure for a persistent, read-only buffer
96 : *
97 : * ELF data from the specified buffer is guaranteed to be accessible for as
98 : * long as the extension is loaded. The LLEXT subsystem may directly access the
99 : * ELF data, as long as no modification is required during loading.
100 : *
101 : * @param _buf Buffer containing the ELF binary
102 : * @param _buf_len Buffer length in bytes
103 : */
104 1 : #define LLEXT_PERSISTENT_BUF_LOADER(_buf, _buf_len) \
105 : Z_LLEXT_BUF_LOADER(_buf, _buf_len, LLEXT_STORAGE_PERSISTENT)
106 :
107 : /**
108 : * @brief Initialize an llext_buf_loader structure for a persistent, writable buffer
109 : *
110 : * ELF data from the specified buffer is guaranteed to be accessible for as
111 : * long as the extension is loaded. The LLEXT subsystem may directly access and
112 : * modify the ELF data.
113 : *
114 : * @param _buf Buffer containing the ELF binary
115 : * @param _buf_len Buffer length in bytes
116 : */
117 1 : #define LLEXT_WRITABLE_BUF_LOADER(_buf, _buf_len) \
118 : Z_LLEXT_BUF_LOADER(_buf, _buf_len, LLEXT_STORAGE_WRITABLE)
119 :
120 : /**
121 : * @}
122 : */
123 :
124 : #ifdef __cplusplus
125 : }
126 : #endif
127 :
128 : #endif /* ZEPHYR_LLEXT_BUF_LOADER_H */
|