1 |
|
|
/*************************************************************************** |
2 |
|
|
* Copyright (c) 2024 Microsoft Corporation |
3 |
|
|
* Copyright (c) 2026-present Eclipse ThreadX contributors |
4 |
|
|
* |
5 |
|
|
* This program and the accompanying materials are made available under the |
6 |
|
|
* terms of the MIT License which is available at |
7 |
|
|
* https://opensource.org/licenses/MIT. |
8 |
|
|
* |
9 |
|
|
* SPDX-License-Identifier: MIT |
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 |
|
|
/**************************************************************************/ |
70 |
|
6 |
UINT _ux_host_class_storage_media_protection_check(UX_HOST_CLASS_STORAGE *storage) |
71 |
|
|
{ |
72 |
|
|
|
73 |
|
|
UINT status; |
74 |
|
|
UCHAR *cbw; |
75 |
|
|
UCHAR *mode_sense_response; |
76 |
|
|
UINT command_length; |
77 |
|
|
ULONG wp_parameter_location; |
78 |
|
|
|
79 |
|
|
|
80 |
|
|
/* Use a pointer for the cbw, easier to manipulate. */ |
81 |
|
6 |
cbw = (UCHAR *) storage -> ux_host_class_storage_cbw; |
82 |
|
|
|
83 |
|
|
/* Get the Write Command Length. */ |
84 |
|
|
#ifdef UX_HOST_CLASS_STORAGE_INCLUDE_LEGACY_PROTOCOL_SUPPORT |
85 |
|
|
if (storage -> ux_host_class_storage_interface -> ux_interface_descriptor.bInterfaceSubClass == UX_HOST_CLASS_STORAGE_SUBCLASS_UFI) |
86 |
|
|
command_length = UX_HOST_CLASS_STORAGE_MODE_SENSE_COMMAND_LENGTH_UFI; |
87 |
|
|
else |
88 |
|
|
command_length = UX_HOST_CLASS_STORAGE_MODE_SENSE_COMMAND_LENGTH_SBC; |
89 |
|
|
#else |
90 |
|
6 |
command_length = UX_HOST_CLASS_STORAGE_MODE_SENSE_COMMAND_LENGTH_SBC; |
91 |
|
|
#endif |
92 |
|
|
|
93 |
|
|
/* Initialize the CBW for this command. */ |
94 |
|
6 |
_ux_host_class_storage_cbw_initialize(storage, UX_HOST_CLASS_STORAGE_DATA_IN, UX_HOST_CLASS_STORAGE_MODE_SENSE_ALL_PAGE_LENGTH, command_length); |
95 |
|
|
|
96 |
|
|
/* Prepare the MODE_SENSE command block. Distinguish between SUBCLASSES. */ |
97 |
✓✓ |
6 |
switch (storage -> ux_host_class_storage_interface -> ux_interface_descriptor.bInterfaceSubClass) |
98 |
|
|
{ |
99 |
|
|
|
100 |
|
3 |
case UX_HOST_CLASS_STORAGE_SUBCLASS_RBC : |
101 |
|
|
#ifdef UX_HOST_CLASS_STORAGE_INCLUDE_LEGACY_PROTOCOL_SUPPORT |
102 |
|
|
case UX_HOST_CLASS_STORAGE_SUBCLASS_UFI : |
103 |
|
|
#endif |
104 |
|
3 |
*(cbw + UX_HOST_CLASS_STORAGE_CBW_CB + UX_HOST_CLASS_STORAGE_MODE_SENSE_OPERATION) = UX_HOST_CLASS_STORAGE_SCSI_MODE_SENSE; |
105 |
|
3 |
wp_parameter_location = UX_HOST_CLASS_STORAGE_MODE_SENSE_RESPONSE_ATTRIBUTES; |
106 |
|
3 |
break; |
107 |
|
|
|
108 |
|
3 |
default : |
109 |
|
3 |
*(cbw + UX_HOST_CLASS_STORAGE_CBW_CB + UX_HOST_CLASS_STORAGE_MODE_SENSE_OPERATION) = UX_HOST_CLASS_STORAGE_SCSI_MODE_SENSE_SHORT; |
110 |
|
3 |
wp_parameter_location = UX_HOST_CLASS_STORAGE_MODE_SENSE_RESPONSE_ATTRIBUTES_SHORT; |
111 |
|
3 |
break; |
112 |
|
|
} |
113 |
|
|
|
114 |
|
|
/* We ask for all pages. */ |
115 |
|
6 |
*(cbw + UX_HOST_CLASS_STORAGE_CBW_CB + UX_HOST_CLASS_STORAGE_MODE_SENSE_PC_PAGE_CODE) = UX_HOST_CLASS_STORAGE_MODE_SENSE_ALL_PAGE; |
116 |
|
|
|
117 |
|
|
/* Store the length of the Inquiry Response. */ |
118 |
|
6 |
_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); |
119 |
|
|
|
120 |
|
|
/* Obtain a block of memory for the answer. */ |
121 |
|
6 |
mode_sense_response = _ux_utility_memory_allocate(UX_SAFE_ALIGN, UX_CACHE_SAFE_MEMORY, UX_HOST_CLASS_STORAGE_MODE_SENSE_ALL_PAGE_LENGTH); |
122 |
✓✓ |
6 |
if (mode_sense_response == UX_NULL) |
123 |
|
1 |
return(UX_MEMORY_INSUFFICIENT); |
124 |
|
|
|
125 |
|
|
/* Send the command to transport layer. */ |
126 |
|
5 |
status = _ux_host_class_storage_transport(storage, mode_sense_response); |
127 |
|
|
|
128 |
|
|
/* Reset the Write Protected flag. */ |
129 |
|
5 |
storage -> ux_host_class_storage_write_protected_media = UX_FALSE; |
130 |
|
|
|
131 |
|
|
/* If we have a transport error, there is not much we can do, simply return the |
132 |
|
|
error. The default will be non protected disk. */ |
133 |
✓✓ |
5 |
if (status == UX_SUCCESS) |
134 |
|
|
{ |
135 |
|
|
/* Check to see that we have at least the header of the MODE_SENSE response, if not, ignore the data payload. |
136 |
|
|
Some devices do not stall this command but rather return 0 byte length. */ |
137 |
✓✓ |
4 |
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) |
138 |
|
|
{ |
139 |
|
|
|
140 |
|
|
/* The Mode Sense response tells us if the media is Write protected or not. */ |
141 |
✓✓ |
3 |
if (*(mode_sense_response + wp_parameter_location) & UX_HOST_CLASS_STORAGE_MODE_SENSE_RESPONSE_ATTRIBUTES_WP) |
142 |
|
|
|
143 |
|
|
/* The Mode Sense response tells us if the media is Write protected or not. */ |
144 |
|
1 |
storage -> ux_host_class_storage_write_protected_media = UX_TRUE; |
145 |
|
|
} |
146 |
|
|
} |
147 |
|
|
|
148 |
|
|
/* Free the memory resource used for the command response. */ |
149 |
|
5 |
_ux_utility_memory_free(mode_sense_response); |
150 |
|
|
|
151 |
|
|
/* Return completion status. */ |
152 |
|
5 |
return(status); |
153 |
|
|
} |
154 |
|
|
|