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