| 1 |
/* |
| 2 |
* Cisco router simulation platform. |
| 3 |
* Copyright (c) 2005,2006 Christophe Fillot. All rights reserved. |
| 4 |
* |
| 5 |
* Dallas DS1216 chip emulation: |
| 6 |
* - NVRAM |
| 7 |
* - Calendar |
| 8 |
* |
| 9 |
* Manuals: |
| 10 |
* http://pdfserv.maxim-ic.com/en/ds/DS1216-DS1216H.pdf |
| 11 |
* |
| 12 |
* Calendar stuff written by Mtve. |
| 13 |
*/ |
| 14 |
|
| 15 |
#include <stdio.h> |
| 16 |
#include <stdlib.h> |
| 17 |
#include <string.h> |
| 18 |
#include <time.h> |
| 19 |
#include <errno.h> |
| 20 |
|
| 21 |
#include "cpu.h" |
| 22 |
#include "vm.h" |
| 23 |
#include "dynamips.h" |
| 24 |
#include "memory.h" |
| 25 |
#include "device.h" |
| 26 |
#include "fs_nvram.h" |
| 27 |
|
| 28 |
#define DEBUG_ACCESS 0 |
| 29 |
|
| 30 |
/* SmartWatch pattern (p.5 of documentation) */ |
| 31 |
#define PATTERN 0x5ca33ac55ca33ac5ULL |
| 32 |
|
| 33 |
/* NVRAM private data */ |
| 34 |
struct nvram_data { |
| 35 |
vm_obj_t vm_obj; |
| 36 |
struct vdevice dev; |
| 37 |
char *filename; |
| 38 |
u_int cal_state; |
| 39 |
m_uint64_t cal_read,cal_write; |
| 40 |
}; |
| 41 |
|
| 42 |
/* Convert an 8-bit number to a BCD form */ |
| 43 |
static m_uint8_t u8_to_bcd(m_uint8_t val) |
| 44 |
{ |
| 45 |
return(((val / 10) << 4) + (val % 10)); |
| 46 |
} |
| 47 |
|
| 48 |
/* Get the current time (p.8) */ |
| 49 |
static m_uint64_t get_current_time(cpu_gen_t *cpu) |
| 50 |
{ |
| 51 |
m_uint64_t res; |
| 52 |
struct tm tmx; |
| 53 |
struct timespec spec; |
| 54 |
|
| 55 |
clock_gettime(CLOCK_REALTIME, &spec); |
| 56 |
gmtime_r(&spec.tv_sec, &tmx); |
| 57 |
|
| 58 |
res = u8_to_bcd(tmx.tm_sec) << 8; |
| 59 |
res += u8_to_bcd(tmx.tm_min) << 16; |
| 60 |
res += u8_to_bcd(tmx.tm_hour) << 24; |
| 61 |
res += ((m_uint64_t)(u8_to_bcd(tmx.tm_wday))) << 32; |
| 62 |
res += ((m_uint64_t)(u8_to_bcd(tmx.tm_mday))) << 40; |
| 63 |
res += ((m_uint64_t)(u8_to_bcd(tmx.tm_mon+1))) << 48; |
| 64 |
res += ((m_uint64_t)(u8_to_bcd(tmx.tm_year))) << 56; |
| 65 |
|
| 66 |
return(res); |
| 67 |
} |
| 68 |
|
| 69 |
/* |
| 70 |
* dev_nvram_access() |
| 71 |
*/ |
| 72 |
void *dev_nvram_access(cpu_gen_t *cpu,struct vdevice *dev, |
| 73 |
m_uint32_t offset,u_int op_size,u_int op_type, |
| 74 |
m_uint64_t *data) |
| 75 |
{ |
| 76 |
struct nvram_data *d = dev->priv_data; |
| 77 |
|
| 78 |
#if DEBUG_ACCESS |
| 79 |
if (op_type == MTS_READ) |
| 80 |
cpu_log(cpu,dev->name,"read access to offset=0x%x, pc=0x%llx\n", |
| 81 |
offset,cpu_get_pc(cpu)); |
| 82 |
else |
| 83 |
cpu_log(cpu,dev->name, |
| 84 |
"write access to vaddr=0x%x, pc=0x%llx, val=0x%llx\n", |
| 85 |
offset,cpu_get_pc(cpu),*data); |
| 86 |
#endif |
| 87 |
|
| 88 |
switch(offset) { |
| 89 |
case 0x03: |
| 90 |
if (op_type == MTS_READ) { |
| 91 |
*data = d->cal_read & 1; |
| 92 |
d->cal_read >>= 1; |
| 93 |
} else { |
| 94 |
d->cal_write >>= 1; |
| 95 |
d->cal_write |= *data << 63; |
| 96 |
|
| 97 |
if (d->cal_write == PATTERN) { |
| 98 |
d->cal_state = 1; |
| 99 |
vm_log(cpu->vm,"Calendar","reset\n"); |
| 100 |
d->cal_read = get_current_time(cpu); |
| 101 |
} else if(d->cal_state > 0 && d->cal_state++ == 64) { |
| 102 |
/* untested */ |
| 103 |
vm_log(cpu->vm,"Calendar","set 0x%016llx\n",d->cal_write); |
| 104 |
d->cal_state = 0; |
| 105 |
} |
| 106 |
} |
| 107 |
return NULL; |
| 108 |
} |
| 109 |
|
| 110 |
return((void *)(dev->host_addr + offset)); |
| 111 |
} |
| 112 |
|
| 113 |
/* Set appropriately the config register if the NVRAM is empty */ |
| 114 |
static void set_config_register(struct vdevice *dev,u_int *conf_reg) |
| 115 |
{ |
| 116 |
m_uint32_t *ptr; |
| 117 |
int i; |
| 118 |
|
| 119 |
ptr = (m_uint32_t *)dev->host_addr; |
| 120 |
for(i=0;i<(dev->phys_len/4);i++,ptr++) |
| 121 |
if (*ptr) return; |
| 122 |
|
| 123 |
/* |
| 124 |
* nvram is empty: tells IOS to ignore its contents. |
| 125 |
* http://www.cisco.com/en/US/products/hw/routers/ps274/products_installation_guide_chapter09186a008007de4c.html |
| 126 |
*/ |
| 127 |
*conf_reg |= 0x0040; |
| 128 |
printf("NVRAM is empty, setting config register to 0x%x\n",*conf_reg); |
| 129 |
} |
| 130 |
|
| 131 |
/* Shutdown the NVRAM device */ |
| 132 |
void dev_nvram_shutdown(vm_instance_t *vm,struct nvram_data *d) |
| 133 |
{ |
| 134 |
if (d != NULL) { |
| 135 |
/* Remove the device */ |
| 136 |
dev_remove(vm,&d->dev); |
| 137 |
|
| 138 |
/* We don't remove the file, since it used as permanent storage */ |
| 139 |
if (d->filename) |
| 140 |
free(d->filename); |
| 141 |
|
| 142 |
/* Free the structure itself */ |
| 143 |
free(d); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
/* Create the NVRAM device */ |
| 148 |
int dev_nvram_init(vm_instance_t *vm,char *name, |
| 149 |
m_uint64_t paddr,m_uint32_t len, |
| 150 |
u_int *conf_reg) |
| 151 |
{ |
| 152 |
struct nvram_data *d; |
| 153 |
u_char *ptr; |
| 154 |
|
| 155 |
/* allocate the private data structure */ |
| 156 |
if (!(d = malloc(sizeof(*d)))) { |
| 157 |
fprintf(stderr,"NVRAM: out of memory\n"); |
| 158 |
return(-1); |
| 159 |
} |
| 160 |
|
| 161 |
memset(d,0,sizeof(*d)); |
| 162 |
|
| 163 |
vm_object_init(&d->vm_obj); |
| 164 |
d->vm_obj.name = name; |
| 165 |
d->vm_obj.data = d; |
| 166 |
d->vm_obj.shutdown = (vm_shutdown_t)dev_nvram_shutdown; |
| 167 |
|
| 168 |
if (!(d->filename = vm_build_filename(vm,name))) { |
| 169 |
fprintf(stderr,"NVRAM: unable to create filename.\n"); |
| 170 |
return(-1); |
| 171 |
} |
| 172 |
|
| 173 |
dev_init(&d->dev); |
| 174 |
d->dev.name = name; |
| 175 |
d->dev.phys_addr = paddr; |
| 176 |
d->dev.phys_len = len; |
| 177 |
d->dev.handler = dev_nvram_access; |
| 178 |
d->dev.fd = memzone_create_file(d->filename,d->dev.phys_len,&ptr); |
| 179 |
d->dev.host_addr = (m_iptr_t)ptr; |
| 180 |
d->dev.flags = VDEVICE_FLAG_NO_MTS_MMAP|VDEVICE_FLAG_SYNC; |
| 181 |
d->dev.priv_data = d; |
| 182 |
|
| 183 |
if (d->dev.fd == -1) { |
| 184 |
fprintf(stderr,"NVRAM: unable to map file '%s'\n",d->filename); |
| 185 |
return(-1); |
| 186 |
} |
| 187 |
|
| 188 |
/* Modify the configuration register if NVRAM is empty */ |
| 189 |
set_config_register(&d->dev,conf_reg); |
| 190 |
|
| 191 |
/* Map this device to the VM */ |
| 192 |
vm_bind_device(vm,&d->dev); |
| 193 |
vm_object_add(vm,&d->vm_obj); |
| 194 |
return(0); |
| 195 |
} |
| 196 |
|
| 197 |
/* Compute NVRAM checksum */ |
| 198 |
m_uint16_t nvram_cksum_old(vm_instance_t *vm,m_uint64_t addr,size_t count) |
| 199 |
{ |
| 200 |
m_uint32_t sum = 0; |
| 201 |
|
| 202 |
while(count > 1) { |
| 203 |
sum = sum + physmem_copy_u16_from_vm(vm,addr); |
| 204 |
addr += sizeof(m_uint16_t); |
| 205 |
count -= sizeof(m_uint16_t); |
| 206 |
} |
| 207 |
|
| 208 |
if (count > 0) |
| 209 |
sum = sum + ((physmem_copy_u16_from_vm(vm,addr) & 0xFF) << 8); |
| 210 |
|
| 211 |
while(sum>>16) |
| 212 |
sum = (sum & 0xffff) + (sum >> 16); |
| 213 |
|
| 214 |
return(~sum); |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
/** Generic function for implementations of vm->platform->nvram_extract_config. |
| 219 |
* If nvram_size is 0, it is set based on the file size. |
| 220 |
* @param dev_name Device name |
| 221 |
* @param nvram_offset Where the filesystem starts |
| 222 |
* @param nvram_size Size of the filesystem |
| 223 |
*/ |
| 224 |
int generic_nvram_extract_config(vm_instance_t *vm, char *dev_name, size_t nvram_offset, size_t nvram_size, m_uint32_t addr, u_int format, |
| 225 |
u_char **startup_config, size_t *startup_len, u_char **private_config, size_t *private_len) |
| 226 |
{ |
| 227 |
// XXX add const to dev_name |
| 228 |
u_char *base_ptr; |
| 229 |
struct vdevice *nvram_dev; |
| 230 |
off_t file_size; |
| 231 |
int fd; |
| 232 |
int ret = 0; |
| 233 |
fs_nvram_t *fs = NULL; |
| 234 |
|
| 235 |
if ((nvram_dev = dev_get_by_name(vm, dev_name))) |
| 236 |
dev_sync(nvram_dev); |
| 237 |
|
| 238 |
fd = vm_mmap_open_file(vm, dev_name, &base_ptr, &file_size); |
| 239 |
if (fd == -1) |
| 240 |
return(-1); |
| 241 |
|
| 242 |
if (nvram_size == 0 && file_size >= nvram_offset + FS_NVRAM_SECTOR_SIZE) |
| 243 |
nvram_size = (size_t)file_size - nvram_offset; |
| 244 |
|
| 245 |
if (file_size < nvram_offset + nvram_size) { |
| 246 |
vm_error(vm,"generic_nvram_extract_config: NVRAM filesystem doesn't fit inside the %s file!\n", dev_name); |
| 247 |
goto done; |
| 248 |
} |
| 249 |
|
| 250 |
// normal + backup |
| 251 |
fs = fs_nvram_open(base_ptr + nvram_offset, nvram_size, addr, (format & FS_NVRAM_FORMAT_MASK)); |
| 252 |
if (fs == NULL) |
| 253 |
goto err_errno; |
| 254 |
|
| 255 |
ret = fs_nvram_read_config(fs, startup_config, startup_len, private_config, private_len); |
| 256 |
if (ret) |
| 257 |
goto err_ret; |
| 258 |
|
| 259 |
done: |
| 260 |
if (fs) |
| 261 |
fs_nvram_close(fs); |
| 262 |
|
| 263 |
vm_mmap_close_file(fd, base_ptr, file_size); |
| 264 |
|
| 265 |
return(ret); |
| 266 |
err_errno: |
| 267 |
ret = errno; |
| 268 |
err_ret: |
| 269 |
vm_error(vm,"generic_nvram_extract_config: %s\n", strerror(ret)); |
| 270 |
ret = -1; |
| 271 |
goto done; |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
/** Generic function for implementations of vm->platform->nvram_push_config. |
| 276 |
* If nvram_size is 0, it is set based on the file size. |
| 277 |
* Preserves startup-config if startup_config is NULL. |
| 278 |
* Preserves private-config if private_config is NULL. |
| 279 |
* @param dev_name Device name |
| 280 |
* @param file_size File size |
| 281 |
* @param nvram_offset Where the filesystem starts |
| 282 |
* @param nvram_size Size of the filesystem |
| 283 |
*/ |
| 284 |
int generic_nvram_push_config(vm_instance_t *vm, char *dev_name, size_t file_size, size_t nvram_offset, size_t nvram_size, m_uint32_t addr, u_int format, |
| 285 |
u_char *startup_config, size_t startup_len, u_char *private_config, size_t private_len) |
| 286 |
{ |
| 287 |
// XXX add const to dev_name |
| 288 |
u_char *base_ptr; |
| 289 |
int fd; |
| 290 |
int ret = 0; |
| 291 |
fs_nvram_t *fs = NULL; |
| 292 |
u_char *prev_startup_config = NULL; |
| 293 |
u_char *prev_private_config = NULL; |
| 294 |
size_t prev_startup_len; |
| 295 |
size_t prev_private_len; |
| 296 |
|
| 297 |
if (nvram_size == 0 && file_size >= nvram_offset + FS_NVRAM_SECTOR_SIZE) |
| 298 |
nvram_size = file_size - nvram_offset; |
| 299 |
|
| 300 |
if (file_size < nvram_offset + nvram_size) { |
| 301 |
vm_error(vm, "generic_nvram_push_config: NVRAM filesystem doesn't fit inside the %s file!\n", dev_name); |
| 302 |
return(-1); |
| 303 |
} |
| 304 |
|
| 305 |
fd = vm_mmap_create_file(vm, dev_name, file_size, &base_ptr); |
| 306 |
if (fd == -1) |
| 307 |
return(-1); |
| 308 |
|
| 309 |
fs = fs_nvram_open(base_ptr + nvram_offset, nvram_size, addr, (format & FS_NVRAM_FORMAT_MASK) | FS_NVRAM_FLAG_OPEN_CREATE); |
| 310 |
if (fs == NULL) |
| 311 |
goto err_errno; |
| 312 |
|
| 313 |
ret = fs_nvram_read_config(fs, &prev_startup_config, &prev_startup_len, &prev_private_config, &prev_private_len); |
| 314 |
if (ret) |
| 315 |
goto err_ret; |
| 316 |
|
| 317 |
if (startup_config == NULL ) { |
| 318 |
startup_config = prev_startup_config; |
| 319 |
startup_len = prev_startup_len; |
| 320 |
} |
| 321 |
|
| 322 |
if (private_config == NULL) { |
| 323 |
private_config = prev_private_config; |
| 324 |
private_len = prev_private_len; |
| 325 |
} |
| 326 |
|
| 327 |
ret = fs_nvram_write_config(fs, startup_config, startup_len, private_config, private_len); |
| 328 |
if (ret) |
| 329 |
goto err_ret; |
| 330 |
|
| 331 |
done: |
| 332 |
if (fs) |
| 333 |
fs_nvram_close(fs); |
| 334 |
|
| 335 |
if (prev_startup_config) |
| 336 |
free(prev_startup_config); |
| 337 |
|
| 338 |
if (prev_private_config) |
| 339 |
free(prev_private_config); |
| 340 |
|
| 341 |
vm_mmap_close_file(fd, base_ptr, file_size); |
| 342 |
|
| 343 |
return(ret); |
| 344 |
err_errno: |
| 345 |
ret = errno; |
| 346 |
err_ret: |
| 347 |
vm_error(vm, "generic_nvram_push_config: %s\n", strerror(ret)); |
| 348 |
ret = -1; |
| 349 |
goto done; |
| 350 |
} |