1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
|
/*
* Cisco router simulation platform.
* Copyright (C) 2006 Christophe Fillot. All rights reserved.
*
* Remote control module.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <unistd.h>
#include <time.h>
#include <errno.h>
#include <pthread.h>
#include <assert.h>
#include "utils.h"
#include "cpu.h"
#include "vm.h"
#include "dynamips.h"
#include "memory.h"
#include "device.h"
#include "net.h"
#include "net_io.h"
#include "registry.h"
#include "ptask.h"
#include "dev_c7200.h"
#include "dev_c3600.h"
#include "dev_c2691.h"
#include "dev_c3725.h"
#include "dev_c3745.h"
#include "dev_c2600.h"
#define DEBUG_ACCESS 0
#define ROMMON_SET_VAR 0x01
#define ROMMON_GET_VAR 0x02
#define ROMMON_CLEAR_VAR_STAT 0x03
/* Remote control private data */
struct remote_data {
vm_obj_t vm_obj;
struct vdevice dev;
/* Console buffer */
char con_buffer[512];
u_int con_buf_pos;
/* ROMMON variables buffer */
char var_buffer[512];
u_int var_buf_pos;
u_int var_status;
/* Position for cookie reading */
u_int cookie_pos;
};
/*
* dev_remote_control_access()
*/
void *dev_remote_control_access(cpu_gen_t *cpu,struct vdevice *dev,
m_uint32_t offset,u_int op_size,u_int op_type,
m_uint64_t *data)
{
vm_instance_t *vm = cpu->vm;
struct remote_data *d = dev->priv_data;
struct vdevice *storage_dev;
size_t len;
if (op_type == MTS_READ)
*data = 0;
#if DEBUG_ACCESS
if (op_type == MTS_READ) {
cpu_log(cpu,"REMOTE","reading reg 0x%x at pc=0x%llx\n",
offset,cpu_get_pc(cpu));
} else {
cpu_log(cpu,"REMOTE","writing reg 0x%x at pc=0x%llx, data=0x%llx\n",
offset,cpu_get_pc(cpu),*data);
}
#endif
switch(offset) {
/* ROM Identification tag */
case 0x000:
if (op_type == MTS_READ)
*data = ROM_ID;
break;
/* CPU ID */
case 0x004:
if (op_type == MTS_READ)
*data = cpu->id;
break;
/* Display CPU registers */
case 0x008:
if (op_type == MTS_WRITE)
cpu->reg_dump(cpu);
break;
/* Display CPU memory info */
case 0x00c:
if (op_type == MTS_WRITE)
cpu->mmu_dump(cpu);
break;
/* Reserved/Unused */
case 0x010:
break;
/* RAM size */
case 0x014:
if (op_type == MTS_READ)
*data = vm->ram_size;
break;
/* ROM size */
case 0x018:
if (op_type == MTS_READ)
*data = vm->rom_size;
break;
/* NVRAM size */
case 0x01c:
if (op_type == MTS_READ)
*data = vm->nvram_size;
break;
/* IOMEM size */
case 0x020:
if (op_type == MTS_READ)
*data = vm->iomem_size;
break;
/* Config Register */
case 0x024:
if (op_type == MTS_READ)
*data = vm->conf_reg;
break;
/* ELF entry point */
case 0x028:
if (op_type == MTS_READ)
*data = vm->ios_entry_point;
break;
/* ELF machine id */
case 0x02c:
if (op_type == MTS_READ)
*data = vm->elf_machine_id;
break;
/* Restart IOS Image */
case 0x030:
/* not implemented */
break;
/* Stop the virtual machine */
case 0x034:
vm->status = VM_STATUS_SHUTDOWN;
break;
/* Debugging/Log message: /!\ physical address */
case 0x038:
if (op_type == MTS_WRITE) {
len = physmem_strlen(vm,*data);
if (len < sizeof(d->con_buffer)) {
physmem_copy_from_vm(vm,d->con_buffer,*data,len+1);
vm_log(vm,"ROM",d->con_buffer);
}
}
break;
/* Console Buffering */
case 0x03c:
if (op_type == MTS_WRITE) {
if (d->con_buf_pos < (sizeof(d->con_buffer)-1)) {
d->con_buffer[d->con_buf_pos++] = *data & 0xFF;
d->con_buffer[d->con_buf_pos] = 0;
if (d->con_buffer[d->con_buf_pos-1] == '\n') {
vm_log(vm,"ROM","%s",d->con_buffer);
d->con_buf_pos = 0;
}
} else
d->con_buf_pos = 0;
}
break;
/* Console output */
case 0x040:
if (op_type == MTS_WRITE)
vtty_put_char(vm->vtty_con,(char)*data);
break;
/* NVRAM address */
case 0x044:
if (op_type == MTS_READ) {
if ((storage_dev = dev_get_by_name(vm,"nvram")))
*data = storage_dev->phys_addr;
if ((storage_dev = dev_get_by_name(vm,"ssa")))
*data = storage_dev->phys_addr;
if (cpu->type == CPU_TYPE_MIPS64)
*data += MIPS_KSEG1_BASE;
}
break;
/* IO memory size for Smart-Init (C3600, others ?) */
case 0x048:
if (op_type == MTS_READ)
*data = vm->nm_iomem_size;
break;
/* Cookie position selector */
case 0x04c:
if (op_type == MTS_READ)
*data = d->cookie_pos;
else
d->cookie_pos = *data;
break;
/* Cookie data */
case 0x050:
if ((op_type == MTS_READ) && (d->cookie_pos < 64))
*data = vm->chassis_cookie[d->cookie_pos];
break;
/* ROMMON variable */
case 0x054:
if (op_type == MTS_WRITE) {
if (d->var_buf_pos < (sizeof(d->var_buffer)-1)) {
d->var_buffer[d->var_buf_pos++] = *data & 0xFF;
d->var_buffer[d->var_buf_pos] = 0;
} else
d->var_buf_pos = 0;
} else {
if (d->var_buf_pos < (sizeof(d->var_buffer)-1)) {
*data = d->var_buffer[d->var_buf_pos++];
} else {
d->var_buf_pos = 0;
*data = 0;
}
}
break;
/* ROMMON variable command */
case 0x058:
if (op_type == MTS_WRITE) {
switch(*data & 0xFF) {
case ROMMON_SET_VAR:
d->var_status = rommon_var_add_str(&vm->rommon_vars,
d->var_buffer);
d->var_buf_pos = 0;
break;
case ROMMON_GET_VAR:
d->var_status = rommon_var_get(&vm->rommon_vars,
d->var_buffer,
d->var_buffer,
sizeof(d->var_buffer));
d->var_buf_pos = 0;
break;
case ROMMON_CLEAR_VAR_STAT:
d->var_buf_pos = 0;
break;
default:
d->var_status = -1;
}
} else {
*data = d->var_status;
}
break;
}
return NULL;
}
/* Shutdown a remote control device */
void dev_remote_control_shutdown(vm_instance_t *vm,struct remote_data *d)
{
if (d != NULL) {
dev_remove(vm,&d->dev);
free(d);
}
}
/* remote control device */
int dev_remote_control_init(vm_instance_t *vm,m_uint64_t paddr,m_uint32_t len)
{
struct remote_data *d;
if (!(d = malloc(sizeof(*d)))) {
fprintf(stderr,"Remote Control: unable to create device.\n");
return(-1);
}
memset(d,0,sizeof(*d));
vm_object_init(&d->vm_obj);
d->vm_obj.name = "remote_ctrl";
d->vm_obj.data = d;
d->vm_obj.shutdown = (vm_shutdown_t)dev_remote_control_shutdown;
dev_init(&d->dev);
d->dev.name = "remote_ctrl";
d->dev.phys_addr = paddr;
d->dev.phys_len = len;
d->dev.handler = dev_remote_control_access;
d->dev.priv_data = d;
/* Map this device to the VM */
vm_bind_device(vm,&d->dev);
vm_object_add(vm,&d->vm_obj);
return(0);
}
|