diff --git a/boot/common/init.c b/boot/common/init.c new file mode 100644 index 0000000..8c50d47 --- /dev/null +++ b/boot/common/init.c @@ -0,0 +1,23 @@ +/*********************************************************************************/ +/* Module Name: init.c */ +/* Project: AurixOS */ +/* */ +/* Copyright (c) 2024-2025 Jozef Nagy */ +/* */ +/* This source is subject to the MIT License. */ +/* See License.txt in the root of this repository. */ +/* All other rights reserved. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR */ +/* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, */ +/* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE */ +/* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER */ +/* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, */ +/* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE */ +/* SOFTWARE. */ +/*********************************************************************************/ + +void axboot_init() +{ + while (1); +} \ No newline at end of file diff --git a/boot/common/lib/string.c b/boot/common/lib/string.c index 6eb5510..533806f 100644 --- a/boot/common/lib/string.c +++ b/boot/common/lib/string.c @@ -18,6 +18,7 @@ /*********************************************************************************/ #include +#include #include #include @@ -49,6 +50,46 @@ size_t mbstowcs(wchar_t *dest, const char **src, size_t len) return len - count; } +size_t strspn(const char *s, const char *accept) +{ + const char *p; + size_t count = 0; + + while (s != NULL) { + for (p = accept; *p; p++) { + if (*s == *p) { + count++; + break; + } + } + + if (!*p) { + break; + } + s++; + } + + return count; +} + +size_t strcspn(const char *s, const char *reject) +{ + const char *p; + size_t count = 0; + + while (*s) { + for (p = reject; *p; p++) { + if (*s == *p) { + return count; + } + } + s++; + count++; + } + + return count; +} + size_t strlen(const char *str) { size_t count = 0; @@ -64,6 +105,26 @@ size_t strlen(const char *str) return count; } +int strcmp(const char *s1, const char *s2) +{ + while (*s1 && (*s1 == *s2)) { + s1++; + s2++; + } + + return *(unsigned char *)s1 - *(unsigned char *)s2; +} + +int strncmp(const char *s1, const char *s2, size_t n) +{ + while (n-- && *s1 && (*s1 == *s2)) { + s1++; + s2++; + } + + return n ? (*(unsigned char *)s1 - *(unsigned char *)s2) : 0; +} + char *strcpy(char *dest, const char *src) { if (dest == NULL || src == NULL) { @@ -82,6 +143,80 @@ char *strcpy(char *dest, const char *src) return pdest; } +// TODO: Get rid of this function +char *strdup(const char *s) +{ + size_t len = strlen(s); + char *new = (char *)mem_alloc(len + 1); + + if (new) { + strcpy(new, s); + } + + return new; +} + +char *strtok(char *str, const char *delim) +{ + static char *last; + char *end; + + if (str == NULL) + { + str = last; + } + if (str == NULL) + { + return NULL; + } + + str += strspn(str, delim); + if (*str == '\0') + { + return NULL; + } + + end = str + strcspn(str, delim); + if (*end) + { + *end++ = '\0'; + } + + last = end; + return str; +} + +char *strchr(char *s, int c) +{ + if (s == NULL) { + return NULL; + } + + while (*s != 0) { + if (*s == c) + return (char *)s; + s++; + } + + return NULL; +} + +char *strrchr(char *s, int c) +{ + const char *last = NULL; + + if (s == NULL) { + return NULL; + } + + while (*s != 0) { + if (*s == (char)c) + last = s; + s++; + } + return (char *)last; +} + void *memset(void *dest, int val, size_t len) { unsigned char *ptr = dest; @@ -91,7 +226,7 @@ void *memset(void *dest, int val, size_t len) return dest; } -void *memcpy(void *dest, void *src, size_t len) +void *memcpy(void *dest, const void *src, size_t len) { char *d = (char *)dest; const char *s = (const char *)src; diff --git a/boot/common/mem/mman.c b/boot/common/mm/mman.c similarity index 65% rename from boot/common/mem/mman.c rename to boot/common/mm/mman.c index 8b3651f..4e1575a 100644 --- a/boot/common/mem/mman.c +++ b/boot/common/mm/mman.c @@ -17,10 +17,49 @@ /* SOFTWARE. */ /*********************************************************************************/ -#include +#include +#include +#include #include #include +struct alloc_header allocation_list[MAX_ALLOCATIONS] = {0}; + +int find_alloc(void *addr) +{ + for (int i = 0; i < MAX_ALLOCATIONS; i++) { + if (allocation_list[i].addr == addr) { + return i; + } + } + + return -1; +} + +int add_alloc(void *addr, size_t size) +{ + for (int i = 0; i < MAX_ALLOCATIONS; i++) { + if (allocation_list[i].addr == 0) { + allocation_list[i].addr = addr; + allocation_list[i].size = size; + return 1; + } + } + + return 0; +} + +void remove_alloc(void *addr) +{ + int i = find_alloc(addr); + if (i == -1) { + return; + } + + allocation_list[i].addr = 0; + allocation_list[i].size = 0; +} + #ifndef AXBOOT_UEFI #warning "Memory management is not implemented yet, expect runtime errors." @@ -40,7 +79,15 @@ int mem_allocat(void *addr, size_t npages) return 0; } -void mem_free(void **addr) +void *mem_realloc(void *addr, size_t n) +{ + (void)addr; + (void)n; + + return NULL; +} + +void mem_free(void *addr) { (void)addr; } @@ -61,6 +108,8 @@ void *mem_alloc(size_t n) return NULL; } + add_alloc(alloc, n); + return alloc; } @@ -75,10 +124,36 @@ int mem_allocat(void *addr, size_t npages) return 0; } + add_alloc(addr, npages * PAGE_SIZE); + return 1; } -void mem_free(void **addr) +void *mem_realloc(void *addr, size_t n) +{ + size_t old_size; + void *new = NULL; + + int i = find_alloc(addr); + if (i == -1) { + debug("mem_realloc(): Couldn't find allocation for 0x%lx.\n"); + return NULL; + } + + old_size = allocation_list[i].size; + + new = mem_alloc(n); + if (!new) { + return NULL; + } + + memcpy(new, addr, old_size); + mem_free(addr); + + return new; +} + +void mem_free(void *addr) { EFI_STATUS status; @@ -86,13 +161,13 @@ void mem_free(void **addr) return; } - status = gBootServices->FreePool(*addr); + status = gBootServices->FreePool(addr); if (EFI_ERROR(status)) { - debug("mem_free(): Couldn't free 0x%lx: %s (%lx)\n", *addr, efi_status_to_str(status), status); + debug("mem_free(): Couldn't free 0x%lx: %s (%lx)\n", addr, efi_status_to_str(status), status); return; } - *addr = NULL; + remove_alloc(addr); } -#endif \ No newline at end of file +#endif diff --git a/boot/common/print.c b/boot/common/print.c index 58b2bbe..359b4e5 100644 --- a/boot/common/print.c +++ b/boot/common/print.c @@ -66,3 +66,10 @@ void debug(const char *fmt, ...) #endif } +void snprintf(char *buf, size_t size, const char *fmt, ...) +{ + va_list args; + va_start(args, fmt); + npf_vsnprintf(buf, size, fmt, args); + va_end(args); +} \ No newline at end of file diff --git a/boot/include/axboot.h b/boot/include/axboot.h index 53deb39..bd2d491 100644 --- a/boot/include/axboot.h +++ b/boot/include/axboot.h @@ -26,7 +26,9 @@ #define BOOTLOADER_VERSION_STR "0.1" #ifndef UNREACHABLE - #define UNREACHABLE() + #define UNREACHABLE() __builtin_unreachable() #endif +void axboot_init(void); + #endif /* _AXBOOT_H */ \ No newline at end of file diff --git a/boot/include/lib/string.h b/boot/include/lib/string.h index 55ce907..7585234 100644 --- a/boot/include/lib/string.h +++ b/boot/include/lib/string.h @@ -24,11 +24,19 @@ size_t mbstowcs(wchar_t *dest, const char **src, size_t len); +size_t strspn(const char *s, const char *accept); +size_t strcspn(const char *s, const char *reject); size_t strlen(const char *str); +int strcmp(const char *s1, const char *s2); +int strncmp(const char *s1, const char *s2, size_t n); char *strcpy(char *dest, const char *src); +char *strdup(const char *s); +char *strtok(char *str, const char *delim); +char *strchr(char *s, int c); +char *strrchr(char *s, int c); void *memset(void *dest, int val, size_t len); -void *memcpy(void *dest, void *src, size_t len); +void *memcpy(void *dest, const void *src, size_t len); int memcmp(const void *a, const void *b, size_t len); #endif /* _LIB_STRING_H */ \ No newline at end of file diff --git a/boot/include/mem/mman.h b/boot/include/mm/mman.h similarity index 88% rename from boot/include/mem/mman.h rename to boot/include/mm/mman.h index 4879d92..10ed008 100644 --- a/boot/include/mem/mman.h +++ b/boot/include/mm/mman.h @@ -22,9 +22,18 @@ #include +// NOTE: If any allocations fail, try increasing this number. +#define MAX_ALLOCATIONS 256 + +struct alloc_header { + void *addr; + size_t size; +}; + void *mem_alloc(size_t n); int mem_allocat(void *addr, size_t npages); +void *mem_realloc(void *addr, size_t n); -void mem_free(void **addr); +void mem_free(void *addr); #endif /* _MEM_MMAN_H */ diff --git a/boot/include/print.h b/boot/include/print.h index dd0add0..751d7f7 100644 --- a/boot/include/print.h +++ b/boot/include/print.h @@ -20,6 +20,7 @@ #ifndef _PRINT_H #define _PRINT_H +#include "nanoprintf.h" #include #include #include @@ -29,4 +30,6 @@ void debug(const char *fmt, ...); void printstr(const char *str); +void snprintf(char *buf, size_t size, const char *fmt, ...); + #endif /* _PRINT_H */ \ No newline at end of file diff --git a/boot/platform/raspi4/boot.S b/boot/platform/raspi4/boot.S index 8b4f7bf..b0993ad 100644 --- a/boot/platform/raspi4/boot.S +++ b/boot/platform/raspi4/boot.S @@ -58,7 +58,7 @@ _start: cbnz w2, 3b 4: - bl main_menu + bl axboot_init // // crazy? i was crazy once. diff --git a/boot/platform/uefi/entry.c b/boot/platform/uefi/entry.c index b468b0a..78c495f 100644 --- a/boot/platform/uefi/entry.c +++ b/boot/platform/uefi/entry.c @@ -20,7 +20,8 @@ #include #include -#include +#include +#include #include #include @@ -48,6 +49,6 @@ EFI_STATUS uefi_entry(EFI_HANDLE ImageHandle, debug("Couldn't disable UEFI watchdog: %s (%x)\n", efi_status_to_str(Status), Status); } - while(1); - return EFI_SUCCESS; + axboot_init(); + UNREACHABLE(); } \ No newline at end of file