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