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