# TcgPlatformSetupPeiPolicy - Analysis

## Module Overview

| Property | Value |
|----------|-------|
| Module   | TcgPlatformSetupPeiPolicy.efi |
| SHA256   | 64cbe3c32de56eb053c18852403ae222b05e6d5ef7bca75b61b199ab1a0de990 |
| Arch     | 32-bit IA32 |
| Base     | 0xffe17774 |
| Size     | 0x83a0 (33696 bytes) |
| IDA Port | 13376 |

## Source File

Original source path: `e:\hs\AmiModulePkg\TCG2\Common\TcgPlatformSetupPeiPolicy\TcgPlatformSetupPeiPolicy.c`

This PEIM installs a TCG Platform Setup Policy PPI to configure TPM-related setup policy during PEI phase.

## Function Table

| # | Address | Name | Size | Calls | Callers | BBs |
|---|---------|------|------|-------|---------|-----|
| 1 | 0xffe179d4 | CompareMem | 29 | 0 | 1 | 1 |
| 2 | 0xffe179f4 | SetMem | 21 | 0 | 0 | 1 |
| 3 | 0xffe17a14 | CopyMemBackwards | 31 | 1 | 0 | 3 |
| 4 | 0xffe17a34 | SetMem32 | 21 | 0 | 0 | 1 |
| 5 | 0xffe17a54 | CopyMem | 63 | 1 | 0 | 5 |
| 6 | 0xffe17ab4 | TcgPlatformSetupPolicyEntry | 697 | 4 | 0 | 20 |
| 7 | 0xffe17d6d | CompareMemWithDebug | 152 | 3 | 1 | 13 |
| 8 | 0xffe17e05 | GetDebugOutputInterface | 49 | 2 | 4 | 4 |
| 9 | 0xffe17e36 | DebugPrint | 42 | 3 | 1 | 4 |
| 10 | 0xffe17e60 | DebugOutputString | 30 | 2 | 2 | 3 |
| 11 | 0xffe17e7e | GetTpmState | 79 | 1 | 1 | 7 |
| 12 | 0xffe17ecd | GetPeiServicesTablePointer | 50 | 3 | 1 | 3 |
| 13 | 0xffe17eff | ReadIdtr | 35 | 2 | 1 | 3 |

## Function Details

### Category: Library Functions

- **CompareMem** (0xffe179d4): 29 bytes
- **SetMem** (0xffe179f4): 21 bytes
- **CopyMemBackwards** (0xffe17a14): 31 bytes
- **SetMem32** (0xffe17a34): 21 bytes
- **CopyMem** (0xffe17a54): 63 bytes
- **CompareMemWithDebug** (0xffe17d6d): 152 bytes

These are internal implementations of BaseMemoryLib (linked statically).

### Category: Debug/Assert Support

- **CompareMemWithDebug** (0xffe17d6d): 152 bytes
- **GetDebugOutputInterface** (0xffe17e05): 49 bytes
- **DebugPrint** (0xffe17e36): 42 bytes
- **DebugOutputString** (0xffe17e60): 30 bytes

These provide ASSERT and debug print support.

### Category: PEI Services

- **GetPeiServicesTablePointer** (0xffe17ecd): 50 bytes
- **ReadIdtr** (0xffe17eff): 35 bytes

GetPeiServicesTablePointer reads the IDTR to locate the PEI Services Table. ReadIdtr wraps `__sidt()`.

### Category: TPM State

- **GetTpmState** (0xffe17e7e): 79 bytes

GetTpmState reads CMOS register 0x4A via I/O ports 0x70/0x71 to determine TPM state. Returns:
- 0x0 (EFI_SUCCESS): TPM not present
- 0x1: EFI_DEVICE_ERROR (TPM disabled)
- -1: TPM present but N/A
- else: EFI_ACCESS_DENIED

### Category: Main Entry Point

- **TcgPlatformSetupPolicyEntry** (0xffe17ab4): 697 bytes (main entry point)

The entry point:
1. Locates the TCG Platform Setup Policy PPI via PEI Services
2. Allocates and fills in the policy buffer (GUID table at unk_FFE18304..unk_FFE18310)
3. Sets up bitfield flags for TPM policy (bits OR'd into v29+47)
4. Installs the PPI via `SystemTable->BootServices->InstallConfigurationTable` (offset +24)
5. Calls ASSERT_EFI_ERROR on failure at source line 213

## Call Graph Summary

```
TcgPlatformSetupPolicyEntry
  |-- GetDebugOutputInterface
  |     |-- GetPeiServicesTablePointer
  |     |     |-- ReadIdtr
  |-- DebugPrint
  |     |-- GetDebugOutputInterface
  |     |-- GetTpmState
  |-- DebugOutputString
  |     |-- GetDebugOutputInterface
  |-- CompareMemWithDebug
        |-- CompareMem
        |-- GetDebugOutputInterface
```

## Strings References

- `e:\hs\AmiModulePkg\TCG2\Common\TcgPlatformSetupPeiPolicy\TcgPlatformSetupPeiPolicy.c` (debug path)
- `!EFI_ERROR (Status)` (assert expression at line 213)
- `\nASSERT_EFI_ERROR (Status = %%r)\n` (debug print format)

## Notes

- All library functions (CompareMem, CopyMem, SetMem, etc.) are statically linked internal implementations
- PEI Services Table pointer is obtained via IDT-based method (`__sidt`)
- TPM state detection uses CMOS I/O ports (0x70/0x71) with index 0x4A
- The policy PPI contains 4 GUID entries (at +8 through +14) and a flags bitfield (at +47)
- `bad sp value` warning from decompiler due to stack frame manipulation (negative offsets)
