<commit_before>/*
 * Copyright (C) 2002
 * 	<NAME>. All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *
 *	This product includes software developed by <NAME>.
 *
 * 4. Neither the name of the author nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * 
 * $FreeBSD$
 */

#include <sys/param.h>
#include <sys/malloc.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/errno.h>
#include <dev/firewire/firewire.h>
#include <dev/firewire/iec13213.h>
#include <dev/firewire/fwphyreg.h>

#include <netinet/in.h>
#include <fcntl.h>
#include <stdio.h>
#include <err.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

extern int dvrecv(int, char *, char, int);
extern int dvsend(int, char *, char, int);

static void
usage(void)
{
	fprintf(stderr,
		"fwcontrol [-u bus_num] [-rt] [-g gap_count] [-o node] "
		    "[-b pri_req] [-c node] [-d node] [-l file] "
		    "[-R file] [-S file]\n"
		"\t-u: specify bus number\n"
		"\t-g: broadcast gap_count by phy_config packet\n"
		"\t-o: send link-on packet to the node\n"
		"\t-s: write RESET_START register on the node\n"
		"\t-b: set PRIORITY_BUDGET register on all supported nodes\n"
		"\t-c: read configuration ROM\n"
		"\t-r: bus reset\n"
		"\t-t: read topology map\n"
		"\t-d: hex dump of configuration ROM\n"
		"\t-l: load and parse hex dump file of configuration ROM\n"
		"\t-R: Receive DV stream\n"
		"\t-S: Send DV stream\n");
	exit(0);
}

static struct fw_devlstreq *
get_dev(int fd)
{
	struct fw_devlstreq *data;

	data = (struct fw_devlstreq *)malloc(sizeof(struct fw_devlstreq));
	if (data == NULL)
		err(1, "malloc");
	if( ioctl(fd, FW_GDEVLST, data) < 0) {
       			err(1, "ioctl");
	}
	return data;
}

static void
list_dev(int fd)
{
	struct fw_devlstreq *data;
	struct fw_devinfo *devinfo;
	int i;

	data = get_dev(fd);
	printf("%d devices (info_len=%d)\n", data->n, data->info_len);
	printf("node        EUI64        status\n");
	for (i = 0; i < data->info_len; i++) {
		devinfo = &data->dev[i];
		printf("%4d  0x%08x%08x %6d\n",
			(devinfo->status || i == 0) ? devinfo->dst : -1,
			devinfo->eui.hi,
			devinfo->eui.lo,
			devinfo->status
		);
	}
	free((void *)data);
}

static u_int32_t
read_write_quad(int fd, struct fw_eui64 eui, u_int32_t addr_lo, int read, u_int32_t data)
{
        struct fw_asyreq *asyreq;
	u_int32_t *qld, res;

        asyreq = (struct fw_asyreq *)malloc(sizeof(struct fw_asyreq_t) + 16);
	asyreq->req.len = 16;
#if 0
	asyreq->req.type = FWASREQNODE;
	asyreq->pkt.mode.rreqq.dst = FWLOCALBUS | node;
#else
	asyreq->req.type = FWASREQEUI;
	asyreq->req.dst.eui = eui;
#endif
	asyreq->pkt.mode.rreqq.tlrt = 0;
	if (read)
		asyreq->pkt.mode.rreqq.tcode = FWTCODE_RREQQ;
	else
		asyreq->pkt.mode.rreqq.tcode = FWTCODE_WREQQ;

	asyreq->pkt.mode.rreqq.dest_hi = 0xffff;
	asyreq->pkt.mode.rreqq.dest_lo = addr_lo;

	qld = (u_int32_t *)&asyreq->pkt;
	if (!read)
		asyreq->pkt.mode.wreqq.data = data;

	if (ioctl(fd, FW_ASYREQ, asyreq) < 0) {
       		err(1, "ioctl");
	}
	res = qld[3];
	free(asyreq);
	if (read)
		return ntohl(res);
	else
		return 0;
}

static void
send_phy_config(int fd, int root_node, int gap_count)
{
        struct fw_asyreq *asyreq;

	asyreq = (struct fw_asyreq *)malloc(sizeof(struct fw_asyreq_t) + 12);
	asyreq->req.len = 12;
	asyreq->req.type = FWASREQNODE;
	asyreq->pkt.mode.ld[0] = 0;
	asyreq->pkt.mode.ld[1] = 0;
	asyreq->pkt.mode.common.tcode = FWTCODE_PHY;
	if (root_node >= 0)
		asyreq->pkt.mode.ld[1] |= (root_node & 0x3f) << 24 | 1 << 23;
	if (gap_count >= 0)
		asyreq->pkt.mode.ld[1] |= 1 << 22 | (gap_count & 0x3f) << 16;
	asyreq->pkt.mode.ld[2] = ~asyreq->pkt.mode.ld[1];

	printf("send phy_config root_node=%d gap_count=%d\n",
						root_node, gap_count);

	if (ioctl(fd, FW_ASYREQ, asyreq) < 0)
       		err(1, "ioctl");
	free(asyreq);
}

