From 77796dd31776a597c9e3e37d0f49c57f42952cf3 Mon Sep 17 00:00:00 2001 From: DcraftBg Date: Wed, 20 May 2026 17:32:36 +0300 Subject: [PATCH] driver-install: implemented a simple installer --- userspace/programs/CMakeLists.txt | 1 + .../programs/driver-install/CMakeLists.txt | 9 ++ userspace/programs/driver-install/main.cpp | 93 +++++++++++++++++++ 3 files changed, 103 insertions(+) create mode 100644 userspace/programs/driver-install/CMakeLists.txt create mode 100644 userspace/programs/driver-install/main.cpp diff --git a/userspace/programs/CMakeLists.txt b/userspace/programs/CMakeLists.txt index cc67e545..f46562d7 100644 --- a/userspace/programs/CMakeLists.txt +++ b/userspace/programs/CMakeLists.txt @@ -57,6 +57,7 @@ set(USERSPACE_PROGRAMS whoami WindowServer yes + driver-install ) foreach(project ${USERSPACE_PROGRAMS}) diff --git a/userspace/programs/driver-install/CMakeLists.txt b/userspace/programs/driver-install/CMakeLists.txt new file mode 100644 index 00000000..68e69fa1 --- /dev/null +++ b/userspace/programs/driver-install/CMakeLists.txt @@ -0,0 +1,9 @@ +set(SOURCES + main.cpp +) + +add_executable(driver-install ${SOURCES}) +banan_link_library(driver-install ban) +banan_link_library(driver-install libc) + +install(TARGETS driver-install OPTIONAL) diff --git a/userspace/programs/driver-install/main.cpp b/userspace/programs/driver-install/main.cpp new file mode 100644 index 00000000..5c6de152 --- /dev/null +++ b/userspace/programs/driver-install/main.cpp @@ -0,0 +1,93 @@ +#include +#include +#include +#include +#include +#include +#include +#include + + +char* g_exe; +char* shift_args(int *argc, char ***argv) { + return (assert((*argc) > 0), ((*argc)--, *((*argv)++))); +} +void help(FILE* sink) { + fprintf(sink, "%s \n", g_exe); +} +#define eprintf(...) fprintf(stderr, __VA_ARGS__) +#define eprintfln(fmt, ...) eprintf(fmt "\n", ## __VA_ARGS__) +char* read_entire_file(const char* path) { + char* result = NULL; + char* head = NULL; + char* end = NULL; + size_t buf_size = 0; + long at = 0; + FILE *f = fopen(path, "rb"); + + if(!f) { + fprintf(stderr, "ERROR Could not open file %s: %s\n",path,strerror(errno)); + return NULL; + } + if(fseek(f, 0, SEEK_END) != 0) { + fprintf(stderr, "ERROR Could not fseek on file %s: %s\n",path,strerror(errno)); + result = NULL; goto DEFER; + } + at = ftell(f); + if(at == -1L) { + fprintf(stderr, "ERROR Could not ftell on file %s: %s\n",path,strerror(errno)); + result = NULL; goto DEFER; + } + buf_size = at+1; + rewind(f); + result = (char*)malloc(buf_size); + assert(result && "Ran out of memory"); + head = result; + end = result+buf_size-1; + while(head != end) { + head += fread(head, 1, end-head, f); + if(ferror(f)) { + fprintf(stderr, "ERROR Could not fread on file %s: %s\n",path,strerror(errno)); + free(result); + result = NULL; goto DEFER; + } + } + result[buf_size-1] = '\0'; +DEFER: + fclose(f); + return result; +} + +static int banos_install(const void* driver_image) { + return syscall(SYS_BANOS_INSTALL, driver_image); +} + + +int main(int argc, char** argv) +{ + g_exe = shift_args(&argc, &argv); + char* input_filename = NULL; + while(argc > 0) { + char* arg = shift_args(&argc, &argv); + if(!input_filename) input_filename = arg; + else { + eprintfln("ERROR: Unexpected argument `%s'", arg); + help(stderr); + return 1; + } + } + if(!input_filename) { + eprintfln("ERROR: Missing input filename!"); + help(stderr); + return 1; + } + char* data = read_entire_file(input_filename); + if(!data) return 1; + int id = banos_install(data); + if(id == -1) { + eprintfln("ERROR: Failed to install driver `%s': %s", input_filename, strerror(errno)); + return 1; + } + printf("%d\n", id); + return 0; +}