changeset 250:8c011177adb9

loadtools reworked to use factored-out libserial
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 21 Sep 2017 23:07:01 +0000
parents d0a4c05d98dc
children fb577c31e960
files libserial-orig/Makefile libserial-orig/setbyname.c loadtools/Makefile loadtools/baudrate.h loadtools/bpmain.c loadtools/clmain.c loadtools/compalload.c loadtools/compalram.c loadtools/labaud.c loadtools/ltmain.c loadtools/romload.c loadtools/sercomm.c loadtools/sertool.c
diffstat 13 files changed, 70 insertions(+), 156 deletions(-) [+]
line wrap: on
line diff
--- a/libserial-orig/Makefile	Thu Sep 21 21:49:07 2017 +0000
+++ b/libserial-orig/Makefile	Thu Sep 21 23:07:01 2017 +0000
@@ -1,6 +1,6 @@
 CC=	gcc
 CFLAGS=	-O2
-OBJS=	baudtab.o openport.o setbaud.o
+OBJS=	baudtab.o openport.o setbaud.o setbyname.o
 LIB=	libserial.a
 
 all:	${LIB}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libserial-orig/setbyname.c	Thu Sep 21 23:07:01 2017 +0000
@@ -0,0 +1,21 @@
+/*
+ * This module contains the wrapper function (used by loadtools)
+ * that looks up a baud rate by name and calls set_serial_baud().
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include "baudrate.h"
+
+extern struct baudrate *find_baudrate_by_name();
+
+set_fixed_baudrate(baudname)
+	char *baudname;
+{
+	struct baudrate *br;
+
+	br = find_baudrate_by_name(baudname);
+	if (!br)
+		exit(1);	/* error msg already printed */
+	set_serial_baudrate(br);
+}
--- a/loadtools/Makefile	Thu Sep 21 21:49:07 2017 +0000
+++ b/loadtools/Makefile	Thu Sep 21 23:07:01 2017 +0000
@@ -7,43 +7,44 @@
 
 EXTRA_OBJ=	compalload.o
 
-COMPALRAM_OBJS=	compalload.o compalram.o defpath.o sercomm.o ttypassthru.o
+COMPALRAM_OBJS=	compalload.o compalram.o defpath.o ttypassthru.o
 
 IRAM_OBJS=	defexitstub.o defpath.o flashstubs.o hexdecode.o hwparam.o \
-		romload.o secondprog.o sercomm.o sertool.o srecreader.o \
-		ttypassthru.o ${EXTRA_OBJ}
+		romload.o secondprog.o sertool.o srecreader.o ttypassthru.o \
+		${EXTRA_OBJ}
 
 LOADTOOL_OBJS=	crc32tab.o defpath.o flashops.o flcmplboot.o flmain.o flmisc.o \
 		flprogbin.o flprogsrec.o flutil.o hexdecode.o hwparam.o \
 		labaud.o ltdispatch.o ltdump.o ltexit.o lthelp.o ltmain.o \
-		ltmisc.o ltpassthru.o ltscript.o romload.o sercomm.o \
-		srecreader.o tpinterf.o tpinterf2.o tpinterf3.o ${EXTRA_OBJ}
+		ltmisc.o ltpassthru.o ltscript.o romload.o srecreader.o \
+		tpinterf.o tpinterf2.o tpinterf3.o ${EXTRA_OBJ}
 
 XRAM_OBJS=	chainload.o clmain.o defexitstub.o defpath.o flashstubs.o \
 		hexdecode.o hwparam.o initscript.o labaud.o romload.o \
-		secondprog.o sercomm.o srecreader.o tpinterf.o ttypassthru.o \
-		${EXTRA_OBJ}
+		secondprog.o srecreader.o tpinterf.o ttypassthru.o ${EXTRA_OBJ}
 
 BUZPLAY_OBJS=	bpdispatch.o bpmain.o buzplay.o defpath.o flashstubs.o \
 		hexdecode.o hwparam.o labaud.o ltexit.o ltpassthru.o romload.o \
-		sercomm.o srecreader.o tpinterf.o ${EXTRA_OBJ}
+		srecreader.o tpinterf.o ${EXTRA_OBJ}
+
+LIBSERIAL=	../libserial/libserial.a
 
 all:	${PROGS}
 
