| 1 |
/* |
| 2 |
* Cisco router simulation platform. |
| 3 |
* Copyright (c) 2006 Christophe Fillot ([email protected]) |
| 4 |
* Patched by Jeremy Grossmann for the GNS3 project (www.gns3.net) |
| 5 |
* |
| 6 |
* Generic Cisco 1700 routines and definitions (EEPROM,...). |
| 7 |
*/ |
| 8 |
|
| 9 |
#include <stdio.h> |
| 10 |
#include <stdlib.h> |
| 11 |
#include <string.h> |
| 12 |
#include <unistd.h> |
| 13 |
#include <sys/types.h> |
| 14 |
#include <assert.h> |
| 15 |
|
| 16 |
#include "cpu.h" |
| 17 |
#include "vm.h" |
| 18 |
#include "dynamips.h" |
| 19 |
#include "memory.h" |
| 20 |
#include "device.h" |
| 21 |
#include "ppc32_mem.h" |
| 22 |
#include "pci_io.h" |
| 23 |
#include "cisco_eeprom.h" |
| 24 |
#include "dev_mpc860.h" |
| 25 |
#include "dev_rom.h" |
| 26 |
#include "dev_c1700.h" |
| 27 |
#include "dev_c1700_iofpga.h" |
| 28 |
#include "dev_vtty.h" |
| 29 |
#include "registry.h" |
| 30 |
#include "fs_nvram.h" |
| 31 |
|
| 32 |
/* ======================================================================== */ |
| 33 |
/* EEPROM definitions */ |
| 34 |
/* ======================================================================== */ |
| 35 |
|
| 36 |
/* Cisco 1700 mainboard EEPROM */ |
| 37 |
static m_uint16_t eeprom_c1700_mb_data[] = { |
| 38 |
0x0101, 0x0404, 0x0000, 0x0000, 0x4320, 0x00FF, 0x00B2, 0x0020, |
| 39 |
0x0000, 0x0000, 0x0000, 0x0000, 0x4654, 0x5809, 0x4557, 0x304D, |
| 40 |
0x5902, 0x0200, 0x0000, 0x0000, 0x00FF, 0xFFFF, 0x5006, 0x490B, |
| 41 |
0x1709, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, |
| 42 |
0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, |
| 43 |
0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, |
| 44 |
0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, |
| 45 |
0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, |
| 46 |
}; |
| 47 |
|
| 48 |
struct c1700_mb_id { |
| 49 |
char *name; |
| 50 |
char *mb_driver; |
| 51 |
m_uint16_t id; |
| 52 |
int supported; |
| 53 |
}; |
| 54 |
|
| 55 |
struct c1700_mb_id c1700_mainboard_id[] = { |
| 56 |
{ "1710" , "C1710-MB-1FE-1E" , 0x02F6, TRUE }, |
| 57 |
{ "1720" , "C1700-MB-1ETH" , 0x00B2, TRUE }, |
| 58 |
{ "1721" , "C1700-MB-1ETH" , 0x035A, TRUE }, |
| 59 |
{ "1750" , "C1700-MB-1ETH" , 0x00C9, TRUE }, |
| 60 |
{ "1751" , "C1700-MB-1ETH" , 0x024D, TRUE }, |
| 61 |
{ "1760" , "C1700-MB-1ETH" , 0x0316, TRUE }, |
| 62 |
{ NULL , NULL , 0x0000, 0 }, |
| 63 |
}; |
| 64 |
|
| 65 |
/* ======================================================================== */ |
| 66 |
/* Network Module Drivers */ |
| 67 |
/* ======================================================================== */ |
| 68 |
static struct cisco_card_driver *nm_drivers[] = { |
| 69 |
&dev_c1700_mb_eth_driver, |
| 70 |
&dev_c1710_mb_eth_driver, |
| 71 |
NULL, |
| 72 |
}; |
| 73 |
|
| 74 |
/* ======================================================================== */ |
| 75 |
/* Cisco 1700 router instances */ |
| 76 |
/* ======================================================================== */ |
| 77 |
|
| 78 |
/* Initialize default parameters for a C1700 */ |
| 79 |
static void c1700_init_defaults(c1700_t *router); |
| 80 |
|
| 81 |
/* Directly extract the configuration from the NVRAM device */ |
| 82 |
static int c1700_nvram_extract_config(vm_instance_t *vm,u_char **startup_config,size_t *startup_len,u_char **private_config,size_t *private_len) |
| 83 |
{ |
| 84 |
int ret; |
| 85 |
|
| 86 |
ret = generic_nvram_extract_config(vm, "nvram", vm->nvram_rom_space, 0, 0, FS_NVRAM_FORMAT_DEFAULT, startup_config, startup_len, private_config, private_len); |
| 87 |
|
| 88 |
return(ret); |
| 89 |
} |
| 90 |
|
| 91 |
/* Directly push the IOS configuration to the NVRAM device */ |
| 92 |
static int c1700_nvram_push_config(vm_instance_t *vm,u_char *startup_config,size_t startup_len,u_char *private_config,size_t private_len) |
| 93 |
{ |
| 94 |
int ret; |
| 95 |
|
| 96 |
ret = generic_nvram_push_config(vm, "nvram", vm->nvram_size*1024, vm->nvram_rom_space, 0, 0, FS_NVRAM_FORMAT_DEFAULT, startup_config, startup_len, private_config, private_len); |
| 97 |
|
| 98 |
return(ret); |
| 99 |
} |
| 100 |
|
| 101 |
/* Create a new router instance */ |
| 102 |
static int c1700_create_instance(vm_instance_t *vm) |
| 103 |
{ |
| 104 |
c1700_t *router; |
| 105 |
|
| 106 |
if (!(router = malloc(sizeof(*router)))) { |
| 107 |
fprintf(stderr,"C1700 '%s': Unable to create new instance!\n",vm->name); |
| 108 |
return(-1); |
| 109 |
} |
| 110 |
|
| 111 |
memset(router,0,sizeof(*router)); |
| 112 |
router->vm = vm; |
| 113 |
vm->hw_data = router; |
| 114 |
|
| 115 |
c1700_init_defaults(router); |
| 116 |
return(0); |
| 117 |
} |
| 118 |
|
| 119 |
/* Free resources used by a router instance */ |
| 120 |
static int c1700_delete_instance(vm_instance_t *vm) |
| 121 |
{ |
| 122 |
c1700_t *router = VM_C1700(vm); |
| 123 |
int i; |
| 124 |
|
| 125 |
/* Stop all CPUs */ |
| 126 |
if (vm->cpu_group != NULL) { |
| 127 |
vm_stop(vm); |
| 128 |
|
| 129 |
if (cpu_group_sync_state(vm->cpu_group) == -1) { |
| 130 |
vm_error(vm,"unable to sync with system CPUs.\n"); |
| 131 |
return(FALSE); |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
/* Remove NIO bindings */ |
| 136 |
for(i=0;i<vm->nr_slots;i++) |
| 137 |
vm_slot_remove_all_nio_bindings(vm,i); |
| 138 |
|
| 139 |
/* Shutdown all Network Modules */ |
| 140 |
vm_slot_shutdown_all(vm); |
| 141 |
|
| 142 |
/* Free mainboard EEPROM */ |
| 143 |
cisco_eeprom_free(&router->mb_eeprom); |
| 144 |
|
| 145 |
/* Free all resources used by VM */ |
| 146 |
vm_free(vm); |
| 147 |
|
| 148 |
/* Free the router structure */ |
| 149 |
free(router); |
| 150 |
return(TRUE); |
| 151 |
} |
| 152 |
|
| 153 |
/* Save configuration of a C1700 instance */ |
| 154 |
void c1700_save_config(vm_instance_t *vm,FILE *fd) |
| 155 |
{ |
| 156 |
c1700_t *router = VM_C1700(vm); |
| 157 |
|
| 158 |
fprintf(fd,"c1700 set_chassis %s %s\n\n",vm->name,router->mainboard_type); |
| 159 |
} |
| 160 |
|
| 161 |
/* Get WIC device address for the specified onboard port */ |
| 162 |
int c1700_get_onboard_wic_addr(u_int slot,m_uint64_t *phys_addr) |
| 163 |
{ |
| 164 |
if (slot >= C1700_MAX_WIC_BAYS) |
| 165 |
return(-1); |
| 166 |
|
| 167 |
*phys_addr = C1700_WIC_ADDR + (slot * C1700_WIC_SIZE); |
| 168 |
return(0); |
| 169 |
} |
| 170 |
|
| 171 |
/* Set EEPROM for the specified slot */ |
| 172 |
int c1700_set_slot_eeprom(c1700_t *router,u_int slot, |
| 173 |
struct cisco_eeprom *eeprom) |
| 174 |
{ |
| 175 |
switch(slot) { |
| 176 |
case 1: |
| 177 |
router->nm_eeprom_group.eeprom[0] = eeprom; |
| 178 |
return(0); |
| 179 |
default: |
| 180 |
return(-1); |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
/* Get slot/port corresponding to specified network IRQ */ |
| 185 |
static inline void |
| 186 |
c1700_net_irq_get_slot_port(u_int irq,u_int *slot,u_int *port) |
| 187 |
{ |
| 188 |
irq -= C1700_NETIO_IRQ_BASE; |
| 189 |
*port = irq & C1700_NETIO_IRQ_PORT_MASK; |
| 190 |
*slot = irq >> C1700_NETIO_IRQ_PORT_BITS; |
| 191 |
} |
| 192 |
|
| 193 |
/* Get network IRQ for specified slot/port */ |
| 194 |
u_int c1700_net_irq_for_slot_port(u_int slot,u_int port) |
| 195 |
{ |
| 196 |
u_int irq; |
| 197 |
|
| 198 |
irq = (slot << C1700_NETIO_IRQ_PORT_BITS) + port; |
| 199 |
irq += C1700_NETIO_IRQ_BASE; |
| 200 |
|
| 201 |
return(irq); |
| 202 |
} |
| 203 |
|
| 204 |
/* Find Cisco 1700 Mainboard info */ |
| 205 |
static struct c1700_mb_id *c1700_get_mb_info(char *mainboard_type) |
| 206 |
{ |
| 207 |
int i; |
| 208 |
|
| 209 |
for(i=0;c1700_mainboard_id[i].name;i++) |
| 210 |
if (!strcmp(c1700_mainboard_id[i].name,mainboard_type)) |
| 211 |
return(&c1700_mainboard_id[i]); |
| 212 |
|
| 213 |
return NULL; |
| 214 |
} |
| 215 |
|
| 216 |
/* Show all available mainboards */ |
| 217 |
static void c1700_mainboard_show_drivers(void) |
| 218 |
{ |
| 219 |
int i; |
| 220 |
|
| 221 |
printf("Available C1700 chassis drivers:\n"); |
| 222 |
|
| 223 |
for(i=0;c1700_mainboard_id[i].name;i++) |
| 224 |
printf(" * %s %s\n", |
| 225 |
c1700_mainboard_id[i].name, |
| 226 |
!c1700_mainboard_id[i].supported ? "(NOT WORKING)" : ""); |
| 227 |
|
| 228 |
printf("\n"); |
| 229 |
} |
| 230 |
|
| 231 |
/* Set the base MAC address of the chassis */ |
| 232 |
static int c1700_burn_mac_addr(c1700_t *router,n_eth_addr_t *addr) |
| 233 |
{ |
| 234 |
int i; |
| 235 |
|
| 236 |
for(i=0;i<3;i++) { |
| 237 |
router->vm->chassis_cookie[i+1] = addr->eth_addr_byte[i*2] << 8; |
| 238 |
router->vm->chassis_cookie[i+1] |= addr->eth_addr_byte[(i*2)+1]; |
| 239 |
} |
| 240 |
|
| 241 |
return(0); |
| 242 |
} |
| 243 |
|
| 244 |
/* Set mainboard type */ |
| 245 |
int c1700_mainboard_set_type(c1700_t *router,char *mainboard_type) |
| 246 |
{ |
| 247 |
struct c1700_mb_id *mb_info; |
| 248 |
|
| 249 |
if (router->vm->status == VM_STATUS_RUNNING) { |
| 250 |
vm_error(router->vm,"unable to change mainboard type when online.\n"); |
| 251 |
return(-1); |
| 252 |
} |
| 253 |
|
| 254 |
if (!(mb_info = c1700_get_mb_info(mainboard_type))) { |
| 255 |
vm_error(router->vm,"unknown mainboard '%s'\n",mainboard_type); |
| 256 |
return(-1); |
| 257 |
} |
| 258 |
|
| 259 |
router->mainboard_type = mainboard_type; |
| 260 |
|
| 261 |
/* Set the cookie */ |
| 262 |
memcpy(router->vm->chassis_cookie, |
| 263 |
eeprom_c1700_mb_data,sizeof(eeprom_c1700_mb_data)); |
| 264 |
|
| 265 |
router->vm->chassis_cookie[6] = mb_info->id; |
| 266 |
|
| 267 |
/* Set the chassis base MAC address */ |
| 268 |
c1700_burn_mac_addr(router,&router->mac_addr); |
| 269 |
|
| 270 |
/* Set the mainboard driver */ |
| 271 |
if (vm_slot_active(router->vm,0,0)) |
| 272 |
vm_slot_remove_binding(router->vm,0,0); |
| 273 |
|
| 274 |
vm_slot_add_binding(router->vm,mb_info->mb_driver,0,0); |
| 275 |
c1700_refresh_systemid(router); |
| 276 |
|
| 277 |
return(0); |
| 278 |
} |
| 279 |
|
| 280 |
/* Set the system id or processor board id in the eeprom */ |
| 281 |
int c1700_set_system_id(c1700_t *router,char *id) |
| 282 |
{ |
| 283 |
/* 11 characters is enough. Array is 20 long */ |
| 284 |
strncpy(router->board_id,id,13); |
| 285 |
/* Make sure it is null terminated */ |
| 286 |
router->board_id[13] = 0x00; |
| 287 |
c1700_refresh_systemid(router); |
| 288 |
return 0; |
| 289 |
} |
| 290 |
int c1700_refresh_systemid(c1700_t *router) |
| 291 |
{ |
| 292 |
if (router->board_id[0] == 0x00) return(0); |
| 293 |
m_uint8_t buf[11]; |
| 294 |
parse_board_id(buf,router->board_id,9); |
| 295 |
// Does not use the cisco_eeprom libraries.. do it by hand |
| 296 |
// cisco_eeprom_set_region(&router->mb_eeprom ,24,buf,9); |
| 297 |
int i; |
| 298 |
for(i=0;i<4;i++) { |
| 299 |
router->vm->chassis_cookie[i+12] = buf[i*2] << 8; |
| 300 |
router->vm->chassis_cookie[i+12] |= buf[(i*2)+1]; |
| 301 |
} |
| 302 |
router->vm->chassis_cookie[i+12] &= 0x00ff; |
| 303 |
router->vm->chassis_cookie[i+12] |= buf[(i*2)] << 8; |
| 304 |
|
| 305 |
return (0); |
| 306 |
} |
| 307 |
|
| 308 |
/* Set chassis MAC address */ |
| 309 |
int c1700_chassis_set_mac_addr(c1700_t *router,char *mac_addr) |
| 310 |
{ |
| 311 |
if (parse_mac_addr(&router->mac_addr,mac_addr) == -1) { |
| 312 |
vm_error(router->vm,"unable to parse MAC address '%s'.\n",mac_addr); |
| 313 |
return(-1); |
| 314 |
} |
| 315 |
|
| 316 |
/* Set the chassis base MAC address */ |
| 317 |
c1700_burn_mac_addr(router,&router->mac_addr); |
| 318 |
return(0); |
| 319 |
} |
| 320 |
|
| 321 |
/* Initialize a Cisco 1700 */ |
| 322 |
static int c1700_init(c1700_t *router) |
| 323 |
{ |
| 324 |
vm_instance_t *vm = router->vm; |
| 325 |
|
| 326 |
/* Create the PCI bus */ |
| 327 |
if (!(vm->pci_bus[0] = pci_bus_create("PCI0",0))) { |
| 328 |
vm_error(vm,"unable to create PCI data.\n"); |
| 329 |
return(-1); |
| 330 |
} |
| 331 |
|
| 332 |
/* Bind PCI bus to slots 0 and 1 */ |
| 333 |
vm->slots_pci_bus[0] = vm->pci_bus[0]; |
| 334 |
vm->slots_pci_bus[1] = vm->pci_bus[0]; |
| 335 |
|
| 336 |
vm->elf_machine_id = C1700_ELF_MACHINE_ID; |
| 337 |
return(0); |
| 338 |
} |
| 339 |
|
| 340 |
/* Show C1700 hardware info */ |
| 341 |
void c1700_show_hardware(c1700_t *router) |
| 342 |
{ |
| 343 |
vm_instance_t *vm = router->vm; |
| 344 |
|
| 345 |
printf("C1700 instance '%s' (id %d):\n",vm->name,vm->instance_id); |
| 346 |
|
| 347 |
printf(" VM Status : %d\n",vm->status); |
| 348 |
printf(" RAM size : %u Mb\n",vm->ram_size); |
| 349 |
printf(" NVRAM size : %u Kb\n",vm->nvram_size); |
| 350 |
printf(" IOS image : %s\n\n",vm->ios_image); |
| 351 |
|
| 352 |
if (vm->debug_level > 0) { |
| 353 |
dev_show_list(vm); |
| 354 |
pci_dev_show_list(vm->pci_bus[0]); |
| 355 |
pci_dev_show_list(vm->pci_bus[1]); |
| 356 |
printf("\n"); |
| 357 |
} |
| 358 |
} |
| 359 |
|
| 360 |
/* Initialize default parameters for a C1700 */ |
| 361 |
static void c1700_init_defaults(c1700_t *router) |
| 362 |
{ |
| 363 |
vm_instance_t *vm = router->vm; |
| 364 |
n_eth_addr_t *m; |
| 365 |
m_uint16_t pid; |
| 366 |
|
| 367 |
/* Set platform slots characteristics */ |
| 368 |
vm->nr_slots = C1700_MAX_NM_BAYS; |
| 369 |
vm->slots_type = CISCO_CARD_TYPE_NM; |
| 370 |
vm->slots_drivers = nm_drivers; |
| 371 |
|
| 372 |
pid = (m_uint16_t)getpid(); |
| 373 |
|
| 374 |
/* Generate a chassis MAC address based on the instance ID */ |
| 375 |
m = &router->mac_addr; |
| 376 |
m->eth_addr_byte[0] = vm_get_mac_addr_msb(vm); |
| 377 |
m->eth_addr_byte[1] = vm->instance_id & 0xFF; |
| 378 |
m->eth_addr_byte[2] = pid >> 8; |
| 379 |
m->eth_addr_byte[3] = pid & 0xFF; |
| 380 |
m->eth_addr_byte[4] = 0x00; |
| 381 |
m->eth_addr_byte[5] = 0x00; |
| 382 |
|
| 383 |
router->board_id[0] = 0x00; |
| 384 |
|
| 385 |
c1700_init_eeprom_groups(router); |
| 386 |
c1700_mainboard_set_type(router,C1700_DEFAULT_MAINBOARD); |
| 387 |
c1700_burn_mac_addr(router,&router->mac_addr); |
| 388 |
|
| 389 |
vm->ram_mmap = C1700_DEFAULT_RAM_MMAP; |
| 390 |
vm->ram_size = C1700_DEFAULT_RAM_SIZE; |
| 391 |
vm->rom_size = C1700_DEFAULT_ROM_SIZE; |
| 392 |
vm->nvram_size = C1700_DEFAULT_NVRAM_SIZE; |
| 393 |
vm->conf_reg_setup = C1700_DEFAULT_CONF_REG; |
| 394 |
vm->clock_divisor = C1700_DEFAULT_CLOCK_DIV; |
| 395 |
vm->nvram_rom_space = C1700_NVRAM_ROM_RES_SIZE; |
| 396 |
vm->nm_iomem_size = C1700_DEFAULT_IOMEM_SIZE; |
| 397 |
|
| 398 |
vm->pcmcia_disk_size[0] = C1700_DEFAULT_DISK0_SIZE; |
| 399 |
vm->pcmcia_disk_size[1] = C1700_DEFAULT_DISK1_SIZE; |
| 400 |
} |
| 401 |
|
| 402 |
/* Set an IRQ */ |
| 403 |
static void c1700_set_irq(vm_instance_t *vm,u_int irq) |
| 404 |
{ |
| 405 |
c1700_t *router = VM_C1700(vm); |
| 406 |
cpu_ppc_t *cpu = CPU_PPC32(vm->boot_cpu); |
| 407 |
u_int slot,port; |
| 408 |
|
| 409 |
switch(irq) { |
| 410 |
case C1700_VTIMER_IRQ: |
| 411 |
mpc860_set_pending_irq(router->mpc_data,30); |
| 412 |
break; |
| 413 |
case C1700_DUART_IRQ: |
| 414 |
mpc860_set_pending_irq(router->mpc_data,29); |
| 415 |
break; |
| 416 |
case C1700_NETIO_IRQ: |
| 417 |
mpc860_set_pending_irq(router->mpc_data,25); |
| 418 |
break; |
| 419 |
case C1700_PA_MGMT_IRQ: |
| 420 |
mpc860_set_pending_irq(router->mpc_data,27); |
| 421 |
break; |
| 422 |
|
| 423 |
case C1700_NETIO_IRQ_BASE ... C1700_NETIO_IRQ_END: |
| 424 |
c1700_net_irq_get_slot_port(irq,&slot,&port); |
| 425 |
dev_c1700_iofpga_net_set_irq(router->iofpga_data,slot,port); |
| 426 |
break; |
| 427 |
|
| 428 |
/* IRQ test */ |
| 429 |
case 255: |
| 430 |
mpc860_set_pending_irq(router->mpc_data,24); |
| 431 |
break; |
| 432 |
} |
| 433 |
|
| 434 |
if (vm->irq_idle_preempt[irq]) |
| 435 |
cpu_idle_break_wait(cpu->gen); |
| 436 |
} |
| 437 |
|
| 438 |
/* Clear an IRQ */ |
| 439 |
static void c1700_clear_irq(vm_instance_t *vm,u_int irq) |
| 440 |
{ |
| 441 |
c1700_t *router = VM_C1700(vm); |
| 442 |
u_int slot,port; |
| 443 |
|
| 444 |
switch(irq) { |
| 445 |
case C1700_VTIMER_IRQ: |
| 446 |
mpc860_clear_pending_irq(router->mpc_data,30); |
| 447 |
break; |
| 448 |
case C1700_DUART_IRQ: |
| 449 |
mpc860_clear_pending_irq(router->mpc_data,29); |
| 450 |
break; |
| 451 |
case C1700_NETIO_IRQ: |
| 452 |
mpc860_clear_pending_irq(router->mpc_data,25); |
| 453 |
break; |
| 454 |
case C1700_PA_MGMT_IRQ: |
| 455 |
mpc860_clear_pending_irq(router->mpc_data,27); |
| 456 |
break; |
| 457 |
|
| 458 |
case C1700_NETIO_IRQ_BASE ... C1700_NETIO_IRQ_END: |
| 459 |
c1700_net_irq_get_slot_port(irq,&slot,&port); |
| 460 |
dev_c1700_iofpga_net_clear_irq(router->iofpga_data,slot,port); |
| 461 |
break; |
| 462 |
|
| 463 |
/* IRQ test */ |
| 464 |
case 255: |
| 465 |
mpc860_clear_pending_irq(router->mpc_data,24); |
| 466 |
break; |
| 467 |
} |
| 468 |
} |
| 469 |
|
| 470 |
/* Initialize the C1700 Platform */ |
| 471 |
static int c1700_init_platform(c1700_t *router) |
| 472 |
{ |
| 473 |
vm_instance_t *vm = router->vm; |
| 474 |
vm_obj_t *obj; |
| 475 |
cpu_ppc_t *cpu; |
| 476 |
cpu_gen_t *gen; |
| 477 |
|
| 478 |
/* Copy config register setup into "active" config register */ |
| 479 |
vm->conf_reg = vm->conf_reg_setup; |
| 480 |
|
| 481 |
/* Create Console and AUX ports */ |
| 482 |
vm_init_vtty(vm); |
| 483 |
|
| 484 |
/* Create a CPU group */ |
| 485 |
vm->cpu_group = cpu_group_create("System CPU"); |
| 486 |
|
| 487 |
/* Initialize the virtual PowerPC processor */ |
| 488 |
if (!(gen = cpu_create(vm,CPU_TYPE_PPC32,0))) { |
| 489 |
vm_error(vm,"unable to create CPU!\n"); |
| 490 |
return(-1); |
| 491 |
} |
| 492 |
|
| 493 |
cpu = CPU_PPC32(gen); |
| 494 |
|
| 495 |
/* Add this CPU to the system CPU group */ |
| 496 |
cpu_group_add(vm->cpu_group,gen); |
| 497 |
vm->boot_cpu = gen; |
| 498 |
|
| 499 |
/* Set processor ID */ |
| 500 |
ppc32_set_pvr(cpu,0x00500202); |
| 501 |
|
| 502 |
/* Mark the Network IO interrupt as high priority */ |
| 503 |
vm->irq_idle_preempt[C1700_NETIO_IRQ] = TRUE; |
| 504 |
vm->irq_idle_preempt[C1700_DUART_IRQ] = TRUE; |
| 505 |
|
| 506 |
/* Copy some parameters from VM to CPU (idle PC, ...) */ |
| 507 |
cpu->idle_pc = vm->idle_pc; |
| 508 |
|
| 509 |
if (vm->timer_irq_check_itv) |
| 510 |
cpu->timer_irq_check_itv = vm->timer_irq_check_itv; |
| 511 |
|
| 512 |
/* Remote emulator control */ |
| 513 |
dev_remote_control_init(vm,0xf6000000,0x1000); |
| 514 |
|
| 515 |
/* MPC860 */ |
| 516 |
cpu->mpc860_immr = C1700_MPC860_ADDR; |
| 517 |
|
| 518 |
if (dev_mpc860_init(vm,"MPC860",C1700_MPC860_ADDR,0x10000) == -1) |
| 519 |
return(-1); |
| 520 |
|
| 521 |
if (!(obj = vm_object_find(router->vm,"MPC860"))) |
| 522 |
return(-1); |
| 523 |
|
| 524 |
router->mpc_data = obj->data; |
| 525 |
|
| 526 |
/* IO FPGA */ |
| 527 |
if (dev_c1700_iofpga_init(router,C1700_IOFPGA_ADDR,0x10000) == -1) |
| 528 |
return(-1); |
| 529 |
|
| 530 |
if (!(obj = vm_object_find(router->vm,"io_fpga"))) |
| 531 |
return(-1); |
| 532 |
|
| 533 |
router->iofpga_data = obj->data; |
| 534 |
|
| 535 |
/* Initialize the chassis */ |
| 536 |
if (c1700_init(router) == -1) |
| 537 |
return(-1); |
| 538 |
|
| 539 |
/* Initialize RAM */ |
| 540 |
vm_ram_init(vm,0x00000000ULL); |
| 541 |
|
| 542 |
/* Initialize ROM */ |
| 543 |
if (!vm->rom_filename) { |
| 544 |
/* use embedded ROM */ |
| 545 |
dev_rom_init(vm,"rom",C1700_ROM_ADDR,512*1024, |
| 546 |
ppc32_microcode,ppc32_microcode_len); |
| 547 |
} else { |
| 548 |
/* use alternate ROM */ |
| 549 |
dev_ram_init(vm,"rom",TRUE,TRUE,NULL,FALSE,C1700_ROM_ADDR,512*1024); |
| 550 |
} |
| 551 |
|
| 552 |
/* RAM aliasing */ |
| 553 |
dev_create_ram_alias(vm,"ram_alias","ram",0x80000000,vm->ram_size*1048576); |
| 554 |
|
| 555 |
/* NVRAM */ |
| 556 |
dev_nvram_init(vm,"nvram", |
| 557 |
C1700_NVRAM_ADDR,vm->nvram_size*1024,&vm->conf_reg); |
| 558 |
|
| 559 |
|
| 560 |
/* Bootflash (4 Mb) */ |
| 561 |
dev_bootflash_init(vm,"bootflash","c1700-bootflash-4mb",C1700_FLASH_ADDR); |
| 562 |
|
| 563 |
/* Initialize the NS16552 DUART */ |
| 564 |
dev_ns16552_init(vm,C1700_DUART_ADDR,0x1000,0,C1700_DUART_IRQ, |
| 565 |
vm->vtty_con,vm->vtty_aux); |
| 566 |
|
| 567 |
/* Initialize Network Modules */ |
| 568 |
if (vm_slot_init_all(vm) == -1) |
| 569 |
return(-1); |
| 570 |
|
| 571 |
/* Show device list */ |
| 572 |
c1700_show_hardware(router); |
| 573 |
return(0); |
| 574 |
} |
| 575 |
|
| 576 |
static struct ppc32_bat_prog bat_array[] = { |
| 577 |
{ PPC32_IBAT_IDX, 0, 0xfff0001e, 0xfff00001 }, |
| 578 |
{ PPC32_IBAT_IDX, 1, 0x00001ffe, 0x00000001 }, |
| 579 |
{ PPC32_IBAT_IDX, 2, 0x00000000, 0xee3e0072 }, |
| 580 |
{ PPC32_IBAT_IDX, 3, 0x80001ffe, 0x00000001 }, |
| 581 |
|
| 582 |
{ PPC32_DBAT_IDX, 0, 0x80001ffe, 0x00000042 }, |
| 583 |
{ PPC32_DBAT_IDX, 1, 0x00001ffe, 0x0000002a }, |
| 584 |
{ PPC32_DBAT_IDX, 2, 0x40007ffe, 0x4000002a }, |
| 585 |
{ PPC32_DBAT_IDX, 3, 0xf0001ffe, 0xf000002a }, |
| 586 |
{ -1, -1, 0, 0 }, |
| 587 |
}; |
| 588 |
|
| 589 |
/* Boot the IOS image */ |
| 590 |
static int c1700_boot_ios(c1700_t *router) |
| 591 |
{ |
| 592 |
vm_instance_t *vm = router->vm; |
| 593 |
cpu_ppc_t *cpu; |
| 594 |
|
| 595 |
if (!vm->boot_cpu) |
| 596 |
return(-1); |
| 597 |
|
| 598 |
/* Suspend CPU activity since we will restart directly from ROM */ |
| 599 |
vm_suspend(vm); |
| 600 |
|
| 601 |
/* Check that CPU activity is really suspended */ |
| 602 |
if (cpu_group_sync_state(vm->cpu_group) == -1) { |
| 603 |
vm_error(vm,"unable to sync with system CPUs.\n"); |
| 604 |
return(-1); |
| 605 |
} |
| 606 |
|
| 607 |
/* Reset the boot CPU */ |
| 608 |
cpu = CPU_PPC32(vm->boot_cpu); |
| 609 |
ppc32_reset(cpu); |
| 610 |
|
| 611 |
/* Adjust stack pointer */ |
| 612 |
cpu->gpr[1] |= 0x80000000; |
| 613 |
|
| 614 |
/* Load BAT registers */ |
| 615 |
printf("Loading BAT registers\n"); |
| 616 |
ppc32_load_bat_array(cpu,bat_array); |
| 617 |
cpu->msr |= PPC32_MSR_IR|PPC32_MSR_DR; |
| 618 |
|
| 619 |
/* IRQ routing */ |
| 620 |
vm->set_irq = c1700_set_irq; |
| 621 |
vm->clear_irq = c1700_clear_irq; |
| 622 |
|
| 623 |
/* Load IOS image */ |
| 624 |
if (ppc32_load_elf_image(cpu,vm->ios_image, |
| 625 |
(vm->ghost_status == VM_GHOST_RAM_USE), |
| 626 |
&vm->ios_entry_point) < 0) |
| 627 |
{ |
| 628 |
vm_error(vm,"failed to load Cisco IOS image '%s'.\n",vm->ios_image); |
| 629 |
return(-1); |
| 630 |
} |
| 631 |
|
| 632 |
/* Launch the simulation */ |
| 633 |
printf("\nC1700 '%s': starting simulation (CPU0 IA=0x%8.8x), " |
| 634 |
"JIT %sabled.\n", |
| 635 |
vm->name,cpu->ia,vm->jit_use ? "en":"dis"); |
| 636 |
|
| 637 |
vm_log(vm,"C1700_BOOT", |
| 638 |
"starting instance (CPU0 PC=0x%8.8x,idle_pc=0x%8.8x,JIT %s)\n", |
| 639 |
cpu->ia,cpu->idle_pc,vm->jit_use ? "on":"off"); |
| 640 |
|
| 641 |
/* Start main CPU */ |
| 642 |
if (vm->ghost_status != VM_GHOST_RAM_GENERATE) { |
| 643 |
vm->status = VM_STATUS_RUNNING; |
| 644 |
cpu_start(vm->boot_cpu); |
| 645 |
} else { |
| 646 |
vm->status = VM_STATUS_SHUTDOWN; |
| 647 |
} |
| 648 |
return(0); |
| 649 |
} |
| 650 |
|
| 651 |
/* Initialize a Cisco 1700 instance */ |
| 652 |
static int c1700_init_instance(vm_instance_t *vm) |
| 653 |
{ |
| 654 |
c1700_t *router = VM_C1700(vm); |
| 655 |
m_uint32_t rom_entry_point; |
| 656 |
cpu_ppc_t *cpu0; |
| 657 |
|
| 658 |
if (!vm->ios_image) { |
| 659 |
vm_error(vm,"no Cisco IOS image defined."); |
| 660 |
return(-1); |
| 661 |
} |
| 662 |
|
| 663 |
/* Initialize the C1700 platform */ |
| 664 |
if (c1700_init_platform(router) == -1) { |
| 665 |
vm_error(vm,"unable to initialize the platform hardware.\n"); |
| 666 |
return(-1); |
| 667 |
} |
| 668 |
|
| 669 |
/* Load IOS configuration files */ |
| 670 |
if (vm->ios_startup_config != NULL || vm->ios_private_config != NULL) { |
| 671 |
vm_nvram_push_config(vm,vm->ios_startup_config,vm->ios_private_config); |
| 672 |
vm->conf_reg &= ~0x40; |
| 673 |
} |
| 674 |
|
| 675 |
/* Load ROM (ELF image or embedded) */ |
| 676 |
cpu0 = CPU_PPC32(vm->boot_cpu); |
| 677 |
rom_entry_point = (m_uint32_t)PPC32_ROM_START; |
| 678 |
|
| 679 |
if ((vm->rom_filename != NULL) && |
| 680 |
(ppc32_load_elf_image(cpu0,vm->rom_filename,0,&rom_entry_point) < 0)) |
| 681 |
{ |
| 682 |
vm_error(vm,"unable to load alternate ROM '%s', " |
| 683 |
"fallback to embedded ROM.\n\n",vm->rom_filename); |
| 684 |
vm->rom_filename = NULL; |
| 685 |
} |
| 686 |
|
| 687 |
return(c1700_boot_ios(router)); |
| 688 |
} |
| 689 |
|
| 690 |
/* Stop a Cisco 1700 instance */ |
| 691 |
static int c1700_stop_instance(vm_instance_t *vm) |
| 692 |
{ |
| 693 |
printf("\nC1700 '%s': stopping simulation.\n",vm->name); |
| 694 |
vm_log(vm,"C1700_STOP","stopping simulation.\n"); |
| 695 |
|
| 696 |
/* Stop all CPUs */ |
| 697 |
if (vm->cpu_group != NULL) { |
| 698 |
vm_stop(vm); |
| 699 |
|
| 700 |
if (cpu_group_sync_state(vm->cpu_group) == -1) { |
| 701 |
vm_error(vm,"unable to sync with system CPUs.\n"); |
| 702 |
return(-1); |
| 703 |
} |
| 704 |
} |
| 705 |
|
| 706 |
/* Free resources that were used during execution to emulate hardware */ |
| 707 |
vm_slot_shutdown_all(vm); |
| 708 |
vm_hardware_shutdown(vm); |
| 709 |
|
| 710 |
/* Cleanup */ |
| 711 |
VM_C1700(vm)->iofpga_data = NULL; |
| 712 |
VM_C1700(vm)->mpc_data = NULL; |
| 713 |
return(0); |
| 714 |
} |
| 715 |
|
| 716 |
/* Get MAC address MSB */ |
| 717 |
static u_int c1700_get_mac_addr_msb(void) |
| 718 |
{ |
| 719 |
return(0xD0); |
| 720 |
} |
| 721 |
|
| 722 |
/* Parse specific options for the Cisco 1700 platform */ |
| 723 |
static int c1700_cli_parse_options(vm_instance_t *vm,int option) |
| 724 |
{ |
| 725 |
c1700_t *router = VM_C1700(vm); |
| 726 |
|
| 727 |
switch(option) { |
| 728 |
/* IO memory reserved for NMs (in percents!) */ |
| 729 |
case OPT_IOMEM_SIZE: |
| 730 |
vm->nm_iomem_size = 0x8000 | atoi(optarg); |
| 731 |
break; |
| 732 |
|
| 733 |
/* Mainboard type */ |
| 734 |
case 't': |
| 735 |
c1700_mainboard_set_type(router,optarg); |
| 736 |
break; |
| 737 |
|
| 738 |
/* Set the base MAC address */ |
| 739 |
case 'm': |
| 740 |
if (!c1700_chassis_set_mac_addr(router,optarg)) |
| 741 |
printf("MAC address set to '%s'.\n",optarg); |
| 742 |
break; |
| 743 |
|
| 744 |
/* Set the System ID */ |
| 745 |
case 'I': |
| 746 |
if (!c1700_set_system_id(router,optarg)) |
| 747 |
printf("System ID set to '%s'.\n",optarg); |
| 748 |
break; |
| 749 |
|
| 750 |
/* Unknown option */ |
| 751 |
default: |
| 752 |
return(-1); |
| 753 |
} |
| 754 |
|
| 755 |
return(0); |
| 756 |
} |
| 757 |
|
| 758 |
/* Show specific CLI options */ |
| 759 |
static void c1700_cli_show_options(vm_instance_t *vm) |
| 760 |
{ |
| 761 |
printf(" --iomem-size <val> : IO memory (in percents, default: %u)\n" |
| 762 |
" -t <chassis_type> : Select Chassis type\n" |
| 763 |
" -p <wic_desc> : Define a WIC Module\n" |
| 764 |
" -I <serialno> : Set Processor Board Serial Number\n" |
| 765 |
" -s <wic_nio> : Bind a Network IO interface to a WIC\n", |
| 766 |
vm->nm_iomem_size); |
| 767 |
} |
| 768 |
|
| 769 |
/* Platform definition */ |
| 770 |
static vm_platform_t c1700_platform = { |
| 771 |
"c1700", "C1700", "1700", |
| 772 |
c1700_create_instance, |
| 773 |
c1700_delete_instance, |
| 774 |
c1700_init_instance, |
| 775 |
c1700_stop_instance, |
| 776 |
NULL, |
| 777 |
NULL, |
| 778 |
c1700_nvram_extract_config, |
| 779 |
c1700_nvram_push_config, |
| 780 |
c1700_get_mac_addr_msb, |
| 781 |
c1700_save_config, |
| 782 |
c1700_cli_parse_options, |
| 783 |
c1700_cli_show_options, |
| 784 |
c1700_mainboard_show_drivers, |
| 785 |
}; |
| 786 |
|
| 787 |
/* Register the c1700 platform */ |
| 788 |
int c1700_platform_register(void) |
| 789 |
{ |
| 790 |
if (vm_platform_register(&c1700_platform) == -1) |
| 791 |
return(-1); |
| 792 |
|
| 793 |
return(hypervisor_c1700_init(&c1700_platform)); |
| 794 |
} |