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
,
32 ZeroMem (Header
, sizeof (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_HEADER
));
34 Header
->Signature
= EFI_ACPI_3_0_ISCSI_BOOT_FIRMWARE_TABLE_SIGNATURE
;
35 Header
->Length
= IBFT_HEAP_OFFSET
;
36 Header
->Revision
= EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_REVISION
;
39 Header
->OemId
[0] = 'I';
40 Header
->OemId
[1] = 'N';
41 Header
->OemId
[2] = 'T';
42 Header
->OemId
[3] = 'E';
43 Header
->OemId
[4] = 'L';
45 CopyMem (Header
->OemId
, OemId
, sizeof (Header
->OemId
));
46 Header
->OemTableId
= *OemTableId
;
50 Initialize the control section of the iSCSI Boot Firmware Table.
52 @param[in] Table The ACPI table.
53 @param[in] HandleCount The number of the handles associated with iSCSI sessions, it's
54 equal to the number of iSCSI sessions.
57 IScsiInitControlSection (
58 IN EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_HEADER
*Table
,
62 EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_CONTROL_STRUCTURE
*Control
;
65 Control
= (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_CONTROL_STRUCTURE
*) (Table
+ 1);
67 ZeroMem (Control
, sizeof (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_CONTROL_STRUCTURE
));
69 Control
->Header
.StructureId
= EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_CONTROL_STRUCTURE_ID
;
70 Control
->Header
.Version
= EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_CONTROL_STRUCTURE_VERSION
;
71 Control
->Header
.Length
= sizeof (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_CONTROL_STRUCTURE
);
74 // Each session occupies two offsets, one for the NIC section,
75 // the other for the Target section.
77 NumOffset
= 2 * HandleCount
;
80 // Need expand the control section if more than 2 NIC/Target sections
83 Control
->Header
.Length
= (UINT16
) (Control
->Header
.Length
+ (NumOffset
- 4) * sizeof (UINT16
));
88 Add one item into the heap.
90 @param[in, out] Heap On input, the current address of the heap; On output, the address of
91 the heap after the item is added.
92 @param[in] Data The data to add into the heap.
93 @param[in] Len Length of the Data in byte.
103 // Add one byte for the NULL delimiter.
107 CopyMem (*Heap
, Data
, Len
);
112 Fill the Initiator section of the iSCSI Boot Firmware Table.
114 @param[in] Table The ACPI table.
115 @param[in, out] Heap The heap.
116 @param[in] Handle The handle associated with the iSCSI session.
119 IScsiFillInitiatorSection (
120 IN EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_HEADER
*Table
,
125 EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_CONTROL_STRUCTURE
*Control
;
126 EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_INITIATOR_STRUCTURE
*Initiator
;
127 ISCSI_DRIVER_DATA
*DriverData
;
128 ISCSI_SESSION
*Session
;
129 ISCSI_PRIVATE_PROTOCOL
*IScsiIdentifier
;
132 Control
= (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_CONTROL_STRUCTURE
*) (Table
+ 1);
135 // Initiator section immediately follows the control section.
137 Initiator
= (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_INITIATOR_STRUCTURE
*) ((UINT8
*) Control
+ IBFT_ROUNDUP (Control
->Header
.Length
));
139 Control
->InitiatorOffset
= (UINT16
) ((UINTN
) Initiator
- (UINTN
) Table
);
141 ZeroMem (Initiator
, sizeof (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_INITIATOR_STRUCTURE
));
143 Initiator
->Header
.StructureId
= EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_INITIATOR_STRUCTURE_ID
;
144 Initiator
->Header
.Version
= EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_INITIATOR_STRUCTURE_VERSION
;
145 Initiator
->Header
.Length
= sizeof (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_INITIATOR_STRUCTURE
);
146 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
;
149 // Get the identifier from the handle.
151 Status
= gBS
->HandleProtocol (Handle
, &gIScsiPrivateGuid
, (VOID
**) &IScsiIdentifier
);
152 if (EFI_ERROR (Status
)) {
157 DriverData
= ISCSI_DRIVER_DATA_FROM_IDENTIFIER (IScsiIdentifier
);
158 Session
= &DriverData
->Session
;
161 // Fill the iSCSI Initiator Name into the heap.
163 IScsiAddHeapItem (Heap
, Session
->InitiatorName
, Session
->InitiatorNameLength
- 1);
165 Initiator
->IScsiNameLength
= (UINT16
) (Session
->InitiatorNameLength
- 1);
166 Initiator
->IScsiNameOffset
= (UINT16
) ((UINTN
) *Heap
- (UINTN
) Table
);
170 Map the v4 IP address into v6 IP address.
172 @param[in] V4 The v4 IP address.
173 @param[out] V6 The v6 IP address.
177 IN EFI_IPv4_ADDRESS
*V4
,
178 OUT EFI_IPv6_ADDRESS
*V6
183 ZeroMem (V6
, sizeof (EFI_IPv6_ADDRESS
));
188 for (Index
= 0; Index
< 4; Index
++) {
189 V6
->Addr
[12 + Index
] = V4
->Addr
[Index
];
194 Get the NIC's PCI location and return it accroding to the composited
195 format defined in iSCSI Boot Firmware Table.
197 @param[in] Controller The handle of the controller.
199 @return UINT16 The composited representation of the NIC PCI location.
200 @retval 0 Other errors as indicated.
203 IScsiGetNICPciLocation (
204 IN EFI_HANDLE Controller
208 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
209 EFI_HANDLE PciIoHandle
;
210 EFI_PCI_IO_PROTOCOL
*PciIo
;
216 Status
= gBS
->HandleProtocol (
218 &gEfiDevicePathProtocolGuid
,
221 if (EFI_ERROR (Status
)) {
225 Status
= gBS
->LocateDevicePath (
226 &gEfiPciIoProtocolGuid
,
230 if (EFI_ERROR (Status
)) {
234 Status
= gBS
->HandleProtocol (PciIoHandle
, &gEfiPciIoProtocolGuid
, (VOID
**)&PciIo
);
235 if (EFI_ERROR (Status
)) {
239 Status
= PciIo
->GetLocation (PciIo
, &Segment
, &Bus
, &Device
, &Function
);
240 if (EFI_ERROR (Status
)) {
244 return (UINT16
) ((Bus
<< 8) | (Device
<< 3) | Function
);
248 Get the MAC address of the controller.
250 @param[in] Controller The handle of the controller.
252 @return EFI_MAC_ADDRESS * The mac address.
256 IN EFI_HANDLE Controller
260 EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
;
262 Status
= gBS
->HandleProtocol (
264 &gEfiSimpleNetworkProtocolGuid
,
267 ASSERT_EFI_ERROR (Status
);
269 return &Snp
->Mode
->PermanentAddress
;
273 Fill the NIC and target sections in iSCSI Boot Firmware Table.
275 @param[in] Table The buffer of the ACPI table.
276 @param[in, out] Heap The heap buffer used to store the variable length parameters such as iSCSI name.
277 @param[in] HandleCount Count The number of handles having iSCSI private protocol installed.
278 @param[in] Handles The handle buffer.
281 IScsiFillNICAndTargetSections (
282 IN EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_HEADER
*Table
,
284 IN UINTN HandleCount
,
285 IN EFI_HANDLE
*Handles
288 EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_CONTROL_STRUCTURE
*Control
;
289 EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_NIC_STRUCTURE
*Nic
;
290 EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_TARGET_STRUCTURE
*Target
;
291 ISCSI_DRIVER_DATA
*DriverData
;
292 ISCSI_SESSION_CONFIG_DATA
*SessionConfigData
;
293 ISCSI_CHAP_AUTH_CONFIG_NVDATA
*AuthConfig
;
294 UINT16
*SectionOffset
;
297 EFI_MAC_ADDRESS
*Mac
;
298 ISCSI_PRIVATE_PROTOCOL
*IScsiIdentifier
;
302 // Get the offset of the first Nic and Target section.
304 Control
= (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_CONTROL_STRUCTURE
*) (Table
+ 1);
305 Nic
= (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_NIC_STRUCTURE
*) ((UINTN
) Table
+
306 Control
->InitiatorOffset
+ IBFT_ROUNDUP (sizeof (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_INITIATOR_STRUCTURE
)));
307 Target
= (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_TARGET_STRUCTURE
*) ((UINTN
) Nic
+
308 IBFT_ROUNDUP (sizeof (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_NIC_STRUCTURE
)));
310 SectionOffset
= &Control
->NIC0Offset
;
312 for (Index
= 0; Index
< HandleCount
; Index
++) {
313 Status
= gBS
->HandleProtocol (Handles
[Index
], &gIScsiPrivateGuid
, (VOID
**)&IScsiIdentifier
);
314 if (EFI_ERROR (Status
)) {
319 DriverData
= ISCSI_DRIVER_DATA_FROM_IDENTIFIER (IScsiIdentifier
);
320 SessionConfigData
= &DriverData
->Session
.ConfigData
;
321 AuthConfig
= &DriverData
->Session
.AuthData
.AuthConfig
;
324 // Fill the Nic section.
326 ZeroMem (Nic
, sizeof (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_NIC_STRUCTURE
));
328 Nic
->Header
.StructureId
= EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_NIC_STRUCTURE_ID
;
329 Nic
->Header
.Version
= EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_NIC_STRUCTURE_VERSION
;
330 Nic
->Header
.Length
= sizeof (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_NIC_STRUCTURE
);
331 Nic
->Header
.Index
= (UINT8
) Index
;
332 Nic
->Header
.Flags
= EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_NIC_STRUCTURE_FLAG_BLOCK_VALID
|
333 EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_NIC_STRUCTURE_FLAG_BOOT_SELECTED
|
334 EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_NIC_STRUCTURE_FLAG_GLOBAL
;
337 // Get the subnet mask prefix length.
339 Nic
->SubnetMaskPrefixLength
= IScsiGetSubnetMaskPrefixLength (&SessionConfigData
->NvData
.SubnetMask
);
341 if (SessionConfigData
->NvData
.InitiatorInfoFromDhcp
) {
342 Nic
->Origin
= IpPrefixOriginDhcp
;
344 Nic
->Origin
= IpPrefixOriginManual
;
347 // Map the various v4 addresses into v6 addresses.
349 IScsiMapV4ToV6Addr (&SessionConfigData
->NvData
.LocalIp
, &Nic
->Ip
);
350 IScsiMapV4ToV6Addr (&SessionConfigData
->NvData
.Gateway
, &Nic
->Gateway
);
351 IScsiMapV4ToV6Addr (&SessionConfigData
->PrimaryDns
, &Nic
->PrimaryDns
);
352 IScsiMapV4ToV6Addr (&SessionConfigData
->SecondaryDns
, &Nic
->SecondaryDns
);
353 IScsiMapV4ToV6Addr (&SessionConfigData
->DhcpServer
, &Nic
->DhcpServer
);
355 Mac
= IScsiGetMacAddress (DriverData
->Controller
);
356 CopyMem (Nic
->Mac
, Mac
, sizeof (Nic
->Mac
));
359 // Get the PCI location of the Nic.
361 Nic
->PciLocation
= IScsiGetNICPciLocation (DriverData
->Controller
);
363 *SectionOffset
= (UINT16
) ((UINTN
) Nic
- (UINTN
) Table
);
367 // Fill the Target section.
369 ZeroMem (Target
, sizeof (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_TARGET_STRUCTURE
));
371 Target
->Header
.StructureId
= EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_TARGET_STRUCTURE_ID
;
372 Target
->Header
.Version
= EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_TARGET_STRUCTURE_VERSION
;
373 Target
->Header
.Length
= sizeof (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_TARGET_STRUCTURE
);
374 Target
->Header
.Index
= (UINT8
) Index
;
375 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
;
376 Target
->Port
= SessionConfigData
->NvData
.TargetPort
;
377 Target
->CHAPType
= AuthConfig
->CHAPType
;
378 Target
->NicIndex
= (UINT8
) Index
;
380 IScsiMapV4ToV6Addr (&SessionConfigData
->NvData
.TargetIp
, &Target
->Ip
);
381 CopyMem (Target
->BootLun
, SessionConfigData
->NvData
.BootLun
, sizeof (Target
->BootLun
));
384 // Target iSCSI Name, CHAP name/secret, reverse CHAP name/secret.
386 Length
= (UINT16
) AsciiStrLen (SessionConfigData
->NvData
.TargetName
);
387 IScsiAddHeapItem (Heap
, SessionConfigData
->NvData
.TargetName
, Length
);
389 Target
->IScsiNameLength
= Length
;
390 Target
->IScsiNameOffset
= (UINT16
) ((UINTN
) *Heap
- (UINTN
) Table
);
392 if (Target
->CHAPType
!= ISCSI_CHAP_NONE
) {
396 Length
= (UINT16
) AsciiStrLen (AuthConfig
->CHAPName
);
397 IScsiAddHeapItem (Heap
, AuthConfig
->CHAPName
, Length
);
398 Target
->CHAPNameLength
= Length
;
399 Target
->CHAPNameOffset
= (UINT16
) ((UINTN
) *Heap
- (UINTN
) Table
);
404 Length
= (UINT16
) AsciiStrLen (AuthConfig
->CHAPSecret
);
405 IScsiAddHeapItem (Heap
, AuthConfig
->CHAPSecret
, Length
);
406 Target
->CHAPSecretLength
= Length
;
407 Target
->CHAPSecretOffset
= (UINT16
) ((UINTN
) *Heap
- (UINTN
) Table
);
409 if (Target
->CHAPType
== ISCSI_CHAP_MUTUAL
) {
413 Length
= (UINT16
) AsciiStrLen (AuthConfig
->ReverseCHAPName
);
414 IScsiAddHeapItem (Heap
, AuthConfig
->ReverseCHAPName
, Length
);
415 Target
->ReverseCHAPNameLength
= Length
;
416 Target
->ReverseCHAPNameOffset
= (UINT16
) ((UINTN
) *Heap
- (UINTN
) Table
);
419 // Reverse CHAP Secret
421 Length
= (UINT16
) AsciiStrLen (AuthConfig
->ReverseCHAPSecret
);
422 IScsiAddHeapItem (Heap
, AuthConfig
->ReverseCHAPSecret
, Length
);
423 Target
->ReverseCHAPSecretLength
= Length
;
424 Target
->ReverseCHAPSecretOffset
= (UINT16
) ((UINTN
) *Heap
- (UINTN
) Table
);
428 *SectionOffset
= (UINT16
) ((UINTN
) Target
- (UINTN
) Table
);
432 // Advance to the next NIC/Target pair
434 Nic
= (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_NIC_STRUCTURE
*) ((UINTN
) Target
+
435 IBFT_ROUNDUP (sizeof (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_TARGET_STRUCTURE
)));
436 Target
= (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_TARGET_STRUCTURE
*) ((UINTN
) Nic
+
437 IBFT_ROUNDUP (sizeof (EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_NIC_STRUCTURE
)));
442 Publish and remove the iSCSI Boot Firmware Table according to the iSCSI
451 EFI_ACPI_TABLE_PROTOCOL
*AcpiTableProtocol
;
452 EFI_ACPI_ISCSI_BOOT_FIRMWARE_TABLE_HEADER
*Table
;
454 EFI_HANDLE
*HandleBuffer
;
458 EFI_ACPI_3_0_ROOT_SYSTEM_DESCRIPTION_POINTER
*Rsdp
;
459 EFI_ACPI_DESCRIPTION_HEADER
*Rsdt
;
461 Status
= gBS
->LocateProtocol (&gEfiAcpiTableProtocolGuid
, NULL
, (VOID
**)&AcpiTableProtocol
);
462 if (EFI_ERROR (Status
)) {
468 // Find ACPI table RSD_PTR from system table
470 for (Index
= 0, Rsdp
= NULL
; Index
< gST
->NumberOfTableEntries
; Index
++) {
471 if (CompareGuid (&(gST
->ConfigurationTable
[Index
].VendorGuid
), &gEfiAcpi20TableGuid
) ||
472 CompareGuid (&(gST
->ConfigurationTable
[Index
].VendorGuid
), &gEfiAcpi10TableGuid
) ||
473 CompareGuid (&(gST
->ConfigurationTable
[Index
].VendorGuid
), &gEfiAcpiTableGuid
)
476 // A match was found.
478 Rsdp
= (EFI_ACPI_3_0_ROOT_SYSTEM_DESCRIPTION_POINTER
*) gST
->ConfigurationTable
[Index
].VendorTable
;
486 Rsdt
= (EFI_ACPI_DESCRIPTION_HEADER
*) (UINTN
) Rsdp
->RsdtAddress
;
490 if (mIbftInstalled
) {
491 Status
= AcpiTableProtocol
->UninstallAcpiTable (
495 if (EFI_ERROR (Status
)) {
498 mIbftInstalled
= FALSE
;
502 // Get all iSCSI private protocols.
504 Status
= gBS
->LocateHandleBuffer (
511 if (EFI_ERROR (Status
)) {
515 // Allocate 4k bytes to hold the ACPI table.
517 Table
= AllocateZeroPool (IBFT_MAX_SIZE
);
522 Heap
= (UINT8
*) Table
+ IBFT_HEAP_OFFSET
;
525 // Fill in the various section of the iSCSI Boot Firmware Table.
527 IScsiInitIbfTableHeader (Table
, Rsdt
->OemId
, &Rsdt
->OemTableId
);
528 IScsiInitControlSection (Table
, HandleCount
);
529 IScsiFillInitiatorSection (Table
, &Heap
, HandleBuffer
[0]);
530 IScsiFillNICAndTargetSections (Table
, &Heap
, HandleCount
, HandleBuffer
);
532 Checksum
= CalculateCheckSum8((UINT8
*)Table
, Table
->Length
);
533 Table
->Checksum
= Checksum
;
535 FreePool (HandleBuffer
);
538 // Install or update the iBFT table.
540 Status
= AcpiTableProtocol
->InstallAcpiTable (
546 if (EFI_ERROR(Status
)) {
550 mIbftInstalled
= TRUE
;