-fc-compalram:	${COMPALRAM_OBJS}
-	${CC} ${CFLAGS} -o $@ ${COMPALRAM_OBJS}
+fc-compalram:	${COMPALRAM_OBJS} ${LIBSERIAL}
+	${CC} ${CFLAGS} -o $@ ${COMPALRAM_OBJS} ${LIBSERIAL}
 
-fc-iram:	${IRAM_OBJS}
-	${CC} ${CFLAGS} -o $@ ${IRAM_OBJS}
+fc-iram:	${IRAM_OBJS} ${LIBSERIAL}
+	${CC} ${CFLAGS} -o $@ ${IRAM_OBJS} ${LIBSERIAL}
 
-fc-loadtool:	${LOADTOOL_OBJS}
-	${CC} ${CFLAGS} -o $@ ${LOADTOOL_OBJS}
+fc-loadtool:	${LOADTOOL_OBJS} ${LIBSERIAL}
+	${CC} ${CFLAGS} -o $@ ${LOADTOOL_OBJS} ${LIBSERIAL}
 
-fc-xram:	${XRAM_OBJS}
-	${CC} ${CFLAGS} -o $@ ${XRAM_OBJS}
+fc-xram:	${XRAM_OBJS} ${LIBSERIAL}
+	${CC} ${CFLAGS} -o $@ ${XRAM_OBJS} ${LIBSERIAL}
 
-fc-buzplay:	${BUZPLAY_OBJS}
-	${CC} ${CFLAGS} -o $@ ${BUZPLAY_OBJS}
+fc-buzplay:	${BUZPLAY_OBJS} ${LIBSERIAL}
+	${CC} ${CFLAGS} -o $@ ${BUZPLAY_OBJS} ${LIBSERIAL}
 
 install:
 	mkdir -p ${INSTBIN}
--- a/loadtools/baudrate.h	Thu Sep 21 21:49:07 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,7 +0,0 @@
-/* this header file defines the data structure for baud rate machinations */
-
-struct baudrate {
-	char	*name;
-	speed_t	termios_code;
-	int	bootrom_code;
-};
--- a/loadtools/bpmain.c	Thu Sep 21 21:49:07 2017 +0000
+++ b/loadtools/bpmain.c	Thu Sep 21 23:07:01 2017 +0000
@@ -9,7 +9,8 @@
 #include <unistd.h>
 #include "srecreader.h"
 
-extern char *target_ttydev;
+char *target_ttydev;
+
 extern struct srecreader iramimage;
 extern char default_buzplayer_image[];
 extern void (*default_exit)();
@@ -70,11 +71,11 @@
 	if (!iramimage.filename)
 		iramimage.filename = default_buzplayer_image;
 
-	open_target_serial();
+	open_serial_port(target_ttydev);
 	if (reattach)
-		switch_baud_rate(reattach);
+		set_serial_baudrate(reattach);
 	else {
-		perform_compal_stage(1);
+		perform_compal_stage();
 		perform_romload();
 		putchar('\n');
 		if (tpinterf_pass_output(1) < 0)
--- a/loadtools/clmain.c	Thu Sep 21 21:49:07 2017 +0000
+++ b/loadtools/clmain.c	Thu Sep 21 23:07:01 2017 +0000
@@ -7,12 +7,12 @@
 #include <stdint.h>
 #include <stdio.h>
 #include <stdlib.h>
-#include <termios.h>
 #include <unistd.h>
-#include "baudrate.h"
+#include "../libserial/baudrate.h"
 #include "srecreader.h"
 
-extern char *target_ttydev;
+char *target_ttydev;
+
 extern struct srecreader iramimage;
 extern char default_loadagent_image[];
 extern struct srecreader xramimage;
@@ -92,8 +92,8 @@
 		passon_argc = argc - optind - 2;
 	}
 
-	open_target_serial();
-	perform_compal_stage(1);
+	open_serial_port(target_ttydev);
+	perform_compal_stage();
 	perform_romload();
 	/* loadagent should be running now */
 	if (tpinterf_pass_output(1) < 0)
--- a/loadtools/compalload.c	Thu Sep 21 21:49:07 2017 +0000
+++ b/loadtools/compalload.c	Thu Sep 21 23:07:01 2017 +0000
@@ -13,13 +13,11 @@
 #include <stdlib.h>
 #include <string.h>
 #include <strings.h>
