FreeCalypso > hg > freecalypso-tools
comparison loadtools/flerase.c @ 668:cd48bc4c5460
fc-loadtool code: erase command split out into flerase.c
| author | Mychaela Falconia <falcon@freecalypso.org> |
|---|---|
| date | Sun, 08 Mar 2020 00:29:11 +0000 |
| parents | loadtools/flmisc.c@4be92bcd1535 |
| children | ba9523ca6ed8 |
comparison
equal
deleted
inserted
replaced
| 667:2772cf8435b4 | 668:cd48bc4c5460 |
|---|---|
| 1 /* | |
| 2 * Flash erase operations are implemented here | |
| 3 */ | |
| 4 | |
| 5 #include <sys/types.h> | |
| 6 #include <stdio.h> | |
| 7 #include <stdint.h> | |
| 8 #include <string.h> | |
| 9 #include <strings.h> | |
| 10 #include <stdlib.h> | |
| 11 #include <time.h> | |
| 12 #include "flash.h" | |
| 13 | |
| 14 extern struct flash_bank_info flash_bank_info[2]; | |
| 15 | |
| 16 flashcmd_erase(argc, argv, bank) | |
| 17 char **argv; | |
| 18 { | |
| 19 struct flash_bank_info *bi; | |
| 20 u_long offset, len; | |
| 21 char *strtoul_endp; | |
| 22 struct sector_info *startsec, *endsec, *sp; | |
| 23 int stat; | |
| 24 time_t start_time, finish_time; | |
| 25 unsigned duration, mm, ss; | |
| 26 | |
| 27 if (argc != 4) { | |
| 28 inv: fprintf(stderr, "usage: %s %s hex-start-offset hex-length\n", | |
| 29 argv[0], argv[1]); | |
| 30 return(-1); | |
| 31 } | |
| 32 offset = strtoul(argv[2], &strtoul_endp, 16); | |
| 33 if (*strtoul_endp) | |
| 34 goto inv; | |
| 35 if (flash_detect(bank, 0) < 0) | |
| 36 return(-1); | |
| 37 bi = flash_bank_info + bank; | |
| 38 if (offset >= bi->geom->total_size) { | |
| 39 fprintf(stderr, | |
| 40 "error: specified offset exceeds flash bank size (0x%lx)\n", | |
| 41 (u_long) bi->geom->total_size); | |
| 42 return(-1); | |
| 43 } | |
| 44 len = strtoul(argv[3], &strtoul_endp, 16); | |
| 45 if (*strtoul_endp) | |
| 46 goto inv; | |
| 47 if (len > bi->geom->total_size - offset) { | |
| 48 fprintf(stderr, | |
| 49 "error: specified offset+length exceed flash bank size (0x%lx)\n", | |
| 50 (u_long) bi->geom->total_size); | |
| 51 return(-1); | |
| 52 } | |
| 53 if (!len) { | |
| 54 printf("Zero length specified - nothing to do!\n"); | |
| 55 return(0); | |
| 56 } | |
| 57 /* now enforce sector alignment for both offset and length */ | |
| 58 if (get_flash_sector_table(bank) < 0) | |
| 59 return(-1); | |
| 60 if (get_flash_sector_range(bi, offset, len, &startsec, &endsec) < 0) | |
| 61 return(-1); | |
| 62 printf("Erasing %d sector(s)\n", endsec - startsec); | |
| 63 time(&start_time); | |
| 64 for (sp = startsec; sp < endsec; sp++) { | |
| 65 stat = bi->ops->erase_sector(bi, sp); | |
| 66 if (stat) | |
| 67 return(stat); | |
| 68 putchar('.'); | |
| 69 fflush(stdout); | |
| 70 } | |
| 71 time(&finish_time); | |
| 72 putchar('\n'); | |
| 73 duration = finish_time - start_time; | |
| 74 mm = duration / 60; | |
| 75 ss = duration - mm * 60; | |
| 76 printf("Operation completed in %um%us\n", mm, ss); | |
| 77 return(0); | |
| 78 } |
