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