Skip to content

Commit d7e42e0

Browse files
committed
Added Bootloader Stage 1 Test
1 parent 0b4d615 commit d7e42e0

File tree

7 files changed

+119
-15
lines changed

7 files changed

+119
-15
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
*.vmdk
22
*.o
33
local_notes/
4-
build/
4+
build/
5+
src_test/

Makefile

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
BUILD_DIR = build
2-
3-
SRC_BOOTLOADER = src/bootloader
4-
SRC_KERNEL = src/kernel
5-
SRC_DRIVERS = src/drivers
6-
SRC_LIB_DS = src/lib/ds
7-
SRC_LIB_SYSCALL = src/lib/syscall
8-
SRC_LIB_UTILS = src/lib/utils
9-
SRC_LIB = src/lib
10-
SRC_MEMMGR = src/memmgr
11-
SRC_APP = src/app
12-
SRC_REALMODE = src/real_mode
2+
SRC_DIR = src
3+
QEMU_SHUT_FLAGS= -no-shutdown -no-reboot
4+
QEMU_EXTRA_FLAGS=
5+
6+
SRC_BOOTLOADER = $(SRC_DIR)/bootloader
7+
SRC_KERNEL = $(SRC_DIR)/kernel
8+
SRC_DRIVERS = $(SRC_DIR)/drivers
9+
SRC_LIB_DS = $(SRC_DIR)/lib/ds
10+
SRC_LIB_SYSCALL = $(SRC_DIR)/lib/syscall
11+
SRC_LIB_UTILS = $(SRC_DIR)/lib/utils
12+
SRC_LIB = $(SRC_DIR)/lib
13+
SRC_MEMMGR = $(SRC_DIR)/memmgr
14+
SRC_APP = $(SRC_DIR)/app
15+
SRC_REALMODE = $(SRC_DIR)/real_mode
1316

1417
BUILD_BOOTLOADER = build/bootloader
1518
BUILD_KERNEL = build/kernel
@@ -127,13 +130,13 @@ debug_kernel: $(kernel_core)
127130
xxd $<
128131

129132
qemu: $(image_vmdk)
130-
cpulimit -f -l 100 -- qemu-system-x86_64 -smp 1 -m 128M -hda $< -no-shutdown -no-reboot
133+
cpulimit -f -l 100 -- qemu-system-x86_64 -smp 1 -m 128M -hda $< $(QEMU_SHUT_FLAGS) $(QEMU_EXTRA_FLAGS)
131134

132135
qemu_debug: $(image_vmdk)
133-
qemu-system-x86_64 -smp 1 -m 128M -hda $< -no-shutdown -no-reboot -d cpu,exec,in_asm
136+
qemu-system-x86_64 -smp 1 -m 128M -hda $< $(QEMU_SHUT_FLAGS) -d cpu,exec,in_asm
134137

135138
qemu_xdebug: $(image_vmdk)
136-
qemu-system-x86_64 -S -gdb tcp::9000 -smp 1 -m 128M -hda $< -no-shutdown -no-reboot -d cpu,exec,in_asm
139+
qemu-system-x86_64 -S -gdb tcp::9000 -smp 1 -m 128M -hda $< $(QEMU_SHUT_FLAGS) -d cpu,exec,in_asm
137140

138141
clean:
139142
rm -r $(BUILD_DIR)/ || echo "Build directory is clean."

src/bootloader/stage1.asm

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
label_bts2_loaded:
3434
move_xy 06, 07, 00
3535
print_hex_string_ext 0x8000, 10, C_WHITE, 0
36+
; __TEST_INJECT__: mov eax, 0x9A11C824
37+
; __TEST_INJECT__: HLT
3638
JMP 0x8000
3739
JMP label_exit
3840

tests/lib.sh

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/bin/bash
2+
3+
SRC_DIR="src/"
4+
SRC_TEST_DIR="src_test/"
5+
MONITOR_PORT=55555
6+
QEMU_SCREENSHOT="/tmp/qemu.ppm"
7+
8+
###################################################
9+
# Turn up OS in QEMU and wait for $TEST_MAGIC_WANT.
10+
# Globals:
11+
# TEST_MAGIC_WANT
12+
# COMMAND_OUTPUT
13+
# SCREEN_CONTENT
14+
# QEMU_PID
15+
# Outputs:
16+
# Logs
17+
# Returns:
18+
# 0 on success, non-zero on error.
19+
###################################################
20+
function os_up() {
21+
# Turn up QEMU in background
22+
make qemu "SRC_DIR=${SRC_TEST_DIR:?}" QEMU_SHUT_FLAGS="" QEMU_EXTRA_FLAGS=" -monitor telnet:127.0.0.1:${MONITOR_PORT:?},server,nowait" &
23+
QEMU_PID=$!
24+
25+
while ! nc -z localhost ${MONITOR_PORT:?}; do
26+
echo "Waiting QEMU monitor to be available..."
27+
sleep 1s
28+
done
29+
echo "QEMU monitor is available."
30+
31+
# Cleanup from any previous execution
32+
rm -f ${QEMU_SCREENSHOT:?}
33+
34+
# Keep polling QEMU monitor until we get our magic word!
35+
COMMAND_OUTPUT=""
36+
while true; do
37+
echo "Sending commands to QEMU."
38+
COMMAND_OUTPUT="$(echo -e 'screendump '${QEMU_SCREENSHOT:?}'\nprint $eax\nquit' | nc 127.0.0.1 ${MONITOR_PORT:?})"
39+
echo "$COMMAND_OUTPUT" | grep -i "$TEST_MAGIC_WANT" && echo "Magic Word Found! Continuing the test..." && break
40+
echo "Magic word not found! Got: $COMMAND_OUTPUT"
41+
done
42+
43+
if [ ! -f ${QEMU_SCREENSHOT:?} ]; then
44+
echo "Magic word found but no screenshot found! :( " >&2
45+
return -1
46+
fi
47+
48+
SCREEN_CONTENT="$(gocr -i ${QEMU_SCREENSHOT:?})"
49+
echo "Screen Content: '${SCREEN_CONTENT:?}'"
50+
51+
echo "os_up done."
52+
return 0
53+
}

tests/preprocess.sh

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/bin/bash
2+
set -e
3+
4+
source tests/lib.sh
5+
6+
##########################################
7+
# Activate Code for testing within source.
8+
# Arguments:
9+
# Filename
10+
##########################################
11+
function inject_test_code() {
12+
echo "Injecting Test Code in $1 (if any)"
13+
sed -i 's/;\s*__TEST_INJECT__:\s*//g' "$1"
14+
}
15+
export -f inject_test_code
16+
17+
# Prepare source code directory for tests.
18+
rsync -r "${SRC_DIR:?}" "${SRC_TEST_DIR:?}"
19+
20+
find "${SRC_TEST_DIR:?}" -iname '*.asm' -exec bash -c 'inject_test_code "$0"' {} \;
21+

tests/run.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/bash
2+
set -e
3+
4+
# Preprocess
5+
bash tests/preprocess.sh
6+
7+
# Execute Tests
8+
bash tests/stage1_test.sh
9+
10+
# Done
11+
echo "All tests passed!"

tests/stage1_test.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/bash
2+
3+
TEST_MAGIC_WANT="9A11C824"
4+
5+
source tests/lib.sh
6+
7+
os_up || exit -1
8+
9+
# Test
10+
set -e
11+
echo "${SCREEN_CONTENT:?}" | grep "Bootloader: Stage 1" || ( echo "Test Failed!" && exit -1 )
12+
wait ${QEMU_PID:?}
13+
echo "$0 passed!!!"

0 commit comments

Comments
 (0)