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_in_place_load                   PORTABLE C      */
38 /*                                                           6.1          */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    Scott Larson, Microsoft Corporation                                 */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*    This function ensures the code-related parts of the module preamble */
46 /*    are valid and calls _txm_module_manager_internal_load to load the   */
47 /*    data and prepare the 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 /*    _txm_module_manager_internal_load Load data and prepare module for  */
62 /*                                        execution                       */
63 /*                                                                        */
64 /*  CALLED BY                                                             */
65 /*                                                                        */
66 /*    Application code                                                    */
67 /*                                                                        */
68 /*  RELEASE HISTORY                                                       */
69 /*                                                                        */
70 /*    DATE              NAME                      DESCRIPTION             */
71 /*                                                                        */
72 /*  09-30-2020      Scott Larson            Initial Version 6.1           */
73 /*                                                                        */
74 /**************************************************************************/
_txm_module_manager_in_place_load(TXM_MODULE_INSTANCE * module_instance,CHAR * module_name,VOID * module_location)75 UINT  _txm_module_manager_in_place_load(TXM_MODULE_INSTANCE *module_instance, CHAR *module_name, VOID *module_location)
76 {
77 
78 TXM_MODULE_PREAMBLE     *module_preamble;
79 ULONG                   code_size;
80 ULONG                   code_alignment;
81 ULONG                   code_allocation_size_ignored;
82 UINT                    status;
83 
84 
85     /* Pickup the module's information.  */
86     module_preamble =  (TXM_MODULE_PREAMBLE *) module_location;
87 
88     /* Pickup the basic module sizes.  */
89     code_size =  module_preamble -> txm_module_preamble_code_size;
90 
91     /* Check for valid sizes.  */
92     if (code_size == 0)
93     {
94 
95         /* Invalid module preamble.  */
96         return(TXM_MODULE_INVALID);
97     }
98 
99     /* Get the amount of the bytes we need to allocate for the module's code
100        as well as the required alignment. Note that because this is an in-place
101        load, we only want the code alignment so we can check it.  */
102     status =  _txm_module_manager_util_code_allocation_size_and_alignment_get(module_preamble, &code_alignment, &code_allocation_size_ignored);
103     if (status != TX_SUCCESS)
104     {
105 
106         /* Math overflow error occurred.  */
107         return(status);
108     }
109 
110     /* Since this is an in-place load, check the alignment of the module's instruction area (code).  */
111     TXM_MODULE_MANAGER_CHECK_CODE_ALIGNMENT(module_location, code_alignment)
112 
113     /* Now load the module in-place.  */
114     status =  _txm_module_manager_internal_load(module_instance, module_name, module_location,
115                                                 code_size, TX_NULL, 0);
116 
117     /* Return status.  */
118     return(status);
119 }
120