added working LFB driver and some helper scripts

This commit is contained in:
Morten Delenk 2017-04-29 11:06:31 +00:00
parent 182404b88f
commit 3382121c3a
8 changed files with 95 additions and 6 deletions

1
.gitignore vendored
View file

@ -10,3 +10,4 @@ build/
__pycache__/ __pycache__/
*.iso *.iso
iso/kernel iso/kernel
*.firm

2
buildtools/grub-iso.sh Executable file
View file

@ -0,0 +1,2 @@
cp build/kernel/kernel iso
grub-mkrescue -o bootable.iso iso

65
buildtools/mkfirm.py Executable file
View file

@ -0,0 +1,65 @@
#!/usr/bin/env python3
import sys
if len(sys.argv) < 4:
print("USAGE: mkfirm.py <kernel9.elf> <kernel11.elf> <output.firm>")
print("WARNING: Currently you cannot install this FIRM, because YOU'LL BRICK YOUR 3DS!")
sighax_sig = b'\x00'*256 #TODO insert sighax signature here.
import struct
import hashlib
def get_elf_seg(f): #Return entry,section_beg,section_size,section
f.seek(0)
if f.read(4) != b"\x7FELF":
raise Exception("Not an ELF file!")
if f.read(1) != b"\x01":
raise Exception("ELF64s are not supported!")
if f.read(1) != b"\x01":
raise Exception("Little Endian ELF required!")
if f.read(1) != b"\x01":
raise Exception("Unknown ELF version!")
if struct.unpack("<H",f.read(2))[0] != 2:
raise Exception("Invalid ELF format")
if struct.unpack("<H",f.read(2))[0] != 0x28:
raise Exception("This is not an ELF for ARM!")
if struct.unpack("<I",f.read(4))[0] != 1:
raise Exception("Unknown ELF version!")
loadAddr = struct.unpack("<I",f.read(4))[0]
phoff = struct.unpack("<I",f.read(4))[0]
f.seek(0x2C)
no_ent = struct.unpack("<H",f.read(2))[0]
f.seek(phoff)
section_begin = 0
section_size = 0
section = b''
for ent in range(no_ent):
if struct.unpack("<I",f.read(4)) != 1:
continue
off,vaddr,filesz,memsz=struct.unpack("<IIxxxxII",f.read(20))
section_begin = vaddr
section_size = memsz
f.seek(off)
section = f.read(filesz)
section = section + b'\x00'*(memsz-filesz)
break
print("Loading section of size {} to {} with entrypoint {}".format(section_size,hex(section_begin),hex(loadAddr)))
return (loadAddr,section_begin,section_size,section)
f1 = open(sys.argv[1],"rb")
f2 = open(sys.argv[2],"rb")
f3 = open(sys.argv[3],"wb")
arm9_entry,arm9_section_beg,arm9_section_size,arm9_section = get_elf_seg(f1)
arm11_entry,arm11_section_beg,arm11_section_size,arm11_section = get_elf_seg(f2)
arm9_hash=hashlib.sha256(arm9_section).digest()
arm11_hash=hashlib.sha256(arm11_section).digest()
arm9_off = 0x200
arm11_off = 0x200+arm9_section_size
f.write(b'FIRM')
f.write(struct.pack("<III",0,arm11_entry,arm9_entry))
f.write(b'\x00'*0x30)
def write_seg(section_beg, section_size, hash, off):
f.write(struct.pack("<IIII",off,section_beg,section_size,2))
f.write(hash)
write_seg(arm9_section_beg,arm9_section_size,arm9_hash,arm9_off)
write_seg(arm11_section_beg,arm11_section_size,arm11_hash,arm11_off)
f.write(b'\x00'*0x60)
f.write(sighax_sig)
f.write(arm9_section)
f.write(arm11_section)

3
buildtools/sighax-firm.sh Executable file
View file

@ -0,0 +1,3 @@
#!/bin/sh
echo "You need kernel9 in build/kernel/kernel9"
buildtools/mkfirm.py build/kernel/kernel9 build/kernel/kernel sighax.firm

View file

@ -7,8 +7,8 @@ auto Framebuffer::plotChar(int x, int y, int c) -> void {
if (width == 16) this->x++; if (width == 16) this->x++;
if (!width) return; if (!width) return;
int color; int color;
for (int px = x * 8; px < (x * 8) + width; x++) for (int px = x * 8; px < (x * 8) + width; px++)
for (int py = y * 16; py < (y * 16) + 16; y++) { for (int py = y * 16; py < (y * 16) + 16; py++) {
color = rgbColor; color = rgbColor;
if (!useRGB) { if (!useRGB) {
switch (curColor) { switch (curColor) {
@ -32,7 +32,19 @@ auto Framebuffer::plotChar(int x, int y, int c) -> void {
rgbColor = color; rgbColor = color;
useRGB = true; useRGB = true;
} }
plotPixel(px, py, color); unsigned char *chara = (unsigned char *)font_ptr[c];
int bx = px - (x * 8);
int by = py - (y * 16);
if (width == 16) {
by *= 2;
if (bx > 7) {
by++;
bx -= 8;
}
plotPixel(px, py, (chara[by] & (1 << (7 - bx))) ? color : 0);
} else {
plotPixel(px, py, (chara[by] & (1 << (7 - bx))) ? color : 0);
}
} }
} }
Framebuffer::Framebuffer(int height, int width) : TTY(height, width) {} Framebuffer::Framebuffer(int height, int width) : TTY(height, width) {}

View file

@ -1,8 +1,13 @@
#include "vesafb.hpp" #include "vesafb.hpp"
#include <stdint.h> #include <stdint.h>
auto VESAfb::plotPixel(int x, int y, int col) -> void { auto VESAfb::plotPixel(int x, int y, int col) -> void {
int *lfb = (int *)((uintptr_t)(mb_info->framebuffer_addr)); unsigned char *lfb = (unsigned char *)((uintptr_t)(mb_info->framebuffer_addr));
lfb[y * mb_info->framebuffer_pitch + x] = col; int off = y * mb_info->framebuffer_pitch / (mb_info->framebuffer_bpp / 8) + x;
off *= mb_info->framebuffer_bpp / 8;
for (int i = 0; i < mb_info->framebuffer_bpp / 8; i++) {
lfb[off++] = col;
col >>= 8;
}
} }
VESAfb::VESAfb(multiboot_info_t *mb_info) VESAfb::VESAfb(multiboot_info_t *mb_info)
: mb_info(mb_info), Framebuffer(mb_info->framebuffer_width / 8, mb_info->framebuffer_height / 16) {} : mb_info(mb_info), Framebuffer(mb_info->framebuffer_width / 8, mb_info->framebuffer_height / 16) {}

View file

@ -7,6 +7,7 @@ extern "C" void (*end_dtors)();
void main() { void main() {
for (auto ctor = &start_ctors; ctor < &end_ctors; ctor++) (**ctor)(); for (auto ctor = &start_ctors; ctor < &end_ctors; ctor++) (**ctor)();
drivers_init(); drivers_init();
*out << "Hallo!\n"; *out << "Hello!\n";
*out << "テスト\n";
for (auto dtor = &start_dtors; dtor != &end_dtors; dtor++) (**dtor)(); for (auto dtor = &start_dtors; dtor != &end_dtors; dtor++) (**dtor)();
} }