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., 51 Franklin Street, Fifth Floor, Boston, MA
20 FILE_LICENCE ( GPL2_OR_LATER );
26 #include <ipxe/image.h>
27 #include <ipxe/downloader.h>
28 #include <ipxe/monojob.h>
29 #include <ipxe/open.h>
31 #include <usr/imgmgmt.h>
40 * Download a new image
43 * @v timeout Download timeout
44 * @v image Image to fill in
45 * @ret rc Return status code
47 int imgdownload ( struct uri *uri, unsigned long timeout,
48 struct image **image ) {
50 char *uri_string_redacted;
53 /* Construct redacted URI */
54 password = uri->password;
56 uri->password = "***";
57 uri_string_redacted = format_uri_alloc ( uri );
58 uri->password = password;
59 if ( ! uri_string_redacted ) {
65 uri = resolve_uri ( cwuri, uri );
72 *image = alloc_image ( uri );
78 /* Create downloader */
79 if ( ( rc = create_downloader ( &monojob, *image ) ) != 0 ) {
80 printf ( "Could not start download: %s\n", strerror ( rc ) );
81 goto err_create_downloader;
84 /* Wait for download to complete */
85 if ( ( rc = monojob_wait ( uri_string_redacted, timeout ) ) != 0 )
86 goto err_monojob_wait;
89 if ( ( rc = register_image ( *image ) ) != 0 ) {
90 printf ( "Could not register image: %s\n", strerror ( rc ) );
91 goto err_register_image;
96 err_create_downloader:
101 free ( uri_string_redacted );
107 * Download a new image
109 * @v uri_string URI string
110 * @v timeout Download timeout
111 * @v image Image to fill in
112 * @ret rc Return status code
114 int imgdownload_string ( const char *uri_string, unsigned long timeout,
115 struct image **image ) {
119 if ( ! ( uri = parse_uri ( uri_string ) ) )
122 rc = imgdownload ( uri, timeout, image );
131 * @v name_uri Name or URI string
132 * @v timeout Download timeout
133 * @v image Image to fill in
134 * @ret rc Return status code
136 int imgacquire ( const char *name_uri, unsigned long timeout,
137 struct image **image ) {
139 /* If we already have an image with the specified name, use it */
140 *image = find_image ( name_uri );
144 /* Otherwise, download a new image */
145 return imgdownload_string ( name_uri, timeout, image );
149 * Display status of an image
151 * @v image Executable/loadable image
153 void imgstat ( struct image *image ) {
154 printf ( "%s : %zd bytes", image->name, image->len );
156 printf ( " [%s]", image->type->name );
157 if ( image->flags & IMAGE_TRUSTED )
158 printf ( " [TRUSTED]" );
159 if ( image->flags & IMAGE_SELECTED )
160 printf ( " [SELECTED]" );
161 if ( image->flags & IMAGE_AUTO_UNREGISTER )
162 printf ( " [AUTOFREE]" );
163 if ( image->cmdline )
164 printf ( " \"%s\"", image->cmdline );