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
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL
);
30 #include <ipxe/image.h>
31 #include <ipxe/downloader.h>
32 #include <ipxe/monojob.h>
33 #include <ipxe/open.h>
35 #include <usr/imgmgmt.h>
44 * Download a new image
47 * @v timeout Download timeout
48 * @v image Image to fill in
49 * @ret rc Return status code
51 int imgdownload ( struct uri
*uri
, unsigned long timeout
,
52 struct image
**image
) {
54 char *uri_string_redacted
;
57 /* Construct redacted URI */
58 password
= uri
->password
;
60 uri
->password
= "***";
61 uri_string_redacted
= format_uri_alloc ( uri
);
62 uri
->password
= password
;
63 if ( ! uri_string_redacted
) {
69 uri
= resolve_uri ( cwuri
, uri
);
76 *image
= alloc_image ( uri
);
82 /* Create downloader */
83 if ( ( rc
= create_downloader ( &monojob
, *image
) ) != 0 ) {
84 printf ( "Could not start download: %s\n", strerror ( rc
) );
85 goto err_create_downloader
;
88 /* Wait for download to complete */
89 if ( ( rc
= monojob_wait ( uri_string_redacted
, timeout
) ) != 0 )
90 goto err_monojob_wait
;
93 if ( ( rc
= register_image ( *image
) ) != 0 ) {
94 printf ( "Could not register image: %s\n", strerror ( rc
) );
95 goto err_register_image
;
100 err_create_downloader
:
101 image_put ( *image
);
105 free ( uri_string_redacted
);
111 * Download a new image
113 * @v uri_string URI string
114 * @v timeout Download timeout
115 * @v image Image to fill in
116 * @ret rc Return status code
118 int imgdownload_string ( const char *uri_string
, unsigned long timeout
,
119 struct image
**image
) {
123 if ( ! ( uri
= parse_uri ( uri_string
) ) )
126 rc
= imgdownload ( uri
, timeout
, image
);
135 * @v name_uri Name or URI string
136 * @v timeout Download timeout
137 * @v image Image to fill in
138 * @ret rc Return status code
140 int imgacquire ( const char *name_uri
, unsigned long timeout
,
141 struct image
**image
) {
143 /* If we already have an image with the specified name, use it */
144 *image
= find_image ( name_uri
);
148 /* Otherwise, download a new image */
149 return imgdownload_string ( name_uri
, timeout
, image
);
153 * Display status of an image
155 * @v image Executable/loadable image
157 void imgstat ( struct image
*image
) {
158 printf ( "%s : %zd bytes", image
->name
, image
->len
);
160 printf ( " [%s]", image
->type
->name
);
161 if ( image
->flags
& IMAGE_TRUSTED
)
162 printf ( " [TRUSTED]" );
163 if ( image
->flags
& IMAGE_SELECTED
)
164 printf ( " [SELECTED]" );
165 if ( image
->flags
& IMAGE_AUTO_UNREGISTER
)
166 printf ( " [AUTOFREE]" );
167 if ( image
->cmdline
)
168 printf ( " \"%s\"", image
->cmdline
);