8 #include <gpxe/iscsi.h>
9 #include <gpxe/netdevice.h>
13 static int test_dhcp_aoe_boot ( struct net_device *netdev,
15 unsigned int drivenum;
17 drivenum = find_global_dhcp_num_option ( DHCP_EB_BIOS_DRIVE );
18 return test_aoeboot ( netdev, aoename, drivenum );
31 static int iscsi_split_root_path ( char *root_path,
32 char * components[NUM_RP_COMPONENTS] ) {
36 components[component++] = root_path;
37 if ( component == NUM_RP_COMPONENTS ) {
40 for ( ; *root_path != ':' ; root_path++ ) {
44 *(root_path++) = '\0';
48 static int test_dhcp_iscsi_boot ( struct net_device *netdev ) {
50 char *rp_components[NUM_RP_COMPONENTS];
51 char initiator_iqn_buf[64];
52 char *initiator_iqn = initiator_iqn_buf;
56 struct sockaddr_in sin;
57 struct sockaddr_tcpip st;
59 unsigned int drivenum;
61 struct dhcp_option *option;
64 memset ( &target, 0, sizeof ( target ) );
65 target.sin.sin_family = AF_INET;
67 dhcp_snprintf ( root_path, sizeof ( root_path ),
68 find_global_dhcp_option ( DHCP_ROOT_PATH ) );
70 printf ( "Root path \"%s\"\n", root_path );
72 if ( ( rc = iscsi_split_root_path ( root_path, rp_components ) ) != 0 )
75 if ( strcmp ( rp_components[RP_LITERAL], "iscsi" ) != 0 )
78 if ( inet_aton ( rp_components[RP_SERVERNAME],
79 &target.sin.sin_addr ) == 0 )
82 target.sin.sin_port = strtoul ( rp_components[RP_PORT], NULL, 0 );
83 if ( ! target.st.st_port )
84 target.st.st_port = htons ( ISCSI_PORT );
86 lun = strtoul ( rp_components[RP_LUN], NULL, 0 );
88 dhcp_snprintf ( initiator_iqn_buf, sizeof ( initiator_iqn_buf ),
89 find_global_dhcp_option ( DHCP_ISCSI_INITIATOR_IQN ) );
90 if ( ! initiator_iqn_buf[0] )
91 initiator_iqn = "iqn.1900-01.localdomain.localhost:initiator";
92 dhcp_snprintf ( username, sizeof ( username ),
93 find_global_dhcp_option ( DHCP_EB_USERNAME ) );
94 dhcp_snprintf ( password, sizeof ( password ),
95 find_global_dhcp_option ( DHCP_EB_PASSWORD ) );
97 drivenum = find_global_dhcp_num_option ( DHCP_EB_BIOS_DRIVE );
99 return test_iscsiboot ( initiator_iqn, &target.st,
100 rp_components[RP_TARGETNAME], lun,
101 username, password, netdev, drivenum );
104 printf ( "Invalid iSCSI root path\n" );
108 static int test_dhcp_hello ( char *helloname ) {
111 struct sockaddr_in sin;
112 struct sockaddr_tcpip st;
115 memset ( &target, 0, sizeof ( target ) );
116 target.sin.sin_family = AF_INET;
117 target.sin.sin_port = htons ( 80 );
118 message = strchr ( helloname, ':' );
121 printf ( "Invalid hello path\n" );
124 inet_aton ( helloname, &target.sin.sin_addr );
126 test_hello ( &target.st, message );
130 static int test_dhcp_http ( struct net_device *netdev, char *url ) {
132 struct sockaddr_in sin;
133 struct sockaddr_tcpip st;
136 memset ( &target, 0, sizeof ( target ) );
137 target.sin.sin_family = AF_INET;
138 target.sin.sin_port = htons ( 80 );
140 char *addr = url + 7; // http://
141 char *file = strchr(addr, '/');
142 *file = '\0'; // for printf and inet_aton to work
143 printf("connecting to %s\n", addr);
144 inet_aton ( addr, &target.sin.sin_addr );
146 test_http ( netdev, &target.st, file );
150 static int test_dhcp_ftp ( struct net_device *netdev, char *ftpname ) {
152 struct sockaddr_in sin;
153 struct sockaddr_tcpip st;
157 filename = strchr ( ftpname, ':' );
159 printf ( "Invalid FTP path \"%s\"\n", ftpname );
164 memset ( &target, 0, sizeof ( target ) );
165 target.sin.sin_family = AF_INET;
166 target.sin.sin_port = htons ( 21 );
167 inet_aton ( ftpname, &target.sin.sin_addr );
169 test_ftp ( &target, filename );
173 static int test_dhcp_tftp ( struct net_device *netdev, char *tftpname ) {
175 struct sockaddr_in sin;
176 struct sockaddr_tcpip st;
179 memset ( &target, 0, sizeof ( target ) );
180 target.sin.sin_family = AF_INET;
181 target.sin.sin_port = htons ( 69 );
182 find_global_dhcp_ipv4_option ( DHCP_EB_SIADDR,
183 &target.sin.sin_addr );
185 return test_tftp ( netdev, &target.st, tftpname );
188 static int test_dhcp_boot ( struct net_device *netdev, char *filename ) {
190 if ( strncmp ( filename, "aoe:", 4 ) == 0 ) {
191 return test_dhcp_aoe_boot ( netdev, &filename[4] );
194 // if ( strncmp ( filename, "iscsi:", 6 ) == 0 ) {
195 if ( ! filename[0] ) {
196 return test_dhcp_iscsi_boot ( netdev );
199 if ( strncmp ( filename, "ftp:", 4 ) == 0 ) {
200 return test_dhcp_ftp ( netdev, &filename[4] );
204 if ( strncmp ( filename, "hello:", 6 ) == 0 ) {
205 return test_dhcp_hello ( &filename[6] );
207 if ( strncmp ( filename, "http:", 5 ) == 0 ) {
208 return test_dhcp_http ( netdev, filename );
211 return test_dhcp_tftp ( netdev, filename );
213 return -EPROTONOSUPPORT;
216 int test_dhcp ( struct net_device *netdev ) {
217 struct dhcp_session dhcp;
218 struct in_addr address = { htonl ( 0 ) };
219 struct in_addr netmask = { htonl ( 0 ) };
220 struct in_addr gateway = { INADDR_NONE };
224 /* Issue DHCP request */
225 printf ( "DHCP (%s)...", netdev->name );
226 memset ( &dhcp, 0, sizeof ( dhcp ) );
227 dhcp.netdev = netdev;
228 if ( ( rc = async_wait ( start_dhcp ( &dhcp ) ) ) != 0 ) {
229 printf ( "failed\n" );
234 /* Register options received via DHCP */
235 register_dhcp_options ( dhcp.options );
237 /* Retrieve IP address configuration */
238 find_global_dhcp_ipv4_option ( DHCP_EB_YIADDR, &address );
239 find_global_dhcp_ipv4_option ( DHCP_SUBNET_MASK, &netmask );
240 find_global_dhcp_ipv4_option ( DHCP_ROUTERS, &gateway );
242 dhcp_snprintf ( filename, sizeof ( filename ),
243 find_global_dhcp_option ( DHCP_BOOTFILE_NAME ) );
246 printf ( "Bootfile name \"%s\"\n", filename );
248 /* Set up new IP address configuration */
249 if ( ( rc = add_ipv4_address ( netdev, address, netmask,
251 goto out_no_del_ipv4;
256 if ( ( rc = test_dhcp_boot ( netdev, filename ) ) != 0 ) {
257 printf ( "Boot failed: %s\n", strerror ( rc ) );
262 /* Unregister and free DHCP options */
263 unregister_dhcp_options ( dhcp.options );
264 dhcpopt_put ( dhcp.options );