static void
send_link_on(int fd, int node)
{
        struct fw_asyreq *asyreq;

	asyreq = (struct fw_asyreq *)malloc(sizeof(struct fw_asyreq_t) + 12);
	asyreq->req.len = 12;
	asyreq->req.type = FWASREQNODE;
	asyreq->pkt.mode.common.tcode = FWTCODE_PHY;
	asyreq->pkt.mode.ld[1] |= (1 << 30) | ((node & 0x3f) << 24);
	asyreq->pkt.mode.ld[2] = ~asyreq->pkt.mode.ld[1];

	if (ioctl(fd, FW_ASYREQ, asyreq) < 0)
       		err(1, "ioctl");
	free(asyreq);
}

static void
reset_start(int fd, int node)
{
        struct fw_asyreq *asyreq;

	asyreq = (struct fw_asyreq *)malloc(sizeof(struct fw_asyreq_t) + 16);
	asyreq->req.len = 16;
	asyreq->req.type = FWASREQNODE;
	asyreq->pkt.mode.wreqq.dst = FWLOCALBUS | (node & 0x3f);
	asyreq->pkt.mode.wreqq.tlrt = 0;
	asyreq->pkt.mode.wreqq.tcode = FWTCODE_WREQQ;

	asyreq->pkt.mode.wreqq.dest_hi = 0xffff;
	asyreq->pkt.mode.wreqq.dest_lo = 0xf0000000 | RESET_START;

	asyreq->pkt.mode.wreqq.data = htonl(0x1);

	if (ioctl(fd, FW_ASYREQ, asyreq) < 0)
       		err(1, "ioctl");
	free(asyreq);
}

static void
set_pri_req(int fd, int pri_req)
{
	struct fw_devlstreq *data;
	struct fw_devinfo *devinfo;
	u_int32_t max, reg, old;
	int i;

	data = get_dev(fd);
#define BUGET_REG 0xf0000218
	for (i = 0; i < data->info_len; i++) {
		devinfo = &data->dev[i];
		if (!devinfo->status)
			continue;
		reg = read_write_quad(fd, devinfo->eui, BUGET_REG, 1, 0);
		printf("%d %08x:%08x, %08x",
			devinfo->dst, devinfo->eui.hi, devinfo->eui.lo, reg);
		if (reg > 0 && pri_req >= 0) {
			old = (reg & 0x3f);
			max = (reg & 0x3f00) >> 8;
			if (pri_req > max)
				pri_req =  max;
			printf(" 0x%x -> 0x%x\n", old, pri_req);
			read_write_quad(fd, devinfo->eui, BUGET_REG, 0, pri_req);
		} else {
			printf("\n");
		}
	}
	free((void *)data);
}

static void
parse_bus_info_block(u_int32_t *p, int info_len)
{
	int i;
	struct bus_info *bi;

	bi = (struct bus_info *)p;
	printf("bus_name: 0x%04x\n"
		"irmc:%d cmc:%d isc:%d bmc:%d pmc:%d\n"
		"cyc_clk_acc:%d max_rec:%d max_rom:%d\n"
		"generation:%d link_spd:%d\n"
		"EUI64: 0x%08x 0x%08x\n",
		bi->bus_name,
		bi->irmc, bi->cmc, bi->isc, bi->bmc, bi->pmc,
		bi->cyc_clk_acc, bi->max_rec, bi->max_rom,
		bi->generation, bi->link_spd,
		bi->eui64.hi, bi->eui64.lo);
}

static int
get_crom(int fd, int node, void *crom_buf, int len)
{
	struct fw_crom_buf buf;
	int i, error;
	struct fw_devlstreq *data;

	data = get_dev(fd);

	for (i = 0; i < data->info_len; i++) {
		if (data->dev[i].dst == node && data->dev[i].eui.lo != 0)
			break;
	}
	if (i == data->info_len)
		errx(1, "no such node %d.", node);
	else
		buf.eui = data->dev[i].eui;
	free((void *)data);

	buf.len = len;
	buf.ptr = crom_buf;
	bzero(crom_buf, len);
	if ((error = ioctl(fd, FW_GCROM, &buf)) < 0) {
       		err(1, "ioctl");
	}

	return error;
}

