blob: caf9389943a2abfae4a60465bdc97e44a4c03dc7 [file] [log] [blame]
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +00001/*
2 * This file is part of the flashrom project.
3 *
4 * Copyright (C) 2009 Urja Rannikko <urjaman@gmail.com>
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +00005 * Copyright (C) 2009,2010 Carl-Daniel Hailfinger
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +00006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22#include <stdio.h>
23#include <stdlib.h>
24#include <unistd.h>
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000025#include <string.h>
26#include <ctype.h>
27#include <fcntl.h>
28#include <sys/types.h>
29#include <sys/stat.h>
30#include <errno.h>
31#include <inttypes.h>
Patrick Georgie48654c2010-01-06 22:14:39 +000032#ifdef _WIN32
33#include <conio.h>
34#else
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000035#include <termios.h>
Patrick Georgie48654c2010-01-06 22:14:39 +000036#endif
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000037#include "flash.h"
38#include "programmer.h"
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000039
Patrick Georgie48654c2010-01-06 22:14:39 +000040fdtype sp_fd;
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000041
42void __attribute__((noreturn)) sp_die(char *msg)
43{
44 perror(msg);
45 exit(1);
46}
47
Patrick Georgie48654c2010-01-06 22:14:39 +000048#ifndef _WIN32
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000049struct baudentry {
50 int flag;
51 unsigned int baud;
52};
53
54/* I'd like if the C preprocessor could have directives in macros */
55#define BAUDENTRY(baud) { B##baud, baud },
56static const struct baudentry sp_baudtable[] = {
57 BAUDENTRY(9600)
58 BAUDENTRY(19200)
59 BAUDENTRY(38400)
60 BAUDENTRY(57600)
61 BAUDENTRY(115200)
62#ifdef B230400
63 BAUDENTRY(230400)
64#endif
65#ifdef B460800
66 BAUDENTRY(460800)
67#endif
68#ifdef B500000
69 BAUDENTRY(500000)
70#endif
71#ifdef B576000
72 BAUDENTRY(576000)
73#endif
74#ifdef B921600
75 BAUDENTRY(921600)
76#endif
77#ifdef B1000000
78 BAUDENTRY(1000000)
79#endif
80#ifdef B1152000
81 BAUDENTRY(1152000)
82#endif
83#ifdef B1500000
84 BAUDENTRY(1500000)
85#endif
86#ifdef B2000000
87 BAUDENTRY(2000000)
88#endif
89#ifdef B2500000
90 BAUDENTRY(2500000)
91#endif
92#ifdef B3000000
93 BAUDENTRY(3000000)
94#endif
95#ifdef B3500000
96 BAUDENTRY(3500000)
97#endif
98#ifdef B4000000
99 BAUDENTRY(4000000)
100#endif
101 {0, 0} /* Terminator */
102};
Patrick Georgie48654c2010-01-06 22:14:39 +0000103#endif
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000104
Patrick Georgie48654c2010-01-06 22:14:39 +0000105fdtype sp_openserport(char *dev, unsigned int baud)
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000106{
Patrick Georgie48654c2010-01-06 22:14:39 +0000107#ifdef _WIN32
108 HANDLE fd;
Uwe Hermann43959702010-03-13 17:28:29 +0000109 char *dev2 = dev;
110 if ((strlen(dev) > 3) && (tolower(dev[0]) == 'c')
111 && (tolower(dev[1]) == 'o') && (tolower(dev[2]) == 'm')) {
112 dev2 = malloc(strlen(dev) + 5);
Patrick Georgi06602c22010-01-26 20:58:40 +0000113 strcpy(dev2, "\\\\.\\");
Uwe Hermann43959702010-03-13 17:28:29 +0000114 strcpy(dev2 + 4, dev);
Patrick Georgi06602c22010-01-26 20:58:40 +0000115 }
Uwe Hermann43959702010-03-13 17:28:29 +0000116 fd = CreateFile(dev2, GENERIC_READ | GENERIC_WRITE, 0, NULL,
117 OPEN_EXISTING, 0, NULL);
Patrick Georgi06602c22010-01-26 20:58:40 +0000118 if (dev2 != dev)
119 free(dev2);
Patrick Georgie48654c2010-01-06 22:14:39 +0000120 if (fd == INVALID_HANDLE_VALUE) {
121 sp_die("Error: cannot open serial port");
122 }
123 DCB dcb;
124 if (!GetCommState(fd, &dcb)) {
125 sp_die("Error: Could not fetch serial port configuration");
126 }
127 switch (baud) {
128 case 9600: dcb.BaudRate = CBR_9600; break;
129 case 19200: dcb.BaudRate = CBR_19200; break;
130 case 38400: dcb.BaudRate = CBR_38400; break;
131 case 57600: dcb.BaudRate = CBR_57600; break;
132 case 115200: dcb.BaudRate = CBR_115200; break;
133 default: sp_die("Error: Could not set baud rate");
134 }
Uwe Hermann43959702010-03-13 17:28:29 +0000135 dcb.ByteSize = 8;
136 dcb.Parity = NOPARITY;
137 dcb.StopBits = ONESTOPBIT;
Patrick Georgie48654c2010-01-06 22:14:39 +0000138 if (!SetCommState(fd, &dcb)) {
139 sp_die("Error: Could not change serial port configuration");
140 }
141 return fd;
142#else
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000143 struct termios options;
144 int fd, i;
145 fd = open(dev, O_RDWR | O_NOCTTY | O_NDELAY);
146 if (fd < 0)
147 sp_die("Error: cannot open serial port");
148 fcntl(fd, F_SETFL, 0);
149 tcgetattr(fd, &options);
150 for (i = 0;; i++) {
151 if (sp_baudtable[i].baud == 0) {
152 close(fd);
Uwe Hermann43959702010-03-13 17:28:29 +0000153 msg_perr("Error: cannot configure for baudrate %d\n",
154 baud);
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000155 exit(1);
156 }
157 if (sp_baudtable[i].baud == baud) {
158 cfsetispeed(&options, sp_baudtable[i].flag);
159 cfsetospeed(&options, sp_baudtable[i].flag);
160 break;
161 }
162 }
163 options.c_cflag &= ~(PARENB | CSTOPB | CSIZE | CRTSCTS);
164 options.c_cflag |= (CS8 | CLOCAL | CREAD);
165 options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
166 options.c_iflag &= ~(IXON | IXOFF | IXANY | ICRNL | IGNCR | INLCR);
167 options.c_oflag &= ~OPOST;
168 tcsetattr(fd, TCSANOW, &options);
169 return fd;
Patrick Georgie48654c2010-01-06 22:14:39 +0000170#endif
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000171}
172
173void sp_flush_incoming(void)
174{
Patrick Georgie48654c2010-01-06 22:14:39 +0000175#ifdef _WIN32
176 PurgeComm(sp_fd, PURGE_RXCLEAR);
177#else
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000178 tcflush(sp_fd, TCIFLUSH);
Patrick Georgie48654c2010-01-06 22:14:39 +0000179#endif
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000180 return;
181}
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000182
183int serialport_shutdown(void)
184{
Patrick Georgie48654c2010-01-06 22:14:39 +0000185#ifdef _WIN32
186 CloseHandle(sp_fd);
187#else
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000188 close(sp_fd);
Patrick Georgie48654c2010-01-06 22:14:39 +0000189#endif
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000190 return 0;
191}
192
193int serialport_write(unsigned char *buf, unsigned int writecnt)
194{
Patrick Georgie48654c2010-01-06 22:14:39 +0000195 long tmp = 0;
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000196
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000197 while (writecnt > 0) {
Patrick Georgie48654c2010-01-06 22:14:39 +0000198#ifdef _WIN32
199 WriteFile(sp_fd, buf, writecnt, &tmp, NULL);
200#else
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000201 tmp = write(sp_fd, buf, writecnt);
Patrick Georgie48654c2010-01-06 22:14:39 +0000202#endif
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000203 if (tmp == -1)
204 return 1;
205 if (!tmp)
Sean Nelsonebb4f5f2010-01-09 23:46:39 +0000206 msg_pdbg("Empty write\n");
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000207 writecnt -= tmp;
208 buf += tmp;
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000209 }
210
211 return 0;
212}
213
214int serialport_read(unsigned char *buf, unsigned int readcnt)
215{
Patrick Georgie48654c2010-01-06 22:14:39 +0000216 long tmp = 0;
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000217
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000218 while (readcnt > 0) {
Patrick Georgie48654c2010-01-06 22:14:39 +0000219#ifdef _WIN32
220 ReadFile(sp_fd, buf, readcnt, &tmp, NULL);
221#else
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000222 tmp = read(sp_fd, buf, readcnt);
Patrick Georgie48654c2010-01-06 22:14:39 +0000223#endif
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000224 if (tmp == -1)
225 return 1;
226 if (!tmp)
Sean Nelsonebb4f5f2010-01-09 23:46:39 +0000227 msg_pdbg("Empty read\n");
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000228 readcnt -= tmp;
229 buf += tmp;
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000230 }
231
232 return 0;
233}