ssb-sprom: Put SPROM data in a master table and add Rev. 8
[b43-tools.git] / ssb_sprom / ssb_sprom.c
1 /*
2
3   Broadcom Sonics Silicon Backplane bus SPROM data modification tool
4
5   Copyright (c) 2006-2008 Michael Buesch <mb@bu3sch.de>
6   Copyright (c) 2008 Larry Finger <Larry.Finger@lwfinger.net>
7
8   This program is free software; you can redistribute it and/or modify
9   it under the terms of the GNU General Public License as published by
10   the Free Software Foundation; either version 2 of the License, or
11   (at your option) any later version.
12
13   This program is distributed in the hope that it will be useful,
14   but WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   GNU General Public License for more details.
17
18   You should have received a copy of the GNU General Public License
19   along with this program; see the file COPYING.  If not, write to
20   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21   Boston, MA 02110-1301, USA.
22
23 */
24
25 #include "ssb_sprom.h"
26 #include "utils.h"
27
28 #include <unistd.h>
29 #include <fcntl.h>
30 #include <string.h>
31 #include <errno.h>
32 #include <sys/stat.h>
33
34
35 struct cmdline_args cmdargs;
36 uint8_t sprom_rev;
37 uint16_t sprom_size;
38
39 /* SPROM layouts are described by the following table. The entries are as follows:
40  *
41  * uint16_t rev_mask    A bit mask of the sprom revisions that contain this data
42  * enum valuetype type  The type of datum represented by this table entry
43  * uint16_t length      The length of this datum in bits. A value of 34 means a MAC address.
44  *                      A value of 33 means a 2 character country code.
45  * uint16_t offset      The offset (in bytes) from the start of the sprom.
46  * uint16_t mask        The mask needed to extract this datum from the 16-bit word.
47  * uint16_t shift       The shift needed to right align this datum.
48  * char *desc           The short character string used to describe this datum.
49  * char *label          The long character string that tells the function of this datum.
50  *
51  * The table is ended with a rev_mask of zero.
52  */
53
54 static const struct var_entry sprom_table[] = {
55         { MASK_1_8, VAL_SUBP,   16, 0x04, 0xFFFF, 0x00, "subp",    "Subsystem Product ID" },
56         { MASK_1_8, VAL_SUBV,   16, 0x06, 0xFFFF, 0x00, "subv",    "Subsystem Vendor ID " },
57         { MASK_1_8, VAL_PPID,   16, 0x08, 0xFFFF, 0x00, "ppid",    "PCI Product ID      " },
58         { MASK_2_3, VAL_BFLHI,  16, 0x38, 0xFFFF, 0x00, "bflhi",   "High 16 bits of boardflags" },
59         { MASK_4,   VAL_BFLHI,  16, 0x46, 0xFFFF, 0x00, "bflhi",   "High 16 bits of boardflags" },
60         { MASK_5,   VAL_BFLHI,  16, 0x4C, 0xFFFF, 0x00, "bflhi",   "High 16 bits of boardflags" },
61         { MASK_8,   VAL_BFLHI,  16, 0x86, 0xFFFF, 0x00, "bflhi",   "High 16 bits of boardflags" },
62         { MASK_1_3, VAL_BFL,    16, 0x72, 0xFFFF, 0x00, "bfl",     "Low 16 bits of boardflags " },
63         { MASK_4,   VAL_BFL,    16, 0x44, 0xFFFF, 0x00, "bfl",     "Low 16 bits of boardflags " },
64         { MASK_5,   VAL_BFL,    16, 0x4A, 0xFFFF, 0x00, "bfl",     "Low 16 bits of boardflags " },
65         { MASK_8,   VAL_BFL,    16, 0x84, 0xFFFF, 0x00, "bfl",     "Low 16 bits of boardflags " },
66         { MASK_1_2, VAL_BGMAC,  34, 0x48, 0xFFFF, 0x00, "bgmac",   "MAC Address for 802.11b/g" },
67         { MASK_3,   VAL_BGMAC,  34, 0x4A, 0xFFFF, 0x00, "bgmac",   "MAC Address for 802.11b/g" },
68         { MASK_4,   VAL_BGMAC,  34, 0x4C, 0xFFFF, 0x00, "macadr",  "MAC Address" },
69         { MASK_5,   VAL_BGMAC,  34, 0x52, 0xFFFF, 0x00, "macadr",  "MAC Address" },
70         { MASK_8,   VAL_BGMAC,  34, 0x8C, 0xFFFF, 0x00, "macadr",  "MAC Address" },
71         { MASK_1_2, VAL_ETMAC,  34, 0x4E, 0xFFFF, 0x00, "etmac",   "MAC Address for ethernet " },
72         { MASK_1_2, VAL_AMAC,   34, 0x54, 0xFFFF, 0x00, "amac",    "MAC Address for 802.11a  " },
73         { MASK_1_3, VAL_ET0PHY,  5, 0x5A, 0x001F, 0x00, "et0phy",  "Ethernet phy settings(0)" },
74         { MASK_1_3, VAL_ET1PHY,  5, 0x5A, 0x03E0, 0x05, "et1phy",  "Ethernet phy settings(1)" },
75         { MASK_1_3, VAL_ET0MDC,  1, 0x5A, 0x4000, 0x0E, "et0mdc",  "MDIO for ethernet 0" },
76         { MASK_1_3, VAL_ET1MDC,  1, 0x5A, 0x8000, 0x0F, "et1mdc",  "MDIO for ethernet 1" },
77         { MASK_1_3, VAL_BREV,    8, 0x5C, 0x00FF, 0x00, "brev",    "Board revision" },
78         { MASK_4_5, VAL_BREV,    8, 0x42, 0x00FF, 0x00, "brev",    "Board revision" },
79         { MASK_8,   VAL_BREV,    8, 0x82, 0x00FF, 0x00, "brev",    "Board revision" },
80         { MASK_1_3, VAL_LOC,     4, 0x5C, 0x0300, 0x08, "loc",     "Locale / Country Code" },
81         { MASK_4,   VAL_LOC,    33, 0x52, 0xFFFF, 0x00, "ccode",   "Country Code" },
82         { MASK_5,   VAL_LOC,    33, 0x44, 0xFFFF, 0x00, "ccode",   "Country Code" },
83         { MASK_8,   VAL_LOC,    33, 0x92, 0xFFFF, 0x00, "ccode",   "Country Code" },
84         { MASK_4_5, VAL_REGREV, 16, 0x54, 0xFFFF, 0x00, "regrev",  "Regulatory revision" },
85         { MASK_8,   VAL_REGREV, 16, 0x94, 0xFFFF, 0x00, "regrev",  "Regulatory revision" },
86         { MASK_1_3, VAL_ANTBG0,  1, 0x5C, 0x1000, 0x0C, "antbg0",  "Antenna 0 available for B/G PHY" },
87         { MASK_1_3, VAL_ANTBG1,  1, 0x5C, 0x2000, 0x0D, "antbg1",  "Antenna 1 available for B/G PHY" },
88         { MASK_1_3, VAL_ANTA0,   1, 0x5C, 0x4000, 0x0E, "anta0",   "Antenna 0 available for A PHY" },
89         { MASK_1_3, VAL_ANTA1,   1, 0x5C, 0x8000, 0x0F, "anta1",   "Antenna 1 available for A PHY" },
90         { MASK_4_5, VAL_ANTBG0,  8, 0x5C, 0x00FF, 0x00, "antbg0",  "Available antenna bitmask for 2 GHz" },
91         { MASK_8,   VAL_ANTBG0,  8, 0x9C, 0x00FF, 0x00, "antbg0",  "Available antenna bitmask for 2 GHz" },
92         { MASK_4_5, VAL_ANTA0,   8, 0x5C, 0xFF00, 0x08, "anta0",   "Available antenna bitmask for 5 GHz" },
93         { MASK_8,   VAL_ANTA0,   8, 0x9C, 0xFF00, 0x08, "anta0",   "Available antenna bitmask for 5 GHz" },
94         { MASK_1_3, VAL_ANTGA,   8, 0x74, 0xFF00, 0x08, "antga" ,  "Antenna gain (5 GHz)" },
95         { MASK_1_3, VAL_ANTGBG,  8, 0x74, 0x00FF, 0x00, "antgbg",  "Antenna gain (2 GHz)" },
96         { MASK_4_5, VAL_ANTG0,   8, 0x5E, 0x00FF, 0x00, "antg0",   "Antenna 0 gain" },
97         { MASK_4_5, VAL_ANTG1,   8, 0x5E, 0xFF00, 0x08, "antg1",   "Antenna 1 gain" },
98         { MASK_4_5, VAL_ANTG2,   8, 0x60, 0x00FF, 0x00, "antg2",   "Antenna 2 gain" },
99         { MASK_4_5, VAL_ANTG3,   8, 0x60, 0xFF00, 0x08, "antg3",   "Antenna 3 gain" },
100         { MASK_8,   VAL_ANTG0,   8, 0x9E, 0x00FF, 0x00, "antg0",   "Antenna 0 gain" },
101         { MASK_8,   VAL_ANTG1,   8, 0x9E, 0xFF00, 0x08, "antg1",   "Antenna 1 gain" },
102         { MASK_8,   VAL_ANTG2,   8, 0xA0, 0x00FF, 0x00, "antg2",   "Antenna 2 gain" },
103         { MASK_8,   VAL_ANTG3,   8, 0xA0, 0xFF00, 0x08, "antg3",   "Antenna 3 gain" },
104         { MASK_1_3, VAL_PA0B0,  16, 0x5E, 0xFFFF, 0x00, "pa0b0",   "Power Amplifier W0 PAB0" },
105         { MASK_1_3, VAL_PA0B1,  16, 0x60, 0xFFFF, 0x00, "pa0b1",   "Power Amplifier W0 PAB1" },
106         { MASK_1_3, VAL_PA0B2,  16, 0x62, 0xFFFF, 0x00, "pa0b2",   "Power Amplifier W0 PAB2" },
107         { MASK_1_3, VAL_PA1B0,  16, 0x6A, 0xFFFF, 0x00, "pa1b0",   "Power Amplifier W1 PAB0" },
108         { MASK_1_3, VAL_PA1B1,  16, 0x6C, 0xFFFF, 0x00, "pa1b1",   "Power Amplifier W1 PAB1" },
109         { MASK_1_3, VAL_PA1B2,  16, 0x6E, 0xFFFF, 0x00, "pa1b2",   "Power Amplifier W1 PAB2" },
110         { MASK_1_3, VAL_LED0,    8, 0x64, 0x00FF, 0x00, "led0",    "LED 0 behavior" },
111         { MASK_1_3, VAL_LED1,    8, 0x64, 0xFF00, 0x08, "led1",    "LED 1 behavior" },
112         { MASK_1_3, VAL_LED2,    8, 0x66, 0x00FF, 0x00, "led2",    "LED 2 behavior" },
113         { MASK_1_3, VAL_LED3,    8, 0x66, 0xFF00, 0x08, "led3",    "LED 3 behavior" },
114         { MASK_4,   VAL_LED0,    8, 0x56, 0x00FF, 0x00, "led0",    "LED 0 behavior" },
115         { MASK_4,   VAL_LED1,    8, 0x56, 0xFF00, 0x08, "led1",    "LED 1 behavior" },
116         { MASK_4,   VAL_LED2,    8, 0x58, 0x00FF, 0x00, "led2",    "LED 2 behavior" },
117         { MASK_4,   VAL_LED3,    8, 0x58, 0xFF00, 0x08, "led3",    "LED 3 behavior" },
118         { MASK_5,   VAL_LED0,    8, 0x76, 0x00FF, 0x00, "led0",    "LED 0 behavior" },
119         { MASK_5,   VAL_LED1,    8, 0x76, 0xFF00, 0x08, "led1",    "LED 1 behavior" },
120         { MASK_5,   VAL_LED2,    8, 0x78, 0x00FF, 0x00, "led2",    "LED 2 behavior" },
121         { MASK_5,   VAL_LED3,    8, 0x78, 0xFF00, 0x08, "led3",    "LED 3 behavior" },
122         { MASK_1_3, VAL_MAXPBG,  8, 0x68, 0x00FF, 0x00, "maxpbg",  "B/G PHY max power out" },
123         { MASK_4_5, VAL_MAXPBG,  8, 0x80, 0x00FF, 0x00, "maxpbg",  "Max power 2GHz - Path 1" },
124         { MASK_8,   VAL_MAXPBG,  8, 0xC0, 0x00FF, 0x00, "maxpbg",  "Max power 2GHz - Path 1" },
125         { MASK_1_3, VAL_MAXPA,   8, 0x68, 0xFF00, 0x08, "maxpa",   "A PHY max power out  " },
126         { MASK_4_5, VAL_MAXPA,   8, 0x8A, 0x00FF, 0x00, "maxpa",   "Max power 5GHz - Path 1" },
127         { MASK_8,   VAL_MAXPA,   8, 0xCA, 0xFF00, 0x08, "maxpa",   "Max power 5GHz - Path 1" },
128         { MASK_1_3, VAL_ITSSIBG, 8, 0x70, 0x00FF, 0x00, "itssibg", "Idle TSSI target 2 GHz" },
129         { MASK_1_3, VAL_ITSSIA,  8, 0x70, 0xFF00, 0x08, "itssia",  "Idle TSSI target 5 GHz" },
130         { MASK_4_5, VAL_ITSSIBG, 8, 0x80, 0xFF00, 0x08, "itssibg", "Idle TSSI target 2 GHz - Path 1" },
131         { MASK_4_5, VAL_ITSSIA,  8, 0x8A, 0xFF00, 0x08, "itssia",  "Idle TSSI target 5 GHz - Path 1" },
132         { MASK_8,   VAL_ITSSIBG, 8, 0xC0, 0xFF00, 0x08, "itssibg", "Idle TSSI target 2 GHz - Path 1" },
133         { MASK_8,   VAL_ITSSIA,  8, 0xCA, 0xFF00, 0x08, "itssia",  "Idle TSSI target 5 GHz - Path 1" },
134         { MASK_8,   VAL_TPI2G0, 16, 0x62, 0xFFFF, 0x00, "tpi2g0",  "TX Power Index 2GHz" },
135         { MASK_8,   VAL_TPI2G1, 16, 0x64, 0xFFFF, 0x00, "tpi2g1",  "TX Power Index 2GHz" },
136         { MASK_8,   VAL_TPI5GM0,16, 0x66, 0xFFFF, 0x00, "tpi5gm0", "TX Power Index 5GHz middle subband" },
137         { MASK_8,   VAL_TPI5GM1,16, 0x68, 0xFFFF, 0x00, "tpi5gm1", "TX Power Index 5GHz middle subband" },
138         { MASK_8,   VAL_TPI5GL0,16, 0x6A, 0xFFFF, 0x00, "tpi5gl0", "TX Power Index 5GHz low subband   " },
139         { MASK_8,   VAL_TPI5GL1,16, 0x6C, 0xFFFF, 0x00, "tpi5gl1", "TX Power Index 5GHz low subband   " },
140         { MASK_8,   VAL_TPI5GH0,16, 0x6E, 0xFFFF, 0x00, "tpi5gh0", "TX Power Index 5GHz high subband  " },
141         { MASK_8,   VAL_TPI5GH1,16, 0x70, 0xFFFF, 0x00, "tpi5gh1", "TX Power Index 5GHz high subband  " },
142         { MASK_8,   VAL_2CCKPO, 16, 0x140,0xFFFF, 0x00, "cckpo2g", "2 GHz CCK power offset " },
143         { MASK_8,   VAL_2OFDMPO,32, 0x142,0xFFFF, 0x00, "ofdm2g",  "2 GHz OFDM power offset" },
144         { MASK_8,   VAL_5MPO,   32, 0x146,0xFFFF, 0x00, "ofdm5gm", "5 GHz OFDM middle subband power offset" },
145         { MASK_8,   VAL_5LPO,   32, 0x14A,0xFFFF, 0x00, "ofdm5gl", "5 GHz OFDM low subband power offset   " },
146         { MASK_8,   VAL_5HPO,   32, 0x14E,0xFFFF, 0x00, "ofdm5gh", "5 GHz OFDM high subband power offset  " },
147         { MASK_8,   VAL_2MCSPO, 16, 0x152,0xFFFF, 0x00, "mcspo2",  "2 GHz MCS power offset" },
148         { MASK_8,   VAL_5MMCSPO,16, 0x162,0xFFFF, 0x00, "mcspo5m", "5 GHz middle subband MCS power offset" },
149         { MASK_8,   VAL_5LMCSPO,16, 0x172,0xFFFF, 0x00, "mcspo5l", "5 GHz low subband MCS power offset   " },
150         { MASK_8,   VAL_5HMCSPO,16, 0x182,0xFFFF, 0x00, "mcspo5h", "5 GHz high subband MCS power offset  " },
151         { MASK_8,   VAL_CCDPO,  16, 0x192,0xFFFF, 0x00, "ccdpo",   "CCD power offset  " },
152         { MASK_8,   VAL_STBCPO, 16, 0x194,0xFFFF, 0x00, "stbcpo",  "STBC power offset " },
153         { MASK_8,   VAL_BW40PO, 16, 0x196,0xFFFF, 0x00, "bw40po",  "BW40 power offset " },
154         { MASK_8,   VAL_BWDUPPO,16, 0x198,0xFFFF, 0x00, "bwduppo", "BWDUP power offset" },
155         { MASK_4_5, VAL_TPI2G0, 16, 0x62, 0xFFFF, 0x00, "tpi2g0",  "TX Power Index 2GHz" },
156         { MASK_4_5, VAL_TPI2G1, 16, 0x64, 0xFFFF, 0x00, "tpi2g1",  "TX Power Index 2GHz" },
157         { MASK_4_5, VAL_TPI5GM0,16, 0x66, 0xFFFF, 0x00, "tpi5gm0", "TX Power Index 5GHz middle subband" },
158         { MASK_4_5, VAL_TPI5GM1,16, 0x68, 0xFFFF, 0x00, "tpi5gm1", "TX Power Index 5GHz middle subband" },
159         { MASK_4_5, VAL_TPI5GL0,16, 0x6A, 0xFFFF, 0x00, "tpi5gl0", "TX Power Index 5GHz low subband   " },
160         { MASK_4_5, VAL_TPI5GL1,16, 0x6C, 0xFFFF, 0x00, "tpi5gl1", "TX Power Index 5GHz low subband   " },
161         { MASK_4_5, VAL_TPI5GH0,16, 0x6E, 0xFFFF, 0x00, "tpi5gh0", "TX Power Index 5GHz high subband  " },
162         { MASK_4_5, VAL_TPI5GH1,16, 0x70, 0xFFFF, 0x00, "tpi5gh1", "TX Power Index 5GHz high subband  " },
163         { MASK_4_5, VAL_2CCKPO, 16, 0x138,0xFFFF, 0x00, "cckpo2g", "2 GHz CCK power offset " },
164         { MASK_4_5, VAL_2OFDMPO,32, 0x13A,0xFFFF, 0x00, "ofdm2g",  "2 GHz OFDM power offset" },
165         { MASK_4_5, VAL_5MPO,   32, 0x13E,0xFFFF, 0x00, "ofdm5gm", "5 GHz OFDM middle subband power offset" },
166         { MASK_4_5, VAL_5LPO,   32, 0x142,0xFFFF, 0x00, "ofdm5gl", "5 GHz OFDM low subband power offset   " },
167         { MASK_4_5, VAL_5HPO,   32, 0x146,0xFFFF, 0x00, "ofdm5gh", "5 GHz OFDM high subband power offset  " },
168         { MASK_4_5, VAL_2MCSPO, 16, 0x14A,0xFFFF, 0x00, "mcspo2",  "2 GHz MCS power offset" },
169         { MASK_4_5, VAL_5MMCSPO,16, 0x15A,0xFFFF, 0x00, "mcspo5m", "5 GHz middle subband MCS power offset" },
170         { MASK_4_5, VAL_5LMCSPO,16, 0x16A,0xFFFF, 0x00, "mcspo5l", "5 GHz low subband MCS power offset   " },
171         { MASK_4_5, VAL_5HMCSPO,16, 0x17A,0xFFFF, 0x00, "mcspo5h", "5 GHz high subband MCS power offset  " },
172         { MASK_4_5, VAL_CCDPO,  16, 0x18A,0xFFFF, 0x00, "ccdpo",   "CCD power offset  " },
173         { MASK_4_5, VAL_STBCPO, 16, 0x18C,0xFFFF, 0x00, "stbcpo",  "STBC power offset " },
174         { MASK_4_5, VAL_BW40PO, 16, 0x18E,0xFFFF, 0x00, "bw40po",  "BW40 power offset " },
175         { MASK_4_5, VAL_BWDUPPO,16, 0x190,0xFFFF, 0x00, "bwduppo", "BWDUP power offset" },
176         /* per path variables are below here - only path 1 decoded for now */
177         { MASK_4_5, VAL_PA0B0,  16, 0xC2, 0xFFFF, 0x00, "pa0b0",   "Path 1: Power Amplifier W0 PAB0" },
178         { MASK_4_5, VAL_PA0B1,  16, 0xC4, 0xFFFF, 0x00, "pa0b1",   "Path 1: Power Amplifier W0 PAB1" },
179         { MASK_4_5, VAL_PA0B2,  16, 0xC6, 0xFFFF, 0x00, "pa0b2",   "Path 1: Power Amplifier W0 PAB2" },
180         { MASK_4_5, VAL_PA0B3,  16, 0xC8, 0xFFFF, 0x00, "pa0b3",   "Path 1: Power Amplifier W0 PAB3" },
181         { MASK_4_5, VAL_PA1B0,   8, 0xCC, 0x00FF, 0x00, "pam5h",   "Path 1: 5 GHz high subband PAM " },
182         { MASK_4_5, VAL_PA1B0,   8, 0xCC, 0xFF00, 0x08, "pam5l",   "Path 1: 5 GHz low subband PAM  " },
183         { MASK_4_5, VAL_5MPA0,  16, 0xCE, 0xFFFF, 0x00, "pa5m0",   "Path 1: 5 GHz Power Amplifier middle 0" },
184         { MASK_4_5, VAL_5MPA1,  16, 0xD0, 0xFFFF, 0x00, "pa5m1",   "Path 1: 5 GHz Power Amplifier middle 1" },
185         { MASK_4_5, VAL_5MPA2,  16, 0xD2, 0xFFFF, 0x00, "pa5m2",   "Path 1: 5 GHz Power Amplifier middle 2" },
186         { MASK_4_5, VAL_5MPA3,  16, 0xD4, 0xFFFF, 0x00, "pa5m3",   "Path 1: 5 GHz Power Amplifier middle 3" },
187         { MASK_4_5, VAL_5LPA0,  16, 0xD6, 0xFFFF, 0x00, "pa5l0",   "Path 1: 5 GHz Power Amplifier low 0   " },
188         { MASK_4_5, VAL_5LPA1,  16, 0xD8, 0xFFFF, 0x00, "pa5l1",   "Path 1: 5 GHz Power Amplifier low 1   " },
189         { MASK_4_5, VAL_5LPA2,  16, 0xDA, 0xFFFF, 0x00, "pa5l2",   "Path 1: 5 GHz Power Amplifier low 2   " },
190         { MASK_4_5, VAL_5LPA3,  16, 0xDC, 0xFFFF, 0x00, "pa5l3",   "Path 1: 5 GHz Power Amplifier low 3   " },
191         { MASK_4_5, VAL_5HPA0,  16, 0xDE, 0xFFFF, 0x00, "pa5h0",   "Path 1: 5 GHz Power Amplifier high 0  " },
192         { MASK_4_5, VAL_5HPA1,  16, 0xE0, 0xFFFF, 0x00, "pa5h1",   "Path 1: 5 GHz Power Amplifier high 1  " },
193         { MASK_4_5, VAL_5HPA2,  16, 0xE2, 0xFFFF, 0x00, "pa5h2",   "Path 1: 5 GHz Power Amplifier high 2  " },
194         { MASK_4_5, VAL_5HPA3,  16, 0xE4, 0xFFFF, 0x00, "pa5h3",   "Path 1: 5 GHz Power Amplifier high 3  " },
195         { MASK_8,   VAL_PA0B0,  16, 0xC2, 0xFFFF, 0x00, "pa0b0",   "SISO (Path 1) Power Amplifier W0 PAB0" },
196         { MASK_8,   VAL_PA0B1,  16, 0xC4, 0xFFFF, 0x00, "pa0b1",   "SISO (Path 1) Power Amplifier W0 PAB1" },
197         { MASK_8,   VAL_PA0B2,  16, 0xC6, 0xFFFF, 0x00, "pa0b2",   "SISO (Path 1) Power Amplifier W0 PAB2" },
198         { MASK_8,   VAL_PA1B0,  16, 0xCC, 0xFFFF, 0x00, "pa5m0",   "SISO (Path 1) 5 GHz Power Amplifier middle 0" },
199         { MASK_8,   VAL_PA1B1,  16, 0xCE, 0xFFFF, 0x00, "pa5m1",   "SISO (Path 1) 5 GHz Power Amplifier middle 1" },
200         { MASK_8,   VAL_PA1B2,  16, 0xD0, 0xFFFF, 0x00, "pa5m2",   "SISO (Path 1) 5 GHz Power Amplifier middle 2" },
201         { MASK_8,   VAL_5MPA0,  16, 0xD2, 0xFFFF, 0x00, "pa5l0",   "SISO (Path 1) 5 GHz Power Amplifier low 0   " },
202         { MASK_8,   VAL_5MPA1,  16, 0xD4, 0xFFFF, 0x00, "pa5l1",   "SISO (Path 1) 5 GHz Power Amplifier low 1   " },
203         { MASK_8,   VAL_5MPA2,  16, 0xD6, 0xFFFF, 0x00, "pa5l2",   "SISO (Path 1) 5 GHz Power Amplifier low 2   " },
204         { MASK_8,   VAL_5LPA0,  16, 0xD8, 0xFFFF, 0x00, "pa5h0",   "SISO (Path 1) 5 GHz Power Amplifier high 0  " },
205         { MASK_8,   VAL_5LPA1,  16, 0xDA, 0xFFFF, 0x00, "pa5h1",   "SISO (Path 1) 5 GHz Power Amplifier high 1  " },
206         { MASK_8,   VAL_5LPA2,  16, 0xDC, 0xFFFF, 0x00, "pa5h2",   "SISO (Path 1) 5 GHz Power Amplifier high 2  " },
207
208         { 0, },
209 };
210
211 /* find an item in the table by sprom revision and short description
212  * returns length and type. The function value is -1 if the item is not
213  * found, otherwise 0.
214  */
215
216 static int locate_item_by_desc(int rev, enum valuetype *type, uint16_t *length, char *desc)
217 {
218         int i;
219
220         for (i = 0; ; i++) {
221                 if (sprom_table[i].rev_mask == 0)
222                         return -1;      /* end of table */
223                 if ((sprom_table[i].rev_mask & rev) &&
224                      (!strcmp(sprom_table[i].desc, desc))) {
225                 /* this is the record we want */
226                         *length = sprom_table[i].length;
227                         *type = sprom_table[i].type;
228                         return 0;
229                 }
230         }
231         return -1; /* flow cannot reach here, but this statement makes gcc happy */
232 }
233
234 /* find an item in the table by sprom revision and type
235  * return length, offset, mask, shift, desc, and label
236  * The function returns -1 if no item matches the request.
237  */
238
239 static int locate_item_rev(int rev, enum valuetype type, uint16_t *length, uint16_t *offset,
240                            uint16_t *mask, uint16_t *shift, char *desc, char *label)
241 {
242         int i;
243
244         for (i = 0; ; i++) {
245                 if (sprom_table[i].rev_mask == 0)
246                         return -1;      /* end of table */
247                 if ((sprom_table[i].rev_mask & rev) &&
248                      (sprom_table[i].type == type)) {
249                 /* this is the record we want */
250                         *length = sprom_table[i].length;
251                         *offset = sprom_table[i].offset;
252                         *mask = sprom_table[i].mask;
253                         *shift = sprom_table[i].shift;
254                         strcpy(desc, sprom_table[i].desc);
255                         strcpy(label, sprom_table[i].label);
256                         return 0;
257                 }
258         }
259         return -1; /* flow cannot reach here, but this statement makes gcc happy */
260 }
261
262 static int check_rev(uint16_t rev)
263 {
264         if ((rev < 0) || (rev > 8) || (rev == 6) || (rev == 7)) {
265                 prerror("\nIllegal value for sprom_rev\n");
266                 return -1;
267         }
268         return 0;
269 }
270
271 static int hexdump_sprom(const uint8_t *sprom, char *buffer, size_t bsize)
272 {
273         int i, pos = 0;
274
275         for (i = 0; i < sprom_size; i++) {
276                 pos += snprintf(buffer + pos, bsize - pos - 1,
277                                 "%02X", sprom[i] & 0xFF);
278         }
279
280         return pos + 1;
281 }
282
283 static uint8_t sprom_crc(const uint8_t *sprom)
284 {
285         int i;
286         uint8_t crc = 0xFF;
287
288         for (i = 0; i < sprom_size - 1; i++)
289                 crc = crc8(crc, sprom[i]);
290         crc ^= 0xFF;
291
292         return crc;
293 }
294
295 static int write_output_binary(int fd, const uint8_t *sprom)
296 {
297         ssize_t w;
298
299         w = write(fd, sprom, sprom_size);
300         if (w < 0)
301                 return -1;
302
303         return 0;
304 }
305
306 static int write_output_hex(int fd, const uint8_t *sprom)
307 {
308         ssize_t w;
309         char tmp[SPROM4_SIZE * 2 + 10] = { 0 };
310
311         hexdump_sprom(sprom, tmp, sizeof(tmp));
312         prinfo("Raw output:  %s\n", tmp);
313         w = write(fd, tmp, sprom_size * 2);
314         if (w < 0)
315                 return -1;
316
317         return 0;
318 }
319
320 static int write_output(int fd, const uint8_t *sprom)
321 {
322         int err;
323
324         if (cmdargs.outfile) {
325                 err = ftruncate(fd, 0);
326                 if (err) {
327                         prerror("Could not truncate --outfile %s\n",
328                                 cmdargs.outfile);
329                         return -1;
330                 }
331         }
332
333         if (cmdargs.bin_mode)
334                 err = write_output_binary(fd, sprom);
335         else
336                 err = write_output_hex(fd, sprom);
337         if (err)
338                 prerror("Could not write output data.\n");
339
340         return err;
341 }
342
343 static int modify_value(uint8_t *sprom,
344                         struct cmdline_vparm *vparm)
345 {
346         const uint32_t v = vparm->u.value;
347         uint16_t tmp = 0;
348         uint16_t offset;
349         char desc[100];
350         char label[200];
351         uint16_t length;
352         uint16_t mask;
353         uint16_t shift;
354         uint16_t old_value;
355         uint32_t value = 0;
356
357         int rev_bit = BIT(sprom_rev);
358
359
360         if (vparm->type == VAL_RAW) {
361                 sprom[vparm->u.raw.offset] = vparm->u.raw.value;
362                 return 0;
363         }
364         if (locate_item_rev(rev_bit, vparm->type, &length, &offset, &mask,
365                             &shift, desc, label))
366                 return -1;
367
368         if (length < 32) {
369                 old_value = sprom[offset + 0];
370                 old_value |= sprom[offset + 1] << 8;
371                 if (length < 16) {
372                         tmp = v << shift;
373                         value = (old_value & ~mask) | tmp;
374                 } else
375                         value = v;
376                 sprom[offset + 0] = (value & 0x00FF);
377                 sprom[offset + 1] = (value & 0xFF00) >> 8;
378         } else if (length == 32) {
379                 value = v;
380                 sprom[offset + 0] = (value & 0x00FF);
381                 sprom[offset + 1] = (value >> 8) & 0xFF;
382                 sprom[offset + 2] = (value >> 16) & 0xFF;
383                 sprom[offset + 3] = (value >> 24) & 0xFF;
384         } else if (length == 34) { /* MAC address */
385                 sprom[offset + 1] = vparm->u.mac[0];
386                 sprom[offset + 0] = vparm->u.mac[1];
387                 sprom[offset + 3] = vparm->u.mac[2];
388                 sprom[offset + 2] = vparm->u.mac[3];
389                 sprom[offset + 5] = vparm->u.mac[4];
390                 sprom[offset + 4] = vparm->u.mac[5];
391         } else if (length == 33) { /* country code */
392                 sprom[offset + 1] = vparm->u.ccode[0];
393                 sprom[offset + 0] = vparm->u.ccode[1];
394         } else {
395                 prerror("Incorrect value for length (%d)\n", length);
396                 exit(1);
397         }
398
399         return 0;
400 }
401
402 static int modify_sprom(uint8_t *sprom)
403 {
404         struct cmdline_vparm *vparm;
405         int i;
406         int modified = 0;
407         uint8_t crc;
408
409         for (i = 0; i < cmdargs.nr_vparm; i++) {
410                 vparm = &(cmdargs.vparm[i]);
411                 if (!vparm->set)
412                         continue;
413                 modify_value(sprom, vparm);
414                 modified = 1;
415         }
416         if (modified) {
417                 /* Recalculate the CRC. */
418                 crc = sprom_crc(sprom);
419                 sprom[sprom_size - 1] = crc;
420         }
421
422         return modified;
423 }
424
425 static void display_value(const uint8_t *sprom,
426                           struct cmdline_vparm *vparm)
427 {
428         char desc[100];
429         char label[200];
430         char buffer[50];
431         char tbuf[2];
432         uint16_t offset;
433         uint16_t length;
434         uint16_t mask;
435         uint16_t shift;
436         uint32_t value = 0;
437         int rev_bit = BIT(sprom_rev);
438         const uint8_t *p;
439         int i;
440
441         if (locate_item_rev(rev_bit, vparm->type, &length, &offset, &mask,
442                             &shift, desc, label))
443                 return;
444         if (length < 32) {
445                 value = sprom[offset + 0];
446                 value |= sprom[offset + 1] << 8;
447                 value = (value & mask) >> shift;
448         } else if (length == 32) {
449                 value = sprom[offset + 0];
450                 value |= sprom[offset + 1] << 8;
451                 value |= sprom[offset + 2] << 16;
452                 value |= sprom[offset + 3] << 24;
453         }
454         sprintf(buffer, "SPROM(0x%03X), %s,        ", offset, desc);
455         buffer[25] = '\0';
456         p = &(sprom[offset]);
457
458         switch (length) {
459         case 1:
460                 prdata("%s%s = %s\n", buffer, label, value ? "ON" : "OFF");
461                 break;
462         case 4:
463                 prdata("%s%s = 0x%01X\n", buffer, label, (value & 0xF));
464                 break;
465         case 5:
466                 prdata("%s%s = 0x%02X\n", buffer, label, (value & 0x1F));
467                 break;
468         case 8:
469                 prdata("%s%s = 0x%02X\n", buffer, label, (value & 0xFF));
470                 break;
471         case 16:
472                 prdata("%s%s = 0x%04X\n", buffer, label, value);
473                 break;
474         case 32:
475                 prdata("%s%s = 0x%08X\n", buffer, label, value);
476                 break;
477         case 33: /* alphabetic country code */
478                 for (i = 0; i < 2; i++) {
479                         tbuf[i] = p[i];
480                         if (!tbuf[i])   /* if not encoded, the value is zero */
481                                 tbuf[i] = ' ';
482                 }
483                 prdata("%s%s = \"%c%c\"\n", buffer, label, tbuf[1], tbuf[0]);
484                 break;
485         case 34:
486                 /* MAC address. */
487                 prdata("%s%s = %02x:%02x:%02x:%02x:%02x:%02x\n",
488                        buffer, label, p[1], p[0], p[3], p[2], p[5], p[4]);
489                 break;
490         default:
491                 prerror("vparm->bits internal error (%d)\n",
492                         vparm->bits);
493                 exit(1);
494         }
495 }
496
497 static int display_sprom(const uint8_t *sprom)
498 {
499         struct cmdline_vparm *vparm;
500         int i;
501
502         for (i = 0; i < cmdargs.nr_vparm; i++) {
503                 vparm = &(cmdargs.vparm[i]);
504                 if (vparm->set)
505                         continue;
506                 display_value(sprom, vparm);
507         }
508
509         return 0;
510 }
511
512 static int validate_input(const uint8_t *sprom)
513 {
514         uint8_t crc, expected_crc;
515
516         crc = sprom_crc(sprom);
517         expected_crc = sprom[sprom_size - 1];
518
519         if (crc != expected_crc) {
520                 prerror("Corrupt input data (crc: 0x%02X, expected: 0x%02X)\n",
521                         crc, expected_crc);
522                 if (!cmdargs.force)
523                         return 1;
524         }
525
526         return 0;
527 }
528
529 static int parse_input(uint8_t *sprom, char *buffer, size_t bsize)
530 {
531         char *input;
532         size_t inlen;
533         size_t cnt;
534         unsigned long parsed;
535         char tmp[SPROM4_SIZE * 2 + 10] = { 0 };
536
537         if (cmdargs.bin_mode) {
538                 /* The input buffer already contains
539                  * the binary sprom data.
540                  */
541                 internal_error_on(bsize != SPROM_SIZE && bsize != SPROM4_SIZE);
542                 memcpy(sprom, buffer, bsize);
543                 return 0;
544         }
545
546         inlen = bsize;
547         input = strchr(buffer, ':');
548         if (input) {
549                 input++;
550                 inlen -= input - buffer;
551         } else
552                 input = buffer;
553
554         if (inlen < SPROM_SIZE * 2) {
555                 prerror("Input data too short\n");
556                 return -1;
557         }
558         for (cnt = 0; cnt < inlen / 2; cnt++) {
559                 memcpy(tmp, input + cnt * 2, 2);
560                 parsed = strtoul(tmp, NULL, 16);
561                 sprom[cnt] = parsed & 0xFF;
562         }
563         /* check for 440 byte versions (V4 and higher) */
564         if (inlen > 300) {
565                 sprom_rev = sprom[SPROM4_SIZE - 2];
566                 sprom_size = SPROM4_SIZE;
567         } else {
568                 sprom_rev = sprom[SPROM_SIZE - 2];
569                 sprom_size = SPROM_SIZE;
570         }
571         if (check_rev(sprom_rev))
572                 exit(1);
573         if (cmdargs.verbose) {
574                 hexdump_sprom(sprom, tmp, sizeof(tmp));
575                 prinfo("Raw input:  %s\n", tmp);
576         }
577
578         return 0;
579 }
580
581 static int read_infile(int fd, char **buffer, size_t *bsize)
582 {
583         struct stat s;
584         int err;
585         ssize_t r;
586
587         err = fstat(fd, &s);
588         if (err) {
589                 prerror("Could not stat input file.\n");
590                 return err;
591         }
592         if (s.st_size == 0) {
593                 prerror("No input data\n");
594                 return -1;
595         }
596         if (cmdargs.bin_mode) {
597                 if (s.st_size != SPROM_SIZE && s.st_size != SPROM4_SIZE) {
598                         prerror("The input data is not SPROM Binary data. "
599                                 "The size must be exactly %d (V1-3) "
600                                 "or %d (V4-8) bytes, "
601                                 "but it is %u bytes\n",
602                                 SPROM_SIZE, SPROM4_SIZE,
603                                 (unsigned int)(s.st_size));
604                         return -1;
605                 }
606         } else {
607                 if (s.st_size > 1024 * 1024) {
608                         prerror("The input data does not look "
609                                 "like SPROM HEX data (too long).\n");
610                         return -1;
611                 }
612         }
613
614         *bsize = s.st_size;
615         if (!cmdargs.bin_mode)
616                 (*bsize)++;
617         *buffer = malloce(*bsize);
618         r = read(fd, *buffer, s.st_size);
619         if (r != s.st_size) {
620                 prerror("Could not read input data.\n");
621                 return -1;
622         }
623         if (!cmdargs.bin_mode)
624                 (*buffer)[r] = '\0';
625
626         return 0;
627 }
628
629 static void close_infile(int fd)
630 {
631         if (cmdargs.infile)
632                 close(fd);
633 }
634
635 static void close_outfile(int fd)
636 {
637         if (cmdargs.outfile)
638                 close(fd);
639 }
640
641 static int open_infile(int *fd)
642 {
643         *fd = STDIN_FILENO;
644         if (!cmdargs.infile)
645                 return 0;
646         *fd = open(cmdargs.infile, O_RDONLY);
647         if (*fd < 0) {
648                 prerror("Could not open --infile %s\n",
649                         cmdargs.infile);
650                 return -1;
651         }
652
653         return 0;
654 }
655
656 static int open_outfile(int *fd)
657 {
658         *fd = STDOUT_FILENO;
659         if (!cmdargs.outfile)
660                 return 0;
661         *fd = open(cmdargs.outfile, O_RDWR | O_CREAT, 0644);
662         if (*fd < 0) {
663                 prerror("Could not open --outfile %s\n",
664                         cmdargs.outfile);
665                 return -1;
666         }
667
668         return 0;
669 }
670
671 static void print_banner(int forceprint)
672 {
673         const char *str = "Broadcom-SSB SPROM data modification tool.\n"
674                           "\n"
675                           "Copyright (C) Michael Buesch\n"
676                           "Licensed under the GNU/GPL version 2 or later\n"
677                           "\n"
678                           "DO NOT USE THIS TOOL. YOU WILL BRICK YOUR DEVICE.\n";
679         if (forceprint)
680                 prdata(str);
681         else
682                 prinfo(str);
683 }
684
685 static void print_usage(int argc, char *argv[])
686 {
687         int tmp;
688         enum valuetype loop;
689         char desc[100];
690         char label[200];
691         char buffer[200];
692         uint16_t offset;
693         uint16_t length;
694         uint16_t mask;
695         uint16_t shift;
696         int rev_bit;
697
698         print_banner(1);
699         prdata("\nUsage: %s [OPTION]\n", argv[0]);
700         prdata("  -i|--input FILE       Input file\n");
701         prdata("  -o|--output FILE      Output file\n");
702         prdata("  -b|--binmode          The Input data is plain binary data and Output will be binary\n");
703         prdata("  -V|--verbose          Be verbose\n");
704         prdata("  -f|--force            Override error checks\n");
705         prdata("  -v|--version          Print version\n");
706         prdata("  -h|--help             Print this help\n");
707         if (sprom_rev == 0) {
708                 prdata("\nThe rest of this help depends on what SPROM version you are using\n\n");
709                 prdata("Please enter it now: ");
710
711                 fgets(label, 50, stdin);
712                 sscanf(label, "%d", &tmp);
713                 sprom_rev = tmp;
714         }
715         if (check_rev(sprom_rev))
716                 exit(1);
717
718         rev_bit = BIT(sprom_rev);
719         prdata("\nValue Parameters:\n");
720         prdata("\n");
721         prdata("  -s|--rawset OFF,VAL   Set a VALue at a byte-OFFset\n");
722         prdata("  -g|--rawget OFF       Get a value at a byte-OFFset\n");
723         prdata("\n");
724         prdata("Predefined values (for displaying (GET) or modification):\n");
725
726         for (loop = 0; loop <= VAL_LAST; loop++) {
727                 if (locate_item_rev(rev_bit, loop, &length, &offset, &mask,
728                             &shift, desc, label))
729                         continue;
730
731                 switch (length) {
732                 case 34:
733                         sprintf(buffer, "  --%s [MAC-ADDR]%30s", desc, " ");
734                         break;
735                 case 33:
736                         sprintf(buffer, "  --%s [2 Char String]%30s", desc, " ");
737                         break;
738                 case 32:
739                         sprintf(buffer, "  --%s [0xFFFFFFFF]%30s", desc, " ");
740                         break;
741                 case 16:
742                         sprintf(buffer, "  --%s [0xFFFF]%30s", desc, " ");
743                         break;
744                 case 8:
745                         sprintf(buffer, "  --%s [0xFF]%30s", desc, " ");
746                         break;
747                 case 5:
748                         sprintf(buffer, "  --%s [0x1F]%30s", desc, " ");
749                         break;
750                 case 4:
751                         sprintf(buffer, "  --%s [0xF]%30s", desc, " ");
752                         break;
753                 case 1:
754                         sprintf(buffer, "  --%s [BOOL]%30s", desc, " ");
755                         break;
756                 default:
757                         prerror("Program error: Incorrect value of item length (%d)\n", length);
758                         exit(1);
759                 }
760                 buffer[28] = '\0';
761                 prdata("%s%s\n", buffer, label);
762         }
763
764         prdata("\n");
765         prdata("  -P|--print-all        Display all values\n");
766         prdata("\n");
767         prdata(" BOOL      is a boolean value. Either 0 or 1\n");
768         prdata(" 0xF..     is a hexadecimal value\n");
769         prdata(" MAC-ADDR  is a MAC address in the format 00:00:00:00:00:00\n");
770         prdata(" If the value parameter is \"GET\", the value will be printed;\n");
771         prdata(" otherwise it is modified.\n");
772 }
773
774 #define ARG_MATCH               0
775 #define ARG_NOMATCH             1
776 #define ARG_ERROR               -1
777
778 static int do_cmp_arg(char **argv, int *pos,
779                       const char *template,
780                       int allow_merged,
781                       char **param)
782 {
783         char *arg;
784         char *next_arg;
785         size_t arg_len, template_len;
786
787         arg = argv[*pos];
788         next_arg = argv[*pos + 1];
789         arg_len = strlen(arg);
790         template_len = strlen(template);
791
792         if (param) {
793                 /* Maybe we have a merged parameter here.
794                  * A merged parameter is "-pfoobar" for example.
795                  */
796                 if (allow_merged && arg_len > template_len) {
797                         if (memcmp(arg, template, template_len) == 0) {
798                                 *param = arg + template_len;
799                                 return ARG_MATCH;
800                         }
801                         return ARG_NOMATCH;
802                 } else if (arg_len != template_len)
803                         return ARG_NOMATCH;
804                 *param = next_arg;
805         }
806         if (strcmp(arg, template) == 0) {
807                 if (param) {
808                         if (*param == 0) {
809                                 prerror("%s needs a parameter\n", arg);
810                                 return ARG_ERROR;
811                         }
812                         /* Skip the parameter on the next iteration. */
813                         (*pos)++;
814                 }
815                 return ARG_MATCH;
816         }
817
818         return ARG_NOMATCH;
819 }
820
821 /* Simple and lean command line argument parsing. */
822 static int cmp_arg(char **argv, int *pos,
823                    const char *long_template,
824                    const char *short_template,
825                    char **param)
826 {
827         int err;
828
829         if (long_template) {
830                 err = do_cmp_arg(argv, pos, long_template, 0, param);
831                 if (err == ARG_MATCH || err == ARG_ERROR)
832                         return err;
833         }
834         err = ARG_NOMATCH;
835         if (short_template)
836                 err = do_cmp_arg(argv, pos, short_template, 1, param);
837         return err;
838 }
839
840 static int parse_err;
841
842 static int arg_match(char **argv, int *i,
843                      const char *long_template,
844                      const char *short_template,
845                      char **param)
846 {
847         int res;
848
849         res = cmp_arg(argv, i, long_template,
850                       short_template, param);
851         if (res == ARG_ERROR) {
852                 parse_err = 1;
853                 return 0;
854         }
855         return (res == ARG_MATCH);
856 }
857
858 static int parse_value(const char *str,
859                        struct cmdline_vparm *vparm,
860                        const char *param)
861 {
862         unsigned long v;
863         int i;
864
865         vparm->set = 1;
866         if (strcmp(str, "GET") == 0 || strcmp(str, "get") == 0) {
867                 vparm->set = 0;
868                 return 0;
869         }
870         if (vparm->bits > 32)
871                 return 0;
872         if (vparm->bits == 1) {
873                 /* This is a boolean value. */
874                 if (strcmp(str, "0") == 0)
875                         vparm->u.value = 0;
876                 else if (strcmp(str, "1") == 0)
877                         vparm->u.value = 1;
878                 else
879                         goto error_bool;
880                 return 1;
881         }
882
883         if (strncmp(str, "0x", 2) != 0)
884                 goto error;
885         str += 2;
886         /* The following logic presents a problem because the offsets
887          * for V4 SPROMs can be greater than 0xFF; however, the arguments
888          * are parsed before the SPROM revision is known. To fix this
889          * problem, if an input is expecting 0xFF-type input, then input
890          * of 0xFFF will be permitted */
891         for (i = 0; i < vparm->bits / 4; i++) {
892                 if (str[i] == '\0')
893                         goto error;
894         }
895         if (str[i] != '\0') {
896                 if (i == 2)
897                         i++;            /* add an extra character */
898                 if (str[i] != '\0')
899                         goto error;
900         }
901         errno = 0;
902         v = strtoul(str, NULL, 16);
903         if (errno)
904                 goto error;
905         vparm->u.value = v;
906
907         return 1;
908 error:
909         if (param) {
910                 prerror("%s value parsing error. Format: 0x", param);
911                 for (i = 0; i < vparm->bits / 4; i++)
912                         prerror("F");
913                 prerror("\n");
914         }
915         return -1;
916
917 error_bool:
918         if (param)
919                 prerror("%s value parsing error. Format: 0 or 1 (boolean)\n", param);
920         return -1;
921 }
922
923 static int parse_ccode(const char *str,
924                        struct cmdline_vparm *vparm,
925                        const char *param)
926 {
927         const char *in = str;
928         char *out = vparm->u.ccode;
929
930         vparm->bits = 33;
931         vparm->set = 1;
932         if (strcmp(str, "GET") == 0 || strcmp(str, "get") == 0) {
933                 vparm->set = 0;
934                 return 0;
935         }
936
937         memcpy(out, in, 2);
938         return 1;
939 }
940
941 static int parse_mac(const char *str,
942                      struct cmdline_vparm *vparm,
943                      const char *param)
944 {
945         int i;
946         char *delim;
947         const char *in = str;
948         uint8_t *out = vparm->u.mac;
949
950         vparm->bits = 34;
951         vparm->set = 1;
952         if (strcmp(str, "GET") == 0 || strcmp(str, "get") == 0) {
953                 vparm->set = 0;
954                 return 0;
955         }
956
957         for (i = 0; ; i++) {
958                 errno = 0;
959                 out[i] = strtoul(in, NULL, 16);
960                 if (errno)
961                         goto error;
962                 if (i == 5) {
963                         if (in[1] != '\0' && in[2] != '\0')
964                                 goto error;
965                         break;
966                 }
967                 delim = strchr(in, ':');
968                 if (!delim)
969                         goto error;
970                 in = delim + 1;
971         }
972
973         return 1;
974 error:
975         prerror("%s MAC parsing error. Format: 00:00:00:00:00:00\n", param);
976         return -1;
977 }
978
979 static int parse_rawset(const char *str,
980                         struct cmdline_vparm *vparm)
981 {
982         char *delim;
983         uint8_t value;
984         uint16_t offset;
985         int err;
986
987         vparm->type = VAL_RAW;
988
989         delim = strchr(str, ',');
990         if (!delim)
991                 goto error;
992         *delim = '\0';
993         err = parse_value(str, vparm, NULL);
994         if (err != 1)
995                 goto error;
996         offset = vparm->u.value;
997         if (offset >= SPROM4_SIZE) {
998                 prerror("--rawset offset too big (>= 0x%02X)\n",
999                         SPROM4_SIZE);
1000                 return -1;
1001         }
1002         err = parse_value(delim + 1, vparm, NULL);
1003         if (err != 1)
1004                 goto error;
1005         value = vparm->u.value;
1006
1007         vparm->u.raw.value = value;
1008         vparm->u.raw.offset = offset;
1009         vparm->set = 1;
1010
1011         return 0;
1012 error:
1013         prerror("--rawset value parsing error. Format: 0xFF,0xFF "
1014                 "(first Offset, second Value)\n");
1015         return -1;
1016 }
1017
1018 static int parse_rawget(const char *str,
1019                         struct cmdline_vparm *vparm)
1020 {
1021         int err;
1022         uint16_t offset;
1023
1024         vparm->type = VAL_RAW;
1025
1026         err = parse_value(str, vparm, "--rawget");
1027         if (err != 1)
1028                 return -1;
1029         offset = vparm->u.value;
1030         if (offset >= SPROM4_SIZE) {
1031                 prerror("--rawget offset too big (>= 0x%02X)\n",
1032                         SPROM4_SIZE);
1033                 return -1;
1034         }
1035
1036         vparm->u.raw.offset = offset;
1037         vparm->type = VAL_RAW;
1038         vparm->set = 0;
1039
1040         return 0;
1041 }
1042
1043 static int generate_printall(void)
1044 {
1045         enum valuetype vt = 0;
1046         int j;
1047
1048         for (vt = 0; vt <= VAL_LAST; vt++) {
1049                 if (cmdargs.nr_vparm == MAX_VPARM) {
1050                         prerror("Too many value parameters.\n");
1051                         return -1;
1052                 }
1053                 for (j = 0; ; j++) {
1054                         enum valuetype type = sprom_table[j].type;
1055                         short mask = sprom_table[j].rev_mask;
1056
1057                         if (mask == 0)
1058                                 break;
1059                         if ((mask & BIT(sprom_rev)) && (type == vt)) {
1060                                 cmdargs.vparm[cmdargs.nr_vparm].type = vt;
1061                                 cmdargs.vparm[cmdargs.nr_vparm].set = 0;
1062                                 cmdargs.vparm[cmdargs.nr_vparm++].bits = sprom_table[j].length;
1063                         }
1064                 }
1065         }
1066         return 0;
1067 }
1068
1069 static int parse_args(int argc, char *argv[], int pass)
1070 {
1071         struct cmdline_vparm *vparm;
1072         int i, err;
1073         char *param;
1074         char *arg;
1075         uint16_t length;
1076         enum valuetype type;
1077
1078         parse_err = 0;
1079         for (i = 1; i < argc; i++) {
1080                 if (cmdargs.nr_vparm == MAX_VPARM) {
1081                         prerror("Too many value parameters.\n");
1082                         return -1;
1083                 }
1084
1085                 if (arg_match(argv, &i, "--version", "-v", 0)) {
1086                         print_banner(1);
1087                         return 1;
1088                 } else if (arg_match(argv, &i, "--help", "-h", 0)) {
1089                         goto out_usage;
1090                 } else if (arg_match(argv, &i, "--input", "-i", &param)) {
1091                         cmdargs.infile = param;
1092                 } else if (arg_match(argv, &i, "--output", "-o", &param)) {
1093                         cmdargs.outfile = param;
1094                 } else if (arg_match(argv, &i, "--verbose", "-V", 0)) {
1095                         cmdargs.verbose = 1;
1096                 } else if (arg_match(argv, &i, "--force", "-n", 0)) {
1097                         cmdargs.force = 1;
1098                 } else if (arg_match(argv, &i, "--binmode", "-b", 0)) {
1099                         cmdargs.bin_mode = 1;
1100                 } else if (pass == 2 && arg_match(argv, &i, "--rawset", "-s", &param)) {
1101                         vparm = &(cmdargs.vparm[cmdargs.nr_vparm++]);
1102                         err = parse_rawset(param, vparm);
1103                         if (err < 0)
1104                                 goto error;
1105                 } else if (pass == 2 && arg_match(argv, &i, "--rawget", "-g", &param)) {
1106                         vparm = &(cmdargs.vparm[cmdargs.nr_vparm++]);
1107                         err = parse_rawget(param, vparm);
1108                         if (err < 0)
1109                                 goto error;
1110
1111                 } else if (pass == 2 && arg_match(argv, &i, "--print-all", "-P", 0)) {
1112                         err = generate_printall();
1113                         if (err)
1114                                 goto error;
1115
1116                 } else if (pass == 2) {
1117                         arg = argv[i];
1118                         if (arg[0] != '-' || arg[1] != '-')
1119                                 goto out_usage;         /* all must start with "--" */
1120                         if (locate_item_by_desc(BIT(sprom_rev), &type, &length, arg + 2))
1121                                 goto out_usage;
1122                         arg_match(argv, &i, arg, NULL, &param);
1123                         vparm = &(cmdargs.vparm[cmdargs.nr_vparm++]);
1124                         vparm->type = type;
1125                         vparm->bits = length;
1126                         err = parse_value(param, vparm, arg);
1127                         if (err < 0)
1128                                 goto error;
1129                         if (length == 34) {
1130                                 err = parse_mac(param, vparm, arg);
1131                                 if (err < 0)
1132                                         goto error;
1133                         }
1134                         if (length == 33) {
1135                                 err = parse_ccode(param, vparm, arg);
1136                                 if (err < 0)
1137                                         goto error;
1138                         }
1139                 }
1140                 if (parse_err)
1141                         goto out_usage;
1142         }
1143         if (pass == 2 && cmdargs.nr_vparm == 0) {
1144                 prerror("No Value parameter given. See --help.\n");
1145                 return -1;
1146         }
1147         return 0;
1148
1149 out_usage:
1150         print_usage(argc, argv);
1151 error:
1152         return -1;      
1153 }
1154
1155
1156 int main(int argc, char **argv)
1157 {
1158         int err;
1159         int fd;
1160         uint8_t sprom[SPROM4_SIZE + 10];
1161         char *buffer = NULL;
1162         size_t buffer_size = 0;
1163
1164         /* Some arguments require that the revision of the sprom be known,
1165          * but that is not known until the sprom data are read. This difficulty
1166          * is handled by making two passes through the argument list. The first
1167          * only process those arguments that do not depend on sprom revision.
1168          *
1169          * Do the first pass through arguments
1170          */
1171         err = parse_args(argc, argv, 1);
1172         if (err == 1)
1173                 return 0;
1174         else if (err != 0)
1175                 goto out;
1176
1177         print_banner(0);
1178         prinfo("\nReading input from \"%s\"...\n",
1179                cmdargs.infile ? cmdargs.infile : "stdin");
1180
1181         err = open_infile(&fd);
1182         if (err)
1183                 goto out;
1184         err = read_infile(fd, &buffer, &buffer_size);
1185         close_infile(fd);
1186         if (err)
1187                 goto out;
1188         err = parse_input(sprom, buffer, buffer_size);
1189         free(buffer);
1190         if (err)
1191                 goto out;
1192         err = validate_input(sprom);
1193         if (err)
1194                 goto out;
1195
1196         /* do second pass through argument list */
1197         err = parse_args(argc, argv, 2);
1198         if (err == 1)
1199                 return 0;
1200         else if (err != 0)
1201                 goto out;
1202
1203         err = display_sprom(sprom);
1204         if (err)
1205                 goto out;
1206         err = modify_sprom(sprom);
1207         if (err < 0)
1208                 goto out;
1209         if (err) {
1210                 err = open_outfile(&fd);
1211                 if (err)
1212                         goto out;
1213                 err = write_output(fd, sprom);
1214                 close_outfile(fd);
1215                 if (err)
1216                         goto out;
1217                 prinfo("SPROM modified.\n");
1218         }
1219         prdata("The input file is data from a revision %d SPROM.\n", sprom_rev);
1220 out:
1221         return err;
1222 }