xref: /openbmc/linux/arch/x86/boot/compressed/mkpiggy.c (revision 78c99ba1)
1 /* ----------------------------------------------------------------------- *
2  *
3  *  Copyright (C) 2009 Intel Corporation. All rights reserved.
4  *
5  *  This program is free software; you can redistribute it and/or
6  *  modify it under the terms of the GNU General Public License version
7  *  2 as published by the Free Software Foundation.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU 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  *  H. Peter Anvin <hpa@linux.intel.com>
20  *
21  * ----------------------------------------------------------------------- */
22 
23 /*
24  * Compute the desired load offset from a compressed program; outputs
25  * a small assembly wrapper with the appropriate symbols defined.
26  */
27 
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <inttypes.h>
32 
33 static uint32_t getle32(const void *p)
34 {
35 	const uint8_t *cp = p;
36 
37 	return (uint32_t)cp[0] + ((uint32_t)cp[1] << 8) +
38 		((uint32_t)cp[2] << 16) + ((uint32_t)cp[3] << 24);
39 }
40 
41 int main(int argc, char *argv[])
42 {
43 	uint32_t olen;
44 	long ilen;
45 	unsigned long offs;
46 	FILE *f;
47 
48 	if (argc < 2) {
49 		fprintf(stderr, "Usage: %s compressed_file\n", argv[0]);
50 		return 1;
51 	}
52 
53 	/* Get the information for the compressed kernel image first */
54 
55 	f = fopen(argv[1], "r");
56 	if (!f) {
57 		perror(argv[1]);
58 		return 1;
59 	}
60 
61 
62 	if (fseek(f, -4L, SEEK_END)) {
63 		perror(argv[1]);
64 	}
65 	fread(&olen, sizeof olen, 1, f);
66 	ilen = ftell(f);
67 	olen = getle32(&olen);
68 	fclose(f);
69 
70 	/*
71 	 * Now we have the input (compressed) and output (uncompressed)
72 	 * sizes, compute the necessary decompression offset...
73 	 */
74 
75 	offs = (olen > ilen) ? olen - ilen : 0;
76 	offs += olen >> 12;	/* Add 8 bytes for each 32K block */
77 	offs += 32*1024 + 18;	/* Add 32K + 18 bytes slack */
78 	offs = (offs+4095) & ~4095; /* Round to a 4K boundary */
79 
80 	printf(".section \".rodata.compressed\",\"a\",@progbits\n");
81 	printf(".globl z_input_len\n");
82 	printf("z_input_len = %lu\n", ilen);
83 	printf(".globl z_output_len\n");
84 	printf("z_output_len = %lu\n", (unsigned long)olen);
85 	printf(".globl z_extract_offset\n");
86 	printf("z_extract_offset = 0x%lx\n", offs);
87 	/* z_extract_offset_negative allows simplification of head_32.S */
88 	printf(".globl z_extract_offset_negative\n");
89 	printf("z_extract_offset_negative = -0x%lx\n", offs);
90 
91 	printf(".globl input_data, input_data_end\n");
92 	printf("input_data:\n");
93 	printf(".incbin \"%s\"\n", argv[1]);
94 	printf("input_data_end:\n");
95 
96 	return 0;
97 }
98