⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dev_c6sup1.c

📁 思科路由器仿真器,用来仿7200系列得,可以在电脑上模拟路由器
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * Cisco router simulation platform. * Copyright (c) 2007 Christophe Fillot (cf@utc.fr) * * Generic C6k-SUP1 routines and definitions (EEPROM,...). * * This is not a working platform! I only added it to play. */#include <stdio.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#include <sys/types.h>#include <assert.h>#include "cpu.h"#include "vm.h"#include "dynamips.h"#include "memory.h"#include "device.h"#include "pci_io.h"#include "dev_gt.h"#include "cisco_eeprom.h"#include "dev_rom.h"#include "dev_am79c971.h"#include "dev_i8254x.h"#include "dev_c6sup1.h"#include "dev_c6sup1_mpfpga.h"#include "dev_vtty.h"#include "registry.h"#include "net.h"/* ====================================================================== *//* EOBC - Ethernet Out of Band Channel                                    *//* ====================================================================== */static int dev_c6sup1_eobc_init(vm_instance_t *vm,struct cisco_card *card){   struct am79c971_data *data;   /* Create the AMD 79c970 chip */   data = dev_am79c971_init(vm,card->dev_name,AM79C971_TYPE_100BASE_TX,                            vm->pci_bus[0],6,                            3/*c6sup1_net_irq_for_slot_port(0,0)*/);   if (!data) return(-1);   /* Store device info into the router structure */   card->drv_info = data;   return(0);}/* Remove EOBC */static int dev_c6sup1_eobc_shutdown(vm_instance_t *vm,struct cisco_card *card){   struct am79c971_data *data = card->drv_info;   dev_am79c971_remove(data);   return(0);}/* Bind a Network IO descriptor */static int dev_c6sup1_eobc_set_nio(vm_instance_t *vm,struct cisco_card *card,                                   u_int port_id,netio_desc_t *nio){   struct am79c971_data *d = card->drv_info;   if (!d || (port_id != 0))      return(-1);   return(dev_am79c971_set_nio(d,nio));}/* Unbind a Network IO descriptor */static int dev_c6sup1_eobc_unset_nio(vm_instance_t *vm,                                     struct cisco_card *card,                                     u_int port_id){   struct am79c971_data *d = card->drv_info;      if (!d || (port_id != 0))      return(-1);      dev_am79c971_unset_nio(d);   return(0);}/* EOBC driver */struct cisco_card_driver dev_c6sup1_eobc = {   "C6SUP1_EOBC", 0, 0,   dev_c6sup1_eobc_init,   dev_c6sup1_eobc_shutdown,   NULL,   dev_c6sup1_eobc_set_nio,   dev_c6sup1_eobc_unset_nio,   NULL,};/* ====================================================================== *//* IBC - InBand Channel                                                   *//* ====================================================================== */static int dev_c6sup1_ibc_init(vm_instance_t *vm,struct cisco_card *card){   struct i8254x_data *data;   /* Create the Intel Wiseman/Livengood chip */   data = dev_i8254x_init(vm,card->dev_name,0,vm->pci_bus[0],7,                          2/*c6sup1_net_irq_for_slot_port(1,0)*/);   if (!data) return(-1);   /* Store device info into the router structure */   card->drv_info = data;   return(0);}/* Remove EOBC */static int dev_c6sup1_ibc_shutdown(vm_instance_t *vm,struct cisco_card *card){   struct i8254x_data *data = card->drv_info;   dev_i8254x_remove(data);   return(0);}/* Bind a Network IO descriptor */static int dev_c6sup1_ibc_set_nio(vm_instance_t *vm,struct cisco_card *card,                                   u_int port_id,netio_desc_t *nio){   struct i8254x_data *d = card->drv_info;   if (!d || (port_id != 0))      return(-1);   return(dev_i8254x_set_nio(d,nio));}/* Unbind a Network IO descriptor */static int dev_c6sup1_ibc_unset_nio(vm_instance_t *vm,                                     struct cisco_card *card,                                     u_int port_id){   struct i8254x_data *d = card->drv_info;   if (!d || (port_id != 0))      return(-1);   dev_i8254x_unset_nio(d);   return(0);}/* IBC driver */struct cisco_card_driver dev_c6sup1_ibc = {   "C6SUP1_IBC", 0, 0,   dev_c6sup1_ibc_init,   dev_c6sup1_ibc_shutdown,   NULL,   dev_c6sup1_ibc_set_nio,   dev_c6sup1_ibc_unset_nio,   NULL,};/* ======================================================================== *//* Port Adapter Drivers                                                     *//* ======================================================================== */static struct cisco_card_driver *pa_drivers[] = {   &dev_c6sup1_eobc,   &dev_c6sup1_ibc,   NULL,};/* ======================================================================== *//* C6SUP1 router instances                                                  *//* ======================================================================== *//* Initialize default parameters for a SUP1 */static void c6sup1_init_defaults(c6sup1_t *router);/* Directly extract the configuration from the NVRAM device */static ssize_t c6sup1_nvram_extract_config(vm_instance_t *vm,u_char **buffer){      u_char *base_ptr,*ios_ptr,*cfg_ptr,*end_ptr;   m_uint32_t start,end,nvlen,clen;   m_uint16_t magic1,magic2;    struct vdevice *nvram_dev;   m_uint64_t nvram_addr;   off_t nvram_size;   int fd;   if ((nvram_dev = dev_get_by_name(vm,"nvram")))      dev_sync(nvram_dev);   fd = vm_mmap_open_file(vm,"nvram",&base_ptr,&nvram_size);   if (fd == -1)      return(-1);   nvram_addr = C6SUP1_NVRAM_ADDR;   ios_ptr = base_ptr + vm->nvram_rom_space;   end_ptr = base_ptr + nvram_size;   if ((ios_ptr + 0x30) >= end_ptr) {      vm_error(vm,"NVRAM file too small\n");      return(-1);   }   magic1  = ntohs(*PTR_ADJUST(m_uint16_t *,ios_ptr,0x06));   magic2  = ntohs(*PTR_ADJUST(m_uint16_t *,ios_ptr,0x08));   if ((magic1 != 0xF0A5) || (magic2 != 0xABCD)) {      vm_error(vm,"unable to find IOS magic numbers (0x%x,0x%x)!\n",               magic1,magic2);      return(-1);   }   start = ntohl(*PTR_ADJUST(m_uint32_t *,ios_ptr,0x10)) + 1;   end   = ntohl(*PTR_ADJUST(m_uint32_t *,ios_ptr,0x14));   nvlen = ntohl(*PTR_ADJUST(m_uint32_t *,ios_ptr,0x18));   clen  = end - start;   if ((clen + 1) != nvlen) {      vm_error(vm,"invalid configuration size (0x%x)\n",nvlen);      return(-1);   }   if (!(*buffer = malloc(clen+1))) {      vm_error(vm,"unable to allocate config buffer (%u bytes)\n",clen);      return(-1);   }   cfg_ptr = base_ptr + (start - nvram_addr);   if ((start < nvram_addr) || ((cfg_ptr + clen) > end_ptr)) {      vm_error(vm,"NVRAM file too small\n");      return(-1);   }   memcpy(*buffer,cfg_ptr,clen);   (*buffer)[clen] = 0;   return(clen);}/* Directly push the IOS configuration to the NVRAM device */static int c6sup1_nvram_push_config(vm_instance_t *vm,                                    u_char *buffer,size_t len){     u_char *base_ptr,*ios_ptr,*cfg_ptr;   m_uint32_t cfg_addr,cfg_offset;   m_uint32_t nvram_addr,cklen;   m_uint16_t cksum;   int fd;   fd = vm_mmap_create_file(vm,"nvram",vm->nvram_size*1024,&base_ptr);   if (fd == -1)      return(-1);   cfg_offset = 0x2c;   ios_ptr = base_ptr + vm->nvram_rom_space;   cfg_ptr = ios_ptr  + cfg_offset;   nvram_addr = C6SUP1_NVRAM_ADDR;   cfg_addr = nvram_addr + vm->nvram_rom_space + cfg_offset;   /* Write IOS tag, uncompressed config... */   *PTR_ADJUST(m_uint16_t *,ios_ptr,0x06) = htons(0xF0A5);   *PTR_ADJUST(m_uint16_t *,ios_ptr,0x08) = htons(0xABCD);   *PTR_ADJUST(m_uint16_t *,ios_ptr,0x0a) = htons(0x0001);   *PTR_ADJUST(m_uint16_t *,ios_ptr,0x0c) = htons(0x0000);   *PTR_ADJUST(m_uint16_t *,ios_ptr,0x0e) = htons(0x0000);   /* Store file contents to NVRAM */   memcpy(cfg_ptr,buffer,len);   /* Write config addresses + size */   *PTR_ADJUST(m_uint32_t *,ios_ptr,0x10) = htonl(cfg_addr);   *PTR_ADJUST(m_uint32_t *,ios_ptr,0x14) = htonl(cfg_addr + len);   *PTR_ADJUST(m_uint32_t *,ios_ptr,0x18) = htonl(len);   /* Compute the checksum */   cklen = (vm->nvram_size*1024) - (vm->nvram_rom_space + 0x08);   cksum = nvram_cksum((m_uint16_t *)(ios_ptr+0x08),cklen);   *PTR_ADJUST(m_uint16_t *,ios_ptr,0x0c) = htons(cksum);   vm_mmap_close_file(fd,base_ptr,vm->nvram_size*1024);   return(0);}/* Get slot/port corresponding to specified network IRQ */static inline void c6sup1_net_irq_get_slot_port(u_int irq,u_int *slot,u_int *port){   *slot = irq - C6SUP1_NETIO_IRQ_BASE;   *port = 0;}/* Get network IRQ for specified slot/port */u_int c6sup1_net_irq_for_slot_port(u_int slot,u_int port){   u_int irq;      irq = C6SUP1_NETIO_IRQ_BASE + slot;   return(irq);}/* Free specific hardware resources used by a SUP1 */static void c6sup1_free_hw_ressources(c6sup1_t *router){   /* Shutdown all Port Adapters */   vm_slot_shutdown_all(router->vm);}/* Create a new router instance */static int c6sup1_create_instance(vm_instance_t *vm){   c6sup1_t *router;   if (!(router = malloc(sizeof(*router)))) {      fprintf(stderr,"C6SUP1 '%s': Unable to create new instance!\n",vm->name);      return(-1);   }   memset(router,0,sizeof(*router));   router->vm = vm;   vm->hw_data = router;   vm->elf_machine_id = C6SUP1_ELF_MACHINE_ID;   c6sup1_init_defaults(router);   return(0);}/* Free resources used by a router instance */static int c6sup1_delete_instance(vm_instance_t *vm){   c6sup1_t *router = VM_C6SUP1(vm);   int i;   /* Stop all CPUs */   if (vm->cpu_group != NULL) {      vm_stop(vm);               if (cpu_group_sync_state(vm->cpu_group) == -1) {         vm_error(vm,"unable to sync with system CPUs.\n");         return(FALSE);      }   }   /* Remove NIO bindings */   for(i=0;i<C6SUP1_MAX_PA_BAYS;i++)      vm_slot_remove_all_nio_bindings(vm,i);   /* Free specific HW resources */   c6sup1_free_hw_ressources(router);   /* Free all resources used by VM */   vm_free(vm);   /* Free the router structure */   free(router);   return(TRUE);}/* Create the main PCI bus for a GT64010 based system */static int c6sup1_init_gt64010(c6sup1_t *router){      vm_instance_t *vm = router->vm;   if (!(vm->pci_bus[0] = pci_bus_create("PCI Bus 0",0))) {      vm_error(vm,"unable to create PCI data.\n");      return(-1);   }      return(dev_gt64010_init(vm,"gt64010",C6SUP1_GT64K_ADDR,0x1000,                           C6SUP1_GT64K_IRQ));}/* Initialize a SUP1 board */static int c6sup1_init_hw(c6sup1_t *router){   vm_instance_t *vm = router->vm;   /* Set the processor type: R5000 */   mips64_set_prid(CPU_MIPS64(vm->boot_cpu),MIPS_PRID_R5000);   /* Initialize the Galileo GT-64010 PCI controller */   if (c6sup1_init_gt64010(router) == -1)      return(-1);   /* Create PCI bus 1 */   vm->pci_bus_pool[24] = pci_bus_create("PCI Bus 1",-1);   dev_dec21154_init(vm->pci_bus[0],1,vm->pci_bus_pool[24]);   /* PCI IO space */   if (!(vm->pci_io_space = pci_io_data_init(vm,C6SUP1_PCI_IO_ADDR)))      return(-1);   /* AGRAM ? */   dev_ram_init(vm,"agram",FALSE,FALSE,NULL,FALSE,0x1EA00000,0x40000);   /* Cirrus Logic PD6729 (PCI-to-PCMCIA host adapter) */   dev_clpd6729_init(vm,vm->pci_bus[0],5,vm->pci_io_space,0x402,0x403);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -