summaryrefslogtreecommitdiff
path: root/include/sys
diff options
context:
space:
mode:
authorNathan Lee <me@nwlee.tech>2026-08-12 17:44:17 -0500
committerNathan Lee <me@nwlee.tech>2026-08-12 17:44:17 -0500
commit2c0efd93530c472e96ff3529aee983b53f3b78d9 (patch)
treecc36e7334e162ba67df79333e267ac93b4d984b9 /include/sys
parent640a81b3ee366c651286efd1b80b37992cd53282 (diff)
rework project structure
Diffstat (limited to 'include/sys')
-rw-r--r--include/sys/cpio.h23
-rw-r--r--include/sys/elf.h81
-rw-r--r--include/sys/log.h10
-rw-r--r--include/sys/mem.h30
-rw-r--r--include/sys/stddef.h77
5 files changed, 221 insertions, 0 deletions
diff --git a/include/sys/cpio.h b/include/sys/cpio.h
new file mode 100644
index 0000000..c501e96
--- /dev/null
+++ b/include/sys/cpio.h
@@ -0,0 +1,23 @@
+#pragma once
+#include <sys/stddef.h>
+#define CPIO_HEADER_LEN 110
+
+struct cpio_newc_hdr {
+ char magic[6];
+ char ino[8];
+ char mode[8];
+ char uid[8];
+ char gid[8];
+ char nlink[8];
+ char mtime[8];
+ char filesize[8];
+ char devmajor[8];
+ char devminor[8];
+ char rdevmajor[8];
+ char rdevminor[8];
+ char namesize[8];
+ char check[8];
+};
+
+void *cpio_get_file(void *archive, size_t size, const char *filename);
+
diff --git a/include/sys/elf.h b/include/sys/elf.h
new file mode 100644
index 0000000..95ed592
--- /dev/null
+++ b/include/sys/elf.h
@@ -0,0 +1,81 @@
+#pragma once
+#include <sys/stddef.h>
+
+#define ELF_MAGIC_LE 0x464C457F
+#define ELF_PAD 0
+
+#define EI_CLASS 4
+#define EI_DATA 5
+#define EI_VERSION 6
+#define EI_OSABI 7
+#define EI_ABI_VERSION 8
+
+#define ELFDATA2LSB 1
+#define ELFDATA2MSB 2
+
+#define ELFCLASS32 1
+#define ELFCLASS64 2
+
+#define EV_CURRENT 1
+
+#ifdef __SYS_LITTLE_ENDIAN__
+ #define ELFHOSTDATA ELFDATA2LSB
+#else
+ #define ELFHOSTDATA ELFDATA2MSB
+#endif
+
+#ifdef __SYS_64_BIT__
+ #define ELFHOSTCLASS ELFCLASS64
+#else
+ #define ELFHOSTCLASS ELFCLASS32
+#endif
+
+struct elf_header {
+ uint8_t e_ident[16];
+
+ uint16_t e_type;
+ uint16_t e_machine;
+ uint32_t e_version;
+
+ uint32_t e_entry;
+ uint32_t e_phoff;
+ uint32_t e_shoff;
+
+ uint32_t e_flags;
+
+ uint16_t e_ehsize;
+ uint16_t e_phentsize;
+ uint16_t e_phnum;
+
+ uint16_t e_shentsize;
+ uint16_t e_shnum;
+ uint16_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;
+};
+
+struct elf_program_header {
+ uint32_t p_type;
+ uint32_t p_offset;
+ uint32_t p_vaddr;
+ uint32_t p_paddr;
+ uint32_t p_filesz;
+ uint32_t p_memsz;
+ uint32_t p_flags;
+ uint32_t p_align;
+};
+
+int elf_chk_header(struct elf_header *header);
+struct elf_section_header *elf_get_section(struct elf_header *hdr, void *data, uint32_t index);
+char *elf_get_section_name(struct elf_header *hdr, struct elf_section_header *section, void *data);
diff --git a/include/sys/log.h b/include/sys/log.h
new file mode 100644
index 0000000..da9274b
--- /dev/null
+++ b/include/sys/log.h
@@ -0,0 +1,10 @@
+#pragma once
+
+#define RINGBUF_SIZE 64 * 1024
+
+typedef void (*console_callback_t)(char);
+
+void vprintk(const char *message, __builtin_va_list args);
+void printk(const char *message, ...);
+
+void register_console_callback(console_callback_t callback);
diff --git a/include/sys/mem.h b/include/sys/mem.h
new file mode 100644
index 0000000..769a162
--- /dev/null
+++ b/include/sys/mem.h
@@ -0,0 +1,30 @@
+#pragma once
+#include <sys/stddef.h>
+
+struct mem_info {
+ void* start;
+ size_t size;
+};
+
+enum PageStatus {
+ MEMPAGE_FREE, MEMPAGE_USED
+};
+
+struct page {
+ struct page* next;
+ size_t size;
+ enum PageStatus status;
+};
+
+struct mmio_device_meta {
+ const char *name;
+ void *start;
+ size_t size;
+};
+
+void* kmalloc(size_t size);
+void* kzalloc(size_t size);
+void kfree(void* ptr);
+
+void km_init(struct mem_info *info);
+
diff --git a/include/sys/stddef.h b/include/sys/stddef.h
new file mode 100644
index 0000000..27aea74
--- /dev/null
+++ b/include/sys/stddef.h
@@ -0,0 +1,77 @@
+#pragma once
+#define NULL (void*)0
+
+#define uintptr_t __wk_size_t
+#define size_t __wk_size_t
+#define uint8_t __wk_uint8_t
+#define uint16_t __wk_uint16_t
+#define uint32_t __wk_uint32_t
+#define uint64_t __wk_uint64_t
+
+#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;
+typedef __UINT16_TYPE__ __wk_uint16_t;
+typedef __UINT32_TYPE__ __wk_uint32_t;
+typedef __UINT64_TYPE__ __wk_uint64_t;
+
+
+static inline size_t strlen(const char *s) {
+ size_t len = 0;
+ while (*s++) {
+ ++len;
+ }
+ return len;
+}
+static inline int strcmp(const char *str1, const char* str2) {
+ while (*str1 && (*str1 == *str2)) {
+ ++str1;
+ ++str2;
+ }
+ return *(const unsigned char *)str1 - *(const unsigned char *)str2;
+}
+
+static inline int memcmp(const void *s1, const void *s2, size_t n) {
+ const unsigned char *p1 = (const unsigned char *)s1;
+ const unsigned char *p2 = (const unsigned char *)s2;
+
+ for (size_t i = 0; i < n; i++) {
+ if (p1[i] != p2[i]) {
+ return p1[i] - p2[i];
+ }
+ }
+
+ return 0;
+}
+
+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;
+}