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.
23 #include <gpxe/image.h>
24 #include <gpxe/command.h>
25 #include <usr/fetch.h>
26 #include <usr/imgmgmt.h>
30 * Image management commands
35 * Print image description
40 * "fetch"/"module"/"kernel" command syntax message
42 * @v argv Argument list
44 static void fetch_syntax ( char **argv ) {
46 " %s [-n|--name <name>] filename [arguments...]\n"
48 "Fetch executable/loadable image\n",
53 * The "fetch"/"module"/"kernel" command body
55 * @v argc Argument count
56 * @v argv Argument list
57 * @v name Default name for image, or NULL
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' },
68 char cmdline[ sizeof ( image->cmdline ) ];
74 while ( ( c = getopt_long ( argc, argv, "hn:",
75 longopts, NULL ) ) >= 0 ) {
82 /* Display help text */
84 /* Unrecognised/invalid option */
85 fetch_syntax ( argv );
90 /* Need at least a filename remaining after the options */
91 if ( optind >= argc ) {
92 fetch_syntax ( argv );
95 filename = argv[optind++];
97 /* Build command line */
98 while ( ( used < sizeof ( cmdline ) ) && ( optind < argc ) ) {
99 used += snprintf ( &cmdline[used], sizeof ( cmdline ) - used,
100 " %s", argv[optind++] );
103 /* Allocate and fill struct image */
104 image = malloc ( sizeof ( *image ) );
106 printf ( "Out of memory\n" );
109 memset ( image, 0, sizeof ( *image ) );
111 strncpy ( image->name, name, ( sizeof ( image->name ) - 1 ) );
113 memcpy ( image->cmdline, cmdline, sizeof ( image->cmdline ) );
116 if ( ( rc = fetch ( image, filename ) ) != 0 ) {
117 printf ( "Could not fetch %s: %s\n", filename,
123 /* Register the image */
124 if ( ( rc = register_image ( image ) ) != 0 ) {
125 printf ( "Could not register %s: %s\n", filename,
136 * The "fetch"/"module" command
138 * @v argc Argument count
139 * @v argv Argument list
142 static int fetch_exec ( int argc, char **argv ) {
143 return fetch_exec_name ( argc, argv, NULL );
147 * The "kernel" command
149 * @v argc Argument count
150 * @v argv Argument list
153 static int kernel_exec ( int argc, char **argv ) {
154 return fetch_exec_name ( argc, argv, "kernel" );
158 * "imgstat" command syntax message
160 * @v argv Argument list
162 static void imgstat_syntax ( char **argv ) {
166 "List executable/loadable images\n",
171 * The "imgstat" command
173 * @v argc Argument count
174 * @v argv Argument list
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 },
186 while ( ( c = getopt_long ( argc, argv, "h", longopts, NULL ) ) >= 0 ){
189 /* Display help text */
191 /* Unrecognised/invalid option */
192 imgstat_syntax ( argv );
197 /* Need at least a filename remaining after the options */
198 if ( optind != argc ) {
199 imgstat_syntax ( argv );
203 /* Show status of all images */
204 for_each_image ( image ) {
210 /** Image management commands */
211 struct command image_commands[] __command = {
218 .exec = fetch_exec, /* synonym for "fetch" */
226 .exec = imgstat_exec,