imgmgmt.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00017  */
00018 
00019 FILE_LICENCE ( GPL2_OR_LATER );
00020 
00021 #include <stdint.h>
00022 #include <stdlib.h>
00023 #include <stdio.h>
00024 #include <errno.h>
00025 #include <gpxe/image.h>
00026 #include <gpxe/downloader.h>
00027 #include <gpxe/monojob.h>
00028 #include <gpxe/open.h>
00029 #include <gpxe/uri.h>
00030 #include <usr/imgmgmt.h>
00031 
00032 /** @file
00033  *
00034  * Image management
00035  *
00036  */
00037 
00038 /**
00039  * Fetch an image
00040  *
00041  * @v uri_string        URI as a string (e.g. "http://www.nowhere.com/vmlinuz")
00042  * @v name              Name for image, or NULL
00043  * @v register_image    Image registration routine
00044  * @ret rc              Return status code
00045  */
00046 int imgfetch ( struct image *image, const char *uri_string,
00047                int ( * image_register ) ( struct image *image ) ) {
00048         char uri_string_redacted[ strlen ( uri_string ) + 3 /* "***" */
00049                                   + 1 /* NUL */ ];
00050         struct uri *uri;
00051         const char *password;
00052         int rc;
00053 
00054         if ( ! ( uri = parse_uri ( uri_string ) ) )
00055                 return -ENOMEM;
00056 
00057         image_set_uri ( image, uri );
00058 
00059         /* Redact password portion of URI, if necessary */
00060         password = uri->password;
00061         if ( password )
00062                 uri->password = "***";
00063         unparse_uri ( uri_string_redacted, sizeof ( uri_string_redacted ),
00064                       uri, URI_ALL );
00065         uri->password = password;
00066 
00067         if ( ( rc = create_downloader ( &monojob, image, image_register,
00068                                         LOCATION_URI, uri ) ) == 0 )
00069                 rc = monojob_wait ( uri_string_redacted );
00070 
00071         uri_put ( uri );
00072         return rc;
00073 }
00074 
00075 /**
00076  * Load an image
00077  *
00078  * @v image             Image
00079  * @ret rc              Return status code
00080  */
00081 int imgload ( struct image *image ) {
00082         int rc;
00083 
00084         /* Try to load image */
00085         if ( ( rc = image_autoload ( image ) ) != 0 )
00086                 return rc;
00087 
00088         return 0;
00089 }
00090 
00091 /**
00092  * Execute an image
00093  *
00094  * @v image             Image
00095  * @ret rc              Return status code
00096  */
00097 int imgexec ( struct image *image ) {
00098         return image_exec ( image );
00099 }
00100 
00101 /**
00102  * Identify the only loaded image
00103  *
00104  * @ret image           Image, or NULL if 0 or >1 images are loaded
00105  */
00106 struct image * imgautoselect ( void ) {
00107         struct image *image;
00108         struct image *selected_image = NULL;
00109         int flagged_images = 0;
00110 
00111         for_each_image ( image ) {
00112                 if ( image->flags & IMAGE_LOADED ) {
00113                         selected_image = image;
00114                         flagged_images++;
00115                 }
00116         }
00117 
00118         return ( ( flagged_images == 1 ) ? selected_image : NULL );
00119 }
00120 
00121 /**
00122  * Display status of an image
00123  *
00124  * @v image             Executable/loadable image
00125  */
00126 void imgstat ( struct image *image ) {
00127         printf ( "%s: %zd bytes", image->name, image->len );
00128         if ( image->type )
00129                 printf ( " [%s]", image->type->name );
00130         if ( image->flags & IMAGE_LOADED )
00131                 printf ( " [LOADED]" );
00132         if ( image->cmdline )
00133                 printf ( " \"%s\"", image->cmdline );
00134         printf ( "\n" );
00135 }
00136 
00137 /**
00138  * Free an image
00139  *
00140  * @v image             Executable/loadable image
00141  */
00142 void imgfree ( struct image *image ) {
00143         unregister_image ( image );
00144 }

Generated on Tue Apr 6 20:01:11 2010 for gPXE by  doxygen 1.5.7.1