Fixed merge conflict

This commit is contained in:
Jozef Nagy 2025-05-21 22:18:09 +02:00
commit 35c922d5ed
Signed by untrusted user who does not match committer: crz
GPG key ID: 459A4811CEAC7068
64 changed files with 3940 additions and 154 deletions

View file

@ -17,7 +17,10 @@
/* SOFTWARE. */
/*********************************************************************************/
#include <config/config.h>
#include <config/ini.h>
#include <lib/string.h>
#include <loader/loader.h>
#include <mm/mman.h>
#include <vfs/vfs.h>
#include <print.h>
@ -26,20 +29,51 @@
#include <stdint.h>
#include <stddef.h>
#define DEFAULT_ENTRY 0
// default timeout of 0 disables the UI entirely, which is essentialy what *should* happen now since the UI
// is... in a catastrophic state. Just remember to set this back to 30 once the UI is ready
#define DEFAULT_TIMEOUT 0
// #define DEFAULT_TIMEOUT 30
char *config_paths[] = {
"\\AxBoot\\axboot.cfg",
"\\axboot.cfg",
"\\System\\axboot.cfg",
"\\EFI\\axboot.cfg",
"\\EFI\\BOOT\\axboot.cfg",
};
struct axboot_cfg cfg = {
.default_entry = DEFAULT_ENTRY,
.timeout = DEFAULT_TIMEOUT,
.ui_mode = UI_TEXT,
//.entry_count = 0
.entry_count = 2
};
struct axboot_entry entries[2] = {
{
.name = "AurixOS",
.description = "Boot the Aurix Operating System",
.image_path = "\\System\\axkrnl",
.protocol = PROTO_AURIX
},
{
.name = "Windows 10",
.description = "",
.image_path = "\\EFI\\Microsoft\\bootmgfw.efi",
.protocol = PROTO_CHAINLOAD
}
};
void config_init(void)
{
char *config_buf;
char *config_buf = NULL;
uint8_t open = 0;
for (size_t i = 0; i < ARRAY_LENGTH(config_paths); i++) {
vfs_read("\\System\\axkrnl", &config_buf);
vfs_read(config_paths[i], &config_buf);
if (config_buf != NULL) {
open = 1;
break;
@ -52,7 +86,30 @@ void config_init(void)
while (1);
}
// TODO: parse configuration file
mem_free(config_buf);
}
int config_get_timeout()
{
return cfg.timeout;
}
int config_get_default()
{
return cfg.default_entry;
}
int config_get_entry_count()
{
return cfg.entry_count;
}
struct axboot_entry *config_get_entries()
{
return entries;
}
int config_get_ui_mode()
{
return cfg.ui_mode;
}

130
boot/common/data/list.c Normal file
View file

@ -0,0 +1,130 @@
/*********************************************************************************/
/* Module Name: list.c */
/* Project: AurixOS */
/* */
/* Copyright (c) 2024-2025 Jozef Nagy */
/* */
/* This source is subject to the MIT License. */
/* See License.txt in the root of this repository. */
/* All other rights reserved. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR */
/* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, */
/* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE */
/* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER */
/* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, */
/* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE */
/* SOFTWARE. */
/*********************************************************************************/
#include <data/list.h>
#include <mm/mman.h>
#include <print.h>
#include <stdint.h>
List *list_new()
{
List *list = (List *)mem_alloc(sizeof(List));
if (!list) {
debug("list_new(): Failed to allocate memory for new list!\n");
return NULL;
}
list->count = 0;
list->root = (ListNode *)0;
return list;
}
ListNode *listnode_new(void *data)
{
ListNode *ln = (ListNode *)mem_alloc(sizeof(ListNode));
if (!ln) {
debug("listnode_new(): Failed to allocate memory for new list node!\n");
return NULL;
}
ln->prev = (ListNode *)0;
ln->next = (ListNode *)0;
ln->data = data;
return ln;
}
int list_add(List *list, void *data)
{
ListNode *node = listnode_new(data);
if (!node) {
return false;
}
if (!list->root) {
list->root = node;
} else {
ListNode *cur = list->root;
while (cur->next) {
cur = cur->next;
}
cur->next = node;
node->prev = cur;
}
list->count++;
return true;
}
void *list_remove_at(List *list, uint32_t idx)
{
void *data;
if (!list || list->count == 0 || idx >= list->count) {
return NULL;
}
ListNode *cur = list->root;
for (uint32_t i = 0; (i < idx) && cur; i++) {
cur = cur->next;
}
if (!cur) {
return NULL;
}
data = cur->data;
if (cur->prev) {
cur->prev->next = cur->next;
}
if (cur->next) {
cur->next->prev = cur->prev;
}
if (idx == 0) {
list->root = cur->next;
}
mem_free(cur);
list->count--;
return data;
}
void *list_get_at(List *list, uint32_t idx)
{
if (!list || list->count == 0 | idx >= list->count) {
return NULL;
}
ListNode *cur = list->root;
for (uint32_t i = 0; (i < idx) && cur; i++) {
cur = cur->next;
}
return cur ? cur->data : NULL;
}

29
boot/common/i18n/cs_CZ.c Normal file
View file

@ -0,0 +1,29 @@
/*********************************************************************************/
/* Module Name: cs_CZ.c */
/* Project: AurixOS */
/* */
/* Copyright (c) 2024-2025 Jozef Nagy */
/* */
/* This source is subject to the MIT License. */
/* See License.txt in the root of this repository. */
/* All other rights reserved. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR */
/* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, */
/* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE */
/* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER */
/* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, */
/* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE */
/* SOFTWARE. */
/*********************************************************************************/
#include <i18n.h>
struct language i18n_csCZ = {
.shutdown = "Vypnout",
.reboot = "Restartovat",
.reboot_to_firmware = "Přejít do nastavení firmwaru",
.resolution = "Rozlišení",
.language = "Jazyk"
};

29
boot/common/i18n/en_US.c Normal file
View file

@ -0,0 +1,29 @@
/*********************************************************************************/
/* Module Name: en_US.c */
/* Project: AurixOS */
/* */
/* Copyright (c) 2024-2025 Jozef Nagy */
/* */
/* This source is subject to the MIT License. */
/* See License.txt in the root of this repository. */
/* All other rights reserved. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR */
/* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, */
/* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE */
/* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER */
/* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, */
/* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE */
/* SOFTWARE. */
/*********************************************************************************/
#include <i18n.h>
struct language i18n_enUS = {
.shutdown = "Shutdown",
.reboot = "Reboot",
.reboot_to_firmware = "Reboot to Firmware",
.resolution = "Resolution",
.language = "Language"
};

38
boot/common/i18n/i18n.c Normal file
View file

@ -0,0 +1,38 @@
/*********************************************************************************/
/* Module Name: i18n.c */
/* Project: AurixOS */
/* */
/* Copyright (c) 2024-2025 Jozef Nagy */
/* */
/* This source is subject to the MIT License. */
/* See License.txt in the root of this repository. */
/* All other rights reserved. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR */
/* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, */
/* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE */
/* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER */
/* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, */
/* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE */
/* SOFTWARE. */
/*********************************************************************************/
#include <i18n.h>
extern struct language i18n_csCZ;
extern struct language i18n_enUS;
struct language_selection i18n_languages[] = {
{
"Čeština",
"cs_CZ",
&i18n_csCZ
},
{
"English",
"en_US",
&i18n_enUS
}
};
struct language_selection *i18n_curlang = &i18n_languages[1]; // english

View file

@ -17,21 +17,50 @@
/* SOFTWARE. */
/*********************************************************************************/
#include <vfs/vfs.h>
#include <mm/mman.h>
#include <mm/vmm.h>
#include <config/config.h>
#include <loader/loader.h>
#include <proto/aurix.h>
#include <uart/uart.h>
#include <vfs/vfs.h>
#include <ui/ui.h>
#include <axboot.h>
#include <print.h>
void axboot_init()
{
uart_init(115200);
if (!vfs_init("\\")) {
debug("axboot_init(): Failed to mount boot drive! Halting...\n");
// TODO: Halt
while (1);
}
aurix_load("\\System\\axkrnl");
#ifdef AXBOOT_UEFI
#include <driver.h>
load_drivers();
#endif
while (1);
//config_init();
// boot straight away
if (config_get_timeout() < 1) {
struct axboot_entry *entries = config_get_entries();
loader_load(&(entries[config_get_default()]));
}
ui_init();
debug("axboot_init(): Returned from main menu, something went wrong. Halting!");
//UNREACHABLE();
// just boot aurixos for now
struct axboot_entry axos = {
.name = "AurixOS",
.description = "",
.image_path = "\\System\\axkrnl",
.protocol = PROTO_AURIX
};
loader_load(&axos);
UNREACHABLE();
}

View file

@ -143,6 +143,38 @@ char *strcpy(char *dest, const char *src)
return pdest;
}
char *strncpy(char *dest, const char *src, size_t n)
{
if (dest == NULL || src == NULL) {
return NULL;
}
char *pdest = dest;
while (*src != '\0' || n--) {
*pdest++ = *src++;
}
*pdest = '\0';
return dest;
}
char *strncat(char *dest, const char *src, size_t n)
{
if (dest == 0 || src == 0) {
return NULL;
}
size_t i = strlen(dest);
size_t a = 0;
while (a < n && src[a] != '\0') {
dest[i++] = src[a++];
}
dest[i] = '\0';
return dest;
}
// TODO: Get rid of this function
char *strdup(const char *s)
{

View file

@ -0,0 +1,58 @@
/*********************************************************************************/
/* Module Name: loader.c */
/* Project: AurixOS */
/* */
/* Copyright (c) 2024-2025 Jozef Nagy */
/* */
/* This source is subject to the MIT License. */
/* See License.txt in the root of this repository. */
/* All other rights reserved. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR */
/* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, */
/* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE */
/* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER */
/* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, */
/* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE */
/* SOFTWARE. */
/*********************************************************************************/
#include <config/config.h>
#include <loader/loader.h>
#include <lib/string.h>
#include <proto/aurix.h>
#include <print.h>
#include <axboot.h>
int proto_str_to_int(char *proto)
{
if (strcmp(proto, "aurix") == 0) {
return PROTO_AURIX;
}
#ifdef AXBOOT_UEFI
if (strcmp(proto, "efi-chainload") == 0) {
return PROTO_CHAINLOAD;
}
#endif
return PROTO_UNSUPPORTED;
}
void loader_load(struct axboot_entry *entry)
{
debug("loader_load(): Booting \"%s\"...\n", entry->name);
switch (entry->protocol) {
case PROTO_AURIX: {
aurix_load(entry->image_path);
break;
}
default: {
debug("Entry doesn't have a supported protocol!\n");
break;
}
}
UNREACHABLE();
}

View file

@ -113,6 +113,7 @@ void *mem_alloc(size_t n)
add_alloc(alloc, n);
debug("mem_alloc(): Allocated %u bytes\n", n);
return alloc;
}
@ -122,7 +123,7 @@ int mem_allocat(void *addr, size_t npages)
status = gBootServices->AllocatePages(AllocateAddress, EfiLoaderData, (EFI_UINTN)npages, addr);
if (EFI_ERROR(status)) {
debug("mem_allocat(): Couldn't allocate %u bytes at 0x%lx: %s (%lx)\n", npages, addr, efi_status_to_str(status), status);
debug("mem_allocat(): Couldn't allocate %u pages at 0x%lx: %s (%lx)\n", npages, addr, efi_status_to_str(status), status);
return 0;
}
@ -137,9 +138,9 @@ void *mem_realloc(void *addr, size_t n)
void *new = NULL;
int i = find_alloc(addr);
if (i == -1) {
debug("mem_realloc(): Couldn't find allocation for 0x%lx.\n");
return NULL;
if (i == -1 || addr == NULL) {
debug("mem_realloc(): Couldn't find allocation for 0x%lx, allocating new memory.\n");
return mem_alloc(n);
}
old_size = allocation_list[i].size;
@ -169,6 +170,8 @@ void mem_free(void *addr)
return;
}
debug("mem_free(): Freed 0x%llx\n", addr);
remove_alloc(addr);
}

