Skip to content

Commit 521dbed

Browse files
committed
add inquiry check length
1 parent cfdd55c commit 521dbed

File tree

3 files changed

+206
-132
lines changed

3 files changed

+206
-132
lines changed

common/usbx_device_classes/inc/ux_device_class_storage.h

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
/***************************************************************************
2-
* Copyright (c) 2024 Microsoft Corporation
3-
*
2+
* Copyright (c) 2024 Microsoft Corporation
3+
*
44
* This program and the accompanying materials are made available under the
55
* terms of the MIT License which is available at
66
* https://opensource.org/licenses/MIT.
7-
*
7+
*
88
* SPDX-License-Identifier: MIT
99
**************************************************************************/
1010

@@ -67,15 +67,15 @@
6767
#ifndef UX_DEVICE_CLASS_STORAGE_H
6868
#define UX_DEVICE_CLASS_STORAGE_H
6969

70-
/* Determine if a C++ compiler is being used. If so, ensure that standard
71-
C is used to process the API information. */
70+
/* Determine if a C++ compiler is being used. If so, ensure that standard
71+
C is used to process the API information. */
7272

73-
#ifdef __cplusplus
73+
#ifdef __cplusplus
7474

75-
/* Yes, C++ compiler is present. Use standard C. */
76-
extern "C" {
75+
/* Yes, C++ compiler is present. Use standard C. */
76+
extern "C" {
7777

78-
#endif
78+
#endif
7979

8080

8181
/* Internal option: enable the basic USBX error checking. This define is typically used
@@ -192,6 +192,13 @@ extern "C" {
192192
#define UX_SLAVE_CLASS_STORAGE_INQUIRY_RESPONSE_LENGTH 36
193193
#define UX_SLAVE_CLASS_STORAGE_INQUIRY_RESPONSE_LENGTH_CD_ROM 0x5b
194194

195+
/* Define Storage Class SCSI inquiry response length constants. */
196+
197+
#define UX_SLAVE_CLASS_STORAGE_INQUIRY_RESPONSE_VENDOR_ID_LENGTH 0x08U
198+
#define UX_SLAVE_CLASS_STORAGE_INQUIRY_RESPONSE_PRODUCT_ID_LENGTH 0x10U
199+
#define UX_SLAVE_CLASS_STORAGE_INQUIRY_RESPONSE_PRODUCT_REVISION_LENGTH 0x04U
200+
#define UX_SLAVE_CLASS_STORAGE_INQUIRY_RESPONSE_PRODUCT_SERIAL_LENGTH 0x14U
201+
195202

196203
/* Define Storage Class SCSI start/stop command constants. */
197204

@@ -674,10 +681,10 @@ UINT _uxe_device_class_storage_initialize(UX_SLAVE_CLASS_COMMAND *command);
674681

675682
#define ux_device_class_storage_entry _ux_device_class_storage_entry
676683

677-
/* Determine if a C++ compiler is being used. If so, complete the standard
678-
C conditional started above. */
684+
/* Determine if a C++ compiler is being used. If so, complete the standard
685+
C conditional started above. */
679686
#ifdef __cplusplus
680-
}
681-
#endif
687+
}
688+
#endif
682689

683690
#endif

common/usbx_device_classes/src/ux_device_class_storage_initialize.c

Lines changed: 103 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
/***************************************************************************
2-
* Copyright (c) 2024 Microsoft Corporation
3-
*
2+
* Copyright (c) 2024 Microsoft Corporation
3+
*
44
* This program and the accompanying materials are made available under the
55
* terms of the MIT License which is available at
66
* https://opensource.org/licenses/MIT.
7-
*
7+
*
88
* SPDX-License-Identifier: MIT
99
**************************************************************************/
1010

@@ -30,10 +30,10 @@
3030

3131
/* Define the Slave Storage Class Inquiry data : DO NOT CHANGE THE LENGTH OF THESE ITEMS */
3232

33-
UCHAR _ux_system_slave_class_storage_vendor_id[] = "AzureRTO";
34-
UCHAR _ux_system_slave_class_storage_product_id[] = "USBX storage dev";
35-
UCHAR _ux_system_slave_class_storage_product_rev[] = "2000";
36-
UCHAR _ux_system_slave_class_storage_product_serial[] = "12345678901234567890";
33+
UCHAR _ux_system_slave_class_storage_vendor_id[] = "Eclipse ";
34+
UCHAR _ux_system_slave_class_storage_product_id[] = "USBX STORAGE DEV";
35+
UCHAR _ux_system_slave_class_storage_product_rev[] = "2000";
36+
UCHAR _ux_system_slave_class_storage_product_serial[] = "12345678901234567890";
3737

