Skip to content

Commit eda8ab7

Browse files
committed
Load and execute calculator as separate binary
1 parent 7f180b6 commit eda8ab7

File tree

9 files changed

+156
-28
lines changed

9 files changed

+156
-28
lines changed

Makefile

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
BL_SRC_DIR = src/bootloader
22
SYSCALLS_SRC_DIR = src/lib/syscalls
3+
LIB_SRC_DIR = src/lib
34
UTIL_SRC_DIR = src/lib/util
45
BUILD_DIR = build
56
APP_DIR = src/app
@@ -12,6 +13,7 @@ bt_stage2_c_o = $(BUILD_DIR)/bt_stage2_c.o
1213
bt_stage2_asm_o = $(BUILD_DIR)/bt_stage2_asm.o
1314
bt_stage2 = $(BUILD_DIR)/bt_stage2
1415
image_vmdk = $(BUILD_DIR)/image.vmdk
16+
app_entry_o = $(BUILD_DIR)/app_entry.o
1517

1618
# Apps
1719
app_calc = $(BUILD_DIR)/calc
@@ -26,32 +28,44 @@ all: images binaries
2628

2729
images: $(image_vmdk)
2830

29-
binaries: $(bt_stage1) $(bt_stage2)
31+
binaries: $(bt_stage1) $(bt_stage2) $(app_calc)
3032

31-
$(image_vmdk): $(bt_stage1) $(bt_stage2)
33+
$(image_vmdk): $(bt_stage1) $(bt_stage2) $(app_calc)
3234
dd bs=512 count=2 if=$(bt_stage1) of=$@
3335
/bin/echo -ne "\x55\xaa" | dd seek=510 bs=1 of=$@
34-
dd seek=1 bs=512 if=$(bt_stage2) of=$@
35-
truncate --size=%512 $@
36+
cat $(bt_stage2) >> $@
37+
cat $(app_calc) >> $@
3638
@echo "Stage 1 Size : " $$(stat -c %s $(bt_stage1))
3739
@echo "Stage 2 Size : " $$(stat -c %s $(bt_stage2))
38-
@echo "Stage 2 LoadSize : " $$(( $$(printf "%d\n" "0x"$(BT_STAGE2_SECTOR_COUNT)) * 512 ))
40+
@echo "App Calc Size : " $$(stat -c %s $(app_calc))
3941
@echo "Image Size : " $$(stat -c %s $@)
40-
@echo "Want BT_STAGE2_SECTOR_COUNT : 0x"$$(printf "%x\n" $$(( $$(stat -c %s $@) / 512 - 1)) )
42+
@echo "Want BT_STAGE2_SECTOR_COUNT : 0x"$$(printf "%x\n" $$(( $$(stat -c %s $(bt_stage2)) / 512)) )
4143
@echo "Got BT_STAGE2_SECTOR_COUNT : 0x"$(BT_STAGE2_SECTOR_COUNT)
44+
@echo "AppSector Sector Count : "$$(( $$(stat -c %s $(app_calc)) / 512))
45+
@echo "AppSector Sector Start : "$$(( 1 + $$(stat -c %s $(bt_stage1)) / 512 + $$(stat -c %s $(bt_stage2)) / 512))
4246

4347
$(bt_stage1): $(BL_SRC_DIR)/stage1.asm $(BL_SRC_DIR)/constants.asm $(BL_SRC_DIR)/io.asm $(BL_SRC_DIR)/disk.asm
4448
nasm -o $@ -f bin -i $(BL_SRC_DIR)/ -D BT_STAGE2_SECTOR_COUNT=$(BT_STAGE2_SECTOR_COUNT) $<
49+
truncate --size=%512 $@
4550

4651
$(bt_stage2): $(bt_stage2_asm_o) $(bt_stage2_c_o)
4752
ld --oformat binary -m elf_i386 -Ttext 0x8000 --strip-all -o $@ $^
53+
truncate --size=%512 $@
4854

49-
$(bt_stage2_c_o): $(BL_SRC_DIR)/stage2.c $(SYSCALLS_SRC_DIR)/basic.h $(SYSCALLS_SRC_DIR)/io.h $(SYSCALLS_SRC_DIR)/time.h $(SYSCALLS_SRC_DIR)/color.h $(UTIL_SRC_DIR)/string.h $(APP_DIR)/dashboard.c $(APP_DIR)/calc.c
55+
$(bt_stage2_c_o): $(BL_SRC_DIR)/stage2.c $(SYSCALLS_SRC_DIR)/basic.h $(SYSCALLS_SRC_DIR)/io.h $(SYSCALLS_SRC_DIR)/time.h $(SYSCALLS_SRC_DIR)/color.h $(UTIL_SRC_DIR)/string.h $(APP_DIR)/dashboard.c
5056
gcc -m16 -fno-pie -c -Isrc -o $@ $<
5157