View file

@ -19,13 +19,14 @@
#define NANOPRINTF_IMPLEMENTATION
#define NANOPRINTF_USE_FIELD_WIDTH_FORMAT_SPECIFIERS 1
#define NANOPRINTF_USE_PRECISION_FORMAT_SPECIFIERS 0
#define NANOPRINTF_USE_PRECISION_FORMAT_SPECIFIERS 1
#define NANOPRINTF_USE_FLOAT_FORMAT_SPECIFIERS 0
#define NANOPRINTF_USE_LARGE_FORMAT_SPECIFIERS 1
#define NANOPRINTF_USE_BINARY_FORMAT_SPECIFIERS 0
#define NANOPRINTF_USE_WRITEBACK_FORMAT_SPECIFIERS 0
#include <nanoprintf.h>
#include <uart/uart.h>
#include <print.h>
#include <stddef.h>
@ -46,10 +47,7 @@ void log(const char *fmt, ...)
npf_vsnprintf(buf, sizeof(buf), fmt, args);
va_end(args);
// TODO: Get rid of this
#ifdef AXBOOT_UEFI
printstr(buf);
#endif
uart_sendstr(buf);
}
void debug(const char *fmt, ...)
@ -61,15 +59,19 @@ void debug(const char *fmt, ...)
npf_vsnprintf(buf, sizeof(buf), fmt, args);
va_end(args);
#ifdef AXBOOT_UEFI
printstr(buf);
#endif
uart_sendstr(buf);
}
void snprintf(char *buf, size_t size, const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
npf_vsnprintf(buf, size, fmt, args);
va_end(args);
}
}
void vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
{
npf_vsnprintf(buf, size, fmt, args);
}

