Replace lpcrc with lpcfix.

lpcfix -c does what lpcrc did (fix the crc)
lpxfix -p <num> sets the protection (CRP) levels

new target "make protect" to enable CRP level 2.
This commit is contained in:
Stefan `Sec` Zehl 2011-06-09 13:20:03 +02:00
parent 86aefd2ff2
commit c272beda05
7 changed files with 169 additions and 106 deletions

2
.gitignore vendored
View File

@ -1,5 +1,3 @@
lpcrc.exe
lpcrc
firmware.bin firmware.bin
firmware.elf firmware.elf
*.org *.org

View File

@ -36,7 +36,7 @@ LD_PATH = lpc1xxx
LD_SCRIPT = $(LD_PATH)/linkscript.ld LD_SCRIPT = $(LD_PATH)/linkscript.ld
LD_TEMP = $(LD_PATH)/memory.ld LD_TEMP = $(LD_PATH)/memory.ld
all: firmware all: $(OUTFILE).bin
%.o : %.c %.o : %.c
$(CC) $(CFLAGS) -o $@ $< $(CC) $(CFLAGS) -o $@ $<
@ -50,10 +50,10 @@ lcd/liblcd.a lcd/render.o lcd/display.o:
modules/libmodules.a: modules/libmodules.a:
cd modules && $(MAKE) ROOT_PATH=../$(ROOT_PATH) cd modules && $(MAKE) ROOT_PATH=../$(ROOT_PATH)
tools/lpcrc: tools/lpcfix:
cd tools && $(MAKE) cd tools && $(MAKE)
firmware: $(OBJS) $(SYS_OBJS) $(LIBS) $(LPCRC) $(OUTFILE).bin: $(OBJS) $(SYS_OBJS) $(LIBS) $(LPCFIX)
-@echo "MEMORY" > $(LD_TEMP) -@echo "MEMORY" > $(LD_TEMP)
-@echo "{" >> $(LD_TEMP) -@echo "{" >> $(LD_TEMP)
-@echo " flash(rx): ORIGIN = 0x00000000, LENGTH = $(FLASH)" >> $(LD_TEMP) -@echo " flash(rx): ORIGIN = 0x00000000, LENGTH = $(FLASH)" >> $(LD_TEMP)
@ -66,7 +66,10 @@ firmware: $(OBJS) $(SYS_OBJS) $(LIBS) $(LPCRC)
-@echo "" -@echo ""
$(OBJCOPY) $(OCFLAGS) -O binary $(OUTFILE).elf $(OUTFILE).bin $(OBJCOPY) $(OCFLAGS) -O binary $(OUTFILE).elf $(OUTFILE).bin
-@echo "" -@echo ""
$(LPCRC) $(OUTFILE).bin $(LPCFIX) -c $(OUTFILE).bin
protect: $(OUTFILE).bin
$(LPCFIX) -p 2 $(OUTFILE).bin
clean: clean:
rm -f $(OBJS) $(LD_TEMP) $(OUTFILE).elf $(OUTFILE).bin $(OUTFILE).hex rm -f $(OBJS) $(LD_TEMP) $(OUTFILE).elf $(OUTFILE).bin $(OUTFILE).hex

View File

@ -24,7 +24,7 @@ SIZE = $(CROSS_COMPILE)size
OBJCOPY = $(CROSS_COMPILE)objcopy OBJCOPY = $(CROSS_COMPILE)objcopy
OBJDUMP = $(CROSS_COMPILE)objdump OBJDUMP = $(CROSS_COMPILE)objdump
OUTFILE = firmware OUTFILE = firmware
LPCRC = tools/lpcrc LPCFIX = tools/lpcfix
ifeq (LPC11xx,$(TARGET)) ifeq (LPC11xx,$(TARGET))
CORTEX_TYPE=m0 CORTEX_TYPE=m0

2
tools/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
lpcfix.exe
lpcfix

View File

@ -1,7 +1,7 @@
CC = gcc CC = gcc
LD = gcc LD = gcc
LDFLAGS = -Wall -O4 -std=c99 LDFLAGS = -Wall -O2 -std=c99
EXES = lpcrc EXES = lpcfix
all: $(EXES) all: $(EXES)

157
tools/lpcfix.c Normal file
View File

@ -0,0 +1,157 @@
/* LPC CRP enabler / CRC fixer all-in-one
*
* BSD Licence
*
* (c) by Sec <sec@42.org> 6/2011
*/
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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; idx<CRC_CNT; idx++) {
crc += buf[idx];
}
crc = (~crc) + 1;
if(verbose)
printf("New CRC is: 0x%08x\n",crc);
if (fseek(fp,CRC_CNT*sizeof(CRC_T),SEEK_SET) != 0){
fprintf(stderr, "Error: Seek failed\n");
exit(253);
}
// Write CRC
if (do_crc)
if (fwrite(&crc,sizeof(CRC_T),1,fp) != 1){
fprintf(stderr, "Error: CRC write failed\n");
exit(253);
}
};
if (verbose){
if (fseek(fp,CRP_OFF,SEEK_SET) != 0){
fprintf(stderr, "Error: CRP Seek failed\n");
exit(253);
}
if (fread(&crp,sizeof(CRP_T),1,fp) != 1){
fprintf(stderr,"Error: CRP Read failed\n");
exit(253);
}
int idx;
for (idx=1; idx <= CRP_MAX; idx++) {
if(crp==magic[idx])
break;
}
if(idx>CRP_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;
}

View File

@ -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 <stdio.h>
#include <stdint.h>
#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 <firmware.bin>\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<BLOCK_COUNT; block++)
{
crc += *((uint32_t*)(buf+(block*BLOCK_LENGTH)));
}
crc = (~crc) + 1;
// Reposition the file stream indicator to switch between read and write
if (fseek(pf,0,SEEK_CUR) != 0)
{
printf("error: could not switch from read to write mode\n");
fclose(pf);
return 1;
}
// Write the crc back to the file
if (fwrite((byte_t*)&crc,1,BLOCK_LENGTH,pf) != BLOCK_LENGTH)
{
printf("error: could not write crc back to file\n");
fclose(pf);
return 1;
}
printf("succesfully updated crc to: %08x\n",crc);
fclose(pf);
return 0;
}