3838
/**************************************************************************/
3939
/* */
@@ -92,6 +92,8 @@ UINT _ux_device_class_storage_initialize(UX_SLAVE_CLASS_COMMAND *command)
9292

9393
UINT status = UX_SUCCESS;
9494
UX_SLAVE_CLASS_STORAGE *storage;
95+
UX_SLAVE_CLASS_STORAGE_LUN *lun;
96+
UX_SLAVE_CLASS_STORAGE_LUN *parameter_lun;
9597
UX_SLAVE_CLASS_STORAGE_PARAMETER *storage_parameter;
9698
UX_SLAVE_CLASS *class_inst;
9799
ULONG lun_index;
@@ -119,18 +121,20 @@ ULONG lun_index;
119121

120122
/* Allocate bulk endpoint buffer. */
121123
UX_ASSERT(!UX_DEVICE_CLASS_STORAGE_ENDPOINT_BUFFER_SIZE_CALC_OVERFLOW);
122-
storage -> ux_device_class_storage_endpoint_buffer = _ux_utility_memory_allocate(UX_NO_ALIGN,
123-
UX_CACHE_SAFE_MEMORY, UX_DEVICE_CLASS_STORAGE_ENDPOINT_BUFFER_SIZE);
124-
#else
124+
125+
storage -> ux_device_class_storage_endpoint_buffer =
126+
_ux_utility_memory_allocate(UX_NO_ALIGN, UX_CACHE_SAFE_MEMORY, UX_DEVICE_CLASS_STORAGE_ENDPOINT_BUFFER_SIZE);
127+
#else /* UX_DEVICE_ENDPOINT_BUFFER_OWNER == 1 */
125128
status = UX_SUCCESS;
126-
#endif
129+
#endif /* UX_DEVICE_ENDPOINT_BUFFER_OWNER == 1 */
127130

128131
#if !defined(UX_DEVICE_STANDALONE)
129132

130133
/* Allocate some memory for the thread stack. */
131134
if (status == UX_SUCCESS)
132135
{
133-
class_inst -> ux_slave_class_thread_stack = _ux_utility_memory_allocate(UX_NO_ALIGN, UX_REGULAR_MEMORY, UX_THREAD_STACK_SIZE);
136+
class_inst -> ux_slave_class_thread_stack = _ux_utility_memory_allocate(UX_NO_ALIGN, UX_REGULAR_MEMORY,
137+
UX_THREAD_STACK_SIZE);
134138

135139
/* If it's OK, create thread. */
136140
if (class_inst -> ux_slave_class_thread_stack != UX_NULL)
@@ -139,18 +143,18 @@ ULONG lun_index;
139143
a new thread. We pass a pointer to the class to the new thread. This thread
140144
does not start until we have a instance of the class. */
141145
status = _ux_device_thread_create(&class_inst -> ux_slave_class_thread, "ux_slave_storage_thread",
142-
_ux_device_class_storage_thread,
143-
(ULONG) (ALIGN_TYPE) class_inst, (VOID *) class_inst -> ux_slave_class_thread_stack,
144-
UX_THREAD_STACK_SIZE, UX_THREAD_PRIORITY_CLASS,
145-
UX_THREAD_PRIORITY_CLASS, UX_NO_TIME_SLICE, UX_DONT_START);
146+
_ux_device_class_storage_thread, (ULONG) (ALIGN_TYPE) class_inst,
147+
(VOID *) class_inst -> ux_slave_class_thread_stack,
148+
UX_THREAD_STACK_SIZE, UX_THREAD_PRIORITY_CLASS,
149+
UX_THREAD_PRIORITY_CLASS, UX_NO_TIME_SLICE, UX_DONT_START);
146150
else
147151
status = UX_MEMORY_INSUFFICIENT;
148152
}
149-
#else
153+
#else /* !UX_DEVICE_STANDALONE */
150154

151155
/* Save tasks run entry. */
152156
class_inst -> ux_slave_class_task_function = _ux_device_class_storage_tasks_run;
153-
#endif
157+
#endif /* !UX_DEVICE_STANDALONE */
154158

