1 /*
2 * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
3 *
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.
8 *
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.
13 *
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
17 * 02110-1301, USA.
18 *
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.
22 */
23
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25
26 #include <stdint.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <errno.h>
30 #include <ipxe/image.h>
31 #include <ipxe/downloader.h>
32 #include <ipxe/monojob.h>
33 #include <ipxe/open.h>
34 #include <ipxe/uri.h>
35 #include <usr/imgmgmt.h>
36
37 /** @file
38 *
39 * Image management
40 *
41 */
42
43 /**
44 * Download a new image
45 *
46 * @v uri URI
47 * @v timeout Download timeout
48 * @v image Image to fill in
49 * @ret rc Return status code
50 */
imgdownload(struct uri * uri,unsigned long timeout,struct image ** image)51 int imgdownload ( struct uri *uri, unsigned long timeout,
52 struct image **image ) {
53 struct uri uri_redacted;
54 char *uri_string_redacted;
55 int rc;
56
57 /* Construct redacted URI */
58 memcpy ( &uri_redacted, uri, sizeof ( uri_redacted ) );
59 uri_redacted.user = NULL;
60 uri_redacted.password = NULL;
61 uri_redacted.query = NULL;
62 uri_redacted.fragment = NULL;
63 uri_string_redacted = format_uri_alloc ( &uri_redacted );
64 if ( ! uri_string_redacted ) {
65 rc = -ENOMEM;
66 goto err_uri_string;
67 }
68
69 /* Resolve URI */
70 uri = resolve_uri ( cwuri, uri );
71 if ( ! uri ) {
72 rc = -ENOMEM;
73 goto err_resolve_uri;
74 }
75
76 /* Allocate image */
77 *image = alloc_image ( uri );
78 if ( ! *image ) {
79 rc = -ENOMEM;
80 goto err_alloc_image;
81 }
82
83 /* Create downloader */
84 if ( ( rc = create_downloader ( &monojob, *image ) ) != 0 ) {
85 printf ( "Could not start download: %s\n", strerror ( rc ) );
86 goto err_create_downloader;
87 }
88
89 /* Wait for download to complete */
90 if ( ( rc = monojob_wait ( uri_string_redacted, timeout ) ) != 0 )
91 goto err_monojob_wait;
92
93 /* Register image */
94 if ( ( rc = register_image ( *image ) ) != 0 ) {
95 printf ( "Could not register image: %s\n", strerror ( rc ) );
96 goto err_register_image;
97 }
98
99 err_register_image:
100 err_monojob_wait:
101 err_create_downloader:
102 image_put ( *image );
103 err_alloc_image:
104 uri_put ( uri );
105 err_resolve_uri:
106 free ( uri_string_redacted );
107 err_uri_string:
108 return rc;
109 }
110
111 /**
112 * Download a new image
113 *
114 * @v uri_string URI string
115 * @v timeout Download timeout
116 * @v image Image to fill in
117 * @ret rc Return status code
118 */
imgdownload_string(const char * uri_string,unsigned long timeout,struct image ** image)119 int imgdownload_string ( const char *uri_string, unsigned long timeout,
120 struct image **image ) {
121 struct uri *uri;
122 int rc;
123
124 if ( ! ( uri = parse_uri ( uri_string ) ) )
125 return -ENOMEM;
126
127 rc = imgdownload ( uri, timeout, image );
128
129 uri_put ( uri );
130 return rc;
131 }
132
133 /**
134 * Acquire an image
135 *
136 * @v name_uri Name or URI string
137 * @v timeout Download timeout
138 * @v image Image to fill in
139 * @ret rc Return status code
140 */
imgacquire(const char * name_uri,unsigned long timeout,struct image ** image)141 int imgacquire ( const char *name_uri, unsigned long timeout,
142 struct image **image ) {
143
144 /* If we already have an image with the specified name, use it */
145 *image = find_image ( name_uri );
146 if ( *image )
147 return 0;
148
149 /* Otherwise, download a new image */
150 return imgdownload_string ( name_uri, timeout, image );
151 }
152
153 /**
154 * Display status of an image
155 *
156 * @v image Executable/loadable image
157 */
imgstat(struct image * image)158 void imgstat ( struct image *image ) {
159 printf ( "%s : %zd bytes", image->name, image->len );
160 if ( image->type )
161 printf ( " [%s]", image->type->name );
162 if ( image->flags & IMAGE_TRUSTED )
163 printf ( " [TRUSTED]" );
164 if ( image->flags & IMAGE_SELECTED )
165 printf ( " [SELECTED]" );
166 if ( image->flags & IMAGE_AUTO_UNREGISTER )
167 printf ( " [AUTOFREE]" );
168 if ( image->cmdline )
169 printf ( " \"%s\"", image->cmdline );
170 printf ( "\n" );
171 }
172