]> git.proxmox.com Git - mirror_edk2.git/blame - OvmfPkg/BlockMmioToBlockIoDxe/BlockIo.c
OvmfPkg: fix conversion specifiers in DEBUG format strings
[mirror_edk2.git] / OvmfPkg / BlockMmioToBlockIoDxe / BlockIo.c
CommitLineData
efd82c57 1/** @file\r
2 The driver wrappers BlockMmio protocol instances to produce\r
3 Block I/O Protocol instances.\r
4\r
8d531995 5 Copyright (c) 2007 - 2012, Intel Corporation. All rights reserved.<BR>\r
56d7640a 6 This program and the accompanying materials\r
efd82c57 7 are licensed and made available under the terms and conditions of the BSD License\r
8 which accompanies this distribution. The full text of the license may be found at\r
9 http://opensource.org/licenses/bsd-license.php\r
10 \r
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13\r
14**/\r
15\r
16#include "BlockIo.h"\r
17\r
18EFI_DRIVER_BINDING_PROTOCOL gBlockIoDriverBinding = {\r
19 BlockIoDriverBindingSupported,\r
20 BlockIoDriverBindingStart,\r
21 BlockIoDriverBindingStop,\r
22 0x11,\r
23 NULL,\r
24 NULL\r
25};\r
26\r
27/**\r
28 Reset the block device.\r
29\r
30 This function implements EFI_BLOCK_IO_PROTOCOL.Reset(). \r
31 It resets the block device hardware.\r
32 ExtendedVerification is ignored in this implementation.\r
33\r
34 @param This Indicates a pointer to the calling context.\r
35 @param ExtendedVerification Indicates that the driver may perform a more exhaustive\r
36 verification operation of the device during reset.\r
37\r
38 @retval EFI_SUCCESS The block device was reset.\r
39 @retval EFI_DEVICE_ERROR The block device is not functioning correctly and could not be reset.\r
40\r
41**/\r
42EFI_STATUS\r
43EFIAPI\r
44BlockIoReset (\r
45 IN EFI_BLOCK_IO_PROTOCOL *This,\r
46 IN BOOLEAN ExtendedVerification\r
47 )\r
48{\r
49 return EFI_SUCCESS;\r
50}\r
51\r
52/**\r
53 Reads the requested number of blocks from the device.\r
54\r
55 This function implements EFI_BLOCK_IO_PROTOCOL.ReadBlocks(). \r
56 It reads the requested number of blocks from the device.\r
57 All the blocks are read, or an error is returned.\r
58\r
59 @param This Indicates a pointer to the calling context.\r
60 @param ReadData If TRUE then read data. If FALSE then write data.\r
61 @param MediaId The media ID that the read request is for.\r
62 @param Lba The starting logical block address to read from on the device.\r
63 @param BufferSize The size of the Buffer in bytes.\r
64 This must be a multiple of the intrinsic block size of the device.\r
65 @param Buffer A pointer to the destination buffer for the data. The caller is\r
66 responsible for either having implicit or explicit ownership of the buffer.\r
67\r
68 @retval EFI_SUCCESS The data was read correctly from the device.\r
69 @retval EFI_DEVICE_ERROR The device reported an error while attempting to perform the read operation.\r
70 @retval EFI_NO_MEDIA There is no media in the device.\r
71 @retval EFI_MEDIA_CHANGED The MediaId is not for the current media.\r
72 @retval EFI_BAD_BUFFER_SIZE The BufferSize parameter is not a multiple of the intrinsic block size of the device.\r
73 @retval EFI_INVALID_PARAMETER The read request contains LBAs that are not valid,\r
74 or the buffer is not on proper alignment.\r
75\r
76**/\r
77EFI_STATUS\r
78EFIAPI\r
79ReadOrWriteBlocks (\r
80 IN EFI_BLOCK_IO_PROTOCOL *This,\r
81 IN BOOLEAN ReadData,\r
82 IN UINT32 MediaId,\r
83 IN EFI_LBA Lba,\r
84 IN UINTN BufferSize,\r
85 OUT VOID *Buffer\r
86 )\r
87{\r
88 EFI_STATUS Status;\r
89 BLOCK_MMIO_TO_BLOCK_IO_DEVICE *Private;\r
90 UINTN TotalBlock;\r
91 EFI_BLOCK_IO_MEDIA *Media;\r
92 UINT64 Address;\r
93 UINTN Count;\r
94 EFI_CPU_IO_PROTOCOL_IO_MEM CpuAccessFunction;\r
95\r
96 //\r
97 // First, validate the parameters\r
98 //\r
99 if ((Buffer == NULL) || (BufferSize == 0)) {\r
100 return EFI_INVALID_PARAMETER;\r
101 }\r
102\r
103 //\r
104 // Get private data structure\r
105 //\r
106 Private = PRIVATE_FROM_BLOCK_IO (This);\r
107 Media = Private->BlockMmio->Media;\r
108\r
109 //\r
110 // BufferSize must be a multiple of the intrinsic block size of the device.\r
111 //\r
f46d494a 112 if (ModU64x32 (BufferSize, Media->BlockSize) != 0) {\r
efd82c57 113 return EFI_BAD_BUFFER_SIZE;\r
114 }\r
115\r
f46d494a 116 TotalBlock = (UINTN) DivU64x32 (BufferSize, Media->BlockSize);\r
efd82c57 117\r
118 //\r
119 // Make sure the range to read is valid.\r
120 //\r
121 if (Lba + TotalBlock - 1 > Media->LastBlock) {\r
122 return EFI_INVALID_PARAMETER;\r
123 }\r
124\r
125 if (!(Media->MediaPresent)) {\r
126 return EFI_NO_MEDIA;\r
127 }\r
128\r
129 if (MediaId != Media->MediaId) {\r
130 return EFI_MEDIA_CHANGED;\r
131 }\r
132\r
133 Address = Private->BlockMmio->BaseAddress;\r
f46d494a 134 Address += MultU64x32 (Lba, Media->BlockSize);\r
efd82c57 135\r
f46d494a 136 Count = BufferSize >> 3;\r
efd82c57 137\r
138 if (ReadData) {\r
139 CpuAccessFunction = Private->CpuIo->Mem.Read;\r
140 } else {\r
141 CpuAccessFunction = Private->CpuIo->Mem.Write;\r
142 }\r
143\r
144 Status = (CpuAccessFunction) (\r
145 Private->CpuIo,\r
146 EfiCpuIoWidthUint64,\r
147 Address,\r
148 Count,\r
149 Buffer\r
150 );\r
151\r
152 return Status;\r
153}\r
154\r
155\r
156/**\r
157 Reads the requested number of blocks from the device.\r
158\r
159 This function implements EFI_BLOCK_IO_PROTOCOL.ReadBlocks(). \r
160 It reads the requested number of blocks from the device.\r
161 All the blocks are read, or an error is returned.\r
162\r
163 @param This Indicates a pointer to the calling context.\r
164 @param MediaId The media ID that the read request is for.\r
165 @param Lba The starting logical block address to read from on the device.\r
166 @param BufferSize The size of the Buffer in bytes.\r
167 This must be a multiple of the intrinsic block size of the device.\r
168 @param Buffer A pointer to the destination buffer for the data. The caller is\r
169 responsible for either having implicit or explicit ownership of the buffer.\r
170\r
171 @retval EFI_SUCCESS The data was read correctly from the device.\r
172 @retval EFI_DEVICE_ERROR The device reported an error while attempting to perform the read operation.\r
173 @retval EFI_NO_MEDIA There is no media in the device.\r
174 @retval EFI_MEDIA_CHANGED The MediaId is not for the current media.\r
175 @retval EFI_BAD_BUFFER_SIZE The BufferSize parameter is not a multiple of the intrinsic block size of the device.\r
176 @retval EFI_INVALID_PARAMETER The read request contains LBAs that are not valid,\r
177 or the buffer is not on proper alignment.\r
178\r
179**/\r
180EFI_STATUS\r
181EFIAPI\r
182BlockIoReadBlocks (\r
183 IN EFI_BLOCK_IO_PROTOCOL *This,\r
184 IN UINT32 MediaId,\r
185 IN EFI_LBA Lba,\r
186 IN UINTN BufferSize,\r
187 OUT VOID *Buffer\r
188 )\r
189{\r
6394c35a
LE
190 DEBUG ((EFI_D_INFO, "BlockIo (MMIO) ReadBlocks: lba=0x%Lx, size=0x%Lx\n",\r
191 Lba, (UINT64)BufferSize));\r
efd82c57 192 return ReadOrWriteBlocks (\r
193 This,\r
194 TRUE,\r
195 MediaId,\r
196 Lba,\r
197 BufferSize,\r
198 Buffer\r
199 );\r
200}\r
201\r
202\r
203/**\r
204 Writes a specified number of blocks to the device.\r
205\r
206 This function implements EFI_BLOCK_IO_PROTOCOL.WriteBlocks(). \r
207 It writes a specified number of blocks to the device.\r
208 All blocks are written, or an error is returned.\r
209\r
210 @param This Indicates a pointer to the calling context.\r
211 @param MediaId The media ID that the write request is for.\r
212 @param Lba The starting logical block address to be written.\r
213 @param BufferSize The size of the Buffer in bytes.\r
214 This must be a multiple of the intrinsic block size of the device.\r
215 @param Buffer Pointer to the source buffer for the data.\r
216\r
217 @retval EFI_SUCCESS The data were written correctly to the device.\r
218 @retval EFI_WRITE_PROTECTED The device cannot be written to.\r
219 @retval EFI_NO_MEDIA There is no media in the device.\r
220 @retval EFI_MEDIA_CHANGED The MediaId is not for the current media.\r
221 @retval EFI_DEVICE_ERROR The device reported an error while attempting to perform the write operation.\r
222 @retval EFI_BAD_BUFFER_SIZE The BufferSize parameter is not a multiple of the intrinsic\r
223 block size of the device.\r
224 @retval EFI_INVALID_PARAMETER The write request contains LBAs that are not valid,\r
225 or the buffer is not on proper alignment.\r
226\r
227**/\r
228EFI_STATUS\r
229EFIAPI\r
230BlockIoWriteBlocks (\r
231 IN EFI_BLOCK_IO_PROTOCOL *This,\r
232 IN UINT32 MediaId,\r
233 IN EFI_LBA Lba,\r
234 IN UINTN BufferSize,\r
235 IN VOID *Buffer\r
236 )\r
237{\r
6394c35a
LE
238 DEBUG ((EFI_D_INFO, "BlockIo (MMIO) WriteBlocks: lba=0x%Lx, size=0x%Lx\n",\r
239 Lba, (UINT64)BufferSize));\r
efd82c57 240 return ReadOrWriteBlocks (\r
241 This,\r
242 FALSE,\r
243 MediaId,\r
244 Lba,\r
245 BufferSize,\r
246 Buffer\r
247 );\r
248}\r
249\r
250/**\r
251 Flushes all modified data to a physical block device.\r
252\r
253 @param This Indicates a pointer to the calling context.\r
254\r
255 @retval EFI_SUCCESS All outstanding data were written correctly to the device.\r
256 @retval EFI_DEVICE_ERROR The device reported an error while attempting to write data.\r
257 @retval EFI_NO_MEDIA There is no media in the device.\r
258\r
259**/\r
260EFI_STATUS\r
261EFIAPI\r
262BlockIoFlushBlocks (\r
263 IN EFI_BLOCK_IO_PROTOCOL *This\r
264 )\r
265{\r
266 return EFI_SUCCESS;\r
267}\r
268\r
269\r
270/**\r
271 Initialize data for device that does not support multiple LUNSs.\r
272\r
273 @param This The Driver Binding Protocol instance.\r
274 @param Controller The device to initialize.\r
275 @param BlockMmio Pointer to USB_MASS_TRANSPORT.\r
276 @param Context Parameter for USB_MASS_DEVICE.Context.\r
277\r
278 @retval EFI_SUCCESS Initialization succeeds.\r
279 @retval Other Initialization fails.\r
280\r
281**/\r
282EFI_STATUS\r
283BlockIoInit (\r
284 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
285 IN EFI_HANDLE Controller\r
286 )\r
287{\r
288 EFI_STATUS Status;\r
289 BLOCK_MMIO_TO_BLOCK_IO_DEVICE *Private;\r
290 BLOCK_MMIO_PROTOCOL *BlockMmio;\r
291\r
8d531995 292 Private = (BLOCK_MMIO_TO_BLOCK_IO_DEVICE*) AllocateZeroPool (sizeof (*Private));\r
efd82c57 293 ASSERT (Private != NULL);\r
294\r
295 Status = gBS->LocateProtocol (\r
296 &gEfiCpuIo2ProtocolGuid,\r
297 NULL,\r
298 (VOID **) &(Private->CpuIo)\r
299 );\r
300 ASSERT_EFI_ERROR (Status);\r
301\r
302 Status = gBS->OpenProtocol (\r
303 Controller,\r
304 &gBlockMmioProtocolGuid,\r
305 (VOID **) &BlockMmio,\r
306 This->DriverBindingHandle,\r
307 Controller,\r
308 EFI_OPEN_PROTOCOL_BY_DRIVER\r
309 );\r
310 if (EFI_ERROR (Status)) {\r
311 DEBUG ((EFI_D_ERROR, "BlockIoInit: OpenBlockMmioProtocol By Driver (%r)\n", Status));\r
312 goto ON_ERROR;\r
313 }\r
6394c35a 314 DEBUG ((EFI_D_INFO, "BlockMmio: %p\n", BlockMmio));\r
efd82c57 315 DEBUG ((EFI_D_INFO, "BlockMmio->Media->LastBlock: 0x%lx\n", BlockMmio->Media->LastBlock));\r
316 \r
317 Private->Signature = BLOCK_MMIO_TO_BLOCK_IO_SIGNATURE;\r
318 Private->Controller = Controller;\r
319 Private->BlockMmio = BlockMmio;\r
320 Private->BlockIo.Media = BlockMmio->Media;\r
321 Private->BlockIo.Reset = BlockIoReset;\r
322 Private->BlockIo.ReadBlocks = BlockIoReadBlocks;\r
323 Private->BlockIo.WriteBlocks = BlockIoWriteBlocks;\r
324 Private->BlockIo.FlushBlocks = BlockIoFlushBlocks;\r
325\r
326 DEBUG ((EFI_D_INFO, "Private->BlockIo.Media->LastBlock: 0x%lx\n", Private->BlockIo.Media->LastBlock));\r
327\r
328 Status = gBS->InstallProtocolInterface (\r
329 &Controller,\r
330 &gEfiBlockIoProtocolGuid,\r
331 EFI_NATIVE_INTERFACE,\r
332 &Private->BlockIo\r
333 );\r
334 if (EFI_ERROR (Status)) {\r
335 goto ON_ERROR;\r
336 }\r
337\r
338 return EFI_SUCCESS;\r
339\r
340ON_ERROR:\r
341 if (Private != NULL) {\r
342 FreePool (Private);\r
343 }\r
344 if (BlockMmio != NULL) {\r
345 gBS->CloseProtocol (\r
346 Controller,\r
347 &gBlockMmioProtocolGuid,\r
348 This->DriverBindingHandle,\r
349 Controller\r
350 );\r
351 }\r
352 return Status; \r
353}\r
354\r
355\r
356/**\r
357 Check whether the controller is a supported USB mass storage.\r
358\r
359 @param This The USB mass storage driver binding protocol.\r
360 @param Controller The controller handle to check.\r
361 @param RemainingDevicePath The remaining device path.\r
362\r
363 @retval EFI_SUCCESS The driver supports this controller.\r
364 @retval other This device isn't supported.\r
365\r
366**/\r
367EFI_STATUS\r
368EFIAPI\r
369BlockIoDriverBindingSupported (\r
370 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
371 IN EFI_HANDLE Controller,\r
372 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath\r
373 )\r
374{\r
375 EFI_STATUS Status;\r
376 BLOCK_MMIO_PROTOCOL *BlockMmio;\r
377\r
378 Status = gBS->OpenProtocol (\r
379 Controller,\r
380 &gBlockMmioProtocolGuid,\r
381 (VOID **) &BlockMmio,\r
382 This->DriverBindingHandle,\r
383 Controller,\r
384 EFI_OPEN_PROTOCOL_BY_DRIVER\r
385 );\r
386 if (EFI_ERROR (Status)) {\r
387 return Status;\r
388 }\r
389\r
390 gBS->CloseProtocol (\r
391 Controller,\r
392 &gBlockMmioProtocolGuid,\r
393 This->DriverBindingHandle,\r
394 Controller\r
395 );\r
396\r
397 return Status;\r
398}\r
399\r
400/**\r
401 Starts the USB mass storage device with this driver.\r
402\r
403 This function consumes USB I/O Portocol, intializes USB mass storage device,\r
404 installs Block I/O Protocol, and submits Asynchronous Interrupt\r
405 Transfer to manage the USB mass storage device.\r
406\r
407 @param This The USB mass storage driver binding protocol.\r
408 @param Controller The USB mass storage device to start on\r
409 @param RemainingDevicePath The remaining device path.\r
410\r
411 @retval EFI_SUCCESS This driver supports this device.\r
412 @retval EFI_UNSUPPORTED This driver does not support this device.\r
413 @retval EFI_DEVICE_ERROR This driver cannot be started due to device Error.\r
414 @retval EFI_OUT_OF_RESOURCES Can't allocate memory resources.\r
415 @retval EFI_ALREADY_STARTED This driver has been started.\r
416\r
417**/\r
418EFI_STATUS\r
419EFIAPI\r
420BlockIoDriverBindingStart (\r
421 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
422 IN EFI_HANDLE Controller,\r
423 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath\r
424 )\r
425{\r
426 EFI_STATUS Status;\r
427 \r
428 Status = BlockIoInit (This, Controller);\r
429 if (EFI_ERROR (Status)) {\r
430 DEBUG ((EFI_D_ERROR, "BlockIoDriverBindingStart: BlockIoInit (%r)\n", Status));\r
431 return Status;\r
432 }\r
433\r
434 DEBUG ((EFI_D_INIT, "BlockIoDriverBindingStart: Successfully started\n"));\r
435 return Status;\r
436}\r
437\r
438\r
439/**\r
440 Stop controlling the device.\r
441\r
442 @param This The USB mass storage driver binding\r
443 @param Controller The device controller controlled by the driver.\r
444 @param NumberOfChildren The number of children of this device\r
445 @param ChildHandleBuffer The buffer of children handle.\r
446\r
447 @retval EFI_SUCCESS The driver stopped from controlling the device.\r
448 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.\r
449 @retval EFI_UNSUPPORTED Block I/O Protocol is not installed on Controller.\r
450 @retval Others Failed to stop the driver\r
451\r
452**/\r
453EFI_STATUS\r
454EFIAPI\r
455BlockIoDriverBindingStop (\r
456 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
457 IN EFI_HANDLE Controller,\r
458 IN UINTN NumberOfChildren,\r
459 IN EFI_HANDLE *ChildHandleBuffer\r
460 )\r
461{\r
462 EFI_STATUS Status;\r
463 BLOCK_MMIO_TO_BLOCK_IO_DEVICE *Private;\r
464\r
465 Private = PRIVATE_FROM_BLOCK_IO (This);\r
466\r
467 //\r
468 // Uninstall Block I/O protocol from the device handle,\r
469 // then call the transport protocol to stop itself.\r
470 //\r
471 Status = gBS->UninstallProtocolInterface (\r
472 Controller,\r
473 &gEfiBlockIoProtocolGuid,\r
474 &Private->BlockIo\r
475 );\r
476 if (EFI_ERROR (Status)) {\r
477 return Status;\r
478 }\r
479\r
480 gBS->CloseProtocol (\r
481 Controller,\r
482 &gBlockMmioProtocolGuid,\r
483 This->DriverBindingHandle,\r
484 Controller\r
485 );\r
486\r
487 FreePool (Private);\r
488 \r
489 DEBUG ((EFI_D_INFO, "Successfully stopped BlockIo on BlockMmio\n"));\r
490 return EFI_SUCCESS;\r
491}\r
492\r
493/**\r
494 Entrypoint of Block MMIO to Block IO Driver.\r
495\r
496 This function is the entrypoint of USB Mass Storage Driver. It installs Driver Binding\r
497 Protocol together with Component Name Protocols.\r
498\r
499 @param ImageHandle The firmware allocated handle for the EFI image.\r
500 @param SystemTable A pointer to the EFI System Table.\r
501\r
502 @retval EFI_SUCCESS The entry point is executed successfully.\r
503\r
504**/\r
505EFI_STATUS\r
506EFIAPI\r
507BlockMmioToBlockIoEntryPoint (\r
508 IN EFI_HANDLE ImageHandle,\r
509 IN EFI_SYSTEM_TABLE *SystemTable\r
510 )\r
511{\r
512 EFI_STATUS Status;\r
513\r
514 //\r
515 // Install driver binding protocol\r
516 //\r
517 Status = EfiLibInstallDriverBindingComponentName2 (\r
518 ImageHandle,\r
519 SystemTable,\r
520 &gBlockIoDriverBinding,\r
521 ImageHandle,\r
522 &gBlockMmioToBlockIoComponentName,\r
523 &gBlockMmioToBlockIoComponentName2\r
524 );\r
525 ASSERT_EFI_ERROR (Status);\r
526\r
527 return EFI_SUCCESS;\r
528}\r