RTC added, new syscalls, i386 code was moved to separate part, better
[ZeXOS.git] / kernel / core / commands.c
blobaa3ebaf518fcf0107280ce4250f99c403147e434
1 /*
2 * ZeX/OS
3 * Copyright (C) 2007 Tomas 'ZeXx86' Jedrzejek (zexx86@gmail.com)
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <system.h>
21 #include <string.h>
22 #include <partition.h>
23 #include <env.h>
24 #include <dev.h>
25 #include <vfs.h>
26 #include <fs.h>
27 #include <tty.h>
28 #include <proc.h>
29 #include <stdlib.h>
30 #include <net/socket.h>
31 #include <module.h>
35 unsigned char test[80];
37 typedef void (*void_fn_void_t)(void);
39 extern task_t task_list;
40 extern vfs_t vfs_list;
41 extern int cstrcmp (char *one, char *two);
42 extern unsigned long file_cache_id;
43 extern task_t *_curr_task;
44 extern module_t *module_load (char *modname);
46 char *argparse (char *cmd)
48 unsigned cmd_len = strlen (cmd);
49 unsigned p = 1;
51 while (p < cmd_len) {
52 if (cmd[p] == ' ')
53 return cmd + p + 1;
55 p ++;
58 return "";
61 #define WAIT 0xFFFFFL
62 static void wait (void)
64 unsigned long wait;
66 for(wait = WAIT; wait != 0; wait--)
67 /* nothing */;
70 unsigned thread ()
72 while (1) {
73 schedule ();
74 //timer_wait (18);
75 putch ('*');
76 wait ();
79 return 1;
82 void commands (int i)
84 if (!cstrcmp ("help", currtty->shell))
86 printf ("hdd Detect HDD\n");
87 printf ("help Print help\n");
88 printf ("reboot Reboot computer\n");
89 printf ("halt Shutdown computer\n");
90 printf ("tasks Print all tasks\n");
91 printf ("ps Print all process\n");
92 printf ("uptime Print uptime in seconds\n");
93 printf ("version Displays a version of system\n");
94 printf ("debug Change to developer mode\n");
95 printf ("mount Mount device to selected directory\n");
96 printf ("env Displays all env variables\n");
97 printf ("cd Change directory\n");
98 printf ("ls Displays files in current directory\n");
99 printf ("cat Displays text in selected file\n");
100 printf ("mkdir Create a directory\n");
101 printf ("exec Execute a selected program\n");
102 printf ("lsdev Displays found devices\n");
103 printf ("login Login as another user\n");
104 printf ("serialw Write data on rs232\n");
105 printf ("serialr Read data from rs232\n");
106 printf ("fdisk Partition table manipulator\n");
107 printf ("hdcat Read selected block of data from drive\n");
108 printf ("nc Clone of netcat (nc <address> <port>)\n");
109 printf ("free Display amount of free and used memory\n");
111 return;
114 if (!cstrcmp ("tasks", currtty->shell))
116 puts ("name\tpriority\n");
118 task_t *task;
119 for (task = task_list.next; task != &task_list; task = task->next)
120 printf ("%s\t%u\n", task->name, task->priority);
122 return;
125 if (!cstrcmp ("reboot", currtty->shell))
127 outportb (0x64, 0xFE);
128 while(1)
129 /* nothing */;
132 if (!cstrcmp ("halt", currtty->shell))
134 halt ();
136 puts ("\nSystem halted\nPlease press power button ..");
137 while(1);
140 if (!cstrcmp ("uptime", currtty->shell))
142 uptime ();
144 return;
147 if (!cstrcmp ("ls", currtty->shell))
149 strcpy (test, argparse (currtty->shell));
151 ls (test);
153 return;
156 if (!cstrcmp ("cd", currtty->shell))
158 strcpy (test, argparse (currtty->shell));
160 cd (test);
162 return;
165 if (!cstrcmp ("cat", currtty->shell))
167 strcpy (test, argparse (currtty->shell));
168 unsigned file_len = strlen (test);
170 char pwd[64];
171 strcpy (pwd, (char *) env_get ("PWD"));
173 vfs_t *vfs;
174 for (vfs = vfs_list.next; vfs != &vfs_list; vfs = vfs->next)
175 if (!strcmp (vfs->mountpoint, pwd) &&
176 !cstrcmp (test, vfs->name)) {
177 if (vfs->attrib & VFS_FILEATTR_DIR) {
178 printf ("ERROR -> this is a directory, not an file\n");
179 return;
182 /* check permissions */
183 if (vfs->attrib & VFS_FILEATTR_SYSTEM && strcmp ((char *) env_get ("USER"), "root")) {
184 printf ("ERROR -> only root can do that\n");
185 return;
189 if (vfs->attrib & VFS_FILEATTR_MOUNTED) {
190 partition_t *p = mount_find ((char *) env_get ("PWD"));
192 if (p)
193 cat (p, test);
194 else {
195 printf ("ERROR -> device not respond\n");
196 return;
200 env_set ("PWD", pwd);
202 return;
205 printf ("No such file : %s\n", test);
207 return;
210 if (!cstrcmp ("mkdir", currtty->shell))
212 strcpy (test, argparse (currtty->shell));
214 mkdir (test);
216 return;
219 if (!cstrcmp ("hdd", currtty->shell))
221 int t;
222 int m;
223 int c;
224 int c2;
225 int h;
226 int s;
228 outportb (0x70, 0x12);
229 t = inportb (0x71);
231 if (t >> 4 == 0)
233 printf ("/dev/hda not installed\n");
235 else
237 outportb (0x70, 0x1b);
238 c = inportb (0x71);
239 outportb (0x70, 0x1c);
240 c2 = inportb (0x71);
241 outportb (0x70, 0x1d);
242 h = inportb (0x71);
243 outportb (0x70, 0x23);
244 s = inportb (0x71);
245 printf ("/dev/hda installed - CHS=%d-%d:%d:%d\n", c, c2, h, s);
248 if (t & 0xF == 0)
250 printf ("/dev/hdb not installed\n");
252 else
254 outportb (0x70, 0x24);
255 c = inportb (0x71);
256 outportb (0x70, 0x25);
257 c2 = inportb (0x71);
258 outportb (0x70, 0x26);
259 h = inportb (0x71);
260 outportb (0x70, 0x2c);
261 s = inportb (0x71);
262 printf ("/dev/hdb installed - CHS=%d-%d:%d:%d\n", c, c2, h, s);
265 return;
268 if (!cstrcmp ("debug", currtty->shell))
270 if (!debug) {
271 debug = 1;
272 DPRINT ("developer mode was enabled.");
274 return;
277 DPRINT ("developer mode was disabled.");
278 debug = 0;
280 return;
283 /* if (!cstrcmp ("thread", currtty->shell))
285 tty_lock (currtty);
287 proc_t *proc = proc_create (currtty, "thread", thread);
289 return;
292 if (!strncmp ("./", currtty->shell, 2) || !cstrcmp ("exec", currtty->shell))
294 if (!strncmp ("./", currtty->shell, 2))
295 strcpy (test, currtty->shell+2);
296 else
297 strcpy (test, argparse (currtty->shell));
300 unsigned arg_len = strlen (argparse (test));
301 char arg[arg_len+1];
302 strcpy (arg, argparse (test));
303 arg[arg_len] = '\0';
305 unsigned test_len = strlen (test);
306 test [test_len-arg_len] = '\0';
308 if (!exec (test))
309 return;
311 //printf ("l: %llu\n", file_cache_id);
313 unsigned char *bin = (unsigned char *) kmalloc (sizeof (char) * file_cache_id + 1);
315 memcpy (bin, file_cache, file_cache_id);
316 bin[file_cache_id] = '\0';
318 unsigned entry;
319 int err = exec_elf (bin, &entry);
321 if (err != NULL) {
322 printf ("ERROR -> invalid ELF exec\n");
323 return;
326 tty_lock (currtty);
328 asm volatile (
329 "movl %1, %%ebx;"
330 "movl %0, %%eax;"
331 :: "g" (arg_len), "b" (arg));
333 proc_t *proc = proc_create (currtty, bin, entry);
335 if (!proc) {
336 printf ("ERROR -> Invalid process: %s\n", test);
337 return;
339 /* send parameters to application */
342 // printf ("arg: '%s' - %d\n", arg, arg_len);
344 /* start application */
345 ((void_fn_void_t) entry) ();
347 /* free memory of app image */
348 flush_elf ();
350 return;
353 if (!cstrcmp ("ps", currtty->shell))
355 proc_display ();
357 return;
360 if (!cstrcmp ("mount", currtty->shell))
363 strcpy (test, argparse (currtty->shell));
364 char devname[20];
365 char mountpoint[32];
367 unsigned l = strlen (test);
368 unsigned x = 0;
369 while (x < l) {
370 if (test[x] == ' ')
371 break;
372 x ++;
375 memcpy (devname, test, x);
376 devname[x] = '\0';
377 strcpy (mountpoint, argparse (test));
378 unsigned y = strlen (mountpoint);
380 if (mountpoint[y-1] != '/') {
381 mountpoint[y] = '/';
382 mountpoint[y+1] = '\0';
385 if (x && y) {
386 partition_t *p = partition_find (devname);
388 if (p)
389 mount (p, "", mountpoint);
390 else
391 printf ("ERROR -> partition %s does not exists\n", devname);
392 } else
393 mount_display ();
395 return;
398 if (!cstrcmp ("env", currtty->shell))
400 env_display ();
402 return;
405 if (!cstrcmp ("lsdev", currtty->shell))
407 dev_display ();
409 return;
412 if (!cstrcmp ("login", currtty->shell))
414 currtty->user = NULL;
415 currtty->logged = false;
417 settextcolor (7, 0);
418 puts ("login: ");
420 return;
423 if (!cstrcmp ("version", currtty->shell))
425 osinfo ();
427 return;
430 if (!cstrcmp ("serialr", currtty->shell))
432 dev_t *dev = dev_find ("/dev/com0");
434 if (dev) {
435 char data[11];
437 dev->handler (DEV_ACT_READ, data, 10);
439 printf ("serialr: %s\n", data);
442 return;
445 if (!cstrcmp ("serialw", currtty->shell))
447 dev_t *dev = dev_find ("/dev/com0");
449 if (dev) {
450 unsigned len = strlen (argparse (currtty->shell));
451 char data[len+1];
452 memcpy (data, argparse (currtty->shell), len);
453 data[len] = '\0';
455 dev->handler (DEV_ACT_WRITE, data, len);
457 printf ("serialw: %s\n", data);
460 return;
463 if (!cstrcmp ("fdisk", currtty->shell))
465 fdisk ();
466 return;
469 if (!cstrcmp ("hdcat", currtty->shell))
471 partition_t *p = partition_find (argparse (currtty->shell));
473 if (p) {
474 dev_t *dev = dev_findbypartition (p);
475 int c, d = 0;
476 unsigned char block[512];
478 printf ("dev: %s\n", dev->devname);
480 while (!key_pressed (1)) {
481 if (key_pressed (72) == 1) {
482 printf ("##block: %d\n", d);
483 dev->handler (DEV_ACT_READ, p, block, "", d);
485 for (c = 0; c < 512; c ++)
486 putch ((unsigned)block[c]);
487 d ++;
490 usleep (5);
492 } else
493 printf ("Please specify partition, example: hdcat /dev/hda0\n");
495 return;
498 /*if (!cstrcmp ("ircc", currtty->shell))
500 irc_client (argparse (currtty->shell), 6667);
501 return;
504 if (!cstrcmp ("nc", currtty->shell))
506 strcpy (test, argparse (currtty->shell));
508 char arg1[20];
509 char arg2[6];
511 unsigned l = strlen (test);
512 unsigned x = 0;
513 while (x < l) {
514 if (test[x] == ' ')
515 break;
516 x ++;
519 memcpy (arg1, test, x);
520 arg1[x] = '\0';
522 strcpy (arg2, argparse (test));
524 nc (arg1, atoi (arg2));
525 return;
528 /* test cmd */
529 if (!cstrcmp ("spk", currtty->shell))
531 unsigned int freq = 20;
533 dev_t *dev = dev_find ("/dev/pcspk");
535 if (dev) {
536 while (freq) {
537 dev->handler (DEV_ACT_PLAY, freq);
538 timer_wait (50);
540 if (key_pressed (72) == 1) {
541 freq ++;
542 printf ("freq: %d\n", freq);
545 if (key_pressed (80) == 1) {
546 freq --;
547 printf ("freq: %d\n", freq);
552 /* printf ("cislo: 0x%x je %d\n", 1193182UL/750, 1193182UL/750);
555 /* MOV BX,freq
556 MOV AX,0x34DD
557 MOV DX,0x0012
558 CMP DX,BX
559 JNB J1
560 DIV BX
561 MOV BX,AX
562 IN AL,0x61
563 TEST AL,0x03
564 JNZ J2
565 OR AL,0x03
566 OUT 0x61,AL
567 MOV AL,-0x4A
568 OUT 0x43,AL
571 asm{
572 MOV AL,BL
573 OUT 0x42,AL
574 MOV AL,BH
575 OUT 0x4*/
577 return;
580 if (!cstrcmp ("killall", currtty->shell))
582 strcpy (test, argparse (currtty->shell));
584 task_t *task;
585 for (task = task_list.next; task != &task_list; task = task->next) {
586 if (!strcmp (task->name, test))
587 task_done (task);
590 return;
593 if (!cstrcmp ("free", currtty->shell))
595 util_cmdfree ();
597 return;
600 if (!cstrcmp ("modprobe", currtty->shell))
602 unsigned arg_len = strlen (argparse (currtty->shell));
603 char arg[arg_len+1];
604 strcpy (arg, argparse (currtty->shell));
605 arg[arg_len] = '\0';
607 module_load (arg);
609 return;
612 if (!cstrcmp ("date", currtty->shell))
614 rtc_getcurrtime ();
616 printf ("%02u:%02u:%02u, %u.%u.%u\n", realtime->tm_hour, realtime->tm_min, realtime->tm_sec,
617 realtime->tm_mday, realtime->tm_mon, realtime->tm_year);
619 return;
622 /* error -> command not found */
623 printf ("%s: command not found\n", currtty->shell);