diff --git a/kernel/inc/utils/memory.h b/kernel/inc/utils/memory.h index 804b7dd..4e22ec1 100644 --- a/kernel/inc/utils/memory.h +++ b/kernel/inc/utils/memory.h @@ -32,5 +32,6 @@ bool memory_compare (void* a, void* b, uint32_t num); void* memory_allocate (uint64_t size); void memory_free (void* address); void memory_allocator_init (void* base); +void memory_hexdump (uint8_t* address, uint64_t num); #endif //NOX_MEMORY_H diff --git a/kernel/inc/utils/string.h b/kernel/inc/utils/string.h index 805ce4d..6d2560a 100644 --- a/kernel/inc/utils/string.h +++ b/kernel/inc/utils/string.h @@ -48,4 +48,6 @@ void string_hex_64bit_to_alpha (string_t string, uint64_t value); void string_bin_to_alpha (string_t string, uint8_t num_bits, uint64_t value); void string_bool_to_alpha (string_t string, bool value); +bool string_is_char_alpha (char chr); + #endif //NOX_STRING_H diff --git a/kernel/src/utils/memory.c b/kernel/src/utils/memory.c index b060dde..26ba79a 100644 --- a/kernel/src/utils/memory.c +++ b/kernel/src/utils/memory.c @@ -22,6 +22,7 @@ */ #include "utils/memory.h" +#include "utils/logger.h" #include "mm/heap.h" heap_T g_kernel_heap; @@ -99,4 +100,78 @@ void memory_allocator_init(void* base) { if (g_kernel_heap.start != NULL) { return; } heap_init(&g_kernel_heap, base); +} + +void memory_hexdump(uint8_t* address, uint64_t num) { + uint64_t rows = num / 8; + uint8_t remainder = num % 8; + + for (uint64_t i = 0; i < rows; i++) { + char buffer[10] = ".... ...."; + for (uint8_t j = 0; j < 8; j++) { + if (string_is_char_alpha((char)address[j])) { + buffer[(j < 4) ? j : (j + 1)] = (char)address[j]; + } + } + + log(LOG_NONE, " 0x%x -> %xb %xb %xb %xb %xb %xb %xb %xb [%s]", + address, address[0], address[1], address[2], address[3], address[4], address[5], address[6], address[7], buffer); + + address += 8; + } + + + char buffer[10] = " "; + for (uint8_t j = 0; j < remainder; j++) { + if (string_is_char_alpha((char)address[j])) { + buffer[(j < 4) ? j : (j + 1)] = (char)address[j]; + } else { + buffer[(j < 4) ? j : (j + 1)] = '.'; + } + } + + // this is not a beautiful solution, but it should be easy to understand / maintain + switch (remainder) { + case 0: { + break; + } + case 1: { + log(LOG_NONE, " 0x%x -> %xb [%s]", + address, address[0], buffer); + break; + } + case 2: { + log(LOG_NONE, " 0x%x -> %xb %xb [%s]", + address, address[0], address[1], buffer); + break; + } + case 3: { + log(LOG_NONE, " 0x%x -> %xb %xb %xb [%s]", + address, address[0], address[1], address[2], buffer); + break; + } + case 4: { + log(LOG_NONE, " 0x%x -> %xb %xb %xb %xb [%s]", + address, address[0], address[1], address[2], address[3], buffer); + break; + } + case 5: { + log(LOG_NONE, " 0x%x -> %xb %xb %xb %xb %xb [%s]", + address, address[0], address[1], address[2], address[3], address[4], buffer); + break; + } + case 6: { + log(LOG_NONE, " 0x%x -> %xb %xb %xb %xb %xb %xb [%s]", + address, address[0], address[1], address[2], address[3], address[4], address[5], buffer); + break; + } + case 7: { + log(LOG_NONE, " 0x%x -> %xb %xb %xb %xb %xb %xb %xb [%s]", + address, address[0], address[1], address[2], address[3], address[4], address[5], address[6], buffer); + break; + } + default: { + break; + } + } } \ No newline at end of file diff --git a/kernel/src/utils/string.c b/kernel/src/utils/string.c index 186bcad..2ad731c 100644 --- a/kernel/src/utils/string.c +++ b/kernel/src/utils/string.c @@ -421,4 +421,11 @@ void string_bin_to_alpha(string_t string, uint8_t num_bits, uint64_t value) { void string_bool_to_alpha(string_t string, bool value) { memory_copy(value ? "true" : "false", (void*)string, value ? 5 : 6); +} + +bool string_is_char_alpha(char chr) { + if (chr >= '!' && chr <= '~') { + return true; + } + return false; } \ No newline at end of file