diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/loadtools/flerase.c	Sun Mar 08 00:29:11 2020 +0000
@@ -0,0 +1,78 @@
+/*
+ * Flash erase operations are implemented here
+ */
+
+#include <sys/types.h>
+#include <stdio.h>
+#include <stdint.h>
+#include <string.h>
+#include <strings.h>
+#include <stdlib.h>
+#include <time.h>
+#include "flash.h"
+
+extern struct flash_bank_info flash_bank_info[2];
+
+flashcmd_erase(argc, argv, bank)
+	char **argv;
+{
+	struct flash_bank_info *bi;
+	u_long offset, len;
+	char *strtoul_endp;
+	struct sector_info *startsec, *endsec, *sp;
+	int stat;
+	time_t start_time, finish_time;
+	unsigned duration, mm, ss;
+
+	if (argc != 4) {
+inv:		fprintf(stderr, "usage: %s %s hex-start-offset hex-length\n",
+			argv[0], argv[1]);
+		return(-1);
+	}
+	offset = strtoul(argv[2], &strtoul_endp, 16);
+	if (*strtoul_endp)
+		goto inv;
+	if (flash_detect(bank, 0) < 0)
+		return(-1);
+	bi = flash_bank_info + bank;
+	if (offset >= bi->geom->total_size) {
+		fprintf(stderr,
+		"error: specified offset exceeds flash bank size (0x%lx)\n",
+			(u_long) bi->geom->total_size);
+		return(-1);
+	}
+	len = strtoul(argv[3], &strtoul_endp, 16);
+	if (*strtoul_endp)
+		goto inv;
+	if (len > bi->geom->total_size - offset) {
+		fprintf(stderr,
+	"error: specified offset+length exceed flash bank size (0x%lx)\n",
+			(u_long) bi->geom->total_size);
+		return(-1);
+	}
+	if (!len) {
+		printf("Zero length specified - nothing to do!\n");
+		return(0);
+	}
+	/* now enforce sector alignment for both offset and length */
+	if (get_flash_sector_table(bank) < 0)
+		return(-1);
+	if (get_flash_sector_range(bi, offset, len, &startsec, &endsec) < 0)
+		return(-1);
+	printf("Erasing %d sector(s)\n", endsec - startsec);
+	time(&start_time);
+	for (sp = startsec; sp < endsec; sp++) {
+		stat = bi->ops->erase_sector(bi, sp);
+		if (stat)
+			return(stat);
+		putchar('.');
+		fflush(stdout);
+	}
+	time(&finish_time);
+	putchar('\n');
+	duration = finish_time - start_time;
+	mm = duration / 60;
+	ss = duration - mm * 60;
+	printf("Operation completed in %um%us\n", mm, ss);
+	return(0);
+}