//----------------------------------------------------------------------------//
// GNU GPL OS/K //
// //
// Desc: Kernel shell //
// //
// //
// Copyright © 2018-2019 The OS/K Team //
// //
// This file is part of OS/K. //
// //
// OS/K 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 //
// any later version. //
// //
// OS/K 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 OS/K. If not, see . //
//----------------------------------------------------------------------------//
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
static Command_t testcmdtable[];
error_t CmdTest(int argc, char **argv, char *cmdline)
{
ExecuteCommand(cmdline+5, testcmdtable);
KernLog ("\n");
return EOK;
}
error_t CmdArgs(int argc, char **argv, char *cmdline)
{
int i;
KernLog("cmdline: '%s'\nargc: %d\n", cmdline, argc);
for (i = 0; i < argc; i++) {
KernLog("argv[%d]: '%s'\n", i, argv[i]);
}
return EOK;
}
error_t CmdAtoi(int argc, char **argv, char *cmdline)
{
int i;
KernLog("cmdline: '%s'\nargc: %d\n", cmdline, argc);
for (i = 0; i < argc; i++) {
KernLog("argv[%d]: '%u'\n", i, atoi(argv[i]));
}
return EOK;
}
error_t CmdDumpATASect(int argc, char **argv, char *cmdline)
{
char sector[512] = {0};
int sectNumber = atoi(argv[1]);
int nb = 1; //atoi(argv[2]);
int x = 0;
int step = 16;
if (sectNumber <= 0 || sectNumber > 255) {
KernLog("Bad argument\n\n");
return EINVAL;
}
if (!nb) nb = 1;
KernLog("Sector begin: %d\n", (sectNumber - 1)*512);
IoReadATA(sector, nb, sectNumber);
while(x < 512*nb) {
KernLog("%C", shcol);
for (int i = 0; i < step; i++) {
KernLog("%02x ", (uchar)sector[i+x]);
}
KernLog(" %C ", VGA_COLOR_LIGHT_BLUE);
for (int i = 0; i < step; i++) {
if (isprint(sector[i+x]))
KernLog("%c",
sector[i+x]
);
else
KernLog("%c", 0);
}
KernLog("\n");
x += step;
}
KernLog("\n\n");
return EOK;
}
error_t CmdDumpMem(int argc, char **argv, char *cmdline)
{
char sector[1024] = {0};
char *address = (char*)strtoul(argv[1], NULL, 16);
int nb = 1; //atoi(argv[2]);
int x = 0;
int step = 16;
KernLog("Address begin: %p\n", address);
for (int i = 0; i < 1024*nb; i++) {
sector[i] = *address++;
}
while(x < 1024*nb) {
KernLog("%C", shcol);
for (int i = 0; i < step; i++) {
KernLog("%02x ", (uchar)sector[i+x]);
}
KernLog(" %C ", VGA_COLOR_LIGHT_BLUE);
for (int i = 0; i < step; i++) {
if (isprint(sector[i+x]))
KernLog("%c",
sector[i+x]
);
else
KernLog("%c", 0);
}
KernLog("\n");
x += step;
}
KernLog("\n\n");
return EOK;
}
error_t CmdFloatDiv(int argc, char **argv, char *cmdline)
{
double a = (double)atoi(argv[1]);
double b = (double)atoi(argv[2]);
double number = a / b;
double number_ent = (double)(ulong)number;
double dec = number - number_ent;
double sub = 0.0;
char res[17]; // 10e-16 is the max precision
for(int i = 0; i < 16; i++) {
dec = (dec * 10.0) - (sub * 10.0);
sub = (double)(ulong)dec;
snprintf(&res[i], 17-i, "%d", (ulong)dec);
}
KernLog("%d / %d = %d.%s \n", (ulong)a, (ulong)b, (ulong)number_ent, res);
return EOK;
}
error_t CmdHelpTest(int argc, char **argv, char *cmdline)
{
uint i, count = 0;
Command_t *cmd;
if (argc == 1) {
KernLog("List of all test built-ins:\n");
for (cmd = testcmdtable; cmd->name != NULL; cmd++, count++) {
KernLog("\t%s", cmd->name);
for (i = strlen(cmd->name)/4; i<3; i++) {
KernLog("\t");
}
KernLog("%C%s%C\n", VGA_COLOR_DARK_GREY, cmd->help, shcol);
}
KernLog("End of list; %u commands total\n", count);
}
return EOK;
}
error_t CmdPageTranslateVirtToPhy(int argc, char **argv, char *cmdline)
{
void *address = (void*)atoul(argv[1]);
if (!(void*)strtoul(argv[1], NULL, 16)) {
KernLog("No argument : translating the userspace address\n");
address = (void *)0x80000000;
}
void *translation = MmTransVirtToPhyAddr(address);
KernLog("Translation of %p is %p\n", address, translation);
return EOK;
}
error_t CmdPageTranslatePhyToVirt(int argc, char **argv, char *cmdline)
{
void *address = (void*)strtoul(argv[1], NULL, 16);
/* if (!(void*)atoul(argv[1])) { */
/* address = (ulong *)0x80000000; */
/* } */
void *translation = MmTransPhyToVirtAddr(address);
KernLog("Translation of %p is %p\n", address, translation);
return EOK;
}
enum
{
PRESENT = 1 << 0,
READWRITE = 1 << 1,
USERMODE = 1 << 2,
WRITETHR = 1 << 3,
CACHEDIS = 1 << 4,
ACCESSED = 1 << 5,
DIRTY = 1 << 6,
HUGE = 1 << 7,
NX = 1UL << 63
};
error_t CmdPageMap(int argc, char **argv, char *cmdline)
{
void *virtual = (void*)strtoul(argv[1], NULL, 16);
void *physical = (void*)strtoul(argv[2], NULL, 16);
MmMapPage(virtual, physical, PRESENT | READWRITE);
return EOK;
}
error_t CmdPageUnmap(int argc, char **argv, char *cmdline)
{
void *virtual = (void*)strtoul(argv[1], NULL, 16);
MmUnmapPage(virtual);
return EOK;
}
error_t CmdPageBlock(int argc, char **argv, char *cmdline)
{
size_t size = (size_t)atoi(argv[1]);
bool usermode = (bool)atoi(argv[2]);
//MmGetPhyPageBlock(size, usermode);
return EOK;
}
error_t CmdPF(int argc, char **argv, char *cmdline)
{
register ulong *address = (ulong*)(ulong)strtoul(argv[1], NULL, 16);
KernLog("Test provoking a fault at %p\n", address);
KernLog("It contained %p\n", *address);
*address = 1;
KernLog("Now it contains %p\n", *address);
KernLog("No page fault : address was valid/present\n");
return EOK;
}
error_t CmdShell(int argc, char **argv, char *cmdline)
{
ShStartShell();
return EOK;
}
error_t CmdStackOverflow(int argc, char **argv, char *cmdline)
{
CmdStackOverflow(0, 0, 0);
return EOK;
}
error_t CmdStackUnderflow(int argc, char **argv, char *cmdline)
{
for (int i = 0 ;; i++) {
asm volatile ("pop %rax\n");
}
return EOK;
}
error_t CmdTimerTest(int argc, char **argv, char *cmdline)
{
int delay = atoi(argv[1]);
Timer_t *timer = KeSetTimer(delay);
while(!(KeGetTimer(timer))) {
bprintf(BStdOut,".");
BStdOut->flusher(BStdOut);
}
KernLog("Finished !\n");
return EOK;
}
static Command_t testcmdtable[] =
{
{ "args", CmdArgs, "Print command line" },
{ "atoi", CmdAtoi, "Print command line atoised" },
{ "dmpsec", CmdDumpATASect, "Dump an ATA sector on screen" },
{ "dmp", CmdDumpMem, "Dump 1MB of memory starting from addr"},
{ "help", CmdHelpTest, "Show this message" },
{ "div", CmdFloatDiv, "Float div. Usage : div a b. Returns a/b"},
{ "transvtp", CmdPageTranslateVirtToPhy, "Translate a virtual to"
" physical address (paging)"},
{ "transptv", CmdPageTranslatePhyToVirt, "Translate a physical to"
" virtual address (paging)"},
{ "pmap", CmdPageMap, "Map a page to given physical addr" },
{ "punmap", CmdPageUnmap, "Unmap a page" },
{ "pblck", CmdPageBlock, "Find a free block of pages" },
{ "pf", CmdPF, "Provoke a PF. Usage: pfault "},
{ "shell", CmdShell, "Start a new shell (nested)", },
{ "stkov", CmdStackOverflow, "Provoke a stack overflow" },
{ "stkun", CmdStackUnderflow, "Provoke a stack underflow" },
{ "timer", CmdTimerTest, "test timer of x ms" },
{ NULL, NULL, NULL }
};