iPXE
archive.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2021 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 <string.h>
27 #include <errno.h>
28 #include <ipxe/image.h>
29 
30 /** @file
31  *
32  * Archive images
33  *
34  */
35 
36 /**
37  * Extract archive image
38  *
39  * @v image Image
40  * @v name Extracted image name
41  * @v extracted Extracted image to fill in
42  * @ret rc Return status code
43  */
44 int image_extract ( struct image *image, const char *name,
45  struct image **extracted ) {
46  int rc;
47 
48  /* Check that this image can be used to extract an archive image */
49  if ( ! ( image->type && image->type->extract ) ) {
50  rc = -ENOTSUP;
51  goto err_unsupported;
52  }
53 
54  /* Allocate new image */
55  *extracted = alloc_image ( image->uri );
56  if ( ! *extracted ) {
57  rc = -ENOMEM;
58  goto err_alloc;
59  }
60 
61  /* Set image name */
62  if ( ( rc = image_set_name ( *extracted,
63  ( name ? name : image->name ) ) ) != 0 ) {
64  goto err_set_name;
65  }
66 
67  /* Strip any archive or compression suffix from implicit name */
68  if ( ! name )
69  image_strip_suffix ( *extracted );
70 
71  /* Try extracting archive image */
72  if ( ( rc = image->type->extract ( image, *extracted ) ) != 0 ) {
73  DBGC ( image, "IMAGE %s could not extract image: %s\n",
74  image->name, strerror ( rc ) );
75  goto err_extract;
76  }
77 
78  /* Register image */
79  if ( ( rc = register_image ( *extracted ) ) != 0 )
80  goto err_register;
81 
82  /* Propagate trust flag */
83  if ( image->flags & IMAGE_TRUSTED )
84  image_trust ( *extracted );
85 
86  /* Drop local reference to image */
87  image_put ( *extracted );
88 
89  return 0;
90 
91  unregister_image ( *extracted );
92  err_register:
93  err_extract:
94  err_set_name:
95  image_put ( *extracted );
96  err_alloc:
97  err_unsupported:
98  return rc;
99 }
100 
101 /**
102  * Extract and execute image
103  *
104  * @v image Image
105  * @ret rc Return status code
106  */
107 int image_extract_exec ( struct image *image ) {
108  struct image *extracted;
109  int rc;
110 
111  /* Extract image */
112  if ( ( rc = image_extract ( image, NULL, &extracted ) ) != 0 )
113  goto err_extract;
114 
115  /* Set image command line */
116  if ( ( rc = image_set_cmdline ( extracted, image->cmdline ) ) != 0 )
117  goto err_set_cmdline;
118 
119  /* Set auto-unregister flag */
120  extracted->flags |= IMAGE_AUTO_UNREGISTER;
121 
122  /* Tail-recurse into extracted image */
123  return image_exec ( extracted );
124 
125  err_set_cmdline:
126  unregister_image ( extracted );
127  err_extract:
128  return rc;
129 }
130 
131 /* Drag in objects via image_extract() */
133 
134 /* Drag in archive image formats */
135 REQUIRE_OBJECT ( config_archive );
unsigned int flags
Flags.
Definition: image.h:36
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
const char * name
Definition: ath9k_hw.c:1984
Error codes.
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
char * image_strip_suffix(struct image *image)
Strip dot suffix from image name, if present.
Definition: image.c:181
struct image_type * type
Image type, if known.
Definition: image.h:46
#define DBGC(...)
Definition: compiler.h:505
An executable image.
Definition: image.h:24
REQUIRING_SYMBOL(image_extract)
#define IMAGE_AUTO_UNREGISTER
Image will be automatically unregistered after execution.
Definition: image.h:70
#define ENOTSUP
Operation not supported.
Definition: errno.h:589
char * cmdline
Command line to pass to image.
Definition: image.h:39
int image_exec(struct image *image)
Execute image.
Definition: image.c:378
#define ENOMEM
Not enough space.
Definition: errno.h:534
int image_extract(struct image *image, const char *name, struct image **extracted)
Extract archive image.
Definition: archive.c:44
Executable images.
int register_image(struct image *image)
Register executable image.
Definition: image.c:286
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
int image_set_name(struct image *image, const char *name)
Set image name.
Definition: image.c:160
#define IMAGE_TRUSTED
Image is trusted.
Definition: image.h:67
struct uri * uri
URI of image.
Definition: image.h:32
static void image_put(struct image *image)
Decrement reference count on an image.
Definition: image.h:229
int(* extract)(struct image *image, struct image *extracted)
Extract archive image.
Definition: image.h:123
void unregister_image(struct image *image)
Unregister executable image.
Definition: image.c:322
REQUIRE_OBJECT(config_archive)
int image_set_cmdline(struct image *image, const char *cmdline)
Set image command line.
Definition: image.c:201
static void image_trust(struct image *image)
Set image as trusted.
Definition: image.h:247
struct image * alloc_image(struct uri *uri)
Allocate executable image.
Definition: image.c:103
char * name
Name.
Definition: image.h:34
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
String functions.
int image_extract_exec(struct image *image)
Extract and execute image.
Definition: archive.c:107