Skip to content

Commit 5b03939

Browse files
committed
Added simple log library
1 parent d923eac commit 5b03939

File tree

19 files changed

+203
-150
lines changed

19 files changed

+203
-150
lines changed

Makefile

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ SECTOR_COUNT_APP_TTT = 25
4747
SECTOR_START_APP_CALC = 93
4848
SECTOR_COUNT_APP_CALC= 25
4949

50+
MEMORY_STATIC_LIBRARY = 0x7E00
5051
MEMORY_LOCATION_KERNEL = 0xC000
5152
MEMORY_LOCATION_APP = 0x20000
5253

@@ -148,6 +149,7 @@ $(bt_stage2): $(SRC_BOOTLOADER)/stage2.asm $(SRC_BOOTLOADER)/stage2.c $(SRC_BOOT
148149
-D SECTOR_COUNT_SHARED_LIBRARY=$(SECTOR_COUNT_SHARED_LIBRARY) \
149150
-D SECTOR_START_KERNEL=$(SECTOR_START_KERNEL) \
150151
-D SECTOR_COUNT_KERNEL=$(SECTOR_COUNT_KERNEL) \
152+
-D MEMORY_STATIC_LIBRARY=$(MEMORY_STATIC_LIBRARY) \
151153
-D MEMORY_LOCATION_KERNEL=$(MEMORY_LOCATION_KERNEL) \
152154
-D MEMORY_LOCATION_APP=$(MEMORY_LOCATION_APP) \
153155
-o $(BUILD_BOOTLOADER)/stage2_c.o $(SRC_BOOTLOADER)/stage2.c
@@ -211,19 +213,25 @@ $(BUILD_DRIVERS)/disk/libdisk: $(SRC_DRIVERS)/disk/disk.c $(SRC_DRIVERS)/disk/di
211213
nasm -o $(BUILD_DRIVERS)/disk/disk_asm.o -f elf32 -i $(SRC_REALMODE)/ $(SRC_DRIVERS)/disk/disk.asm
212214
ar rc $@ $(BUILD_DRIVERS)/disk/disk_c.o $(BUILD_DRIVERS)/disk/disk_asm.o
213215

214-
$(BUILD_LIB_UTILS)/libutils_16: $(SRC_LIB_UTILS)/output.c $(SRC_LIB_UTILS)/output.h $(SRC_LIB_UTILS)/string.c $(SRC_LIB_UTILS)/string.h $(SRC_LIB_UTILS)/panic.c $(SRC_LIB_UTILS)/panic.h $(SRC_LIB_UTILS)/panic.asm $(SRC_LIB_UTILS)/time.c $(SRC_LIB_UTILS)/time.h $(SRC_LIB_UTILS)/time.asm $(SRC_LIB_UTILS)/color.c $(SRC_LIB_UTILS)/color.h
216+
$(BUILD_LIB_UTILS)/libutils_16: $(SRC_LIB_UTILS)/basic.c $(SRC_LIB_UTILS)/basic_16.asm $(SRC_LIB_UTILS)/logging.c $(SRC_LIB_UTILS)/output.c $(SRC_LIB_UTILS)/output.h $(SRC_LIB_UTILS)/string.c $(SRC_LIB_UTILS)/string.h $(SRC_LIB_UTILS)/panic.c $(SRC_LIB_UTILS)/panic.h $(SRC_LIB_UTILS)/panic.asm $(SRC_LIB_UTILS)/time.c $(SRC_LIB_UTILS)/time.h $(SRC_LIB_UTILS)/time.asm $(SRC_LIB_UTILS)/color.c $(SRC_LIB_UTILS)/color.h
215217
mkdir -p $(BUILD_LIB_UTILS)/
216-
gcc -m16 -fno-pie -c -Isrc -o $(BUILD_LIB_UTILS)/output.o $(SRC_LIB_UTILS)/output.c
217-
gcc -m16 -fno-pie -c -Isrc -o $(BUILD_LIB_UTILS)/string.o $(SRC_LIB_UTILS)/string.c
218-
gcc -m16 -fno-pie -c -Isrc -o $(BUILD_LIB_UTILS)/color.o $(SRC_LIB_UTILS)/color.c
219-
gcc -m16 -fno-pie -c -D__SOURCE_SNAPSHOT__=$(SOURCE_SNAPSHOT) -Isrc -o $(BUILD_LIB_UTILS)/panic_c.o $(SRC_LIB_UTILS)/panic.c
220-
nasm -o $(BUILD_LIB_UTILS)/panic_asm.o -f elf32 $(SRC_LIB_UTILS)/panic.asm
221-
gcc -m16 -fno-pie -c -Isrc -o $(BUILD_LIB_UTILS)/time_c.o $(SRC_LIB_UTILS)/time.c
222-
nasm -o $(BUILD_LIB_UTILS)/time_asm.o -f elf32 $(SRC_LIB_UTILS)/time.asm
223-
ar rc $@ $(BUILD_LIB_UTILS)/output.o $(BUILD_LIB_UTILS)/string.o $(BUILD_LIB_UTILS)/color.o $(BUILD_LIB_UTILS)/panic_c.o $(BUILD_LIB_UTILS)/panic_asm.o $(BUILD_LIB_UTILS)/time_c.o $(BUILD_LIB_UTILS)/time_asm.o
224-
225-
$(BUILD_LIB_UTILS)/libutils: $(SRC_LIB_UTILS)/output.c $(SRC_LIB_UTILS)/output.h $(SRC_LIB_UTILS)/input.c $(SRC_LIB_UTILS)/input.h $(SRC_LIB_UTILS)/string.c $(SRC_LIB_UTILS)/string.h $(SRC_LIB_UTILS)/panic.c $(SRC_LIB_UTILS)/panic.h $(SRC_LIB_UTILS)/panic.asm $(SRC_LIB_UTILS)/time.c $(SRC_LIB_UTILS)/time.h $(SRC_LIB_UTILS)/time.asm $(SRC_LIB_UTILS)/color.c $(SRC_LIB_UTILS)/color.h
218+
gcc -m16 -fno-pie -c -Isrc -o $(BUILD_LIB_UTILS)/basic_16_c.o $(SRC_LIB_UTILS)/basic.c
219+
nasm -o $(BUILD_LIB_UTILS)/basic_16_asm.o -f elf32 $(SRC_LIB_UTILS)/basic_16.asm
220+
gcc -m16 -fno-pie -c -Isrc -o $(BUILD_LIB_UTILS)/logging_16.o $(SRC_LIB_UTILS)/logging.c
221+
gcc -m16 -fno-pie -c -Isrc -o $(BUILD_LIB_UTILS)/output_16.o $(SRC_LIB_UTILS)/output.c
222+
gcc -m16 -fno-pie -c -Isrc -o $(BUILD_LIB_UTILS)/string_16.o $(SRC_LIB_UTILS)/string.c
223+
gcc -m16 -fno-pie -c -Isrc -o $(BUILD_LIB_UTILS)/color_16.o $(SRC_LIB_UTILS)/color.c
224+
gcc -m16 -fno-pie -c -D__SOURCE_SNAPSHOT__=$(SOURCE_SNAPSHOT) -Isrc -o $(BUILD_LIB_UTILS)/panic_16_c.o $(SRC_LIB_UTILS)/panic.c
225+
nasm -o $(BUILD_LIB_UTILS)/panic_16_asm.o -f elf32 $(SRC_LIB_UTILS)/panic.asm
226+
gcc -m16 -fno-pie -c -Isrc -o $(BUILD_LIB_UTILS)/time_16_c.o $(SRC_LIB_UTILS)/time.c
227+
nasm -o $(BUILD_LIB_UTILS)/time_16_asm.o -f elf32 $(SRC_LIB_UTILS)/time.asm
228+
ar rc $@ $(BUILD_LIB_UTILS)/basic_16_asm.o $(BUILD_LIB_UTILS)/basic_16_c.o $(BUILD_LIB_UTILS)/logging_16.o $(BUILD_LIB_UTILS)/output_16.o $(BUILD_LIB_UTILS)/string_16.o $(BUILD_LIB_UTILS)/color_16.o $(BUILD_LIB_UTILS)/panic_16_c.o $(BUILD_LIB_UTILS)/panic_16_asm.o $(BUILD_LIB_UTILS)/time_16_c.o $(BUILD_LIB_UTILS)/time_16_asm.o
229+
230+
$(BUILD_LIB_UTILS)/libutils: $(SRC_LIB_UTILS)/basic.c $(SRC_LIB_UTILS)/basic.asm $(SRC_LIB_UTILS)/logging.c $(SRC_LIB_UTILS)/output.c $(SRC_LIB_UTILS)/output.h $(SRC_LIB_UTILS)/input.c $(SRC_LIB_UTILS)/input.h $(SRC_LIB_UTILS)/string.c $(SRC_LIB_UTILS)/string.h $(SRC_LIB_UTILS)/panic.c $(SRC_LIB_UTILS)/panic.h $(SRC_LIB_UTILS)/panic.asm $(SRC_LIB_UTILS)/time.c $(SRC_LIB_UTILS)/time.h $(SRC_LIB_UTILS)/time.asm $(SRC_LIB_UTILS)/color.c $(SRC_LIB_UTILS)/color.h
226231
mkdir -p $(BUILD_LIB_UTILS)/
232+
gcc -m32 -fno-pie -c -Isrc -o $(BUILD_LIB_UTILS)/basic_c.o $(SRC_LIB_UTILS)/basic.c
233+
nasm -o $(BUILD_LIB_UTILS)/basic_asm.o -f elf32 $(SRC_LIB_UTILS)/basic.asm
234+
gcc -m32 -fno-pie -c -Isrc -o $(BUILD_LIB_UTILS)/logging.o $(SRC_LIB_UTILS)/logging.c
227235
gcc -m32 -fno-pie -c -Isrc -o $(BUILD_LIB_UTILS)/output.o $(SRC_LIB_UTILS)/output.c
228236
gcc -m32 -fno-pie -c -Isrc -o $(BUILD_LIB_UTILS)/input.o $(SRC_LIB_UTILS)/input.c
229237
nasm -o $(BUILD_LIB_UTILS)/input_asm.o -f elf32 $(SRC_LIB_UTILS)/input.asm
@@ -233,7 +241,7 @@ $(BUILD_LIB_UTILS)/libutils: $(SRC_LIB_UTILS)/output.c $(SRC_LIB_UTILS)/output.h
233241
nasm -o $(BUILD_LIB_UTILS)/panic_asm.o -f elf32 $(SRC_LIB_UTILS)/panic.asm
234242
gcc -m32 -fno-pie -c -Isrc -o $(BUILD_LIB_UTILS)/time_c.o $(SRC_LIB_UTILS)/time.c
235243
nasm -o $(BUILD_LIB_UTILS)/time_asm.o -f elf32 $(SRC_LIB_UTILS)/time.asm
236-
ar rc $@ $(BUILD_LIB_UTILS)/output.o $(BUILD_LIB_UTILS)/input_asm.o $(BUILD_LIB_UTILS)/input.o $(BUILD_LIB_UTILS)/string.o $(BUILD_LIB_UTILS)/color.o $(BUILD_LIB_UTILS)/panic_c.o $(BUILD_LIB_UTILS)/panic_asm.o $(BUILD_LIB_UTILS)/time_c.o $(BUILD_LIB_UTILS)/time_asm.o
244+
ar rc $@ $(BUILD_LIB_UTILS)/basic_asm.o $(BUILD_LIB_UTILS)/basic_c.o $(BUILD_LIB_UTILS)/logging.o $(BUILD_LIB_UTILS)/output.o $(BUILD_LIB_UTILS)/input_asm.o $(BUILD_LIB_UTILS)/input.o $(BUILD_LIB_UTILS)/string.o $(BUILD_LIB_UTILS)/color.o $(BUILD_LIB_UTILS)/panic_c.o $(BUILD_LIB_UTILS)/panic_asm.o $(BUILD_LIB_UTILS)/time_c.o $(BUILD_LIB_UTILS)/time_asm.o
237245

238246
$(BUILD_LIB_DS)/libds: $(SRC_LIB_DS)/queue.h $(SRC_LIB_DS)/queue.c
239247
mkdir -p $(BUILD_LIB_DS)/

src/bootloader/stage2.c

Lines changed: 22 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <lib/utils/color.h>
22
#include <drivers/disk/disk.h>
33
#include <lib/utils/output.h>
4+
#include <lib/utils/logging.h>
45
#include <lib/utils/time.h>
56

67
#define GDT_TABLE_SIZE 7
@@ -42,9 +43,9 @@ extern void enter_protected_mode();
4243
extern void label_exit();
4344

4445
void populate_gdt_table() {
45-
// Assumption DS = 0
46-
// Populate simple overlapping code and data segment.
46+
print_log("Populating GDT Table at 0x%d", gdt_table);
4747

48+
// NULL selector
4849
populate_gct_entry(
4950
&gdt_table[0],
5051
0,0,0,0);
@@ -91,50 +92,36 @@ void populate_gdt_table() {
9192
gdtr->size = (sizeof(gdt_table));
9293

9394
// Print the table and table addresse.
94-
move_xy(8,15);
95-
print_int((int)gdtr);
96-
move_xy(8,16);
97-
print_int(gdtr->base_address);
98-
move_xy(8,17);
99-
print_int(gdtr->size);
100-
for(int i=0;i<GDT_TABLE_SIZE;i++) {
101-
move_xy(8,18+i);
102-
print_memory_hex(8*i+(char*)&gdt_table, 8);
95+
print_log("GDTR: 0x%x; base address: 0x%x, size: %d",
96+
(int)gdtr, gdtr->base_address, gdtr->size);
97+
int gdt_entries = gdtr->size/sizeof(struct GDTEntry);
98+
for(int i=0;i<gdt_entries;i++) {
99+
print_log(" GDT Entry %d: %x%x",
100+
i,
101+
*(int*)(8*i+(int)gdt_table),
102+
*(int*)(8*i+4+(int)gdt_table));
103103
}
104104
}
105105

106106
void load_kernel() {
107-
// As we are in real mode with DS as 0
108-
// MEMORY_LOCATION_KERNEL should be within 16 bit for now.
107+
print_log("Loading Kernel");
109108
int err = load_sectors(MEMORY_LOCATION_KERNEL, 0x80, SECTOR_START_KERNEL, SECTOR_COUNT_KERNEL);
110109
if(err) {
111-
print_line("Failed to load kernel in memory: ");
112-
print_int(err);
110+
print_log("Failed to load kernel in memory: %d", err);
113111
label_exit();
114112
} else {
115-
print_memory_hex((char*)MEMORY_LOCATION_KERNEL, 16);
116-
}
117-
}
118-
119-
void load_calc() {
120-
int err = load_sectors(0x2000, 0x80, 27, 25);
121-
if(err) {
122-
print_line("Failed to load calc in memory.");
123-
print_int(err);
124-
label_exit();
125-
} else {
126-
print_memory_hex((char*)0x2000, 16);
113+
print_log("Kernel loaded at 0x%x: %x...", MEMORY_LOCATION_KERNEL, *(int*)MEMORY_LOCATION_KERNEL);
127114
}
128115
}
129116

130117
void load_static_library() {
131-
int err = load_sectors(0x7E00, 0x80, SECTOR_START_SHARED_LIBRARY, SECTOR_COUNT_SHARED_LIBRARY);
118+
print_log("Loading Static Library");
119+
int err = load_sectors(MEMORY_STATIC_LIBRARY, 0x80, SECTOR_START_SHARED_LIBRARY, SECTOR_COUNT_SHARED_LIBRARY);
132120
if(err) {
133-
print_line("Failed to load calc in memory.");
134-
print_int(err);
121+
print_log("Failed to load static library in memory: %d", err);
135122
label_exit();
136123
} else {
137-
print_memory_hex((char*)0x7E00, 16);
124+
print_log("Static library loaded at 0x%x: %x...", MEMORY_STATIC_LIBRARY, *(int*)MEMORY_STATIC_LIBRARY);
138125
}
139126
}
140127

@@ -144,16 +131,14 @@ void entry_stage() {
144131
set_color_fg(C_GREEN);
145132
print_line("C says 'Hello World'");
146133
set_color_fg(C_WHITE);
147-
move_xy(6, 12);
148-
print_line("Loading Static Library....");
134+
135+
print_log("");
149136
load_static_library();
150-
move_xy(6, 13);
151-
print_line("Loading Kernel....");
152137
load_kernel();
153-
move_xy(6, 14);
154-
print_line("Enabling Protected Mode...");
155138
populate_gdt_table();
139+
156140
// Enter_protected_mode never returns.
141+
print_log("Entering protected mode");
157142
enter_protected_mode();
158143
// And thus PC should never reach here :)
159144
}

src/drivers/display/text_mode.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
#define TEXT_WINDOW_HEIGHT 0x19
44
#define TEXT_WINDOW_WIDTH 0x50
55

6-
unsigned char get_display_text_x();
7-
unsigned char get_display_text_y();
8-
void set_display_text_x(unsigned char x);
9-
void set_display_text_y(unsigned char y);
10-
void set_display_text_xy(unsigned char x, unsigned char y);
6+
int get_display_text_x();
7+
int get_display_text_y();
8+
void set_display_text_x(int x);
9+
void set_display_text_y(int y);
10+
void set_display_text_xy(int x, int y);
1111

12-
void io_low_scroll_screen(unsigned char count, unsigned char color,
13-
unsigned char x1, unsigned char y1,
14-
unsigned char x2, unsigned char y2);
12+
void io_low_scroll_screen(char count, unsigned char color,
13+
int x1, int y1,
14+
int x2, int y2);
1515

1616
void io_low_put_char(char c, unsigned char color);

src/drivers/display/text_mode_bios.asm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@ global _low_read_char
3939
push ebp
4040
mov ebp, esp
4141

42-
mov ah, 0x06 ; (scroll)
4342
mov al, [ebp + 0x08] ; (line count)
4443
mov bh, [ebp + 0x0c] ; (attribute)
4544
mov cl, [ebp + 0x10] ; (window top-left x)
4645
mov ch, [ebp + 0x14] ; (window top-left y)
4746
mov dl, [ebp + 0x18] ; (window bottom-right x)
4847
mov dh, [ebp + 0x1c] ; (window bottom-right y)
48+
mov ah, [ebp + 0x20] ; (scroll up:0x06, down:0x07)
4949
int 0x10
5050

5151
mov esp, ebp

src/drivers/display/text_mode_bios.c

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,40 +10,45 @@ extern void _low_move_xy(unsigned char x, unsigned char y, unsigned char page);
1010
// _low_scroll_screen with count 0 implies fill.
1111
extern void _low_scroll_screen(unsigned char count, unsigned char color,
1212
unsigned char x1,unsigned char y1,
13-
unsigned char x2, unsigned char y2);
13+
unsigned char x2, unsigned char y2,
14+
unsigned char scroll_ah_value);
1415
extern char _low_read_char();
1516

16-
unsigned char IO_CURRENT_X = 0;
17-
unsigned char IO_CURRENT_Y = 0;
17+
int IO_CURRENT_X = 0;
18+
int IO_CURRENT_Y = 0;
1819

19-
unsigned char get_display_text_x() {
20+
int get_display_text_x() {
2021
return IO_CURRENT_X;
2122
}
2223

23-
unsigned char get_display_text_y() {
24+
int get_display_text_y() {
2425
return IO_CURRENT_Y;
2526
}
2627

27-
void set_display_text_x(unsigned char x) {
28+
void set_display_text_x(int x) {
2829
IO_CURRENT_X = x;
2930
_low_move_xy(IO_CURRENT_X, IO_CURRENT_Y, 0);
3031
}
3132

32-
void set_display_text_y(unsigned char y) {
33+
void set_display_text_y(int y) {
3334
IO_CURRENT_Y = y;
3435
_low_move_xy(IO_CURRENT_X, IO_CURRENT_Y, 0);
3536
}
3637

37-
void set_display_text_xy(unsigned char x, unsigned char y) {
38+
void set_display_text_xy(int x, int y) {
3839
IO_CURRENT_X = x;
3940
IO_CURRENT_Y = y;
4041
_low_move_xy(IO_CURRENT_X, IO_CURRENT_Y, 0);
4142
}
4243

43-
void io_low_scroll_screen(unsigned char count, unsigned char color,
44-
unsigned char x1,unsigned char y1,
45-
unsigned char x2, unsigned char y2) {
46-
_low_scroll_screen(count, color, x1, y1, x2, y2);
44+
void io_low_scroll_screen(char count, unsigned char color,
45+
int x1,int y1,
46+
int x2, int y2) {
47+
if(count>=0) {
48+
_low_scroll_screen(count, color, x1, y1, x2, y2, 0x06);
49+
} else {
50+
_low_scroll_screen(-count, color, x1, y1, x2, y2, 0x07);
51+
}
4752
}
4853

4954
void io_low_put_char(char c, unsigned char color) {

src/drivers/display/text_mode_vga.c

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,46 +4,46 @@ extern void _low_put_char(char c, unsigned char color, unsigned int xy);
44

55
extern void _low_vga_copy_step(unsigned int xy1, unsigned int xy2, unsigned int count);
66

7-
static unsigned int location_xy = 0;
7+
static int location_xy = 0;
88

9-
unsigned char IO_CURRENT_X = 0;
10-
unsigned char IO_CURRENT_Y = 0;
9+
int IO_CURRENT_X = 0;
10+
int IO_CURRENT_Y = 0;
1111

12-
unsigned char get_display_text_x() {
12+
int get_display_text_x() {
1313
return IO_CURRENT_X;
1414
}
1515

16-
unsigned char get_display_text_y() {
16+
int get_display_text_y() {
1717
return IO_CURRENT_Y;
1818
}
1919

20-
void set_display_text_x(unsigned char x) {
20+
void set_display_text_x(int x) {
2121
IO_CURRENT_X = x;
2222
location_xy = IO_CURRENT_X + IO_CURRENT_Y*TEXT_WINDOW_WIDTH;
2323
}
2424

25-
void set_display_text_y(unsigned char y) {
25+
void set_display_text_y(int y) {
2626
IO_CURRENT_Y = y;
2727
location_xy = IO_CURRENT_X + IO_CURRENT_Y*TEXT_WINDOW_WIDTH;
2828
}
2929

30-
void set_display_text_xy(unsigned char x, unsigned char y) {
30+
void set_display_text_xy(int x, int y) {
3131
IO_CURRENT_X = x;
3232
IO_CURRENT_Y = y;
3333
location_xy = IO_CURRENT_X + IO_CURRENT_Y*TEXT_WINDOW_WIDTH;
3434
}
3535

36-
void io_low_scroll_screen(unsigned char count, unsigned char color,
37-
unsigned char x1, unsigned char y1,
38-
unsigned char x2, unsigned char y2) {
36+
void io_low_scroll_screen(char count, unsigned char color,
37+
int x1, int y1,
38+
int x2, int y2) {
3939
if (count == 0) {
4040
for (int r = y1; r <= y2; ++r) {
4141
int index = r*TEXT_WINDOW_WIDTH;
4242
for (int c = x1; c <= x2; ++c, index++) {
4343
_low_put_char(' ', color, index);
4444
}
4545
}
46-
} else if (count > 0 ) {
46+
} else if (count > 0) {
4747
// Not yet tested.
4848
int width = x2-x1+1;
4949
for (int r1=y1,r2 = y1+count; r2 <= y2; r1++,r2++) {
@@ -59,7 +59,7 @@ void io_low_scroll_screen(unsigned char count, unsigned char color,
5959
} else {
6060
// Not yet tested.
6161
int width = x2-x1+1;
62-
for (int r1=y2,r2 = y2-count; r2 >= y1; r1--,r2--) {
62+
for (int r1=y2,r2 = y2+count; r2 >= y1; r1--,r2--) {
6363
_low_vga_copy_step(
6464
r2*TEXT_WINDOW_WIDTH + x1,
6565
r1*TEXT_WINDOW_WIDTH + x1,

0 commit comments

Comments
 (0)