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