summaryrefslogtreecommitdiffstats
path: root/src/arch
diff options
context:
space:
mode:
authorMichael Brown2008-11-19 04:16:42 +0100
committerMichael Brown2008-11-19 20:20:45 +0100
commitd9b3d0991091b8dd568b17221e335ecf81beb02f (patch)
tree79553bf0b4a40064506d13a730767266c938eec6 /src/arch
parent[i386] Change [u]int32_t to [unsigned] int, rather than [unsigned] long (diff)
downloadipxe-d9b3d0991091b8dd568b17221e335ecf81beb02f.tar.gz
ipxe-d9b3d0991091b8dd568b17221e335ecf81beb02f.tar.xz
ipxe-d9b3d0991091b8dd568b17221e335ecf81beb02f.zip
[i386] Move iSCSI and AoE boot code to arch/i386/interface/pcbios
Diffstat (limited to 'src/arch')
-rw-r--r--src/arch/i386/include/gpxe/abft.h35
-rw-r--r--src/arch/i386/include/gpxe/ibft.h300
-rw-r--r--src/arch/i386/interface/pcbios/abft.c60
-rw-r--r--src/arch/i386/interface/pcbios/aoeboot.c79
-rw-r--r--src/arch/i386/interface/pcbios/ibft.c394
-rw-r--r--src/arch/i386/interface/pcbios/iscsiboot.c108
6 files changed, 976 insertions, 0 deletions
diff --git a/src/arch/i386/include/gpxe/abft.h b/src/arch/i386/include/gpxe/abft.h
new file mode 100644
index 000000000..1c651ef11
--- /dev/null
+++ b/src/arch/i386/include/gpxe/abft.h
@@ -0,0 +1,35 @@
+#ifndef _GPXE_ABFT_H
+#define _GPXE_ABFT_H
+
+/** @file
+ *
+ * AoE boot firmware table
+ *
+ */
+
+#include <stdint.h>
+#include <gpxe/acpi.h>
+#include <gpxe/if_ether.h>
+
+/** AoE boot firmware table signature */
+#define ABFT_SIG "aBFT"
+
+/**
+ * AoE Boot Firmware Table (aBFT)
+ */
+struct abft_table {
+ /** ACPI header */
+ struct acpi_description_header acpi;
+ /** AoE shelf */
+ uint16_t shelf;
+ /** AoE slot */
+ uint8_t slot;
+ /** Reserved */
+ uint8_t reserved_a;
+ /** MAC address */
+ uint8_t mac[ETH_ALEN];
+} __attribute__ (( packed ));
+
+extern void abft_fill_data ( struct aoe_session *aoe );
+
+#endif /* _GPXE_ABFT_H */
diff --git a/src/arch/i386/include/gpxe/ibft.h b/src/arch/i386/include/gpxe/ibft.h
new file mode 100644
index 000000000..5eef547b7
--- /dev/null
+++ b/src/arch/i386/include/gpxe/ibft.h
@@ -0,0 +1,300 @@
+#ifndef _GPXE_IBFT_H
+#define _GPXE_IBFT_H
+
+/*
+ * Copyright Fen Systems Ltd. 2007. Portions of this code are derived
+ * from IBM Corporation Sample Programs. Copyright IBM Corporation
+ * 2004, 2007. All rights reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person
+ * obtaining a copy of this software and associated documentation
+ * files (the "Software"), to deal in the Software without
+ * restriction, including without limitation the rights to use, copy,
+ * modify, merge, publish, distribute, sublicense, and/or sell copies
+ * of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ *
+ */
+
+/** @file
+ *
+ * iSCSI boot firmware table
+ *
+ * The information in this file is derived from the document "iSCSI
+ * Boot Firmware Table (iBFT)" as published by IBM at
+ *
+ * ftp://ftp.software.ibm.com/systems/support/system_x_pdf/ibm_iscsi_boot_firmware_table_v1.02.pdf
+ *
+ */
+
+#include <stdint.h>
+#include <gpxe/acpi.h>
+#include <gpxe/in.h>
+
+/** iSCSI Boot Firmware Table signature */
+#define IBFT_SIG "iBFT"
+
+/** An offset from the start of the iBFT */
+typedef uint16_t ibft_off_t;
+
+/** Length of a string within the iBFT (excluding terminating NUL) */
+typedef uint16_t ibft_size_t;
+
+/** A string within the iBFT */
+struct ibft_string {
+ /** Length of string */
+ ibft_size_t length;
+ /** Offset to string */
+ ibft_off_t offset;
+} __attribute__ (( packed ));
+
+/** An IP address within the iBFT */
+struct ibft_ipaddr {
+ /** Reserved; must be zero */
+ uint16_t zeroes[5];
+ /** Must be 0xffff if IPv4 address is present, otherwise zero */
+ uint16_t ones;
+ /** The IPv4 address, or zero if not present */
+ struct in_addr in;
+} __attribute__ (( packed ));
+
+/**
+ * iBFT structure header
+ *
+ * This structure is common to several sections within the iBFT.
+ */
+struct ibft_header {
+ /** Structure ID
+ *
+ * This is an IBFT_STRUCTURE_ID_XXX constant
+ */
+ uint8_t structure_id;
+ /** Version (always 1) */
+ uint8_t version;
+ /** Length, including this header */
+ uint16_t length;
+ /** Index
+ *
+ * This is the number of the NIC or Target, when applicable.
+ */
+ uint8_t index;
+ /** Flags */
+ uint8_t flags;
+} __attribute__ (( packed ));
+
+/**
+ * iBFT Control structure
+ *
+ */
+struct ibft_control {
+ /** Common header */
+ struct ibft_header header;
+ /** Extensions */
+ uint16_t extensions;
+ /** Offset to Initiator structure */
+ ibft_off_t initiator;
+ /** Offset to NIC structure for NIC 0 */
+ ibft_off_t nic_0;
+ /** Offset to Target structure for target 0 */
+ ibft_off_t target_0;
+ /** Offset to NIC structure for NIC 1 */
+ ibft_off_t nic_1;
+ /** Offset to Target structure for target 1 */
+ ibft_off_t target_1;
+} __attribute__ (( packed ));
+
+/** Structure ID for Control section */
+#define IBFT_STRUCTURE_ID_CONTROL 0x01
+
+/** Attempt login only to specified target
+ *
+ * If this flag is not set, all targets will be logged in to.
+ */
+#define IBFT_FL_CONTROL_SINGLE_LOGIN_ONLY 0x01
+
+/**
+ * iBFT Initiator structure
+ *
+ */
+struct ibft_initiator {
+ /** Common header */
+ struct ibft_header header;
+ /** iSNS server */
+ struct ibft_ipaddr isns_server;
+ /** SLP server */
+ struct ibft_ipaddr slp_server;
+ /** Primary and secondary Radius servers */
+ struct ibft_ipaddr radius[2];
+ /** Initiator name */
+ struct ibft_string initiator_name;
+} __attribute__ (( packed ));
+
+/** Structure ID for Initiator section */
+#define IBFT_STRUCTURE_ID_INITIATOR 0x02
+
+/** Initiator block valid */
+#define IBFT_FL_INITIATOR_BLOCK_VALID 0x01
+
+/** Initiator firmware boot selected */
+#define IBFT_FL_INITIATOR_FIRMWARE_BOOT_SELECTED 0x02
+
+/**
+ * iBFT NIC structure
+ *
+ */
+struct ibft_nic {
+ /** Common header */
+ struct ibft_header header;
+ /** IP address */
+ struct ibft_ipaddr ip_address;
+ /** Subnet mask
+ *
+ * This is the length of the subnet mask in bits (e.g. /24).
+ */
+ uint8_t subnet_mask_prefix;
+ /** Origin */
+ uint8_t origin;
+ /** Default gateway */
+ struct ibft_ipaddr gateway;
+ /** Primary and secondary DNS servers */
+ struct ibft_ipaddr dns[2];
+ /** DHCP server */
+ struct ibft_ipaddr dhcp;
+ /** VLAN tag */
+ uint16_t vlan;
+ /** MAC address */
+ uint8_t mac_address[6];
+ /** PCI bus:dev:fn */
+ uint16_t pci_bus_dev_func;
+ /** Hostname */
+ struct ibft_string hostname;
+} __attribute__ (( packed ));
+
+/** Structure ID for NIC section */
+#define IBFT_STRUCTURE_ID_NIC 0x03
+
+/** NIC block valid */
+#define IBFT_FL_NIC_BLOCK_VALID 0x01
+
+/** NIC firmware boot selected */
+#define IBFT_FL_NIC_FIRMWARE_BOOT_SELECTED 0x02
+
+/** NIC global / link local */
+#define IBFT_FL_NIC_GLOBAL 0x04
+
+/**
+ * iBFT Target structure
+ *
+ */
+struct ibft_target {
+ /** Common header */
+ struct ibft_header header;
+ /** IP address */
+ struct ibft_ipaddr ip_address;
+ /** TCP port */
+ uint16_t socket;
+ /** Boot LUN */
+ uint64_t boot_lun;
+ /** CHAP type
+ *
+ * This is an IBFT_CHAP_XXX constant.
+ */
+ uint8_t chap_type;
+ /** NIC association */
+ uint8_t nic_association;
+ /** Target name */
+ struct ibft_string target_name;
+ /** CHAP name */
+ struct ibft_string chap_name;
+ /** CHAP secret */
+ struct ibft_string chap_secret;
+ /** Reverse CHAP name */
+ struct ibft_string reverse_chap_name;
+ /** Reverse CHAP secret */
+ struct ibft_string reverse_chap_secret;
+} __attribute__ (( packed ));
+
+/** Structure ID for Target section */
+#define IBFT_STRUCTURE_ID_TARGET 0x04
+
+/** Target block valid */
+#define IBFT_FL_TARGET_BLOCK_VALID 0x01
+
+/** Target firmware boot selected */
+#define IBFT_FL_TARGET_FIRMWARE_BOOT_SELECTED 0x02
+
+/** Target use Radius CHAP */
+#define IBFT_FL_TARGET_USE_CHAP 0x04
+
+/** Target use Radius rCHAP */
+#define IBFT_FL_TARGET_USE_RCHAP 0x08
+
+/* Values for chap_type */
+#define IBFT_CHAP_NONE 0 /**< No CHAP authentication */
+#define IBFT_CHAP_ONE_WAY 1 /**< One-way CHAP */
+#define IBFT_CHAP_MUTUAL 2 /**< Mutual CHAP */
+
+/**
+ * iSCSI Boot Firmware Table (iBFT)
+ */
+struct ibft_table {
+ /** ACPI header */
+ struct acpi_description_header acpi;
+ /** Reserved */
+ uint8_t reserved[12];
+ /** Control structure */
+ struct ibft_control control;
+} __attribute__ (( packed ));
+
+/**
+ * iSCSI string block descriptor
+ *
+ * This is an internal structure that we use to keep track of the
+ * allocation of string data.
+ */
+struct ibft_string_block {
+ /** The iBFT containing these strings */
+ struct ibft_table *table;
+ /** Offset of first free byte within iBFT */
+ unsigned int offset;
+};
+
+/** Amount of space reserved for strings in a gPXE iBFT */
+#define IBFT_STRINGS_SIZE 384
+
+/**
+ * An iBFT created by gPXE
+ *
+ */
+struct gpxe_ibft {
+ /** The fixed section */
+ struct ibft_table table;
+ /** The Initiator section */
+ struct ibft_initiator initiator __attribute__ (( aligned ( 16 ) ));
+ /** The NIC section */
+ struct ibft_nic nic __attribute__ (( aligned ( 16 ) ));
+ /** The Target section */
+ struct ibft_target target __attribute__ (( aligned ( 16 ) ));
+ /** Strings block */
+ char strings[IBFT_STRINGS_SIZE];
+} __attribute__ (( packed, aligned ( 16 ) ));
+
+struct net_device;
+struct iscsi_session;
+
+extern int ibft_fill_data ( struct net_device *netdev,
+ struct iscsi_session *iscsi );
+
+#endif /* _GPXE_IBFT_H */
diff --git a/src/arch/i386/interface/pcbios/abft.c b/src/arch/i386/interface/pcbios/abft.c
new file mode 100644
index 000000000..af28bbcfb
--- /dev/null
+++ b/src/arch/i386/interface/pcbios/abft.c
@@ -0,0 +1,60 @@
+/*
+ * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+#include <realmode.h>
+#include <gpxe/aoe.h>
+#include <gpxe/netdevice.h>
+#include <gpxe/abft.h>
+
+/** @file
+ *
+ * AoE Boot Firmware Table
+ *
+ */
+
+#define abftab __use_data16 ( abftab )
+/** The aBFT used by gPXE */
+struct abft_table __data16 ( abftab ) __attribute__ (( aligned ( 16 ) )) = {
+ /* ACPI header */
+ .acpi = {
+ .signature = ABFT_SIG,
+ .length = sizeof ( abftab ),
+ .revision = 1,
+ .oem_id = "FENSYS",
+ .oem_table_id = "gPXE",
+ },
+};
+
+/**
+ * Fill in all variable portions of aBFT
+ *
+ * @v aoe AoE session
+ */
+void abft_fill_data ( struct aoe_session *aoe ) {
+
+ /* Fill in boot parameters */
+ abftab.shelf = aoe->major;
+ abftab.slot = aoe->minor;
+ memcpy ( abftab.mac, aoe->netdev->ll_addr, sizeof ( abftab.mac ) );
+
+ /* Update checksum */
+ acpi_fix_checksum ( &abftab.acpi );
+
+ DBG ( "AoE boot firmware table:\n" );
+ DBG_HD ( &abftab, sizeof ( abftab ) );
+}
diff --git a/src/arch/i386/interface/pcbios/aoeboot.c b/src/arch/i386/interface/pcbios/aoeboot.c
new file mode 100644
index 000000000..e977b10fe
--- /dev/null
+++ b/src/arch/i386/interface/pcbios/aoeboot.c
@@ -0,0 +1,79 @@
+#include <stdint.h>
+#include <string.h>
+#include <stdio.h>
+#include <byteswap.h>
+#include <gpxe/aoe.h>
+#include <gpxe/ata.h>
+#include <gpxe/netdevice.h>
+#include <gpxe/settings.h>
+#include <gpxe/sanboot.h>
+#include <gpxe/abft.h>
+#include <int13.h>
+
+/**
+ * Guess boot network device
+ *
+ * @ret netdev Boot network device
+ */
+static struct net_device * guess_boot_netdev ( void ) {
+ struct net_device *netdev;
+
+ /* Just use the first network device */
+ for_each_netdev ( netdev ) {
+ if ( netdev->state & NETDEV_OPEN )
+ return netdev;
+ }
+
+ return NULL;
+}
+
+static int aoeboot ( const char *root_path ) {
+ struct ata_device ata;
+ struct int13_drive drive;
+ int rc;
+
+ memset ( &ata, 0, sizeof ( ata ) );
+ memset ( &drive, 0, sizeof ( drive ) );
+
+ printf ( "AoE booting from %s\n", root_path );
+
+ /* FIXME: ugly, ugly hack */
+ struct net_device *netdev = guess_boot_netdev();
+
+ if ( ( rc = aoe_attach ( &ata, netdev, root_path ) ) != 0 ) {
+ printf ( "Could not attach AoE device: %s\n",
+ strerror ( rc ) );
+ goto error_attach;
+ }
+ if ( ( rc = init_atadev ( &ata ) ) != 0 ) {
+ printf ( "Could not initialise AoE device: %s\n",
+ strerror ( rc ) );
+ goto error_init;
+ }
+
+ /* FIXME: ugly, ugly hack */
+ struct aoe_session *aoe =
+ container_of ( ata.backend, struct aoe_session, refcnt );
+ abft_fill_data ( aoe );
+
+ drive.blockdev = &ata.blockdev;
+
+ register_int13_drive ( &drive );
+ printf ( "Registered as BIOS drive %#02x\n", drive.drive );
+ printf ( "Booting from BIOS drive %#02x\n", drive.drive );
+ rc = int13_boot ( drive.drive );
+ printf ( "Boot failed\n" );
+
+ printf ( "Unregistering BIOS drive %#02x\n", drive.drive );
+ unregister_int13_drive ( &drive );
+
+ error_init:
+ aoe_detach ( &ata );
+ error_attach:
+ return rc;
+}
+
+struct sanboot_protocol aoe_sanboot_protocol __sanboot_protocol = {
+ .prefix = "aoe:",
+ .boot = aoeboot,
+};
diff --git a/src/arch/i386/interface/pcbios/ibft.c b/src/arch/i386/interface/pcbios/ibft.c
new file mode 100644
index 000000000..ffa65964a
--- /dev/null
+++ b/src/arch/i386/interface/pcbios/ibft.c
@@ -0,0 +1,394 @@
+/*
+ * Copyright Fen Systems Ltd. 2007. Portions of this code are derived
+ * from IBM Corporation Sample Programs. Copyright IBM Corporation
+ * 2004, 2007. All rights reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person
+ * obtaining a copy of this software and associated documentation
+ * files (the "Software"), to deal in the Software without
+ * restriction, including without limitation the rights to use, copy,
+ * modify, merge, publish, distribute, sublicense, and/or sell copies
+ * of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ *
+ */
+
+#include <stdint.h>
+#include <stdio.h>
+#include <string.h>
+#include <errno.h>
+#include <byteswap.h>
+#include <realmode.h>
+#include <gpxe/pci.h>
+#include <gpxe/acpi.h>
+#include <gpxe/in.h>
+#include <gpxe/netdevice.h>
+#include <gpxe/dhcp.h>
+#include <gpxe/iscsi.h>
+#include <gpxe/ibft.h>
+
+/** @file
+ *
+ * iSCSI boot firmware table
+ *
+ * The information in this file is derived from the document "iSCSI
+ * Boot Firmware Table (iBFT)" as published by IBM at
+ *
+ * ftp://ftp.software.ibm.com/systems/support/system_x_pdf/ibm_iscsi_boot_firmware_table_v1.02.pdf
+ *
+ */
+
+#define ibftab __use_data16 ( ibftab )
+/** The iBFT used by gPXE */
+struct gpxe_ibft __data16 ( ibftab ) = {
+ /* Table header */
+ .table = {
+ /* ACPI header */
+ .acpi = {
+ .signature = IBFT_SIG,
+ .length = sizeof ( ibftab ),
+ .revision = 1,
+ .oem_id = "FENSYS",
+ .oem_table_id = "gPXE",
+ },
+ /* Control block */
+ .control = {
+ .header = {
+ .structure_id = IBFT_STRUCTURE_ID_CONTROL,
+ .version = 1,
+ .length = sizeof ( ibftab.table.control ),
+ .flags = 0,
+ },
+ .initiator = offsetof ( typeof ( ibftab ), initiator ),
+ .nic_0 = offsetof ( typeof ( ibftab ), nic ),
+ .target_0 = offsetof ( typeof ( ibftab ), target ),
+ },
+ },
+ /* iSCSI initiator information */
+ .initiator = {
+ .header = {
+ .structure_id = IBFT_STRUCTURE_ID_INITIATOR,
+ .version = 1,
+ .length = sizeof ( ibftab.initiator ),
+ .flags = ( IBFT_FL_INITIATOR_BLOCK_VALID |
+ IBFT_FL_INITIATOR_FIRMWARE_BOOT_SELECTED ),
+ },
+ },
+ /* NIC information */
+ .nic = {
+ .header = {
+ .structure_id = IBFT_STRUCTURE_ID_NIC,
+ .version = 1,
+ .length = sizeof ( ibftab.nic ),
+ .flags = ( IBFT_FL_NIC_BLOCK_VALID |
+ IBFT_FL_NIC_FIRMWARE_BOOT_SELECTED ),
+ },
+ },
+ /* iSCSI target information */
+ .target = {
+ .header = {
+ .structure_id = IBFT_STRUCTURE_ID_TARGET,
+ .version = 1,
+ .length = sizeof ( ibftab.target ),
+ .flags = ( IBFT_FL_TARGET_BLOCK_VALID |
+ IBFT_FL_TARGET_FIRMWARE_BOOT_SELECTED ),
+ },
+ },
+};
+
+/**
+ * Fill in an IP address field within iBFT
+ *
+ * @v ipaddr IP address field
+ * @v in IPv4 address
+ */
+static void ibft_set_ipaddr ( struct ibft_ipaddr *ipaddr, struct in_addr in ) {
+ memset ( ipaddr, 0, sizeof ( ipaddr ) );
+ if ( in.s_addr ) {
+ ipaddr->in = in;
+ ipaddr->ones = 0xffff;
+ }
+}
+
+/**
+ * Fill in an IP address within iBFT from configuration setting
+ *
+ * @v ipaddr IP address field
+ * @v setting Configuration setting
+ * @v tag DHCP option tag
+ */
+static void ibft_set_ipaddr_option ( struct ibft_ipaddr *ipaddr,
+ struct setting *setting ) {
+ struct in_addr in = { 0 };
+ fetch_ipv4_setting ( NULL, setting, &in );
+ ibft_set_ipaddr ( ipaddr, in );
+}
+
+/**
+ * Allocate a string within iBFT
+ *
+ * @v strings iBFT string block descriptor
+ * @v string String field to fill in
+ * @v len Length of string to allocate (excluding NUL)
+ * @ret rc Return status code
+ */
+static int ibft_alloc_string ( struct ibft_string_block *strings,
+ struct ibft_string *string, size_t len ) {
+ char *dest;
+ unsigned int remaining;
+
+ dest = ( ( ( char * ) strings->table ) + strings->offset );
+ remaining = ( strings->table->acpi.length - strings->offset );
+ if ( len >= remaining )
+ return -ENOMEM;
+
+ string->offset = strings->offset;
+ string->length = len;
+ strings->offset += ( len + 1 );
+ return 0;
+}
+
+/**
+ * Fill in a string field within iBFT
+ *
+ * @v strings iBFT string block descriptor
+ * @v string String field
+ * @v data String to fill in, or NULL
+ * @ret rc Return status code
+ */
+static int ibft_set_string ( struct ibft_string_block *strings,
+ struct ibft_string *string, const char *data ) {
+ char *dest;
+ int rc;
+
+ if ( ! data )
+ return 0;
+
+ if ( ( rc = ibft_alloc_string ( strings, string,
+ strlen ( data ) ) ) != 0 )
+ return rc;
+ dest = ( ( ( char * ) strings->table ) + string->offset );
+ strcpy ( dest, data );
+
+ return 0;
+}
+
+/**
+ * Fill in a string field within iBFT from configuration setting
+ *
+ * @v strings iBFT string block descriptor
+ * @v string String field
+ * @v setting Configuration setting
+ * @ret rc Return status code
+ */
+static int ibft_set_string_option ( struct ibft_string_block *strings,
+ struct ibft_string *string,
+ struct setting *setting ) {
+ int len;
+ char *dest;
+ int rc;
+
+ len = fetch_setting_len ( NULL, setting );
+ if ( len < 0 ) {
+ string->offset = 0;
+ string->length = 0;
+ return 0;
+ }
+
+ if ( ( rc = ibft_alloc_string ( strings, string, len ) ) != 0 )
+ return rc;
+ dest = ( ( ( char * ) strings->table ) + string->offset );
+ fetch_string_setting ( NULL, setting, dest, ( len + 1 ) );
+ return 0;
+}
+
+/**
+ * Fill in NIC portion of iBFT
+ *
+ * @v nic NIC portion of iBFT
+ * @v strings iBFT string block descriptor
+ * @v netdev Network device
+ * @ret rc Return status code
+ */
+static int ibft_fill_nic ( struct ibft_nic *nic,
+ struct ibft_string_block *strings,
+ struct net_device *netdev ) {
+ struct in_addr netmask_addr = { 0 };
+ unsigned int netmask_count = 0;
+ int rc;
+
+ /* Extract values from DHCP configuration */
+ ibft_set_ipaddr_option ( &nic->ip_address, &ip_setting );
+ ibft_set_ipaddr_option ( &nic->gateway, &gateway_setting );
+ ibft_set_ipaddr_option ( &nic->dns[0], &dns_setting );
+ if ( ( rc = ibft_set_string_option ( strings, &nic->hostname,
+ &hostname_setting ) ) != 0 )
+ return rc;
+
+ /* Derive subnet mask prefix from subnet mask */
+ fetch_ipv4_setting ( NULL, &netmask_setting, &netmask_addr );
+ while ( netmask_addr.s_addr ) {
+ if ( netmask_addr.s_addr & 0x1 )
+ netmask_count++;
+ netmask_addr.s_addr >>= 1;
+ }
+ nic->subnet_mask_prefix = netmask_count;
+
+ /* Extract values from net-device configuration */
+ memcpy ( nic->mac_address, netdev->ll_addr,
+ sizeof ( nic->mac_address ) );
+ nic->pci_bus_dev_func = netdev->dev->desc.location;
+
+ return 0;
+}
+
+/**
+ * Fill in Initiator portion of iBFT
+ *
+ * @v initiator Initiator portion of iBFT
+ * @v strings iBFT string block descriptor
+ * @ret rc Return status code
+ */
+static int ibft_fill_initiator ( struct ibft_initiator *initiator,
+ struct ibft_string_block *strings ) {
+ const char *initiator_iqn = iscsi_initiator_iqn();
+ int rc;
+
+ if ( ( rc = ibft_set_string ( strings, &initiator->initiator_name,
+ initiator_iqn ) ) != 0 )
+ return rc;
+
+ return 0;
+}
+
+/**
+ * Fill in Target CHAP portion of iBFT
+ *
+ * @v target Target portion of iBFT
+ * @v strings iBFT string block descriptor
+ * @v iscsi iSCSI session
+ * @ret rc Return status code
+ */
+static int ibft_fill_target_chap ( struct ibft_target *target,
+ struct ibft_string_block *strings,
+ struct iscsi_session *iscsi ) {
+ int rc;
+
+ if ( ! iscsi->initiator_username )
+ return 0;
+ assert ( iscsi->initiator_password );
+
+ target->chap_type = IBFT_CHAP_ONE_WAY;
+ if ( ( rc = ibft_set_string ( strings, &target->chap_name,
+ iscsi->initiator_username ) ) != 0 )
+ return rc;
+ if ( ( rc = ibft_set_string ( strings, &target->chap_secret,
+ iscsi->initiator_password ) ) != 0 )
+ return rc;
+ return 0;
+}
+
+/**
+ * Fill in Target Reverse CHAP portion of iBFT
+ *
+ * @v target Target portion of iBFT
+ * @v strings iBFT string block descriptor
+ * @v iscsi iSCSI session
+ * @ret rc Return status code
+ */
+static int ibft_fill_target_reverse_chap ( struct ibft_target *target,
+ struct ibft_string_block *strings,
+ struct iscsi_session *iscsi ) {
+ int rc;
+
+ if ( ! iscsi->target_username )
+ return 0;
+ assert ( iscsi->target_password );
+ assert ( iscsi->initiator_username );
+ assert ( iscsi->initiator_password );
+
+ target->chap_type = IBFT_CHAP_MUTUAL;
+ if ( ( rc = ibft_set_string ( strings, &target->reverse_chap_name,
+ iscsi->target_username ) ) != 0 )
+ return rc;
+ if ( ( rc = ibft_set_string ( strings, &target->reverse_chap_secret,
+ iscsi->target_password ) ) != 0 )
+ return rc;
+ return 0;
+}
+
+/**
+ * Fill in Target portion of iBFT
+ *
+ * @v target Target portion of iBFT
+ * @v strings iBFT string block descriptor
+ * @v iscsi iSCSI session
+ * @ret rc Return status code
+ */
+static int ibft_fill_target ( struct ibft_target *target,
+ struct ibft_string_block *strings,
+ struct iscsi_session *iscsi ) {
+ struct sockaddr_in *sin_target =
+ ( struct sockaddr_in * ) &iscsi->target_sockaddr;
+ int rc;
+
+ /* Fill in Target values */
+ ibft_set_ipaddr ( &target->ip_address, sin_target->sin_addr );
+ target->socket = ntohs ( sin_target->sin_port );
+ if ( ( rc = ibft_set_string ( strings, &target->target_name,
+ iscsi->target_iqn ) ) != 0 )
+ return rc;
+ if ( ( rc = ibft_fill_target_chap ( target, strings, iscsi ) ) != 0 )
+ return rc;
+ if ( ( rc = ibft_fill_target_reverse_chap ( target, strings,
+ iscsi ) ) != 0 )
+ return rc;
+
+ return 0;
+}
+
+/**
+ * Fill in all variable portions of iBFT
+ *
+ * @v netdev Network device
+ * @v initiator_iqn Initiator IQN
+ * @v st_target Target socket address
+ * @v target_iqn Target IQN
+ * @ret rc Return status code
+ *
+ */
+int ibft_fill_data ( struct net_device *netdev,
+ struct iscsi_session *iscsi ) {
+ struct ibft_string_block strings = {
+ .table = &ibftab.table,
+ .offset = offsetof ( typeof ( ibftab ), strings ),
+ };
+ int rc;
+
+ /* Fill in NIC, Initiator and Target portions */
+ if ( ( rc = ibft_fill_nic ( &ibftab.nic, &strings, netdev ) ) != 0 )
+ return rc;
+ if ( ( rc = ibft_fill_initiator ( &ibftab.initiator,
+ &strings ) ) != 0 )
+ return rc;
+ if ( ( rc = ibft_fill_target ( &ibftab.target, &strings,
+ iscsi ) ) != 0 )
+ return rc;
+
+ /* Update checksum */
+ acpi_fix_checksum ( &ibftab.table.acpi );
+
+ return 0;
+}
diff --git a/src/arch/i386/interface/pcbios/iscsiboot.c b/src/arch/i386/interface/pcbios/iscsiboot.c
new file mode 100644
index 000000000..cdf7790bb
--- /dev/null
+++ b/src/arch/i386/interface/pcbios/iscsiboot.c
@@ -0,0 +1,108 @@
+#include <stdint.h>
+#include <string.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <errno.h>
+#include <gpxe/iscsi.h>
+#include <gpxe/settings.h>
+#include <gpxe/dhcp.h>
+#include <gpxe/netdevice.h>
+#include <gpxe/ibft.h>
+#include <gpxe/init.h>
+#include <gpxe/sanboot.h>
+#include <int13.h>
+#include <usr/autoboot.h>
+
+struct setting keep_san_setting __setting = {
+ .name = "keep-san",
+ .description = "Preserve SAN connection",
+ .tag = DHCP_EB_KEEP_SAN,
+ .type = &setting_type_int8,
+};
+
+/**
+ * Guess boot network device
+ *
+ * @ret netdev Boot network device
+ */
+static struct net_device * guess_boot_netdev ( void ) {
+ struct net_device *netdev;
+
+ /* Just use the first network device */
+ for_each_netdev ( netdev ) {
+ if ( netdev->state & NETDEV_OPEN )
+ return netdev;
+ }
+
+ return NULL;
+}
+
+static int iscsiboot ( const char *root_path ) {
+ struct scsi_device *scsi;
+ struct int13_drive *drive;
+ int keep_san;
+ int rc;
+
+ scsi = zalloc ( sizeof ( *scsi ) );
+ if ( ! scsi ) {
+ rc = -ENOMEM;
+ goto err_alloc_scsi;
+ }
+ drive = zalloc ( sizeof ( *drive ) );
+ if ( ! drive ) {
+ rc = -ENOMEM;
+ goto err_alloc_drive;
+ }
+
+ printf ( "iSCSI booting from %s\n", root_path );
+
+ if ( ( rc = iscsi_attach ( scsi, root_path ) ) != 0 ) {
+ printf ( "Could not attach iSCSI device: %s\n",
+ strerror ( rc ) );
+ goto err_attach;
+ }
+ if ( ( rc = init_scsidev ( scsi ) ) != 0 ) {
+ printf ( "Could not initialise iSCSI device: %s\n",
+ strerror ( rc ) );
+ goto err_init;
+ }
+
+ drive->blockdev = &scsi->blockdev;
+
+ /* FIXME: ugly, ugly hack */
+ struct net_device *netdev = guess_boot_netdev();
+ struct iscsi_session *iscsi =
+ container_of ( scsi->backend, struct iscsi_session, refcnt );
+ ibft_fill_data ( netdev, iscsi );
+
+ register_int13_drive ( drive );
+ printf ( "Registered as BIOS drive %#02x\n", drive->drive );
+ printf ( "Booting from BIOS drive %#02x\n", drive->drive );
+ rc = int13_boot ( drive->drive );
+ printf ( "Boot failed\n" );
+
+ /* Leave drive registered, if instructed to do so */
+ keep_san = fetch_intz_setting ( NULL, &keep_san_setting );
+ if ( keep_san ) {
+ printf ( "Preserving connection to SAN disk\n" );
+ shutdown_exit_flags |= SHUTDOWN_KEEP_DEVICES;
+ return rc;
+ }
+
+ printf ( "Unregistering BIOS drive %#02x\n", drive->drive );
+ unregister_int13_drive ( drive );
+
+ err_init:
+ iscsi_detach ( scsi );
+ err_attach:
+ free ( drive );
+ err_alloc_drive:
+ free ( scsi );
+ err_alloc_scsi:
+ return rc;
+}
+
+struct sanboot_protocol iscsi_sanboot_protocol __sanboot_protocol = {
+ .prefix = "iscsi:",
+ .boot = iscsiboot,
+};