view loadtools/simmain.c @ 964:a96cb97b66a2

ringtools/imy: fix duplicate definition of tdma_durations[] The bug was reported by Vadim Yanitskiy <fixeria@osmocom.org>, although the present fix is slightly different from the contributed patch: because main.c doesn't need this tdma_durations[] array at all, let's simply remove the reference to this array from main.c rather than turn it into an extern. I no longer remember my original thought flow that resulted (by mistake) in tdma_durations[] being multiply defined in main.c and durations.c. My intent might have been to define all globals in main.c and have the reference in durations.c be an extern - and I missed that extern - but without clear memory, I have no certainty. In any case, having this data array defined in the same module that fills it (durations.c) is sensible, so let's make it the new way.
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 31 Aug 2023 19:38:18 +0000
parents 02490e26f53d
children
line wrap: on
line source

/*
 * This module contains the main() function for fc-simint.
 */

#include <sys/types.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include "../libserial/baudrate.h"
#include "srecreader.h"

char *target_ttydev;

extern struct srecreader iramimage;
extern struct baudrate *current_baud_rate;
extern void (*default_exit)();

extern struct baudrate *find_baudrate_by_name();

static char simagent_pathname[] = "/opt/freecalypso/target-bin/simagent.srec";
static char simtool_pathname[] = "/opt/freecalypso/bin/fc-simtool";
static char uicc_tool_pathname[] = "/opt/freecalypso/bin/fc-uicc-tool";

static char *selected_simtool = simtool_pathname;
static struct baudrate *session_baudrate;

int sim_voltage_mode_1v8;
int sim_allow_spenh = 1;

static void
tool_select_arg(arg)
	char *arg;
{
	if (!strcmp(arg, "sim"))
		selected_simtool = simtool_pathname;
	else if (!strcmp(arg, "uicc"))
		selected_simtool = uicc_tool_pathname;
	else {
		fprintf(stderr, "error: invalid -T argument\n");
		exit(1);
	}
}

static void
voltage_select_arg(arg)
	char *arg;
{
	if (!strcmp(arg, "1.8"))
		sim_voltage_mode_1v8 = 1;
	else if (!strcmp(arg, "3") || !strcmp(arg, "3.0"))
		sim_voltage_mode_1v8 = 0;
	else {
		fprintf(stderr, "error: invalid -v argument\n");
		exit(1);
	}
}

static void
exec_simtool(passon_argc, passon_argv)
	char **passon_argv;
{
	char **exec_argv, *prog_base_name;
	char **sp, **dp;
	extern int target_fd;
	char desc_arg[16];

	prog_base_name = rindex(selected_simtool, '/') + 1;
	sprintf(desc_arg, "-C%d", target_fd);
	exec_argv = (char **) malloc(sizeof(char *) * (passon_argc + 3));
	if (!exec_argv) {
		perror("malloc argv for execv");
		exit(1);
	}
	sp = passon_argv;
	dp = exec_argv;
	*dp++ = prog_base_name;
	*dp++ = desc_arg;
	while (*sp)
		*dp++ = *sp++;
	*dp = NULL;
	execv(selected_simtool, exec_argv);
	fprintf(stderr, "Unable to exec %s\n", selected_simtool);
	exit(1);
}

main(argc, argv)
	char **argv;
{
	extern char *optarg;
	extern int optind;
	int c;

	while ((c = getopt(argc, argv, "a:b:B:c:C:h:H:i:nP:t:T:v:")) != EOF)
		switch (c) {
		case 'a':
			iramimage.filename = optarg;
			continue;
		case 'b':
			set_romload_baudrate(optarg);
			continue;
		case 'B':
			session_baudrate = find_baudrate_by_name(optarg);
			if (!session_baudrate)
				exit(1);	/* error msg already printed */
			continue;
		case 'c':
			set_compalstage_short(optarg);
			continue;
		case 'C':
			set_compalstage_fullpath(optarg);
			continue;
		case 'h':
			read_hwparam_file_shortname(optarg);
			continue;
		case 'H':
			read_hwparam_file_fullpath(optarg);
			continue;
		case 'i':
			set_beacon_interval(optarg);
			continue;
		case 'n':
			sim_allow_spenh = 0;
			continue;
		case 'P':
			if (find_bootctrl_entry(optarg) < 0)
				exit(1);	/* error msg already printed */
			continue;
		case 't':
			set_romload_timeout(optarg);
			continue;
		case 'T':
			tool_select_arg(optarg);
			continue;
		case 'v':
			voltage_select_arg(optarg);
			continue;
		case '?':
		default:
usage:			fprintf(stderr,
			"usage: fc-simint [options] ttyport [batch command]\n");
			exit(1);
		}
	if (argc - optind < 1)
		goto usage;
	target_ttydev = argv[optind];
	if (!iramimage.filename)
		iramimage.filename = simagent_pathname;

	open_serial_port(target_ttydev);
	pwon_if_needed();
	perform_compal_stage();
	perform_romload();
	/* simagent target program should be running now */
	putchar('\n');
	if (tpinterf_pass_output(1) < 0)
		exit(1);
	putchar('\n');
	if (session_baudrate && session_baudrate != current_baud_rate) {
		c = loadagent_switch_baud(session_baudrate);
		if (c)
			exit(1);
	}
	do_sim_up();
	sim_atr_validate();
	sim_spenh_logic();
	exec_simtool(argc - optind - 1, argv + optind + 1);
}