blob: b5049744f598d99befcf02443a2c15e9c1a03e2f [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>
Patrick Georgie48654c2010-01-06 22:14:39 +000035#endif
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000036#include "flash.h"
37#include "programmer.h"
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000038
Patrick Georgie48654c2010-01-06 22:14:39 +000039fdtype sp_fd;
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000040
41void __attribute__((noreturn)) sp_die(char *msg)
42{
43 perror(msg);
44 exit(1);
45}
46
Patrick Georgie48654c2010-01-06 22:14:39 +000047#ifndef _WIN32
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000048struct baudentry {
49 int flag;
50 unsigned int baud;
51};
52
53/* I'd like if the C preprocessor could have directives in macros */
54#define BAUDENTRY(baud) { B##baud, baud },
55static const struct baudentry sp_baudtable[] = {
56 BAUDENTRY(9600)
57 BAUDENTRY(19200)
58 BAUDENTRY(38400)
59 BAUDENTRY(57600)
60 BAUDENTRY(115200)
61#ifdef B230400
62 BAUDENTRY(230400)
63#endif
64#ifdef B460800
65 BAUDENTRY(460800)
66#endif
67#ifdef B500000
68 BAUDENTRY(500000)
69#endif
70#ifdef B576000
71 BAUDENTRY(576000)
72#endif
73#ifdef B921600
74 BAUDENTRY(921600)
75#endif
76#ifdef B1000000
77 BAUDENTRY(1000000)
78#endif
79#ifdef B1152000
80 BAUDENTRY(1152000)
81#endif
82#ifdef B1500000
83 BAUDENTRY(1500000)
84#endif
85#ifdef B2000000
86 BAUDENTRY(2000000)
87#endif
88#ifdef B2500000
89 BAUDENTRY(2500000)
90#endif
91#ifdef B3000000
92 BAUDENTRY(3000000)
93#endif
94#ifdef B3500000
95 BAUDENTRY(3500000)
96#endif
97#ifdef B4000000
98 BAUDENTRY(4000000)
99#endif
100 {0, 0} /* Terminator */
101};
Patrick Georgie48654c2010-01-06 22:14:39 +0000102#endif
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000103
Patrick Georgie48654c2010-01-06 22:14:39 +0000104fdtype sp_openserport(char *dev, unsigned int baud)
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000105{
Patrick Georgie48654c2010-01-06 22:14:39 +0000106#ifdef _WIN32
107 HANDLE fd;
Uwe Hermann43959702010-03-13 17:28:29 +0000108 char *dev2 = dev;
Carl-Daniel Hailfinger9e3a6c42010-10-08 12:40:09 +0000109 if ((strlen(dev) > 3) && (tolower((unsigned char)dev[0]) == 'c') &&
110 (tolower((unsigned char)dev[1]) == 'o') &&
111 (tolower((unsigned char)dev[2]) == 'm')) {
Uwe Hermann43959702010-03-13 17:28:29 +0000112 dev2 = malloc(strlen(dev) + 5);
Stefan Tauner269de352011-07-12 22:35:21 +0000113 if (!dev2)
114 sp_die("Error: Out of memory");
Patrick Georgi06602c22010-01-26 20:58:40 +0000115 strcpy(dev2, "\\\\.\\");
Uwe Hermann43959702010-03-13 17:28:29 +0000116 strcpy(dev2 + 4, dev);
Patrick Georgi06602c22010-01-26 20:58:40 +0000117 }
Uwe Hermann43959702010-03-13 17:28:29 +0000118 fd = CreateFile(dev2, GENERIC_READ | GENERIC_WRITE, 0, NULL,
119 OPEN_EXISTING, 0, NULL);
Patrick Georgi06602c22010-01-26 20:58:40 +0000120 if (dev2 != dev)
121 free(dev2);
Patrick Georgie48654c2010-01-06 22:14:39 +0000122 if (fd == INVALID_HANDLE_VALUE) {
123 sp_die("Error: cannot open serial port");
124 }
125 DCB dcb;
126 if (!GetCommState(fd, &dcb)) {
127 sp_die("Error: Could not fetch serial port configuration");
128 }
129 switch (baud) {
130 case 9600: dcb.BaudRate = CBR_9600; break;
131 case 19200: dcb.BaudRate = CBR_19200; break;
132 case 38400: dcb.BaudRate = CBR_38400; break;
133 case 57600: dcb.BaudRate = CBR_57600; break;
134 case 115200: dcb.BaudRate = CBR_115200; break;
135 default: sp_die("Error: Could not set baud rate");
136 }
Uwe Hermann43959702010-03-13 17:28:29 +0000137 dcb.ByteSize = 8;
138 dcb.Parity = NOPARITY;
139 dcb.StopBits = ONESTOPBIT;
Patrick Georgie48654c2010-01-06 22:14:39 +0000140 if (!SetCommState(fd, &dcb)) {
141 sp_die("Error: Could not change serial port configuration");
142 }
143 return fd;
144#else
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000145 struct termios options;
146 int fd, i;
147 fd = open(dev, O_RDWR | O_NOCTTY | O_NDELAY);
148 if (fd < 0)
149 sp_die("Error: cannot open serial port");
150 fcntl(fd, F_SETFL, 0);
151 tcgetattr(fd, &options);
152 for (i = 0;; i++) {
153 if (sp_baudtable[i].baud == 0) {
154 close(fd);
Uwe Hermann43959702010-03-13 17:28:29 +0000155 msg_perr("Error: cannot configure for baudrate %d\n",
156 baud);
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000157 exit(1);
158 }
159 if (sp_baudtable[i].baud == baud) {
160 cfsetispeed(&options, sp_baudtable[i].flag);
161 cfsetospeed(&options, sp_baudtable[i].flag);
162 break;
163 }
164 }
165 options.c_cflag &= ~(PARENB | CSTOPB | CSIZE | CRTSCTS);
166 options.c_cflag |= (CS8 | CLOCAL | CREAD);
167 options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
168 options.c_iflag &= ~(IXON | IXOFF | IXANY | ICRNL | IGNCR | INLCR);
169 options.c_oflag &= ~OPOST;
170 tcsetattr(fd, TCSANOW, &options);
171 return fd;
Patrick Georgie48654c2010-01-06 22:14:39 +0000172#endif
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000173}
174
175void sp_flush_incoming(void)
176{
Patrick Georgie48654c2010-01-06 22:14:39 +0000177#ifdef _WIN32
178 PurgeComm(sp_fd, PURGE_RXCLEAR);
179#else
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000180 tcflush(sp_fd, TCIFLUSH);
Patrick Georgie48654c2010-01-06 22:14:39 +0000181#endif
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000182 return;
183}
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000184
David Hendricks8bb20212011-06-14 01:35:36 +0000185int serialport_shutdown(void *data)
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000186{
Patrick Georgie48654c2010-01-06 22:14:39 +0000187#ifdef _WIN32
188 CloseHandle(sp_fd);
189#else
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000190 close(sp_fd);
Patrick Georgie48654c2010-01-06 22:14:39 +0000191#endif
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000192 return 0;
193}
194
195int serialport_write(unsigned char *buf, unsigned int writecnt)
196{
Uwe Hermannd5e85d62011-07-03 19:44:12 +0000197#ifdef _WIN32
198 DWORD tmp = 0;
199#else
200 ssize_t tmp = 0;
201#endif
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000202
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000203 while (writecnt > 0) {
Patrick Georgie48654c2010-01-06 22:14:39 +0000204#ifdef _WIN32
205 WriteFile(sp_fd, buf, writecnt, &tmp, NULL);
206#else
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000207 tmp = write(sp_fd, buf, writecnt);
Patrick Georgie48654c2010-01-06 22:14:39 +0000208#endif
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000209 if (tmp == -1) {
210 msg_perr("Serial port write error!\n");
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000211 return 1;
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000212 }
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000213 if (!tmp)
Sean Nelsonebb4f5f2010-01-09 23:46:39 +0000214 msg_pdbg("Empty write\n");
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000215 writecnt -= tmp;
216 buf += tmp;
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000217 }
218
219 return 0;
220}
221
222int serialport_read(unsigned char *buf, unsigned int readcnt)
223{
Uwe Hermannd5e85d62011-07-03 19:44:12 +0000224#ifdef _WIN32
225 DWORD tmp = 0;
226#else
227 ssize_t tmp = 0;
228#endif
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000229
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000230 while (readcnt > 0) {
Patrick Georgie48654c2010-01-06 22:14:39 +0000231#ifdef _WIN32
232 ReadFile(sp_fd, buf, readcnt, &tmp, NULL);
233#else
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000234 tmp = read(sp_fd, buf, readcnt);
Patrick Georgie48654c2010-01-06 22:14:39 +0000235#endif
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000236 if (tmp == -1) {
237 msg_perr("Serial port read error!\n");
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000238 return 1;
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000239 }
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000240 if (!tmp)
Sean Nelsonebb4f5f2010-01-09 23:46:39 +0000241 msg_pdbg("Empty read\n");
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000242 readcnt -= tmp;
243 buf += tmp;
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000244 }
245
246 return 0;
247}