From c272beda053fbbd4c19fe3b459cedb746848763b Mon Sep 17 00:00:00 2001 From: Stefan `Sec` Zehl Date: Thu, 9 Jun 2011 13:20:03 +0200 Subject: [PATCH] Replace lpcrc with lpcfix. lpcfix -c does what lpcrc did (fix the crc) lpxfix -p sets the protection (CRP) levels new target "make protect" to enable CRP level 2. --- .gitignore | 2 - Makefile | 11 ++-- Makefile.inc | 2 +- tools/.gitignore | 2 + tools/Makefile | 4 +- tools/lpcfix.c | 157 +++++++++++++++++++++++++++++++++++++++++++++++ tools/lpcrc.c | 97 ----------------------------- 7 files changed, 169 insertions(+), 106 deletions(-) create mode 100644 tools/.gitignore create mode 100644 tools/lpcfix.c delete mode 100644 tools/lpcrc.c diff --git a/.gitignore b/.gitignore index 11042bb..28a9ca4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,3 @@ -lpcrc.exe -lpcrc firmware.bin firmware.elf *.org diff --git a/Makefile b/Makefile index 7ba3c5d..87c8faf 100644 --- a/Makefile +++ b/Makefile @@ -36,7 +36,7 @@ LD_PATH = lpc1xxx LD_SCRIPT = $(LD_PATH)/linkscript.ld LD_TEMP = $(LD_PATH)/memory.ld -all: firmware +all: $(OUTFILE).bin %.o : %.c $(CC) $(CFLAGS) -o $@ $< @@ -50,10 +50,10 @@ lcd/liblcd.a lcd/render.o lcd/display.o: modules/libmodules.a: cd modules && $(MAKE) ROOT_PATH=../$(ROOT_PATH) -tools/lpcrc: +tools/lpcfix: cd tools && $(MAKE) -firmware: $(OBJS) $(SYS_OBJS) $(LIBS) $(LPCRC) +$(OUTFILE).bin: $(OBJS) $(SYS_OBJS) $(LIBS) $(LPCFIX) -@echo "MEMORY" > $(LD_TEMP) -@echo "{" >> $(LD_TEMP) -@echo " flash(rx): ORIGIN = 0x00000000, LENGTH = $(FLASH)" >> $(LD_TEMP) @@ -66,7 +66,10 @@ firmware: $(OBJS) $(SYS_OBJS) $(LIBS) $(LPCRC) -@echo "" $(OBJCOPY) $(OCFLAGS) -O binary $(OUTFILE).elf $(OUTFILE).bin -@echo "" - $(LPCRC) $(OUTFILE).bin + $(LPCFIX) -c $(OUTFILE).bin + +protect: $(OUTFILE).bin + $(LPCFIX) -p 2 $(OUTFILE).bin clean: rm -f $(OBJS) $(LD_TEMP) $(OUTFILE).elf $(OUTFILE).bin $(OUTFILE).hex diff --git a/Makefile.inc b/Makefile.inc index 0dd19e7..82a84d4 100644 --- a/Makefile.inc +++ b/Makefile.inc @@ -24,7 +24,7 @@ SIZE = $(CROSS_COMPILE)size OBJCOPY = $(CROSS_COMPILE)objcopy OBJDUMP = $(CROSS_COMPILE)objdump OUTFILE = firmware -LPCRC = tools/lpcrc +LPCFIX = tools/lpcfix ifeq (LPC11xx,$(TARGET)) CORTEX_TYPE=m0 diff --git a/tools/.gitignore b/tools/.gitignore new file mode 100644 index 0000000..ab60634 --- /dev/null +++ b/tools/.gitignore @@ -0,0 +1,2 @@ +lpcfix.exe +lpcfix diff --git a/tools/Makefile b/tools/Makefile index 6957146..73febed 100644 --- a/tools/Makefile +++ b/tools/Makefile @@ -1,7 +1,7 @@ CC = gcc LD = gcc -LDFLAGS = -Wall -O4 -std=c99 -EXES = lpcrc +LDFLAGS = -Wall -O2 -std=c99 +EXES = lpcfix all: $(EXES) diff --git a/tools/lpcfix.c b/tools/lpcfix.c new file mode 100644 index 0000000..20e27ce --- /dev/null +++ b/tools/lpcfix.c @@ -0,0 +1,157 @@ +/* LPC CRP enabler / CRC fixer all-in-one + * + * BSD Licence + * + * (c) by Sec 6/2011 + */ + +#include +#include +#include +#include + +#define CRC_CNT 7 +#define CRC_T uint32_t + +#define CRP_OFF 0x2fc +#define CRP_T uint32_t +#define CRP_MAX 4 + +int main(int argc, char *argv[]) { + FILE *fp; + CRC_T buf[CRC_CNT]; + CRC_T crc = 0; + CRP_T magic[]= { 0x0, 0x12345678, 0x87654321, 0x43218765, 0x4e697370}; + CRP_T crp; + + char *prog; + char c; /* for getopt */ + + /* Default values */ + char verbose=0; // be silent + char do_crc= 0; // do fix crc + int do_crp=-1; // do not change crp + + /* init section */ + prog=argv[0]; + if(!prog)prog="blink"; + if(strrchr(prog,'/')){ + prog=strrchr(argv[0],'/'); + prog++; + } + + /* The big getopt loop */ + while ((c = getopt(argc, argv, "vcp:nh")) != EOF) + switch (c) { + case 'c': + do_crc = 1; + break; + + case 'v': + verbose++; + break; + + case 'p': + do_crp = atoi(optarg); + break; + + case 'h': + default: + fprintf(stderr, "Usage: %s [options]\n\n" +"This program fixes the CRC field for LPC firmware and optionally\n" +"enables/disables the content read protection flags\n" +"\n\n" +"-n Do not change CRC information\n" +"-v Be verbose (-v -v = even more)\n" +"-c mode Enable CRP (Content read protection) mode\n" +"-h This help\n\n" +"CRP modes valid:\n" +"0 Disable CRP\n" +"1-3 Enable CRP 1-3\n" +"4 Enable NO_ISP\n" +"\n",prog); + exit(255); + + } + + argc -= optind; argv += optind; + + if (argc !=1){ + fprintf(stderr,"Error: No filename given!\n"); + exit(254); + }; + + if(do_crp>CRP_MAX){ + fprintf(stderr, "CRP %d is not allowed\n",do_crp); + exit(254); + }; + + if ((fp = fopen(argv[0],"r+b")) == NULL){ + fprintf(stderr,"Error: Can't open file %s\n",argv[0]); + exit(253); + } + + if (do_crc) { + if (fread(buf,sizeof(CRC_T),CRC_CNT,fp) != CRC_CNT){ + fprintf(stderr,"Error: Read failed\n"); + exit(253); + } + + for (int idx=0; idxCRP_MAX) + idx=0; + printf("Curent CRP: 0x%08x (%d)\n", crp, idx); + }; + + if (do_crp!=-1){ + crp=magic[do_crp]; + if (fseek(fp,CRP_OFF,SEEK_SET) != 0){ + fprintf(stderr, "Error: CRP Seek failed\n"); + exit(253); + } + if (fwrite(&crp,sizeof(CRP_T),1,fp) != 1){ + fprintf(stderr,"Error: CRP Write failed\n"); + exit(253); + } + printf("New CRP value: 0x%08x\n", crp); + }; + + fclose(fp); + + return 0; +} + diff --git a/tools/lpcrc.c b/tools/lpcrc.c deleted file mode 100644 index ed316b2..0000000 --- a/tools/lpcrc.c +++ /dev/null @@ -1,97 +0,0 @@ -/* - * Software License Agreement (BSD License) - * - * Copyright (c) 2010, Roel Verdult - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holders nor the - * names of its contributors may be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY - * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY - * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND - * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ - -#include -#include - -#define BLOCK_COUNT 7 -#define BLOCK_LENGTH 4 -#define BLOCK_TOTAL (BLOCK_COUNT*BLOCK_LENGTH) - -typedef unsigned char byte_t; - -int main(int argc, char *argv[]) -{ - FILE* pf; - byte_t buf[BLOCK_TOTAL]; - uint32_t crc = 0; - uint32_t block; - - // Check for required arguments - if (argc < 2) - { - printf("syntax: lpcrc \n"); - return 1; - } - - // Try to open the supplied firmware - if ((pf = fopen(argv[1],"rb+")) == NULL) - { - printf("error: could not open file [%s] with write access\n",argv[1]); - return 1; - } - - // Read out the data blocks used for crc calculation - if (fread(buf,1,BLOCK_TOTAL,pf) != BLOCK_TOTAL) - { - printf("error: could not read required bytes\n"); - fclose(pf); - return 1; - } - - // Compute the crc value - for (block=0; block