Forráskód Böngészése

Добавил файлы

Anatoliy Chigirinskiy 1 éve
szülő
commit
26081e14a4
2 módosított fájl, 105 hozzáadás és 0 törlés
  1. 97 0
      DmaUtils/utils.c
  2. 8 0
      DmaUtils/utils.h

+ 97 - 0
DmaUtils/utils.c

@@ -0,0 +1,97 @@
+#include <stdio.h>
+#include <stdint.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <errno.h>
+
+#include "utils.h"
+
+
+ssize_t read_from_buffer(char *fname, int fd, char *buffer, uint64_t size, uint64_t base) {
+    ssize_t rc;
+    char *buf = buffer;
+    off_t offset = base;
+    size_t count = 0;
+    int loop = 0;
+
+    while (count < size) {
+        uint64_t bytes = size - count;
+
+        if (bytes > RW_MAX_SIZE) {
+            bytes = RW_MAX_SIZE;
+        }
+        if (offset) {
+            rc = lseek(fd, offset, SEEK_SET);
+            if (rc != offset) {
+                fprintf(stderr, "%s, seek off 0x%lx != 0x%lx\n", fname, rc, offset);
+                perror("seek file");
+                return -EIO;
+            }
+        }
+        /* Read data from file into buffer */
+        rc = read(fd, buf, bytes);
+        if (rc < 0) {
+            fprintf(stderr, "%s, read failed\n", fname);
+            perror("read file");
+            return -EIO;
+        }
+        count += rc;
+        if (rc != bytes) {
+            fprintf(stderr, "%s, read failed, rc=%ld, bytes=%ld\n", fname, rc, bytes);
+            break;
+        }
+        buf += bytes;
+        offset += bytes;
+        loop++;
+    }
+    if (count != size && loop) {
+        fprintf(stderr, "%s, read failed, count=%ld, size=%ld\n", fname, count, size);
+        return -EIO;
+    }
+    return count;
+}
+
+ssize_t write_to_buffer(char *fname, int fd, char *buffer, uint64_t size, uint64_t base) {
+    ssize_t rc;
+    char *buf = buffer;
+    off_t offset = base;
+    size_t count = 0;
+    int loop = 0;
+
+    while (count < size) {
+        uint64_t bytes = size - count;
+
+        if (bytes > RW_MAX_SIZE) {
+            bytes = RW_MAX_SIZE;
+        }
+        if (offset) {
+            rc = lseek(fd, offset, SEEK_SET);
+            if (rc != offset) {
+                fprintf(stderr, "%s, seek off 0x%lx != 0x%lx\n", fname, rc, offset);
+                perror("seek file");
+                return -EIO;
+            }
+        }
+        /* Write data from buffer into file */
+        rc = write(fd, buf, bytes);
+        if (rc < 0) {
+            fprintf(stderr, "%s,write 0x%lx @ 0x%lx @ 0x%lx failed %ld.\n", fname, bytes, offset, rc);
+            perror("write file");
+            return -EIO;
+        }
+        count += rc;
+        printf("count = %ld, bytes = %ld\n", count, size);
+        if (rc != bytes) {
+            fprintf(stderr, "%s, write underflow 0x%lx/0x%lx @ 0x%lx.\n", fname, rc, bytes, offset);
+            break;
+        }
+        buf += bytes;
+        offset += bytes;
+        loop++;
+    }
+    if (count != size && loop) {
+        fprintf(stderr, "%s, write underflow, 0x%lx/0x%lx.\n", fname, count, size);
+        return -EIO;
+    }
+    return count;
+}

+ 8 - 0
DmaUtils/utils.h

@@ -0,0 +1,8 @@
+#define RW_MAX_SIZE    0x7ffff000
+
+
+
+
+
+ssize_t read_from_buffer(char *fname, int fd, char *buffer, uint64_t size, uint64_t base);
+ssize_t write_to_buffer(char *fname, int fd, char *buffer, uint64_t size, uint64_t base);