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/>.
23 #include <partition.h>
31 #include <net/socket.h>
39 command_t command_list
;
41 unsigned char test
[80];
43 typedef void (*void_fn_void_t
)(void);
45 extern task_t task_list
;
46 extern vfs_t vfs_list
;
47 extern int cstrcmp (char *one
, char *two
);
48 extern unsigned long file_cache_id
;
49 extern task_t
*_curr_task
;
50 extern module_t
*module_load (char *modname
);
52 char *argparse (char *cmd
)
54 unsigned cmd_len
= strlen (cmd
);
68 static void wait (void)
72 for(wait
= WAIT
; wait
!= 0; wait
--)
76 /*************************************************************\
78 \*************************************************************/
80 unsigned command_help (char *command
, unsigned len
)
88 for (ctx
= command_list
.next
; ctx
!= &command_list
; ctx
= ctx
->next
) {
91 printf ("\n%s : %s", ctx
->name
, ctx
->desc
);
95 printf ("\nPress enter to continue");
97 /* wait, until enter key is pressed */
98 while (getkey () != '\n')
101 settextcolor (15, 0);
113 unsigned command_hdd (char *command
, unsigned len
)
122 outportb (0x70, 0x12);
127 printf ("/dev/hda not installed\n");
131 outportb (0x70, 0x1b);
133 outportb (0x70, 0x1c);
135 outportb (0x70, 0x1d);
137 outportb (0x70, 0x23);
139 printf ("/dev/hda installed - CHS=%d-%d:%d:%d\n", c
, c2
, h
, s
);
144 printf ("/dev/hdb not installed\n");
148 outportb (0x70, 0x24);
150 outportb (0x70, 0x25);
152 outportb (0x70, 0x26);
154 outportb (0x70, 0x2c);
156 printf ("/dev/hdb installed - CHS=%d-%d:%d:%d\n", c
, c2
, h
, s
);
162 unsigned command_reboot (char *command
, unsigned len
)
164 outportb (0x64, 0xFE);
171 unsigned command_halt (char *command
, unsigned len
)
175 puts ("\nSystem halted\nPlease press power button ..");
184 unsigned command_tasks (char *command
, unsigned len
)
186 puts ("name\tpriority\n");
189 for (task
= task_list
.next
; task
!= &task_list
; task
= task
->next
)
190 printf ("%s\t%u\n", task
->name
, task
->priority
);
195 unsigned command_ps (char *command
, unsigned len
)
202 unsigned command_uptime (char *command
, unsigned len
)
209 unsigned command_version (char *command
, unsigned len
)
216 unsigned command_debug (char *command
, unsigned len
)
220 DPRINT ("developer mode was enabled.");
225 DPRINT ("developer mode was disabled.");
231 unsigned command_mount (char *command
, unsigned len
)
233 strcpy (test
, argparse (command
));
237 unsigned l
= strlen (test
);
245 memcpy (devname
, test
, x
);
247 strcpy (mountpoint
, argparse (test
));
248 unsigned y
= strlen (mountpoint
);
250 if (mountpoint
[y
-1] != '/') {
252 mountpoint
[y
+1] = '\0';
256 partition_t
*p
= partition_find (devname
);
259 mount (p
, "", mountpoint
);
261 printf ("ERROR -> partition %s does not exists\n", devname
);
268 unsigned command_env (char *command
, unsigned len
)
275 unsigned command_cd (char *command
, unsigned len
)
277 strcpy (test
, argparse (command
));
284 unsigned command_ls (char *command
, unsigned len
)
286 strcpy (test
, argparse (command
));
293 unsigned command_cat (char *command
, unsigned len
)
295 strcpy (test
, argparse (command
));
296 unsigned file_len
= strlen (test
);
299 strcpy (pwd
, (char *) env_get ("PWD"));
302 for (vfs
= vfs_list
.next
; vfs
!= &vfs_list
; vfs
= vfs
->next
)
303 if (!strcmp (vfs
->mountpoint
, pwd
) &&
304 !cstrcmp (test
, vfs
->name
)) {
305 if (vfs
->attrib
& VFS_FILEATTR_DIR
) {
306 printf ("ERROR -> this is a directory, not an file\n");
310 /* check permissions */
311 if (vfs
->attrib
& VFS_FILEATTR_SYSTEM
&& strcmp ((char *) env_get ("USER"), "root")) {
312 printf ("ERROR -> only root can do that\n");
317 if (vfs
->attrib
& VFS_FILEATTR_MOUNTED
) {
318 partition_t
*p
= (partition_t
*) mount_find ((char *) env_get ("PWD"));
323 printf ("ERROR -> device not respond\n");
328 env_set ("PWD", pwd
);
333 printf ("No such file : %s\n", test
);
338 unsigned command_mkdir (char *command
, unsigned len
)
340 strcpy (test
, argparse (command
));
347 unsigned command_exec (char *command
, unsigned len
)
349 if (!strncmp ("./", command
, 2))
350 strcpy (test
, command
+2);
352 strcpy (test
, argparse (command
));
355 unsigned arg_len
= strlen (argparse (test
));
357 strcpy (arg
, argparse (test
));
360 unsigned test_len
= strlen (test
);
361 test
[test_len
-arg_len
] = '\0';
366 //printf ("l: %llu\n", file_cache_id);
368 unsigned char *bin
= (unsigned char *) kmalloc (sizeof (char) * file_cache_id
+ 1);
370 memcpy (bin
, file_cache
, file_cache_id
);
371 bin
[file_cache_id
] = '\0';
374 int err
= exec_elf (bin
, &entry
);
377 printf ("ERROR -> invalid ELF exec\n");
384 proc_t
*proc
= proc_create (currtty
, test
, entry
);
387 printf ("ERROR -> Invalid process: %s\n", test
);
391 /* well, set argument for out app */
392 proc_arg_set (proc
, arg
, arg_len
);
394 /* wait here, until pid != 0*/
398 /* enable interrupts */
401 /* unlock tty console - user can write in */
402 tty_unlock (proc
->tty
);
404 _curr_task
= proc
->tty
->task
; // this is pretty needed for correct return to our tty task
406 if (!proc_done (proc
))
407 while (1) // NOTE: heh, this is good stuff :P
410 /* free image of app */
413 /* switch to next task */
419 unsigned command_lsdev (char *command
, unsigned len
)
426 unsigned command_login (char *command
, unsigned len
)
428 currtty
->user
= NULL
;
429 currtty
->logged
= false;
437 unsigned command_serialw (char *command
, unsigned len
)
439 dev_t
*dev
= dev_find ("/dev/com0");
442 unsigned len
= strlen (argparse (command
));
444 memcpy (data
, argparse (command
), len
);
447 dev
->handler (DEV_ACT_WRITE
, data
, len
);
449 printf ("serialw: %s\n", data
);
455 unsigned command_serialr (char *command
, unsigned len
)
457 dev_t
*dev
= dev_find ("/dev/com0");
462 dev
->handler (DEV_ACT_READ
, data
, 1);
464 printf ("serialr: %s\n", data
);
470 unsigned command_fdisk (char *command
, unsigned len
)
477 unsigned command_hdcat (char *command
, unsigned len
)
479 partition_t
*p
= partition_find (argparse (command
));
482 dev_t
*dev
= (dev_t
*) dev_findbypartition (p
);
484 unsigned char block
[512];
486 printf ("dev: %s\n", dev
->devname
);
488 while (!key_pressed (1)) {
489 if (key_pressed (72) == 1) {
490 printf ("##block: %d\n", d
);
491 dev
->handler (DEV_ACT_READ
, p
, block
, "", d
);
493 for (c
= 0; c
< 512; c
++)
494 putch ((unsigned)block
[c
]);
501 printf ("Please specify partition, example: hdcat /dev/hda0\n");
506 unsigned command_free (char *command
, unsigned len
)
513 unsigned command_date (char *command
, unsigned len
)
517 printf ("%02u:%02u:%02u, %u.%u.%u\n", realtime
->tm_hour
, realtime
->tm_min
, realtime
->tm_sec
,
518 realtime
->tm_mday
, realtime
->tm_mon
, realtime
->tm_year
);
558 void play_tone (dev_t
*dev
, char tone
, char type
, char time
)
580 printf ("play_tone (.., %c, %d, %d);\n", tone
, t
, tt
);
584 dev
->handler (DEV_ACT_PLAY
, 131*t
);
587 dev
->handler (DEV_ACT_PLAY
, 147*t
);
590 dev
->handler (DEV_ACT_PLAY
, 165*t
);
593 dev
->handler (DEV_ACT_PLAY
, 175*t
);
596 dev
->handler (DEV_ACT_PLAY
, 196*t
);
599 dev
->handler (DEV_ACT_PLAY
, 220*t
);
602 dev
->handler (DEV_ACT_PLAY
, 247*t
);
605 dev
->handler (DEV_ACT_PLAY
, 136*t
);
608 dev
->handler (DEV_ACT_PLAY
, 188*t
);
626 printf ("end: play_tone ();\n");
628 dev
->handler (DEV_ACT_PLAY
, 0);
631 void play_song (dev_t
*dev
, char *data
)
633 printf ("play_song ();\n");
634 unsigned i
= 0, l
= strlen (data
);
638 printf ("switch ()\n");
659 play_tone (dev
, data
[i
], data
[i
+1], data
[i
+2]);
669 unsigned command_spk (char *command
, unsigned len
)
671 unsigned int freq
= 20;
673 dev_t
*dev
= dev_find ("/dev/pcspk");
677 We wish you a merry christmas v1.0
679 D2(1/8) G2(1/8) G2(1/16) A2(1/16) G2(1/16) Fis2(1/16) E2(1/8) C2(1/8) E2(1/8) A2(1/8) A2(1/16) H2(1/16) A2(1/16) G2(1/16) Fis2(1/8) D2(1/4) H2(1/8) H2(1/16) C3(1/16) H2(1/16) A2(1/16) G2(1/8) E2(1/8) D2(1/16) D2(1/16) E2(1/8) A2(1/8) Fis2(1/8) G2(1/8)
682 play_song (dev
, "d21 g21 g22 a22 g22 F22 e21 c21 e21 a21 a22 h22 a22 g22 F21 d20 h21 h22 c32 h22 a22 g21 e21 d22 d22 e21 a21 F21 g21");
687 We wish you a merry christmas v3.0
689 G2(1/8) C3(1/8) C3(1/16) D3(1/16) C3(1/16) H2(1/16) A2(1/4) D3(1/8) D3(1/16) E3(1/16) D3(1/16) C3(1/16) H2(1/4) G2(1/8) E3(1/8) E3(1/16) F3(1/16) E3(1/16) D3(1/16) C3(1/8) A2(1/8) G2(1/8) A2(1/8) D3(1/8) H2(1/8) C3(1/8)
692 play_song (dev
, "g21 c31 c32 d32 c32 h22 a20 d31 d32 e32 d32 c32 h20 g21 e31 e32 f32 e32 d32 c31 a21 g21 a21 d31 h21 c31");
698 G2(1/8) C3(1/8) P(1/16) G2(1/16) G3(1/8) C3(1/16) E3(1/8) D3(1/16) E3(1/16) C3(1/8) G2(1/8) C3(1/8) A3(1/8) G3(1/8) E3(1/16) F3(1/16) G3(1/8) G3(1/16) P(1/16) G2(1/8) C3(1/8) A3(1/8) G3(1/8) E3(1/16) F3(1/16) G3(1/8) D3(1/16) E3(1/16) C3(1/8) G2(1/8) C3(1/8) E3(1/16) F3(1/16) D3(1/8) G3(1/8) G3(1/8)
700 /* play_song (dev, "g21 c31 p2 g22 g31 c32 e31 d32 e32 c31 g21 c31 a31 g31 e32 f32 g31 g32 p2 g21 c31 a31 g31 e32 f32 g31 d32 e32 c31 g21 c31 e32 f32 d31 g31 g31");*/
708 extern bool bus_pci_acthandler (unsigned act
, char *block
, unsigned block_len
);
709 extern netif_t netif_list
;
710 extern void ext2_dump ();
711 unsigned command_test (char *command
, unsigned len
)
715 /*ipv4_addr_t sender_ipaddr;
716 sender_ipaddr = IPV4_DOTADDR_TO_ADDR (192, 168, 1, 1);
719 ip_addr = IPV4_DOTADDR_TO_ADDR (192, 168, 1, 2);
722 for (netif = netif_list.next; netif != &netif_list; netif = netif->next) {
725 arp_send_request(netif, sender_ipaddr, ip_addr);
728 /* dev_t *eth = dev_find ("/dev/pcnet32");
733 char *buf = (char *) kmalloc (512 * sizeof (char));*/
734 /*memcpy (buf, "Ahoj hovna !!", 14);
735 eth->handler (DEV_ACT_WRITE, buf, 14);
737 printf ("pcnet32->write '%s'\n", buf);*/
740 /*eth->handler (DEV_ACT_READ, buf, 10);
742 printf ("pcnet32->read '%s'\n", buf);*/
749 partition_t
*p
= partition_find ("/dev/fd0");
752 mount (p
, "", "/mnt/floppy/");
754 printf ("WARNING -> partition /dev/fd0 does not exists\n");
757 cd ((char *) "/mnt/floppy");
759 command_exec ("exec websrv 1959", 16); // irc 207.158.1.150 6667
767 extern void ps2mouse_get_status (int *x
, int *y
, int *state
);
768 extern unsigned ps2mouse_install ();
769 unsigned command_mouse (char *command
, unsigned len
)
771 printf ("PS/2 Mouse test\n");
780 ps2mouse_get_status (&x
, &y
, &state
);
782 if (x
!= x2
&& y
!= y2
)
783 printf ("mys: %d %d %d\n", x
, y
, state
);
795 unsigned command_vesa (char *command
, unsigned len
)
797 if (!init_video_vesa ())
798 printf ("vesa -> failed\n");
803 unsigned command_kill (char *command
, unsigned len
)
805 strcpy (test
, argparse (currtty
->shell
));
807 pid_t pid
= (pid_t
) atoi (test
);
809 proc_t
*proc
= proc_findbypid (pid
);
812 printf ("kill: (%s) - No such process\n", test
);
816 /* send SIGTERM to selected process */
817 if (!proc_signal (proc
, SIGTERM
))
823 unsigned command_modprobe (char *command
, unsigned len
)
825 unsigned arg_len
= strlen (argparse (currtty
->shell
));
827 strcpy (arg
, argparse (currtty
->shell
));
835 unsigned command_lsmod (char *command
, unsigned len
)
842 #ifdef CONFIG_DRV_PCI
843 unsigned command_lspci (char *command
, unsigned len
)
851 unsigned command_iflist (char *command
, unsigned len
)
858 /*************************************************************\
860 \*************************************************************/
862 unsigned command_parser (char *command
, unsigned len
)
866 for (ctx
= command_list
.next
; ctx
!= &command_list
; ctx
= ctx
->next
) {
867 if (!cstrcmp (ctx
->name
, command
)) {
868 ctx
->handler (command
, len
);
874 /* error -> command not found */
875 printf ("%s: command not found\n", command
);
880 void commands (int i
)
882 command_parser (currtty
->shell
, currtty
->shell_len
);
887 if (!cstrcmp ("killall", currtty
->shell
))
889 strcpy (test
, argparse (currtty
->shell
));
892 for (task
= task_list
.next
; task
!= &task_list
; task
= task
->next
) {
893 if (!strcmp (task
->name
, test
))
902 unsigned command_register (char *name
, char *desc
, command_handler_t
*handler
, unsigned flags
)
905 /* we dont want commands with same name */
906 for (c
= command_list
.next
; c
!= &command_list
; c
= c
->next
)
907 if (!strcmp(c
->name
, name
))
912 // alloc and init context
913 ctx
= (command_t
*) malloc (sizeof (command_t
));
915 unsigned name_len
= strlen (name
);
916 ctx
->name
= (char *) malloc (sizeof (char) * name_len
+1);
917 memcpy (ctx
->name
, name
, name_len
);
918 ctx
->name
[name_len
] = '\0';
920 unsigned desc_len
= strlen (desc
);
921 ctx
->desc
= (char *) malloc (sizeof (char) * desc_len
+1);
922 memcpy (ctx
->desc
, desc
, desc_len
);
923 ctx
->desc
[desc_len
] = '\0';
925 ctx
->handler
= handler
;
928 ctx
->next
= &command_list
;
929 ctx
->prev
= command_list
.prev
;
930 ctx
->prev
->next
= ctx
;
931 ctx
->next
->prev
= ctx
;
936 unsigned command_unregister (char *name
)
940 for (ctx
= command_list
.next
; ctx
!= &command_list
; ctx
= ctx
->next
) {
941 if (!strcmp(ctx
->name
, name
)) {
942 ctx
->next
->prev
= ctx
->prev
;
943 ctx
->prev
->next
= ctx
->next
;
952 /*************************************************************\
953 | INITIALIZATION OF COMMANDS |
954 \*************************************************************/
956 unsigned int init_commands ()
958 command_list
.next
= &command_list
;
959 command_list
.prev
= &command_list
;
962 command_register ("help", "Displays list of available commands", &command_help
, 0);
963 command_register ("hdd", "Detect HDD", &command_hdd
, 0);
964 command_register ("reboot", "Reboot computer", &command_reboot
, 0);
965 command_register ("halt", "Shutdown computer", &command_halt
, 0);
966 command_register ("tasks", "Print all tasks", &command_tasks
, 0);
967 command_register ("ps", "Print all process", &command_ps
, 0);
968 command_register ("uptime", "Print uptime in seconds", &command_uptime
, 0);
969 command_register ("version", "Displays a version of system", &command_version
, 0);
970 command_register ("debug", "Change to developer mode", &command_debug
, 0);
971 command_register ("mount", "Mount device to selected directory", &command_mount
, 0);
972 command_register ("env", "Displays all env variables", &command_env
, 0);
973 command_register ("cd", "Change directory", &command_cd
, 0);
974 command_register ("ls", " Displays files in current directory", &command_ls
, 0);
975 command_register ("cat", "Displays text in selected file", &command_cat
, 0);
976 command_register ("mkdir", "Create a directory", &command_mkdir
, 0);
977 command_register ("exec", "Execute a selected program", &command_exec
, 0);
978 command_register ("lsdev", "Displays found devices", &command_lsdev
, 0);
979 command_register ("login", "Login as another user", &command_login
, 0);
980 command_register ("serialw", "Write data on rs232", &command_serialw
, 0);
981 command_register ("serialr", "Read data from rs232", &command_serialr
, 0);
982 command_register ("fdisk", "Partition table manipulator", &command_fdisk
, 0);
983 command_register ("hdcat", "Read selected block of data from drive", &command_hdcat
, 0);
984 command_register ("free", "Display amount of free and used memory", &command_free
, 0);
985 command_register ("date", "Show current date and time", &command_date
, 0);
986 //command_register ("xmastime", "play noel on pc-speaker", &command_spk, 0);
987 //command_register ("test", "Some test", &command_test, 0);
988 //command_register ("vesa", "Some test", &command_vesa, 0);
989 command_register ("kill", "Send a signal to process", &command_kill
, 0);
990 command_register ("modprobe", "program to add modules from the kernel", &command_modprobe
, 0);
991 command_register ("lsmod", "program to show the status of modules", &command_lsmod
, 0);
992 #ifdef CONFIG_DRV_PCI
993 command_register ("lspci", "list all pci devices", &command_lspci
, 0);
995 // command_register ("mouse", "ps2 mouse test", &command_mouse, 0);
996 command_register ("iflist", "Show network interface", &command_iflist
, 0);