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 /** USBX Component                                                        */
17 /**                                                                       */
18 /**   PIMA Class                                                          */
19 /**                                                                       */
20 /**************************************************************************/
21 /**************************************************************************/
22 
23 
24 /* Include necessary system files.  */
25 
26 #define UX_SOURCE_CODE
27 
28 #include "ux_api.h"
29 #include "ux_host_class_pima.h"
30 #include "ux_host_stack.h"
31 
32 
33 /**************************************************************************/
34 /*                                                                        */
35 /*  FUNCTION                                               RELEASE        */
36 /*                                                                        */
37 /*    _ux_host_class_pima_storage_ids_get                 PORTABLE C      */
38 /*                                                           6.1.12       */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    Chaoqiong Xiao, Microsoft Corporation                               */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*    This function gets a list if the current valid Storage IDS. There   */
46 /*    is one Storage ID for each valid logical store.                     */
47 /*                                                                        */
48 /*  INPUT                                                                 */
49 /*                                                                        */
50 /*    pima                                       Pointer to pima class    */
51 /*    pima_session                               Pointer to pima session  */
52 /*    storage_ids_array                          Pointer to buffer to     */
53 /*                                               fill storage IDs         */
54 /*    storage_id_length                          Array length in N of IDs */
55 /*                                                                        */
56 /*  OUTPUT                                                                */
57 /*                                                                        */
58 /*    Completion Status                                                   */
59 /*                                                                        */
60 /*  CALLS                                                                 */
61 /*                                                                        */
62 /*    _ux_host_class_pima_command           Pima command function         */
63 /*    _ux_utility_memory_allocate           Allocate memory               */
64 /*    _ux_utility_memory_free               Free memory                   */
65 /*    _ux_utility_long_get                  Get 32-bit value              */
66 /*                                                                        */
67 /*  CALLED BY                                                             */
68 /*                                                                        */
69 /*    USB application                                                     */
70 /*                                                                        */
71 /*  RELEASE HISTORY                                                       */
72 /*                                                                        */
73 /*    DATE              NAME                      DESCRIPTION             */
74 /*                                                                        */
75 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
76 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
77 /*                                            resulting in version 6.1    */
78 /*  07-29-2022     Chaoqiong Xiao           Modified comment(s),          */
79 /*                                            improved array size check,  */
80 /*                                            resulting in version 6.1.12 */
81 /*                                                                        */
82 /**************************************************************************/
_ux_host_class_pima_storage_ids_get(UX_HOST_CLASS_PIMA * pima,UX_HOST_CLASS_PIMA_SESSION * pima_session,ULONG * storage_ids_array,ULONG storage_id_length)83 UINT  _ux_host_class_pima_storage_ids_get(UX_HOST_CLASS_PIMA *pima, UX_HOST_CLASS_PIMA_SESSION *pima_session,
84                                     ULONG *storage_ids_array, ULONG storage_id_length)
85 {
86 
87 UX_HOST_CLASS_PIMA_COMMAND           command;
88 UINT                                 status;
89 UCHAR                                *storage_ids;
90 ULONG                                count_storage_ids;
91 ULONG                                nb_storage_ids;
92 
93     /* If trace is enabled, insert this event into the trace buffer.  */
94     UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_CLASS_PIMA_STORAGE_IDS_GET, pima, storage_ids_array, storage_id_length, 0, UX_TRACE_HOST_CLASS_EVENTS, 0, 0)
95 
96     /* Check if this session is valid or not.  */
97     if (pima_session -> ux_host_class_pima_session_magic != UX_HOST_CLASS_PIMA_MAGIC_NUMBER)
98         return (UX_HOST_CLASS_PIMA_RC_SESSION_NOT_OPEN);
99 
100     /* Check if this session is opened or not.  */
101     if (pima_session -> ux_host_class_pima_session_state != UX_HOST_CLASS_PIMA_SESSION_STATE_OPENED)
102         return (UX_HOST_CLASS_PIMA_RC_SESSION_NOT_OPEN);
103 
104     /* Issue command to get the storage IDs.  No parameter.  */
105     command.ux_host_class_pima_command_nb_parameters =  0;
106 
107     /* Other parameters unused.  */
108     command.ux_host_class_pima_command_parameter_1 =  0;
109     command.ux_host_class_pima_command_parameter_2 =  0;
110     command.ux_host_class_pima_command_parameter_3 =  0;
111     command.ux_host_class_pima_command_parameter_4 =  0;
112     command.ux_host_class_pima_command_parameter_5 =  0;
113 
114     /* Then set the command to GET_STORAGE_IDS.  */
115     command.ux_host_class_pima_command_operation_code =  UX_HOST_CLASS_PIMA_OC_GET_STORAGE_IDS;
116 
117     /* Allocate some DMA safe memory for receiving the IDs.
118      * UX_HOST_CLASS_PIMA_STORAGE_IDS_LENGTH = (UX_HOST_CLASS_PIMA_MAX_STORAGE_IDS + 1) * 4,
119      * it is checked no calculation overflow.
120      */
121     storage_ids =  _ux_utility_memory_allocate(UX_SAFE_ALIGN, UX_CACHE_SAFE_MEMORY, UX_HOST_CLASS_PIMA_STORAGE_IDS_LENGTH);
122     if (storage_ids == UX_NULL)
123         return(UX_MEMORY_INSUFFICIENT);
124 
125     /* Issue the command.  */
126     status = _ux_host_class_pima_command(pima, &command, UX_HOST_CLASS_PIMA_DATA_PHASE_IN , storage_ids,
127                                         UX_HOST_CLASS_PIMA_STORAGE_IDS_LENGTH, UX_HOST_CLASS_PIMA_STORAGE_IDS_LENGTH);
128 
129     /* Check the result. If OK, the storage ID array is returned properly.  */
130     if (status == UX_SUCCESS)
131     {
132 
133         /* Read the number of Storage IDs in the returned array.  */
134         nb_storage_ids =  _ux_utility_long_get(storage_ids);
135 
136         /* Ensure we do read the array beyond the allocated memory. */
137         if (nb_storage_ids > UX_HOST_CLASS_PIMA_MAX_STORAGE_IDS)
138 
139             /* If we get here we should probably increase the value for UX_HOST_CLASS_PIMA_STORAGE_IDS_LENGTH  */
140             nb_storage_ids =  UX_HOST_CLASS_PIMA_MAX_STORAGE_IDS;
141 
142         /* Save the number of storage IDs.  */
143         pima_session ->  ux_host_class_pima_session_nb_storage_ids =  nb_storage_ids;
144 
145         /* Check if the user gave us enough memory.  */
146         if (nb_storage_ids > storage_id_length)
147 
148             /* No, not enough memory to store the array.  */
149             return(UX_MEMORY_INSUFFICIENT);
150 
151         /* Unpack all storage IDS.  */
152         for(count_storage_ids = 0; count_storage_ids < nb_storage_ids; count_storage_ids++)
153 
154             /* Unpack one ID at a time  */
155             *(storage_ids_array + count_storage_ids) = _ux_utility_long_get(storage_ids + sizeof(ULONG) +
156                                                                                                         (count_storage_ids * sizeof(ULONG)));
157     }
158 
159     /* Free the original storage ID buffer.  */
160     _ux_utility_memory_free(storage_ids);
161 
162     /* Return completion status.  */
163     return(status);
164 }
165