1 /***************************************************************************
2 * Copyright (c) 2024 Microsoft Corporation
3 *
4 * This program and the accompanying materials are made available under the
5 * terms of the MIT License which is available at
6 * https://opensource.org/licenses/MIT.
7 *
8 * SPDX-License-Identifier: MIT
9 **************************************************************************/
10
11
12 /**************************************************************************/
13 /**************************************************************************/
14 /** */
15 /** ThreadX Component */
16 /** */
17 /** Module Manager */
18 /** */
19 /**************************************************************************/
20 /**************************************************************************/
21
22 #define TX_SOURCE_CODE
23
24 #include "tx_api.h"
25 #include "tx_initialize.h"
26 #include "tx_mutex.h"
27 #include "tx_thread.h"
28 #include "tx_byte_pool.h"
29 #include "txm_module.h"
30 #include "txm_module_manager_util.h"
31
32
33 /**************************************************************************/
34 /* */
35 /* FUNCTION RELEASE */
36 /* */
37 /* _txm_module_manager_memory_load PORTABLE C */
38 /* 6.1 */
39 /* AUTHOR */
40 /* */
41 /* Scott Larson, Microsoft Corporation */
42 /* */
43 /* DESCRIPTION */
44 /* */
45 /* This function allocates memory for module code and and calls */
46 /* _txm_module_manager_internal_load to load the data and prepare the */
47 /* module for execution. */
48 /* */
49 /* INPUT */
50 /* */
51 /* module_instance Module instance pointer */
52 /* module_name Module name pointer */
53 /* module_location Module code location */
54 /* */
55 /* OUTPUT */
56 /* */
57 /* status Completion status */
58 /* */
59 /* CALLS */
60 /* */
61 /* _tx_byte_allocate Allocate data area */
62 /* _txm_module_manager_internal_load Load data and prepare module for */
63 /* execution */
64 /* */
65 /* CALLED BY */
66 /* */
67 /* Application code */
68 /* */
69 /* RELEASE HISTORY */
70 /* */
71 /* DATE NAME DESCRIPTION */
72 /* */
73 /* 09-30-2020 Scott Larson Initial Version 6.1 */
74 /* */
75 /**************************************************************************/
_txm_module_manager_memory_load(TXM_MODULE_INSTANCE * module_instance,CHAR * module_name,VOID * module_location)76 UINT _txm_module_manager_memory_load(TXM_MODULE_INSTANCE *module_instance, CHAR *module_name, VOID *module_location)
77 {
78
79
80 TXM_MODULE_PREAMBLE *module_preamble;
81 ALIGN_TYPE code_start;
82 ULONG code_size;
83 ULONG code_alignment;
84 ULONG code_allocation_size;
85 CHAR *code_memory_ptr;
86 UCHAR *source_ptr;
87 UCHAR *destination_ptr;
88 ULONG copy_size;
89 ULONG i;
90 UINT status;
91
92
93 /* Pickup the module's information. */
94 module_preamble = (TXM_MODULE_PREAMBLE *) module_location;
95
96 /* Pickup the basic module sizes. */
97 code_size = module_preamble -> txm_module_preamble_code_size;
98
99 /* Check for valid sizes. */
100 if (code_size == 0)
101 {
102
103 /* Invalid module preamble. */
104 return(TXM_MODULE_INVALID);
105 }
106
107 /* Get the amount of the bytes we need to allocate for the module's code as well as the required alignment. */
108 status = _txm_module_manager_util_code_allocation_size_and_alignment_get(module_preamble, &code_alignment, &code_allocation_size);
109 if (status != TX_SUCCESS)
110 {
111
112 /* Math overflow error occurred. */
113 return(status);
114 }
115
116 /* Allocate code memory for the module. */
117 status = _tx_byte_allocate(&_txm_module_manager_byte_pool, (VOID **) &code_memory_ptr, code_allocation_size, TX_NO_WAIT);
118
119 /* Determine if the module's code memory allocation was successful. */
120 if (status != TX_SUCCESS)
121 {
122
123 /* No memory, return an error. */
124 return(TX_NO_MEMORY);
125 }
126
127 /* Copy the module code into memory. */
128 source_ptr = (UCHAR *) module_location;
129 code_start = (ALIGN_TYPE) code_memory_ptr;
130 code_start = (code_start + (code_alignment - 1)) & ~(code_alignment - 1);
131 destination_ptr = (UCHAR *) code_start;
132
133 /* Calculate the size. */
134 copy_size = module_preamble -> txm_module_preamble_code_size;
135
136 /* Loop to copy the code to RAM. */
137 for (i = 0; i < copy_size; i++)
138 {
139
140 /* Copy one byte at a time. */
141 *destination_ptr++ = *source_ptr++;
142 }
143
144 /* At this point, the module's instruction area is now in the RAM code area. */
145
146 /* Now load it in-place. */
147 status = _txm_module_manager_internal_load(module_instance, module_name, (VOID *) code_start,
148 code_size, code_memory_ptr, code_allocation_size);
149 if (status != TX_SUCCESS)
150 {
151
152 /* Release code memory. */
153 _tx_byte_release(code_memory_ptr);
154
155 /* Return error. */
156 return(status);
157 }
158
159 /* Return success. */
160 return(TX_SUCCESS);
161 }
162