TianoCore EDK2 master
Loading...
Searching...
No Matches
Tpm2NVStorage.c
Go to the documentation of this file.
1
13#include <Library/BaseLib.h>
14#include <Library/DebugLib.h>
15
16#pragma pack(1)
17
18#define RC_NV_ReadPublic_nvIndex (TPM_RC_H + TPM_RC_1)
19
20#define RC_NV_DefineSpace_authHandle (TPM_RC_H + TPM_RC_1)
21#define RC_NV_DefineSpace_auth (TPM_RC_P + TPM_RC_1)
22#define RC_NV_DefineSpace_publicInfo (TPM_RC_P + TPM_RC_2)
23
24#define RC_NV_UndefineSpace_authHandle (TPM_RC_H + TPM_RC_1)
25#define RC_NV_UndefineSpace_nvIndex (TPM_RC_H + TPM_RC_2)
26
27#define RC_NV_Read_authHandle (TPM_RC_H + TPM_RC_1)
28#define RC_NV_Read_nvIndex (TPM_RC_H + TPM_RC_2)
29#define RC_NV_Read_size (TPM_RC_P + TPM_RC_1)
30#define RC_NV_Read_offset (TPM_RC_P + TPM_RC_2)
31
32#define RC_NV_Write_authHandle (TPM_RC_H + TPM_RC_1)
33#define RC_NV_Write_nvIndex (TPM_RC_H + TPM_RC_2)
34#define RC_NV_Write_data (TPM_RC_P + TPM_RC_1)
35#define RC_NV_Write_offset (TPM_RC_P + TPM_RC_2)
36
37typedef struct {
39 TPMI_RH_NV_INDEX NvIndex;
41
42typedef struct {
44 TPM2B_NV_PUBLIC NvPublic;
45 TPM2B_NAME NvName;
47
48typedef struct {
50 TPMI_RH_PROVISION AuthHandle;
51 UINT32 AuthSessionSize;
52 TPMS_AUTH_COMMAND AuthSession;
53 TPM2B_AUTH Auth;
54 TPM2B_NV_PUBLIC NvPublic;
56
57typedef struct {
59 UINT32 AuthSessionSize;
60 TPMS_AUTH_RESPONSE AuthSession;
62
63typedef struct {
65 TPMI_RH_PROVISION AuthHandle;
66 TPMI_RH_NV_INDEX NvIndex;
67 UINT32 AuthSessionSize;
68 TPMS_AUTH_COMMAND AuthSession;
70
71typedef struct {
73 UINT32 AuthSessionSize;
74 TPMS_AUTH_RESPONSE AuthSession;
76
77typedef struct {
79 TPMI_RH_NV_AUTH AuthHandle;
80 TPMI_RH_NV_INDEX NvIndex;
81 UINT32 AuthSessionSize;
82 TPMS_AUTH_COMMAND AuthSession;
83 UINT16 Size;
84 UINT16 Offset;
86
87typedef struct {
89 UINT32 AuthSessionSize;
91 TPMS_AUTH_RESPONSE AuthSession;
93
94typedef struct {
96 TPMI_RH_NV_AUTH AuthHandle;
97 TPMI_RH_NV_INDEX NvIndex;
98 UINT32 AuthSessionSize;
99 TPMS_AUTH_COMMAND AuthSession;
100 TPM2B_MAX_BUFFER Data;
101 UINT16 Offset;
103
104typedef struct {
106 UINT32 AuthSessionSize;
107 TPMS_AUTH_RESPONSE AuthSession;
109
110typedef struct {
111 TPM2_COMMAND_HEADER Header;
112 TPMI_RH_NV_AUTH AuthHandle;
113 TPMI_RH_NV_INDEX NvIndex;
114 UINT32 AuthSessionSize;
115 TPMS_AUTH_COMMAND AuthSession;
117
118typedef struct {
120 UINT32 AuthSessionSize;
121 TPMS_AUTH_RESPONSE AuthSession;
123
124typedef struct {
125 TPM2_COMMAND_HEADER Header;
126 TPMI_RH_NV_AUTH AuthHandle;
127 TPMI_RH_NV_INDEX NvIndex;
128 UINT32 AuthSessionSize;
129 TPMS_AUTH_COMMAND AuthSession;
131
132typedef struct {
134 UINT32 AuthSessionSize;
135 TPMS_AUTH_RESPONSE AuthSession;
137
138typedef struct {
139 TPM2_COMMAND_HEADER Header;
140 TPMI_RH_PROVISION AuthHandle;
141 UINT32 AuthSessionSize;
142 TPMS_AUTH_COMMAND AuthSession;
144
145typedef struct {
147 UINT32 AuthSessionSize;
148 TPMS_AUTH_RESPONSE AuthSession;
150
151typedef struct {
152 TPM2_COMMAND_HEADER Header;
153 TPMI_RH_NV_AUTH AuthHandle;
154 TPMI_RH_NV_INDEX NvIndex;
155 UINT32 AuthSessionSize;
156 TPMS_AUTH_COMMAND AuthSession;
157 TPM2B_MAX_BUFFER Data;
158 UINT16 Offset;
160
161typedef struct {
163 UINT32 AuthSessionSize;
164 TPMS_AUTH_RESPONSE AuthSession;
166
167#pragma pack()
168
181EFIAPI
183 IN TPMI_RH_NV_INDEX NvIndex,
184 OUT TPM2B_NV_PUBLIC *NvPublic,
185 OUT TPM2B_NAME *NvName
186 )
187{
188 EFI_STATUS Status;
191 UINT32 SendBufferSize;
192 UINT32 RecvBufferSize;
193 UINT16 NvPublicSize;
194 UINT16 NvNameSize;
195 UINT8 *Buffer;
196 TPM_RC ResponseCode;
197
198 //
199 // Construct command
200 //
201 SendBuffer.Header.tag = SwapBytes16 (TPM_ST_NO_SESSIONS);
202 SendBuffer.Header.commandCode = SwapBytes32 (TPM_CC_NV_ReadPublic);
203
204 SendBuffer.NvIndex = SwapBytes32 (NvIndex);
205
206 SendBufferSize = (UINT32)sizeof (SendBuffer);
207 SendBuffer.Header.paramSize = SwapBytes32 (SendBufferSize);
208
209 //
210 // send Tpm command
211 //
212 RecvBufferSize = sizeof (RecvBuffer);
213 Status = Tpm2SubmitCommand (SendBufferSize, (UINT8 *)&SendBuffer, &RecvBufferSize, (UINT8 *)&RecvBuffer);
214 if (EFI_ERROR (Status)) {
215 return Status;
216 }
217
218 if (RecvBufferSize < sizeof (TPM2_RESPONSE_HEADER)) {
219 DEBUG ((DEBUG_ERROR, "Tpm2NvReadPublic - RecvBufferSize Error - %x\n", RecvBufferSize));
220 return EFI_DEVICE_ERROR;
221 }
222
223 ResponseCode = SwapBytes32 (RecvBuffer.Header.responseCode);
224 if (ResponseCode != TPM_RC_SUCCESS) {
225 DEBUG ((DEBUG_ERROR, "Tpm2NvReadPublic - responseCode - %x\n", SwapBytes32 (RecvBuffer.Header.responseCode)));
226 }
227
228 switch (ResponseCode) {
229 case TPM_RC_SUCCESS:
230 // return data
231 break;
232 case TPM_RC_HANDLE + RC_NV_ReadPublic_nvIndex: // TPM_RC_NV_DEFINED:
233 return EFI_NOT_FOUND;
234 case TPM_RC_VALUE + RC_NV_ReadPublic_nvIndex:
235 return EFI_INVALID_PARAMETER;
236 default:
237 return EFI_DEVICE_ERROR;
238 }
239
240 if (RecvBufferSize <= sizeof (TPM2_RESPONSE_HEADER) + sizeof (UINT16) + sizeof (UINT16)) {
241 DEBUG ((DEBUG_ERROR, "Tpm2NvReadPublic - RecvBufferSize Error - %x\n", RecvBufferSize));
242 return EFI_NOT_FOUND;
243 }
244
245 //
246 // Basic check
247 //
248 NvPublicSize = SwapBytes16 (RecvBuffer.NvPublic.size);
249 if (NvPublicSize > sizeof (TPMS_NV_PUBLIC)) {
250 DEBUG ((DEBUG_ERROR, "Tpm2NvReadPublic - NvPublic.size error %x\n", NvPublicSize));
251 return EFI_DEVICE_ERROR;
252 }
253
254 NvNameSize = SwapBytes16 (ReadUnaligned16 ((UINT16 *)((UINT8 *)&RecvBuffer + sizeof (TPM2_RESPONSE_HEADER) + sizeof (UINT16) + NvPublicSize)));
255 if (NvNameSize > sizeof (TPMU_NAME)) {
256 DEBUG ((DEBUG_ERROR, "Tpm2NvReadPublic - NvNameSize error %x\n", NvNameSize));
257 return EFI_DEVICE_ERROR;
258 }
259
260 if (RecvBufferSize != sizeof (TPM2_RESPONSE_HEADER) + sizeof (UINT16) + NvPublicSize + sizeof (UINT16) + NvNameSize) {
261 DEBUG ((DEBUG_ERROR, "Tpm2NvReadPublic - RecvBufferSize Error - NvPublicSize %x\n", RecvBufferSize));
262 return EFI_NOT_FOUND;
263 }
264
265 //
266 // Return the response
267 //
268 CopyMem (NvPublic, &RecvBuffer.NvPublic, sizeof (UINT16) + NvPublicSize);
269 NvPublic->size = NvPublicSize;
270 NvPublic->nvPublic.nvIndex = SwapBytes32 (NvPublic->nvPublic.nvIndex);
271 NvPublic->nvPublic.nameAlg = SwapBytes16 (NvPublic->nvPublic.nameAlg);
272 WriteUnaligned32 ((UINT32 *)&NvPublic->nvPublic.attributes, SwapBytes32 (ReadUnaligned32 ((UINT32 *)&NvPublic->nvPublic.attributes)));
273 NvPublic->nvPublic.authPolicy.size = SwapBytes16 (NvPublic->nvPublic.authPolicy.size);
274 Buffer = (UINT8 *)&RecvBuffer.NvPublic.nvPublic.authPolicy;
275 Buffer += sizeof (UINT16) + NvPublic->nvPublic.authPolicy.size;
276 NvPublic->nvPublic.dataSize = SwapBytes16 (ReadUnaligned16 ((UINT16 *)Buffer));
277
278 CopyMem (NvName->name, (UINT8 *)&RecvBuffer + sizeof (TPM2_RESPONSE_HEADER) + sizeof (UINT16) + NvPublicSize + sizeof (UINT16), NvNameSize);
279 NvName->size = NvNameSize;
280
281 return EFI_SUCCESS;
282}
283
299EFIAPI
301 IN TPMI_RH_PROVISION AuthHandle,
302 IN TPMS_AUTH_COMMAND *AuthSession OPTIONAL,
303 IN TPM2B_AUTH *Auth,
304 IN TPM2B_NV_PUBLIC *NvPublic
305 )
306{
307 EFI_STATUS Status;
310 UINT32 SendBufferSize;
311 UINT32 RecvBufferSize;
312 UINT16 NvPublicSize;
313 UINT8 *Buffer;
314 UINT32 SessionInfoSize;
315 TPM_RC ResponseCode;
316
317 //
318 // Construct command
319 //
320 SendBuffer.Header.tag = SwapBytes16 (TPM_ST_SESSIONS);
321 SendBuffer.Header.commandCode = SwapBytes32 (TPM_CC_NV_DefineSpace);
322 SendBuffer.AuthHandle = SwapBytes32 (AuthHandle);
323
324 //
325 // Add in Auth session
326 //
327 Buffer = (UINT8 *)&SendBuffer.AuthSession;
328
329 // sessionInfoSize
330 SessionInfoSize = CopyAuthSessionCommand (AuthSession, Buffer);
331 Buffer += SessionInfoSize;
332 SendBuffer.AuthSessionSize = SwapBytes32 (SessionInfoSize);
333
334 //
335 // IndexAuth
336 //
337 WriteUnaligned16 ((UINT16 *)Buffer, SwapBytes16 (Auth->size));
338 Buffer += sizeof (UINT16);
339 CopyMem (Buffer, Auth->buffer, Auth->size);
340 Buffer += Auth->size;
341
342 //
343 // NvPublic
344 //
345 NvPublicSize = NvPublic->size;
346
347 WriteUnaligned16 ((UINT16 *)Buffer, SwapBytes16 (NvPublicSize));
348 Buffer += sizeof (UINT16);
349 WriteUnaligned32 ((UINT32 *)Buffer, SwapBytes32 (NvPublic->nvPublic.nvIndex));
350 Buffer += sizeof (UINT32);
351 WriteUnaligned16 ((UINT16 *)Buffer, SwapBytes16 (NvPublic->nvPublic.nameAlg));
352 Buffer += sizeof (UINT16);
353 WriteUnaligned32 ((UINT32 *)Buffer, SwapBytes32 (ReadUnaligned32 ((UINT32 *)&NvPublic->nvPublic.attributes)));
354 Buffer += sizeof (UINT32);
355 WriteUnaligned16 ((UINT16 *)Buffer, SwapBytes16 (NvPublic->nvPublic.authPolicy.size));
356 Buffer += sizeof (UINT16);
357 CopyMem (Buffer, NvPublic->nvPublic.authPolicy.buffer, NvPublic->nvPublic.authPolicy.size);
358 Buffer += NvPublic->nvPublic.authPolicy.size;
359 WriteUnaligned16 ((UINT16 *)Buffer, SwapBytes16 (NvPublic->nvPublic.dataSize));
360 Buffer += sizeof (UINT16);
361
362 SendBufferSize = (UINT32)(Buffer - (UINT8 *)&SendBuffer);
363 SendBuffer.Header.paramSize = SwapBytes32 (SendBufferSize);
364
365 //
366 // send Tpm command
367 //
368 RecvBufferSize = sizeof (RecvBuffer);
369 Status = Tpm2SubmitCommand (SendBufferSize, (UINT8 *)&SendBuffer, &RecvBufferSize, (UINT8 *)&RecvBuffer);
370 if (EFI_ERROR (Status)) {
371 goto Done;
372 }
373
374 if (RecvBufferSize < sizeof (TPM2_RESPONSE_HEADER)) {
375 DEBUG ((DEBUG_ERROR, "Tpm2NvDefineSpace - RecvBufferSize Error - %x\n", RecvBufferSize));
376 Status = EFI_DEVICE_ERROR;
377 goto Done;
378 }
379
380 ResponseCode = SwapBytes32 (RecvBuffer.Header.responseCode);
381 if (ResponseCode != TPM_RC_SUCCESS) {
382 DEBUG ((DEBUG_ERROR, "Tpm2NvDefineSpace - responseCode - %x\n", SwapBytes32 (RecvBuffer.Header.responseCode)));
383 }
384
385 switch (ResponseCode) {
386 case TPM_RC_SUCCESS:
387 // return data
388 break;
389 case TPM_RC_SIZE + RC_NV_DefineSpace_publicInfo:
390 case TPM_RC_SIZE + RC_NV_DefineSpace_auth:
391 Status = EFI_BAD_BUFFER_SIZE;
392 break;
393 case TPM_RC_ATTRIBUTES:
394 case TPM_RC_ATTRIBUTES + RC_NV_DefineSpace_publicInfo:
395 Status = EFI_UNSUPPORTED;
396 break;
397 case TPM_RC_ATTRIBUTES + RC_NV_DefineSpace_authHandle:
398 Status = EFI_INVALID_PARAMETER;
399 break;
400 case TPM_RC_NV_DEFINED:
401 Status = EFI_ALREADY_STARTED;
402 break;
403 case TPM_RC_VALUE + RC_NV_DefineSpace_publicInfo:
404 case TPM_RC_VALUE + RC_NV_DefineSpace_authHandle:
405 Status = EFI_INVALID_PARAMETER;
406 break;
407 case TPM_RC_NV_SPACE:
408 Status = EFI_OUT_OF_RESOURCES;
409 break;
410 default:
411 Status = EFI_DEVICE_ERROR;
412 break;
413 }
414
415Done:
416 //
417 // Clear AuthSession Content
418 //
419 ZeroMem (&SendBuffer, sizeof (SendBuffer));
420 ZeroMem (&RecvBuffer, sizeof (RecvBuffer));
421 return Status;
422}
423
436EFIAPI
438 IN TPMI_RH_PROVISION AuthHandle,
439 IN TPMI_RH_NV_INDEX NvIndex,
440 IN TPMS_AUTH_COMMAND *AuthSession OPTIONAL
441 )
442{
443 EFI_STATUS Status;
446 UINT32 SendBufferSize;
447 UINT32 RecvBufferSize;
448 UINT8 *Buffer;
449 UINT32 SessionInfoSize;
450 TPM_RC ResponseCode;
451
452 //
453 // Construct command
454 //
455 SendBuffer.Header.tag = SwapBytes16 (TPM_ST_SESSIONS);
456 SendBuffer.Header.commandCode = SwapBytes32 (TPM_CC_NV_UndefineSpace);
457
458 SendBuffer.AuthHandle = SwapBytes32 (AuthHandle);
459 SendBuffer.NvIndex = SwapBytes32 (NvIndex);
460
461 //
462 // Add in Auth session
463 //
464 Buffer = (UINT8 *)&SendBuffer.AuthSession;
465
466 // sessionInfoSize
467 SessionInfoSize = CopyAuthSessionCommand (AuthSession, Buffer);
468 Buffer += SessionInfoSize;
469 SendBuffer.AuthSessionSize = SwapBytes32 (SessionInfoSize);
470
471 SendBufferSize = (UINT32)(Buffer - (UINT8 *)&SendBuffer);
472 SendBuffer.Header.paramSize = SwapBytes32 (SendBufferSize);
473
474 //
475 // send Tpm command
476 //
477 RecvBufferSize = sizeof (RecvBuffer);
478 Status = Tpm2SubmitCommand (SendBufferSize, (UINT8 *)&SendBuffer, &RecvBufferSize, (UINT8 *)&RecvBuffer);
479 if (EFI_ERROR (Status)) {
480 goto Done;
481 }
482
483 if (RecvBufferSize < sizeof (TPM2_RESPONSE_HEADER)) {
484 DEBUG ((DEBUG_ERROR, "Tpm2NvUndefineSpace - RecvBufferSize Error - %x\n", RecvBufferSize));
485 Status = EFI_DEVICE_ERROR;
486 goto Done;
487 }
488
489 ResponseCode = SwapBytes32 (RecvBuffer.Header.responseCode);
490 if (ResponseCode != TPM_RC_SUCCESS) {
491 DEBUG ((DEBUG_ERROR, "Tpm2NvUndefineSpace - responseCode - %x\n", SwapBytes32 (RecvBuffer.Header.responseCode)));
492 }
493
494 switch (ResponseCode) {
495 case TPM_RC_SUCCESS:
496 // return data
497 break;
498 case TPM_RC_ATTRIBUTES:
499 case TPM_RC_ATTRIBUTES + RC_NV_UndefineSpace_nvIndex:
500 Status = EFI_UNSUPPORTED;
501 break;
502 case TPM_RC_NV_AUTHORIZATION:
503 Status = EFI_SECURITY_VIOLATION;
504 break;
505 case TPM_RC_HANDLE + RC_NV_UndefineSpace_nvIndex: // TPM_RC_NV_DEFINED:
506 Status = EFI_NOT_FOUND;
507 break;
508 case TPM_RC_HANDLE + RC_NV_UndefineSpace_authHandle: // TPM_RC_NV_DEFINED:
509 Status = EFI_INVALID_PARAMETER;
510 break;
511 case TPM_RC_VALUE + RC_NV_UndefineSpace_authHandle:
512 case TPM_RC_VALUE + RC_NV_UndefineSpace_nvIndex:
513 Status = EFI_INVALID_PARAMETER;
514 break;
515 default:
516 Status = EFI_DEVICE_ERROR;
517 break;
518 }
519
520Done:
521 //
522 // Clear AuthSession Content
523 //
524 ZeroMem (&SendBuffer, sizeof (SendBuffer));
525 ZeroMem (&RecvBuffer, sizeof (RecvBuffer));
526 return Status;
527}
528
544EFIAPI
546 IN TPMI_RH_NV_AUTH AuthHandle,
547 IN TPMI_RH_NV_INDEX NvIndex,
548 IN TPMS_AUTH_COMMAND *AuthSession OPTIONAL,
549 IN UINT16 Size,
550 IN UINT16 Offset,
551 IN OUT TPM2B_MAX_BUFFER *OutData
552 )
553{
554 EFI_STATUS Status;
555 TPM2_NV_READ_COMMAND SendBuffer;
556 TPM2_NV_READ_RESPONSE RecvBuffer;
557 UINT32 SendBufferSize;
558 UINT32 RecvBufferSize;
559 UINT8 *Buffer;
560 UINT32 SessionInfoSize;
561 TPM_RC ResponseCode;
562
563 //
564 // Construct command
565 //
566 SendBuffer.Header.tag = SwapBytes16 (TPM_ST_SESSIONS);
567 SendBuffer.Header.commandCode = SwapBytes32 (TPM_CC_NV_Read);
568
569 SendBuffer.AuthHandle = SwapBytes32 (AuthHandle);
570 SendBuffer.NvIndex = SwapBytes32 (NvIndex);
571
572 //
573 // Add in Auth session
574 //
575 Buffer = (UINT8 *)&SendBuffer.AuthSession;
576
577 // sessionInfoSize
578 SessionInfoSize = CopyAuthSessionCommand (AuthSession, Buffer);
579 Buffer += SessionInfoSize;
580 SendBuffer.AuthSessionSize = SwapBytes32 (SessionInfoSize);
581
582 WriteUnaligned16 ((UINT16 *)Buffer, SwapBytes16 (Size));
583 Buffer += sizeof (UINT16);
584 WriteUnaligned16 ((UINT16 *)Buffer, SwapBytes16 (Offset));
585 Buffer += sizeof (UINT16);
586
587 SendBufferSize = (UINT32)(Buffer - (UINT8 *)&SendBuffer);
588 SendBuffer.Header.paramSize = SwapBytes32 (SendBufferSize);
589
590 //
591 // send Tpm command
592 //
593 RecvBufferSize = sizeof (RecvBuffer);
594 Status = Tpm2SubmitCommand (SendBufferSize, (UINT8 *)&SendBuffer, &RecvBufferSize, (UINT8 *)&RecvBuffer);
595 if (EFI_ERROR (Status)) {
596 goto Done;
597 }
598
599 if (RecvBufferSize < sizeof (TPM2_RESPONSE_HEADER)) {
600 DEBUG ((DEBUG_ERROR, "Tpm2NvRead - RecvBufferSize Error - %x\n", RecvBufferSize));
601 Status = EFI_DEVICE_ERROR;
602 goto Done;
603 }
604
605 ResponseCode = SwapBytes32 (RecvBuffer.Header.responseCode);
606 if (ResponseCode != TPM_RC_SUCCESS) {
607 DEBUG ((DEBUG_ERROR, "Tpm2NvRead - responseCode - %x\n", ResponseCode));
608 }
609
610 switch (ResponseCode) {
611 case TPM_RC_SUCCESS:
612 // return data
613 break;
614 case TPM_RC_NV_AUTHORIZATION:
615 Status = EFI_SECURITY_VIOLATION;
616 break;
617 case TPM_RC_NV_LOCKED:
618 Status = EFI_ACCESS_DENIED;
619 break;
620 case TPM_RC_NV_RANGE:
621 Status = EFI_BAD_BUFFER_SIZE;
622 break;
623 case TPM_RC_NV_UNINITIALIZED:
624 Status = EFI_NOT_READY;
625 break;
626 case TPM_RC_HANDLE + RC_NV_Read_nvIndex: // TPM_RC_NV_DEFINED:
627 Status = EFI_NOT_FOUND;
628 break;
629 case TPM_RC_HANDLE + RC_NV_Read_authHandle: // TPM_RC_NV_DEFINED:
630 Status = EFI_INVALID_PARAMETER;
631 break;
632 case TPM_RC_VALUE + RC_NV_Read_nvIndex:
633 case TPM_RC_VALUE + RC_NV_Read_authHandle:
634 Status = EFI_INVALID_PARAMETER;
635 break;
636 case TPM_RC_BAD_AUTH + RC_NV_Read_authHandle + TPM_RC_S:
637 Status = EFI_INVALID_PARAMETER;
638 break;
639 case TPM_RC_AUTH_UNAVAILABLE:
640 Status = EFI_INVALID_PARAMETER;
641 break;
642 case TPM_RC_AUTH_FAIL + RC_NV_Read_authHandle + TPM_RC_S:
643 Status = EFI_INVALID_PARAMETER;
644 break;
645 case TPM_RC_ATTRIBUTES + RC_NV_Read_authHandle + TPM_RC_S:
646 Status = EFI_UNSUPPORTED;
647 break;
648 default:
649 Status = EFI_DEVICE_ERROR;
650 break;
651 }
652
653 if (Status != EFI_SUCCESS) {
654 goto Done;
655 }
656
657 //
658 // Return the response
659 //
660 OutData->size = SwapBytes16 (RecvBuffer.Data.size);
661 if (OutData->size > MAX_DIGEST_BUFFER) {
662 DEBUG ((DEBUG_ERROR, "Tpm2NvRead - OutData->size error %x\n", OutData->size));
663 Status = EFI_DEVICE_ERROR;
664 goto Done;
665 }
666
667 CopyMem (OutData->buffer, &RecvBuffer.Data.buffer, OutData->size);
668
669Done:
670 //
671 // Clear AuthSession Content
672 //
673 ZeroMem (&SendBuffer, sizeof (SendBuffer));
674 ZeroMem (&RecvBuffer, sizeof (RecvBuffer));
675 return Status;
676}
677
692EFIAPI
694 IN TPMI_RH_NV_AUTH AuthHandle,
695 IN TPMI_RH_NV_INDEX NvIndex,
696 IN TPMS_AUTH_COMMAND *AuthSession OPTIONAL,
697 IN TPM2B_MAX_BUFFER *InData,
698 IN UINT16 Offset
699 )
700{
701 EFI_STATUS Status;
702 TPM2_NV_WRITE_COMMAND SendBuffer;
703 TPM2_NV_WRITE_RESPONSE RecvBuffer;
704 UINT32 SendBufferSize;
705 UINT32 RecvBufferSize;
706 UINT8 *Buffer;
707 UINT32 SessionInfoSize;
708 TPM_RC ResponseCode;
709
710 //
711 // Construct command
712 //
713 SendBuffer.Header.tag = SwapBytes16 (TPM_ST_SESSIONS);
714 SendBuffer.Header.commandCode = SwapBytes32 (TPM_CC_NV_Write);
715
716 SendBuffer.AuthHandle = SwapBytes32 (AuthHandle);
717 SendBuffer.NvIndex = SwapBytes32 (NvIndex);
718
719 //
720 // Add in Auth session
721 //
722 Buffer = (UINT8 *)&SendBuffer.AuthSession;
723
724 // sessionInfoSize
725 SessionInfoSize = CopyAuthSessionCommand (AuthSession, Buffer);
726 Buffer += SessionInfoSize;
727 SendBuffer.AuthSessionSize = SwapBytes32 (SessionInfoSize);
728
729 WriteUnaligned16 ((UINT16 *)Buffer, SwapBytes16 (InData->size));
730 Buffer += sizeof (UINT16);
731 CopyMem (Buffer, InData->buffer, InData->size);
732 Buffer += InData->size;
733 WriteUnaligned16 ((UINT16 *)Buffer, SwapBytes16 (Offset));
734 Buffer += sizeof (UINT16);
735
736 SendBufferSize = (UINT32)(Buffer - (UINT8 *)&SendBuffer);
737 SendBuffer.Header.paramSize = SwapBytes32 (SendBufferSize);
738
739 //
740 // send Tpm command
741 //
742 RecvBufferSize = sizeof (RecvBuffer);
743 Status = Tpm2SubmitCommand (SendBufferSize, (UINT8 *)&SendBuffer, &RecvBufferSize, (UINT8 *)&RecvBuffer);
744 if (EFI_ERROR (Status)) {
745 goto Done;
746 }
747
748 if (RecvBufferSize < sizeof (TPM2_RESPONSE_HEADER)) {
749 DEBUG ((DEBUG_ERROR, "Tpm2NvWrite - RecvBufferSize Error - %x\n", RecvBufferSize));
750 Status = EFI_DEVICE_ERROR;
751 goto Done;
752 }
753
754 ResponseCode = SwapBytes32 (RecvBuffer.Header.responseCode);
755 if (ResponseCode != TPM_RC_SUCCESS) {
756 DEBUG ((DEBUG_ERROR, "Tpm2NvWrite - responseCode - %x\n", ResponseCode));
757 }
758
759 switch (ResponseCode) {
760 case TPM_RC_SUCCESS:
761 // return data
762 break;
763 case TPM_RC_ATTRIBUTES:
764 Status = EFI_UNSUPPORTED;
765 break;
766 case TPM_RC_NV_AUTHORIZATION:
767 Status = EFI_SECURITY_VIOLATION;
768 break;
769 case TPM_RC_NV_LOCKED:
770 Status = EFI_ACCESS_DENIED;
771 break;
772 case TPM_RC_NV_RANGE:
773 Status = EFI_BAD_BUFFER_SIZE;
774 break;
775 case TPM_RC_HANDLE + RC_NV_Write_nvIndex: // TPM_RC_NV_DEFINED:
776 Status = EFI_NOT_FOUND;
777 break;
778 case TPM_RC_HANDLE + RC_NV_Write_authHandle: // TPM_RC_NV_DEFINED:
779 Status = EFI_INVALID_PARAMETER;
780 break;
781 case TPM_RC_VALUE + RC_NV_Write_nvIndex:
782 case TPM_RC_VALUE + RC_NV_Write_authHandle:
783 Status = EFI_INVALID_PARAMETER;
784 break;
785 case TPM_RC_BAD_AUTH + RC_NV_Write_authHandle + TPM_RC_S:
786 Status = EFI_INVALID_PARAMETER;
787 break;
788 case TPM_RC_AUTH_UNAVAILABLE:
789 Status = EFI_INVALID_PARAMETER;
790 break;
791 case TPM_RC_AUTH_FAIL + RC_NV_Write_authHandle + TPM_RC_S:
792 Status = EFI_INVALID_PARAMETER;
793 break;
794 case TPM_RC_ATTRIBUTES + RC_NV_Write_authHandle + TPM_RC_S:
795 Status = EFI_UNSUPPORTED;
796 break;
797 default:
798 Status = EFI_DEVICE_ERROR;
799 break;
800 }
801
802Done:
803 //
804 // Clear AuthSession Content
805 //
806 ZeroMem (&SendBuffer, sizeof (SendBuffer));
807 ZeroMem (&RecvBuffer, sizeof (RecvBuffer));
808 return Status;
809}
810
823EFIAPI
825 IN TPMI_RH_NV_AUTH AuthHandle,
826 IN TPMI_RH_NV_INDEX NvIndex,
827 IN TPMS_AUTH_COMMAND *AuthSession OPTIONAL
828 )
829{
830 EFI_STATUS Status;
831 TPM2_NV_READLOCK_COMMAND SendBuffer;
832 TPM2_NV_READLOCK_RESPONSE RecvBuffer;
833 UINT32 SendBufferSize;
834 UINT32 RecvBufferSize;
835 UINT8 *Buffer;
836 UINT32 SessionInfoSize;
837 TPM_RC ResponseCode;
838
839 //
840 // Construct command
841 //
842 SendBuffer.Header.tag = SwapBytes16 (TPM_ST_SESSIONS);
843 SendBuffer.Header.commandCode = SwapBytes32 (TPM_CC_NV_ReadLock);
844
845 SendBuffer.AuthHandle = SwapBytes32 (AuthHandle);
846 SendBuffer.NvIndex = SwapBytes32 (NvIndex);
847
848 //
849 // Add in Auth session
850 //
851 Buffer = (UINT8 *)&SendBuffer.AuthSession;
852
853 // sessionInfoSize
854 SessionInfoSize = CopyAuthSessionCommand (AuthSession, Buffer);
855 Buffer += SessionInfoSize;
856 SendBuffer.AuthSessionSize = SwapBytes32 (SessionInfoSize);
857
858 SendBufferSize = (UINT32)(Buffer - (UINT8 *)&SendBuffer);
859 SendBuffer.Header.paramSize = SwapBytes32 (SendBufferSize);
860
861 //
862 // send Tpm command
863 //
864 RecvBufferSize = sizeof (RecvBuffer);
865 Status = Tpm2SubmitCommand (SendBufferSize, (UINT8 *)&SendBuffer, &RecvBufferSize, (UINT8 *)&RecvBuffer);
866 if (EFI_ERROR (Status)) {
867 goto Done;
868 }
869
870 if (RecvBufferSize < sizeof (TPM2_RESPONSE_HEADER)) {
871 DEBUG ((DEBUG_ERROR, "Tpm2NvReadLock - RecvBufferSize Error - %x\n", RecvBufferSize));
872 Status = EFI_DEVICE_ERROR;
873 goto Done;
874 }
875
876 ResponseCode = SwapBytes32 (RecvBuffer.Header.responseCode);
877 if (ResponseCode != TPM_RC_SUCCESS) {
878 DEBUG ((DEBUG_ERROR, "Tpm2NvReadLock - responseCode - %x\n", SwapBytes32 (RecvBuffer.Header.responseCode)));
879 }
880
881 switch (ResponseCode) {
882 case TPM_RC_SUCCESS:
883 // return data
884 break;
885 default:
886 Status = EFI_DEVICE_ERROR;
887 break;
888 }
889
890Done:
891 //
892 // Clear AuthSession Content
893 //
894 ZeroMem (&SendBuffer, sizeof (SendBuffer));
895 ZeroMem (&RecvBuffer, sizeof (RecvBuffer));
896 return Status;
897}
898
911EFIAPI
913 IN TPMI_RH_NV_AUTH AuthHandle,
914 IN TPMI_RH_NV_INDEX NvIndex,
915 IN TPMS_AUTH_COMMAND *AuthSession OPTIONAL
916 )
917{
918 EFI_STATUS Status;
919 TPM2_NV_WRITELOCK_COMMAND SendBuffer;
921 UINT32 SendBufferSize;
922 UINT32 RecvBufferSize;
923 UINT8 *Buffer;
924 UINT32 SessionInfoSize;
925 TPM_RC ResponseCode;
926
927 //
928 // Construct command
929 //
930 SendBuffer.Header.tag = SwapBytes16 (TPM_ST_SESSIONS);
931 SendBuffer.Header.commandCode = SwapBytes32 (TPM_CC_NV_WriteLock);
932
933 SendBuffer.AuthHandle = SwapBytes32 (AuthHandle);
934 SendBuffer.NvIndex = SwapBytes32 (NvIndex);
935
936 //
937 // Add in Auth session
938 //
939 Buffer = (UINT8 *)&SendBuffer.AuthSession;
940
941 // sessionInfoSize
942 SessionInfoSize = CopyAuthSessionCommand (AuthSession, Buffer);
943 Buffer += SessionInfoSize;
944 SendBuffer.AuthSessionSize = SwapBytes32 (SessionInfoSize);
945
946 SendBufferSize = (UINT32)(Buffer - (UINT8 *)&SendBuffer);
947 SendBuffer.Header.paramSize = SwapBytes32 (SendBufferSize);
948
949 //
950 // send Tpm command
951 //
952 RecvBufferSize = sizeof (RecvBuffer);
953 Status = Tpm2SubmitCommand (SendBufferSize, (UINT8 *)&SendBuffer, &RecvBufferSize, (UINT8 *)&RecvBuffer);
954 if (EFI_ERROR (Status)) {
955 goto Done;
956 }
957
958 if (RecvBufferSize < sizeof (TPM2_RESPONSE_HEADER)) {
959 DEBUG ((DEBUG_ERROR, "Tpm2NvWriteLock - RecvBufferSize Error - %x\n", RecvBufferSize));
960 Status = EFI_DEVICE_ERROR;
961 goto Done;
962 }
963
964 ResponseCode = SwapBytes32 (RecvBuffer.Header.responseCode);
965 if (ResponseCode != TPM_RC_SUCCESS) {
966 DEBUG ((DEBUG_ERROR, "Tpm2NvWriteLock - responseCode - %x\n", SwapBytes32 (RecvBuffer.Header.responseCode)));
967 }
968
969 switch (ResponseCode) {
970 case TPM_RC_SUCCESS:
971 // return data
972 break;
973 default:
974 Status = EFI_DEVICE_ERROR;
975 break;
976 }
977
978Done:
979 //
980 // Clear AuthSession Content
981 //
982 ZeroMem (&SendBuffer, sizeof (SendBuffer));
983 ZeroMem (&RecvBuffer, sizeof (RecvBuffer));
984 return Status;
985}
986
998EFIAPI
1000 IN TPMI_RH_PROVISION AuthHandle,
1001 IN TPMS_AUTH_COMMAND *AuthSession OPTIONAL
1002 )
1003{
1004 EFI_STATUS Status;
1007 UINT32 SendBufferSize;
1008 UINT32 RecvBufferSize;
1009 UINT8 *Buffer;
1010 UINT32 SessionInfoSize;
1011 TPM_RC ResponseCode;
1012
1013 //
1014 // Construct command
1015 //
1016 SendBuffer.Header.tag = SwapBytes16 (TPM_ST_SESSIONS);
1017 SendBuffer.Header.commandCode = SwapBytes32 (TPM_CC_NV_GlobalWriteLock);
1018
1019 SendBuffer.AuthHandle = SwapBytes32 (AuthHandle);
1020
1021 //
1022 // Add in Auth session
1023 //
1024 Buffer = (UINT8 *)&SendBuffer.AuthSession;
1025
1026 // sessionInfoSize
1027 SessionInfoSize = CopyAuthSessionCommand (AuthSession, Buffer);
1028 Buffer += SessionInfoSize;
1029 SendBuffer.AuthSessionSize = SwapBytes32 (SessionInfoSize);
1030
1031 SendBufferSize = (UINT32)(Buffer - (UINT8 *)&SendBuffer);
1032 SendBuffer.Header.paramSize = SwapBytes32 (SendBufferSize);
1033
1034 //
1035 // send Tpm command
1036 //
1037 RecvBufferSize = sizeof (RecvBuffer);
1038 Status = Tpm2SubmitCommand (SendBufferSize, (UINT8 *)&SendBuffer, &RecvBufferSize, (UINT8 *)&RecvBuffer);
1039 if (EFI_ERROR (Status)) {
1040 goto Done;
1041 }
1042
1043 if (RecvBufferSize < sizeof (TPM2_RESPONSE_HEADER)) {
1044 DEBUG ((DEBUG_ERROR, "Tpm2NvGlobalWriteLock - RecvBufferSize Error - %x\n", RecvBufferSize));
1045 Status = EFI_DEVICE_ERROR;
1046 goto Done;
1047 }
1048
1049 ResponseCode = SwapBytes32 (RecvBuffer.Header.responseCode);
1050 if (ResponseCode != TPM_RC_SUCCESS) {
1051 DEBUG ((DEBUG_ERROR, "Tpm2NvGlobalWriteLock - responseCode - %x\n", SwapBytes32 (RecvBuffer.Header.responseCode)));
1052 }
1053
1054 switch (ResponseCode) {
1055 case TPM_RC_SUCCESS:
1056 // return data
1057 break;
1058 default:
1059 Status = EFI_DEVICE_ERROR;
1060 break;
1061 }
1062
1063Done:
1064 //
1065 // Clear AuthSession Content
1066 //
1067 ZeroMem (&SendBuffer, sizeof (SendBuffer));
1068 ZeroMem (&RecvBuffer, sizeof (RecvBuffer));
1069 return Status;
1070}
1071
1085EFIAPI
1087 IN TPMI_RH_NV_AUTH AuthHandle,
1088 IN TPMI_RH_NV_INDEX NvIndex,
1089 IN TPMS_AUTH_COMMAND *AuthSession OPTIONAL,
1090 IN TPM2B_MAX_BUFFER *InData
1091 )
1092{
1093 EFI_STATUS Status;
1094 TPM2_NV_EXTEND_COMMAND SendBuffer;
1095 TPM2_NV_EXTEND_RESPONSE RecvBuffer;
1096 UINT32 SendBufferSize;
1097 UINT32 RecvBufferSize;
1098 UINT8 *Buffer;
1099 UINT32 SessionInfoSize;
1100 TPM_RC ResponseCode;
1101
1102 //
1103 // Construct command
1104 //
1105 SendBuffer.Header.tag = SwapBytes16 (TPM_ST_SESSIONS);
1106 SendBuffer.Header.commandCode = SwapBytes32 (TPM_CC_NV_Extend);
1107
1108 SendBuffer.AuthHandle = SwapBytes32 (AuthHandle);
1109 SendBuffer.NvIndex = SwapBytes32 (NvIndex);
1110
1111 //
1112 // Add in Auth session
1113 //
1114 Buffer = (UINT8 *)&SendBuffer.AuthSession;
1115
1116 // sessionInfoSize
1117 SessionInfoSize = CopyAuthSessionCommand (AuthSession, Buffer);
1118 Buffer += SessionInfoSize;
1119 SendBuffer.AuthSessionSize = SwapBytes32 (SessionInfoSize);
1120
1121 WriteUnaligned16 ((UINT16 *)Buffer, SwapBytes16 (InData->size));
1122 Buffer += sizeof (UINT16);
1123 CopyMem (Buffer, InData->buffer, InData->size);
1124 Buffer += InData->size;
1125
1126 SendBufferSize = (UINT32)(Buffer - (UINT8 *)&SendBuffer);
1127 SendBuffer.Header.paramSize = SwapBytes32 (SendBufferSize);
1128
1129 //
1130 // send Tpm command
1131 //
1132 RecvBufferSize = sizeof (RecvBuffer);
1133 Status = Tpm2SubmitCommand (SendBufferSize, (UINT8 *)&SendBuffer, &RecvBufferSize, (UINT8 *)&RecvBuffer);
1134 if (EFI_ERROR (Status)) {
1135 goto Done;
1136 }
1137
1138 if (RecvBufferSize < sizeof (TPM2_RESPONSE_HEADER)) {
1139 DEBUG ((DEBUG_ERROR, "Tpm2NvExtend - RecvBufferSize Error - %x\n", RecvBufferSize));
1140 Status = EFI_DEVICE_ERROR;
1141 goto Done;
1142 }
1143
1144 ResponseCode = SwapBytes32 (RecvBuffer.Header.responseCode);
1145 if (ResponseCode != TPM_RC_SUCCESS) {
1146 DEBUG ((DEBUG_ERROR, "Tpm2NvExtend - responseCode - %x\n", ResponseCode));
1147 }
1148
1149 switch (ResponseCode) {
1150 case TPM_RC_SUCCESS:
1151 // return data
1152 break;
1153 case TPM_RC_ATTRIBUTES:
1154 Status = EFI_UNSUPPORTED;
1155 break;
1156 case TPM_RC_NV_AUTHORIZATION:
1157 Status = EFI_SECURITY_VIOLATION;
1158 break;
1159 case TPM_RC_NV_LOCKED:
1160 Status = EFI_ACCESS_DENIED;
1161 break;
1162 default:
1163 Status = EFI_DEVICE_ERROR;
1164 break;
1165 }
1166
1167Done:
1168 //
1169 // Clear AuthSession Content
1170 //
1171 ZeroMem (&SendBuffer, sizeof (SendBuffer));
1172 ZeroMem (&RecvBuffer, sizeof (RecvBuffer));
1173 return Status;
1174}
UINT16 EFIAPI SwapBytes16(IN UINT16 Value)
Definition: SwapBytes16.c:25
UINT16 EFIAPI ReadUnaligned16(IN CONST UINT16 *Buffer)
Definition: Unaligned.c:29
UINT32 EFIAPI SwapBytes32(IN UINT32 Value)
Definition: SwapBytes32.c:25
UINT32 EFIAPI WriteUnaligned32(OUT UINT32 *Buffer, IN UINT32 Value)
Definition: Unaligned.c:177
UINT16 EFIAPI WriteUnaligned16(OUT UINT16 *Buffer, IN UINT16 Value)
Definition: Unaligned.c:61
UINT32 EFIAPI ReadUnaligned32(IN CONST UINT32 *Buffer)
Definition: Unaligned.c:145
VOID *EFIAPI CopyMem(OUT VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)
VOID *EFIAPI ZeroMem(OUT VOID *Buffer, IN UINTN Length)
#define IN
Definition: Base.h:279
#define OUT
Definition: Base.h:284
#define DEBUG(Expression)
Definition: DebugLib.h:434
UINT32 EFIAPI CopyAuthSessionCommand(IN TPMS_AUTH_COMMAND *AuthSessionIn OPTIONAL, OUT UINT8 *AuthSessionOut)
Definition: Tpm2Help.c:88
EFI_STATUS EFIAPI Tpm2SubmitCommand(IN UINT32 InputParameterBlockSize, IN UINT8 *InputParameterBlock, IN OUT UINT32 *OutputParameterBlockSize, IN UINT8 *OutputParameterBlock)
EFI_STATUS EFIAPI Tpm2NvDefineSpace(IN TPMI_RH_PROVISION AuthHandle, IN TPMS_AUTH_COMMAND *AuthSession OPTIONAL, IN TPM2B_AUTH *Auth, IN TPM2B_NV_PUBLIC *NvPublic)
EFI_STATUS EFIAPI Tpm2NvGlobalWriteLock(IN TPMI_RH_PROVISION AuthHandle, IN TPMS_AUTH_COMMAND *AuthSession OPTIONAL)
EFI_STATUS EFIAPI Tpm2NvWriteLock(IN TPMI_RH_NV_AUTH AuthHandle, IN TPMI_RH_NV_INDEX NvIndex, IN TPMS_AUTH_COMMAND *AuthSession OPTIONAL)
EFI_STATUS EFIAPI Tpm2NvRead(IN TPMI_RH_NV_AUTH AuthHandle, IN TPMI_RH_NV_INDEX NvIndex, IN TPMS_AUTH_COMMAND *AuthSession OPTIONAL, IN UINT16 Size, IN UINT16 Offset, IN OUT TPM2B_MAX_BUFFER *OutData)
EFI_STATUS EFIAPI Tpm2NvExtend(IN TPMI_RH_NV_AUTH AuthHandle, IN TPMI_RH_NV_INDEX NvIndex, IN TPMS_AUTH_COMMAND *AuthSession OPTIONAL, IN TPM2B_MAX_BUFFER *InData)
EFI_STATUS EFIAPI Tpm2NvReadLock(IN TPMI_RH_NV_AUTH AuthHandle, IN TPMI_RH_NV_INDEX NvIndex, IN TPMS_AUTH_COMMAND *AuthSession OPTIONAL)
EFI_STATUS EFIAPI Tpm2NvUndefineSpace(IN TPMI_RH_PROVISION AuthHandle, IN TPMI_RH_NV_INDEX NvIndex, IN TPMS_AUTH_COMMAND *AuthSession OPTIONAL)
EFI_STATUS EFIAPI Tpm2NvReadPublic(IN TPMI_RH_NV_INDEX NvIndex, OUT TPM2B_NV_PUBLIC *NvPublic, OUT TPM2B_NAME *NvName)
EFI_STATUS EFIAPI Tpm2NvWrite(IN TPMI_RH_NV_AUTH AuthHandle, IN TPMI_RH_NV_INDEX NvIndex, IN TPMS_AUTH_COMMAND *AuthSession OPTIONAL, IN TPM2B_MAX_BUFFER *InData, IN UINT16 Offset)
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29
#define EFI_SUCCESS
Definition: UefiBaseType.h:112