iPXE
zlib.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 <stdlib.h>
28#include <errno.h>
29#include <assert.h>
30#include <ipxe/deflate.h>
31#include <ipxe/image.h>
32#include <ipxe/zlib.h>
33
34/** @file
35 *
36 * zlib compressed images
37 *
38 */
39
40/**
41 * Extract compressed data to image
42 *
43 * @v format Compression format code
44 * @v data Compressed input data
45 * @v len Length of compressed input data
46 * @v extracted Extracted image
47 * @ret rc Return status code
48 */
49int zlib_deflate ( enum deflate_format format, const void *data, size_t len,
50 struct image *extracted ) {
51 struct deflate *deflate;
52 struct deflate_chunk out;
53 int rc;
54
55 /* Allocate and initialise decompressor */
56 deflate = zalloc ( sizeof ( *deflate ) );
57 if ( ! deflate ) {
58 rc = -ENOMEM;
59 goto err_alloc;
60 }
61
62 /* Decompress data, (re)allocating if necessary */
63 while ( 1 ) {
64
65 /* (Re)initialise decompressor */
67
68 /* Initialise output chunk */
69 deflate_chunk_init ( &out, extracted->rwdata, 0,
70 extracted->len );
71
72 /* Decompress data */
73 if ( ( rc = deflate_inflate ( deflate, data, len,
74 &out ) ) != 0 ) {
75 DBGC ( extracted, "ZLIB %s could not decompress: %s\n",
76 extracted->name, strerror ( rc ) );
77 goto err_inflate;
78 }
79
80 /* Check that decompression is valid */
81 if ( ! deflate_finished ( deflate ) ) {
82 DBGC ( extracted, "ZLIB %s decompression incomplete\n",
83 extracted->name );
84 rc = -EINVAL;
85 goto err_unfinished;
86 }
87
88 /* Finish if output image size was correct */
89 if ( out.offset == extracted->len )
90 break;
91
92 /* Otherwise, resize output image and retry */
93 if ( ( rc = image_set_len ( extracted, out.offset ) ) != 0 ) {
94 DBGC ( extracted, "ZLIB %s could not resize: %s\n",
95 extracted->name, strerror ( rc ) );
96 goto err_set_size;
97 }
98 }
99
100 /* Success */
101 rc = 0;
102
103 err_set_size:
104 err_unfinished:
105 err_inflate:
106 free ( deflate );
107 err_alloc:
108 return rc;
109}
110
111/**
112 * Extract zlib image
113 *
114 * @v image Image
115 * @v extracted Extracted image
116 * @ret rc Return status code
117 */
118static int zlib_extract ( struct image *image, struct image *extracted ) {
119 int rc;
120
121 /* Decompress image */
123 extracted ) ) != 0 )
124 return rc;
125
126 return 0;
127}
128
129/**
130 * Probe zlib image
131 *
132 * @v image zlib image
133 * @ret rc Return status code
134 */
135static int zlib_probe ( struct image *image ) {
136 const union zlib_magic *magic;
137
138 /* Sanity check */
139 if ( image->len < sizeof ( *magic ) ) {
140 DBGC ( image, "ZLIB %s image too short\n", image->name );
141 return -ENOEXEC;
142 }
143 magic = image->data;
144
145 /* Check magic header */
146 if ( ! zlib_magic_is_valid ( magic ) ) {
147 DBGC ( image, "ZLIB %s invalid magic data\n", image->name );
148 return -ENOEXEC;
149 }
150
151 return 0;
152}
153
154/** zlib image type */
155struct image_type zlib_image_type __image_type ( PROBE_NORMAL ) = {
156 .name = "zlib",
157 .probe = zlib_probe,
158 .extract = zlib_extract,
159 .exec = image_extract_exec,
160};
__be32 out[4]
Definition CIB_PRM.h:8
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
Assertions.
uint16_t magic
Magic signature.
Definition bzimage.h:1
void deflate_init(struct deflate *deflate, enum deflate_format format)
Initialise decompressor.
Definition deflate.c:992
int deflate_inflate(struct deflate *deflate, const void *data, size_t len, struct deflate_chunk *out)
Inflate compressed data.
Definition deflate.c:484
DEFLATE decompression algorithm.
static int deflate_finished(struct deflate *deflate)
Check if decompression has finished.
Definition deflate.h:278
deflate_format
Compression formats.
Definition deflate.h:17
@ DEFLATE_ZLIB
ZLIB header and footer.
Definition deflate.h:21
ring len
Length.
Definition dwmac.h:226
uint8_t data[48]
Additional event data.
Definition ena.h:11
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 EINVAL
Invalid argument.
Definition errno.h:429
#define ENOEXEC
Exec format error.
Definition errno.h:520
#define ENOMEM
Not enough space.
Definition errno.h:535
#define FILE_SECBOOT(_status)
Declare a file's UEFI Secure Boot permission status.
Definition compiler.h:926
int image_set_len(struct image *image, size_t len)
Set image length.
Definition image.c:245
Executable images.
#define PROBE_NORMAL
Normal image probe priority.
Definition image.h:156
#define __image_type(probe_order)
An executable image type.
Definition image.h:170
void * zalloc(size_t size)
Allocate cleared memory.
Definition malloc.c:662
static void(* free)(struct refcnt *refcnt))
Definition refcnt.h:55
char * strerror(int errno)
Retrieve string representation of error number.
Definition strerror.c:79
A chunk of data.
Definition deflate.h:246
Decompressor.
Definition deflate.h:156
An executable image type.
Definition image.h:95
An executable image.
Definition image.h:24
const void * data
Read-only data.
Definition image.h:51
char * name
Name.
Definition image.h:38
size_t len
Length of raw file image.
Definition image.h:56
void * rwdata
Writable data.
Definition image.h:53
zlib magic header
Definition zlib.h:19
int const char * format
Definition xfer.h:105
static int zlib_probe(struct image *image)
Probe zlib image.
Definition zlib.c:135
int zlib_deflate(enum deflate_format format, const void *data, size_t len, struct image *extracted)
Extract compressed data to image.
Definition zlib.c:49
static int zlib_extract(struct image *image, struct image *extracted)
Extract zlib image.
Definition zlib.c:118
zlib compressed images
static int zlib_magic_is_valid(const union zlib_magic *magic)
Check that zlib magic header is valid.
Definition zlib.h:32