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  char *dot;
47  int rc;
48 
49  /* Check that this image can be used to extract an archive image */
50  if ( ! ( image->type && image->type->extract ) ) {
51  rc = -ENOTSUP;
52  goto err_unsupported;
53  }
54 
55  /* Allocate new image */
56  *extracted = alloc_image ( image->uri );
57  if ( ! *extracted ) {
58  rc = -ENOMEM;
59  goto err_alloc;
60  }
61 
62  /* Set image name */
63  if ( ( rc = image_set_name ( *extracted,
64  ( name ? name : image->name ) ) ) != 0 ) {
65  goto err_set_name;
66  }
67 
68  /* Strip any archive or compression suffix from implicit name */
69  if ( ( ! name ) && ( (*extracted)->name ) &&
70  ( ( dot = strrchr ( (*extracted)->name, '.' ) ) != NULL ) ) {
71  *dot = '\0';
72  }
73 
74  /* Try extracting archive image */
75  if ( ( rc = image->type->extract ( image, *extracted ) ) != 0 ) {
76  DBGC ( image, "IMAGE %s could not extract image: %s\n",
77  image->name, strerror ( rc ) );
78  goto err_extract;
79  }
80 
81  /* Register image */
82  if ( ( rc = register_image ( *extracted ) ) != 0 )
83  goto err_register;
84 
85  /* Propagate trust flag */
86  if ( image->flags & IMAGE_TRUSTED )
87  image_trust ( *extracted );
88 
89  /* Drop local reference to image */
90  image_put ( *extracted );
91 
92  return 0;
93 
94  unregister_image ( *extracted );
95  err_register:
96  err_extract:
97  err_set_name:
98  image_put ( *extracted );
99  err_alloc:
100  err_unsupported:
101  return rc;
102 }
103 
104 /**
105  * Extract and execute image
106  *
107  * @v image Image
108  * @ret rc Return status code
109  */
110 int image_extract_exec ( struct image *image ) {
111  struct image *extracted;
112  int rc;
113 
114  /* Extract image */
115  if ( ( rc = image_extract ( image, NULL, &extracted ) ) != 0 )
116  goto err_extract;
117 
118  /* Set image command line */
119  if ( ( rc = image_set_cmdline ( extracted, image->cmdline ) ) != 0 )
120  goto err_set_cmdline;
121 
122  /* Set auto-unregister flag */
123  extracted->flags |= IMAGE_AUTO_UNREGISTER;
124 
125  /* Tail-recurse into extracted image */
126  return image_exec ( extracted );
127 
128  err_set_cmdline:
129  unregister_image ( extracted );
130  err_extract:
131  return rc;
132 }
133 
134 /* Drag in objects via image_extract() */
136 
137 /* Drag in archive image formats */
138 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
char * strrchr(const char *src, int character)
Find rightmost character within a string.
Definition: string.c:289
Error codes.
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
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:73
#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:332
#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:251
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:144
#define IMAGE_TRUSTED
Image is trusted.
Definition: image.h:70
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:221
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:293
REQUIRE_OBJECT(config_archive)
int image_set_cmdline(struct image *image, const char *cmdline)
Set image command line.
Definition: image.c:166
static void image_trust(struct image *image)
Set image as trusted.
Definition: image.h:239
struct image * alloc_image(struct uri *uri)
Allocate executable image.
Definition: image.c:90
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:110