Update kernel

This commit is contained in:
EnderIce2
2024-01-19 06:47:42 +02:00
parent fd15592608
commit 96daa43d38
282 changed files with 25486 additions and 15700 deletions

View File

@ -21,8 +21,10 @@
#include <types.h>
void cmd_lsof(const char *args);
void cmd_clear(const char *args);
void cmd_echo(const char *args);
void cmd_ls(const char *args);
void cmd_tree(const char *args);
void cmd_cd(const char *args);
void cmd_cat(const char *args);
void cmd_ps(const char *args);
@ -37,5 +39,10 @@ void cmd_exit(const char *args);
void cmd_shutdown(const char *args);
void cmd_reboot(const char *args);
void cmd_lspci(const char *args);
void cmd_lsacpi(const char *args);
void cmd_lsmod(const char *args);
void cmd_modinfo(const char *args);
#define IF_ARG(x) strcmp(args, x) == 0
#endif // !__FENNIX_KERNEL_SHELL_CMDS_H__

View File

@ -47,8 +47,11 @@ void cmd_cat(const char *args)
fstat(fd, &st);
char *buffer = new char[st.st_size + 1];
fread(fd, buffer, st.st_size);
printf("%s\n", buffer);
ssize_t rBytes = fread(fd, buffer, st.st_size);
if (rBytes > 0)
printf("%s\n", buffer);
else
printf("cat: %s: Could not read file\n", args);
delete[] buffer;
fclose(fd);
}

26
kshell/commands/clear.cpp Normal file
View File

@ -0,0 +1,26 @@
/*
This file is part of Fennix Kernel.
Fennix Kernel is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
Fennix Kernel is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Fennix Kernel. If not, see <https://www.gnu.org/licenses/>.
*/
#include "../cmds.hpp"
#include "../../kernel.h"
void cmd_clear(const char *)
{
Display->SetBufferCursor(0, 0, 0);
Display->ClearBuffer(0);
}

View File

@ -0,0 +1,35 @@
/*
This file is part of Fennix Kernel.
Fennix Kernel is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
Fennix Kernel is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Fennix Kernel. If not, see <https://www.gnu.org/licenses/>.
*/
#include "../cmds.hpp"
#include <filesystem.hpp>
#include <acpi.hpp>
#include "../../kernel.h"
using namespace vfs;
void cmd_lsacpi(const char *)
{
ACPI::ACPI *acpi = (ACPI::ACPI *)PowerManager->GetACPI();
foreach (auto Table in acpi->Tables)
{
printf("%s ", Table.first);
}
putchar('\n');
}

37
kshell/commands/lsmod.cpp Normal file
View File

@ -0,0 +1,37 @@
/*
This file is part of Fennix Kernel.
Fennix Kernel is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
Fennix Kernel is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Fennix Kernel. If not, see <https://www.gnu.org/licenses/>.
*/
#include "../cmds.hpp"
#include "../../kernel.h"
void cmd_lsmod(const char *)
{
std::unordered_map<dev_t, Driver::DriverObject> drivers =
DriverManager->GetDrivers();
printf("DRIVER | ID | INIT | MEMORY\n");
foreach (auto &drv in drivers)
{
printf("%-15s | %5ld | %s | %ld KiB\n",
drv.second.Name,
drv.first,
drv.second.Initialized ? "Y" : "N",
TO_KiB(drv.second.vma->GetAllocatedMemorySize()));
}
}

View File

