Skip to content

Commit 9ceab50

Browse files
committed
[Test] Compare memory loaded at BT Stage 2
1 parent a8c35a5 commit 9ceab50

File tree

4 files changed

+42
-12
lines changed

4 files changed

+42
-12
lines changed

src/bootloader/stage2.c

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,33 @@
99
#define GDT_TABLE_SIZE 5
1010
struct GDTEntry gdt_table[GDT_TABLE_SIZE];
1111

12+
char DIGIT_TO_HEX[] = "0123456789ABCDEF";
13+
1214
extern struct GDTReference* _low_get_gdtr_address();
1315
extern void enter_protected_mode();
1416
extern void label_exit();
1517

18+
19+
char *get_memdump_8byte(void *address) {
20+
static char shared_memdump[17];
21+
for(int i=0;i<8;i++) {
22+
unsigned char byte = *(char*)address;
23+
address++;
24+
shared_memdump[i<<1] = DIGIT_TO_HEX[byte/16];
25+
shared_memdump[(i<<1)|1] = DIGIT_TO_HEX[byte%16];
26+
}
27+
shared_memdump[16] = '\0';
28+
return shared_memdump;
29+
}
30+
1631
void load_kernel() {
1732
print_log("Loading Kernel");
1833
int err = load_sectors(MEMORY_LOCATION_KERNEL, 0x80, SECTOR_START_KERNEL, SECTOR_COUNT_KERNEL);
1934
if(err) {
2035
print_log("Failed to load kernel in memory: %d", err);
2136
label_exit();
2237
} else {
23-
print_log("Kernel loaded at 0x%x: %x...", MEMORY_LOCATION_KERNEL, *(int*)MEMORY_LOCATION_KERNEL);
38+
print_log("Kernel loaded at 0x%x: %s...", MEMORY_LOCATION_KERNEL, get_memdump_8byte(MEMORY_LOCATION_KERNEL));
2439
}
2540
}
2641

@@ -31,7 +46,7 @@ void load_static_library() {
3146
print_log("Failed to load static library in memory: %d", err);
3247
label_exit();
3348
} else {
34-
print_log("Static library loaded at 0x%x: %x...", MEMORY_STATIC_LIBRARY, *(int*)MEMORY_STATIC_LIBRARY);
49+
print_log("Static library loaded at 0x%x: %s...", MEMORY_STATIC_LIBRARY, get_memdump_8byte(MEMORY_STATIC_LIBRARY));
3550
}
3651
}
3752

tests/bootloader_stage1_test.sh

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,8 @@ os_test_up "${TEST_MAGIC_WANT:?}" "${TEST_INJECT_WORD:?}" || exit -1
1111
set -e
1212
set -x
1313

14-
echo "${SCREEN_CONTENT:?}" | grep "Bootloader: Stage 1" || \
15-
err $LINENO "Test Failed! "
16-
17-
echo "${SCREEN_CONTENT:?}" | grep "Stage 2 Loaded: `build_8hexbyte bootloader/stage2`" || \
18-
err $LINENO "Test Failed! "
14+
test_screen_content $LINENO "Bootloader: Stage 1"
15+
test_screen_content $LINENO "Stage 2 Loaded: `build_8hexbyte bootloader/stage2`"
1916

2017
wait ${QEMU_PID:?}
2118
echo "$0 passed!!!"

tests/bootloader_stage2_test.sh

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ os_test_up "${TEST_MAGIC_WANT:?}" "${TEST_INJECT_WORD:?}" || exit -1
99

1010
# Test
1111
set -e
12-
echo "${SCREEN_CONTENT:?}" | grep "Bootloader: Stage 2" || \
13-
( echo "Test Failed!" && exit -1 )
14-
echo "${SCREEN_CONTENT:?}" | grep "Loading GDT Table and entering protected mode" || \
15-
( echo "Test Failed!" && exit -1 )
12+
13+
test_screen_content $LINENO "Bootloader: Stage 2"
14+
test_screen_content $LINENO "Static library loaded at.*: `build_8hexbyte real_mode/static_library`"
15+
test_screen_content $LINENO "Kernel loaded at.*: `build_8hexbyte kernel/core`"
16+
test_screen_content $LINENO "Loading GDT Table and entering protected mode"
1617

1718
wait ${QEMU_PID:?}
1819
echo "$0 passed!!!"

tests/lib.sh

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ function err() {
2525
# Resolves filename from build target.
2626
# Arguments:
2727
# build target
28+
# Globals:
29+
# BUILD_TEST_DIR
2830
# Output:
2931
# filename
3032
##########################################
@@ -43,6 +45,21 @@ function build_8hexbyte() {
4345
xxd -p -g1 -l 8 -u "`build_filename $1`"
4446
}
4547

48+
##########################################
49+
# Test Helper: Check if content is present
50+
# on screen.
51+
# Arguments:
52+
# line number
53+
# content
54+
# Globals:
55+
# test_screen_content
56+
# Output:
57+
# raises error or nothing.
58+
##########################################
59+
function test_screen_content() {
60+
echo "${SCREEN_CONTENT:?}" | grep "${2:?}" || \
61+
err "${1:?}" "Test Failed! Couldn't find '${2:?}' on screen."
62+
}
4663

4764
##########################################
4865
# Activate Code for testing within source.
@@ -121,7 +138,7 @@ function os_test_up() {
121138
sleep 1s
122139
done
123140

124-
sleep 5s
141+
sleep 1s
125142
./tests/qemu_monitor_expect.sh ${MONITOR_PORT:?} "screendump ${QEMU_SCREENSHOT:?}"
126143
./tests/qemu_monitor_expect.sh ${MONITOR_PORT:?} "quit"
127144

0 commit comments

Comments
 (0)