Debian Codegraph @

util/cbfstool/amdcompress.c

raw symbols SCIP
1 /* AMD Family 17h and later BIOS compressor */
2 /* SPDX-License-Identifier: GPL-2.0-only */
3
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <fcntl.h>
7 #include <sys/stat.h>
8 #include <getopt.h>
9 #include <elfparsing.h>
10 #include "zlib.h"
11
12 #define DEBUG_FILE 0
13
14 #define HDR_SIZE 256
15 #define UNCOMP_MAX 0x300000
16
17 #define DIR_UNDEF 0
18 #define DIR_COMP 1
19 #define DIR_UNCOMP 2
20
21 typedef struct _header {
22 uint32_t rsvd1[5];
23 uint32_t size;
24 uint32_t rsvd2[58];
25 } __attribute__((packed)) header;
26
27 static const char *optstring = "i:o:cm:uh";
28
29 static struct option long_options[] = {
30 {"infile", required_argument, 0, 'i' },
31 {"outfile", required_argument, 0, 'o' },
32 {"compress", no_argument, 0, 'c' },
33 {"maxsize", required_argument, 0, 'm' },
34 {"uncompress", no_argument, 0, 'u' },
35 {"help", no_argument, 0, 'h' },
36 };
37
38 static void usage(void)
39 {
40 printf("<name>: Extract or create a zlib compressed BIOS binary\n");
41 printf(" image. A compressed image contains a 256 byte\n");
42 printf(" header with a 32-bit size at 0x14.\n");
43 printf("Usage: <name> -i in_file -o out_file -[c|u]\n");
44 printf("-i | --infile <FILE> Input file\n");
45 printf("-o | --outfile <FILE> Output file\n");
46 printf("-c | --compress Compress\n");
47 printf("-m | --maxsize <HEX_VAL> Maximum uncompressed size (optional)\n");
48 printf(" * On compress: verify uncompressed size\n");
49 printf(" will be less than or equal maxsize\n");
50 printf(" * On uncompress: override default buffer size\n");
51 printf(" allocation of 0x%x bytes\n", UNCOMP_MAX);
52 printf("-u | --uncompress Uncompress\n");
53 printf("-h | --help Display this message\n");
54
55 exit(1);
56 }
57
58 static int do_file(char *name, size_t *size, int oflag)
59 {
60 struct stat fd_stat;
61 int fd;
62
63 fd = open(name, oflag, 0666);
64 if (fd < 0)
65 return -1;
66
67 if (fstat(fd, &fd_stat)) {
68 close(fd);
69 return -1;
70 }
71
72 if (size)
73 *size = fd_stat.st_size;
74 return fd;
75 }
76
77 static int parse_elf_to_xip_ram(const struct buffer *input,
78 struct buffer *output)
79 {
80 struct parsed_elf pelf;
81 const Elf64_Phdr *phdr;
82
83 if (parse_elf(input, &pelf, ELF_PARSE_ALL))
84 return 1;
85
86 /* Validate ELF. Expect only on PT_LOAD program header at index 0. */
87 for (int i = 0; i < pelf.ehdr.e_phnum; i++) {
88 phdr = &pelf.phdr[i];
89 /* Expect PT_LOAD on first entry */
90 if (i == 0 && (phdr->p_type != PT_LOAD || !phdr->p_memsz)) {
91 printf("\tProgram header #0 is invalid.\n");
92 }
93 /* Expect not to find more PT_LOAD program headers */
94 if (i > 0 && phdr->p_type == PT_LOAD && phdr->p_memsz) {
95 printf("\tMultiple program headers found, but only one is supported!\n");
96 return 1;
97 }
98 }
99
100 if (buffer_create(output, pelf.phdr->p_filesz, "") != 0)
101 return 1;
102
103 memcpy(output->data, input->data + pelf.phdr->p_offset, output->size);
104
105 return 0;
106 }
107
108 static int convert_elf(struct buffer *buf)
109 {
110 struct buffer out;
111
112 if (parse_elf_to_xip_ram(buf, &out)) {
113 printf("\tError parsing ELF file\n");
114 return -1;
115 }
116
117 /* Discard the elf file in buf and replace with the progbits */
118 free(buf->data);
119 buf->data = out.data;
120 buf->size = out.size;
121
122 return 0;
123 }
124
125 static int iself(const void *input)
126 {
127 const Elf32_Ehdr *ehdr = input;
128 return !memcmp(ehdr->e_ident, ELFMAG, 4);
129 }
130
131 /* todo: Consider using deflate() and inflate() instead of compress() and
132 * decompress(), especially if memory allocation somehow becomes a problem.
133 * Those two functions can operate on streams and process chunks of data.
134 */
135
136 /* Build the required header and follow it with the compressed image. Detect
137 * whether the input is an elf image, and if so, compress only the progbits.
138 *
139 * header
140 * 0 +------+-------+-------+-------+
141 * | | | | |
142 * +----------------------+-------+
143 * | | size | | |
144 * +----------------------+-------+
145 * | | | | |
146 * | | | ... |
147 * 256 +------------------------------+
148 * |compressed image |
149 * | ... |
150 * | ... |
151 * | ... |
152 * n +------------------------------+
153 */
154 static void do_compress(char *outf, char *inf, size_t max_size)
155 {
156 int out_fd, in_fd;
157 struct buffer inbf, outbf;
158 int err;
159
160 in_fd = do_file(inf, &inbf.size, O_RDONLY);
161 if (in_fd < 0) {
162 printf("\tError opening input file %s\n", inf);
163 err = 1;
164 goto out;
165 }
166
167 out_fd = do_file(outf, 0, O_CREAT | O_WRONLY);
168 if (out_fd < 0) {
169 printf("\tError opening output file %s\n", outf);
170 err = 1;
171 goto out_close_in;
172 }
173
174 inbf.data = calloc(inbf.size, 1);
175 if (!inbf.data) {
176 printf("\tError allocating 0x%zx bytes for input buffer\n", inbf.size);
177 err = 1;
178 goto out_close_out;
179 }
180
181 if (read(in_fd, inbf.data, inbf.size) != (ssize_t)inbf.size) {
182 printf("\tError reading input file %s\n", inf);
183 err = 1;
184 goto out_free_in;
185 }
186
187 if (iself(inbf.data)) {
188 if (convert_elf(&inbf)) {
189 err = 1;
190 goto out_free_in;
191 }
192 }
193
194 if (max_size && inbf.size > max_size) {
195 printf("\tError - size (%zx) exceeds specified max_size (%zx)\n",
196 inbf.size, max_size);
197 err = 1;
198 goto out_free_in;
199 }
200
201 outbf.size = inbf.size; /* todo: tbd worst case? */
202 outbf.size += sizeof(header);
203 outbf.data = calloc(outbf.size, 1);
204 if (!outbf.size) {
205 printf("\tError allocating 0x%zx bytes for output buffer\n", outbf.size);
206 err = 1;
207 goto out_free_in;
208 }
209
210 err = compress((Bytef *)(outbf.data + sizeof(header)), &outbf.size,
211 (Bytef *)inbf.data, inbf.size);
212 if (err != Z_OK) {
213 printf("\tzlib compression error %d\n", err);
214 err = 1;
215 goto out_free_out;
216 }
217
218 if (DEBUG_FILE)
219 printf("\tCompressed 0x%zx bytes into 0x%zx\n", inbf.size,
220 outbf.size - sizeof(header));
221
222 ((header *)outbf.data)->size = outbf.size;
223
224 if (write(out_fd, outbf.data, outbf.size + sizeof(header))
225 != (ssize_t)(outbf.size + sizeof(header))) {
226 printf("\tError writing to %s\n", outf);
227 err = 1;
228 /* fall through to out_free_out */
229 }
230
231 out_free_out:
232 free(outbf.data);
233 out_free_in:
234 free(inbf.data);
235 out_close_out:
236 close(out_fd);
237 out_close_in:
238 close(in_fd);
239 out:
240 if (err)
241 exit(err);
242 }
243
244 static void do_uncompress(char *outf, char *inf, size_t max_size)
245 {
246 int out_fd, in_fd;
247 char *in_buf, *out_buf;
248 size_t size_unc, size_comp;
249 size_t bytes;
250 int err;
251
252 in_fd = do_file(inf, &size_comp, O_RDONLY);
253 if (in_fd < 0) {
254 printf("\tError opening input file %s\n", inf);
255 err = 1;
256 goto out;
257 }
258
259 out_fd = do_file(outf, 0, O_CREAT | O_WRONLY);
260 if (out_fd < 0) {
261 printf("\tError opening output file %s\n", outf);
262 err = 1;
263 goto out_close_in;
264 }
265
266 in_buf = calloc(size_comp, 1);
267 if (!in_buf) {
268 printf("\tError allocating 0x%zx bytes for input buffer\n", size_comp);
269 err = 1;
270 goto out_close_out;
271 }
272
273 bytes = read(in_fd, in_buf, size_comp);
274 if (bytes != size_comp) {
275 printf("\tError reading input file %s\n", inf);
276 err = 1;
277 goto out_free_in;
278 }
279
280 size_comp = ((header *)in_buf)->size;
281
282 size_unc = max_size ? max_size : UNCOMP_MAX;
283 out_buf = calloc(size_unc, 1);
284 if (!out_buf) {
285 printf("\tError allocating 0x%zx bytes for output buffer\n", size_unc);
286 err = 1;
287 goto out_free_in;
288 }
289
290 err = uncompress((Bytef *)out_buf, &size_unc,
291 (Bytef *)in_buf + sizeof(header), size_comp);
292 if (err != Z_OK) {
293 printf("\tzlib uncompression error %d\n", err);
294 err = 1;
295 goto out_free_out;
296 }
297
298 if (DEBUG_FILE)
299 printf("Uncompressed 0x%zx bytes into 0x%zx\n", size_comp, size_unc);
300
301 bytes = write(out_fd, out_buf, size_unc);
302 if (bytes != size_unc) {
303 printf("\tError writing to %s\n", outf);
304 err = 1;
305 /* fall through to out_free_out */
306 }
307
308 out_free_out:
309 free(out_buf);
310 out_free_in:
311 free(in_buf);
312 out_close_out:
313 close(out_fd);
314 out_close_in:
315 close(in_fd);
316 out:
317 if (err)
318 exit(err);
319 }
320
321 int main(int argc, char *argv[])
322 {
323 int c;
324 char *inf = 0, *outf = 0, *scratch;
325 int direction = DIR_UNDEF;
326 size_t max_size = 0;
327
328 while (1) {
329 int optindex = 0;
330
331 c = getopt_long(argc, argv, optstring, long_options, &optindex);
332 if (c == -1)
333 break;
334
335 switch (c) {
336 case 'i':
337 inf = optarg;
338 break;
339 case 'o':
340 outf = optarg;
341 break;
342 case 'c':
343 if (direction != DIR_UNDEF)
344 usage();
345 direction = DIR_COMP;
346 break;
347 case 'u':
348 if (direction != DIR_UNDEF)
349 usage();
350 direction = DIR_UNCOMP;
351 break;
352 case 'm':
353 max_size = strtoull(optarg, &scratch, 16);
354 break;
355 case 'h':
356 usage();
357 }
358 }
359 if (!inf || !outf || direction == DIR_UNDEF)
360 usage();
361
362 if (direction == DIR_COMP)
363 do_compress(outf, inf, max_size);
364 else
365 do_uncompress(outf, inf, max_size);
366
367 return 0;
368 }