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 /**   Storage 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_storage.h"
29 #include "ux_host_stack.h"
30 
31 
32 /**************************************************************************/
33 /*                                                                        */
34 /*  FUNCTION                                               RELEASE        */
35 /*                                                                        */
36 /*    _ux_host_class_storage_cbw_initialize               PORTABLE C      */
37 /*                                                           6.1.3        */
38 /*  AUTHOR                                                                */
39 /*                                                                        */
40 /*    Chaoqiong Xiao, Microsoft Corporation                               */
41 /*                                                                        */
42 /*  DESCRIPTION                                                           */
43 /*                                                                        */
44 /*    This function will initialize the Command Block Wrapper (CBW) that  */
45 /*    encapsulate the SCSI request to be sent to the storage device.      */
46 /*    The CBW is normally used only for the BO protocol.                  */
47 /*                                                                        */
48 /*  INPUT                                                                 */
49 /*                                                                        */
50 /*    storage                               Pointer to storage class      */
51 /*    flags                                 Flags for transfer            */
52 /*    data_transfer_length                  Length of data transfer       */
53 /*    command_length                        Length of command             */
54 /*                                                                        */
55 /*  OUTPUT                                                                */
56 /*                                                                        */
57 /*    None                                                                */
58 /*                                                                        */
59 /*  CALLS                                                                 */
60 /*                                                                        */
61 /*    _ux_utility_long_put                  Write a 32-bit value          */
62 /*    _ux_utility_memory_set                Set memory to a value         */
63 /*                                                                        */
64 /*  CALLED BY                                                             */
65 /*                                                                        */
66 /*    Storage Class                                                       */
67 /*                                                                        */
68 /*  RELEASE HISTORY                                                       */
69 /*                                                                        */
70 /*    DATE              NAME                      DESCRIPTION             */
71 /*                                                                        */
72 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
73 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
74 /*                                            verified memset and memcpy  */
75 /*                                            cases,                      */
76 /*                                            resulting in version 6.1    */
77 /*  12-31-2020     Chaoqiong Xiao           Modified comment(s),          */
78 /*                                            resulting in version 6.1.3  */
79 /*                                                                        */
80 /**************************************************************************/
_ux_host_class_storage_cbw_initialize(UX_HOST_CLASS_STORAGE * storage,UINT flags,ULONG data_transfer_length,UINT command_length)81 VOID  _ux_host_class_storage_cbw_initialize(UX_HOST_CLASS_STORAGE *storage, UINT flags,
82                                             ULONG data_transfer_length, UINT command_length)
83 {
84 
85 UCHAR   *cbw;
86 
87 
88     /* Use a pointer for the cbw, easier to manipulate.  */
89     cbw =  (UCHAR *) storage -> ux_host_class_storage_cbw;
90 
91     /* Store the signature of the CBW.  */
92     _ux_utility_long_put(cbw, UX_HOST_CLASS_STORAGE_CBW_SIGNATURE_MASK);
93 
94     /* Set the Tag, this value is simply an arbitrary number that is echoed by
95        the device in the CSW.  */
96     _ux_utility_long_put(cbw + UX_HOST_CLASS_STORAGE_CBW_TAG, UX_HOST_CLASS_STORAGE_CBW_TAG_MASK);
97 
98     /* Store the Data Transfer Length expected for the data payload.  */
99     _ux_utility_long_put(cbw + UX_HOST_CLASS_STORAGE_CBW_DATA_LENGTH, data_transfer_length);
100 
101     /* Store the CBW Flag field that contains the transfer flags.  */
102     *(cbw + UX_HOST_CLASS_STORAGE_CBW_FLAGS) =  (UCHAR)flags;
103 
104     /* Store the LUN value.  */
105     *(cbw + UX_HOST_CLASS_STORAGE_CBW_LUN) =  (UCHAR)storage -> ux_host_class_storage_lun;
106 
107     /* Store the size of the SCSI command block that follows.  */
108     *(cbw + UX_HOST_CLASS_STORAGE_CBW_CB_LENGTH) =  (UCHAR)command_length;
109 
110     /* Reset the SCSI command block.  */
111     _ux_utility_memory_set(cbw + UX_HOST_CLASS_STORAGE_CBW_CB, 0, (ULONG) command_length); /* Use case of memset is verified. */
112 
113     /* Return to caller.  */
114     return;
115 }
116 
117