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