iPXE
Functions
imgmgmt.c File Reference

Image management. More...

#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <ipxe/image.h>
#include <ipxe/downloader.h>
#include <ipxe/monojob.h>
#include <ipxe/open.h>
#include <ipxe/uri.h>
#include <usr/imgmgmt.h>

Go to the source code of this file.

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 
int imgdownload (struct uri *uri, unsigned long timeout, struct image **image)
 Download a new image. More...
 
int imgdownload_string (const char *uri_string, unsigned long timeout, struct image **image)
 Download a new image. More...
 
int imgacquire (const char *name_uri, unsigned long timeout, struct image **image)
 Acquire an image. More...
 
void imgstat (struct image *image)
 Display status of an image. More...
 

Detailed Description

Image management.

Definition in file imgmgmt.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ imgdownload()

int imgdownload ( struct uri uri,
unsigned long  timeout,
struct image **  image 
)

Download a new image.

Parameters
uriURI
timeoutDownload timeout
imageImage to fill in
Return values
rcReturn status code

Definition at line 51 of file imgmgmt.c.

52  {
53  struct uri uri_redacted;
54  char *uri_string_redacted;
55  int rc;
56 
57  /* Construct redacted URI */
58  memcpy ( &uri_redacted, uri, sizeof ( uri_redacted ) );
59  uri_redacted.user = NULL;
60  uri_redacted.password = NULL;
61  uri_redacted.query = NULL;
62  uri_redacted.fragment = NULL;
63  uri_string_redacted = format_uri_alloc ( &uri_redacted );
64  if ( ! uri_string_redacted ) {
65  rc = -ENOMEM;
66  goto err_uri_string;
67  }
68 
69  /* Resolve URI */
70  uri = resolve_uri ( cwuri, uri );
71  if ( ! uri ) {
72  rc = -ENOMEM;
73  goto err_resolve_uri;
74  }
75 
76  /* Allocate image */
77  *image = alloc_image ( uri );
78  if ( ! *image ) {
79  rc = -ENOMEM;
80  goto err_alloc_image;
81  }
82 
83  /* Create downloader */
84  if ( ( rc = create_downloader ( &monojob, *image ) ) != 0 ) {
85  printf ( "Could not start download: %s\n", strerror ( rc ) );
86  goto err_create_downloader;
87  }
88 
89  /* Wait for download to complete */
90  if ( ( rc = monojob_wait ( uri_string_redacted, timeout ) ) != 0 )
91  goto err_monojob_wait;
92 
93  /* Register image */
94  if ( ( rc = register_image ( *image ) ) != 0 ) {
95  printf ( "Could not register image: %s\n", strerror ( rc ) );
96  goto err_register_image;
97  }
98 
99  err_register_image:
100  err_monojob_wait:
101  err_create_downloader:
102  image_put ( *image );
103  err_alloc_image:
104  uri_put ( uri );
105  err_resolve_uri:
106  free ( uri_string_redacted );
107  err_uri_string:
108  return rc;
109 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
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:188
int monojob_wait(const char *string, unsigned long timeout)
Wait for single foreground job to complete.
Definition: monojob.c:81
An executable image.
Definition: image.h:24
#define ENOMEM
Not enough space.
Definition: errno.h:534
void * memcpy(void *dest, const void *src, size_t len) __nonnull
struct interface monojob
Definition: monojob.c:56
char * format_uri_alloc(const struct uri *uri)
Format URI.
Definition: uri.c:522
int create_downloader(struct interface *job, struct image *image)
Instantiate a downloader.
Definition: downloader.c:258
int register_image(struct image *image)
Register executable image.
Definition: image.c:210
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:54
static void image_put(struct image *image)
Decrement reference count on an image.
Definition: image.h:206
A Uniform Resource Identifier.
Definition: uri.h:50
void timeout(int)
struct uri * resolve_uri(const struct uri *base_uri, struct uri *relative_uri)
Resolve base+relative URI.
Definition: uri.c:676
struct uri * cwuri
Current working URI.
Definition: cwuri.c:38
struct image * alloc_image(struct uri *uri)
Allocate executable image.
Definition: image.c:90
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362

References alloc_image(), create_downloader(), cwuri, ENOMEM, format_uri_alloc(), uri::fragment, free, image_put(), memcpy(), monojob, monojob_wait(), NULL, uri::password, printf(), uri::query, rc, register_image(), resolve_uri(), strerror(), timeout(), uri_put(), and uri::user.

Referenced by imgdownload_string(), and uriboot().

◆ imgdownload_string()

int imgdownload_string ( const char *  uri_string,
unsigned long  timeout,
struct image **  image 
)

Download a new image.

Parameters
uri_stringURI string
timeoutDownload timeout
imageImage to fill in
Return values
rcReturn status code

Definition at line 119 of file imgmgmt.c.

120  {
121  struct uri *uri;
122  int rc;
123 
124  if ( ! ( uri = parse_uri ( uri_string ) ) )
125  return -ENOMEM;
126 
127  rc = imgdownload ( uri, timeout, image );
128 
129  uri_put ( uri );
130  return rc;
131 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
static void uri_put(struct uri *uri)
Decrement URI reference count.
Definition: uri.h:188
An executable image.
Definition: image.h:24
#define ENOMEM
Not enough space.
Definition: errno.h:534
A Uniform Resource Identifier.
Definition: uri.h:50
int imgdownload(struct uri *uri, unsigned long timeout, struct image **image)
Download a new image.
Definition: imgmgmt.c:51
void timeout(int)
struct uri * parse_uri(const char *uri_string)
Parse URI.
Definition: uri.c:295

References ENOMEM, imgdownload(), parse_uri(), rc, timeout(), and uri_put().

Referenced by comboot_fetch_kernel(), and imgacquire().

◆ imgacquire()

int imgacquire ( const char *  name_uri,
unsigned long  timeout,
struct image **  image 
)

Acquire an image.

Parameters
name_uriName or URI string
timeoutDownload timeout
imageImage to fill in
Return values
rcReturn status code

Definition at line 141 of file imgmgmt.c.

142  {
143 
144  /* If we already have an image with the specified name, use it */
145  *image = find_image ( name_uri );
146  if ( *image )
147  return 0;
148 
149  /* Otherwise, download a new image */
150  return imgdownload_string ( name_uri, timeout, image );
151 }
struct image * find_image(const char *name)
Find image by name.
Definition: image.c:270
An executable image.
Definition: image.h:24
int imgdownload_string(const char *uri_string, unsigned long timeout, struct image **image)
Download a new image.
Definition: imgmgmt.c:119
void timeout(int)

References find_image(), imgdownload_string(), and timeout().

Referenced by cert_exec(), console_exec(), digest_exec(), and imgverify_exec().

◆ imgstat()

void imgstat ( struct image image)

Display status of an image.

Parameters
imageExecutable/loadable image

Definition at line 158 of file imgmgmt.c.

158  {
159  printf ( "%s : %zd bytes", image->name, image->len );
160  if ( image->type )
161  printf ( " [%s]", image->type->name );
162  if ( image->flags & IMAGE_TRUSTED )
163  printf ( " [TRUSTED]" );
164  if ( image->flags & IMAGE_SELECTED )
165  printf ( " [SELECTED]" );
167  printf ( " [AUTOFREE]" );
168  if ( image->cmdline )
169  printf ( " \"%s\"", image->cmdline );
170  printf ( "\n" );
171 }
unsigned int flags
Flags.
Definition: image.h:36
int printf(const char *fmt,...)
Write a formatted string to the console.
Definition: vsprintf.c:464
struct image_type * type
Image type, if known.
Definition: image.h:46
An executable image.
Definition: image.h:24
#define IMAGE_AUTO_UNREGISTER
Image will be automatically unregistered after execution.
Definition: image.h:73
char * name
Name of this image type.
Definition: image.h:78
char * cmdline
Command line to pass to image.
Definition: image.h:39
#define IMAGE_SELECTED
Image is selected for execution.
Definition: image.h:67
size_t len
Length of raw file image.
Definition: image.h:43
#define IMAGE_TRUSTED
Image is trusted.
Definition: image.h:70
char * name
Name.
Definition: image.h:34

References image::cmdline, image::flags, IMAGE_AUTO_UNREGISTER, IMAGE_SELECTED, IMAGE_TRUSTED, image::len, image::name, image_type::name, printf(), and image::type.

Referenced by imgstat_exec(), and uriboot().