2 * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 FILE_LICENCE ( GPL2_OR_LATER
);
30 #include <gpxe/netdevice.h>
32 #include <gpxe/command.h>
33 #include <usr/dhcpmgmt.h>
34 #include <usr/ifmgmt.h>
38 * DHCP management commands
43 * "dhcp" command syntax message
45 * @v argv Argument list
47 static void dhcp_syntax ( char **argv
) {
49 " %s <interface> [<interface> ...]\n"
52 "Configure a network interface using DHCP\n",
57 * Attempt to configure a device with dhcp
59 * @v netdev Device to configure
62 static int dhcp_one_device ( struct net_device
*netdev
) {
66 if ( ( rc
= dhcp ( netdev
) ) != 0 ) {
67 /* Close the device on error to avoid out-of-memory */
68 netdev_close ( netdev
);
70 printf ( "Could not configure %s: %s\n", netdev
->name
,
79 * Call dhcp_one_device() for each name in argv
81 * @v argc Number of devices
82 * @v argv List of device names
85 static int dhcp_each_device_name ( int argc
, char **argv
) {
88 struct net_device
*netdev
;
90 for ( i
= 0; i
< argc
; i
++ ) {
91 netdev_name
= argv
[i
];
92 netdev
= find_netdev ( netdev_name
);
95 printf ( "No such interface: %s\n", netdev_name
);
99 if ( dhcp_one_device ( netdev
) == 0 )
103 printf ( "Could not configure any interface.\n" );
108 * Call dhcp_one_device() for each device in net_devices
112 static int dhcp_each_device ( void ) {
113 struct net_device
*netdev
;
115 for_each_netdev ( netdev
) {
116 if ( dhcp_one_device ( netdev
) == 0 )
120 printf ( "Could not configure any interface.\n" );
127 * @v argc Argument count
128 * @v argv Argument list
131 static int dhcp_exec ( int argc
, char **argv
) {
132 static struct option longopts
[] = {
133 { "help", 0, NULL
, 'h' },
134 { NULL
, 0, NULL
, 0 },
139 while ( ( c
= getopt_long ( argc
, argv
, "h", longopts
, NULL
) ) >= 0 ){
142 /* Display help text */
144 /* Unrecognised/invalid option */
145 dhcp_syntax ( argv
);
150 /* Need one or more interface names remaining after the options */
151 if ( ( argc
- optind
) < 1 ) {
152 dhcp_syntax ( argv
);
156 if ( strcmp ( argv
[optind
], "any" ) == 0 )
157 return dhcp_each_device();
159 return dhcp_each_device_name ( argc
- optind
, argv
+ optind
);
163 * "pxebs" command syntax message
165 * @v argv Argument list
167 static void pxebs_syntax ( char **argv
) {
169 " %s <interface> <server_type>\n"
171 "Perform PXE Boot Server discovery\n",
176 * The "pxebs" command
178 * @v argc Argument count
179 * @v argv Argument list
182 static int pxebs_exec ( int argc
, char **argv
) {
183 static struct option longopts
[] = {
184 { "help", 0, NULL
, 'h' },
185 { NULL
, 0, NULL
, 0 },
187 const char *netdev_txt
;
188 const char *pxe_type_txt
;
189 struct net_device
*netdev
;
190 unsigned int pxe_type
;
196 while ( ( c
= getopt_long ( argc
, argv
, "h", longopts
, NULL
) ) >= 0 ){
199 /* Display help text */
201 /* Unrecognised/invalid option */
202 pxebs_syntax ( argv
);
206 if ( optind
!= ( argc
- 2 ) ) {
207 pxebs_syntax ( argv
);
210 netdev_txt
= argv
[optind
];
211 pxe_type_txt
= argv
[ optind
+ 1 ];
213 /* Parse arguments */
214 netdev
= find_netdev ( netdev_txt
);
216 printf ( "No such interface: %s\n", netdev_txt
);
219 pxe_type
= strtoul ( pxe_type_txt
, &end
, 0 );
221 printf ( "Bad server type: %s\n", pxe_type_txt
);
225 /* Perform Boot Server Discovery */
226 if ( ( rc
= pxebs ( netdev
, pxe_type
) ) != 0 ) {
227 printf ( "Could not discover boot server on %s: %s\n",
228 netdev
->name
, strerror ( rc
) );
235 /** DHCP management commands */
236 struct command dhcp_commands
[] __command
= {