6 #include <gpxe/iscsi.h>
8 static int test_dhcp_aoe_boot ( struct net_device *netdev,
10 unsigned int drivenum;
12 drivenum = find_global_dhcp_num_option ( DHCP_EB_BIOS_DRIVE );
13 return test_aoeboot ( netdev, aoename, drivenum );
16 static int test_dhcp_iscsi_boot ( char *iscsiname ) {
17 char *initiator_iqn = "iqn.1900-01.localdomain.localhost:initiator";
20 struct sockaddr_in sin;
21 struct sockaddr_tcpip st;
24 memset ( &target, 0, sizeof ( target ) );
25 target.sin.sin_family = AF_INET;
26 target.sin.sin_port = htons ( ISCSI_PORT );
27 target_iqn = strchr ( iscsiname, ':' );
30 printf ( "Invalid iSCSI DHCP path\n" );
33 inet_aton ( iscsiname, &target.sin.sin_addr );
35 return test_iscsiboot ( initiator_iqn, &target.st, target_iqn );
38 static int test_dhcp_hello ( char *helloname ) {
41 struct sockaddr_in sin;
42 struct sockaddr_tcpip st;
45 memset ( &target, 0, sizeof ( target ) );
46 target.sin.sin_family = AF_INET;
47 target.sin.sin_port = htons ( 80 );
48 message = strchr ( helloname, ':' );
51 printf ( "Invalid hello path\n" );
54 inet_aton ( helloname, &target.sin.sin_addr );
56 test_hello ( &target.st, message );
60 static int test_dhcp_http ( struct net_device *netdev, char *url ) {
62 struct sockaddr_in sin;
63 struct sockaddr_tcpip st;
66 memset ( &target, 0, sizeof ( target ) );
67 target.sin.sin_family = AF_INET;
68 target.sin.sin_port = htons ( 80 );
70 char *addr = url + 7; // http://
71 char *file = strchr(addr, '/');
72 *file = '\0'; // for printf and inet_aton to work
73 printf("connecting to %s\n", addr);
74 inet_aton ( addr, &target.sin.sin_addr );
76 test_http ( netdev, &target.st, file );
80 static int test_dhcp_tftp ( struct net_device *netdev, char *tftpname ) {
82 struct sockaddr_in sin;
83 struct sockaddr_tcpip st;
86 memset ( &target, 0, sizeof ( target ) );
87 target.sin.sin_family = AF_INET;
88 target.sin.sin_port = htons ( 69 );
89 find_global_dhcp_ipv4_option ( DHCP_EB_SIADDR,
90 &target.sin.sin_addr );
92 return test_tftp ( netdev, &target.st, tftpname );
95 static int test_dhcp_boot ( struct net_device *netdev, char *filename ) {
96 if ( strncmp ( filename, "aoe:", 4 ) == 0 ) {
97 return test_dhcp_aoe_boot ( netdev, &filename[4] );
98 } else if ( strncmp ( filename, "iscsi:", 6 ) == 0 ) {
99 return test_dhcp_iscsi_boot ( &filename[6] );
100 } else if ( strncmp ( filename, "hello:", 6 ) == 0 ) {
101 return test_dhcp_hello ( &filename[6] );
102 } else if ( strncmp ( filename, "http:", 5 ) == 0 ) {
103 return test_dhcp_http ( netdev, filename );
105 return test_dhcp_tftp ( netdev, filename );
109 int test_dhcp ( struct net_device *netdev ) {
110 struct dhcp_session dhcp;
111 struct in_addr address = { htonl ( 0 ) };
112 struct in_addr netmask = { htonl ( 0 ) };
113 struct in_addr gateway = { INADDR_NONE };
117 /* Bring IP interface up with address 0.0.0.0 */
118 if ( ( rc = add_ipv4_address ( netdev, address, netmask,
120 goto out_no_del_ipv4;
122 /* Issue DHCP request */
123 printf ( "DHCP..." );
124 memset ( &dhcp, 0, sizeof ( dhcp ) );
125 dhcp.netdev = netdev;
126 if ( ( rc = async_wait ( start_dhcp ( &dhcp ) ) ) != 0 ) {
127 printf ( "failed\n" );
132 /* Register options received via DHCP */
133 register_dhcp_options ( dhcp.options );
135 /* Retrieve IP address configuration */
136 find_global_dhcp_ipv4_option ( DHCP_EB_YIADDR, &address );
137 find_global_dhcp_ipv4_option ( DHCP_SUBNET_MASK, &netmask );
138 find_global_dhcp_ipv4_option ( DHCP_ROUTERS, &gateway );
140 printf ( "IP %s", inet_ntoa ( address ) );
141 printf ( " netmask %s", inet_ntoa ( netmask ) );
142 printf ( " gateway %s\n", inet_ntoa ( gateway ) );
144 dhcp_snprintf ( filename, sizeof ( filename ),
145 find_global_dhcp_option ( DHCP_BOOTFILE_NAME ) );
146 if ( ! filename[0] ) {
147 printf ( "No filename specified!\n" );
151 printf ( "Bootfile name %s\n", filename );
153 /* Remove old IP address configuration */
154 del_ipv4_address ( netdev );
156 /* Set up new IP address configuration */
157 if ( ( rc = add_ipv4_address ( netdev, address, netmask,
159 goto out_no_del_ipv4;
162 if ( ( rc = test_dhcp_boot ( netdev, filename ) ) != 0 ) {
163 printf ( "Boot failed\n" );
168 /* Unregister and free DHCP options */
169 unregister_dhcp_options ( dhcp.options );
170 free_dhcp_options ( dhcp.options );
172 /* Take down IP interface */
173 del_ipv4_address ( netdev );