155159
/* If thread resources allocated, go on. */
156160
if (status == UX_SUCCESS)
@@ -159,59 +163,97 @@ ULONG lun_index;
159163
UX_THREAD_EXTENSION_PTR_SET(&(class_inst -> ux_slave_class_thread), class_inst)
160164

161165
/* Store the number of LUN declared. */
162-
storage -> ux_slave_class_storage_number_lun = storage_parameter -> ux_slave_class_storage_parameter_number_lun;
166+
storage -> ux_slave_class_storage_number_lun =
167+
storage_parameter -> ux_slave_class_storage_parameter_number_lun;
163168

164169
/* Copy each individual LUN parameters. */
165170
for (lun_index = 0; lun_index < storage -> ux_slave_class_storage_number_lun; lun_index++)
166171
{
167172

173+
lun = &storage -> ux_slave_class_storage_lun[lun_index];
174+
parameter_lun = &storage_parameter -> ux_slave_class_storage_parameter_lun[lun_index];
175+
168176
/* Check block length size. */
169-
if (storage_parameter -> ux_slave_class_storage_parameter_lun[lun_index].ux_slave_class_storage_media_block_length > UX_SLAVE_CLASS_STORAGE_BUFFER_SIZE)
177+
if (parameter_lun -> ux_slave_class_storage_media_block_length > UX_SLAVE_CLASS_STORAGE_BUFFER_SIZE)
170178
{
171179
/* Cannot proceed. */
172180
status = (UX_MEMORY_INSUFFICIENT);
173181
break;
174182
}
175183

176184
/* Store all the application parameter information about the media. */
177-
storage -> ux_slave_class_storage_lun[lun_index].ux_slave_class_storage_media_last_lba = storage_parameter -> ux_slave_class_storage_parameter_lun[lun_index].ux_slave_class_storage_media_last_lba;
178-
storage -> ux_slave_class_storage_lun[lun_index].ux_slave_class_storage_media_block_length = storage_parameter -> ux_slave_class_storage_parameter_lun[lun_index].ux_slave_class_storage_media_block_length;
179-
storage -> ux_slave_class_storage_lun[lun_index].ux_slave_class_storage_media_type = storage_parameter -> ux_slave_class_storage_parameter_lun[lun_index].ux_slave_class_storage_media_type;
180-
storage -> ux_slave_class_storage_lun[lun_index].ux_slave_class_storage_media_removable_flag = storage_parameter -> ux_slave_class_storage_parameter_lun[lun_index].ux_slave_class_storage_media_removable_flag;
181-
storage -> ux_slave_class_storage_lun[lun_index].ux_slave_class_storage_media_read_only_flag = storage_parameter -> ux_slave_class_storage_parameter_lun[lun_index].ux_slave_class_storage_media_read_only_flag;
182-
storage -> ux_slave_class_storage_lun[lun_index].ux_slave_class_storage_media_read = storage_parameter -> ux_slave_class_storage_parameter_lun[lun_index].ux_slave_class_storage_media_read;
183-
storage -> ux_slave_class_storage_lun[lun_index].ux_slave_class_storage_media_flush = storage_parameter -> ux_slave_class_storage_parameter_lun[lun_index].ux_slave_class_storage_media_flush;
184-
storage -> ux_slave_class_storage_lun[lun_index].ux_slave_class_storage_media_write = storage_parameter -> ux_slave_class_storage_parameter_lun[lun_index].ux_slave_class_storage_media_write;
185-
storage -> ux_slave_class_storage_lun[lun_index].ux_slave_class_storage_media_status = storage_parameter -> ux_slave_class_storage_parameter_lun[lun_index].ux_slave_class_storage_media_status;
186-
storage -> ux_slave_class_storage_lun[lun_index].ux_slave_class_storage_media_notification = storage_parameter -> ux_slave_class_storage_parameter_lun[lun_index].ux_slave_class_storage_media_notification;
185+
lun -> ux_slave_class_storage_media_last_lba =
186+
parameter_lun -> ux_slave_class_storage_media_last_lba;
187+
188+
lun -> ux_slave_class_storage_media_block_length =
189+
parameter_lun -> ux_slave_class_storage_media_block_length;
190+
191+
lun -> ux_slave_class_storage_media_type =
192+
parameter_lun -> ux_slave_class_storage_media_type;
193+
194+
lun -> ux_slave_class_storage_media_removable_flag =
195+
parameter_lun -> ux_slave_class_storage_media_removable_flag;
196+
197+
lun -> ux_slave_class_storage_media_read_only_flag =
198+
parameter_lun -> ux_slave_class_storage_media_read_only_flag;
199+
200+
lun -> ux_slave_class_storage_media_read =
201+
parameter_lun -> ux_slave_class_storage_media_read;
202+
203+
lun -> ux_slave_class_storage_media_flush =
204+
parameter_lun -> ux_slave_class_storage_media_flush;
205+
206+
lun -> ux_slave_class_storage_media_write =
207+
parameter_lun -> ux_slave_class_storage_media_write;
208+
209+
lun -> ux_slave_class_storage_media_status =
210+
parameter_lun -> ux_slave_class_storage_media_status;
211+
212+
lun -> ux_slave_class_storage_media_notification =
213+
parameter_lun -> ux_slave_class_storage_media_notification;
187214
}
188215

