| 1 |
/** @file |
| 2 |
* @brief Cisco NVRAM filesystem. |
| 3 |
* |
| 4 |
* Format was inferred by analysing the NVRAM data after changing/erasing stuff. |
| 5 |
* All data is big endian. |
| 6 |
* |
| 7 |
* Based on the platforms c1700/c2600/c2692/c3600/c3725/c3745/c7200/c6msfc1. |
| 8 |
*/ |
| 9 |
|
| 10 |
/* |
| 11 |
* Copyright (c) 2013 Flávio J. Saraiva <[email protected]> |
| 12 |
*/ |
| 13 |
|
| 14 |
#ifndef FS_NVRAM_H__ |
| 15 |
#define FS_NVRAM_H__ |
| 16 |
|
| 17 |
#include "utils.h" |
| 18 |
|
| 19 |
|
| 20 |
/////////////////////////////////////////////////////////// |
| 21 |
// Filesystem |
| 22 |
|
| 23 |
|
| 24 |
/** Size of a sector. */ |
| 25 |
#define FS_NVRAM_SECTOR_SIZE 0x400 |
| 26 |
|
| 27 |
/** Sector contains the start of the file .*/ |
| 28 |
#define FS_NVRAM_FLAG_FILE_START 0x01 |
| 29 |
|
| 30 |
/** Sector contains the end of the file. */ |
| 31 |
#define FS_NVRAM_FLAG_FILE_END 0x02 |
| 32 |
|
| 33 |
/** File does not have read or write permission. */ |
| 34 |
#define FS_NVRAM_FLAG_FILE_NO_RW |
| 35 |
|
| 36 |
#define FS_NVRAM_MAGIC_FILESYSTEM 0xF0A5 |
| 37 |
#define FS_NVRAM_MAGIC_STARTUP_CONFIG 0xABCD |
| 38 |
#define FS_NVRAM_MAGIC_PRIVATE_CONFIG 0xFEDC |
| 39 |
#define FS_NVRAM_MAGIC_FILE_SECTOR 0xDCBA |
| 40 |
|
| 41 |
/** Data is not compressed. */ |
| 42 |
#define FS_NVRAM_FORMAT_RAW 1 |
| 43 |
|
| 44 |
/** Data is compressed in .Z file format. */ |
| 45 |
#define FS_NVRAM_FORMAT_LZC 2 |
| 46 |
|
| 47 |
/** Magic not found - custom errno code. */ |
| 48 |
#define FS_NVRAM_ERR_NO_MAGIC ( - FS_NVRAM_MAGIC_FILESYSTEM ) |
| 49 |
|
| 50 |
/** Backup data doesn't match. */ |
| 51 |
#define FS_NVRAM_ERR_BACKUP_MISSMATCH ( FS_NVRAM_ERR_NO_MAGIC - 1 ) |
| 52 |
|
| 53 |
/** Invalid address found in filesystem. */ |
| 54 |
#define FS_NVRAM_ERR_INVALID_ADDRESS ( FS_NVRAM_ERR_NO_MAGIC - 2 ) |
| 55 |
|
| 56 |
/* Size of blocks in a NVRAM filesystem with backup (total size is 0x4C000 in c3745) */ |
| 57 |
#define FS_NVRAM_NORMAL_FILESYSTEM_BLOCK1 0x20000 |
| 58 |
#define FS_NVRAM_BACKUP_FILESYSTEM_BLOCK1 0x1C000 |
| 59 |
|
| 60 |
|
| 61 |
/////////////////////////////////////////////////////////// |
| 62 |
// Optional flags for open |
| 63 |
|
| 64 |
|
| 65 |
/** Create NVRAM filesystem if no magic. */ |
| 66 |
#define FS_NVRAM_FLAG_OPEN_CREATE 0x0001 |
| 67 |
|
| 68 |
/** Don't scale byte offsets. (default, ignored) */ |
| 69 |
#define FS_NVRAM_FLAG_NO_SCALE 0x0010 |
| 70 |
|
| 71 |
/** Scale byte offsets by 4. */ |
| 72 |
#define FS_NVRAM_FLAG_SCALE_4 0x0020 |
| 73 |
|
| 74 |
/** Align the private-config header to 4 bytes with a padding of 7/6/5/0 bytes. (default, ignored) */ |
| 75 |
#define FS_NVRAM_FLAG_ALIGN_4_PAD_8 0x0040 |
| 76 |
|
| 77 |
/** Align the private-config header to 4 bytes with a padding of 3/2/1/0 bytes. */ |
| 78 |
#define FS_NVRAM_FLAG_ALIGN_4_PAD_4 0x0080 |
| 79 |
|
| 80 |
/** Has a backup filesystem. |
| 81 |
* Data is not continuous: |
| 82 |
* up to 0x20000 bytes of the normal filesystem; |
| 83 |
* up to 0x1C000 bytes of the backup filesystem; |
| 84 |
* rest of normal filesystem; |
| 85 |
* rest of backup filesystem. |
| 86 |
*/ |
| 87 |
#define FS_NVRAM_FLAG_WITH_BACKUP 0x0100 |
| 88 |
|
| 89 |
/** Use addresses relative to the the end of the filesystem magic. (default, ignored) |
| 90 |
* Add 8 to get the raw offset. |
| 91 |
*/ |
| 92 |
#define FS_NVRAM_FLAG_ADDR_RELATIVE 0x0200 |
| 93 |
|
| 94 |
/** Use absolute addresses. |
| 95 |
* The base address of the filesystem is the addr argument. |
| 96 |
*/ |
| 97 |
#define FS_NVRAM_FLAG_ADDR_ABSOLUTE 0x0400 |
| 98 |
|
| 99 |
/** Value of unk1 is set to 0x0C04. (default, ignored) */ |
| 100 |
#define FS_NVRAM_FLAGS_UNK1_0C04 0x0800 |
| 101 |
|
| 102 |
/** Value of unk1 is set to 0x0C03. */ |
| 103 |
#define FS_NVRAM_FLAGS_UNK1_0C03 0x1000 |
| 104 |
|
| 105 |
/** Value of unk1 is set to 0x0C01. */ |
| 106 |
#define FS_NVRAM_FLAGS_UNK1_0C01 0x2000 |
| 107 |
|
| 108 |
#define FS_NVRAM_FORMAT_MASK 0x3FF0 |
| 109 |
|
| 110 |
|
| 111 |
/** Default filesystem format. (default, ignored) */ |
| 112 |
#define FS_NVRAM_FORMAT_DEFAULT ( FS_NVRAM_FLAG_NO_SCALE | FS_NVRAM_FLAG_ALIGN_4_PAD_8 | FS_NVRAM_FLAG_ADDR_RELATIVE | FS_NVRAM_FLAGS_UNK1_0C04 ) |
| 113 |
|
| 114 |
/** Filesystem format for the c2600 platform. */ |
| 115 |
#define FS_NVRAM_FORMAT_SCALE_4 ( FS_NVRAM_FLAG_SCALE_4 | FS_NVRAM_FLAG_ALIGN_4_PAD_8 | FS_NVRAM_FLAG_ADDR_RELATIVE | FS_NVRAM_FLAGS_UNK1_0C03 ) |
| 116 |
|
| 117 |
/** Filesystem format for the c3725/c3745 platforms. */ |
| 118 |
#define FS_NVRAM_FORMAT_WITH_BACKUP ( FS_NVRAM_FLAG_NO_SCALE | FS_NVRAM_FLAG_ALIGN_4_PAD_4 | FS_NVRAM_FLAG_ADDR_RELATIVE | FS_NVRAM_FLAGS_UNK1_0C04 | FS_NVRAM_FLAG_WITH_BACKUP ) |
| 119 |
|
| 120 |
/** Filesystem format for the c7000 platform. */ |
| 121 |
#define FS_NVRAM_FORMAT_ABSOLUTE ( FS_NVRAM_FLAG_NO_SCALE | FS_NVRAM_FLAG_ALIGN_4_PAD_4 | FS_NVRAM_FLAG_ADDR_ABSOLUTE | FS_NVRAM_FLAGS_UNK1_0C04 ) |
| 122 |
|
| 123 |
/** Filesystem format for the c6msfc1 platform. */ |
| 124 |
#define FS_NVRAM_FORMAT_ABSOLUTE_C6 ( FS_NVRAM_FLAG_NO_SCALE | FS_NVRAM_FLAG_ALIGN_4_PAD_4 | FS_NVRAM_FLAG_ADDR_ABSOLUTE | FS_NVRAM_FLAGS_UNK1_0C01 ) |
| 125 |
|
| 126 |
|
| 127 |
/////////////////////////////////////////////////////////// |
| 128 |
// Flags for verify |
| 129 |
|
| 130 |
/** Verify backup data. */ |
| 131 |
#define FS_NVRAM_VERIFY_BACKUP 0x01 |
| 132 |
|
| 133 |
/** Verify config data. */ |
| 134 |
#define FS_NVRAM_VERIFY_CONFIG 0x02 |
| 135 |
|
| 136 |
// TODO Verify file data. |
| 137 |
//#define FS_NVRAM_VERIFY_FILES 0x04 |
| 138 |
|
| 139 |
/** Verify everything. */ |
| 140 |
#define FS_NVRAM_VERIFY_ALL 0x07 |
| 141 |
|
| 142 |
|
| 143 |
/////////////////////////////////////////////////////////// |
| 144 |
|
| 145 |
|
| 146 |
/** Header of the NVRAM filesystem. |
| 147 |
* When empty, only this magic and the checksum are filled. |
| 148 |
* @see nvram_header_startup_config |
| 149 |
* @see nvram_header_private_config |
| 150 |
*/ |
| 151 |
struct fs_nvram_header { |
| 152 |
/** Padding. */ |
| 153 |
u_char padding[6]; |
| 154 |
|
| 155 |
/** Magic value 0xF0A5. */ |
| 156 |
m_uint16_t magic; |
| 157 |
// Following data: |
| 158 |
// - nvram_header_startup_config |
| 159 |
// - startup-config data |
| 160 |
// - padding to align the next header to a multiple of 4 |
| 161 |
// - nvram_header_private_config |
| 162 |
// - private-config data |
| 163 |
// - padding till end of sector |
| 164 |
// - the next 2 sectors are reserved for expansion of config files |
| 165 |
// - the rest of sectors are for normal files |
| 166 |
} __attribute__((__packed__)); |
| 167 |
|
| 168 |
|
| 169 |
/** Header of special file startup-config. |
| 170 |
* @see nvram_header |
| 171 |
*/ |
| 172 |
struct fs_nvram_header_startup_config { |
| 173 |
/** Magic value 0xABCD. */ |
| 174 |
m_uint16_t magic; |
| 175 |
|
| 176 |
/** Format of the data. |
| 177 |
* 0x0001 - raw data; |
| 178 |
* 0x0002 - .Z compressed (12 bits); |
| 179 |
*/ |
| 180 |
m_uint16_t format; |
| 181 |
|
| 182 |
/** Checksum of filesystem data. (all data after the filesystem magic) */ |
| 183 |
m_uint16_t checksum; |
| 184 |
|
| 185 |
/** 0x0C04 - maybe maximum amount of free space that will be reserved? */ |
| 186 |
m_uint16_t unk1; |
| 187 |
|
| 188 |
/** Address of the data. */ |
| 189 |
m_uint32_t start; |
| 190 |
|
| 191 |
/** Address right after the data. */ |
| 192 |
m_uint32_t end; |
| 193 |
|
| 194 |
/** Length of block. */ |
| 195 |
m_uint32_t len; |
| 196 |
|
| 197 |
/** 0x00000000 */ |
| 198 |
m_uint32_t unk2; |
| 199 |
|
| 200 |
/** 0x00000000 if raw data, 0x00000001 if compressed */ |
| 201 |
m_uint32_t unk3; |
| 202 |
|
| 203 |
/** 0x0000 if raw data, 0x0001 if compressed */ |
| 204 |
m_uint16_t unk4; |
| 205 |
|
| 206 |
/** 0x0000 */ |
| 207 |
m_uint16_t unk5; |
| 208 |
|
| 209 |
/** Length of uncompressed data, 0 if raw data. */ |
| 210 |
m_uint32_t uncompressed_len; |
| 211 |
|
| 212 |
// startup-config data comes after this header |
| 213 |
} __attribute__((__packed__)); |
| 214 |
|
| 215 |
|
| 216 |
/** Header of special file private-config. |
| 217 |
* @see nvram_header |
| 218 |
*/ |
| 219 |
struct fs_nvram_header_private_config { |
| 220 |
/** Magic value 0xFEDC. */ |
| 221 |
m_uint16_t magic; |
| 222 |
|
| 223 |
/** Format of the file. |
| 224 |
* 0x0001 - raw data; |
| 225 |
*/ |
| 226 |
m_uint16_t format; |
| 227 |
|
| 228 |
/** Address of the data. */ |
| 229 |
m_uint32_t start; |
| 230 |
|
| 231 |
/** Address right after the data. */ |
| 232 |
m_uint32_t end; |
| 233 |
|
| 234 |
/** Length of block. */ |
| 235 |
m_uint32_t len; |
| 236 |
|
| 237 |
// private-config data comes after this header |
| 238 |
} __attribute__((__packed__)); |
| 239 |
|
| 240 |
|
| 241 |
/** Sector containing file data. */ |
| 242 |
struct fs_nvram_file_sector { |
| 243 |
/** Magic value 0xDCBA */ |
| 244 |
m_uint16_t magic; |
| 245 |
|
| 246 |
/** Next sector with data, 0 by default */ |
| 247 |
m_uint16_t next_sector; |
| 248 |
|
| 249 |
/** Flags. |
| 250 |
* @see FS_NVRAM_FLAG_FILE_START |
| 251 |
* @see FS_NVRAM_FLAG_FILE_END |
| 252 |
* @see FS_NVRAM_FLAG_FILE_NO_RW |
| 253 |
*/ |
| 254 |
m_uint16_t flags; |
| 255 |
|
| 256 |
/** Amount of data in this sector. */ |
| 257 |
m_uint16_t length; |
| 258 |
|
| 259 |
/** File name, always NUL-terminated. */ |
| 260 |
char filename[24]; |
| 261 |
|
| 262 |
/** File data. */ |
| 263 |
u_char data[992]; |
| 264 |
} __attribute__((__packed__)); |
| 265 |
|
| 266 |
|
| 267 |
typedef struct fs_nvram fs_nvram_t; |
| 268 |
|
| 269 |
|
| 270 |
/* Functions */ |
| 271 |
fs_nvram_t *fs_nvram_open(u_char *base, size_t len, m_uint32_t addr, u_int flags); |
| 272 |
void fs_nvram_close(fs_nvram_t *fs); |
| 273 |
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); |
| 274 |
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); |
| 275 |
size_t fs_nvram_num_sectors(fs_nvram_t *fs); |
| 276 |
// TODO read/write file sectors |
| 277 |
int fs_nvram_verify(fs_nvram_t *fs, u_int what); |
| 278 |
|
| 279 |
#endif |