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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2011-03-09 17:55:51 +01:00
|
|
|
/**
|
|
|
|
* Download an image
|
|
|
|
*
|
2011-01-27 19:48:47 +01:00
|
|
|
* @v uri URI
|
2011-03-09 17:55:51 +01:00
|
|
|
* @v name Image name, or NULL to use default
|
|
|
|
* @v cmdline Command line, or NULL for no command line
|
2011-10-25 02:41:41 +02:00
|
|
|
* @v action Action to take upon a successful download, or NULL
|
2007-01-12 08:22:20 +01:00
|
|
|
* @ret rc Return status code
|
|
|
|
*/
|
2011-03-09 17:55:51 +01:00
|
|
|
int imgdownload ( struct uri *uri, const char *name, const char *cmdline,
|
2011-03-02 20:29:24 +01:00
|
|
|
int ( * action ) ( struct image *image ) ) {
|
2011-03-09 17:55:51 +01:00
|
|
|
struct image *image;
|
2011-01-27 19:48:47 +01:00
|
|
|
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-03-09 17:55:51 +01:00
|
|
|
/* Allocate image */
|
|
|
|
image = alloc_image();
|
2011-10-25 02:41:41 +02:00
|
|
|
if ( ! image ) {
|
|
|
|
rc = -ENOMEM;
|
|
|
|
goto err_alloc_image;
|
|
|
|
}
|
2011-03-09 17:55:51 +01:00
|
|
|
|
|
|
|
/* Set image name */
|
2011-03-27 20:19:47 +02:00
|
|
|
if ( name )
|
|
|
|
image_set_name ( image, name );
|
2011-03-09 17:55:51 +01:00
|
|
|
|
2011-01-27 19:48:47 +01:00
|
|
|
/* Set image URI */
|
2007-08-02 21:18:32 +02:00
|
|
|
image_set_uri ( image, uri );
|
|
|
|
|
2011-03-09 17:55:51 +01:00
|
|
|
/* Set image command line */
|
|
|
|
image_set_cmdline ( image, cmdline );
|
|
|
|
|
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 */
|
2011-03-02 20:29:24 +01:00
|
|
|
if ( ( rc = create_downloader ( &monojob, image, LOCATION_URI,
|
2011-03-09 17:55:51 +01:00
|
|
|
uri ) ) != 0 ) {
|
2011-10-25 02:41:41 +02:00
|
|
|
goto err_create_downloader;
|
2011-03-09 17:55:51 +01:00
|
|
|
}
|
2011-01-27 19:48:47 +01:00
|
|
|
|
|
|
|
/* Wait for download to complete */
|
2011-10-25 02:41:41 +02:00
|
|
|
if ( ( rc = monojob_wait ( uri_string_redacted ) ) != 0 )
|
|
|
|
goto err_monojob_wait;
|
2011-01-27 19:48:47 +01:00
|
|
|
|
2011-10-25 02:41:41 +02:00
|
|
|
/* Register image */
|
|
|
|
if ( ( rc = register_image ( image ) ) != 0 )
|
|
|
|
goto err_register_image;
|
|
|
|
|
|
|
|
/* Drop local reference to image. Image is guaranteed to
|
|
|
|
* remain in scope since it is registered.
|
2011-03-09 17:55:51 +01:00
|
|
|
*/
|
2011-10-25 02:41:41 +02:00
|
|
|
image_put ( image );
|
2011-03-02 20:29:24 +01:00
|
|
|
|
2011-10-25 02:41:41 +02:00
|
|
|
/* Carry out specified post-download action, if applicable */
|
|
|
|
return ( action ? action ( image ) : 0 );
|
|
|
|
|
|
|
|
err_register_image:
|
|
|
|
err_monojob_wait:
|
|
|
|
err_create_downloader:
|
|
|
|
image_put ( image );
|
|
|
|
err_alloc_image:
|
|
|
|
return rc;
|
2011-01-27 19:48:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-03-09 17:55:51 +01:00
|
|
|
* Download an image
|
2011-01-27 19:48:47 +01:00
|
|
|
*
|
|
|
|
* @v uri_string URI as a string (e.g. "http://www.nowhere.com/vmlinuz")
|
2011-03-09 17:55:51 +01:00
|
|
|
* @v name Image name, or NULL to use default
|
|
|
|
* @v cmdline Command line, or NULL for no command line
|
2011-03-02 20:29:24 +01:00
|
|
|
* @v action Action to take upon a successful download
|
2011-01-27 19:48:47 +01:00
|
|
|
* @ret rc Return status code
|
|
|
|
*/
|
2011-03-09 17:55:51 +01:00
|
|
|
int imgdownload_string ( const char *uri_string, const char *name,
|
|
|
|
const char *cmdline,
|
|
|
|
int ( * action ) ( struct image *image ) ) {
|
2011-01-27 19:48:47 +01:00
|
|
|
struct uri *uri;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
if ( ! ( uri = parse_uri ( uri_string ) ) )
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2011-03-09 17:55:51 +01:00
|
|
|
rc = imgdownload ( uri, name, cmdline, action );
|
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;
|
|
|
|
}
|
|
|
|
|
2007-01-12 07:05:27 +01:00
|
|
|
/**
|
|
|
|
* Display status of an image
|
|
|
|
*
|
|
|
|
* @v image Executable/loadable image
|
|
|
|
*/
|
|
|
|
void imgstat ( struct image *image ) {
|
2011-03-07 01:37:50 +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 );
|
2012-03-22 14:39:45 +01:00
|
|
|
if ( image->flags & IMAGE_TRUSTED )
|
|
|
|
printf ( " [TRUSTED]" );
|
2011-03-07 01:37:50 +01:00
|
|
|
if ( image->flags & IMAGE_SELECTED )
|
|
|
|
printf ( " [SELECTED]" );
|
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 );
|
|
|
|
}
|