1/*-
2 * Copyright (c) 2008 Sam Leffler, Errno Consulting
3 * 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 *    without modification.
11 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12 *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
13 *    redistribution must be conditioned upon including a substantially
14 *    similar Disclaimer requirement for further binary redistribution.
15 *
16 * NO WARRANTY
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
20 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
22 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27 * THE POSSIBILITY OF SUCH DAMAGES.
28 *
29 * $FreeBSD$
30 */
31#include "diag.h"
32
33#include "ah.h"
34#include "ah_diagcodes.h"
35
36#include <getopt.h>
37#include <errno.h>
38#include <err.h>
39#include <stdlib.h>
40#include <string.h>
41#include <ctype.h>
42
43struct	ath_diag atd;
44int	s;
45const char *progname;
46
47/* XXX this should likely be defined somewhere in the HAL */
48/* XXX This is a lot larger than the v14 ROM */
49#define	MAX_EEPROM_SIZE		16384
50
51uint16_t eep[MAX_EEPROM_SIZE];
52
53static void
54usage()
55{
56	fprintf(stderr, "	%s [-i ifname] -d <dumpfile>\n", progname);
57	exit(-1);
58}
59
60#define	NUM_PER_LINE	8
61
62static void
63do_eeprom_dump(const char *dumpfile, uint16_t *eebuf, int eelen)
64{
65	FILE *fp;
66	int i;
67
68	fp = fopen(dumpfile, "w");
69	if (!fp) {
70		err(1, "fopen");
71	}
72
73	/* eelen is in bytes; eebuf is in 2 byte words */
74	for (i = 0; i < eelen / 2; i++) {
75		if (i % NUM_PER_LINE == 0)
76			fprintf(fp, "%.4x: ", i);
77		fprintf(fp, "%.4x%s", (int32_t)(eebuf[i]), i % NUM_PER_LINE == (NUM_PER_LINE - 1) ? "\n" : " ");
78	}
79	fprintf(fp, "\n");
80	fclose(fp);
81}
82
83int
84main(int argc, char *argv[])
85{
86	FILE *fd = NULL;
87	const char *ifname;
88	int c;
89	const char *dumpname = NULL;
90
91	s = socket(AF_INET, SOCK_DGRAM, 0);
92	if (s < 0)
93		err(1, "socket");
94	ifname = getenv("ATH");
95	if (!ifname)
96		ifname = ATH_DEFAULT;
97
98	progname = argv[0];
99	while ((c = getopt(argc, argv, "d:i:t:")) != -1)
100		switch (c) {
101		case 'd':
102			dumpname = optarg;
103			break;
104		case 'i':
105			ifname = optarg;
106			break;
107		case 't':
108			fd = fopen(optarg, "r");
109			if (fd == NULL)
110				err(-1, "Cannot open %s", optarg);
111			break;
112		default:
113			usage();
114			/*NOTREACHED*/
115		}
116	argc -= optind;
117	argv += optind;
118
119	strncpy(atd.ad_name, ifname, sizeof (atd.ad_name));
120
121	/* Read in the entire EEPROM */
122	atd.ad_id = HAL_DIAG_EEPROM;
123	atd.ad_out_data = (caddr_t) eep;
124	atd.ad_out_size = sizeof(eep);
125	if (ioctl(s, SIOCGATHDIAG, &atd) < 0)
126		err(1, "ioctl: %s", atd.ad_name);
127
128	/* Dump file? Then just write to it */
129	if (dumpname != NULL) {
130		do_eeprom_dump(dumpname, (uint16_t *) &eep, sizeof(eep));
131	}
132	return 0;
133}
134
135