xref: /aosp_15_r20/external/flashrom/serial.c (revision 0d6140be3aa665ecc836e8907834fcd3e3b018fc)
1 /*
2  * This file is part of the flashrom project.
3  *
4  * Copyright (C) 2009 Urja Rannikko <[email protected]>
5  * Copyright (C) 2009,2010 Carl-Daniel Hailfinger
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 
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <unistd.h>
21 #include <string.h>
22 #include <ctype.h>
23 #include <fcntl.h>
24 #include <sys/stat.h>
25 #include <errno.h>
26 #include <inttypes.h>
27 #if IS_WINDOWS
28 #include <conio.h>
29 #else
30 #include <termios.h>
31 #include <unistd.h>
32 #include <sys/types.h>
33 #include <sys/ioctl.h>
34 #endif
35 #include "flash.h"
36 #include "programmer.h"
37 #include "custom_baud.h"
38 
39 fdtype sp_fd = SER_INV_FD;
40 
41 /* There is no way defined by POSIX to use arbitrary baud rates. It only defines some macros that can be used to
42  * specify respective baud rates and many implementations extend this list with further macros, cf. TERMIOS(3)
43  * and http://git.kernel.org/?p=linux/kernel/git/torvalds/linux.git;a=blob;f=include/uapi/asm-generic/termbits.h
44  * The code below creates a mapping in sp_baudtable between these macros and the numerical baud rates to deal
45  * with numerical user input.
46  *
47  * On Linux there is a non-standard way to use arbitrary baud rates that we use if there is no
48  * matching standard rate, see custom_baud.c
49  *
50  * On Darwin there is also a non-standard ioctl() to set arbitrary baud rates
51  * and any above 230400, see custom_baud_darwin.c and
52  * https://opensource.apple.com/source/IOSerialFamily/IOSerialFamily-91/tests/IOSerialTestLib.c.auto.html
53  *
54  * On Windows there exist similar macros (starting with CBR_ instead of B) but they are only defined for
55  * backwards compatibility and the API supports arbitrary baud rates in the same manner as the macros, see
56  * http://msdn.microsoft.com/en-us/library/windows/desktop/aa363214(v=vs.85).aspx
57  */
58 #if !IS_WINDOWS
59 #define BAUDENTRY(baud) { B##baud, baud },
60 
61 static const struct baudentry sp_baudtable[] = {
62 	BAUDENTRY(9600) /* unconditional default */
63 #ifdef B19200
64 	BAUDENTRY(19200)
65 #endif
66 #ifdef B38400
67 	BAUDENTRY(38400)
68 #endif
69 #ifdef B57600
70 	BAUDENTRY(57600)
71 #endif
72 #ifdef B115200
73 	BAUDENTRY(115200)
74 #endif
75 #ifdef B230400
76 	BAUDENTRY(230400)
77 #endif
78 #ifdef B460800
79 	BAUDENTRY(460800)
80 #endif
81 #ifdef B500000
82 	BAUDENTRY(500000)
83 #endif
84 #ifdef B576000
85 	BAUDENTRY(576000)
86 #endif
87 #ifdef B921600
88 	BAUDENTRY(921600)
89 #endif
90 #ifdef B1000000
91 	BAUDENTRY(1000000)
92 #endif
93 #ifdef B1152000
94 	BAUDENTRY(1152000)
95 #endif
96 #ifdef B1500000
97 	BAUDENTRY(1500000)
98 #endif
99 #ifdef B2000000
100 	BAUDENTRY(2000000)
101 #endif
102 #ifdef B2500000
103 	BAUDENTRY(2500000)
104 #endif
105 #ifdef B3000000
106 	BAUDENTRY(3000000)
107 #endif
108 #ifdef B3500000
109 	BAUDENTRY(3500000)
110 #endif
111 #ifdef B4000000
112 	BAUDENTRY(4000000)
113 #endif
114 	{0, 0}			/* Terminator */
115 };
116 
round_baud(unsigned int baud)117 static const struct baudentry *round_baud(unsigned int baud)
118 {
119 	int i;
120 	/* Round baud rate to next lower entry in sp_baudtable if it exists, else use the lowest entry. */
121 	for (i = ARRAY_SIZE(sp_baudtable) - 2; i >= 0 ; i--) {
122 		if (sp_baudtable[i].baud == baud)
123 			return &sp_baudtable[i];
124 
125 		if (sp_baudtable[i].baud < baud) {
126 			msg_pwarn("Warning: given baudrate %d rounded down to %d.\n",
127 				  baud, sp_baudtable[i].baud);
128 			return &sp_baudtable[i];
129 		}
130 	}
131 	msg_pinfo("Using slowest possible baudrate: %d.\n", sp_baudtable[0].baud);
132 	return &sp_baudtable[0];
133 }
134 #endif
135 
136 /* Uses msg_perr to print the last system error.
137  * Prints "Error: " followed first by \c msg and then by the description of the last error retrieved via
138  * strerror() or FormatMessage() and ending with a linebreak. */
msg_perr_strerror(const char * msg)139 static void msg_perr_strerror(const char *msg)
140 {
141 	msg_perr("Error: %s", msg);
142 #if IS_WINDOWS
143 	char *lpMsgBuf;
144 	DWORD nErr = GetLastError();
145 	FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, nErr,
146 		      MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &lpMsgBuf, 0, NULL);
147 	msg_perr(lpMsgBuf);
148 	/* At least some formatted messages contain a line break at the end. Make sure to always print one */
149 	if (lpMsgBuf[strlen(lpMsgBuf)-1] != '\n')
150 		msg_perr("\n");
151 	LocalFree(lpMsgBuf);
152 #else
153 	msg_perr("%s\n", strerror(errno));
154 #endif
155 }
156 
serialport_config(fdtype fd,int baud)157 int serialport_config(fdtype fd, int baud)
158 {
159 	if (fd == SER_INV_FD) {
160 		msg_perr("%s: File descriptor is invalid.\n", __func__);
161 		return 1;
162 	}
163 
164 #if IS_WINDOWS
165 	DCB dcb;
166 	if (!GetCommState(fd, &dcb)) {
167 		msg_perr_strerror("Could not fetch original serial port configuration: ");
168 		return 1;
169 	}
170 	if (baud >= 0) {
171 		dcb.BaudRate = baud;
172 	}
173 	dcb.ByteSize = 8;
174 	dcb.Parity = NOPARITY;
175 	dcb.StopBits = ONESTOPBIT;
176 	if (!SetCommState(fd, &dcb)) {
177 		msg_perr_strerror("Could not change serial port configuration: ");
178 		return 1;
179 	}
180 	if (!GetCommState(fd, &dcb)) {
181 		msg_perr_strerror("Could not fetch new serial port configuration: ");
182 		return 1;
183 	}
184 	msg_pdbg("Baud rate is %ld.\n", dcb.BaudRate);
185 #else
186 	int custom_baud = (baud >= 0 && use_custom_baud(baud, sp_baudtable));
187 	struct termios wanted, observed;
188 	if (tcgetattr(fd, &observed) != 0) {
189 		msg_perr_strerror("Could not fetch original serial port configuration: ");
190 		return 1;
191 	}
192 	wanted = observed;
193 	if (baud >= 0) {
194 		if (custom_baud) {
195 			if (set_custom_baudrate(fd, baud, BEFORE_FLAGS, NULL)) {
196 				msg_perr_strerror("Could not set custom baudrate: ");
197 				return 1;
198 			}
199 			/* We want whatever the termios looks like now, so the rest of the
200 			   setup doesn't mess up the custom rate. */
201 			if (tcgetattr(fd, &wanted) != 0) {
202 				/* This should pretty much never happen (see above), but.. */
203 				msg_perr_strerror("Could not fetch serial port configuration: ");
204 				return 1;
205 			}
206 		} else {
207 			const struct baudentry *entry = round_baud(baud);
208 			if (cfsetispeed(&wanted, entry->flag) != 0 || cfsetospeed(&wanted, entry->flag) != 0) {
209 				msg_perr_strerror("Could not set serial baud rate: ");
210 				return 1;
211 			}
212 		}
213 	}
214 	wanted.c_cflag &= ~(PARENB | CSTOPB | CSIZE | CRTSCTS);
215 	wanted.c_cflag |= (CS8 | CLOCAL | CREAD);
216 	wanted.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG | IEXTEN);
217 	wanted.c_iflag &= ~(IXON | IXOFF | IXANY | ICRNL | IGNCR | INLCR);
218 	wanted.c_oflag &= ~OPOST;
219 	if (custom_baud && set_custom_baudrate(fd, baud, WITH_FLAGS, &wanted)) {
220 		msg_perr_strerror("Could not set custom baudrate: ");
221 		return 1;
222 	}
223 	if (tcsetattr(fd, TCSANOW, &wanted) != 0) {
224 		msg_perr_strerror("Could not change serial port configuration: ");
225 		return 1;
226 	}
227 	if (tcgetattr(fd, &observed) != 0) {
228 		msg_perr_strerror("Could not fetch new serial port configuration: ");
229 		return 1;
230 	}
231 	if (observed.c_cflag != wanted.c_cflag ||
232 	    observed.c_lflag != wanted.c_lflag ||
233 	    observed.c_iflag != wanted.c_iflag ||
234 	    observed.c_oflag != wanted.c_oflag) {
235 		msg_pwarn("Some requested serial options did not stick, continuing anyway.\n");
236 		msg_pdbg("          observed    wanted\n"
237 			 "c_cflag:  0x%08lX  0x%08lX\n"
238 			 "c_lflag:  0x%08lX  0x%08lX\n"
239 			 "c_iflag:  0x%08lX  0x%08lX\n"
240 			 "c_oflag:  0x%08lX  0x%08lX\n",
241 			 (long)observed.c_cflag, (long)wanted.c_cflag,
242 			 (long)observed.c_lflag, (long)wanted.c_lflag,
243 			 (long)observed.c_iflag, (long)wanted.c_iflag,
244 			 (long)observed.c_oflag, (long)wanted.c_oflag
245 			);
246 	}
247 	if (custom_baud) {
248 		if (set_custom_baudrate(fd, baud, AFTER_FLAGS, &wanted)) {
249 			msg_perr_strerror("Could not set custom baudrate: ");
250 			return 1;
251 		}
252 		msg_pdbg("Using custom baud rate.\n");
253 	}
254 	if (cfgetispeed(&observed) != cfgetispeed(&wanted) ||
255 	    cfgetospeed(&observed) != cfgetospeed(&wanted)) {
256 		msg_pwarn("Could not set baud rates exactly.\n");
257 		msg_pdbg("Actual baud flags are: ispeed: 0x%08lX, ospeed: 0x%08lX\n",
258 			  (long)cfgetispeed(&observed), (long)cfgetospeed(&observed));
259 	}
260 	// FIXME: display actual baud rate - at least if none was specified by the user.
261 #endif
262 	return 0;
263 }
264 
sp_openserport(char * dev,int baud)265 fdtype sp_openserport(char *dev, int baud)
266 {
267 	fdtype fd;
268 #if IS_WINDOWS
269 	char *dev2 = dev;
270 	if ((strlen(dev) > 3) &&
271 	    (tolower((unsigned char)dev[0]) == 'c') &&
272 	    (tolower((unsigned char)dev[1]) == 'o') &&
273 	    (tolower((unsigned char)dev[2]) == 'm')) {
274 		dev2 = malloc(strlen(dev) + 5);
275 		if (!dev2) {
276 			msg_perr_strerror("Out of memory: ");
277 			return SER_INV_FD;
278 		}
279 		strcpy(dev2, "\\\\.\\");
280 		strcpy(dev2 + 4, dev);
281 	}
282 	fd = CreateFile(dev2, GENERIC_READ | GENERIC_WRITE, 0, NULL,
283 			OPEN_EXISTING, 0, NULL);
284 	if (dev2 != dev)
285 		free(dev2);
286 	if (fd == INVALID_HANDLE_VALUE) {
287 		msg_perr_strerror("Cannot open serial port: ");
288 		return SER_INV_FD;
289 	}
290 	if (serialport_config(fd, baud) != 0) {
291 		CloseHandle(fd);
292 		return SER_INV_FD;
293 	}
294 	return fd;
295 #else
296 	fd = open(dev, O_RDWR | O_NOCTTY | O_NDELAY); // Use O_NDELAY to ignore DCD state
297 	if (fd < 0) {
298 		msg_perr_strerror("Cannot open serial port: ");
299 		return SER_INV_FD;
300 	}
301 
302 	/* Ensure that we use blocking I/O */
303 	const int flags = fcntl(fd, F_GETFL);
304 	if (flags == -1) {
305 		msg_perr_strerror("Could not get serial port mode: ");
306 		goto err;
307 	}
308 	if (fcntl(fd, F_SETFL, flags & ~O_NONBLOCK) != 0) {
309 		msg_perr_strerror("Could not set serial port mode to blocking: ");
310 		goto err;
311 	}
312 
313 	if (serialport_config(fd, baud) != 0) {
314 		goto err;
315 	}
316 	return fd;
317 err:
318 	close(fd);
319 	return SER_INV_FD;
320 #endif
321 }
322 
sp_set_pin(enum SP_PIN pin,int val)323 void sp_set_pin(enum SP_PIN pin, int val) {
324 #if IS_WINDOWS
325 	DWORD ctl;
326 
327 	if(pin == PIN_TXD) {
328 		ctl = val ? SETBREAK: CLRBREAK;
329 	}
330 	else if(pin == PIN_DTR) {
331 		ctl = val ? SETDTR: CLRDTR;
332 	}
333 	else {
334 		ctl = val ? SETRTS: CLRRTS;
335 	}
336 	EscapeCommFunction(sp_fd, ctl);
337 #else
338 	int ctl, s;
339 
340 	if(pin == PIN_TXD) {
341 		ioctl(sp_fd, val ? TIOCSBRK : TIOCCBRK, 0);
342 	}
343 	else {
344 		s = (pin == PIN_DTR) ? TIOCM_DTR : TIOCM_RTS;
345 		ioctl(sp_fd, TIOCMGET, &ctl);
346 
347 		if (val) {
348 			ctl |= s;
349 		}
350 		else {
351 			ctl &= ~s;
352 		}
353 		ioctl(sp_fd, TIOCMSET, &ctl);
354 	}
355 #endif
356 }
357 
sp_get_pin(enum SP_PIN pin)358 int sp_get_pin(enum SP_PIN pin) {
359 	int s;
360 #if IS_WINDOWS
361 	DWORD ctl;
362 
363 	s = (pin == PIN_CTS) ? MS_CTS_ON : MS_DSR_ON;
364 	GetCommModemStatus(sp_fd, &ctl);
365 #else
366 	int ctl;
367 	s = (pin == PIN_CTS) ? TIOCM_CTS : TIOCM_DSR;
368 	ioctl(sp_fd, TIOCMGET, &ctl);
369 #endif
370 
371 	return ((ctl & s) ? 1 : 0);
372 
373 }
374 
sp_flush_incoming(void)375 void sp_flush_incoming(void)
376 {
377 #if IS_WINDOWS
378 	PurgeComm(sp_fd, PURGE_RXCLEAR);
379 #else
380 	if (!tcflush(sp_fd, TCIFLUSH))
381 		return;
382 
383 	if (errno == ENOTTY) { // TCP socket case: sp_fd is not a terminal descriptor - tcflush is not supported
384 		unsigned char c;
385 		int ret;
386 
387 		do {
388 			ret = serialport_read_nonblock(&c, 1, 1, NULL);
389 		} while (ret == 0);
390 
391 		// positive error code indicates no data available immediately - similar to EAGAIN/EWOULDBLOCK
392 		//   i.e. all buffered data was read
393 		// negative error code indicates a permanent error
394 		if (ret < 0)
395 			msg_perr("Could not flush serial port incoming buffer: read has failed");
396 	} else { // any other errno indicates an unrecoverable sp_fd state
397 		msg_perr_strerror("Could not flush serial port incoming buffer: ");
398 	}
399 #endif
400 }
401 
serialport_shutdown(void * data)402 int serialport_shutdown(void *data)
403 {
404 #if IS_WINDOWS
405 	CloseHandle(sp_fd);
406 #else
407 	close(sp_fd);
408 #endif
409 	return 0;
410 }
411 
serialport_write(const unsigned char * buf,unsigned int writecnt)412 int serialport_write(const unsigned char *buf, unsigned int writecnt)
413 {
414 #if IS_WINDOWS
415 	DWORD tmp = 0;
416 #else
417 	ssize_t tmp = 0;
418 #endif
419 	unsigned int empty_writes = 250; /* results in a ca. 125ms timeout */
420 
421 	while (writecnt > 0) {
422 #if IS_WINDOWS
423 		if (!WriteFile(sp_fd, buf, writecnt, &tmp, NULL)) {
424 			msg_perr("Serial port write error!\n");
425 			return 1;
426 		}
427 #else
428 		tmp = write(sp_fd, buf, writecnt);
429 		if (tmp == -1) {
430 			msg_perr("Serial port write error!\n");
431 			return 1;
432 		}
433 #endif
434 		if (!tmp) {
435 			msg_pdbg2("Empty write\n");
436 			empty_writes--;
437 			default_delay(500);
438 			if (empty_writes == 0) {
439 				msg_perr("Serial port is unresponsive!\n");
440 				return 1;
441 			}
442 		}
443 		writecnt -= tmp;
444 		buf += tmp;
445 	}
446 
447 	return 0;
448 }
449 
serialport_read(unsigned char * buf,unsigned int readcnt)450 int serialport_read(unsigned char *buf, unsigned int readcnt)
451 {
452 #if IS_WINDOWS
453 	DWORD tmp = 0;
454 #else
455 	ssize_t tmp = 0;
456 #endif
457 
458 	while (readcnt > 0) {
459 #if IS_WINDOWS
460 		if (!ReadFile(sp_fd, buf, readcnt, &tmp, NULL)) {
461 			msg_perr("Serial port read error!\n");
462 			return 1;
463 		}
464 #else
465 		tmp = read(sp_fd, buf, readcnt);
466 		if (tmp == -1) {
467 			msg_perr("Serial port read error!\n");
468 			return 1;
469 		}
470 #endif
471 		if (!tmp)
472 			msg_pdbg2("Empty read\n");
473 		readcnt -= tmp;
474 		buf += tmp;
475 	}
476 
477 	return 0;
478 }
479 
480 /* Tries up to timeout ms to read readcnt characters and places them into the array starting at c. Returns
481  * 0 on success, positive values on temporary errors (e.g. timeouts) and negative ones on permanent errors.
482  * If really_read is not NULL, this function sets its contents to the number of bytes read successfully. */
serialport_read_nonblock(unsigned char * c,unsigned int readcnt,unsigned int timeout,unsigned int * really_read)483 int serialport_read_nonblock(unsigned char *c, unsigned int readcnt, unsigned int timeout, unsigned int *really_read)
484 {
485 	int ret = 1;
486 	/* disable blocked i/o and declare platform-specific variables */
487 #if IS_WINDOWS
488 	DWORD rv;
489 	COMMTIMEOUTS oldTimeout;
490 	COMMTIMEOUTS newTimeout = {
491 		.ReadIntervalTimeout = MAXDWORD,
492 		.ReadTotalTimeoutMultiplier = 0,
493 		.ReadTotalTimeoutConstant = 0,
494 		.WriteTotalTimeoutMultiplier = 0,
495 		.WriteTotalTimeoutConstant = 0
496 	};
497 	if(!GetCommTimeouts(sp_fd, &oldTimeout)) {
498 		msg_perr_strerror("Could not get serial port timeout settings: ");
499 		return -1;
500 	}
501 	if(!SetCommTimeouts(sp_fd, &newTimeout)) {
502 		msg_perr_strerror("Could not set serial port timeout settings: ");
503 		return -1;
504 	}
505 #else
506 	ssize_t rv;
507 	const int flags = fcntl(sp_fd, F_GETFL);
508 	if (flags == -1) {
509 		msg_perr_strerror("Could not get serial port mode: ");
510 		return -1;
511 	}
512 	if (fcntl(sp_fd, F_SETFL, flags | O_NONBLOCK) != 0) {
513 		msg_perr_strerror("Could not set serial port mode to non-blocking: ");
514 		return -1;
515 	}
516 #endif
517 
518 	unsigned int i;
519 	unsigned int rd_bytes = 0;
520 	for (i = 0; i < timeout; i++) {
521 		msg_pspew("readcnt %u rd_bytes %u\n", readcnt, rd_bytes);
522 #if IS_WINDOWS
523 		if (!ReadFile(sp_fd, c + rd_bytes, readcnt - rd_bytes, &rv, NULL)) {
524 			msg_perr_strerror("Serial port read error: ");
525 			ret = -1;
526 			break;
527 		}
528 		msg_pspew("read %lu bytes\n", rv);
529 #else
530 		rv = read(sp_fd, c + rd_bytes, readcnt - rd_bytes);
531 		msg_pspew("read %zd bytes\n", rv);
532 		if ((rv == -1) && (errno != EAGAIN)) {
533 			msg_perr_strerror("Serial port read error: ");
534 			ret = -1;
535 			break;
536 		}
537 #endif
538 		if (rv > 0)
539 			rd_bytes += rv;
540 		if (rd_bytes == readcnt) {
541 			ret = 0;
542 			break;
543 		}
544 		default_delay(1000);	/* 1ms units */
545 	}
546 	if (really_read != NULL)
547 		*really_read = rd_bytes;
548 
549 	/* restore original blocking behavior */
550 #if IS_WINDOWS
551 	if (!SetCommTimeouts(sp_fd, &oldTimeout)) {
552 		msg_perr_strerror("Could not restore serial port timeout settings: ");
553 		ret = -1;
554 	}
555 #else
556 	if (fcntl(sp_fd, F_SETFL, flags) != 0) {
557 		msg_perr_strerror("Could not restore serial port mode to blocking: ");
558 		ret = -1;
559 	}
560 #endif
561 	return ret;
562 }
563 
564 /* Tries up to timeout ms to write writecnt characters from the array starting at buf. Returns
565  * 0 on success, positive values on temporary errors (e.g. timeouts) and negative ones on permanent errors.
566  * If really_wrote is not NULL, this function sets its contents to the number of bytes written successfully. */
serialport_write_nonblock(const unsigned char * buf,unsigned int writecnt,unsigned int timeout,unsigned int * really_wrote)567 int serialport_write_nonblock(const unsigned char *buf, unsigned int writecnt, unsigned int timeout, unsigned int *really_wrote)
568 {
569 	int ret = 1;
570 	/* disable blocked i/o and declare platform-specific variables */
571 #if IS_WINDOWS
572 	DWORD rv;
573 	COMMTIMEOUTS oldTimeout;
574 	COMMTIMEOUTS newTimeout = {
575 		.ReadIntervalTimeout = MAXDWORD,
576 		.ReadTotalTimeoutMultiplier = 0,
577 		.ReadTotalTimeoutConstant = 0,
578 		.WriteTotalTimeoutMultiplier = 0,
579 		.WriteTotalTimeoutConstant = 0
580 	};
581 	if(!GetCommTimeouts(sp_fd, &oldTimeout)) {
582 		msg_perr_strerror("Could not get serial port timeout settings: ");
583 		return -1;
584 	}
585 	if(!SetCommTimeouts(sp_fd, &newTimeout)) {
586 		msg_perr_strerror("Could not set serial port timeout settings: ");
587 		return -1;
588 	}
589 #else
590 	ssize_t rv;
591 	const int flags = fcntl(sp_fd, F_GETFL);
592 	if (flags == -1) {
593 		msg_perr_strerror("Could not get serial port mode: ");
594 		return -1;
595 	}
596 	if (fcntl(sp_fd, F_SETFL, flags | O_NONBLOCK) != 0) {
597 		msg_perr_strerror("Could not set serial port mode to non-blocking: ");
598 		return -1;
599 	}
600 #endif
601 
602 	unsigned int i;
603 	unsigned int wr_bytes = 0;
604 	for (i = 0; i < timeout; i++) {
605 		msg_pspew("writecnt %u wr_bytes %u\n", writecnt, wr_bytes);
606 #if IS_WINDOWS
607 		if (!WriteFile(sp_fd, buf + wr_bytes, writecnt - wr_bytes, &rv, NULL)) {
608 			msg_perr_strerror("Serial port write error: ");
609 			ret = -1;
610 			break;
611 		}
612 		msg_pspew("wrote %lu bytes\n", rv);
613 #else
614 		rv = write(sp_fd, buf + wr_bytes, writecnt - wr_bytes);
615 		msg_pspew("wrote %zd bytes\n", rv);
616 		if ((rv == -1) && (errno != EAGAIN)) {
617 			msg_perr_strerror("Serial port write error: ");
618 			ret = -1;
619 			break;
620 		}
621 #endif
622 		if (rv > 0) {
623 			wr_bytes += rv;
624 			if (wr_bytes == writecnt) {
625 				msg_pspew("write successful\n");
626 				ret = 0;
627 				break;
628 			}
629 		}
630 		default_delay(1000);	/* 1ms units */
631 	}
632 	if (really_wrote != NULL)
633 		*really_wrote = wr_bytes;
634 
635 	/* restore original blocking behavior */
636 #if IS_WINDOWS
637 	if (!SetCommTimeouts(sp_fd, &oldTimeout)) {
638 		msg_perr_strerror("Could not restore serial port timeout settings: ");
639 		return -1;
640 	}
641 #else
642 	if (fcntl(sp_fd, F_SETFL, flags) != 0) {
643 		msg_perr_strerror("Could not restore serial port blocking behavior: ");
644 		return -1;
645 	}
646 #endif
647 	return ret;
648 }
649