aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarvin Borner2020-07-25 17:41:54 +0200
committerMarvin Borner2020-07-25 17:41:54 +0200
commit6e8fd8c61a77e4eb08d859f8e0fc42226cc4c7f3 (patch)
treeb0b23183affa278eff2c7c142e89e47dc40d9dd9
parentbe9ade5ac65209277dc18f8eb9b03a55e2b9e6ad (diff)
Added very basic PSF parser
Version 1 works; Version 2 not so much
-rw-r--r--Makefile13
-rwxr-xr-xrun13
-rw-r--r--src/drivers/vesa.c25
-rw-r--r--src/features/psf.c75
-rw-r--r--src/inc/psf.h48
-rw-r--r--src/inc/vesa.h7
-rw-r--r--src/main.c8
7 files changed, 166 insertions, 23 deletions
diff --git a/Makefile b/Makefile
index b0326a0..d1576dc 100644
--- a/Makefile
+++ b/Makefile
@@ -9,6 +9,7 @@ COBJS = src/main.o \
src/drivers/keyboard.o \
src/drivers/ide.o \
src/features/fs.o \
+ src/features/psf.o \
src/lib/str.o \
src/lib/mem.o \
src/lib/math.o \
@@ -26,17 +27,17 @@ ASFLAGS = -f elf32
all: compile clean
%.o: %.c
- $(CC) $(CFLAGS) $< -o $@
+ @$(CC) $(CFLAGS) $< -o $@
%_asm.o: %.asm
- $(AS) $(ASFLAGS) $< -o $@
+ @$(AS) $(ASFLAGS) $< -o $@
kernel: $(COBJS)
compile: kernel
- mkdir -p build/
- $(AS) -f bin src/entry.asm -o build/boot.bin
- $(LD) -N -emain -Ttext 0x00050000 -o build/kernel.bin $(COBJS) --oformat binary
+ @mkdir -p build/
+ @$(AS) -f bin src/entry.asm -o build/boot.bin
+ @$(LD) -N -emain -Ttext 0x00050000 -o build/kernel.bin $(COBJS) --oformat binary
clean:
- find src/ -name "*.o" -type f -delete
+ @find src/ -name "*.o" -type f -delete
diff --git a/run b/run
index 63ccc21..bf79de2 100755
--- a/run
+++ b/run
@@ -76,6 +76,7 @@ make_disk() {
make_build() {
if ! [ -d "disk/" ]; then
+ echo "Creating disk..."
make_disk
fi
@@ -86,9 +87,9 @@ make_build() {
make
# Create disk image
- dd if=/dev/zero of=build/disk.img bs=1k count=16k
- sudo mke2fs build/disk.img >/dev/null
- dd if=build/boot.bin of=build/disk.img conv=notrunc
+ dd if=/dev/zero of=build/disk.img bs=1k count=16k status=none
+ sudo mke2fs -q build/disk.img
+ dd if=build/boot.bin of=build/disk.img conv=notrunc status=none
cp build/kernel.bin . # For nicer disk img
./ext2util/ext2util -x build/disk.img -wf kernel.bin -i 5 >/dev/null
rm kernel.bin
@@ -130,13 +131,13 @@ make_sync() {
make_tidy() {
shfmt -w ./run
- find ./src -type f -regex '.*\.\(c\|h\)' -exec clang-format -i {} \;
+ find src/ -type f -regex '.*\.\(c\|h\)' -exec clang-format -i {} \;
# This may or may not work
- find ./src -type f -print0 | xargs -0 -l -i sh -c '[ -n "$(tail -c1 {})" ] && printf "\n" >> {}'
+ find src/ -type f -print0 | xargs -0 -l -i sh -c '[ -n "$(tail -c1 {})" ] && printf "\n" >> {}'
}
make_clean() {
- rm -rf ./build/
+ rm -rf build/
}
if [ "${mode}" = "cross" ]; then
diff --git a/src/drivers/vesa.c b/src/drivers/vesa.c
index 54e7e34..6bafd9e 100644
--- a/src/drivers/vesa.c
+++ b/src/drivers/vesa.c
@@ -1,12 +1,8 @@
#include <def.h>
#include <vesa.h>
-void vesa_draw_rectangle(int x1, int y1, int x2, int y2, const u8 color[3])
+void vesa_draw_rectangle(int x1, int y1, int x2, int y2, const u32 color[3])
{
- int vbe_bpl = vbe->bpp >> 3;
- int vbe_pitch = vbe->pitch;
- u8 *fb = (u8 *)vbe->framebuffer;
-
int pos1 = x1 * vbe_bpl + y1 * vbe_pitch;
u8 *draw = &fb[pos1];
for (int i = 0; i <= y2 - y1; i++) {
@@ -19,7 +15,24 @@ void vesa_draw_rectangle(int x1, int y1, int x2, int y2, const u8 color[3])
}
}
-void vesa_fill(const u8 color[3])
+void vesa_set_pixel(u16 x, u16 y, const u32 color[3])
+{
+ u8 pos = x * vbe_bpl + y * vbe_pitch;
+ u8 *draw = &fb[pos];
+ draw[pos] = (char)color[2];
+ draw[pos + 1] = (char)color[1];
+ draw[pos + 2] = (char)color[0];
+}
+
+void vesa_fill(const u32 color[3])
{
vesa_draw_rectangle(0, 0, vbe->width - 1, vbe->height - 1, color);
}
+
+void vesa_init(struct vbe *info)
+{
+ vbe = info;
+ vbe_bpl = vbe->bpp >> 3;
+ vbe_pitch = vbe->pitch;
+ fb = (u8 *)vbe->framebuffer;
+}
diff --git a/src/features/psf.c b/src/features/psf.c
new file mode 100644
index 0000000..7b6a90a
--- /dev/null
+++ b/src/features/psf.c
@@ -0,0 +1,75 @@
+// MIT License, Copyright (c) 2020 Marvin Borner
+// PSF parser
+
+#include <def.h>
+#include <print.h>
+#include <psf.h>
+#include <vesa.h>
+
+// Verifies the PSF magics
+// Returns the PSF version or 0
+int psf_verify(char *data)
+{
+ struct psf1_header *header1 = (struct psf1_header *)data;
+ struct psf2_header *header2 = (struct psf2_header *)data;
+
+ if (header1->magic[0] == PSF1_MAGIC_0 && header1->magic[1] == PSF1_MAGIC_1)
+ return 1;
+ else if (header2->magic[0] == PSF2_MAGIC_0 && header2->magic[1] == PSF2_MAGIC_1 &&
+ header2->magic[2] == PSF2_MAGIC_2 && header2->magic[3] == PSF2_MAGIC_3)
+ return 2;
+ else
+ return 0;
+}
+
+// Will be removed in the future
+void psf_test(char *chars)
+{
+ char ch = 'a';
+ int x = 50;
+ int y = 400;
+ const u32 c[3] = { 0xff, 0x00, 0x00 };
+
+ int font_height = 16;
+ int font_width = 8;
+
+ int pos = x * vbe_bpl + y * vbe_pitch;
+ char *draw = (char *)&fb[pos];
+ u16 row = 0;
+
+ for (int cy = 0; cy <= font_height; cy++) {
+ row = chars[ch * font_height + cy];
+
+ for (int cx = 0; cx <= font_width + 1; cx++) {
+ if (row & 0x80) {
+ draw[vbe_bpl * cx] = (char)c[2];
+ draw[vbe_bpl * cx + 1] = (char)c[1];
+ draw[vbe_bpl * cx + 2] = (char)c[0];
+ /* } else { */
+ /* draw[vbe_bpl * cx] = (char)c[2]; */
+ /* draw[vbe_bpl * cx + 1] = (char)terminal_background[1]; */
+ /* draw[vbe_bpl * cx + 2] = (char)terminal_background[0]; */
+ /* } */
+ }
+ row <<= 1;
+ }
+ draw += vbe_pitch;
+ }
+}
+
+char *psf_parse(char *data)
+{
+ int version = psf_verify(data);
+
+ char *chars;
+ if (version == 1)
+ chars = data + sizeof(struct psf1_header);
+ else if (version == 2)
+ chars = data + sizeof(struct psf2_header);
+ else
+ return 0;
+
+ psf_test(chars);
+
+ return chars;
+}
diff --git a/src/inc/psf.h b/src/inc/psf.h
new file mode 100644
index 0000000..3394f9c
--- /dev/null
+++ b/src/inc/psf.h
@@ -0,0 +1,48 @@
+// MIT License, Copyright (c) 2020 Marvin Borner
+// PSF parser
+
+#ifndef PSF_H
+#define PSF_H
+
+#include <def.h>
+
+/**
+ * PSF version 1
+ */
+
+#define PSF1_MAGIC_0 0x36
+#define PSF1_MAGIC_1 0x04
+#define PSF1_MODE_256 0
+#define PSF1_MODE_512 1
+#define PSF1_MODE_256_UNICODE 2
+#define PSF1_MODE_512_UNICODE 3
+
+struct psf1_header {
+ u8 magic[2];
+ u8 file_mode;
+ u8 font_height;
+};
+
+/**
+ * PSF version 2
+ */
+
+#define PSF2_MAGIC_0 0x72
+#define PSF2_MAGIC_1 0xb5
+#define PSF2_MAGIC_2 0x4a
+#define PSF2_MAGIC_3 0x86
+
+struct psf2_header {
+ u8 magic[4];
+ u32 version;
+ u32 size;
+ u32 flags;
+ u32 glyph_count;
+ u32 bytes_per_glyph;
+ u32 height;
+ u32 width;
+};
+
+char *psf_parse(char *data);
+
+#endif
diff --git a/src/inc/vesa.h b/src/inc/vesa.h
index c4863f1..e1ea521 100644
--- a/src/inc/vesa.h
+++ b/src/inc/vesa.h
@@ -44,7 +44,12 @@ struct vbe {
};
struct vbe *vbe;
+int vbe_bpl;
+int vbe_pitch;
+u8 *fb;
-void vesa_fill(const u8 color[3]);
+void vesa_fill(const u32 color[3]);
+void vesa_set_pixel(u16 x, u16 y, const u32 color[3]);
+void vesa_init(struct vbe *info);
#endif
diff --git a/src/main.c b/src/main.c
index 708f74d..f95680e 100644
--- a/src/main.c
+++ b/src/main.c
@@ -6,6 +6,7 @@
#include <interrupts.h>
#include <keyboard.h>
#include <print.h>
+#include <psf.h>
#include <serial.h>
#include <vesa.h>
@@ -19,16 +20,15 @@ void main(struct mem_info *mem_info, struct vid_info *vid_info)
keyboard_install();
mem_info++; // TODO: Use the mmap (or remove)!
- vbe = vid_info->info;
- u8 terminal_background[3] = { 0x1d, 0x1f, 0x24 };
+ vesa_init(vid_info->info);
+ u32 terminal_background[3] = { 0x1d, 0x1f, 0x24 };
vesa_fill(terminal_background);
serial_install();
- printf("hello\n");
ls_root();
- printf("%s", read_file("test.txt", 2));
+ psf_parse(read_file("/font/spleen-8x16.psfu"));
while (1) {
};