/* * 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 #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; } 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[]) { unsigned int header[NMEMB]; int i,count,opt; int nmax, lastmax, fd; unsigned int previous, lastmaxval; 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); } 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"); 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 for the bootstrap header\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; }