From d79556a58fb859c018674c93bcfca36d52182ebe Mon Sep 17 00:00:00 2001 From: kamkow1 Date: Fri, 3 Apr 2026 02:33:47 +0200 Subject: [PATCH] devices utility app --- devices/.gitignore | 6 +++++ devices/Makefile | 10 ++++++++ devices/app.mk | 1 + devices/devices.c | 50 +++++++++++++++++++++++++++++++++++++ devices/src.mk | 3 +++ include/device_info.h | 3 +++ include/devices.h | 6 +++++ kernel/device/device.c | 5 ++++ kernel/device/pci/pci_ide.c | 1 + make/apps.mk | 3 ++- 10 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 devices/.gitignore create mode 100644 devices/Makefile create mode 100644 devices/app.mk create mode 100644 devices/devices.c create mode 100644 devices/src.mk diff --git a/devices/.gitignore b/devices/.gitignore new file mode 100644 index 0000000..b44a3a4 --- /dev/null +++ b/devices/.gitignore @@ -0,0 +1,6 @@ +*.o +*.json +docs/ +.cache/ +*.map +devices diff --git a/devices/Makefile b/devices/Makefile new file mode 100644 index 0000000..117cc7d --- /dev/null +++ b/devices/Makefile @@ -0,0 +1,10 @@ +include ../make/ufuncs.mk + +$(eval $(call add_lib,libstring)) +$(eval $(call add_lib,libprocess)) +$(eval $(call add_lib,libaux)) +$(eval $(call add_lib,libmalloc)) + +cflags += -DPRINTF_INCLUDE_CONFIG_H=1 + +include ../make/user.mk diff --git a/devices/app.mk b/devices/app.mk new file mode 100644 index 0000000..9bf4d5f --- /dev/null +++ b/devices/app.mk @@ -0,0 +1 @@ +app := devices diff --git a/devices/devices.c b/devices/devices.c new file mode 100644 index 0000000..b0b0cb3 --- /dev/null +++ b/devices/devices.c @@ -0,0 +1,50 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +static const char* device_types_str[] = { + [DEVICE_TYPE_DEBUGCONSOLE] = "Debug console", [DEVICE_TYPE_TERMINAL] = "System terminal", + [DEVICE_TYPE_KEYBOARD] = "Keyboard", [DEVICE_TYPE_DRIVE] = "Drive", + [DEVICE_TYPE_USB_CTRL] = "USB controller", +}; + +static void list_all_devices (void) { + struct device_info* infos = malloc (sizeof (struct device_info) * 1024); + memset (infos, 0, sizeof (struct device_info) * 1024); + + int device_count = get_device_info (infos, 1024); + + mprintf ("%-30s %-20s\n", "NAME", "TYPE"); + for (int dev = 0; dev < device_count; dev++) { + struct device_info* info = &infos[dev]; + + mprintf ("%-30s %-20s\n", info->key, device_types_str[info->type]); + } +} + +void app_main (void) { + libprocess_self_init (); + + char commandbuf[32]; + memset (commandbuf, 0, sizeof (commandbuf)); + + if (env_get (process_get_pgid (), "help", (void*)commandbuf, sizeof (commandbuf)) == ST_OK) { + mprintf ("devices -C command\n"); + mprintf ("commands: list_all\n"); + return; + } + + if (env_get (process_get_pgid (), "C", (void*)commandbuf, sizeof (commandbuf)) != ST_OK) { + mprintf ("ERROR C=???. No command provided\n"); + return; + } + + if (strcmp (commandbuf, "list_all") == 0) { + list_all_devices (); + } +} diff --git a/devices/src.mk b/devices/src.mk new file mode 100644 index 0000000..7fc0478 --- /dev/null +++ b/devices/src.mk @@ -0,0 +1,3 @@ +c += devices.c + +o += devices.o diff --git a/include/device_info.h b/include/device_info.h index 72c12a3..01e9269 100644 --- a/include/device_info.h +++ b/include/device_info.h @@ -1,9 +1,12 @@ #ifndef _DEVICE_INFO_H #define _DEVICE_INFO_H +#include + struct device_info { int type; char key[0x100]; + uint64_t flags; }; #endif // _DEVICE_INFO_H diff --git a/include/devices.h b/include/devices.h index 8d31ea9..5de4ff2 100644 --- a/include/devices.h +++ b/include/devices.h @@ -7,6 +7,12 @@ #define DEVICE_TYPE_DRIVE 3 #define DEVICE_TYPE_USB_CTRL 4 +/* Device flags */ +#define DEVICE_FLAG_PCI (1 << 0) +#define DEVICE_FLAG_ISA (1 << 1) +#define DEVICE_FLAG_USB (1 << 2) +#define DEVICE_FLAG_VIRT (1 << 3) + /* debugconsole device */ #define DEBUGCONSOLE_PUTSTR 0 diff --git a/kernel/device/device.c b/kernel/device/device.c index a1800d1..3d79d2c 100644 --- a/kernel/device/device.c +++ b/kernel/device/device.c @@ -134,6 +134,7 @@ static void debugconsole_device_init (void) { static device_op_func_t ops[] = { [DEBUGCONSOLE_PUTSTR] = &debugconsole_putstr, }; + device_create (DEVICE_TYPE_DEBUGCONSOLE, "debugconsole", ops, lengthof (ops), &debugconsole_init, &debugconsole_fini, NULL, thiscpu->kproc, &rctx); } @@ -146,6 +147,7 @@ static void terminal_device_init (void) { [TERMINAL_PUTSTR] = &terminal_putstr, [TERMINAL_DIMENSIONS] = &terminal_dimensions, }; + device_create (DEVICE_TYPE_TERMINAL, "terminal", ops, lengthof (ops), &terminal_init, &terminal_fini, NULL, thiscpu->kproc, &rctx); } @@ -191,6 +193,7 @@ static void sys_device_init (void) { .sector_size = 512, .buffer = unpack_buffer, }; + device_create (DEVICE_TYPE_DRIVE, "sys0", ops, lengthof (ops), &ramdrv_init, &ramdrv_fini, &init, thiscpu->kproc, &rctx); @@ -213,6 +216,7 @@ static void temp_device_init (void) { .total_size = 1024 * 1024 * 20, .sector_size = 512, }; + device_create (DEVICE_TYPE_DRIVE, "temp0", ops, lengthof (ops), &ramdrv_init, &ramdrv_fini, &init, thiscpu->kproc, &rctx); } @@ -225,6 +229,7 @@ static void ps2kb_device_init (void) { device_op_func_t ops[] = { [KB_READ_KEY] = &ps2kb_read_key, }; + device_create (DEVICE_TYPE_KEYBOARD, "kb", ops, lengthof (ops), &ps2kb_init, &ps2kb_fini, NULL, thiscpu->kproc, &rctx); } diff --git a/kernel/device/pci/pci_ide.c b/kernel/device/pci/pci_ide.c index ad5d9e7..678d918 100644 --- a/kernel/device/pci/pci_ide.c +++ b/kernel/device/pci/pci_ide.c @@ -58,6 +58,7 @@ static void ide_make_device (struct proc* proc, struct reschedule_ctx* rctx, .irq = probe.irq, .irqs_support = probe.irqs_support, }; + struct device* ide = device_create (DEVICE_TYPE_DRIVE, device_key, ops, lengthof (ops), &idedrv_init, &idedrv_fini, &init, proc, rctx); device_probe_partitions (proc, rctx, ide); diff --git a/make/apps.mk b/make/apps.mk index 5f84dbc..2ff1aa4 100644 --- a/make/apps.mk +++ b/make/apps.mk @@ -4,7 +4,8 @@ apps := \ ce \ sdutil \ usb \ - mailtest + mailtest \ + devices all_apps: @for d in $(apps); do make -C $$d platform=$(platform) all; done