Compress the .data section as well (saves another 4KBytes and comes for free)
authorpwpiwi <pwpiwi@users.noreply.github.com>
Fri, 8 May 2015 06:17:40 +0000 (08:17 +0200)
committerpwpiwi <pwpiwi@users.noreply.github.com>
Tue, 26 May 2015 06:31:32 +0000 (08:31 +0200)
zlib tuning: prevent fpga_compress from generating fixed code blocks
armsrc/Makefile: replace osimage with fullimage

armsrc/Makefile
armsrc/appmain.c
armsrc/ldscript
armsrc/start.c
client/Makefile
client/fpga_compress.c
zlib/trees.c

index 1771c8c4919156f1ad578782ba29b07f5830c2c6..8cd927d8c7196ae2e241654ee2d0a1459987aa76 100644 (file)
@@ -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)
 
index 80ae4bc2a7d4bae1648bbe66192a6e8c640e23c0..737873ad00917de3d5fdf93209b1fbc0b04d276c 100644 (file)
@@ -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
index 6175564dceff930950c4b272d3b60536cc1cbda2..34da26bcdfd8cee58e6bfa99cb6bfb4c57eb59af 100644 (file)
@@ -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)
index d7332bda5682255bb03e4d306e87763d97218a51..f1e58ab0ed9359e80714c4be2fd146494b9fa4d4 100644 (file)
 
 #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();
 }
index da0c27280506a08bb50e7e9679455d6068440582..aa1d33c59119ba8c57283bfc9c216b85e5776a1f 100644 (file)
@@ -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 $@ $<
index d4376ccc96a1cdc50f92bcc21e1ba0f258639005..5164f6d41bbb6612255d943a0c7cdf5b5cac6ebd 100644 (file)
@@ -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';
+                               }
                        }
                }
 
index 1fd7759ef004c66fd920873efb030eda5d1eafe0..a4f04057e7b38aa566c5a528689fcefc556a476e 100644 (file)
@@ -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,
Impressum, Datenschutz