[misc] Remove accidentally commited .dotest directory
[gpxe.git] / src / usr / imgmgmt.c
blob023e3f0f52e55c15bb79bf700fa0d86096db20f9
1 /*
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.
19 FILE_LICENCE ( GPL2_OR_LATER );
21 #include <stdint.h>
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <errno.h>
25 #include <gpxe/image.h>
26 #include <gpxe/downloader.h>
27 #include <gpxe/monojob.h>
28 #include <gpxe/open.h>
29 #include <gpxe/uri.h>
30 #include <usr/imgmgmt.h>
32 /** @file
34 * Image management
38 /**
39 * Fetch an image
41 * @v uri_string URI as a string (e.g. "http://www.nowhere.com/vmlinuz")
42 * @v name Name for image, or NULL
43 * @v register_image Image registration routine
44 * @ret rc Return status code
46 int imgfetch ( struct image *image, const char *uri_string,
47 int ( * image_register ) ( struct image *image ) ) {
48 char uri_string_redacted[ strlen ( uri_string ) + 3 /* "***" */
49 + 1 /* NUL */ ];
50 struct uri *uri;
51 const char *password;
52 int rc;
54 if ( ! ( uri = parse_uri ( uri_string ) ) )
55 return -ENOMEM;
57 image_set_uri ( image, uri );
59 /* Redact password portion of URI, if necessary */
60 password = uri->password;
61 if ( password )
62 uri->password = "***";
63 unparse_uri ( uri_string_redacted, sizeof ( uri_string_redacted ),
64 uri, URI_ALL );
65 uri->password = password;
67 if ( ( rc = create_downloader ( &monojob, image, image_register,
68 LOCATION_URI, uri ) ) == 0 )
69 rc = monojob_wait ( uri_string_redacted );
71 uri_put ( uri );
72 return rc;
75 /**
76 * Load an image
78 * @v image Image
79 * @ret rc Return status code
81 int imgload ( struct image *image ) {
82 int rc;
84 /* Try to load image */
85 if ( ( rc = image_autoload ( image ) ) != 0 )
86 return rc;
88 return 0;
91 /**
92 * Execute an image
94 * @v image Image
95 * @ret rc Return status code
97 int imgexec ( struct image *image ) {
98 return image_exec ( image );
102 * Identify the only loaded image
104 * @ret image Image, or NULL if 0 or >1 images are loaded
106 struct image * imgautoselect ( void ) {
107 struct image *image;
108 struct image *selected_image = NULL;
109 int flagged_images = 0;
111 for_each_image ( image ) {
112 if ( image->flags & IMAGE_LOADED ) {
113 selected_image = image;
114 flagged_images++;
118 return ( ( flagged_images == 1 ) ? selected_image : NULL );
122 * Display status of an image
124 * @v image Executable/loadable image
126 void imgstat ( struct image *image ) {
127 printf ( "%s: %zd bytes", image->name, image->len );
128 if ( image->type )
129 printf ( " [%s]", image->type->name );
130 if ( image->flags & IMAGE_LOADED )
131 printf ( " [LOADED]" );
132 if ( image->cmdline )
133 printf ( " \"%s\"", image->cmdline );
134 printf ( "\n" );
138 * Free an image
140 * @v image Executable/loadable image
142 void imgfree ( struct image *image ) {
143 unregister_image ( image );