2007-01-12 07:05:27 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2 of the
|
|
|
|
* License, or any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
*/
|
|
|
|
|
2009-05-01 16:41:06 +02:00
|
|
|
FILE_LICENCE ( GPL2_OR_LATER );
|
|
|
|
|
2007-01-12 07:05:27 +01:00
|
|
|
#include <stdint.h>
|
2007-01-12 08:22:20 +01:00
|
|
|
#include <stdlib.h>
|
2007-01-19 02:13:12 +01:00
|
|
|
#include <stdio.h>
|
2007-01-12 08:22:20 +01:00
|
|
|
#include <errno.h>
|
2010-04-19 21:16:01 +02:00
|
|
|
#include <ipxe/image.h>
|
|
|
|
#include <ipxe/downloader.h>
|
|
|
|
#include <ipxe/monojob.h>
|
|
|
|
#include <ipxe/open.h>
|
|
|
|
#include <ipxe/uri.h>
|
2007-01-12 07:05:27 +01:00
|
|
|
#include <usr/imgmgmt.h>
|
|
|
|
|
|
|
|
/** @file
|
|
|
|
*
|
|
|
|
* Image management
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2007-01-12 08:22:20 +01:00
|
|
|
/**
|
2011-01-27 19:48:47 +01:00
|
|
|
* Download an image
|
2007-01-12 08:22:20 +01:00
|
|
|
*
|
2011-01-27 19:48:47 +01:00
|
|
|
* @v image Image
|
|
|
|
* @v uri URI
|
|
|
|
* @v image_register Action to take upon a successful download
|
2007-01-12 08:22:20 +01:00
|
|
|
* @ret rc Return status code
|
|
|
|
*/
|
2011-01-27 19:48:47 +01:00
|
|
|
int imgdownload ( struct image *image, struct uri *uri,
|
|
|
|
int ( * image_register ) ( struct image *image ) ) {
|
|
|
|
size_t len = ( unparse_uri ( NULL, 0, uri, URI_ALL ) + 1 );
|
|
|
|
char uri_string_redacted[len];
|
2009-02-17 13:10:35 +01:00
|
|
|
const char *password;
|
2007-01-12 08:22:20 +01:00
|
|
|
int rc;
|
|
|
|
|
2011-01-27 19:48:47 +01:00
|
|
|
/* Set image URI */
|
2007-08-02 21:18:32 +02:00
|
|
|
image_set_uri ( image, uri );
|
|
|
|
|
2009-02-17 13:10:35 +01:00
|
|
|
/* Redact password portion of URI, if necessary */
|
|
|
|
password = uri->password;
|
|
|
|
if ( password )
|
|
|
|
uri->password = "***";
|
|
|
|
unparse_uri ( uri_string_redacted, sizeof ( uri_string_redacted ),
|
2009-12-30 04:36:04 +01:00
|
|
|
uri, URI_ALL );
|
2009-02-17 13:10:35 +01:00
|
|
|
uri->password = password;
|
|
|
|
|
2011-01-27 19:48:47 +01:00
|
|
|
/* Create downloader */
|
2007-08-02 21:18:32 +02:00
|
|
|
if ( ( rc = create_downloader ( &monojob, image, image_register,
|
2011-01-27 19:48:47 +01:00
|
|
|
LOCATION_URI, uri ) ) != 0 )
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
/* Wait for download to complete */
|
|
|
|
if ( ( rc = monojob_wait ( uri_string_redacted ) ) != 0 )
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch an image
|
|
|
|
*
|
|
|
|
* @v image Image
|
|
|
|
* @v uri_string URI as a string (e.g. "http://www.nowhere.com/vmlinuz")
|
|
|
|
* @v image_register Action to take upon a successful fetch
|
|
|
|
* @ret rc Return status code
|
|
|
|
*/
|
|
|
|
int imgfetch ( struct image *image, const char *uri_string,
|
|
|
|
int ( * image_register ) ( struct image *image ) ) {
|
|
|
|
struct uri *uri;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
if ( ! ( uri = parse_uri ( uri_string ) ) )
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
rc = imgdownload ( image, uri, image_register );
|
2007-01-12 08:22:20 +01:00
|
|
|
|
2007-08-02 21:18:32 +02:00
|
|
|
uri_put ( uri );
|
2007-01-12 08:22:20 +01:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load an image
|
|
|
|
*
|
|
|
|
* @v image Image
|
|
|
|
* @ret rc Return status code
|
|
|
|
*/
|
|
|
|
int imgload ( struct image *image ) {
|
2007-01-12 10:46:10 +01:00
|
|
|
int rc;
|
|
|
|
|
|
|
|
/* Try to load image */
|
|
|
|
if ( ( rc = image_autoload ( image ) ) != 0 )
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
return 0;
|
2007-01-12 08:22:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute an image
|
|
|
|
*
|
|
|
|
* @v image Image
|
|
|
|
* @ret rc Return status code
|
|
|
|
*/
|
|
|
|
int imgexec ( struct image *image ) {
|
|
|
|
return image_exec ( image );
|
|
|
|
}
|
|
|
|
|
2007-01-12 08:32:46 +01:00
|
|
|
/**
|
2008-07-08 04:50:44 +02:00
|
|
|
* Identify the only loaded image
|
2007-01-12 08:32:46 +01:00
|
|
|
*
|
2008-07-08 04:50:44 +02:00
|
|
|
* @ret image Image, or NULL if 0 or >1 images are loaded
|
2007-01-12 08:32:46 +01:00
|
|
|
*/
|
|
|
|
struct image * imgautoselect ( void ) {
|
|
|
|
struct image *image;
|
2008-07-08 04:50:44 +02:00
|
|
|
struct image *selected_image = NULL;
|
|
|
|
int flagged_images = 0;
|
2007-01-12 08:32:46 +01:00
|
|
|
|
|
|
|
for_each_image ( image ) {
|
2008-07-08 04:50:44 +02:00
|
|
|
if ( image->flags & IMAGE_LOADED ) {
|
|
|
|
selected_image = image;
|
|
|
|
flagged_images++;
|
|
|
|
}
|
2007-01-12 08:32:46 +01:00
|
|
|
}
|
|
|
|
|
2008-07-08 04:50:44 +02:00
|
|
|
return ( ( flagged_images == 1 ) ? selected_image : NULL );
|
2007-01-12 08:32:46 +01:00
|
|
|
}
|
|
|
|
|
2007-01-12 07:05:27 +01:00
|
|
|
/**
|
|
|
|
* Display status of an image
|
|
|
|
*
|
|
|
|
* @v image Executable/loadable image
|
|
|
|
*/
|
|
|
|
void imgstat ( struct image *image ) {
|
2007-01-12 08:22:20 +01:00
|
|
|
printf ( "%s: %zd bytes", image->name, image->len );
|
2007-01-12 07:05:27 +01:00
|
|
|
if ( image->type )
|
|
|
|
printf ( " [%s]", image->type->name );
|
|
|
|
if ( image->flags & IMAGE_LOADED )
|
|
|
|
printf ( " [LOADED]" );
|
2007-08-02 21:18:32 +02:00
|
|
|
if ( image->cmdline )
|
2007-01-12 08:22:20 +01:00
|
|
|
printf ( " \"%s\"", image->cmdline );
|
2007-01-12 07:05:27 +01:00
|
|
|
printf ( "\n" );
|
|
|
|
}
|
|
|
|
|
2007-01-12 08:22:20 +01:00
|
|
|
/**
|
|
|
|
* Free an image
|
|
|
|
*
|
|
|
|
* @v image Executable/loadable image
|
|
|
|
*/
|
|
|
|
void imgfree ( struct image *image ) {
|
|
|
|
unregister_image ( image );
|
|
|
|
}
|