first commit

This commit is contained in:
2024-11-27 17:06:47 +09:00
commit 3f556cba19
23 changed files with 1498 additions and 0 deletions

17
boot/Makefile Normal file
View File

@@ -0,0 +1,17 @@
include ../*.inc
PROG=boot
CSRCS= $(wildcard *.c)
SSRCS= $(wildcard *.S)
OBJS = $(SSRCS:.S=.o) $(CSRCS:.c=.o)
CFLAGS+=-c -fno-pie
CPPFLAGS+=-nostdinc -DLOADADDR=$(LOADADDR) -DLINKADDR=$(LINKADDR) -DBOOTMAGIC=$(BOOTMAGIC)
LDFLAGS+= -s -T ld.script -Ttext=$(LINKADDR) --no-omagic
${PROG}: $(OBJS)
@rm -f $(PROG)
$(LD) $(LDFLAGS) -o $(BDIR)/$(PROG) $(OBJS)
clean:
rm -f $(PROG) $(PROG).bin $(OBJS)

56
boot/boot.c Normal file
View File

@@ -0,0 +1,56 @@
typedef int dev_t;
#define SCREEN_WIDTH 80
#define SCREEN_LENGTH 25
#define VGA_BUFFER_ADDR 0xb8000
char*
memcpy(char *a, char *b, int l)
{
for(int i = 0; i < l; ++i)
a[i] = b[i];
return a;
}
int
strlen(char *s)
{
int i = 0;
for(; s[i]; ++i)
;
return i;
}
char*
itostr(int n, char *s)
{
char buf[16];
char *p = buf;
do {
*p++ = n%16;
}while((n/=16) != 0);
while(p > buf){
*s++ = "0123456789abcdef"[(int)*--p];
}
return s;
}
void
boot(dev_t bootdev)
{
char *p;
char s[SCREEN_WIDTH] = "booted on disk:0x";
short* vga = (short*)VGA_BUFFER_ADDR;
p = s+strlen(s);
p = itostr((int)bootdev, p);
for(int j = 0; j < SCREEN_LENGTH; ++j){
for (int i = 0; i < sizeof(s); ++i) {
vga[(j*SCREEN_WIDTH) + (i + SCREEN_WIDTH*2)] = 0x9100 | s[i];
}
}
while(1)
;
}

74
boot/gdt.S Normal file
View File

@@ -0,0 +1,74 @@
.file "gdt.S"
/* memory segment types */
#define SDT_MEMRO 16 /* memory read only */
#define SDT_MEMROA 17 /* memory read only accessed */
#define SDT_MEMRW 18 /* memory read write */
#define SDT_MEMRWA 19 /* memory read write accessed */
#define SDT_MEMROD 20 /* memory read only expand dwn limit */
#define SDT_MEMRODA 21 /* memory read only expand dwn limit accessed */
#define SDT_MEMRWD 22 /* memory read write expand dwn limit */
#define SDT_MEMRWDA 23 /* memory read write expand dwn limit acessed */
#define SDT_MEME 24 /* memory execute only */
#define SDT_MEMEA 25 /* memory execute only accessed */
#define SDT_MEMER 26 /* memory execute read */
#define SDT_MEMERA 27 /* memory execute read accessed */
#define SDT_MEMEC 28 /* memory execute only conforming */
#define SDT_MEMEAC 29 /* memory execute only accessed conforming */
#define SDT_MEMERC 30 /* memory execute read conforming */
#define SDT_MEMERAC 31 /* memory execute read accessed conforming */
#ifndef _ALIGN_TEXT
# define _ALIGN_TEXT .align 2, 0x120
#endif
/* NB == No Binding: use .globl or .weak as necessary */
#define _ENTRY_NB(x) \
.text; _ALIGN_TEXT; .type x,@function; x:
#define _ENTRY(x) .globl x; _ENTRY_NB(x)
.text
.code32
.align 8
gdt:
/* 0x00 : null */
.space 8
/* 0x08 : flat code */
.word 0xFFFF # lolimit
.word 0 # lobase
.byte 0 # midbase
.byte SDT_MEMERAC | 0 | 0x80 # RXAC, dpl = 0, present
.byte 0xf | 0 | 0x40 | 0x80 # hilimit, xx, 32bit, 4k granularity
.byte 0 # hibase
/* 0x10 : flat data */
.word 0xFFFF # lolimit
.word 0 # lobase
.byte 0 # midbase
.byte SDT_MEMRWA | 0 | 0x80 # RWA, dpl = 0, present
.byte 0xf | 0 | 0x40 | 0x80 # hilimit, xx, 32bit, 4k granularity
.byte 0 # hibase
/* 0x18 : 16 bit code */
.word 0xFFFF # lolimit
.word (LINKADDR & 0xffff) # lobase
.byte (LINKADDR >> 16) & 0xff # midbase
.byte SDT_MEMERAC | 0 | 0x80 # RXAC, dpl = 0, present
.byte 0x0 | 0 | 0 | 0 # hilimit, xx, 16bit, byte granularity
.byte (LINKADDR >> 20) & 0xff # hibase
/* 0x20 : 16 bit data */
.word 0xFFFF # lolimit
.word (LINKADDR & 0xffff) # lobase
.byte (LINKADDR >> 16) & 0xff # midbase
.byte SDT_MEMRWA | 0 | 0x80 # RWA, dpl = 0, present
.byte 0x0 | 0 | 0 | 0 # hilimit, xx, 16bit, byte granularity
.byte (LINKADDR >> 20) & 0xff # hibase
.globl Gdtr
Gdtr:
.word . - gdt -1
.long gdt
.word 0
.end

26
boot/ld.script Normal file
View File

@@ -0,0 +1,26 @@
OUTPUT_FORMAT("elf32-i386", "elf32-i386", "elf32-i386")
OUTPUT_ARCH(i386)
PHDRS
{
text PT_LOAD;
}
SECTIONS
{
.text : {
KEEP(srt0.o(.text))
*(.text)
} :text
etext = .;
.data : { *(.data) } :text
edata = .;
.bss : { *(.bss) } :text
ebss = .;
end = .;
/DISCARD/ :
{
*(.note.gnu.property)
*(.comment)
}
}

47
boot/srt0.S Normal file
View File

@@ -0,0 +1,47 @@
.file "srt0.S"
#define BOOTSTACK 0xfffc
#define CR0_PE 0x00000001 /* Protected mode Enable */
.globl end
.globl edata
.globl Gdtr
.globl boot
.text
.code16
.align 16
.globl _start
_start:
popl %eax
cmpl $BOOTMAGIC, %eax
je 1f
1:
popl %edx
cli
pushl %cs
popl %ds
addr32 data32 lgdt (Gdtr - LINKADDR)
movl %cr0, %eax
orl $CR0_PE, %eax
data32 movl %eax, %cr0
data32 ljmp $8, $1f
1:
.code32
movl $0x10, %eax
mov %ax, %ds
mov %ax, %ss
mov %ax, %es
mov %ax, %fs
mov %ax, %gs
movl $BOOTSTACK, %esp
pushl %edx
// fill 0 .bss
xorl %eax, %eax
call boot;