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_info_get                PORTABLE C      */
38 /*                                                           6.1          */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    Chaoqiong Xiao, Microsoft Corporation                               */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*    This function gets the current storage information block.           */
46 /*                                                                        */
47 /*  INPUT                                                                 */
48 /*                                                                        */
49 /*    pima                                       Pointer to pima class    */
50 /*    pima_session                               Pointer to pima session  */
51 /*    storage_id                                 The storage ID           */
52 /*                                                                        */
53 /*  OUTPUT                                                                */
54 /*                                                                        */
55 /*    Completion Status                                                   */
56 /*                                                                        */
57 /*  CALLS                                                                 */
58 /*                                                                        */
59 /*    _ux_host_class_pima_command           Pima command function         */
60 /*    _ux_utility_descriptor_parse          Parse descriptor              */
61 /*    _ux_utility_memory_allocate           Allocate memory               */
62 /*    _ux_utility_memory_copy               Copy memory                   */
63 /*    _ux_utility_memory_free               Free memory                   */
64 /*                                                                        */
65 /*  CALLED BY                                                             */
66 /*                                                                        */
67 /*    USB application                                                     */
68 /*                                                                        */
69 /*  RELEASE HISTORY                                                       */
70 /*                                                                        */
71 /*    DATE              NAME                      DESCRIPTION             */
72 /*                                                                        */
73 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
74 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
75 /*                                            verified memset and memcpy  */
76 /*                                            cases,                      */
77 /*                                            resulting in version 6.1    */
78 /*                                                                        */
79 /**************************************************************************/
_ux_host_class_pima_storage_info_get(UX_HOST_CLASS_PIMA * pima,UX_HOST_CLASS_PIMA_SESSION * pima_session,ULONG storage_id,UX_HOST_CLASS_PIMA_STORAGE * storage)80 UINT  _ux_host_class_pima_storage_info_get(UX_HOST_CLASS_PIMA *pima,
81                                         UX_HOST_CLASS_PIMA_SESSION *pima_session,
82                                         ULONG storage_id, UX_HOST_CLASS_PIMA_STORAGE *storage)
83 {
84 
85 UX_HOST_CLASS_PIMA_COMMAND           command;
86 UINT                                 status;
87 UCHAR                                *storage_buffer;
88 UCHAR                                *storage_pointer;
89 ULONG                                unicode_string_length;
90 
91     /* If trace is enabled, insert this event into the trace buffer.  */
92     UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_CLASS_PIMA_STORAGE_INFO_GET, pima, storage_id, storage, 0, UX_TRACE_HOST_CLASS_EVENTS, 0, 0)
93 
94     /* Check if this session is valid or not.  */
95     if (pima_session -> ux_host_class_pima_session_magic != UX_HOST_CLASS_PIMA_MAGIC_NUMBER)
96         return (UX_HOST_CLASS_PIMA_RC_SESSION_NOT_OPEN);
97 
98     /* Check if this session is opened or not.  */
99     if (pima_session -> ux_host_class_pima_session_state != UX_HOST_CLASS_PIMA_SESSION_STATE_OPENED)
100         return (UX_HOST_CLASS_PIMA_RC_SESSION_NOT_OPEN);
101 
102     /* Issue command to get the storage IDs.  1 parameter.  */
103     command.ux_host_class_pima_command_nb_parameters =  1;
104 
105     /* Parameter 1 is the Storage ID.  */
106     command.ux_host_class_pima_command_parameter_1 =  storage_id;
107 
108     /* Other parameters unused.  */
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_INFO.  */
115     command.ux_host_class_pima_command_operation_code =  UX_HOST_CLASS_PIMA_OC_GET_STORAGE_INFO;
116 
117     /* Allocate some DMA safe memory for receiving the storage info block.  */
118     storage_buffer =  _ux_utility_memory_allocate(UX_SAFE_ALIGN, UX_CACHE_SAFE_MEMORY, UX_HOST_CLASS_PIMA_STORAGE_MAX_LENGTH);
119     if (storage == UX_NULL)
120         return(UX_MEMORY_INSUFFICIENT);
121 
122     /* Issue the command.  */
123     status = _ux_host_class_pima_command(pima, &command, UX_HOST_CLASS_PIMA_DATA_PHASE_IN , storage_buffer,
124                                         UX_HOST_CLASS_PIMA_STORAGE_MAX_LENGTH, UX_HOST_CLASS_PIMA_STORAGE_MAX_LENGTH);
125 
126     /* Check the result. If the result is OK, the storage info block was read properly. */
127     if (status == UX_SUCCESS)
128     {
129         /* Uncompress the storage descriptor, at least the fixed part.  */
130         _ux_utility_descriptor_parse(storage_buffer,
131                             _ux_system_class_pima_object_structure,
132                             UX_HOST_CLASS_PIMA_OBJECT_ENTRIES,
133                             (UCHAR *) storage);
134 
135         /* Copy the storage description field.  Point to the beginning of the storage description string.  */
136         storage_pointer =  storage_buffer + UX_HOST_CLASS_PIMA_STORAGE_VARIABLE_OFFSET;
137 
138         /* Get the unicode string length.  */
139         unicode_string_length =  (ULONG) *storage_pointer ;
140 
141         /* Copy that string into the storage description field.  */
142         _ux_utility_memory_copy(storage -> ux_host_class_pima_storage_description, storage_pointer, unicode_string_length); /* Use case of memcpy is verified. */
143 
144         /* Point to the volume label.  */
145         storage_pointer =  storage_buffer + UX_HOST_CLASS_PIMA_STORAGE_VARIABLE_OFFSET + unicode_string_length;
146 
147         /* Get the unicode string length.  */
148         unicode_string_length =  (ULONG) *storage_pointer ;
149 
150         /* Copy that string into the storage volume label field.  */
151         _ux_utility_memory_copy(storage -> ux_host_class_pima_storage_volume_label, storage_pointer, unicode_string_length); /* Use case of memcpy is verified. */
152 
153     }
154 
155     /* Free the original storage info buffer.  */
156     _ux_utility_memory_free(storage_buffer);
157 
158     /* Return completion status.  */
159     return(status);
160 }
161 
162