Main Page | Directories | File List | Globals

agnix_console.c

Go to the documentation of this file.
00001 /*
00002  * kernel_sys/console.c
00003  *
00004  * Copyright (c) 2003-2005 Lukasz Dembinski <dembol@nasa.com.pl>
00005  * All Rights Reserved
00006  * 
00007  * Date:        2005/04
00008  * Author:      Lukasz Dembinski
00009  * Info:
00010  * Contact:     mailto: <dembol@nasa.com.pl>
00011  *
00012  */
00013 
00014 #include <agnix/agnix.h>
00015 #include <agnix/adi/adi.h>
00016 #include <agnix/net/net.h>
00017 #include <agnix/terminal.h>
00018 #include <agnix/threads.h>
00019 #include <agnix/sprintf.h>
00020 #include <agnix/memory.h>
00021 #include <agnix/machine.h>
00022 #include <agnix/sched.h>
00023 #include <agnix/console.h>
00024 #include <agnix/strings.h>
00025 #include <agnix/unistd.h>
00026 #include <agnix/agnix_console.h>
00027 
00028 #define MOD_NAME        "CONSOLE: "
00029 
00030 extern struct adi_netdev_s rtl_8029_netdev;
00031 
00032 struct agnix_console_menu_s * agnix_console_print_help_name(struct agnix_console_menu_s *menu, struct agnix_console_command_s *command)
00033 {
00034     struct agnix_console_command_s *commands = menu->commands;
00035     int i, j;
00036 
00037     for (i = 0; i < COMMAND_MAX_ENTRIES; i++) {
00038         if (commands[i].name == NULL)
00039             break;
00040         
00041         printf("%s", commands[i].name);
00042         for (j = strlen(commands[i].name); j < 10; j++)
00043             printf(" ");
00044         
00045         printf(" - %s\n", commands[i].desc);
00046     }
00047     
00048     return NULL;
00049 }
00050 
00051 struct agnix_console_menu_s * agnix_console_print_help(struct agnix_console_menu_s *menu, struct agnix_console_command_s *command, char *buf)
00052 {
00053     return agnix_console_print_help_name(menu, command);
00054 }
00055 
00056 struct agnix_console_menu_s * agnix_console_next_node(struct agnix_console_menu_s *menu, struct agnix_console_command_s *command, char *buf)
00057 {
00058     return command->submenu;
00059 }
00060 
00061 struct agnix_console_menu_s * agnix_console_prev_node(struct agnix_console_menu_s *menu, struct agnix_console_command_s *command, char *buf)
00062 {
00063     return menu->parent;
00064 }
00065 
00066 struct agnix_console_menu_s * agnix_console_ip_stats(struct agnix_console_menu_s *menu, struct agnix_console_command_s *command, char *buf)
00067 {
00068     ip_print_stats();    
00069     return NULL;
00070 }
00071 
00072 struct agnix_console_menu_s * agnix_console_ip_debug(struct agnix_console_menu_s *menu, struct agnix_console_command_s *command, char *buf)
00073 {
00074     int debug;
00075     
00076     debug = ip_ioctl_get_debug() ^ 1; 
00077     ip_ioctl_set_debug(debug);
00078     
00079     return NULL;
00080 }
00081 
00082 struct agnix_console_menu_s * agnix_console_arp_stats(struct agnix_console_menu_s *menu, struct agnix_console_command_s *command, char *buf)
00083 {
00084     arp_print_stats();    
00085     return NULL;
00086 }
00087 
00088 struct agnix_console_menu_s * agnix_console_arp_request(struct agnix_console_menu_s *menu, struct agnix_console_command_s *command, char *buf)
00089 {
00090     struct arp_neigh_s neigh;
00091     char *req_addr;
00092 
00093     req_addr = buf;
00094 
00095     if (req_addr == NULL) {
00096         printf("\nYou must specify IP address for ARP request\n");
00097         printf("Usage: request [IP]\n");
00098         return NULL;
00099     }
00100 
00101     if (net_addr(req_addr, &neigh.addr) < 0) {
00102         printf("\nInvalid format of interface address\n");
00103         return NULL;
00104     }
00105 
00106     neigh.addr.address_len = 4;
00107     memset(neigh.mac, 0, ETH_ALEN);
00108 
00109     printf("\nWho-has %d.%d.%d.%d\n", 
00110     neigh.addr.address[0], neigh.addr.address[1], neigh.addr.address[2], neigh.addr.address[3]);
00111 
00112     if (arp_resolve(&rtl_8029_netdev, &neigh) < 0) {
00113         printf("%d.%d.%d.%d unreachable\n", 
00114         neigh.addr.address[0], neigh.addr.address[1], neigh.addr.address[2], neigh.addr.address[3]);
00115     } else {
00116         printf("%d.%d.%d.%d is-at %02x:%02x:%02x:%02x:%02x:%02x\n",
00117         neigh.addr.address[0], neigh.addr.address[1], neigh.addr.address[2], neigh.addr.address[3],
00118         neigh.mac[0], neigh.mac[1], neigh.mac[2], neigh.mac[3], neigh.mac[4], neigh.mac[5]);
00119     }
00120     
00121     return NULL;
00122 }
00123 
00124 struct agnix_console_menu_s * agnix_console_pci_list(struct agnix_console_menu_s *menu, struct agnix_console_command_s *command, char *buf)
00125 {
00126     pcibus_print_all();    
00127     return NULL;
00128 }
00129 
00130 struct agnix_console_menu_s * agnix_console_print_memory(struct agnix_console_menu_s *menu, struct agnix_console_command_s *command, char *buf)
00131 {
00132     bootmem_print_memory();    
00133     return NULL;
00134 }
00135 
00136 struct agnix_console_menu_s * agnix_console_print_cpuinfo(struct agnix_console_menu_s *menu, struct agnix_console_command_s *command, char *buf)
00137 {
00138     cpu_print_cpuinfo();
00139     return NULL;
00140 }
00141 
00142 struct agnix_console_menu_s * agnix_console_halt(struct agnix_console_menu_s *menu, struct agnix_console_command_s *command, char *buf)
00143 {
00144     do_machine_halt();
00145     return NULL;
00146 }
00147 
00148 struct agnix_console_menu_s * agnix_console_powerdown(struct agnix_console_menu_s *menu, struct agnix_console_command_s *command, char *buf)
00149 {
00150     do_machine_powerdown();
00151     return NULL;
00152 }
00153 
00154 struct agnix_console_menu_s * agnix_console_reboot(struct agnix_console_menu_s *menu, struct agnix_console_command_s *command, char *buf)
00155 {
00156     do_machine_reboot();
00157     return NULL;
00158 }
00159 
00160 struct agnix_console_menu_s * agnix_console_ps(struct agnix_console_menu_s *menu, struct agnix_console_command_s *command, char *buf)
00161 {
00162     scheduler_print_tasks();
00163     return NULL;
00164 }
00165 
00166 struct agnix_console_menu_s * agnix_console_net_ifconfig(struct agnix_console_menu_s *menu, struct agnix_console_command_s *command, char *buf)
00167 {
00168     struct net_addr_s addr;
00169     struct adi_netdev_s *netdev;
00170     char *if_name;
00171     char *if_addr;
00172 
00173     if_name = buf;
00174     if_addr = word_next(buf, COMMAND_MAX_NAME_LEN);
00175 
00176     if ((if_name != NULL) && (if_addr == NULL)) {
00177         printf("\nYou must specify IP address for interface\n");
00178         printf("Usage: ifconfig [ifname addr]\n");
00179         return NULL;
00180     }
00181 
00182     if (if_name != NULL) {
00183         if ((netdev = adi_netdev_find_by_name(if_name)) == NULL) {
00184             printf("\n%s interface not found\n", if_name);
00185             return NULL;
00186         } 
00187     
00188         if (net_addr(if_addr, &addr) < 0) {
00189             printf("\nInvalid format of interface address\n");
00190             return NULL;
00191         }
00192 
00193         printf("\nSetting %s IP to %d.%d.%d.%d\n", netdev->if_name,
00194         addr.address[0], addr.address[1], addr.address[2], addr.address[3]);
00195         addr.address_len = 4;
00196         
00197         ip_ioctl_set_ifaddr(netdev, &addr);
00198     } else {
00199         adi_netdev_print_interfaces();
00200     }
00201      
00202     return NULL;
00203 }
00204 
00205 struct agnix_console_menu_s * agnix_console_vm_stats(struct agnix_console_menu_s *menu, struct agnix_console_command_s *command, char *buf)
00206 {
00207     buddy_alloc_print_stats();
00208     return NULL;
00209 }
00210 
00211 struct agnix_console_menu_s * agnix_console_vm_free(struct agnix_console_menu_s *menu, struct agnix_console_command_s *command, char *buf)
00212 {
00213     buddy_alloc_print_free();
00214     return NULL;
00215 }
00216 
00217 struct agnix_console_menu_s * agnix_console_command_exec(struct agnix_console_menu_s *menu, char *buf)
00218 {
00219     struct agnix_console_command_s *command = menu->commands;
00220     struct agnix_console_menu_s *next_menu;
00221     char *ptr;
00222     int i;
00223 
00224     for (i = 0; i < COMMAND_MAX_ENTRIES; i++) {
00225         if (command[i].name == NULL)
00226             break;
00227         
00228         if (!word_cmp(command[i].name, buf, COMMAND_MAX_NAME_LEN)) {
00229 
00230             ptr = word_next(buf, COMMAND_MAX_NAME_LEN);
00231             if (command[i].action != NULL) {
00232                 if ((next_menu = command[i].action(menu, &command[i], ptr)) == NULL)
00233                     return menu;
00234                 else
00235                     return next_menu;
00236                 } else {
00237                     printf("Command action is NULL\n");
00238                     return menu;
00239                 }
00240         }
00241     }
00242 
00243     printf("Unknown command, try 'help'\n");
00244     
00245     return menu;
00246 }
00247 
00248 struct agnix_console_menu_s console_main_menu;
00249 struct agnix_console_menu_s console_net_menu;
00250 struct agnix_console_menu_s console_ip_menu;
00251 struct agnix_console_menu_s console_arp_menu;
00252 struct agnix_console_menu_s console_adi_menu;
00253 struct agnix_console_menu_s console_pci_menu;
00254 struct agnix_console_menu_s console_arch_menu;
00255 struct agnix_console_menu_s console_vm_menu;
00256 
00257 struct agnix_console_command_s console_ip_commands[] = {
00258     { "help",   "Print this help",              agnix_console_print_help, &console_ip_menu, NULL },
00259     { "return", "Return to parent menu",        agnix_console_prev_node,  &console_ip_menu, NULL },
00260     { "stats",  "Statistics of IP protocol",    agnix_console_ip_stats,   &console_ip_menu, NULL },
00261     { "debug",  "Enable/disable packets debug", agnix_console_ip_debug,   &console_ip_menu, NULL },
00262     { NULL }
00263 };
00264 
00265 struct agnix_console_command_s console_arp_commands[] = {
00266     { "help",   "Print this help",            agnix_console_print_help, &console_arp_menu, NULL },
00267     { "return", "Return to parent menu",      agnix_console_prev_node,  &console_arp_menu, NULL },
00268     { "request","Send ARP Request",           agnix_console_arp_request,  &console_arp_menu, NULL },
00269     { "stats",  "Statistics of ARP protocol", agnix_console_arp_stats,  &console_arp_menu, NULL },
00270     { NULL }
00271 };
00272 
00273 struct agnix_console_command_s console_net_commands[] = {
00274     { "help",   "Print this help",       agnix_console_print_help, &console_net_menu, NULL },
00275     { "ip",     "IP protocol module",    agnix_console_next_node,  &console_net_menu, &console_ip_menu  },
00276     { "arp",    "ARP protocol module",   agnix_console_next_node,  &console_net_menu, &console_arp_menu  },
00277     { "ifconfig", "ifconfig emulator",   agnix_console_net_ifconfig, &console_net_menu, NULL  },
00278     { "return", "Return to parent menu", agnix_console_prev_node,  &console_net_menu, NULL },
00279     { NULL }
00280 };
00281 
00282 struct agnix_console_command_s console_adi_commands[] = {
00283     { "help",   "Print this help",       agnix_console_print_help, &console_adi_menu, NULL },
00284     { "pci",    "PCI Bus module",        agnix_console_next_node,  &console_adi_menu, &console_pci_menu  },
00285     { "return", "Return to parent menu", agnix_console_prev_node,  &console_adi_menu, NULL },
00286     { NULL }
00287 };
00288 
00289 struct agnix_console_command_s console_pci_commands[] = {
00290     { "help",   "Print this help",            agnix_console_print_help, &console_pci_menu, NULL },
00291     { "return", "Return to parent menu",      agnix_console_prev_node,  &console_pci_menu, NULL },
00292     { "list",   "List all PCI devices",       agnix_console_pci_list,   &console_pci_menu, NULL },
00293     { NULL }
00294 };
00295 
00296 struct agnix_console_command_s console_arch_commands[] = {
00297     { "cpuinfo", "Print cpu info",        agnix_console_print_cpuinfo, &console_adi_menu, NULL },
00298     { "help",    "Print this help",       agnix_console_print_help,    &console_adi_menu, NULL },
00299     { "memmap",  "Print memory map",      agnix_console_print_memory,  &console_adi_menu, NULL },
00300     { "return",  "Return to parent menu", agnix_console_prev_node,     &console_adi_menu, NULL },
00301     { NULL }
00302 };
00303 
00304 struct agnix_console_command_s console_vm_commands[] = {
00305     { "free",     "VM free",               agnix_console_vm_free,    &console_vm_menu, NULL  },
00306     { "help",     "Print this help",       agnix_console_print_help, &console_vm_menu, NULL },
00307     { "stats",    "VM statistics",         agnix_console_vm_stats,   &console_vm_menu, NULL  },
00308     { "return",   "Return to parent menu", agnix_console_prev_node,  &console_vm_menu, NULL },
00309     { NULL }
00310 };
00311 
00312 struct agnix_console_command_s console_main_commands[] = {
00313     { "adi",       "Driver Interface Subsystem",   agnix_console_next_node,  &console_main_menu,  &console_adi_menu },
00314     { "arch",      "Architecture dependent subsystem",
00315                                                    agnix_console_next_node,  &console_main_menu,  &console_arch_menu },
00316     { "halt",      "Halt machine",                 agnix_console_halt,       &console_main_menu,  NULL },
00317     { "help",      "Print this help",              agnix_console_print_help, &console_main_menu,  NULL },
00318     { "net",       "Network subsystem",            agnix_console_next_node,  &console_main_menu,  &console_net_menu },
00319     { "powerdown", "Powerdown machine",            agnix_console_powerdown,  &console_main_menu,  NULL },
00320     { "ps",        "Task list",                    agnix_console_ps,         &console_main_menu,  NULL },
00321     { "reboot",    "Reboot machine",               agnix_console_reboot,     &console_main_menu,  NULL },
00322     { "vm",        "Virtual memory subsystem",     agnix_console_next_node,  &console_main_menu,  &console_vm_menu },
00323     { NULL }
00324 };
00325 
00326 struct agnix_console_menu_s console_main_menu = { "main", console_main_commands, NULL };
00327 struct agnix_console_menu_s console_adi_menu  = { "adi",  console_adi_commands,  &console_main_menu };
00328 struct agnix_console_menu_s console_pci_menu  = { "pci",  console_pci_commands,  &console_adi_menu };
00329 struct agnix_console_menu_s console_net_menu  = { "net",  console_net_commands,  &console_main_menu };
00330 struct agnix_console_menu_s console_ip_menu   = { "ip",   console_ip_commands,   &console_net_menu };
00331 struct agnix_console_menu_s console_arp_menu  = { "arp",  console_arp_commands,  &console_net_menu };
00332 struct agnix_console_menu_s console_arch_menu = { "arch", console_arch_commands, &console_main_menu };
00333 struct agnix_console_menu_s console_vm_menu   = { "vm",   console_vm_commands,   &console_main_menu };
00334 
00335 extern struct terminal_s term_sys[MAX_TERMINALS];
00336 extern int term_cur;
00337 
00338 void agnix_console_main(void)
00339 {
00340     struct agnix_console_menu_s *cur_menu = &console_main_menu;
00341     char buf[COMMAND_MAX_LEN + 1];
00342     int ret;
00343 
00344     for (;;) {
00345 
00346         printf("agnix %s> ", cur_menu->name); 
00347 
00348         memset(buf, 0, COMMAND_MAX_LEN + 1);
00349         if ((ret = read(0, buf, COMMAND_MAX_LEN)) > 0) {
00350             cur_menu = agnix_console_command_exec(cur_menu, buf);
00351             printf("\n");
00352         }
00353     }
00354 }
00355 
00356 void agnix_console_thread(void *data)
00357 {
00358     char console_name[10];
00359     int console_nr;
00360 
00361     console_nr = (int)data;
00362 
00363     sprintf(console_name, "tty%d", console_nr);
00364     if ((open(console_name, 0, 0)) < 0) {
00365         printf("can not open terminal\n");
00366         for(;;);
00367     }
00368 
00369     printf(MOD_NAME "Agnix Console %s starting ... ok\n\n", console_name, console_nr);
00370 
00371     agnix_console_main();
00372 }
00373 
00374 int agnix_console_start(void)
00375 {
00376     int i;
00377     
00378     for (i = 0; i < MAX_TERMINALS; i++) {    
00379         create_kernel_thread("console", agnix_console_thread, (void *)i);
00380     }
00381     
00382     return 0;
00383 }
Dokumentacje wygenerowano programem Doxygen 1.4.2 dla projektu Agnix