Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions competition-osdev-08-pajadam/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
bin
tmp
*.pyc
16 changes: 16 additions & 0 deletions competition-osdev-08-pajadam/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# How to build and run project?

You need to use simple Python script. Just run it `python manage.py` with one of following arguments:

- `build` - generate our disk image
- `run` - run generated disk image in bochs
- `test` - `build` and `run`
- `clean` - empty temp directory

# What is in project's directories?

- /src - Code
- /scripts - Building scripts in python
- /conf - Bochs config
- /bin - Generated disk image
- /tmp - Directory with temporary files
1,131 changes: 1,131 additions & 0 deletions competition-osdev-08-pajadam/conf/osdev.bochsrc

Large diffs are not rendered by default.

47 changes: 47 additions & 0 deletions competition-osdev-08-pajadam/manage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from scripts import build, run
import shutil
import sys
import os


def main():

try:
# Add missing directories if needed
for directory in ("tmp", "bin",):
if not os.path.exists(directory):
os.makedirs(directory)

# Parse console arguments
if len(sys.argv) == 2:
os.chdir("tmp/")
if sys.argv[1] == "build":
build()
elif sys.argv[1] == "run":
run()
elif sys.argv[1] == "test":
build()
run()
elif sys.argv[1] == "clean":
os.chdir("..")
shutil.rmtree("tmp")
print "Cleaned ./tmp directory."
else:
raise ValueError("Invalid argument: " + sys.argv[1])
else:
raise ValueError("Missing arguments")

except ValueError as exc:
print exc
print "Usage:"
print " build\t- Build project"
print " run\t- Run project"
print " test\t- Build and run project"
print " clean\t- Clean temporary directory\n"

except Exception as exc:
print exc


if __name__ == "__main__":
main()
2 changes: 2 additions & 0 deletions competition-osdev-08-pajadam/scripts/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from .build import *
from .run import *
80 changes: 80 additions & 0 deletions competition-osdev-08-pajadam/scripts/build.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#!/usr/bin/python
import os
import sys
import subprocess
from glob import glob


def fix_stage1_size():
stage2_size = os.stat("stage2").st_size
kernel_size = os.stat("kernel64").st_size

stage2_size = (stage2_size + kernel_size + 511) / 512

if stage2_size >= 255:
raise Exception("stage2 & kernel are too large")

with open("stage1", "rb+") as f:
d = f.read()
idx = d.index("\xb0\xcc\x90\x90")
d = bytearray(d)
d[idx + 1] = stage2_size
f.seek(0)
f.write(d)


def build():
""" Build disk image with OS """
cmds_to_run = []

cc_flags = "-std=c99 -nostdlib -c -O0 -Wall -Wextra -masm=intel -ggdb"
ld_flags = "-std=c99 -nostdlib -o kernel64 -O0 -Wall -Wextra -masm=intel -ggdb"

objfiles = []

for fname in glob("../src/os/*.c"):
cmds_to_run.append("gcc %s %s" % (fname, cc_flags))
objfiles.append("%s.o" % os.path.basename(os.path.splitext(fname)[0]))

as_flags = "-masm=intel -ggdb -c"

for fname in glob("../src/os/*.s"):
cmds_to_run.append("gcc %s %s" % (fname, as_flags))
objfiles.append("%s.o" % os.path.basename(os.path.splitext(fname)[0]))

cmds_to_run.extend([
"gcc %s %s" % (' '.join(objfiles), ld_flags),
"strip kernel64",
"nasm ../src/boot/stage1.asm -o stage1",
"nasm ../src/boot/stage2.asm -o stage2",
fix_stage1_size
])

files_to_img = [
"stage1",
"stage2",
"kernel64"
]

for cmd in cmds_to_run:
if type(cmd) is str:
print "Running:", cmd
print subprocess.check_output(cmd, shell=True)
else:
print "Calling:", cmd.func_name
cmd()

buf = []
for fn in files_to_img:
with open(fn, "rb") as f:
d = f.read()
buf.append(d)

if len(d) % 512 == 0:
continue

padding_size = 512 - len(d) % 512
buf.append("\0" * padding_size)

with open("../bin/floppy.bin", "wb") as f:
f.write(''.join(buf))
13 changes: 13 additions & 0 deletions competition-osdev-08-pajadam/scripts/run.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from subprocess import call
import os


def run():
if not os.path.exists('../conf/osdev.bochsrc'):
raise Exception('Missing bochs config!')

if not os.path.exists('../bin/floppy.bin'):
raise Exception("Missing disk image.\nYou should run 'python manage.py build' first.")

print "Running bochs..."
call('bochs -f "../conf/osdev.bochsrc"', shell=True)
54 changes: 54 additions & 0 deletions competition-osdev-08-pajadam/src/boot/stage1.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
[bits 16]
[org 0x7c00]
; 0x7c00
; SEGMENT:OFFSET
; adres = SEGMENT * 0x10 + OFFSET
; CS 07c0 0000
; IP 0000 7c00
; ****

jmp word 0x0000:start

start:
;mov ax, 0xb800
;mov es, ax
;xor di, di
;mov word [es:di], 0x4141
;jmp $

; Load from floppy stage 2.
; DL == already set by BIOS
; AX -- 16 bits, AH AL -- 8 bits
; EAX -- AX

mov ax, 0x2000 ; 0x2000:0x0000
mov es, ax
xor bx, bx ; bx == 0

mov ah, 2 ; read sectors into memory
mov al, 0xcc ; 1337 stage2 3 * 512
nop
nop
mov ch, 0
mov cl, 2 ; sectors start from 1, or so they say ;)
mov dh, 0

int 13h

; Jump to stage 2
jmp word 0x2000:0x0000

; The CPU doesn't reach here. Ever. Hopefully.

epilogue:
%if ($ - $$) > 510
%fatal "Bootloader code exceed 512 bytes."
%endif

times 510 - ($ - $$) db 0
db 0x55
db 0xAA




Loading