View file

@ -44,12 +44,13 @@ void aurix_load(char *kernel_path)
// TODO: Halt
while (1);
}
axboot_memmap *memmap = get_memmap(pm);
(void)memmap;
map_pages(pm, (uintptr_t)pm, (uintptr_t)pm, PAGE_SIZE, VMM_WRITABLE);
map_pages(pm, (uintptr_t)_aurix_handoff_start, (uintptr_t)_aurix_handoff_start, (uint64_t)_aurix_handoff_end - (uint64_t)_aurix_handoff_start, 0);
void *stack = mem_alloc(16*1024); // 16 KiB stack should be well more than enough
if (!stack) {
debug("aurix_load(): Failed to allocate stack! Halting...\n");
@ -61,11 +62,12 @@ void aurix_load(char *kernel_path)
void *kernel_entry = (void *)elf_load(kbuf, pm);
if (!kernel_entry) {
debug("aurix_load(): Failed to load '%s'! Halting...\n", kernel_path);
mem_free(kbuf);
while (1);
}
// mem_free(kbuf);
void *parameters = NULL;
(void)parameters;
debug("aurix_load(): Handoff state: pm=0x%llx, stack=0x%llx, kernel_entry=0x%llx\n", pm, stack, kernel_entry);
@ -76,6 +78,6 @@ void aurix_load(char *kernel_path)
__asm__ volatile("movq %[pml4], %%cr3\n"
"movq %[stack], %%rsp\n"
"callq *%[entry]\n"
:: [pml4]"r"(pm), [stack]"r"(stack), [entry]"r"(kernel_entry) : "memory");
:: [pml4]"r"(pm), [stack]"r"(stack + (16 * 1024)), [entry]"r"(kernel_entry) : "memory");
__builtin_unreachable();
}

