changeset 713:cf7bd5e705ed

fc-loadtool: flash compal-imei command implemented
author Mychaela Falconia <falcon@freecalypso.org>
date Fri, 05 Jun 2020 07:54:33 +0000
parents a167d7b376b7
children e78c7ab3b0e6
files loadtools/flmain.c loadtools/flprotreg.c
diffstat 2 files changed, 92 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/loadtools/flmain.c	Fri Jun 05 07:29:59 2020 +0000
+++ b/loadtools/flmain.c	Fri Jun 05 07:54:33 2020 +0000
@@ -92,6 +92,7 @@
 }
 
 extern int flashcmd_blankchk();
+extern int flashcmd_compal_imei();
 extern int flashcmd_dump2file();
 extern int flashcmd_erase();
 extern int flashcmd_erase_program_boot();
@@ -110,6 +111,7 @@
 	int (*func)();
 } cmdtab[] = {
 	{"blankchk", flashcmd_blankchk},
+	{"compal-imei", flashcmd_compal_imei},
 	{"dump2bin", flashcmd_dump2file},
 	{"dump2srec", flashcmd_dump2file},
 	{"erase", flashcmd_erase},
--- a/loadtools/flprotreg.c	Fri Jun 05 07:29:59 2020 +0000
+++ b/loadtools/flprotreg.c	Fri Jun 05 07:54:33 2020 +0000
@@ -37,3 +37,93 @@
 		data[8]);
 	return(0);
 }
+
+static int
+compute_cd(digits)
+	u_char *digits;
+{
+	int i, dig, sum;
+
+	sum = 0;
+	for (i = 0; i < 14; i++) {
+		dig = digits[i];
+		if (i & 1) {
+			dig *= 2;
+			if (dig > 9)
+				dig -= 9;
+		}
+		sum += dig;
+	}
+	dig = sum % 10;
+	if (dig)
+		dig = 10 - dig;
+	return dig;
+}
+
+static void
+format_imei(digits, strout)
+	u_char *digits;
+	char *strout;
+{
+	int i;
+	char *dp;
+
+	dp = strout;
+	for (i = 0; i < 15; i++) {
+		if (i == 8 || i == 14)
+			*dp++ = '-';
+		*dp++ = digits[i] + '0';
+	}
+	*dp = '\0';
+}
+
+flashcmd_compal_imei(argc, argv, bank)
+	char **argv;
+{
+	struct flash_bank_info *bi;
+	uint16_t data[9];
+	u_char nibbles[16], *dp;
+	char imei_str[18];
+	int rc, i, j;
+	FILE *of;
+
+	if (argc > 3) {
+		fprintf(stderr, "error: too many arguments\n");
+		return(-1);
+	}
+	if (flash_detect(bank, 0) < 0)
+		return(-1);
+	bi = flash_bank_info + bank;
+	rc = bi->ops->read_prot_reg(bi, data);
+	if (rc)
+		return(rc);
+	dp = nibbles;
+	for (i = 0; i < 4; i++) {
+		rc = data[i + 5];
+		for (j = 0; j < 4; j++) {
+			*dp++ = rc & 0xF;
+			rc >>= 4;
+		}
+	}
+	for (i = 1; i < 16; i++) {
+		if (nibbles[i] > 9) {
+bad_imei:		fprintf(stderr, "No Compal IMEI found\n");
+			return(-1);
+		}
+	}
+	if (compute_cd(nibbles+1) != nibbles[15])
+		goto bad_imei;
+	format_imei(nibbles+1, imei_str);
+	puts(imei_str);
+	if (argc < 3)
+		return(0);
+	of = fopen(argv[2], "w");
+	if (!of) {
+		perror(argv[2]);
+		return(-1);
+	}
+	fprintf(of, "%s\n", imei_str);
+	fclose(of);
+	printf("Saved to %s\n", argv[2]);
+	return(0);
+}