diff options
author | dzwdz | 2022-08-06 00:12:51 +0200 |
---|---|---|
committer | dzwdz | 2022-08-06 00:12:51 +0200 |
commit | 03c6f3cf458c89df17b02557cd232f9cde73ed54 (patch) | |
tree | a89076307f32d9df6eec550f175accaa73d72408 /src | |
parent | 641b857dcfe1cfca625fbd75646b743142a9069c (diff) |
make snprintf shared; dynamic resolution support
Diffstat (limited to 'src')
-rw-r--r-- | src/kernel/arch/amd64/boot.c | 1 | ||||
-rw-r--r-- | src/kernel/arch/amd64/driver/video.c | 21 | ||||
-rw-r--r-- | src/kernel/arch/amd64/driver/video.h | 1 | ||||
-rw-r--r-- | src/kernel/arch/amd64/multiboot.s | 4 | ||||
-rw-r--r-- | src/shared/mem.h | 4 | ||||
-rw-r--r-- | src/shared/printf.c | 31 | ||||
-rw-r--r-- | src/user/app/vterm/vterm.c | 52 | ||||
-rw-r--r-- | src/user/app/vterm/vterm.h | 1 | ||||
-rw-r--r-- | src/user/lib/include/stdio.h | 2 | ||||
-rw-r--r-- | src/user/lib/printf.c | 27 |
10 files changed, 98 insertions, 46 deletions
diff --git a/src/kernel/arch/amd64/boot.c b/src/kernel/arch/amd64/boot.c index 570cebb..2707223 100644 --- a/src/kernel/arch/amd64/boot.c +++ b/src/kernel/arch/amd64/boot.c @@ -52,6 +52,7 @@ void kmain_early(struct multiboot_info *multiboot) { vid.width = multiboot->framebuffer_width; vid.height = multiboot->framebuffer_height; vid.bpp = multiboot->framebuffer_bpp; + vid.size = vid.pitch * vid.height; kprintf("framebuffer at 0x%x, %ux%u bpp %u\n", vid.b, vid.width, vid.height, vid.bpp); video_init(vid); diff --git a/src/kernel/arch/amd64/driver/video.c b/src/kernel/arch/amd64/driver/video.c index 2d1ac03..d22adc4 100644 --- a/src/kernel/arch/amd64/driver/video.c +++ b/src/kernel/arch/amd64/driver/video.c @@ -5,8 +5,12 @@ #include <kernel/mem/virt.h> #include <kernel/panic.h> #include <kernel/vfs/request.h> +#include <shared/mem.h> +#include <shared/printf.h> static struct fb_info fb; +static char namebuf[64]; +static size_t namelen; enum { H_ROOT, @@ -16,9 +20,10 @@ enum { static int handle(struct vfs_request *req) { switch (req->type) { case VFSOP_OPEN: + if (!req->input.kern) panic_invalid_state(); if (req->input.len == 1) { return H_ROOT; - } else if (req->input.len == 2 && req->input.kern && req->input.buf_kern[1] == 'b') { + } else if (req->input.len == namelen + 1 && !memcmp(req->input.buf_kern + 1, namebuf, namelen)) { return H_FB; } else { return -1; @@ -26,26 +31,26 @@ static int handle(struct vfs_request *req) { case VFSOP_READ: if (req->id == H_ROOT) { - // TODO list available modes, e.g /640x480x24 - const char src[] = "b"; - return req_readcopy(req, src, sizeof src); + return req_readcopy(req, namebuf, namelen + 1); } else { - return req_readcopy(req, fb.b, fb.pitch * fb.height); + return req_readcopy(req, fb.b, fb.size); } case VFSOP_WRITE: if ((long)req->id != H_FB) { return -1; } - fs_normslice(&req->offset, &req->input.len, fb.pitch * fb.height, false); + fs_normslice(&req->offset, &req->input.len, fb.size, false); if (!virt_cpy_from(req->caller->pages, fb.b + req->offset, req->input.buf, req->input.len)) { - panic_invalid_state(); return -EFAULT; } return req->input.len; + case VFSOP_GETSIZE: + return fb.size; + default: return -1; } @@ -67,5 +72,7 @@ static bool is_ready(struct vfs_backend *self) { static struct vfs_backend backend = BACKEND_KERN(is_ready, accept); void video_init(struct fb_info fb_) { fb = fb_; + snprintf(namebuf, sizeof namebuf, "%ux%ux%u", fb.width, fb.height, fb.bpp); + namelen = strlen(namebuf); vfs_mount_root_register("/video", &backend); } diff --git a/src/kernel/arch/amd64/driver/video.h b/src/kernel/arch/amd64/driver/video.h index c1680c4..dfc1710 100644 --- a/src/kernel/arch/amd64/driver/video.h +++ b/src/kernel/arch/amd64/driver/video.h @@ -5,6 +5,7 @@ struct fb_info { char *b; uint32_t width, height; uint32_t pitch; /* width in bytes of a single scanline */ + size_t size; uint8_t bpp; }; diff --git a/src/kernel/arch/amd64/multiboot.s b/src/kernel/arch/amd64/multiboot.s index c809297..4af0cd0 100644 --- a/src/kernel/arch/amd64/multiboot.s +++ b/src/kernel/arch/amd64/multiboot.s @@ -15,6 +15,6 @@ multiboot_header: .skip 5 * 4 .long 0 - .long 640 - .long 480 .long 0 + .long 0 + .long 32 diff --git a/src/shared/mem.h b/src/shared/mem.h index 8c83f2f..aac87f5 100644 --- a/src/shared/mem.h +++ b/src/shared/mem.h @@ -1,4 +1,5 @@ #pragma once +#include <stdarg.h> #include <stddef.h> /* note: (partially) tested in the userland tests */ @@ -11,3 +12,6 @@ void *memset(void *s, int c, size_t n); int strcmp(const char *s1, const char *s2); size_t strlen(const char *s); + +int snprintf(char *restrict str, size_t len, const char *restrict fmt, ...); +int vsnprintf(char *restrict str, size_t len, const char *restrict fmt, va_list ap); diff --git a/src/shared/printf.c b/src/shared/printf.c index c1206f3..2dc3048 100644 --- a/src/shared/printf.c +++ b/src/shared/printf.c @@ -1,6 +1,7 @@ -#include <stdbool.h> #include <shared/mem.h> #include <shared/printf.h> +#include <stdarg.h> +#include <stdbool.h> struct out_state { void (*back)(void *, const char *, size_t); @@ -165,3 +166,31 @@ int __printf_internal(const char *fmt, va_list argp, seg = fmt; } } + + +static void vsnprintf_backend(void *arg, const char *buf, size_t len) { + char **ptrs = arg; + size_t space = ptrs[1] - ptrs[0]; + if (len > space) len = space; + + memcpy(ptrs[0], buf, len); + ptrs[0] += len; + /* ptrs[1] is the last byte of the buffer, it must be 0. + * on overflow: + * ptrs[0] + (ptrs[1] - ptrs[0]) = ptrs[1] */ + *ptrs[0] = '\0'; +} + +int vsnprintf(char *restrict str, size_t len, const char *restrict fmt, va_list ap) { + char *ptrs[2] = {str, str + len - 1}; + return __printf_internal(fmt, ap, vsnprintf_backend, &ptrs); +} + +int snprintf(char *restrict str, size_t len, const char *restrict fmt, ...) { + int ret; + va_list argp; + va_start(argp, fmt); + ret = vsnprintf(str, len, fmt, argp); + va_end(argp); + return ret; +} diff --git a/src/user/app/vterm/vterm.c b/src/user/app/vterm/vterm.c index 0468477..43180b2 100644 --- a/src/user/app/vterm/vterm.c +++ b/src/user/app/vterm/vterm.c @@ -1,7 +1,10 @@ #include "vterm.h" #include <camellia/syscalls.h> #include <stdbool.h> +#include <stdio.h> #include <stdlib.h> +#include <string.h> +#include <unistd.h> struct point cursor = {0}; @@ -26,15 +29,50 @@ void in_char(char c) { while (cursor.y * font.h >= fb.height) scroll(); } -int main(void) { - fb_fd = _syscall_open("/kdev/video/b", 13, 0); - // TODO don't hardcode size - fb.len = 640 * 480 * 4; - fb.width = 640; - fb.height = 480; - fb.pitch = 640 * 4; +int main(); + +void fb_setup(void) { + #define BASEPATH "/kdev/video/" + char path[64], *spec; + size_t pos; + FILE *f; + + f = fopen(BASEPATH, "r"); + if (!f) { + eprintf("couldn't open %s", BASEPATH); + exit(1); + } + + pos = strlen(BASEPATH); + memcpy(path, BASEPATH, pos); + spec = path + pos; + fread(spec, 1, sizeof(path) - pos, f); + /* assumes the read went correctly */ + fclose(f); + + fb_fd = _syscall_open(path, strlen(path), 0); + if (fb_fd < 0) { + eprintf("failed to open %s", path); + exit(1); + } + + fb.width = strtol(spec, &spec, 0); + if (*spec++ != 'x') { eprintf("bad filename format"); exit(1); } + fb.height = strtol(spec, &spec, 0); + if (*spec++ != 'x') { eprintf("bad filename format"); exit(1); } + fb.bpp = strtol(spec, &spec, 0); + fb.len = _syscall_getsize(fb_fd); + fb.pitch = fb.len / fb.height; fb.b = malloc(fb.len); + if (fb.bpp != 32) { + eprintf("unsupported format %ux%ux%u", fb.width, fb.height, fb.bpp); + exit(1); + } +} + +int main(void) { + fb_setup(); font_load("/init/font.psf"); static char buf[512]; diff --git a/src/user/app/vterm/vterm.h b/src/user/app/vterm/vterm.h index 5e09105..5cfc609 100644 --- a/src/user/app/vterm/vterm.h +++ b/src/user/app/vterm/vterm.h @@ -24,6 +24,7 @@ void font_blit(uint32_t glyph, int x, int y); struct framebuf { size_t len, width, height, pitch; + uint8_t bpp; char *b; }; extern struct framebuf fb; diff --git a/src/user/lib/include/stdio.h b/src/user/lib/include/stdio.h index 063c81f..4d9cf94 100644 --- a/src/user/lib/include/stdio.h +++ b/src/user/lib/include/stdio.h @@ -11,11 +11,9 @@ int printf(const char *restrict fmt, ...); int fprintf(FILE *restrict f, const char *restrict fmt, ...); -int snprintf(char *restrict str, size_t len, const char *restrict fmt, ...); int vprintf(const char *restrict fmt, va_list ap); int vfprintf(FILE *restrict f, const char *restrict fmt, va_list ap); -int vsnprintf(char *restrict str, size_t len, const char *restrict fmt, va_list ap); int _klogf(const char *fmt, ...); // for kernel debugging only diff --git a/src/user/lib/printf.c b/src/user/lib/printf.c index 53b1140..cb6561b 100644 --- a/src/user/lib/printf.c +++ b/src/user/lib/printf.c @@ -12,24 +12,6 @@ int vfprintf(FILE *restrict f, const char *restrict fmt, va_list ap) { return __printf_internal(fmt, ap, backend_file, f); } -static void backend_buf(void *arg, const char *buf, size_t len) { - char **ptrs = arg; - size_t space = ptrs[1] - ptrs[0]; - if (len > space) len = space; - - memcpy(ptrs[0], buf, len); - ptrs[0] += len; - /* ptrs[1] is the last byte of the buffer, it must be 0. - * on overflow: - * ptrs[0] + (ptrs[1] - ptrs[0]) = ptrs[1] */ - *ptrs[0] = '\0'; -} - -int vsnprintf(char *restrict str, size_t len, const char *restrict fmt, va_list ap) { - char *ptrs[2] = {str, str + len - 1}; - return __printf_internal(fmt, ap, backend_buf, &ptrs); -} - int printf(const char *restrict fmt, ...) { int ret; @@ -49,15 +31,6 @@ int fprintf(FILE *restrict f, const char *restrict fmt, ...) { return ret; } -int snprintf(char *restrict str, size_t len, const char *restrict fmt, ...) { - int ret; - va_list argp; - va_start(argp, fmt); - ret = vsnprintf(str, len, fmt, argp); - va_end(argp); - return ret; -} - int vprintf(const char *restrict fmt, va_list ap) { return vfprintf(stdout, fmt, ap); } |