-#include <termios.h>
 #include <unistd.h>
 
 extern char default_compalstage_dir[];
 extern char *target_ttydev;
 extern int target_fd;
-extern struct termios target_termios;
 
 static char compalstage_pathname[MAXPATHLEN];
 static u_char *compalstage_imgbuf;
@@ -153,7 +151,7 @@
 	}
 }
 
-perform_compal_stage(for_boot_rom)
+perform_compal_stage()
 {
 	int fd;
 	struct stat st;
@@ -197,26 +195,12 @@
 	compute_checksum();
 
 	printf("Using Compal stage image %s\n", compalstage_pathname);
-	cfsetispeed(&target_termios, B115200);
-	cfsetospeed(&target_termios, B115200);
-	if (tcsetattr(target_fd, TCSAFLUSH, &target_termios) < 0) {
-		perror("tcsetattr to switch baud rate");
-		exit(1);
-	}
+	set_fixed_baudrate("115200");
 	ioctl(target_fd, FIONBIO, &zero);
 	printf("Waiting for PROMPT1 from target (%s) at 115200 baud\n",
 		target_ttydev);
 	read_loop();
 	free(compalstage_imgbuf);
 
-	if (for_boot_rom) {
-		cfsetispeed(&target_termios, B19200);
-		cfsetospeed(&target_termios, B19200);
-		if (tcsetattr(target_fd, TCSAFLUSH, &target_termios) < 0) {
-			perror("tcsetattr to switch baud rate");
-			exit(1);
-		}
-	}
-
 	return(1);
 }
--- a/loadtools/compalram.c	Thu Sep 21 21:49:07 2017 +0000
+++ b/loadtools/compalram.c	Thu Sep 21 23:07:01 2017 +0000
@@ -7,7 +7,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 
-extern char *target_ttydev;
+char *target_ttydev;
 
 main(argc, argv)
 	char **argv;
@@ -19,8 +19,8 @@
 	target_ttydev = argv[1];
 	set_compalstage_fullpath(argv[2]);
 
-	open_target_serial();
-	perform_compal_stage(0);
+	open_serial_port(target_ttydev);
+	perform_compal_stage();
 	tty_passthru();
 	exit(0);
 }
--- a/loadtools/labaud.c	Thu Sep 21 21:49:07 2017 +0000
+++ b/loadtools/labaud.c	Thu Sep 21 23:07:01 2017 +0000
@@ -5,9 +5,8 @@
 
 #include <stdio.h>
 #include <stdlib.h>
-#include <termios.h>
 #include <unistd.h>
-#include "baudrate.h"
+#include "../libserial/baudrate.h"
 
 extern int target_fd;
 extern struct baudrate *current_baud_rate;
@@ -26,7 +25,7 @@
 	tpinterf_make_cmd(argv);
 	if (tpinterf_send_cmd() < 0)
 		return(-1);
-	switch_baud_rate(newbr);
+	set_serial_baudrate(newbr);
 	usleep(150000);
 	write(target_fd, &U, 1);
 	return tpinterf_pass_output(1);
--- a/loadtools/ltmain.c	Thu Sep 21 21:49:07 2017 +0000
+++ b/loadtools/ltmain.c	Thu Sep 21 23:07:01 2017 +0000
@@ -9,7 +9,8 @@
 #include <unistd.h>
 #include "srecreader.h"
 
-extern char *target_ttydev;
+char *target_ttydev;
+
 extern struct srecreader iramimage;
 extern char default_loadagent_image[];
 extern char hw_init_script[];
@@ -76,11 +77,11 @@
 	if (!iramimage.filename)
 		iramimage.filename = default_loadagent_image;
 
-	open_target_serial();
+	open_serial_port(target_ttydev);
 	if (reattach)
-		switch_baud_rate(reattach);
+		set_serial_baudrate(reattach);
 	else {
-		perform_compal_stage(1);
+		perform_compal_stage();
 		perform_romload();
 		putchar('\n');
 		if (tpinterf_pass_output(1) < 0)
--- a/loadtools/romload.c	Thu Sep 21 21:49:07 2017 +0000
+++ b/loadtools/romload.c	Thu Sep 21 23:07:01 2017 +0000
@@ -13,7 +13,7 @@
 #include <strings.h>
 #include <termios.h>
 #include <unistd.h>
-#include "baudrate.h"
+#include "../libserial/baudrate.h"
 #include "srecreader.h"
 
 extern int errno;
@@ -179,6 +179,7 @@
 
 	if (open_srec_file(&iramimage) < 0)
 		exit(1);
+	set_fixed_baudrate("19200");
 	ioctl(target_fd, FIONBIO, &zero);
 	send_beacons();
 	printf("Got beacon response, attempting download\n");
@@ -203,7 +204,7 @@
 	}
 	printf("<p command successful, switching to %s baud\n",
 		romload_baud_rate->name);
