/**
* IScsiDxe.c - UEFI iSCSI Driver
*
* AMI iSCSI initiator driver for UEFI.
* Source layout from AmiNetworkPkg/IScsiDxe/
*
* Address range: 0x2C0 - 0x199B0 (282 functions)
* Entry point: 0x428 (_ModuleEntryPoint)
*
* Driver Architecture:
* The driver implements the UEFI Driver Binding Protocol to manage
* iSCSI connections over any available network controller. It also
* implements:
* - EFI_EXT_SCSI_PASS_THRU_PROTOCOL for SCSI command pass-through
* - EFI_HII_CONFIG_ACCESS_PROTOCOL for configuration via BIOS setup
* - EFI_COMPONENT_NAME2_PROTOCOL for human-readable names
* - EFI_ISCSI4_PROTOCOL (AMI private) for iSCSI session management
*
* ============================================================================
* SOURCE FILE MAPPING (by address range)
* ============================================================================
*
* File: IScsiDriver.c
* Range: 0x428 - 0x18F0 (6 functions)
* _ModuleEntryPoint 0x428 Driver entry point
* ISCSI_DRIVER_ENTRY_POIN 0x4A4 Init globals, locate HII protocols, seed RNG
* ISCSI_DRIVER_SEARCH_AIP 0x710 Search for AIP (Adapter Information) protocol
* ISCSI_DRIVER_BINDING 0xAF0 DriverBinding Supported/Start/Stop
* ISCSI_DRIVER_START 0x15C4 Start iSCSI on a controller
* ISCSI_DRIVER_STOP 0x18F0 Driver Binding Stop
*
* Key behaviors:
* - Entry point validates ImageHandle, SystemTable, BootServices, RuntimeServices
* - Seeds RNG via RDRAND instruction
* - Locates HII Database, HII Config Routing, HII Config Access protocols
* - DriverBinding: Supported checks for AIP + SNP, Start creates child handle,
* Stop destroys child and removes protocols
* - DriverBinding start calls ISCSI_DRIVER_START which allocates per-NIC
* context, installs iSCSI4 protocol, configures DHCP/TCP, registers HII
*
* File: IScsiMisc.c
* Range: 0x2008 - 0x4AC4 (21 functions)
* ISCSI_MAC_ADDR_TO_STR 0x2008 Convert MAC addr to string (XX:XX:XX:XX:XX:XX)
* ISCSI_IP_TO_STR 0x21CC Convert IP address (v4/v6) to string
* ISCSI_GUID_TO_STR 0x2318 Convert GUID to "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"
* ISCSI_STR_TO_GUID 0x2390 Parse string to GUID
* ISCSI_STR_TO_INT 0x2440 Convert string to integer (base 10/16 auto-detect)
* ISCSI_GET_VARIABLE_AND_SIZ 0x247C Read UEFI variable by name
* ISCSI_IPV6_SUPPORT 0x25D8 Check IPv6 support on NIC
* ISCSI_ADD_NIC 0x2768 Add NIC to attempt list
* ISCSI_REMOVE_NIC 0x2978 Remove NIC from attempt list
* ISCSI_SET_CONFIG_DATA 0x2B20 Set iSCSI config data via UEFI variable
* ISCSI_CONFIG_READ 0x2CC8 Read full config from NVRAM vars
* ISCSI_GET_VARIABLE 0x3AF8 Get UEFI variable with size discovery
* ISCSI_CREATE_EVENT 0x3BA4 Create timer event for retry
* ISCSI_DESTROY_CHILD 0x3C78 Destroy child handle
* ISCSI_ATTEMPT_ORDER_SELECT 0x3D28 Select attempt by order (checking MAC/IP match)
* ISCSI_ATTEMPT_ORDER_EXIST 0x3EF8 Check if attempt exists in order list
* ISCSI_GET_CONFIG_DATA 0x408C Get full config from NVRAM
* ISCSI_GET_CHILD_CONFIG 0x48E0 Configure child handle via DHCP
* ISCSI_CALLBACK_TIMER 0x4A94 Timer notification callback
* ISCSI_MATCH_PROTOCOL 0x4AC4 Match/compare protocol on handle
*
* Key behaviors:
* - Config reading from NVRAM reads "x-UEFI-ns" namespace and iSCSI attempt vars
* - NIC management via linked list of ISCSI_NIC_DATA entries
* - Attempt config is stored as UEFI variables named "Attempt %d"
* - Supports both DHCP and static IP configuration
*
* File: ComponentName.c
* Range: 0x4B9C - 0x4CEC (3 functions)
* ISCSI_COMPONENT_NAME_GET 0x4B9C Driver Name Get
* ISCSI_COMPONENT_NAME_CONTROLLE 0x4BD0 Controller Name Get (returns "<IPv4/IPv6> MAC(xx)")
* ISCSI_COMPONENT_NAME2 0x4CEC Driver Name2 (supports child handles)
*
* File: IScsiConfig.c
* Range: 0x4E48 - 0x97CC (25 functions)
* ISCSI_CONFIG_IP_TO_STR 0x4E48 Format IP address (v4: %d.%d.%d.%d, v6: %x:%x)
* ISCSI_CONFIG_MAC_TO_STR 0x4FF0 Format MAC address to hex string
* ISCSI_CONFIG_EXTRACT 0x5094 Extract config from config string
* ISCSI_CONFIG_GET_STRING 0x5188 Get config string from buffer
* ISCSI_CONFIG_GET_IP 0x5210 Get IP config from config string
* ISCSI_CONFIG_GET_LUN 0x539C Get LUN from config string
* ISCSI_CONFIG_GET_KEY_VALUE 0x53C0 Get key=value from config string
* ISCSI_CONFIG_SET_CONFIG 0x5470 Set iSCSI config from key=value pairs
* ISCSI_CONFIG_EXTRACT_CONFIG 0x56F4 Extract attempt config to string
* ISCSI_CONFIG_FORM_CALLBACK 0x5B84 HII form callback (question changes)
* ISCSI_CONFIG_ROUTE_STRING 0x6468 Route config string (Extract/Set)
* ISCSI_CONFIG_KEYWORD_EXTRACT 0x705C Extract keyword from handle
* ISCSI_CONFIG_KEYWORD_GET 0x714C Get keyword from handle
* ISCSI_CONFIG_KEYWORD_SET 0x724C Set keyword on handle
* ISCSI_CONFIG_EXTRACT_ATTEMPT 0x7478 Extract attempt to config string
* ISCSI_CONFIG_INIT_FROM_NVRAM 0x7794 Init config from NVRAM
* ISCSI_CONFIG_VALIDATE 0x7A30 Validate config values
* ISCSI_CONFIG_IFR_EXTRACT 0x7B98 Extract IFR data
* ISCSI_CONFIG_IFR_READ 0x7D38 Read IFR data
* ISCSI_CONFIG_IFR_WRITE 0x7EAC Write IFR data
* ISCSI_CONFIG_IFR_ROUTE 0x8198 Route IFR config
* ISCSI_CONFIG_IFR_DRIVER 0x8470 Config driver entry
* ISCSI_CONFIG_IFR_CALLBACK 0x8808 Config IFR callback
* ISCSI_CONFIG_REGISTER_HII 0x9684 Register HII packages
* ISCSI_CONFIG_UNREGISTER_HII 0x97CC Unregister HII
*
* Key behaviors:
* - Implements EFI_HII_CONFIG_ACCESS_PROTOCOL (Extract, Route, Callback)
* - Form callback handles all iSCSI setup menu interactions:
* enabling/disabling attempts, setting IP, CHAP, target info, etc.
* - IFR data stored as DISCSI_CONFIG_IFR_NVDATA UEFI variable
* - Supports keyword handler for external configuration tools
* - Validates all config values before saving to NVRAM
* - Registers HII package list with strings, IFR binary, and images
*
* File: IScsiProto.c
* Range: 0x9968 - 0xC9B0 (32 functions)
* ISCSI_PROTO_LOGIN 0x9968 Login to target (main entry)
* ISCSI_PROTO_LOGIN_EXEC 0x99AC Execute login sequence
* ISCSI_PROTO_DISCOVER 0x9A30 Discover targets
* ISCSI_PROTO_LOGIN_SESSION 0x9AC4 Login session (build login PDU)
* ISCSI_PROTO_NEGOTIATE 0x9CF8 Negotiate operational parameters
* ISCSI_PROTO_CONNECT 0x9F78 Establish TCP connection
* ISCSI_PROTO_POLL_TCP 0xA310 Poll TCP for completion
* ISCSI_PROTO_SEND_PDU 0xA3D0 Send iSCSI PDU
* ISCSI_PROTO_RECV_PDU 0xA4D4 Receive iSCSI PDU
* ISCSI_PROTO_PROCESS_LOGIN 0xA684 Process login response
* ISCSI_PROTO_PROCESS_RESP 0xA9DC Process login response parameters
* ISCSI_PROTO_SEND_OPCODE 0xABA8 Send opcode
* ISCSI_PROTO_SEND_TEXTPDU 0xABE8 Send text PDU
* ISCSI_PROTO_SESSION_CMD 0xAE48 Session command (send/receive)
* ISCSI_PROTO_SESSION_SETUP 0xB340 Setup session parameters
* ISCSI_PROTO_PARSE_PDU 0xB658 Parse PDU header
* ISCSI_PROTO_GET_PAGE_AND_OFFS 0xB72C Page address calculation
* ISCSI_PROTO_ADD_SG_LIST 0xB798 Add scatter-gather list entry
* ISCSI_PROTO_GET_DATA_XFER_SIZ 0xB7CC Calculate data transfer size
* ISCSI_PROTO_PROCESS_SCSI_CMD 0xB84C Process SCSI command PDU
* ISCSI_PROTO_CHECK_OPCODE 0xB934 Check PDU opcode
* ISCSI_PROTO_PROCESS_EXEC 0xB988 Process command execution
* ISCSI_PROTO_PROCESS_SCSI_IN 0xBC80 Process SCSI-In PDU
* ISCSI_PROTO_PROCESS_R2T 0xBE3C Process Ready-to-Transfer PDU
* ISCSI_PROTO_PROCESS_DATA_OUT 0xBF50 Send Data-Out PDU
* ISCSI_PROTO_PROCESS_DATA_IN 0xC108 Process Data-In PDU
* ISCSI_PROTO_PROCESS_STATUS 0xC284 Process status/response PDU
* ISCSI_PROTO_UPDATE_CMD 0xC4EC Update command state
* ISCSI_PROTO_EXEC_SCSI_CMD 0xC5C0 Execute SCSI command (full flow)
* ISCSI_PROTO_SET_SESSION_STATE 0xC8FC Set session state
* ISCSI_PROTO_RESET_SESSION 0xC9B0 Reset session
*
* Key behaviors:
* - Full iSCSI protocol initiator implementation (RFC 3720)
* - Supports both IPv4 and IPv6
* - Login sequence: discovery -> login -> parameter negotiation
* - Session management: state machine, error recovery
* - PDU I/O with scatter-gather support
* - Digest support (CRC32 for header and data)
* - CHAP authentication support
*
* File: IScsiIbft.c
* Range: 0xCC78 - 0xD3D8 (3 functions)
* ISCSI_IBFT_INSTALL 0xCC78 Install iBFT ACPI table
* ISCSI_IBFT_UNINSTALL 0xD154 Uninstall iBFT table
* ISCSI_IBFT_GET_ACPI 0xD3D8 Get ACPI iBFT table data
*
* Key behaviors:
* - iBFT (iSCSI Boot Firmware Table) is an ACPI table that communicates
* iSCSI boot parameters from firmware to OS
* - Installed during driver start if iSCSI boot is configured
* - Contains NIC MAC, initiator name, target name/IP/LUN, CHAP credentials
*
* File: IScsiExtScsiPassThru.c
* Range: 0xE310 - 0xF218 (10 functions)
* ISCSI_EXTPASS_PASS_THRU 0xE310 Ext SCSI Pass Thru (packet-based I/O)
* ISCSI_EXTPASS_GET_NEXT 0xE414 Get Next Target LUN
* ISCSI_EXTPASS_BUILD_DEVICE_PAT 0xE4F0 Build Device Path node
* ISCSI_EXTPASS_GET_DEVICE_PATH 0xE654 Get Device Path from node
* ISCSI_EXTPASS_INIT 0xE754 Init Ext SCSI Pass Thru
* ISCSI_EXTPASS_INSTALL 0xE7B4 Install Ext SCSI Pass Thru protocol
* ISCSI_EXTPASS_UNINSTALL 0xEAC8 Uninstall protocol
* ISCSI_EXTPASS_BUILD_DEVICE 0xEDAC Build device path node
* ISCSI_EXTPASS_MAP_LUN 0xEEB0 Map LUN to device (iBFT-based)
* ISCSI_EXTPASS_FREE_MAP 0xF218 Free LUN mapping
*
* Key behaviors:
* - Implements EFI_EXT_SCSI_PASS_THRU_PROTOCOL
* - Maps SCSI commands through the iSCSI session
* - LUN mapping reads iBFT table entries
* - Device path construction for iSCSI targets
*
* File: IScsiCHAP.c
* Range: 0xF4AC (1 function, CHAP init)
* ISCSI_CHAP_INIT 0xF4AC Initialize CHAP authentication
*
* File: Utility/libraries (BaseLib, BaseMemoryLib, DxeNetLib, HiiLib, etc.)
* Range: 0xF52C - 0x199B0 (~120 functions)
*
* Library functions linked:
* - BaseLib: String.c, SafeString.c, CheckSum.c, Unaligned.c, LinkedList.c
* - BaseMemoryLibRepStr: CopyMem, ZeroMem, SetMem, CompareMem, SetMem16
* - BasePrintLib: PrintLib.c, PrintLibInternal.c
* - UefiMemoryAllocationLib: AllocatePool, FreePool, etc.
* - UefiDevicePathLib: DevicePathUtilities.c
* - UefiLib: UefiLib.c, UefiDriverModel.c, Console.c
* - DxeHobLib: HobLib.c
* - DxePcdLib: DxePcdLib.c
* - DxeNetLib: DxeNetLib.c, NetBuffer.c
* - DxeTcpIoLib: DxeTcpIoLib.c
* - UefiHiiLib: HiiLib.c, HiiString.c, HiiLanguage.c
* - BaseIoLibIntrinsic: IoLib.c, IoLibMsc.c
* - UefiHiiServicesLib: protocol lookups for HII handles
* - AmiNetworkPkg internal IScsiDxe helpers (IP/net utility functions)
*
* ============================================================================
* PROTOCOL USAGE
* ============================================================================
*
* Consumed protocols:
* - gEfiAdapterInfoProtocolGuid (0xE5DD1403...): locate AIP handles
* - gEfiIp4Config2ProtocolGuid: IPv4 configuration
* - gEfiIp6ConfigProtocolGuid: IPv6 configuration
* - gEfiTcp4ProtocolGuid: TCP/IPv4 connection
* - gEfiTcp6ProtocolGuid: TCP/IPv6 connection
* - gEfiDhcp4ProtocolGuid: DHCPv4
* - gEfiDhcp6ProtocolGuid: DHCPv6
* - gEfiDns4ProtocolGuid: DNSv4
* - gEfiDns6ProtocolGuid: DNSv6
* - EFI_HII_DATABASE_PROTOCOL
* - EFI_HII_STRING_PROTOCOL
* - EFI_HII_CONFIG_ROUTING_PROTOCOL
* - EFI_HII_CONFIG_ACCESS_PROTOCOL
* - EFI_DRIVER_BINDING_PROTOCOL
* - EFI_COMPONENT_NAME2_PROTOCOL
*
* Produced protocols:
* - ISCSI4_PROTOCOL (0x7671D9D0...): iSCSI session control
* - EFI_EXT_SCSI_PASS_THRU_PROTOCOL: SCSI command pass-through
*
* ============================================================================
* DATA STRUCTURES
* ============================================================================
*
* Global data (in .data @ 0x1E2E0):
* 0x20AF8: ISCSI_DRIVER_DATA* (main private data, 1049 bytes allocated)
* 0x20B00: Component name language table pointer
* 0x20B08: ISCSI_DRIVER_DATA* secondary reference
* 0x20B18: SystemTable pointer
* 0x20B20: BootServices pointer
* 0x20B28: ImageHandle
* 0x20B30: RuntimeServices pointer
* 0x20B38: Not used
* 0x20B40: Not used
* 0x20B48: PCD pointer (PcdCpuNumberOfCores)
* 0x20B50: HII Database protocol pointer
* 0x20B58: HII Config Routing protocol pointer
* 0x20B60: HII Config Access protocol pointer (unused)
* 0x20B68: HII Config Routing v2 (unused)
* 0x20B70: HII String protocol pointer
* 0x20B98: Counter (3 bytes: attempt count)
* 0x20BA8: byte flag (NIC add success)
*
* Protocol dispatch tables (in .rdata):
* 0x202E0: EFI_DRIVER_BINDING_PROTOCOL (3 entries+)
* 0x202A0: EFI_COMPONENT_NAME2_PROTOCOL (3 entries+)
* 0x20290: EFI_COMPONENT_NAME2_PROTOCOL for IPv4
* 0x20270: EFI_COMPONENT_NAME2_PROTOCOL for IPv6
* 0x20260: EFI_COMPONENT_NAME_PROTOCOL for IPv6
* 0x202D0: EFI_COMPONENT_NAME_PROTOCOL for IPv4
* 0x203C0: EFI_EXT_SCSI_PASS_THRU_PROTOCOL function table
* 0x203B0: EFI_EXT_SCSI_PASS_THRU_PROTOCOL for second instance
* 0x20310: ISCSI4_PROTOCOL function table
* 0x20328: ISCSI4_PROTOCOL function table (v2)
* 0x20340: ISCSI4_PROTOCOL with GUID
* 0x20380: HII package list (DISCSI_CONFIG_IFR_NVDATA)
*
* ============================================================================
* FUNCTION DISPATCH TABLES
* ============================================================================
* off_202E0 (IScsiDxe DriverBinding):
* +0x00: sub_1868 (ISCSI_DRIVER_BINDING.Supported)
* +0x08: sub_1870 (ISCSI_DRIVER_BINDING.Start) - actually ISCSI_DRIVER_BINDING
* +0x10: sub_1898 (ISCSI_DRIVER_BINDING.Stop)
* +0x18: version=0x0A
* +0x40: sub_4B9C (ISCSI_COMPONENT_NAME_GET)
* +0x48: sub_4CEC (ISCSI_COMPONENT_NAME2)
*
* off_202A0 (ComponentName2):
* +0x00: sub_18AC (ComponentNameGetDriverName) - nullsub?
* +0x08: sub_18B4 (ComponentNameGetControllerName)
* +0x10: sub_18DC (ComponentName2.DriverName)
* +0x18: version=0x0A
* +0x40: "IScsi Driver" language struct
*
* ============================================================================
* iSCSI CONFIG VARIABLES
* ============================================================================
* Variable GUID Type Description
* -------- ---- ---- -----------
* x-UEFI-ns {0x...} ns Namespace marker
* AttemptOrder {0x...} raw Order of existing attempts
* InitialAttemptOrder {0x...} raw Initial boot order
* Attempt %d {59324945-...} data Per-attempt config (1049 bytes)
* StrIScsiAttempConfig {59324945-...} str Config string for attempt
* StrIScsiModePred {59324945-...} str IP mode string
* IScsiBootEnableList {59324945-...} str Boot enable list
* StrIScsiIpMode {59324945-...} str IP mode configuration
* IScsiIpAddress {59324945-...} str IP address
* StrIScsiConnectRetry {59324945-...} str Connect retry count
* IScsiConnectTimeout {59324945-...} str Connect timeout
* StrIScsiConnectTimeout {59324945-...} str Connect timeout string
* IScsiConnectTimeoutVal {59324945-...} str Connect timeout value
* StrIScsiIsidPredict {59324945-...} str ISID value
* IScsiIsid {59324945-...} str ISID as string
* StrIScsiInitiatorName {59324945-...} str Initiator name
* IScsiInitiatorName {59324945-...} str Initiator name value
* StrIScsiInitiatorSecret {59324945-...} str Initiator CHAP secret
* IScsiInitiatorSecretData {59324945-...} str Secret data
* StrIScsiTargetName {59324945-...} str Target name
* IScsiTargetInfoViaDhcp {59324945-...} str Target info by DHCP
* StrIScsiTargetTcpPort {59324945-...} str Target TCP port
* IScsiTargetTcpPort {59324945-...} str Target port value
* StrIScsiTargetName2 {59324945-...} str Target name (2nd)
* IScsiTargetNameValue {59324945-...} str Target name value
* StrIScsiTargetIpAddr {59324945-...} str Target IP address
* IScsiTargetIpAddress {59324945-...} str Target IP value
* StrIScsiLunPredict {59324945-...} str LUN (keyword)
* IScsiLun {59324945-...} str LUN value
* StrIScsiAuthMethod {59324945-...} str Auth method
* IScsiAuthenticationData {59324945-...} str Auth mode value
* StrIScsiChapTypePredict {59324945-...} str CHAP type
* IScsiChapTypeData {59324945-...} str CHAP type value
* StrIScsiCharUserName {59324945-...} str CHAP user name
* IScsiChapUserName {59324945-...} str User name value
* StrIScsiCharSecret {59324945-...} str CHAP secret
* IScsiChapSecretData {59324945-...} str CHAP secret value
* StrIScsiCharReverseName {59324945-...} str Reverse CHAP name
* IScsiReverseChapNameData {59324945-...} str Reverse name value
* StrIScsiCharReverseSecr {59324945-...} str Reverse CHAP secret
* IScsiReverseChapSecretDa {59324945-...} str Reverse secret value
* DISCSI_CONFIG_IFR_NVDAT {59324945-...} IFR IFR NVRAM data
*
* ============================================================================
* IMPORTANT OFFSETS IN ISCSI_DRIVER_DATA (at qword_20AF8, 1049 bytes)
* ============================================================================
* +0x00: Signature "AMIS" (0x414D4953)
* +0x08: Driver handle (EFI_HANDLE)
* +0x10: Controller handle (EFI_HANDLE)
* +0x17: AttemptConfigIndex
* +0x1A: PortString[112] (CHAR16 string)
* +0xFA: NicIndex
* +0xFB: NicValid
* +0xFC: BddPolicy
* +0xFD: IpMode (0=None, 1=DHCP, 2=Static)
* +0x17: AttemptConfigIndex
* +0x??: AttemptCount
* +0x??: SinglePathCount
* +0x??: MpioCount
* +0x1C0: NicList (LIST_ENTRY at offset ~448)
* +0x1D0: NicList tail
* +0x1D8: SessionList (LIST_ENTRY at offset ~480)
* +0x1E0: MacString[96]
* +0x??: HiiHandle
*
* ISCSI_NIC_DATA entry (88 bytes):
* +0x00: LIST_ENTRY (Link, 16 bytes: Flink, Blink)
* +0x10: AttemptConfigIndex (UINT32)
* +0x14: Reserved
* +0x18: MacString[32] (MAC address as hex)
* +0x34: VlanId (UINT16)
* +0x36: NicIndex (UINT8)
* +0x37: Enabled (UINT8, 0/1)
* +0x38: Ipv6Available (UINT8, 0/1)
*
* ============================================================================
* iSCSI SESSION STATE
* ============================================================================
* Session states:
* 0 = IDLE
* 1 = CONNECTING (TCP connect in progress)
* 2 = LOGGING_IN (iSCSI login phase)
* 3 = LOGGED_IN (fully operational)
* 4 = ERROR (recovery needed)
* 5 = LOGGED_OUT (explicit logout)
*
* ============================================================================
* BUILD
* ============================================================================
* Build path: e:\hs\Build\HR6N0XMLK\DEBUG_VS2015\X64\Build\IScsiDxe\DEBUG\
* PDB: IScsiDxe.pdb
* Platform: HR6N0XMLK (HR650X)
* Toolchain: VS2015 DEBUG X64
*
* ============================================================================
* FUNCTION INDEX
* ============================================================================
*
* See IScsiDxe.md for full function listing.
*/