-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1 parent
cd3be76
commit 12021d5
Showing
10 changed files
with
203 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
|
||
} | ||
} |