3 * Copyright (C) 2007 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
4 * Copyright (C) 2008 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
5 * Copyright (C) 2009 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
6 * Copyright (C) 2010 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
27 #include <partition.h>
36 #include <net/socket.h>
50 #include <net/hostname.h>
54 #include <sound/audio.h>
56 command_t command_list
;
57 extern partition_t partition_list
;
59 unsigned char cmd_buf
[256];
61 typedef void (*void_fn_void_t
)(void);
63 extern task_t task_list
;
64 extern vfs_t vfs_list
;
65 extern unsigned long file_cache_id
;
66 extern task_t
*_curr_task
;
67 extern module_t
*module_load (char *modname
);
68 extern unsigned long timer_ticks
;
71 char *argparse (char *cmd
)
73 unsigned cmd_len
= strlen (cmd
);
86 /*************************************************************\
88 \*************************************************************/
91 unsigned command_help (char *command
, unsigned len
)
99 for (ctx
= command_list
.next
; ctx
!= &command_list
; ctx
= ctx
->next
) {
100 printf ("%s : %s\n", ctx
->name
, ctx
->desc
);
102 if (tty
->screen_y
>= (tty_char
.y
-1)) {
106 printf ("Press enter to continue");
111 /* wait, until enter key is pressed */
113 if (tty
== currtty
) {
114 if (getkey () != '\n')
128 unsigned command_hdd (char *command
, unsigned len
)
142 printf ("/dev/hda not installed\n");
153 printf ("/dev/hda installed - CHS=%d-%d:%d:%d\n", c
, c2
, h
, s
);
157 printf ("/dev/hdb not installed\n");
168 printf ("/dev/hdb installed - CHS=%d-%d:%d:%d\n", c
, c2
, h
, s
);
175 unsigned command_reboot (char *command
, unsigned len
)
184 unsigned command_halt (char *command
, unsigned len
)
186 arch_cpu_shutdown ();
188 kprintf ("\nSystem halted\nPlease press power button ..");
197 unsigned command_tasks (char *command
, unsigned len
)
199 printf ("id\tname\tpriority\n");
202 for (task
= task_list
.next
; task
!= &task_list
; task
= task
->next
)
203 printf ("%d\t%s\t%u\n", task
->id
, task
->name
, task
->priority
);
208 unsigned command_ps (char *command
, unsigned len
)
215 unsigned command_uptime (char *command
, unsigned len
)
222 unsigned command_version (char *command
, unsigned len
)
229 unsigned command_debug (char *command
, unsigned len
)
231 strcpy (cmd_buf
, argparse (command
));
233 unsigned l
= strlen (cmd_buf
);
236 debug
= atoi (cmd_buf
);
238 printf ("Developer mode was enabled with mask 0x%x.\n", debug
);
243 printf ("Developer mode was disabled.\n");
249 unsigned command_mount (char *command
, unsigned len
)
251 strcpy (cmd_buf
, argparse (command
));
255 unsigned l
= strlen (cmd_buf
);
258 if (cmd_buf
[x
] == ' ')
263 memcpy (devname
, cmd_buf
, x
);
265 strcpy (mountpoint
, argparse (cmd_buf
));
266 unsigned y
= strlen (mountpoint
);
268 if (mountpoint
[y
-1] != '/') {
270 mountpoint
[y
+1] = '\0';
274 partition_t
*p
= partition_find (devname
);
277 mount (p
, "", mountpoint
);
279 printf ("ERROR -> partition %s does not exists\n", devname
);
286 unsigned command_umount (char *command
, unsigned len
)
289 strcpy (mountpoint
, argparse (command
));
291 unsigned y
= strlen (mountpoint
);
294 if (mountpoint
[y
-1] != '/') {
296 mountpoint
[y
+1] = '\0';
300 partition_t
*partition
;
301 for (partition
= partition_list
.next
; partition
!= &partition_list
; partition
= partition
->next
) {
302 if (umount (partition
, mountpoint
))
307 printf ("ERROR -> directory %s is not mounted\n", mountpoint
);
310 printf ("WARNING -> multiple (%d) umount of %s\n", x
, mountpoint
);
317 unsigned command_env (char *command
, unsigned len
)
324 unsigned command_cd (char *command
, unsigned len
)
326 strcpy (cmd_buf
, argparse (command
));
333 unsigned command_ls (char *command
, unsigned len
)
335 strcpy (cmd_buf
, argparse (command
));
342 unsigned command_cat (char *command
, unsigned len
)
344 strcpy (cmd_buf
, argparse (command
));
351 unsigned command_cp (char *command
, unsigned len
)
353 strcpy (cmd_buf
, argparse (command
));
355 unsigned l
= strlen (cmd_buf
);
359 if (cmd_buf
[l
] == ' ') {
368 printf ("Syntax:\ncp <source> <destination>\n");
372 cp (cmd_buf
, cmd_buf
+p
+1);
378 unsigned command_rm (char *command
, unsigned len
)
380 strcpy (cmd_buf
, argparse (command
));
387 unsigned command_mkdir (char *command
, unsigned len
)
389 strcpy (cmd_buf
, argparse (command
));
396 unsigned command_touch (char *command
, unsigned len
)
398 strcpy (cmd_buf
, argparse (command
));
405 unsigned command_exec (char *command
, unsigned len
)
407 if (!strncmp ("./", command
, 2))
408 strcpy (cmd_buf
, command
+2);
410 strcpy (cmd_buf
, argparse (command
));
412 unsigned arg_len
= strlen (argparse (cmd_buf
));
414 strcpy (arg
, argparse (cmd_buf
));
417 unsigned cmd_buf_len
= strlen (cmd_buf
);
419 tty_t
*tty
= currtty
;
424 /* lock current tty keyboard */
427 /* if there is argument after name, rewrite space character to zero */
428 if (cmd_buf
[cmd_buf_len
-(arg_len
+1)] == ' ')
429 cmd_buf
[cmd_buf_len
-(arg_len
+1)] = '\0';
431 cmd_buf
[cmd_buf_len
-arg_len
] = '\0';
433 vfs_content_t content
;
434 /* read file data from filesystem */
435 int ret
= vfs_read (cmd_buf
, strlen (cmd_buf
), &content
);
438 printf ("%s: command not found\n", cmd_buf
);
439 DPRINT (DBG_CMDS
, "ERROR -> vfs_read () - '%s'\n", cmd_buf
);
444 unsigned bin_len
= content
.len
;
446 unsigned char *bin
= (unsigned char *) swmalloc (bin_len
);
449 printf ("ERROR -> out of memory\n");
455 /* copy image to page aligned memory */
456 memcpy (bin
, content
.ptr
, bin_len
);
460 cache_closebyptr (content
.ptr
);
462 unsigned entry
, code
, data
, data_off
, bss
;
463 int err
= exec_elf (bin
, &entry
, &data
, &data_off
, &bss
);
466 printf ("ERROR -> invalid ELF exec\n");
468 /* free program image */
469 swfree (bin
, bin_len
);
476 proc_t
*proc
= proc_create (tty
, cmd_buf
, entry
);
478 /* check for & character as app parameter */
480 while (d
< arg_len
) {
482 if (proc_flag_set (proc
, PROC_FLAG_DAEMON
))
483 printf ("%s: started as daemon\n", cmd_buf
);
487 if (arg_len
> 2 && arg
[d
-1] == ' ') {
500 printf ("ERROR -> Invalid process: %s\n", cmd_buf
);
502 /* free program image */
503 swfree (bin
, bin_len
);
509 /* save process information to structure for later use */
510 proc
->start
= (unsigned) bin
;
513 proc
->data_off
= data_off
;
515 proc
->end
= (unsigned) (bin
+ bin_len
);
517 /* well, set argument for out app */
518 proc_arg_set (proc
, arg
, arg_len
);
520 proc_vmem_map (proc
);
522 /* Is this process started as daemon ? */
523 /* wait here, until pid != 0*/
524 if (!(proc
->flags
& PROC_FLAG_DAEMON
))
528 /* enable interrupts */
531 /* unlock tty console - user can write in */
534 _curr_task
= proc
->tty
->task
; // this is pretty needed for correct return to our tty task
536 if (!(proc
->flags
& PROC_FLAG_DAEMON
)) {
537 if (!proc_done (proc
))
538 for (;;) // NOTE: heh, this is good stuff :P
541 /* free program image */
542 swfree (bin
, bin_len
);
545 /* switch to next task */
551 unsigned command_lsdev (char *command
, unsigned len
)
558 unsigned command_login (char *command
, unsigned len
)
560 currtty
->user
= NULL
;
561 currtty
->logged
= false;
569 unsigned command_serialw (char *command
, unsigned len
)
571 dev_t
*dev
= dev_find ("/dev/com0");
574 unsigned len
= strlen (argparse (command
));
576 memcpy (data
, argparse (command
), len
);
579 dev
->handler (DEV_ACT_WRITE
, data
, len
);
581 printf ("serialw: %s\n", data
);
587 unsigned command_serialr (char *command
, unsigned len
)
589 dev_t
*dev
= dev_find ("/dev/com0");
594 dev
->handler (DEV_ACT_READ
, data
, 1);
596 printf ("serialr: %s\n", data
);
602 unsigned command_fdisk (char *command
, unsigned len
)
604 char *parm
= argparse (command
);
606 fdisk (parm
, strlen (parm
));
611 unsigned command_hdcat (char *command
, unsigned len
)
615 dev_t
*dev
= (dev_t
*) dev_find (argparse (command
));
621 switch (dev
->devname
[7]) {
641 unsigned char block
[512];
643 printf ("dev: %s\n", dev
->devname
);
645 while (!key_pressed (1)) {
646 if (key_pressed (72) == 1) {
647 printf ("##block: %d\n", d
);
648 dev
->handler (DEV_ACT_READ
, &p
, block
, "", d
);
650 for (c
= 0; c
< 512; c
++)
651 printf ("%c", (unsigned) block
[c
]);
658 printf ("Please specify block device, example: hdcat /dev/hda\n");
660 tty_unlock (currtty
);
665 unsigned command_free (char *command
, unsigned len
)
672 unsigned command_top (char *command
, unsigned len
)
678 tty_unlock (currtty
);
683 unsigned command_date (char *command
, unsigned len
)
685 tm
*t
= rtc_getcurrtime ();
687 printf ("%02u:%02u:%02u, %u.%u.%u\n",
688 t
->tm_hour
, t
->tm_min
, t
->tm_sec
, t
->tm_mday
, t
->tm_mon
, t
->tm_year
);
728 void play_tone (dev_t
*dev
, char tone
, char type
, char time
)
750 printf ("play_tone (.., %c, %d, %d);\n", tone
, t
, tt
);
754 dev
->handler (DEV_ACT_PLAY
, 131*t
);
757 dev
->handler (DEV_ACT_PLAY
, 147*t
);
760 dev
->handler (DEV_ACT_PLAY
, 165*t
);
763 dev
->handler (DEV_ACT_PLAY
, 175*t
);
766 dev
->handler (DEV_ACT_PLAY
, 196*t
);
769 dev
->handler (DEV_ACT_PLAY
, 220*t
);
772 dev
->handler (DEV_ACT_PLAY
, 247*t
);
775 dev
->handler (DEV_ACT_PLAY
, 136*t
);
778 dev
->handler (DEV_ACT_PLAY
, 188*t
);
796 printf ("end: play_tone ();\n");
798 dev
->handler (DEV_ACT_PLAY
, 0);
801 void play_song (dev_t
*dev
, char *data
)
803 printf ("play_song ();\n");
804 unsigned i
= 0, l
= strlen (data
);
808 printf ("switch ()\n");
829 play_tone (dev
, data
[i
], data
[i
+1], data
[i
+2]);
839 unsigned command_spk (char *command
, unsigned len
)
841 unsigned int freq
= 20;
843 dev_t
*dev
= dev_find ("/dev/pcspk");
847 We wish you a merry christmas v1.0
849 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)
852 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");
857 We wish you a merry christmas v3.0
859 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)
862 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");
868 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)
870 /* 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");*/
878 extern bool bus_pci_acthandler (unsigned act
, char *block
, unsigned block_len
);
879 extern netif_t netif_list
;
880 extern void ext2_dump ();
887 unsigned long timer_start
= timer_ticks
+ ((unsigned) 1 * 1000);
889 while (timer_start
> timer_ticks
)
894 unsigned command_test (char *command
, unsigned len
)
896 partition_t
*p
= partition_find ("/dev/hda0");
899 mount (p
, "", "/mnt/hdd/");
908 unsigned command_test2 (char *command
, unsigned len
)
910 partition_t
*p
= partition_find ("/dev/cdc0");
913 mount (p
, "", "/mnt/cdrom/");
919 /* tty_t *tty = tty_find ("tty0");
924 int r = tty_read (tty, str, 512);
927 printf ("str: r: %d\n", r);
929 for (i = 0; i < r; i ++)
934 /* int ret = vfs_read ("wav", 3);
941 cache_t *cache = cache_read ();
943 unsigned char *p = (unsigned char *) &cache->data;
945 unsigned bin_len = cache->limit;
947 unsigned char *bin = (unsigned char *) 0x400000 - 44;
949 memcpy (bin, p, bin_len);
958 unsigned subchunk1id;
959 unsigned subchunk1size;
960 unsigned short audioformat;
961 unsigned short numchannels;
964 unsigned short blockalign;
965 unsigned short bitspersample;
969 unsigned subchunk2id;
970 unsigned subchunk2size;
973 riff_head_t *r = (riff_head_t *) bin;
974 fmt_head_t *fmt = (fmt_head_t *) ((char *) bin + sizeof (riff_head_t));
975 data_head_t *data = (data_head_t *) ((char *) bin + sizeof (riff_head_t) + sizeof (fmt_head_t));
977 printf ("riff_head_t 0x%x : %d : 0x%x\n", r->chunkid, r->chunksize, r->format);
978 printf ("fmt_head_t %d : %d : %d : %d : %d : %d\n", fmt->audioformat, fmt->subchunk1size, fmt->numchannels, fmt->samplerate, fmt->byterate, fmt->bitspersample);
979 printf ("data_head_t 0x%x : %d\n", data->subchunk2id, data->subchunk2size);
980 //memset (bin+44, 0, bin_len-44);
981 //dev->handler (DEV_ACT_WRITE, bin+44, bin_len-44);
984 cfg.dev = "/dev/ac97";
985 cfg.rate = fmt->samplerate;
986 cfg.format = SOUND_FORMAT_S16;
987 cfg.channels = fmt->numchannels;
989 snd_audio_t *aud = audio_open (&cfg);
994 audio_write (aud, bin+44, (bin_len-44));
996 //swfree (bin, bin_len);*/
1003 unsigned command_test3 (char *command
, unsigned len
)
1005 netif_t
*netif
= netif_findbyname ("eth0");
1006 dhcp_config_if(netif
);
1011 unsigned command_test4 (char *command
, unsigned len
)
1018 unsigned command_mouse (char *command
, unsigned len
)
1020 printf ("PS/2 Mouse test\n");
1022 dev_t
*dev
= dev_find ("/dev/mouseps2");
1029 int ps2_fd
= open ("/dev/mouseps2", O_RDONLY
);
1036 while (!key_pressed (1)) {
1037 int r
= read (ps2_fd
, &mouse
, sizeof (dev_mouse_t
));
1040 kprintf ("oj oj\n");
1044 kprintf ("ps2mouse: x,y: %d, %d | 0x%x \n", mouse
.pos_x
, mouse
.pos_y
, mouse
.flags
);
1046 lseek (ps2_fd
, 0, SEEK_SET
);
1054 unsigned command_vesa (char *command
, unsigned len
)
1056 if (!init_video_vesa ())
1057 printf ("vesa -> failed\n");
1062 unsigned command_kill (char *command
, unsigned len
)
1064 strcpy (cmd_buf
, argparse (currtty
->shell
));
1066 pid_t pid
= (pid_t
) atoi (cmd_buf
);
1068 proc_t
*proc
= proc_findbypid (pid
);
1071 printf ("kill: (%s) - No such process\n", cmd_buf
);
1075 /* send SIGTERM to selected process */
1076 if (!proc_signal (proc
, SIGTERM
))
1079 /* check for daemon */
1080 if (proc
->flags
& PROC_FLAG_DAEMON
)
1081 if (!proc_done (proc
))
1087 unsigned command_modprobe (char *command
, unsigned len
)
1089 unsigned arg_len
= strlen (argparse (currtty
->shell
));
1090 char arg
[arg_len
+1];
1091 strcpy (arg
, argparse (currtty
->shell
));
1092 arg
[arg_len
] = '\0';
1099 unsigned command_lsmod (char *command
, unsigned len
)
1106 #ifdef CONFIG_DRV_PCI
1107 unsigned command_lspci (char *command
, unsigned len
)
1116 unsigned command_iflist (char *command
, unsigned len
)
1123 unsigned command_ifconfig (char *command
, unsigned len
)
1125 strcpy (cmd_buf
, argparse (command
));
1129 unsigned l
= strlen (cmd_buf
);
1132 if (cmd_buf
[x
] == ' ')
1137 memcpy (ifid
, cmd_buf
, x
);
1140 strcpy (ip
, argparse (cmd_buf
));
1141 unsigned y
= strlen (ip
);
1143 netif_t
*netif
= netif_findbyname (ifid
);
1146 printf ("ifconfig -> bad network interface name, example: eth0\n");
1152 if (ipv4
= net_proto_ip_convert (ip
)) {
1153 netif_ip_addr (netif
, ipv4
, IF_CFG_TYPE_STATIC
);
1155 printf ("%s: IPv4 ", ifid
);
1156 net_proto_ip_print (ipv4
);
1158 } else if (net_proto_ipv6_convert (ipv6
, ip
)) {
1159 netif_ipv6_addr (netif
, ipv6
, IF_CFG_TYPE_STATIC
);
1161 printf ("%s: IPv6 ", ifid
);
1162 net_proto_ipv6_print (ipv6
);
1165 printf ("ifconfig -> bad ip address format, example:\n\t\t192.168.1.1 for IPv4\n\t\tfc00:0:0:0:0:0:0:10 for IPv6\n");
1172 unsigned command_ifroute (char *command
, unsigned len
)
1174 strcpy (cmd_buf
, argparse (command
));
1178 unsigned l
= strlen (cmd_buf
);
1181 if (cmd_buf
[x
] == ' ')
1186 memcpy (ifid
, cmd_buf
, x
);
1189 strcpy (ip
, argparse (cmd_buf
));
1190 unsigned y
= strlen (ip
);
1192 netif_t
*netif
= netif_findbyname (ifid
);
1195 printf ("ifroute -> bad network interface name, example: eth0\n");
1220 printf ("ifroute -> bad ip address format, example: 192.168.1.254\n");
1229 printf ("%s->gw: %d.%d.%d.%d - OK\n", ifid
, a
, b
, c
, d
);
1231 return netif_gw_addr (netif
, NET_IPV4_TO_ADDR (a
, b
, c
, d
));
1234 unsigned command_dnsconfig (char *command
, unsigned len
)
1236 strcpy (cmd_buf
, argparse (currtty
->shell
));
1241 unsigned l
= strlen (cmd_buf
);
1244 printf ("dnsconfig -> your domain name server is: ");
1245 net_proto_ip_print (dns_addr_get ());
1251 printf ("dnsconfig: %s - OK\n", cmd_buf
);
1253 net_ipv4 dns
= net_proto_ip_convert (cmd_buf
);
1263 unsigned command_dhcpcl (char *command
, unsigned len
)
1265 strcpy (cmd_buf
, argparse (currtty
->shell
));
1270 unsigned l
= strlen (cmd_buf
);
1273 printf ("dhcpcl -> please specify interface\n");
1277 netif_t
*netif
= netif_findbyname (cmd_buf
);
1280 printf ("dhcpcl -> bad network interface name, example: eth0\n");
1284 unsigned ret
= dhcp_config_if (netif
);
1288 case DHCP_OUT_OF_MEMORY
:
1289 printf ("dhcpcl -> out of memory\n");
1291 case DHCP_SOCKET_FAILED
:
1292 case DHCP_CONNECT_FAILED
:
1293 case DHCP_CANT_SEND_REQUEST
:
1294 printf ("dhcpcl -> problem with network layer\n");
1296 case DHCP_CANT_RECV_RESPONSE
:
1297 printf ("dhcpcl -> can't receive response\n");
1299 case DHCP_BAD_PACKET
:
1300 printf ("dhcpcl -> bad packet received\n");
1302 case DHCP_SRV_DIDNT_UNDERSTAND
:
1303 printf ("dhcpcl -> server didn't understand\n");
1307 printf ("dhcpcl -> interface %s successfully configured\n", cmd_buf
);
1312 unsigned command_tunconfig (char *command
, unsigned len
)
1314 strcpy (cmd_buf
, argparse (currtty
->shell
));
1319 unsigned l
= strlen (cmd_buf
);
1322 printf ("tunconfig -> wrong syntax !\ntunconfig <ipv4tunnel> for enable or tunconfig 0 for disable\n");
1327 /* you can disable this service over character '0' as parameter */
1328 if (l
== 1 && cmd_buf
[0] == '0') {
1331 printf ("tunconfig: disabled - OK\n");
1336 /* classical ipv4 address from tunnel server */
1337 net_ipv4 tunnel
= net_proto_ip_convert (cmd_buf
);
1340 printf ("tunconfig -> wrong syntax !\nExample: tunconfig 216.66.80.30\n");
1347 printf ("tunconfig: ::%s - OK\n", cmd_buf
);
1353 unsigned command_hostname (char *command
, unsigned len
)
1355 strcpy (cmd_buf
, argparse (currtty
->shell
));
1360 unsigned l
= strlen (cmd_buf
);
1363 printf ("%s\n", hostname_get ());
1368 printf ("hostname: %s - OK\n", cmd_buf
);
1370 hostname_set (cmd_buf
);
1375 unsigned command_kbdmap (char *command
, unsigned len
)
1377 strcpy (cmd_buf
, argparse (currtty
->shell
));
1382 unsigned ret
= keyboard_setlayout (cmd_buf
);
1385 printf ("Keyboard layout was changed to '%s'\n", cmd_buf
);
1390 unsigned command_mkzexfs (char *command
, unsigned len
)
1393 strcpy (cmd_buf
, argparse (currtty
->shell
));
1395 partition_t
*p
= partition_find (cmd_buf
);
1405 unsigned command_mkext2 (char *command
, unsigned len
)
1408 strcpy (cmd_buf
, argparse (currtty
->shell
));
1413 partition_t
*p
= partition_find ("/dev/hda0");
1420 unsigned command_ping (char *command
, unsigned len
)
1423 strcpy (cmd_buf
, argparse (currtty
->shell
));
1428 netif_t
*netif
= netif_findbyname ("eth0");
1431 printf ("ping -> network interface does not exists\n");
1442 unsigned y
= strlen (cmd_buf
);
1445 printf ("ping -> wrong syntax, please specify address\n");
1452 if (cmd_buf
[i
] == '.') {
1461 net_ipv4 target
= 0;
1464 strcpy (cmd_buf
, argparse (currtty
->shell
));
1466 if (dns_cache_get (cmd_buf
, &target
, 4) != 1)
1467 if (dns_send_request (cmd_buf
, &target
, 4) != 1) {
1468 printf ("ping -> bad hostname or ip address format, example: 192.168.1.1\n");
1473 b
= atoi (cmd_buf
+h
[0]);
1474 c
= atoi (cmd_buf
+h
[1]);
1475 d
= atoi (cmd_buf
+h
[2]);
1477 target
= NET_IPV4_TO_ADDR (a
, b
, c
, d
);
1479 strcpy (cmd_buf
, argparse (currtty
->shell
));
1483 net_proto_ip_convert2 (target
, address
);
1488 unsigned long time_a
= timer_ticks
;
1489 unsigned ret
= net_proto_icmp_ping (netif
, target
);
1491 printf ("ping -> %s (%s) - %ums\n", cmd_buf
, address
, (timer_ticks
- time_a
));
1493 printf ("ping -> Host %s is unreachable\n", cmd_buf
);
1503 unsigned command_ping6 (char *command
, unsigned len
)
1505 strcpy (cmd_buf
, argparse (currtty
->shell
));
1510 netif_t
*netif
= netif_findbyname ("eth0");
1513 printf ("ping6 -> network interface does not exists\n");
1528 unsigned y
= strlen (cmd_buf
);
1531 printf ("ping6 -> wrong syntax, please specify ipv6 address\n");
1538 if (cmd_buf
[i
] == ':') {
1550 strcpy (cmd_buf
, argparse (currtty
->shell
));
1552 if (dns_cache_get (cmd_buf
, &target
, 16) != 1)
1553 if (dns_send_request (cmd_buf
, &target
, 16) != 1) {
1554 printf ("ping6 -> bad hostname or ip address format, example: 2001:4d3:8fe:2:0:0:0:1\n");
1560 a
= strtol (cmd_buf
, &endptr
, 16);
1561 b
= strtol (cmd_buf
+k
[0], &endptr
, 16);
1562 c
= strtol (cmd_buf
+k
[1], &endptr
, 16);
1563 d
= strtol (cmd_buf
+k
[2], &endptr
, 16);
1564 e
= strtol (cmd_buf
+k
[3], &endptr
, 16);
1565 f
= strtol (cmd_buf
+k
[4], &endptr
, 16);
1566 g
= strtol (cmd_buf
+k
[5], &endptr
, 16);
1567 h
= strtol (cmd_buf
+k
[6], &endptr
, 16);
1569 NET_IPV6_TO_ADDR (target
, a
, b
, c
, d
, e
, f
, g
, h
);
1571 //printf ("ipv6: %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n", a, b, c, d, e, f, g, h);
1573 strcpy (cmd_buf
, argparse (currtty
->shell
));
1577 //net_proto_ipv6_convert2 (target, address);
1582 unsigned long time_a
= timer_ticks
;
1583 unsigned ret
= net_proto_icmp6_ping (netif
, target
);
1585 printf ("ping6 -> %s (%s) - %ums\n", cmd_buf
, cmd_buf
, (timer_ticks
- time_a
));
1587 printf ("ping6 -> Host %s is unreachable\n", cmd_buf
);
1597 unsigned command_netexec (char *command
, unsigned len
)
1599 strcpy (cmd_buf
, argparse (command
));
1604 if (!strlen (cmd_buf
)) {
1605 printf ("Syntax:\nnetexec <ip_of_tftp> <filename> [parameters]\n");
1609 unsigned arg_len
= strlen (argparse (cmd_buf
));
1610 char arg
[arg_len
+1];
1611 strcpy (arg
, argparse (cmd_buf
));
1612 arg
[arg_len
] = '\0';
1614 unsigned cmd_buf_len
= strlen (cmd_buf
);
1615 cmd_buf
[cmd_buf_len
-arg_len
] = '\0';
1617 if (cmd_buf
[cmd_buf_len
-arg_len
-1] == ' ') {
1618 cmd_buf
[cmd_buf_len
-arg_len
-1] = '\0';
1622 char file
[arg_len
+1];
1623 unsigned file_len
= 0;
1624 memcpy (file
, arg
, arg_len
);
1625 while (file_len
< arg_len
) {
1626 if (file
[file_len
] == ' ') {
1627 file
[file_len
] = '\0';
1633 if (file
[arg_len
] != '\0')
1634 file
[arg_len
] = '\0';
1636 unsigned l
= file_len
;
1638 unsigned ret
= tftp_client_connect (cmd_buf
, 69, file
, &l
);
1641 printf ("tftp -> something go wrong !\n");
1645 printf ("tftp -> file length: %ubytes\t\t[OK]\n", l
);
1647 char *bin
= (unsigned char *) swmalloc (sizeof (char) * (l
+1));
1652 int ret2
= tftp_client_read (ret
, bin
, l
);
1655 printf ("tftp -> tftp_client_read () error\n");
1660 unsigned entry
, code
, data
, data_off
, bss
;
1661 int err
= exec_elf (bin
, &entry
, &data
, &data_off
, &bss
);
1664 printf ("ERROR -> invalid ELF exec\n");
1670 /* create process */
1671 proc_t
*proc
= proc_create (currtty
, "netexec", entry
);
1675 /* check for & character as app parameter */
1677 while (d < arg_len) {
1678 if (arg[d] == '&') {
1679 if (proc_flag_set (proc, PROC_FLAG_DAEMON))
1680 printf ("%s: started as daemon\n", cmd_buf);
1684 if (arg_len > 2 && arg[d-1] == ' ') {
1697 printf ("ERROR -> Invalid process: %s\n", cmd_buf
);
1703 /* save process information to structure for later use */
1704 proc
->start
= (unsigned) bin
;
1707 proc
->data_off
= data_off
;
1709 proc
->end
= (unsigned) (bin
+ l
);
1713 /* well, set argument for out app */
1714 proc_arg_set (proc
, arg
+file_len
+1, arg_len
-file_len
-1);
1716 proc_vmem_map (proc
);
1720 /* Is this process started as daemon ? */
1721 /* wait here, until pid != 0*/
1722 if (!(proc
->flags
& PROC_FLAG_DAEMON
))
1728 /* enable interrupts */
1731 /* unlock tty console - user can write in */
1732 tty_unlock (proc
->tty
);
1734 _curr_task
= proc
->tty
->task
; // this is pretty needed for correct return to our tty task
1736 if (!(proc
->flags
& PROC_FLAG_DAEMON
)) {
1737 if (!proc_done (proc
))
1738 while (1) // NOTE: heh, this is good stuff :P
1741 /* free image of app */
1745 /* switch to next task */
1753 unsigned command_netcp (char *command
, unsigned len
)
1755 strcpy (cmd_buf
, argparse (command
));
1760 if (!strlen (cmd_buf
)) {
1761 printf ("Syntax:\nnetcp <ip_of_tftp> <tftp_file> <output_file>\n");
1765 unsigned arg_len
= strlen (argparse (cmd_buf
));
1766 char arg
[arg_len
+1];
1767 strcpy (arg
, argparse (cmd_buf
));
1768 arg
[arg_len
] = '\0';
1770 unsigned cmd_buf_len
= strlen (cmd_buf
);
1771 cmd_buf
[cmd_buf_len
-arg_len
] = '\0';
1773 if (cmd_buf
[cmd_buf_len
-arg_len
-1] == ' ') {
1774 cmd_buf
[cmd_buf_len
-arg_len
-1] = '\0';
1778 char file
[arg_len
+1];
1779 unsigned file_len
= 0;
1780 memcpy (file
, arg
, arg_len
);
1781 while (file_len
< arg_len
) {
1782 if (file
[file_len
] == ' ') {
1783 file
[file_len
] = '\0';
1789 if (file
[arg_len
] != '\0')
1790 file
[arg_len
] = '\0';
1792 unsigned l
= file_len
;
1794 unsigned ret
= tftp_client_connect (cmd_buf
, 69, file
, &l
);
1797 printf ("tftp -> something go wrong !\n");
1801 printf ("tftp -> file length: %ubytes\t\t[OK]\n", l
);
1803 char *bin
= (unsigned char *) swmalloc (sizeof (char) * (l
+1));
1808 int ret2
= tftp_client_read (ret
, bin
, l
);
1811 printf ("tftp -> tftp_client_read () error\n");
1818 char *output
= argparse (arg
);
1822 printf ("ERROR -> output file name is empty\n");
1826 int fd
= open (output
, O_WRONLY
| O_CREAT
);
1830 printf ("ERROR -> something go wrong with %s\n", output
);
1834 ret
= write (fd
, bin
, l
);
1838 printf ("ERROR -> File '%s' could'nt be writed\n", output
);
1844 extern bool ide_acthandler (unsigned act
, partition_t
*p
, char *block
, char *more
, int n
);
1845 unsigned command_znfscl (char *command
, unsigned len
)
1848 strcpy (cmd_buf
, argparse (command
));
1850 if (!strlen (cmd_buf
)) {
1851 printf ("Syntax:\nznfscl <ip_of_znfs>\n");
1855 dev_t
*dev
= dev_find ("/dev/vbd");
1858 printf ("znfscl: You are already connected to znfs server\n");
1862 dev
= (dev_t
*) dev_register ("vbd", "Virtual Block Device", DEV_ATTR_BLOCK
, (dev_handler_t
*) &ide_acthandler
);
1867 partition_t
*p
= partition_add (dev
, fs_supported ("znfs"), 0);
1872 /* connect to specified address */
1873 if (znfs_init (p
, cmd_buf
) != 1) {
1874 printf ("znfs: Wrong znfs server\n");
1880 sprintf (str
, "/mnt/%s", p
->name
+5);
1886 printf ("znfs: You are succefully connected to ZNFS server '%s'\nZNFS was mounted into '%s'\n", cmd_buf
, str
);
1888 printf ("ERROR -> this architecture is not supported yet\n");
1893 void task_savemode ()
1895 /* disable cpu for cycle */
1896 for (;; schedule ())
1900 unsigned command_savemode (char *command
, unsigned len
)
1902 task_t
*task
= (task_t
*) task_create ("savemode", (unsigned) &task_savemode
, 16);
1905 printf ("ERROR -> Save mode thread was'nt created\n");
1909 printf ("Save mode was succefully started\n");
1914 unsigned command_cpuinfo (char *command
, unsigned len
)
1916 printf ("Processor info:\n");
1918 cat ("/proc/cpuinfo");
1923 unsigned command_adm (char *command
, unsigned len
)
1925 printf ("Automatic Device Mounter - init ()\n");
1932 unsigned command_ttyctrl (char *command
, unsigned len
)
1934 char *parm
= argparse (command
);
1936 if (!strlen (parm
)) {
1937 printf ("ttyctrl <operation> [parameter]\n\toperations: add; list; change <ttyid>\n");
1941 if (!strncmp (parm
, "add", 3)) {
1942 tty_t
*tty
= tty_create ();
1949 tty_write (tty
, "\nlogin: ", 8);
1951 printf ("New tty '%s' was created\n", tty
->name
);
1953 if (!strncmp (parm
, "list", 4)) {
1956 if (!strncmp (parm
, "change", 6)) {
1957 tty_t
*tty
= tty_find ((char *) argparse (parm
));
1970 unsigned command_cmd (char *command
, unsigned len
)
1975 if (!strlen (argparse (command
))) {
1976 printf ("cmd <command1>;<command2>;...;\n");
1980 char *cmds
= kmalloc (len
- 3);
1985 memcpy (cmds
, command
+ 4, len
- 4);
1986 cmds
[len
- 4] = '\0';
1989 for (i
= 1; i
< len
- 4; i
++) {
1990 if (cmds
[i
] == ';') {
1994 command_parser (cmds
+s
, e
-s
+1);
2005 /*************************************************************\
2007 \*************************************************************/
2009 unsigned command_parser (char *command
, unsigned len
)
2011 /* find first non-space in command */
2013 for (i
= 0; i
< len
; i
++) {
2014 if (command
[i
] != ' ')
2019 /* try to find the command */
2020 for (ctx
= command_list
.next
; ctx
!= &command_list
; ctx
= ctx
->next
) {
2021 if (!cstrcmp (ctx
->name
, command
+i
)) {
2022 ctx
->handler (command
+i
, len
-i
); /* execute context function */
2027 printf ("%s: command not found\n", command
);
2032 void commands (int i
)
2034 // currtty->shell_len has been set to zero already
2035 command_parser (currtty
->shell
, strlen(currtty
->shell
));
2038 unsigned command_register (char *name
, char *desc
, command_handler_t
*handler
, unsigned flags
)
2041 /* we dont want commands with same name */
2042 for (c
= command_list
.next
; c
!= &command_list
; c
= c
->next
)
2043 if (!strcmp(c
->name
, name
))
2048 // alloc and init context
2049 ctx
= (command_t
*) kmalloc (sizeof (command_t
));
2054 unsigned name_len
= strlen (name
);
2055 ctx
->name
= (char *) kmalloc (sizeof (char) * name_len
+1);
2060 memcpy (ctx
->name
, name
, name_len
);
2061 ctx
->name
[name_len
] = '\0';
2063 unsigned desc_len
= strlen (desc
);
2064 ctx
->desc
= (char *) kmalloc (sizeof (char) * desc_len
+1);
2069 memcpy (ctx
->desc
, desc
, desc_len
);
2070 ctx
->desc
[desc_len
] = '\0';
2072 ctx
->handler
= handler
;
2075 ctx
->next
= &command_list
;
2076 ctx
->prev
= command_list
.prev
;
2077 ctx
->prev
->next
= ctx
;
2078 ctx
->next
->prev
= ctx
;
2083 unsigned command_unregister (char *name
)
2087 for (ctx
= command_list
.next
; ctx
!= &command_list
; ctx
= ctx
->next
) {
2088 if (!strcmp(ctx
->name
, name
)) {
2089 ctx
->next
->prev
= ctx
->prev
;
2090 ctx
->prev
->next
= ctx
->next
;
2099 /*************************************************************\
2100 | INITIALIZATION OF COMMANDS |
2101 \*************************************************************/
2103 unsigned int init_commands ()
2105 command_list
.next
= &command_list
;
2106 command_list
.prev
= &command_list
;
2109 command_register ("help", "Displays list of available commands", &command_help
, 0);
2110 command_register ("hdd", "Detect HDD", &command_hdd
, 0);
2111 command_register ("reboot", "Reboot computer", &command_reboot
, 0);
2112 command_register ("halt", "Shutdown computer", &command_halt
, 0);
2113 command_register ("tasks", "Print all tasks", &command_tasks
, 0);
2114 command_register ("ps", "Print all process", &command_ps
, 0);
2115 command_register ("uptime", "Print uptime in seconds", &command_uptime
, 0);
2116 command_register ("version", "Displays a version of system", &command_version
, 0);
2117 command_register ("debug", "Change to developer mode", &command_debug
, 0);
2118 command_register ("mount", "Mount device to selected directory", &command_mount
, 0);
2119 command_register ("umount", "Unmount mounted directory", &command_umount
, 0);
2120 command_register ("env", "Displays all env variables", &command_env
, 0);
2121 command_register ("cd", "Change directory", &command_cd
, 0);
2122 command_register ("ls", " Displays files in current directory", &command_ls
, 0);
2123 command_register ("cat", "Displays text in selected file", &command_cat
, 0);
2124 command_register ("cp", "Copy a file", &command_cp
, 0);
2125 command_register ("rm", "Remove a file", &command_rm
, 0);
2126 command_register ("mkdir", "Create a directory", &command_mkdir
, 0);
2127 command_register ("touch", "Create a file", &command_touch
, 0);
2128 command_register ("exec", "Execute a selected program", &command_exec
, 0);
2129 command_register ("lsdev", "Displays found devices", &command_lsdev
, 0);
2130 command_register ("login", "Login as another user", &command_login
, 0);
2131 command_register ("fdisk", "Partition table manipulator", &command_fdisk
, 0);
2132 command_register ("hdcat", "Read selected block of data from drive", &command_hdcat
, 0);
2133 command_register ("free", "Display amount of free and used memory", &command_free
, 0);
2134 command_register ("top", "Stats of cpu usage", &command_top
, 0);
2135 command_register ("date", "Show current date and time", &command_date
, 0);
2136 //command_register ("xmastime", "play noels on pc-speaker", &command_spk, 0);
2137 command_register ("test", "Some test", &command_test
, 0);
2138 //command_register ("test2", "Some test", &command_test2, 0);
2139 //command_register ("test3", "Some test", &command_test3, 0);
2140 //command_register ("testd", "Some test", &command_test4, 0);
2141 //command_register ("vesa", "Some test", &command_vesa, 0);
2142 command_register ("kill", "Send a signal to process", &command_kill
, 0);
2143 command_register ("modprobe", "program to add modules from the kernel", &command_modprobe
, 0);
2144 command_register ("lsmod", "program to show the status of modules", &command_lsmod
, 0);
2145 #ifdef CONFIG_DRV_PCI
2146 command_register ("lspci", "list all pci devices", &command_lspci
, 0);
2148 command_register ("mouse", "ps2 mouse test", &command_mouse
, 0);
2149 command_register ("iflist", "Show network interface", &command_iflist
, 0);
2150 command_register ("ifconfig", "Configure network interface", &command_ifconfig
, 0);
2151 command_register ("ifroute", "Configure gateway address", &command_ifroute
, 0);
2152 command_register ("dnsconfig", "Configure domain name server address", &command_dnsconfig
, 0);
2153 command_register ("dhcpcl", "Configure interface using DHCP", &command_dhcpcl
, 0);
2154 #ifdef CONFIG_PROTO_TUN6
2155 command_register ("tunconfig", "Configure IPv6 tunnel server address", &command_tunconfig
, 0);
2157 command_register ("hostname", "Configure network hostname", &command_hostname
, 0);
2158 command_register ("kbdmap", "Change keyboard layout", &command_kbdmap
, 0);
2159 #ifdef CONFIG_DRV_ZEXFS
2160 command_register ("mkzexfs", "Create zexfs filesystem on hdd", &command_mkzexfs
, 0);
2162 #ifdef CONFIG_DRV_EXT2
2163 command_register ("mkext2", "Create ext2 filesystem on hdd", &command_mkext2
, 0);
2165 #ifdef CONFIG_PROTO_IPV4
2166 command_register ("ping", "Send ICMP echo request", &command_ping
, 0);
2168 #ifdef CONFIG_PROTO_IPV6
2169 command_register ("ping6", "Send ICMPv6 echo request", &command_ping6
, 0);
2171 command_register ("netexec", "Execute file from network", &command_netexec
, 0);
2172 command_register ("netcp", "Copy file from network to filesystem", &command_netcp
, 0);
2173 #ifdef CONFIG_DRV_ZNFS
2174 command_register ("znfscl", "Connect to znfs server - network filesystem", &command_znfscl
, 0);
2176 command_register ("savemode", "Make less cpu load", &command_savemode
, 0);
2177 command_register ("cpuinfo", "Show CPU info", &command_cpuinfo
, 0);
2179 command_register ("adm", "Automatic Device Mounter", &command_adm
, 0);
2180 command_register ("ttyctrl", "TTY Console control", &command_ttyctrl
, 0);
2181 command_register ("cmd", "Execute command sequence", &command_cmd
, 0);