]> git.proxmox.com Git - mirror_edk2.git/blame - OvmfPkg/VirtioFsDxe/VirtioFsDxe.h
OvmfPkg/VirtioFsDxe: add helper for appending and sanitizing paths
[mirror_edk2.git] / OvmfPkg / VirtioFsDxe / VirtioFsDxe.h
CommitLineData
b55d6622
LE
1/** @file\r
2 Internal macro definitions, type definitions, and function declarations for\r
3 the Virtio Filesystem device driver.\r
4\r
5 Copyright (C) 2020, Red Hat, Inc.\r
6\r
7 SPDX-License-Identifier: BSD-2-Clause-Patent\r
8**/\r
9\r
10#ifndef VIRTIO_FS_DXE_H_\r
11#define VIRTIO_FS_DXE_H_\r
12\r
13#include <Base.h> // SIGNATURE_64()\r
eaa7115d 14#include <IndustryStandard/VirtioFs.h> // VIRTIO_FS_TAG_BYTES\r
b55d6622
LE
15#include <Library/DebugLib.h> // CR()\r
16#include <Protocol/SimpleFileSystem.h> // EFI_SIMPLE_FILE_SYSTEM_PROTOCOL\r
17#include <Protocol/VirtioDevice.h> // VIRTIO_DEVICE_PROTOCOL\r
eaa7115d 18#include <Uefi/UefiBaseType.h> // EFI_EVENT\r
b55d6622
LE
19\r
20#define VIRTIO_FS_SIG SIGNATURE_64 ('V', 'I', 'R', 'T', 'I', 'O', 'F', 'S')\r
21\r
334c13e1
LE
22#define VIRTIO_FS_FILE_SIG \\r
23 SIGNATURE_64 ('V', 'I', 'O', 'F', 'S', 'F', 'I', 'L')\r
24\r
9307d7c7
LE
25//\r
26// The following limit applies to two kinds of pathnames.\r
27//\r
28// - The length of a POSIX-style, canonical pathname *at rest* never exceeds\r
29// VIRTIO_FS_MAX_PATHNAME_LENGTH. (Length is defined as the number of CHAR8\r
30// elements in the canonical pathname, excluding the terminating '\0'.) This\r
31// is an invariant that is ensured for canonical pathnames created, and that\r
32// is assumed about canonical pathname inputs (which all originate\r
33// internally).\r
34//\r
35// - If the length of a UEFI-style pathname *argument*, originating directly or\r
36// indirectly from the EFI_FILE_PROTOCOL caller, exceeds\r
37// VIRTIO_FS_MAX_PATHNAME_LENGTH, then the argument is rejected. (Length is\r
38// defined as the number of CHAR16 elements in the UEFI-style pathname,\r
39// excluding the terminating L'\0'.) This is a restriction that's checked on\r
40// external UEFI-style pathname inputs.\r
41//\r
42// The limit is not expected to be a practical limitation; it's only supposed\r
43// to prevent attempts at overflowing size calculations. For both kinds of\r
44// pathnames, separate limits could be used; a common limit is used purely for\r
45// simplicity.\r
46//\r
47#define VIRTIO_FS_MAX_PATHNAME_LENGTH ((UINTN)65535)\r
48\r
eaa7115d
LE
49//\r
50// Filesystem label encoded in UCS-2, transformed from the UTF-8 representation\r
51// in "VIRTIO_FS_CONFIG.Tag", and NUL-terminated. Only the printable ASCII code\r
52// points (U+0020 through U+007E) are supported.\r
53//\r
54typedef CHAR16 VIRTIO_FS_LABEL[VIRTIO_FS_TAG_BYTES + 1];\r
55\r
b55d6622
LE
56//\r
57// Main context structure, expressing an EFI_SIMPLE_FILE_SYSTEM_PROTOCOL\r
58// interface on top of the Virtio Filesystem device.\r
59//\r
60typedef struct {\r
61 //\r
62 // Parts of this structure are initialized / torn down in various functions\r
63 // at various call depths. The table to the right should make it easier to\r
64 // track them.\r
65 //\r
66 // field init function init depth\r
67 // ----------- ------------------ ----------\r
68 UINT64 Signature; // DriverBindingStart 0\r
69 VIRTIO_DEVICE_PROTOCOL *Virtio; // DriverBindingStart 0\r
eaa7115d
LE
70 VIRTIO_FS_LABEL Label; // VirtioFsInit 1\r
71 UINT16 QueueSize; // VirtioFsInit 1\r
72 VRING Ring; // VirtioRingInit 2\r
73 VOID *RingMap; // VirtioRingMap 2\r
fa97e372 74 UINT64 RequestId; // FuseInitSession 1\r
eaa7115d 75 EFI_EVENT ExitBoot; // DriverBindingStart 0\r
334c13e1 76 LIST_ENTRY OpenFiles; // DriverBindingStart 0\r
b55d6622
LE
77 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL SimpleFs; // DriverBindingStart 0\r
78} VIRTIO_FS;\r
79\r
80#define VIRTIO_FS_FROM_SIMPLE_FS(SimpleFsReference) \\r
81 CR (SimpleFsReference, VIRTIO_FS, SimpleFs, VIRTIO_FS_SIG);\r
82\r
6578cacb
LE
83//\r
84// Structure for describing a contiguous buffer, potentially mapped for Virtio\r
85// transfer.\r
86//\r
87typedef struct {\r
88 //\r
89 // The following fields originate from the owner of the buffer.\r
90 //\r
91 VOID *Buffer;\r
92 UINTN Size;\r
93 //\r
94 // All of the fields below, until the end of the structure, are\r
95 // zero-initialized when the structure is initially validated.\r
96 //\r
97 // Mapped, MappedAddress and Mapping are updated when the buffer is mapped\r
98 // for VirtioOperationBusMasterRead or VirtioOperationBusMasterWrite. They\r
99 // are again updated when the buffer is unmapped.\r
100 //\r
101 BOOLEAN Mapped;\r
102 EFI_PHYSICAL_ADDRESS MappedAddress;\r
103 VOID *Mapping;\r
104 //\r
105 // Transferred is updated after VirtioFlush() returns successfully:\r
106 // - for VirtioOperationBusMasterRead, Transferred is set to Size;\r
107 // - for VirtioOperationBusMasterWrite, Transferred is calculated from the\r
108 // UsedLen output parameter of VirtioFlush().\r
109 //\r
110 UINTN Transferred;\r
111} VIRTIO_FS_IO_VECTOR;\r
112\r
113//\r
114// Structure for describing a list of IO Vectors.\r
115//\r
116typedef struct {\r
117 //\r
118 // The following fields originate from the owner of the buffers.\r
119 //\r
120 VIRTIO_FS_IO_VECTOR *IoVec;\r
121 UINTN NumVec;\r
122 //\r
123 // TotalSize is calculated when the scatter-gather list is initially\r
124 // validated.\r
125 //\r
126 UINT32 TotalSize;\r
127} VIRTIO_FS_SCATTER_GATHER_LIST;\r
128\r
334c13e1
LE
129//\r
130// Private context structure that exposes EFI_FILE_PROTOCOL on top of an open\r
131// FUSE file reference.\r
132//\r
133typedef struct {\r
134 UINT64 Signature;\r
135 EFI_FILE_PROTOCOL SimpleFile;\r
136 BOOLEAN IsDirectory;\r
28092a39 137 BOOLEAN IsOpenForWriting;\r
334c13e1
LE
138 VIRTIO_FS *OwnerFs;\r
139 LIST_ENTRY OpenFilesEntry;\r
140 //\r
141 // In the FUSE wire protocol, every request except FUSE_INIT refers to a\r
142 // file, namely by the "VIRTIO_FS_FUSE_REQUEST.NodeId" field; that is, by the\r
143 // inode number of the file. However, some of the FUSE requests that we need\r
144 // for some of the EFI_FILE_PROTOCOL member functions require an open file\r
145 // handle *in addition* to the inode number. For simplicity, whenever a\r
146 // VIRTIO_FS_FILE object is created, primarily defined by its NodeId field,\r
147 // we also *open* the referenced file at once, and save the returned file\r
148 // handle in the FuseHandle field. This way, when an EFI_FILE_PROTOCOL member\r
149 // function must send a FUSE request that needs the file handle *in addition*\r
150 // to the inode number, FuseHandle will be at our disposal at once.\r
151 //\r
152 UINT64 NodeId;\r
153 UINT64 FuseHandle;\r
154} VIRTIO_FS_FILE;\r
155\r
156#define VIRTIO_FS_FILE_FROM_SIMPLE_FILE(SimpleFileReference) \\r
157 CR (SimpleFileReference, VIRTIO_FS_FILE, SimpleFile, VIRTIO_FS_FILE_SIG);\r
158\r
159#define VIRTIO_FS_FILE_FROM_OPEN_FILES_ENTRY(OpenFilesEntryReference) \\r
160 CR (OpenFilesEntryReference, VIRTIO_FS_FILE, OpenFilesEntry, \\r
161 VIRTIO_FS_FILE_SIG);\r
162\r
eaa7115d
LE
163//\r
164// Initialization and helper routines for the Virtio Filesystem device.\r
165//\r
166\r
167EFI_STATUS\r
168VirtioFsInit (\r
169 IN OUT VIRTIO_FS *VirtioFs\r
170 );\r
171\r
172VOID\r
173VirtioFsUninit (\r
174 IN OUT VIRTIO_FS *VirtioFs\r
175 );\r
176\r
177VOID\r
178EFIAPI\r
179VirtioFsExitBoot (\r
180 IN EFI_EVENT ExitBootEvent,\r
181 IN VOID *VirtioFsAsVoid\r
182 );\r
183\r
6578cacb
LE
184EFI_STATUS\r
185VirtioFsSgListsValidate (\r
186 IN VIRTIO_FS *VirtioFs,\r
187 IN OUT VIRTIO_FS_SCATTER_GATHER_LIST *RequestSgList,\r
188 IN OUT VIRTIO_FS_SCATTER_GATHER_LIST *ResponseSgList OPTIONAL\r
189 );\r
190\r
191EFI_STATUS\r
192VirtioFsSgListsSubmit (\r
193 IN OUT VIRTIO_FS *VirtioFs,\r
194 IN OUT VIRTIO_FS_SCATTER_GATHER_LIST *RequestSgList,\r
195 IN OUT VIRTIO_FS_SCATTER_GATHER_LIST *ResponseSgList OPTIONAL\r
196 );\r
197\r
6a2dc768
LE
198EFI_STATUS\r
199VirtioFsFuseNewRequest (\r
200 IN OUT VIRTIO_FS *VirtioFs,\r
201 OUT VIRTIO_FS_FUSE_REQUEST *Request,\r
202 IN UINT32 RequestSize,\r
fa97e372 203 IN VIRTIO_FS_FUSE_OPCODE Opcode,\r
6a2dc768
LE
204 IN UINT64 NodeId\r
205 );\r
206\r
207EFI_STATUS\r
208VirtioFsFuseCheckResponse (\r
209 IN VIRTIO_FS_SCATTER_GATHER_LIST *ResponseSgList,\r
210 IN UINT64 RequestId,\r
211 OUT UINTN *TailBufferFill\r
212 );\r
213\r
e8a74c9a
LE
214EFI_STATUS\r
215VirtioFsErrnoToEfiStatus (\r
216 IN INT32 Errno\r
217 );\r
218\r
9307d7c7
LE
219EFI_STATUS\r
220VirtioFsAppendPath (\r
221 IN CHAR8 *LhsPath8,\r
222 IN CHAR16 *RhsPath16,\r
223 OUT CHAR8 **ResultPath8,\r
224 OUT BOOLEAN *RootEscape\r
225 );\r
226\r
fa97e372
LE
227//\r
228// Wrapper functions for FUSE commands (primitives).\r
229//\r
230\r
92a4d30e
LE
231EFI_STATUS\r
232VirtioFsFuseForget (\r
233 IN OUT VIRTIO_FS *VirtioFs,\r
234 IN UINT64 NodeId\r
235 );\r
236\r
72d4f133
LE
237EFI_STATUS\r
238VirtioFsFuseReleaseFileOrDir (\r
239 IN OUT VIRTIO_FS *VirtioFs,\r
240 IN UINT64 NodeId,\r
241 IN UINT64 FuseHandle,\r
242 IN BOOLEAN IsDir\r
243 );\r
244\r
2e151d26
LE
245EFI_STATUS\r
246VirtioFsFuseFsyncFileOrDir (\r
247 IN OUT VIRTIO_FS *VirtioFs,\r
248 IN UINT64 NodeId,\r
249 IN UINT64 FuseHandle,\r
250 IN BOOLEAN IsDir\r
251 );\r
252\r
d0474399
LE
253EFI_STATUS\r
254VirtioFsFuseFlush (\r
255 IN OUT VIRTIO_FS *VirtioFs,\r
256 IN UINT64 NodeId,\r
257 IN UINT64 FuseHandle\r
258 );\r
259\r
fa97e372
LE
260EFI_STATUS\r
261VirtioFsFuseInitSession (\r
262 IN OUT VIRTIO_FS *VirtioFs\r
263 );\r
264\r
b62a0c56
LE
265EFI_STATUS\r
266VirtioFsFuseOpenDir (\r
267 IN OUT VIRTIO_FS *VirtioFs,\r
268 IN UINT64 NodeId,\r
269 OUT UINT64 *FuseHandle\r
270 );\r
271\r
b55d6622
LE
272//\r
273// EFI_SIMPLE_FILE_SYSTEM_PROTOCOL member functions for the Virtio Filesystem\r
274// driver.\r
275//\r
276\r
277EFI_STATUS\r
278EFIAPI\r
279VirtioFsOpenVolume (\r
280 IN EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *This,\r
281 OUT EFI_FILE_PROTOCOL **Root\r
282 );\r
283\r
334c13e1
LE
284//\r
285// EFI_FILE_PROTOCOL member functions for the Virtio Filesystem driver.\r
286//\r
287\r
288EFI_STATUS\r
289EFIAPI\r
290VirtioFsSimpleFileClose (\r
291 IN EFI_FILE_PROTOCOL *This\r
292 );\r
293\r
294EFI_STATUS\r
295EFIAPI\r
296VirtioFsSimpleFileDelete (\r
297 IN EFI_FILE_PROTOCOL *This\r
298 );\r
299\r
300EFI_STATUS\r
301EFIAPI\r
302VirtioFsSimpleFileFlush (\r
303 IN EFI_FILE_PROTOCOL *This\r
304 );\r
305\r
306EFI_STATUS\r
307EFIAPI\r
308VirtioFsSimpleFileGetInfo (\r
309 IN EFI_FILE_PROTOCOL *This,\r
310 IN EFI_GUID *InformationType,\r
311 IN OUT UINTN *BufferSize,\r
312 OUT VOID *Buffer\r
313 );\r
314\r
315EFI_STATUS\r
316EFIAPI\r
317VirtioFsSimpleFileGetPosition (\r
318 IN EFI_FILE_PROTOCOL *This,\r
319 OUT UINT64 *Position\r
320 );\r
321\r
322EFI_STATUS\r
323EFIAPI\r
324VirtioFsSimpleFileOpen (\r
325 IN EFI_FILE_PROTOCOL *This,\r
326 OUT EFI_FILE_PROTOCOL **NewHandle,\r
327 IN CHAR16 *FileName,\r
328 IN UINT64 OpenMode,\r
329 IN UINT64 Attributes\r
330 );\r
331\r
332EFI_STATUS\r
333EFIAPI\r
334VirtioFsSimpleFileRead (\r
335 IN EFI_FILE_PROTOCOL *This,\r
336 IN OUT UINTN *BufferSize,\r
337 OUT VOID *Buffer\r
338 );\r
339\r
340EFI_STATUS\r
341EFIAPI\r
342VirtioFsSimpleFileSetInfo (\r
343 IN EFI_FILE_PROTOCOL *This,\r
344 IN EFI_GUID *InformationType,\r
345 IN UINTN BufferSize,\r
346 IN VOID *Buffer\r
347 );\r
348\r
349EFI_STATUS\r
350EFIAPI\r
351VirtioFsSimpleFileSetPosition (\r
352 IN EFI_FILE_PROTOCOL *This,\r
353 IN UINT64 Position\r
354 );\r
355\r
356EFI_STATUS\r
357EFIAPI\r
358VirtioFsSimpleFileWrite (\r
359 IN EFI_FILE_PROTOCOL *This,\r
360 IN OUT UINTN *BufferSize,\r
361 IN VOID *Buffer\r
362 );\r
363\r
b55d6622 364#endif // VIRTIO_FS_DXE_H_\r