/[dynamips]/upstream/dynamips-0.2.5/nvram_export.c
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /upstream/dynamips-0.2.5/nvram_export.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1 - (hide annotations)
Sat Oct 6 16:01:44 2007 UTC (12 years, 11 months ago) by dpavlin
File MIME type: text/plain
File size: 2528 byte(s)
import 0.2.5 from upstream

1 dpavlin 1 /*
2     * Cisco C7200 Simulation Platform.
3     * Copyright (c) 2005,2006 Christophe Fillot. All rights reserved.
4     *
5     * Extract IOS configuration from a NVRAM file (standalone tool)
6     */
7    
8     #include <stdio.h>
9     #include <stdlib.h>
10     #include <string.h>
11     #include <time.h>
12     #include <errno.h>
13    
14     #include "mips64.h"
15     #include "dynamips.h"
16     #include "memory.h"
17     #include "device.h"
18     #include "dev_c7200.h"
19    
20     /* Export configuration from NVRAM */
21     int dev_nvram_export_config(char *nvram_filename,char *cfg_filename)
22     {
23     m_uint32_t tag,start,end,len,clen,nvlen;
24     FILE *nvram_fd,*cfg_fd;
25     char buffer[512];
26     int res = -1;
27    
28     if (!(nvram_fd = fopen(nvram_filename,"r"))) {
29     fprintf(stderr,"Unable to open NVRAM file '%s'!\n",nvram_filename);
30     return(-1);
31     }
32    
33     if (!(cfg_fd = fopen(cfg_filename,"w"))) {
34     fprintf(stderr,"Unable to create config file '%s'!\n",cfg_filename);
35     return(-1);
36     }
37    
38     fseek(nvram_fd,C7200_NVRAM_ROM_RES_SIZE+6,SEEK_SET);
39     fread(&tag,sizeof(tag),1,nvram_fd);
40     if (ntohl(tag) != 0xF0A5ABCD) {
41     fprintf(stderr,"NVRAM: Unable to find IOS tag (tag=0x%8.8x)!\n",
42     ntohl(tag));
43     goto done;
44     }
45    
46     fseek(nvram_fd,0x06,SEEK_CUR);
47     fread(&start,sizeof(start),1,nvram_fd);
48     fread(&end,sizeof(end),1,nvram_fd);
49     fread(&nvlen,sizeof(nvlen),1,nvram_fd);
50     start = htonl(start) + 1;
51     end = htonl(end);
52     nvlen = htonl(nvlen);
53    
54     if ((start <= C7200_NVRAM_ADDR) || (end <= C7200_NVRAM_ADDR) ||
55     (end <= start))
56     {
57     fprintf(stderr,"NVRAM: invalid configuration markers "
58     "(start=0x%x,end=0x%x).\n",start,end);
59     goto done;
60     }
61    
62     clen = len = end - start;
63     if ((clen + 1) != nvlen) {
64     fprintf(stderr,"NVRAM: invalid configuration size (0x%x)\n",nvlen);
65     goto done;
66     }
67    
68     start -= C7200_NVRAM_ADDR;
69     fseek(nvram_fd,start,SEEK_SET);
70    
71     while(len > 0) {
72     if (len > sizeof(buffer))
73     clen = sizeof(buffer);
74     else
75     clen = len;
76    
77     fread(buffer,clen,1,nvram_fd);
78     fwrite(buffer,clen,1,cfg_fd);
79     len -= clen;
80     }
81    
82     res = 0;
83     done:
84     fclose(nvram_fd);
85     fclose(cfg_fd);
86     return(res);
87     }
88    
89     int main(int argc,char *argv[])
90     {
91     printf("Cisco 7200 NVRAM configuration export.\n");
92     printf("Copyright (c) 2006 Christophe Fillot.\n\n");
93    
94     if (argc != 3) {
95     fprintf(stderr,"Usage: %s nvram_file config_file\n",argv[0]);
96     exit(EXIT_FAILURE);
97     }
98    
99     if (!dev_nvram_export_config(argv[1],argv[2]))
100     printf("Configuration written to %s\n",argv[2]);
101    
102     return(0);
103     }

  ViewVC Help
Powered by ViewVC 1.1.26