novatek-tcon-fw-update-tool: Print errors to stderr instead of stdout.

BUG=b:112485400
TEST=build_packages and direct make invocation both continue to succeed.

Change-Id: I24f14be20d113371543ac4da6dac81af25bceb86
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/novatek-tcon-fw-update-tool/+/1965607
Commit-Queue: Matthew Blecker <matthewb@chromium.org>
Tested-by: Matthew Blecker <matthewb@chromium.org>
Reviewed-by: Mike Frysinger <vapier@chromium.org>
diff --git a/src/NVTAuxCtrl.cc b/src/NVTAuxCtrl.cc
index 1424da2..2446e63 100644
--- a/src/NVTAuxCtrl.cc
+++ b/src/NVTAuxCtrl.cc
@@ -6,7 +6,7 @@
 
 #include "NVTAuxCtrl.h"
 
-#include <errno.h>
+#include <err.h>
 #include <fcntl.h>
 #include <linux/i2c-dev.h>
 #include <linux/i2c.h>
@@ -35,15 +35,13 @@
         "find /sys/class/drm/*eDP*/drm_dp_aux* -maxdepth 0 | sed 's/.*\\///'",
         "r");
     if (fp == NULL) {
-      printf(
-          "Fail to get eDP interface - DpInt==2 drm_dp_aux* popen() failed\n");
+      warnx("Fail to get eDP interface - DpInt==2 drm_dp_aux* popen() failed");
       return AUX_STATUS_FAIL;
     }
     chrret = fgets(path, sizeof(path) - 1, fp);
     pclose(fp);
     if (!chrret) {
-      printf(
-          "Fail to get eDP interface - DpInt==2 drm_dp_aux* fgets() failed\n");
+      warnx("Fail to get eDP interface - DpInt==2 drm_dp_aux* fgets() failed");
       return AUX_STATUS_FAIL;
     }
 
@@ -55,13 +53,13 @@
     fp = popen("find /sys/class/drm/*eDP*/i2c-* -maxdepth 0 | sed 's/.*\\///'",
                "r");
     if (fp == NULL) {
-      printf("Fail to get eDP interface - DpInt==2 i2c-* popen() failed\n");
+      warnx("Fail to get eDP interface - DpInt==2 i2c-* popen() failed");
       return AUX_STATUS_FAIL;
     }
     chrret = fgets(path, sizeof(path) - 1, fp);
     pclose(fp);
     if (!chrret) {
-      printf("Fail to get eDP interface - DpInt==2 i2c-* fgets() failed\n");
+      warnx("Fail to get eDP interface - DpInt==2 i2c-* fgets() failed");
       return AUX_STATUS_FAIL;
     }
 
@@ -79,7 +77,7 @@
       openStat = open(path, O_RDONLY);
       if (openStat < 0) continue;
       if (close(openStat) < 0) {
-        printf("error %d failed when attempting to close %s\n", errno, path);
+        warn("failed when attempting to close %s", path);
         return AUX_STATUS_FAIL;
       }
 
@@ -87,19 +85,15 @@
               path);
       fp = popen(pollingPath, "r");
       if (fp == NULL) {
-        printf(
-            "Fail to get eDP interface - DpInt==3 portNo==%d drm_dp_aux* "
-            "popen() failed\n",
-            portNo);
+        warnx("Fail to get eDP interface - DpInt==3 portNo==%d drm_dp_aux* "
+              "popen() failed", portNo);
         return AUX_STATUS_FAIL;
       }
       chrret = fgets(pollingPath, sizeof(pollingPath) - 1, fp);
       pclose(fp);
       if (!chrret) {
-        printf(
-            "Fail to get eDP interface - DpInt==3 portNo==%d drm_dp_aux* "
-            "fgets() failed\n",
-            portNo);
+        warnx("Fail to get eDP interface - DpInt==3 portNo==%d drm_dp_aux* "
+              "fgets() failed", portNo);
         return AUX_STATUS_FAIL;
       }
       sprintf(auxPath, "/dev/%s", pollingPath);
