[infiniband] Parse MLID, rate, and SL from multicast membership record
[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 * 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 */
51 int imgdownload ( struct uri *uri, unsigned long timeout,
52 struct image **image ) {
53 const char *password;
54 char *uri_string_redacted;
55 int rc;
56
57 /* Construct redacted URI */
58 password = uri->password;
59 if ( password )
60 uri->password = "***";
61 uri_string_redacted = format_uri_alloc ( uri );
62 uri->password = password;
63 if ( ! uri_string_redacted ) {
64 rc = -ENOMEM;
65 goto err_uri_string;
66 }
67
68 /* Resolve URI */
69 uri = resolve_uri ( cwuri, uri );
70 if ( ! uri ) {
71 rc = -ENOMEM;
72 goto err_resolve_uri;
73 }
74
75 /* Allocate image */
76 *image = alloc_image ( uri );
77 if ( ! *image ) {
78 rc = -ENOMEM;
79 goto err_alloc_image;
80 }
81
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;
86 }
87
88 /* Wait for download to complete */
89 if ( ( rc = monojob_wait ( uri_string_redacted, timeout ) ) != 0 )
90 goto err_monojob_wait;
91
92 /* Register image */
93 if ( ( rc = register_image ( *image ) ) != 0 ) {
94 printf ( "Could not register image: %s\n", strerror ( rc ) );
95 goto err_register_image;
96 }
97
98 err_register_image:
99 err_monojob_wait:
100 err_create_downloader:
101 image_put ( *image );
102 err_alloc_image:
103 uri_put ( uri );
104 err_resolve_uri:
105 free ( uri_string_redacted );
106 err_uri_string:
107 return rc;
108 }
109
110 /**
111 * Download a new image
112 *
113 * @v uri_string URI string
114 * @v timeout Download timeout
115 * @v image Image to fill in
116 * @ret rc Return status code
117 */
118 int imgdownload_string ( const char *uri_string, unsigned long timeout,
119 struct image **image ) {
120 struct uri *uri;
121 int rc;
122
123 if ( ! ( uri = parse_uri ( uri_string ) ) )
124 return -ENOMEM;
125
126 rc = imgdownload ( uri, timeout, image );
127
128 uri_put ( uri );
129 return rc;
130 }
131
132 /**
133 * Acquire an image
134 *
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
139 */
140 int imgacquire ( const char *name_uri, unsigned long timeout,
141 struct image **image ) {
142
143 /* If we already have an image with the specified name, use it */
144 *image = find_image ( name_uri );
145 if ( *image )
146 return 0;
147
148 /* Otherwise, download a new image */
149 return imgdownload_string ( name_uri, timeout, image );
150 }
151
152 /**
153 * Display status of an image
154 *
155 * @v image Executable/loadable image
156 */
157 void imgstat ( struct image *image ) {
158 printf ( "%s : %zd bytes", image->name, image->len );
159 if ( image->type )
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 );
169 printf ( "\n" );
170 }