tizen 2.3.1 release
[external/qemu.git] / roms / ipxe / 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 image             Image to fill in
44  * @ret rc              Return status code
45  */
46 int imgdownload ( struct uri *uri, struct image **image ) {
47         size_t len = ( unparse_uri ( NULL, 0, uri, URI_ALL ) + 1 );
48         char uri_string_redacted[len];
49         const char *password;
50         int rc;
51
52         /* Allocate image */
53         *image = alloc_image ( uri );
54         if ( ! *image ) {
55                 rc = -ENOMEM;
56                 goto err_alloc_image;
57         }
58
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;
66
67         /* Create downloader */
68         if ( ( rc = create_downloader ( &monojob, *image, LOCATION_URI,
69                                         uri ) ) != 0 ) {
70                 printf ( "Could not start download: %s\n", strerror ( rc ) );
71                 goto err_create_downloader;
72         }
73
74         /* Wait for download to complete */
75         if ( ( rc = monojob_wait ( uri_string_redacted ) ) != 0 )
76                 goto err_monojob_wait;
77
78         /* Register image */
79         if ( ( rc = register_image ( *image ) ) != 0 ) {
80                 printf ( "Could not register image: %s\n", strerror ( rc ) );
81                 goto err_register_image;
82         }
83
84         /* Drop local reference to image.  Image is guaranteed to
85          * remain in scope since it is registered.
86          */
87         image_put ( *image );
88
89         return 0;
90
91  err_register_image:
92  err_monojob_wait:
93  err_create_downloader:
94         image_put ( *image );
95  err_alloc_image:
96         return rc;
97 }
98
99 /**
100  * Download a new image
101  *
102  * @v uri_string        URI string
103  * @v image             Image to fill in
104  * @ret rc              Return status code
105  */
106 int imgdownload_string ( const char *uri_string, struct image **image ) {
107         struct uri *uri;
108         int rc;
109
110         if ( ! ( uri = parse_uri ( uri_string ) ) )
111                 return -ENOMEM;
112
113         rc = imgdownload ( uri, image );
114
115         uri_put ( uri );
116         return rc;
117 }
118
119 /**
120  * Acquire an image
121  *
122  * @v name_uri          Name or URI string
123  * @v image             Image to fill in
124  * @ret rc              Return status code
125  */
126 int imgacquire ( const char *name_uri, struct image **image ) {
127
128         /* If we already have an image with the specified name, use it */
129         *image = find_image ( name_uri );
130         if ( *image )
131                 return 0;
132
133         /* Otherwise, download a new image */
134         return imgdownload_string ( name_uri, image );
135 }
136
137 /**
138  * Display status of an image
139  *
140  * @v image             Executable/loadable image
141  */
142 void imgstat ( struct image *image ) {
143         printf ( "%s : %zd bytes", image->name, image->len );
144         if ( image->type )
145                 printf ( " [%s]", image->type->name );
146         if ( image->flags & IMAGE_TRUSTED )
147                 printf ( " [TRUSTED]" );
148         if ( image->flags & IMAGE_SELECTED )
149                 printf ( " [SELECTED]" );
150         if ( image->flags & IMAGE_AUTO_UNREGISTER )
151                 printf ( " [AUTOFREE]" );
152         if ( image->cmdline )
153                 printf ( " \"%s\"", image->cmdline );
154         printf ( "\n" );
155 }