2 Implementation for iSCSI Boot Firmware Table publication.
4 Copyright (c) 2004 - 2009, Intel Corporation.<BR>
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.
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.
26 IScsiInitIbfTableHeader (
27 OUT EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_HEADER
*Header
30 ZeroMem (Header
, sizeof (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_HEADER
));
32 Header
->Signature
= EFI_ACPI_3_0_ISCSI_BOOT_FIRMWARE_TABLE_SIGNATURE
;
33 Header
->Length
= IBFT_HEAP_OFFSET
;
34 Header
->Revision
= EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_REVISION
;
37 Header
->OemId
[0] = 'I';
38 Header
->OemId
[1] = 'N';
39 Header
->OemId
[2] = 'T';
40 Header
->OemId
[3] = 'E';
41 Header
->OemId
[4] = 'L';
45 Initialize the control section of the iSCSI Boot Firmware Table.
47 @param[in] Table The ACPI table.
48 @param[in] HandleCount The number of the handles associated with iSCSI sessions, it's
49 equal to the number of iSCSI sessions.
52 IScsiInitControlSection (
53 IN EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_HEADER
*Table
,
57 EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_CONTROL_STRUCTURE
*Control
;
60 Control
= (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_CONTROL_STRUCTURE
*) (Table
+ 1);
62 ZeroMem (Control
, sizeof (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_CONTROL_STRUCTURE
));
64 Control
->Header
.StructureId
= EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_CONTROL_STRUCTURE_ID
;
65 Control
->Header
.Version
= EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_CONTROL_STRUCTURE_VERSION
;
66 Control
->Header
.Length
= sizeof (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_CONTROL_STRUCTURE
);
69 // Each session occupies two offsets, one for the NIC section,
70 // the other for the Target section.
72 NumOffset
= 2 * HandleCount
;
75 // Need expand the control section if more than 2 NIC/Target sections
78 Control
->Header
.Length
= (UINT16
) (Control
->Header
.Length
+ (NumOffset
- 4) * sizeof (UINT16
));
83 Add one item into the heap.
85 @param[in, out] Heap On input, the current address of the heap; On output, the address of
86 the heap after the item is added.
87 @param[in] Data The data to add into the heap.
88 @param[in] Len Length of the Data in byte.
98 // Add one byte for the NULL delimiter.
102 CopyMem (*Heap
, Data
, Len
);
107 Fill the Initiator section of the iSCSI Boot Firmware Table.
109 @param[in] Table The ACPI table.
110 @param[in, out] Heap The heap.
111 @param[in] Handle The handle associated with the iSCSI session.
114 IScsiFillInitiatorSection (
115 IN EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_HEADER
*Table
,
120 EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_CONTROL_STRUCTURE
*Control
;
121 EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_INITIATOR_STRUCTURE
*Initiator
;
122 ISCSI_DRIVER_DATA
*DriverData
;
123 ISCSI_SESSION
*Session
;
124 ISCSI_PRIVATE_PROTOCOL
*IScsiIdentifier
;
127 Control
= (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_CONTROL_STRUCTURE
*) (Table
+ 1);
130 // Initiator section immediately follows the control section.
132 Initiator
= (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_INITIATOR_STRUCTURE
*) ((UINT8
*) Control
+ IBFT_ROUNDUP (Control
->Header
.Length
));
134 Control
->InitiatorOffset
= (UINT16
) ((UINTN
) Initiator
- (UINTN
) Table
);
136 ZeroMem (Initiator
, sizeof (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_INITIATOR_STRUCTURE
));
138 Initiator
->Header
.StructureId
= EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_INITIATOR_STRUCTURE_ID
;
139 Initiator
->Header
.Version
= EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_INITIATOR_STRUCTURE_VERSION
;
140 Initiator
->Header
.Length
= sizeof (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_INITIATOR_STRUCTURE
);
141 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
;
144 // Get the identifier from the handle.
146 Status
= gBS
->HandleProtocol (Handle
, &gIScsiPrivateGuid
, (VOID
**) &IScsiIdentifier
);
147 if (EFI_ERROR (Status
)) {
152 DriverData
= ISCSI_DRIVER_DATA_FROM_IDENTIFIER (IScsiIdentifier
);
153 Session
= &DriverData
->Session
;
156 // Fill the iSCSI Initiator Name into the heap.
158 IScsiAddHeapItem (Heap
, Session
->InitiatorName
, Session
->InitiatorNameLength
- 1);
160 Initiator
->IScsiNameLength
= (UINT16
) (Session
->InitiatorNameLength
- 1);
161 Initiator
->IScsiNameOffset
= (UINT16
) ((UINTN
) *Heap
- (UINTN
) Table
);
165 Map the v4 IP address into v6 IP address.
167 @param[in] V4 The v4 IP address.
168 @param[out] V6 The v6 IP address.
172 IN EFI_IPv4_ADDRESS
*V4
,
173 OUT EFI_IPv6_ADDRESS
*V6
178 ZeroMem (V6
, sizeof (EFI_IPv6_ADDRESS
));
183 for (Index
= 0; Index
< 4; Index
++) {
184 V6
->Addr
[12 + Index
] = V4
->Addr
[Index
];
189 Get the NIC's PCI location and return it accroding to the composited
190 format defined in iSCSI Boot Firmware Table.
192 @param[in] Controller The handle of the controller.
194 @return UINT16 The composited representation of the NIC PCI location.
195 @retval 0 Other errors as indicated.
198 IScsiGetNICPciLocation (
199 IN EFI_HANDLE Controller
203 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
204 EFI_HANDLE PciIoHandle
;
205 EFI_PCI_IO_PROTOCOL
*PciIo
;
211 Status
= gBS
->HandleProtocol (
213 &gEfiDevicePathProtocolGuid
,
216 if (EFI_ERROR (Status
)) {
220 Status
= gBS
->LocateDevicePath (
221 &gEfiPciIoProtocolGuid
,
225 if (EFI_ERROR (Status
)) {
229 Status
= gBS
->HandleProtocol (PciIoHandle
, &gEfiPciIoProtocolGuid
, (VOID
**)&PciIo
);
230 if (EFI_ERROR (Status
)) {
234 Status
= PciIo
->GetLocation (PciIo
, &Segment
, &Bus
, &Device
, &Function
);
235 if (EFI_ERROR (Status
)) {
239 return (UINT16
) ((Bus
<< 8) | (Device
<< 3) | Function
);
243 Get the MAC address of the controller.
245 @param[in] Controller The handle of the controller.
247 @return EFI_MAC_ADDRESS * The mac address.
251 IN EFI_HANDLE Controller
255 EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
;
257 Status
= gBS
->HandleProtocol (
259 &gEfiSimpleNetworkProtocolGuid
,
262 ASSERT_EFI_ERROR (Status
);
264 return &Snp
->Mode
->PermanentAddress
;
268 Fill the NIC and target sections in iSCSI Boot Firmware Table.
270 @param[in] Table The buffer of the ACPI table.
271 @param[in, out] Heap The heap buffer used to store the variable length parameters such as iSCSI name.
272 @param[in] HandleCount Count The number of handles having iSCSI private protocol installed.
273 @param[in] Handles The handle buffer.
276 IScsiFillNICAndTargetSections (
277 IN EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_HEADER
*Table
,
279 IN UINTN HandleCount
,
280 IN EFI_HANDLE
*Handles
283 EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_CONTROL_STRUCTURE
*Control
;
284 EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_NIC_STRUCTURE
*Nic
;
285 EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_TARGET_STRUCTURE
*Target
;
286 ISCSI_DRIVER_DATA
*DriverData
;
287 ISCSI_SESSION_CONFIG_DATA
*SessionConfigData
;
288 ISCSI_CHAP_AUTH_CONFIG_NVDATA
*AuthConfig
;
289 UINT16
*SectionOffset
;
292 EFI_MAC_ADDRESS
*Mac
;
293 ISCSI_PRIVATE_PROTOCOL
*IScsiIdentifier
;
297 // Get the offset of the first Nic and Target section.
299 Control
= (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_CONTROL_STRUCTURE
*) (Table
+ 1);
300 Nic
= (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_NIC_STRUCTURE
*) ((UINTN
) Table
+
301 Control
->InitiatorOffset
+ IBFT_ROUNDUP (sizeof (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_INITIATOR_STRUCTURE
)));
302 Target
= (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_TARGET_STRUCTURE
*) ((UINTN
) Nic
+
303 IBFT_ROUNDUP (sizeof (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_NIC_STRUCTURE
)));
305 SectionOffset
= &Control
->NIC0Offset
;
307 for (Index
= 0; Index
< HandleCount
; Index
++) {
308 Status
= gBS
->HandleProtocol (Handles
[Index
], &gIScsiPrivateGuid
, (VOID
**)&IScsiIdentifier
);
309 if (EFI_ERROR (Status
)) {
314 DriverData
= ISCSI_DRIVER_DATA_FROM_IDENTIFIER (IScsiIdentifier
);
315 SessionConfigData
= &DriverData
->Session
.ConfigData
;
316 AuthConfig
= &DriverData
->Session
.AuthData
.AuthConfig
;
319 // Fill the Nic section.
321 ZeroMem (Nic
, sizeof (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_NIC_STRUCTURE
));
323 Nic
->Header
.StructureId
= EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_NIC_STRUCTURE_ID
;
324 Nic
->Header
.Version
= EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_NIC_STRUCTURE_VERSION
;
325 Nic
->Header
.Length
= sizeof (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_NIC_STRUCTURE
);
326 Nic
->Header
.Index
= (UINT8
) Index
;
327 Nic
->Header
.Flags
= EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_NIC_STRUCTURE_FLAG_BLOCK_VALID
|
328 EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_NIC_STRUCTURE_FLAG_BOOT_SELECTED
|
329 EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_NIC_STRUCTURE_FLAG_GLOBAL
;
332 // Get the subnet mask prefix length.
334 Nic
->SubnetMaskPrefixLength
= IScsiGetSubnetMaskPrefixLength (&SessionConfigData
->NvData
.SubnetMask
);
336 if (SessionConfigData
->NvData
.InitiatorInfoFromDhcp
) {
337 Nic
->Origin
= IpPrefixOriginDhcp
;
339 Nic
->Origin
= IpPrefixOriginManual
;
342 // Map the various v4 addresses into v6 addresses.
344 IScsiMapV4ToV6Addr (&SessionConfigData
->NvData
.LocalIp
, &Nic
->Ip
);
345 IScsiMapV4ToV6Addr (&SessionConfigData
->NvData
.Gateway
, &Nic
->Gateway
);
346 IScsiMapV4ToV6Addr (&SessionConfigData
->PrimaryDns
, &Nic
->PrimaryDns
);
347 IScsiMapV4ToV6Addr (&SessionConfigData
->SecondaryDns
, &Nic
->SecondaryDns
);
348 IScsiMapV4ToV6Addr (&SessionConfigData
->DhcpServer
, &Nic
->DhcpServer
);
350 Mac
= IScsiGetMacAddress (DriverData
->Controller
);
351 CopyMem (Nic
->Mac
, Mac
, sizeof (Nic
->Mac
));
354 // Get the PCI location of the Nic.
356 Nic
->PciLocation
= IScsiGetNICPciLocation (DriverData
->Controller
);
358 *SectionOffset
= (UINT16
) ((UINTN
) Nic
- (UINTN
) Table
);
362 // Fill the Target section.
364 ZeroMem (Target
, sizeof (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_TARGET_STRUCTURE
));
366 Target
->Header
.StructureId
= EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_TARGET_STRUCTURE_ID
;
367 Target
->Header
.Version
= EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_TARGET_STRUCTURE_VERSION
;
368 Target
->Header
.Length
= sizeof (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_TARGET_STRUCTURE
);
369 Target
->Header
.Index
= (UINT8
) Index
;
370 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
;
371 Target
->Port
= SessionConfigData
->NvData
.TargetPort
;
372 Target
->CHAPType
= AuthConfig
->CHAPType
;
373 Target
->NicIndex
= (UINT8
) Index
;
375 IScsiMapV4ToV6Addr (&SessionConfigData
->NvData
.TargetIp
, &Target
->Ip
);
376 CopyMem (Target
->BootLun
, SessionConfigData
->NvData
.BootLun
, sizeof (Target
->BootLun
));
379 // Target iSCSI Name, CHAP name/secret, reverse CHAP name/secret.
381 Length
= (UINT16
) AsciiStrLen (SessionConfigData
->NvData
.TargetName
);
382 IScsiAddHeapItem (Heap
, SessionConfigData
->NvData
.TargetName
, Length
);
384 Target
->IScsiNameLength
= Length
;
385 Target
->IScsiNameOffset
= (UINT16
) ((UINTN
) *Heap
- (UINTN
) Table
);
387 if (Target
->CHAPType
!= ISCSI_CHAP_NONE
) {
391 Length
= (UINT16
) AsciiStrLen (AuthConfig
->CHAPName
);
392 IScsiAddHeapItem (Heap
, AuthConfig
->CHAPName
, Length
);
393 Target
->CHAPNameLength
= Length
;
394 Target
->CHAPNameOffset
= (UINT16
) ((UINTN
) *Heap
- (UINTN
) Table
);
399 Length
= (UINT16
) AsciiStrLen (AuthConfig
->CHAPSecret
);
400 IScsiAddHeapItem (Heap
, AuthConfig
->CHAPSecret
, Length
);
401 Target
->CHAPSecretLength
= Length
;
402 Target
->CHAPSecretOffset
= (UINT16
) ((UINTN
) *Heap
- (UINTN
) Table
);
404 if (Target
->CHAPType
== ISCSI_CHAP_MUTUAL
) {
408 Length
= (UINT16
) AsciiStrLen (AuthConfig
->ReverseCHAPName
);
409 IScsiAddHeapItem (Heap
, AuthConfig
->ReverseCHAPName
, Length
);
410 Target
->ReverseCHAPNameLength
= Length
;
411 Target
->ReverseCHAPNameOffset
= (UINT16
) ((UINTN
) *Heap
- (UINTN
) Table
);
414 // Reverse CHAP Secret
416 Length
= (UINT16
) AsciiStrLen (AuthConfig
->ReverseCHAPSecret
);
417 IScsiAddHeapItem (Heap
, AuthConfig
->ReverseCHAPSecret
, Length
);
418 Target
->ReverseCHAPSecretLength
= Length
;
419 Target
->ReverseCHAPSecretOffset
= (UINT16
) ((UINTN
) *Heap
- (UINTN
) Table
);
423 *SectionOffset
= (UINT16
) ((UINTN
) Target
- (UINTN
) Table
);
427 // Advance to the next NIC/Target pair
429 Nic
= (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_NIC_STRUCTURE
*) ((UINTN
) Target
+
430 IBFT_ROUNDUP (sizeof (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_TARGET_STRUCTURE
)));
431 Target
= (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_TARGET_STRUCTURE
*) ((UINTN
) Nic
+
432 IBFT_ROUNDUP (sizeof (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_NIC_STRUCTURE
)));
437 Publish and remove the iSCSI Boot Firmware Table according to the iSCSI
446 EFI_ACPI_TABLE_PROTOCOL
*AcpiTableProtocol
;
447 EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_HEADER
*Table
;
449 EFI_HANDLE
*HandleBuffer
;
452 Status
= gBS
->LocateProtocol (&gEfiAcpiTableProtocolGuid
, NULL
, (VOID
**)&AcpiTableProtocol
);
453 if (EFI_ERROR (Status
)) {
457 if (mIbftInstalled
) {
458 Status
= AcpiTableProtocol
->UninstallAcpiTable (
462 if (EFI_ERROR (Status
)) {
465 mIbftInstalled
= FALSE
;
469 // Get all iSCSI private protocols.
471 Status
= gBS
->LocateHandleBuffer (
478 if (EFI_ERROR (Status
)) {
482 // Allocate 4k bytes to hold the ACPI table.
484 Table
= AllocatePool (IBFT_MAX_SIZE
);
489 Heap
= (UINT8
*) Table
+ IBFT_HEAP_OFFSET
;
492 // Fill in the various section of the iSCSI Boot Firmware Table.
494 IScsiInitIbfTableHeader (Table
);
495 IScsiInitControlSection (Table
, HandleCount
);
496 IScsiFillInitiatorSection (Table
, &Heap
, HandleBuffer
[0]);
497 IScsiFillNICAndTargetSections (Table
, &Heap
, HandleCount
, HandleBuffer
);
499 FreePool (HandleBuffer
);
502 // Install or update the iBFT table.
504 Status
= AcpiTableProtocol
->InstallAcpiTable (
510 if (EFI_ERROR(Status
)) {
514 mIbftInstalled
= TRUE
;