GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usbx_host_classes/src/ux_host_class_storage_media_protection_check.c Lines: 26 26 100.0 %
Date: 2024-12-12 17:16:36 Branches: 10 10 100.0 %

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