From 8f4e14a7d89a14b84d577f43e0bdfab3c95d5105 Mon Sep 17 00:00:00 2001 From: John Klug Date: Tue, 30 Jan 2018 16:14:00 -0600 Subject: Add mts-hdr-cpy command for copying the bootstrap header --- src/Makefile.am | 6 ++- src/mts_hdr_copy.c | 105 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 110 insertions(+), 1 deletion(-) create mode 100644 src/mts_hdr_copy.c (limited to 'src') diff --git a/src/Makefile.am b/src/Makefile.am index 14fa47a..b56adf7 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -3,7 +3,7 @@ AUTOMAKE_OPTIONS = gnu AM_CFLAGS = -Wall bin_PROGRAMS = mts-id-eeprom -sbin_PROGRAMS = mts-hashpwd mts-fpga-loader +sbin_PROGRAMS = mts-hashpwd mts-fpga-loader mts-hdr-copy mts_hashpwd_SOURCES = hashpwd.cpp mts_id_eeprom_SOURCES = eeprom_main.c noinst_HEADERS = eeprom.h log.h mts_error_codes.h mts_fpga_hash.h mts_fpga_reg.h mts_fpga_spi.h @@ -12,6 +12,8 @@ mts_hashpwd_LDADD = -lcrypto mts_fpga_loader_SOURCES = mts_fpga_hash.c mts_fpga_reg.c mts_fpga_spi.c mts_fpga_loader.c mts_fpga_loader_LDADD = -lrt -lm -lssl -lcrypto +mts_hdr_copy_SOURCES = mts_hdr_copy.c + sbin_SCRIPTS = ubpasswd.sh EXTRA_DIST = @@ -33,4 +35,6 @@ install-exec-hook: mv mts-hashpwd ../../sbin/mts-hashpwd cd $(DESTDIR)$(sbindir) && \ mv mts-fpga-loader ../../sbin/mts-fpga-loader + cd $(DESTDIR)$(sbindir) && \ + mv mts-hdr-cpy ../../sbin/mts-hdr-cpy rmdir $(DESTDIR)$(sbindir) diff --git a/src/mts_hdr_copy.c b/src/mts_hdr_copy.c new file mode 100644 index 0000000..2f1da3e --- /dev/null +++ b/src/mts_hdr_copy.c @@ -0,0 +1,105 @@ +/* + * Read in the 1st 52 4 byte words, and if over half + * match, write the majority value to stdout. + * This is intended to look at the ROMBOOT header for + * bootstrap that sets the NAND flash parameters + * for ATMEL's ROMBOOT to load bootstrap. + */ +#include +#include +#include +#include +#include +#include + +#ifdef DEBUG + #define DBGPRT(fmt,...) fprintf(stderr,fmt,##__VA_ARGS__) +#else + #define DBGPRT(fmt,...) +#endif + +int +compare(const void *a, const void *b) +{ + unsigned int a1 = *((const unsigned int *)a); + unsigned int b1 = *((const unsigned int *)b); + if(a1 < b1) return -1; + if(a1 > b1) return 1; + return 0; +} + +#define NMEMB 52 +int +main(int argc, char *argv) +{ + unsigned int header[NMEMB]; + int i,count; + int nmax, lastmax; + unsigned int previous, lastmaxval; + + if(argc > 1) { + fprintf(stderr, + "Usage:\n" + " cat /dev/mtd1 | mts_hdr_copy >/tmp/hdr.bin\n" + " This comand reads standard input, and if over\n" + " half the 32 bit words for the first 52 words\n" + " are in agreement, 52 copies of the 32 bit word\n" + " are written to stdout. Otherwise we exit with\n" + " status 1. This command is intended to read the\n" + " existing bootstrap in /dev/mtd1, to create a\n" + " new bootstrap\n" + ); + exit(0); + } + + count = read(0,header,sizeof header); + + if (count != sizeof header) { + DBGPRT("Could not read complete input\n"); + exit(1); + } + qsort(header,NMEMB,sizeof(header[0]),compare); + + for (i=0;i lastmax) { + lastmax = nmax; + DBGPRT("nmax > lastmax, lastmax %d\n",lastmax); + lastmaxval = previous; + } + previous = header[i]; + nmax = 1; + } + } + if (nmax > lastmax) { + lastmax = nmax; + lastmaxval = previous; + } + DBGPRT("lastmax=%d\n",lastmax); + if (lastmax > NMEMB/2) { + for (i=0; i< NMEMB; i++) + header[i] = lastmaxval; + fprintf(stderr,"Attempting to write 52 copies of 0x%8.8x\n",lastmaxval); + if ((count = write(1,header,sizeof header)) == sizeof header) + exit(0); + else { + if(count >= 0) + fprintf(stderr,"ERROR: Wrote only %d of %llu bytes\n",count,(unsigned long long)(sizeof header)); + else + perror("Error writing header"); + } + } + fprintf(stderr,"ERROR: Found value 0x%x %d times out of 52 possible\n",lastmaxval,lastmax); + return 1; +} -- cgit v1.2.3