Skip to content

Commit

Permalink
Add project
Browse files Browse the repository at this point in the history
  • Loading branch information
iluoeli committed Jun 1, 2018
0 parents commit f32b1d4
Show file tree
Hide file tree
Showing 117 changed files with 4,703 additions and 0 deletions.
15 changes: 15 additions & 0 deletions lab1/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
os.img:
cd bootloader; make bootloader.bin
cd app; make app.bin
cat bootloader/bootloader.bin app/app.bin > os.img

clean:
cd bootloader; make clean
cd app; make clean
rm -f os.img

play:
qemu-system-i386 os.img

debug:
qemu-system-i386 -s -S os.img
7 changes: 7 additions & 0 deletions lab1/app/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
app.bin: app.s
gcc -c -m32 app.s -o app.o
ld -m elf_i386 -e start -Ttext 0x8c00 app.o -o app.elf
objcopy -S -j .text -O binary app.elf app.bin

clean:
rm -rf *.o *.elf *.bin
73 changes: 73 additions & 0 deletions lab1/app/app.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
.code32

.global start
start:

movl $((80*5+0)*2), %edi #在第5行第0列打印
movb $0x0c, %ah #黑底红字
movb $0x48, %al #0x48为H的ASCII码
movw %ax, %gs:(%edi) #写显存

movl $((80*5+1)*2), %edi
movb $0x0c, %ah
movb $0x65, %al
movw %ax, %gs:(%edi)

movl $((80*5+2)*2), %edi
movb $0x0c, %ah
movb $0x6c, %al
movw %ax, %gs:(%edi)

movl $((80*5+3)*2), %edi
movb $0x0c, %ah
movb $0x6c, %al
movw %ax, %gs:(%edi)

movl $((80*5+4)*2), %edi
movb $0x0c, %ah
movb $0x6f, %al
movw %ax, %gs:(%edi)

movl $((80*5+5)*2), %edi
movb $0x0c, %ah
movb $0x2c, %al
movw %ax, %gs:(%edi)

movl $((80*5+6)*2), %edi
movb $0x0c, %ah
movb $0x20, %al
movw %ax, %gs:(%edi)

movl $((80*5+7)*2), %edi
movb $0x0c, %ah
movb $0x57, %al
movw %ax, %gs:(%edi)

movl $((80*5+8)*2), %edi
movb $0x0c, %ah
movb $0x6f, %al
movw %ax, %gs:(%edi)

movl $((80*5+9)*2), %edi
movb $0x0c, %ah
movb $0x72, %al
movw %ax, %gs:(%edi)

movl $((80*5+10)*2), %edi
movb $0x0c, %ah
movb $0x6c, %al
movw %ax, %gs:(%edi)

movl $((80*5+11)*2), %edi
movb $0x0c, %ah
movb $0x64, %al
movw %ax, %gs:(%edi)

movl $((80*5+12)*2), %edi
movb $0x0c, %ah
movb $0x21, %al
movw %ax, %gs:(%edi)

loop:
jmp loop

9 changes: 9 additions & 0 deletions lab1/bootloader/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
bootloader.bin: start.s boot.c boot.h
gcc -c -m32 start.s -o start.o
gcc -c -m32 -O1 -fno-stack-protector boot.c -o boot.o
ld -m elf_i386 -e start -Ttext 0x7c00 start.o boot.o -o bootloader.elf
objcopy -S -j .text -O binary bootloader.elf bootloader.bin
../utils/genboot.pl bootloader.bin

clean:
rm -rf *.o *.elf *.bin
31 changes: 31 additions & 0 deletions lab1/bootloader/boot.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include "boot.h"

#define SECTSIZE 512

void bootMain(void) {
void (*elf)(void);
// loading sector 1 to memory
readSect((void *)0x8c00, 1);
elf = (void *)0x8c00;
elf();
}

void waitDisk(void) { // waiting for disk
while((inByte(0x1F7) & 0xC0) != 0x40);
}