static void
show_crom(u_int32_t *crom_buf)
{
	int i;
	struct crom_context cc;
	char *desc, info[256];
	static char *key_types = "ICLD";
	struct csrreg *reg;
	struct csrdirectory *dir;
	struct csrhdr *hdr;
	u_int16_t crc;

	printf("first quad: 0x%08x ", *crom_buf);
	hdr = (struct csrhdr *)crom_buf;
	if (hdr->info_len == 1) {
		/* minimum ROM */
		struct csrreg *reg;
		reg = (struct csrreg *)hdr;
		printf("verndor ID: 0x%06x\n",  reg->val);
		return;
	}
	printf("info_len=%d crc_len=%d crc=0x%04x",
		hdr->info_len, hdr->crc_len, hdr->crc);
	crc = crom_crc(crom_buf+1, hdr->crc_len);
	if (crc == hdr->crc)
		printf("(OK)\n");
	else
		printf("(NG)\n");
	parse_bus_info_block(crom_buf+1, hdr->info_len);

	crom_init_context(&cc, crom_buf);
	dir = cc.stack[0].dir;
	printf("root_directory: len=0x%04x(%d) crc=0x%04x",
			dir->crc_len, dir->crc_len, dir->crc);
	crc = crom_crc((u_int32_t *)&dir->entry[0], dir->crc_len);
	if (crc == dir->crc)
		printf("(OK)\n");
	else
		printf("(NG)\n");
	if (dir->crc_len < 1)
		return;
	while (cc.depth >= 0) {
		desc = crom_desc(&cc, info, sizeof(info));
		reg = crom_get(&cc);
		for (i = 0; i < cc.depth; i++)
			printf("\t");
		printf("%02x(%c:%02x) %06x %s: %s\n",
			reg->key,
			key_types[(reg->key & CSRTYPE_MASK)>>6],
			reg->key & CSRKEY_MASK, reg->val,
			desc, info);
		crom_next(&cc);
	}
}
<commit_msg>Don't crash if the CROM is all zeros.<commit_after>/*
 * Copyright (C) 2002
 * 	<NAME>. All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *
 *	This product includes software developed by <NAME>.
 *
 * 4. Neither the name of the author nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * 
 * $FreeBSD$
 */

#include <sys/param.h>
#include <sys/malloc.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/errno.h>
#include <dev/firewire/firewire.h>
#include <dev/firewire/iec13213.h>
#include <dev/firewire/fwphyreg.h>

#include <netinet/in.h>
#include <fcntl.h>
#include <stdio.h>
#include <err.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

extern int dvrecv(int, char *, char, int);
extern int dvsend(int, char *, char, int);

static void
usage(void)
{
	fprintf(stderr,
		"fwcontrol [-u bus_num] [-rt] [-g gap_count] [-o node] "
		    "[-b pri_req] [-c node] [-d node] [-l file] "
		    "[-R file] [-S file]\n"
		"\t-u: specify bus number\n"
		"\t-g: broadcast gap_count by phy_config packet\n"
		"\t-o: send link-on packet to the node\n"
		"\t-s: write RESET_START register on the node\n"
		"\t-b: set PRIORITY_BUDGET register on all supported nodes\n"
		"\t-c: read configuration ROM\n"
		"\t-r: bus reset\n"
		"\t-t: read topology map\n"
		"\t-d: hex dump of configuration ROM\n"
		"\t-l: load and parse hex dump file of configuration ROM\n"
		"\t-R: Receive DV stream\n"
		"\t-S: Send DV stream\n");
	exit(0);
}

static struct fw_devlstreq *
get_dev(int fd)
{
	struct fw_devlstreq *data;

	data = (struct fw_devlstreq *)malloc(sizeof(struct fw_devlstreq));
	if (data == NULL)
		err(1, "malloc");
	if( ioctl(fd, FW_GDEVLST, data) < 0) {
       			err(1, "ioctl");
	}
	return data;
}

static void
list_dev(int fd)
{
	struct fw_devlstreq *data;
	struct fw_devinfo *devinfo;
	int i;

	data = get_dev(fd);
	printf("%d devices (info_len=%d)\n", data->n, data->info_len);
	printf("node        EUI64        status\n");
	for (i = 0; i < data->info_len; i++) {
		devinfo = &data->dev[i];
		printf("%4d  0x%08x%08x %6d\n",
			(devinfo->status || i == 0) ? devinfo->dst : -1,
			devinfo->eui.hi,
			devinfo->eui.lo,
			devinfo->status
		);
	}
	free((void *)data);
}

