stdio: add vsnprintf and change printf to kprintf
This commit is contained in:
parent
05c528f64c
commit
0395b65ad8
21 changed files with 102 additions and 186 deletions
2
Makefile
2
Makefile
|
|
@ -16,7 +16,7 @@ define DEFAULT_VAR =
|
|||
endef
|
||||
|
||||
# User controllable CFLAGS.
|
||||
CFLAGS ?= -g -O0 -pipe -Wall -Wextra
|
||||
CFLAGS ?= -g -O2 -pipe -Wall -Wextra
|
||||
|
||||
# User controllable linker flags. We set none by default.
|
||||
LDFLAGS ?=
|
||||
|
|
|
|||
26
dev/ahci.c
26
dev/ahci.c
|
|
@ -25,7 +25,7 @@ static paddr_t ahci_pbase = 0;
|
|||
|
||||
static void ahci_irq_handler()
|
||||
{
|
||||
printf(LOG_ERROR "AHCI INTERRUPT\n");
|
||||
kprintf(LOG_ERROR "AHCI INTERRUPT\n");
|
||||
}
|
||||
|
||||
static int ahci_port_type(hbaport_t *port)
|
||||
|
|
@ -116,13 +116,13 @@ static void ahci_probe_ports(struct pci_device *dev, hbamem_t *abar)
|
|||
|
||||
ahci_rebase(&abar->ports[i], i);
|
||||
sata_device_count++;
|
||||
printf(LOG_INFO "SATA drive found at port %d\n", i);
|
||||
kprintf(LOG_INFO "SATA drive found at port %d\n", i);
|
||||
} else if (type == AHCI_DEV_SATAPI) {
|
||||
printf(LOG_INFO "SATAPI drive found at port %d\n", i);
|
||||
kprintf(LOG_INFO "SATAPI drive found at port %d\n", i);
|
||||
} else if (type == AHCI_DEV_SEMB) {
|
||||
printf(LOG_INFO "SEMB drive found at port %d\n", i);
|
||||
kprintf(LOG_INFO "SEMB drive found at port %d\n", i);
|
||||
} else if (type == AHCI_DEV_PM) {
|
||||
printf(LOG_INFO "PM drive found at port %d\n", i);
|
||||
kprintf(LOG_INFO "PM drive found at port %d\n", i);
|
||||
}
|
||||
}
|
||||
pi >>= 1;
|
||||
|
|
@ -195,7 +195,7 @@ bool ahci_access_sectors(struct sata_device *dev, paddr_t lba, uint16_t count, p
|
|||
}
|
||||
|
||||
if (spin == 1000000) {
|
||||
printf(LOG_ERROR "AHCI: Port is hung\n");
|
||||
kprintf(LOG_ERROR "AHCI: Port is hung\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
@ -207,20 +207,20 @@ bool ahci_access_sectors(struct sata_device *dev, paddr_t lba, uint16_t count, p
|
|||
if ((port->ci & (1 << slot)) == 0)
|
||||
break;
|
||||
if (port->is & HBA_PORT_IS_TFES) {
|
||||
printf(LOG_ERROR "AHCI: Read disk error\n");
|
||||
kprintf(LOG_ERROR "AHCI: Read disk error\n");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/* check again */
|
||||
if (port->is & HBA_PORT_IS_TFES) {
|
||||
printf(LOG_ERROR "AHCI: Read disk error\n");
|
||||
kprintf(LOG_ERROR "AHCI: Read disk error\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
/* disk read success */
|
||||
#ifdef KDEBUG
|
||||
printf(LOG_DEBUG "Read %d sectors from disk\n", ocount);
|
||||
kprintf(LOG_DEBUG "Read %d sectors from disk\n", ocount);
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
|
|
@ -268,13 +268,13 @@ void ahci_init()
|
|||
struct pci_device *dev = pci_find_device(0x01, 0x06);
|
||||
/* check interrupt number */
|
||||
if (dev == NULL) {
|
||||
printf(LOG_WARN "No SATA controllers found\n");
|
||||
kprintf(LOG_WARN "No SATA controllers found\n");
|
||||
return;
|
||||
}
|
||||
abar = (hbamem_t *)((uintptr_t)dev->bar5 | hhdm_start);
|
||||
|
||||
if (!(abar->ghc & (1 << 31))) {
|
||||
printf(LOG_WARN "AHCI controller not enabled\n");
|
||||
kprintf(LOG_WARN "AHCI controller not enabled\n");
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
@ -294,7 +294,7 @@ void ahci_init()
|
|||
kmap(ahci_pbase, ahci_pbase | hhdm_start, ahci_zone_size, attrs.val);
|
||||
|
||||
if (sata_device_count == 0) {
|
||||
printf(LOG_WARN "No SATA devices found\n");
|
||||
kprintf(LOG_WARN "No SATA devices found\n");
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
@ -306,5 +306,5 @@ void ahci_init()
|
|||
pci_config_write_long(dev->bus, dev->slot, dev->func, 0x3C, tmp);
|
||||
irq_map(irq, ahci_irq_handler);
|
||||
|
||||
printf(LOG_SUCCESS "AHCI controller ready\n");
|
||||
kprintf(LOG_SUCCESS "AHCI controller ready\n");
|
||||
}
|
||||
|
|
|
|||
24
dev/apic.c
24
dev/apic.c
|
|
@ -69,7 +69,7 @@ int madt_parse_next_entry(int offset)
|
|||
int type = madt[offset];
|
||||
int len = madt[offset + 1];
|
||||
|
||||
printf(LOG_INFO "MADT Entry %d: %s\n", n_madt_entries, madt_entry_types[type]);
|
||||
kprintf(LOG_INFO "MADT Entry %d: %s\n", n_madt_entries, madt_entry_types[type]);
|
||||
|
||||
/* Can't declare variables inside switch statement */
|
||||
struct madt_ioapic *m_ioapic = (struct madt_ioapic *)madt;
|
||||
|
|
@ -78,11 +78,11 @@ int madt_parse_next_entry(int offset)
|
|||
switch (type) {
|
||||
case MADT_LAPIC:
|
||||
lapic_id = m_lapic->apic_id;
|
||||
printf(LOG_INFO "LAPIC ID: %xh\n", m_lapic->apic_id);
|
||||
kprintf(LOG_INFO "LAPIC ID: %xh\n", m_lapic->apic_id);
|
||||
break;
|
||||
case MADT_IOAPIC:
|
||||
ioapic_addr = m_ioapic->ioapic_addr;
|
||||
printf(LOG_INFO "IOAPIC address: %xh\n", m_ioapic->ioapic_addr);
|
||||
kprintf(LOG_INFO "IOAPIC address: %xh\n", m_ioapic->ioapic_addr);
|
||||
break;
|
||||
case MADT_IOAPIC_OVRD:
|
||||
break;
|
||||
|
|
@ -95,7 +95,7 @@ int madt_parse_next_entry(int offset)
|
|||
case MADT_X2_LAPIC:
|
||||
break;
|
||||
default:
|
||||
printf(LOG_WARN "Unrecognized MADT entry #%d, type=%d\n", n_madt_entries, type);
|
||||
kprintf(LOG_WARN "Unrecognized MADT entry #%d, type=%d\n", n_madt_entries, type);
|
||||
break;
|
||||
}
|
||||
|
||||
|
|
@ -114,33 +114,33 @@ void apic_eoi()
|
|||
void apic_init()
|
||||
{
|
||||
if (__madt == NULL) {
|
||||
printf(LOG_WARN "Failed to locate MADT\n");
|
||||
kprintf(LOG_WARN "Failed to locate MADT\n");
|
||||
return;
|
||||
}
|
||||
|
||||
int offset = 0x2C;
|
||||
int length = __madt->h.length;
|
||||
printf(LOG_INFO "MADT Length: %x\n", length);
|
||||
kprintf(LOG_INFO "MADT Length: %x\n", length);
|
||||
|
||||
while ((offset = madt_parse_next_entry(offset)))
|
||||
;
|
||||
|
||||
lapic_addr = __madt->lapic_addr;
|
||||
|
||||
printf(LOG_INFO "IOAPIC ADDR: %X\n", ioapic_addr);
|
||||
printf(LOG_INFO "LAPIC ADDR : %X\n", lapic_addr);
|
||||
kprintf(LOG_INFO "IOAPIC ADDR: %X\n", ioapic_addr);
|
||||
kprintf(LOG_INFO "LAPIC ADDR : %X\n", lapic_addr);
|
||||
|
||||
uint32_t r = ioapic_read(ioapic_addr, 0);
|
||||
r |= 0x04000000;
|
||||
|
||||
ioapic_write(ioapic_addr, 0, r);
|
||||
printf(LOG_WARN "IOAPIC ID:%x\n", r);
|
||||
kprintf(LOG_WARN "IOAPIC ID:%x\n", r);
|
||||
|
||||
printf(LOG_WARN "LAPIC_ID:%xh\n", lapic_id);
|
||||
kprintf(LOG_WARN "LAPIC_ID:%xh\n", lapic_id);
|
||||
ioapic_redirect_insert(4, 0x21, 0, 0);
|
||||
/* Set spurious interrupt vector to 0xFF and enable the LAPIC */
|
||||
lapic_id = lapic_write(lapic_addr, 0xF0, 0x1FF);
|
||||
printf(LOG_WARN "%x\n", lapic_id);
|
||||
kprintf(LOG_WARN "%x\n", lapic_id);
|
||||
|
||||
printf(LOG_SUCCESS "APIC initialized\n");
|
||||
kprintf(LOG_SUCCESS "APIC initialized\n");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -22,7 +22,7 @@ void console_init()
|
|||
{
|
||||
memset(console.line, 0, 512);
|
||||
console.resizemode = 0;
|
||||
printf(LOG_SUCCESS "Console ready\n");
|
||||
kprintf(LOG_SUCCESS "Console ready\n");
|
||||
}
|
||||
|
||||
bool console_ready()
|
||||
|
|
@ -37,18 +37,18 @@ void console_resize()
|
|||
memset(console.resize_cols, 0, 8);
|
||||
console.l_resize_rows = 0;
|
||||
console.l_resize_cols = 0;
|
||||
printf("\e[s"); /* save cursor position */
|
||||
kprintf("\e[s"); /* save cursor position */
|
||||
|
||||
printf("\e[999;999H"); /* move cursor FAR away */
|
||||
printf("\e[6n"); /* cursor position request */
|
||||
printf("\e[u"); /* restore cursor position */
|
||||
kprintf("\e[999;999H"); /* move cursor FAR away */
|
||||
kprintf("\e[6n"); /* cursor position request */
|
||||
kprintf("\e[u"); /* restore cursor position */
|
||||
memset(console.line, 0, 512);
|
||||
}
|
||||
|
||||
void console_input(char c)
|
||||
{
|
||||
if (!console.resizemode) {
|
||||
printf("%c", c);
|
||||
kprintf("%c", c);
|
||||
} else {
|
||||
if (c == 'R') {
|
||||
/* detect end of escape sequence */
|
||||
|
|
|
|||
|
|
@ -111,7 +111,7 @@ void pci_init()
|
|||
struct pci_device *dev = &pci_devices[i];
|
||||
const char fmt[] = LOG_DEBUG "PCI device BUS[%d:%d] CLASS[%d:%d] vendor_id=%xh device_id=%xh, header_type=%d\n";
|
||||
|
||||
printf(fmt, dev->bus, dev->slot, dev->class, dev->subclass, dev->vendor_id, dev->device_id, dev->header_type);
|
||||
kprintf(fmt, dev->bus, dev->slot, dev->class, dev->subclass, dev->vendor_id, dev->device_id, dev->header_type);
|
||||
}
|
||||
#endif /* KDEBUG */
|
||||
}
|
||||
|
|
|
|||
|
|
@ -33,7 +33,7 @@ void pic_eoi(uint8_t irq)
|
|||
|
||||
void exception(int vector, int error)
|
||||
{
|
||||
printf(LOG_WARN "EXCEPTION: %x, error code: %d\n", vector, error);
|
||||
kprintf(LOG_WARN "EXCEPTION: %x, error code: %d\n", vector, error);
|
||||
}
|
||||
|
||||
void pic_init()
|
||||
|
|
@ -63,5 +63,5 @@ void pic_init()
|
|||
outb(PIC2_DATA, mask2);
|
||||
|
||||
pic_eoi(15);
|
||||
printf(LOG_SUCCESS "8259 PIC initialized\n");
|
||||
kprintf(LOG_SUCCESS "8259 PIC initialized\n");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,13 +20,13 @@ int serial_init()
|
|||
|
||||
outb(COM1 | 0, 0xAE); /* Enable loopback mode */
|
||||
if (inb(COM1 | 0) != 0xAE) { /* Loopback test */
|
||||
printf(LOG_WARN "Serial COM1 loopback test failed\n");
|
||||
kprintf(LOG_WARN "Serial COM1 loopback test failed\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
outb(COM1 | 1, 0x01); /* Enable interrupts */
|
||||
outb(COM1 | 4, 0x0F); /* Enter normal operation */
|
||||
printf(LOG_SUCCESS "Serial COM1 initialized\n");
|
||||
kprintf(LOG_SUCCESS "Serial COM1 initialized\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -29,7 +29,7 @@ struct block_device *block_register(char *name, struct block_device_ops *ops, vo
|
|||
block_devices_count++;
|
||||
|
||||
#ifdef KDEBUG
|
||||
printf(LOG_DEBUG "Registered block device %s\n", name);
|
||||
kprintf(LOG_DEBUG "Registered block device %s\n", name);
|
||||
#endif
|
||||
|
||||
return bdev;
|
||||
|
|
|
|||
3
fs/fat32.c
Normal file
3
fs/fat32.c
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#include <kernel/block.h>
|
||||
#include <fs/fat32.h>
|
||||
10
fs/gpt.c
10
fs/gpt.c
|
|
@ -18,22 +18,22 @@ void block_gpt_init(struct block_device *dev)
|
|||
goto free_header;
|
||||
|
||||
if (block_read(dev, header, 1, 1)) {
|
||||
printf(LOG_ERROR "Failed to read GPT header for device %s\n", dev->name);
|
||||
kprintf(LOG_ERROR "Failed to read GPT header for device %s\n", dev->name);
|
||||
goto free_entries;
|
||||
}
|
||||
|
||||
if (header->signature != GPT_HEADER_SIGNATURE) {
|
||||
printf(LOG_ERROR "Invalid GPT header signature for device %s\n", dev->name);
|
||||
kprintf(LOG_ERROR "Invalid GPT header signature for device %s\n", dev->name);
|
||||
goto free_entries;
|
||||
}
|
||||
|
||||
if (header->entry_size != sizeof(struct gpt_entry)) {
|
||||
printf(LOG_ERROR "Invalid GPT entry size for device %s\n", dev->name);
|
||||
kprintf(LOG_ERROR "Invalid GPT entry size for device %s\n", dev->name);
|
||||
goto free_entries;
|
||||
}
|
||||
|
||||
if (block_read(dev, entries, header->lba_first, 1)) {
|
||||
printf(LOG_ERROR "Failed to read GPT entries for device %s\n", dev->name);
|
||||
kprintf(LOG_ERROR "Failed to read GPT entries for device %s\n", dev->name);
|
||||
goto free_entries;
|
||||
}
|
||||
|
||||
|
|
@ -42,7 +42,7 @@ void block_gpt_init(struct block_device *dev)
|
|||
dev->gpt_entries_count = header->entry_count;
|
||||
|
||||
#ifdef KDEBUG
|
||||
printf(LOG_DEBUG "GPT header for device %s loaded\n", dev->name);
|
||||
kprintf(LOG_DEBUG "GPT header for device %s loaded\n", dev->name);
|
||||
#endif
|
||||
return; /* Success */
|
||||
|
||||
|
|
|
|||
|
|
@ -3,12 +3,12 @@
|
|||
#define _ASSERT_H_
|
||||
|
||||
#ifdef KDEBUG
|
||||
#define assert(expr) \
|
||||
do { \
|
||||
if (!(expr)) { \
|
||||
printf(LOG_ERROR "assertion failed: %s, file %s, line %d\n", #expr, __FILE__, __LINE__); \
|
||||
yield(); \
|
||||
} \
|
||||
#define assert(expr) \
|
||||
do { \
|
||||
if (!(expr)) { \
|
||||
kprintf(LOG_ERROR "assertion failed: %s, file %s, line %d\n", #expr, __FILE__, __LINE__); \
|
||||
yield(); \
|
||||
} \
|
||||
} while (0)
|
||||
#else
|
||||
#define assert(expr) ((void)0)
|
||||
|
|
|
|||
8
include/fs/fat32.h
Normal file
8
include/fs/fat32.h
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#ifndef _FAT32_H_
|
||||
#define _FAT32_H_
|
||||
|
||||
#include <kernel/common.h>
|
||||
#include <kernel/block.h>
|
||||
|
||||
#endif /* _FAT32_H_ */
|
||||
|
|
@ -15,7 +15,11 @@
|
|||
#define LOG_SUCCESS "[" COL_GREEN_BLACK "RDY" COL_RESET "] "
|
||||
#define LOG_INFO "[LOG] "
|
||||
|
||||
int printf(const char *restrict fmt, ...);
|
||||
#include <stddef.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
int kprintf(const char *restrict fmt, ...);
|
||||
int snprintf(char *str, const char *restrict fmt, size_t size, ...);
|
||||
int vsnprintf(char *str, const char *restrict fmt, size_t size, va_list args);
|
||||
|
||||
#endif /* _SDTIO_H_ */
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ static void rsdt_parse(struct rsdt *rsdt)
|
|||
int num_sdt = (rsdt->h.length - sizeof(rsdt->h)) >> 2;
|
||||
|
||||
#ifdef KDEBUG
|
||||
printf(LOG_DEBUG "Number of RSDT entries: %d\n", num_sdt);
|
||||
kprintf(LOG_DEBUG "Number of RSDT entries: %d\n", num_sdt);
|
||||
#endif /* KDEBUG */
|
||||
|
||||
for (int i = 0; i < num_sdt; i++) {
|
||||
|
|
@ -38,7 +38,7 @@ static void rsdt_parse(struct rsdt *rsdt)
|
|||
#ifdef KDEBUG
|
||||
char buf[8] = { 0 };
|
||||
memcpy(buf, h->signature, 4);
|
||||
printf(LOG_DEBUG "RSDT entry: %s %d %x \n", buf, h->revision, h->checksum);
|
||||
kprintf(LOG_DEBUG "RSDT entry: %s %d %x \n", buf, h->revision, h->checksum);
|
||||
#endif /* KDEBUG */
|
||||
}
|
||||
}
|
||||
|
|
@ -53,7 +53,7 @@ void acpi_init()
|
|||
rsdt_parse(rsdt);
|
||||
|
||||
#ifdef KDEBUG
|
||||
printf(LOG_DEBUG "ACPI version: %d\n", rsdp->revision);
|
||||
kprintf(LOG_DEBUG "ACPI version: %d\n", rsdp->revision);
|
||||
#endif /* KDEBUG */
|
||||
printf(LOG_SUCCESS "ACPI initialized\n");
|
||||
kprintf(LOG_SUCCESS "ACPI initialized\n");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -33,5 +33,5 @@ void gdt_init()
|
|||
|
||||
gdt_load();
|
||||
|
||||
printf(LOG_SUCCESS "GDT initialized\n");
|
||||
kprintf(LOG_SUCCESS "GDT initialized\n");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -180,5 +180,5 @@ void idt_init()
|
|||
__idtr.offset = (uint64_t)__idt;
|
||||
idt_load();
|
||||
|
||||
printf(LOG_SUCCESS "IDT successfully loaded\n");
|
||||
kprintf(LOG_SUCCESS "IDT successfully loaded\n");
|
||||
}
|
||||
|
|
|
|||
27
init/main.c
27
init/main.c
|
|
@ -85,8 +85,8 @@ void kmain(void)
|
|||
kroot_part = kfile->gpt_part_uuid;
|
||||
|
||||
#ifdef KDEBUG
|
||||
printf(LOG_DEBUG "Kernel file: path='%s' cmdline='%s'\n", kpath, kcmdline);
|
||||
printf(LOG_DEBUG "Root partition: disk=%X-%X part=%X-%X\n", kroot_disk.a, kroot_disk.b, kroot_part.a, kroot_part.b);
|
||||
kprintf(LOG_DEBUG "Kernel file: path='%s' cmdline='%s'\n", kpath, kcmdline);
|
||||
kprintf(LOG_DEBUG "Root partition: disk=%X-%X part=%X-%X\n", kroot_disk.a, kroot_disk.b, kroot_part.a, kroot_part.b);
|
||||
#endif /* KDEBUG */
|
||||
|
||||
console_init();
|
||||
|
|
@ -99,33 +99,12 @@ void kmain(void)
|
|||
stack_init(ptr, ptr);
|
||||
}
|
||||
|
||||
#ifdef KDEBUG
|
||||
static void kmalloc_init_test()
|
||||
{
|
||||
/* consistency test */
|
||||
void *a = kmalloc(0x1000, ALLOC_KERN);
|
||||
kfree(a);
|
||||
void *b = kmalloc(0x1000, ALLOC_KERN);
|
||||
if (a != b) {
|
||||
printf(LOG_DEBUG "kmalloc consistency test failed\n");
|
||||
} else {
|
||||
printf(LOG_DEBUG "kmalloc consistency test passed\n");
|
||||
}
|
||||
|
||||
kfree(b);
|
||||
}
|
||||
#endif /* KDEBUG */
|
||||
|
||||
void kmain_post_stack_init()
|
||||
{
|
||||
ahci_init();
|
||||
|
||||
_pic_init();
|
||||
|
||||
#ifdef KDEBUG
|
||||
kmalloc_init_test();
|
||||
#endif /* KDEBUG */
|
||||
|
||||
printf("\n\n# ");
|
||||
kprintf("\n\n# ");
|
||||
yield();
|
||||
}
|
||||
|
|
|
|||
16
lib/rbtree.c
16
lib/rbtree.c
|
|
@ -159,7 +159,7 @@ static void rbt_delete_fixup(struct rbtree *tree, struct rbnode *node)
|
|||
if (!node)
|
||||
return;
|
||||
|
||||
struct rbnode *sibling;
|
||||
struct rbnode *sibling = NULL;
|
||||
while (node != tree->root && node->color == RB_BLACK) {
|
||||
if (node == node->parent->left) {
|
||||
sibling = node->parent->right;
|
||||
|
|
@ -318,7 +318,7 @@ struct rbnode *rbt_search(struct rbtree *tree, uint64_t key)
|
|||
void rbtree_print_diagram_fancy(struct rbtree *tree, struct rbnode *node, int indent)
|
||||
{
|
||||
if (node == NULL) {
|
||||
printf("<empty tree>\n");
|
||||
kprintf("<empty tree>\n");
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
@ -328,23 +328,23 @@ void rbtree_print_diagram_fancy(struct rbtree *tree, struct rbnode *node, int in
|
|||
|
||||
if (indent) {
|
||||
for (int i = 0; i < indent; i++) {
|
||||
printf(" ");
|
||||
kprintf(" ");
|
||||
}
|
||||
}
|
||||
|
||||
if (node->right != NULL) {
|
||||
printf(" /\n");
|
||||
kprintf(" /\n");
|
||||
for (int i = 0; i < indent; i++) {
|
||||
printf(" ");
|
||||
kprintf(" ");
|
||||
}
|
||||
}
|
||||
printf("%s%X%s\n", node->color == RB_RED ? "\033[31m" : "\033[0m", node->key, "\033[0m");
|
||||
kprintf("%s%X%s\n", node->color == RB_RED ? "\033[31m" : "\033[0m", node->key, "\033[0m");
|
||||
|
||||
if (node->left != NULL) {
|
||||
for (int i = 0; i < indent; i++) {
|
||||
printf(" ");
|
||||
kprintf(" ");
|
||||
}
|
||||
printf(" \\\n");
|
||||
kprintf(" \\\n");
|
||||
rbtree_print_diagram_fancy(tree, node->left, indent + 4);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
108
lib/stdio.c
108
lib/stdio.c
|
|
@ -20,8 +20,7 @@ static int printf_write(const char *restrict s)
|
|||
return ret;
|
||||
}
|
||||
|
||||
/* this is seriously the approach i'm going with for now */
|
||||
int snprintf(char *str, const char *restrict fmt, size_t size, ...)
|
||||
int vsnprintf(char *str, const char *restrict fmt, size_t size, va_list args)
|
||||
{
|
||||
int ret = 0;
|
||||
char c;
|
||||
|
|
@ -31,9 +30,6 @@ int snprintf(char *str, const char *restrict fmt, size_t size, ...)
|
|||
|
||||
int n;
|
||||
|
||||
va_list args;
|
||||
va_start(args, size);
|
||||
|
||||
const char *convert = "0123456789ABCDEF";
|
||||
char buf[16] = { 0 };
|
||||
char *bufstr = NULL;
|
||||
|
|
@ -127,96 +123,22 @@ out:
|
|||
return ret;
|
||||
}
|
||||
|
||||
int printf(const char *restrict fmt, ...)
|
||||
int snprintf(char *str, const char *restrict fmt, size_t size, ...)
|
||||
{
|
||||
int ret = 0;
|
||||
char c;
|
||||
char *s;
|
||||
int i;
|
||||
long l;
|
||||
|
||||
int n;
|
||||
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
|
||||
const char *convert = "0123456789ABCDEF";
|
||||
char buf[16] = { 0 };
|
||||
|
||||
while ((c = *fmt++)) {
|
||||
if (c == '%') {
|
||||
c = *fmt++;
|
||||
switch (c) {
|
||||
case '\0':
|
||||
printf_out('%');
|
||||
ret++;
|
||||
goto out;
|
||||
case 'd':
|
||||
i = va_arg(args, int);
|
||||
|
||||
if (i < 0) {
|
||||
printf_out('-');
|
||||
ret++;
|
||||
i = -i;
|
||||
}
|
||||
|
||||
n = 0;
|
||||
memset(buf, 0, sizeof(buf));
|
||||
|
||||
do {
|
||||
buf[n] = convert[i % 10];
|
||||
n++;
|
||||
} while ((i /= 10));
|
||||
|
||||
goto printf_reverse;
|
||||
case 's':
|
||||
s = va_arg(args, char *);
|
||||
ret += printf_write(s);
|
||||
break;
|
||||
case 'c':
|
||||
i = va_arg(args, int);
|
||||
ret++;
|
||||
printf_out(i);
|
||||
break;
|
||||
case 'x':
|
||||
i = va_arg(args, int);
|
||||
n = 0;
|
||||
|
||||
memset(buf, 0, sizeof(buf));
|
||||
|
||||
do {
|
||||
buf[n] = convert[i & 0xF];
|
||||
n++;
|
||||
} while ((i >>= 4));
|
||||
|
||||
goto printf_reverse;
|
||||
break;
|
||||
case 'X':
|
||||
l = va_arg(args, long);
|
||||
for (uint64_t j = 0; j < (sizeof(l) << 1); j++) {
|
||||
int sel = ((sizeof(l) << 1) - j - 1) << 2;
|
||||
int num = (l >> sel) & 0xF;
|
||||
printf_out(convert[num]);
|
||||
ret++;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
printf_out(c);
|
||||
ret++;
|
||||
break;
|
||||
printf_reverse:
|
||||
for (int j = 0; j < n; j++) {
|
||||
printf_out(buf[(n - 1) - j]);
|
||||
ret++;
|
||||
}
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
printf_out(c);
|
||||
ret++;
|
||||
}
|
||||
}
|
||||
out:
|
||||
va_start(args, size);
|
||||
int ret = vsnprintf(str, fmt, size, args);
|
||||
va_end(args);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int kprintf(const char *restrict fmt, ...)
|
||||
{
|
||||
char buf[1024] = { 0 };
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
int ret = vsnprintf(buf, fmt, 1024, args);
|
||||
va_end(args);
|
||||
printf_write(buf);
|
||||
return ret;
|
||||
}
|
||||
|
|
|
|||
12
mem/buddy.c
12
mem/buddy.c
|
|
@ -143,11 +143,11 @@ static paddr_t buddy_alloc_traverse(struct buddy_region_header *head, size_t dep
|
|||
static paddr_t buddy_alloc_helper(struct buddy_region_header *head, size_t size)
|
||||
{
|
||||
if (npow2(size) != size) {
|
||||
printf(LOG_WARN "buddy_alloc: size not a power of 2\n");
|
||||
kprintf(LOG_WARN "buddy_alloc: size not a power of 2\n");
|
||||
}
|
||||
|
||||
if (size < 0x1000) {
|
||||
printf(LOG_WARN "buddy_alloc: size too small\n");
|
||||
kprintf(LOG_WARN "buddy_alloc: size too small\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
@ -244,7 +244,7 @@ static void *alloc_page_early()
|
|||
static uintptr_t hwm = 0;
|
||||
void *r = (void *)(hwm + kmem);
|
||||
if (hwm >= kmem_len)
|
||||
printf(LOG_ERROR "Out of early kmem\n");
|
||||
kprintf(LOG_ERROR "Out of early kmem\n");
|
||||
hwm += 0x1000;
|
||||
return r;
|
||||
}
|
||||
|
|
@ -309,8 +309,8 @@ static size_t limine_parse_memregions()
|
|||
|
||||
#ifdef KDEBUG
|
||||
const char *type_str = "base=%X, len=%X %s\n";
|
||||
printf(LOG_DEBUG "Memmap entry: ");
|
||||
printf(type_str, entry->base, entry->length, limine_types[entry->type]);
|
||||
kprintf(LOG_DEBUG "Memmap entry: ");
|
||||
kprintf(type_str, entry->base, entry->length, limine_types[entry->type]);
|
||||
|
||||
#endif
|
||||
|
||||
|
|
@ -436,5 +436,5 @@ void mem_early_init(char *mem, size_t len)
|
|||
cr3_write(cr3.val);
|
||||
|
||||
/* done */
|
||||
printf(LOG_SUCCESS "Early memory map initialized\n");
|
||||
kprintf(LOG_SUCCESS "Early memory map initialized\n");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -206,7 +206,7 @@ void kfree(void *ptr)
|
|||
|
||||
struct rbnode *n = rbt_search(&kmalloc_tree, (uint64_t)ptr);
|
||||
if (n == NULL) {
|
||||
printf(LOG_ERROR "kmalloc: invalid free: %X\n", ptr);
|
||||
kprintf(LOG_ERROR "kmalloc: invalid free: %X\n", ptr);
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue