1 /**************************************************************************/
2 /* */
3 /* Copyright (c) Microsoft Corporation. All rights reserved. */
4 /* */
5 /* This software is licensed under the Microsoft Software License */
6 /* Terms for Microsoft Azure RTOS. Full text of the license can be */
7 /* found in the LICENSE file at https://aka.ms/AzureRTOS_EULA */
8 /* and in the root directory of this software. */
9 /* */
10 /**************************************************************************/
11
12
13 /**************************************************************************/
14 /**************************************************************************/
15 /** */
16 /** ThreadX Component */
17 /** */
18 /** Module Manager */
19 /** */
20 /**************************************************************************/
21 /**************************************************************************/
22
23 #ifdef FX_FILEX_PRESENT
24
25 #define TX_SOURCE_CODE
26
27 #include "tx_api.h"
28 #include "tx_initialize.h"
29 #include "tx_mutex.h"
30 #include "tx_thread.h"
31 #include "tx_byte_pool.h"
32 #include "fx_api.h"
33 #include "txm_module.h"
34 #include "txm_module_manager_util.h"
35
36
37 /**************************************************************************/
38 /* */
39 /* FUNCTION RELEASE */
40 /* */
41 /* _txm_module_manager_file_load PORTABLE C */
42 /* 6.1 */
43 /* AUTHOR */
44 /* */
45 /* Scott Larson, Microsoft Corporation */
46 /* */
47 /* DESCRIPTION */
48 /* */
49 /* This function reads the module preamble, allocates memory for */
50 /* module code, loads the module code from the file, and calls */
51 /* _txm_module_manager_internal_load to load the data and prepare the */
52 /* module for execution. */
53 /* */
54 /* INPUT */
55 /* */
56 /* module_instance Module instance pointer */
57 /* module_name Name of module */
58 /* media_ptr FileX media pointer */
59 /* file_name Name of module binary file */
60 /* */
61 /* OUTPUT */
62 /* */
63 /* status Completion status */
64 /* */
65 /* CALLS */
66 /* */
67 /* fx_file_close Close file */
68 /* fx_file_open Open file */
69 /* fx_file_read File read */
70 /* fx_file_seek File seek */
71 /* _tx_byte_allocate Allocate data area */
72 /* _txm_module_manager_internal_load Load data and prepare module for */
73 /* execution */
74 /* */
75 /* CALLED BY */
76 /* */
77 /* Application code */
78 /* */
79 /* RELEASE HISTORY */
80 /* */
81 /* DATE NAME DESCRIPTION */
82 /* */
83 /* 09-30-2020 Scott Larson Initial Version 6.1 */
84 /* */
85 /**************************************************************************/
_txm_module_manager_file_load(TXM_MODULE_INSTANCE * module_instance,CHAR * module_name,FX_MEDIA * media_ptr,CHAR * file_name)86 UINT _txm_module_manager_file_load(TXM_MODULE_INSTANCE *module_instance, CHAR *module_name, FX_MEDIA *media_ptr, CHAR *file_name)
87 {
88
89
90 TXM_MODULE_PREAMBLE *module_preamble;
91 FX_FILE module_file;
92 TXM_MODULE_PREAMBLE preamble;
93 ULONG code_start;
94 ULONG code_size;
95 ULONG code_alignment;
96 ULONG code_allocation_size;
97 CHAR *code_memory_ptr;
98 UCHAR *destination_ptr;
99 ULONG actual_size;
100 UINT status;
101
102
103 /* Attempt to open the file. */
104 status = fx_file_open(media_ptr, &module_file, file_name, FX_OPEN_FOR_READ);
105
106 /* Check the file open status. */
107 if (status == FX_SUCCESS)
108 {
109
110 /* Read the preamble of the module. */
111 status = fx_file_read(&module_file, (VOID *) &preamble, sizeof(TXM_MODULE_PREAMBLE), &actual_size);
112
113 /* Check the file read status. */
114 if (status == FX_SUCCESS)
115 {
116
117 /* Check the number of bytes read. */
118 if (actual_size == sizeof(TXM_MODULE_PREAMBLE))
119 {
120
121 /* Pickup the module's information. */
122 module_preamble = (TXM_MODULE_PREAMBLE *) &preamble;
123
124 /* Pickup the module code size. */
125 code_size = module_preamble -> txm_module_preamble_code_size;
126
127 /* Check for valid sizes. */
128 if (code_size != 0)
129 {
130
131 /* Initialize module control block to all zeros. */
132 TX_MEMSET(module_instance, 0, sizeof(TXM_MODULE_INSTANCE));
133
134 /* Get the amount of the bytes we need to allocate for the module's code as well as the required alignment. */
135 status = _txm_module_manager_util_code_allocation_size_and_alignment_get(module_preamble, &code_alignment, &code_allocation_size);
136 if (status == TX_SUCCESS)
137 {
138
139 /* Allocate code memory for the module. */
140 status = _tx_byte_allocate(&_txm_module_manager_byte_pool, (VOID **) &code_memory_ptr, code_allocation_size, TX_NO_WAIT);
141
142 /* Determine if the module's code memory allocation was successful. */
143 if (status == TX_SUCCESS)
144 {
145
146 /* Prepare to read the module code into memory. */
147 code_start = (ULONG) code_memory_ptr;
148 code_start = (code_start + (code_alignment - 1)) & ~(code_alignment - 1);
149 destination_ptr = (UCHAR *) code_start;
150
151 /* Seek back to the beginning of the file. */
152 status = fx_file_seek(&module_file, 0);
153 if (status == FX_SUCCESS)
154 {
155
156 /* Read the module into memory. */
157 status = fx_file_read(&module_file, (VOID *) destination_ptr, code_size, &actual_size);
158 if (status == FX_SUCCESS)
159 {
160
161 /* Check the actual size read. */
162 if (actual_size == code_size)
163 {
164
165 /* At this point, the module's instruction area is now in the RAM code area. */
166
167 /* Now load it in-place. */
168 status = _txm_module_manager_internal_load(module_instance, module_name, (VOID *) code_start,
169 code_size, code_memory_ptr, code_allocation_size);
170 if (status == TX_SUCCESS)
171 {
172
173 /* Close the file. */
174 fx_file_close(&module_file);
175
176 /* Return success. */
177 return(TX_SUCCESS);
178 }
179 }
180 else
181 {
182
183 /* Invalid number of bytes read. */
184 status = TXM_MODULE_FILEX_INVALID_BYTES_READ;
185 }
186 }
187 }
188
189 /* Release the memory. */
190 _tx_byte_release(code_memory_ptr);
191 }
192 }
193 }
194 else
195 {
196
197 /* Invalid module preamble. */
198 status = TXM_MODULE_INVALID;
199 }
200 }
201 else
202 {
203
204 /* Invalid number of bytes read. */
205 status = TXM_MODULE_FILEX_INVALID_BYTES_READ;
206 }
207 }
208
209 /* Close the file. */
210 fx_file_close(&module_file);
211 }
212
213 /* Return success. */
214 return(status);
215 }
216
217 #endif
218