Added mem_realloc and string functions

This commit is contained in:
Jozef Nagy 2025-01-25 23:28:33 +01:00
parent 56c522d05a
commit 12c9b4fdcc
Signed by untrusted user who does not match committer: crz
GPG key ID: 459A4811CEAC7068
10 changed files with 278 additions and 15 deletions

23
boot/common/init.c Normal file
View file

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

View file

@ -18,6 +18,7 @@
/*********************************************************************************/
#include <lib/string.h>
#include <mm/mman.h>
#include <stdint.h>
#include <stddef.h>
@ -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;

View file

@ -17,10 +17,49 @@
/* SOFTWARE. */
/*********************************************************************************/
#include <mem/mman.h>
#include <arch/mm/paging.h>
#include <mm/mman.h>
#include <lib/string.h>
#include <print.h>
#include <stddef.h>
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
#endif

View file

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

View file

@ -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 */

View file

@ -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 */

View file

@ -22,9 +22,18 @@
#include <stddef.h>
// 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 */

View file

@ -20,6 +20,7 @@
#ifndef _PRINT_H
#define _PRINT_H
#include "nanoprintf.h"
#include <stdarg.h>
#include <stdint.h>
#include <stdbool.h>
@ -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 */

View file

@ -58,7 +58,7 @@ _start:
cbnz w2, 3b
4:
bl main_menu
bl axboot_init
//
// crazy? i was crazy once.

View file

@ -20,7 +20,8 @@
#include <efi.h>
#include <efilib.h>
#include <mem/mman.h>
#include <axboot.h>
#include <mm/mman.h>
#include <lib/string.h>
#include <print.h>
@ -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();
}