91
boot/common/ui/font.c Normal file
View file

@ -0,0 +1,91 @@
/*********************************************************************************/
/* Module Name: font.c */
/* Project: AurixOS */
/* */
/* Copyright (c) 2024-2025 Jozef Nagy */
/* */
/* This source is subject to the MIT License. */
/* See License.txt in the root of this repository. */
/* All other rights reserved. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR */
/* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, */
/* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE */
/* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER */
/* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, */
/* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE */
/* SOFTWARE. */
/*********************************************************************************/
#include <mm/mman.h>
#include <lib/string.h>
#include <lib/assert.h>
#include <vfs/vfs.h>
#define FONT_IMPLEMENTATION
#include <ui/ui.h>
#include <ui/font.h>
#include <print.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdint.h>
bool font_init(struct ui_context *ctx, char *font_path, int size)
{
vfs_read(font_path, &(ctx->font_file));
if (!ctx->font_file) {
return false;
}
int ssfn_status;
ssfn_status = ssfn_load(&(ctx->font), (void *)(ctx->font_file));
if (ssfn_status != SSFN_OK) {
debug("font_init(): SSFN failed to load font: %s!\n", ssfn_error(ssfn_status));
goto error;
}
ssfn_status = ssfn_select(&(ctx->font), SSFN_FAMILY_ANY, NULL, SSFN_STYLE_REGULAR, size);
if (ssfn_status != SSFN_OK) {
debug("font_init(): SSFN failed to select font: %s!\n", ssfn_error(ssfn_status));
goto error;
}
// initialize terminal
ctx->terminal.font_size = size;
ctx->terminal.cx = 0;
ctx->terminal.cy = size;
return true;
error:
mem_free(ctx->font_file);
return false;
}
void font_write(struct ui_context *ctx, char *s, uint32_t cx, uint32_t cy)
{
ctx->font_buf.x = cx;
ctx->font_buf.y = cy;
ssfn_render(&(ctx->font), &(ctx->font_buf), s);
}
void font_free(struct ui_context *ctx)
{
ssfn_free(&(ctx->font));
mem_free(ctx->font_file);
}
/*
void font_ttf_init(char *font_path, int initial_size)
{
vfs_read(font_path, (char **)&font_buf);
if (!font_buf) {
debug("Font not loaded, returning...\n");
return;
}
}
void font_psf2_init()
{
}
*/

