Debian Codegraph @

common/nvram_export.c

raw
1 /*
2 * Cisco C7200 Simulation Platform.
3 * Copyright (c) 2013 Flávio J. Saraiva
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
13 #include "cpu.h"
14 #include "dynamips.h"
15 #include "memory.h"
16 #include "device.h"
17 #include "dev_c7200.h"
18 #include "dev_c3745.h"
19 #include "dev_c3725.h"
20 #include "dev_c3600.h"
21 #include "dev_c2691.h"
22 #include "dev_c2600.h"
23 #include "dev_c1700.h"
24 #include "dev_c6msfc1.h"
25 #include "fs_nvram.h"
26
27
28 struct nvram_format {
29 const char *name;
30 const char *rom_res_0x200;
31 size_t offset;
32 size_t size;
33 m_uint32_t addr;
34 u_int format;
35 } nvram_formats[] = {
36 {"c1700", "C1700", C1700_NVRAM_ROM_RES_SIZE, 0, 0, FS_NVRAM_FORMAT_DEFAULT},
37 {"c2600", "C2600", C2600_NVRAM_ROM_RES_SIZE*4, 0, 0, FS_NVRAM_FORMAT_SCALE_4},
38 {"c3600", "3600", C3600_NVRAM_ROM_RES_SIZE, 0, 0, FS_NVRAM_FORMAT_DEFAULT},
39 {"c7200", "7200", C7200_NVRAM_ROM_RES_SIZE, 0, C7200_NVRAM_ADDR + C7200_NVRAM_ROM_RES_SIZE, FS_NVRAM_FORMAT_ABSOLUTE},
40 {"c7200-npe-g2", "7200", C7200_NVRAM_ROM_RES_SIZE, 0, C7200_G2_NVRAM_ADDR + C7200_NVRAM_ROM_RES_SIZE, FS_NVRAM_FORMAT_ABSOLUTE},
41 {"c6msfc1", NULL, C6MSFC1_NVRAM_ROM_RES_SIZE, 0, C6MSFC1_NVRAM_ADDR + C6MSFC1_NVRAM_ROM_RES_SIZE, FS_NVRAM_FORMAT_ABSOLUTE_C6},
42 {"c2691", NULL, C2691_NVRAM_OFFSET, C2691_NVRAM_SIZE, 0, FS_NVRAM_FORMAT_WITH_BACKUP},
43 {"c3725", NULL, C3725_NVRAM_OFFSET, C3725_NVRAM_SIZE, 0, FS_NVRAM_FORMAT_WITH_BACKUP}, // XXX same as c2691
44 {"c3745", NULL, C3745_NVRAM_OFFSET, C3745_NVRAM_SIZE, 0, FS_NVRAM_FORMAT_WITH_BACKUP},
45 {"c7200-npe-g1", NULL, C7200_NVRAM_ROM_RES_SIZE, 0, C7200_G1_NVRAM_ADDR + C7200_NVRAM_ROM_RES_SIZE, FS_NVRAM_FORMAT_ABSOLUTE}, // XXX didn't find working image
46 {NULL, NULL, 0, 0, 0, 0}
47 };
48
49
50 /** Read file data. */
51 int read_file(const char *filename, u_char **data, size_t *data_len)
52 {
53 FILE *fd;
54 long len;
55
56 // open
57 fd = fopen(filename,"rb");
58 if (fd == NULL) {
59 return(-1);
60 }
61
62 // len
63 fseek(fd, 0, SEEK_END);
64 len = ftell(fd);
65 fseek(fd, 0, SEEK_SET);
66 if (len < 0 || ferror(fd)) {
67 fclose(fd);
68 return(-1);
69 }
70
71 if (data_len) {
72 *data_len = (size_t)len;
73 }
74
75 // data
76 if (data) {
77 *data = (u_char *)malloc((size_t)len);
78 if (fread(*data, (size_t)len, 1, fd) != 1) {
79 free(*data);
80 *data = NULL;
81 fclose(fd);
82 return(-1);
83 }
84 }
85
86 // close
87 fclose(fd);
88 return(0);
89 }
90
91
92 /** Write file data. */
93 int write_file(const char *filename, u_char *data, size_t len)
94 {
95 FILE *fp;
96
97 fp = fopen(filename,"wb+");
98 if (fp == NULL) {
99 return(-1);
100 }
101
102 if (fwrite(data, len, 1, fp) != 1) {
103 fclose(fp);
104 return(-1);
105 }
106
107 fclose(fp);
108 return(0);
109 }
110
111
112 /** Export configuration from NVRAM. */
113 int nvram_export_config(const char *nvram_filename, const char *startup_filename, const char *private_filename)
114 {
115 u_char *data = NULL;
116 u_char *startup_config = NULL;
117 u_char *private_config = NULL;
118 size_t data_len = 0;
119 size_t startup_len = 0;
120 size_t private_len;
121 fs_nvram_t *fs;
122 size_t len;
123 struct nvram_format *fmt;
124
125 // read nvram
126 printf("Reading %s...\n", nvram_filename);
127 if (read_file(nvram_filename, &data, &data_len)) {
128 perror(nvram_filename);
129 return(-1);
130 }
131
132 // try each format
133 for (fmt = &nvram_formats[0]; ; fmt++) {
134 if (fmt->name == NULL) {
135 fprintf(stderr,"NVRAM not found\n");
136 return(-1);
137 }
138
139 if (fmt->rom_res_0x200) {
140 len = strlen(fmt->rom_res_0x200);
141 if (data_len < 0x200 + len || memcmp(data + 0x200, fmt->rom_res_0x200, len)) {
142 continue; // must match
143 }
144 }
145
146 if (fmt->size > 0) {
147 if (data_len < fmt->offset + fmt->size) {
148 continue; // must fit
149 }
150 len = fmt->size;
151 }
152 else {
153 if (data_len < fmt->offset) {
154 continue; // must fit
155 }
156 len = data_len - fmt->offset;
157 }
158
159 fs = fs_nvram_open(data + fmt->offset, len, fmt->addr, fmt->format);
160 if (fs == NULL) {
161 continue; // filesystem not found
162 }
163
164 if (fs_nvram_verify(fs, FS_NVRAM_VERIFY_ALL) || fs_nvram_read_config(fs, &startup_config, &startup_len, &private_config, &private_len)) {
165 fs_nvram_close(fs);
166 fs = NULL;
167 continue; // filesystem error
168 }
169
170 printf("Found NVRAM format %s\n", fmt->name);
171 fs_nvram_close(fs);
172 fs = NULL;
173 break;
174 }
175
176 // write config
177 if (startup_filename) {
178 printf("Writing startup-config to %s...\n", startup_filename);
179 if (write_file(startup_filename, startup_config, startup_len)) {
180 perror(startup_filename);
181 return(-1);
182 }
183 }
184 if (private_filename) {
185 printf("Writing private-config to %s...\n", private_filename);
186 if (write_file(private_filename, private_config, private_len)) {
187 perror(private_filename);
188 return(-1);
189 }
190 }
191
192 // cleanup
193 if (startup_config) {
194 free(startup_config);
195 startup_config = NULL;
196 }
197 if (private_config) {
198 free(private_config);
199 private_config = NULL;
200 }
201 return(0);
202 }
203
204 int main(int argc,char *argv[])
205 {
206 const char *nvram_filename;
207 const char *startup_filename;
208 const char *private_filename = NULL;
209 struct nvram_format *fmt;
210
211 printf("Cisco NVRAM configuration export.\n");
212 printf("Copyright (c) 2013 Flávio J. Saraiva.\n\n");
213
214 if (argc < 3 || argc > 4) {
215 fprintf(stderr,"Usage: %s nvram_file config_file [private_file]\n",argv[0]);
216 fprintf(stderr,"\n");
217 fprintf(stderr,"This tools extracts 'startup-config' and 'private-config' from NVRAM.\n");
218 fprintf(stderr," nvram_file - file that contains the NVRAM data\n");
219 fprintf(stderr," (on some platforms, NVRAM is simulated inside the ROM)\n");
220 fprintf(stderr," config_file - file for 'startup-config'\n");
221 fprintf(stderr," private_file - file for 'private-config' (optional)\n");
222 fprintf(stderr,"\n");
223 fprintf(stderr,"Supports:");
224 for (fmt = &nvram_formats[0]; fmt->name; fmt++) {
225 fprintf(stderr," %s",fmt->name);
226 }
227 fprintf(stderr,"\n");
228 exit(EXIT_FAILURE);
229 }
230
231 nvram_filename = argv[1];
232 startup_filename = argv[2];
233 if (argc > 3)
234 private_filename = argv[3];
235
236 if (nvram_export_config(nvram_filename,startup_filename,private_filename))
237 return(EXIT_FAILURE);
238
239 printf("Done\n");
240 return(0);
241 }