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.
35 IScsiInitIbfTableHeader (
36 IN EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_HEADER
*Header
39 ZeroMem (Header
, sizeof (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_HEADER
));
41 Header
->Signature
= EFI_ACPI_3_0_ISCSI_BOOT_FIRMWARE_TABLE_SIGNATURE
;
42 Header
->Length
= IBFT_HEAP_OFFSET
;
43 Header
->Revision
= EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_REVISION
;
46 Header
->OemId
[0] = 'I';
47 Header
->OemId
[1] = 'N';
48 Header
->OemId
[2] = 'T';
49 Header
->OemId
[3] = 'E';
50 Header
->OemId
[4] = 'L';
54 Initialize the control section of the iSCSI Boot Firmware Table.
56 @param Table[in] The ACPI table.
58 @param HandleCount[in] The number of the handles associated with iSCSI sessions, it's
59 equal to the number of iSCSI sessions.
66 IScsiInitControlSection (
67 IN EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_HEADER
*Table
,
71 EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_CONTROL_STRUCTURE
*Control
;
74 Control
= (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_CONTROL_STRUCTURE
*) (Table
+ 1);
76 ZeroMem (Control
, sizeof (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_CONTROL_STRUCTURE
));
78 Control
->Header
.StructureId
= EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_CONTROL_STRUCTURE_ID
;
79 Control
->Header
.Version
= EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_CONTROL_STRUCTURE_VERSION
;
80 Control
->Header
.Length
= sizeof (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_CONTROL_STRUCTURE
);
83 // Each session occupies two offsets, one for the NIC section,
84 // the other for the Target section.
86 NumOffset
= 2 * HandleCount
;
89 // Need expand the control section if more than 2 NIC/Target sections
92 Control
->Header
.Length
= (UINT16
) (Control
->Header
.Length
+ (NumOffset
- 4) * sizeof (UINT16
));
97 Add one item into the heap.
99 @param Heap[in][out] On input, the current address of the heap; On output, the address of
100 the heap after the item is added.
102 @param Data[in] The data to add into the heap.
104 @param Len[in] Length of the Data in byte.
118 // Add one byte for the NULL delimiter.
122 CopyMem (*Heap
, Data
, Len
);
127 Fill the Initiator section of the iSCSI Boot Firmware Table.
129 @param Table[in] The ACPI table.
131 @param Heap[in][out] The heap.
133 @param Handle[in] The handle associated with the iSCSI session.
140 IScsiFillInitiatorSection (
141 IN EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_HEADER
*Table
,
146 EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_CONTROL_STRUCTURE
*Control
;
147 EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_INITIATOR_STRUCTURE
*Initiator
;
148 ISCSI_DRIVER_DATA
*DriverData
;
149 ISCSI_SESSION
*Session
;
150 ISCSI_PRIVATE_PROTOCOL
*IScsiIdentifier
;
153 Control
= (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_CONTROL_STRUCTURE
*) (Table
+ 1);
156 // Initiator section immediately follows the control section.
158 Initiator
= (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_INITIATOR_STRUCTURE
*) ((UINT8
*) Control
+ IBFT_ROUNDUP (Control
->Header
.Length
));
160 Control
->InitiatorOffset
= (UINT16
) ((UINTN
) Initiator
- (UINTN
) Table
);
162 ZeroMem (Initiator
, sizeof (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_INITIATOR_STRUCTURE
));
164 Initiator
->Header
.StructureId
= EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_INITIATOR_STRUCTURE_ID
;
165 Initiator
->Header
.Version
= EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_INITIATOR_STRUCTURE_VERSION
;
166 Initiator
->Header
.Length
= sizeof (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_INITIATOR_STRUCTURE
);
167 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
;
170 // Get the identifier from the handle.
172 Status
= gBS
->HandleProtocol (Handle
, &mIScsiPrivateGuid
, (VOID
**) &IScsiIdentifier
);
173 if (EFI_ERROR (Status
)) {
178 DriverData
= ISCSI_DRIVER_DATA_FROM_IDENTIFIER (IScsiIdentifier
);
179 Session
= &DriverData
->Session
;
182 // Fill the iSCSI Initiator Name into the heap.
184 IScsiAddHeapItem (Heap
, Session
->InitiatorName
, Session
->InitiatorNameLength
- 1);
186 Initiator
->IScsiNameLength
= (UINT16
) (Session
->InitiatorNameLength
- 1);
187 Initiator
->IScsiNameOffset
= (UINT16
) ((UINTN
) *Heap
- (UINTN
) Table
);
191 Map the v4 IP address into v6 IP address.
193 @param V4 The v4 IP address.
195 @param V6 The v6 IP address.
203 IN EFI_IPv4_ADDRESS
*V4
,
204 OUT EFI_IPv6_ADDRESS
*V6
209 ZeroMem (V6
, sizeof (EFI_IPv6_ADDRESS
));
214 for (Index
= 0; Index
< 4; Index
++) {
215 V6
->Addr
[12 + Index
] = V4
->Addr
[Index
];
220 Get the NIC's PCI location and return it accroding to the composited
221 format defined in iSCSI Boot Firmware Table.
223 @param Controller[in] The handle of the controller.
225 @retval UINT16 The composited representation of the NIC PCI location.
230 IScsiGetNICPciLocation (
231 IN EFI_HANDLE Controller
235 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
236 EFI_HANDLE PciIoHandle
;
237 EFI_PCI_IO_PROTOCOL
*PciIo
;
243 Status
= gBS
->HandleProtocol (
245 &gEfiDevicePathProtocolGuid
,
248 if (EFI_ERROR (Status
)) {
252 Status
= gBS
->LocateDevicePath (
253 &gEfiPciIoProtocolGuid
,
257 if (EFI_ERROR (Status
)) {
261 Status
= gBS
->HandleProtocol (PciIoHandle
, &gEfiPciIoProtocolGuid
, (VOID
**)&PciIo
);
262 if (EFI_ERROR (Status
)) {
266 Status
= PciIo
->GetLocation (PciIo
, &Segment
, &Bus
, &Device
, &Function
);
267 if (EFI_ERROR (Status
)) {
271 return (UINT16
) ((Bus
<< 8) | (Device
<< 3) | Function
);
275 Get the MAC address of the controller.
277 @param Controller[in] The handle of the controller.
279 @retval EFI_MAC_ADDRESS * The mac address.
285 IN EFI_HANDLE Controller
289 EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
;
291 Status
= gBS
->HandleProtocol (
293 &gEfiSimpleNetworkProtocolGuid
,
296 ASSERT_EFI_ERROR (Status
);
298 return &Snp
->Mode
->PermanentAddress
;
302 Fill the NIC and target sections in iSCSI Boot Firmware Table.
304 @param Table[in] The buffer of the ACPI table.
306 @param Heap[in][out] The heap buffer used to store the variable length parameters such as iSCSI name.
308 @param HandleCount[in] The number of handles having iSCSI private protocol installed.
310 @param Handles[in] The handle buffer.
317 IScsiFillNICAndTargetSections (
318 IN EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_HEADER
*Table
,
320 IN UINTN HandleCount
,
321 IN EFI_HANDLE
*Handles
324 EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_CONTROL_STRUCTURE
*Control
;
325 EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_NIC_STRUCTURE
*Nic
;
326 EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_TARGET_STRUCTURE
*Target
;
327 ISCSI_DRIVER_DATA
*DriverData
;
328 ISCSI_SESSION_CONFIG_DATA
*SessionConfigData
;
329 ISCSI_CHAP_AUTH_CONFIG_NVDATA
*AuthConfig
;
330 UINT16
*SectionOffset
;
333 EFI_MAC_ADDRESS
*Mac
;
334 ISCSI_PRIVATE_PROTOCOL
*IScsiIdentifier
;
338 // Get the offset of the first Nic and Target section.
340 Control
= (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_CONTROL_STRUCTURE
*) (Table
+ 1);
341 Nic
= (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_NIC_STRUCTURE
*) ((UINTN
) Table
+
342 Control
->InitiatorOffset
+ IBFT_ROUNDUP (sizeof (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_INITIATOR_STRUCTURE
)));
343 Target
= (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_TARGET_STRUCTURE
*) ((UINTN
) Nic
+
344 IBFT_ROUNDUP (sizeof (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_NIC_STRUCTURE
)));
346 SectionOffset
= &Control
->NIC0Offset
;
348 for (Index
= 0; Index
< HandleCount
; Index
++) {
349 Status
= gBS
->HandleProtocol (Handles
[Index
], &mIScsiPrivateGuid
, (VOID
**)&IScsiIdentifier
);
350 if (EFI_ERROR (Status
)) {
355 DriverData
= ISCSI_DRIVER_DATA_FROM_IDENTIFIER (IScsiIdentifier
);
356 SessionConfigData
= &DriverData
->Session
.ConfigData
;
357 AuthConfig
= &DriverData
->Session
.AuthData
.AuthConfig
;
360 // Fill the Nic section.
362 ZeroMem (Nic
, sizeof (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_NIC_STRUCTURE
));
364 Nic
->Header
.StructureId
= EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_NIC_STRUCTURE_ID
;
365 Nic
->Header
.Version
= EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_NIC_STRUCTURE_VERSION
;
366 Nic
->Header
.Length
= sizeof (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_NIC_STRUCTURE
);
367 Nic
->Header
.Index
= (UINT8
) Index
;
368 Nic
->Header
.Flags
= EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_NIC_STRUCTURE_FLAG_BLOCK_VALID
|
369 EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_NIC_STRUCTURE_FLAG_BOOT_SELECTED
|
370 EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_NIC_STRUCTURE_FLAG_GLOBAL
;
373 // Get the subnet mask prefix length.
375 Nic
->SubnetMaskPrefixLength
= IScsiGetSubnetMaskPrefixLength (&SessionConfigData
->NvData
.SubnetMask
);
377 if (SessionConfigData
->NvData
.InitiatorInfoFromDhcp
) {
378 Nic
->Origin
= IpPrefixOriginDhcp
;
380 Nic
->Origin
= IpPrefixOriginManual
;
383 // Map the various v4 addresses into v6 addresses.
385 IScsiMapV4ToV6Addr (&SessionConfigData
->NvData
.LocalIp
, &Nic
->Ip
);
386 IScsiMapV4ToV6Addr (&SessionConfigData
->NvData
.Gateway
, &Nic
->Gateway
);
387 IScsiMapV4ToV6Addr (&SessionConfigData
->PrimaryDns
, &Nic
->PrimaryDns
);
388 IScsiMapV4ToV6Addr (&SessionConfigData
->SecondaryDns
, &Nic
->SecondaryDns
);
389 IScsiMapV4ToV6Addr (&SessionConfigData
->DhcpServer
, &Nic
->DhcpServer
);
391 Mac
= IScsiGetMacAddress (DriverData
->Controller
);
392 CopyMem (Nic
->Mac
, Mac
, sizeof (Nic
->Mac
));
395 // Get the PCI location of the Nic.
397 Nic
->PciLocation
= IScsiGetNICPciLocation (DriverData
->Controller
);
399 *SectionOffset
= (UINT16
) ((UINTN
) Nic
- (UINTN
) Table
);
403 // Fill the Target section.
405 ZeroMem (Target
, sizeof (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_TARGET_STRUCTURE
));
407 Target
->Header
.StructureId
= EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_TARGET_STRUCTURE_ID
;
408 Target
->Header
.Version
= EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_TARGET_STRUCTURE_VERSION
;
409 Target
->Header
.Length
= sizeof (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_TARGET_STRUCTURE
);
410 Target
->Header
.Index
= (UINT8
) Index
;
411 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
;
412 Target
->Port
= SessionConfigData
->NvData
.TargetPort
;
413 Target
->CHAPType
= AuthConfig
->CHAPType
;
414 Target
->NicIndex
= (UINT8
) Index
;
416 IScsiMapV4ToV6Addr (&SessionConfigData
->NvData
.TargetIp
, &Target
->Ip
);
417 CopyMem (Target
->BootLun
, SessionConfigData
->NvData
.BootLun
, sizeof (Target
->BootLun
));
420 // Target iSCSI Name, CHAP name/secret, reverse CHAP name/secret.
422 Length
= (UINT16
) AsciiStrLen (SessionConfigData
->NvData
.TargetName
);
423 IScsiAddHeapItem (Heap
, SessionConfigData
->NvData
.TargetName
, Length
);
425 Target
->IScsiNameLength
= Length
;
426 Target
->IScsiNameOffset
= (UINT16
) ((UINTN
) *Heap
- (UINTN
) Table
);
428 if (Target
->CHAPType
!= ISCSI_CHAP_NONE
) {
432 Length
= (UINT16
) AsciiStrLen (AuthConfig
->CHAPName
);
433 IScsiAddHeapItem (Heap
, AuthConfig
->CHAPName
, Length
);
434 Target
->CHAPNameLength
= Length
;
435 Target
->CHAPNameOffset
= (UINT16
) ((UINTN
) *Heap
- (UINTN
) Table
);
440 Length
= (UINT16
) AsciiStrLen (AuthConfig
->CHAPSecret
);
441 IScsiAddHeapItem (Heap
, AuthConfig
->CHAPSecret
, Length
);
442 Target
->CHAPSecretLength
= Length
;
443 Target
->CHAPSecretOffset
= (UINT16
) ((UINTN
) *Heap
- (UINTN
) Table
);
445 if (Target
->CHAPType
== ISCSI_CHAP_MUTUAL
) {
449 Length
= (UINT16
) AsciiStrLen (AuthConfig
->ReverseCHAPName
);
450 IScsiAddHeapItem (Heap
, AuthConfig
->ReverseCHAPName
, Length
);
451 Target
->ReverseCHAPNameLength
= Length
;
452 Target
->ReverseCHAPNameOffset
= (UINT16
) ((UINTN
) *Heap
- (UINTN
) Table
);
455 // Reverse CHAP Secret
457 Length
= (UINT16
) AsciiStrLen (AuthConfig
->ReverseCHAPSecret
);
458 IScsiAddHeapItem (Heap
, AuthConfig
->ReverseCHAPSecret
, Length
);
459 Target
->ReverseCHAPSecretLength
= Length
;
460 Target
->ReverseCHAPSecretOffset
= (UINT16
) ((UINTN
) *Heap
- (UINTN
) Table
);
464 *SectionOffset
= (UINT16
) ((UINTN
) Target
- (UINTN
) Table
);
468 // Advance to the next NIC/Target pair
470 Nic
= (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_NIC_STRUCTURE
*) ((UINTN
) Target
+
471 IBFT_ROUNDUP (sizeof (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_TARGET_STRUCTURE
)));
472 Target
= (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_TARGET_STRUCTURE
*) ((UINTN
) Nic
+
473 IBFT_ROUNDUP (sizeof (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_NIC_STRUCTURE
)));
478 Publish and remove the iSCSI Boot Firmware Table according to the iSCSI
493 EFI_ACPI_SUPPORT_PROTOCOL
*AcpiSupport
;
494 EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_HEADER
*Table
;
496 EFI_HANDLE
*HandleBuffer
;
499 EFI_ACPI_TABLE_VERSION Version
;
502 Status
= gBS
->LocateProtocol (&gEfiAcpiSupportProtocolGuid
, NULL
, (VOID
**)&AcpiSupport
);
503 if (EFI_ERROR (Status
)) {
507 // Try to remove the old iSCSI Boot Firmware Table.
509 for (Index
= 0;; Index
++) {
510 Status
= AcpiSupport
->GetAcpiTable (
517 if (EFI_ERROR (Status
)) {
521 Signature
= Table
->Signature
;
522 gBS
->FreePool (Table
);
524 if (Signature
== EFI_ACPI_3_0_ISCSI_BOOT_FIRMWARE_TABLE_SIGNATURE
) {
528 Status
= AcpiSupport
->SetAcpiTable (
535 if (EFI_ERROR (Status
)) {
543 // Get all iSCSI private protocols.
545 Status
= gBS
->LocateHandleBuffer (
552 if (EFI_ERROR (Status
)) {
556 // Allocate 4k bytes to hold the ACPI table.
558 Table
= AllocatePool (IBFT_MAX_SIZE
);
563 Heap
= (UINT8
*) Table
+ IBFT_HEAP_OFFSET
;
566 // Fill in the various section of the iSCSI Boot Firmware Table.
568 IScsiInitIbfTableHeader (Table
);
569 IScsiInitControlSection (Table
, HandleCount
);
570 IScsiFillInitiatorSection (Table
, &Heap
, HandleBuffer
[0]);
571 IScsiFillNICAndTargetSections (Table
, &Heap
, HandleCount
, HandleBuffer
);
573 gBS
->FreePool (HandleBuffer
);
578 // Install or update the iBFT table.
580 Status
= AcpiSupport
->SetAcpiTable (
584 EFI_ACPI_TABLE_VERSION_3_0
,
587 if (!EFI_ERROR (Status
)) {
588 AcpiSupport
->PublishTables (AcpiSupport
, EFI_ACPI_TABLE_VERSION_3_0
);
591 gBS
->FreePool (Table
);