52-
$(bt_stage2_asm_o): $(BL_SRC_DIR)/stage2.asm $(BL_SRC_DIR)/constants.asm $(BL_SRC_DIR)/io.asm $(SYSCALLS_SRC_DIR)/io_syscall.asm $(SYSCALLS_SRC_DIR)/time_syscall.asm $(bt_stage2_o)
58+
$(bt_stage2_asm_o): $(BL_SRC_DIR)/stage2.asm $(BL_SRC_DIR)/constants.asm $(BL_SRC_DIR)/io.asm $(SYSCALLS_SRC_DIR)/io_syscall.asm $(SYSCALLS_SRC_DIR)/time_syscall.asm $(bt_stage2_o) $(SYSCALLS_SRC_DIR)/disk_syscall.asm
5359
nasm -o $@ -f elf32 -i $(BL_SRC_DIR)/ -i$(SYSCALLS_SRC_DIR)/ $<
5460

61+
$(app_entry_o): $(LIB_SRC_DIR)/app/entry.asm $(BL_SRC_DIR)/constants.asm $(BL_SRC_DIR)/io.asm $(SYSCALLS_SRC_DIR)/io_syscall.asm $(SYSCALLS_SRC_DIR)/time_syscall.asm
62+
nasm -o $@ -f elf32 -i $(BL_SRC_DIR)/ -i$(SYSCALLS_SRC_DIR)/ $<
63+
64+
$(app_calc): $(app_entry_o) $(APP_DIR)/calc.c $(SYSCALLS_SRC_DIR)/basic.h $(SYSCALLS_SRC_DIR)/io.h $(SYSCALLS_SRC_DIR)/time.h $(SYSCALLS_SRC_DIR)/color.h $(UTIL_SRC_DIR)/string.h $(BL_SRC_DIR)/constants.asm $(BL_SRC_DIR)/io.asm $(SYSCALLS_SRC_DIR)/io_syscall.asm $(SYSCALLS_SRC_DIR)/time_syscall.asm
65+
gcc -m16 -fno-pie -c -Isrc -o $(BUILD_DIR)/calc.o $(APP_DIR)/calc.c
66+
ld --oformat binary -m elf_i386 -Ttext 0xC000 --strip-all -o $@ $(app_entry_o) $(BUILD_DIR)/calc.o
67+
truncate --size=%512 $@
68+
5569
debug_stage1: $(bt_stage1)
5670
objdump -b binary -mi386 -Maddr16,data16 -D $<
5771
xxd $<

src/app/calc.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,15 @@ int handle_expression(char str[]) {
103103
return 1;
104104
}
105105

