[zbin] Fix check for existence of most recent output byte
[ipxe.git] / src / usr / imgmgmt.c
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
20 FILE_LICENCE ( GPL2_OR_LATER );
21
22 #include <stdint.h>
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <errno.h>
26 #include <ipxe/image.h>
27 #include <ipxe/downloader.h>
28 #include <ipxe/monojob.h>
29 #include <ipxe/open.h>
30 #include <ipxe/uri.h>
31 #include <usr/imgmgmt.h>
32
33 /** @file
34 *
35 * Image management
36 *
37 */
38
39 /**
40 * Download a new image
41 *
42 * @v uri URI
43 * @v timeout Download timeout
44 * @v image Image to fill in
45 * @ret rc Return status code
46 */
47 int imgdownload ( struct uri *uri, unsigned long timeout,
48 struct image **image ) {
49 const char *password;
50 char *uri_string_redacted;
51 int rc;
52
53 /* Construct redacted URI */
54 password = uri->password;
55 if ( password )
56 uri->password = "***";
57 uri_string_redacted = format_uri_alloc ( uri );
58 uri->password = password;
59 if ( ! uri_string_redacted ) {
60 rc = -ENOMEM;
61 goto err_uri_string;
62 }
63
64 /* Resolve URI */
65 uri = resolve_uri ( cwuri, uri );
66 if ( ! uri ) {
67 rc = -ENOMEM;
68 goto err_resolve_uri;
69 }
70
71 /* Allocate image */
72 *image = alloc_image ( uri );
73 if ( ! *image ) {
74 rc = -ENOMEM;
75 goto err_alloc_image;
76 }
77
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;
82 }
83
84 /* Wait for download to complete */
85 if ( ( rc = monojob_wait ( uri_string_redacted, timeout ) ) != 0 )
86 goto err_monojob_wait;
87
88 /* Register image */
89 if ( ( rc = register_image ( *image ) ) != 0 ) {
90 printf ( "Could not register image: %s\n", strerror ( rc ) );
91 goto err_register_image;
92 }
93
94 err_register_image:
95 err_monojob_wait:
96 err_create_downloader:
97 image_put ( *image );
98 err_alloc_image:
99 uri_put ( uri );
100 err_resolve_uri:
101 free ( uri_string_redacted );
102 err_uri_string:
103 return rc;
104 }
105
106 /**
107 * Download a new image
108 *
109 * @v uri_string URI string
110 * @v timeout Download timeout
111 * @v image Image to fill in
112 * @ret rc Return status code
113 */
114 int imgdownload_string ( const char *uri_string, unsigned long timeout,
115 struct image **image ) {
116 struct uri *uri;
117 int rc;
118
119 if ( ! ( uri = parse_uri ( uri_string ) ) )
120 return -ENOMEM;
121
122 rc = imgdownload ( uri, timeout, image );
123
124 uri_put ( uri );
125 return rc;
126 }
127
128 /**
129 * Acquire an image
130 *
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
135 */
136 int imgacquire ( const char *name_uri, unsigned long timeout,
137 struct image **image ) {
138
139 /* If we already have an image with the specified name, use it */
140 *image = find_image ( name_uri );
141 if ( *image )
142 return 0;
143
144 /* Otherwise, download a new image */
145 return imgdownload_string ( name_uri, timeout, image );
146 }
147
148 /**
149 * Display status of an image
150 *
151 * @v image Executable/loadable image
152 */
153 void imgstat ( struct image *image ) {
154 printf ( "%s : %zd bytes", image->name, image->len );
155 if ( image->type )
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 );
165 printf ( "\n" );
166 }