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