Procházet zdrojové kódy

realtek: rt-loader: memory library enhancements

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]>
Markus Stockhausen před 6 měsíci
rodič
revize
908cda6943

+ 3 - 2
target/linux/realtek/image/rt-loader/include/memory.h

@@ -15,14 +15,15 @@
 #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;

+ 26 - 9
target/linux/realtek/image/rt-loader/src/memory.c

@@ -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;
 }