realtek: rt-loader: memory library enhancements
authorMarkus Stockhausen <[email protected]>
Fri, 22 Aug 2025 08:41:29 +0000 (04:41 -0400)
committerHauke Mehrtens <[email protected]>
Wed, 3 Sep 2025 19:36:34 +0000 (21:36 +0200)
Provide a crc32 function (will be needed later). Do some
minor naming and coding cleanups

Signed-off-by: Markus Stockhausen <[email protected]>
Link: https://github.com/openwrt/openwrt/pull/19832
Signed-off-by: Hauke Mehrtens <[email protected]>
target/linux/realtek/image/rt-loader/include/memory.h
target/linux/realtek/image/rt-loader/src/memory.c

index 80d0f8a2835c44189a22a4b5903a3696f4a157f1..a43c83219ee1bca7861f14b2dce402552012fed9 100644 (file)
 #define ioread32(reg)                  (*(volatile int *)(reg))
 #define iowrite32(val, reg)            (*(volatile int *)(reg) = val)
 
-void flush_cache(void *start_addr, unsigned long size);
+void flush_cache(void *start_addr, size_t count);
 void free(void *ptr);
-void *malloc(size_t size);
+void *malloc(size_t count);
 int memcmp(const void *s1, const void *s2, size_t count);
 void *memmove(void *dst, const void *src, size_t count);
 void *memcpy(void *dst, const void *src, size_t count);
 void *memset(void *dst, int value, size_t count);
 size_t strlen(const char *s);
+unsigned int crc32(void *m, size_t count);
 
 extern void *_heap_addr;
 extern void *_heap_addr_max;
index 6ff5a448974c42f60da6118f49e8aa776446a68e..2e16d8afaa714477386197ea427342836b73a00c 100644 (file)
@@ -21,7 +21,7 @@
        :                                       \
        : "i" (op), "R" (*(unsigned char *)(addr)))
 
-void flush_cache(void *start_addr, unsigned long size)
+void flush_cache(void *start_addr, size_t count)
 {
        /*
         * MIPS cores may have different cache lines. Most common are 16 and 32 bytes. Avoid
@@ -31,7 +31,7 @@ void flush_cache(void *start_addr, unsigned long size)
 
        unsigned long lsize = 16;
        unsigned long addr = (unsigned long)start_addr & ~(lsize - 1);
-       unsigned long aend = ((unsigned long)start_addr + size - 1) & ~(lsize - 1);
+       unsigned long aend = ((unsigned long)start_addr + count - 1) & ~(lsize - 1);
 
        while (1) {
                CACHE_OP(CACHE_HIT_INVALIDATE_I, addr);
@@ -96,27 +96,44 @@ void *memset(void *dst, int c, size_t count)
        return (void *)d;
 }
 
-void *malloc(size_t size)
+void *malloc(size_t count)
 {
        void *start;
 
        start = (void *)(((unsigned int)_heap_addr + MEMORY_ALIGNMENT - 1) & ~(MEMORY_ALIGNMENT - 1));
-       if ((start + size) > _heap_addr_max) {
+       if ((start + count) > _heap_addr_max) {
                printf("malloc(%d) failed. Only %dkB of %dkB heap left.\n",
-                      size, (_heap_addr_max - start) >> 10, HEAP_SIZE >> 10);
+                      count, (_heap_addr_max - start) >> 10, HEAP_SIZE >> 10);
                board_panic();
        }
 
-       _heap_addr += size;
+       _heap_addr += count;
 
        return start;
 }
 
 size_t strlen(const char *s)
 {
-       const char *p = s;
+       size_t len = 0;
 
-       while (*p) ++p;
+       while (s[len]) len++;
 
-       return (size_t)(p - s);
+       return len;
+}
+
+unsigned int crc32(void *m, size_t count)
+{
+       unsigned int crc = 0xffffffff;
+       unsigned char *data = m;
+
+       for (size_t i = 0; i < count; i++) {
+               crc ^= data[i];
+               for (int j = 0; j < 8; j++)
+                       if (crc & 1)
+                               crc = (crc >> 1) ^ 0xEDB88320;
+                       else
+                               crc >>= 1;
+       }
+
+       return ~crc;
 }