From 0fa01ec7da07559ab8ebbad1e4e9ec3ef2d85809 Mon Sep 17 00:00:00 2001 From: pwpiwi Date: Fri, 8 May 2015 08:17:40 +0200 Subject: [PATCH 1/1] Compress the .data section as well (saves another 4KBytes and comes for free) zlib tuning: prevent fpga_compress from generating fixed code blocks armsrc/Makefile: replace osimage with fullimage --- armsrc/Makefile | 24 +++++++++++++-- armsrc/appmain.c | 8 +++-- armsrc/ldscript | 5 ++-- armsrc/start.c | 66 +++++++++++++++++++++++++++++++++++++----- client/Makefile | 9 +++--- client/fpga_compress.c | 6 +++- zlib/trees.c | 7 +++-- 7 files changed, 103 insertions(+), 22 deletions(-) diff --git a/armsrc/Makefile b/armsrc/Makefile index 1771c8c4..8cd927d8 100644 --- a/armsrc/Makefile +++ b/armsrc/Makefile @@ -65,7 +65,7 @@ ARMSRC = fpgaloader.c \ # Do not move this inclusion before the definition of {THUMB,ASM,ARM}SRC include ../common/Makefile.common -OBJS = $(OBJDIR)/osimage.s19 +OBJS = $(OBJDIR)/fullimage.s19 FPGA_COMPRESSOR = ../client/fpga_compress all: $(OBJS) @@ -75,10 +75,28 @@ $(OBJDIR)/fpga_all.o: $(OBJDIR)/fpga_all.bit.z $(OBJDIR)/fpga_all.bit.z: $(FPGA_BITSTREAMS) $(FPGA_COMPRESSOR) $(FPGA_COMPRESSOR) $(filter %.bit,$^) $@ - -$(OBJDIR)/fullimage.elf: $(VERSIONOBJ) $(OBJDIR)/fpga_all.o $(THUMBOBJ) $(ARMOBJ) + +$(OBJDIR)/fullimage.stage1.elf: $(VERSIONOBJ) $(OBJDIR)/fpga_all.o $(THUMBOBJ) $(ARMOBJ) $(CC) $(LDFLAGS) -Wl,-T,ldscript,-Map,$(patsubst %.elf,%.map,$@) -o $@ $^ $(LIBS) +$(OBJDIR)/fullimage.nodata.bin: $(OBJDIR)/fullimage.stage1.elf + $(OBJCOPY) -O binary -I elf32-littlearm --remove-section .data $^ $@ + +$(OBJDIR)/fullimage.nodata.o: $(OBJDIR)/fullimage.nodata.bin + $(OBJCOPY) -O elf32-littlearm -I binary -B arm --rename-section .data=stage1_image $^ $@ + +$(OBJDIR)/fullimage.data.bin: $(OBJDIR)/fullimage.stage1.elf + $(OBJCOPY) -O binary -I elf32-littlearm --only-section .data $^ $@ + +$(OBJDIR)/fullimage.data.bin.z: $(OBJDIR)/fullimage.data.bin $(FPGA_COMPRESSOR) + $(FPGA_COMPRESSOR) $(filter %.bin,$^) $@ + +$(OBJDIR)/fullimage.data.o: $(OBJDIR)/fullimage.data.bin.z + $(OBJCOPY) -O elf32-littlearm -I binary -B arm --rename-section .data=compressed_data $^ $@ + +$(OBJDIR)/fullimage.elf: $(OBJDIR)/fullimage.nodata.o $(OBJDIR)/fullimage.data.o + $(CC) $(LDFLAGS) -Wl,-T,ldscript,-Map,$(patsubst %.elf,%.map,$@) -o $@ $^ + tarbin: $(OBJS) $(TAR) $(TARFLAGS) ../proxmark3-$(platform)-bin.tar $(OBJS:%=armsrc/%) $(OBJS:%.s19=armsrc/%.elf) diff --git a/armsrc/appmain.c b/armsrc/appmain.c index 80ae4bc2..737873ad 100644 --- a/armsrc/appmain.c +++ b/armsrc/appmain.c @@ -310,7 +310,7 @@ void ReadMem(int addr) /* osimage version information is linked in */ extern struct version_information version_information; /* bootrom version information is pointed to from _bootphase1_version_pointer */ -extern char *_bootphase1_version_pointer, _flash_start, _flash_end, _bootrom_start, _bootrom_end, __os_size__; +extern char *_bootphase1_version_pointer, _flash_start, _flash_end, _bootrom_start, _bootrom_end, __data_src_start__; void SendVersion(void) { char temp[512]; /* Limited data payload in USB packets */ @@ -335,9 +335,11 @@ void SendVersion(void) DbpString(temp); FpgaGatherVersion(FPGA_BITSTREAM_HF, temp, sizeof(temp)); DbpString(temp); - + // Send Chip ID and used flash memory - cmd_send(CMD_ACK, *(AT91C_DBGU_CIDR), (uint32_t)&_bootrom_end - (uint32_t)&_bootrom_start + (uint32_t)&__os_size__, 0, NULL, 0); + uint32_t text_and_rodata_section_size = (uint32_t)&__data_src_start__ - (uint32_t)&_flash_start; + uint32_t compressed_data_section_size = common_area.arg1; + cmd_send(CMD_ACK, *(AT91C_DBGU_CIDR), text_and_rodata_section_size + compressed_data_section_size, 0, NULL, 0); } #ifdef WITH_LF diff --git a/armsrc/ldscript b/armsrc/ldscript index 6175564d..34da26bc 100644 --- a/armsrc/ldscript +++ b/armsrc/ldscript @@ -24,6 +24,7 @@ SECTIONS } >osimage :text .text : { + KEEP(*(stage1_image)) *(.text) *(.text.*) *(.eh_frame) @@ -36,11 +37,11 @@ SECTIONS *(.rodata.*) *(fpga_all_bit.data) KEEP(*(.version_information)) + . = ALIGN(8); } >osimage :text - . = ALIGN(4); - .data : { + KEEP(*(compressed_data)) *(.data) *(.data.*) *(.ramfunc) diff --git a/armsrc/start.c b/armsrc/start.c index d7332bda..f1e58ab0 100644 --- a/armsrc/start.c +++ b/armsrc/start.c @@ -11,23 +11,75 @@ #include "proxmark3.h" #include "apps.h" +#include "zlib.h" +#include "BigBuf.h" + +static uint8_t *next_free_memory; +extern struct common_area common_area; +extern char __data_src_start__, __data_start__, __data_end__, __bss_start__, __bss_end__; + + +static voidpf inflate_malloc(voidpf opaque, uInt items, uInt size) +{ + uint8_t *allocated_memory; + + allocated_memory = next_free_memory; + next_free_memory += items*size; + return allocated_memory; +} + + +static void inflate_free(voidpf opaque, voidpf address) +{ + // nothing to do + +} + +static void uncompress_data_section(void) +{ + z_stream data_section; + + next_free_memory = BigBuf_get_addr(); + + // initialize zstream structure + data_section.next_in = (uint8_t *) &__data_src_start__; + data_section.avail_in = &__data_end__ - &__data_start__; // uncompressed size. Wrong but doesn't matter. + data_section.next_out = (uint8_t *) &__data_start__; + data_section.avail_out = &__data_end__ - &__data_start__; // uncompressed size. Correct. + data_section.zalloc = &inflate_malloc; + data_section.zfree = &inflate_free; + data_section.opaque = NULL; + + // initialize zlib for inflate + inflateInit2(&data_section, 15); + + // uncompress data segment to RAM + inflate(&data_section, Z_FINISH); + + // save the size of the compressed data section + common_area.arg1 = data_section.total_in; +} + -extern char __data_start__, __data_src_start__, __data_end__, __bss_start__, __bss_end__; void __attribute__((section(".startos"))) Vector(void) { /* Stack should have been set up by the bootloader */ - char *src, *dst, *end; + // char *src; + char *dst, *end; + + uncompress_data_section(); /* Set up (that is: clear) BSS. */ dst = &__bss_start__; end = &__bss_end__; while(dst < end) *dst++ = 0; - /* Set up data segment: Copy from flash to ram */ - src = &__data_src_start__; - dst = &__data_start__; - end = &__data_end__; - while(dst < end) *dst++ = *src++; + // Set up data segment: Copy from flash to ram + // src = &__data_src_start__; + // dst = &__data_start__; + // end = &__data_end__; + // while(dst < end) *dst++ = *src++; + AppMain(); } diff --git a/client/Makefile b/client/Makefile index da0c2728..aa1d33c5 100644 --- a/client/Makefile +++ b/client/Makefile @@ -15,7 +15,7 @@ OBJDIR = obj LDLIBS = -L/opt/local/lib -L/usr/local/lib -lreadline -lpthread -lm LUALIB = ../liblua/liblua.a LDFLAGS = $(COMMON_FLAGS) -CFLAGS = -std=c99 -I. -I../include -I../common -I../zlib -I/opt/local/include -I../liblua -Wall $(COMMON_FLAGS) -DZ_SOLO -DZ_PREFIX -DNO_GZIP -g -O4 +CFLAGS = -std=c99 -I. -I../include -I../common -I../zlib -I/opt/local/include -I../liblua -Wall $(COMMON_FLAGS) -g -O4 LUAPLATFORM = generic ifneq (,$(findstring MINGW,$(platform))) @@ -43,7 +43,6 @@ else LUAPLATFORM = linux endif - ifneq ($(QTLDLIBS),) QTGUI = $(OBJDIR)/proxgui.o $(OBJDIR)/proxguiqt.o $(OBJDIR)/proxguiqt.moc.o CFLAGS += -DHAVE_GUI @@ -105,6 +104,8 @@ CMDSRCS = nonce2key/crapto1.c\ protocols.c\ ZLIBSRCS = deflate.c adler32.c trees.c zutil.c +ZLIB_FLAGS = -DZ_SOLO -DZ_PREFIX -DNO_GZIP -DZLIB_PM3_TUNED + COREOBJS = $(CORESRCS:%.c=$(OBJDIR)/%.o) CMDOBJS = $(CMDSRCS:%.c=$(OBJDIR)/%.o) @@ -133,10 +134,10 @@ flasher: $(OBJDIR)/flash.o $(OBJDIR)/flasher.o $(COREOBJS) $(CXX) $(CXXFLAGS) $^ $(LDLIBS) -o $@ fpga_compress: $(OBJDIR)/fpga_compress.o $(ZLIBOBJS) - $(CXX) $(CXXFLAGS) $^ $(LDLIBS) -o $@ + $(CXX) $(CXXFLAGS) $(ZLIB_FLAGS) $^ $(LDLIBS) -o $@ $(OBJDIR)/%.o: %.c - $(CC) $(CFLAGS) -c -o $@ $< + $(CC) $(CFLAGS) $(ZLIB_FLAGS) -c -o $@ $< $(OBJDIR)/%.o: %.cpp $(CXX) $(CXXFLAGS) -c -o $@ $< diff --git a/client/fpga_compress.c b/client/fpga_compress.c index d4376ccc..5164f6d4 100644 --- a/client/fpga_compress.c +++ b/client/fpga_compress.c @@ -85,7 +85,11 @@ int zlib_compress(FILE *infile[], uint8_t num_infiles, FILE *outfile) for(uint16_t j = 0; j < num_infiles; j++) { for(uint16_t k = 0; k < FPGA_INTERLEAVE_SIZE; k++) { c = fgetc(infile[j]); - if (!feof(infile[j])) fpga_config[i++] = c; else fpga_config[i++] = '\0'; + if (!feof(infile[j])) { + fpga_config[i++] = c; + } else if (num_infiles > 1) { + fpga_config[i++] = '\0'; + } } } diff --git a/zlib/trees.c b/zlib/trees.c index 1fd7759e..a4f04057 100644 --- a/zlib/trees.c +++ b/zlib/trees.c @@ -913,9 +913,10 @@ void ZLIB_INTERNAL _tr_flush_block(s, buf, stored_len, last) ulg opt_lenb, static_lenb; /* opt_len and static_len in bytes */ int max_blindex = 0; /* index of last bit length code of non zero freq */ +#ifndef ZLIB_PM3_TUNED /* Build the Huffman trees unless a stored block is forced */ if (s->level > 0) { - +#endif /* Check if the file is binary or text */ if (s->strm->data_type == Z_UNKNOWN) s->strm->data_type = detect_data_type(s); @@ -945,6 +946,7 @@ void ZLIB_INTERNAL _tr_flush_block(s, buf, stored_len, last) opt_lenb, s->opt_len, static_lenb, s->static_len, stored_len, s->last_lit)); +#ifndef ZLIB_PM3_TUNED if (static_lenb <= opt_lenb) opt_lenb = static_lenb; } else { @@ -978,7 +980,8 @@ void ZLIB_INTERNAL _tr_flush_block(s, buf, stored_len, last) s->compressed_len += 3 + s->static_len; #endif } else { - send_bits(s, (DYN_TREES<<1)+last, 3); +#endif /* ZLIB_PM3_TUNED */ + send_bits(s, (DYN_TREES<<1)+last, 3); send_all_trees(s, s->l_desc.max_code+1, s->d_desc.max_code+1, max_blindex+1); compress_block(s, (const ct_data *)s->dyn_ltree, -- 2.39.5