fwcrom.c revision 1.3.6.3 1 1.3.6.3 yamt /* $NetBSD: fwcrom.c,v 1.3.6.3 2007/09/03 14:35:25 yamt Exp $ */
2 1.3.6.2 yamt /*-
3 1.3.6.2 yamt * Copyright (c) 2002-2003
4 1.3.6.2 yamt * Hidetoshi Shimokawa. All rights reserved.
5 1.3.6.2 yamt *
6 1.3.6.2 yamt * Redistribution and use in source and binary forms, with or without
7 1.3.6.2 yamt * modification, are permitted provided that the following conditions
8 1.3.6.2 yamt * are met:
9 1.3.6.2 yamt * 1. Redistributions of source code must retain the above copyright
10 1.3.6.2 yamt * notice, this list of conditions and the following disclaimer.
11 1.3.6.2 yamt * 2. Redistributions in binary form must reproduce the above copyright
12 1.3.6.2 yamt * notice, this list of conditions and the following disclaimer in the
13 1.3.6.2 yamt * documentation and/or other materials provided with the distribution.
14 1.3.6.2 yamt * 3. All advertising materials mentioning features or use of this software
15 1.3.6.2 yamt * must display the following acknowledgement:
16 1.3.6.2 yamt *
17 1.3.6.2 yamt * This product includes software developed by Hidetoshi Shimokawa.
18 1.3.6.2 yamt *
19 1.3.6.2 yamt * 4. Neither the name of the author nor the names of its contributors
20 1.3.6.2 yamt * may be used to endorse or promote products derived from this software
21 1.3.6.2 yamt * without specific prior written permission.
22 1.3.6.2 yamt *
23 1.3.6.2 yamt * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.3.6.2 yamt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.3.6.2 yamt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.3.6.2 yamt * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.3.6.2 yamt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.3.6.2 yamt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.3.6.2 yamt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.3.6.2 yamt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.3.6.2 yamt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.3.6.2 yamt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.3.6.2 yamt * SUCH DAMAGE.
34 1.3.6.2 yamt */
35 1.3.6.2 yamt
36 1.3.6.2 yamt #ifdef __FreeBSD__
37 1.3.6.2 yamt #include <sys/cdefs.h>
38 1.3.6.3 yamt __FBSDID("$FreeBSD: /repoman/r/ncvs/src/sys/dev/firewire/fwcrom.c,v 1.14 2006/02/04 21:37:39 imp Exp $");
39 1.3.6.2 yamt #endif
40 1.3.6.2 yamt
41 1.3.6.2 yamt #if defined(__FreeBSD__)
42 1.3.6.2 yamt #include <sys/param.h>
43 1.3.6.3 yamt
44 1.3.6.3 yamt #ifdef _BOOT
45 1.3.6.3 yamt #include <stand.h>
46 1.3.6.3 yamt #include <bootstrap.h>
47 1.3.6.3 yamt #else
48 1.3.6.2 yamt #if defined(_KERNEL) || defined(TEST)
49 1.3.6.2 yamt #include <sys/queue.h>
50 1.3.6.2 yamt #endif
51 1.3.6.2 yamt #ifdef _KERNEL
52 1.3.6.2 yamt #include <sys/systm.h>
53 1.3.6.2 yamt #include <sys/kernel.h>
54 1.3.6.2 yamt #else
55 1.3.6.2 yamt #include <netinet/in.h>
56 1.3.6.2 yamt #include <fcntl.h>
57 1.3.6.2 yamt #include <stdio.h>
58 1.3.6.2 yamt #include <err.h>
59 1.3.6.2 yamt #include <stdlib.h>
60 1.3.6.2 yamt #include <string.h>
61 1.3.6.2 yamt #endif
62 1.3.6.3 yamt #endif
63 1.3.6.2 yamt
64 1.3.6.2 yamt #ifdef __DragonFly__
65 1.3.6.2 yamt #include "fw_port.h"
66 1.3.6.2 yamt #include "firewire.h"
67 1.3.6.2 yamt #include "iec13213.h"
68 1.3.6.2 yamt #else
69 1.3.6.2 yamt #include <dev/firewire/fw_port.h>
70 1.3.6.2 yamt #include <dev/firewire/firewire.h>
71 1.3.6.2 yamt #include <dev/firewire/iec13213.h>
72 1.3.6.2 yamt #endif
73 1.3.6.2 yamt #elif defined(__NetBSD__)
74 1.3.6.2 yamt #include <sys/param.h>
75 1.3.6.2 yamt #ifdef _KERNEL
76 1.3.6.2 yamt #include <sys/device.h>
77 1.3.6.2 yamt #include <sys/errno.h>
78 1.3.6.2 yamt #include <sys/systm.h>
79 1.3.6.2 yamt #else
80 1.3.6.2 yamt #include <stdio.h>
81 1.3.6.2 yamt #include <string.h>
82 1.3.6.2 yamt #endif
83 1.3.6.2 yamt #include <dev/ieee1394/fw_port.h>
84 1.3.6.2 yamt #include <dev/ieee1394/firewire.h>
85 1.3.6.2 yamt #include <dev/ieee1394/iec13213.h>
86 1.3.6.2 yamt #endif
87 1.3.6.2 yamt
88 1.3.6.2 yamt #define MAX_ROM (1024 - sizeof(uint32_t) * 5)
89 1.3.6.3 yamt #define CROM_END(cc) ((char *)(cc)->stack[0].dir + MAX_ROM - 1)
90 1.3.6.2 yamt
91 1.3.6.2 yamt void
92 1.3.6.2 yamt crom_init_context(struct crom_context *cc, uint32_t *p)
93 1.3.6.2 yamt {
94 1.3.6.2 yamt struct csrhdr *hdr;
95 1.3.6.2 yamt
96 1.3.6.2 yamt hdr = (struct csrhdr *)p;
97 1.3.6.2 yamt if (hdr->info_len <= 1) {
98 1.3.6.2 yamt /* minimum or invalid ROM */
99 1.3.6.2 yamt cc->depth = -1;
100 1.3.6.2 yamt return;
101 1.3.6.2 yamt }
102 1.3.6.2 yamt p += 1 + hdr->info_len;
103 1.3.6.2 yamt
104 1.3.6.2 yamt /* check size of root directory */
105 1.3.6.2 yamt if (((struct csrdirectory *)p)->crc_len == 0) {
106 1.3.6.2 yamt cc->depth = -1;
107 1.3.6.2 yamt return;
108 1.3.6.2 yamt }
109 1.3.6.2 yamt cc->depth = 0;
110 1.3.6.2 yamt cc->stack[0].dir = (struct csrdirectory *)p;
111 1.3.6.2 yamt cc->stack[0].index = 0;
112 1.3.6.2 yamt }
113 1.3.6.2 yamt
114 1.3.6.2 yamt struct csrreg *
115 1.3.6.2 yamt crom_get(struct crom_context *cc)
116 1.3.6.2 yamt {
117 1.3.6.2 yamt struct crom_ptr *ptr;
118 1.3.6.2 yamt
119 1.3.6.2 yamt ptr = &cc->stack[cc->depth];
120 1.3.6.2 yamt return (&ptr->dir->entry[ptr->index]);
121 1.3.6.2 yamt }
122 1.3.6.2 yamt
123 1.3.6.2 yamt void
124 1.3.6.2 yamt crom_next(struct crom_context *cc)
125 1.3.6.2 yamt {
126 1.3.6.2 yamt struct crom_ptr *ptr;
127 1.3.6.2 yamt struct csrreg *reg;
128 1.3.6.2 yamt
129 1.3.6.2 yamt if (cc->depth < 0)
130 1.3.6.2 yamt return;
131 1.3.6.2 yamt reg = crom_get(cc);
132 1.3.6.2 yamt if ((reg->key & CSRTYPE_MASK) == CSRTYPE_D) {
133 1.3.6.2 yamt if (cc->depth >= CROM_MAX_DEPTH) {
134 1.3.6.2 yamt printf("crom_next: too deep\n");
135 1.3.6.2 yamt goto again;
136 1.3.6.2 yamt }
137 1.3.6.2 yamt cc->depth ++;
138 1.3.6.2 yamt
139 1.3.6.2 yamt ptr = &cc->stack[cc->depth];
140 1.3.6.2 yamt ptr->dir = (struct csrdirectory *) (reg + reg->val);
141 1.3.6.2 yamt ptr->index = 0;
142 1.3.6.2 yamt goto check;
143 1.3.6.2 yamt }
144 1.3.6.2 yamt again:
145 1.3.6.2 yamt ptr = &cc->stack[cc->depth];
146 1.3.6.2 yamt ptr->index ++;
147 1.3.6.2 yamt check:
148 1.3.6.2 yamt if (ptr->index < ptr->dir->crc_len &&
149 1.3.6.3 yamt (char *)crom_get(cc) <= CROM_END(cc))
150 1.3.6.2 yamt return;
151 1.3.6.2 yamt
152 1.3.6.2 yamt if (ptr->index < ptr->dir->crc_len)
153 1.3.6.2 yamt printf("crom_next: bound check failed\n");
154 1.3.6.2 yamt
155 1.3.6.2 yamt if (cc->depth > 0) {
156 1.3.6.2 yamt cc->depth--;
157 1.3.6.2 yamt goto again;
158 1.3.6.2 yamt }
159 1.3.6.2 yamt /* no more data */
160 1.3.6.2 yamt cc->depth = -1;
161 1.3.6.2 yamt }
162 1.3.6.2 yamt
163 1.3.6.2 yamt
164 1.3.6.2 yamt struct csrreg *
165 1.3.6.2 yamt crom_search_key(struct crom_context *cc, uint8_t key)
166 1.3.6.2 yamt {
167 1.3.6.2 yamt struct csrreg *reg;
168 1.3.6.2 yamt
169 1.3.6.2 yamt while(cc->depth >= 0) {
170 1.3.6.2 yamt reg = crom_get(cc);
171 1.3.6.2 yamt if (reg->key == key)
172 1.3.6.2 yamt return reg;
173 1.3.6.2 yamt crom_next(cc);
174 1.3.6.2 yamt }
175 1.3.6.2 yamt return NULL;
176 1.3.6.2 yamt }
177 1.3.6.2 yamt
178 1.3.6.2 yamt int
179 1.3.6.2 yamt crom_has_specver(uint32_t *p, uint32_t spec, uint32_t ver)
180 1.3.6.2 yamt {
181 1.3.6.2 yamt struct csrreg *reg;
182 1.3.6.2 yamt struct crom_context c, *cc;
183 1.3.6.2 yamt int state = 0;
184 1.3.6.2 yamt
185 1.3.6.2 yamt cc = &c;
186 1.3.6.2 yamt crom_init_context(cc, p);
187 1.3.6.2 yamt while(cc->depth >= 0) {
188 1.3.6.2 yamt reg = crom_get(cc);
189 1.3.6.2 yamt if (state == 0) {
190 1.3.6.2 yamt if (reg->key == CSRKEY_SPEC && reg->val == spec)
191 1.3.6.2 yamt state = 1;
192 1.3.6.2 yamt else
193 1.3.6.2 yamt state = 0;
194 1.3.6.2 yamt } else {
195 1.3.6.2 yamt if (reg->key == CSRKEY_VER && reg->val == ver)
196 1.3.6.2 yamt return 1;
197 1.3.6.2 yamt else
198 1.3.6.2 yamt state = 0;
199 1.3.6.2 yamt }
200 1.3.6.2 yamt crom_next(cc);
201 1.3.6.2 yamt }
202 1.3.6.2 yamt return 0;
203 1.3.6.2 yamt }
204 1.3.6.2 yamt
205 1.3.6.2 yamt void
206 1.3.6.2 yamt crom_parse_text(struct crom_context *cc, char *buf, int len)
207 1.3.6.2 yamt {
208 1.3.6.2 yamt struct csrreg *reg;
209 1.3.6.2 yamt struct csrtext *textleaf;
210 1.3.6.2 yamt uint32_t *bp;
211 1.3.6.2 yamt int i, qlen;
212 1.3.6.2 yamt static char *nullstr = (char *)&"(null)";
213 1.3.6.2 yamt
214 1.3.6.2 yamt if (cc->depth < 0)
215 1.3.6.2 yamt return;
216 1.3.6.2 yamt
217 1.3.6.2 yamt reg = crom_get(cc);
218 1.3.6.2 yamt if (reg->key != CROM_TEXTLEAF ||
219 1.3.6.3 yamt (char *)(reg + reg->val) > CROM_END(cc)) {
220 1.3.6.2 yamt strncpy(buf, nullstr, len);
221 1.3.6.2 yamt return;
222 1.3.6.2 yamt }
223 1.3.6.2 yamt textleaf = (struct csrtext *)(reg + reg->val);
224 1.3.6.2 yamt
225 1.3.6.3 yamt if ((char *)textleaf + textleaf->crc_len > CROM_END(cc)) {
226 1.3.6.2 yamt strncpy(buf, nullstr, len);
227 1.3.6.2 yamt return;
228 1.3.6.2 yamt }
229 1.3.6.2 yamt
230 1.3.6.2 yamt /* XXX should check spec and type */
231 1.3.6.2 yamt
232 1.3.6.2 yamt bp = (uint32_t *)&buf[0];
233 1.3.6.2 yamt qlen = textleaf->crc_len - 2;
234 1.3.6.2 yamt if (len < qlen * 4)
235 1.3.6.2 yamt qlen = len/4;
236 1.3.6.2 yamt for (i = 0; i < qlen; i ++)
237 1.3.6.2 yamt *bp++ = ntohl(textleaf->text[i]);
238 1.3.6.2 yamt /* make sure to terminate the string */
239 1.3.6.2 yamt if (len <= qlen * 4)
240 1.3.6.2 yamt buf[len - 1] = 0;
241 1.3.6.2 yamt else
242 1.3.6.2 yamt buf[qlen * 4] = 0;
243 1.3.6.2 yamt }
244 1.3.6.2 yamt
245 1.3.6.2 yamt uint16_t
246 1.3.6.2 yamt crom_crc(uint32_t *ptr, int len)
247 1.3.6.2 yamt {
248 1.3.6.2 yamt int i, shift;
249 1.3.6.2 yamt uint32_t data, sum, crc = 0;
250 1.3.6.2 yamt
251 1.3.6.2 yamt for (i = 0; i < len; i++) {
252 1.3.6.2 yamt data = ptr[i];
253 1.3.6.2 yamt for (shift = 28; shift >= 0; shift -= 4) {
254 1.3.6.2 yamt sum = ((crc >> 12) ^ (data >> shift)) & 0xf;
255 1.3.6.2 yamt crc = (crc << 4) ^ (sum << 12) ^ (sum << 5) ^ sum;
256 1.3.6.2 yamt }
257 1.3.6.2 yamt crc &= 0xffff;
258 1.3.6.2 yamt }
259 1.3.6.2 yamt return((uint16_t) crc);
260 1.3.6.2 yamt }
261 1.3.6.2 yamt
262 1.3.6.3 yamt #if !defined(_KERNEL) && !defined(_BOOT)
263 1.3.6.2 yamt static void
264 1.3.6.2 yamt crom_desc_specver(uint32_t spec, uint32_t ver, char *buf, int len)
265 1.3.6.2 yamt {
266 1.3.6.2 yamt char *s = NULL;
267 1.3.6.2 yamt
268 1.3.6.2 yamt if (spec == CSRVAL_ANSIT10 || spec == 0) {
269 1.3.6.2 yamt switch (ver) {
270 1.3.6.2 yamt case CSRVAL_T10SBP2:
271 1.3.6.2 yamt s = "SBP-2";
272 1.3.6.2 yamt break;
273 1.3.6.2 yamt default:
274 1.3.6.2 yamt if (spec != 0)
275 1.3.6.2 yamt s = "unknown ANSIT10";
276 1.3.6.2 yamt }
277 1.3.6.2 yamt }
278 1.3.6.2 yamt if (spec == CSRVAL_1394TA || spec == 0) {
279 1.3.6.2 yamt switch (ver) {
280 1.3.6.2 yamt case CSR_PROTAVC:
281 1.3.6.2 yamt s = "AV/C";
282 1.3.6.2 yamt break;
283 1.3.6.2 yamt case CSR_PROTCAL:
284 1.3.6.2 yamt s = "CAL";
285 1.3.6.2 yamt break;
286 1.3.6.2 yamt case CSR_PROTEHS:
287 1.3.6.2 yamt s = "EHS";
288 1.3.6.2 yamt break;
289 1.3.6.2 yamt case CSR_PROTHAVI:
290 1.3.6.2 yamt s = "HAVi";
291 1.3.6.2 yamt break;
292 1.3.6.2 yamt case CSR_PROTCAM104:
293 1.3.6.2 yamt s = "1394 Cam 1.04";
294 1.3.6.2 yamt break;
295 1.3.6.2 yamt case CSR_PROTCAM120:
296 1.3.6.2 yamt s = "1394 Cam 1.20";
297 1.3.6.2 yamt break;
298 1.3.6.2 yamt case CSR_PROTCAM130:
299 1.3.6.2 yamt s = "1394 Cam 1.30";
300 1.3.6.2 yamt break;
301 1.3.6.2 yamt case CSR_PROTDPP:
302 1.3.6.2 yamt s = "1394 Direct print";
303 1.3.6.2 yamt break;
304 1.3.6.2 yamt case CSR_PROTIICP:
305 1.3.6.2 yamt s = "Industrial & Instrument";
306 1.3.6.2 yamt break;
307 1.3.6.2 yamt default:
308 1.3.6.2 yamt if (spec != 0)
309 1.3.6.2 yamt s = "unknown 1394TA";
310 1.3.6.2 yamt }
311 1.3.6.2 yamt }
312 1.3.6.2 yamt if (s != NULL)
313 1.3.6.2 yamt snprintf(buf, len, "%s", s);
314 1.3.6.2 yamt }
315 1.3.6.2 yamt
316 1.3.6.2 yamt char *
317 1.3.6.2 yamt crom_desc(struct crom_context *cc, char *buf, int len)
318 1.3.6.2 yamt {
319 1.3.6.2 yamt struct csrreg *reg;
320 1.3.6.2 yamt struct csrdirectory *dir;
321 1.3.6.2 yamt char *desc;
322 1.3.6.2 yamt uint16_t crc;
323 1.3.6.2 yamt
324 1.3.6.2 yamt reg = crom_get(cc);
325 1.3.6.2 yamt switch (reg->key & CSRTYPE_MASK) {
326 1.3.6.2 yamt case CSRTYPE_I:
327 1.3.6.2 yamt #if 0
328 1.3.6.2 yamt len -= snprintf(buf, len, "%d", reg->val);
329 1.3.6.2 yamt buf += strlen(buf);
330 1.3.6.2 yamt #else
331 1.3.6.2 yamt *buf = '\0';
332 1.3.6.2 yamt #endif
333 1.3.6.2 yamt break;
334 1.3.6.2 yamt case CSRTYPE_C:
335 1.3.6.2 yamt len -= snprintf(buf, len, "offset=0x%04x(%d)",
336 1.3.6.2 yamt reg->val, reg->val);
337 1.3.6.2 yamt buf += strlen(buf);
338 1.3.6.2 yamt break;
339 1.3.6.2 yamt case CSRTYPE_L:
340 1.3.6.2 yamt /* XXX fall through */
341 1.3.6.2 yamt case CSRTYPE_D:
342 1.3.6.2 yamt dir = (struct csrdirectory *) (reg + reg->val);
343 1.3.6.2 yamt crc = crom_crc((uint32_t *)&dir->entry[0], dir->crc_len);
344 1.3.6.2 yamt len -= snprintf(buf, len, "len=%d crc=0x%04x(%s) ",
345 1.3.6.2 yamt dir->crc_len, dir->crc,
346 1.3.6.2 yamt (crc == dir->crc) ? "OK" : "NG");
347 1.3.6.2 yamt buf += strlen(buf);
348 1.3.6.2 yamt }
349 1.3.6.2 yamt switch (reg->key) {
350 1.3.6.2 yamt case 0x03:
351 1.3.6.2 yamt desc = "module_vendor_ID";
352 1.3.6.2 yamt break;
353 1.3.6.2 yamt case 0x04:
354 1.3.6.2 yamt desc = "hardware_version";
355 1.3.6.2 yamt break;
356 1.3.6.2 yamt case 0x0c:
357 1.3.6.2 yamt desc = "node_capabilities";
358 1.3.6.2 yamt break;
359 1.3.6.2 yamt case 0x12:
360 1.3.6.2 yamt desc = "unit_spec_ID";
361 1.3.6.2 yamt break;
362 1.3.6.2 yamt case 0x13:
363 1.3.6.2 yamt desc = "unit_sw_version";
364 1.3.6.2 yamt crom_desc_specver(0, reg->val, buf, len);
365 1.3.6.2 yamt break;
366 1.3.6.2 yamt case 0x14:
367 1.3.6.2 yamt desc = "logical_unit_number";
368 1.3.6.2 yamt break;
369 1.3.6.2 yamt case 0x17:
370 1.3.6.2 yamt desc = "model_ID";
371 1.3.6.2 yamt break;
372 1.3.6.2 yamt case 0x38:
373 1.3.6.2 yamt desc = "command_set_spec_ID";
374 1.3.6.2 yamt break;
375 1.3.6.2 yamt case 0x39:
376 1.3.6.2 yamt desc = "command_set";
377 1.3.6.2 yamt break;
378 1.3.6.2 yamt case 0x3a:
379 1.3.6.2 yamt desc = "unit_characteristics";
380 1.3.6.2 yamt break;
381 1.3.6.2 yamt case 0x3b:
382 1.3.6.2 yamt desc = "command_set_revision";
383 1.3.6.2 yamt break;
384 1.3.6.2 yamt case 0x3c:
385 1.3.6.2 yamt desc = "firmware_revision";
386 1.3.6.2 yamt break;
387 1.3.6.2 yamt case 0x3d:
388 1.3.6.2 yamt desc = "reconnect_timeout";
389 1.3.6.2 yamt break;
390 1.3.6.2 yamt case 0x54:
391 1.3.6.2 yamt desc = "management_agent";
392 1.3.6.2 yamt break;
393 1.3.6.2 yamt case 0x81:
394 1.3.6.2 yamt desc = "text_leaf";
395 1.3.6.2 yamt crom_parse_text(cc, buf + strlen(buf), len);
396 1.3.6.2 yamt break;
397 1.3.6.2 yamt case 0xd1:
398 1.3.6.2 yamt desc = "unit_directory";
399 1.3.6.2 yamt break;
400 1.3.6.2 yamt case 0xd4:
401 1.3.6.2 yamt desc = "logical_unit_directory";
402 1.3.6.2 yamt break;
403 1.3.6.2 yamt default:
404 1.3.6.2 yamt desc = "unknown";
405 1.3.6.2 yamt }
406 1.3.6.2 yamt return desc;
407 1.3.6.2 yamt }
408 1.3.6.2 yamt #endif
409 1.3.6.2 yamt
410 1.3.6.3 yamt #if defined(_KERNEL) || defined(_BOOT) || defined(TEST)
411 1.3.6.2 yamt
412 1.3.6.2 yamt int
413 1.3.6.2 yamt crom_add_quad(struct crom_chunk *chunk, uint32_t entry)
414 1.3.6.2 yamt {
415 1.3.6.2 yamt int index;
416 1.3.6.2 yamt
417 1.3.6.2 yamt index = chunk->data.crc_len;
418 1.3.6.2 yamt if (index >= CROM_MAX_CHUNK_LEN - 1) {
419 1.3.6.2 yamt printf("too large chunk %d\n", index);
420 1.3.6.2 yamt return(-1);
421 1.3.6.2 yamt }
422 1.3.6.2 yamt chunk->data.buf[index] = entry;
423 1.3.6.2 yamt chunk->data.crc_len++;
424 1.3.6.2 yamt return(index);
425 1.3.6.2 yamt }
426 1.3.6.2 yamt
427 1.3.6.2 yamt int
428 1.3.6.2 yamt crom_add_entry(struct crom_chunk *chunk, int key, int val)
429 1.3.6.2 yamt {
430 1.3.6.2 yamt union {
431 1.3.6.2 yamt struct csrreg reg;
432 1.3.6.2 yamt uint32_t i;
433 1.3.6.3 yamt } foo;
434 1.3.6.2 yamt
435 1.3.6.3 yamt foo.reg.key = key;
436 1.3.6.3 yamt foo.reg.val = val;
437 1.3.6.2 yamt
438 1.3.6.3 yamt return(crom_add_quad(chunk, foo.i));
439 1.3.6.2 yamt }
440 1.3.6.2 yamt
441 1.3.6.2 yamt int
442 1.3.6.2 yamt crom_add_chunk(struct crom_src *src, struct crom_chunk *parent,
443 1.3.6.2 yamt struct crom_chunk *child, int key)
444 1.3.6.2 yamt {
445 1.3.6.2 yamt int index;
446 1.3.6.2 yamt
447 1.3.6.2 yamt if (parent == NULL) {
448 1.3.6.2 yamt STAILQ_INSERT_TAIL(&src->chunk_list, child, link);
449 1.3.6.2 yamt return(0);
450 1.3.6.2 yamt }
451 1.3.6.2 yamt
452 1.3.6.2 yamt index = crom_add_entry(parent, key, 0);
453 1.3.6.2 yamt if (index < 0) {
454 1.3.6.2 yamt return(-1);
455 1.3.6.2 yamt }
456 1.3.6.2 yamt child->ref_chunk = parent;
457 1.3.6.2 yamt child->ref_index = index;
458 1.3.6.2 yamt STAILQ_INSERT_TAIL(&src->chunk_list, child, link);
459 1.3.6.2 yamt return(index);
460 1.3.6.2 yamt }
461 1.3.6.2 yamt
462 1.3.6.2 yamt #if defined(__FreeBSD__)
463 1.3.6.2 yamt #define MAX_TEXT ((CROM_MAX_CHUNK_LEN + 1) * 4 - sizeof(struct csrtext))
464 1.3.6.2 yamt #elif defined(__NetBSD__)
465 1.3.6.2 yamt #define MAX_TEXT (int)((CROM_MAX_CHUNK_LEN + 1) * 4 - sizeof(struct csrtext))
466 1.3.6.2 yamt #endif
467 1.3.6.2 yamt int
468 1.3.6.2 yamt crom_add_simple_text(struct crom_src *src, struct crom_chunk *parent,
469 1.3.6.2 yamt struct crom_chunk *chunk, const char *buf)
470 1.3.6.2 yamt {
471 1.3.6.2 yamt struct csrtext *tl;
472 1.3.6.2 yamt uint32_t *p;
473 1.3.6.2 yamt int len, i;
474 1.3.6.2 yamt char t[MAX_TEXT];
475 1.3.6.2 yamt
476 1.3.6.2 yamt len = strlen(buf);
477 1.3.6.2 yamt if (len > MAX_TEXT) {
478 1.3.6.2 yamt #if defined(__DragonFly__) || __FreeBSD_version < 500000 || defined(__NetBSD__)
479 1.3.6.2 yamt printf("text(%d) trancated to %d.\n", len, MAX_TEXT);
480 1.3.6.2 yamt #else
481 1.3.6.2 yamt printf("text(%d) trancated to %td.\n", len, MAX_TEXT);
482 1.3.6.2 yamt #endif
483 1.3.6.2 yamt len = MAX_TEXT;
484 1.3.6.2 yamt }
485 1.3.6.2 yamt
486 1.3.6.2 yamt tl = (struct csrtext *) &chunk->data;
487 1.3.6.2 yamt tl->crc_len = howmany(sizeof(struct csrtext) + len, sizeof(uint32_t));
488 1.3.6.2 yamt tl->spec_id = 0;
489 1.3.6.2 yamt tl->spec_type = 0;
490 1.3.6.2 yamt tl->lang_id = 0;
491 1.3.6.2 yamt bzero(&t[0], roundup2(len, sizeof(uint32_t)));
492 1.3.6.2 yamt bcopy(buf, &t[0], len);
493 1.3.6.2 yamt p = (uint32_t *)&t[0];
494 1.3.6.2 yamt for (i = 0; i < howmany(len, sizeof(uint32_t)); i ++)
495 1.3.6.2 yamt tl->text[i] = ntohl(*p++);
496 1.3.6.2 yamt return (crom_add_chunk(src, parent, chunk, CROM_TEXTLEAF));
497 1.3.6.2 yamt }
498 1.3.6.2 yamt
499 1.3.6.2 yamt static int
500 1.3.6.2 yamt crom_copy(uint32_t *src, uint32_t *dst, int *offset, int len, int maxlen)
501 1.3.6.2 yamt {
502 1.3.6.2 yamt if (*offset + len > maxlen) {
503 1.3.6.2 yamt printf("Config. ROM is too large for the buffer\n");
504 1.3.6.2 yamt return(-1);
505 1.3.6.2 yamt }
506 1.3.6.2 yamt bcopy(src, (char *)(dst + *offset), len * sizeof(uint32_t));
507 1.3.6.2 yamt *offset += len;
508 1.3.6.2 yamt return(0);
509 1.3.6.2 yamt }
510 1.3.6.2 yamt
511 1.3.6.2 yamt int
512 1.3.6.2 yamt crom_load(struct crom_src *src, uint32_t *buf, int maxlen)
513 1.3.6.2 yamt {
514 1.3.6.2 yamt struct crom_chunk *chunk, *parent;
515 1.3.6.2 yamt struct csrhdr *hdr;
516 1.3.6.3 yamt #if defined(_KERNEL) || defined(_BOOT)
517 1.3.6.2 yamt uint32_t *ptr;
518 1.3.6.2 yamt int i;
519 1.3.6.2 yamt #endif
520 1.3.6.2 yamt int count, offset;
521 1.3.6.2 yamt int len;
522 1.3.6.2 yamt
523 1.3.6.2 yamt offset = 0;
524 1.3.6.2 yamt /* Determine offset */
525 1.3.6.2 yamt STAILQ_FOREACH(chunk, &src->chunk_list, link) {
526 1.3.6.2 yamt chunk->offset = offset;
527 1.3.6.2 yamt /* Assume the offset of the parent is already known */
528 1.3.6.2 yamt parent = chunk->ref_chunk;
529 1.3.6.2 yamt if (parent != NULL) {
530 1.3.6.2 yamt struct csrreg *reg;
531 1.3.6.2 yamt reg = (struct csrreg *)
532 1.3.6.2 yamt &parent->data.buf[chunk->ref_index];
533 1.3.6.2 yamt reg->val = offset -
534 1.3.6.2 yamt (parent->offset + 1 + chunk->ref_index);
535 1.3.6.2 yamt }
536 1.3.6.2 yamt offset += 1 + chunk->data.crc_len;
537 1.3.6.2 yamt }
538 1.3.6.2 yamt
539 1.3.6.2 yamt /* Calculate CRC and dump to the buffer */
540 1.3.6.2 yamt len = 1 + src->hdr.info_len;
541 1.3.6.2 yamt count = 0;
542 1.3.6.2 yamt if (crom_copy((uint32_t *)&src->hdr, buf, &count, len, maxlen) < 0)
543 1.3.6.2 yamt return(-1);
544 1.3.6.2 yamt STAILQ_FOREACH(chunk, &src->chunk_list, link) {
545 1.3.6.2 yamt chunk->data.crc =
546 1.3.6.2 yamt crom_crc(&chunk->data.buf[0], chunk->data.crc_len);
547 1.3.6.2 yamt
548 1.3.6.2 yamt len = 1 + chunk->data.crc_len;
549 1.3.6.2 yamt if (crom_copy((uint32_t *)&chunk->data, buf,
550 1.3.6.2 yamt &count, len, maxlen) < 0)
551 1.3.6.2 yamt return(-1);
552 1.3.6.2 yamt }
553 1.3.6.2 yamt hdr = (struct csrhdr *)buf;
554 1.3.6.2 yamt hdr->crc_len = count - 1;
555 1.3.6.2 yamt hdr->crc = crom_crc(&buf[1], hdr->crc_len);
556 1.3.6.2 yamt
557 1.3.6.3 yamt #if defined(_KERNEL) || defined(_BOOT)
558 1.3.6.2 yamt /* byte swap */
559 1.3.6.2 yamt ptr = buf;
560 1.3.6.2 yamt for (i = 0; i < count; i ++) {
561 1.3.6.2 yamt *ptr = htonl(*ptr);
562 1.3.6.2 yamt ptr++;
563 1.3.6.2 yamt }
564 1.3.6.2 yamt #endif
565 1.3.6.2 yamt
566 1.3.6.2 yamt return(count);
567 1.3.6.2 yamt }
568 1.3.6.2 yamt #endif
569 1.3.6.2 yamt
570 1.3.6.2 yamt #ifdef TEST
571 1.3.6.2 yamt int
572 1.3.6.2 yamt main () {
573 1.3.6.2 yamt struct crom_src src;
574 1.3.6.2 yamt struct crom_chunk root,unit1,unit2,unit3;
575 1.3.6.2 yamt struct crom_chunk text1,text2,text3,text4,text5,text6,text7;
576 1.3.6.2 yamt uint32_t buf[256], *p;
577 1.3.6.2 yamt int i;
578 1.3.6.2 yamt
579 1.3.6.2 yamt bzero(&src, sizeof(src));
580 1.3.6.2 yamt bzero(&root, sizeof(root));
581 1.3.6.2 yamt bzero(&unit1, sizeof(unit1));
582 1.3.6.2 yamt bzero(&unit2, sizeof(unit2));
583 1.3.6.2 yamt bzero(&unit3, sizeof(unit3));
584 1.3.6.2 yamt bzero(&text1, sizeof(text1));
585 1.3.6.2 yamt bzero(&text2, sizeof(text2));
586 1.3.6.2 yamt bzero(&text3, sizeof(text3));
587 1.3.6.2 yamt bzero(&text3, sizeof(text4));
588 1.3.6.2 yamt bzero(&text3, sizeof(text5));
589 1.3.6.2 yamt bzero(&text3, sizeof(text6));
590 1.3.6.2 yamt bzero(&text3, sizeof(text7));
591 1.3.6.2 yamt bzero(buf, sizeof(buf));
592 1.3.6.2 yamt
593 1.3.6.2 yamt /* BUS info sample */
594 1.3.6.2 yamt src.hdr.info_len = 4;
595 1.3.6.2 yamt src.businfo.bus_name = CSR_BUS_NAME_IEEE1394;
596 1.3.6.2 yamt src.businfo.eui64.hi = 0x11223344;
597 1.3.6.2 yamt src.businfo.eui64.lo = 0x55667788;
598 1.3.6.2 yamt src.businfo.link_spd = FWSPD_S400;
599 1.3.6.2 yamt src.businfo.generation = 0;
600 1.3.6.2 yamt src.businfo.max_rom = MAXROM_4;
601 1.3.6.2 yamt src.businfo.max_rec = 10;
602 1.3.6.2 yamt src.businfo.cyc_clk_acc = 100;
603 1.3.6.2 yamt src.businfo.pmc = 0;
604 1.3.6.2 yamt src.businfo.bmc = 1;
605 1.3.6.2 yamt src.businfo.isc = 1;
606 1.3.6.2 yamt src.businfo.cmc = 1;
607 1.3.6.2 yamt src.businfo.irmc = 1;
608 1.3.6.2 yamt STAILQ_INIT(&src.chunk_list);
609 1.3.6.2 yamt
610 1.3.6.2 yamt /* Root directory */
611 1.3.6.2 yamt crom_add_chunk(&src, NULL, &root, 0);
612 1.3.6.2 yamt crom_add_entry(&root, CSRKEY_NCAP, 0x123456);
613 1.3.6.2 yamt /* private company_id */
614 1.3.6.2 yamt crom_add_entry(&root, CSRKEY_VENDOR, 0xacde48);
615 1.3.6.2 yamt
616 1.3.6.2 yamt crom_add_simple_text(&src, &root, &text1, OS_STR);
617 1.3.6.2 yamt crom_add_entry(&root, CSRKEY_HW, OS_VER);
618 1.3.6.2 yamt crom_add_simple_text(&src, &root, &text2, OS_VER_STR);
619 1.3.6.2 yamt
620 1.3.6.2 yamt /* SBP unit directory */
621 1.3.6.2 yamt crom_add_chunk(&src, &root, &unit1, CROM_UDIR);
622 1.3.6.2 yamt crom_add_entry(&unit1, CSRKEY_SPEC, CSRVAL_ANSIT10);
623 1.3.6.2 yamt crom_add_entry(&unit1, CSRKEY_VER, CSRVAL_T10SBP2);
624 1.3.6.2 yamt crom_add_entry(&unit1, CSRKEY_COM_SPEC, CSRVAL_ANSIT10);
625 1.3.6.2 yamt crom_add_entry(&unit1, CSRKEY_COM_SET, CSRVAL_SCSI);
626 1.3.6.2 yamt /* management_agent */
627 1.3.6.2 yamt crom_add_entry(&unit1, CROM_MGM, 0x1000);
628 1.3.6.2 yamt crom_add_entry(&unit1, CSRKEY_UNIT_CH, (10<<8) | 8);
629 1.3.6.2 yamt /* Device type and LUN */
630 1.3.6.2 yamt crom_add_entry(&unit1, CROM_LUN, 0);
631 1.3.6.2 yamt crom_add_entry(&unit1, CSRKEY_MODEL, 1);
632 1.3.6.2 yamt crom_add_simple_text(&src, &unit1, &text3, "scsi_target");
633 1.3.6.2 yamt
634 1.3.6.2 yamt /* RFC2734 IPv4 over IEEE1394 */
635 1.3.6.2 yamt crom_add_chunk(&src, &root, &unit2, CROM_UDIR);
636 1.3.6.2 yamt crom_add_entry(&unit2, CSRKEY_SPEC, CSRVAL_IETF);
637 1.3.6.2 yamt crom_add_simple_text(&src, &unit2, &text4, "IANA");
638 1.3.6.2 yamt crom_add_entry(&unit2, CSRKEY_VER, 1);
639 1.3.6.2 yamt crom_add_simple_text(&src, &unit2, &text5, "IPv4");
640 1.3.6.2 yamt
641 1.3.6.2 yamt /* RFC3146 IPv6 over IEEE1394 */
642 1.3.6.2 yamt crom_add_chunk(&src, &root, &unit3, CROM_UDIR);
643 1.3.6.2 yamt crom_add_entry(&unit3, CSRKEY_SPEC, CSRVAL_IETF);
644 1.3.6.2 yamt crom_add_simple_text(&src, &unit3, &text6, "IANA");
645 1.3.6.2 yamt crom_add_entry(&unit3, CSRKEY_VER, 2);
646 1.3.6.2 yamt crom_add_simple_text(&src, &unit3, &text7, "IPv6");
647 1.3.6.2 yamt
648 1.3.6.2 yamt crom_load(&src, buf, 256);
649 1.3.6.2 yamt p = buf;
650 1.3.6.2 yamt #define DUMP_FORMAT "%08x %08x %08x %08x %08x %08x %08x %08x\n"
651 1.3.6.2 yamt for (i = 0; i < 256/8; i ++) {
652 1.3.6.2 yamt printf(DUMP_FORMAT,
653 1.3.6.2 yamt p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
654 1.3.6.2 yamt p += 8;
655 1.3.6.2 yamt }
656 1.3.6.2 yamt return(0);
657 1.3.6.2 yamt }
658 1.3.6.2 yamt #endif
659