void readSect(void *dst, int offset) { // reading one sector of disk
int i;
waitDisk();
outByte(0x1F2, 1);
outByte(0x1F3, offset);
outByte(0x1F4, offset >> 8);
outByte(0x1F5, offset >> 16);
outByte(0x1F6, (offset >> 24) | 0xE0);
outByte(0x1F7, 0x20);

waitDisk();
for (i = 0; i < SECTSIZE / 4; i ++) {
((int *)dst)[i] = inLong(0x1F0);
}
}
25 changes: 25 additions & 0 deletions lab1/bootloader/boot.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#ifndef BOOT_H
#define BOOT_H

void waitDisk(void);

void readSect(void *dst, int offset);

/* I/O functions */
static inline char inByte(short port) {
char data;
asm volatile("in %1,%0" : "=a" (data) : "d" (port));
return data;
}

static inline int inLong(short port) {
int data;
asm volatile("in %1, %0" : "=a" (data) : "d" (port));
return data;
}

static inline void outByte(short port, char data) {
asm volatile("out %0,%1" : : "a" (data), "d" (port));
}

#endif
43 changes: 43 additions & 0 deletions lab1/bootloader/start.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
.code16

.global start
start:
cli #关闭中断
inb $0x92, %al #启动A20总线
orb $0x02, %al
outb %al, $0x92
data32 addr32 lgdt gdtDesc #加载GDTR
movl %cr0, %eax #启动保护模式
orb $0x01, %al
movl %eax, %cr0
data32 ljmp $0x08, $start32 #长跳转切换至保护

.code32
start32:
movw $0x18, %ax
movw %ax, %gs
movw $0x10, %ax
movw %ax, %ds
movw %ax, %es
movw %ax, %fs
movw %ax, %ss
movl $(128<<20), %esp
jmp bootMain

.p2align 2
gdt:
.word 0,0 # empty entry
.byte 0,0,0,0

.word 0xffff,0
.byte 0,0x9a,0xcf,0

.word 0xffff,0
.byte 0,0x92,0xcf,0

.word 0xffff,0x8000
.byte 0x0b,0x92,0xcf,0

gdtDesc:
.word (gdtDesc - gdt -1)
.long gdt
19 changes: 19 additions & 0 deletions lab1/utils/genboot.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/perl

open(SIG, $ARGV[0]) || die "open $ARGV[0]: $!";

$n = sysread(SIG, $buf, 1000);

if($n > 510){
print STDERR "ERROR: boot block too large: $n bytes (max 510)\n";
exit 1;
}

print STDERR "OK: boot block is $n bytes (max 510)\n";

$buf .= "\0" x (510-$n);
$buf .= "\x55\xAA";

open(SIG, ">$ARGV[0]") || die "open >$ARGV[0]: $!";
print SIG $buf;
close SIG;
19 changes: 19 additions & 0 deletions lab2/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
QEMU = qemu-system-i386

os.img:
@cd bootloader; make
@cd kernel; make
@cd app; make
cat bootloader/bootloader.bin kernel/kMain.elf app/uMain.elf > os.img

play: os.img
$(QEMU) -serial stdio os.img

debug: os.img
$(QEMU) -serial stdio -s -S os.img

clean:
@cd bootloader; make clean
@cd kernel; make clean
@cd app; make clean
rm -f os.img
Binary file added lab2/app/.main.c.swp
Binary file not shown.
17 changes: 17 additions & 0 deletions lab2/app/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
CC = gcc
LD = ld

CFLAGS = -m32 -march=i386 -static \
-fno-builtin -fno-stack-protector -fno-omit-frame-pointer \
-Wall -Werror -O2 -I../lib
LDFLAGS = -m elf_i386

UCFILES = $(shell find ./ -name "*.c")
LCFILES = $(shell find ../lib -name "*.c")
UOBJS = $(UCFILES:.c=.o) $(LCFILES:.c=.o)

umain.bin: $(UOBJS)
$(LD) $(LDFLAGS) -e uEntry -Ttext 0x00200000 -o uMain.elf $(UOBJS)

clean:
rm -rf $(UOBJS) uMain.elf
30 changes: 30 additions & 0 deletions lab2/app/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include "lib.h"
#include "types.h"

