GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usbx_host_classes/src/ux_host_class_storage_media_read.c Lines: 23 27 85.2 %
Date: 2026-03-06 18:57:10 Branches: 8 12 66.7 %

Line Branch Exec Source
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
#if defined(UX_HOST_STANDALONE)
34
VOID _ux_host_class_storage_read_initialize(UX_HOST_CLASS_STORAGE *storage,
35
                ULONG sector_start, ULONG sector_count);
36
37
VOID
38
#else
39
static inline VOID
40
#endif
41
613
_ux_host_class_storage_read_initialize(UX_HOST_CLASS_STORAGE *storage,
42
                ULONG sector_start, ULONG sector_count)
43
{
44
UCHAR       *cbw;
45
UCHAR       *cbw_cb;
46
ULONG       command_length;
47
48
    /* Use a pointer for the cbw, easier to manipulate.  */
49
613
    cbw = (UCHAR *)storage -> ux_host_class_storage_cbw;
50
613
    cbw_cb = cbw + UX_HOST_CLASS_STORAGE_CBW_CB;
51
52
    /* Get the Read Command Length.  */
53
#ifdef UX_HOST_CLASS_STORAGE_INCLUDE_LEGACY_PROTOCOL_SUPPORT
54
    if (storage -> ux_host_class_storage_interface -> ux_interface_descriptor.bInterfaceSubClass ==
55
        UX_HOST_CLASS_STORAGE_SUBCLASS_UFI)
56
        command_length =  UX_HOST_CLASS_STORAGE_READ_COMMAND_LENGTH_UFI;
57
    else
58
        command_length =  UX_HOST_CLASS_STORAGE_READ_COMMAND_LENGTH_SBC;
59
#else
60
613
    command_length =  UX_HOST_CLASS_STORAGE_READ_COMMAND_LENGTH_SBC;
61
#endif
62
63
    /* Initialize the CBW for this command.  */
64
613
    _ux_host_class_storage_cbw_initialize(storage,
65
                    UX_HOST_CLASS_STORAGE_DATA_IN,
66
613
                    sector_count * storage -> ux_host_class_storage_sector_size,
67
                    command_length);
68
69
    /* Prepare the MEDIA READ command block.  */
70
613
    *(cbw_cb + UX_HOST_CLASS_STORAGE_READ_OPERATION) =  UX_HOST_CLASS_STORAGE_SCSI_READ16;
71
72
    /* Store the sector start (LBA field).  */
73
613
    _ux_utility_long_put_big_endian(cbw_cb + UX_HOST_CLASS_STORAGE_READ_LBA, sector_start);
74
75
    /* Store the number of sectors to read.  */
76
613
    _ux_utility_short_put_big_endian(cbw_cb + UX_HOST_CLASS_STORAGE_READ_TRANSFER_LENGTH, (USHORT) sector_count);
77
613
}
78
79
80
/**************************************************************************/
81
/*                                                                        */
82
/*  FUNCTION                                               RELEASE        */
83
/*                                                                        */
84
/*    _ux_host_class_storage_media_read                   PORTABLE C      */
85
/*                                                           6.2.1        */
86
/*  AUTHOR                                                                */
87
/*                                                                        */
88
/*    Chaoqiong Xiao, Microsoft Corporation                               */
89
/*                                                                        */
90
/*  DESCRIPTION                                                           */
91
/*                                                                        */
92
/*    This function will read one or more logical sector from the media.  */
93
/*                                                                        */
94
/*  INPUT                                                                 */
95
/*                                                                        */
96
/*    storage                               Pointer to storage class      */
97
/*    sector_start                          Starting sector               */
98
/*    sector_count                          Number of sectors to read     */
99
/*    data_pointer                          Pointer to data to read       */
100
/*                                                                        */
101
/*  OUTPUT                                                                */
102
/*                                                                        */
103
/*    Completion Status                                                   */
104
/*                                                                        */
105
/*  CALLS                                                                 */
106
/*                                                                        */
107
/*    _ux_host_class_storage_cbw_initialize Initialize the CBW            */
108
/*    _ux_host_class_storage_transport      Send command                  */
109
/*    _ux_utility_long_put_big_endian       Put 32-bit word               */
110
/*    _ux_utility_short_put_big_endian      Put 16-bit word               */
111
/*                                                                        */
112
/*  CALLED BY                                                             */
113
/*                                                                        */
114
/*    Storage Class                                                       */
115
/*                                                                        */
116
/**************************************************************************/
117
584
UINT  _ux_host_class_storage_media_read(UX_HOST_CLASS_STORAGE *storage, ULONG sector_start,
118
                                    ULONG sector_count, UCHAR *data_pointer)
