]> git.proxmox.com Git - mirror_edk2.git/blame - OvmfPkg/XenPvBlkDxe/BlockIo.c
OvmfPkg: Fix typing errors
[mirror_edk2.git] / OvmfPkg / XenPvBlkDxe / BlockIo.c
CommitLineData
5de8a35c
AP
1/** @file\r
2 BlockIo implementation for Xen PV Block driver.\r
3\r
4 This file is implementing the interface between the actual driver in\r
5 BlockFront.c to the BlockIo protocol.\r
6\r
7 Copyright (C) 2014, Citrix Ltd.\r
8\r
9 This program and the accompanying materials\r
10 are licensed and made available under the terms and conditions of the BSD License\r
11 which accompanies this distribution. The full text of the license may be found at\r
12 http://opensource.org/licenses/bsd-license.php\r
13\r
14 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
15 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
16\r
17**/\r
18\r
19#include "XenPvBlkDxe.h"\r
20\r
21#include "BlockFront.h"\r
22\r
23///\r
24/// Block I/O Media structure\r
25///\r
26GLOBAL_REMOVE_IF_UNREFERENCED\r
27EFI_BLOCK_IO_MEDIA gXenPvBlkDxeBlockIoMedia = {\r
28 0, // MediaId\r
29 FALSE, // RemovableMedia\r
30 FALSE, // MediaPresent\r
31 FALSE, // LogicalPartition\r
32 TRUE, // ReadOnly\r
33 FALSE, // WriteCaching\r
34 512, // BlockSize\r
35 512, // IoAlign, BlockFront does not support less than 512 bits-aligned.\r
36 0, // LastBlock\r
37 0, // LowestAlignedLba\r
38 0, // LogicalBlocksPerPhysicalBlock\r
39 0 // OptimalTransferLengthGranularity\r
40};\r
41\r
42///\r
43/// Block I/O Protocol instance\r
44///\r
45GLOBAL_REMOVE_IF_UNREFERENCED\r
46EFI_BLOCK_IO_PROTOCOL gXenPvBlkDxeBlockIo = {\r
47 EFI_BLOCK_IO_PROTOCOL_REVISION3, // Revision\r
48 &gXenPvBlkDxeBlockIoMedia, // Media\r
49 XenPvBlkDxeBlockIoReset, // Reset\r
50 XenPvBlkDxeBlockIoReadBlocks, // ReadBlocks\r
51 XenPvBlkDxeBlockIoWriteBlocks, // WriteBlocks\r
52 XenPvBlkDxeBlockIoFlushBlocks // FlushBlocks\r
53};\r
54\r
55\r
56\r
57\r
58/**\r
59 Read/Write BufferSize bytes from Lba into Buffer.\r
60\r
61 This function is commun to XenPvBlkDxeBlockIoReadBlocks and\r
62 XenPvBlkDxeBlockIoWriteBlocks.\r
63\r
64 @param This Indicates a pointer to the calling context.\r
65 @param MediaId Id of the media, changes every time the media is replaced.\r
66 @param Lba The starting Logical Block Address to read from/write to.\r
67 @param BufferSize Size of Buffer, must be a multiple of device block size.\r
68 @param Buffer A pointer to the destination/source buffer for the data.\r
69 @param IsWrite Indicate if the operation is write or read.\r
70\r
71 @return See description of XenPvBlkDxeBlockIoReadBlocks and\r
72 XenPvBlkDxeBlockIoWriteBlocks.\r
73**/\r
74STATIC\r
75EFI_STATUS\r
76XenPvBlkDxeBlockIoReadWriteBlocks (\r
77 IN EFI_BLOCK_IO_PROTOCOL *This,\r
78 IN UINT32 MediaId,\r
79 IN EFI_LBA Lba,\r
80 IN UINTN BufferSize,\r
81 IN OUT VOID *Buffer,\r
82 IN BOOLEAN IsWrite\r
83 )\r
84{\r
85 XEN_BLOCK_FRONT_IO IoData;\r
86 EFI_BLOCK_IO_MEDIA *Media = This->Media;\r
87 UINTN Sector;\r
88 EFI_STATUS Status;\r
89\r
90 if (Buffer == NULL) {\r
91 return EFI_INVALID_PARAMETER;\r
92 }\r
93 if (BufferSize == 0) {\r
94 return EFI_SUCCESS;\r
95 }\r
96\r
97 if (BufferSize % Media->BlockSize != 0) {\r
6394c35a
LE
98 DEBUG ((EFI_D_ERROR, "XenPvBlkDxe: Bad buffer size: 0x%Lx\n",\r
99 (UINT64)BufferSize));\r
5de8a35c
AP
100 return EFI_BAD_BUFFER_SIZE;\r
101 }\r
102\r
103 if (Lba > Media->LastBlock ||\r
104 (BufferSize / Media->BlockSize) - 1 > Media->LastBlock - Lba) {\r
6394c35a
LE
105 DEBUG ((EFI_D_ERROR,\r
106 "XenPvBlkDxe: %a with invalid LBA: 0x%Lx, size: 0x%Lx\n",\r
107 IsWrite ? "Write" : "Read", Lba, (UINT64)BufferSize));\r
5de8a35c
AP
108 return EFI_INVALID_PARAMETER;\r
109 }\r
110\r
111 if (IsWrite && Media->ReadOnly) {\r
112 return EFI_WRITE_PROTECTED;\r
113 }\r
114\r
115 if ((Media->IoAlign > 1) && (UINTN)Buffer & (Media->IoAlign - 1)) {\r
116 //\r
117 // Grub2 does not appear to respect IoAlign of 512, so reallocate the\r
118 // buffer here.\r
119 //\r
120 VOID *NewBuffer;\r
121\r
122 //\r
123 // Try again with a properly aligned buffer.\r
124 //\r
125 NewBuffer = AllocateAlignedPages((BufferSize + EFI_PAGE_SIZE) / EFI_PAGE_SIZE,\r
126 Media->IoAlign);\r
127 if (!IsWrite) {\r
128 Status = XenPvBlkDxeBlockIoReadBlocks (This, MediaId,\r
129 Lba, BufferSize, NewBuffer);\r
130 CopyMem (Buffer, NewBuffer, BufferSize);\r
131 } else {\r
132 CopyMem (NewBuffer, Buffer, BufferSize);\r
133 Status = XenPvBlkDxeBlockIoWriteBlocks (This, MediaId,\r
134 Lba, BufferSize, NewBuffer);\r
135 }\r
136 FreeAlignedPages (NewBuffer, (BufferSize + EFI_PAGE_SIZE) / EFI_PAGE_SIZE);\r
137 return Status;\r
138 }\r
139\r
140 IoData.Dev = XEN_BLOCK_FRONT_FROM_BLOCK_IO (This);\r
860088f2 141 Sector = (UINTN)MultU64x32 (Lba, Media->BlockSize / 512);\r
5de8a35c
AP
142\r
143 while (BufferSize > 0) {\r
144 if (((UINTN)Buffer & EFI_PAGE_MASK) == 0) {\r
145 IoData.Size = MIN (BLKIF_MAX_SEGMENTS_PER_REQUEST * EFI_PAGE_SIZE,\r
146 BufferSize);\r
147 } else {\r
148 IoData.Size = MIN ((BLKIF_MAX_SEGMENTS_PER_REQUEST - 1) * EFI_PAGE_SIZE,\r
149 BufferSize);\r
150 }\r
151\r
152 IoData.Buffer = Buffer;\r
153 IoData.Sector = Sector;\r
154 BufferSize -= IoData.Size;\r
155 Buffer = (VOID*) ((UINTN) Buffer + IoData.Size);\r
156 Sector += IoData.Size / 512;\r
157 Status = XenPvBlockIo (&IoData, IsWrite);\r
158 if (EFI_ERROR (Status)) {\r
8c0b0b34 159 DEBUG ((EFI_D_ERROR, "XenPvBlkDxe: Error during %a operation.\n",\r
5de8a35c
AP
160 IsWrite ? "write" : "read"));\r
161 return Status;\r
162 }\r
163 }\r
164 return EFI_SUCCESS;\r
165}\r
166\r
167\r
168/**\r
169 Read BufferSize bytes from Lba into Buffer.\r
170\r
171 @param This Indicates a pointer to the calling context.\r
172 @param MediaId Id of the media, changes every time the media is replaced.\r
173 @param Lba The starting Logical Block Address to read from\r
174 @param BufferSize Size of Buffer, must be a multiple of device block size.\r
175 @param Buffer A pointer to the destination buffer for the data. The caller is\r
176 responsible for either having implicit or explicit ownership of the buffer.\r
177\r
178 @retval EFI_SUCCESS The data was read correctly from the device.\r
179 @retval EFI_DEVICE_ERROR The device reported an error while performing the read.\r
180 @retval EFI_NO_MEDIA There is no media in the device.\r
181 @retval EFI_MEDIA_CHANGED The MediaId does not matched the current device.\r
182 @retval EFI_BAD_BUFFER_SIZE The Buffer was not a multiple of the block size of the device.\r
183 @retval EFI_INVALID_PARAMETER The read request contains LBAs that are not valid,\r
184 or the buffer is not on proper alignment.\r
185\r
186**/\r
187EFI_STATUS\r
188EFIAPI\r
189XenPvBlkDxeBlockIoReadBlocks (\r
190 IN EFI_BLOCK_IO_PROTOCOL *This,\r
191 IN UINT32 MediaId,\r
192 IN EFI_LBA Lba,\r
193 IN UINTN BufferSize,\r
194 OUT VOID *Buffer\r
195 )\r
196{\r
197 return XenPvBlkDxeBlockIoReadWriteBlocks (This,\r
198 MediaId, Lba, BufferSize, Buffer, FALSE);\r
199}\r
200\r
201/**\r
202 Write BufferSize bytes from Lba into Buffer.\r
203\r
204 @param This Indicates a pointer to the calling context.\r
205 @param MediaId The media ID that the write request is for.\r
206 @param Lba The starting logical block address to be written. The caller is\r
207 responsible for writing to only legitimate locations.\r
208 @param BufferSize Size of Buffer, must be a multiple of device block size.\r
209 @param Buffer A pointer to the source buffer for the data.\r
210\r
211 @retval EFI_SUCCESS The data was written correctly to the device.\r
212 @retval EFI_WRITE_PROTECTED The device can not be written to.\r
213 @retval EFI_DEVICE_ERROR The device reported an error while performing the write.\r
214 @retval EFI_NO_MEDIA There is no media in the device.\r
215 @retval EFI_MEDIA_CHNAGED The MediaId does not matched the current device.\r
216 @retval EFI_BAD_BUFFER_SIZE The Buffer was not a multiple of the block size of the device.\r
217 @retval EFI_INVALID_PARAMETER The write request contains LBAs that are not valid,\r
218 or the buffer is not on proper alignment.\r
219\r
220**/\r
221EFI_STATUS\r
222EFIAPI\r
223XenPvBlkDxeBlockIoWriteBlocks (\r
224 IN EFI_BLOCK_IO_PROTOCOL *This,\r
225 IN UINT32 MediaId,\r
226 IN EFI_LBA Lba,\r
227 IN UINTN BufferSize,\r
228 IN VOID *Buffer\r
229 )\r
230{\r
231 return XenPvBlkDxeBlockIoReadWriteBlocks (This,\r
232 MediaId, Lba, BufferSize, Buffer, TRUE);\r
233}\r
234\r
235/**\r
236 Flush the Block Device.\r
237\r
238 @param This Indicates a pointer to the calling context.\r
239\r
240 @retval EFI_SUCCESS All outstanding data was written to the device\r
241 @retval EFI_DEVICE_ERROR The device reported an error while writting back the data\r
242 @retval EFI_NO_MEDIA There is no media in the device.\r
243\r
244**/\r
245EFI_STATUS\r
246EFIAPI\r
247XenPvBlkDxeBlockIoFlushBlocks (\r
248 IN EFI_BLOCK_IO_PROTOCOL *This\r
249 )\r
250{\r
251 XenPvBlockSync (XEN_BLOCK_FRONT_FROM_BLOCK_IO (This));\r
252 return EFI_SUCCESS;\r
253}\r
254\r
255/**\r
256 Reset the block device hardware.\r
257\r
258 @param[in] This Indicates a pointer to the calling context.\r
259 @param[in] ExtendedVerification Not used.\r
260\r
261 @retval EFI_SUCCESS The device was reset.\r
262\r
263**/\r
264EFI_STATUS\r
265EFIAPI\r
266XenPvBlkDxeBlockIoReset (\r
267 IN EFI_BLOCK_IO_PROTOCOL *This,\r
268 IN BOOLEAN ExtendedVerification\r
269 )\r
270{\r
271 //\r
272 // Since the initialization of the devices is done, then the device is\r
273 // working correctly.\r
274 //\r
275 return EFI_SUCCESS;\r
276}\r