int uEntry(void) {

printf("printf test begin...\n");
printf("the answer should be:\n");
printf("#######################################################\n");
printf("Hello, welcome to OSlab! I'm the body of the game. ");
printf("Bootblock loads me to the memory position of 0x100000, and Makefile also tells me that I'm at the location of 0x100000. ");
printf("~!@#/(^&*()_+`1234567890-=...... ");
printf("Now I will test your printf: ");
printf("1 + 1 = 2, 123 * 456 = 56088\n0, -1, -2147483648, -1412505855, -32768, 102030\n0, ffffffff, 80000000, abcdef01, ffff8000, 18e8e\n");
printf("#######################################################\n");
printf("your answer:\n");
printf("=======================================================\n");
printf("%s %s%scome %co%s", "Hello,", "", "wel", 't', " ");
printf("%c%c%c%c%c! ", 'O', 'S', 'l', 'a', 'b');
printf("I'm the %s of %s. %s 0x%x, %s 0x%x. ", "body", "the game", "Bootblock loads me to the memory position of", 0x100000, "and Makefile also tells me that I'm at the location of", 0x100000);
printf("~!@#/(^&*()_+`1234567890-=...... ");
printf("Now I will test your printf: ");
printf("%d + %d = %d, %d * %d = %d\n", 1, 1, 1 + 1, 123, 456, 123 * 456);
printf("%d, %d, %d, %d, %d, %d\n", 0, 0xffffffff, 0x80000000, 0xabcedf01, -32768, 102030);
printf("%x, %x, %x, %x, %x, %x\n", 0, 0xffffffff, 0x80000000, 0xabcedf01, -32768, 102030);
printf("=======================================================\n");
printf("Test end!!! Good luck!!!\n");

while(1);
return 0;
}
20 changes: 20 additions & 0 deletions lab2/bootloader/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
CC = gcc
LD = ld

CFLAGS = -m32 -march=i386 -static \
-fno-builtin -fno-stack-protector -fno-omit-frame-pointer \
-Wall -Werror -O2
ASFLAGS = -m32
LDFLAGS = -m elf_i386

BSFILES = $(shell find ./ -name "*.S")
BCFILES = $(shell find ./ -name "*.c")
BOBJS = $(BSFILES:.S=.o) $(BCFILES:.c=.o)

bootloader.bin: $(BOBJS)
$(LD) $(LDFLAGS) -e start -Ttext 0x7c00 -o bootloader.elf $(BOBJS)
objcopy -O binary bootloader.elf bootloader.bin
@../utils/genBoot.pl bootloader.bin

clean:
rm -rf $(BOBJS) bootloader.elf bootloader.bin
55 changes: 55 additions & 0 deletions lab2/bootloader/boot.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include "boot.h"
#include <string.h>

#define SECTSIZE 512
#define MAXNUM 200
#define BUFSIZE (SECTSIZE*MAXNUM)

#define NULL ((void *)0)

void bootMain(void) {
/* 加载内核至内存,并跳转执行 */
void (*kernel) (void);

ELFHeader *elf;
ProgramHeader *ph;

unsigned char buf[BUFSIZE];

int i=0;
for(i=0; i < MAXNUM; i++)
readSect((void *)(buf + i*SECTSIZE), i+1);

elf = (void *)buf;
for(i=0; i < elf->phnum; i++){
ph = (ProgramHeader *)(buf + elf->phoff + i * elf->phentsize);
int vaddr = ph->vaddr;
int filesz = ph->filesz;
memcpy((void *)vaddr, buf+ph->off, filesz);
memset((void *)(vaddr+filesz), 0, ph->memsz-filesz);
}
kernel = (void *)elf->entry;
kernel();
}

void waitDisk(void) { // waiting for disk
while((inByte(0x1F7) & 0xC0) != 0x40);
}

void readSect(void *dst, int offset) { // reading a sector of disk
int i;
waitDisk();
outByte(0x1F2, 1);
outByte(0x1F3, offset);
outByte(0x1F4, offset >> 8);
outByte(0x1F5, offset >> 16);
outByte(0x1F6, (offset >> 24) | 0xE0);
outByte(0x1F7, 0x20);

waitDisk();
for (i = 0; i < SECTSIZE / 4; i ++) {
((int *)dst)[i] = inLong(0x1F0);
}
}


Loading

0 comments on commit f32b1d4

Please sign in to comment.