[Replicant] [libsamsung-ipc][PATCH] file_data_{read, write}: Add logging

Denis 'GNUtoo' Carikli GNUtoo at cyberdimension.org
Thu Jan 23 18:43:18 UTC 2020


Signed-off-by: Denis 'GNUtoo' Carikli <GNUtoo at cyberdimension.org>
---
 include/samsung-ipc.h               |  9 ++--
 samsung-ipc/devices/aries/aries.c   |  2 +-
 samsung-ipc/devices/crespo/crespo.c |  2 +-
 samsung-ipc/devices/maguro/maguro.c |  2 +-
 samsung-ipc/rfs.c                   | 28 +++++------
 samsung-ipc/utils.c                 | 76 ++++++++++++++++++++++-------
 6 files changed, 81 insertions(+), 38 deletions(-)

diff --git a/include/samsung-ipc.h b/include/samsung-ipc.h
index d495ba2..5fc2c76 100644
--- a/include/samsung-ipc.h
+++ b/include/samsung-ipc.h
@@ -135,10 +135,11 @@ int ipc_rfs_header_setup(struct ipc_rfs_header *header,
 int ipc_rfs_message_setup(const struct ipc_rfs_header *header,
     struct ipc_message *message);
 
-void *file_data_read(const char *path, size_t size, size_t chunk_size,
-    unsigned int offset);
-int file_data_write(const char *path, const void *data, size_t size,
-    size_t chunk_size, unsigned int offset);
+void *file_data_read(struct ipc_client *client, const char *path, size_t size,
+                     size_t chunk_size, unsigned int offset);
+int file_data_write(struct ipc_client *client, const char *path,
+                    const void *data, size_t size, size_t chunk_size,
+                    unsigned int offset);
 int network_iface_up(const char *iface, int domain, int type);
 int network_iface_down(const char *iface, int domain, int type);
 int sysfs_value_read(const char *path);
diff --git a/samsung-ipc/devices/aries/aries.c b/samsung-ipc/devices/aries/aries.c
index a3da208..e41a4e2 100644
--- a/samsung-ipc/devices/aries/aries.c
+++ b/samsung-ipc/devices/aries/aries.c
@@ -62,7 +62,7 @@ int aries_boot(struct ipc_client *client)
 
     ipc_client_log(client, "Starting aries modem boot");
 
-    modem_image_data = file_data_read(ARIES_MODEM_IMAGE_DEVICE, ARIES_MODEM_IMAGE_SIZE, 0x1000, 0);
+    modem_image_data = file_data_read(client, ARIES_MODEM_IMAGE_DEVICE, ARIES_MODEM_IMAGE_SIZE, 0x1000, 0);
     if (modem_image_data == NULL) {
         ipc_client_log(client, "Reading modem image data failed");
         goto error;
diff --git a/samsung-ipc/devices/crespo/crespo.c b/samsung-ipc/devices/crespo/crespo.c
index 1bf9a97..612ce5d 100644
--- a/samsung-ipc/devices/crespo/crespo.c
+++ b/samsung-ipc/devices/crespo/crespo.c
@@ -50,7 +50,7 @@ int crespo_boot(struct ipc_client *client)
 
     ipc_client_log(client, "Starting crespo modem boot");
 
-    modem_image_data = file_data_read(CRESPO_MODEM_IMAGE_DEVICE, CRESPO_MODEM_IMAGE_SIZE, 0x1000, 0);
+    modem_image_data = file_data_read(client, CRESPO_MODEM_IMAGE_DEVICE, CRESPO_MODEM_IMAGE_SIZE, 0x1000, 0);
     if (modem_image_data == NULL) {
         ipc_client_log(client, "Reading modem image data failed");
         goto error;
diff --git a/samsung-ipc/devices/maguro/maguro.c b/samsung-ipc/devices/maguro/maguro.c
index b41eadd..76eac54 100644
--- a/samsung-ipc/devices/maguro/maguro.c
+++ b/samsung-ipc/devices/maguro/maguro.c
@@ -140,7 +140,7 @@ int maguro_boot(struct ipc_client *client)
     }
     ipc_client_log(client, "Sent XMM626 MIPI nv_data");
 
-    mps_data = file_data_read(MAGURO_MPS_DATA_DEVICE, MAGURO_MPS_DATA_SIZE, MAGURO_MPS_DATA_SIZE, 0);
+    mps_data = file_data_read(client, MAGURO_MPS_DATA_DEVICE, MAGURO_MPS_DATA_SIZE, MAGURO_MPS_DATA_SIZE, 0);
     if (mps_data == NULL) {
         ipc_client_log(client, "Reading MPS data failed");
         goto error;
diff --git a/samsung-ipc/rfs.c b/samsung-ipc/rfs.c
index 91ff2c0..1bf986f 100644
--- a/samsung-ipc/rfs.c
+++ b/samsung-ipc/rfs.c
@@ -46,7 +46,7 @@ char *ipc_nv_data_md5_calculate(struct ipc_client *client,
 	    return NULL;
     }
 
-    data = file_data_read(path, size, chunk_size, 0);
+    data = file_data_read(client, path, size, chunk_size, 0);
     if (data == NULL) {
 	    ipc_client_log(client, "%s failed: data is NULL",  __FUNCTION__);
 	    return NULL;
@@ -227,7 +227,7 @@ int ipc_nv_data_check(struct ipc_client *client)
 
     length = strlen(md5_string);
 
-    buffer = file_data_read(md5_path, length, length, 0);
+    buffer = file_data_read(client, md5_path, length, length, 0);
     if (buffer == NULL) {
         ipc_client_log(client, "Reading nv_data md5 failed");
         goto error;
@@ -306,7 +306,7 @@ int ipc_nv_data_backup_check(struct ipc_client *client)
 
     length = strlen(backup_md5_string);
 
-    buffer = file_data_read(backup_md5_path, length, length, 0);
+    buffer = file_data_read(client, backup_md5_path, length, length, 0);
     if (buffer == NULL) {
         ipc_client_log(client, "Reading nv_data backup md5 failed");
         goto error;
@@ -371,7 +371,7 @@ int ipc_nv_data_backup(struct ipc_client *client)
         goto error;
     }
 
-    data = file_data_read(path, size, chunk_size, 0);
+    data = file_data_read(client, path, size, chunk_size, 0);
     if (data == NULL) {
         ipc_client_log(client, "Reading nv_data failed");
         goto error;
@@ -389,7 +389,7 @@ int ipc_nv_data_backup(struct ipc_client *client)
     if (rc < 0)
         ipc_client_log(client, "Removing nv_data backup path failed");
 
-    rc = file_data_write(backup_path, data, size, chunk_size, 0);
+    rc = file_data_write(client, backup_path, data, size, chunk_size, 0);
     if (rc < 0) {
         ipc_client_log(client, "Writing nv_data backup failed");
         goto error;
@@ -399,7 +399,7 @@ int ipc_nv_data_backup(struct ipc_client *client)
     if (rc < 0)
         ipc_client_log(client, "Removing nv_data backup md5 path failed");
 
-    rc = file_data_write(backup_md5_path, md5_string, length, length, 0);
+    rc = file_data_write(client, backup_md5_path, md5_string, length, length, 0);
     if (rc < 0) {
         ipc_client_log(client, "Writing nv_data backup md5 failed");
         goto error;
@@ -455,7 +455,7 @@ int ipc_nv_data_restore(struct ipc_client *client)
         goto error;
     }
 
-    data = file_data_read(backup_path, size, chunk_size, 0);
+    data = file_data_read(client, backup_path, size, chunk_size, 0);
     if (data == NULL) {
         ipc_client_log(client, "Reading nv_data backup failed");
         goto error;
@@ -465,7 +465,7 @@ int ipc_nv_data_restore(struct ipc_client *client)
     if (rc < 0)
         ipc_client_log(client, "Removing nv_data path failed");
 
-    rc = file_data_write(path, data, size, chunk_size, 0);
+    rc = file_data_write(client, path, data, size, chunk_size, 0);
     if (rc < 0) {
         ipc_client_log(client, "Writing nv_data failed");
         goto error;
@@ -476,7 +476,7 @@ int ipc_nv_data_restore(struct ipc_client *client)
 
     length = 2 * sizeof(char) * MD5_DIGEST_LENGTH;
 
-    data = file_data_read(backup_md5_path, length, length, 0);
+    data = file_data_read(client, backup_md5_path, length, length, 0);
     if (data == NULL) {
         ipc_client_log(client, "Reading nv_data backup md5 failed");
         goto error;
@@ -486,7 +486,7 @@ int ipc_nv_data_restore(struct ipc_client *client)
     if (rc < 0)
         ipc_client_log(client, "Removing nv_data md5 path failed");
 
-    rc = file_data_write(md5_path, data, length, length, 0);
+    rc = file_data_write(client, md5_path, data, length, length, 0);
     if (rc < 0) {
         ipc_client_log(client, "Writing nv_data md5 failed");
         goto error;
@@ -550,7 +550,7 @@ void *ipc_nv_data_load(struct ipc_client *client)
             ipc_client_log(client, "Backing up nv_data failed");
     }
 
-    data = file_data_read(path, size, chunk_size, 0);
+    data = file_data_read(client, path, size, chunk_size, 0);
     if (data == NULL) {
         ipc_client_log(client, "Reading nv_data failed");
         return NULL;
@@ -581,7 +581,7 @@ void *ipc_nv_data_read(struct ipc_client *client, size_t size,
         return NULL;
     }
 
-    data = file_data_read(path, size, chunk_size > size ? size : chunk_size, offset);
+    data = file_data_read(client, path, size, chunk_size > size ? size : chunk_size, offset);
     if (data == NULL) {
         ipc_client_log(client, "Reading nv_data failed");
         return NULL;
@@ -617,7 +617,7 @@ int ipc_nv_data_write(struct ipc_client *client, const void *data, size_t size,
         goto error;
     }
 
-    rc = file_data_write(path, data, size, chunk_size > size ? size : chunk_size, offset);
+    rc = file_data_write(client, path, data, size, chunk_size > size ? size : chunk_size, offset);
     if (rc < 0) {
         ipc_client_log(client, "Writing nv_data failed");
         goto error;
@@ -641,7 +641,7 @@ int ipc_nv_data_write(struct ipc_client *client, const void *data, size_t size,
         goto error;
     }
 
-    rc = file_data_write(md5_path, md5_string, length, length, 0);
+    rc = file_data_write(client, md5_path, md5_string, length, length, 0);
     if (rc < 0) {
         ipc_client_log(client, "Writing nv_data md5 failed");
         goto error;
diff --git a/samsung-ipc/utils.c b/samsung-ipc/utils.c
index 756d191..1bba874 100644
--- a/samsung-ipc/utils.c
+++ b/samsung-ipc/utils.c
@@ -32,9 +32,10 @@
 #include <linux/netlink.h>
 #include <net/if.h>
 
+#include <samsung-ipc.h>
 
-void *file_data_read(const char *path, size_t size, size_t chunk_size,
-    unsigned int offset)
+void *file_data_read(struct ipc_client *client, const char *path, size_t size,
+		     size_t chunk_size, unsigned int offset)
 {
     void *data = NULL;
     int fd = -1;
@@ -43,16 +44,34 @@ void *file_data_read(const char *path, size_t size, size_t chunk_size,
     unsigned char *p;
     int rc;
 
-    if (path == NULL || size == 0 || chunk_size == 0 || chunk_size > size)
-        return NULL;
+    if (path == NULL || size == 0 || chunk_size == 0 || chunk_size > size) {
+	    if (path == NULL) {
+		    ipc_client_log(client, "%s: Failed: path is NULL", __FUNCTION__);
+	    }
+	    if (size == 0) {
+		    ipc_client_log(client, "%s: Failed: size is 0", __FUNCTION__);
+	    }
+	    if (chunk_size == 0) {
+		    ipc_client_log(client, "%s: Failed: chunk_size is 0", __FUNCTION__);
+	    }
+	    if (chunk_size > size) {
+		    ipc_client_log(client, "%s: Failed: chunk_size > size ", __FUNCTION__);
+	    }
+
+	    return NULL;
+    }
 
     fd = open(path, O_RDONLY);
-    if (fd < 0)
-        goto error;
+    if (fd < 0) {
+	    ipc_client_log(client, "%s: Error: fd: %d ", __FUNCTION__, fd);
+	    goto error;
+    }
 
     seek = lseek(fd, (off_t) offset, SEEK_SET);
-    if (seek < (off_t) offset)
-        goto error;
+    if (seek < (off_t) offset) {
+	    ipc_client_log(client, "%s: Error: seek < (off_t) offset", __FUNCTION__);
+	    goto error;
+    }
 
     data = calloc(1, size);
 
@@ -61,8 +80,10 @@ void *file_data_read(const char *path, size_t size, size_t chunk_size,
     count = 0;
     while (count < size) {
         rc = read(fd, p, size - count > chunk_size ? chunk_size : size - count);
-        if (rc <= 0)
-            goto error;
+        if (rc <= 0) {
+		ipc_client_log(client, "%s: Error: rc < 0", __FUNCTION__);
+		goto error;
+	}
 
         p += rc;
         count += rc;
@@ -83,8 +104,9 @@ complete:
     return data;
 }
 
-int file_data_write(const char *path, const void *data, size_t size,
-    size_t chunk_size, unsigned int offset)
+int file_data_write(struct ipc_client *client, const char *path,
+                    const void *data, size_t size, size_t chunk_size,
+                    unsigned int offset)
 {
     int fd = -1;
     size_t count;
@@ -92,24 +114,44 @@ int file_data_write(const char *path, const void *data, size_t size,
     unsigned char *p;
     int rc;
 
-    if (path == NULL || data == NULL || size == 0 || chunk_size == 0 || chunk_size > size)
+    if (path == NULL || data == NULL || size == 0 || chunk_size == 0 || chunk_size > size) {
+        if (path == NULL) {
+            ipc_client_log(client, "%s failed: path is NULL", __FUNCTION__);
+        }
+        if (size == 0) {
+            ipc_client_log(client, "%s failed: size is 0", __FUNCTION__);
+        }
+        if (chunk_size == 0) {
+            ipc_client_log(client, "%s failed: chunk_size is 0", __FUNCTION__);
+        }
+        if (chunk_size > size) {
+            ipc_client_log(client, "%s failed: chunk_size > size",
+                           __FUNCTION__);
+        }
         return -1;
+    }
 
     fd = open(path, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
-    if (fd < 0)
+    if (fd < 0) {
+        ipc_client_log(client, "%s: open failed with error %d", __FUNCTION__, fd);
         goto error;
+    }
 
     seek = lseek(fd, (off_t) offset, SEEK_SET);
-    if (seek < (off_t) offset)
+    if (seek < (off_t) offset) {
+        ipc_client_log(client, "%s failed: seek < (off_t) offset", __FUNCTION__);
         goto error;
+    }
 
     p = (unsigned char *) data;
 
     count = 0;
     while (count < size) {
         rc = write(fd, p, size - count > chunk_size ? chunk_size : size - count);
-        if (rc <= 0)
-            goto error;
+        if (rc <= 0) {
+                ipc_client_log(client, "%s: write failed with error %d", __FUNCTION__, rc);
+                goto error;
+        }
 
         p += rc;
         count += rc;
-- 
2.24.1



More information about the Replicant mailing list