ftdiuart: bugfix for buffer pointer on partial write to FD.

Previously I neglected to increment the buffer pointer which allow me
to double write same data and exclude tail if write didn't complete in
one shot.

Also fixing bug with EAGAIN/EWOULDBLOCK to retry in those cases.
Previously we'd just have quitely ignored the write.

BUG=chromium:371147
TEST=manual
- start servod on board with active uart
- send & receive uart traffic successfully

Change-Id: I0d8b0a2f87346c3667c711f0c2ebb491cefcd5ae
Reviewed-on: https://chromium-review.googlesource.com/198867
Reviewed-by: Vincent Palatin <vpalatin@chromium.org>
Commit-Queue: Todd Broch <tbroch@chromium.org>
Tested-by: Todd Broch <tbroch@chromium.org>
diff --git a/lib/ftdiuart.c b/lib/ftdiuart.c
index 96e5143..cab3ff9 100644
--- a/lib/ftdiuart.c
+++ b/lib/ftdiuart.c
@@ -339,10 +339,19 @@
   bytes = ftdi_read_data(fc, fuartc->buf, sizeof(fuartc->buf));
   if (bytes > 0) {
     int bytes_remaining = bytes;
-    while ((bytes = write(fuartc->fd, fuartc->buf, bytes_remaining)) > 0) {
+    uint8_t *rd_buf = fuartc->buf;
+
+ retry_write:
+    while (bytes_remaining &&
+           ((bytes = write(fuartc->fd, rd_buf, bytes_remaining)) > 0)) {
+      rd_buf += bytes;
       bytes_remaining -= bytes;
     }
-    if ((bytes == -1) && (errno != EAGAIN) && (errno != EWOULDBLOCK)) {
+    if ((bytes == -1) && ((errno == EAGAIN) || (errno == EWOULDBLOCK))) {
+      goto retry_write;
+    }
+
+    if (bytes == -1) {
       perror("writing ftdi data to pty");
     }