Path: blob/master/arch/x86/boot/compressed/mkpiggy.c
10820 views
/* ----------------------------------------------------------------------- *1*2* Copyright (C) 2009 Intel Corporation. All rights reserved.3*4* This program is free software; you can redistribute it and/or5* modify it under the terms of the GNU General Public License version6* 2 as published by the Free Software Foundation.7*8* This program is distributed in the hope that it will be useful,9* but WITHOUT ANY WARRANTY; without even the implied warranty of10* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the11* GNU General Public License for more details.12*13* You should have received a copy of the GNU General Public License14* along with this program; if not, write to the Free Software15* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA16* 02110-1301, USA.17*18* H. Peter Anvin <[email protected]>19*20* ----------------------------------------------------------------------- */2122/*23* Compute the desired load offset from a compressed program; outputs24* a small assembly wrapper with the appropriate symbols defined.25*/2627#include <stdlib.h>28#include <stdio.h>29#include <string.h>30#include <inttypes.h>3132static uint32_t getle32(const void *p)33{34const uint8_t *cp = p;3536return (uint32_t)cp[0] + ((uint32_t)cp[1] << 8) +37((uint32_t)cp[2] << 16) + ((uint32_t)cp[3] << 24);38}3940int main(int argc, char *argv[])41{42uint32_t olen;43long ilen;44unsigned long offs;45FILE *f;4647if (argc < 2) {48fprintf(stderr, "Usage: %s compressed_file\n", argv[0]);49return 1;50}5152/* Get the information for the compressed kernel image first */5354f = fopen(argv[1], "r");55if (!f) {56perror(argv[1]);57return 1;58}596061if (fseek(f, -4L, SEEK_END)) {62perror(argv[1]);63}6465if (fread(&olen, sizeof(olen), 1, f) != 1) {66perror(argv[1]);67return 1;68}6970ilen = ftell(f);71olen = getle32(&olen);72fclose(f);7374/*75* Now we have the input (compressed) and output (uncompressed)76* sizes, compute the necessary decompression offset...77*/7879offs = (olen > ilen) ? olen - ilen : 0;80offs += olen >> 12; /* Add 8 bytes for each 32K block */81offs += 64*1024 + 128; /* Add 64K + 128 bytes slack */82offs = (offs+4095) & ~4095; /* Round to a 4K boundary */8384printf(".section \".rodata..compressed\",\"a\",@progbits\n");85printf(".globl z_input_len\n");86printf("z_input_len = %lu\n", ilen);87printf(".globl z_output_len\n");88printf("z_output_len = %lu\n", (unsigned long)olen);89printf(".globl z_extract_offset\n");90printf("z_extract_offset = 0x%lx\n", offs);91/* z_extract_offset_negative allows simplification of head_32.S */92printf(".globl z_extract_offset_negative\n");93printf("z_extract_offset_negative = -0x%lx\n", offs);9495printf(".globl input_data, input_data_end\n");96printf("input_data:\n");97printf(".incbin \"%s\"\n", argv[1]);98printf("input_data_end:\n");99100return 0;101}102103104