view loadtools/romdump.c @ 669:ba9523ca6ed8

fc-loadtool code: preparations for e-program-* addition
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 08 Mar 2020 01:32:08 +0000
parents cef70d53fc5e
children ecea01f65146
line wrap: on
line source

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

#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 char default_dspdump_image[];
extern struct baudrate *current_baud_rate;
extern void (*default_exit)();

extern struct baudrate *find_baudrate_by_name();

static struct baudrate *romdump_baudrate;
static char *output_filename;
static FILE *outfile;

static
dump_receiver(line)
	char *line;
{
	if (!strncmp(line, "DSP dump:", 9) || !strncmp(line, "ERROR:", 6))
		puts(line);
	fprintf(outfile, "%s\n", line);
	return(1);
}

main(argc, argv)
	char **argv;
{
	extern char *optarg;
	extern int optind;
	int c;
	struct baudrate *br;
	char *targv[2];

	while ((c = getopt(argc, argv, "a:b:B:c:C:h:H:i:P:")) != EOF)
		switch (c) {
		case 'a':
			iramimage.filename = optarg;
			continue;
		case 'b':
			set_romload_baudrate(optarg);
			continue;
		case 'B':
			br = find_baudrate_by_name(optarg);
			if (!br)
				exit(1);	/* error msg already printed */
			romdump_baudrate = br;
			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 'P':
			if (find_bootctrl_entry(optarg) < 0)
				exit(1);	/* error msg already printed */
			continue;
		case '?':
		default:
usage:			fprintf(stderr,
			"usage: fc-dspromdump [options] ttyport output-file\n");
			exit(1);
		}
	if (argc - optind != 2)
		goto usage;
	target_ttydev = argv[optind];
	output_filename = argv[optind+1];
	if (!iramimage.filename)
		iramimage.filename = default_dspdump_image;

	open_serial_port(target_ttydev);
	pwon_if_needed();
	perform_compal_stage();
	perform_romload();
	/* dspdump target program should be running now */
	if (tpinterf_pass_output(1) < 0)
		exit(1);
	if (romdump_baudrate && romdump_baudrate != current_baud_rate) {
		c = loadagent_switch_baud(romdump_baudrate);
		if (c)
			exit(1);
	}
	outfile = fopen(output_filename, "w");
	if (!outfile) {
		perror(output_filename);
		exit(1);
	}
	printf("Requesting DSP ROM dump\n");
	targv[0] = "fulldump";
	targv[1] = 0;
	tpinterf_make_cmd(targv);
	if (tpinterf_send_cmd() < 0)
		exit(1);
	c = tpinterf_capture_output(2, dump_receiver);
	fclose(outfile);
	if (c < 0)
		exit(1);
	default_exit(0);
}