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