@ -23,8 +23,48 @@
using namespace vfs;
void cmd_lspci(const char *)
void cmd_lspci(const char *args)
{
if (args)
{
if (IF_ARG("-i") || IF_ARG("--info"))
{
foreach (auto Device in PCIManager->GetDevices())
{
const char *HdrType;
switch (Device.Header->HeaderType)
{
case 0:
HdrType = "Normal ";
break;
case 1:
HdrType = "PCI-PCI";
break;
case 2:
HdrType = "Cardbus";
break;
default:
HdrType = "Unknown";
break;
}
printf("%04x:%04x | %s:%03d | %02x:%02x.%d | %s: %s %s\n",
Device.Header->VendorID,
Device.Header->DeviceID,
HdrType, Device.Header->HeaderType,
Device.Bus,
Device.Device,
Device.Function,
PCI::Descriptors::GetSubclassName(Device.Header->Class,
Device.Header->Subclass),
PCI::Descriptors::GetVendorName(Device.Header->VendorID),
PCI::Descriptors::GetDeviceName(Device.Header->VendorID,
Device.Header->DeviceID));
}
return;
}
}
foreach (auto Device in PCIManager->GetDevices())
{
printf("%02x:%02x.%d: %s: %s %s\n",

View File

@ -0,0 +1,73 @@
/*
This file is part of Fennix Kernel.
Fennix Kernel is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
Fennix Kernel is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Fennix Kernel. If not, see <https://www.gnu.org/licenses/>.
*/
#include "../cmds.hpp"
#include "../../kernel.h"
void cmd_modinfo(const char *args)
{
if (args[0] == '\0')
{
printf("Usage: modinfo <driver id/name>\n");
return;
}
dev_t id = atoi(args);
std::unordered_map<dev_t, Driver::DriverObject> drivers =
DriverManager->GetDrivers();
if (drivers.find(id) == drivers.end())
{
bool found = false;
foreach (auto var in drivers)
{
if (strcmp(var.second.Name, args) == 0)
{
id = var.first;
found = true;
break;
}
}
if (!found)
{
printf("Driver not found\n");
return;
}
}
Driver::DriverObject drv = drivers[id];
printf("Base Info:\n");
printf(" Name: %s\n", drv.Name);
printf(" Description: %s\n", drv.Description);
printf(" Author: %s\n", drv.Author);
printf(" Version: %s\n", drv.Version);
printf(" License: %s\n", drv.License);
printf("Resource Info:\n");
printf(" Initialized: %s\n", drv.Initialized ? "yes" : "no");
printf(" Error Code: %i (%s)\n", drv.ErrorCode, strerror(drv.ErrorCode));
printf(" Path: %s\n", drv.Path);
printf(" Used Memory: %ld KiB\n", TO_KiB(drv.vma->GetAllocatedMemorySize()));
printf(" Used IRQs:%s\n", drv.InterruptHandlers->empty() ? " none" : "");
foreach (auto var in *drv.InterruptHandlers)
{
printf(" IRQ%-2d: %#lx\n",
var.first, (uintptr_t)var.second);
}
}

View File

@ -25,6 +25,20 @@
using namespace vfs;
using namespace Tasking;
const char *TaskStateStrings[] = {
"Unknown", // Unknown
"Ready", // Ready
"Running", // Running
"Sleeping", // Sleeping
"Blocked", // Blocked
"Stopped", // Stopped
"Waiting", // Waiting
"CoreDump", // Core dump
"Zombie", // Zombie
"Terminated", // Terminated
};
void cmd_top(const char *)
{
printf("\e9400A1PID \e9CA100Name \e00A15BState \eCCCCCCPriority Memory Usage CPU Usage\n");
@ -32,12 +46,12 @@ void cmd_top(const char *)
{
#if defined(a64)
printf("\e9400A1%-4d \e9CA100%-20s \e00A15B%s \eCCCCCC%d %ld KiB %ld\n",
Proc->ID, Proc->Name, Proc->State == Running ? "Running" : "Stopped",
Proc->ID, Proc->Name, TaskStateStrings[Proc->State.load()],
Proc->Info.Priority, TO_KiB(Proc->GetSize()),
Proc->Info.UserTime + Proc->Info.KernelTime);
#elif defined(a32)
printf("\e9400A1%-4d \e9CA100%-20s \e00A15B%s \eCCCCCC%d %lld KiB %lld\n",
Proc->ID, Proc->Name, Proc->State == Running ? "Running" : "Stopped",
Proc->ID, Proc->Name, TaskStateStrings[Proc->State.load()],
Proc->Info.Priority, TO_KiB(Proc->GetSize()),
Proc->Info.UserTime + Proc->Info.KernelTime);
#endif
@ -46,12 +60,12 @@ void cmd_top(const char *)
{
#if defined(a64)
printf(" \eA80011%-4d \e9CA100%-20s \e00A15B%s \eCCCCCC%d %ld KiB %ld\n",
Thrd->ID, Thrd->Name, Thrd->State == Running ? "Running" : "Stopped",
Thrd->ID, Thrd->Name, TaskStateStrings[Thrd->State.load()],
Thrd->Info.Priority, TO_KiB(Thrd->GetSize()),
Thrd->Info.UserTime + Thrd->Info.KernelTime);
#elif defined(a32)
printf(" \eA80011%-4d \e9CA100%-20s \e00A15B%s \eCCCCCC%d %lld KiB %lld\n",
Thrd->ID, Thrd->Name, Thrd->State == Running ? "Running" : "Stopped",
Thrd->ID, Thrd->Name, TaskStateStrings[Thrd->State.load()],
Thrd->Info.Priority, TO_KiB(Thrd->GetSize()),
Thrd->Info.UserTime + Thrd->Info.KernelTime);
#endif

75
kshell/commands/tree.cpp Normal file
View File

@ -0,0 +1,75 @@
/*
This file is part of Fennix Kernel.
Fennix Kernel is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
Fennix Kernel is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Fennix Kernel. If not, see <https://www.gnu.org/licenses/>.
*/
#include "../cmds.hpp"
#include <filesystem.hpp>
#include "../../kernel.h"
using namespace vfs;
void tree_loop(Node *rootNode, int depth = 0)
{
foreach (auto Child in rootNode->Children)
{
Display->SetBuffer(0);
if (Child->Type == NodeType::DIRECTORY ||
Child->Type == NodeType::MOUNTPOINT)
{
printf("%*s%*s%*s|- %s\n",
depth, "",
depth, "",
depth, "",
Child->Name);
tree_loop(Child, depth + 1);
}
else
printf("%*s%*s%*s|- %s\n",
depth, "",
depth, "",
depth, "",
Child->Name);
}
}
void cmd_tree(const char *args)
{
Node *rootNode = thisProcess->CurrentWorkingDirectory;
if (args[0] == '\0')
{
if (rootNode == nullptr)
rootNode = fs->GetRootNode()->Children[0];
}
else
{
rootNode = fs->GetNodeFromPath(args, thisProcess->CurrentWorkingDirectory);
if (rootNode == nullptr)
{
printf("ls: %s: No such file or directory\n", args);
return;
}
if (rootNode->Type != NodeType::DIRECTORY)
{
printf("%s\n", rootNode->Name);
return;
}
}
printf("%s\n", rootNode->Name);
tree_loop(rootNode);
}

View File

@ -27,29 +27,29 @@ void cmd_uname(const char *args)
{
if (args)
{
if (strcmp(args, "-a") == 0)
if (IF_ARG("-a") || IF_ARG("--all"))
{
printf("Fennix Kernel %s %s %s %s\n",
KERNEL_VERSION, KERNEL_NAME, __DATE__,
KERNEL_ARCH);
}
else if (strcmp(args, "-s") == 0)
else if (IF_ARG("-s") || IF_ARG("--kernel-name"))
{
printf("%s\n", KERNEL_NAME);
}
else if (strcmp(args, "-v") == 0)
else if (IF_ARG("-v") || IF_ARG("--kernel-version"))
{
printf("%s\n", KERNEL_VERSION);
}
else if (strcmp(args, "-n") == 0)
else if (IF_ARG("-n") || IF_ARG("--nodename"))
{
printf("unknown\n");
}
else if (strcmp(args, "-r") == 0)
else if (IF_ARG("-r") || IF_ARG("--kernel-release"))
{
printf("%s\n", KERNEL_NAME);
}
else if (strcmp(args, "-m") == 0)
else if (IF_ARG("-m") || IF_ARG("--machine"))
{
printf("%s\n", KERNEL_ARCH);
}

View File

@ -18,83 +18,15 @@
#include <kshell.hpp>
#include <filesystem.hpp>
#include <module.hpp>
#include <driver.hpp>
#include <lock.hpp>
#include <debug.h>
#include "../modules/PersonalSystem2/keyboard.hpp"
#include "../kernel.h"
#include "../Fex.hpp"
#include "../mapi.hpp"
#include "../driver.h"
#include "cmds.hpp"
using namespace PS2Keyboard;
NewLock(ShellLock);
const char sc_ascii_low[] = {'?', '?', '1', '2', '3', '4', '5', '6',
'7', '8', '9', '0', '-', '=', '?', '?', 'q', 'w', 'e', 'r', 't', 'y',
'u', 'i', 'o', 'p', '[', ']', '?', '?', 'a', 's', 'd', 'f', 'g',
'h', 'j', 'k', 'l', ';', '\'', '`', '?', '\\', 'z', 'x', 'c', 'v',
'b', 'n', 'm', ',', '.', '/', '?', '?', '?', ' '};
const char sc_ascii_high[] = {'?', '?', '!', '@', '#', '$', '%', '^',
'&', '*', '(', ')', '_', '+', '?', '?', 'Q', 'W', 'E', 'R', 'T', 'Y',
'U', 'I', 'O', 'P', '{', '}', '?', '?', 'A', 'S', 'D', 'F', 'G',
'H', 'J', 'K', 'L', ':', '\"', '~', '?', '|', 'Z', 'X', 'C', 'V',
'B', 'N', 'M', '<', '>', '?', '?', '?', '?', ' '};
static int LowerCase = true;
char GetLetterFromScanCode(uint8_t ScanCode)
{
if (ScanCode & 0x80)
{
switch (ScanCode)
{
case KEY_U_LSHIFT:
LowerCase = true;
return 0;
case KEY_U_RSHIFT:
LowerCase = true;
return 0;
default:
return 0;
}
}
else
{
switch (ScanCode)
{
case KEY_D_RETURN:
return '\n';
case KEY_D_LSHIFT:
LowerCase = false;
return 0;
case KEY_D_RSHIFT:
LowerCase = false;
return 0;
case KEY_D_BACKSPACE:
return ScanCode;
default:
{
if (ScanCode > 0x39)
break;
if (LowerCase)
return sc_ascii_low[ScanCode];
else
return sc_ascii_high[ScanCode];
}
}
}
return 0;
}
int GetChar()
{
return 0;
}
struct Command
{
const char *Name;
@ -104,6 +36,7 @@ struct Command
static Command commands[] = {
{"lsof", cmd_lsof},
{"ls", cmd_ls},
{"tree", cmd_tree},
{"cd", cmd_cd},
{"cat", cmd_cat},
{"echo", cmd_echo},
@ -113,7 +46,7 @@ static Command commands[] = {
{"rmdir", nullptr},
{"mv", nullptr},
{"cp", nullptr},
{"clear", nullptr},
{"clear", cmd_clear},
{"help", nullptr},
{"exit", cmd_exit},
{"reboot", cmd_reboot},
@ -136,40 +69,42 @@ static Command commands[] = {
{"chgrp", nullptr},
{"chmod", nullptr},
{"chroot", nullptr},
{"lspci", cmd_lspci}};
{"lspci", cmd_lspci},
{"lsacpi", cmd_lsacpi},
{"lsmod", cmd_lsmod},
{"modinfo", cmd_modinfo},
{"insmod", nullptr},
{"rmmod", nullptr},
{"modprobe", nullptr},
{"depmod", nullptr},
};
void StartKernelShell()
{
if (ShellLock.Locked())
return;
SmartLock(ShellLock);
debug("Starting kernel shell...");
printf("Starting kernel shell...\n");
KPrint("Starting kernel shell...");
thisThread->SetPriority(Tasking::TaskPriority::High);
Display->SetBuffer(0);
Module::ModuleFile KeyboardModule;
if (likely(ModuleManager->GetModules().size() > 0))
{
foreach (auto Module in ModuleManager->GetModules())
{
if (((FexExtended *)Module.ExtendedHeaderAddress)->Module.Type == FexModuleType::FexModuleType_Input &&
((FexExtended *)Module.ExtendedHeaderAddress)->Module.TypeFlags & FexDriverInputTypes::FexDriverInputTypes_Keyboard)
{
KeyboardModule = Module;
printf("Using driver \eCA21F6%s\eCCCCCC for keyboard input.\n",
((FexExtended *)Module.ExtendedHeaderAddress)->Module.Name);
break;
}
}
}
Display->SetBuffer(0);
std::string Buffer;
std::vector<std::string *> History;
size_t HistoryIndex = 0;
bool CtrlDown = false;
bool UpperCase = false;
bool TabDoublePress = false;
int kfd = fopen("/dev/key", "r");
if (kfd < 0)
{
KPrint("Failed to open keyboard device! %s",
strerror(kfd));
return;
}
printf("Using \eCA21F6/dev/key\eCCCCCC for keyboard input.\n");
while (true)
{
size_t BackspaceCount = 0;
@ -185,27 +120,50 @@ void StartKernelShell()
cwd->FullPath);
Display->SetBuffer(0);
uint8_t scBuf[2];
scBuf[1] = 0x00; /* Request scan code */
ssize_t nBytes;
while (true)
{
KernelCallback callback{};
callback.Reason = PollWaitReason;
ModuleManager->IOCB(KeyboardModule.modUniqueID, &callback);
char c = GetLetterFromScanCode(callback.InputCallback.Keyboard.Key);
nBytes = fread(kfd, scBuf, 2);
if (nBytes == 0)
continue;
if (nBytes < 0)
{
KPrint("Failed to read from keyboard device: %s",
strerror((int)nBytes));
return;
}
switch (callback.InputCallback.Keyboard.Key)
if (scBuf[0] == 0x00)
continue;
uint8_t sc = scBuf[0];
switch (sc & ~KEY_PRESSED)
{
case KEY_D_LCTRL:
case KEY_LEFT_CTRL:
case KEY_RIGHT_CTRL:
{
CtrlDown = true;
if (sc & KEY_PRESSED)
CtrlDown = true;
else
CtrlDown = false;
continue;
}
case KEY_U_LCTRL:
case KEY_LEFT_SHIFT:
case KEY_RIGHT_SHIFT:
{
CtrlDown = false;
if (sc & KEY_PRESSED)
UpperCase = true;
else
UpperCase = false;
continue;
}
case KEY_D_BACKSPACE:
case KEY_BACKSPACE:
{
if (!(sc & KEY_PRESSED))
continue;
if (BackspaceCount == 0)
continue;
@ -215,8 +173,11 @@ void StartKernelShell()
Display->SetBuffer(0);
continue;
}
case KEY_D_UP:
case KEY_UP_ARROW:
{
if (!(sc & KEY_PRESSED))
continue;
if (History.size() == 0 ||
HistoryIndex == 0)
continue;
@ -235,8 +196,11 @@ void StartKernelShell()
Display->SetBuffer(0);
continue;
}
case KEY_D_DOWN:
case KEY_DOWN_ARROW:
{
if (!(sc & KEY_PRESSED))
continue;
if (History.size() == 0 ||
HistoryIndex == History.size())
continue;
@ -265,8 +229,11 @@ void StartKernelShell()
Display->SetBuffer(0);
continue;
}
case KEY_D_TAB:
case KEY_TAB:
{
if (!(sc & KEY_PRESSED))
continue;
if (!TabDoublePress)
{
TabDoublePress = true;
@ -276,10 +243,8 @@ void StartKernelShell()
if (Buffer.size() == 0)
{
for (size_t i = 0; i < sizeof(commands) / sizeof(commands[0]); i++)
{
printf("%s ", commands[i].Name);
Display->SetBuffer(0);
}
Display->Print('\n', 0);
Display->SetBuffer(0);
goto SecLoopEnd;
@ -307,9 +272,14 @@ void StartKernelShell()
break;
}
if (c == 0)
if (!(sc & KEY_PRESSED))
continue;
if (!Driver::IsValidChar(sc))
continue;
char c = Driver::GetScanCode(sc, UpperCase);
if (CtrlDown)
{
switch (std::toupper((char)c))
@ -369,7 +339,7 @@ void StartKernelShell()
cmd_extracted += Buffer[i];
}
debug("cmd: %s, array[%d]: %s", cmd_extracted.c_str(), i, commands[i].Name);
// debug("cmd: %s, array[%d]: %s", cmd_extracted.c_str(), i, commands[i].Name);
if (strncmp(commands[i].Name, cmd_extracted.c_str(), cmd_extracted.size()) == 0)
{
if (strlen(commands[i].Name) != cmd_extracted.size())