Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the flashrom project. |
| 3 | * |
| 4 | * Copyright (C) 2009 Urja Rannikko <urjaman@gmail.com> |
Carl-Daniel Hailfinger | efa151e | 2010-01-06 16:09:10 +0000 | [diff] [blame] | 5 | * Copyright (C) 2009,2010 Carl-Daniel Hailfinger |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 6 | * |
| 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 Georgi | e48654c | 2010-01-06 22:14:39 +0000 | [diff] [blame] | 33 | #ifdef _WIN32 |
| 34 | #include <conio.h> |
| 35 | #else |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 36 | #include <termios.h> |
Patrick Georgi | e48654c | 2010-01-06 22:14:39 +0000 | [diff] [blame] | 37 | #endif |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 38 | |
Patrick Georgi | e48654c | 2010-01-06 22:14:39 +0000 | [diff] [blame] | 39 | fdtype sp_fd; |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 40 | |
| 41 | void __attribute__((noreturn)) sp_die(char *msg) |
| 42 | { |
| 43 | perror(msg); |
| 44 | exit(1); |
| 45 | } |
| 46 | |
Patrick Georgi | e48654c | 2010-01-06 22:14:39 +0000 | [diff] [blame] | 47 | #ifndef _WIN32 |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 48 | struct 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 }, |
| 55 | static 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 Georgi | e48654c | 2010-01-06 22:14:39 +0000 | [diff] [blame] | 102 | #endif |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 103 | |
Patrick Georgi | e48654c | 2010-01-06 22:14:39 +0000 | [diff] [blame] | 104 | fdtype sp_openserport(char *dev, unsigned int baud) |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 105 | { |
Patrick Georgi | e48654c | 2010-01-06 22:14:39 +0000 | [diff] [blame] | 106 | #ifdef _WIN32 |
| 107 | HANDLE fd; |
| 108 | fd = CreateFile(dev, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL); |
| 109 | if (fd == INVALID_HANDLE_VALUE) { |
| 110 | sp_die("Error: cannot open serial port"); |
| 111 | } |
| 112 | DCB dcb; |
| 113 | if (!GetCommState(fd, &dcb)) { |
| 114 | sp_die("Error: Could not fetch serial port configuration"); |
| 115 | } |
| 116 | switch (baud) { |
| 117 | case 9600: dcb.BaudRate = CBR_9600; break; |
| 118 | case 19200: dcb.BaudRate = CBR_19200; break; |
| 119 | case 38400: dcb.BaudRate = CBR_38400; break; |
| 120 | case 57600: dcb.BaudRate = CBR_57600; break; |
| 121 | case 115200: dcb.BaudRate = CBR_115200; break; |
| 122 | default: sp_die("Error: Could not set baud rate"); |
| 123 | } |
| 124 | dcb.ByteSize=8; |
| 125 | dcb.Parity=NOPARITY; |
| 126 | dcb.StopBits=ONESTOPBIT; |
| 127 | if (!SetCommState(fd, &dcb)) { |
| 128 | sp_die("Error: Could not change serial port configuration"); |
| 129 | } |
| 130 | return fd; |
| 131 | #else |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 132 | struct termios options; |
| 133 | int fd, i; |
| 134 | fd = open(dev, O_RDWR | O_NOCTTY | O_NDELAY); |
| 135 | if (fd < 0) |
| 136 | sp_die("Error: cannot open serial port"); |
| 137 | fcntl(fd, F_SETFL, 0); |
| 138 | tcgetattr(fd, &options); |
| 139 | for (i = 0;; i++) { |
| 140 | if (sp_baudtable[i].baud == 0) { |
| 141 | close(fd); |
| 142 | fprintf(stderr, |
| 143 | "Error: cannot configure for baudrate %d\n", |
| 144 | baud); |
| 145 | exit(1); |
| 146 | } |
| 147 | if (sp_baudtable[i].baud == baud) { |
| 148 | cfsetispeed(&options, sp_baudtable[i].flag); |
| 149 | cfsetospeed(&options, sp_baudtable[i].flag); |
| 150 | break; |
| 151 | } |
| 152 | } |
| 153 | options.c_cflag &= ~(PARENB | CSTOPB | CSIZE | CRTSCTS); |
| 154 | options.c_cflag |= (CS8 | CLOCAL | CREAD); |
| 155 | options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); |
| 156 | options.c_iflag &= ~(IXON | IXOFF | IXANY | ICRNL | IGNCR | INLCR); |
| 157 | options.c_oflag &= ~OPOST; |
| 158 | tcsetattr(fd, TCSANOW, &options); |
| 159 | return fd; |
Patrick Georgi | e48654c | 2010-01-06 22:14:39 +0000 | [diff] [blame] | 160 | #endif |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | void sp_flush_incoming(void) |
| 164 | { |
Patrick Georgi | e48654c | 2010-01-06 22:14:39 +0000 | [diff] [blame] | 165 | #ifdef _WIN32 |
| 166 | PurgeComm(sp_fd, PURGE_RXCLEAR); |
| 167 | #else |
Patrick Georgi | 3b6237d | 2010-01-06 19:09:40 +0000 | [diff] [blame] | 168 | tcflush(sp_fd, TCIFLUSH); |
Patrick Georgi | e48654c | 2010-01-06 22:14:39 +0000 | [diff] [blame] | 169 | #endif |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 170 | return; |
| 171 | } |
Carl-Daniel Hailfinger | efa151e | 2010-01-06 16:09:10 +0000 | [diff] [blame] | 172 | |
| 173 | int serialport_shutdown(void) |
| 174 | { |
Patrick Georgi | e48654c | 2010-01-06 22:14:39 +0000 | [diff] [blame] | 175 | #ifdef _WIN32 |
| 176 | CloseHandle(sp_fd); |
| 177 | #else |
Carl-Daniel Hailfinger | efa151e | 2010-01-06 16:09:10 +0000 | [diff] [blame] | 178 | close(sp_fd); |
Patrick Georgi | e48654c | 2010-01-06 22:14:39 +0000 | [diff] [blame] | 179 | #endif |
Carl-Daniel Hailfinger | efa151e | 2010-01-06 16:09:10 +0000 | [diff] [blame] | 180 | return 0; |
| 181 | } |
| 182 | |
| 183 | int serialport_write(unsigned char *buf, unsigned int writecnt) |
| 184 | { |
Patrick Georgi | e48654c | 2010-01-06 22:14:39 +0000 | [diff] [blame] | 185 | long tmp = 0; |
Carl-Daniel Hailfinger | efa151e | 2010-01-06 16:09:10 +0000 | [diff] [blame] | 186 | |
Patrick Georgi | 3b6237d | 2010-01-06 19:09:40 +0000 | [diff] [blame] | 187 | while (writecnt > 0) { |
Patrick Georgi | e48654c | 2010-01-06 22:14:39 +0000 | [diff] [blame] | 188 | #ifdef _WIN32 |
| 189 | WriteFile(sp_fd, buf, writecnt, &tmp, NULL); |
| 190 | #else |
Patrick Georgi | 3b6237d | 2010-01-06 19:09:40 +0000 | [diff] [blame] | 191 | tmp = write(sp_fd, buf, writecnt); |
Patrick Georgi | e48654c | 2010-01-06 22:14:39 +0000 | [diff] [blame] | 192 | #endif |
Carl-Daniel Hailfinger | efa151e | 2010-01-06 16:09:10 +0000 | [diff] [blame] | 193 | if (tmp == -1) |
| 194 | return 1; |
| 195 | if (!tmp) |
| 196 | printf_debug("Empty write\n"); |
Patrick Georgi | 3b6237d | 2010-01-06 19:09:40 +0000 | [diff] [blame] | 197 | writecnt -= tmp; |
| 198 | buf += tmp; |
Carl-Daniel Hailfinger | efa151e | 2010-01-06 16:09:10 +0000 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | return 0; |
| 202 | } |
| 203 | |
| 204 | int serialport_read(unsigned char *buf, unsigned int readcnt) |
| 205 | { |
Patrick Georgi | e48654c | 2010-01-06 22:14:39 +0000 | [diff] [blame] | 206 | long tmp = 0; |
Carl-Daniel Hailfinger | efa151e | 2010-01-06 16:09:10 +0000 | [diff] [blame] | 207 | |
Patrick Georgi | 3b6237d | 2010-01-06 19:09:40 +0000 | [diff] [blame] | 208 | while (readcnt > 0) { |
Patrick Georgi | e48654c | 2010-01-06 22:14:39 +0000 | [diff] [blame] | 209 | #ifdef _WIN32 |
| 210 | ReadFile(sp_fd, buf, readcnt, &tmp, NULL); |
| 211 | #else |
Patrick Georgi | 3b6237d | 2010-01-06 19:09:40 +0000 | [diff] [blame] | 212 | tmp = read(sp_fd, buf, readcnt); |
Patrick Georgi | e48654c | 2010-01-06 22:14:39 +0000 | [diff] [blame] | 213 | #endif |
Carl-Daniel Hailfinger | efa151e | 2010-01-06 16:09:10 +0000 | [diff] [blame] | 214 | if (tmp == -1) |
| 215 | return 1; |
| 216 | if (!tmp) |
| 217 | printf_debug("Empty read\n"); |
Patrick Georgi | 3b6237d | 2010-01-06 19:09:40 +0000 | [diff] [blame] | 218 | readcnt -= tmp; |
| 219 | buf += tmp; |
Carl-Daniel Hailfinger | efa151e | 2010-01-06 16:09:10 +0000 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | return 0; |
| 223 | } |