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 /**   Storage 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_storage.h"
30 #include "ux_host_stack.h"
31 
32 
33 /**************************************************************************/
34 /*                                                                        */
35 /*  FUNCTION                                               RELEASE        */
36 /*                                                                        */
37 /*    _ux_host_class_storage_media_protection_check       PORTABLE C      */
38 /*                                                           6.1          */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    Chaoqiong Xiao, Microsoft Corporation                               */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*    This function will send a MODE_SENSE command to retrieve the medium */
46 /*    and device parameters.                                              */
47 /*                                                                        */
48 /*  INPUT                                                                 */
49 /*                                                                        */
50 /*    storage                               Pointer to storage class      */
51 /*                                                                        */
52 /*  OUTPUT                                                                */
53 /*                                                                        */
54 /*    Completion Status                                                   */
55 /*                                                                        */
56 /*  CALLS                                                                 */
57 /*                                                                        */
58 /*    _ux_host_class_storage_cbw_initialize Initialize CBW                */
59 /*    _ux_host_class_storage_transport      Send command                  */
60 /*    _ux_utility_memory_allocate           Allocate memory block         */
61 /*    _ux_utility_memory_free               Release memory block          */
62 /*    _ux_utility_short_get_big_endian      Get short value               */
63 /*    _ux_utility_short_put_big_endian      Put short value               */
64 /*                                                                        */
65 /*  CALLED BY                                                             */
66 /*                                                                        */
67 /*    Storage Class                                                       */
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 /*                                            resulting in version 6.1    */
76 /*                                                                        */
77 /**************************************************************************/
_ux_host_class_storage_media_protection_check(UX_HOST_CLASS_STORAGE * storage)78 UINT  _ux_host_class_storage_media_protection_check(UX_HOST_CLASS_STORAGE *storage)
79 {
80 
81 UINT            status;
82 UCHAR             *cbw;
83 UCHAR             *mode_sense_response;
84 UINT            command_length;
85 ULONG           wp_parameter_location;
86 
87 
88     /* Use a pointer for the cbw, easier to manipulate.  */
89     cbw =  (UCHAR *) storage -> ux_host_class_storage_cbw;
90 
91     /* Get the Write Command Length.  */
92 #ifdef UX_HOST_CLASS_STORAGE_INCLUDE_LEGACY_PROTOCOL_SUPPORT
93     if (storage -> ux_host_class_storage_interface -> ux_interface_descriptor.bInterfaceSubClass == UX_HOST_CLASS_STORAGE_SUBCLASS_UFI)
94         command_length =  UX_HOST_CLASS_STORAGE_MODE_SENSE_COMMAND_LENGTH_UFI;
95     else
96         command_length =  UX_HOST_CLASS_STORAGE_MODE_SENSE_COMMAND_LENGTH_SBC;
97 #else
98     command_length =  UX_HOST_CLASS_STORAGE_MODE_SENSE_COMMAND_LENGTH_SBC;
99 #endif
100 
101     /* Initialize the CBW for this command.  */
102     _ux_host_class_storage_cbw_initialize(storage, UX_HOST_CLASS_STORAGE_DATA_IN, UX_HOST_CLASS_STORAGE_MODE_SENSE_ALL_PAGE_LENGTH, command_length);
103 
104     /* Prepare the MODE_SENSE command block.  Distinguish between SUBCLASSES. */
105     switch (storage -> ux_host_class_storage_interface -> ux_interface_descriptor.bInterfaceSubClass)
106     {
107 
108         case UX_HOST_CLASS_STORAGE_SUBCLASS_RBC         :
109 #ifdef UX_HOST_CLASS_STORAGE_INCLUDE_LEGACY_PROTOCOL_SUPPORT
110         case UX_HOST_CLASS_STORAGE_SUBCLASS_UFI         :
111 #endif
112             *(cbw + UX_HOST_CLASS_STORAGE_CBW_CB + UX_HOST_CLASS_STORAGE_MODE_SENSE_OPERATION) =  UX_HOST_CLASS_STORAGE_SCSI_MODE_SENSE;
113             wp_parameter_location =  UX_HOST_CLASS_STORAGE_MODE_SENSE_RESPONSE_ATTRIBUTES;
114             break;
115 
116         default                                         :
117             *(cbw + UX_HOST_CLASS_STORAGE_CBW_CB + UX_HOST_CLASS_STORAGE_MODE_SENSE_OPERATION) =  UX_HOST_CLASS_STORAGE_SCSI_MODE_SENSE_SHORT;
118             wp_parameter_location =  UX_HOST_CLASS_STORAGE_MODE_SENSE_RESPONSE_ATTRIBUTES_SHORT;
119             break;
120     }
121 
122     /* We ask for all pages.  */
123     *(cbw + UX_HOST_CLASS_STORAGE_CBW_CB + UX_HOST_CLASS_STORAGE_MODE_SENSE_PC_PAGE_CODE) = UX_HOST_CLASS_STORAGE_MODE_SENSE_ALL_PAGE;
124 
125     /* Store the length of the Inquiry Response.  */
126     _ux_utility_short_put_big_endian(cbw + UX_HOST_CLASS_STORAGE_CBW_CB + UX_HOST_CLASS_STORAGE_MODE_SENSE_PARAMETER_LIST_LENGTH, UX_HOST_CLASS_STORAGE_MODE_SENSE_ALL_PAGE_LENGTH);
127 
128     /* Obtain a block of memory for the answer.  */
129     mode_sense_response =  _ux_utility_memory_allocate(UX_SAFE_ALIGN, UX_CACHE_SAFE_MEMORY, UX_HOST_CLASS_STORAGE_MODE_SENSE_ALL_PAGE_LENGTH);
130     if (mode_sense_response == UX_NULL)
131         return(UX_MEMORY_INSUFFICIENT);
132 
133     /* Send the command to transport layer.  */
134     status =  _ux_host_class_storage_transport(storage, mode_sense_response);
135 
136     /* Reset the Write Protected flag. */
137     storage -> ux_host_class_storage_write_protected_media =  UX_FALSE;
138 
139     /* If we have a transport error, there is not much we can do, simply return the
140        error. The default will be non protected disk. */
141     if (status == UX_SUCCESS)
142     {
143         /* Check to see that we have at least the header of the MODE_SENSE response, if not, ignore the data payload.
144            Some devices do not stall this command but rather return 0 byte length.  */
145         if(_ux_utility_short_get_big_endian(mode_sense_response + UX_HOST_CLASS_STORAGE_MODE_SENSE_RESPONSE_MODE_DATA_LENGTH) >= UX_HOST_CLASS_STORAGE_MODE_SENSE_HEADER_PAGE_LENGTH)
146         {
147 
148             /* The Mode Sense response tells us if the media is Write protected or not. */
149             if (*(mode_sense_response + wp_parameter_location) & UX_HOST_CLASS_STORAGE_MODE_SENSE_RESPONSE_ATTRIBUTES_WP)
150 
151                 /* The Mode Sense response tells us if the media is Write protected or not. */
152                 storage -> ux_host_class_storage_write_protected_media =  UX_TRUE;
153         }
154     }
155 
156     /* Free the memory resource used for the command response.  */
157     _ux_utility_memory_free(mode_sense_response);
158 
159     /* Return completion status.  */
160     return(status);
161 }
162 
163