ssb_sprom.c 38 KB

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