Parcourir la source

[autoboot] Use generic option-parsing library

Total saving: 32 bytes.

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown il y a 14 ans
Parent
révision
17b337d4a8
3 fichiers modifiés avec 95 ajouts et 36 suppressions
  1. 89
    32
      src/hci/commands/autoboot_cmd.c
  2. 1
    1
      src/include/usr/autoboot.h
  3. 5
    3
      src/usr/autoboot.c

+ 89
- 32
src/hci/commands/autoboot_cmd.c Voir le fichier

@@ -1,53 +1,110 @@
1
+/*
2
+ * Copyright (C) 2010 Michael Brown <mbrown@fensystems.co.uk>.
3
+ *
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.
8
+ *
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.
13
+ *
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.
17
+ */
18
+
1 19
 #include <stdio.h>
20
+#include <getopt.h>
2 21
 #include <ipxe/command.h>
22
+#include <ipxe/parseopt.h>
3 23
 #include <ipxe/netdevice.h>
4 24
 #include <usr/autoboot.h>
5 25
 
6 26
 FILE_LICENCE ( GPL2_OR_LATER );
7 27
 
28
+/** @file
29
+ *
30
+ * Booting commands
31
+ *
32
+ */
33
+
34
+/** "autoboot" options */
35
+struct autoboot_options {};
36
+
37
+/** "autoboot" option list */
38
+static struct option_descriptor autoboot_opts[] = {};
39
+
40
+/** "autoboot" command descriptor */
41
+static struct command_descriptor autoboot_cmd =
42
+	COMMAND_DESC ( struct autoboot_options, autoboot_opts, 0, 0,
43
+		       "",
44
+		       "Attempt to boot the system" );
45
+
46
+/**
47
+ * "autoboot" command
48
+ *
49
+ * @v argc		Argument count
50
+ * @v argv		Argument list
51
+ * @ret rc		Return status code
52
+ */
8 53
 static int autoboot_exec ( int argc, char **argv ) {
54
+	struct autoboot_options opts;
55
+	int rc;
9 56
 
10
-	if ( argc != 1 ) {
11
-		printf ( "Usage:\n"
12
-			 "  %s\n"
13
-			 "\n"
14
-			 "Attempts to boot the system\n",
15
-			 argv[0] );
16
-		return 1;
17
-	}
57
+	/* Parse options */
58
+	if ( ( rc = parse_options ( argc, argv, &autoboot_cmd, &opts ) ) != 0 )
59
+		return rc;
18 60
 
19
-	autoboot();
61
+	/* Try to boot */
62
+	if ( ( rc = autoboot() ) != 0 )
63
+		return rc;
20 64
 
21
-	/* Can never return success by definition */
22
-	return 1;
65
+	return 0;
23 66
 }
24 67
 
68
+/** "netboot" options */
69
+struct netboot_options {};
70
+
71
+/** "netboot" option list */
72
+static struct option_descriptor netboot_opts[] = {};
73
+
74
+/** "netboot" command descriptor */
75
+static struct command_descriptor netboot_cmd =
76
+	COMMAND_DESC ( struct netboot_options, netboot_opts, 1, 1,
77
+		       "<interface>",
78
+		       "Attempt to boot the system from <interface>" );
79
+
80
+/**
81
+ * "netboot" command
82
+ *
83
+ * @v argc		Argument count
84
+ * @v argv		Argument list
85
+ * @ret rc		Return status code
86
+ */
25 87
 static int netboot_exec ( int argc, char **argv ) {
26
-	const char *netdev_name;
88
+	struct netboot_options opts;
27 89
 	struct net_device *netdev;
90
+	int rc;
91
+
92
+	/* Parse options */
93
+	if ( ( rc = parse_options ( argc, argv, &netboot_cmd, &opts ) ) != 0 )
94
+		return rc;
95
+
96
+	/* Parse interface */
97
+	if ( ( rc = parse_netdev ( argv[optind], &netdev ) ) != 0 )
98
+		return rc;
99
+
100
+	/* Try to boot */
101
+	if ( ( rc = netboot ( netdev ) ) != 0 )
102
+		return rc;
28 103
 
29
-	if ( argc != 2 ) {
30
-		printf ( "Usage:\n"
31
-			 "  %s <interface>\n"
32
-			 "\n"
33
-			 "Attempts to boot the system from <interface>\n",
34
-			 argv[0] );
35
-		return 1;
36
-	}
37
-	netdev_name = argv[1];
38
-
39
-	netdev = find_netdev ( netdev_name );
40
-	if ( ! netdev ) {
41
-		printf ( "%s: no such interface\n", netdev_name );
42
-		return 1;
43
-	}
44
-
45
-	netboot ( netdev );
46
-
47
-	/* Can never return success by definition */
48
-	return 1;
104
+	return 0;
49 105
 }
50 106
 
107
+/** Booting commands */
51 108
 struct command autoboot_commands[] __command = {
52 109
 	{
53 110
 		.name = "autoboot",

+ 1
- 1
src/include/usr/autoboot.h Voir le fichier

@@ -15,7 +15,7 @@ struct net_device;
15 15
 extern int shutdown_exit_flags;
16 16
 
17 17
 extern int netboot ( struct net_device *netdev );
18
-extern void autoboot ( void );
18
+extern int autoboot ( void );
19 19
 extern int boot_next_server_and_filename ( struct in_addr next_server,
20 20
 					   const char *filename );
21 21
 extern int boot_root_path ( const char *root_path );

+ 5
- 3
src/usr/autoboot.c Voir le fichier

@@ -293,22 +293,24 @@ static void close_all_netdevs ( void ) {
293 293
 /**
294 294
  * Boot the system
295 295
  */
296
-void autoboot ( void ) {
296
+int autoboot ( void ) {
297 297
 	struct net_device *boot_netdev;
298 298
 	struct net_device *netdev;
299
+	int rc = -ENODEV;
299 300
 
300 301
 	/* If we have an identifable boot device, try that first */
301 302
 	close_all_netdevs();
302 303
 	if ( ( boot_netdev = find_boot_netdev() ) )
303
-		netboot ( boot_netdev );
304
+		rc = netboot ( boot_netdev );
304 305
 
305 306
 	/* If that fails, try booting from any of the other devices */
306 307
 	for_each_netdev ( netdev ) {
307 308
 		if ( netdev == boot_netdev )
308 309
 			continue;
309 310
 		close_all_netdevs();
310
-		netboot ( netdev );
311
+		rc = netboot ( netdev );
311 312
 	}
312 313
 
313 314
 	printf ( "No more network devices\n" );
315
+	return rc;
314 316
 }

Chargement…
Annuler
Enregistrer