From e3615d09e64b1584b9e7820e2a8bcdb5569d2994 Mon Sep 17 00:00:00 2001 From: John Klug Date: Tue, 30 Jan 2018 17:45:15 -0600 Subject: Command to copy bootstrap header for Romboot --- src/Makefile.am | 2 +- src/mts_hdr_copy.c | 64 +++++++++++++++++++++++++++++++++++++++--------------- 2 files changed, 48 insertions(+), 18 deletions(-) (limited to 'src') diff --git a/src/Makefile.am b/src/Makefile.am index b56adf7..a559d8e 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -36,5 +36,5 @@ install-exec-hook: cd $(DESTDIR)$(sbindir) && \ mv mts-fpga-loader ../../sbin/mts-fpga-loader cd $(DESTDIR)$(sbindir) && \ - mv mts-hdr-cpy ../../sbin/mts-hdr-cpy + mv mts-hdr-copy ../../sbin/mts-hdr-copy rmdir $(DESTDIR)$(sbindir) diff --git a/src/mts_hdr_copy.c b/src/mts_hdr_copy.c index 2f1da3e..dfd6472 100644 --- a/src/mts_hdr_copy.c +++ b/src/mts_hdr_copy.c @@ -9,6 +9,9 @@ #include #include #include +#include +#include +#include #include #include @@ -28,31 +31,58 @@ compare(const void *a, const void *b) return 0; } +void +usage(void) +{ + fprintf(stderr, + "Usage:\n" + " mts-hdr-copy /dev/mtd1 >/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(1); +} + #define NMEMB 52 int -main(int argc, char *argv) +main(int argc, char *argv[]) { unsigned int header[NMEMB]; - int i,count; - int nmax, lastmax; + int i,count,opt; + int nmax, lastmax, fd; 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" - ); + fclose(stdin); + + while((opt = getopt(argc,argv,"")) != -1) switch(opt){ + case 'h': + usage(); + break; + default: + usage(); + break; + } + if(argc - optind != 1) { + fprintf(stderr,"Missing device parameter\n"); + usage(); exit(0); } - count = read(0,header,sizeof header); + fd = open(argv[optind],O_RDONLY); + + if (fd == -1) { + fprintf(stderr,"mts-hdr-copy: File argument \"%s\" could not be opened: %s\n", + argv[optind],strerror(errno)); + usage(); + } + + count = read(fd,header,sizeof header); + close(fd); if (count != sizeof header) { DBGPRT("Could not read complete input\n"); @@ -90,7 +120,7 @@ main(int argc, char *argv) 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); + fprintf(stderr,"Attempting to write 52 copies of 0x%8.8x for the bootstrap header\n",lastmaxval); if ((count = write(1,header,sizeof header)) == sizeof header) exit(0); else { -- cgit v1.2.3