189216
/* If it's OK, complete it. */
190217
if (status == UX_SUCCESS)
191218
{
192219

193220
/* Store the start and stop signals if needed by the application. */
194-
storage -> ux_slave_class_storage_instance_activate = storage_parameter -> ux_slave_class_storage_instance_activate;
195-
storage -> ux_slave_class_storage_instance_deactivate = storage_parameter -> ux_slave_class_storage_instance_deactivate;
221+
storage -> ux_slave_class_storage_instance_activate =
222+
storage_parameter -> ux_slave_class_storage_instance_activate;
223+
224+
storage -> ux_slave_class_storage_instance_deactivate =
225+
storage_parameter -> ux_slave_class_storage_instance_deactivate;
196226

197227
/* Store the vendor id, product id, product revision and product serial. */
198228
if (storage_parameter -> ux_slave_class_storage_parameter_vendor_id)
199-
storage -> ux_slave_class_storage_vendor_id = storage_parameter -> ux_slave_class_storage_parameter_vendor_id;
229+
{
230+
storage -> ux_slave_class_storage_vendor_id =
231+
storage_parameter -> ux_slave_class_storage_parameter_vendor_id;
232+
}
200233
else
201234
storage -> ux_slave_class_storage_vendor_id = _ux_system_slave_class_storage_vendor_id;
202235

203236
if (storage_parameter -> ux_slave_class_storage_parameter_product_id)
204-
storage -> ux_slave_class_storage_product_id = storage_parameter -> ux_slave_class_storage_parameter_product_id;
237+
{
238+
storage -> ux_slave_class_storage_product_id =
239+
storage_parameter -> ux_slave_class_storage_parameter_product_id;
240+
}
205241
else
206242
storage -> ux_slave_class_storage_product_id = _ux_system_slave_class_storage_product_id;
207243

208244
if (storage_parameter -> ux_slave_class_storage_parameter_product_rev)
209-
storage -> ux_slave_class_storage_product_rev = storage_parameter -> ux_slave_class_storage_parameter_product_rev;
245+
{
246+
storage -> ux_slave_class_storage_product_rev =
247+
storage_parameter -> ux_slave_class_storage_parameter_product_rev;
248+
}
210249
else
211250
storage -> ux_slave_class_storage_product_rev = _ux_system_slave_class_storage_product_rev;
212251

213252
if (storage_parameter -> ux_slave_class_storage_parameter_product_serial)
214-
storage -> ux_slave_class_storage_product_serial = storage_parameter -> ux_slave_class_storage_parameter_product_serial;
253+
{
254+
storage -> ux_slave_class_storage_product_serial =
255+
storage_parameter -> ux_slave_class_storage_parameter_product_serial;
256+
}
215257
else
216258
storage -> ux_slave_class_storage_product_serial = _ux_system_slave_class_storage_product_serial;
217259

@@ -228,12 +270,12 @@ ULONG lun_index;
228270
#if !defined(UX_DEVICE_STANDALONE)
229271
if (class_inst -> ux_slave_class_thread_stack != UX_NULL)
230272
_ux_utility_memory_free(&class_inst -> ux_slave_class_thread_stack);
231-
#endif
273+
#endif /* !UX_DEVICE_STANDALONE*/
232274

233275
#if UX_DEVICE_ENDPOINT_BUFFER_OWNER == 1
234276
if (storage -> ux_device_class_storage_endpoint_buffer != UX_NULL)
235277
_ux_utility_memory_free(storage -> ux_device_class_storage_endpoint_buffer);
236-
#endif
278+
#endif /* UX_DEVICE_ENDPOINT_BUFFER_OWNER == 1 */
237279

238280
/* Free instance. */
239281
_ux_utility_memory_free(storage);
@@ -242,7 +284,7 @@ ULONG lun_index;
242284
return(status);
243285
}
244286

