Browse Source

Add basic "fetch" and "imgstat" commands.

tags/v0.9.3
Michael Brown 18 years ago
parent
commit
9817f93094
7 changed files with 387 additions and 0 deletions
  1. 1
    0
      src/config.h
  2. 3
    0
      src/core/config.c
  3. 228
    0
      src/hci/commands/image_cmd.c
  4. 13
    0
      src/include/usr/fetch.h
  5. 12
    0
      src/include/usr/imgmgmt.h
  6. 85
    0
      src/usr/fetch.c
  7. 45
    0
      src/usr/imgmgmt.c

+ 1
- 0
src/config.h View File

@@ -120,6 +120,7 @@
120 120
 #define	CONFIG_CMD		/* Option configuration console */
121 121
 #define	IFMGMT_CMD		/* Interface management commands */
122 122
 #define	ROUTE_CMD		/* Routing table management commands */
123
+#define IMAGE_CMD		/* Image management commands */
123 124
 
124 125
 /* @END general.h */ 
125 126
 

+ 3
- 0
src/core/config.c View File

@@ -157,6 +157,9 @@ REQUIRE_OBJECT ( ifmgmt_cmd );
157 157
 #ifdef ROUTE_CMD
158 158
 REQUIRE_OBJECT ( route_cmd );
159 159
 #endif
160
+#ifdef IMAGE_CMD
161
+REQUIRE_OBJECT ( image_cmd );
162
+#endif
160 163
 