86
boot/common/ui/terminal.c Normal file
View file

@ -0,0 +1,86 @@
/*********************************************************************************/
/* Module Name: terminal.c */
/* Project: AurixOS */
/* */
/* Copyright (c) 2024-2025 Jozef Nagy */
/* */
/* This source is subject to the MIT License. */
/* See License.txt in the root of this repository. */
/* All other rights reserved. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR */
/* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, */
/* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE */
/* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER */
/* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, */
/* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE */
/* SOFTWARE. */
/*********************************************************************************/
#include <ui/ui.h>
#include <ui/terminal.h>
#include <axboot.h>
#include <print.h>
#include <stdarg.h>
#define HORIZONTAL_TAB_WIDTH 8
void terminal_print(struct ui_context *ctx, char *fmt, ...)
{
va_list args;
char buf[4096] = {0};
char *s = (char *)&buf;
va_start(args, fmt);
vsnprintf((char *)&buf, sizeof(buf), fmt, args);
va_end(args);
while (*s) {
switch (*s) {
case '\t': {
// horizontal tab - 4 spaces
int w, h;
ssfn_bbox(&(ctx->font), " ", &w, &h, NULL, NULL);
if (ctx->terminal.cx >= ctx->fb_modes[ctx->current_mode].width - (w * HORIZONTAL_TAB_WIDTH)) {
for (int i = 1; i <= HORIZONTAL_TAB_WIDTH; i++) {
if (ctx->terminal.cx >= ctx->fb_modes[ctx->current_mode].width - (w * i)) {
ctx->terminal.cx = ((HORIZONTAL_TAB_WIDTH * (w + 1)) - (w * i));
ctx->terminal.cy += h;
break;
}
}
} else {
ctx->terminal.cx += w * HORIZONTAL_TAB_WIDTH;
}
break;
}
case '\n': {
// newline
ctx->terminal.cx = 0;
ctx->terminal.cy += ctx->terminal.font_size;
break;
}
default: {
// printable character
const char str[2] = {*s, 0};
int w, h;
ssfn_bbox(&(ctx->font), (char *)&str, &w, &h, NULL, NULL);
if (ctx->terminal.cx + w >= ctx->fb_modes[ctx->current_mode].width) {
ctx->terminal.cx = 0;
ctx->terminal.cy += h;
}
font_write(ctx, (char *)&str, ctx->terminal.cx, ctx->terminal.cy);
ctx->terminal.cx += w;
break;
}
}
s++;
}
}
void terminal_setcur(struct ui_context *ui, uint32_t x, uint32_t y)
{
ui->terminal.cx = x;
ui->terminal.cy = y;
}