-	switch_baud_rate(romload_baud_rate);
+	set_serial_baudrate(romload_baud_rate);
 	usleep(SERIAL_FLUSH_DELAY * 1000);
 	tcflush(target_fd, TCIFLUSH);
 
--- a/loadtools/sercomm.c	Thu Sep 21 21:49:07 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,88 +0,0 @@
-/*
- * This module handles the establishment of serial communication
- * with the target, i.e., the host-side termios stuff.
- */
-
-#include <sys/types.h>
-#include <sys/file.h>
-#include <sys/ioctl.h>
-#include <termios.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <strings.h>
-#include <unistd.h>
-#include "baudrate.h"
-
-char *target_ttydev;
-int target_fd;
-struct termios target_termios;
-
-struct baudrate baud_rate_table[] = {
-	/* the first listed rate will be our default */
-	{"115200",	B115200,	0},
-	{"57600",	B57600,		1},
-	{"38400",	B38400,		2},
-	{"19200",	B19200,		4},
-	/* non-standard high baud rates "remapped" by CP2102 usb2serial IC */
-	{"812500",	B921600,	-1},
-	{"406250",	B460800,	-1},
-	{"203125",	B230400,	-1},
-	/* table search terminator */
-	{NULL,		B0,		-1},
-};
-struct baudrate *current_baud_rate;
-
-open_target_serial()
-{
-	target_fd = open(target_ttydev, O_RDWR|O_NONBLOCK);
-	if (target_fd < 0) {
-		perror(target_ttydev);
-		exit(1);
-	}
-	target_termios.c_iflag = IGNBRK;
-	target_termios.c_oflag = 0;
-	target_termios.c_cflag = CLOCAL|HUPCL|CREAD|CS8;
-	target_termios.c_lflag = 0;
-	target_termios.c_cc[VMIN] = 1;
-	target_termios.c_cc[VTIME] = 0;
-	/* start at B19200, as that's what we'll need to use initially */
-	cfsetispeed(&target_termios, B19200);
-	cfsetospeed(&target_termios, B19200);
-	if (tcsetattr(target_fd, TCSAFLUSH, &target_termios) < 0) {
-		perror("initial tcsetattr on target");
-		exit(1);
-	}
-	ioctl(target_fd, TIOCEXCL);
-	return 0;
-}
-
-struct baudrate *
-find_baudrate_by_name(srch_name)
-	char *srch_name;
-{
-	struct baudrate *br;
-
-	for (br = baud_rate_table; br->name; br++)
-		if (!strcmp(br->name, srch_name))
-			break;
-	if (br->name)
-		return(br);
-	else {
-		fprintf(stderr, "error: baud rate \"%s\" not known\n",
-			srch_name);
-		return(NULL);
-	}
-}
-
-switch_baud_rate(br)
-	struct baudrate *br;
-{
-	cfsetispeed(&target_termios, br->termios_code);
-	cfsetospeed(&target_termios, br->termios_code);
-	if (tcsetattr(target_fd, TCSAFLUSH, &target_termios) < 0) {
-		perror("tcsetattr to switch baud rate");
-		exit(1);
-	}
-	current_baud_rate = br;
-}
--- a/loadtools/sertool.c	Thu Sep 21 21:49:07 2017 +0000
+++ b/loadtools/sertool.c	Thu Sep 21 23:07:01 2017 +0000
@@ -12,7 +12,8 @@
 #include <unistd.h>
 #include "srecreader.h"
 
-extern char *target_ttydev;
+char *target_ttydev;
+
 extern struct srecreader iramimage;
 extern int gta_modem_poweron;
 
@@ -64,8 +65,8 @@
 		passon_argc = argc - optind - 2;
 	}
 
-	open_target_serial();
-	perform_compal_stage(1);
+	open_serial_port(target_ttydev);
+	perform_compal_stage();
 	perform_romload();
 	if (passon_argv)
 		exec_2nd_prog();