@@ -110,19 +104,15 @@
       sprintf(pollingPath, "find %s/i2c* -maxdepth 0 | sed 's/.*\\///'", path);
       fp = popen(pollingPath, "r");
       if (fp == NULL) {
-        printf(
-            "Fail to get eDP interface - DpInt==3 portNo==%d i2c-* popen() "
-            "failed\n",
-            portNo);
+        warnx("Fail to get eDP interface - DpInt==3 portNo==%d i2c-* popen() "
+              "failed", portNo);
         return AUX_STATUS_FAIL;
       }
       chrret = fgets(pollingPath, sizeof(pollingPath) - 1, fp);
       pclose(fp);
       if (!chrret) {
-        printf(
-            "Fail to get eDP interface - DpInt==3 portNo==%d i2c-* fgets() "
-            "failed\n",
-            portNo);
+        warnx("Fail to get eDP interface - DpInt==3 portNo==%d i2c-* fgets() "
+              "failed", portNo);
         return AUX_STATUS_FAIL;
       }
       sprintf(i2cPath, "/dev/%s", pollingPath);
@@ -133,19 +123,19 @@
       break;
     }
   } else {
-    printf("unknown DP Interface\n");
+    warnx("unknown DP Interface");
     return AUX_STATUS_FAIL;
   }
 
   // open Native aux device
   if ((fpNativeAux = open(auxPath, O_RDWR)) < 0) {
-    printf("open fpNativeAux device fail: %d\n", errno);
+    warn("open fpNativeAux device failed");
     return AUX_STATUS_FAIL;
   }
 
   // open I2CoverAux device
   if ((fpI2CoverAux = open(i2cPath, O_RDWR)) < 0) {
-    printf("open I2CoverAux device fail: %d\n", errno);
+    warn("open I2CoverAux device failed");
     return AUX_STATUS_FAIL;
   }
 