161 164
 /*
162 165
  * Drag in miscellaneous objects

+ 228
- 0
src/hci/commands/image_cmd.c View File

@@ -0,0 +1,228 @@
1
+/*
2
+ * Copyright (C) 2007 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
+
19
+#include <stdint.h>
20
+#include <stdlib.h>
21
+#include <getopt.h>
22
+#include <vsprintf.h>
23
+#include <gpxe/image.h>
24
+#include <gpxe/command.h>
25
+#include <usr/fetch.h>
26
+#include <usr/imgmgmt.h>
27
+
28
+/** @file
29
+ *
30
+ * Image management commands
31
+ *
32
+ */
33
+
34
+/**
35
+ * Print image description
36
+ *
37
+ */
38
+
39
+/**
40
+ * "fetch"/"module"/"kernel" command syntax message
41
+ *
42
+ * @v argv		Argument list
43
+ */
44
+static void fetch_syntax ( char **argv ) {
45
+	printf ( "Usage:\n"
46
+		 "  %s [-n|--name <name>] filename [arguments...]\n"
47
+		 "\n"
48
+		 "Fetch executable/loadable image\n",
49
+		 argv[0] );
50
+}
51
+
52
+/**
53
+ * The "fetch"/"module"/"kernel" command body
54
+ *
55
+ * @v argc		Argument count
56
+ * @v argv		Argument list
57
+ * @v name		Default name for image, or NULL
58
+ * @ret rc		Exit code
59
+ */
60
+static int fetch_exec_name ( int argc, char **argv, const char *name ) {
61
+	static struct option longopts[] = {
62
+		{ "help", 0, NULL, 'h' },
63
+		{ "name", required_argument, NULL, 'n' },
64
+		{ NULL, 0, NULL, 0 },
65
+	};
66
+	struct image *image;
67
+	const char *filename;
68
+	char cmdline[ sizeof ( image->cmdline ) ];
69
+	size_t used = 0;
70
+	int c;
71
+	int rc;
72
+
73
+	/* Parse options */
74
+	while ( ( c = getopt_long ( argc, argv, "hn:",
75
+				    longopts, NULL ) ) >= 0 ) {
76
+		switch ( c ) {
77
+		case 'n':
78
+			/* Set image name */
79
+			name = optarg;
80
+			break;
81
+		case 'h':
82
+			/* Display help text */
83
+		default:
84
+			/* Unrecognised/invalid option */
85
+			fetch_syntax ( argv );
86
+			return 1;
87
+		}
88
+	}
89
+
90
+	/* Need at least a filename remaining after the options */
91
+	if ( optind >= argc ) {
92
+		fetch_syntax ( argv );
93
+		return 1;
94
+	}
95
+	filename = argv[optind++];
96
+
97
+	/* Build command line */
98
+	while ( ( used < sizeof ( cmdline ) ) && ( optind < argc ) ) {
99
+		used += snprintf ( &cmdline[used], sizeof ( cmdline ) - used,
100
+				   " %s",  argv[optind++] );
101
+	}
102
+
103
+	/* Allocate and fill struct image */
104
+	image = malloc ( sizeof ( *image ) );
105
+	if ( ! image ) {
106
+		printf ( "Out of memory\n" );
107
+		return 1;
108
+	}
109
+	memset ( image, 0, sizeof ( *image ) );
110
+	if ( name )
111
+		strncpy ( image->name, name, ( sizeof ( image->name ) - 1 ) );
112
+	if ( used )
113
+		memcpy ( image->cmdline, cmdline, sizeof ( image->cmdline ) );
114
+
115
+	/* Fetch the file */
116
+	if ( ( rc = fetch ( image, filename ) ) != 0 ) {
117
+		printf ( "Could not fetch %s: %s\n", filename,
118
+			 strerror ( rc ) );
119
+		free ( image );
120
+		return 1;
121
+	}
122
+
123
+	/* Register the image */
124
+	if ( ( rc = register_image ( image ) ) != 0 ) {
125
+		printf ( "Could not register %s: %s\n", filename,
126
+			 strerror ( rc ) );
127
+		free ( image );
128
+		return 1;
129
+	}
130
+
131
+	imgstat ( image );
132
+	return 0;
133
+}
134
+
135
+/**
136
+ * The "fetch"/"module" command
137
+ *
138
+ * @v argc		Argument count
139
+ * @v argv		Argument list
140
+ * @ret rc		Exit code
141
+ */
142
+static int fetch_exec ( int argc, char **argv ) {
143
+	return fetch_exec_name ( argc, argv, NULL );
144
+}
145
+
146
+/**
147
+ * The "kernel" command
148
+ *
149
+ * @v argc		Argument count
150
+ * @v argv		Argument list
151
+ * @ret rc		Exit code
152
+ */
153
+static int kernel_exec ( int argc, char **argv ) {
154
+	return fetch_exec_name ( argc, argv, "kernel" );
155
+}
156
+
157
+/**
158
+ * "imgstat" command syntax message
159
+ *
160
+ * @v argv		Argument list
161
+ */
162
+static void imgstat_syntax ( char **argv ) {
163
+	printf ( "Usage:\n"
164
+		 "  %s\n"
165
+		 "\n"
166
+		 "List executable/loadable images\n",
167
+		 argv[0] );
168
+}
169
+
170
+/**
171
+ * The "imgstat" command
172
+ *
173
+ * @v argc		Argument count
174
+ * @v argv		Argument list
175
+ * @ret rc		Exit code
176
+ */
177
+static int imgstat_exec ( int argc __unused, char **argv __unused ) {
178
+	static struct option longopts[] = {
179
+		{ "help", 0, NULL, 'h' },
180
+		{ NULL, 0, NULL, 0 },
181
+	};
182
+	struct image *image;
183
+	int c;
184
+
185
+	/* Parse options */
186
+	while ( ( c = getopt_long ( argc, argv, "h", longopts, NULL ) ) >= 0 ){
187
+		switch ( c ) {
188
+		case 'h':
189
+			/* Display help text */
190
+		default:
191
+			/* Unrecognised/invalid option */
192
+			imgstat_syntax ( argv );
193
+			return 1;
194
+		}
195
+	}
196
+
197
+	/* Need at least a filename remaining after the options */
198
+	if ( optind != argc ) {
199
+		imgstat_syntax ( argv );
200
+		return 1;
201
+	}
202
+
203
+	/* Show status of all images */
204
+	for_each_image ( image ) {
205
+		imgstat ( image );
206
+	}
207
+	return 0;
208
+}
209
+
210
+/** Image management commands */
211
+struct command image_commands[] __command = {
212
+	{
213
+		.name = "fetch",
214
+		.exec = fetch_exec,
215
+	},
216
+	{
217
+		.name = "module",
218
+		.exec = fetch_exec, /* synonym for "fetch" */
219
+	},
220
+	{
221
+		.name = "kernel",
222
+		.exec = kernel_exec,
223
+	},
224
+	{
225
+		.name = "imgstat",
226
+		.exec = imgstat_exec,
227
+	},
228
+};

+ 13
- 0
src/include/usr/fetch.h View File