static u_int32_t
read_write_quad(int fd, struct fw_eui64 eui, u_int32_t addr_lo, int read, u_int32_t data)
{
        struct fw_asyreq *asyreq;
	u_int32_t *qld, res;

        asyreq = (struct fw_asyreq *)malloc(sizeof(struct fw_asyreq_t) + 16);
	asyreq->req.len = 16;
#if 0
	asyreq->req.type = FWASREQNODE;
	asyreq->pkt.mode.rreqq.dst = FWLOCALBUS | node;
#else
	asyreq->req.type = FWASREQEUI;
	asyreq->req.dst.eui = eui;
#endif
	asyreq->pkt.mode.rreqq.tlrt = 0;
	if (read)
		asyreq->pkt.mode.rreqq.tcode = FWTCODE_RREQQ;
	else
		asyreq->pkt.mode.rreqq.tcode = FWTCODE_WREQQ;

	asyreq->pkt.mode.rreqq.dest_hi = 0xffff;
	asyreq->pkt.mode.rreqq.dest_lo = addr_lo;

	qld = (u_int32_t *)&asyreq->pkt;
	if (!read)
		asyreq->pkt.mode.wreqq.data = data;

	if (ioctl(fd, FW_ASYREQ, asyreq) < 0) {
       		err(1, "ioctl");
	}
	res = qld[3];
	free(asyreq);
	if (read)
		return ntohl(res);
	else
		return 0;
}

static void
send_phy_config(int fd, int root_node, int gap_count)
{
        struct fw_asyreq *asyreq;

	asyreq = (struct fw_asyreq *)malloc(sizeof(struct fw_asyreq_t) + 12);
	asyreq->req.len = 12;
	asyreq->req.type = FWASREQNODE;
	asyreq->pkt.mode.ld[0] = 0;
	asyreq->pkt.mode.ld[1] = 0;
	asyreq->pkt.mode.common.tcode = FWTCODE_PHY;
	if (root_node >= 0)
		asyreq->pkt.mode.ld[1] |= (root_node & 0x3f) << 24 | 1 << 23;
	if (gap_count >= 0)
		asyreq->pkt.mode.ld[1] |= 1 << 22 | (gap_count & 0x3f) << 16;
	asyreq->pkt.mode.ld[2] = ~asyreq->pkt.mode.ld[1];

	printf("send phy_config root_node=%d gap_count=%d\n",
						root_node, gap_count);

	if (ioctl(fd, FW_ASYREQ, asyreq) < 0)
       		err(1, "ioctl");
	free(asyreq);
}

static void
send_link_on(int fd, int node)
{
        struct fw_asyreq *asyreq;

	asyreq = (struct fw_asyreq *)malloc(sizeof(struct fw_asyreq_t) + 12);
	asyreq->req.len = 12;
	asyreq->req.type = FWASREQNODE;
	asyreq->pkt.mode.common.tcode = FWTCODE_PHY;
	asyreq->pkt.mode.ld[1] |= (1 << 30) | ((node & 0x3f) << 24);
	asyreq->pkt.mode.ld[2] = ~asyreq->pkt.mode.ld[1];

	if (ioctl(fd, FW_ASYREQ, asyreq) < 0)
       		err(1, "ioctl");
	free(asyreq);
}

static void
reset_start(int fd, int node)
{
        struct fw_asyreq *asyreq;

	asyreq = (struct fw_asyreq *)malloc(sizeof(struct fw_asyreq_t) + 16);
	asyreq->req.len = 16;
	asyreq->req.type = FWASREQNODE;
	asyreq->pkt.mode.wreqq.dst = FWLOCALBUS | (node & 0x3f);
	asyreq->pkt.mode.wreqq.tlrt = 0;
	asyreq->pkt.mode.wreqq.tcode = FWTCODE_WREQQ;

	asyreq->pkt.mode.wreqq.dest_hi = 0xffff;
	asyreq->pkt.mode.wreqq.dest_lo = 0xf0000000 | RESET_START;

	asyreq->pkt.mode.wreqq.data = htonl(0x1);

	if (ioctl(fd, FW_ASYREQ, asyreq) < 0)
       		err(1, "ioctl");
	free(asyreq);
}

