1 |
/* |
2 |
* Cisco C7200 (Predator) simulation platform. |
3 |
* Copyright (c) 2006 Christophe Fillot. All rights reserved. |
4 |
* |
5 |
* Cisco EEPROM manipulation functions. |
6 |
*/ |
7 |
|
8 |
#include <stdio.h> |
9 |
#include <stdlib.h> |
10 |
#include <string.h> |
11 |
#include <unistd.h> |
12 |
#include <sys/types.h> |
13 |
#include <sys/socket.h> |
14 |
|
15 |
#include "utils.h" |
16 |
#include "cisco_eeprom.h" |
17 |
|
18 |
/* Get a byte from an EEPROM */ |
19 |
int cisco_eeprom_get_byte(m_uint16_t *eeprom,size_t eeprom_len, |
20 |
size_t offset,m_uint8_t *val) |
21 |
{ |
22 |
m_uint16_t tmp; |
23 |
|
24 |
if (offset >= eeprom_len) |
25 |
return(-1); |
26 |
|
27 |
tmp = eeprom[offset >> 1]; |
28 |
|
29 |
if (!(offset & 1)) |
30 |
tmp >>= 8; |
31 |
|
32 |
*val = tmp & 0xFF; |
33 |
return(0); |
34 |
} |
35 |
|
36 |
/* Set a byte to an EEPROM */ |
37 |
int cisco_eeprom_set_byte(m_uint16_t *eeprom,size_t eeprom_len, |
38 |
size_t offset,m_uint8_t val) |
39 |
{ |
40 |
m_uint16_t tmp; |
41 |
|
42 |
if (offset >= eeprom_len) |
43 |
return(-1); |
44 |
|
45 |
tmp = eeprom[offset >> 1]; |
46 |
|
47 |
if (offset & 1) |
48 |
tmp = (tmp & 0xFF00) | val; |
49 |
else |
50 |
tmp = (tmp & 0x00FF) | (val << 8); |
51 |
|
52 |
eeprom[offset >> 1] = tmp; |
53 |
return(0); |
54 |
} |
55 |
|
56 |
/* Get an EEPROM region */ |
57 |
int cisco_eeprom_get_region(m_uint16_t *eeprom,size_t eeprom_len, |
58 |
size_t offset,m_uint8_t *data,size_t data_len) |
59 |
{ |
60 |
size_t i; |
61 |
|
62 |
for(i=0;i<data_len;i++) { |
63 |
if (cisco_eeprom_get_byte(eeprom,eeprom_len,offset+i,&data[i]) == -1) |
64 |
return(-1); |
65 |
} |
66 |
|
67 |
return(0); |
68 |
} |
69 |
|
70 |
/* Set an EEPROM region */ |
71 |
int cisco_eeprom_set_region(m_uint16_t *eeprom,size_t eeprom_len, |
72 |
size_t offset,m_uint8_t *data,size_t data_len) |
73 |
{ |
74 |
size_t i; |
75 |
|
76 |
for(i=0;i<data_len;i++) { |
77 |
if (cisco_eeprom_set_byte(eeprom,eeprom_len,offset+i,data[i]) == -1) |
78 |
return(-1); |
79 |
} |
80 |
|
81 |
return(0); |
82 |
} |
83 |
|
84 |
/* Get a field of a Cisco EEPROM v4 */ |
85 |
int cisco_eeprom_v4_get_field(m_uint16_t *eeprom,size_t eeprom_len, |
86 |
m_uint8_t *type,m_uint8_t *len,size_t *offset) |
87 |
{ |
88 |
m_uint8_t tmp; |
89 |
|
90 |
/* Read field type */ |
91 |
if (cisco_eeprom_get_byte(eeprom,eeprom_len,(*offset)++,type) == -1) |
92 |
return(-1); |
93 |
|
94 |
/* No more field */ |
95 |
if (*type == 0xFF) |
96 |
return(0); |
97 |
|
98 |
/* Get field length */ |
99 |
tmp = (*type >> 6) & 0x03; |
100 |
|
101 |
if (tmp == 0x03) { |
102 |
/* Variable len */ |
103 |
if (cisco_eeprom_get_byte(eeprom,eeprom_len,(*offset)++,&tmp) == -1) |
104 |
return(-1); |
105 |
|
106 |
*len = tmp & 0x0F; |
107 |
} else { |
108 |
/* Fixed len */ |
109 |
*len = 1 << tmp; |
110 |
} |
111 |
|
112 |
return(1); |
113 |
} |
114 |
|
115 |
/* Dump a Cisco EEPROM with format version 4 */ |
116 |
void cisco_eeprom_v4_dump(m_uint16_t *eeprom,size_t eeprom_len) |
117 |
{ |
118 |
m_uint8_t type,len,tmp; |
119 |
size_t i,offset=2; |
120 |
|
121 |
printf("Dumping EEPROM contents:\n"); |
122 |
|
123 |
do { |
124 |
/* Read field */ |
125 |
if (cisco_eeprom_v4_get_field(eeprom,eeprom_len,&type,&len,&offset) < 1) |
126 |
break; |
127 |
|
128 |
printf(" Field 0x%2.2x: ",type); |
129 |
|
130 |
for(i=0;i<len;i++) { |
131 |
if (cisco_eeprom_get_byte(eeprom,eeprom_len,offset+i,&tmp) == -1) |
132 |
break; |
133 |
|
134 |
printf("%2.2x ",tmp); |
135 |
} |
136 |
|
137 |
printf("\n"); |
138 |
|
139 |
offset += len; |
140 |
}while(offset < eeprom_len); |
141 |
} |
142 |
|
143 |
/* Returns the offset of the specified field */ |
144 |
int cisco_eeprom_v4_find_field(m_uint16_t *eeprom,size_t eeprom_len, |
145 |
m_uint8_t field_type,size_t *field_offset) |
146 |
{ |
147 |
m_uint8_t type,len; |
148 |
size_t offset=2; |
149 |
|
150 |
do { |
151 |
/* Read field */ |
152 |
if (cisco_eeprom_v4_get_field(eeprom,eeprom_len,&type,&len,&offset) < 1) |
153 |
break; |
154 |
|
155 |
if (type == field_type) { |
156 |
*field_offset = offset; |
157 |
return(0); |
158 |
} |
159 |
|
160 |
offset += len; |
161 |
}while(offset < eeprom_len); |
162 |
|
163 |
return(-1); |
164 |
} |