| 1 |
/** @file |
| 2 |
* @brief Cisco NVRAM filesystem. |
| 3 |
*/ |
| 4 |
|
| 5 |
/* |
| 6 |
* Copyright (c) 2013 Flávio J. Saraiva <[email protected]> |
| 7 |
*/ |
| 8 |
|
| 9 |
#include <assert.h> |
| 10 |
#include <errno.h> |
| 11 |
#include <stddef.h> // offsetof |
| 12 |
#include <stdio.h> |
| 13 |
#include <stdlib.h> |
| 14 |
#include <string.h> |
| 15 |
|
| 16 |
#include "fs_nvram.h" |
| 17 |
|
| 18 |
#define DEBUG_BACKUP 0 |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
/** NVRAM filesystem. */ |
| 23 |
struct fs_nvram { |
| 24 |
u_char *base; |
| 25 |
size_t len; |
| 26 |
m_uint32_t addr; /// start address of the filesystem (for absolute addresses) |
| 27 |
u_int flags; /// filesystem flags |
| 28 |
u_int shift; /// scale byte offsets |
| 29 |
u_int padding; /// base padding value |
| 30 |
size_t backup; /// start offset of the backup filesystem |
| 31 |
|
| 32 |
m_uint8_t (*read_byte)(fs_nvram_t *fs, u_int offset); |
| 33 |
void (*write_byte)(fs_nvram_t *fs, u_int offset, m_uint8_t val); |
| 34 |
}; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
//========================================================= |
| 40 |
// Auxiliary |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
/** Convert a 16 bit value from big endian to native. */ |
| 45 |
static inline void be_to_native16(m_uint16_t *val) |
| 46 |
{ |
| 47 |
union { |
| 48 |
m_uint16_t val; |
| 49 |
m_uint8_t b[2]; |
| 50 |
} u; |
| 51 |
u.val = *val; |
| 52 |
*val = (((m_uint16_t)u.b[0] << 8) | u.b[1]); |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
/** Convert a 32 bit value from big endian to native. */ |
| 57 |
static inline void be_to_native32(m_uint32_t *val) |
| 58 |
{ |
| 59 |
union { |
| 60 |
m_uint32_t val; |
| 61 |
m_uint8_t b[4]; |
| 62 |
} u; |
| 63 |
u.val = *val; |
| 64 |
*val = (((m_uint32_t)u.b[0] << 24) | ((m_uint32_t)u.b[1] << 16) | ((m_uint32_t)u.b[2] << 8) | u.b[3]); |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
/** Convert startup-config header values from big endian to native. */ |
| 69 |
static void be_to_native_header_startup(struct fs_nvram_header_startup_config *head) |
| 70 |
{ |
| 71 |
be_to_native16(&head->magic); |
| 72 |
be_to_native16(&head->format); |
| 73 |
be_to_native16(&head->checksum); |
| 74 |
be_to_native16(&head->unk1); |
| 75 |
be_to_native32(&head->start); |
| 76 |
be_to_native32(&head->end); |
| 77 |
be_to_native32(&head->len); |
| 78 |
be_to_native32(&head->unk2); |
| 79 |
be_to_native32(&head->unk3); |
| 80 |
be_to_native16(&head->unk4); |
| 81 |
be_to_native16(&head->unk5); |
| 82 |
be_to_native32(&head->uncompressed_len); |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
/** Convert private-config header values from big endian to native. */ |
| 87 |
static void be_to_native_header_private(struct fs_nvram_header_private_config *head) |
| 88 |
{ |
| 89 |
be_to_native16(&head->magic); |
| 90 |
be_to_native16(&head->format); |
| 91 |
be_to_native32(&head->start); |
| 92 |
be_to_native32(&head->end); |
| 93 |
be_to_native32(&head->len); |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
/** Convert a 16 bit value from native to big endian. */ |
| 99 |
static inline void native_to_be16(m_uint16_t *val) |
| 100 |
{ |
| 101 |
union { |
| 102 |
m_uint16_t val; |
| 103 |
m_uint8_t b[2]; |
| 104 |
} u; |
| 105 |
u.b[0] = (m_uint8_t)(*val >> 8); |
| 106 |
u.b[1] = (m_uint8_t)(*val & 0xFF); |
| 107 |
*val = u.val; |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
/** Convert a 32 bit value from native to big endian. */ |
| 112 |
static inline void native_to_be32(m_uint32_t *val) |
| 113 |
{ |
| 114 |
union { |
| 115 |
m_uint32_t val; |
| 116 |
m_uint8_t b[4]; |
| 117 |
} u; |
| 118 |
u.b[0] = (m_uint8_t)(*val >> 24); |
| 119 |
u.b[1] = (m_uint8_t)(*val >> 16); |
| 120 |
u.b[2] = (m_uint8_t)(*val >> 8); |
| 121 |
u.b[3] = (m_uint8_t)(*val & 0xFF); |
| 122 |
*val = u.val; |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
/** Convert startup-config header values from native to big endian. */ |
| 127 |
static void native_to_be_header_startup(struct fs_nvram_header_startup_config *head) |
| 128 |
{ |
| 129 |
native_to_be16(&head->magic); |
| 130 |
native_to_be16(&head->format); |
| 131 |
native_to_be16(&head->checksum); |
| 132 |
native_to_be16(&head->unk1); |
| 133 |
native_to_be32(&head->start); |
| 134 |
native_to_be32(&head->end); |
| 135 |
native_to_be32(&head->len); |
| 136 |
native_to_be32(&head->unk2); |
| 137 |
native_to_be32(&head->unk3); |
| 138 |
native_to_be16(&head->unk4); |
| 139 |
native_to_be16(&head->unk5); |
| 140 |
native_to_be32(&head->uncompressed_len); |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
/** Convert private-config header values from native to big endian. */ |
| 145 |
static void native_to_be_header_private(struct fs_nvram_header_private_config *head) |
| 146 |
{ |
| 147 |
native_to_be16(&head->magic); |
| 148 |
native_to_be16(&head->format); |
| 149 |
native_to_be32(&head->start); |
| 150 |
native_to_be32(&head->end); |
| 151 |
native_to_be32(&head->len); |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
/** Uncompress data in .Z file format. |
| 156 |
* Adapted from 7zip's ZDecoder.cpp, which is licensed under LGPL 2.1. |
| 157 |
*/ |
| 158 |
static int uncompress_LZC(const u_char *in_data, u_int in_len, u_char *out_data, u_int out_len) |
| 159 |
{ |
| 160 |
#define LZC_MAGIC_1 0x1F |
| 161 |
#define LZC_MAGIC_2 0x9D |
| 162 |
#define LZC_NUM_BITS_MASK 0x1F |
| 163 |
#define LZC_BLOCK_MODE_MASK 0x80 |
| 164 |
#define LZC_NUM_BITS_MIN 9 |
| 165 |
#define LZC_NUM_BITS_MAX 16 |
| 166 |
m_uint16_t *parents = NULL; |
| 167 |
m_uint8_t *suffixes = NULL; |
| 168 |
m_uint8_t *stack = NULL; |
| 169 |
int err; |
| 170 |
|
| 171 |
if (in_len < 3 || (in_data == NULL && in_len > 0) || (out_data == NULL && out_len > 0)) |
| 172 |
return EINVAL; // invalid argument |
| 173 |
|
| 174 |
if (in_data[0] != LZC_MAGIC_1 || in_data[1] != LZC_MAGIC_2) |
| 175 |
return ENOTSUP; // no magic |
| 176 |
|
| 177 |
int maxbits = in_data[2] & LZC_NUM_BITS_MASK; |
| 178 |
if (maxbits < LZC_NUM_BITS_MIN || maxbits > LZC_NUM_BITS_MAX) |
| 179 |
return ENOTSUP; // maxbits not supported |
| 180 |
|
| 181 |
m_uint32_t numItems = 1 << maxbits; |
| 182 |
m_uint8_t blockMode = ((in_data[2] & LZC_BLOCK_MODE_MASK) != 0); |
| 183 |
|
| 184 |
parents = (m_uint16_t *)malloc(numItems * sizeof(*parents)); if (parents == NULL) goto err_memory; |
| 185 |
suffixes = (m_uint8_t *)malloc(numItems * sizeof(*suffixes)); if (suffixes == NULL) goto err_memory; |
| 186 |
stack = (m_uint8_t *)malloc(numItems * sizeof(*stack)); if (stack == NULL) goto err_memory; |
| 187 |
|
| 188 |
u_int in_pos = 3; |
| 189 |
u_int out_pos = 0; |
| 190 |
int numBits = LZC_NUM_BITS_MIN; |
| 191 |
m_uint32_t head = blockMode ? 257 : 256; |
| 192 |
|
| 193 |
m_uint8_t needPrev = 0; |
| 194 |
|
| 195 |
u_int bitPos = 0; |
| 196 |
u_int numBufBits = 0; |
| 197 |
|
| 198 |
u_char buf[LZC_NUM_BITS_MAX + 4]; |
| 199 |
|
| 200 |
parents[256] = 0; |
| 201 |
suffixes[256] = 0; |
| 202 |
|
| 203 |
for (;;) |
| 204 |
{ |
| 205 |
if (numBufBits == bitPos) |
| 206 |
{ |
| 207 |
u_int len = m_min(in_len - in_pos, numBits); |
| 208 |
memcpy(buf, in_data + in_pos, len); |
| 209 |
numBufBits = len << 3; |
| 210 |
bitPos = 0; |
| 211 |
in_pos += len; |
| 212 |
} |
| 213 |
u_int bytePos = bitPos >> 3; |
| 214 |
m_uint32_t symbol = buf[bytePos] | ((m_uint32_t)buf[bytePos + 1] << 8) | ((m_uint32_t)buf[bytePos + 2] << 16); |
| 215 |
symbol >>= (bitPos & 7); |
| 216 |
symbol &= (1 << numBits) - 1; |
| 217 |
bitPos += numBits; |
| 218 |
if (bitPos > numBufBits) |
| 219 |
break; |
| 220 |
if (symbol >= head) |
| 221 |
goto err_data; |
| 222 |
if (blockMode && symbol == 256) |
| 223 |
{ |
| 224 |
numBufBits = bitPos = 0; |
| 225 |
numBits = LZC_NUM_BITS_MIN; |
| 226 |
head = 257; |
| 227 |
needPrev = 0; |
| 228 |
continue; |
| 229 |
} |
| 230 |
m_uint32_t cur = symbol; |
| 231 |
int i = 0; |
| 232 |
while (cur >= 256) |
| 233 |
{ |
| 234 |
stack[i++] = suffixes[cur]; |
| 235 |
cur = parents[cur]; |
| 236 |
} |
| 237 |
stack[i++] = (u_char)cur; |
| 238 |
if (needPrev) |
| 239 |
{ |
| 240 |
suffixes[head - 1] = (u_char)cur; |
| 241 |
if (symbol == head - 1) |
| 242 |
stack[0] = (u_char)cur; |
| 243 |
} |
| 244 |
do { |
| 245 |
if (out_pos < out_len) |
| 246 |
out_data[out_pos++] = stack[--i]; |
| 247 |
else |
| 248 |
i = 0; |
| 249 |
} while (i > 0); |
| 250 |
if (head < numItems) |
| 251 |
{ |
| 252 |
needPrev = 1; |
| 253 |
parents[head++] = (m_uint16_t)symbol; |
| 254 |
if (head > ((m_uint32_t)1 << numBits)) |
| 255 |
{ |
| 256 |
if (numBits < maxbits) |
| 257 |
{ |
| 258 |
numBufBits = bitPos = 0; |
| 259 |
numBits++; |
| 260 |
} |
| 261 |
} |
| 262 |
} |
| 263 |
else { |
| 264 |
needPrev = 0; |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
free(parents); |
| 269 |
free(suffixes); |
| 270 |
free(stack); |
| 271 |
return 0; |
| 272 |
err_memory: |
| 273 |
err = ENOMEM; // out of memory |
| 274 |
goto err_end; |
| 275 |
err_data: |
| 276 |
err = -1;//EIO; // invalid data |
| 277 |
goto err_end; |
| 278 |
err_end: |
| 279 |
if (parents) |
| 280 |
free(parents); |
| 281 |
|
| 282 |
if (suffixes) |
| 283 |
free(suffixes); |
| 284 |
|
| 285 |
if (stack) |
| 286 |
free(stack); |
| 287 |
|
| 288 |
return err; |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
//========================================================= |
| 294 |
// Private |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
/** Retuns address of the specified filesystem offset */ |
| 299 |
static inline m_uint32_t fs_nvram_address_of(fs_nvram_t *fs, m_uint32_t offset) |
| 300 |
{ |
| 301 |
if ((fs->flags & FS_NVRAM_FLAG_ADDR_ABSOLUTE)) |
| 302 |
return(fs->addr + offset); |
| 303 |
else |
| 304 |
return(offset - 8); |
| 305 |
} |
| 306 |
|
| 307 |
|
| 308 |
/** Retuns filesystem offset of the specified address */ |
| 309 |
static inline m_uint32_t fs_nvram_offset_of(fs_nvram_t *fs, m_uint32_t address) |
| 310 |
{ |
| 311 |
if ((fs->flags & FS_NVRAM_FLAG_ADDR_ABSOLUTE)) |
| 312 |
return(address - fs->addr); |
| 313 |
else |
| 314 |
return(address + 8); |
| 315 |
} |
| 316 |
|
| 317 |
|
| 318 |
/** Retuns padding at the specified offset */ |
| 319 |
static inline u_int fs_nvram_padding_at(fs_nvram_t *fs, u_int offset) |
| 320 |
{ |
| 321 |
u_int padding = 0; |
| 322 |
|
| 323 |
if (offset % 4 != 0) |
| 324 |
padding = fs->padding - offset % 4; |
| 325 |
|
| 326 |
return padding; |
| 327 |
} |
| 328 |
|
| 329 |
|
| 330 |
/** Read a 16-bit value from NVRAM. */ |
| 331 |
static inline m_uint16_t fs_nvram_read16(fs_nvram_t *fs,u_int offset) |
| 332 |
{ |
| 333 |
m_uint16_t val; |
| 334 |
val = fs->read_byte(fs,offset) << 8; |
| 335 |
val |= fs->read_byte(fs,offset+1); |
| 336 |
return(val); |
| 337 |
} |
| 338 |
|
| 339 |
/** Write a 16-bit value to NVRAM. */ |
| 340 |
static void fs_nvram_write16(fs_nvram_t *fs,u_int offset,m_uint16_t val) |
| 341 |
{ |
| 342 |
fs->write_byte(fs,offset,val >> 8); |
| 343 |
fs->write_byte(fs,offset+1,val & 0xFF); |
| 344 |
} |
| 345 |
|
| 346 |
|
| 347 |
/** Read a 32-bit value from NVRAM. */ |
| 348 |
static m_uint32_t fs_nvram_read32(fs_nvram_t *fs,u_int offset) |
| 349 |
{ |
| 350 |
m_uint32_t val; |
| 351 |
val = fs->read_byte(fs,offset) << 24; |
| 352 |
val |= fs->read_byte(fs,offset+1) << 16; |
| 353 |
val |= fs->read_byte(fs,offset+2) << 8; |
| 354 |
val |= fs->read_byte(fs,offset+3); |
| 355 |
return(val); |
| 356 |
} |
| 357 |
|
| 358 |
|
| 359 |
/** Write a 32-bit value to NVRAM. */ |
| 360 |
_Unused static void fs_nvram_write32(fs_nvram_t *fs,u_int offset,m_uint32_t val) |
| 361 |
{ |
| 362 |
fs->write_byte(fs,offset,val >> 24); |
| 363 |
fs->write_byte(fs,offset+1,val >> 16); |
| 364 |
fs->write_byte(fs,offset+2,val >> 8); |
| 365 |
fs->write_byte(fs,offset+3,val & 0xFF); |
| 366 |
} |
| 367 |
|
| 368 |
|
| 369 |
/** Read a buffer from NVRAM. */ |
| 370 |
static void fs_nvram_memcpy_from(fs_nvram_t *fs, u_int offset, u_char *data, u_int len) |
| 371 |
{ |
| 372 |
u_int i; |
| 373 |
|
| 374 |
for (i = 0; i < len; i++) { |
| 375 |
*data = fs->read_byte(fs, offset + i); |
| 376 |
data++; |
| 377 |
} |
| 378 |
} |
| 379 |
|
| 380 |
|
| 381 |
/** Write a buffer to NVRAM. */ |
| 382 |
static void fs_nvram_memcpy_to(fs_nvram_t *fs, u_int offset, const u_char *data, u_int len) |
| 383 |
{ |
| 384 |
u_int i; |
| 385 |
|
| 386 |
for (i = 0; i < len; i++) { |
| 387 |
fs->write_byte(fs, offset + i, *data); |
| 388 |
data++; |
| 389 |
} |
| 390 |
} |
| 391 |
|
| 392 |
|
| 393 |
/** Clear section of NVRAM. */ |
| 394 |
static void fs_nvram_clear(fs_nvram_t *fs, u_int offset, u_int len) |
| 395 |
{ |
| 396 |
u_int i; |
| 397 |
|
| 398 |
for (i = 0; i < len; i++) { |
| 399 |
fs->write_byte(fs, offset + i, 0); |
| 400 |
} |
| 401 |
} |
| 402 |
|
| 403 |
|
| 404 |
/** Update the filesystem checksum. */ |
| 405 |
static void fs_nvram_update_checksum(fs_nvram_t *fs) |
| 406 |
{ |
| 407 |
u_int offset, count; |
| 408 |
m_uint32_t sum = 0; |
| 409 |
|
| 410 |
fs_nvram_write16(fs, sizeof(struct fs_nvram_header) + offsetof(struct fs_nvram_header_startup_config, checksum), 0x0000); |
| 411 |
|
| 412 |
offset = sizeof(struct fs_nvram_header); |
| 413 |
count = fs->len - offset; |
| 414 |
while (count > 1) { |
| 415 |
sum = sum + fs_nvram_read16(fs, offset); |
| 416 |
offset += 2; |
| 417 |
count -= sizeof(m_uint16_t); |
| 418 |
} |
| 419 |
|
| 420 |
if (count > 0) |
| 421 |
sum = sum + ((fs->read_byte(fs, offset)) << 8); |
| 422 |
|
| 423 |
while (sum >> 16) |
| 424 |
sum = (sum & 0xffff) + (sum >> 16); |
| 425 |
|
| 426 |
sum = ~sum; |
| 427 |
|
| 428 |
fs_nvram_write16(fs, sizeof(struct fs_nvram_header) + offsetof(struct fs_nvram_header_startup_config, checksum), sum); |
| 429 |
} |
| 430 |
|
| 431 |
|
| 432 |
/** Read data from NVRAM.*/ |
| 433 |
static inline u_char *fs_nvram_read_data(fs_nvram_t *fs, u_int offset, u_int len) |
| 434 |
{ |
| 435 |
u_char *data; |
| 436 |
|
| 437 |
data = (u_char *)malloc(len + 1); |
| 438 |
if (data == NULL) |
| 439 |
return NULL; // out of memory |
| 440 |
|
| 441 |
fs_nvram_memcpy_from(fs, offset, data, len); |
| 442 |
data[len] = 0; |
| 443 |
|
| 444 |
return data; |
| 445 |
} |
| 446 |
|
| 447 |
|
| 448 |
/** Create a NVRAM filesystem. */ |
| 449 |
static void fs_nvram_create(fs_nvram_t *fs) |
| 450 |
{ |
| 451 |
fs_nvram_clear(fs, 0, fs->len); |
| 452 |
fs_nvram_write16(fs, offsetof(struct fs_nvram_header, magic), FS_NVRAM_MAGIC_FILESYSTEM); |
| 453 |
fs_nvram_write16(fs, sizeof(struct fs_nvram_header) + offsetof(struct fs_nvram_header_startup_config, checksum), 0xFFFF); |
| 454 |
} |
| 455 |
|
| 456 |
|
| 457 |
/** Read a byte from the NVRAM filesystem. */ |
| 458 |
static m_uint8_t fs_nvram_read_byte(fs_nvram_t *fs, u_int offset) |
| 459 |
{ |
| 460 |
m_uint8_t *ptr; |
| 461 |
|
| 462 |
ptr = (m_uint8_t *)fs->base + (offset << fs->shift); |
| 463 |
return(*ptr); |
| 464 |
} |
| 465 |
|
| 466 |
|
| 467 |
/** Write a byte to the NVRAM filesystem. */ |
| 468 |
static void fs_nvram_write_byte(fs_nvram_t *fs, u_int offset, m_uint8_t val) |
| 469 |
{ |
| 470 |
m_uint8_t *ptr; |
| 471 |
|
| 472 |
ptr = (m_uint8_t *)fs->base + (offset << fs->shift); |
| 473 |
*ptr = val; |
| 474 |
} |
| 475 |
|
| 476 |
|
| 477 |
/** Returns the normal offset of the NVRAM filesystem with backup. */ |
| 478 |
static inline u_int fs_nvram_offset1_with_backup(fs_nvram_t *fs, u_int offset) |
| 479 |
{ |
| 480 |
if (offset < FS_NVRAM_NORMAL_FILESYSTEM_BLOCK1) |
| 481 |
return(offset << fs->shift); |
| 482 |
else |
| 483 |
return((FS_NVRAM_BACKUP_FILESYSTEM_BLOCK1 + offset) << fs->shift); |
| 484 |
} |
| 485 |
|
| 486 |
|
| 487 |
/** Returns the backup offset of the NVRAM filesystem with backup. */ |
| 488 |
static inline u_int fs_nvram_offset2_with_backup(fs_nvram_t *fs, u_int offset) |
| 489 |
{ |
| 490 |
if (offset < FS_NVRAM_BACKUP_FILESYSTEM_BLOCK1) |
| 491 |
return((fs->backup + offset) << fs->shift); |
| 492 |
else |
| 493 |
return((fs->len + offset) << fs->shift); |
| 494 |
} |
| 495 |
|
| 496 |
|
| 497 |
/** Read a byte from the NVRAM filesystem with backup. */ |
| 498 |
static m_uint8_t fs_nvram_read_byte_with_backup(fs_nvram_t *fs, u_int offset) |
| 499 |
{ |
| 500 |
m_uint8_t *ptr1; |
| 501 |
|
| 502 |
ptr1 = (m_uint8_t *)fs->base + fs_nvram_offset1_with_backup(fs, offset); |
| 503 |
#if DEBUG_BACKUP |
| 504 |
{ |
| 505 |
m_uint8_t *ptr2 = (m_uint8_t *)fs->base + fs_nvram_offset2_with_backup(fs, offset); |
| 506 |
if (*ptr1 != *ptr2) |
| 507 |
fprintf(stderr, "fs_nvram_read_byte_with_backup: data in backup filesystem doesn't match (offset=%u, offset1=%u, offset2=%u, normal=0x%02X, backup=0x%02X)\n", |
| 508 |
offset, fs_nvram_offset1_with_backup(fs, offset), fs_nvram_offset2_with_backup(fs, offset), *ptr1, *ptr2); |
| 509 |
} |
| 510 |
#endif |
| 511 |
|
| 512 |
return(*ptr1); |
| 513 |
} |
| 514 |
|
| 515 |
|
| 516 |
/** Write a byte to the NVRAM filesystem with backup. */ |
| 517 |
static void fs_nvram_write_byte_with_backup(fs_nvram_t *fs, u_int offset, m_uint8_t val) |
| 518 |
{ |
| 519 |
m_uint8_t *ptr1; |
| 520 |
m_uint8_t *ptr2; |
| 521 |
|
| 522 |
ptr1 = (m_uint8_t *)fs->base + fs_nvram_offset1_with_backup(fs, offset); |
| 523 |
ptr2 = (m_uint8_t *)fs->base + fs_nvram_offset2_with_backup(fs, offset); |
| 524 |
|
| 525 |
*ptr1 = val; |
| 526 |
*ptr2 = val; |
| 527 |
} |
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
//========================================================= |
| 532 |
// Public |
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
/** Open NVRAM filesystem. Sets errno. */ |
| 537 |
fs_nvram_t *fs_nvram_open(u_char *base, size_t len, m_uint32_t addr, u_int flags) |
| 538 |
{ |
| 539 |
struct fs_nvram *fs; |
| 540 |
size_t len_div = 1; |
| 541 |
|
| 542 |
if ((flags & FS_NVRAM_FLAG_SCALE_4)) |
| 543 |
len_div *= 4; // a quarter of the size |
| 544 |
|
| 545 |
if ((flags & FS_NVRAM_FLAG_WITH_BACKUP)) |
| 546 |
len_div *= 2; // half the size is for the backup |
| 547 |
|
| 548 |
if (base == NULL || len < sizeof(struct fs_nvram_header) * len_div || len % (FS_NVRAM_SECTOR_SIZE * len_div) != 0) { |
| 549 |
errno = EINVAL; |
| 550 |
return NULL; // invalid argument |
| 551 |
} |
| 552 |
|
| 553 |
fs = (struct fs_nvram*)malloc(sizeof(*fs)); |
| 554 |
if (fs == NULL) { |
| 555 |
errno = ENOMEM; |
| 556 |
return NULL; // out of memory |
| 557 |
} |
| 558 |
|
| 559 |
fs->base = base; |
| 560 |
fs->len = len / len_div; |
| 561 |
fs->addr = addr; |
| 562 |
fs->flags = flags; |
| 563 |
fs->shift = (flags & FS_NVRAM_FLAG_SCALE_4) ? 2 : 0; |
| 564 |
fs->padding = (flags & FS_NVRAM_FLAG_ALIGN_4_PAD_4) ? 4 : 8; |
| 565 |
fs->backup = (flags & FS_NVRAM_FLAG_WITH_BACKUP) ? m_min(fs->len, FS_NVRAM_NORMAL_FILESYSTEM_BLOCK1) : 0; |
| 566 |
fs->read_byte = (flags & FS_NVRAM_FLAG_WITH_BACKUP) ? fs_nvram_read_byte_with_backup : fs_nvram_read_byte; |
| 567 |
fs->write_byte = (flags & FS_NVRAM_FLAG_WITH_BACKUP) ? fs_nvram_write_byte_with_backup : fs_nvram_write_byte; |
| 568 |
|
| 569 |
if (FS_NVRAM_MAGIC_FILESYSTEM != fs_nvram_read16(fs, offsetof(struct fs_nvram_header, magic))) { |
| 570 |
if (!(flags & FS_NVRAM_FLAG_OPEN_CREATE)) { |
| 571 |
fs_nvram_close(fs); |
| 572 |
errno = FS_NVRAM_ERR_NO_MAGIC; |
| 573 |
return NULL; // no magic |
| 574 |
} |
| 575 |
|
| 576 |
fs_nvram_create(fs); |
| 577 |
} |
| 578 |
|
| 579 |
errno = 0; |
| 580 |
return fs; |
| 581 |
} |
| 582 |
|
| 583 |
|
| 584 |
/** Close NVRAM filesystem. */ |
| 585 |
void fs_nvram_close(fs_nvram_t *fs) |
| 586 |
{ |
| 587 |
if (fs) |
| 588 |
free(fs); |
| 589 |
} |
| 590 |
|
| 591 |
|
| 592 |
/** Read startup-config and/or private-config from NVRAM. |
| 593 |
* Returns 0 on success. |
| 594 |
*/ |
| 595 |
int fs_nvram_read_config(fs_nvram_t *fs, u_char **startup_config, size_t *startup_len, u_char **private_config, size_t *private_len) |
| 596 |
{ |
| 597 |
int err; |
| 598 |
size_t off; |
| 599 |
u_char *buf; |
| 600 |
struct fs_nvram_header_startup_config startup_head; |
| 601 |
struct fs_nvram_header_private_config private_head; |
| 602 |
|
| 603 |
if (fs == NULL) |
| 604 |
return(EINVAL); // invalid argument |
| 605 |
|
| 606 |
// initial values |
| 607 |
if (startup_config) |
| 608 |
*startup_config = NULL; |
| 609 |
|
| 610 |
if (startup_len) |
| 611 |
*startup_len = 0; |
| 612 |
|
| 613 |
if (private_config) |
| 614 |
*private_config = NULL; |
| 615 |
|
| 616 |
if (private_len) |
| 617 |
*private_len = 0; |
| 618 |
|
| 619 |
// read headers |
| 620 |
off = sizeof(struct fs_nvram_header); |
| 621 |
fs_nvram_memcpy_from(fs, off, (u_char *)&startup_head, sizeof(startup_head)); |
| 622 |
be_to_native_header_startup(&startup_head); |
| 623 |
if (FS_NVRAM_MAGIC_STARTUP_CONFIG != startup_head.magic) |
| 624 |
return(0); // done, no startup-config and no private-config |
| 625 |
|
| 626 |
off = fs_nvram_offset_of(fs, startup_head.start + startup_head.len); |
| 627 |
off += fs_nvram_padding_at(fs, off); |
| 628 |
|
| 629 |
if (off + sizeof(private_head) > fs->len) |
| 630 |
goto err_memory; |
| 631 |
|
| 632 |
fs_nvram_memcpy_from(fs, off, (u_char *)&private_head, sizeof(private_head)); |
| 633 |
be_to_native_header_private(&private_head); |
| 634 |
|
| 635 |
// read startup-config |
| 636 |
if (FS_NVRAM_FORMAT_RAW == startup_head.format) { |
| 637 |
if (startup_config) { |
| 638 |
off = fs_nvram_offset_of(fs, startup_head.start); |
| 639 |
*startup_config = fs_nvram_read_data(fs, off, startup_head.len); |
| 640 |
if (*startup_config == NULL) |
| 641 |
goto err_memory; |
| 642 |
} |
| 643 |
|
| 644 |
if (startup_len) |
| 645 |
*startup_len = startup_head.len; |
| 646 |
} |
| 647 |
else if (FS_NVRAM_FORMAT_LZC == startup_head.format) { |
| 648 |
if (startup_config) { |
| 649 |
off = fs_nvram_offset_of(fs, startup_head.start); |
| 650 |
*startup_config = (u_char *)malloc(startup_head.uncompressed_len + 1); |
| 651 |
if (*startup_config == NULL) |
| 652 |
goto err_memory; |
| 653 |
|
| 654 |
buf = fs_nvram_read_data(fs, off, startup_head.len); |
| 655 |
if (buf == NULL) |
| 656 |
goto err_memory; |
| 657 |
|
| 658 |
err = uncompress_LZC(buf, startup_head.len, *startup_config, startup_head.uncompressed_len); |
| 659 |
if (err) |
| 660 |
goto err_uncompress; |
| 661 |
|
| 662 |
(*startup_config)[startup_head.uncompressed_len] = 0; |
| 663 |
free(buf); |
| 664 |
} |
| 665 |
|
| 666 |
if (startup_len) |
| 667 |
*startup_len = startup_head.uncompressed_len; |
| 668 |
} |
| 669 |
else { |
| 670 |
goto err_format; |
| 671 |
} |
| 672 |
|
| 673 |
// read private-config |
| 674 |
if (fs_nvram_offset_of(fs, private_head.start + private_head.len) > fs->len || FS_NVRAM_MAGIC_PRIVATE_CONFIG != private_head.magic) |
| 675 |
return(0); // done, no private-config |
| 676 |
|
| 677 |
if (FS_NVRAM_FORMAT_RAW == private_head.format) { |
| 678 |
if (private_config) { |
| 679 |
off = fs_nvram_offset_of(fs, private_head.start); |
| 680 |
*private_config = fs_nvram_read_data(fs, off, private_head.len); |
| 681 |
if (*private_config == NULL) |
| 682 |
goto err_memory; |
| 683 |
} |
| 684 |
|
| 685 |
if (private_len) |
| 686 |
*private_len = private_head.len; |
| 687 |
} |
| 688 |
else { |
| 689 |
goto err_format; |
| 690 |
} |
| 691 |
|
| 692 |
return(0); // done |
| 693 |
err_memory: |
| 694 |
err = ENOMEM; // out of memory |
| 695 |
goto err_end; |
| 696 |
err_format: |
| 697 |
err = ENOTSUP; // unsupported format |
| 698 |
goto err_end; |
| 699 |
err_uncompress: |
| 700 |
free(buf); |
| 701 |
goto err_end; |
| 702 |
err_end: |
| 703 |
if (startup_config && *startup_config) { |
| 704 |
free(*startup_config); |
| 705 |
*startup_config = NULL; |
| 706 |
} |
| 707 |
|
| 708 |
if (startup_len) |
| 709 |
*startup_len = 0; |
| 710 |
|
| 711 |
if (private_config && *private_config) { |
| 712 |
free(*private_config); |
| 713 |
*private_config = NULL; |
| 714 |
} |
| 715 |
|
| 716 |
if (private_len) |
| 717 |
*private_len = 0; |
| 718 |
|
| 719 |
return(err); |
| 720 |
} |
| 721 |
|
| 722 |
|
| 723 |
/** Write startup-config and private-config to NVRAM. |
| 724 |
* Returns 0 on success. |
| 725 |
*/ |
| 726 |
int fs_nvram_write_config(fs_nvram_t *fs, const u_char *startup_config, size_t startup_len, const u_char *private_config, size_t private_len) |
| 727 |
{ |
| 728 |
size_t len; |
| 729 |
size_t off; |
| 730 |
size_t padding; |
| 731 |
struct fs_nvram_header_startup_config startup_head; |
| 732 |
struct fs_nvram_header_private_config private_head; |
| 733 |
|
| 734 |
if (fs == NULL || (startup_config == NULL && startup_len > 0) || (private_config == NULL && private_len > 0)) |
| 735 |
return(EINVAL); // invalid argument |
| 736 |
|
| 737 |
// check space and padding |
| 738 |
// XXX ignores normal files in NVRAM |
| 739 |
len = sizeof(struct fs_nvram_header) + sizeof(struct fs_nvram_header_startup_config) + startup_len; |
| 740 |
padding = fs_nvram_padding_at(fs, len); |
| 741 |
len += padding + sizeof(struct fs_nvram_header_private_config) + private_len; |
| 742 |
if (fs->len < len) |
| 743 |
return(ENOSPC); // not enough space |
| 744 |
|
| 745 |
// old length |
| 746 |
len = sizeof(struct fs_nvram_header); |
| 747 |
if (FS_NVRAM_MAGIC_STARTUP_CONFIG == fs_nvram_read16(fs, len + offsetof(typeof(startup_head), magic))) { |
| 748 |
len += fs_nvram_read32(fs, len + offsetof(typeof(startup_head), len)); |
| 749 |
if (len % 4 != 0) |
| 750 |
len += 8 - len % 4; |
| 751 |
|
| 752 |
if (FS_NVRAM_MAGIC_PRIVATE_CONFIG == fs_nvram_read16(fs, len + offsetof(typeof(private_head), magic))) |
| 753 |
len += fs_nvram_read32(fs, len + offsetof(typeof(private_head), len)); |
| 754 |
} |
| 755 |
|
| 756 |
if (len % FS_NVRAM_SECTOR_SIZE != 0) |
| 757 |
len += FS_NVRAM_SECTOR_SIZE - len % FS_NVRAM_SECTOR_SIZE; // whole sector |
| 758 |
|
| 759 |
if (len > fs->len) |
| 760 |
len = fs->len; // should never happen |
| 761 |
|
| 762 |
// prepare headers |
| 763 |
memset(&startup_head, 0, sizeof(startup_head)); |
| 764 |
startup_head.magic = FS_NVRAM_MAGIC_STARTUP_CONFIG; |
| 765 |
startup_head.format = FS_NVRAM_FORMAT_RAW; |
| 766 |
startup_head.unk1 = (fs->flags & FS_NVRAM_FLAGS_UNK1_0C01) ? 0x0C01 : (fs->flags & FS_NVRAM_FLAGS_UNK1_0C03) ? 0x0C03 : 0x0C04; |
| 767 |
startup_head.start = fs_nvram_address_of(fs, sizeof(struct fs_nvram_header) + sizeof(struct fs_nvram_header_startup_config)); |
| 768 |
startup_head.end = startup_head.start + startup_len; |
| 769 |
startup_head.len = startup_len; |
| 770 |
|
| 771 |
memset(&private_head, 0, sizeof(private_head)); |
| 772 |
private_head.magic = FS_NVRAM_MAGIC_PRIVATE_CONFIG; |
| 773 |
private_head.format = FS_NVRAM_FORMAT_RAW; |
| 774 |
private_head.start = startup_head.end + padding + sizeof(struct fs_nvram_header_private_config); |
| 775 |
private_head.end = private_head.start + private_len; |
| 776 |
private_head.len = private_len; |
| 777 |
|
| 778 |
native_to_be_header_startup(&startup_head); |
| 779 |
native_to_be_header_private(&private_head); |
| 780 |
|
| 781 |
// write data |
| 782 |
off = sizeof(struct fs_nvram_header); |
| 783 |
|
| 784 |
fs_nvram_memcpy_to(fs, off, (const u_char *)&startup_head, sizeof(struct fs_nvram_header_startup_config)); |
| 785 |
off += sizeof(struct fs_nvram_header_startup_config); |
| 786 |
fs_nvram_memcpy_to(fs, off, startup_config, startup_len); |
| 787 |
off += startup_len; |
| 788 |
|
| 789 |
fs_nvram_clear(fs, off, padding); |
| 790 |
off += padding; |
| 791 |
|
| 792 |
fs_nvram_memcpy_to(fs, off, (const u_char *)&private_head, sizeof(struct fs_nvram_header_private_config)); |
| 793 |
off += sizeof(struct fs_nvram_header_private_config); |
| 794 |
fs_nvram_memcpy_to(fs, off, private_config, private_len); |
| 795 |
off += private_len; |
| 796 |
|
| 797 |
if (off < len) |
| 798 |
fs_nvram_clear(fs, off, len - off); |
| 799 |
|
| 800 |
fs_nvram_update_checksum(fs); |
| 801 |
|
| 802 |
return(0); |
| 803 |
} |
| 804 |
|
| 805 |
|
| 806 |
/** Returns the number of sectors in the NVRAM filesystem. */ |
| 807 |
size_t fs_nvram_num_sectors(fs_nvram_t *fs) |
| 808 |
{ |
| 809 |
if (fs == NULL) |
| 810 |
return(0); |
| 811 |
|
| 812 |
return( fs->len / FS_NVRAM_SECTOR_SIZE ); |
| 813 |
} |
| 814 |
|
| 815 |
|
| 816 |
/** Verify the contents of the filesystem. |
| 817 |
* Returns 0 on success. |
| 818 |
*/ |
| 819 |
int fs_nvram_verify(fs_nvram_t *fs, u_int what) |
| 820 |
{ |
| 821 |
size_t offset; |
| 822 |
|
| 823 |
if (fs == NULL) |
| 824 |
return(EINVAL); // invalid argument |
| 825 |
|
| 826 |
if ((what & FS_NVRAM_VERIFY_BACKUP)) { |
| 827 |
if ((fs->flags & FS_NVRAM_FLAG_WITH_BACKUP)) { |
| 828 |
for (offset = 0; offset < fs->len; offset++) { |
| 829 |
m_uint8_t b1 = fs->base[fs_nvram_offset1_with_backup(fs, offset)]; |
| 830 |
m_uint8_t b2 = fs->base[fs_nvram_offset2_with_backup(fs, offset)]; |
| 831 |
if (b1 != b2) |
| 832 |
return(FS_NVRAM_ERR_BACKUP_MISSMATCH); // data is corrupted? length is wrong? |
| 833 |
} |
| 834 |
} |
| 835 |
} |
| 836 |
|
| 837 |
if ((what & FS_NVRAM_VERIFY_CONFIG)) { |
| 838 |
struct fs_nvram_header_startup_config startup_head; |
| 839 |
struct fs_nvram_header_private_config private_head; |
| 840 |
|
| 841 |
offset = sizeof(struct fs_nvram_header); |
| 842 |
fs_nvram_memcpy_from(fs, offset, (u_char *)&startup_head, sizeof(startup_head)); |
| 843 |
be_to_native_header_startup(&startup_head); |
| 844 |
if (FS_NVRAM_MAGIC_STARTUP_CONFIG == startup_head.magic) { |
| 845 |
if (startup_head.end != startup_head.start + startup_head.len || startup_head.len > fs->len) |
| 846 |
return(FS_NVRAM_ERR_INVALID_ADDRESS); // data is corrupted? |
| 847 |
if (startup_head.start < fs->addr || startup_head.end > fs->addr + fs->len) |
| 848 |
return(FS_NVRAM_ERR_INVALID_ADDRESS); // fs->addr has the wrong value? |
| 849 |
|
| 850 |
offset = fs_nvram_offset_of(fs, startup_head.end); |
| 851 |
offset += fs_nvram_padding_at(fs, offset); |
| 852 |
if (fs->len < offset + sizeof(private_head)) |
| 853 |
return(FS_NVRAM_ERR_INVALID_ADDRESS); // data is corrupted? |
| 854 |
|
| 855 |
fs_nvram_memcpy_from(fs, offset, (u_char *)&private_head, sizeof(private_head)); |
| 856 |
be_to_native_header_private(&private_head); |
| 857 |
if (FS_NVRAM_MAGIC_PRIVATE_CONFIG == private_head.magic) { |
| 858 |
if (private_head.end != private_head.start + private_head.len || private_head.len > fs->len) |
| 859 |
return(FS_NVRAM_ERR_INVALID_ADDRESS); // data is corrupted? |
| 860 |
if (private_head.start < fs->addr || private_head.end > fs->addr + fs->len) |
| 861 |
return(FS_NVRAM_ERR_INVALID_ADDRESS); // fs->addr has the wrong value? |
| 862 |
if (private_head.end != private_head.start + private_head.len) |
| 863 |
return(FS_NVRAM_ERR_INVALID_ADDRESS); // data is corrupted? |
| 864 |
} |
| 865 |
} |
| 866 |
} |
| 867 |
|
| 868 |
return(0); |
| 869 |
} |