static void
set_pri_req(int fd, int pri_req)
{
	struct fw_devlstreq *data;
	struct fw_devinfo *devinfo;
	u_int32_t max, reg, old;
	int i;

	data = get_dev(fd);
#define BUGET_REG 0xf0000218
	for (i = 0; i < data->info_len; i++) {
		devinfo = &data->dev[i];
		if (!devinfo->status)
			continue;
		reg = read_write_quad(fd, devinfo->eui, BUGET_REG, 1, 0);
		printf("%d %08x:%08x, %08x",
			devinfo->dst, devinfo->eui.hi, devinfo->eui.lo, reg);
		if (reg > 0 && pri_req >= 0) {
			old = (reg & 0x3f);
			max = (reg & 0x3f00) >> 8;
			if (pri_req > max)
				pri_req =  max;
			printf(" 0x%x -> 0x%x\n", old, pri_req);
			read_write_quad(fd, devinfo->eui, BUGET_REG, 0, pri_req);
		} else {
			printf("\n");
		}
	}
	free((void *)data);
}

static void
parse_bus_info_block(u_int32_t *p, int info_len)
{
	int i;
	struct bus_info *bi;

	bi = (struct bus_info *)p;
	printf("bus_name: 0x%04x\n"
		"irmc:%d cmc:%d isc:%d bmc:%d pmc:%d\n"
		"cyc_clk_acc:%d max_rec:%d max_rom:%d\n"
		"generation:%d link_spd:%d\n"
		"EUI64: 0x%08x 0x%08x\n",
		bi->bus_name,
		bi->irmc, bi->cmc, bi->isc, bi->bmc, bi->pmc,
		bi->cyc_clk_acc, bi->max_rec, bi->max_rom,
		bi->generation, bi->link_spd,
		bi->eui64.hi, bi->eui64.lo);
}

static int
get_crom(int fd, int node, void *crom_buf, int len)
{
	struct fw_crom_buf buf;
	int i, error;
	struct fw_devlstreq *data;

	data = get_dev(fd);

	for (i = 0; i < data->info_len; i++) {
		if (data->dev[i].dst == node && data->dev[i].eui.lo != 0)
			break;
	}
	if (i == data->info_len)
		errx(1, "no such node %d.", node);
	else
		buf.eui = data->dev[i].eui;
	free((void *)data);

	buf.len = len;
	buf.ptr = crom_buf;
	bzero(crom_buf, len);
	if ((error = ioctl(fd, FW_GCROM, &buf)) < 0) {
       		err(1, "ioctl");
	}

	return error;
}

static void
show_crom(u_int32_t *crom_buf)
{
	int i;
	struct crom_context cc;
	char *desc, info[256];
	static char *key_types = "ICLD";
	struct csrreg *reg;
	struct csrdirectory *dir;
	struct csrhdr *hdr;
	u_int16_t crc;

	printf("first quad: 0x%08x ", *crom_buf);
	hdr = (struct csrhdr *)crom_buf;
	if (hdr->info_len == 1) {
		/* minimum ROM */
		struct csrreg *reg;
		reg = (struct csrreg *)hdr;
		printf("verndor ID: 0x%06x\n",  reg->val);
		return;
	}
	printf("info_len=%d crc_len=%d crc=0x%04x",
		hdr->info_len, hdr->crc_len, hdr->crc);
	crc = crom_crc(crom_buf+1, hdr->crc_len);
	if (crc == hdr->crc)
		printf("(OK)\n");
	else
		printf("(NG)\n");
	parse_bus_info_block(crom_buf+1, hdr->info_len);

	crom_init_context(&cc, crom_buf);
	dir = cc.stack[0].dir;
	if (!dir) {
		printf("no root directory - giving up\n");
		return;
	}
	printf("root_directory: len=0x%04x(%d) crc=0x%04x",
			dir->crc_len, dir->crc_len, dir->crc);
	crc = crom_crc((u_int32_t *)&dir->entry[0], dir->crc_len);
	if (crc == dir->crc)
		printf("(OK)\n");
	else
		printf("(NG)\n");
	if (dir->crc_len < 1)
		return;
	while (cc.depth >= 0) {
		desc = crom_desc(&cc, info, sizeof(info));
		reg = crom_get(&cc);
		for (i = 0; i < cc.depth; i++)
			printf("\t");
		printf("%02x(%c:%02x) %06x %s: %s\n",
			reg->key,
			key_types[(reg->key & CSRTYPE_MASK)>>6],
			reg->key & CSRKEY_MASK, reg->val,
			desc, info);
		crom_next(&cc);
	}
}

{'fraction_non_alphanumeric': 0.1609590853755359, 'fraction_numerical': 0.02223045572434235, 'mean_word_length': 3.7046812749003983, 'pattern_counts': {'":': 0, '<': 83, '<?xml version=': 0, '>': 237, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5084935', 'n_tokens_mistral': 8495, 'n_tokens_neox': 7862, 'n_words': 1959}