diff options
author | Josh Boyer <jwboyer@gmail.com> | 2006-10-09 20:49:57 -0500 |
---|---|---|
committer | Josh Boyer <jwboyer@gmail.com> | 2006-10-09 20:49:57 -0500 |
commit | 37f40f5574e04ae050507133ade8fe0e6bae2f0d (patch) | |
tree | c1100ad9ef33d3d7097a00459e0e3de03d2a3b75 /flashcp.c | |
parent | 6041c88699b0b81a9cdc48a93e96629d99966137 (diff) |
Fixup whitespace
Signed-off-by: Josh Boyer <jwboyer@gmail.com>
Diffstat (limited to 'flashcp.c')
-rw-r--r-- | flashcp.c | 439 |
1 files changed, 219 insertions, 220 deletions
@@ -1,4 +1,3 @@ - /* * Copyright (c) 2d3D, Inc. * Written by Abraham vd Merwe <abraham@2d3d.co.za> @@ -79,81 +78,81 @@ typedef int bool; static void log_printf (int level,const char *fmt, ...) { - FILE *fp = level == LOG_NORMAL ? stdout : stderr; - va_list ap; - va_start (ap,fmt); - vfprintf (fp,fmt,ap); - va_end (ap); - fflush (fp); + FILE *fp = level == LOG_NORMAL ? stdout : stderr; + va_list ap; + va_start (ap,fmt); + vfprintf (fp,fmt,ap); + va_end (ap); + fflush (fp); } static void showusage (const char *progname,bool error) { - int level = error ? LOG_ERROR : LOG_NORMAL; - - log_printf (level, - "\n" - "Flash Copy - Written by Abraham van der Merwe <abraham@2d3d.co.za>\n" - "\n" - "usage: %s [ -v | --verbose ] <filename> <device>\n" - " %s -h | --help\n" - "\n" - " -h | --help Show this help message\n" - " -v | --verbose Show progress reports\n" - " <filename> File which you want to copy to flash\n" - " <device> Flash device to write to (e.g. /dev/mtd0, /dev/mtd1, etc.)\n" - "\n", - progname,progname); - - exit (error ? EXIT_FAILURE : EXIT_SUCCESS); + int level = error ? LOG_ERROR : LOG_NORMAL; + + log_printf (level, + "\n" + "Flash Copy - Written by Abraham van der Merwe <abraham@2d3d.co.za>\n" + "\n" + "usage: %s [ -v | --verbose ] <filename> <device>\n" + " %s -h | --help\n" + "\n" + " -h | --help Show this help message\n" + " -v | --verbose Show progress reports\n" + " <filename> File which you want to copy to flash\n" + " <device> Flash device to write to (e.g. /dev/mtd0, /dev/mtd1, etc.)\n" + "\n", + progname,progname); + + exit (error ? EXIT_FAILURE : EXIT_SUCCESS); } static int safe_open (const char *pathname,int flags) { - int fd; + int fd; - fd = open (pathname,flags); - if (fd < 0) - { + fd = open (pathname,flags); + if (fd < 0) + { log_printf (LOG_ERROR,"While trying to open %s",pathname); if (flags & O_RDWR) - log_printf (LOG_ERROR," for read/write access"); + log_printf (LOG_ERROR," for read/write access"); else if (flags & O_RDONLY) - log_printf (LOG_ERROR," for read access"); + log_printf (LOG_ERROR," for read access"); else if (flags & O_WRONLY) - log_printf (LOG_ERROR," for write access"); + log_printf (LOG_ERROR," for write access"); log_printf (LOG_ERROR,": %m\n"); exit (EXIT_FAILURE); - } + } - return (fd); + return (fd); } static void safe_read (int fd,const char *filename,void *buf,size_t count,bool verbose) { - ssize_t result; + ssize_t result; - result = read (fd,buf,count); - if (count != result) - { + result = read (fd,buf,count); + if (count != result) + { if (verbose) log_printf (LOG_NORMAL,"\n"); if (result < 0) - { - log_printf (LOG_ERROR,"While reading data from %s: %m\n",filename); - exit (EXIT_FAILURE); - } + { + log_printf (LOG_ERROR,"While reading data from %s: %m\n",filename); + exit (EXIT_FAILURE); + } log_printf (LOG_ERROR,"Short read count returned while reading from %s\n",filename); exit (EXIT_FAILURE); - } + } } static void safe_rewind (int fd,const char *filename) { - if (lseek (fd,0L,SEEK_SET) < 0) - { + if (lseek (fd,0L,SEEK_SET) < 0) + { log_printf (LOG_ERROR,"While seeking to start of %s: %m\n",filename); exit (EXIT_FAILURE); - } + } } /******************************************************************************/ @@ -162,154 +161,154 @@ static int dev_fd = -1,fil_fd = -1; static void cleanup (void) { - if (dev_fd > 0) close (dev_fd); - if (fil_fd > 0) close (fil_fd); + if (dev_fd > 0) close (dev_fd); + if (fil_fd > 0) close (fil_fd); } int main (int argc,char *argv[]) { - const char *progname,*filename = NULL,*device = NULL; - int i,flags = FLAG_NONE; - ssize_t result; - size_t size,written; - struct mtd_info_user mtd; - struct erase_info_user erase; - struct stat filestat; - unsigned char src[BUFSIZE],dest[BUFSIZE]; - - (progname = strrchr (argv[0],'/')) ? progname++ : (progname = argv[0]); - - /********************* - * parse cmd-line - *****************/ - - for (;;) { - int option_index = 0; - static const char *short_options = "hv"; - static const struct option long_options[] = { - {"help", no_argument, 0, 'h'}, - {"verbose", no_argument, 0, 'v'}, - {0, 0, 0, 0}, - }; - - int c = getopt_long(argc, argv, short_options, - long_options, &option_index); - if (c == EOF) { - break; - } - - switch (c) { - case 'h': - flags |= FLAG_HELP; - DEBUG("Got FLAG_HELP\n"); - break; - case 'v': - flags |= FLAG_VERBOSE; - DEBUG("Got FLAG_VERBOSE\n"); - break; - default: - DEBUG("Unknown parameter: %s\n",argv[option_index]); - showusage (progname,true); - } - } - if (optind+2 == argc) { - flags |= FLAG_FILENAME; - filename = argv[optind]; - DEBUG("Got filename: %s\n",filename); - - flags |= FLAG_DEVICE; - device = argv[optind+1]; - DEBUG("Got device: %s\n",device); - } - - if (flags & FLAG_HELP || progname == NULL || device == NULL) - showusage (progname,flags != FLAG_HELP); - - atexit (cleanup); - - /* get some info about the flash device */ - dev_fd = safe_open (device,O_SYNC | O_RDWR); - if (ioctl (dev_fd,MEMGETINFO,&mtd) < 0) - { + const char *progname,*filename = NULL,*device = NULL; + int i,flags = FLAG_NONE; + ssize_t result; + size_t size,written; + struct mtd_info_user mtd; + struct erase_info_user erase; + struct stat filestat; + unsigned char src[BUFSIZE],dest[BUFSIZE]; + + (progname = strrchr (argv[0],'/')) ? progname++ : (progname = argv[0]); + + /********************* + * parse cmd-line + *****************/ + + for (;;) { + int option_index = 0; + static const char *short_options = "hv"; + static const struct option long_options[] = { + {"help", no_argument, 0, 'h'}, + {"verbose", no_argument, 0, 'v'}, + {0, 0, 0, 0}, + }; + + int c = getopt_long(argc, argv, short_options, + long_options, &option_index); + if (c == EOF) { + break; + } + + switch (c) { + case 'h': + flags |= FLAG_HELP; + DEBUG("Got FLAG_HELP\n"); + break; + case 'v': + flags |= FLAG_VERBOSE; + DEBUG("Got FLAG_VERBOSE\n"); + break; + default: + DEBUG("Unknown parameter: %s\n",argv[option_index]); + showusage (progname,true); + } + } + if (optind+2 == argc) { + flags |= FLAG_FILENAME; + filename = argv[optind]; + DEBUG("Got filename: %s\n",filename); + + flags |= FLAG_DEVICE; + device = argv[optind+1]; + DEBUG("Got device: %s\n",device); + } + + if (flags & FLAG_HELP || progname == NULL || device == NULL) + showusage (progname,flags != FLAG_HELP); + + atexit (cleanup); + + /* get some info about the flash device */ + dev_fd = safe_open (device,O_SYNC | O_RDWR); + if (ioctl (dev_fd,MEMGETINFO,&mtd) < 0) + { DEBUG("ioctl(): %m\n"); log_printf (LOG_ERROR,"This doesn't seem to be a valid MTD flash device!\n"); exit (EXIT_FAILURE); - } + } - /* get some info about the file we want to copy */ - fil_fd = safe_open (filename,O_RDONLY); - if (fstat (fil_fd,&filestat) < 0) - { + /* get some info about the file we want to copy */ + fil_fd = safe_open (filename,O_RDONLY); + if (fstat (fil_fd,&filestat) < 0) + { log_printf (LOG_ERROR,"While trying to get the file status of %s: %m\n",filename); exit (EXIT_FAILURE); - } + } - /* does it fit into the device/partition? */ - if (filestat.st_size > mtd.size) - { + /* does it fit into the device/partition? */ + if (filestat.st_size > mtd.size) + { log_printf (LOG_ERROR,"%s won't fit into %s!\n",filename,device); exit (EXIT_FAILURE); - } + } - /***************************************************** - * erase enough blocks so that we can write the file * - *****************************************************/ + /***************************************************** + * erase enough blocks so that we can write the file * + *****************************************************/ #warning "Check for smaller erase regions" - erase.start = 0; - erase.length = filestat.st_size & ~(mtd.erasesize - 1); - if (filestat.st_size % mtd.erasesize) erase.length += mtd.erasesize; - if (flags & FLAG_VERBOSE) - { + erase.start = 0; + erase.length = filestat.st_size & ~(mtd.erasesize - 1); + if (filestat.st_size % mtd.erasesize) erase.length += mtd.erasesize; + if (flags & FLAG_VERBOSE) + { /* if the user wants verbose output, erase 1 block at a time and show him/her what's going on */ int blocks = erase.length / mtd.erasesize; erase.length = mtd.erasesize; log_printf (LOG_NORMAL,"Erasing blocks: 0/%d (0%%)",blocks); for (i = 1; i <= blocks; i++) - { - log_printf (LOG_NORMAL,"\rErasing blocks: %d/%d (%d%%)",i,blocks,PERCENTAGE (i,blocks)); - if (ioctl (dev_fd,MEMERASE,&erase) < 0) - { - log_printf (LOG_NORMAL,"\n"); - log_printf (LOG_ERROR, - "While erasing blocks 0x%.8x-0x%.8x on %s: %m\n", - (unsigned int) erase.start,(unsigned int) (erase.start + erase.length),device); - exit (EXIT_FAILURE); - } - erase.start += mtd.erasesize; - } + { + log_printf (LOG_NORMAL,"\rErasing blocks: %d/%d (%d%%)",i,blocks,PERCENTAGE (i,blocks)); + if (ioctl (dev_fd,MEMERASE,&erase) < 0) + { + log_printf (LOG_NORMAL,"\n"); + log_printf (LOG_ERROR, + "While erasing blocks 0x%.8x-0x%.8x on %s: %m\n", + (unsigned int) erase.start,(unsigned int) (erase.start + erase.length),device); + exit (EXIT_FAILURE); + } + erase.start += mtd.erasesize; + } log_printf (LOG_NORMAL,"\rErasing blocks: %d/%d (100%%)\n",blocks,blocks); - } - else - { + } + else + { /* if not, erase the whole chunk in one shot */ if (ioctl (dev_fd,MEMERASE,&erase) < 0) - { - log_printf (LOG_ERROR, - "While erasing blocks from 0x%.8x-0x%.8x on %s: %m\n", - (unsigned int) erase.start,(unsigned int) (erase.start + erase.length),device); - exit (EXIT_FAILURE); - } - } - DEBUG("Erased %u / %luk bytes\n",erase.length,filestat.st_size); - - /********************************** - * write the entire file to flash * - **********************************/ - - if (flags & FLAG_VERBOSE) log_printf (LOG_NORMAL,"Writing data: 0k/%luk (0%%)",KB (filestat.st_size)); - size = filestat.st_size; - i = BUFSIZE; - written = 0; - while (size) - { + { + log_printf (LOG_ERROR, + "While erasing blocks from 0x%.8x-0x%.8x on %s: %m\n", + (unsigned int) erase.start,(unsigned int) (erase.start + erase.length),device); + exit (EXIT_FAILURE); + } + } + DEBUG("Erased %u / %luk bytes\n",erase.length,filestat.st_size); + + /********************************** + * write the entire file to flash * + **********************************/ + + if (flags & FLAG_VERBOSE) log_printf (LOG_NORMAL,"Writing data: 0k/%luk (0%%)",KB (filestat.st_size)); + size = filestat.st_size; + i = BUFSIZE; + written = 0; + while (size) + { if (size < BUFSIZE) i = size; if (flags & FLAG_VERBOSE) - log_printf (LOG_NORMAL,"\rWriting data: %dk/%luk (%lu%%)", - KB (written + i), - KB (filestat.st_size), - PERCENTAGE (written + i,filestat.st_size)); + log_printf (LOG_NORMAL,"\rWriting data: %dk/%luk (%lu%%)", + KB (written + i), + KB (filestat.st_size), + PERCENTAGE (written + i,filestat.st_size)); /* read from filename */ safe_read (fil_fd,filename,src,i,flags & FLAG_VERBOSE); @@ -317,50 +316,50 @@ int main (int argc,char *argv[]) /* write to device */ result = write (dev_fd,src,i); if (i != result) - { - if (flags & FLAG_VERBOSE) log_printf (LOG_NORMAL,"\n"); - if (result < 0) - { - log_printf (LOG_ERROR, - "While writing data to 0x%.8x-0x%.8x on %s: %m\n", - written,written + i,device); - exit (EXIT_FAILURE); - } - log_printf (LOG_ERROR, - "Short write count returned while writing to x%.8x-0x%.8x on %s: %d/%lu bytes written to flash\n", - written,written + i,device,written + result,filestat.st_size); - exit (EXIT_FAILURE); - } + { + if (flags & FLAG_VERBOSE) log_printf (LOG_NORMAL,"\n"); + if (result < 0) + { + log_printf (LOG_ERROR, + "While writing data to 0x%.8x-0x%.8x on %s: %m\n", + written,written + i,device); + exit (EXIT_FAILURE); + } + log_printf (LOG_ERROR, + "Short write count returned while writing to x%.8x-0x%.8x on %s: %d/%lu bytes written to flash\n", + written,written + i,device,written + result,filestat.st_size); + exit (EXIT_FAILURE); + } written += i; size -= i; - } - if (flags & FLAG_VERBOSE) - log_printf (LOG_NORMAL, - "\rWriting data: %luk/%luk (100%%)\n", - KB (filestat.st_size), - KB (filestat.st_size)); - DEBUG("Wrote %d / %luk bytes\n",written,filestat.st_size); - - /********************************** - * verify that flash == file data * - **********************************/ - - safe_rewind (fil_fd,filename); - safe_rewind (dev_fd,device); - size = filestat.st_size; - i = BUFSIZE; - written = 0; - if (flags & FLAG_VERBOSE) log_printf (LOG_NORMAL,"Verifying data: 0k/%luk (0%%)",KB (filestat.st_size)); - while (size) - { + } + if (flags & FLAG_VERBOSE) + log_printf (LOG_NORMAL, + "\rWriting data: %luk/%luk (100%%)\n", + KB (filestat.st_size), + KB (filestat.st_size)); + DEBUG("Wrote %d / %luk bytes\n",written,filestat.st_size); + + /********************************** + * verify that flash == file data * + **********************************/ + + safe_rewind (fil_fd,filename); + safe_rewind (dev_fd,device); + size = filestat.st_size; + i = BUFSIZE; + written = 0; + if (flags & FLAG_VERBOSE) log_printf (LOG_NORMAL,"Verifying data: 0k/%luk (0%%)",KB (filestat.st_size)); + while (size) + { if (size < BUFSIZE) i = size; if (flags & FLAG_VERBOSE) - log_printf (LOG_NORMAL, - "\rVerifying data: %dk/%luk (%lu%%)", - KB (written + i), - KB (filestat.st_size), - PERCENTAGE (written + i,filestat.st_size)); + log_printf (LOG_NORMAL, + "\rVerifying data: %dk/%luk (%lu%%)", + KB (written + i), + KB (filestat.st_size), + PERCENTAGE (written + i,filestat.st_size)); /* read from filename */ safe_read (fil_fd,filename,src,i,flags & FLAG_VERBOSE); @@ -370,23 +369,23 @@ int main (int argc,char *argv[]) /* compare buffers */ if (memcmp (src,dest,i)) - { - log_printf (LOG_ERROR, - "File does not seem to match flash data. First mismatch at 0x%.8x-0x%.8x\n", - written,written + i); - exit (EXIT_FAILURE); - } + { + log_printf (LOG_ERROR, + "File does not seem to match flash data. First mismatch at 0x%.8x-0x%.8x\n", + written,written + i); + exit (EXIT_FAILURE); + } written += i; size -= i; - } - if (flags & FLAG_VERBOSE) - log_printf (LOG_NORMAL, - "\rVerifying data: %luk/%luk (100%%)\n", - KB (filestat.st_size), - KB (filestat.st_size)); - DEBUG("Verified %d / %luk bytes\n",written,filestat.st_size); - - exit (EXIT_SUCCESS); + } + if (flags & FLAG_VERBOSE) + log_printf (LOG_NORMAL, + "\rVerifying data: %luk/%luk (100%%)\n", + KB (filestat.st_size), + KB (filestat.st_size)); + DEBUG("Verified %d / %luk bytes\n",written,filestat.st_size); + + exit (EXIT_SUCCESS); } |