290
boot/common/ui/ui.c Normal file
View file

@ -0,0 +1,290 @@
/*********************************************************************************/
/* Module Name: ui.c */
/* Project: AurixOS */
/* */
/* Copyright (c) 2024-2025 Jozef Nagy */
/* */
/* This source is subject to the MIT License. */
/* See License.txt in the root of this repository. */
/* All other rights reserved. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR */
/* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, */
/* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE */
/* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER */
/* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, */
/* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE */
/* SOFTWARE. */
/*********************************************************************************/
#include <config/config.h>
#include <lib/string.h>
#include <ui/framebuffer.h>
#include <ui/keyboard.h>
#include <ui/mouse.h>
#include <ui/font.h>
#include <ui/ui.h>
#include <loader/loader.h>
#include <time/dt.h>
#include <i18n.h>
#include <axboot.h>
#include <print.h>
#include <stdint.h>
enum {
EVENT_TIME = (1 << 0),
EVENT_TIMEOUT = (1 << 1),
EVENT_MOUSE = (1 << 2),
EVENT_NEXT_ENTRY = (1 << 3),
EVENT_PREV_ENTRY = (1 << 4),
EVENT_NEXT_TAB = (1 << 5),
EVENT_LANG_CHANGE = (1 << 6),
};
struct datetime last_dt;
bool gui_init(struct ui_context *ctx)
{
if (!font_init(ctx, "\\AxBoot\\fonts\\vera\\Vera.sfn", 16)) {
return false;
}
return false;
}
bool tui_init(struct ui_context *ctx)
{
if (!font_init(ctx, "\\AxBoot\\fonts\\u_vga16\\u_vga16.sfn", 16)) {
return false;
}
int width = ctx->fb_modes[ctx->current_mode].width;
int height = ctx->fb_modes[ctx->current_mode].height;
int width_center = width / 2;
int height_center = height / 2;
int w, h;
// display string at the top center of the screen
char *top_string = BOOTLOADER_NAME_STR " v" BOOTLOADER_VERSION_STR;
ssfn_bbox(&(ctx->font), top_string, &w, NULL, NULL, NULL);
terminal_setcur(ctx, width_center - (w / 2), height / 32);
terminal_print(ctx, top_string);
// display current language
char lang_str[64];
snprintf((char *)&lang_str, 64, "%s: %s", i18n_curlang->lang->language, i18n_curlang->name);
ssfn_bbox(&(ctx->font), (char *)lang_str, &w, &h, NULL, NULL);
terminal_setcur(ctx, width - w - ctx->font.size, height - h);
terminal_print(ctx, (char *)lang_str);
// display all entries
int entry_count = config_get_entry_count();
int entry_height_offset = height_center - ((h * entry_count) / 2);
struct axboot_entry *entries = config_get_entries();
for (int i = 0; i < entry_count; i++) {
debug("tui_init(): Drawing entries... (%u/%u)\n", i, entry_count);
ssfn_bbox(&(ctx->font), entries[i].name, &w, &h, NULL, NULL);
terminal_setcur(ctx, width_center - (w/2), entry_height_offset + (i * h));
terminal_print(ctx, entries[i].name);
}
// highlight default entry
int default_entry = ctx->current_selection;
char final_selent[128];
int f_w, f_h;
ssfn_bbox(&(ctx->font), " ", &f_w, &f_h, NULL, NULL);
snprintf((char *)&final_selent, 128, "> %s <", entries[default_entry].name);
ssfn_bbox(&(ctx->font), entries[default_entry].name, &w, &h, NULL, NULL);
terminal_setcur(ctx, width_center - (w/2) - (f_w*2), entry_height_offset + (default_entry * h));
terminal_print(ctx, "> ");
terminal_setcur(ctx, width_center + (w/2), entry_height_offset + (default_entry * h));
terminal_print(ctx, " <");
return true;
}
void gui_draw(struct ui_context *ctx, struct datetime *dt, struct language_selection *newlang, struct mouse_event *mouse_status, uint8_t event)
{
(void)ctx;
(void)dt;
(void)newlang;
(void)mouse_status;
(void)event;
}
void tui_draw(struct ui_context *ctx, struct datetime *dt, struct language_selection *newlang, struct mouse_event *mouse_status, uint8_t event)
{
(void)mouse_status;
// display the current date and time at the bottom left corner of the screen
if (event & EVENT_TIME) {
int w, h;
char dt_str[20] = {0}; // YYYY/mm/dd HH:MM:SS
snprintf((char *)&dt_str, 20, "%.4u/%.2u/%.2u %.2u:%.2u:%.2u", dt->year, dt->month, dt->day, dt->h, dt->m, dt->s);
ssfn_bbox(&(ctx->font), (char *)dt_str, &w, &h, NULL, NULL);
for (uint32_t y = ctx->fb_modes[ctx->current_mode].height - (2*h); y < ctx->fb_modes[ctx->current_mode].height - h; y++) {
for (int x = 0; x < w; x++) {
*((uint32_t *)ctx->fb_addr + (ctx->fb_modes[ctx->current_mode].pitch / ctx->fb_modes[ctx->current_mode].bpp) * y + x) = 0xFF000000;
}
}
terminal_setcur(ctx, 0, ctx->fb_modes[ctx->current_mode].height - h);
terminal_print(ctx, (char *)dt_str);
// update last datetime
last_dt = *dt;
}
// timeout update
if (event & EVENT_TIMEOUT) {
// TODO
}
// select next entry
if (event & EVENT_NEXT_ENTRY) {
// TODO
}
// select previous entry
if (event & EVENT_PREV_ENTRY) {
// TODO
}
// update language
if (event & EVENT_LANG_CHANGE) {
int w, h;
char curlang_str[64];
snprintf((char *)&curlang_str, 64, "%s: %s", i18n_curlang->lang->language);
ssfn_bbox(&(ctx->font), (char *)curlang_str, &w, &h, NULL, NULL);
for (uint32_t y = ctx->fb_modes[ctx->current_mode].height - (2*h); y < ctx->fb_modes[ctx->current_mode].height - h; y++) {
for (int x = 0; x < w; x++) {
*((uint32_t *)ctx->fb_addr + (ctx->fb_modes[ctx->current_mode].pitch / ctx->fb_modes[ctx->current_mode].bpp) * y + x) = 0xFF000000;
}
}
i18n_curlang = newlang;
char newlang_str[64];
snprintf((char *)&curlang_str, 64, "%s: %s", i18n_curlang->lang->language);
ssfn_bbox(&(ctx->font), (char *)curlang_str, &w, &h, NULL, NULL);
terminal_setcur(ctx, ctx->fb_modes[ctx->current_mode].width - w, ctx->fb_modes[ctx->current_mode].height - h);
terminal_print(ctx, (char *)newlang_str);
}
}
void ui_init()
{
struct ui_context ctx = {0};
if (!get_framebuffer(&ctx.fb_addr, &ctx.fb_modes, &ctx.total_modes, &ctx.current_mode)) {
debug("ui_init(): Failed to acquire a framebuffer!\n");
while (1);
}
ctx.ui = config_get_ui_mode();
debug("Dumping framebuffer information\n");
debug("--------------------------------\n");
debug("Address: 0x%llx\n", ctx.fb_addr);
for (int i = 0; i < ctx.total_modes; i++) {
debug("Mode %u:%s | ", i, (i == ctx.current_mode) ? " (current)" : "");
debug("Resolution: %ux%u | ", ctx.fb_modes[i].width, ctx.fb_modes[i].height);
debug("Bytes Per Pixel: %u | ", ctx.fb_modes[i].bpp);
debug("Pitch: %u | ", ctx.fb_modes[i].pitch);
debug("Format: %s\n", ctx.fb_modes[i].format == FB_RGBA ? "RGBA" : "BGRA");
}
ctx.font_buf.ptr = (uint8_t *)ctx.fb_addr;
ctx.font_buf.w = ctx.fb_modes[ctx.current_mode].width;
ctx.font_buf.h = ctx.fb_modes[ctx.current_mode].height;
ctx.font_buf.p = ctx.fb_modes[ctx.current_mode].pitch;
ctx.font_buf.x = 0;
ctx.font_buf.y = 0;
ctx.font_buf.fg = 0xFFFFFFFF;
ctx.current_selection = config_get_default();
void (*ui_callback)(struct ui_context*,struct datetime*,struct language_selection*,struct mouse_event*,uint8_t) = NULL;
switch (ctx.ui) {
case UI_MODERN: {
if (!gui_init(&ctx)) {
debug("ui_init(): Failed to initialize modern UI, booting default selection...\n");
break;
}
ui_callback = gui_draw;
break;
}
default:
case UI_TEXT: {
if (!tui_init(&ctx)) {
debug("ui_init(): Failed to initialize text UI, booting default selection...\n");
break;
}
ui_callback = tui_draw;
break;
}
}
struct datetime dt;
struct mouse_event me;
while (1) {
uint8_t event = 0;
// datetime?
get_datetime(&dt);
if (memcmp(&dt, &last_dt, sizeof(struct datetime)) != 0) {
event |= EVENT_TIME;
}
// mouse movement?
get_mouse(&(me.x), &(me.y), &(me.but));
if (memcmp(&me, &(ctx.last_mouse), sizeof(struct mouse_event)) != 0) {
event |= EVENT_MOUSE;
}
// keypress?
uint16_t scancode;
get_keypress(&scancode);
if (scancode != 0) {
switch (scancode) {
case SCANCODE_ARROW_DOWN:
event |= EVENT_NEXT_ENTRY;
break;
case SCANCODE_ARROW_UP:
event |= EVENT_PREV_ENTRY;
break;
case SCANCODE_ENTER:
// clear the screen
for (uint32_t y = 0; y < ctx.fb_modes[ctx.current_mode].height; y++) {
for (uint32_t x = 0; x < ctx.fb_modes[ctx.current_mode].width; x++) {
*((uint32_t *)ctx.fb_addr + (ctx.fb_modes[ctx.current_mode].pitch / ctx.fb_modes[ctx.current_mode].bpp) * y + x) = 0xFF000000;
}
}
struct axboot_entry *entries = config_get_entries();
loader_load(&entries[ctx.current_selection]);
break;
default:
break;
}
}
if (event != 0) {
ui_callback(&ctx, &dt, NULL, &me, event);
} else {
#ifdef __x86_64
__asm__ volatile("hlt");
#endif
//arch_wait();
}
}
}