1 | /** @file
|
---|
2 |
|
---|
3 | This driver produces EFI_RNG_PROTOCOL instances for virtio-rng devices.
|
---|
4 |
|
---|
5 | The implementation is based on OvmfPkg/VirtioScsiDxe/VirtioScsi.c
|
---|
6 |
|
---|
7 | Copyright (C) 2012, Red Hat, Inc.
|
---|
8 | Copyright (c) 2012 - 2014, Intel Corporation. All rights reserved.<BR>
|
---|
9 | Copyright (c) 2017, AMD Inc, All rights reserved.<BR>
|
---|
10 |
|
---|
11 | This driver:
|
---|
12 |
|
---|
13 | Copyright (C) 2016, Linaro Ltd.
|
---|
14 |
|
---|
15 | SPDX-License-Identifier: BSD-2-Clause-Patent
|
---|
16 |
|
---|
17 | **/
|
---|
18 |
|
---|
19 | #include <Library/BaseMemoryLib.h>
|
---|
20 | #include <Library/DebugLib.h>
|
---|
21 | #include <Library/MemoryAllocationLib.h>
|
---|
22 | #include <Library/UefiBootServicesTableLib.h>
|
---|
23 | #include <Library/UefiLib.h>
|
---|
24 | #include <Library/VirtioLib.h>
|
---|
25 |
|
---|
26 | #include "VirtioRng.h"
|
---|
27 |
|
---|
28 | /**
|
---|
29 | Returns information about the random number generation implementation.
|
---|
30 |
|
---|
31 | @param[in] This A pointer to the EFI_RNG_PROTOCOL
|
---|
32 | instance.
|
---|
33 | @param[in,out] RNGAlgorithmListSize On input, the size in bytes of
|
---|
34 | RNGAlgorithmList.
|
---|
35 | On output with a return code of
|
---|
36 | EFI_SUCCESS, the size in bytes of the
|
---|
37 | data returned in RNGAlgorithmList. On
|
---|
38 | output with a return code of
|
---|
39 | EFI_BUFFER_TOO_SMALL, the size of
|
---|
40 | RNGAlgorithmList required to obtain the
|
---|
41 | list.
|
---|
42 | @param[out] RNGAlgorithmList A caller-allocated memory buffer filled
|
---|
43 | by the driver with one EFI_RNG_ALGORITHM
|
---|
44 | element for each supported RNG algorithm.
|
---|
45 | The list must not change across multiple
|
---|
46 | calls to the same driver. The first
|
---|
47 | algorithm in the list is the default
|
---|
48 | algorithm for the driver.
|
---|
49 |
|
---|
50 | @retval EFI_SUCCESS The RNG algorithm list was returned
|
---|
51 | successfully.
|
---|
52 | @retval EFI_UNSUPPORTED The services is not supported by this
|
---|
53 | driver.
|
---|
54 | @retval EFI_DEVICE_ERROR The list of algorithms could not be
|
---|
55 | retrieved due to a hardware or firmware
|
---|
56 | error.
|
---|
57 | @retval EFI_INVALID_PARAMETER One or more of the parameters are
|
---|
58 | incorrect.
|
---|
59 | @retval EFI_BUFFER_TOO_SMALL The buffer RNGAlgorithmList is too small
|
---|
60 | to hold the result.
|
---|
61 |
|
---|
62 | **/
|
---|
63 | STATIC
|
---|
64 | EFI_STATUS
|
---|
65 | EFIAPI
|
---|
66 | VirtioRngGetInfo (
|
---|
67 | IN EFI_RNG_PROTOCOL *This,
|
---|
68 | IN OUT UINTN *RNGAlgorithmListSize,
|
---|
69 | OUT EFI_RNG_ALGORITHM *RNGAlgorithmList
|
---|
70 | )
|
---|
71 | {
|
---|
72 | if ((This == NULL) || (RNGAlgorithmListSize == NULL)) {
|
---|
73 | return EFI_INVALID_PARAMETER;
|
---|
74 | }
|
---|
75 |
|
---|
76 | if (*RNGAlgorithmListSize < sizeof (EFI_RNG_ALGORITHM)) {
|
---|
77 | *RNGAlgorithmListSize = sizeof (EFI_RNG_ALGORITHM);
|
---|
78 | return EFI_BUFFER_TOO_SMALL;
|
---|
79 | }
|
---|
80 |
|
---|
81 | if (RNGAlgorithmList == NULL) {
|
---|
82 | return EFI_INVALID_PARAMETER;
|
---|
83 | }
|
---|
84 |
|
---|
85 | *RNGAlgorithmListSize = sizeof (EFI_RNG_ALGORITHM);
|
---|
86 | CopyGuid (RNGAlgorithmList, &gEfiRngAlgorithmRaw);
|
---|
87 |
|
---|
88 | return EFI_SUCCESS;
|
---|
89 | }
|
---|
90 |
|
---|
91 | /**
|
---|
92 | Produces and returns an RNG value using either the default or specified RNG
|
---|
93 | algorithm.
|
---|
94 |
|
---|
95 | @param[in] This A pointer to the EFI_RNG_PROTOCOL
|
---|
96 | instance.
|
---|
97 | @param[in] RNGAlgorithm A pointer to the EFI_RNG_ALGORITHM that
|
---|
98 | identifies the RNG algorithm to use. May
|
---|
99 | be NULL in which case the function will
|
---|
100 | use its default RNG algorithm.
|
---|
101 | @param[in] RNGValueLength The length in bytes of the memory buffer
|
---|
102 | pointed to by RNGValue. The driver shall
|
---|
103 | return exactly this numbers of bytes.
|
---|
104 | @param[out] RNGValue A caller-allocated memory buffer filled
|
---|
105 | by the driver with the resulting RNG
|
---|
106 | value.
|
---|
107 |
|
---|
108 | @retval EFI_SUCCESS The RNG value was returned successfully.
|
---|
109 | @retval EFI_UNSUPPORTED The algorithm specified by RNGAlgorithm
|
---|
110 | is not supported by this driver.
|
---|
111 | @retval EFI_DEVICE_ERROR An RNG value could not be retrieved due
|
---|
112 | to a hardware or firmware error.
|
---|
113 | @retval EFI_NOT_READY There is not enough random data available
|
---|
114 | to satisfy the length requested by
|
---|
115 | RNGValueLength.
|
---|
116 | @retval EFI_INVALID_PARAMETER RNGValue is NULL or RNGValueLength is
|
---|
117 | zero.
|
---|
118 |
|
---|
119 | **/
|
---|
120 | STATIC
|
---|
121 | EFI_STATUS
|
---|
122 | EFIAPI
|
---|
123 | VirtioRngGetRNG (
|
---|
124 | IN EFI_RNG_PROTOCOL *This,
|
---|
125 | IN EFI_RNG_ALGORITHM *RNGAlgorithm OPTIONAL,
|
---|
126 | IN UINTN RNGValueLength,
|
---|
127 | OUT UINT8 *RNGValue
|
---|
128 | )
|
---|
129 | {
|
---|
130 | VIRTIO_RNG_DEV *Dev;
|
---|
131 | DESC_INDICES Indices;
|
---|
132 | volatile UINT8 *Buffer;
|
---|
133 | UINTN Index;
|
---|
134 | UINT32 Len;
|
---|
135 | UINT32 BufferSize;
|
---|
136 | EFI_STATUS Status;
|
---|
137 | EFI_PHYSICAL_ADDRESS DeviceAddress;
|
---|
138 | VOID *Mapping;
|
---|
139 |
|
---|
140 | if ((This == NULL) || (RNGValueLength == 0) || (RNGValue == NULL)) {
|
---|
141 | return EFI_INVALID_PARAMETER;
|
---|
142 | }
|
---|
143 |
|
---|
144 | //
|
---|
145 | // We only support the raw algorithm, so reject requests for anything else
|
---|
146 | //
|
---|
147 | if ((RNGAlgorithm != NULL) &&
|
---|
148 | !CompareGuid (RNGAlgorithm, &gEfiRngAlgorithmRaw))
|
---|
149 | {
|
---|
150 | return EFI_UNSUPPORTED;
|
---|
151 | }
|
---|
152 |
|
---|
153 | Buffer = (volatile UINT8 *)AllocatePool (RNGValueLength);
|
---|
154 | if (Buffer == NULL) {
|
---|
155 | return EFI_DEVICE_ERROR;
|
---|
156 | }
|
---|
157 |
|
---|
158 | Dev = VIRTIO_ENTROPY_SOURCE_FROM_RNG (This);
|
---|
159 | //
|
---|
160 | // Map Buffer's system physical address to device address
|
---|
161 | //
|
---|
162 | Status = VirtioMapAllBytesInSharedBuffer (
|
---|
163 | Dev->VirtIo,
|
---|
164 | VirtioOperationBusMasterWrite,
|
---|
165 | (VOID *)Buffer,
|
---|
166 | RNGValueLength,
|
---|
167 | &DeviceAddress,
|
---|
168 | &Mapping
|
---|
169 | );
|
---|
170 | if (EFI_ERROR (Status)) {
|
---|
171 | Status = EFI_DEVICE_ERROR;
|
---|
172 | goto FreeBuffer;
|
---|
173 | }
|
---|
174 |
|
---|
175 | //
|
---|
176 | // The Virtio RNG device may return less data than we asked it to, and can
|
---|
177 | // only return MAX_UINT32 bytes per invocation. So loop as long as needed to
|
---|
178 | // get all the entropy we were asked for.
|
---|
179 | //
|
---|
180 | for (Index = 0; Index < RNGValueLength; Index += Len) {
|
---|
181 | BufferSize = (UINT32)MIN (RNGValueLength - Index, (UINTN)MAX_UINT32);
|
---|
182 |
|
---|
183 | VirtioPrepare (&Dev->Ring, &Indices);
|
---|
184 | VirtioAppendDesc (
|
---|
185 | &Dev->Ring,
|
---|
186 | DeviceAddress + Index,
|
---|
187 | BufferSize,
|
---|
188 | VRING_DESC_F_WRITE,
|
---|
189 | &Indices
|
---|
190 | );
|
---|
191 |
|
---|
192 | if (VirtioFlush (Dev->VirtIo, 0, &Dev->Ring, &Indices, &Len) !=
|
---|
193 | EFI_SUCCESS)
|
---|
194 | {
|
---|
195 | Status = EFI_DEVICE_ERROR;
|
---|
196 | goto UnmapBuffer;
|
---|
197 | }
|
---|
198 |
|
---|
199 | ASSERT (Len > 0);
|
---|
200 | ASSERT (Len <= BufferSize);
|
---|
201 | }
|
---|
202 |
|
---|
203 | //
|
---|
204 | // Unmap the device buffer before accessing it.
|
---|
205 | //
|
---|
206 | Status = Dev->VirtIo->UnmapSharedBuffer (Dev->VirtIo, Mapping);
|
---|
207 | if (EFI_ERROR (Status)) {
|
---|
208 | Status = EFI_DEVICE_ERROR;
|
---|
209 | goto FreeBuffer;
|
---|
210 | }
|
---|
211 |
|
---|
212 | for (Index = 0; Index < RNGValueLength; Index++) {
|
---|
213 | RNGValue[Index] = Buffer[Index];
|
---|
214 | }
|
---|
215 |
|
---|
216 | Status = EFI_SUCCESS;
|
---|
217 |
|
---|
218 | UnmapBuffer:
|
---|
219 | //
|
---|
220 | // If we are reached here due to the error then unmap the buffer otherwise
|
---|
221 | // the buffer is already unmapped after VirtioFlush().
|
---|
222 | //
|
---|
223 | if (EFI_ERROR (Status)) {
|
---|
224 | Dev->VirtIo->UnmapSharedBuffer (Dev->VirtIo, Mapping);
|
---|
225 | }
|
---|
226 |
|
---|
227 | FreeBuffer:
|
---|
228 | FreePool ((VOID *)Buffer);
|
---|
229 | return Status;
|
---|
230 | }
|
---|
231 |
|
---|
232 | STATIC
|
---|
233 | EFI_STATUS
|
---|
234 | EFIAPI
|
---|
235 | VirtioRngInit (
|
---|
236 | IN OUT VIRTIO_RNG_DEV *Dev
|
---|
237 | )
|
---|
238 | {
|
---|
239 | UINT8 NextDevStat;
|
---|
240 | EFI_STATUS Status;
|
---|
241 | UINT16 QueueSize;
|
---|
242 | UINT64 Features;
|
---|
243 | UINT64 RingBaseShift;
|
---|
244 |
|
---|
245 | //
|
---|
246 | // Execute virtio-0.9.5, 2.2.1 Device Initialization Sequence.
|
---|
247 | //
|
---|
248 | NextDevStat = 0; // step 1 -- reset device
|
---|
249 | Status = Dev->VirtIo->SetDeviceStatus (Dev->VirtIo, NextDevStat);
|
---|
250 | if (EFI_ERROR (Status)) {
|
---|
251 | goto Failed;
|
---|
252 | }
|
---|
253 |
|
---|
254 | NextDevStat |= VSTAT_ACK; // step 2 -- acknowledge device presence
|
---|
255 | Status = Dev->VirtIo->SetDeviceStatus (Dev->VirtIo, NextDevStat);
|
---|
256 | if (EFI_ERROR (Status)) {
|
---|
257 | goto Failed;
|
---|
258 | }
|
---|
259 |
|
---|
260 | NextDevStat |= VSTAT_DRIVER; // step 3 -- we know how to drive it
|
---|
261 | Status = Dev->VirtIo->SetDeviceStatus (Dev->VirtIo, NextDevStat);
|
---|
262 | if (EFI_ERROR (Status)) {
|
---|
263 | goto Failed;
|
---|
264 | }
|
---|
265 |
|
---|
266 | //
|
---|
267 | // Set Page Size - MMIO VirtIo Specific
|
---|
268 | //
|
---|
269 | Status = Dev->VirtIo->SetPageSize (Dev->VirtIo, EFI_PAGE_SIZE);
|
---|
270 | if (EFI_ERROR (Status)) {
|
---|
271 | goto Failed;
|
---|
272 | }
|
---|
273 |
|
---|
274 | //
|
---|
275 | // step 4a -- retrieve and validate features
|
---|
276 | //
|
---|
277 | Status = Dev->VirtIo->GetDeviceFeatures (Dev->VirtIo, &Features);
|
---|
278 | if (EFI_ERROR (Status)) {
|
---|
279 | goto Failed;
|
---|
280 | }
|
---|
281 |
|
---|
282 | Features &= VIRTIO_F_VERSION_1 | VIRTIO_F_IOMMU_PLATFORM;
|
---|
283 |
|
---|
284 | //
|
---|
285 | // In virtio-1.0, feature negotiation is expected to complete before queue
|
---|
286 | // discovery, and the device can also reject the selected set of features.
|
---|
287 | //
|
---|
288 | if (Dev->VirtIo->Revision >= VIRTIO_SPEC_REVISION (1, 0, 0)) {
|
---|
289 | Status = Virtio10WriteFeatures (Dev->VirtIo, Features, &NextDevStat);
|
---|
290 | if (EFI_ERROR (Status)) {
|
---|
291 | goto Failed;
|
---|
292 | }
|
---|
293 | }
|
---|
294 |
|
---|
295 | //
|
---|
296 | // step 4b -- allocate request virtqueue, just use #0
|
---|
297 | //
|
---|
298 | Status = Dev->VirtIo->SetQueueSel (Dev->VirtIo, 0);
|
---|
299 | if (EFI_ERROR (Status)) {
|
---|
300 | goto Failed;
|
---|
301 | }
|
---|
302 |
|
---|
303 | Status = Dev->VirtIo->GetQueueNumMax (Dev->VirtIo, &QueueSize);
|
---|
304 | if (EFI_ERROR (Status)) {
|
---|
305 | goto Failed;
|
---|
306 | }
|
---|
307 |
|
---|
308 | //
|
---|
309 | // VirtioRngGetRNG() uses one descriptor
|
---|
310 | //
|
---|
311 | if (QueueSize < 1) {
|
---|
312 | Status = EFI_UNSUPPORTED;
|
---|
313 | goto Failed;
|
---|
314 | }
|
---|
315 |
|
---|
316 | Status = VirtioRingInit (Dev->VirtIo, QueueSize, &Dev->Ring);
|
---|
317 | if (EFI_ERROR (Status)) {
|
---|
318 | goto Failed;
|
---|
319 | }
|
---|
320 |
|
---|
321 | //
|
---|
322 | // If anything fails from here on, we must release the ring resources.
|
---|
323 | //
|
---|
324 | Status = VirtioRingMap (
|
---|
325 | Dev->VirtIo,
|
---|
326 | &Dev->Ring,
|
---|
327 | &RingBaseShift,
|
---|
328 | &Dev->RingMap
|
---|
329 | );
|
---|
330 | if (EFI_ERROR (Status)) {
|
---|
331 | goto ReleaseQueue;
|
---|
332 | }
|
---|
333 |
|
---|
334 | //
|
---|
335 | // Additional steps for MMIO: align the queue appropriately, and set the
|
---|
336 | // size. If anything fails from here on, we must unmap the ring resources.
|
---|
337 | //
|
---|
338 | Status = Dev->VirtIo->SetQueueNum (Dev->VirtIo, QueueSize);
|
---|
339 | if (EFI_ERROR (Status)) {
|
---|
340 | goto UnmapQueue;
|
---|
341 | }
|
---|
342 |
|
---|
343 | Status = Dev->VirtIo->SetQueueAlign (Dev->VirtIo, EFI_PAGE_SIZE);
|
---|
344 | if (EFI_ERROR (Status)) {
|
---|
345 | goto UnmapQueue;
|
---|
346 | }
|
---|
347 |
|
---|
348 | //
|
---|
349 | // step 4c -- Report GPFN (guest-physical frame number) of queue.
|
---|
350 | //
|
---|
351 | Status = Dev->VirtIo->SetQueueAddress (
|
---|
352 | Dev->VirtIo,
|
---|
353 | &Dev->Ring,
|
---|
354 | RingBaseShift
|
---|
355 | );
|
---|
356 | if (EFI_ERROR (Status)) {
|
---|
357 | goto UnmapQueue;
|
---|
358 | }
|
---|
359 |
|
---|
360 | //
|
---|
361 | // step 5 -- Report understood features and guest-tuneables.
|
---|
362 | //
|
---|
363 | if (Dev->VirtIo->Revision < VIRTIO_SPEC_REVISION (1, 0, 0)) {
|
---|
364 | Features &= ~(UINT64)(VIRTIO_F_VERSION_1 | VIRTIO_F_IOMMU_PLATFORM);
|
---|
365 | Status = Dev->VirtIo->SetGuestFeatures (Dev->VirtIo, Features);
|
---|
366 | if (EFI_ERROR (Status)) {
|
---|
367 | goto UnmapQueue;
|
---|
368 | }
|
---|
369 | }
|
---|
370 |
|
---|
371 | //
|
---|
372 | // step 6 -- initialization complete
|
---|
373 | //
|
---|
374 | NextDevStat |= VSTAT_DRIVER_OK;
|
---|
375 | Status = Dev->VirtIo->SetDeviceStatus (Dev->VirtIo, NextDevStat);
|
---|
376 | if (EFI_ERROR (Status)) {
|
---|
377 | goto UnmapQueue;
|
---|
378 | }
|
---|
379 |
|
---|
380 | //
|
---|
381 | // populate the exported interface's attributes
|
---|
382 | //
|
---|
383 | Dev->Rng.GetInfo = VirtioRngGetInfo;
|
---|
384 | Dev->Rng.GetRNG = VirtioRngGetRNG;
|
---|
385 |
|
---|
386 | return EFI_SUCCESS;
|
---|
387 |
|
---|
388 | UnmapQueue:
|
---|
389 | Dev->VirtIo->UnmapSharedBuffer (Dev->VirtIo, Dev->RingMap);
|
---|
390 |
|
---|
391 | ReleaseQueue:
|
---|
392 | VirtioRingUninit (Dev->VirtIo, &Dev->Ring);
|
---|
393 |
|
---|
394 | Failed:
|
---|
395 | //
|
---|
396 | // Notify the host about our failure to setup: virtio-0.9.5, 2.2.2.1 Device
|
---|
397 | // Status. VirtIo access failure here should not mask the original error.
|
---|
398 | //
|
---|
399 | NextDevStat |= VSTAT_FAILED;
|
---|
400 | Dev->VirtIo->SetDeviceStatus (Dev->VirtIo, NextDevStat);
|
---|
401 |
|
---|
402 | return Status; // reached only via Failed above
|
---|
403 | }
|
---|
404 |
|
---|
405 | STATIC
|
---|
406 | VOID
|
---|
407 | EFIAPI
|
---|
408 | VirtioRngUninit (
|
---|
409 | IN OUT VIRTIO_RNG_DEV *Dev
|
---|
410 | )
|
---|
411 | {
|
---|
412 | //
|
---|
413 | // Reset the virtual device -- see virtio-0.9.5, 2.2.2.1 Device Status. When
|
---|
414 | // VIRTIO_CFG_WRITE() returns, the host will have learned to stay away from
|
---|
415 | // the old comms area.
|
---|
416 | //
|
---|
417 | Dev->VirtIo->SetDeviceStatus (Dev->VirtIo, 0);
|
---|
418 |
|
---|
419 | Dev->VirtIo->UnmapSharedBuffer (Dev->VirtIo, Dev->RingMap);
|
---|
420 |
|
---|
421 | VirtioRingUninit (Dev->VirtIo, &Dev->Ring);
|
---|
422 | }
|
---|
423 |
|
---|
424 | //
|
---|
425 | // Event notification function enqueued by ExitBootServices().
|
---|
426 | //
|
---|
427 |
|
---|
428 | STATIC
|
---|
429 | VOID
|
---|
430 | EFIAPI
|
---|
431 | VirtioRngExitBoot (
|
---|
432 | IN EFI_EVENT Event,
|
---|
433 | IN VOID *Context
|
---|
434 | )
|
---|
435 | {
|
---|
436 | VIRTIO_RNG_DEV *Dev;
|
---|
437 |
|
---|
438 | DEBUG ((DEBUG_VERBOSE, "%a: Context=0x%p\n", __FUNCTION__, Context));
|
---|
439 | //
|
---|
440 | // Reset the device. This causes the hypervisor to forget about the virtio
|
---|
441 | // ring.
|
---|
442 | //
|
---|
443 | // We allocated said ring in EfiBootServicesData type memory, and code
|
---|
444 | // executing after ExitBootServices() is permitted to overwrite it.
|
---|
445 | //
|
---|
446 | Dev = Context;
|
---|
447 | Dev->VirtIo->SetDeviceStatus (Dev->VirtIo, 0);
|
---|
448 | }
|
---|
449 |
|
---|
450 | //
|
---|
451 | // Probe, start and stop functions of this driver, called by the DXE core for
|
---|
452 | // specific devices.
|
---|
453 | //
|
---|
454 | // The following specifications document these interfaces:
|
---|
455 | // - Driver Writer's Guide for UEFI 2.3.1 v1.01, 9 Driver Binding Protocol
|
---|
456 | // - UEFI Spec 2.3.1 + Errata C, 10.1 EFI Driver Binding Protocol
|
---|
457 | //
|
---|
458 | // The implementation follows:
|
---|
459 | // - Driver Writer's Guide for UEFI 2.3.1 v1.01
|
---|
460 | // - 5.1.3.4 OpenProtocol() and CloseProtocol()
|
---|
461 | // - UEFI Spec 2.3.1 + Errata C
|
---|
462 | // - 6.3 Protocol Handler Services
|
---|
463 | //
|
---|
464 |
|
---|
465 | STATIC
|
---|
466 | EFI_STATUS
|
---|
467 | EFIAPI
|
---|
468 | VirtioRngDriverBindingSupported (
|
---|
469 | IN EFI_DRIVER_BINDING_PROTOCOL *This,
|
---|
470 | IN EFI_HANDLE DeviceHandle,
|
---|
471 | IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
|
---|
472 | )
|
---|
473 | {
|
---|
474 | EFI_STATUS Status;
|
---|
475 | VIRTIO_DEVICE_PROTOCOL *VirtIo;
|
---|
476 |
|
---|
477 | //
|
---|
478 | // Attempt to open the device with the VirtIo set of interfaces. On success,
|
---|
479 | // the protocol is "instantiated" for the VirtIo device. Covers duplicate
|
---|
480 | // open attempts (EFI_ALREADY_STARTED).
|
---|
481 | //
|
---|
482 | Status = gBS->OpenProtocol (
|
---|
483 | DeviceHandle, // candidate device
|
---|
484 | &gVirtioDeviceProtocolGuid, // for generic VirtIo access
|
---|
485 | (VOID **)&VirtIo, // handle to instantiate
|
---|
486 | This->DriverBindingHandle, // requestor driver identity
|
---|
487 | DeviceHandle, // ControllerHandle, according to
|
---|
488 | // the UEFI Driver Model
|
---|
489 | EFI_OPEN_PROTOCOL_BY_DRIVER // get exclusive VirtIo access to
|
---|
490 | // the device; to be released
|
---|
491 | );
|
---|
492 | if (EFI_ERROR (Status)) {
|
---|
493 | return Status;
|
---|
494 | }
|
---|
495 |
|
---|
496 | if (VirtIo->SubSystemDeviceId != VIRTIO_SUBSYSTEM_ENTROPY_SOURCE) {
|
---|
497 | Status = EFI_UNSUPPORTED;
|
---|
498 | }
|
---|
499 |
|
---|
500 | //
|
---|
501 | // We needed VirtIo access only transitorily, to see whether we support the
|
---|
502 | // device or not.
|
---|
503 | //
|
---|
504 | gBS->CloseProtocol (
|
---|
505 | DeviceHandle,
|
---|
506 | &gVirtioDeviceProtocolGuid,
|
---|
507 | This->DriverBindingHandle,
|
---|
508 | DeviceHandle
|
---|
509 | );
|
---|
510 | return Status;
|
---|
511 | }
|
---|
512 |
|
---|
513 | STATIC
|
---|
514 | EFI_STATUS
|
---|
515 | EFIAPI
|
---|
516 | VirtioRngDriverBindingStart (
|
---|
517 | IN EFI_DRIVER_BINDING_PROTOCOL *This,
|
---|
518 | IN EFI_HANDLE DeviceHandle,
|
---|
519 | IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
|
---|
520 | )
|
---|
521 | {
|
---|
522 | VIRTIO_RNG_DEV *Dev;
|
---|
523 | EFI_STATUS Status;
|
---|
524 |
|
---|
525 | Dev = (VIRTIO_RNG_DEV *)AllocateZeroPool (sizeof *Dev);
|
---|
526 | if (Dev == NULL) {
|
---|
527 | return EFI_OUT_OF_RESOURCES;
|
---|
528 | }
|
---|
529 |
|
---|
530 | Status = gBS->OpenProtocol (
|
---|
531 | DeviceHandle,
|
---|
532 | &gVirtioDeviceProtocolGuid,
|
---|
533 | (VOID **)&Dev->VirtIo,
|
---|
534 | This->DriverBindingHandle,
|
---|
535 | DeviceHandle,
|
---|
536 | EFI_OPEN_PROTOCOL_BY_DRIVER
|
---|
537 | );
|
---|
538 | if (EFI_ERROR (Status)) {
|
---|
539 | goto FreeVirtioRng;
|
---|
540 | }
|
---|
541 |
|
---|
542 | //
|
---|
543 | // VirtIo access granted, configure virtio-rng device.
|
---|
544 | //
|
---|
545 | Status = VirtioRngInit (Dev);
|
---|
546 | if (EFI_ERROR (Status)) {
|
---|
547 | goto CloseVirtIo;
|
---|
548 | }
|
---|
549 |
|
---|
550 | Status = gBS->CreateEvent (
|
---|
551 | EVT_SIGNAL_EXIT_BOOT_SERVICES,
|
---|
552 | TPL_CALLBACK,
|
---|
553 | &VirtioRngExitBoot,
|
---|
554 | Dev,
|
---|
555 | &Dev->ExitBoot
|
---|
556 | );
|
---|
557 | if (EFI_ERROR (Status)) {
|
---|
558 | goto UninitDev;
|
---|
559 | }
|
---|
560 |
|
---|
561 | //
|
---|
562 | // Setup complete, attempt to export the driver instance's EFI_RNG_PROTOCOL
|
---|
563 | // interface.
|
---|
564 | //
|
---|
565 | Dev->Signature = VIRTIO_RNG_SIG;
|
---|
566 | Status = gBS->InstallProtocolInterface (
|
---|
567 | &DeviceHandle,
|
---|
568 | &gEfiRngProtocolGuid,
|
---|
569 | EFI_NATIVE_INTERFACE,
|
---|
570 | &Dev->Rng
|
---|
571 | );
|
---|
572 | if (EFI_ERROR (Status)) {
|
---|
573 | goto CloseExitBoot;
|
---|
574 | }
|
---|
575 |
|
---|
576 | return EFI_SUCCESS;
|
---|
577 |
|
---|
578 | CloseExitBoot:
|
---|
579 | gBS->CloseEvent (Dev->ExitBoot);
|
---|
580 |
|
---|
581 | UninitDev:
|
---|
582 | VirtioRngUninit (Dev);
|
---|
583 |
|
---|
584 | CloseVirtIo:
|
---|
585 | gBS->CloseProtocol (
|
---|
586 | DeviceHandle,
|
---|
587 | &gVirtioDeviceProtocolGuid,
|
---|
588 | This->DriverBindingHandle,
|
---|
589 | DeviceHandle
|
---|
590 | );
|
---|
591 |
|
---|
592 | FreeVirtioRng:
|
---|
593 | FreePool (Dev);
|
---|
594 |
|
---|
595 | return Status;
|
---|
596 | }
|
---|
597 |
|
---|
598 | STATIC
|
---|
599 | EFI_STATUS
|
---|
600 | EFIAPI
|
---|
601 | VirtioRngDriverBindingStop (
|
---|
602 | IN EFI_DRIVER_BINDING_PROTOCOL *This,
|
---|
603 | IN EFI_HANDLE DeviceHandle,
|
---|
604 | IN UINTN NumberOfChildren,
|
---|
605 | IN EFI_HANDLE *ChildHandleBuffer
|
---|
606 | )
|
---|
607 | {
|
---|
608 | EFI_STATUS Status;
|
---|
609 | EFI_RNG_PROTOCOL *Rng;
|
---|
610 | VIRTIO_RNG_DEV *Dev;
|
---|
611 |
|
---|
612 | Status = gBS->OpenProtocol (
|
---|
613 | DeviceHandle, // candidate device
|
---|
614 | &gEfiRngProtocolGuid, // retrieve the RNG iface
|
---|
615 | (VOID **)&Rng, // target pointer
|
---|
616 | This->DriverBindingHandle, // requestor driver ident.
|
---|
617 | DeviceHandle, // lookup req. for dev.
|
---|
618 | EFI_OPEN_PROTOCOL_GET_PROTOCOL // lookup only, no new ref.
|
---|
619 | );
|
---|
620 | if (EFI_ERROR (Status)) {
|
---|
621 | return Status;
|
---|
622 | }
|
---|
623 |
|
---|
624 | Dev = VIRTIO_ENTROPY_SOURCE_FROM_RNG (Rng);
|
---|
625 |
|
---|
626 | //
|
---|
627 | // Handle Stop() requests for in-use driver instances gracefully.
|
---|
628 | //
|
---|
629 | Status = gBS->UninstallProtocolInterface (
|
---|
630 | DeviceHandle,
|
---|
631 | &gEfiRngProtocolGuid,
|
---|
632 | &Dev->Rng
|
---|
633 | );
|
---|
634 | if (EFI_ERROR (Status)) {
|
---|
635 | return Status;
|
---|
636 | }
|
---|
637 |
|
---|
638 | gBS->CloseEvent (Dev->ExitBoot);
|
---|
639 |
|
---|
640 | VirtioRngUninit (Dev);
|
---|
641 |
|
---|
642 | gBS->CloseProtocol (
|
---|
643 | DeviceHandle,
|
---|
644 | &gVirtioDeviceProtocolGuid,
|
---|
645 | This->DriverBindingHandle,
|
---|
646 | DeviceHandle
|
---|
647 | );
|
---|
648 |
|
---|
649 | FreePool (Dev);
|
---|
650 |
|
---|
651 | return EFI_SUCCESS;
|
---|
652 | }
|
---|
653 |
|
---|
654 | //
|
---|
655 | // The static object that groups the Supported() (ie. probe), Start() and
|
---|
656 | // Stop() functions of the driver together. Refer to UEFI Spec 2.3.1 + Errata
|
---|
657 | // C, 10.1 EFI Driver Binding Protocol.
|
---|
658 | //
|
---|
659 | STATIC EFI_DRIVER_BINDING_PROTOCOL gDriverBinding = {
|
---|
660 | &VirtioRngDriverBindingSupported,
|
---|
661 | &VirtioRngDriverBindingStart,
|
---|
662 | &VirtioRngDriverBindingStop,
|
---|
663 | 0x10, // Version, must be in [0x10 .. 0xFFFFFFEF] for IHV-developed drivers
|
---|
664 | NULL, // ImageHandle, to be overwritten by
|
---|
665 | // EfiLibInstallDriverBindingComponentName2() in VirtioRngEntryPoint()
|
---|
666 | NULL // DriverBindingHandle, ditto
|
---|
667 | };
|
---|
668 |
|
---|
669 | //
|
---|
670 | // The purpose of the following scaffolding (EFI_COMPONENT_NAME_PROTOCOL and
|
---|
671 | // EFI_COMPONENT_NAME2_PROTOCOL implementation) is to format the driver's name
|
---|
672 | // in English, for display on standard console devices. This is recommended for
|
---|
673 | // UEFI drivers that follow the UEFI Driver Model. Refer to the Driver Writer's
|
---|
674 | // Guide for UEFI 2.3.1 v1.01, 11 UEFI Driver and Controller Names.
|
---|
675 | //
|
---|
676 |
|
---|
677 | STATIC
|
---|
678 | EFI_UNICODE_STRING_TABLE mDriverNameTable[] = {
|
---|
679 | { "eng;en", L"Virtio Random Number Generator Driver" },
|
---|
680 | { NULL, NULL }
|
---|
681 | };
|
---|
682 |
|
---|
683 | STATIC
|
---|
684 | EFI_COMPONENT_NAME_PROTOCOL gComponentName;
|
---|
685 |
|
---|
686 | STATIC
|
---|
687 | EFI_STATUS
|
---|
688 | EFIAPI
|
---|
689 | VirtioRngGetDriverName (
|
---|
690 | IN EFI_COMPONENT_NAME_PROTOCOL *This,
|
---|
691 | IN CHAR8 *Language,
|
---|
692 | OUT CHAR16 **DriverName
|
---|
693 | )
|
---|
694 | {
|
---|
695 | return LookupUnicodeString2 (
|
---|
696 | Language,
|
---|
697 | This->SupportedLanguages,
|
---|
698 | mDriverNameTable,
|
---|
699 | DriverName,
|
---|
700 | (BOOLEAN)(This == &gComponentName) // Iso639Language
|
---|
701 | );
|
---|
702 | }
|
---|
703 |
|
---|
704 | STATIC
|
---|
705 | EFI_STATUS
|
---|
706 | EFIAPI
|
---|
707 | VirtioRngGetDeviceName (
|
---|
708 | IN EFI_COMPONENT_NAME_PROTOCOL *This,
|
---|
709 | IN EFI_HANDLE DeviceHandle,
|
---|
710 | IN EFI_HANDLE ChildHandle,
|
---|
711 | IN CHAR8 *Language,
|
---|
712 | OUT CHAR16 **ControllerName
|
---|
713 | )
|
---|
714 | {
|
---|
715 | return EFI_UNSUPPORTED;
|
---|
716 | }
|
---|
717 |
|
---|
718 | STATIC
|
---|
719 | EFI_COMPONENT_NAME_PROTOCOL gComponentName = {
|
---|
720 | &VirtioRngGetDriverName,
|
---|
721 | &VirtioRngGetDeviceName,
|
---|
722 | "eng" // SupportedLanguages, ISO 639-2 language codes
|
---|
723 | };
|
---|
724 |
|
---|
725 | STATIC
|
---|
726 | EFI_COMPONENT_NAME2_PROTOCOL gComponentName2 = {
|
---|
727 | (EFI_COMPONENT_NAME2_GET_DRIVER_NAME)&VirtioRngGetDriverName,
|
---|
728 | (EFI_COMPONENT_NAME2_GET_CONTROLLER_NAME)&VirtioRngGetDeviceName,
|
---|
729 | "en" // SupportedLanguages, RFC 4646 language codes
|
---|
730 | };
|
---|
731 |
|
---|
732 | //
|
---|
733 | // Entry point of this driver.
|
---|
734 | //
|
---|
735 | EFI_STATUS
|
---|
736 | EFIAPI
|
---|
737 | VirtioRngEntryPoint (
|
---|
738 | IN EFI_HANDLE ImageHandle,
|
---|
739 | IN EFI_SYSTEM_TABLE *SystemTable
|
---|
740 | )
|
---|
741 | {
|
---|
742 | return EfiLibInstallDriverBindingComponentName2 (
|
---|
743 | ImageHandle,
|
---|
744 | SystemTable,
|
---|
745 | &gDriverBinding,
|
---|
746 | ImageHandle,
|
---|
747 | &gComponentName,
|
---|
748 | &gComponentName2
|
---|
749 | );
|
---|
750 | }
|
---|