@@ -155,11 +145,11 @@
 NVTAUX_STATUS NVTAUX_Close() {
   NVTAUX_STATUS retval = AUX_STATUS_OK;
   if (close(fpNativeAux) < 0) {
-    printf("error %d failed when attempting to close fpNativeAux\n", errno);
+    warn("failed when attempting to close fpNativeAux");
     retval = AUX_STATUS_FAIL;
   }
   if (close(fpI2CoverAux) < 0) {
-    printf("error %d failed when attempting to close fpI2CoverAux\n", errno);
+    warn("failed when attempting to close fpI2CoverAux");
     retval = AUX_STATUS_FAIL;
   }
   return retval;
@@ -252,14 +242,14 @@
 
   ret = ioctl(fpI2CoverAux, I2C_RDWR, (unsigned long)&i2c_data);
   if (ret < 0) {
-    printf("i2c write fail\n");
+    warnx("i2c write fail");
     return AUX_STATUS_FAIL;
   }
 #else
   // intel driver provided interface
   ret = write(fpI2CoverAux, wrBuf, wrLen);
   if (ret != wrLen) {
-    printf("i2c write fail\n");
+    warnx("i2c write fail");
     return AUX_STATUS_FAIL;
   }
 #endif  // 0
@@ -283,7 +273,7 @@
   ret = ioctl(fpI2CoverAux, I2C_RDWR, (unsigned long)&i2c_data);
 
   if (ret < 0) {
-    printf("i2c read fail\n");
+    warnx("i2c read fail");
     return AUX_STATUS_FAIL;
   }
 
@@ -348,7 +338,7 @@
   ret = ioctl(fpI2CoverAux, I2C_RDWR, (unsigned long)&i2c_data);
 
   if (ret < 0) {
-    printf("i2c burst read fail\n");
+    warnx("i2c burst read fail");
     return AUX_STATUS_FAIL;
   }
 
diff --git a/src/NVTSettingProc.cc b/src/NVTSettingProc.cc
index 29d01e9..7d28e90 100644
--- a/src/NVTSettingProc.cc
+++ b/src/NVTSettingProc.cc
@@ -6,7 +6,7 @@
 
 #include "NVTSettingProc.h"
 
-#include <errno.h>
+#include <err.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -41,7 +41,7 @@
   size_t size_ret;
 
   if (fp == NULL) {
-    printf("can't open model file:%s\n", path);
+    warn("can't open model file:%s", path);
     return INIT_STATUS_FAIL;
   }
   while ((getline(&line, &lineLen, fp)) > 0) {
@@ -73,23 +73,23 @@
   // check setting
   fp = fopen(gConfig.m_GoldenFilePath, "rb");
   if (fp == NULL) {
-    printf("%s not exist!!\n", gConfig.m_GoldenFilePath);
+    warn("%s not exist!!", gConfig.m_GoldenFilePath);
     return INIT_STATUS_FAIL;
   }
 
   if (fseek(fp, 0, SEEK_END) < 0) {
-    printf("error %d failed to seek on %s\n", errno, path);
+    warn("failed to seek on %s", path);
     fclose(fp);
     return INIT_STATUS_FAIL;
   }
   long fSize = ftell(fp);
   if (fSize != gConfig.m_BufferSize) {
-    printf("wrong Buffer Size\n");
+    warnx("wrong Buffer Size");
     fclose(fp);
     return INIT_STATUS_FAIL;
   }
   if ((gConfig.m_DpInterface < 2) || (gConfig.m_DpInterface > 3)) {
-    printf("unknown DP Interface\n");
+    warnx("unknown DP Interface");
     fclose(fp);
     return INIT_STATUS_FAIL;
   }
@@ -98,14 +98,14 @@
   if (m_GoldenBuf != NULL) delete m_GoldenBuf;
   m_GoldenBuf = new UCHAR[fSize];
   if (fseek(fp, 0, SEEK_SET) < 0) {
-    printf("error %d failed to seek on %s\n", errno, path);
+    warn("failed to seek on %s", path);
     fclose(fp);
     return INIT_STATUS_FAIL;
   }
   size_ret = fread(m_GoldenBuf, fSize, 1, fp);
   fclose(fp);
   if (size_ret < 1) {
-    printf("failed to read from m_GoldenBuf\n");
+    warnx("failed to read from m_GoldenBuf");
     return INIT_STATUS_FAIL;
   }
   auxConfig.m_AuxRequestByte = gConfig.m_AuxRequestByte;
diff --git a/src/main.cc b/src/main.cc
index 46ff57f..1d70731 100644
--- a/src/main.cc
+++ b/src/main.cc
@@ -4,6 +4,7 @@
 /*  found in the LICENSE file.                                              */
 /****************************************************************************/
 
+#include <err.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -49,7 +50,7 @@
       // update code
       status = LoadIni(argv[1], false, paraRomPath);
       if (status != INIT_STATUS_OK) {
-        printf("load model file fail\n");
+        warnx("load model file fail");
         return LOAD_MODEL_INIT_FAIL;
       }
       gettimeofday(&t1, NULL);
@@ -66,7 +67,7 @@
       // read code
       status = LoadIni(argv[2], false, paraRomPath);
       if (status != INIT_STATUS_OK) {
-        printf("load model file fail\n");
+        warnx("load model file fail");
         return LOAD_MODEL_INIT_FAIL;
       }
       gettimeofday(&t1, NULL);
@@ -85,7 +86,7 @@
       status = LoadIni(argv[2], true, paraRomPath);
 
       if (status != INIT_STATUS_OK) {
-        printf("load model file fail\n");
+        warnx("load model file fail");
         return LOAD_MODEL_INIT_FAIL;
       }
       gettimeofday(&t1, NULL);
@@ -112,7 +113,7 @@
   buf[0] = 0xC0;
   status = NVTAUX_NativeAuxAccess(NATIVE_AUX_WRITE, 0x00102, 1, buf);
   if (status != AUX_STATUS_OK) {
-    printf("Init TCON Fail(1.%d)\n", status);
+    warnx("Init TCON Fail(1.%d)", status);
     return TCON_ACC_STATUS_FAIL;
   }
   return TCON_ACC_STATUS_OK;
@@ -137,13 +138,13 @@
   //*******************************************
   status = NVTAUX_Open();
   if (status != AUX_STATUS_OK) {
-    printf("Open DP fail\n");
+    warnx("Open DP fail");
     NVTAUX_Close();
     return TCON_ACC_STATUS_FAIL;
   }
   TCON_Init();
   if (status != TCON_ACC_STATUS_OK) {
-    printf("Init TCON fail\n");
+    warnx("Init TCON fail");
     NVTAUX_Close();
     return TCON_ACC_STATUS_FAIL;
   }
@@ -195,7 +196,7 @@
       wBuf[j + 2] = m_GoldenBuf[i + j];
     status = NVTAUX_I2COverAuxWrite(slvAddr, one_wr_size + 2, wBuf);
     if (status != AUX_STATUS_OK) {
-      printf("\nWrite Fail\n");
+      warnx("\nWrite Fail");
       NVTAUX_Close();
       return TCON_ACC_STATUS_FAIL;
     }
@@ -206,15 +207,15 @@
     wBuf[1] = fw_i2c_addr;
     status = NVTAUX_I2COverAuxBurstRead(slvAddr, 2, wBuf, one_wr_size, rBuf);
     if (status != AUX_STATUS_OK) {
-      printf("\nWrite fail\n");
+      warnx("\nWrite fail");
       NVTAUX_Close();
       return TCON_ACC_STATUS_FAIL;
     }
 
     for (unsigned int j = 0; j < one_wr_size; j++) {
       if (m_GoldenBuf[i + j] != rBuf[j]) {
-        printf("\nWrite fail at address 0x%06X, write:%02X <=> read:%02X\n", i,
-               m_GoldenBuf[i], rBuf[j]);
+        warnx("\nWrite fail at address 0x%06X, write:%02X <=> read:%02X", i,
+              m_GoldenBuf[i], rBuf[j]);
         NVTAUX_Close();
         return TCON_ACC_STATUS_FAIL;
       }
@@ -247,14 +248,14 @@
     wBuf[1] = fw_i2c_addr;
     status = NVTAUX_I2COverAuxBurstRead(slvAddr, 2, wBuf, one_wr_size, rBuf);
     if (status != AUX_STATUS_OK) {
-      printf("\nVerify fail on reading\n");
+      warnx("\nVerify fail on reading");
       return TCON_ACC_STATUS_FAIL;
     }
 
     for (unsigned int j = 0; j < one_wr_size; j++) {
       if (m_GoldenBuf[i + j] != rBuf[j]) {
-        printf("\nVerify fail at address 0x%06X, write:%02X <=> read:%02X\n", i,
-               m_GoldenBuf[i], rBuf[j]);
+        warnx("\nVerify fail at address 0x%06X, write:%02X <=> read:%02X", i,
+              m_GoldenBuf[i], rBuf[j]);
         NVTAUX_Close();
         return TCON_ACC_STATUS_FAIL;
       }
@@ -285,18 +286,22 @@
 
   status = NVTAUX_Open();
   if (status != AUX_STATUS_OK) {
-    printf("Open DP fail\n");
+    warnx("Open DP fail");
     NVTAUX_Close();
     return TCON_ACC_STATUS_FAIL;
   }
   TCON_Init();
   if (status != TCON_ACC_STATUS_OK) {
-    printf("Init TCON fail\n");
+    warnx("Init TCON fail");
     NVTAUX_Close();
     return TCON_ACC_STATUS_FAIL;
   }
 
   FILE* fp = fopen("readBackRom.bin", "wb");
+  if (fp == NULL) {
+    warn("can't open readBackRom.bin");
+    return TCON_ACC_STATUS_FAIL;
+  }
 
   printf("Reading......\n");
   ProgressPrintf(0, buf_size);
@@ -318,13 +323,13 @@
 
     status = NVTAUX_I2COverAuxBurstRead(slvAddr, 2, wBuf, one_wr_size, rBuf);
     if (status != AUX_STATUS_OK) {
-      printf("Read Rom code fail\n");
+      warnx("Read Rom code fail");
       fclose(fp);
       NVTAUX_Close();
       return TCON_ACC_STATUS_FAIL;
     }
     if (fwrite(rBuf, one_wr_size, 1, fp) < 1) {
-      printf("Writing Rom code to file failed\n");
+      warnx("Writing Rom code to file failed");
       fclose(fp);
       NVTAUX_Close();
       return TCON_ACC_STATUS_FAIL;