Browse Source

Add the "initrd" command

tags/v0.9.3
Michael Brown 18 years ago
parent
commit
8edf8f6fa8
1 changed files with 57 additions and 20 deletions
  1. 57
    20
      src/hci/commands/image_cmd.c

+ 57
- 20
src/hci/commands/image_cmd.c View File

18
 
18
 
19
 #include <stdint.h>
19
 #include <stdint.h>
20
 #include <stdlib.h>
20
 #include <stdlib.h>
21
+#include <errno.h>
21
 #include <libgen.h>
22
 #include <libgen.h>
22
 #include <getopt.h>
23
 #include <getopt.h>
23
 #include <vsprintf.h>
24
 #include <vsprintf.h>
24
 #include <gpxe/image.h>
25
 #include <gpxe/image.h>
25
 #include <gpxe/command.h>
26
 #include <gpxe/command.h>
27
+#include <gpxe/initrd.h>
26
 #include <usr/imgmgmt.h>
28
 #include <usr/imgmgmt.h>
27
 
29
 
28
 /** @file
30
 /** @file
72
  *
74
  *
73
  * @v argc		Argument count
75
  * @v argc		Argument count
74
  * @v argv		Argument list
76
  * @v argv		Argument list
75
- * @v load		Load image after fetching
76
- * @ret rc		Exit code
77
+ * @v load		Image will be automatically loaded after fetching
78
+ * @ret image		Fetched image
79
+ * @ret rc		Return status code
77
  */
80
  */
78
-static int imgfetch_core_exec ( int argc, char **argv, int load ) {
81
+static int imgfetch_core_exec ( int argc, char **argv, int load,
82
+				struct image **image ) {
79
 	static struct option longopts[] = {
83
 	static struct option longopts[] = {
80
 		{ "help", 0, NULL, 'h' },
84
 		{ "help", 0, NULL, 'h' },
81
 		{ "name", required_argument, NULL, 'n' },
85
 		{ "name", required_argument, NULL, 'n' },
82
 		{ NULL, 0, NULL, 0 },
86
 		{ NULL, 0, NULL, 0 },
83
 	};
87
 	};
84
-	struct image *image;
85
 	const char *name = NULL;
88
 	const char *name = NULL;
86
 	char *filename;
89
 	char *filename;
87
 	int c;
90
 	int c;
100
 		default:
103
 		default:
101
 			/* Unrecognised/invalid option */
104
 			/* Unrecognised/invalid option */
102
 			imgfetch_core_syntax ( argv, load );
105
 			imgfetch_core_syntax ( argv, load );
103
-			return 1;
106
+			return -EINVAL;
104
 		}
107
 		}
105
 	}
108
 	}
106
 
109
 
107
 	/* Need at least a filename remaining after the options */
110
 	/* Need at least a filename remaining after the options */
108
 	if ( optind == argc ) {
111
 	if ( optind == argc ) {
109
 		imgfetch_core_syntax ( argv, load );
112
 		imgfetch_core_syntax ( argv, load );
110
-		return 1;
113
+		return -EINVAL;
111
 	}
114
 	}
112
 	filename = argv[optind++];
115
 	filename = argv[optind++];
113
 	if ( ! name )
116
 	if ( ! name )
114
 		name = basename ( filename );
117
 		name = basename ( filename );
115
 
118
 
116
 	/* Fetch the image */
119
 	/* Fetch the image */
117
-	if ( ( rc = imgfetch ( filename, name, &image ) ) != 0 ) {
120
+	if ( ( rc = imgfetch ( filename, name, image ) ) != 0 ) {
118
 		printf ( "Could not fetch %s: %s\n", name, strerror ( rc ) );
121
 		printf ( "Could not fetch %s: %s\n", name, strerror ( rc ) );
119
-		return 1;
122
+		return rc;
120
 	}
123
 	}
121
 
124
 
122
 	/* Fill in command line */
125
 	/* Fill in command line */
123
-	imgfill_cmdline ( image, ( argc - optind ), &argv[optind] );
124
-
125
-	/* Load image if required */
126
-	if ( load ) {
127
-		if ( ( rc = imgload ( image ) ) != 0 ) {
128
-			printf ( "Could not load %s: %s\n", name,
129
-				 strerror ( rc ) );
130
-			return 1;
131
-		}
132
-	}
126
+	imgfill_cmdline ( *image, ( argc - optind ), &argv[optind] );
133
 
127
 
134
 	return 0;
128
 	return 0;
135
 }
129
 }
142
  * @ret rc		Exit code
136
  * @ret rc		Exit code
143
  */
137
  */
144
 static int imgfetch_exec ( int argc, char **argv ) {
138
 static int imgfetch_exec ( int argc, char **argv ) {
145
-	return imgfetch_core_exec ( argc, argv, 0 );
139
+	struct image *image;
140
+	int rc;
141
+
142
+	if ( ( rc = imgfetch_core_exec ( argc, argv, 0, &image ) ) != 0 )
143
+		return 1;
144
+
145
+	return 0;
146
 }
146
 }
147
 
147
 
148
 /**
148
 /**
153
  * @ret rc		Exit code
153
  * @ret rc		Exit code
154
  */
154
  */
155
 static int kernel_exec ( int argc, char **argv ) {
155
 static int kernel_exec ( int argc, char **argv ) {
156
-	return imgfetch_core_exec ( argc, argv, 1  );
156
+	struct image *image;
157
+	int rc;
158
+
159
+	if ( ( rc = imgfetch_core_exec ( argc, argv, 1, &image ) != 0 ) )
160
+		return 1;
161
+
162
+	/* Load image */
163
+	if ( ( rc = imgload ( image ) ) != 0 ) {
164
+		printf ( "Could not load %s: %s\n", image->name,
165
+			 strerror ( rc ) );
166
+		return 1;
167
+	}
168
+
169
+	return 0;
170
+}
171
+
172
+/**
173
+ * The "initrd" command
174
+ *
175
+ * @v argc		Argument count
176
+ * @v argv		Argument list
177
+ * @ret rc		Exit code
178
+ */
179
+static int initrd_exec ( int argc, char **argv ) {
180
+	struct image *image;
181
+	int rc;
182
+
183
+	if ( ( rc = imgfetch_core_exec ( argc, argv, 0, &image ) != 0 ) )
184
+		return 1;
185
+
186
+	/* Mark image as an intird */
187
+	image->type = &initrd_image_type;
188
+
189
+	return 0;
157
 }
190
 }
158
 
191
 
159
 /**
192
 /**
473
 		.name = "kernel",
506
 		.name = "kernel",
474
 		.exec = kernel_exec,
507
 		.exec = kernel_exec,
475
 	},
508
 	},
509
+	{
510
+		.name = "initrd",
511
+		.exec = initrd_exec,
512
+	},
476
 	{
513
 	{
477
 		.name = "imgload",
514
 		.name = "imgload",
478
 		.exec = imgload_exec,
515
 		.exec = imgload_exec,

Loading…
Cancel
Save