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_open PORTABLE C */ |
37 |
|
|
/* 6.2.0 */ |
38 |
|
|
/* AUTHOR */ |
39 |
|
|
/* */ |
40 |
|
|
/* Chaoqiong Xiao, Microsoft Corporation */ |
41 |
|
|
/* */ |
42 |
|
|
/* DESCRIPTION */ |
43 |
|
|
/* */ |
44 |
|
|
/* This function will ask UX_MEDIA (default FileX) to mount a new */ |
45 |
|
|
/* partition for this device. This function has some */ |
46 |
|
|
/* UX_MEDIA (default FileX) dependencies. */ |
47 |
|
|
/* */ |
48 |
|
|
/* INPUT */ |
49 |
|
|
/* */ |
50 |
|
|
/* storage Pointer to storage class */ |
51 |
|
|
/* hidden_sectors Number of hidden sectors */ |
52 |
|
|
/* */ |
53 |
|
|
/* OUTPUT */ |
54 |
|
|
/* */ |
55 |
|
|
/* Completion Status */ |
56 |
|
|
/* */ |
57 |
|
|
/* CALLS */ |
58 |
|
|
/* */ |
59 |
|
|
/* ux_media_open Media open */ |
60 |
|
|
/* _ux_host_class_storage_media_protection_check */ |
61 |
|
|
/* Check for protection */ |
62 |
|
|
/* _ux_utility_memory_allocate Allocate memory block */ |
63 |
|
|
/* _ux_utility_memory_free Free memory block */ |
64 |
|
|
/* */ |
65 |
|
|
/* CALLED BY */ |
66 |
|
|
/* */ |
67 |
|
|
/* _ux_host_class_storage_media_mount Media open */ |
68 |
|
|
/* */ |
69 |
|
|
/* RELEASE HISTORY */ |
70 |
|
|
/* */ |
71 |
|
|
/* DATE NAME DESCRIPTION */ |
72 |
|
|
/* */ |
73 |
|
|
/* 05-19-2020 Chaoqiong Xiao Initial Version 6.0 */ |
74 |
|
|
/* 09-30-2020 Chaoqiong Xiao Modified comment(s), */ |
75 |
|
|
/* added option to disable FX */ |
76 |
|
|
/* media integration, used UX_ */ |
77 |
|
|
/* things instead of FX_ */ |
78 |
|
|
/* things directly, used host */ |
79 |
|
|
/* class extension pointer for */ |
80 |
|
|
/* class specific structured */ |
81 |
|
|
/* data, */ |
82 |
|
|
/* resulting in version 6.1 */ |
83 |
|
|
/* 10-31-2022 Chaoqiong Xiao Modified comment(s), */ |
84 |
|
|
/* added buffer size check, */ |
85 |
|
|
/* resulting in version 6.2.0 */ |
86 |
|
|
/* */ |
87 |
|
|
/**************************************************************************/ |
88 |
|
161 |
UINT _ux_host_class_storage_media_open(UX_HOST_CLASS_STORAGE *storage, ULONG hidden_sectors) |
89 |
|
|
{ |
90 |
|
|
|
91 |
|
|
#if defined(UX_HOST_CLASS_STORAGE_NO_FILEX) |
92 |
|
|
UX_PARAMETER_NOT_USED(storage); |
93 |
|
|
UX_PARAMETER_NOT_USED(hidden_sectors); |
94 |
|
|
return(UX_FUNCTION_NOT_SUPPORTED); |
95 |
|
|
#else |
96 |
|
|
UINT status; |
97 |
|
|
UINT media_index; |
98 |
|
|
UX_HOST_CLASS_STORAGE_MEDIA *storage_media; |
99 |
|
|
UX_MEDIA *media; |
100 |
|
|
UX_HOST_CLASS *class_inst; |
101 |
|
|
|
102 |
|
|
|
103 |
|
|
/* We need the class container. */ |
104 |
|
161 |
class_inst = storage -> ux_host_class_storage_class; |
105 |
|
|
|
106 |
|
|
/* Point the media structure to the first media in the container. */ |
107 |
|
161 |
storage_media = (UX_HOST_CLASS_STORAGE_MEDIA *) class_inst -> ux_host_class_media; |
108 |
|
|
|
109 |
|
|
/* Locate a free partition in the storage instance. */ |
110 |
✓✓ |
185 |
for (media_index = 0; media_index < UX_HOST_CLASS_STORAGE_MAX_MEDIA; media_index++) |
111 |
|
|
{ |
112 |
|
|
|
113 |
|
|
/* Get the USBX Integrated Media pointer for that media. */ |
114 |
|
184 |
media = &storage_media -> ux_host_class_storage_media; |
115 |
|
|
|
116 |
|
|
/* Is the media valid? */ |
117 |
✓✓ |
184 |
if (ux_media_id_get(media) == 0) |
118 |
|
|
{ |
119 |
|
|
|
120 |
|
|
/* Save the storage instance in the media instance. */ |
121 |
|
160 |
ux_media_driver_info_set(media, storage); |
122 |
|
|
|
123 |
|
|
/* Save the number of hidden sectors in this partition. */ |
124 |
|
160 |
storage_media -> ux_host_class_storage_media_partition_start = hidden_sectors; |
125 |
|
|
|
126 |
|
|
/* Save the LUN number in the storage media instance. */ |
127 |
|
160 |
storage_media -> ux_host_class_storage_media_lun = storage -> ux_host_class_storage_lun; |
128 |
|
|
|
129 |
|
|
/* Save the Sector size in the storage media instance. */ |
130 |
|
160 |
storage_media -> ux_host_class_storage_media_sector_size = storage -> ux_host_class_storage_sector_size; |
131 |
|
|
|
132 |
|
|
/* Check if media setting can support the sector size. */ |
133 |
✗✓ |
160 |
if (storage -> ux_host_class_storage_sector_size > UX_HOST_CLASS_STORAGE_MEMORY_BUFFER_SIZE) |
134 |
|
|
{ |
135 |
|
|
/* Error trap. */ |
136 |
|
|
_ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_HOST_CLASS_MEMORY_ERROR); |
137 |
|
|
|
138 |
|
|
/* Required memory is over system setting. */ |
139 |
|
|
return(UX_HOST_CLASS_MEMORY_ERROR); |
140 |
|
|
} |
141 |
|
|
|
142 |
|
|
/* Save the storage media instance in the user reserved area in the UX_MEDIA structure. */ |
143 |
|
160 |
ux_media_reserved_for_user_set(media, storage_media); |
144 |
|
|
|
145 |
|
|
/* We now need to allocate a block of memory for UX_MEDIA (default FileX) to use when doing transfers |
146 |
|
|
The default buffer size is 8K. The value used for the definition is UX_HOST_CLASS_STORAGE_MEMORY_BUFFER_SIZE. |
147 |
|
|
This value can be changed to save on memory space but should not be smaller than |
148 |
|
|
the media sector size (which should be 512 bytes). Because USB devices are SCSI |
149 |
|
|
devices and there is a great deal of overhead when doing read/writes, it is better |
150 |
|
|
to leave the default buffer size or even increase it. */ |
151 |
|
160 |
storage_media -> ux_host_class_storage_media_memory = _ux_utility_memory_allocate(UX_SAFE_ALIGN, UX_CACHE_SAFE_MEMORY, UX_HOST_CLASS_STORAGE_MEMORY_BUFFER_SIZE); |
152 |
✓✓ |
160 |
if (storage_media -> ux_host_class_storage_media_memory == UX_NULL) |
153 |
|
3 |
return(UX_MEMORY_INSUFFICIENT); |
154 |
|
|
|
155 |
|
|
/* If trace is enabled, insert this event into the trace buffer. */ |
156 |
|
|
UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_CLASS_STORAGE_MEDIA_OPEN, storage, media, 0, 0, UX_TRACE_HOST_CLASS_EVENTS, 0, 0) |
157 |
|
|
|
158 |
|
|
/* Ask UX_MEDIA (default FileX) to mount the partition. */ |
159 |
|
157 |
status = ux_media_open(media, UX_HOST_CLASS_STORAGE_MEDIA_NAME, _ux_host_class_storage_driver_entry, |
160 |
|
|
storage, storage_media -> ux_host_class_storage_media_memory, |
161 |
|
|
UX_HOST_CLASS_STORAGE_MEMORY_BUFFER_SIZE); |
162 |
|
|
|
163 |
|
|
/* If the media is mounted, update the status for the application. */ |
164 |
✓✓ |
157 |
if (status == UX_SUCCESS) |
165 |
|
150 |
storage_media -> ux_host_class_storage_media_status = UX_HOST_CLASS_STORAGE_MEDIA_MOUNTED; |
166 |
|
|
|
167 |
|
|
else |
168 |
|
|
|
169 |
|
|
/* Free the memory resources. */ |
170 |
|
7 |
_ux_utility_memory_free(storage_media -> ux_host_class_storage_media_memory); |
171 |
|
|
|
172 |
|
|
/* Return completion status. */ |
173 |
|
157 |
return(status); |
174 |
|
|
} |
175 |
|
|
|
176 |
|
|
/* Move to next entry in the media array. */ |
177 |
|
24 |
storage_media++; |
178 |
|
|
} |
179 |
|
|
|
180 |
|
|
/* Error trap. */ |
181 |
|
1 |
_ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_HOST_CLASS_MEMORY_ERROR); |
182 |
|
|
|
183 |
|
|
/* If trace is enabled, insert this event into the trace buffer. */ |
184 |
|
|
UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_HOST_CLASS_MEMORY_ERROR, storage, 0, 0, UX_TRACE_ERRORS, 0, 0) |
185 |
|
|
|
186 |
|
|
/* Return error. */ |
187 |
|
1 |
return(UX_HOST_CLASS_MEMORY_ERROR); |
188 |
|
|
#endif /* !defined(UX_HOST_CLASS_STORAGE_NO_FILEX) */ |
189 |
|
|
} |