diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/Makefile.am | 6 | ||||
-rw-r--r-- | src/mts_hdr_copy.c | 105 |
2 files changed, 110 insertions, 1 deletions
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 <fcntl.h> +#include <stdlib.h> +#include <unistd.h> +#include <stdio.h> +#include <sys/types.h> +#include <sys/stat.h> + +#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<NMEMB;i++) + DBGPRT("0x%8.8x\n",header[i]); + + nmax = 0, lastmax = -1; + lastmaxval = 0; + previous = header[0]+1; + + for (i=0;i<NMEMB;i++) { + DBGPRT("Considering 0x%x, previous 0x%x\n",header[i],previous); + if (previous == header[i]) { + nmax++; + DBGPRT("Matches, i=%d 0x%x, nmax=%d\n",i,header[i],nmax); + } else { + if (nmax > 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; +} |