view loadtools/flmain.c @ 962:e3706ce1d01c

CHANGES: document sms-pdu-decode -n option
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 05 Aug 2023 00:01:42 +0000
parents cf7bd5e705ed
children 511e2b85c115
line wrap: on
line source

/*
 * This module is the main entry point for fc-loadtool flash functions
 */

#include <sys/types.h>
#include <ctype.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <strings.h>
#include <stdlib.h>
#include "flash.h"

extern int flash_global_config;
extern struct flash_bank_info flash_bank_info[2];

flashcmd_geom(argc, argv, bank)
	char **argv;
{
	struct flash_bank_info *bi;
	unsigned rn;

	if (argc > 2) {
		fprintf(stderr, "error: too many arguments\n");
		return(-1);
	}
	if (flash_detect(bank, 0) < 0)
		return(-1);
	bi = flash_bank_info + bank;
	printf("Detected flash device: %s\n", bi->device->name);
	if (bi->device->bank_geom[1])
		printf("Device has two banks, looking at bank %d\n", bank);
	else
		printf("Single-bank flash device\n");
	printf("Bank %d total size: 0x%lx\n", bank,
		(u_long) bi->geom->total_size);
	printf("Sectors in bank %d: %u (%u regions)\n", bank,
		bi->geom->total_sectors, bi->geom->nregions);
	for (rn = 0; rn < bi->geom->nregions; rn++)
		printf("Region %u: %u sectors of 0x%lx bytes\n", rn,
			bi->geom->regions[rn].nsectors,
			(u_long) bi->geom->regions[rn].sector_size);
	printf("Command set style: %s\n", bi->ops->cmdset_name);
	return(0);
}

flashcmd_help()
{
	return loadtool_help("flash");
}

flashcmd_id(argc, argv, bank)
	char **argv;
{
	if (argc > 2) {
		fprintf(stderr, "error: too many arguments\n");
		return(-1);
	}
	return flash_detect(bank, 1);
}

flashcmd_info(argc, argv)
	char **argv;
{
	int bank, nbanks;

	if (argc > 2) {
		fprintf(stderr, "error: too many arguments\n");
		return(-1);
	}
	switch (flash_global_config) {
	case FLASH_GLOBAL_CFG_SINGLE_4M:
		printf("Configured for a single flash bank of up to 4 MiB\n");
		nbanks = 1;
		break;
	case FLASH_GLOBAL_CFG_SINGLE_8M:
		printf("Configured for a single flash bank of up to 8 MiB\n");
		nbanks = 1;
		break;
	case FLASH_GLOBAL_CFG_DUAL_8M:
		printf("Configured for two flash banks of up to 8 MiB each\n");
		nbanks = 2;
		break;
	default:
		fprintf(stderr, "error: invalid global config\n");
		return(-1);
	}
	for (bank = 0; bank < nbanks; bank++)
		printf("Bank %d base address: %08lX\n", bank,
			(u_long) flash_bank_info[bank].base_addr);
	return(0);
}

extern int flashcmd_blankchk();
extern int flashcmd_compal_imei();
extern int flashcmd_dump2file();
extern int flashcmd_erase();
extern int flashcmd_erase_program_boot();
extern int flashcmd_progbin_wrap();
extern int flashcmd_program_m0();
extern int flashcmd_program_srec();
extern int flashcmd_protreg();
extern int flashcmd_quickprog();
extern int flashcmd_reset();
extern int flashcmd_sectors();
extern int flashcmd_status();
extern int flashcmd_unlock();

static struct cmdtab {
	char *cmd;
	int (*func)();
} cmdtab[] = {
	{"blankchk", flashcmd_blankchk},
	{"compal-imei", flashcmd_compal_imei},
	{"dump2bin", flashcmd_dump2file},
	{"dump2srec", flashcmd_dump2file},
	{"erase", flashcmd_erase},
	{"erase-program-boot", flashcmd_erase_program_boot},
	{"e-program-bin", flashcmd_progbin_wrap},
	{"e-program-m0", flashcmd_program_m0},
	{"e-program-srec", flashcmd_program_srec},
	{"geom", flashcmd_geom},
	{"help", flashcmd_help},
	{"id", flashcmd_id},
	{"info", flashcmd_info},
	{"program-bin", flashcmd_progbin_wrap},
	{"program-m0", flashcmd_program_m0},
	{"program-srec", flashcmd_program_srec},
	{"prot-reg", flashcmd_protreg},
	{"quickprog", flashcmd_quickprog},
	{"reset", flashcmd_reset},
	{"sectors", flashcmd_sectors},
	{"status", flashcmd_status},
	{"unlock", flashcmd_unlock},
	{0, 0}
};

cmd_flash(argc, argv)
	char **argv;
{
	int bank;
	struct cmdtab *tp;

	if (!flash_global_config) {
		fprintf(stderr, "No flash configuration defined\n");
		return(-1);
	}
	if (argv[0][5] == '2') {
		if (flash_global_config != FLASH_GLOBAL_CFG_DUAL_8M) {
			fprintf(stderr, "No second flash bank configured\n");
			return(-1);
		}
		bank = 1;
	} else
		bank = 0;
	for (tp = cmdtab; tp->cmd; tp++)
		if (!strcmp(tp->cmd, argv[1]))
			break;
	if (!tp->func) {
		fprintf(stderr, "%s %s: unknown/unimplemented subcommand\n",
			argv[0], argv[1]);
		return(-1);
	}
	return tp->func(argc, argv, bank);
}