summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile7
-rw-r--r--include/common.h9
-rw-r--r--include/elf.h47
-rw-r--r--include/mem.h2
-rw-r--r--include/types.h25
-rw-r--r--kernel/bump.c4
-rw-r--r--kernel/dtb.c10
-rw-r--r--kernel/elf.c12
-rw-r--r--kernel/kernel.c14
-rw-r--r--kernel/mem.c12
10 files changed, 103 insertions, 39 deletions
diff --git a/Makefile b/Makefile
index 2de743c..cc021ca 100644
--- a/Makefile
+++ b/Makefile
@@ -3,9 +3,10 @@ LD = ld.lld
OBJCOPY = llvm-objcopy
ARCH_FLAG = \
- --target=riscv32-unknown-elf \
- -march=rv32im \
- -mabi=ilp32
+ --target=riscv32-unknown-elf \
+ -march=rv32im \
+ -mabi=ilp32 \
+ -D__SYS_LITTLE_ENDIAN__
COMMON_FLAGS = \
diff --git a/include/common.h b/include/common.h
index 86f1f9c..88ee8c4 100644
--- a/include/common.h
+++ b/include/common.h
@@ -16,4 +16,11 @@ static inline int strcmp(const char *str1, const char* str2) {
return *(const unsigned char *)str1 - *(const unsigned char *)str2;
}
-
+static inline void *memcpy(void *restrict dest, const void* restrict src, size_t size) {
+ unsigned char *d = (unsigned char *)dest;
+ unsigned char *s = (unsigned char *)src;
+ for (size_t i = 0; i < size; i++) {
+ d[i] = s[i];
+ }
+ return d;
+}
diff --git a/include/elf.h b/include/elf.h
new file mode 100644
index 0000000..ad8c550
--- /dev/null
+++ b/include/elf.h
@@ -0,0 +1,47 @@
+#pragma once
+#include <types.h>
+
+#define ELF_MAGIC_LE 0x464C457F
+#define ELF_PAD 0
+
+#define EI_MAG0 0
+#define EI_MAG1 1
+#define EI_MAG2 2
+#define EI_MAG3 3
+#define EI_CLASS 4
+#define EI_DATA 5
+#define EI_VERSION 6
+#define EI_OSABI 7
+#define EI_ABI_VERSION 8
+
+struct elf_header {
+ uint8_t e_ident[16];
+ uint8_t e_type;
+ uint8_t e_machine;
+ uint8_t e_version;
+ uint8_t e_entry;
+ uint8_t e_phoff;
+ uint8_t e_shoff;
+ uint8_t e_flags;
+ uint8_t e_ehsize;
+ uint8_t e_phentsize;
+ uint8_t e_phnum;
+ uint8_t e_shentsize;
+ uint8_t e_shnum;
+ uint8_t e_shstrndx;
+};
+
+struct elf_section_header {
+ uint32_t sh_name;
+ uint32_t sh_type;
+ uint32_t sh_flags;
+ uint32_t sh_addr;
+ uint32_t sh_offset;
+ uint32_t sh_size;
+ uint32_t sh_link;
+ uint32_t sh_info;
+ uint32_t sh_addralign;
+ uint32_t sh_entsize;
+};
+
+int elf_chk_header(struct elf_header *header);
diff --git a/include/mem.h b/include/mem.h
index edba547..bd6c911 100644
--- a/include/mem.h
+++ b/include/mem.h
@@ -20,7 +20,5 @@ void* kmalloc(size_t size);
void* kzalloc(size_t size);
void kfree(void* ptr);
-void* memcpy(void* to, const void* from, size_t size);
-
void km_init(struct mem_info *info);
diff --git a/include/types.h b/include/types.h
index 4c7965c..821e108 100644
--- a/include/types.h
+++ b/include/types.h
@@ -7,7 +7,30 @@
#define uint16_t __wk_uint16_t
#define uint32_t __wk_uint32_t
#define uint64_t __wk_uint64_t
-#define be32_to_cpu(x) __builtin_bswap32(x)
+
+#define htole
+
+#ifdef __SYS_LITTLE_ENDIAN__
+ #define ltohs(x) x
+ #define ltohi(x) x
+ #define htols(x) x
+ #define htoli(x) x
+
+ #define btohs(x) __builtin_bswap16(x)
+ #define btohi(x) __builtin_bswap32(x)
+ #define htobs(x) __builtin_bswap16(x)
+ #define htobi(x) __builtin_bswap32(x)
+#else
+ #define ltohs(x) __builtin_bswap16(x)
+ #define ltohi(x) __builtin_bswap32(x)
+ #define htols(x) __builtin_bswap16(x)
+ #define htoli(x) __builtin_bswap32(x)
+
+ #define btohs(x) x
+ #define btohi(x) x
+ #define htobs(x) x
+ #define htobi(x) x
+#endif
typedef __UINTPTR_TYPE__ __wk_size_t;
typedef __UINT8_TYPE__ __wk_uint8_t;
diff --git a/kernel/bump.c b/kernel/bump.c
index 369f92f..fd4f25d 100644
--- a/kernel/bump.c
+++ b/kernel/bump.c
@@ -19,8 +19,8 @@ struct mem_info *km_find_memory(struct dt_node *head) {
size_t len = 0;
uint32_t *reg = (uint32_t *)dt_get_prop(head, "reg", &len);
if (reg && len >= 8) {
- uint32_t base = be32_to_cpu(reg[0]);
- uint32_t size = be32_to_cpu(reg[1]);
+ uint32_t base = btohi(reg[0]);
+ uint32_t size = btohi(reg[1]);
struct mem_info *ret = kemalloc(sizeof(struct mem_info));
ret->start = (void*)base;
ret->size = (size_t)size;
diff --git a/kernel/dtb.c b/kernel/dtb.c
index 99b1679..7647767 100644
--- a/kernel/dtb.c
+++ b/kernel/dtb.c
@@ -10,15 +10,15 @@ struct dt_node *dt_parse(void *fdt) {
return NULL;
}
- uint32_t *p = (uint32_t *)(fdt + be32_to_cpu(hdr->off_dt_struct));
- char *strings = fdt + be32_to_cpu(hdr->off_dt_strings);
+ uint32_t *p = (uint32_t *)(fdt + btohi(hdr->off_dt_struct));
+ char *strings = fdt + btohi(hdr->off_dt_strings);
struct dt_node *root = NULL;
struct dt_node *stack[DTB_MAX_NODES];
int sp = 0;
while (1) {
- uint32_t token = be32_to_cpu(*p++);
+ uint32_t token = btohi(*p++);
switch (token) {
case FDT_BEGIN_NODE: {
char *name = (char *)p;
@@ -35,8 +35,8 @@ struct dt_node *dt_parse(void *fdt) {
break;
}
case FDT_PROP: {
- uint32_t len = be32_to_cpu(*p++);
- uint32_t nameoff = be32_to_cpu(*p++);
+ uint32_t len = btohi(*p++);
+ uint32_t nameoff = btohi(*p++);
void *value = p;
p += (len + 3) / 4;
diff --git a/kernel/elf.c b/kernel/elf.c
new file mode 100644
index 0000000..f1ceb2d
--- /dev/null
+++ b/kernel/elf.c
@@ -0,0 +1,12 @@
+#include <elf.h>
+#include <common.h>
+
+int elf_chk_header(struct elf_header *header) {
+ uint32_t magic_chk;
+ memcpy(&magic_chk, header->e_ident, sizeof(uint32_t));
+ if (magic_chk != ltohi(ELF_MAGIC_LE)) {
+ return -1;
+ }
+
+ return 0;
+}
diff --git a/kernel/kernel.c b/kernel/kernel.c
index 3952f8f..da44428 100644
--- a/kernel/kernel.c
+++ b/kernel/kernel.c
@@ -1,18 +1,6 @@
#include <types.h>
#include <dtb.h>
#include <bump.h>
-#define UART_BASE 0x10000000 // Example UART address
-
-static inline void putchar(char c) {
- volatile char *uart = (volatile char *)UART_BASE;
- *uart = c;
-}
-
-static void print_string(const char *str) {
- while (*str) {
- putchar(*str++);
- }
-}
void kernel_main(size_t hart, void *fdt) {
(void)hart;
@@ -21,11 +9,11 @@ void kernel_main(size_t hart, void *fdt) {
struct mem_info *info = km_find_memory(head);
if (!info) {
- print_string("failed to get memory!\n");
while (1);
}
km_init(info);
+
while(1);
}
diff --git a/kernel/mem.c b/kernel/mem.c
index 0556dfb..2a6bd35 100644
--- a/kernel/mem.c
+++ b/kernel/mem.c
@@ -106,18 +106,6 @@ void kfree(void *ptr) {
}
-
-void* memcpy(void *to, const void *from, size_t size) {
- char *dest = (char *)to;
- const char *src = (const char *)from;
-
- while (size--) {
- *dest++ = *src++;
- }
-
- return dest;
-}
-
void km_init(struct mem_info *info) {
struct page *base = (struct page *)info->start;
base->next = NULL;