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#include "diag.h"
30
31#include "ah.h"
32#include "ah_diagcodes.h"
33
34#include <getopt.h>
35#include <errno.h>
36#include <err.h>
37#include <stdlib.h>
38#include <string.h>
39#include <ctype.h>
40
41struct	ath_diag atd;
42int	s;
43const char *progname;
44
45/* XXX this should likely be defined somewhere in the HAL */
46/* XXX This is a lot larger than the v14 ROM */
47#define	MAX_EEPROM_SIZE		16384
48
49uint16_t eep[MAX_EEPROM_SIZE];
50
51static void
52usage()
53{
54	fprintf(stderr, "	%s [-i ifname] -d <dumpfile>\n", progname);
55	exit(-1);
56}
57
58#define	NUM_PER_LINE	8
59
60static void
61do_eeprom_dump(const char *dumpfile, uint16_t *eebuf, int eelen)
62{
63	FILE *fp;
64	int i;
65
66	fp = fopen(dumpfile, "w");
67	if (!fp) {
68		err(1, "fopen");
69	}
70
71	/* eelen is in bytes; eebuf is in 2 byte words */
72	for (i = 0; i < eelen / 2; i++) {
73		if (i % NUM_PER_LINE == 0)
74			fprintf(fp, "%.4x: ", i);
75		fprintf(fp, "%.4x%s", (int32_t)(eebuf[i]), i % NUM_PER_LINE == (NUM_PER_LINE - 1) ? "\n" : " ");
76	}
77	fprintf(fp, "\n");
78	fclose(fp);
79}
80
81int
82main(int argc, char *argv[])
83{
84	FILE *fd = NULL;
85	const char *ifname;
86	int c;
87	const char *dumpname = NULL;
88
89	s = socket(AF_INET, SOCK_DGRAM, 0);
90	if (s < 0)
91		err(1, "socket");
92	ifname = getenv("ATH");
93	if (!ifname)
94		ifname = ATH_DEFAULT;
95
96	progname = argv[0];
97	while ((c = getopt(argc, argv, "d:i:t:")) != -1)
98		switch (c) {
99		case 'd':
100			dumpname = optarg;
101			break;
102		case 'i':
103			ifname = optarg;
104			break;
105		case 't':
106			fd = fopen(optarg, "r");
107			if (fd == NULL)
108				err(-1, "Cannot open %s", optarg);
109			break;
110		default:
111			usage();
112			/*NOTREACHED*/
113		}
114	argc -= optind;
115	argv += optind;
116
117	strncpy(atd.ad_name, ifname, sizeof (atd.ad_name));
118
119	/* Read in the entire EEPROM */
120	atd.ad_id = HAL_DIAG_EEPROM;
121	atd.ad_out_data = (caddr_t) eep;
122	atd.ad_out_size = sizeof(eep);
123	if (ioctl(s, SIOCGATHDIAG, &atd) < 0)
124		err(1, "ioctl: %s", atd.ad_name);
125
126	/* Dump file? Then just write to it */
127	if (dumpname != NULL) {
128		do_eeprom_dump(dumpname, (uint16_t *) &eep, sizeof(eep));
129	}
130	return 0;
131}
132
133