source: mainline/boot/generic/src/gzip.c@ 1bebc906

Last change on this file since 1bebc906 was 63a045c, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 7 years ago

Unify handling of compressed init data and use regular tar + gzip to achieve it

There are two issues this commit solves.

First is that architecture-specific code duplicates most of the init binary
handling in each architecture, each with miniscule and confusing variations.
After this commit, the init binary expansion is almost entirely handled by
unified generic code.

Second is that the way we used to generate the incorporated data is somewhat
convoluted. Previously we have a Python script which generates a zip archive
with individual deflate-compressed files and accompanying header and C files
which contain structures describing the archive contents.
The zip file is then extracted and the individual deflate-compressed files are
included in the binary via assembler code.
Since gas doesn't take particular care to be consistent between architectures,
the assembly portions are also not uniform and the build script needs to know
particulars of the architecture's assembly.

Instead of doing that, after this commit we first gzip each included file, then
we pack the gzipped files into a tar archive, and then we include the archive
into the binary using objcopy.
Linker script provides symbols for the start and end of the archive,
and the payload is in a self-describing format, so there is no need for any
generated code.

Note that we are doing the opposite of the conventional .tar.gz format.
It would be somewhat inconvenient to use .tar.gz since the uncompressed files
need to be aligned to page size, so we'd have to first decompress the entire
payload to determine the final position of the files (and hence the required
amount of memory).

  • Property mode set to 100644
File size: 5.3 KB
Line 
1/*
2 * Copyright (c) 2014 Martin Decky
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29// XXX: This file is a duplicate of the same in uspace/lib/compress
30
31#include <stdint.h>
32#include <stddef.h>
33#include <errno.h>
34#include <memstr.h>
35#include <byteorder.h>
36#include <gzip.h>
37#include <inflate.h>
38
39#define GZIP_ID1 UINT8_C(0x1f)
40#define GZIP_ID2 UINT8_C(0x8b)
41
42#define GZIP_METHOD_DEFLATE UINT8_C(0x08)
43
44#define GZIP_FLAGS_MASK UINT8_C(0x1f)
45#define GZIP_FLAG_FHCRC UINT8_C(1 << 1)
46#define GZIP_FLAG_FEXTRA UINT8_C(1 << 2)
47#define GZIP_FLAG_FNAME UINT8_C(1 << 3)
48#define GZIP_FLAG_FCOMMENT UINT8_C(1 << 4)
49
50typedef struct {
51 uint8_t id1;
52 uint8_t id2;
53 uint8_t method;
54 uint8_t flags;
55 uint32_t mtime;
56 uint8_t extra_flags;
57 uint8_t os;
58} __attribute__((packed)) gzip_header_t;
59
60typedef struct {
61 uint32_t crc32;
62 uint32_t size;
63} __attribute__((packed)) gzip_footer_t;
64
65size_t gzip_size(const void *src, size_t srclen)
66{
67 gzip_header_t header;
68 gzip_footer_t footer;
69
70 if ((srclen < sizeof(header)) || (srclen < sizeof(footer)))
71 return 0;
72
73 memcpy(&header, src, sizeof(header));
74 memcpy(&footer, src + srclen - sizeof(footer), sizeof(footer));
75
76 if ((header.id1 != GZIP_ID1) ||
77 (header.id2 != GZIP_ID2) ||
78 (header.method != GZIP_METHOD_DEFLATE) ||
79 ((header.flags & (~GZIP_FLAGS_MASK)) != 0))
80 return 0;
81
82 return uint32_t_le2host(footer.size);
83}
84
85/** Expand GZIP compressed data
86 *
87 * The routine allocates the output buffer based
88 * on the size encoded in the input stream. This
89 * effectively limits the size of the uncompressed
90 * data to 4 GiB (expanding input streams that actually
91 * encode more data will always fail).
92 *
93 * So far, no CRC is perfomed.
94 *
95 * @param[in] src Source data buffer.
96 * @param[in] srclen Source buffer size (bytes).
97 * @param[out] dest Destination data buffer.
98 * @param[out] destlen Destination buffer size (bytes).
99 *
100 * @return EOK on success.
101 * @return ENOENT on distance too large.
102 * @return EINVAL on invalid Huffman code, invalid deflate data,
103 * invalid compression method or invalid stream.
104 * @return ELIMIT on input buffer overrun.
105 * @return ENOMEM on output buffer overrun.
106 *
107 */
108int gzip_expand(const void *src, size_t srclen, void *dest, size_t destlen)
109{
110 gzip_header_t header;
111 gzip_footer_t footer;
112
113 if ((srclen < sizeof(header)) || (srclen < sizeof(footer)))
114 return EINVAL;
115
116 /* Decode header and footer */
117
118 memcpy(&header, src, sizeof(header));
119 memcpy(&footer, src + srclen - sizeof(footer), sizeof(footer));
120
121 if ((header.id1 != GZIP_ID1) ||
122 (header.id2 != GZIP_ID2) ||
123 (header.method != GZIP_METHOD_DEFLATE) ||
124 ((header.flags & (~GZIP_FLAGS_MASK)) != 0))
125 return EINVAL;
126
127 if (destlen != uint32_t_le2host(footer.size))
128 return EINVAL;
129
130 /* Ignore extra metadata */
131
132 const void *stream = src + sizeof(header);
133 size_t stream_length = srclen - sizeof(header) - sizeof(footer);
134
135 if ((header.flags & GZIP_FLAG_FEXTRA) != 0) {
136 uint16_t extra_length;
137
138 if (stream_length < sizeof(extra_length))
139 return EINVAL;
140
141 memcpy(&extra_length, stream, sizeof(extra_length));
142 stream += sizeof(extra_length);
143 stream_length -= sizeof(extra_length);
144
145 if (stream_length < extra_length)
146 return EINVAL;
147
148 stream += extra_length;
149 stream_length -= extra_length;
150 }
151
152 if ((header.flags & GZIP_FLAG_FNAME) != 0) {
153 while (*((uint8_t *) stream) != 0) {
154 if (stream_length == 0)
155 return EINVAL;
156
157 stream++;
158 stream_length--;
159 }
160
161 if (stream_length == 0)
162 return EINVAL;
163
164 stream++;
165 stream_length--;
166 }
167
168 if ((header.flags & GZIP_FLAG_FCOMMENT) != 0) {
169 while (*((uint8_t *) stream) != 0) {
170 if (stream_length == 0)
171 return EINVAL;
172
173 stream++;
174 stream_length--;
175 }
176
177 if (stream_length == 0)
178 return EINVAL;
179
180 stream++;
181 stream_length--;
182 }
183
184 if ((header.flags & GZIP_FLAG_FHCRC) != 0) {
185 if (stream_length < 2)
186 return EINVAL;
187
188 stream += 2;
189 stream_length -= 2;
190 }
191
192 /* Inflate the data */
193 return inflate(stream, stream_length, dest, destlen);
194}
Note: See TracBrowser for help on using the repository browser.