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