blob: 4d49e08eb60725961e4fd5721fd95b2c743f428e [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>
25#include "flash.h"
26#include <string.h>
27#include <ctype.h>
28#include <fcntl.h>
29#include <sys/types.h>
30#include <sys/stat.h>
31#include <errno.h>
32#include <inttypes.h>
Patrick Georgie48654c2010-01-06 22:14:39 +000033#ifdef _WIN32
34#include <conio.h>
35#else
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000036#include <termios.h>
Patrick Georgie48654c2010-01-06 22:14:39 +000037#endif
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;
Patrick Georgi06602c22010-01-26 20:58:40 +0000108 char* dev2 = dev;
109 if ((strlen(dev) > 3) && (tolower(dev[0])=='c') && (tolower(dev[1])=='o') && (tolower(dev[2])=='m')) {
110 dev2 = malloc(strlen(dev)+5);
111 strcpy(dev2, "\\\\.\\");
112 strcpy(dev2+4, dev);
113 }
114 fd = CreateFile(dev2, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
115 if (dev2 != dev)
116 free(dev2);
Patrick Georgie48654c2010-01-06 22:14:39 +0000117 if (fd == INVALID_HANDLE_VALUE) {
118 sp_die("Error: cannot open serial port");
119 }
120 DCB dcb;
121 if (!GetCommState(fd, &dcb)) {
122 sp_die("Error: Could not fetch serial port configuration");
123 }
124 switch (baud) {
125 case 9600: dcb.BaudRate = CBR_9600; break;
126 case 19200: dcb.BaudRate = CBR_19200; break;
127 case 38400: dcb.BaudRate = CBR_38400; break;
128 case 57600: dcb.BaudRate = CBR_57600; break;
129 case 115200: dcb.BaudRate = CBR_115200; break;
130 default: sp_die("Error: Could not set baud rate");
131 }
132 dcb.ByteSize=8;
133 dcb.Parity=NOPARITY;
134 dcb.StopBits=ONESTOPBIT;
135 if (!SetCommState(fd, &dcb)) {
136 sp_die("Error: Could not change serial port configuration");
137 }
138 return fd;
139#else
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000140 struct termios options;
141 int fd, i;
142 fd = open(dev, O_RDWR | O_NOCTTY | O_NDELAY);
143 if (fd < 0)
144 sp_die("Error: cannot open serial port");
145 fcntl(fd, F_SETFL, 0);
146 tcgetattr(fd, &options);
147 for (i = 0;; i++) {
148 if (sp_baudtable[i].baud == 0) {
149 close(fd);
Sean Nelsonebb4f5f2010-01-09 23:46:39 +0000150 msg_perr(
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000151 "Error: cannot configure for baudrate %d\n",
152 baud);
153 exit(1);
154 }
155 if (sp_baudtable[i].baud == baud) {
156 cfsetispeed(&options, sp_baudtable[i].flag);
157 cfsetospeed(&options, sp_baudtable[i].flag);
158 break;
159 }
160 }
161 options.c_cflag &= ~(PARENB | CSTOPB | CSIZE | CRTSCTS);
162 options.c_cflag |= (CS8 | CLOCAL | CREAD);
163 options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
164 options.c_iflag &= ~(IXON | IXOFF | IXANY | ICRNL | IGNCR | INLCR);
165 options.c_oflag &= ~OPOST;
166 tcsetattr(fd, TCSANOW, &options);
167 return fd;
Patrick Georgie48654c2010-01-06 22:14:39 +0000168#endif
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000169}
170
171void sp_flush_incoming(void)
172{
Patrick Georgie48654c2010-01-06 22:14:39 +0000173#ifdef _WIN32
174 PurgeComm(sp_fd, PURGE_RXCLEAR);
175#else
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000176 tcflush(sp_fd, TCIFLUSH);
Patrick Georgie48654c2010-01-06 22:14:39 +0000177#endif
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000178 return;
179}
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000180
181int serialport_shutdown(void)
182{
Patrick Georgie48654c2010-01-06 22:14:39 +0000183#ifdef _WIN32
184 CloseHandle(sp_fd);
185#else
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000186 close(sp_fd);
Patrick Georgie48654c2010-01-06 22:14:39 +0000187#endif
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000188 return 0;
189}
190
191int serialport_write(unsigned char *buf, unsigned int writecnt)
192{
Patrick Georgie48654c2010-01-06 22:14:39 +0000193 long tmp = 0;
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000194
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000195 while (writecnt > 0) {
Patrick Georgie48654c2010-01-06 22:14:39 +0000196#ifdef _WIN32
197 WriteFile(sp_fd, buf, writecnt, &tmp, NULL);
198#else
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000199 tmp = write(sp_fd, buf, writecnt);
Patrick Georgie48654c2010-01-06 22:14:39 +0000200#endif
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000201 if (tmp == -1)
202 return 1;
203 if (!tmp)
Sean Nelsonebb4f5f2010-01-09 23:46:39 +0000204 msg_pdbg("Empty write\n");
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000205 writecnt -= tmp;
206 buf += tmp;
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000207 }
208
209 return 0;
210}
211
212int serialport_read(unsigned char *buf, unsigned int readcnt)
213{
Patrick Georgie48654c2010-01-06 22:14:39 +0000214 long tmp = 0;
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000215
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000216 while (readcnt > 0) {
Patrick Georgie48654c2010-01-06 22:14:39 +0000217#ifdef _WIN32
218 ReadFile(sp_fd, buf, readcnt, &tmp, NULL);
219#else
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000220 tmp = read(sp_fd, buf, readcnt);
Patrick Georgie48654c2010-01-06 22:14:39 +0000221#endif
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000222 if (tmp == -1)
223 return 1;
224 if (!tmp)
Sean Nelsonebb4f5f2010-01-09 23:46:39 +0000225 msg_pdbg("Empty read\n");
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000226 readcnt -= tmp;
227 buf += tmp;
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000228 }
229
230 return 0;
231}