From 45ae92ff1ac863b798e20f5fdffbd53eda02d316 Mon Sep 17 00:00:00 2001 From: Peter Stuge Date: Tue, 14 Apr 2009 19:48:32 +0000 Subject: util/cbfstool/tools/rom-mk*->cbfs-mk* rename Signed-off-by: Peter Stuge Acked-by: Ronald G. Minnich git-svn-id: svn://svn.coreboot.org/coreboot/trunk@4114 2b7e53f0-3cfb-0310-b3e9-8179ed1497e1 --- util/cbfstool/Makefile | 8 +- util/cbfstool/add.c | 8 +- util/cbfstool/tools/Makefile | 12 +- util/cbfstool/tools/cbfs-mkpayload.c | 269 +++++++++++++++++++++++++++++++++++ util/cbfstool/tools/cbfs-mkstage.c | 209 +++++++++++++++++++++++++++ util/cbfstool/tools/rom-mkpayload.c | 269 ----------------------------------- util/cbfstool/tools/rom-mkstage.c | 209 --------------------------- 7 files changed, 492 insertions(+), 492 deletions(-) create mode 100644 util/cbfstool/tools/cbfs-mkpayload.c create mode 100644 util/cbfstool/tools/cbfs-mkstage.c delete mode 100644 util/cbfstool/tools/rom-mkpayload.c delete mode 100644 util/cbfstool/tools/rom-mkstage.c diff --git a/util/cbfstool/Makefile b/util/cbfstool/Makefile index 71e0d3b7d7..919021d667 100644 --- a/util/cbfstool/Makefile +++ b/util/cbfstool/Makefile @@ -13,7 +13,7 @@ CFLAGS=-g -Wall # -W -Werror DESTDIR ?= /usr/local/bin -all: $(obj)/cbfstool $(obj)/tools/rom-mkpayload $(obj)/tools/rom-mkstage +all: $(obj)/cbfstool $(obj)/tools/cbfs-mkpayload $(obj)/tools/cbfs-mkstage $(obj)/cbfstool: $(patsubst %,$(obj)/%,$(OBJ)) $(CC) -o $@ $(patsubst %,$(obj)/%,$(OBJ)) @@ -26,11 +26,11 @@ include $(tsrc)/Makefile $(obj)/%.o: %.c $(INC) $(CC) $(CFLAGS) -c -o $@ $< -install: $(obj)/cbfstool $(obj)/tools/rom-mkpayload $(obj)/tools/rom-mkstage +install: $(obj)/cbfstool $(obj)/tools/cbfs-mkpayload $(obj)/tools/cbfs-mkstage @ install -d $(DESTDIR) @ install -m 0755 $(obj)/cbfstool $(DESTDIR)/cbfstool - @ install -m 0755 $(obj)/tools/rom-mkstage $(DESTDIR)/rom-mkstage - @ install -m 0755 $(obj)/tools/rom-mkpayload $(DESTDIR)/rom-mkpayload + @ install -m 0755 $(obj)/tools/cbfs-mkstage $(DESTDIR)/cbfs-mkstage + @ install -m 0755 $(obj)/tools/cbfs-mkpayload $(DESTDIR)/cbfs-mkpayload tags: ctags *.[ch] */*.[ch] diff --git a/util/cbfstool/add.c b/util/cbfstool/add.c index 642d8ce519..aadb68d841 100644 --- a/util/cbfstool/add.c +++ b/util/cbfstool/add.c @@ -270,7 +270,7 @@ char *find_tool(char *tool) return toolpath; } -/* Invoke the rom-mkpayload utility */ +/* Invoke the cbfs-mkpayload utility */ int add_payload_handler(struct rom *rom, int argc, char **argv) { @@ -293,12 +293,12 @@ int add_payload_handler(struct rom *rom, int argc, char **argv) return -1; } - return fork_tool_and_add(rom, find_tool("rom-mkpayload"), argv[0], + return fork_tool_and_add(rom, find_tool("cbfs-mkpayload"), argv[0], argv[1], CBFS_COMPONENT_PAYLOAD, argc - 2, argc > 2 ? &argv[2] : NULL); } -/* Invoke the rom-mkstage utility */ +/* Invoke the cbfs-mkstage utility */ int add_stage_handler(struct rom *rom, int argc, char **argv) { @@ -321,7 +321,7 @@ int add_stage_handler(struct rom *rom, int argc, char **argv) return -1; } - return fork_tool_and_add(rom, find_tool("rom-mkstage"), argv[0], + return fork_tool_and_add(rom, find_tool("cbfs-mkstage"), argv[0], argv[1], CBFS_COMPONENT_STAGE, argc - 2, argc > 2 ? &argv[2] : NULL); } diff --git a/util/cbfstool/tools/Makefile b/util/cbfstool/tools/Makefile index bcecf46636..f5b0e04bcc 100644 --- a/util/cbfstool/tools/Makefile +++ b/util/cbfstool/tools/Makefile @@ -1,24 +1,24 @@ tobj ?= $(shell pwd) tsrc ?= $(shell pwd) -TARGETS += $(tobj)/rom-mkstage $(tobj)/rom-mkpayload +TARGETS += $(tobj)/cbfs-mkstage $(tobj)/cbfs-mkpayload -tools: $(tobj)/rom-mkstage $(tobj)/rom-mkpayload +tools: $(tobj)/cbfs-mkstage $(tobj)/cbfs-mkpayload include $(tsrc)/lzma/Makefile COMMON = common.o compress.o $(LZMA_OBJ) -$(tobj)/rom-mkstage: $(tobj)/rom-mkstage.o $(patsubst %,$(tobj)/%,$(COMMON)) +$(tobj)/cbfs-mkstage: $(tobj)/cbfs-mkstage.o $(patsubst %,$(tobj)/%,$(COMMON)) $(CXX) $(CFLAGS) -o $@ $^ -$(tobj)/rom-mkpayload: $(tobj)/rom-mkpayload.o $(patsubst %,$(tobj)/%,$(COMMON)) +$(tobj)/cbfs-mkpayload: $(tobj)/cbfs-mkpayload.o $(patsubst %,$(tobj)/%,$(COMMON)) $(CXX) $(CFLAGS) -o $@ $^ $(tobj)/%.o: %.c $(CC) $(CFLAGS) -c -o $@ $< tools-clean: - rm -f $(tobj)/rom-mkpayload.o $(tobj)/rom-mkstage.o $(patsubst %,$(tobj)/%,$(COMMON)) - rm -f $(tobj)/rom-mkpayload $(tobj)/rom-mkstage + rm -f $(tobj)/cbfs-mkpayload.o $(tobj)/cbfs-mkstage.o $(patsubst %,$(tobj)/%,$(COMMON)) + rm -f $(tobj)/cbfs-mkpayload $(tobj)/cbfs-mkstage diff --git a/util/cbfstool/tools/cbfs-mkpayload.c b/util/cbfstool/tools/cbfs-mkpayload.c new file mode 100644 index 0000000000..9b3946a926 --- /dev/null +++ b/util/cbfstool/tools/cbfs-mkpayload.c @@ -0,0 +1,269 @@ +/* + * cbfs-mkpayload + * + * Copyright (C) 2008 Jordan Crouse + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA + */ + +#include +#include +#include +#include +#include "elf.h" +#include +#include +#include +#include + +#include "common.h" +#include "../cbfs.h" + +int parse_elf(unsigned char *input, unsigned char **output, int algo, + void (*compress) (char *, int, char *, int *)) +{ + Elf32_Phdr *phdr; + Elf32_Ehdr *ehdr; + Elf32_Shdr *shdr; + char *header; + char *strtab; + unsigned char *sptr; + int headers; + int segments = 1; + int isize = 0, osize = 0; + int doffset = 0; + struct cbfs_payload_segment *segs; + int i; + + ehdr = (Elf32_Ehdr *) input; + headers = ehdr->e_phnum; + header = (char *)ehdr; + + phdr = (Elf32_Phdr *) & (header[ehdr->e_phoff]); + shdr = (Elf32_Shdr *) & (header[ehdr->e_shoff]); + + strtab = &header[shdr[ehdr->e_shstrndx].sh_offset]; + + /* Count the number of headers - look for the .notes.pinfo + * section */ + + for (i = 0; i < ehdr->e_shnum; i++) { + char *name; + + if (i == ehdr->e_shstrndx) + continue; + + if (shdr[i].sh_size == 0) + continue; + + name = (char *)(strtab + shdr[i].sh_name); + + if (!strcmp(name, ".note.pinfo")) + segments++; + } + + /* Now, regular headers - we only care about PT_LOAD headers, + * because thats what we're actually going to load + */ + + for (i = 0; i < headers; i++) { + if (phdr[i].p_type != PT_LOAD) + continue; + + /* Empty segments are never interesting */ + if (phdr[i].p_memsz == 0) + continue; + + isize += phdr[i].p_filesz; + + segments++; + } + + /* Allocate a block of memory to store the data in */ + + sptr = + calloc((segments * sizeof(struct cbfs_payload_segment)) + isize, + 1); + doffset = (segments * sizeof(struct cbfs_payload_segment)); + + if (sptr == NULL) + goto err; + + segs = (struct cbfs_payload_segment *)sptr; + segments = 0; + + for (i = 0; i < ehdr->e_shnum; i++) { + char *name; + + if (i == ehdr->e_shstrndx) + continue; + + if (shdr[i].sh_size == 0) + continue; + + name = (char *)(strtab + shdr[i].sh_name); + + if (!strcmp(name, ".note.pinfo")) { + segs[segments].type = PAYLOAD_SEGMENT_PARAMS; + segs[segments].load_addr = 0; + segs[segments].len = (unsigned int)shdr[i].sh_size; + segs[segments].offset = doffset; + + memcpy((unsigned long *)(sptr + doffset), + &header[shdr[i].sh_offset], shdr[i].sh_size); + + doffset += segs[segments].len; + osize += segs[segments].len; + + segments++; + } + } + + for (i = 0; i < headers; i++) { + if (phdr[i].p_type != PT_LOAD) + continue; + + if (phdr[i].p_memsz == 0) + continue; + + if (phdr[i].p_filesz == 0) { + segs[segments].type = PAYLOAD_SEGMENT_BSS; + segs[segments].load_addr = + (unsigned long long)htonl(phdr[i].p_paddr); + segs[segments].mem_len = (unsigned int)htonl(phdr[i].p_memsz); + segs[segments].offset = htonl(doffset); + + segments++; + continue; + } + + segs[segments].type = PAYLOAD_SEGMENT_DATA; + segs[segments].load_addr = (unsigned int)htonl(phdr[i].p_paddr); + segs[segments].mem_len = (unsigned int)htonl(phdr[i].p_memsz); + segs[segments].compression = htonl(algo); + segs[segments].offset = htonl(doffset); + + int len; + compress((char *)&header[phdr[i].p_offset], + phdr[i].p_filesz, + (char *)(sptr + doffset), &len); + segs[segments].len = htonl(len); + + /* If the compressed section is larger, then use the + original stuff */ + + if ((unsigned int)len > phdr[i].p_filesz) { + segs[segments].compression = 0; + segs[segments].len = htonl(phdr[i].p_filesz); + + memcpy((char *)(sptr + doffset), + &header[phdr[i].p_offset], phdr[i].p_filesz); + } + + doffset += ntohl(segs[segments].len); + osize += ntohl(segs[segments].len); + + segments++; + } + + segs[segments].type = PAYLOAD_SEGMENT_ENTRY; + segs[segments++].load_addr = (unsigned long long)htonl(ehdr->e_entry); + + *output = sptr; + + return (segments * sizeof(struct cbfs_payload_segment)) + osize; + +err: + return -1; +} + +int main(int argc, char **argv) +{ + void (*compress) (char *, int, char *, int *); + int algo; + + char *output = NULL; + char *input = NULL; + + unsigned char *buffer, *obuffer; + int size, osize; + + while (1) { + int option_index; + static struct option longopt[] = { + {"output", 1, 0, 'o'}, + {"lzma", 0, 0, 'l'}, + {"nocompress", 0, 0, 'n'}, + }; + + signed char ch = getopt_long(argc, argv, "o:ln", + longopt, &option_index); + + if (ch == -1) + break; + + switch (ch) { + case 'o': + output = optarg; + break; + case 'l': + algo = CBFS_COMPRESS_LZMA; + break; + case 'n': + algo = CBFS_COMPRESS_NONE; + break; + default: + //usage(); + return -1; + } + } + + if (optind < argc) + input = argv[optind]; + + if (input == NULL || !strcmp(input, "-")) + buffer = file_read_to_buffer(STDIN_FILENO, &size); + else { + printf("Reading from %s\n", input); + buffer = file_read(input, &size); + } + + if (!iself(buffer)) { + fprintf(stderr, "E: This does not appear to be an ELF file\n"); + return -1; + } + + switch (algo) { + case CBFS_COMPRESS_NONE: + compress = none_compress; + break; + case CBFS_COMPRESS_LZMA: + compress = lzma_compress; + break; + } + + osize = parse_elf(buffer, &obuffer, algo, compress); + + if (osize == -1) { + fprintf(stderr, "E: Error while converting the payload\n"); + return -1; + } + + if (output == NULL || !strcmp(output, "-")) + file_write_from_buffer(STDOUT_FILENO, obuffer, osize); + else + file_write(output, obuffer, osize); + + return 0; +} diff --git a/util/cbfstool/tools/cbfs-mkstage.c b/util/cbfstool/tools/cbfs-mkstage.c new file mode 100644 index 0000000000..0c472da517 --- /dev/null +++ b/util/cbfstool/tools/cbfs-mkstage.c @@ -0,0 +1,209 @@ +/* + * cbfs-mkstage + * + * Copyright (C) 2008 Jordan Crouse + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA + */ + +#include +#include +#include +#include +#include "elf.h" +#include +#include +#include + +#include "common.h" +#include "../cbfs.h" + +int parse_elf(unsigned char *input, unsigned char **output, + int mode, void (*compress) (char *, int, char *, int *)) +{ + Elf32_Phdr *phdr; + Elf32_Ehdr *ehdr = (Elf32_Ehdr *) input; + Elf32_Shdr *shdr; + char *header, *buffer; + unsigned char *out; + + int headers; + int i; + struct cbfs_stage *stage; + unsigned int data_start, data_end, mem_end; + + headers = ehdr->e_phnum; + header = (char *)ehdr; + + phdr = (Elf32_Phdr *) & (header[ehdr->e_phoff]); + shdr = (Elf32_Shdr *) & (header[ehdr->e_shoff]); + + /* Now, regular headers - we only care about PT_LOAD headers, + * because thats what we're actually going to load + */ + + data_start = 0xFFFFFFFF; + data_end = 0; + mem_end = 0; + + for (i = 0; i < headers; i++) { + unsigned int start, mend, rend; + + if (phdr[i].p_type != PT_LOAD) + continue; + + /* Empty segments are never interesting */ + if (phdr[i].p_memsz == 0) + continue; + + /* BSS */ + + start = phdr[i].p_paddr; + + mend = start + phdr[i].p_memsz; + rend = start + phdr[i].p_filesz; + + if (start < data_start) + data_start = start; + + if (rend > data_end) + data_end = rend; + + if (mend > mem_end) + mem_end = mend; + } + + /* allocate an intermediate buffer for the data */ + buffer = calloc(data_end - data_start, 1); + + if (buffer == NULL) { + fprintf(stderr, "E: Unable to allocate memory: %m\n"); + return -1; + } + + /* Copy the file data into the buffer */ + + for (i = 0; i < headers; i++) { + + if (phdr[i].p_type != PT_LOAD) + continue; + + if (phdr[i].p_memsz == 0) + continue; + + memcpy(buffer + (phdr[i].p_paddr - data_start), + &header[phdr[i].p_offset], phdr[i].p_filesz); + } + + /* Now make the output buffer */ + out = calloc(sizeof(struct cbfs_stage) + data_end - data_start, 1); + + if (out == NULL) { + fprintf(stderr, "E: Unable to allocate memory: %m\n"); + return -1; + } + + stage = (struct cbfs_stage *)out; + + stage->load = data_start; + stage->memlen = mem_end - data_start; + stage->compression = mode; + stage->entry = ehdr->e_entry; + + compress(buffer, data_end - data_start, + (char *)(out + sizeof(struct cbfs_stage)), + (int *)&stage->len); + + *output = out; + + return sizeof(struct cbfs_stage) + stage->len; +} + +int main(int argc, char **argv) +{ + void (*compress) (char *, int, char *, int *); + int algo = CBFS_COMPRESS_LZMA; + + char *output = NULL; + char *input = NULL; + + unsigned char *buffer, *obuffer; + int size, osize; + + while (1) { + int option_index; + static struct option longopt[] = { + {"output", 1, 0, 'o'}, + {"lzma", 0, 0, 'l'}, + {"nocompress", 0, 0, 'n'}, + }; + + signed char ch = getopt_long(argc, argv, "o:ln", + longopt, &option_index); + + if (ch == -1) + break; + + switch (ch) { + case 'o': + output = optarg; + break; + case 'l': + algo = CBFS_COMPRESS_LZMA; + break; + case 'n': + algo = CBFS_COMPRESS_NONE; + break; + default: + //usage(); + return -1; + } + } + + if (optind < argc) + input = argv[optind]; + + if (input == NULL || !strcmp(input, "-")) + buffer = file_read_to_buffer(STDIN_FILENO, &size); + else + buffer = file_read(input, &size); + + if (!iself(buffer)) { + fprintf(stderr, "E: The incoming file is not an ELF\n"); + return -1; + } + + switch (algo) { + case CBFS_COMPRESS_NONE: + compress = none_compress; + break; + case CBFS_COMPRESS_LZMA: + compress = lzma_compress; + break; + } + + osize = parse_elf(buffer, &obuffer, algo, compress); + + if (osize == -1) { + fprintf(stderr, "E: Error while converting the ELF\n"); + return -1; + } + + if (output == NULL || !strcmp(output, "-")) + file_write_from_buffer(STDOUT_FILENO, obuffer, osize); + else + file_write(output, obuffer, osize); + + return 0; +} diff --git a/util/cbfstool/tools/rom-mkpayload.c b/util/cbfstool/tools/rom-mkpayload.c deleted file mode 100644 index 4466cc1254..0000000000 --- a/util/cbfstool/tools/rom-mkpayload.c +++ /dev/null @@ -1,269 +0,0 @@ -/* - * rom-mkpayload - * - * Copyright (C) 2008 Jordan Crouse - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; version 2 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA - */ - -#include -#include -#include -#include -#include "elf.h" -#include -#include -#include -#include - -#include "common.h" -#include "../cbfs.h" - -int parse_elf(unsigned char *input, unsigned char **output, int algo, - void (*compress) (char *, int, char *, int *)) -{ - Elf32_Phdr *phdr; - Elf32_Ehdr *ehdr; - Elf32_Shdr *shdr; - char *header; - char *strtab; - unsigned char *sptr; - int headers; - int segments = 1; - int isize = 0, osize = 0; - int doffset = 0; - struct cbfs_payload_segment *segs; - int i; - - ehdr = (Elf32_Ehdr *) input; - headers = ehdr->e_phnum; - header = (char *)ehdr; - - phdr = (Elf32_Phdr *) & (header[ehdr->e_phoff]); - shdr = (Elf32_Shdr *) & (header[ehdr->e_shoff]); - - strtab = &header[shdr[ehdr->e_shstrndx].sh_offset]; - - /* Count the number of headers - look for the .notes.pinfo - * section */ - - for (i = 0; i < ehdr->e_shnum; i++) { - char *name; - - if (i == ehdr->e_shstrndx) - continue; - - if (shdr[i].sh_size == 0) - continue; - - name = (char *)(strtab + shdr[i].sh_name); - - if (!strcmp(name, ".note.pinfo")) - segments++; - } - - /* Now, regular headers - we only care about PT_LOAD headers, - * because thats what we're actually going to load - */ - - for (i = 0; i < headers; i++) { - if (phdr[i].p_type != PT_LOAD) - continue; - - /* Empty segments are never interesting */ - if (phdr[i].p_memsz == 0) - continue; - - isize += phdr[i].p_filesz; - - segments++; - } - - /* Allocate a block of memory to store the data in */ - - sptr = - calloc((segments * sizeof(struct cbfs_payload_segment)) + isize, - 1); - doffset = (segments * sizeof(struct cbfs_payload_segment)); - - if (sptr == NULL) - goto err; - - segs = (struct cbfs_payload_segment *)sptr; - segments = 0; - - for (i = 0; i < ehdr->e_shnum; i++) { - char *name; - - if (i == ehdr->e_shstrndx) - continue; - - if (shdr[i].sh_size == 0) - continue; - - name = (char *)(strtab + shdr[i].sh_name); - - if (!strcmp(name, ".note.pinfo")) { - segs[segments].type = PAYLOAD_SEGMENT_PARAMS; - segs[segments].load_addr = 0; - segs[segments].len = (unsigned int)shdr[i].sh_size; - segs[segments].offset = doffset; - - memcpy((unsigned long *)(sptr + doffset), - &header[shdr[i].sh_offset], shdr[i].sh_size); - - doffset += segs[segments].len; - osize += segs[segments].len; - - segments++; - } - } - - for (i = 0; i < headers; i++) { - if (phdr[i].p_type != PT_LOAD) - continue; - - if (phdr[i].p_memsz == 0) - continue; - - if (phdr[i].p_filesz == 0) { - segs[segments].type = PAYLOAD_SEGMENT_BSS; - segs[segments].load_addr = - (unsigned long long)htonl(phdr[i].p_paddr); - segs[segments].mem_len = (unsigned int)htonl(phdr[i].p_memsz); - segs[segments].offset = htonl(doffset); - - segments++; - continue; - } - - segs[segments].type = PAYLOAD_SEGMENT_DATA; - segs[segments].load_addr = (unsigned int)htonl(phdr[i].p_paddr); - segs[segments].mem_len = (unsigned int)htonl(phdr[i].p_memsz); - segs[segments].compression = htonl(algo); - segs[segments].offset = htonl(doffset); - - int len; - compress((char *)&header[phdr[i].p_offset], - phdr[i].p_filesz, - (char *)(sptr + doffset), &len); - segs[segments].len = htonl(len); - - /* If the compressed section is larger, then use the - original stuff */ - - if ((unsigned int)len > phdr[i].p_filesz) { - segs[segments].compression = 0; - segs[segments].len = htonl(phdr[i].p_filesz); - - memcpy((char *)(sptr + doffset), - &header[phdr[i].p_offset], phdr[i].p_filesz); - } - - doffset += ntohl(segs[segments].len); - osize += ntohl(segs[segments].len); - - segments++; - } - - segs[segments].type = PAYLOAD_SEGMENT_ENTRY; - segs[segments++].load_addr = (unsigned long long)htonl(ehdr->e_entry); - - *output = sptr; - - return (segments * sizeof(struct cbfs_payload_segment)) + osize; - -err: - return -1; -} - -int main(int argc, char **argv) -{ - void (*compress) (char *, int, char *, int *); - int algo; - - char *output = NULL; - char *input = NULL; - - unsigned char *buffer, *obuffer; - int size, osize; - - while (1) { - int option_index; - static struct option longopt[] = { - {"output", 1, 0, 'o'}, - {"lzma", 0, 0, 'l'}, - {"nocompress", 0, 0, 'n'}, - }; - - signed char ch = getopt_long(argc, argv, "o:ln", - longopt, &option_index); - - if (ch == -1) - break; - - switch (ch) { - case 'o': - output = optarg; - break; - case 'l': - algo = CBFS_COMPRESS_LZMA; - break; - case 'n': - algo = CBFS_COMPRESS_NONE; - break; - default: - //usage(); - return -1; - } - } - - if (optind < argc) - input = argv[optind]; - - if (input == NULL || !strcmp(input, "-")) - buffer = file_read_to_buffer(STDIN_FILENO, &size); - else { - printf("Reading from %s\n", input); - buffer = file_read(input, &size); - } - - if (!iself(buffer)) { - fprintf(stderr, "E: This does not appear to be an ELF file\n"); - return -1; - } - - switch (algo) { - case CBFS_COMPRESS_NONE: - compress = none_compress; - break; - case CBFS_COMPRESS_LZMA: - compress = lzma_compress; - break; - } - - osize = parse_elf(buffer, &obuffer, algo, compress); - - if (osize == -1) { - fprintf(stderr, "E: Error while converting the payload\n"); - return -1; - } - - if (output == NULL || !strcmp(output, "-")) - file_write_from_buffer(STDOUT_FILENO, obuffer, osize); - else - file_write(output, obuffer, osize); - - return 0; -} diff --git a/util/cbfstool/tools/rom-mkstage.c b/util/cbfstool/tools/rom-mkstage.c deleted file mode 100644 index ff43e774fc..0000000000 --- a/util/cbfstool/tools/rom-mkstage.c +++ /dev/null @@ -1,209 +0,0 @@ -/* - * rom-mkstage - * - * Copyright (C) 2008 Jordan Crouse - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; version 2 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA - */ - -#include -#include -#include -#include -#include "elf.h" -#include -#include -#include - -#include "common.h" -#include "../cbfs.h" - -int parse_elf(unsigned char *input, unsigned char **output, - int mode, void (*compress) (char *, int, char *, int *)) -{ - Elf32_Phdr *phdr; - Elf32_Ehdr *ehdr = (Elf32_Ehdr *) input; - Elf32_Shdr *shdr; - char *header, *buffer; - unsigned char *out; - - int headers; - int i; - struct cbfs_stage *stage; - unsigned int data_start, data_end, mem_end; - - headers = ehdr->e_phnum; - header = (char *)ehdr; - - phdr = (Elf32_Phdr *) & (header[ehdr->e_phoff]); - shdr = (Elf32_Shdr *) & (header[ehdr->e_shoff]); - - /* Now, regular headers - we only care about PT_LOAD headers, - * because thats what we're actually going to load - */ - - data_start = 0xFFFFFFFF; - data_end = 0; - mem_end = 0; - - for (i = 0; i < headers; i++) { - unsigned int start, mend, rend; - - if (phdr[i].p_type != PT_LOAD) - continue; - - /* Empty segments are never interesting */ - if (phdr[i].p_memsz == 0) - continue; - - /* BSS */ - - start = phdr[i].p_paddr; - - mend = start + phdr[i].p_memsz; - rend = start + phdr[i].p_filesz; - - if (start < data_start) - data_start = start; - - if (rend > data_end) - data_end = rend; - - if (mend > mem_end) - mem_end = mend; - } - - /* allocate an intermediate buffer for the data */ - buffer = calloc(data_end - data_start, 1); - - if (buffer == NULL) { - fprintf(stderr, "E: Unable to allocate memory: %m\n"); - return -1; - } - - /* Copy the file data into the buffer */ - - for (i = 0; i < headers; i++) { - - if (phdr[i].p_type != PT_LOAD) - continue; - - if (phdr[i].p_memsz == 0) - continue; - - memcpy(buffer + (phdr[i].p_paddr - data_start), - &header[phdr[i].p_offset], phdr[i].p_filesz); - } - - /* Now make the output buffer */ - out = calloc(sizeof(struct cbfs_stage) + data_end - data_start, 1); - - if (out == NULL) { - fprintf(stderr, "E: Unable to allocate memory: %m\n"); - return -1; - } - - stage = (struct cbfs_stage *)out; - - stage->load = data_start; - stage->memlen = mem_end - data_start; - stage->compression = mode; - stage->entry = ehdr->e_entry; - - compress(buffer, data_end - data_start, - (char *)(out + sizeof(struct cbfs_stage)), - (int *)&stage->len); - - *output = out; - - return sizeof(struct cbfs_stage) + stage->len; -} - -int main(int argc, char **argv) -{ - void (*compress) (char *, int, char *, int *); - int algo = CBFS_COMPRESS_LZMA; - - char *output = NULL; - char *input = NULL; - - unsigned char *buffer, *obuffer; - int size, osize; - - while (1) { - int option_index; - static struct option longopt[] = { - {"output", 1, 0, 'o'}, - {"lzma", 0, 0, 'l'}, - {"nocompress", 0, 0, 'n'}, - }; - - signed char ch = getopt_long(argc, argv, "o:ln", - longopt, &option_index); - - if (ch == -1) - break; - - switch (ch) { - case 'o': - output = optarg; - break; - case 'l': - algo = CBFS_COMPRESS_LZMA; - break; - case 'n': - algo = CBFS_COMPRESS_NONE; - break; - default: - //usage(); - return -1; - } - } - - if (optind < argc) - input = argv[optind]; - - if (input == NULL || !strcmp(input, "-")) - buffer = file_read_to_buffer(STDIN_FILENO, &size); - else - buffer = file_read(input, &size); - - if (!iself(buffer)) { - fprintf(stderr, "E: The incoming file is not an ELF\n"); - return -1; - } - - switch (algo) { - case CBFS_COMPRESS_NONE: - compress = none_compress; - break; - case CBFS_COMPRESS_LZMA: - compress = lzma_compress; - break; - } - - osize = parse_elf(buffer, &obuffer, algo, compress); - - if (osize == -1) { - fprintf(stderr, "E: Error while converting the ELF\n"); - return -1; - } - - if (output == NULL || !strcmp(output, "-")) - file_write_from_buffer(STDOUT_FILENO, obuffer, osize); - else - file_write(output, obuffer, osize); - - return 0; -} -- cgit v1.2.3