2 Implementation for iSCSI Boot Firmware Table publication.
4 Copyright (c) 2004 - 2008, Intel Corporation
5 All rights reserved. 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.
19 Implementation for iSCSI Boot Firmware Table publication.
23 #include "IScsiImpl.h"
26 Initialize the header of the iSCSI Boot Firmware Table.
28 @param Header[in] The header of the iSCSI Boot Firmware Table.
34 IScsiInitIbfTableHeader (
35 IN EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_HEADER
*Header
38 ZeroMem (Header
, sizeof (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_HEADER
));
40 Header
->Signature
= EFI_ACPI_3_0_ISCSI_BOOT_FIRMWARE_TABLE_SIGNATURE
;
41 Header
->Length
= IBFT_HEAP_OFFSET
;
42 Header
->Revision
= EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_REVISION
;
45 Header
->OemId
[0] = 'I';
46 Header
->OemId
[1] = 'N';
47 Header
->OemId
[2] = 'T';
48 Header
->OemId
[3] = 'E';
49 Header
->OemId
[4] = 'L';
53 Initialize the control section of the iSCSI Boot Firmware Table.
55 @param Table[in] The ACPI table.
57 @param HandleCount[in] The number of the handles associated with iSCSI sessions, it's
58 equal to the number of iSCSI sessions.
64 IScsiInitControlSection (
65 IN EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_HEADER
*Table
,
69 EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_CONTROL_STRUCTURE
*Control
;
72 Control
= (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_CONTROL_STRUCTURE
*) (Table
+ 1);
74 ZeroMem (Control
, sizeof (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_CONTROL_STRUCTURE
));
76 Control
->Header
.StructureId
= EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_CONTROL_STRUCTURE_ID
;
77 Control
->Header
.Version
= EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_CONTROL_STRUCTURE_VERSION
;
78 Control
->Header
.Length
= sizeof (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_CONTROL_STRUCTURE
);
81 // Each session occupies two offsets, one for the NIC section,
82 // the other for the Target section.
84 NumOffset
= 2 * HandleCount
;
87 // Need expand the control section if more than 2 NIC/Target sections
90 Control
->Header
.Length
= (UINT16
) (Control
->Header
.Length
+ (NumOffset
- 4) * sizeof (UINT16
));
95 Add one item into the heap.
97 @param Heap[in][out] On input, the current address of the heap; On output, the address of
98 the heap after the item is added.
100 @param Data[in] The data to add into the heap.
102 @param Len[in] Length of the Data in byte.
115 // Add one byte for the NULL delimiter.
119 CopyMem (*Heap
, Data
, Len
);
124 Fill the Initiator section of the iSCSI Boot Firmware Table.
126 @param Table[in] The ACPI table.
128 @param Heap[in][out] The heap.
130 @param Handle[in] The handle associated with the iSCSI session.
136 IScsiFillInitiatorSection (
137 IN EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_HEADER
*Table
,
142 EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_CONTROL_STRUCTURE
*Control
;
143 EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_INITIATOR_STRUCTURE
*Initiator
;
144 ISCSI_DRIVER_DATA
*DriverData
;
145 ISCSI_SESSION
*Session
;
146 ISCSI_PRIVATE_PROTOCOL
*IScsiIdentifier
;
149 Control
= (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_CONTROL_STRUCTURE
*) (Table
+ 1);
152 // Initiator section immediately follows the control section.
154 Initiator
= (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_INITIATOR_STRUCTURE
*) ((UINT8
*) Control
+ IBFT_ROUNDUP (Control
->Header
.Length
));
156 Control
->InitiatorOffset
= (UINT16
) ((UINTN
) Initiator
- (UINTN
) Table
);
158 ZeroMem (Initiator
, sizeof (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_INITIATOR_STRUCTURE
));
160 Initiator
->Header
.StructureId
= EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_INITIATOR_STRUCTURE_ID
;
161 Initiator
->Header
.Version
= EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_INITIATOR_STRUCTURE_VERSION
;
162 Initiator
->Header
.Length
= sizeof (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_INITIATOR_STRUCTURE
);
163 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
;
166 // Get the identifier from the handle.
168 Status
= gBS
->HandleProtocol (Handle
, &mIScsiPrivateGuid
, (VOID
**) &IScsiIdentifier
);
169 if (EFI_ERROR (Status
)) {
174 DriverData
= ISCSI_DRIVER_DATA_FROM_IDENTIFIER (IScsiIdentifier
);
175 Session
= &DriverData
->Session
;
178 // Fill the iSCSI Initiator Name into the heap.
180 IScsiAddHeapItem (Heap
, Session
->InitiatorName
, Session
->InitiatorNameLength
- 1);
182 Initiator
->IScsiNameLength
= (UINT16
) (Session
->InitiatorNameLength
- 1);
183 Initiator
->IScsiNameOffset
= (UINT16
) ((UINTN
) *Heap
- (UINTN
) Table
);
187 Map the v4 IP address into v6 IP address.
189 @param V4 The v4 IP address.
191 @param V6 The v6 IP address.
198 IN EFI_IPv4_ADDRESS
*V4
,
199 OUT EFI_IPv6_ADDRESS
*V6
204 ZeroMem (V6
, sizeof (EFI_IPv6_ADDRESS
));
209 for (Index
= 0; Index
< 4; Index
++) {
210 V6
->Addr
[12 + Index
] = V4
->Addr
[Index
];
215 Get the NIC's PCI location and return it accroding to the composited
216 format defined in iSCSI Boot Firmware Table.
218 @param Controller[in] The handle of the controller.
220 @retval UINT16 The composited representation of the NIC PCI location.
224 IScsiGetNICPciLocation (
225 IN EFI_HANDLE Controller
229 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
230 EFI_HANDLE PciIoHandle
;
231 EFI_PCI_IO_PROTOCOL
*PciIo
;
237 Status
= gBS
->HandleProtocol (
239 &gEfiDevicePathProtocolGuid
,
242 if (EFI_ERROR (Status
)) {
246 Status
= gBS
->LocateDevicePath (
247 &gEfiPciIoProtocolGuid
,
251 if (EFI_ERROR (Status
)) {
255 Status
= gBS
->HandleProtocol (PciIoHandle
, &gEfiPciIoProtocolGuid
, (VOID
**)&PciIo
);
256 if (EFI_ERROR (Status
)) {
260 Status
= PciIo
->GetLocation (PciIo
, &Segment
, &Bus
, &Device
, &Function
);
261 if (EFI_ERROR (Status
)) {
265 return (UINT16
) ((Bus
<< 8) | (Device
<< 3) | Function
);
269 Get the MAC address of the controller.
271 @param Controller[in] The handle of the controller.
273 @retval EFI_MAC_ADDRESS * The mac address.
278 IN EFI_HANDLE Controller
282 EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
;
284 Status
= gBS
->HandleProtocol (
286 &gEfiSimpleNetworkProtocolGuid
,
289 ASSERT_EFI_ERROR (Status
);
291 return &Snp
->Mode
->PermanentAddress
;
295 Fill the NIC and target sections in iSCSI Boot Firmware Table.
297 @param Table[in] The buffer of the ACPI table.
299 @param Heap[in][out] The heap buffer used to store the variable length parameters such as iSCSI name.
301 @param HandleCount[in] The number of handles having iSCSI private protocol installed.
303 @param Handles[in] The handle buffer.
309 IScsiFillNICAndTargetSections (
310 IN EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_HEADER
*Table
,
312 IN UINTN HandleCount
,
313 IN EFI_HANDLE
*Handles
316 EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_CONTROL_STRUCTURE
*Control
;
317 EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_NIC_STRUCTURE
*Nic
;
318 EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_TARGET_STRUCTURE
*Target
;
319 ISCSI_DRIVER_DATA
*DriverData
;
320 ISCSI_SESSION_CONFIG_DATA
*SessionConfigData
;
321 ISCSI_CHAP_AUTH_CONFIG_NVDATA
*AuthConfig
;
322 UINT16
*SectionOffset
;
325 EFI_MAC_ADDRESS
*Mac
;
326 ISCSI_PRIVATE_PROTOCOL
*IScsiIdentifier
;
330 // Get the offset of the first Nic and Target section.
332 Control
= (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_CONTROL_STRUCTURE
*) (Table
+ 1);
333 Nic
= (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_NIC_STRUCTURE
*) ((UINTN
) Table
+
334 Control
->InitiatorOffset
+ IBFT_ROUNDUP (sizeof (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_INITIATOR_STRUCTURE
)));
335 Target
= (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_TARGET_STRUCTURE
*) ((UINTN
) Nic
+
336 IBFT_ROUNDUP (sizeof (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_NIC_STRUCTURE
)));
338 SectionOffset
= &Control
->NIC0Offset
;
340 for (Index
= 0; Index
< HandleCount
; Index
++) {
341 Status
= gBS
->HandleProtocol (Handles
[Index
], &mIScsiPrivateGuid
, (VOID
**)&IScsiIdentifier
);
342 if (EFI_ERROR (Status
)) {
347 DriverData
= ISCSI_DRIVER_DATA_FROM_IDENTIFIER (IScsiIdentifier
);
348 SessionConfigData
= &DriverData
->Session
.ConfigData
;
349 AuthConfig
= &DriverData
->Session
.AuthData
.AuthConfig
;
352 // Fill the Nic section.
354 ZeroMem (Nic
, sizeof (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_NIC_STRUCTURE
));
356 Nic
->Header
.StructureId
= EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_NIC_STRUCTURE_ID
;
357 Nic
->Header
.Version
= EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_NIC_STRUCTURE_VERSION
;
358 Nic
->Header
.Length
= sizeof (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_NIC_STRUCTURE
);
359 Nic
->Header
.Index
= (UINT8
) Index
;
360 Nic
->Header
.Flags
= EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_NIC_STRUCTURE_FLAG_BLOCK_VALID
|
361 EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_NIC_STRUCTURE_FLAG_BOOT_SELECTED
|
362 EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_NIC_STRUCTURE_FLAG_GLOBAL
;
365 // Get the subnet mask prefix length.
367 Nic
->SubnetMaskPrefixLength
= IScsiGetSubnetMaskPrefixLength (&SessionConfigData
->NvData
.SubnetMask
);
369 if (SessionConfigData
->NvData
.InitiatorInfoFromDhcp
) {
370 Nic
->Origin
= IpPrefixOriginDhcp
;
372 Nic
->Origin
= IpPrefixOriginManual
;
375 // Map the various v4 addresses into v6 addresses.
377 IScsiMapV4ToV6Addr (&SessionConfigData
->NvData
.LocalIp
, &Nic
->Ip
);
378 IScsiMapV4ToV6Addr (&SessionConfigData
->NvData
.Gateway
, &Nic
->Gateway
);
379 IScsiMapV4ToV6Addr (&SessionConfigData
->PrimaryDns
, &Nic
->PrimaryDns
);
380 IScsiMapV4ToV6Addr (&SessionConfigData
->SecondaryDns
, &Nic
->SecondaryDns
);
381 IScsiMapV4ToV6Addr (&SessionConfigData
->DhcpServer
, &Nic
->DhcpServer
);
383 Mac
= IScsiGetMacAddress (DriverData
->Controller
);
384 CopyMem (Nic
->Mac
, Mac
, sizeof (Nic
->Mac
));
387 // Get the PCI location of the Nic.
389 Nic
->PciLocation
= IScsiGetNICPciLocation (DriverData
->Controller
);
391 *SectionOffset
= (UINT16
) ((UINTN
) Nic
- (UINTN
) Table
);
395 // Fill the Target section.
397 ZeroMem (Target
, sizeof (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_TARGET_STRUCTURE
));
399 Target
->Header
.StructureId
= EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_TARGET_STRUCTURE_ID
;
400 Target
->Header
.Version
= EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_TARGET_STRUCTURE_VERSION
;
401 Target
->Header
.Length
= sizeof (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_TARGET_STRUCTURE
);
402 Target
->Header
.Index
= (UINT8
) Index
;
403 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
;
404 Target
->Port
= SessionConfigData
->NvData
.TargetPort
;
405 Target
->CHAPType
= AuthConfig
->CHAPType
;
406 Target
->NicIndex
= (UINT8
) Index
;
408 IScsiMapV4ToV6Addr (&SessionConfigData
->NvData
.TargetIp
, &Target
->Ip
);
409 CopyMem (Target
->BootLun
, SessionConfigData
->NvData
.BootLun
, sizeof (Target
->BootLun
));
412 // Target iSCSI Name, CHAP name/secret, reverse CHAP name/secret.
414 Length
= (UINT16
) AsciiStrLen (SessionConfigData
->NvData
.TargetName
);
415 IScsiAddHeapItem (Heap
, SessionConfigData
->NvData
.TargetName
, Length
);
417 Target
->IScsiNameLength
= Length
;
418 Target
->IScsiNameOffset
= (UINT16
) ((UINTN
) *Heap
- (UINTN
) Table
);
420 if (Target
->CHAPType
!= ISCSI_CHAP_NONE
) {
424 Length
= (UINT16
) AsciiStrLen (AuthConfig
->CHAPName
);
425 IScsiAddHeapItem (Heap
, AuthConfig
->CHAPName
, Length
);
426 Target
->CHAPNameLength
= Length
;
427 Target
->CHAPNameOffset
= (UINT16
) ((UINTN
) *Heap
- (UINTN
) Table
);
432 Length
= (UINT16
) AsciiStrLen (AuthConfig
->CHAPSecret
);
433 IScsiAddHeapItem (Heap
, AuthConfig
->CHAPSecret
, Length
);
434 Target
->CHAPSecretLength
= Length
;
435 Target
->CHAPSecretOffset
= (UINT16
) ((UINTN
) *Heap
- (UINTN
) Table
);
437 if (Target
->CHAPType
== ISCSI_CHAP_MUTUAL
) {
441 Length
= (UINT16
) AsciiStrLen (AuthConfig
->ReverseCHAPName
);
442 IScsiAddHeapItem (Heap
, AuthConfig
->ReverseCHAPName
, Length
);
443 Target
->ReverseCHAPNameLength
= Length
;
444 Target
->ReverseCHAPNameOffset
= (UINT16
) ((UINTN
) *Heap
- (UINTN
) Table
);
447 // Reverse CHAP Secret
449 Length
= (UINT16
) AsciiStrLen (AuthConfig
->ReverseCHAPSecret
);
450 IScsiAddHeapItem (Heap
, AuthConfig
->ReverseCHAPSecret
, Length
);
451 Target
->ReverseCHAPSecretLength
= Length
;
452 Target
->ReverseCHAPSecretOffset
= (UINT16
) ((UINTN
) *Heap
- (UINTN
) Table
);
456 *SectionOffset
= (UINT16
) ((UINTN
) Target
- (UINTN
) Table
);
460 // Advance to the next NIC/Target pair
462 Nic
= (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_NIC_STRUCTURE
*) ((UINTN
) Target
+
463 IBFT_ROUNDUP (sizeof (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_TARGET_STRUCTURE
)));
464 Target
= (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_TARGET_STRUCTURE
*) ((UINTN
) Nic
+
465 IBFT_ROUNDUP (sizeof (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_NIC_STRUCTURE
)));
470 Publish and remove the iSCSI Boot Firmware Table according to the iSCSI
485 EFI_ACPI_SUPPORT_PROTOCOL
*AcpiSupport
;
486 EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_HEADER
*Table
;
488 EFI_HANDLE
*HandleBuffer
;
491 EFI_ACPI_TABLE_VERSION Version
;
494 Status
= gBS
->LocateProtocol (&gEfiAcpiSupportProtocolGuid
, NULL
, (VOID
**)&AcpiSupport
);
495 if (EFI_ERROR (Status
)) {
499 // Try to remove the old iSCSI Boot Firmware Table.
501 for (Index
= 0;; Index
++) {
502 Status
= AcpiSupport
->GetAcpiTable (
509 if (EFI_ERROR (Status
)) {
513 Signature
= Table
->Signature
;
514 gBS
->FreePool (Table
);
516 if (Signature
== EFI_ACPI_3_0_ISCSI_BOOT_FIRMWARE_TABLE_SIGNATURE
) {
520 Status
= AcpiSupport
->SetAcpiTable (
527 if (EFI_ERROR (Status
)) {
535 // Get all iSCSI private protocols.
537 Status
= gBS
->LocateHandleBuffer (
544 if (EFI_ERROR (Status
)) {
548 // Allocate 4k bytes to hold the ACPI table.
550 Table
= AllocatePool (IBFT_MAX_SIZE
);
555 Heap
= (UINT8
*) Table
+ IBFT_HEAP_OFFSET
;
558 // Fill in the various section of the iSCSI Boot Firmware Table.
560 IScsiInitIbfTableHeader (Table
);
561 IScsiInitControlSection (Table
, HandleCount
);
562 IScsiFillInitiatorSection (Table
, &Heap
, HandleBuffer
[0]);
563 IScsiFillNICAndTargetSections (Table
, &Heap
, HandleCount
, HandleBuffer
);
565 gBS
->FreePool (HandleBuffer
);
570 // Install or update the iBFT table.
572 Status
= AcpiSupport
->SetAcpiTable (
576 EFI_ACPI_TABLE_VERSION_3_0
,
579 if (!EFI_ERROR (Status
)) {
580 AcpiSupport
->PublishTables (AcpiSupport
, EFI_ACPI_TABLE_VERSION_3_0
);
583 gBS
->FreePool (Table
);