6 #include <gpxe/iscsi.h>
7 #include <gpxe/netdevice.h>
9 static int test_dhcp_aoe_boot ( struct net_device *netdev,
11 unsigned int drivenum;
13 drivenum = find_global_dhcp_num_option ( DHCP_EB_BIOS_DRIVE );
14 return test_aoeboot ( netdev, aoename, drivenum );
17 static int test_dhcp_iscsi_boot ( char *iscsiname ) {
18 char *initiator_iqn = "iqn.1900-01.localdomain.localhost:initiator";
21 struct sockaddr_in sin;
22 struct sockaddr_tcpip st;
25 memset ( &target, 0, sizeof ( target ) );
26 target.sin.sin_family = AF_INET;
27 target.sin.sin_port = htons ( ISCSI_PORT );
28 target_iqn = strchr ( iscsiname, ':' );
31 printf ( "Invalid iSCSI DHCP path\n" );
34 inet_aton ( iscsiname, &target.sin.sin_addr );
36 return test_iscsiboot ( initiator_iqn, &target.st, target_iqn );
39 static int test_dhcp_hello ( char *helloname ) {
42 struct sockaddr_in sin;
43 struct sockaddr_tcpip st;
46 memset ( &target, 0, sizeof ( target ) );
47 target.sin.sin_family = AF_INET;
48 target.sin.sin_port = htons ( 80 );
49 message = strchr ( helloname, ':' );
52 printf ( "Invalid hello path\n" );
55 inet_aton ( helloname, &target.sin.sin_addr );
57 test_hello ( &target.st, message );
61 static int test_dhcp_http ( struct net_device *netdev, char *url ) {
63 struct sockaddr_in sin;
64 struct sockaddr_tcpip st;
67 memset ( &target, 0, sizeof ( target ) );
68 target.sin.sin_family = AF_INET;
69 target.sin.sin_port = htons ( 80 );
71 char *addr = url + 7; // http://
72 char *file = strchr(addr, '/');
73 *file = '\0'; // for printf and inet_aton to work
74 printf("connecting to %s\n", addr);
75 inet_aton ( addr, &target.sin.sin_addr );
77 test_http ( netdev, &target.st, file );
81 static int test_dhcp_tftp ( struct net_device *netdev, char *tftpname ) {
83 struct sockaddr_in sin;
84 struct sockaddr_tcpip st;
87 memset ( &target, 0, sizeof ( target ) );
88 target.sin.sin_family = AF_INET;
89 target.sin.sin_port = htons ( 69 );
90 find_global_dhcp_ipv4_option ( DHCP_EB_SIADDR,
91 &target.sin.sin_addr );
93 return test_tftp ( netdev, &target.st, tftpname );
96 static int test_dhcp_boot ( struct net_device *netdev, char *filename ) {
97 if ( strncmp ( filename, "aoe:", 4 ) == 0 ) {
98 return test_dhcp_aoe_boot ( netdev, &filename[4] );
99 } else if ( strncmp ( filename, "iscsi:", 6 ) == 0 ) {
100 return test_dhcp_iscsi_boot ( &filename[6] );
101 } else if ( strncmp ( filename, "hello:", 6 ) == 0 ) {
102 return test_dhcp_hello ( &filename[6] );
103 } else if ( strncmp ( filename, "http:", 5 ) == 0 ) {
104 return test_dhcp_http ( netdev, filename );
106 return test_dhcp_tftp ( netdev, filename );
110 int test_dhcp ( struct net_device *netdev ) {
111 struct dhcp_session dhcp;
112 struct in_addr address = { htonl ( 0 ) };
113 struct in_addr netmask = { htonl ( 0 ) };
114 struct in_addr gateway = { INADDR_NONE };
118 /* Bring IP interface up with address 0.0.0.0 */
119 if ( ( rc = add_ipv4_address ( netdev, address, netmask,
121 goto out_no_del_ipv4;
123 /* Issue DHCP request */
124 printf ( "DHCP (%s)...", netdev_name ( netdev ) );
125 memset ( &dhcp, 0, sizeof ( dhcp ) );
126 dhcp.netdev = netdev;
127 if ( ( rc = async_wait ( start_dhcp ( &dhcp ) ) ) != 0 ) {
128 printf ( "failed\n" );
133 /* Register options received via DHCP */
134 register_dhcp_options ( dhcp.options );
136 /* Retrieve IP address configuration */
137 find_global_dhcp_ipv4_option ( DHCP_EB_YIADDR, &address );
138 find_global_dhcp_ipv4_option ( DHCP_SUBNET_MASK, &netmask );
139 find_global_dhcp_ipv4_option ( DHCP_ROUTERS, &gateway );
141 printf ( "IP %s", inet_ntoa ( address ) );
142 printf ( " netmask %s", inet_ntoa ( netmask ) );
143 printf ( " gateway %s\n", inet_ntoa ( gateway ) );
145 dhcp_snprintf ( filename, sizeof ( filename ),
146 find_global_dhcp_option ( DHCP_BOOTFILE_NAME ) );
147 if ( ! filename[0] ) {
148 printf ( "No filename specified!\n" );
152 printf ( "Bootfile name %s\n", filename );
154 /* Remove old IP address configuration */
155 del_ipv4_address ( netdev );
157 /* Set up new IP address configuration */
158 if ( ( rc = add_ipv4_address ( netdev, address, netmask,
160 goto out_no_del_ipv4;
163 if ( ( rc = test_dhcp_boot ( netdev, filename ) ) != 0 ) {
164 printf ( "Boot failed\n" );
169 /* Unregister and free DHCP options */
170 unregister_dhcp_options ( dhcp.options );
171 free_dhcp_options ( dhcp.options );
173 /* Take down IP interface */
174 del_ipv4_address ( netdev );