Skip to content

Commit

Permalink
Improve exemple kernel.
Browse files Browse the repository at this point in the history
  • Loading branch information
sleepy-monax committed Oct 31, 2022
1 parent b49ce78 commit edf54c3
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 10 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
.vscode/
*.o
*.d
*.elf
128 changes: 124 additions & 4 deletions exemple/kmain.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,132 @@
#define HANDOVER_INCLUDE_MACROS
#define HANDOVER_INCLUDE_HEADER
#define HANDOVER_INCLUDE_UTILITES

#include "handover.h"

void ho_printf(void *ctx, char const *format, ...);
#include <stdarg.h>
#include "../handover.h"

HANDOVER(WITH_FB, WITH_FILES);

// simple printf implementation that supports %s %d %x %p

size_t cstrlen(const char *str)
{
size_t len = 0;
while (str[len])
len++;
return len;
}

void putc(char c)
{
asm volatile("outb %0, %1"
:
: "a"(c), "Nd"(0xe9));
}

void cstrrev(char s[])
{
for (int i = 0, j = cstrlen(s) - 1; i < j; i++, j--)
{
char c = s[i];
s[i] = s[j];
s[j] = c;
}
}

void itoa(int n, char s[], int base)
{
int i, sign;

if ((sign = n) < 0)
n = -n;
i = 0;
do
{
s[i++] = n % base + '0';
} while ((n /= base) > 0);

if (sign < 0)
s[i++] = '-';

s[i] = '\0';
cstrrev(s);
}

void puts(const char *s)
{
while (*s)
{
putc(*s);
s++;
}
}

void printf(const char *fmt, ...)
{
va_list va;
va_start(va, fmt);

while (*fmt)
{
if (*fmt == '%')
{
fmt++;

if (*fmt == 's')
{
puts(va_arg(va, const char *));
}
else if (*fmt == 'd')
{
int number = va_arg(va, int);
if (number < 0)
{
putc('-');
number = -number;
}
char buffer[sizeof(int) * 8 + 1];
itoa(number, buffer, 10);
puts(buffer);
}
else if (*fmt == 'x')
{
int number = va_arg(va, int);
char buffer[sizeof(size_t) * 8 + 1];
itoa(number, buffer, 16);
puts(buffer);
}
else if (*fmt == '%')
{
putc('%');
}
}
else
{
putc(*fmt);
}

fmt++;
}

va_end(va);
}

void _kmain(uint64_t magic, HandoverPayload *payload)
{
printf("Handover magic: %x", magic);
printf("Handover payload: %x", payload);

printf("Handover agent: %s", payload->agent);
printf("Handover size: %d", payload->size);
printf("Handover count: %d", payload->count);

for (size_t i = 0; i < payload->count; i++)
{
HandoverRecord record = payload->records[i];
printf("Handover tag: %s(%x)", handover_tag_name(record.tag), record.tag);
printf(" flags: %x", record.flags);
printf(" start: %d", record.start);
printf(" size: %d", record.size);
printf(" more: %x", record.more);
}
}
2 changes: 1 addition & 1 deletion exemple/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ override INTERNALLDFLAGS := \
-Wl,-T,linker.ld

# Use find to glob all *.c files in the directory and extract the object names.
override CFILES := $(shell find ./ -type f -name '*.c')
override CFILES := $(shell find ./ -type f -name '*.c') ../handover.c
override OBJ := $(CFILES:.c=.o)
override HEADER_DEPS := $(CFILES:.c=.d)

Expand Down
13 changes: 9 additions & 4 deletions handover.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,31 +50,31 @@ HandoverRecord handover_half_under(HandoverRecord self, HandoverRecord other)
if (handover_overlap(self, other) &&
self.start < other.start)
{
return {
return (HandoverRecord){
.tag = self.tag,
.flags = 0,
.start = self.start,
.size = other.start - self.start,
};
}

return {};
return (HandoverRecord){};
}

HandoverRecord handover_half_over(HandoverRecord self, HandoverRecord other)
{
if (handover_overlap(self, other) &&
self.start + self.size > other.start + other.size)
{
return {
return (HandoverRecord){
.tag = self.tag,
.flags = 0,
.start = other.start + other.size,
.size = self.start + self.size - other.start - other.size,
};
}

return {};
return (HandoverRecord){};
}

void handover_insert(HandoverPayload *payload, size_t index, HandoverRecord record)
Expand Down Expand Up @@ -156,3 +156,8 @@ void handover_append(HandoverPayload *payload, HandoverRecord record)

payload->records[payload->count++] = record;
}

char const *handover_str(HandoverPayload const *payload, uint32_t offset)
{
return (char const *)payload + offset;
}
5 changes: 4 additions & 1 deletion handover.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef HANDOVER_H_INCLUDED
#define HANDOVER_H_INCLUDED

#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>

Expand Down Expand Up @@ -131,7 +132,7 @@ typedef void HandoverEntry(

#ifdef HANDOVER_INCLUDE_UTILITES

char const *handover_tag_name(HandoverTag tag);
char const *handover_tag_name(uint32_t tag);

bool handover_mergeable(uint32_t tag);

Expand All @@ -151,6 +152,8 @@ void handover_remove(HandoverPayload *payload, size_t index);

void handover_append(HandoverPayload *payload, HandoverRecord record);

char const *handover_str(HandoverPayload const *payload, uint32_t offset);

#endif

#endif

0 comments on commit edf54c3

Please sign in to comment.