iPXE
imgmgmt.c
Go to the documentation of this file.
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 <string.h>
30 #include <errno.h>
31 #include <ipxe/image.h>
32 #include <ipxe/downloader.h>
33 #include <ipxe/monojob.h>
34 #include <ipxe/open.h>
35 #include <ipxe/uri.h>
36 #include <usr/imgmgmt.h>
37 
38 /** @file
39  *
40  * Image management
41  *
42  */
43 
44 /**
45  * Download a new image
46  *
47  * @v uri URI
48  * @v timeout Download timeout
49  * @v image Image to fill in
50  * @ret rc Return status code
51  */
52 int imgdownload ( struct uri *uri, unsigned long timeout,
53  struct image **image ) {
54  struct uri uri_redacted;
55  char *uri_string_redacted;
56  int rc;
57 
58  /* Construct redacted URI */
59  memcpy ( &uri_redacted, uri, sizeof ( uri_redacted ) );
60  uri_redacted.user = NULL;
61  uri_redacted.password = NULL;
62  uri_redacted.equery = NULL;
63  uri_redacted.efragment = NULL;
64  uri_string_redacted = format_uri_alloc ( &uri_redacted );
65  if ( ! uri_string_redacted ) {
66  rc = -ENOMEM;
67  goto err_uri_string;
68  }
69 
70  /* Resolve URI */
71  uri = resolve_uri ( cwuri, uri );
72  if ( ! uri ) {
73  rc = -ENOMEM;
74  goto err_resolve_uri;
75  }
76 
77  /* Allocate image */
78  *image = alloc_image ( uri );
79  if ( ! *image ) {
80  rc = -ENOMEM;
81  goto err_alloc_image;
82  }
83 
84  /* Create downloader */
85  if ( ( rc = create_downloader ( &monojob, *image ) ) != 0 ) {
86  printf ( "Could not start download: %s\n", strerror ( rc ) );
87  goto err_create_downloader;
88  }
89 
90  /* Wait for download to complete */
91  if ( ( rc = monojob_wait ( uri_string_redacted, timeout ) ) != 0 )
92  goto err_monojob_wait;
93 
94  /* Register image */
95  if ( ( rc = register_image ( *image ) ) != 0 ) {
96  printf ( "Could not register image: %s\n", strerror ( rc ) );
97  goto err_register_image;
98  }
99 
100  err_register_image:
101  err_monojob_wait:
102  err_create_downloader:
103  image_put ( *image );
104  err_alloc_image:
105  uri_put ( uri );
106  err_resolve_uri:
107  free ( uri_string_redacted );
108  err_uri_string:
109  return rc;
110 }
111 
112 /**
113  * Download a new image
114  *
115  * @v uri_string URI string
116  * @v timeout Download timeout
117  * @v image Image to fill in
118  * @ret rc Return status code
119  */
120 int imgdownload_string ( const char *uri_string, unsigned long timeout,
121  struct image **image ) {
122  struct uri *uri;
123  int rc;
124 
125  if ( ! ( uri = parse_uri ( uri_string ) ) )
126  return -ENOMEM;
127 
128  rc = imgdownload ( uri, timeout, image );
129 
130  uri_put ( uri );
131  return rc;
132 }
133 
134 /**
135  * Acquire an image
136  *
137  * @v name_uri Name or URI string
138  * @v timeout Download timeout
139  * @v image Image to fill in
140  * @ret rc Return status code
141  */
142 int imgacquire ( const char *name_uri, unsigned long timeout,
143  struct image **image ) {
144 
145  /* If we already have an image with the specified name, use it */
146  *image = find_image ( name_uri );
147  if ( *image )
148  return 0;
149 
150  /* Otherwise, download a new image */
151  return imgdownload_string ( name_uri, timeout, image );
152 }
153 
154 /**
155  * Display status of an image
156  *
157  * @v image Executable/loadable image
158  */
159 void imgstat ( struct image *image ) {
160  struct image_tag *tag;
161 
162  printf ( "%s : %zd bytes", image->name, image->len );
163  if ( image->type )
164  printf ( " [%s]", image->type->name );
166  if ( tag->image == image )
167  printf ( " [%s]", tag->name );
168  }
169  if ( image->flags & IMAGE_TRUSTED )
170  printf ( " [TRUSTED]" );
172  printf ( " [AUTOFREE]" );
173  if ( image->flags & IMAGE_HIDDEN )
174  printf ( " [HIDDEN]" );
175  if ( image->cmdline )
176  printf ( " \"%s\"", image->cmdline );
177  printf ( "\n" );
178 }
179 
180 /**
181  * Create image from block of memory
182  *
183  * @v name Name
184  * @v data Image data
185  * @v len Length
186  * @ret rc Return status code
187  */
188 int imgmem ( const char *name, const void *data, size_t len ) {
189  struct image *image;
190 
191  /* Create image */
192  image = image_memory ( name, data, len );
193  if ( ! image ) {
194  printf ( "Could not create image\n" );
195  return -ENOMEM;
196  }
197 
198  return 0;
199 }
unsigned int flags
Flags.
Definition: image.h:39
const char * equery
Query (with original URI encoding)
Definition: uri.h:84
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
const char * name
Definition: ath9k_hw.c:1984
int printf(const char *fmt,...)
Write a formatted string to the console.
Definition: vsprintf.c:464
static void uri_put(struct uri *uri)
Decrement URI reference count.
Definition: uri.h:205
An image tag.
Definition: image.h:172
int monojob_wait(const char *string, unsigned long timeout)
Wait for single foreground job to complete.
Definition: monojob.c:81
struct image * find_image(const char *name)
Find image by name.
Definition: image.c:375
Error codes.
struct image_type * type
Image type, if known.
Definition: image.h:58
Image downloader.
An executable image.
Definition: image.h:23
Uniform Resource Identifiers.
#define IMAGE_AUTO_UNREGISTER
Image will be automatically unregistered after execution.
Definition: image.h:82
char * name
Name of this image type.
Definition: image.h:96
char * cmdline
Command line to pass to image.
Definition: image.h:42
Single foreground job.
#define ENOMEM
Not enough space.
Definition: errno.h:534
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
void * memcpy(void *dest, const void *src, size_t len) __nonnull
Executable images.
ring len
Length.
Definition: dwmac.h:231
void imgstat(struct image *image)
Display status of an image.
Definition: imgmgmt.c:159
#define IMAGE_TAGS
Image tag table.
Definition: image.h:180
struct interface monojob
Definition: monojob.c:56
char * format_uri_alloc(const struct uri *uri)
Format URI.
Definition: uri.c:540
int create_downloader(struct interface *job, struct image *image)
Instantiate a downloader.
Definition: downloader.c:262
int register_image(struct image *image)
Register executable image.
Definition: image.c:314
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:54
size_t len
Length of raw file image.
Definition: image.h:55
#define IMAGE_HIDDEN
Image will be hidden from enumeration.
Definition: image.h:85
#define IMAGE_TRUSTED
Image is trusted.
Definition: image.h:79
#define for_each_table_entry(pointer, table)
Iterate through all entries within a linker table.
Definition: tables.h:385
static void image_put(struct image *image)
Decrement reference count on an image.
Definition: image.h:249
Data transfer interface opening.
struct image * image_memory(const char *name, const void *data, size_t len)
Create registered image from block of memory.
Definition: image.c:608
const char * efragment
Fragment (with original URI encoding)
Definition: uri.h:86
Image management.
int imgdownload_string(const char *uri_string, unsigned long timeout, struct image **image)
Download a new image.
Definition: imgmgmt.c:120
uint8_t data[48]
Additional event data.
Definition: ena.h:22
const char * password
Password.
Definition: uri.h:74
const char * user
User name.
Definition: uri.h:72
A Uniform Resource Identifier.
Definition: uri.h:64
int imgdownload(struct uri *uri, unsigned long timeout, struct image **image)
Download a new image.
Definition: imgmgmt.c:52
void timeout(int)
struct uri * resolve_uri(const struct uri *base_uri, struct uri *relative_uri)
Resolve base+relative URI.
Definition: uri.c:694
int imgmem(const char *name, const void *data, size_t len)
Create image from block of memory.
Definition: imgmgmt.c:188
struct uri * cwuri
Current working URI.
Definition: cwuri.c:38
struct image * alloc_image(struct uri *uri)
Allocate executable image.
Definition: image.c:123
uint64_t tag
Identity tag.
Definition: edd.h:30
char * name
Name.
Definition: image.h:37
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
String functions.
int imgacquire(const char *name_uri, unsigned long timeout, struct image **image)
Acquire an image.
Definition: imgmgmt.c:142
struct uri * parse_uri(const char *uri_string)
Parse URI.
Definition: uri.c:296