123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498 |
-
-
- FILE_LICENCE ( BSD2 );
-
- #include <stdint.h>
- #include <stdio.h>
- #include <string.h>
- #include <errno.h>
- #include <byteswap.h>
- #include <ipxe/pci.h>
- #include <ipxe/acpi.h>
- #include <ipxe/in.h>
- #include <ipxe/netdevice.h>
- #include <ipxe/ethernet.h>
- #include <ipxe/vlan.h>
- #include <ipxe/dhcp.h>
- #include <ipxe/iscsi.h>
- #include <ipxe/ibft.h>
-
-
-
-
- struct ipxe_ibft {
-
- struct ibft_table table;
-
- struct ibft_initiator initiator __attribute__ (( aligned ( 16 ) ));
-
- struct ibft_nic nic __attribute__ (( aligned ( 16 ) ));
-
- struct ibft_target target __attribute__ (( aligned ( 16 ) ));
-
- char strings[0];
- } __attribute__ (( packed, aligned ( 16 ) ));
-
-
- struct ibft_strings {
-
- struct ibft_table *table;
-
- size_t offset;
-
- size_t len;
- };
-
-
- 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;
- }
- }
-
-
- static void ibft_set_ipaddr_setting ( struct settings *settings,
- struct ibft_ipaddr *ipaddr,
- const struct setting *setting,
- unsigned int count ) {
- struct in_addr in[count];
- unsigned int i;
-
- fetch_ipv4_array_setting ( settings, setting, in, count );
- for ( i = 0 ; i < count ; i++ ) {
- ibft_set_ipaddr ( &ipaddr[i], in[i] );
- }
- }
-
-
- static const char * ibft_ipaddr ( struct ibft_ipaddr *ipaddr ) {
- return inet_ntoa ( ipaddr->in );
- }
-
-
- static char * ibft_alloc_string ( struct ibft_strings *strings,
- struct ibft_string *string, size_t len ) {
-
- if ( ( strings->offset + len ) >= strings->len )
- return NULL;
-
- string->offset = cpu_to_le16 ( strings->offset );
- string->len = cpu_to_le16 ( len );
- strings->offset += ( len + 1 );
-
- return ( ( ( char * ) strings->table ) + string->offset );
- }
-
-
- static int ibft_set_string ( struct ibft_strings *strings,
- struct ibft_string *string, const char *data ) {
- char *dest;
-
- if ( ! data )
- return 0;
-
- dest = ibft_alloc_string ( strings, string, strlen ( data ) );
- if ( ! dest )
- return -ENOBUFS;
- strcpy ( dest, data );
-
- return 0;
- }
-
-
- static int ibft_set_string_setting ( struct settings *settings,
- struct ibft_strings *strings,
- struct ibft_string *string,
- const struct setting *setting ) {
- struct settings *origin;
- struct setting fetched;
- int len;
- char *dest;
-
- len = fetch_setting ( settings, setting, &origin, &fetched, NULL, 0 );
- if ( len < 0 ) {
- string->offset = 0;
- string->len = 0;
- return 0;
- }
-
- dest = ibft_alloc_string ( strings, string, len );
- if ( ! dest )
- return -ENOBUFS;
- fetch_string_setting ( origin, &fetched, dest, ( len + 1 ));
-
- return 0;
- }
-
-
- static const char * ibft_string ( struct ibft_strings *strings,
- struct ibft_string *string ) {
- return ( string->offset ?
- ( ( ( char * ) strings->table ) + string->offset ) : NULL );
- }
-
-
- static int ibft_fill_nic ( struct ibft_nic *nic,
- struct ibft_strings *strings,
- struct net_device *netdev ) {
- struct ll_protocol *ll_protocol = netdev->ll_protocol;
- struct in_addr netmask_addr = { 0 };
- unsigned int netmask_count = 0;
- struct settings *parent = netdev_settings ( netdev );
- struct settings *origin;
- int rc;
-
-
- nic->header.structure_id = IBFT_STRUCTURE_ID_NIC;
- nic->header.version = 1;
- nic->header.length = cpu_to_le16 ( sizeof ( *nic ) );
- nic->header.flags = ( IBFT_FL_NIC_BLOCK_VALID |
- IBFT_FL_NIC_FIRMWARE_BOOT_SELECTED );
-
-
- fetch_setting ( parent, &ip_setting, &origin, NULL, NULL, 0 );
- nic->origin = ( ( origin == parent ) ?
- IBFT_NIC_ORIGIN_MANUAL : IBFT_NIC_ORIGIN_DHCP );
- DBG ( "iBFT NIC origin = %d\n", nic->origin );
-
-
- ibft_set_ipaddr_setting ( parent, &nic->ip_address, &ip_setting, 1 );
- DBG ( "iBFT NIC IP = %s\n", ibft_ipaddr ( &nic->ip_address ) );
- ibft_set_ipaddr_setting ( parent, &nic->gateway, &gateway_setting, 1 );
- DBG ( "iBFT NIC gateway = %s\n", ibft_ipaddr ( &nic->gateway ) );
- ibft_set_ipaddr_setting ( NULL, &nic->dns[0], &dns_setting,
- ( sizeof ( nic->dns ) /
- sizeof ( nic->dns[0] ) ) );
- ibft_set_ipaddr_setting ( parent, &nic->dhcp, &dhcp_server_setting, 1 );
- DBG ( "iBFT NIC DNS = %s", ibft_ipaddr ( &nic->dns[0] ) );
- DBG ( ", %s\n", ibft_ipaddr ( &nic->dns[1] ) );
- if ( ( rc = ibft_set_string_setting ( NULL, strings, &nic->hostname,
- &hostname_setting ) ) != 0 )
- return rc;
- DBG ( "iBFT NIC hostname = %s\n",
- ibft_string ( strings, &nic->hostname ) );
-
-
- fetch_ipv4_setting ( parent, &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;
- DBG ( "iBFT NIC subnet = /%d\n", nic->subnet_mask_prefix );
-
-
- nic->vlan = cpu_to_le16 ( vlan_tag ( netdev ) );
- DBG ( "iBFT NIC VLAN = %02x\n", le16_to_cpu ( nic->vlan ) );
- if ( ( rc = ll_protocol->eth_addr ( netdev->ll_addr,
- nic->mac_address ) ) != 0 ) {
- DBG ( "Could not determine iBFT MAC: %s\n", strerror ( rc ) );
- return rc;
- }
- DBG ( "iBFT NIC MAC = %s\n", eth_ntoa ( nic->mac_address ) );
- nic->pci_bus_dev_func = cpu_to_le16 ( netdev->dev->desc.location );
- DBG ( "iBFT NIC PCI = %04x\n", le16_to_cpu ( nic->pci_bus_dev_func ) );
-
- return 0;
- }
-
-
- static int ibft_fill_initiator ( struct ibft_initiator *initiator,
- struct ibft_strings *strings,
- struct iscsi_session *iscsi ) {
- int rc;
-
-
- initiator->header.structure_id = IBFT_STRUCTURE_ID_INITIATOR;
- initiator->header.version = 1;
- initiator->header.length = cpu_to_le16 ( sizeof ( *initiator ) );
- initiator->header.flags = ( IBFT_FL_INITIATOR_BLOCK_VALID |
- IBFT_FL_INITIATOR_FIRMWARE_BOOT_SELECTED );
-
-
- if ( ( rc = ibft_set_string ( strings, &initiator->initiator_name,
- iscsi->initiator_iqn ) ) != 0 )
- return rc;
- DBG ( "iBFT initiator hostname = %s\n",
- ibft_string ( strings, &initiator->initiator_name ) );
-
- return 0;
- }
-
-
- static int ibft_fill_target_chap ( struct ibft_target *target,
- struct ibft_strings *strings,
- struct iscsi_session *iscsi ) {
- int rc;
-
- if ( ! ( iscsi->status & ISCSI_STATUS_AUTH_FORWARD_REQUIRED ) )
- return 0;
-
- assert ( iscsi->initiator_username );
- 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;
- DBG ( "iBFT target username = %s\n",
- ibft_string ( strings, &target->chap_name ) );
- if ( ( rc = ibft_set_string ( strings, &target->chap_secret,
- iscsi->initiator_password ) ) != 0 )
- return rc;
- DBG ( "iBFT target password = <redacted>\n" );
-
- return 0;
- }
-
-
- static int ibft_fill_target_reverse_chap ( struct ibft_target *target,
- struct ibft_strings *strings,
- struct iscsi_session *iscsi ) {
- int rc;
-
- if ( ! ( iscsi->status & ISCSI_STATUS_AUTH_REVERSE_REQUIRED ) )
- return 0;
-
- assert ( iscsi->initiator_username );
- assert ( iscsi->initiator_password );
- assert ( iscsi->target_username );
- assert ( iscsi->target_password );
-
- target->chap_type = IBFT_CHAP_MUTUAL;
- if ( ( rc = ibft_set_string ( strings, &target->reverse_chap_name,
- iscsi->target_username ) ) != 0 )
- return rc;
- DBG ( "iBFT target reverse username = %s\n",
- ibft_string ( strings, &target->chap_name ) );
- if ( ( rc = ibft_set_string ( strings, &target->reverse_chap_secret,
- iscsi->target_password ) ) != 0 )
- return rc;
- DBG ( "iBFT target reverse password = <redacted>\n" );
-
- return 0;
- }
-
-
- static int ibft_fill_target ( struct ibft_target *target,
- struct ibft_strings *strings,
- struct iscsi_session *iscsi ) {
- struct sockaddr_in *sin_target =
- ( struct sockaddr_in * ) &iscsi->target_sockaddr;
- int rc;
-
-
- target->header.structure_id = IBFT_STRUCTURE_ID_TARGET;
- target->header.version = 1;
- target->header.length = cpu_to_le16 ( sizeof ( *target ) );
- target->header.flags = ( IBFT_FL_TARGET_BLOCK_VALID |
- IBFT_FL_TARGET_FIRMWARE_BOOT_SELECTED );
-
-
- ibft_set_ipaddr ( &target->ip_address, sin_target->sin_addr );
- DBG ( "iBFT target IP = %s\n", ibft_ipaddr ( &target->ip_address ) );
- target->socket = cpu_to_le16 ( ntohs ( sin_target->sin_port ) );
- DBG ( "iBFT target port = %d\n", target->socket );
- memcpy ( &target->boot_lun, &iscsi->lun, sizeof ( target->boot_lun ) );
- DBG ( "iBFT target boot LUN = " SCSI_LUN_FORMAT "\n",
- SCSI_LUN_DATA ( target->boot_lun ) );
- if ( ( rc = ibft_set_string ( strings, &target->target_name,
- iscsi->target_iqn ) ) != 0 )
- return rc;
- DBG ( "iBFT target name = %s\n",
- ibft_string ( strings, &target->target_name ) );
- 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;
- }
-
-
- int ibft_describe ( struct iscsi_session *iscsi,
- struct acpi_description_header *acpi,
- size_t len ) {
- struct ipxe_ibft *ibft =
- container_of ( acpi, struct ipxe_ibft, table.acpi );
- struct ibft_strings strings = {
- .table = &ibft->table,
- .offset = offsetof ( typeof ( *ibft ), strings ),
- .len = len,
- };
- struct net_device *netdev;
- int rc;
-
-
-
- netdev = last_opened_netdev();
- if ( ! netdev ) {
- DBGC ( iscsi, "iSCSI %p cannot guess network device\n",
- iscsi );
- return -ENODEV;
- }
-
-
- ibft->table.acpi.signature = cpu_to_le32 ( IBFT_SIG );
- ibft->table.acpi.length = cpu_to_le32 ( len );
- ibft->table.acpi.revision = 1;
-
-
- ibft->table.control.header.structure_id = IBFT_STRUCTURE_ID_CONTROL;
- ibft->table.control.header.version = 1;
- ibft->table.control.header.length =
- cpu_to_le16 ( sizeof ( ibft->table.control ) );
- ibft->table.control.initiator =
- cpu_to_le16 ( offsetof ( typeof ( *ibft ), initiator ) );
- ibft->table.control.nic_0 =
- cpu_to_le16 ( offsetof ( typeof ( *ibft ), nic ) );
- ibft->table.control.target_0 =
- cpu_to_le16 ( offsetof ( typeof ( *ibft ), target ) );
-
-
- if ( ( rc = ibft_fill_nic ( &ibft->nic, &strings, netdev ) ) != 0 )
- return rc;
- if ( ( rc = ibft_fill_initiator ( &ibft->initiator, &strings,
- iscsi ) ) != 0 )
- return rc;
- if ( ( rc = ibft_fill_target ( &ibft->target, &strings,
- iscsi ) ) != 0 )
- return rc;
-
- return 0;
- }
|