Rewritten MBR Payload - The first steps
This commit is contained in:
parent
e8db74b744
commit
ac47c0582e
12 changed files with 158 additions and 227 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -244,3 +244,5 @@ _Pvt_Extensions
|
|||
*.bat
|
||||
*.zip
|
||||
/VCProject/MEMZ.VC.db
|
||||
build/
|
||||
Build/
|
|
@ -1,9 +0,0 @@
|
|||
cd Image
|
||||
py png2bin.py frames/*.png image.bin
|
||||
cd ../Song
|
||||
py midi2bin.py nyan.mid song.bin
|
||||
|
||||
cd ..
|
||||
|
||||
cat Image/image.bin Song/song.bin Other/message.txt > data.bin
|
||||
Compressor/compress.exe data.bin compressed.bin
|
|
@ -1,4 +1,4 @@
|
|||
BUILDDIR = build
|
||||
BUILDDIR = Build
|
||||
|
||||
CC = cc
|
||||
PY = python2
|
||||
|
@ -11,9 +11,14 @@ ASFLAGS =
|
|||
IMAGES = $(sort $(wildcard Data/Image/frames/*.png))
|
||||
MUSIC = Data/Song/nyan.mid
|
||||
|
||||
STAGE1ASM = $(wildcard Source/Stage1/*.asm)
|
||||
STAGE2ASM = $(wildcard Source/Stage2/*.asm)
|
||||
|
||||
|
||||
all: disk.img
|
||||
|
||||
dir: $(BUILDDIR)
|
||||
|
||||
$(BUILDDIR):
|
||||
mkdir -p $(BUILDDIR)
|
||||
|
||||
|
@ -26,14 +31,17 @@ $(BUILDDIR)/song.bin: $(MUSIC)
|
|||
$(BUILDDIR)/data.bin: $(BUILDDIR)/song.bin $(BUILDDIR)/image.bin
|
||||
cat $(BUILDDIR)/image.bin $(BUILDDIR)/song.bin Data/Other/message.txt > $(BUILDDIR)/data.bin
|
||||
|
||||
$(BUILDDIR)/compress: Data/Compressor/compress.c
|
||||
$(CC) Data/Compressor/compress.c -o $@ $(CCFLAGS)
|
||||
|
||||
$(BUILDDIR)/compressed.bin: $(BUILDDIR)/data.bin $(BUILDDIR)/compress
|
||||
$(BUILDDIR)/compress $(BUILDDIR)/data.bin $(BUILDDIR)/compressed.bin
|
||||
$(BUILDDIR)/compress: Source/Compressor/compress.c
|
||||
$(CC) Source/Compressor/compress.c -o $@ $(CCFLAGS)
|
||||
|
||||
disk.img: dir kernel.asm decompress.asm $(BUILDDIR)/compressed.bin
|
||||
$(NASM) kernel.asm -o $@ $(ASFLAGS)
|
||||
$(BUILDDIR)/stage2.bin: $(STAGE2ASM) $(BUILDDIR)/data.bin
|
||||
cd Source/Stage2 && $(NASM) main.asm -o ../../$@ $(ASFLAGS)
|
||||
|
||||
$(BUILDDIR)/stage2-compressed.bin: $(BUILDDIR)/stage2.bin $(BUILDDIR)/compress
|
||||
$(BUILDDIR)/compress $(BUILDDIR)/stage2.bin $@
|
||||
|
||||
disk.img: dir $(STAGE1ASM) $(BUILDDIR)/stage2-compressed.bin
|
||||
cd Source/Stage1 && $(NASM) main.asm -o ../../disk.img $(ASFLAGS)
|
||||
|
||||
test: disk.img
|
||||
$(QEMU) -s -soundhw pcspk -fda disk.img
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
use16
|
||||
|
||||
start:
|
||||
mov bx, daddr
|
||||
xor bx, bx
|
||||
mov es, bx
|
||||
mov ds, bx
|
||||
|
||||
|
@ -12,20 +10,20 @@ start:
|
|||
mov bx, compressed
|
||||
int 13h
|
||||
|
||||
xor ax, ax
|
||||
xor ax, ax
|
||||
mov bx, ax
|
||||
mov cx, ax
|
||||
mov dx, ax
|
||||
|
||||
mov si, compressed
|
||||
mov di, image
|
||||
mov di, decompressed
|
||||
jmp readcommand
|
||||
|
||||
readcommand:
|
||||
lodsb
|
||||
|
||||
cmp si, compressed+compsize
|
||||
jae startanimation
|
||||
jae exit
|
||||
|
||||
cmp al, 128
|
||||
jae newdata
|
||||
|
@ -54,7 +52,7 @@ olddata:
|
|||
|
||||
mov dx, si
|
||||
mov si, bx
|
||||
add si, image
|
||||
add si, decompressed
|
||||
mov cl, al
|
||||
|
||||
oldnextbyte:
|
||||
|
@ -67,3 +65,5 @@ olddata:
|
|||
|
||||
mov si, dx
|
||||
jmp readcommand
|
||||
|
||||
exit:
|
19
PayloadMBR/Source/Stage1/main.asm
Normal file
19
PayloadMBR/Source/Stage1/main.asm
Normal file
|
@ -0,0 +1,19 @@
|
|||
use16
|
||||
org 0x7c00
|
||||
|
||||
compressed: equ 0x1000
|
||||
decompressed: equ 0x8000
|
||||
|
||||
%include "decompress.asm" ; Decompress Code & Data
|
||||
jmp decompressed ; Jump to the decompressed Data, booting the actual Kernel
|
||||
|
||||
; Boot sector signature
|
||||
times 510 - ($ - $$) db 0
|
||||
dw 0xAA55
|
||||
|
||||
; Include the compressed data
|
||||
comp: incbin "../../Build/stage2-compressed.bin" ; Hardcoded build dir :(
|
||||
compsize: equ $-comp
|
||||
|
||||
; Align it to sectors
|
||||
align 512
|
30
PayloadMBR/Source/Stage2/displayframe.asm
Normal file
30
PayloadMBR/Source/Stage2/displayframe.asm
Normal file
|
@ -0,0 +1,30 @@
|
|||
frameCount: equ 12 ; Don't hardcode that later
|
||||
frameSize: equ (80*50) / 2 ; Raw binary size of a frame
|
||||
|
||||
; Set base address for video memory
|
||||
mov cx, 0xb800
|
||||
mov es, cx
|
||||
|
||||
; Set base address for input data
|
||||
mov cx, xdata
|
||||
mov ds, cx
|
||||
|
||||
mov si, [cs:frameIndex]
|
||||
|
||||
; Display the frame
|
||||
mov ah, 220 ; Save the character used
|
||||
mov di, 1 ; Offset one byte
|
||||
|
||||
mov cx, frameSize
|
||||
.draw:
|
||||
lodsb
|
||||
stosw
|
||||
loop .draw
|
||||
|
||||
cmp si, frameCount*frameSize
|
||||
jne .end
|
||||
|
||||
mov si, 0
|
||||
|
||||
.end:
|
||||
mov [cs:frameIndex], si
|
37
PayloadMBR/Source/Stage2/main.asm
Normal file
37
PayloadMBR/Source/Stage2/main.asm
Normal file
|
@ -0,0 +1,37 @@
|
|||
; This file contains the actual animation
|
||||
use16
|
||||
org 0x8000
|
||||
|
||||
%include "setup.asm"
|
||||
|
||||
timerHandler:
|
||||
pusha
|
||||
|
||||
inc word [cs:frameTickCounter]
|
||||
|
||||
checkNextFrame:
|
||||
cmp word [cs:frameTickCounter], 10
|
||||
jne checkNextNote
|
||||
|
||||
mov word [cs:frameTickCounter], 0
|
||||
|
||||
%include "displayframe.asm"
|
||||
|
||||
checkNextNote:
|
||||
; Do the same for sound later
|
||||
|
||||
|
||||
; Acknowledge Interrupt
|
||||
mov al, 0x20
|
||||
out 0x20, al
|
||||
|
||||
popa
|
||||
iret
|
||||
|
||||
frameTickCounter dw 0
|
||||
|
||||
frameIndex dw 0
|
||||
|
||||
align 0x10
|
||||
xdata: equ 0x800+(($-$$)>>4)
|
||||
data: incbin "../../Build/data.bin"
|
47
PayloadMBR/Source/Stage2/setup.asm
Normal file
47
PayloadMBR/Source/Stage2/setup.asm
Normal file
|
@ -0,0 +1,47 @@
|
|||
; Make sure all registers are clean
|
||||
xor ax, ax
|
||||
xor bx, bx
|
||||
xor cx, cx
|
||||
xor dx, dx
|
||||
xor si, si
|
||||
xor di, di
|
||||
|
||||
; Set video mode
|
||||
mov ax, 0x0003
|
||||
int 10h
|
||||
|
||||
; Set base address for video memory
|
||||
mov cx, 0xb800
|
||||
mov es, cx
|
||||
|
||||
; Set base address for input data
|
||||
mov cx, xdata
|
||||
mov ds, cx
|
||||
|
||||
; Put the stack somewhere safe
|
||||
mov sp, 0x1000
|
||||
|
||||
cli ; Disable Interrupts
|
||||
|
||||
; Setup the main timer
|
||||
mov al, 00110100b
|
||||
out 0x43, al
|
||||
|
||||
; Set the frequency
|
||||
mov ax, 11932 ; ~100 Hz
|
||||
out 0x40, al
|
||||
mov al, ah
|
||||
out 0x40, al
|
||||
|
||||
; Setup the interrupt handler
|
||||
mov bx, 0x0000
|
||||
mov es, bx
|
||||
mov word [es:0x8*4], timerHandler ; Timer Handler
|
||||
mov word [es:0x8*4+2], 0 ; Segment 0x0000
|
||||
|
||||
sti ; Enable interrupts again
|
||||
|
||||
; Keep running only on interrupts
|
||||
rip:
|
||||
hlt
|
||||
jmp rip
|
|
@ -1,178 +0,0 @@
|
|||
use16
|
||||
org 0x7c00
|
||||
|
||||
%include "decompress.asm" ; Include decompressor part
|
||||
|
||||
%macro sleep 2
|
||||
; Use BIOS interrupt to sleep
|
||||
push dx
|
||||
mov ah, 86h
|
||||
mov cx, %1
|
||||
mov dx, %2
|
||||
int 15h
|
||||
pop dx
|
||||
%endmacro
|
||||
|
||||
%macro beepfreq 0
|
||||
out 42h, al
|
||||
mov al, ah
|
||||
out 42h, al
|
||||
%endmacro
|
||||
|
||||
%macro beepon 0
|
||||
in al, 61h
|
||||
or al, 00000011b
|
||||
out 61h, al
|
||||
%endmacro
|
||||
|
||||
%macro beepoff 0
|
||||
in al, 61h
|
||||
and al, 11111100b
|
||||
out 61h, al
|
||||
%endmacro
|
||||
|
||||
startanimation:
|
||||
; Init PC speaker
|
||||
mov al, 182
|
||||
out 43h, al
|
||||
|
||||
; Remove blinking
|
||||
mov ax, 1003h
|
||||
mov bl, 0
|
||||
int 10h
|
||||
|
||||
mov di, 0
|
||||
|
||||
mov dx, image+24000
|
||||
|
||||
mov cx, 0xb800 ; Set base address for video memory
|
||||
mov es, cx
|
||||
|
||||
; Clear screen
|
||||
mov ax, 0
|
||||
mov cx, 2000
|
||||
rep stosw
|
||||
|
||||
mov si, image+24000+476
|
||||
mov di, 0
|
||||
|
||||
beepon
|
||||
mov bl, 0
|
||||
|
||||
startmsg:
|
||||
sleep 0x0, 0x6000
|
||||
|
||||
cmp si, image+24000+476+msglen
|
||||
jge note
|
||||
|
||||
lodsb
|
||||
mov ah, 0xf0
|
||||
stosw
|
||||
|
||||
note:
|
||||
dec bl
|
||||
cmp bl, -1
|
||||
jne startmsg
|
||||
|
||||
push si
|
||||
mov si, dx
|
||||
|
||||
lodsw
|
||||
mov cx, ax
|
||||
and ah, 0b00011111
|
||||
|
||||
beepfreq
|
||||
|
||||
shr ch, 5
|
||||
shl ch, 2
|
||||
add ch, 3
|
||||
mov bl, ch
|
||||
|
||||
mov dx, si
|
||||
|
||||
pop si
|
||||
|
||||
cmp dx, image+24000+26*2
|
||||
jne startmsg
|
||||
|
||||
; Set image address
|
||||
mov si, image
|
||||
mov di, 0
|
||||
|
||||
mov ax, daddr
|
||||
mov ds, ax
|
||||
|
||||
mov ax, 0xb800
|
||||
mov es, ax
|
||||
|
||||
jmp transition
|
||||
|
||||
wrimg:
|
||||
; Write character
|
||||
mov al, 220
|
||||
stosb
|
||||
|
||||
; Write attributes
|
||||
lodsb
|
||||
stosb
|
||||
|
||||
; Check if animation is done
|
||||
cmp si, image+24000
|
||||
je repeat
|
||||
|
||||
; Check if the next frame is reached
|
||||
cmp di, 4000
|
||||
je nextframe
|
||||
|
||||
; Repeat the loop
|
||||
jmp wrimg
|
||||
|
||||
nextframe:
|
||||
sleep 0x1, 0x6000 ; Sleep some time
|
||||
|
||||
transition:
|
||||
mov di, 0 ; Reset video memory address
|
||||
|
||||
cmp dx, image+24000+476
|
||||
jne nextnote
|
||||
|
||||
mov dx, image+24000+26*2 ; Loop song
|
||||
|
||||
nextnote:
|
||||
dec bl
|
||||
cmp bl, -1
|
||||
jne wrimg
|
||||
|
||||
push si
|
||||
mov si, dx
|
||||
|
||||
lodsw
|
||||
mov cx, ax
|
||||
and ah, 0b00011111
|
||||
|
||||
beepfreq
|
||||
|
||||
shr ch, 5
|
||||
mov bl, ch
|
||||
|
||||
mov dx, si
|
||||
|
||||
pop si
|
||||
jmp wrimg ; Go back
|
||||
|
||||
repeat:
|
||||
mov si, image
|
||||
jmp nextframe
|
||||
|
||||
daddr: equ 0x07e0
|
||||
compressed: equ 0x0000
|
||||
image: equ 0x4000
|
||||
msglen: equ 76
|
||||
|
||||
times 510 - ($ - $$) db 0
|
||||
dw 0xAA55 ; Boot sector signature
|
||||
|
||||
comp: incbin "build/compressed.bin" ; Hardcoded, I hope I can change that later
|
||||
compsize: equ $-comp
|
||||
|
||||
times 4*1024 - ($ - $$) db 0
|
|
@ -1,22 +0,0 @@
|
|||
OUTPUT_FORMAT("binary")
|
||||
|
||||
ENTRY(start)
|
||||
codea = 0x7c00;
|
||||
dataa = 0x0000;
|
||||
SECTIONS
|
||||
{
|
||||
.text : AT(codea)
|
||||
{
|
||||
code = .;
|
||||
*(.text)
|
||||
*(.rodata)
|
||||
. = ALIGN(4096);
|
||||
}
|
||||
.data : AT(dataa)
|
||||
{
|
||||
data = .;
|
||||
*(.data)
|
||||
. = ALIGN(4096);
|
||||
}
|
||||
end = .;
|
||||
}
|
|
@ -1,3 +0,0 @@
|
|||
@echo off
|
||||
nasm -o disk.img kernel.asm
|
||||
pause
|
Reference in a new issue