Cleaning code and provide minimal libc

This commit is contained in:
Loic Guegan 2021-04-09 10:29:23 +02:00
parent 8fee35522d
commit f6323421e2
17 changed files with 132 additions and 69 deletions

View file

@ -7,10 +7,12 @@ LD_SCRIPT := linker.ld
# first in the kernel binary (thus it must be linked first, cf the $(EXEC) rule)
BOOT_OBJ := $(addsuffix .o,$(basename $(shell find ./boot -name "*.[c|S]" ! -name "boot.S")))
UTILS_OBJ := $(addsuffix .o,$(basename $(shell find ./utils -name "*.[c|S]")))
LIBC_OBJ := $(addsuffix .o,$(basename $(shell find ./libc -name "*.[c|S]")))
all: $(EXEC)
$(EXEC): boot/boot.o $(BOOT_OBJ) $(UTILS_OBJ) bringelle.o
$(EXEC): boot/boot.o $(BOOT_OBJ) $(UTILS_OBJ) $(LIBC_OBJ) bringelle.o
ld -n -T $(LD_SCRIPT) -nostdlib -o bringelle $^
%.o: %.S

View file

@ -44,7 +44,7 @@ MB_INFO:
.int 0xABCDEF # Will contains the Multiboot2 information data structure address
_start:
mov %ebx, (MB_INFO)
mov %ebx, (MB_INFO) # Store Bootloader informations address
# Copy GDT into memory then load its register
call gdt_memcpy

View file

@ -1,23 +1,15 @@
#include "utils/print.h"
#include "utils/asm.h"
#include "libc/stdio.h"
#include "utils/pic.h"
#include "utils/8042.h"
#include "boot/multiboot.h"
extern char *name_addr;
void bringelle(){
clear();
printc("Booting Bringelle...\n",GREEN);
printc("Booting Bringelle...",GREEN);
// Kernel boot sequence
pic_enable_interrupt();
// Search for bootloader informations
MBI_TAG_BL_NAME bl_infos;
if(!mb_load_bl_name(&bl_infos)){
print(bl_infos.name);
print(" detected!\n");
}
printc(" done!\n",GREEN);
while(1);
}

Binary file not shown.

14
src/libc/math.c Normal file
View file

@ -0,0 +1,14 @@
#include "math.h"
int pow(int x,int n){
if(n<0)
return -1;
else if(n==0)
return 1;
else if(n==1)
return x;
int ret=x;
for(int i=0;i<(n-1);i++)
ret*=x;
return ret;
}

6
src/libc/math.h Normal file
View file

@ -0,0 +1,6 @@
#ifndef MATH_H
#define MATH_H
int pow(int x,int n);
#endif

25
src/libc/stdio.c Normal file
View file

@ -0,0 +1,25 @@
#include "stdio.h"
#include "string.h"
extern VIDEO_STATE VS;
void print(char *str){
int i=0;
while(str[i]!='\0'){
putchar(str[i]);
i++;
}
}
void printc(char* str,VIDEO_COLORS c){
VIDEO_COLORS backup=VS.fg;
VS.fg=c;
print(str);
VS.fg=backup;
}
void printi(int i){
char str[12];
itoa(i,str);
print(str);
}

10
src/libc/stdio.h Normal file
View file

@ -0,0 +1,10 @@
#ifndef STDIO_H
#define STDIO_H
#include "utils/framebuffer.h"
void print(char*);
void printc(char*,VIDEO_COLORS c);
void printi(int i);
#endif

29
src/libc/string.c Normal file
View file

@ -0,0 +1,29 @@
#include "string.h"
#include "math.h"
void itoa(int i, char *a){
// Check if lower than 0
char neg=0;
if(i<0){
neg=1;
i=-i;
a[0]='-';
}
// Count number of digits
int len=1;
while(i/pow(10,len)>=1)
{
len++;
}
// Build string
int max_pow=len-1;
for(int j=0;j<=max_pow;j++){
int cur_pow=pow(10,max_pow-j);
char digit=i/cur_pow;
a[j+neg]='0'+digit;
i=i-digit*cur_pow; // Remove first digits (most significant)
}
a[len+neg]='\0';
}

6
src/libc/string.h Normal file
View file

@ -0,0 +1,6 @@
#ifndef STRING_H
#define STRING_H
void itoa(int i, char *a);
#endif

View file

@ -1,5 +1,5 @@
#include "8042.h"
#include "print.h"
#include "framebuffer.h"
#include "asm.h"
DEFINE_AZERTY;

View file

@ -6,22 +6,22 @@
void _8042_keypress();
#define DEFINE_AZERTY char AZERTY[]={\
'?',\
'?',\
'?',\
'?',\
'?',\
'?',\
'?',\
'?',\
'?',\
'?',/* 10 */\
'?',\
'?',\
'?',\
'?',\
'?',\
'?',\
'\0',\
'\0',\
'&',\
'\0',\
'"',\
'\'',\
'(',\
'-',\
'\0',\
'_',/* 10 */\
'\0',\
'\0',\
')',\
'=',\
'\0',\
'\t',\
'a',\
'z',\
'e',\
@ -34,8 +34,8 @@ void _8042_keypress();
'p',\
'^',\
'$',\
'?',\
'?',\
'\0',\
'\0',\
'q',/* 0x1E (30) */\
's',\
'd',\
@ -46,9 +46,9 @@ void _8042_keypress();
'k',\
'l',\
'm',\
'?',\
'?',\
'?',\
'\0',\
'\0',\
'\0',\
'*',\
'w',\
'x',\

View file

@ -1,16 +1,8 @@
#include "print.h"
#include "framebuffer.h"
#define MAX_COL 80
#define MAX_LINE 25
struct VIDEO_STATE {
u8 *mem;
u8 col;
u8 line;
u8 bg;
u8 fg;
};
VIDEO_STATE VS={
(u8 *)0xB8000,
0,
@ -47,21 +39,6 @@ void putchar(char c){
}
}
void print(char *str){
int i=0;
while(str[i]!='\0'){
putchar(str[i]);
i++;
}
}
void printc(char* str,VIDEO_COLORS c){
VIDEO_COLORS backup=VS.fg;
VS.fg=c;
print(str);
VS.fg=backup;
}
void clear(){
for(char i=0;i<MAX_LINE;i++){
scrollup();

View file

@ -1,5 +1,5 @@
#ifndef PRINT_H
#define PRINT_H
#ifndef FRAMEBUFFER_H
#define FRAMEBUFFER_H
#include "types.h"
@ -9,14 +9,18 @@ typedef enum VIDEO_COLORS {
} VIDEO_COLORS;
typedef struct VIDEO_STATE VIDEO_STATE;
typedef struct VIDEO_STATE {
u8 *mem;
u8 col;
u8 line;
u8 bg;
u8 fg;
} VIDEO_STATE;
/**
* Print char
*/
void putchar(char);
void print(char*);
void printc(char*,VIDEO_COLORS c);
void scrollup();
void clear();

View file

@ -1,5 +1,4 @@
#include "gdt.h"
#include "print.h"
#include "mem.h"
struct GDT_REGISTER GDTR = { 0, 0 };

View file

@ -3,7 +3,6 @@
#include "types.h"
void memcpy(void *src, void *dst, int size);
#endif

View file

@ -56,7 +56,7 @@ void pic_enable_interrupt(){
outbj(0xA1,0x01); // Default operating mode
// OCW: Masking
outbj(0x21,0);
outbj(0x21,0b11111100);
asm("lidtl (IDTR)");