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