245-
287+
#ifdef UX_ENABLE_ERROR_CHECKING
246288
/**************************************************************************/
247289
/* */
248290
/* FUNCTION RELEASE */
@@ -284,26 +326,36 @@ UINT _uxe_device_class_storage_initialize(UX_SLAVE_CLASS_COMMAND *command)
284326
{
285327

286328
UX_SLAVE_CLASS_STORAGE_PARAMETER *storage_parameter;
287-
UINT i;
329+
UX_SLAVE_CLASS_STORAGE_LUN *lun;
330+
UINT lun_index;
288331

289332
/* Get the pointer to the application parameters for the storage class. */
290333
storage_parameter = command -> ux_slave_class_command_parameter;
291334

292335
/* Sanity checks. */
293336
if (storage_parameter -> ux_slave_class_storage_parameter_number_lun > UX_MAX_SLAVE_LUN)
294337
return(UX_INVALID_PARAMETER);
295-
for (i = 0; i < storage_parameter -> ux_slave_class_storage_parameter_number_lun; i ++)
338+
339+
if ((storage_parameter -> ux_slave_class_storage_parameter_vendor_id != UX_NULL) &&
340+
(( _ux_utility_string_length_get(storage_parameter -> ux_slave_class_storage_parameter_vendor_id) != 8)) ||
341+
((storage_parameter -> ux_slave_class_storage_parameter_product_id != UX_NULL) &&
342+
( _ux_utility_string_length_get(storage_parameter -> ux_slave_class_storage_parameter_product_id) != 16)) ||
343+
((storage_parameter -> ux_slave_class_storage_parameter_product_rev != UX_NULL) &&
344+
( _ux_utility_string_length_get(storage_parameter -> ux_slave_class_storage_parameter_product_rev) != 4)) ||
345+
((storage_parameter -> ux_slave_class_storage_parameter_product_serial != UX_NULL) &&
346+
( _ux_utility_string_length_get(storage_parameter -> ux_slave_class_storage_parameter_product_serial) != 20)))
347+
return(UX_INVALID_PARAMETER);
348+
349+
for (lun_index = 0; lun_index < storage_parameter -> ux_slave_class_storage_parameter_number_lun; lun_index ++)
296350
{
297-
if ((storage_parameter -> ux_slave_class_storage_parameter_lun[i].
298-
ux_slave_class_storage_media_read == UX_NULL) ||
299-
(storage_parameter -> ux_slave_class_storage_parameter_lun[i].
300-
ux_slave_class_storage_media_write == UX_NULL) ||
301-
(storage_parameter -> ux_slave_class_storage_parameter_lun[i].
302-
ux_slave_class_storage_media_status == UX_NULL)
351+
lun = &storage_parameter -> ux_slave_class_storage_parameter_lun[lun_index];
352+
353+
if ((lun -> ux_slave_class_storage_media_read == UX_NULL) ||
354+
(lun -> ux_slave_class_storage_media_write == UX_NULL) ||
355+
(lun -> ux_slave_class_storage_media_status == UX_NULL)
303356
#if defined(UX_SLAVE_CLASS_STORAGE_INCLUDE_MMC)
304-
|| (storage_parameter -> ux_slave_class_storage_parameter_lun[i].
305-
ux_slave_class_storage_media_notification == UX_NULL)
306-
#endif
357+
|| (lun -> ux_slave_class_storage_media_notification == UX_NULL)
358+
#endif /* UX_SLAVE_CLASS_STORAGE_INCLUDE_MMC */
307359
)
308360
{
309361
return(UX_INVALID_PARAMETER);
@@ -313,3 +365,4 @@ UINT i;
313365
/* Invoke storage initialize function. */
314366
return(_ux_device_class_storage_initialize(command));
315367
}
368+
#endif /* UX_ENABLE_ERROR_CHECKING */

0 commit comments

Comments
 (0)