iPXE
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., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  *
00019  * You can also choose to distribute this program under the terms of
00020  * the Unmodified Binary Distribution Licence (as given in the file
00021  * COPYING.UBDL), provided that you have satisfied its requirements.
00022  */
00023 
00024 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00025 
00026 #include <stdint.h>
00027 #include <stdlib.h>
00028 #include <stdio.h>
00029 #include <errno.h>
00030 #include <ipxe/image.h>
00031 #include <ipxe/downloader.h>
00032 #include <ipxe/monojob.h>
00033 #include <ipxe/open.h>
00034 #include <ipxe/uri.h>
00035 #include <usr/imgmgmt.h>
00036 
00037 /** @file
00038  *
00039  * Image management
00040  *
00041  */
00042 
00043 /**
00044  * Download a new image
00045  *
00046  * @v uri               URI
00047  * @v timeout           Download timeout
00048  * @v image             Image to fill in
00049  * @ret rc              Return status code
00050  */
00051 int imgdownload ( struct uri *uri, unsigned long timeout,
00052                   struct image **image ) {
00053         struct uri uri_redacted;
00054         char *uri_string_redacted;
00055         int rc;
00056 
00057         /* Construct redacted URI */
00058         memcpy ( &uri_redacted, uri, sizeof ( uri_redacted ) );
00059         uri_redacted.user = NULL;
00060         uri_redacted.password = NULL;
00061         uri_redacted.query = NULL;
00062         uri_redacted.fragment = NULL;
00063         uri_string_redacted = format_uri_alloc ( &uri_redacted );
00064         if ( ! uri_string_redacted ) {
00065                 rc = -ENOMEM;
00066                 goto err_uri_string;
00067         }
00068 
00069         /* Resolve URI */
00070         uri = resolve_uri ( cwuri, uri );
00071         if ( ! uri ) {
00072                 rc = -ENOMEM;
00073                 goto err_resolve_uri;
00074         }
00075 
00076         /* Allocate image */
00077         *image = alloc_image ( uri );
00078         if ( ! *image ) {
00079                 rc = -ENOMEM;
00080                 goto err_alloc_image;
00081         }
00082 
00083         /* Create downloader */
00084         if ( ( rc = create_downloader ( &monojob, *image ) ) != 0 ) {
00085                 printf ( "Could not start download: %s\n", strerror ( rc ) );
00086                 goto err_create_downloader;
00087         }
00088 
00089         /* Wait for download to complete */
00090         if ( ( rc = monojob_wait ( uri_string_redacted, timeout ) ) != 0 )
00091                 goto err_monojob_wait;
00092 
00093         /* Register image */
00094         if ( ( rc = register_image ( *image ) ) != 0 ) {
00095                 printf ( "Could not register image: %s\n", strerror ( rc ) );
00096                 goto err_register_image;
00097         }
00098 
00099  err_register_image:
00100  err_monojob_wait:
00101  err_create_downloader:
00102         image_put ( *image );
00103  err_alloc_image:
00104         uri_put ( uri );
00105  err_resolve_uri:
00106         free ( uri_string_redacted );
00107  err_uri_string:
00108         return rc;
00109 }
00110 
00111 /**
00112  * Download a new image
00113  *
00114  * @v uri_string        URI string
00115  * @v timeout           Download timeout
00116  * @v image             Image to fill in
00117  * @ret rc              Return status code
00118  */
00119 int imgdownload_string ( const char *uri_string, unsigned long timeout,
00120                          struct image **image ) {
00121         struct uri *uri;
00122         int rc;
00123 
00124         if ( ! ( uri = parse_uri ( uri_string ) ) )
00125                 return -ENOMEM;
00126 
00127         rc = imgdownload ( uri, timeout, image );
00128 
00129         uri_put ( uri );
00130         return rc;
00131 }
00132 
00133 /**
00134  * Acquire an image
00135  *
00136  * @v name_uri          Name or URI string
00137  * @v timeout           Download timeout
00138  * @v image             Image to fill in
00139  * @ret rc              Return status code
00140  */
00141 int imgacquire ( const char *name_uri, unsigned long timeout,
00142                  struct image **image ) {
00143 
00144         /* If we already have an image with the specified name, use it */
00145         *image = find_image ( name_uri );
00146         if ( *image )
00147                 return 0;
00148 
00149         /* Otherwise, download a new image */
00150         return imgdownload_string ( name_uri, timeout, image );
00151 }
00152 
00153 /**
00154  * Display status of an image
00155  *
00156  * @v image             Executable/loadable image
00157  */
00158 void imgstat ( struct image *image ) {
00159         printf ( "%s : %zd bytes", image->name, image->len );
00160         if ( image->type )
00161                 printf ( " [%s]", image->type->name );
00162         if ( image->flags & IMAGE_TRUSTED )
00163                 printf ( " [TRUSTED]" );
00164         if ( image->flags & IMAGE_SELECTED )
00165                 printf ( " [SELECTED]" );
00166         if ( image->flags & IMAGE_AUTO_UNREGISTER )
00167                 printf ( " [AUTOFREE]" );
00168         if ( image->cmdline )
00169                 printf ( " \"%s\"", image->cmdline );
00170         printf ( "\n" );
00171 }