2 Implementation for iSCSI Boot Firmware Table publication.
4 Copyright (c) 2004 - 2011, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 #include "IScsiImpl.h"
17 BOOLEAN mIbftInstalled
= FALSE
;
21 Initialize the header of the iSCSI Boot Firmware Table.
23 @param[out] Header The header of the iSCSI Boot Firmware Table.
24 @param[in] OemId The OEM ID.
25 @param[in] OemTableId The OEM table ID for the iBFT.
28 IScsiInitIbfTableHeader (
29 OUT EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_HEADER
*Header
,
34 ZeroMem (Header
, sizeof (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_HEADER
));
36 Header
->Signature
= EFI_ACPI_3_0_ISCSI_BOOT_FIRMWARE_TABLE_SIGNATURE
;
37 Header
->Length
= IBFT_HEAP_OFFSET
;
38 Header
->Revision
= EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_REVISION
;
41 Header
->OemId
[0] = 'I';
42 Header
->OemId
[1] = 'N';
43 Header
->OemId
[2] = 'T';
44 Header
->OemId
[3] = 'E';
45 Header
->OemId
[4] = 'L';
47 CopyMem (Header
->OemId
, OemId
, sizeof (Header
->OemId
));
48 CopyMem (&Header
->OemTableId
, OemTableId
, sizeof (UINT64
));
52 Initialize the control section of the iSCSI Boot Firmware Table.
54 @param[in] Table The ACPI table.
55 @param[in] HandleCount The number of the handles associated with iSCSI sessions, it's
56 equal to the number of iSCSI sessions.
59 IScsiInitControlSection (
60 IN EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_HEADER
*Table
,
64 EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_CONTROL_STRUCTURE
*Control
;
67 Control
= (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_CONTROL_STRUCTURE
*) (Table
+ 1);
69 ZeroMem (Control
, sizeof (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_CONTROL_STRUCTURE
));
71 Control
->Header
.StructureId
= EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_CONTROL_STRUCTURE_ID
;
72 Control
->Header
.Version
= EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_CONTROL_STRUCTURE_VERSION
;
73 Control
->Header
.Length
= (UINT16
) sizeof (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_CONTROL_STRUCTURE
);
76 // Each session occupies two offsets, one for the NIC section,
77 // the other for the Target section.
79 NumOffset
= 2 * HandleCount
;
82 // Need expand the control section if more than 2 NIC/Target sections
85 Control
->Header
.Length
= (UINT16
) (Control
->Header
.Length
+ (NumOffset
- 4) * sizeof (UINT16
));
90 Add one item into the heap.
92 @param[in, out] Heap On input, the current address of the heap; On output, the address of
93 the heap after the item is added.
94 @param[in] Data The data to add into the heap.
95 @param[in] Len Length of the Data in byte.
105 // Add one byte for the NULL delimiter.
109 CopyMem (*Heap
, Data
, Len
);
114 Fill the Initiator section of the iSCSI Boot Firmware Table.
116 @param[in] Table The ACPI table.
117 @param[in, out] Heap The heap.
118 @param[in] Handle The handle associated with the iSCSI session.
121 IScsiFillInitiatorSection (
122 IN EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_HEADER
*Table
,
127 EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_CONTROL_STRUCTURE
*Control
;
128 EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_INITIATOR_STRUCTURE
*Initiator
;
129 ISCSI_DRIVER_DATA
*DriverData
;
130 ISCSI_SESSION
*Session
;
131 ISCSI_PRIVATE_PROTOCOL
*IScsiIdentifier
;
134 Control
= (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_CONTROL_STRUCTURE
*) (Table
+ 1);
137 // Initiator section immediately follows the control section.
139 Initiator
= (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_INITIATOR_STRUCTURE
*) ((UINT8
*) Control
+ IBFT_ROUNDUP (Control
->Header
.Length
));
141 Control
->InitiatorOffset
= (UINT16
) ((UINTN
) Initiator
- (UINTN
) Table
);
143 ZeroMem (Initiator
, sizeof (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_INITIATOR_STRUCTURE
));
145 Initiator
->Header
.StructureId
= EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_INITIATOR_STRUCTURE_ID
;
146 Initiator
->Header
.Version
= EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_INITIATOR_STRUCTURE_VERSION
;
147 Initiator
->Header
.Length
= (UINT16
) sizeof (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_INITIATOR_STRUCTURE
);
148 Initiator
->Header
.Flags
= EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_INITIATOR_STRUCTURE_FLAG_BLOCK_VALID
| EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_INITIATOR_STRUCTURE_FLAG_BOOT_SELECTED
;
151 // Get the identifier from the handle.
153 Status
= gBS
->HandleProtocol (Handle
, &gEfiCallerIdGuid
, (VOID
**) &IScsiIdentifier
);
154 if (EFI_ERROR (Status
)) {
159 DriverData
= ISCSI_DRIVER_DATA_FROM_IDENTIFIER (IScsiIdentifier
);
160 Session
= &DriverData
->Session
;
163 // Fill the iSCSI Initiator Name into the heap.
165 IScsiAddHeapItem (Heap
, Session
->InitiatorName
, Session
->InitiatorNameLength
- 1);
167 Initiator
->IScsiNameLength
= (UINT16
) (Session
->InitiatorNameLength
- 1);
168 Initiator
->IScsiNameOffset
= (UINT16
) ((UINTN
) *Heap
- (UINTN
) Table
);
172 Map the v4 IP address into v6 IP address.
174 @param[in] V4 The v4 IP address.
175 @param[out] V6 The v6 IP address.
179 IN EFI_IPv4_ADDRESS
*V4
,
180 OUT EFI_IPv6_ADDRESS
*V6
185 ZeroMem (V6
, sizeof (EFI_IPv6_ADDRESS
));
190 for (Index
= 0; Index
< 4; Index
++) {
191 V6
->Addr
[12 + Index
] = V4
->Addr
[Index
];
196 Get the NIC's PCI location and return it accroding to the composited
197 format defined in iSCSI Boot Firmware Table.
199 @param[in] Controller The handle of the controller.
201 @return UINT16 The composited representation of the NIC PCI location.
202 @retval 0 Other errors as indicated.
205 IScsiGetNICPciLocation (
206 IN EFI_HANDLE Controller
210 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
211 EFI_HANDLE PciIoHandle
;
212 EFI_PCI_IO_PROTOCOL
*PciIo
;
218 Status
= gBS
->HandleProtocol (
220 &gEfiDevicePathProtocolGuid
,
223 if (EFI_ERROR (Status
)) {
227 Status
= gBS
->LocateDevicePath (
228 &gEfiPciIoProtocolGuid
,
232 if (EFI_ERROR (Status
)) {
236 Status
= gBS
->HandleProtocol (PciIoHandle
, &gEfiPciIoProtocolGuid
, (VOID
**)&PciIo
);
237 if (EFI_ERROR (Status
)) {
241 Status
= PciIo
->GetLocation (PciIo
, &Segment
, &Bus
, &Device
, &Function
);
242 if (EFI_ERROR (Status
)) {
246 return (UINT16
) ((Bus
<< 8) | (Device
<< 3) | Function
);
250 Fill the NIC and target sections in iSCSI Boot Firmware Table.
252 @param[in] Table The buffer of the ACPI table.
253 @param[in, out] Heap The heap buffer used to store the variable length parameters such as iSCSI name.
254 @param[in] HandleCount Count The number of handles having iSCSI private protocol installed.
255 @param[in] Handles The handle buffer.
258 IScsiFillNICAndTargetSections (
259 IN EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_HEADER
*Table
,
261 IN UINTN HandleCount
,
262 IN EFI_HANDLE
*Handles
265 EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_CONTROL_STRUCTURE
*Control
;
266 EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_NIC_STRUCTURE
*Nic
;
267 EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_TARGET_STRUCTURE
*Target
;
268 ISCSI_DRIVER_DATA
*DriverData
;
269 ISCSI_SESSION_CONFIG_DATA
*SessionConfigData
;
270 ISCSI_CHAP_AUTH_CONFIG_NVDATA
*AuthConfig
;
271 UINT16
*SectionOffset
;
274 EFI_MAC_ADDRESS MacAddress
;
276 ISCSI_PRIVATE_PROTOCOL
*IScsiIdentifier
;
280 // Get the offset of the first Nic and Target section.
282 Control
= (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_CONTROL_STRUCTURE
*) (Table
+ 1);
283 Nic
= (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_NIC_STRUCTURE
*) ((UINTN
) Table
+
284 Control
->InitiatorOffset
+ IBFT_ROUNDUP (sizeof (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_INITIATOR_STRUCTURE
)));
285 Target
= (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_TARGET_STRUCTURE
*) ((UINTN
) Nic
+
286 IBFT_ROUNDUP (sizeof (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_NIC_STRUCTURE
)));
288 SectionOffset
= &Control
->NIC0Offset
;
290 for (Index
= 0; Index
< HandleCount
; Index
++) {
291 Status
= gBS
->HandleProtocol (Handles
[Index
], &gEfiCallerIdGuid
, (VOID
**)&IScsiIdentifier
);
292 if (EFI_ERROR (Status
)) {
297 DriverData
= ISCSI_DRIVER_DATA_FROM_IDENTIFIER (IScsiIdentifier
);
298 SessionConfigData
= &DriverData
->Session
.ConfigData
;
299 AuthConfig
= &DriverData
->Session
.AuthData
.AuthConfig
;
302 // Fill the Nic section.
304 ZeroMem (Nic
, sizeof (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_NIC_STRUCTURE
));
306 Nic
->Header
.StructureId
= EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_NIC_STRUCTURE_ID
;
307 Nic
->Header
.Version
= EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_NIC_STRUCTURE_VERSION
;
308 Nic
->Header
.Length
= (UINT16
) sizeof (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_NIC_STRUCTURE
);
309 Nic
->Header
.Index
= (UINT8
) Index
;
310 Nic
->Header
.Flags
= EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_NIC_STRUCTURE_FLAG_BLOCK_VALID
|
311 EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_NIC_STRUCTURE_FLAG_BOOT_SELECTED
|
312 EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_NIC_STRUCTURE_FLAG_GLOBAL
;
315 // Get the subnet mask prefix length.
317 Nic
->SubnetMaskPrefixLength
= IScsiGetSubnetMaskPrefixLength (&SessionConfigData
->NvData
.SubnetMask
);
319 if (SessionConfigData
->NvData
.InitiatorInfoFromDhcp
) {
320 Nic
->Origin
= IpPrefixOriginDhcp
;
322 Nic
->Origin
= IpPrefixOriginManual
;
325 // Map the various v4 addresses into v6 addresses.
327 IScsiMapV4ToV6Addr (&SessionConfigData
->NvData
.LocalIp
, &Nic
->Ip
);
328 IScsiMapV4ToV6Addr (&SessionConfigData
->NvData
.Gateway
, &Nic
->Gateway
);
329 IScsiMapV4ToV6Addr (&SessionConfigData
->PrimaryDns
, &Nic
->PrimaryDns
);
330 IScsiMapV4ToV6Addr (&SessionConfigData
->SecondaryDns
, &Nic
->SecondaryDns
);
331 IScsiMapV4ToV6Addr (&SessionConfigData
->DhcpServer
, &Nic
->DhcpServer
);
333 Nic
->VLanTag
= NetLibGetVlanId (DriverData
->Controller
);
335 Status
= NetLibGetMacAddress (DriverData
->Controller
, &MacAddress
, &HwAddressSize
);
336 ASSERT (Status
== EFI_SUCCESS
);
337 CopyMem (Nic
->Mac
, MacAddress
.Addr
, sizeof (Nic
->Mac
));
340 // Get the PCI location of the Nic.
342 Nic
->PciLocation
= IScsiGetNICPciLocation (DriverData
->Controller
);
344 *SectionOffset
= (UINT16
) ((UINTN
) Nic
- (UINTN
) Table
);
348 // Fill the Target section.
350 ZeroMem (Target
, sizeof (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_TARGET_STRUCTURE
));
352 Target
->Header
.StructureId
= EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_TARGET_STRUCTURE_ID
;
353 Target
->Header
.Version
= EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_TARGET_STRUCTURE_VERSION
;
354 Target
->Header
.Length
= (UINT16
) sizeof (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_TARGET_STRUCTURE
);
355 Target
->Header
.Index
= (UINT8
) Index
;
356 Target
->Header
.Flags
= EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_TARGET_STRUCTURE_FLAG_BLOCK_VALID
| EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_TARGET_STRUCTURE_FLAG_BOOT_SELECTED
;
357 Target
->Port
= SessionConfigData
->NvData
.TargetPort
;
358 Target
->CHAPType
= AuthConfig
->CHAPType
;
359 Target
->NicIndex
= (UINT8
) Index
;
361 IScsiMapV4ToV6Addr (&SessionConfigData
->NvData
.TargetIp
, &Target
->Ip
);
362 CopyMem (Target
->BootLun
, SessionConfigData
->NvData
.BootLun
, sizeof (Target
->BootLun
));
365 // Target iSCSI Name, CHAP name/secret, reverse CHAP name/secret.
367 Length
= (UINT16
) AsciiStrLen (SessionConfigData
->NvData
.TargetName
);
368 IScsiAddHeapItem (Heap
, SessionConfigData
->NvData
.TargetName
, Length
);
370 Target
->IScsiNameLength
= Length
;
371 Target
->IScsiNameOffset
= (UINT16
) ((UINTN
) *Heap
- (UINTN
) Table
);
373 if (Target
->CHAPType
!= ISCSI_CHAP_NONE
) {
377 Length
= (UINT16
) AsciiStrLen (AuthConfig
->CHAPName
);
378 IScsiAddHeapItem (Heap
, AuthConfig
->CHAPName
, Length
);
379 Target
->CHAPNameLength
= Length
;
380 Target
->CHAPNameOffset
= (UINT16
) ((UINTN
) *Heap
- (UINTN
) Table
);
385 Length
= (UINT16
) AsciiStrLen (AuthConfig
->CHAPSecret
);
386 IScsiAddHeapItem (Heap
, AuthConfig
->CHAPSecret
, Length
);
387 Target
->CHAPSecretLength
= Length
;
388 Target
->CHAPSecretOffset
= (UINT16
) ((UINTN
) *Heap
- (UINTN
) Table
);
390 if (Target
->CHAPType
== ISCSI_CHAP_MUTUAL
) {
394 Length
= (UINT16
) AsciiStrLen (AuthConfig
->ReverseCHAPName
);
395 IScsiAddHeapItem (Heap
, AuthConfig
->ReverseCHAPName
, Length
);
396 Target
->ReverseCHAPNameLength
= Length
;
397 Target
->ReverseCHAPNameOffset
= (UINT16
) ((UINTN
) *Heap
- (UINTN
) Table
);
400 // Reverse CHAP Secret
402 Length
= (UINT16
) AsciiStrLen (AuthConfig
->ReverseCHAPSecret
);
403 IScsiAddHeapItem (Heap
, AuthConfig
->ReverseCHAPSecret
, Length
);
404 Target
->ReverseCHAPSecretLength
= Length
;
405 Target
->ReverseCHAPSecretOffset
= (UINT16
) ((UINTN
) *Heap
- (UINTN
) Table
);
409 *SectionOffset
= (UINT16
) ((UINTN
) Target
- (UINTN
) Table
);
413 // Advance to the next NIC/Target pair
415 Nic
= (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_NIC_STRUCTURE
*) ((UINTN
) Target
+
416 IBFT_ROUNDUP (sizeof (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_TARGET_STRUCTURE
)));
417 Target
= (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_TARGET_STRUCTURE
*) ((UINTN
) Nic
+
418 IBFT_ROUNDUP (sizeof (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_NIC_STRUCTURE
)));
423 Publish and remove the iSCSI Boot Firmware Table according to the iSCSI
432 EFI_ACPI_TABLE_PROTOCOL
*AcpiTableProtocol
;
433 EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_HEADER
*Table
;
435 EFI_HANDLE
*HandleBuffer
;
439 EFI_ACPI_3_0_ROOT_SYSTEM_DESCRIPTION_POINTER
*Rsdp
;
440 EFI_ACPI_DESCRIPTION_HEADER
*Rsdt
;
442 Status
= gBS
->LocateProtocol (&gEfiAcpiTableProtocolGuid
, NULL
, (VOID
**)&AcpiTableProtocol
);
443 if (EFI_ERROR (Status
)) {
449 // Find ACPI table RSD_PTR from system table
451 for (Index
= 0, Rsdp
= NULL
; Index
< gST
->NumberOfTableEntries
; Index
++) {
452 if (CompareGuid (&(gST
->ConfigurationTable
[Index
].VendorGuid
), &gEfiAcpi20TableGuid
) ||
453 CompareGuid (&(gST
->ConfigurationTable
[Index
].VendorGuid
), &gEfiAcpi10TableGuid
) ||
454 CompareGuid (&(gST
->ConfigurationTable
[Index
].VendorGuid
), &gEfiAcpiTableGuid
)
457 // A match was found.
459 Rsdp
= (EFI_ACPI_3_0_ROOT_SYSTEM_DESCRIPTION_POINTER
*) gST
->ConfigurationTable
[Index
].VendorTable
;
467 Rsdt
= (EFI_ACPI_DESCRIPTION_HEADER
*) (UINTN
) Rsdp
->RsdtAddress
;
471 if (mIbftInstalled
) {
472 Status
= AcpiTableProtocol
->UninstallAcpiTable (
476 if (EFI_ERROR (Status
)) {
479 mIbftInstalled
= FALSE
;
483 // Get all iSCSI private protocols.
485 Status
= gBS
->LocateHandleBuffer (
492 if (EFI_ERROR (Status
)) {
496 // Allocate 4k bytes to hold the ACPI table.
498 Table
= AllocateZeroPool (IBFT_MAX_SIZE
);
503 Heap
= (UINT8
*) Table
+ IBFT_HEAP_OFFSET
;
506 // Fill in the various section of the iSCSI Boot Firmware Table.
508 IScsiInitIbfTableHeader (Table
, Rsdt
->OemId
, &Rsdt
->OemTableId
);
509 IScsiInitControlSection (Table
, HandleCount
);
510 IScsiFillInitiatorSection (Table
, &Heap
, HandleBuffer
[0]);
511 IScsiFillNICAndTargetSections (Table
, &Heap
, HandleCount
, HandleBuffer
);
513 Checksum
= CalculateCheckSum8((UINT8
*)Table
, Table
->Length
);
514 Table
->Checksum
= Checksum
;
516 FreePool (HandleBuffer
);
519 // Install or update the iBFT table.
521 Status
= AcpiTableProtocol
->InstallAcpiTable (
527 if (EFI_ERROR(Status
)) {
531 mIbftInstalled
= TRUE
;