blob: 7e47dcc9799e8d66e55ba8a78a39b9aa838aef62 [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>
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000028#include <sys/stat.h>
29#include <errno.h>
30#include <inttypes.h>
Patrick Georgie48654c2010-01-06 22:14:39 +000031#ifdef _WIN32
32#include <conio.h>
33#else
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000034#include <termios.h>
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +000035#include <unistd.h>
36#include <sys/types.h>
37#include <sys/ioctl.h>
Patrick Georgie48654c2010-01-06 22:14:39 +000038#endif
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000039#include "flash.h"
40#include "programmer.h"
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000041
Patrick Georgie48654c2010-01-06 22:14:39 +000042fdtype sp_fd;
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000043
44void __attribute__((noreturn)) sp_die(char *msg)
45{
46 perror(msg);
47 exit(1);
48}
49
Patrick Georgie48654c2010-01-06 22:14:39 +000050#ifndef _WIN32
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000051struct baudentry {
52 int flag;
53 unsigned int baud;
54};
55
56/* I'd like if the C preprocessor could have directives in macros */
57#define BAUDENTRY(baud) { B##baud, baud },
58static const struct baudentry sp_baudtable[] = {
59 BAUDENTRY(9600)
60 BAUDENTRY(19200)
61 BAUDENTRY(38400)
62 BAUDENTRY(57600)
63 BAUDENTRY(115200)
64#ifdef B230400
65 BAUDENTRY(230400)
66#endif
67#ifdef B460800
68 BAUDENTRY(460800)
69#endif
70#ifdef B500000
71 BAUDENTRY(500000)
72#endif
73#ifdef B576000
74 BAUDENTRY(576000)
75#endif
76#ifdef B921600
77 BAUDENTRY(921600)
78#endif
79#ifdef B1000000
80 BAUDENTRY(1000000)
81#endif
82#ifdef B1152000
83 BAUDENTRY(1152000)
84#endif
85#ifdef B1500000
86 BAUDENTRY(1500000)
87#endif
88#ifdef B2000000
89 BAUDENTRY(2000000)
90#endif
91#ifdef B2500000
92 BAUDENTRY(2500000)
93#endif
94#ifdef B3000000
95 BAUDENTRY(3000000)
96#endif
97#ifdef B3500000
98 BAUDENTRY(3500000)
99#endif
100#ifdef B4000000
101 BAUDENTRY(4000000)
102#endif
103 {0, 0} /* Terminator */
104};
Patrick Georgie48654c2010-01-06 22:14:39 +0000105#endif
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000106
Patrick Georgie48654c2010-01-06 22:14:39 +0000107fdtype sp_openserport(char *dev, unsigned int baud)
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000108{
Patrick Georgie48654c2010-01-06 22:14:39 +0000109#ifdef _WIN32
110 HANDLE fd;
Uwe Hermann43959702010-03-13 17:28:29 +0000111 char *dev2 = dev;
Carl-Daniel Hailfinger9e3a6c42010-10-08 12:40:09 +0000112 if ((strlen(dev) > 3) && (tolower((unsigned char)dev[0]) == 'c') &&
113 (tolower((unsigned char)dev[1]) == 'o') &&
114 (tolower((unsigned char)dev[2]) == 'm')) {
Uwe Hermann43959702010-03-13 17:28:29 +0000115 dev2 = malloc(strlen(dev) + 5);
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000116 if (!dev2) {
117 msg_perr("Error: Out of memory: %s\n", strerror(errno));
Stefan Taunerbb4fed72012-09-01 21:47:19 +0000118 return SER_INV_FD;
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000119 }
Patrick Georgi06602c22010-01-26 20:58:40 +0000120 strcpy(dev2, "\\\\.\\");
Uwe Hermann43959702010-03-13 17:28:29 +0000121 strcpy(dev2 + 4, dev);
Patrick Georgi06602c22010-01-26 20:58:40 +0000122 }
Uwe Hermann43959702010-03-13 17:28:29 +0000123 fd = CreateFile(dev2, GENERIC_READ | GENERIC_WRITE, 0, NULL,
124 OPEN_EXISTING, 0, NULL);
Patrick Georgi06602c22010-01-26 20:58:40 +0000125 if (dev2 != dev)
126 free(dev2);
Patrick Georgie48654c2010-01-06 22:14:39 +0000127 if (fd == INVALID_HANDLE_VALUE) {
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000128 msg_perr("Error: cannot open serial port: %s\n", strerror(errno));
Stefan Taunerbb4fed72012-09-01 21:47:19 +0000129 return SER_INV_FD;
Patrick Georgie48654c2010-01-06 22:14:39 +0000130 }
131 DCB dcb;
132 if (!GetCommState(fd, &dcb)) {
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000133 msg_perr("Error: Could not fetch serial port configuration: %s\n", strerror(errno));
Stefan Taunerbb4fed72012-09-01 21:47:19 +0000134 return SER_INV_FD;
Patrick Georgie48654c2010-01-06 22:14:39 +0000135 }
136 switch (baud) {
137 case 9600: dcb.BaudRate = CBR_9600; break;
138 case 19200: dcb.BaudRate = CBR_19200; break;
139 case 38400: dcb.BaudRate = CBR_38400; break;
140 case 57600: dcb.BaudRate = CBR_57600; break;
141 case 115200: dcb.BaudRate = CBR_115200; break;
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000142 default: msg_perr("Error: Could not set baud rate: %s\n", strerror(errno));
Stefan Taunerbb4fed72012-09-01 21:47:19 +0000143 return SER_INV_FD;
Patrick Georgie48654c2010-01-06 22:14:39 +0000144 }
Uwe Hermann43959702010-03-13 17:28:29 +0000145 dcb.ByteSize = 8;
146 dcb.Parity = NOPARITY;
147 dcb.StopBits = ONESTOPBIT;
Patrick Georgie48654c2010-01-06 22:14:39 +0000148 if (!SetCommState(fd, &dcb)) {
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000149 msg_perr("Error: Could not change serial port configuration: %s\n", strerror(errno));
Stefan Taunerbb4fed72012-09-01 21:47:19 +0000150 return SER_INV_FD;
Patrick Georgie48654c2010-01-06 22:14:39 +0000151 }
152 return fd;
153#else
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000154 struct termios options;
155 int fd, i;
156 fd = open(dev, O_RDWR | O_NOCTTY | O_NDELAY);
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000157 if (fd < 0) {
158 msg_perr("Error: cannot open serial port: %s\n", strerror(errno));
Stefan Taunerbb4fed72012-09-01 21:47:19 +0000159 return SER_INV_FD;
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000160 }
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000161 fcntl(fd, F_SETFL, 0);
162 tcgetattr(fd, &options);
163 for (i = 0;; i++) {
164 if (sp_baudtable[i].baud == 0) {
165 close(fd);
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000166 msg_perr("Error: cannot configure for baudrate %d\n", baud);
Stefan Taunerbb4fed72012-09-01 21:47:19 +0000167 return SER_INV_FD;
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000168 }
169 if (sp_baudtable[i].baud == baud) {
170 cfsetispeed(&options, sp_baudtable[i].flag);
171 cfsetospeed(&options, sp_baudtable[i].flag);
172 break;
173 }
174 }
175 options.c_cflag &= ~(PARENB | CSTOPB | CSIZE | CRTSCTS);
176 options.c_cflag |= (CS8 | CLOCAL | CREAD);
177 options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
178 options.c_iflag &= ~(IXON | IXOFF | IXANY | ICRNL | IGNCR | INLCR);
179 options.c_oflag &= ~OPOST;
180 tcsetattr(fd, TCSANOW, &options);
181 return fd;
Patrick Georgie48654c2010-01-06 22:14:39 +0000182#endif
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000183}
184
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +0000185void sp_set_pin(enum SP_PIN pin, int val) {
186#ifdef _WIN32
187 DWORD ctl;
188
189 if(pin == PIN_TXD) {
190 ctl = val ? SETBREAK: CLRBREAK;
191 }
192 else if(pin == PIN_DTR) {
193 ctl = val ? SETDTR: CLRDTR;
194 }
195 else {
196 ctl = val ? SETRTS: CLRRTS;
197 }
198 EscapeCommFunction(sp_fd, ctl);
199#else
200 int ctl, s;
201
202 if(pin == PIN_TXD) {
203 ioctl(sp_fd, val ? TIOCSBRK : TIOCCBRK, 0);
204 }
205 else {
206 s = (pin == PIN_DTR) ? TIOCM_DTR : TIOCM_RTS;
207 ioctl(sp_fd, TIOCMGET, &ctl);
208
209 if (val) {
210 ctl |= s;
211 }
212 else {
213 ctl &= ~s;
214 }
215 ioctl(sp_fd, TIOCMSET, &ctl);
216 }
217#endif
218}
219
220int sp_get_pin(enum SP_PIN pin) {
221 int s;
222#ifdef _WIN32
223 DWORD ctl;
224
225 s = (pin == PIN_CTS) ? MS_CTS_ON : MS_DSR_ON;
226 GetCommModemStatus(sp_fd, &ctl);
227#else
228 int ctl;
229 s = (pin == PIN_CTS) ? TIOCM_CTS : TIOCM_DSR;
230 ioctl(sp_fd, TIOCMGET, &ctl);
231#endif
232
233 return ((ctl & s) ? 1 : 0);
234
235}
236
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000237void sp_flush_incoming(void)
238{
Patrick Georgie48654c2010-01-06 22:14:39 +0000239#ifdef _WIN32
240 PurgeComm(sp_fd, PURGE_RXCLEAR);
241#else
Niklas Söderlund7145a502012-09-07 07:07:07 +0000242 /* FIXME: error handling */
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000243 tcflush(sp_fd, TCIFLUSH);
Patrick Georgie48654c2010-01-06 22:14:39 +0000244#endif
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000245 return;
246}
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000247
David Hendricks8bb20212011-06-14 01:35:36 +0000248int serialport_shutdown(void *data)
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000249{
Patrick Georgie48654c2010-01-06 22:14:39 +0000250#ifdef _WIN32
251 CloseHandle(sp_fd);
252#else
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000253 close(sp_fd);
Patrick Georgie48654c2010-01-06 22:14:39 +0000254#endif
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000255 return 0;
256}
257
258int serialport_write(unsigned char *buf, unsigned int writecnt)
259{
Uwe Hermannd5e85d62011-07-03 19:44:12 +0000260#ifdef _WIN32
261 DWORD tmp = 0;
262#else
263 ssize_t tmp = 0;
264#endif
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000265
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000266 while (writecnt > 0) {
Patrick Georgie48654c2010-01-06 22:14:39 +0000267#ifdef _WIN32
268 WriteFile(sp_fd, buf, writecnt, &tmp, NULL);
269#else
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000270 tmp = write(sp_fd, buf, writecnt);
Patrick Georgie48654c2010-01-06 22:14:39 +0000271#endif
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000272 if (tmp == -1) {
273 msg_perr("Serial port write error!\n");
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000274 return 1;
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000275 }
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000276 if (!tmp)
Sean Nelsonebb4f5f2010-01-09 23:46:39 +0000277 msg_pdbg("Empty write\n");
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000278 writecnt -= tmp;
279 buf += tmp;
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000280 }
281
282 return 0;
283}
284
285int serialport_read(unsigned char *buf, unsigned int readcnt)
286{
Uwe Hermannd5e85d62011-07-03 19:44:12 +0000287#ifdef _WIN32
288 DWORD tmp = 0;
289#else
290 ssize_t tmp = 0;
291#endif
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000292
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000293 while (readcnt > 0) {
Patrick Georgie48654c2010-01-06 22:14:39 +0000294#ifdef _WIN32
295 ReadFile(sp_fd, buf, readcnt, &tmp, NULL);
296#else
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000297 tmp = read(sp_fd, buf, readcnt);
Patrick Georgie48654c2010-01-06 22:14:39 +0000298#endif
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000299 if (tmp == -1) {
300 msg_perr("Serial port read error!\n");
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000301 return 1;
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000302 }
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000303 if (!tmp)
Sean Nelsonebb4f5f2010-01-09 23:46:39 +0000304 msg_pdbg("Empty read\n");
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000305 readcnt -= tmp;
306 buf += tmp;
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000307 }
308
309 return 0;
310}