Compare commits

...

5 Commits

Author SHA1 Message Date
KoDer 676634a3c7 0.1 2026-05-08 15:21:18 +07:00
KoDer d6a8619e70 hz 2026-05-06 20:17:26 +07:00
KoDer 801dac9328 hz 2026-05-06 20:15:17 +07:00
KoDer d429ce6d2c fix: cleanup submodule and sync 2026-05-06 20:02:56 +07:00
KoDer a27a1d0c33 sync submodule 2026-05-06 20:01:17 +07:00
26 changed files with 82 additions and 798 deletions
+3
View File
@@ -3,6 +3,8 @@ platform = espressif32
board = esp32-s3-devkitc-1
framework = espidf
board_build.partitions = partitions.csv
board_build.arduino.memory_type = qio_opi
build_flags =
-D BOARD_HAS_PSRAM
@@ -10,3 +12,4 @@ build_flags =
-D CONFIG_SPIRAM_USE_MALLOC=1
-D CONFIG_SPIRAM_TYPE_AUTO=1
-D CONFIG_SPIRAM_SPEED_80M=1
monitor_speed = 115200
-111
View File
@@ -1,111 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include "boot/apios.h"
#include "boot/typedef.h"
#include "boot/answer_code.h"
#include "boot/entry.h"
apios_main_table_t *apios_table = NULL;
int apios_dump() {
if (apios_table == NULL) {
printf("\033[31mError: APIOS table is NULL (not initialized)\033[0m\n");
return APIOS_ERR_NOT_INITIALIZED;
}
printf("\n--- \033[34mAPIOS System Dump\033[0m ---\n");
printf("Main Table Address: %p\n", (void*)apios_table);
printf("Magic: 0x%04X\n", apios_table->magic);
printf("Total Drivers: %d\n", apios_table->drivers_count);
printf("---------------------------\n");
for (int i = 0; i < apios_table->drivers_count; i++) {
driver_header_t *header = apios_table->drivers[i];
if (header == NULL) {
printf(" [%d] Header is NULL!\n", i);
continue;
}
printf(" [%d] Driver Name: \033[32m%s\033[0m\n", i, header->driver_name);
printf(" Header Addr: %p\n", (void*)header);
driver_api_t *api = header->api;
if (api == NULL) {
printf(" \033[31m! API Table is NULL\033[0m\n");
continue;
}
printf(" API Addr: %p (v%d)\n", (void*)api, api->driver_version);
printf(" Functions Count: %d\n", api->fn_count);
if (api->functions != NULL) {
printf(" Functions List:\n");
for (int f = 0; f < api->fn_count; f++) {
printf(" - [%d] %-15s -> Addr: %p\n",
f,
api->functions[f].name,
(void*)api->functions[f].driver_fn);
}
} else {
printf(" \033[31m! No functions array found\033[0m\n");
}
printf("---------------------------\n");
}
return OS_OK;
}
int hendle_func(int_fn_t *func, char driver_name[16], char func_name[16]) {
if (apios_table == NULL) return APIOS_ERR_NOT_INITIALIZED;
bool drv_found = false;
for (int i = 0; i < apios_table->drivers_count; i++) {
if (strcmp(apios_table->drivers[i]->driver_name, driver_name) == 0) {
drv_found = true;
driver_api_t *api = apios_table->drivers[i]->api;
bool fn_found = false;
for (int f = 0; f < api->fn_count; f++) {
if (strcmp(api->functions[f].name, func_name) == 0) {
*func = api->functions[f].driver_fn;
fn_found = true;
return OS_OK;
}
}
if (!fn_found) return APIOS_ERR_DRIVER_CRASH;
}
}
if (!drv_found) return APIOS_ERR_DRIVER_NOT_FOUND;
return OS_ERR_NOT_FOUND;
}
int init_apios(apios_main_table_t *apios_table_) {
if (apios_table == NULL) {
if (load_log) printf("APIOS initings...\n");
apios_table = (apios_main_table_t *)malloc(sizeof(apios_main_table_t));
if (apios_table == NULL){
if (load_log) printf("Failed to initialize table\n");
return OS_ERR_NO_MEMORY;
}
static driver_fn_t fs_function[] = {
{
.name = "dump",
.driver_fn = apios_dump,
}
};
driver_api_t *fs_api = (driver_api_t *)malloc(sizeof(driver_api_t));
fs_api->driver_version = 1;
fs_api->fn_count = (int)(sizeof(fs_function)/sizeof(fs_function[0]));
fs_api->functions = fs_function;
driver_header_t *driver_header = (driver_header_t *)malloc(sizeof(driver_header_t));
strcpy(driver_header->driver_name, "apios");
driver_header->api = fs_api;
apios_table->magic = 0xfa0c;
apios_table->drivers = (driver_header_t **)malloc(sizeof(driver_header_t *) * 100);
apios_table->drivers_count = 0;
apios_table->drivers[apios_table->drivers_count] = driver_header;
apios_table->drivers_count++;
}
else {
if (load_log) printf("The table does not require initialization\n");
return OS_WRN_NOT_NEED_INIT;
}
return OS_OK;
}
-10
View File
@@ -1,10 +0,0 @@
#ifndef APIOS_H
#define APIOS_H
#include "boot/typedef.h"
int init_apios(apios_main_table_t *apios_table_);
extern apios_main_table_t *apios_table;
#endif
-36
View File
@@ -1,36 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include "FreeRTOS.h"
#include "boot/apios.h"
#include "drivers/fs/fs.h"
#include "drivers/monitor/driver.h"
#include "lib/video/vram.h"
#include "boot/answer_code.h"
#include "nvs_flash.h"
#include "nvs.h"
void log_result(int res, char *service_name){
if (res == OS_OK) {
printf("[ \033[0;32mOK\033[0m ] APIOS initialization\n");
} else {
char *err_text = "Error";
const char *msg = get_error(res);
if (strncmp(msg, err_text, 5) == 0) {
printf("[ \033[0;31mEROR\033[0m ] %s initialization: %s (%d)\n", service_name, msg, res);
} else {
printf("[ \033[0;33mWARN\033[0m ] %s initialization: %s (%d)\n", service_name, msg, res);
}
}
}
void OS_entry(void *pvParameters)
{
printf("Initializing subsystems\n");
log_result(init_apios(apios_table), "APIOS");
}
-1
View File
@@ -1 +0,0 @@
void OS_entry(void *pvParameters);
+20 -32
View File
@@ -1,50 +1,38 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include "FreeRTOS.h"
#include "boot/recovery/boot.h"
#include "boot/boot.h"
#include "drivers/fs/fs.h"
#include "boot/entry.h"
#include "boot/answer_code.h"
bool load_log = true;
void log_result(int res, char *service_name){
if (res == OS_OK) {
printf("[ \033[0;32mOK\033[0m ] APIOS initialization\n");
} else {
char *err_text = "Error";
const char *msg = get_error(res);
if (strncmp(msg, err_text, 5) == 0) {
printf("[ \033[0;31mEROR\033[0m ] %s initialization: %s (%d)\n", service_name, msg, res);
} else {
printf("[ \033[0;33mWARN\033[0m ] %s initialization: %s (%d)\n", service_name, msg, res);
}
}
}
int main_entry()
{
printf("\033[2J\033[H");
printf("OS started\n");
load_log = false;
init_fs();
xTaskCreatePinnedToCore(
OS_entry,
"Os main task",
4096,
NULL,
10,
NULL,
0
);
return 0;
}
int recovery_entry()
while (true)
{
printf("\033[2J\033[H");
printf("Recovery started\n");
load_log = true;
xTaskCreatePinnedToCore(
recovery_shell,
"Recovery Shell",
4096,
NULL,
10,
NULL,
0
);
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
return 0;
}
-2
View File
@@ -6,6 +6,4 @@
extern bool load_log;
int main_entry();
int recovery_entry();
#endif
-175
View File
@@ -1,175 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include "FreeRTOS.h"
#include "boot/recovery/boot.h"
#include "boot/recovery/typedef.h"
#include "boot/recovery/table.h"
#include "boot/apios.h"
void recovery_shell(void *pvParameters) {
vTaskDelay(pdMS_TO_TICKS(10));
char buffer[64];
char prm[8][16];
int current_pos = 0;
printf("\033[2J\033[H");
printf("\033[32mrecovery:\033[33m> \033[0m");
fflush(stdout);
memset(buffer, 0, 64);
while (1) {
int in = getchar();
if (in == EOF) {
vTaskDelay(pdMS_TO_TICKS(10));
continue;
}
if (in == 0x08 || in == 0x7F) {
if (current_pos > 0) {
current_pos--;
buffer[current_pos] = '\0';
printf("\b \b");
fflush(stdout);
}
continue;
}
if (in == '\r' || in == '\n') {
printf("\n");
if (current_pos > 0) {
memset(prm, 0, sizeof(prm));
int count = 0;
char *token = strtok(buffer, " ");
while (token != NULL && count < 8) {
strncpy(prm[count], token, 15);
prm[count][15] = '\0';
count++;
token = strtok(NULL, " ");
}
if (count > 0) {
if (strcmp(prm[0], "load") == 0) {
char source[16] = "none";
char name_load[16] = "none";
for (int q = 1; q < count; q++) {
char *key = strtok(prm[q], "=");
char *val = strtok(NULL, "=");
if (key && val) {
if (strcmp(key, "source") == 0) strncpy(source, val, 15);
else if (strcmp(key, "name") == 0) strncpy(name_load, val, 15);
}
}
printf("Action: Loading module [%s] from [%s]\n", name_load, source);
bool found = false;
int table_size = sizeof(moduls_table) / sizeof(moduls_table[0]);
for (int i = 0; i < table_size; i++) {
if (strcmp(moduls_table[i].module.name, name_load) == 0) {
printf("\033[32mFound!\033[0m Starting driver...\n");
int res = moduls_table[i].module.init_fn(apios_table);
printf("Driver exited with code: %d\n", res);
found = true;
break;
}
}
if (!found && strcmp(name_load, "none") != 0) {
printf("\033[31mError:\033[0m Module '%s' not found\n", name_load);
}
}
else if (strcmp(prm[0], "init") == 0) {
char name_load[16] = "none";
for (int q = 1; q < count; q++) {
char *key = strtok(prm[q], "=");
char *val = strtok(NULL, "=");
if (key && val) {
if (strcmp(key, "name") == 0) strncpy(name_load, val, 15);
}
}
int table_size = sizeof(service_init) / sizeof(service_init[0]);
bool found = false;
for (int i = 0; i < table_size; i++) {
if (strcmp(service_init[i].name, name_load) == 0) {
printf("\033[32mFound!\033[0m Starting service ...\n");
int res = service_init[i].init_fn(apios_table);
printf("Service exited with code: %d\n", res);
found = true;
break;
}
}
if (!found) {
printf("\033[31mError:\033[0m Service '%s' not found \n", name_load);
}
}
else if (strcmp(prm[0], "apios") == 0) {
char drv_name[16] = "none";
char fn_name[16] = "none";
for (int q = 1; q < count; q++) {
char *key = strtok(prm[q], "=");
char *val = strtok(NULL, "=");
if (key && val) {
if (strcmp(key, "driver") == 0) strncpy(drv_name, val, 15);
else if (strcmp(key, "func") == 0) strncpy(fn_name, val, 15);
}
}
if (apios_table == NULL) {
printf("\033[31mError:\033[0m APIOS table not initialized\n");
} else {
bool drv_found = false;
for (int i = 0; i < apios_table->drivers_count; i++) {
if (strcmp(apios_table->drivers[i]->driver_name, drv_name) == 0) {
drv_found = true;
driver_api_t *api = apios_table->drivers[i]->api;
bool fn_found = false;
for (int f = 0; f < api->fn_count; f++) {
if (strcmp(api->functions[f].name, fn_name) == 0) {
printf("\033[32mExecuting:\033[0m %s->%s()...\n", drv_name, fn_name);
int res = api->functions[f].driver_fn(NULL);
printf("\n\033[32mDone.\033[0m Return code: %d\n", res);
fn_found = true;
break;
}
}
if (!fn_found) printf("\033[31mError:\033[0m Function '%s' not found in driver '%s'\n", fn_name, drv_name);
break;
}
}
if (!drv_found) printf("\033[31mError:\033[0m Driver '%s' not found\n", drv_name);
}
}
else if (strcmp(prm[0], "help") == 0) {
printf("Commands: load source=X name=Y\n");
printf("Commands: init name=Y, help\n");
printf("Commands: apios driver=Y, func\n");
}
else {
printf("Unknown command: %s\n", prm[0]);
}
}
}
current_pos = 0;
memset(buffer, 0, 64);
printf("\033[32mrecovery:\033[33m> \033[0m");
fflush(stdout);
}
else {
if (current_pos < 63) {
buffer[current_pos] = (char)in;
current_pos++;
buffer[current_pos] = '\0';
putchar(in);
fflush(stdout);
}
}
}
}
-3
View File
@@ -1,3 +0,0 @@
void recovery_shell(void *pvParameters);
-27
View File
@@ -1,27 +0,0 @@
#ifndef TABLE_RECOVERY
#define TABLE_RECOVERY
#include "boot/recovery/typedef.h"
#include "drivers/fs/fs.h"
#include "boot/apios.h"
load_source_t moduls_table[] = {
{
.name = "drivers",
.module = {
.name = "fs",
.init_fn = init_fs,
}
}
};
module_t service_init[] = {
{
.name = "apios",
.init_fn = init_apios
}
};
#endif
-21
View File
@@ -1,21 +0,0 @@
#ifndef TYPEDEF_RECOVERY_H
#define TYPEDEF_RECOVERY_H
struct apios_main_table_t;
typedef struct apios_main_table_t apios_main_table_t;
typedef int (*init_fn_t)(apios_main_table_t *apios_table);
typedef struct
{
char name[16];
init_fn_t init_fn;
} module_t;
typedef struct
{
char name[16];
module_t module;
} load_source_t;
#endif
Executable → Regular
-27
View File
@@ -3,13 +3,8 @@
#ifndef TYPEDEF
#define TYPEDEF
struct apios_main_table_t;
typedef struct apios_main_table_t apios_main_table_t;
typedef int (*entry_fn_t)(void *args);
typedef int (*int_fn_t)();
typedef struct {
char name[128];
char type[128];
@@ -17,26 +12,4 @@ typedef struct {
entry_fn_t entry_function;
} boot_entry_t;
typedef struct {
char name[16];
entry_fn_t driver_fn;
} driver_fn_t;
typedef struct {
int driver_version;
int fn_count;
driver_fn_t *functions;
} driver_api_t;
typedef struct {
char driver_name[16];
driver_api_t *api;
} driver_header_t;
struct apios_main_table_t {
int magic;
int drivers_count;
driver_header_t **drivers;
};
#endif
+1 -15
View File
@@ -1,6 +1,5 @@
#include "boot/entry.h"
#include "boot/typedef.h"
#include "drivers/eft/eft.h"
boot_entry_t entry_data[] = {
{
@@ -8,21 +7,8 @@ boot_entry_t entry_data[] = {
.type = "operating system",
.path = "/boot/boot.bin",
.entry_function = main_entry
},
{
.name = "ESP32S3-Recovery",
.type = "recovery OS",
.path = "/boot/recovery.bin",
.entry_function = recovery_entry
},
{
.name = "EFT Server",
.type = "server shell",
.path = "/drivers/eft/eft_server.bin",
.entry_function = eft_server_shell
}
};
const int autostart_index = 0;
const int entry_table_count = 3;
const int entry_table_count = 1;
-23
View File
@@ -1,23 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include "esp_system.h"
#include "esp_heap_caps.h"
int eft_server_shell() {
printf("\033[2J\033[H");
printf("EFT server shell running\n");
size_t internal = heap_caps_get_free_size(MALLOC_CAP_INTERNAL);
size_t total = esp_get_free_heap_size();
size_t max_block = heap_caps_get_largest_free_block(MALLOC_CAP_8BIT);
printf("\n--- Memory Check ---\n");
printf(" Internal RAM free: %u KB\n", internal / 1024);
printf(" Total Heap free: %u KB\n", total / 1024);
printf(" Max Alloc Block: %u KB\n", max_block / 1024);
printf("---------------------------\n");
return 0;
}
-3
View File
@@ -1,3 +0,0 @@
#pragma once
int eft_server_shell();
+43 -53
View File
@@ -2,15 +2,47 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "esp_vfs_fat.h"
#include "esp_spiffs.h"
#include "esp_littlefs.h"
#include "drivers/fs/fs.h"
#include "boot/typedef.h"
#include "boot/answer_code.h"
#include "boot/entry.h"
int mount() {
printf("fs: mount");
return 0;
esp_err_t mount(fs_type_t type, const char *path, const char *label) {
switch (type) {
case MOUNT_LITTLEFS: {
printf("fs: mount littlefs\n");
esp_vfs_littlefs_conf_t conf = {
.base_path = path,
.partition_label = label,
.format_if_mount_failed = true
};
return esp_vfs_littlefs_register(&conf);
}
case MOUNT_SD_CARD: {
printf("fs: sd card mount is not configured yet\n");
// Для SD нужны sdmmc_host_t и sdmmc_slot_config_t
// Пока возвращаем ошибку, чтобы скомпилировалось
return ESP_ERR_NOT_SUPPORTED;
}
case MOUNT_SPIFFS: {
printf("fs: mount spiffs\n");
esp_vfs_spiffs_conf_t conf = {
.base_path = path,
.partition_label = label,
.max_files = 5,
.format_if_mount_failed = true
};
return esp_vfs_spiffs_register(&conf);
}
default:
return ESP_ERR_NOT_SUPPORTED;
}
}
int unmount() {
printf("fs: unmount");
@@ -33,54 +65,12 @@ int get_vfs_config() {
return 0;
}
int init_fs(apios_main_table_t *apios_table) {
(void)apios_table;
if (apios_table == NULL) {
if (load_log) printf("The driver table is not initialized\n");
return APIOS_ERR_NOT_INITIALIZED;
int init_fs() {
// Проверьте, как называется макрос ошибки в boot/answer_code.h
// Если там ANS_ERR или что-то другое, замените здесь
if (mount(MOUNT_SPIFFS, "/spiffs", "storage") != ESP_OK) {
printf("fs: init failed\n");
return 1; // Используйте число или верный макрос из answer_code.h
}
static driver_fn_t fs_function[] = {
{
.name = "mount",
.driver_fn = mount,
},
{
.name = "unmount",
.driver_fn = unmount,
},
{
.name = "format",
.driver_fn = format,
},
{
.name = "get_info",
.driver_fn = get_info,
},
{
.name = "is_present",
.driver_fn = is_present,
},
{
.name = "get_vfs_config",
.driver_fn = get_vfs_config,
}
};
driver_api_t *fs_api = (driver_api_t *)malloc(sizeof(driver_api_t));
fs_api->driver_version = 1;
fs_api->fn_count = (int)(sizeof(fs_function)/sizeof(fs_function[0]));
fs_api->functions = fs_function;
driver_header_t *driver_header = (driver_header_t *)malloc(sizeof(driver_header_t));
strcpy(driver_header->driver_name, "FileSystem");
driver_header->api = fs_api;
apios_table->drivers[apios_table->drivers_count] = driver_header;
apios_table->drivers_count++;
return OS_OK;
return 0; // Аналогично для OK
}
+13 -2
View File
@@ -1,10 +1,21 @@
#ifndef _FS_H_
#define _FS_H_
#include "boot/typedef.h"
int init_fs(apios_main_table_t *apios_table);
typedef enum {
MOUNT_LITTLEFS,
MOUNT_SD_CARD,
MOUNT_SPIFFS
} fs_type_t;
int mount();
int init_fs();
esp_err_t mount(fs_type_t type, const char *path, const char *label);
int unmount();
int format();
int get_info();
int is_present();
int get_vfs_config();
#endif
View File
View File
View File
View File
View File
View File
View File
-214
View File
@@ -1,214 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "nvs_flash.h"
#include "nvs.h"
#include "lib/video/vram.h"
#include "boot/answer_code.h"
#include "boot/entry.h"
vram_manager_t *vram_sys = NULL;
int init_vram(apios_main_table_t *apios_table)
{
(void)apios_table;
if (vram_sys != NULL) return OS_WRN_NOT_NEED_INIT;
int vram_kb[3] = {128, 512, 1024};
int8_t val = 1;
nvs_handle_t h;
if (nvs_open("config", NVS_READONLY, &h) == ESP_OK) {
if (nvs_get_i8(h, "vram", &val) != ESP_OK || val < 0 || val > 2) {
val = 1;
}
nvs_close(h);
} else {
if (load_log) printf("VRAM: No saved config, using default 512kb\n");
val = 1;
}
vram_sys = (vram_manager_t*)malloc(sizeof(vram_manager_t));
if (!vram_sys) return OS_ERR_NO_MEMORY;
memset(vram_sys, 0, sizeof(vram_manager_t));
vram_sys->capacity = (uint32_t)(vram_kb[val] * 1024);
vram_sys->next_id = 1;
vram_sys->max_blocks = 256;
vram_sys->blocks_count = 1;
vram_sys->base_ptr = (uint8_t*)malloc(vram_sys->capacity);
vram_sys->blocks = (vram_block_t*)malloc(sizeof(vram_block_t) * vram_sys->max_blocks);
if (!vram_sys->base_ptr || !vram_sys->blocks) {
if (vram_sys->base_ptr) free(vram_sys->base_ptr);
if (vram_sys->blocks) free(vram_sys->blocks);
free(vram_sys);
vram_sys = NULL;
return OS_ERR_NO_MEMORY;
}
vram_sys->blocks[0].offset = 0;
vram_sys->blocks[0].size = vram_sys->capacity;
vram_sys->blocks[0].is_free = true;
vram_sys->blocks[0].id = -1;
if (load_log) {
printf("VRAM: Initialized %d kb at %p\n",
vram_kb[val],
(void*)vram_sys->base_ptr);
}
return OS_OK;
}
vram_handle_t vram_alloc(uint32_t size)
{
if (!vram_sys) return -1;
size = (size + 3) & ~3;
for (int i = 0; i < vram_sys->blocks_count; i++) {
if (vram_sys->blocks[i].is_free && vram_sys->blocks[i].size >= size) {
uint32_t remaining = vram_sys->blocks[i].size - size;
vram_sys->blocks[i].size = size;
vram_sys->blocks[i].is_free = false;
vram_sys->blocks[i].id = vram_sys->next_id++;
if (remaining > 0 && vram_sys->blocks_count < vram_sys->max_blocks) {
for (int j = vram_sys->blocks_count; j > i + 1; j--) {
vram_sys->blocks[j] = vram_sys->blocks[j - 1];
}
vram_sys->blocks[i + 1].offset = vram_sys->blocks[i].offset + size;
vram_sys->blocks[i + 1].size = remaining;
vram_sys->blocks[i + 1].is_free = true;
vram_sys->blocks[i + 1].id = -1;
vram_sys->blocks_count++;
}
return vram_sys->blocks[i].id;
}
}
if (load_log) printf("VRAM: No space for %lu bytes! Need defrag.\n", (unsigned long)size);
return VRAM_WRN_DEFRAG_REQ;
}
void* vram_get_ptr(vram_handle_t handle)
{
if (!vram_sys || handle <= 0) return NULL;
for (int i = 0; i < vram_sys->blocks_count; i++) {
if (vram_sys->blocks[i].id == handle) {
return (void*)(vram_sys->base_ptr + vram_sys->blocks[i].offset);
}
}
return NULL;
}
void vram_free(vram_handle_t handle)
{
if (!vram_sys) return;
for (int i = 0; i < vram_sys->blocks_count; i++) {
if (vram_sys->blocks[i].id == handle) {
vram_sys->blocks[i].is_free = true;
vram_sys->blocks[i].id = -1;
if (i + 1 < vram_sys->blocks_count && vram_sys->blocks[i + 1].is_free) {
vram_sys->blocks[i].size += vram_sys->blocks[i + 1].size;
for (int j = i + 1; j < vram_sys->blocks_count - 1; j++) {
vram_sys->blocks[j] = vram_sys->blocks[j + 1];
}
vram_sys->blocks_count--;
}
if (i > 0 && vram_sys->blocks[i - 1].is_free) {
vram_sys->blocks[i - 1].size += vram_sys->blocks[i].size;
for (int j = i; j < vram_sys->blocks_count - 1; j++) {
vram_sys->blocks[j] = vram_sys->blocks[j + 1];
}
vram_sys->blocks_count--;
}
return;
}
}
}
void vram_defrag(void)
{
if (!vram_sys) return;
uint32_t current_offset = 0;
int write_idx = 0;
for (int i = 0; i < vram_sys->blocks_count; i++) {
if (!vram_sys->blocks[i].is_free) {
if (vram_sys->blocks[i].offset != current_offset) {
memmove(vram_sys->base_ptr + current_offset,
vram_sys->base_ptr + vram_sys->blocks[i].offset,
vram_sys->blocks[i].size);
vram_sys->blocks[i].offset = current_offset;
}
vram_sys->blocks[write_idx] = vram_sys->blocks[i];
current_offset += vram_sys->blocks[write_idx].size;
write_idx++;
}
}
vram_sys->blocks_count = write_idx;
if (current_offset < vram_sys->capacity) {
vram_sys->blocks[write_idx].offset = current_offset;
vram_sys->blocks[write_idx].size = vram_sys->capacity - current_offset;
vram_sys->blocks[write_idx].is_free = true;
vram_sys->blocks[write_idx].id = -1;
vram_sys->blocks_count++;
}
if (load_log) {
printf("VRAM: Defrag complete. Free: %lu bytes\n",
(unsigned long)(vram_sys->capacity - current_offset));
}
}
int vram_write(vram_handle_t handle, const void *src, uint32_t size)
{
if (!vram_sys || handle <= 0 || !src) return -1;
for (int i = 0; i < vram_sys->blocks_count; i++) {
if (vram_sys->blocks[i].id == handle) {
uint32_t write_size = (size < vram_sys->blocks[i].size)
? size
: vram_sys->blocks[i].size;
memcpy(vram_sys->base_ptr + vram_sys->blocks[i].offset, src, write_size);
return OS_OK;
}
}
return VRAM_ERR_WR_or_RD;
}
int vram_read(vram_handle_t handle, void *dst, uint32_t size)
{
if (!vram_sys || handle <= 0 || !dst) return -1;
for (int i = 0; i < vram_sys->blocks_count; i++) {
if (vram_sys->blocks[i].id == handle) {
uint32_t read_size = (size < vram_sys->blocks[i].size)
? size
: vram_sys->blocks[i].size;
memcpy(dst, vram_sys->base_ptr + vram_sys->blocks[i].offset, read_size);
return OS_OK;
}
}
return VRAM_ERR_WR_or_RD;
}
-41
View File
@@ -1,41 +0,0 @@
#ifndef VRAM_H
#define VRAM_H
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#include "boot/typedef.h"
typedef int32_t vram_handle_t;
typedef struct {
uint32_t offset;
uint32_t size;
bool is_free;
vram_handle_t id;
} vram_block_t;
typedef struct {
uint8_t* base_ptr;
uint32_t capacity;
vram_block_t* blocks;
int blocks_count;
int max_blocks;
vram_handle_t next_id;
} vram_manager_t;
extern vram_manager_t *vram_sys;
int init_vram(apios_main_table_t *apios_table_);
vram_handle_t vram_alloc(uint32_t size);
void vram_free(vram_handle_t handle);
void* vram_get_ptr(vram_handle_t handle);
int vram_write(vram_handle_t handle, const void* src, uint32_t size);
int vram_read(vram_handle_t handle, void* dst, uint32_t size);
void vram_defrag();
#endif