119
{
120
#if defined(UX_HOST_STANDALONE)
121
UINT            status;
122
    do {
123
        status = _ux_host_class_storage_read_write_run(storage, UX_TRUE,
124
                                    sector_start, sector_count, data_pointer);
125
    } while(status == UX_STATE_WAIT);
126
    if (status < UX_STATE_IDLE)
127
        return(UX_HOST_CLASS_INSTANCE_UNKNOWN);
128
    return(storage -> ux_host_class_storage_status);
129
#else
130
UINT            status;
131
UINT            media_retry;
132
133
    /* If trace is enabled, insert this event into the trace buffer.  */
134
    UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_CLASS_STORAGE_MEDIA_READ, storage, sector_start, sector_count, data_pointer, UX_TRACE_HOST_CLASS_EVENTS, 0, 0)
135
136
    /* Reset the retry count.  */
137
584
    media_retry =  UX_HOST_CLASS_STORAGE_REQUEST_SENSE_RETRY;
138
139
    /* We may need several attempts.  */
140
616
    while (media_retry-- != 0)
141
    {
142
143
        /* Initialize CBW.  */
144
613
        _ux_host_class_storage_read_initialize(storage, sector_start, sector_count);
145
146
        /* Send the command to transport layer.  */
147
613
        status =  _ux_host_class_storage_transport(storage, data_pointer);
148
613
        if (status != UX_SUCCESS)
149
3
            return(status);
150
151
        /* Did the command succeed?  */
152
610
        if (storage -> ux_host_class_storage_sense_code == UX_SUCCESS)
153
        {
154
155
            /* Check for completeness of sector read.  */
156
578
            if (storage -> ux_host_class_storage_data_phase_length != sector_count * storage -> ux_host_class_storage_sector_size)
157
            {
158
159
                /* This can happen if the device sent less data than the host
160
                   requested. This does not fit our definition of success and
161
                   retrying shouldn't change the outcome, so we return an error.  */
162
163
                /* We got an error during read. Packet not complete.  */
164
1
                _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_TRANSFER_DATA_LESS_THAN_EXPECTED);
165
166
                /* Return to UX_MEDIA (default FileX).  */
167
1
                return(UX_ERROR);
168
            }
169
170
            /* The read succeeded.  */
171
577
            return(UX_SUCCESS);
172
        }
173
174
        /* The command did not succeed. Retry.  */
175
    }
176
177
    /* Check if the media in the device has been removed. If so
178
       we have to tell UX_MEDIA (default FileX) that the media is closed.  */
179
3
    return(UX_HOST_CLASS_STORAGE_SENSE_ERROR);
180
#endif
181
}
182
183
184
/**************************************************************************/
185
/*                                                                        */
186
/*  FUNCTION                                               RELEASE        */
187
/*                                                                        */
188
/*    _ux_host_class_storage_media_read                   PORTABLE C      */
189
/*                                                           6.3.0        */
190
/*  AUTHOR                                                                */
191
/*                                                                        */
192
/*    Chaoqiong Xiao, Microsoft Corporation                               */
193
/*                                                                        */
194
/*  DESCRIPTION                                                           */
195
/*                                                                        */
196
/*    This function checks errors in storage media read function call.    */
197
/*                                                                        */
198
/*  INPUT                                                                 */
199
/*                                                                        */
200
/*    storage                               Pointer to storage class      */
201
/*    sector_start                          Starting sector               */
202
/*    sector_count                          Number of sectors to read     */
203
/*    data_pointer                          Pointer to data to read       */
204
/*                                                                        */
205
/*  OUTPUT                                                                */
206
/*                                                                        */
207
/*    Status                                                              */
208
/*                                                                        */
209
/*  CALLS                                                                 */
210
/*                                                                        */
211
/*    _ux_host_class_storage_media_read     Read storage media            */
212
/*                                                                        */
213
/*  CALLED BY                                                             */
214
/*                                                                        */
215
/*    Application                                                         */
216
/*                                                                        */
217
/**************************************************************************/
218
UINT  _uxe_host_class_storage_media_read(UX_HOST_CLASS_STORAGE *storage, ULONG sector_start,
219
                                    ULONG sector_count, UCHAR *data_pointer)
220
{
221
222
    /* Sanity checks.  */
223
    if ((storage == UX_NULL) || (data_pointer == UX_NULL))
224
        return(UX_INVALID_PARAMETER);
225
226
    /* Invoke storage media read function.  */
227
    return(_ux_host_class_storage_media_read(storage, sector_start, sector_count, data_pointer));
228
}