From 54faa3892053848e244b2ccb5346283e701ac08b Mon Sep 17 00:00:00 2001 From: CTCaer Date: Thu, 30 Apr 2020 03:30:51 +0300 Subject: [PATCH] utils: Add normal CRC32 --- bootloader/utils/util.c | 38 +++++++++++++++++++++++++++++++++++++ bootloader/utils/util.h | 1 + nyx/nyx_gui/utils/util.c | 41 ++++++++++++++++++++++++++++++++++++++-- nyx/nyx_gui/utils/util.h | 1 + 4 files changed, 79 insertions(+), 2 deletions(-) diff --git a/bootloader/utils/util.c b/bootloader/utils/util.c index 9bf56ab..a6455fe 100644 --- a/bootloader/utils/util.c +++ b/bootloader/utils/util.c @@ -17,6 +17,7 @@ #include "util.h" #include "../gfx/di.h" +#include "../mem/heap.h" #include "../mem/minerva.h" #include "../power/max77620.h" #include "../rtc/max77620-rtc.h" @@ -81,6 +82,43 @@ void exec_cfg(u32 *base, const cfg_op_t *ops, u32 num_ops) base[ops[i].off] = ops[i].val; } +u32 crc32_calc(u32 crc, const u8 *buf, u32 len) +{ + const u8 *p, *q; + static u32 *table = NULL; + + // Calculate CRC table. + if (!table) + { + table = calloc(256, sizeof(u32)); + for (u32 i = 0; i < 256; i++) + { + u32 rem = i; + for (u32 j = 0; j < 8; j++) + { + if (rem & 1) + { + rem >>= 1; + rem ^= 0xedb88320; + } + else + rem >>= 1; + } + table[i] = rem; + } + } + + crc = ~crc; + q = buf + len; + for (p = buf; p < q; p++) + { + u8 oct = *p; + crc = (crc >> 8) ^ table[(crc & 0xff) ^ oct]; + } + + return ~crc; +} + void panic(u32 val) { // Set panic code. diff --git a/bootloader/utils/util.h b/bootloader/utils/util.h index 5c0fecd..d1b8634 100644 --- a/bootloader/utils/util.h +++ b/bootloader/utils/util.h @@ -71,5 +71,6 @@ void reboot_normal(); void reboot_rcm(); void power_off(); void exec_cfg(u32 *base, const cfg_op_t *ops, u32 num_ops); +u32 crc32_calc(u32 crc, const u8 *buf, u32 len); #endif diff --git a/nyx/nyx_gui/utils/util.c b/nyx/nyx_gui/utils/util.c index ba0b753..a7e2b1f 100644 --- a/nyx/nyx_gui/utils/util.c +++ b/nyx/nyx_gui/utils/util.c @@ -17,6 +17,7 @@ #include "util.h" #include "../gfx/di.h" +#include "../mem/heap.h" #include "../mem/minerva.h" #include "../power/max77620.h" #include "../rtc/max77620-rtc.h" @@ -24,13 +25,12 @@ #include "../soc/i2c.h" #include "../soc/pmc.h" #include "../soc/t210.h" +#include "../storage/nx_sd.h" #define USE_RTC_TIMER extern volatile nyx_storage_t *nyx_str; -extern void sd_unmount(bool deinit); - u32 get_tmr_s() { return RTC(APBDEV_RTC_SECONDS); @@ -82,6 +82,43 @@ void exec_cfg(u32 *base, const cfg_op_t *ops, u32 num_ops) base[ops[i].off] = ops[i].val; } +u32 crc32_calc(u32 crc, const u8 *buf, u32 len) +{ + const u8 *p, *q; + static u32 *table = NULL; + + // Calculate CRC table. + if (!table) + { + table = calloc(256, sizeof(u32)); + for (u32 i = 0; i < 256; i++) + { + u32 rem = i; + for (u32 j = 0; j < 8; j++) + { + if (rem & 1) + { + rem >>= 1; + rem ^= 0xedb88320; + } + else + rem >>= 1; + } + table[i] = rem; + } + } + + crc = ~crc; + q = buf + len; + for (p = buf; p < q; p++) + { + u8 oct = *p; + crc = (crc >> 8) ^ table[(crc & 0xff) ^ oct]; + } + + return ~crc; +} + void panic(u32 val) { // Set panic code. diff --git a/nyx/nyx_gui/utils/util.h b/nyx/nyx_gui/utils/util.h index 5c0fecd..d1b8634 100644 --- a/nyx/nyx_gui/utils/util.h +++ b/nyx/nyx_gui/utils/util.h @@ -71,5 +71,6 @@ void reboot_normal(); void reboot_rcm(); void power_off(); void exec_cfg(u32 *base, const cfg_op_t *ops, u32 num_ops); +u32 crc32_calc(u32 crc, const u8 *buf, u32 len); #endif