@@ -0,0 +1,13 @@
1
+#ifndef _USR_FETCH_H
2
+#define _USR_FETCH_H
3
+
4
+/**
5
+ * @file
6
+ *
7
+ * Fetch file as executable/loadable image
8
+ *
9
+ */
10
+
11
+extern int fetch ( struct image *image, const char *filename );
12
+
13
+#endif /* _USR_FETCH_H */

+ 12
- 0
src/include/usr/imgmgmt.h View File

@@ -0,0 +1,12 @@
1
+#ifndef _USR_IMGMGMT_H
2
+#define _USR_IMGMGMT_H
3
+
4
+/** @file
5
+ *
6
+ * Image management
7
+ *
8
+ */
9
+
10
+extern void imgstat ( struct image *image );
11
+
12
+#endif /* _USR_IMGMGMT_H */

+ 85
- 0
src/usr/fetch.c View File

@@ -0,0 +1,85 @@
1
+/*
2
+ * Copyright (C) 2007 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
+
19
+/**
20
+ * @file
21
+ *
22
+ * Fetch file as executable/loadable image
23
+ *
24
+ */
25
+
26
+#include <libgen.h>
27
+#include <vsprintf.h>
28
+#include <gpxe/emalloc.h>
29
+#include <gpxe/ebuffer.h>
30
+#include <gpxe/image.h>
31
+#include <usr/fetch.h>
32
+
33
+#include <byteswap.h>
34
+#include <gpxe/tftp.h>
35
+#include <gpxe/dhcp.h>
36
+
37
+/**
38
+ * Fetch file as executable/loadable image
39
+ *
40
+ * @v image		Executable/loadable image
41
+ * @v filename		Filename
42
+ * @ret rc		Return status code
43
+ */
44
+int fetch ( struct image *image, const char *filename ) {
45
+	struct buffer buffer;
46
+	int rc;
47
+
48
+	/* Name the image, if it isn't explicitly named */
49
+	if ( ! image->name[0] ) {
50
+		strncpy ( image->name, basename ( filename ),
51
+			  ( sizeof ( image->name ) - 1 ) );
52
+	}
53
+
54
+	/* Allocate an expandable buffer to hold the file */
55
+	if ( ( rc = ebuffer_alloc ( &buffer, 0 ) ) != 0 )
56
+		return rc;
57
+
58
+	/* Retrieve the file */
59
+	struct tftp_session tftp;
60
+	union {
61
+		struct sockaddr_tcpip st;
62
+		struct sockaddr_in sin;
63
+	} server;
64
+
65
+	memset ( &tftp, 0, sizeof ( tftp ) );
66
+	memset ( &server, 0, sizeof ( server ) );
67
+	server.sin.sin_family = AF_INET;
68
+	find_global_dhcp_ipv4_option ( DHCP_EB_SIADDR,
69
+				       &server.sin.sin_addr );
70
+	server.sin.sin_port = htons ( TFTP_PORT );
71
+	udp_connect ( &tftp.udp, &server.st );
72
+	tftp.filename = filename;
73
+	tftp.buffer = &buffer;
74
+	if ( ( rc = async_wait ( tftp_get ( &tftp ) ) ) != 0 ) {
75
+		efree ( buffer.addr );
76
+		return rc;
77
+	}
78
+
79
+	/* Transfer ownserhip of the data buffer to the image */
80
+	image->data = buffer.addr;
81
+	image->len = buffer.fill;
82
+	image->free = efree;
83
+
84
+	return 0;
85
+}

+ 45
- 0
src/usr/imgmgmt.c View File

@@ -0,0 +1,45 @@
1
+/*
2
+ * Copyright (C) 2007 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
+
19
+#include <stdint.h>
20
+#include <vsprintf.h>
21
+#include <gpxe/image.h>
22
+#include <usr/imgmgmt.h>
23
+
24
+/** @file
25
+ *
26
+ * Image management
27
+ *
28
+ */
29
+
30
+/**
31
+ * Display status of an image
32
+ *
33
+ * @v image		Executable/loadable image
34
+ */
35
+void imgstat ( struct image *image ) {
36
+	printf ( "%s: %zd bytes ", image->name, image->len );
37
+	if ( image->type )
38
+		printf ( " [%s]", image->type->name );
39
+	if ( image->flags & IMAGE_LOADED )
40
+		printf ( " [LOADED]" );
41
+	if ( image->cmdline[0] )
42
+		printf ( "\"%s\"", image->cmdline );
43
+	printf ( "\n" );
44
+}
45
+

Loading…
Cancel
Save