Skip to content

Commit

Permalink
drivers,fb: add framebuffer console output support
Browse files Browse the repository at this point in the history
Signed-off-by: Pawel Wieczorkiewicz <[email protected]>
  • Loading branch information
wipawel committed Feb 20, 2022
1 parent 282d531 commit 8d1ab33
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 0 deletions.
5 changes: 5 additions & 0 deletions common/console.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <spinlock.h>
#include <string.h>

#include <drivers/fb.h>
#include <drivers/serial.h>
#include <drivers/vga.h>

Expand Down Expand Up @@ -98,6 +99,10 @@ void vga_console_write(void *vga_memory, const char *buf, size_t len) {
vga_write(vga_memory, buf, len, VGA_WHITE);
}

void fb_console_write(void *fb_memory, const char *buf, size_t len) {
fb_write(fb_memory, buf, len, FB_WHITE);
}

void register_console_callback(console_callback_t cb, void *arg) {
console_callbacks[num_console_callbacks].cb = cb;
console_callbacks[num_console_callbacks++].arg = arg;
Expand Down
41 changes: 41 additions & 0 deletions drivers/fb/fb.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,15 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <console.h>
#include <drivers/fb.h>
#include <drivers/logo.h>
#include <ktf.h>
#include <multiboot.h>
#include <page.h>
#include <string.h>

extern uint64_t fonts[];

static uint32_t width;
static uint32_t height;
Expand Down Expand Up @@ -111,9 +115,21 @@ bool init_framebuffer(const multiboot_info_t *mbi) {
map_fb_area(mbi->framebuffer_addr, buffer_size);
memset(video_memory, 0, buffer_size);

register_console_callback(fb_console_write, video_memory);
return true;
}

void put_char(char c, uint32_t x, uint32_t y, uint32_t color) {
uint64_t font = fonts[(uint8_t) c];

for (int yy = 0; yy < 8; yy++) {
for (int xx = 0; xx < 8; xx++, font >>= 1) {
if (font & 1)
put_pixel(x + xx, y + yy, color);
}
}
}

void draw_line(uint32_t x1, uint32_t y1, uint32_t x2, uint32_t y2, uint32_t color) {
while (x1 <= x2 || y1 <= y2) {
put_pixel(x1, y1, color);
Expand All @@ -138,3 +154,28 @@ void draw_logo(void) {
static void clear_screen(void *fb_addr) {
memset((uint8_t *) video_memory + banner_size, 0, buffer_size - banner_size);
}

void fb_write(void *fb_addr, const char *buf, size_t len, uint32_t color) {
static uint32_t row = LOGO_HEIGHT + 8, col = 0;

for (unsigned int i = 0; i < len; i++) {
char c = buf[i];

if ((col + 8) > width || c == '\n') {
row += sizeof(fonts[0]);
col = 0;
}

if ((row + 8) > height) {
clear_screen(fb_addr);
row = LOGO_HEIGHT + 8;
col = 0;
}

if (c == '\n')
continue;

put_char(c, col, row, color);
col += sizeof(fonts[0]);
}
}
1 change: 1 addition & 0 deletions include/console.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ extern void printk(const char *fmt, ...);
extern void serial_console_write(void *arg, const char *buf, size_t len);
extern void qemu_console_write(void *arg, const char *buf, size_t len);
extern void vga_console_write(void *arg, const char *buf, size_t len);
extern void fb_console_write(void *arg, const char *buf, size_t len);

extern void register_console_callback(console_callback_t func, void *arg);

Expand Down
2 changes: 2 additions & 0 deletions include/drivers/fb.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@
#define FB_WHITE 0xFFFFFFFF

extern bool init_framebuffer(const multiboot_info_t *mbi);
extern void put_char(char c, uint32_t x, uint32_t y, uint32_t color);
extern void draw_line(uint32_t x1, uint32_t y1, uint32_t x2, uint32_t y2, uint32_t color);
extern void draw_logo(void);
extern void fb_write(void *fb_addr, const char *buf, size_t len, uint32_t color);

#endif /* KTF_DRV_FB_H */

0 comments on commit 8d1ab33

Please sign in to comment.