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
24FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25FILE_SECBOOT ( PERMITTED );
26
27#include <string.h>
28#include <errno.h>
29#include <ipxe/image.h>
30
31/** @file
32 *
33 * Archive images
34 *
35 */
36
37/**
38 * Extract archive image
39 *
40 * @v image Image
41 * @v name Extracted image name
42 * @v extracted Extracted image to fill in
43 * @ret rc Return status code
44 */
45int image_extract ( struct image *image, const char *name,
46 struct image **extracted ) {
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 )
70 image_strip_suffix ( *extracted );
71
72 /* Try extracting archive image */
73 if ( ( rc = image->type->extract ( image, *extracted ) ) != 0 ) {
74 DBGC ( image, "IMAGE %s could not extract image: %s\n",
75 image->name, strerror ( rc ) );
76 goto err_extract;
77 }
78
79 /* Register image */
80 if ( ( rc = register_image ( *extracted ) ) != 0 )
81 goto err_register;
82
83 /* Propagate trust flag */
84 if ( image->flags & IMAGE_TRUSTED )
85 image_trust ( *extracted );
86
87 /* Drop local reference to image */
88 image_put ( *extracted );
89
90 return 0;
91
92 unregister_image ( *extracted );
93 err_register:
94 err_extract:
95 err_set_name:
96 image_put ( *extracted );
97 err_alloc:
98 err_unsupported:
99 return rc;
100}
101
102/**
103 * Extract and execute image
104 *
105 * @v image Image
106 * @ret rc Return status code
107 */
109 struct image *extracted;
110 int rc;
111
112 /* Extract image */
113 if ( ( rc = image_extract ( image, NULL, &extracted ) ) != 0 )
114 goto err_extract;
115
116 /* Set image command line */
117 if ( ( rc = image_set_cmdline ( extracted, image->cmdline ) ) != 0 )
118 goto err_set_cmdline;
119
120 /* Set auto-unregister flag */
121 extracted->flags |= IMAGE_AUTO_UNREGISTER;
122
123 /* Replace current image */
124 if ( ( rc = image_replace ( extracted ) ) != 0 )
125 goto err_replace;
126
127 /* Return to allow replacement image to be executed */
128 return 0;
129
130 err_replace:
131 err_set_cmdline:
132 unregister_image ( extracted );
133 err_extract:
134 return rc;
135}
136
137/* Drag in objects via image_extract() */
139
140/* Drag in archive image formats */
141REQUIRE_OBJECT ( config_archive );
#define NULL
NULL pointer (VOID *)
Definition Base.h:322
struct arbelprm_rc_send_wqe rc
Definition arbel.h:3
int image_extract_exec(struct image *image)
Extract and execute image.
Definition archive.c:108
int image_extract(struct image *image, const char *name, struct image **extracted)
Extract archive image.
Definition archive.c:45
const char * name
Definition ath9k_hw.c:1986
Error codes.
#define DBGC(...)
Definition compiler.h:505
#define FILE_LICENCE(_licence)
Declare a particular licence as applying to a file.
Definition compiler.h:896
#define REQUIRE_OBJECT(object)
Require an object.
Definition compiler.h:202
#define ENOMEM
Not enough space.
Definition errno.h:535
#define ENOTSUP
Operation not supported.
Definition errno.h:590
#define FILE_SECBOOT(_status)
Declare a file's UEFI Secure Boot permission status.
Definition compiler.h:926
#define REQUIRING_SYMBOL(symbol)
Specify the file's requiring symbol.
Definition compiler.h:140
struct image * alloc_image(struct uri *uri)
Allocate executable image.
Definition image.c:124
char * image_strip_suffix(struct image *image)
Strip dot suffix from image name, if present.
Definition image.c:206
void unregister_image(struct image *image)
Unregister executable image.
Definition image.c:358
int register_image(struct image *image)
Register executable image.
Definition image.c:315
int image_set_cmdline(struct image *image, const char *cmdline)
Set image command line.
Definition image.c:226
int image_replace(struct image *replacement)
Set replacement image.
Definition image.c:529
int image_set_name(struct image *image, const char *name)
Set image name.
Definition image.c:181
Executable images.
#define IMAGE_TRUSTED
Image is trusted.
Definition image.h:80
static void image_trust(struct image *image)
Set image as trusted.
Definition image.h:268
static void image_put(struct image *image)
Decrement reference count on an image.
Definition image.h:250
#define IMAGE_AUTO_UNREGISTER
Image will be automatically unregistered after execution.
Definition image.h:83
String functions.
char * strerror(int errno)
Retrieve string representation of error number.
Definition strerror.c:79
int(* extract)(struct image *image, struct image *extracted)
Extract archive image.
Definition image.h:142
An executable image.
Definition image.h:24
unsigned int flags
Flags.
Definition image.h:40
struct image_type * type
Image type, if known.
Definition image.h:59
char * name
Name.
Definition image.h:38
struct uri * uri
URI of image.
Definition image.h:32
char * cmdline
Command line to pass to image.
Definition image.h:43