Skip to content

Commit

Permalink
增加内核shell框架
Browse files Browse the repository at this point in the history
xiaoyi1212 committed Nov 7, 2024
1 parent cd3be76 commit 12021d5
Showing 10 changed files with 203 additions and 103 deletions.
10 changes: 3 additions & 7 deletions src/core/main.c
Original file line number Diff line number Diff line change
@@ -17,15 +17,10 @@
#include "keyboard.h"
#include "scheduler.h"
#include "krlibc.h"
#include "pipfs.h"
#include "shell.h"

extern void* program_break_end;

int test_proc(void* arg) {
while(1) printk("%c\n",kernel_getch());
return -1;
}

/*
* 内核初始化函数, 最终会演变为CPU0的IDLE进程
* > 注意, 其所有的函数调用顺序均不可改变. 需按顺序初始化OS功能
@@ -65,7 +60,8 @@ _Noreturn void kernel_main(multiboot_t *multiboot, uint32_t kernel_stack) {
init_pcb();

keyboard_init();
create_kernel_thread(test_proc, NULL, "Test");
create_kernel_thread((void*)setup_shell, NULL, "Shell");
klogf(true,"Enable kernel shell service.\n");

klogf(true,"Kernel load done!\n");
enable_scheduler();
3 changes: 1 addition & 2 deletions src/core/task/pcb.c
Original file line number Diff line number Diff line change
@@ -6,7 +6,6 @@
#include "krlibc.h"
#include "io.h"
#include "description_table.h"
#include "devfs.h"

extern pcb_t *current_pcb;
extern pcb_t *running_proc_head;
@@ -23,6 +22,7 @@ static void add_task(pcb_t *new_task){ //添加进程至调度链
if(new_task == NULL) return;
pcb_t *tailt = running_proc_head;
while (tailt->next != running_proc_head) {
if(tailt->next == NULL) break;
tailt = tailt->next;
}
tailt->next = new_task;
@@ -148,7 +148,6 @@ void kill_proc(pcb_t *pcb){
if (head->pid == pcb->pid) {
last->next = pcb->next;
kfree(pcb);
update_pipfs();
io_sti();
return;
}
64 changes: 64 additions & 0 deletions src/driver/cmos.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include "cmos.h"
#include "io.h"
#include "krlibc.h"
#include "kmalloc.h"

#define bcd2hex(n) ((n >> 4) * 10) + (n & 0xf)

uint8_t read_cmos(uint8_t p) {
uint8_t data;
outb(CMOS_INDEX, p);
data = inb(CMOS_DATA);
outb(CMOS_INDEX, 0x80);
return data;
}

uint32_t get_hour() {
return bcd2hex(read_cmos(CMOS_CUR_HOUR));
}

uint32_t get_min() {
return bcd2hex(read_cmos(CMOS_CUR_MIN));
}

uint32_t get_sec() {
return bcd2hex(read_cmos(CMOS_CUR_SEC));
}

uint32_t get_day_of_month() {
return bcd2hex(read_cmos(CMOS_MON_DAY));
}

uint32_t get_day_of_week() {
return bcd2hex(read_cmos(CMOS_WEEK_DAY));
}

uint32_t get_mon() {
return bcd2hex(read_cmos(CMOS_CUR_MON));
}

uint32_t get_year() {
return (bcd2hex(read_cmos(CMOS_CUR_CEN)) * 100) + bcd2hex(read_cmos(CMOS_CUR_YEAR)) + 1980;
}

int is_leap_year(int year) {
if (year % 4 != 0) return 0;
if (year % 400 == 0) return 1;
return year % 100 != 0;
}

char *get_date_time() {
char *s = (char *) kmalloc(40);
int year = get_year(), month = get_mon(), day = get_day_of_month();
int hour = get_hour(), min = get_min(), sec = get_sec();
int day_of_months[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (is_leap_year(year)) day_of_months[2]++;
#ifdef NEED_UTC_8
hour += 8;
if (hour >= 24) hour -= 24, day++;
if (day > day_of_months[month]) day = 1, month++;
if (month > 12) month = 1, year++;
#endif
sprintf(s, "%d/%d/%d %d:%d:%d", year, month, day, hour, min, sec);
return s;
}
17 changes: 11 additions & 6 deletions src/driver/tty.c
Original file line number Diff line number Diff line change
@@ -9,20 +9,25 @@ tty_t default_tty;

uint32_t tty_status = 0;

static void tty_print(tty_t *tty_d,const char* msg){
size_t size = strlen(msg);
if (tty_status == TTY_VGA_OUTPUT){
for (size_t i = 0; i < strlen(msg); i++) vga_putchar(msg[i]);
} else if (tty_status == TTY_OST_OUTPUT) {
terminal_advance_state(msg);
}
}

static void tty_putchar(tty_t *tty_d,int c){
char buf[2] = {c,0};
tty_print(tty_d, buf);
if(tty_status == TTY_VGA_OUTPUT){
vga_putchar(c);
} else if(tty_status == TTY_OST_OUTPUT){
terminal_advance_state_single(c);
}
}

static void tty_print(tty_t *tty_d,const char* msg){
size_t size = strlen(msg);
for (size_t i = 0; i < size; i++)
tty_putchar(tty_d,msg[i]);
}

static void tty_move_cursor(tty_t *tty_d, int x, int y){
vga_move_cursor(x,y);
}
71 changes: 0 additions & 71 deletions src/fs/pipfs.c

This file was deleted.

28 changes: 28 additions & 0 deletions src/include/cmos.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#pragma once

#define NEED_UTC_8

#define CMOS_INDEX 0x70
#define CMOS_DATA 0x71

#define CMOS_CUR_SEC 0x0
#define CMOS_CUR_MIN 0x2
#define CMOS_CUR_HOUR 0x4
#define CMOS_WEEK_DAY 0x6
#define CMOS_MON_DAY 0x7
#define CMOS_CUR_MON 0x8
#define CMOS_CUR_YEAR 0x9
#define CMOS_CUR_CEN 0x32

#include "ctypes.h"

char *get_date_time();

uint32_t get_hour();
uint32_t get_min();
uint32_t get_sec();
uint32_t get_day_of_month();
uint32_t get_day_of_week();
uint32_t get_mon();
uint32_t get_year();
int is_leap_year(int year);
1 change: 0 additions & 1 deletion src/include/devfs.h
Original file line number Diff line number Diff line change
@@ -8,5 +8,4 @@ int dev_get_sector_size(char *path);
int dev_get_size(char *path);
int dev_get_type(char *path); //1:HDD 2:CDROM
void print_devfs();
void update_pipfs();
void devfs_sysinfo_init();
16 changes: 0 additions & 16 deletions src/include/pipfs.h

This file was deleted.

6 changes: 6 additions & 0 deletions src/include/shell.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#pragma once

#define MAX_COMMAND_LEN 100
#define MAX_ARG_NR 50

void setup_shell();
90 changes: 90 additions & 0 deletions src/sysapp/shell.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#include "shell.h"
#include "krlibc.h"
#include "klog.h"
#include "keyboard.h"
#include "cmos.h"

static inline int isprint_syshell(int c) {
return (c > 0x1F && c < 0x7F);
}

static char getc() {
char c;
do{
c = kernel_getch();
if(c == '\b' || c == '\n') break;
} while (!isprint_syshell(c));
return c;
}

int gets(char *buf, int buf_size) {
int index = 0;
char c;
while ((c = getc()) != '\n') {
if (c == '\b') {
if (index > 0) {
index--;
printk("\b \b");
}
} else {
buf[index++] = c;
printk("%c",c);
}
}
buf[index] = '\0';
printk("%c",c);
return index;
}

int cmd_parse(char *cmd_str, char **argv, char token) {
int arg_idx = 0;
while (arg_idx < MAX_ARG_NR) {
argv[arg_idx] = NULL;
arg_idx++;
}
char *next = cmd_str;
int argc = 0;

while (*next) {
while (*next == token) *next++;
if (*next == 0) break;
argv[argc] = next;
while (*next && *next != token) *next++;
if (*next) *next++ = 0;
if (argc > MAX_ARG_NR) return -1;
argc++;
}

return argc;
}

void setup_shell(){
printk("Welcome to MdrOS (%s)\n"
"\n"
" * SourceCode: https://github.com/Mdr-C-Tutorial/MdrOS\n"
" * Website: https://github.com/plos-clan\n"
"\n"
" System information as of %s \n"
"\n"
" Users logged in: Kernel\n"
"\n"
"Copyright 2024 XIAOYI12 (Build by GCC i686-elf-tools)\n"
,KERNEL_NAME
,get_date_time());
char com[MAX_COMMAND_LEN];
char *argv[MAX_ARG_NR];
int argc = -1;
while (1){
printk("\033[32mKernel@localhost: \033[39m$ ");
if (gets(com, MAX_COMMAND_LEN) <= 0) continue;

argc = cmd_parse(com, argv, ' ');

if (argc == -1) {
printk("[Shell]: Error: out of arguments buffer\n");
continue;
}


}
}

0 comments on commit 12021d5

Please sign in to comment.