106-
int calc_main(int argc,char **argv) {
106+
void console_init() {
107+
set_color_bg(C_BLACK);
108+
set_color_fg(C_WHITE);
109+
print_rectangle(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
110+
move_xy(0,0);
111+
}
112+
113+
int main(int argc,char *argv[]) {
114+
console_init();
107115
print_line("Simple Calculator\n");
108116
print_line("-----------------\n");
109117
print_line("\n");

src/app/dashboard.c

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <lib/syscalls/io.h>
33
#include <lib/syscalls/time.h>
44
#include <lib/syscalls/color.h>
5+
#include <lib/syscalls/disk.h>
56
#include <lib/util/string.h>
67

78
char query_app_number[] = "Enter Application Number: ";
@@ -18,32 +19,51 @@ void print_applications(unsigned char x, unsigned char y, char *list, unsigned c
1819
}
1920
}
2021

21-
void console_init() {
22-
set_color_bg(C_BLACK);
22+
void print_board() {
23+
set_color_bg(C_DARK_GRAY);
2324
set_color_fg(C_WHITE);
2425
print_rectangle(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
25-
move_xy(0,0);
26+
move_xy(1,0);
27+
print_line("Fuzzy OS");
28+
29+
set_color_bg(C_LIGHT_GRAY);
30+
set_color_fg(C_BLACK);
31+
print_rectangle(1, 1, WINDOW_WIDTH-1, WINDOW_HEIGHT-1);
32+
move_xy(3,3);
33+
34+
print_line(query_app_number);
35+
print_applications(4,4, (char*)application_list, 5, 15);
36+
}
37+
38+
int call_main(unsigned int address, int argc, char *argv[]) {
39+
return ((int (*) (int, char *[]))address)(argc, argv);
2640
}
2741

2842
int run_dashboard(int argc,char **argv) {
2943
while(1) {
30-
set_color_bg(C_DARK_GRAY);
31-
set_color_fg(C_WHITE);
32-
print_rectangle(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
33-
move_xy(1,0);
34-
print_line("Fuzzy OS");
35-
36-
set_color_bg(C_LIGHT_GRAY);
37-
set_color_fg(C_BLACK);
38-
print_rectangle(1, 1, WINDOW_WIDTH-1, WINDOW_HEIGHT-1);
39-
move_xy(3,3);
40-
41-
print_line(query_app_number);
42-
print_applications(4,4, (char*)application_list, 5, 15);
44+
print_board();
4345
move_xy(3+sizeof(query_app_number)-1,3);
4446
int num = read_int();
45-
console_init();
46-
calc_main(0,0);
47+
48+
// Load and launch calculator
49+
int err = load_sectors(0xC000, 0x80, 27, 25);
50+
if(err) {
51+
set_color_bg(C_DARK_GRAY);
52+
move_xy(2,WINDOW_HEIGHT);
53+
print_line("Failed to load app in memory, Error: ");
54+
print_int(err);
55+
sleep(1000);
56+
continue;
57+
}
58+
char *argv[] = {"argv1, argv2", "argv3"};
59+
int return_status = call_main(0xC000, 3, argv);
60+
61+
print_board();
62+
set_color_bg(C_DARK_GRAY);
63+
move_xy(2,WINDOW_HEIGHT);
64+
print_line("Program Exited: ");
65+
print_int(return_status);
66+
sleep(1000);
4767
}
4868
return 0;
4969
}

src/bootloader/stage2.asm

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
%include "io.asm"
44
%include "io_syscall.asm"
55
%include "time_syscall.asm"
6+
%include "disk_syscall.asm"
67

78
[BITS 16]
89

@@ -24,6 +25,8 @@ label_exit:
2425

2526
PLUGIN_SYSCALLS_IO
2627
PLUGIN_SYSCALLS_TIME
28+
PLUGIN_SYSCALLS_DISK
29+
2730

2831
[SECTION .data]
2932
bl_stage_2 db "Bootloader: Stage 2"

src/bootloader/stage2.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ void entry_stage() {
1414
set_color_fg(C_WHITE);
1515
move_xy(6, 13);
1616
print_line(message_dashboard);
17-
sleep(1000);
17+
sleep(100);
1818
run_dashboard(0,0);
1919
while(1);
2020
}

src/lib/app/entry.asm

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
; Any app entry point
2+
%include "constants.asm"
3+
%include "io.asm"
4+
%include "io_syscall.asm"
5+
%include "time_syscall.asm"
6+
%include "disk_syscall.asm"
7+
8+
[BITS 16]
9+
10+
extern main
11+
12+
[SECTION .text]
13+
jmp main
14+
15+
PLUGIN_SYSCALLS_IO
16+
PLUGIN_SYSCALLS_TIME
17+
PLUGIN_SYSCALLS_DISK

src/lib/syscalls/disk.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#ifndef __LIB_SYSCALLS_DISK
2+
#define __LIB_SYSCALLS_DISK
3+
4+
extern int _low_disk_read_sectors(
5+
unsigned char count,
6+
unsigned short cylinder_sector, // 10 bit + 6bit
7+
unsigned short head_driveindex, // 8 bit + 8bit
8+
unsigned short memory_address);
9+
10+
int load_sectors(unsigned short address,
11+
unsigned char drive,
12+
unsigned int sector_index, // 1-based
13+
unsigned char count) {
14+
_low_disk_read_sectors(
15+
count,
16+
((sector_index>>8)&0xFFC0) | (sector_index&0x3F),
17+
((sector_index>>16)&0xF00) | (drive),
18+
address);
19+
}
20+
21+
#endif

src/lib/syscalls/disk_syscall.asm

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
%include "disk.asm"
2+
global _low_disk_read_sectors
3+
4+
%macro PLUGIN_SYSCALLS_DISK 0
5+
6+
_low_disk_read_sectors:
7+
push ebp
8+
mov ebp, esp
9+
10+
; check es
11+
mov ah, 0x02 ; (read sectors)
12+
mov al, [ebp + 0x8] ; (sector count)
13+
mov cx, [ebp + 0xc] ; (cylinder 10bit, sector 6bit)
14+
mov dx, [ebp + 0x10] ; (head, drive index)
15+
mov bx, [ebp + 0x14] ; (es:bx as write address)
16+
int 0x13
17+
18+
mov ah, 0x01 ; (get status of last drive operation)
19+
int 0x13
20+
mov al, ah ; (result status code, 0 means no error)
21+
and eax, 0xFF
22+
23+
mov esp, ebp
24+
pop ebp
25+
ret
26+
27+
%endmacro

src/lib/syscalls/io.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,16 @@ void print_char(char c) {
8080
move_xy_diff(1, 0);
8181
}
8282
}
83+
char digit_to_hex[] = "0123456789ABCDEF";
84+
85+
void print_hex_nibble(unsigned char x) {
86+
print_char(digit_to_hex[x]);
87+
}
88+
89+
void print_hex_byte(unsigned char x) {
90+
print_hex_nibble(x>>4);
91+
print_hex_nibble(x&0xF);
92+
}
8393

8494
void print_line(char *str) {
8595
while((*str)!='\0') {
@@ -88,6 +98,14 @@ void print_line(char *str) {
8898
}
8999
}
90100

101+
void print_memory_hex(unsigned char *str,unsigned short count) {
102+
while(count) {
103+
print_hex_byte(*str);
104+
str++;
105+
count--;
106+
}
107+
}
108+
91109
void print_int(int x) {
92110
int is_negative = 0;
93111
int tailing_zero = 0;

0 commit comments

Comments
 (0)