rb_hardconfig.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Driver for MikroTik RouterBoot hard config.
  4. *
  5. * Copyright (C) 2020 Thibaut VARÈNE <[email protected]>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License version 2 as published
  9. * by the Free Software Foundation.
  10. *
  11. * This driver exposes the data encoded in the "hard_config" flash segment of
  12. * MikroTik RouterBOARDs devices. It presents the data in a sysfs folder
  13. * named "hard_config". The WLAN calibration data is available on demand via
  14. * the 'wlan_data' sysfs file in that folder.
  15. *
  16. * This driver permanently allocates a chunk of RAM as large as the hard_config
  17. * MTD partition, although it is technically possible to operate entirely from
  18. * the MTD device without using a local buffer (except when requesting WLAN
  19. * calibration data), at the cost of a performance penalty.
  20. *
  21. * Note: PAGE_SIZE is assumed to be >= 4K, hence the device attribute show
  22. * routines need not check for output overflow.
  23. *
  24. * Some constant defines extracted from routerboot.{c,h} by Gabor Juhos
  25. * <[email protected]>
  26. */
  27. #include <linux/types.h>
  28. #include <linux/init.h>
  29. #include <linux/kernel.h>
  30. #include <linux/slab.h>
  31. #include <linux/errno.h>
  32. #include <linux/kobject.h>
  33. #include <linux/bitops.h>
  34. #include <linux/string.h>
  35. #include <linux/mtd/mtd.h>
  36. #include <linux/sysfs.h>
  37. #include <linux/lzo.h>
  38. #include "routerboot.h"
  39. #define RB_HARDCONFIG_VER "0.07"
  40. #define RB_HC_PR_PFX "[rb_hardconfig] "
  41. /* ID values for hardware settings */
  42. #define RB_ID_FLASH_INFO 0x03
  43. #define RB_ID_MAC_ADDRESS_PACK 0x04
  44. #define RB_ID_BOARD_PRODUCT_CODE 0x05
  45. #define RB_ID_BIOS_VERSION 0x06
  46. #define RB_ID_SDRAM_TIMINGS 0x08
  47. #define RB_ID_DEVICE_TIMINGS 0x09
  48. #define RB_ID_SOFTWARE_ID 0x0A
  49. #define RB_ID_SERIAL_NUMBER 0x0B
  50. #define RB_ID_MEMORY_SIZE 0x0D
  51. #define RB_ID_MAC_ADDRESS_COUNT 0x0E
  52. #define RB_ID_HW_OPTIONS 0x15
  53. #define RB_ID_WLAN_DATA 0x16
  54. #define RB_ID_BOARD_IDENTIFIER 0x17
  55. #define RB_ID_PRODUCT_NAME 0x21
  56. #define RB_ID_DEFCONF 0x26
  57. #define RB_ID_BOARD_REVISION 0x27
  58. /* Bit definitions for hardware options */
  59. #define RB_HW_OPT_NO_UART BIT(0)
  60. #define RB_HW_OPT_HAS_VOLTAGE BIT(1)
  61. #define RB_HW_OPT_HAS_USB BIT(2)
  62. #define RB_HW_OPT_HAS_ATTINY BIT(3)
  63. #define RB_HW_OPT_PULSE_DUTY_CYCLE BIT(9)
  64. #define RB_HW_OPT_NO_NAND BIT(14)
  65. #define RB_HW_OPT_HAS_LCD BIT(15)
  66. #define RB_HW_OPT_HAS_POE_OUT BIT(16)
  67. #define RB_HW_OPT_HAS_uSD BIT(17)
  68. #define RB_HW_OPT_HAS_SIM BIT(18)
  69. #define RB_HW_OPT_HAS_SFP BIT(20)
  70. #define RB_HW_OPT_HAS_WIFI BIT(21)
  71. #define RB_HW_OPT_HAS_TS_FOR_ADC BIT(22)
  72. #define RB_HW_OPT_HAS_PLC BIT(29)
  73. /*
  74. * Tag ID values for ERD data.
  75. * Mikrotik used to pack all calibration data under a single tag id 0x1, but
  76. * recently switched to a new scheme where each radio calibration gets a
  77. * separate tag. The new scheme has tag id bit 15 always set and seems to be
  78. * mutually exclusive with the old scheme.
  79. */
  80. #define RB_WLAN_ERD_ID_SOLO 0x0001
  81. #define RB_WLAN_ERD_ID_MULTI_8001 0x8001
  82. #define RB_WLAN_ERD_ID_MULTI_8201 0x8201
  83. static struct kobject *hc_kobj;
  84. static u8 *hc_buf; // ro buffer after init(): no locking required
  85. static size_t hc_buflen;
  86. /*
  87. * For LZOR style WLAN data unpacking.
  88. * This binary blob is prepended to the data encoded on some devices as
  89. * RB_ID_WLAN_DATA, the result is then first decompressed with LZO, and then
  90. * finally RLE-decoded.
  91. * This binary blob has been extracted from RouterOS by
  92. * https://forum.openwrt.org/u/ius
  93. */
  94. static const u8 hc_lzor_prefix[] = {
  95. 0x00, 0x05, 0x4c, 0x4c, 0x44, 0x00, 0x34, 0xfe,
  96. 0xfe, 0x34, 0x11, 0x3c, 0x1e, 0x3c, 0x2e, 0x3c,
  97. 0x4c, 0x34, 0x00, 0x52, 0x62, 0x92, 0xa2, 0xb2,
  98. 0xc3, 0x2a, 0x14, 0x00, 0x00, 0x05, 0xfe, 0x6a,
  99. 0x3c, 0x16, 0x32, 0x16, 0x11, 0x1e, 0x12, 0x46,
  100. 0x32, 0x46, 0x11, 0x4e, 0x12, 0x36, 0x32, 0x36,
  101. 0x11, 0x3e, 0x12, 0x5a, 0x9a, 0x64, 0x00, 0x04,
  102. 0xfe, 0x10, 0x3c, 0x00, 0x01, 0x00, 0x00, 0x28,
  103. 0x0c, 0x00, 0x0f, 0xfe, 0x14, 0x00, 0x24, 0x24,
  104. 0x23, 0x24, 0x24, 0x23, 0x25, 0x22, 0x21, 0x21,
  105. 0x23, 0x22, 0x21, 0x22, 0x21, 0x2d, 0x38, 0x00,
  106. 0x0c, 0x25, 0x25, 0x24, 0x25, 0x25, 0x24, 0x23,
  107. 0x22, 0x21, 0x20, 0x23, 0x21, 0x21, 0x22, 0x21,
  108. 0x2d, 0x38, 0x00, 0x28, 0xb0, 0x00, 0x00, 0x22,
  109. 0x00, 0x00, 0xc0, 0xfe, 0x03, 0x00, 0xc0, 0x00,
  110. 0x62, 0xff, 0x62, 0xff, 0xfe, 0x06, 0x00, 0xbb,
  111. 0xff, 0xba, 0xff, 0xfe, 0x08, 0x00, 0x9e, 0xff,
  112. 0xfe, 0x0a, 0x00, 0x53, 0xff, 0xfe, 0x02, 0x00,
  113. 0x20, 0xff, 0xb1, 0xfe, 0xfe, 0xb2, 0xfe, 0xfe,
  114. 0xed, 0xfe, 0xfe, 0xfe, 0x04, 0x00, 0x3a, 0xff,
  115. 0x3a, 0xff, 0xde, 0xfd, 0x5f, 0x04, 0x33, 0xff,
  116. 0x4c, 0x74, 0x03, 0x05, 0x05, 0xff, 0x6d, 0xfe,
  117. 0xfe, 0x6d, 0xfe, 0xfe, 0xaf, 0x08, 0x63, 0xff,
  118. 0x64, 0x6f, 0x08, 0xac, 0xff, 0xbf, 0x6d, 0x08,
  119. 0x7a, 0x6d, 0x08, 0x96, 0x74, 0x04, 0x00, 0x08,
  120. 0x79, 0xff, 0xda, 0xfe, 0xfe, 0xdb, 0xfe, 0xfe,
  121. 0x56, 0xff, 0xfe, 0x04, 0x00, 0x5e, 0xff, 0x5e,
  122. 0xff, 0x6c, 0xfe, 0xfe, 0xfe, 0x06, 0x00, 0x41,
  123. 0xff, 0x7f, 0x74, 0x03, 0x00, 0x11, 0x44, 0xff,
  124. 0xa9, 0xfe, 0xfe, 0xa9, 0xfe, 0xfe, 0xa5, 0x8f,
  125. 0x01, 0x00, 0x08, 0x01, 0x01, 0x02, 0x04, 0x08,
  126. 0x02, 0x04, 0x08, 0x08, 0x01, 0x01, 0xfe, 0x22,
  127. 0x00, 0x4c, 0x60, 0x64, 0x8c, 0x90, 0xd0, 0xd4,
  128. 0xd8, 0x5c, 0x10, 0x09, 0xd8, 0xff, 0xb0, 0xff,
  129. 0x00, 0x00, 0xba, 0xff, 0x14, 0x00, 0xba, 0xff,
  130. 0x64, 0x00, 0x00, 0x08, 0xfe, 0x06, 0x00, 0x74,
  131. 0xff, 0x42, 0xff, 0xce, 0xff, 0x60, 0xff, 0x0a,
  132. 0x00, 0xb4, 0x00, 0xa0, 0x00, 0xa0, 0xfe, 0x07,
  133. 0x00, 0x0a, 0x00, 0xb0, 0xff, 0x96, 0x4d, 0x00,
  134. 0x56, 0x57, 0x18, 0xa6, 0xff, 0x92, 0x70, 0x11,
  135. 0x00, 0x12, 0x90, 0x90, 0x76, 0x5a, 0x54, 0x54,
  136. 0x4c, 0x46, 0x38, 0x00, 0x10, 0x10, 0x08, 0xfe,
  137. 0x05, 0x00, 0x38, 0x29, 0x25, 0x23, 0x22, 0x22,
  138. 0x1f, 0x00, 0x00, 0x00, 0xf6, 0xe1, 0xdd, 0xf8,
  139. 0xfe, 0x00, 0xfe, 0x15, 0x00, 0x00, 0xd0, 0x02,
  140. 0x74, 0x02, 0x08, 0xf8, 0xe5, 0xde, 0x02, 0x04,
  141. 0x04, 0xfd, 0x00, 0x00, 0x00, 0x07, 0x50, 0x2d,
  142. 0x01, 0x90, 0x90, 0x76, 0x60, 0xb0, 0x07, 0x07,
  143. 0x0c, 0x0c, 0x04, 0xfe, 0x05, 0x00, 0x66, 0x66,
  144. 0x5a, 0x56, 0xbc, 0x01, 0x06, 0xfc, 0xfc, 0xf1,
  145. 0xfe, 0x07, 0x00, 0x24, 0x95, 0x70, 0x64, 0x18,
  146. 0x06, 0x2c, 0xff, 0xb5, 0xfe, 0xfe, 0xb5, 0xfe,
  147. 0xfe, 0xe2, 0x8c, 0x24, 0x02, 0x2f, 0xff, 0x2f,
  148. 0xff, 0xb4, 0x78, 0x02, 0x05, 0x73, 0xff, 0xed,
  149. 0xfe, 0xfe, 0x4f, 0xff, 0x36, 0x74, 0x1e, 0x09,
  150. 0x4f, 0xff, 0x50, 0xff, 0xfe, 0x16, 0x00, 0x70,
  151. 0xac, 0x70, 0x8e, 0xac, 0x40, 0x0e, 0x01, 0x70,
  152. 0x7f, 0x8e, 0xac, 0x6c, 0x00, 0x0b, 0xfe, 0x02,
  153. 0x00, 0xfe, 0x0a, 0x2c, 0x2a, 0x2a, 0x28, 0x26,
  154. 0x1e, 0x1e, 0xfe, 0x02, 0x20, 0x65, 0x20, 0x00,
  155. 0x00, 0x05, 0x12, 0x00, 0x11, 0x1e, 0x11, 0x11,
  156. 0x41, 0x1e, 0x41, 0x11, 0x31, 0x1e, 0x31, 0x11,
  157. 0x70, 0x75, 0x7a, 0x7f, 0x84, 0x89, 0x8e, 0x93,
  158. 0x98, 0x30, 0x20, 0x00, 0x02, 0x00, 0xfe, 0x06,
  159. 0x3c, 0xbc, 0x32, 0x0c, 0x00, 0x00, 0x2a, 0x12,
  160. 0x1e, 0x12, 0x2e, 0x12, 0xcc, 0x12, 0x11, 0x1a,
  161. 0x1e, 0x1a, 0x2e, 0x1a, 0x4c, 0x10, 0x1e, 0x10,
  162. 0x11, 0x18, 0x1e, 0x42, 0x1e, 0x42, 0x2e, 0x42,
  163. 0xcc, 0x42, 0x11, 0x4a, 0x1e, 0x4a, 0x2e, 0x4a,
  164. 0x4c, 0x40, 0x1e, 0x40, 0x11, 0x48, 0x1e, 0x32,
  165. 0x1e, 0x32, 0x2e, 0x32, 0xcc, 0x32, 0x11, 0x3a,
  166. 0x1e, 0x3a, 0x2e, 0x3a, 0x4c, 0x30, 0x1e, 0x30,
  167. 0x11, 0x38, 0x1e, 0x27, 0x9a, 0x01, 0x9d, 0xa2,
  168. 0x2f, 0x28, 0x00, 0x00, 0x46, 0xde, 0xc4, 0xbf,
  169. 0xa6, 0x9d, 0x81, 0x7b, 0x5c, 0x61, 0x40, 0xc7,
  170. 0xc0, 0xae, 0xa9, 0x8c, 0x83, 0x6a, 0x62, 0x50,
  171. 0x3e, 0xce, 0xc2, 0xae, 0xa3, 0x8c, 0x7b, 0x6a,
  172. 0x5a, 0x50, 0x35, 0xd7, 0xc2, 0xb7, 0xa4, 0x95,
  173. 0x7e, 0x72, 0x5a, 0x59, 0x37, 0xfe, 0x02, 0xf8,
  174. 0x8c, 0x95, 0x90, 0x8f, 0x00, 0xd7, 0xc0, 0xb7,
  175. 0xa2, 0x95, 0x7b, 0x72, 0x56, 0x59, 0x32, 0xc7,
  176. 0xc3, 0xae, 0xad, 0x8c, 0x85, 0x6a, 0x63, 0x50,
  177. 0x3e, 0xce, 0xc3, 0xae, 0xa4, 0x8c, 0x7c, 0x6a,
  178. 0x59, 0x50, 0x34, 0xd7, 0xc2, 0xb7, 0xa5, 0x95,
  179. 0x7e, 0x72, 0x59, 0x59, 0x36, 0xfc, 0x05, 0x00,
  180. 0x02, 0xce, 0xc5, 0xae, 0xa5, 0x95, 0x83, 0x72,
  181. 0x5c, 0x59, 0x36, 0xbf, 0xc6, 0xa5, 0xab, 0x8c,
  182. 0x8c, 0x6a, 0x67, 0x50, 0x41, 0x64, 0x07, 0x00,
  183. 0x02, 0x95, 0x8c, 0x72, 0x65, 0x59, 0x3f, 0xce,
  184. 0xc7, 0xae, 0xa8, 0x95, 0x86, 0x72, 0x5f, 0x59,
  185. 0x39, 0xfe, 0x02, 0xf8, 0x8b, 0x7c, 0x0b, 0x09,
  186. 0xb7, 0xc2, 0x9d, 0xa4, 0x83, 0x85, 0x6a, 0x6b,
  187. 0x50, 0x44, 0xb7, 0xc1, 0x64, 0x01, 0x00, 0x06,
  188. 0x61, 0x5d, 0x48, 0x3d, 0xae, 0xc4, 0x9d, 0xad,
  189. 0x7b, 0x85, 0x61, 0x66, 0x48, 0x46, 0xae, 0xc3,
  190. 0x95, 0xa3, 0x72, 0x7c, 0x59, 0x56, 0x38, 0x31,
  191. 0x7c, 0x0b, 0x00, 0x0c, 0x96, 0x91, 0x8f, 0x00,
  192. 0xb7, 0xc0, 0xa5, 0xab, 0x8c, 0x8a, 0x6a, 0x64,
  193. 0x50, 0x3c, 0xb7, 0xc0, 0x9d, 0xa0, 0x83, 0x80,
  194. 0x6a, 0x64, 0x50, 0x3d, 0xb7, 0xc5, 0x9d, 0xa5,
  195. 0x83, 0x87, 0x6c, 0x08, 0x07, 0xae, 0xc0, 0x9d,
  196. 0xa8, 0x83, 0x88, 0x6a, 0x6d, 0x50, 0x46, 0xfc,
  197. 0x05, 0x00, 0x16, 0xbf, 0xc0, 0xa5, 0xa2, 0x8c,
  198. 0x7f, 0x6a, 0x57, 0x50, 0x2f, 0xb7, 0xc7, 0xa5,
  199. 0xb1, 0x8c, 0x8e, 0x72, 0x6d, 0x59, 0x45, 0xbf,
  200. 0xc6, 0xa5, 0xa8, 0x8c, 0x87, 0x6a, 0x5f, 0x50,
  201. 0x37, 0xbf, 0xc2, 0xa5, 0xa4, 0x8c, 0x83, 0x6a,
  202. 0x5c, 0x50, 0x34, 0xbc, 0x05, 0x00, 0x0e, 0x90,
  203. 0x00, 0xc7, 0xc2, 0xae, 0xaa, 0x95, 0x82, 0x7b,
  204. 0x60, 0x61, 0x3f, 0xb7, 0xc6, 0xa5, 0xb1, 0x8c,
  205. 0x8d, 0x72, 0x6b, 0x61, 0x51, 0xbf, 0xc4, 0xa5,
  206. 0xa5, 0x8c, 0x82, 0x72, 0x61, 0x59, 0x39, 0x6c,
  207. 0x26, 0x03, 0x95, 0x82, 0x7b, 0x61, 0x61, 0x40,
  208. 0xfc, 0x05, 0x00, 0x00, 0x7e, 0xd7, 0xc3, 0xb7,
  209. 0xa8, 0x9d, 0x80, 0x83, 0x5d, 0x6a, 0x3f, 0xbf,
  210. 0xc7, 0xa5, 0xa8, 0x8c, 0x84, 0x72, 0x60, 0x61,
  211. 0x46, 0xbf, 0xc2, 0xae, 0xb0, 0x9d, 0x92, 0x83,
  212. 0x6f, 0x6a, 0x50, 0xd7, 0xc3, 0xb7, 0xa7, 0x9d,
  213. 0x80, 0x83, 0x5e, 0x6a, 0x40, 0xfe, 0x02, 0xf8,
  214. 0x8d, 0x96, 0x90, 0x90, 0xfe, 0x05, 0x00, 0x8a,
  215. 0xc4, 0x63, 0xb8, 0x3c, 0xa6, 0x29, 0x97, 0x16,
  216. 0x81, 0x84, 0xb7, 0x5b, 0xa9, 0x33, 0x94, 0x1e,
  217. 0x83, 0x11, 0x70, 0xb8, 0xc2, 0x70, 0xb1, 0x4d,
  218. 0xa3, 0x2a, 0x8d, 0x1b, 0x7b, 0xa8, 0xbc, 0x68,
  219. 0xab, 0x47, 0x9d, 0x27, 0x87, 0x18, 0x75, 0xae,
  220. 0xc6, 0x7d, 0xbb, 0x4d, 0xaa, 0x1c, 0x84, 0x11,
  221. 0x72, 0xa3, 0xbb, 0x6e, 0xad, 0x3c, 0x97, 0x24,
  222. 0x85, 0x16, 0x71, 0x80, 0xb2, 0x57, 0xa4, 0x30,
  223. 0x8e, 0x1c, 0x7c, 0x10, 0x68, 0xbb, 0xbd, 0x75,
  224. 0xac, 0x4f, 0x9e, 0x2b, 0x87, 0x1a, 0x76, 0x96,
  225. 0xc5, 0x5e, 0xb5, 0x3e, 0xa5, 0x1f, 0x8c, 0x12,
  226. 0x7a, 0xc1, 0xc6, 0x42, 0x9f, 0x27, 0x8c, 0x16,
  227. 0x77, 0x0f, 0x67, 0x9d, 0xbc, 0x68, 0xad, 0x36,
  228. 0x95, 0x20, 0x83, 0x11, 0x6d, 0x9b, 0xb8, 0x67,
  229. 0xa8, 0x34, 0x90, 0x1f, 0x7c, 0x10, 0x67, 0x9e,
  230. 0xc9, 0x6a, 0xbb, 0x37, 0xa4, 0x20, 0x90, 0x11,
  231. 0x7b, 0xc6, 0xc8, 0x47, 0xa4, 0x2a, 0x90, 0x18,
  232. 0x7b, 0x10, 0x6c, 0xae, 0xc4, 0x5d, 0xad, 0x37,
  233. 0x9a, 0x1f, 0x85, 0x13, 0x75, 0x70, 0xad, 0x42,
  234. 0x99, 0x25, 0x84, 0x17, 0x74, 0x0b, 0x56, 0x87,
  235. 0xc8, 0x57, 0xb8, 0x2b, 0x9e, 0x19, 0x8a, 0x0d,
  236. 0x74, 0xa7, 0xc8, 0x6e, 0xb9, 0x36, 0xa0, 0x1f,
  237. 0x8b, 0x11, 0x75, 0x94, 0xbe, 0x4b, 0xa5, 0x2a,
  238. 0x92, 0x18, 0x7c, 0x0f, 0x6b, 0xaf, 0xc0, 0x58,
  239. 0xa8, 0x34, 0x94, 0x1d, 0x7d, 0x12, 0x6d, 0x82,
  240. 0xc0, 0x52, 0xb0, 0x25, 0x94, 0x14, 0x7f, 0x0c,
  241. 0x68, 0x84, 0xbf, 0x3e, 0xa4, 0x22, 0x8e, 0x10,
  242. 0x76, 0x0b, 0x65, 0x88, 0xb6, 0x42, 0x9b, 0x26,
  243. 0x87, 0x14, 0x70, 0x0c, 0x5f, 0xc5, 0xc2, 0x3e,
  244. 0x97, 0x23, 0x83, 0x13, 0x6c, 0x0c, 0x5c, 0xb1,
  245. 0xc9, 0x76, 0xbc, 0x4a, 0xaa, 0x20, 0x8d, 0x12,
  246. 0x78, 0x93, 0xbf, 0x46, 0xa3, 0x26, 0x8d, 0x14,
  247. 0x74, 0x0c, 0x62, 0xc8, 0xc4, 0x3b, 0x97, 0x21,
  248. 0x82, 0x11, 0x6a, 0x0a, 0x59, 0xa3, 0xb9, 0x68,
  249. 0xa9, 0x30, 0x8d, 0x1a, 0x78, 0x0f, 0x61, 0xa0,
  250. 0xc9, 0x73, 0xbe, 0x50, 0xb1, 0x30, 0x9f, 0x14,
  251. 0x80, 0x83, 0xb7, 0x3c, 0x9a, 0x20, 0x84, 0x0e,
  252. 0x6a, 0x0a, 0x57, 0xac, 0xc2, 0x68, 0xb0, 0x2e,
  253. 0x92, 0x19, 0x7c, 0x0d, 0x63, 0x93, 0xbe, 0x62,
  254. 0xb0, 0x3c, 0x9e, 0x1a, 0x80, 0x0e, 0x6b, 0xbb,
  255. 0x02, 0xa0, 0x02, 0xa0, 0x02, 0x6f, 0x00, 0x75,
  256. 0x00, 0x75, 0x00, 0x00, 0x00, 0xad, 0x02, 0xb3,
  257. 0x02, 0x6f, 0x00, 0x87, 0x00, 0x85, 0xfe, 0x03,
  258. 0x00, 0xc2, 0x02, 0x82, 0x4d, 0x92, 0x6e, 0x4d,
  259. 0xb1, 0xa8, 0x84, 0x01, 0x00, 0x07, 0x7e, 0x00,
  260. 0xa8, 0x02, 0xa4, 0x02, 0xa4, 0x02, 0xa2, 0x00,
  261. 0xa6, 0x00, 0xa6, 0x00, 0x00, 0x00, 0xb4, 0x02,
  262. 0xb4, 0x02, 0x92, 0x00, 0x96, 0x00, 0x96, 0x46,
  263. 0x04, 0xb0, 0x02, 0x64, 0x02, 0x0a, 0x8c, 0x00,
  264. 0x90, 0x02, 0x98, 0x02, 0x98, 0x02, 0x0e, 0x01,
  265. 0x11, 0x01, 0x11, 0x50, 0xc3, 0x08, 0x88, 0x02,
  266. 0x88, 0x02, 0x19, 0x01, 0x02, 0x01, 0x02, 0x01,
  267. 0xf3, 0x2d, 0x00, 0x00
  268. };
  269. /* Array of known hw_options bits with human-friendly parsing */
  270. static struct hc_hwopt {
  271. const u32 bit;
  272. const char *str;
  273. } const hc_hwopts[] = {
  274. {
  275. .bit = RB_HW_OPT_NO_UART,
  276. .str = "no UART\t\t",
  277. }, {
  278. .bit = RB_HW_OPT_HAS_VOLTAGE,
  279. .str = "has Vreg\t",
  280. }, {
  281. .bit = RB_HW_OPT_HAS_USB,
  282. .str = "has usb\t\t",
  283. }, {
  284. .bit = RB_HW_OPT_HAS_ATTINY,
  285. .str = "has ATtiny\t",
  286. }, {
  287. .bit = RB_HW_OPT_NO_NAND,
  288. .str = "no NAND\t\t",
  289. }, {
  290. .bit = RB_HW_OPT_HAS_LCD,
  291. .str = "has LCD\t\t",
  292. }, {
  293. .bit = RB_HW_OPT_HAS_POE_OUT,
  294. .str = "has POE out\t",
  295. }, {
  296. .bit = RB_HW_OPT_HAS_uSD,
  297. .str = "has MicroSD\t",
  298. }, {
  299. .bit = RB_HW_OPT_HAS_SIM,
  300. .str = "has SIM\t\t",
  301. }, {
  302. .bit = RB_HW_OPT_HAS_SFP,
  303. .str = "has SFP\t\t",
  304. }, {
  305. .bit = RB_HW_OPT_HAS_WIFI,
  306. .str = "has WiFi\t",
  307. }, {
  308. .bit = RB_HW_OPT_HAS_TS_FOR_ADC,
  309. .str = "has TS ADC\t",
  310. }, {
  311. .bit = RB_HW_OPT_HAS_PLC,
  312. .str = "has PLC\t\t",
  313. },
  314. };
  315. /*
  316. * The MAC is stored network-endian on all devices, in 2 32-bit segments:
  317. * <XX:XX:XX:XX> <XX:XX:00:00>. Kernel print has us covered.
  318. */
  319. static ssize_t hc_tag_show_mac(const u8 *pld, u16 pld_len, char *buf)
  320. {
  321. if (8 != pld_len)
  322. return -EINVAL;
  323. return sprintf(buf, "%pM\n", pld);
  324. }
  325. /*
  326. * Print HW options in a human readable way:
  327. * The raw number and in decoded form
  328. */
  329. static ssize_t hc_tag_show_hwoptions(const u8 *pld, u16 pld_len, char *buf)
  330. {
  331. char *out = buf;
  332. u32 data; // cpu-endian
  333. int i;
  334. if (sizeof(data) != pld_len)
  335. return -EINVAL;
  336. data = *(u32 *)pld;
  337. out += sprintf(out, "raw\t\t: 0x%08x\n\n", data);
  338. for (i = 0; i < ARRAY_SIZE(hc_hwopts); i++)
  339. out += sprintf(out, "%s: %s\n", hc_hwopts[i].str,
  340. (data & hc_hwopts[i].bit) ? "true" : "false");
  341. return out - buf;
  342. }
  343. static ssize_t hc_wlan_data_bin_read(struct file *filp, struct kobject *kobj,
  344. struct bin_attribute *attr, char *buf,
  345. loff_t off, size_t count);
  346. static struct hc_wlan_attr {
  347. const u16 erd_tag_id;
  348. struct bin_attribute battr;
  349. u16 pld_ofs;
  350. u16 pld_len;
  351. } hc_wd_multi_battrs[] = {
  352. {
  353. .erd_tag_id = RB_WLAN_ERD_ID_MULTI_8001,
  354. .battr = __BIN_ATTR(data_0, S_IRUSR, hc_wlan_data_bin_read, NULL, 0),
  355. }, {
  356. .erd_tag_id = RB_WLAN_ERD_ID_MULTI_8201,
  357. .battr = __BIN_ATTR(data_2, S_IRUSR, hc_wlan_data_bin_read, NULL, 0),
  358. }
  359. };
  360. static struct hc_wlan_attr hc_wd_solo_battr = {
  361. .erd_tag_id = RB_WLAN_ERD_ID_SOLO,
  362. .battr = __BIN_ATTR(wlan_data, S_IRUSR, hc_wlan_data_bin_read, NULL, 0),
  363. };
  364. static ssize_t hc_attr_show(struct kobject *kobj, struct kobj_attribute *attr,
  365. char *buf);
  366. /* Array of known tags to publish in sysfs */
  367. static struct hc_attr {
  368. const u16 tag_id;
  369. ssize_t (* const tshow)(const u8 *pld, u16 pld_len, char *buf);
  370. struct kobj_attribute kattr;
  371. u16 pld_ofs;
  372. u16 pld_len;
  373. } hc_attrs[] = {
  374. {
  375. .tag_id = RB_ID_FLASH_INFO,
  376. .tshow = routerboot_tag_show_u32s,
  377. .kattr = __ATTR(flash_info, S_IRUSR, hc_attr_show, NULL),
  378. }, {
  379. .tag_id = RB_ID_MAC_ADDRESS_PACK,
  380. .tshow = hc_tag_show_mac,
  381. .kattr = __ATTR(mac_base, S_IRUSR, hc_attr_show, NULL),
  382. }, {
  383. .tag_id = RB_ID_BOARD_PRODUCT_CODE,
  384. .tshow = routerboot_tag_show_string,
  385. .kattr = __ATTR(board_product_code, S_IRUSR, hc_attr_show, NULL),
  386. }, {
  387. .tag_id = RB_ID_BIOS_VERSION,
  388. .tshow = routerboot_tag_show_string,
  389. .kattr = __ATTR(booter_version, S_IRUSR, hc_attr_show, NULL),
  390. }, {
  391. .tag_id = RB_ID_SERIAL_NUMBER,
  392. .tshow = routerboot_tag_show_string,
  393. .kattr = __ATTR(board_serial, S_IRUSR, hc_attr_show, NULL),
  394. }, {
  395. .tag_id = RB_ID_MEMORY_SIZE,
  396. .tshow = routerboot_tag_show_u32s,
  397. .kattr = __ATTR(mem_size, S_IRUSR, hc_attr_show, NULL),
  398. }, {
  399. .tag_id = RB_ID_MAC_ADDRESS_COUNT,
  400. .tshow = routerboot_tag_show_u32s,
  401. .kattr = __ATTR(mac_count, S_IRUSR, hc_attr_show, NULL),
  402. }, {
  403. .tag_id = RB_ID_HW_OPTIONS,
  404. .tshow = hc_tag_show_hwoptions,
  405. .kattr = __ATTR(hw_options, S_IRUSR, hc_attr_show, NULL),
  406. }, {
  407. .tag_id = RB_ID_WLAN_DATA,
  408. .tshow = NULL,
  409. }, {
  410. .tag_id = RB_ID_BOARD_IDENTIFIER,
  411. .tshow = routerboot_tag_show_string,
  412. .kattr = __ATTR(board_identifier, S_IRUSR, hc_attr_show, NULL),
  413. }, {
  414. .tag_id = RB_ID_PRODUCT_NAME,
  415. .tshow = routerboot_tag_show_string,
  416. .kattr = __ATTR(product_name, S_IRUSR, hc_attr_show, NULL),
  417. }, {
  418. .tag_id = RB_ID_DEFCONF,
  419. .tshow = routerboot_tag_show_string,
  420. .kattr = __ATTR(defconf, S_IRUSR, hc_attr_show, NULL),
  421. }, {
  422. .tag_id = RB_ID_BOARD_REVISION,
  423. .tshow = routerboot_tag_show_string,
  424. .kattr = __ATTR(board_revision, S_IRUSR, hc_attr_show, NULL),
  425. }
  426. };
  427. /*
  428. * If the RB_ID_WLAN_DATA payload starts with RB_MAGIC_ERD, then past
  429. * that magic number the payload itself contains a routerboot tag node
  430. * locating the LZO-compressed calibration data. So far this scheme is only
  431. * known to use a single tag at id 0x1.
  432. */
  433. static int hc_wlan_data_unpack_erd(const u16 tag_id, const u8 *inbuf, size_t inlen,
  434. void *outbuf, size_t *outlen)
  435. {
  436. u16 lzo_ofs, lzo_len;
  437. int ret;
  438. /* Find embedded tag */
  439. ret = routerboot_tag_find(inbuf, inlen, tag_id, &lzo_ofs, &lzo_len);
  440. if (ret) {
  441. pr_debug(RB_HC_PR_PFX "no ERD data for id 0x%04x\n", tag_id);
  442. goto fail;
  443. }
  444. if (lzo_len > inlen) {
  445. pr_debug(RB_HC_PR_PFX "Invalid ERD data length\n");
  446. ret = -EINVAL;
  447. goto fail;
  448. }
  449. ret = lzo1x_decompress_safe(inbuf+lzo_ofs, lzo_len, outbuf, outlen);
  450. if (ret)
  451. pr_debug(RB_HC_PR_PFX "LZO decompression error (%d)\n", ret);
  452. fail:
  453. return ret;
  454. }
  455. /*
  456. * If the RB_ID_WLAN_DATA payload starts with RB_MAGIC_LZOR, then past
  457. * that magic number is a payload that must be appended to the hc_lzor_prefix,
  458. * the resulting blob is LZO-compressed. In the LZO decompression result,
  459. * the RB_MAGIC_ERD magic number (aligned) must be located. Following that
  460. * magic, there is one or more routerboot tag node(s) locating the RLE-encoded
  461. * calibration data payload.
  462. */
  463. static int hc_wlan_data_unpack_lzor(const u16 tag_id, const u8 *inbuf, size_t inlen,
  464. void *outbuf, size_t *outlen)
  465. {
  466. u16 rle_ofs, rle_len;
  467. const u32 *needle;
  468. u8 *tempbuf;
  469. size_t templen, lzo_len;
  470. int ret;
  471. lzo_len = inlen + sizeof(hc_lzor_prefix);
  472. if (lzo_len > *outlen)
  473. return -EFBIG;
  474. /* Temporary buffer same size as the outbuf */
  475. templen = *outlen;
  476. tempbuf = kmalloc(templen, GFP_KERNEL);
  477. if (!tempbuf)
  478. return -ENOMEM;
  479. /* Concatenate into the outbuf */
  480. memcpy(outbuf, hc_lzor_prefix, sizeof(hc_lzor_prefix));
  481. memcpy(outbuf + sizeof(hc_lzor_prefix), inbuf, inlen);
  482. /* LZO-decompress lzo_len bytes of outbuf into the tempbuf */
  483. ret = lzo1x_decompress_safe(outbuf, lzo_len, tempbuf, &templen);
  484. if (ret) {
  485. if (LZO_E_INPUT_NOT_CONSUMED == ret) {
  486. /*
  487. * The tag length is always aligned thus the LZO payload may be padded,
  488. * which can trigger a spurious error which we ignore here.
  489. */
  490. pr_debug(RB_HC_PR_PFX "LZOR: LZO EOF before buffer end - this may be harmless\n");
  491. } else {
  492. pr_debug(RB_HC_PR_PFX "LZOR: LZO decompression error (%d)\n", ret);
  493. goto fail;
  494. }
  495. }
  496. /*
  497. * Post decompression we have a blob (possibly byproduct of the lzo
  498. * dictionary). We need to find RB_MAGIC_ERD. The magic number seems to
  499. * be 32bit-aligned in the decompression output.
  500. */
  501. needle = (const u32 *)tempbuf;
  502. while (RB_MAGIC_ERD != *needle++) {
  503. if ((u8 *)needle >= tempbuf+templen) {
  504. pr_debug(RB_HC_PR_PFX "LZOR: ERD magic not found\n");
  505. ret = -ENODATA;
  506. goto fail;
  507. }
  508. };
  509. templen -= (u8 *)needle - tempbuf;
  510. /* Past magic. Look for tag node */
  511. ret = routerboot_tag_find((u8 *)needle, templen, tag_id, &rle_ofs, &rle_len);
  512. if (ret) {
  513. pr_debug(RB_HC_PR_PFX "LZOR: no RLE data for id 0x%04x\n", tag_id);
  514. goto fail;
  515. }
  516. if (rle_len > templen) {
  517. pr_debug(RB_HC_PR_PFX "LZOR: Invalid RLE data length\n");
  518. ret = -EINVAL;
  519. goto fail;
  520. }
  521. /* RLE-decode tempbuf from needle back into the outbuf */
  522. ret = routerboot_rle_decode((u8 *)needle+rle_ofs, rle_len, outbuf, outlen);
  523. if (ret)
  524. pr_debug(RB_HC_PR_PFX "LZOR: RLE decoding error (%d)\n", ret);
  525. fail:
  526. kfree(tempbuf);
  527. return ret;
  528. }
  529. static int hc_wlan_data_unpack(const u16 tag_id, const size_t tofs, size_t tlen,
  530. void *outbuf, size_t *outlen)
  531. {
  532. const u8 *lbuf;
  533. u32 magic;
  534. int ret;
  535. /* Caller ensure tlen > 0. tofs is aligned */
  536. if ((tofs + tlen) > hc_buflen)
  537. return -EIO;
  538. lbuf = hc_buf + tofs;
  539. magic = *(u32 *)lbuf;
  540. ret = -ENODATA;
  541. switch (magic) {
  542. case RB_MAGIC_LZOR:
  543. /* Skip magic */
  544. lbuf += sizeof(magic);
  545. tlen -= sizeof(magic);
  546. ret = hc_wlan_data_unpack_lzor(tag_id, lbuf, tlen, outbuf, outlen);
  547. break;
  548. case RB_MAGIC_ERD:
  549. /* Skip magic */
  550. lbuf += sizeof(magic);
  551. tlen -= sizeof(magic);
  552. ret = hc_wlan_data_unpack_erd(tag_id, lbuf, tlen, outbuf, outlen);
  553. break;
  554. default:
  555. /*
  556. * If the RB_ID_WLAN_DATA payload doesn't start with a
  557. * magic number, the payload itself is the raw RLE-encoded
  558. * calibration data. Only RB_WLAN_ERD_ID_SOLO makes sense here.
  559. */
  560. if (RB_WLAN_ERD_ID_SOLO == tag_id) {
  561. ret = routerboot_rle_decode(lbuf, tlen, outbuf, outlen);
  562. if (ret)
  563. pr_debug(RB_HC_PR_PFX "RLE decoding error (%d)\n", ret);
  564. }
  565. break;
  566. }
  567. return ret;
  568. }
  569. static ssize_t hc_attr_show(struct kobject *kobj, struct kobj_attribute *attr,
  570. char *buf)
  571. {
  572. const struct hc_attr *hc_attr;
  573. const u8 *pld;
  574. u16 pld_len;
  575. hc_attr = container_of(attr, typeof(*hc_attr), kattr);
  576. if (!hc_attr->pld_len)
  577. return -ENOENT;
  578. pld = hc_buf + hc_attr->pld_ofs;
  579. pld_len = hc_attr->pld_len;
  580. return hc_attr->tshow(pld, pld_len, buf);
  581. }
  582. /*
  583. * This function will allocate and free memory every time it is called. This
  584. * is not the fastest way to do this, but since the data is rarely read (mainly
  585. * at boot time to load wlan caldata), this makes it possible to save memory for
  586. * the system.
  587. */
  588. static ssize_t hc_wlan_data_bin_read(struct file *filp, struct kobject *kobj,
  589. struct bin_attribute *attr, char *buf,
  590. loff_t off, size_t count)
  591. {
  592. struct hc_wlan_attr *hc_wattr;
  593. size_t outlen;
  594. void *outbuf;
  595. int ret;
  596. hc_wattr = container_of(attr, typeof(*hc_wattr), battr);
  597. if (!hc_wattr->pld_len)
  598. return -ENOENT;
  599. outlen = RB_ART_SIZE;
  600. /* Don't bother unpacking if the source is already too large */
  601. if (hc_wattr->pld_len > outlen)
  602. return -EFBIG;
  603. outbuf = kmalloc(outlen, GFP_KERNEL);
  604. if (!outbuf)
  605. return -ENOMEM;
  606. ret = hc_wlan_data_unpack(hc_wattr->erd_tag_id, hc_wattr->pld_ofs, hc_wattr->pld_len, outbuf, &outlen);
  607. if (ret) {
  608. kfree(outbuf);
  609. return ret;
  610. }
  611. if (off >= outlen) {
  612. kfree(outbuf);
  613. return 0;
  614. }
  615. if (off + count > outlen)
  616. count = outlen - off;
  617. memcpy(buf, outbuf + off, count);
  618. kfree(outbuf);
  619. return count;
  620. }
  621. int rb_hardconfig_init(struct kobject *rb_kobj, struct mtd_info *mtd)
  622. {
  623. struct kobject *hc_wlan_kobj;
  624. size_t bytes_read, buflen, outlen;
  625. const u8 *buf;
  626. void *outbuf;
  627. int i, j, ret;
  628. u32 magic;
  629. hc_buf = NULL;
  630. hc_kobj = NULL;
  631. hc_wlan_kobj = NULL;
  632. ret = __get_mtd_device(mtd);
  633. if (ret)
  634. return -ENODEV;
  635. hc_buflen = mtd->size;
  636. hc_buf = kmalloc(hc_buflen, GFP_KERNEL);
  637. if (!hc_buf) {
  638. __put_mtd_device(mtd);
  639. return -ENOMEM;
  640. }
  641. ret = mtd_read(mtd, 0, hc_buflen, &bytes_read, hc_buf);
  642. __put_mtd_device(mtd);
  643. if (ret)
  644. goto fail;
  645. if (bytes_read != hc_buflen) {
  646. ret = -EIO;
  647. goto fail;
  648. }
  649. /* Check we have what we expect */
  650. magic = *(const u32 *)hc_buf;
  651. if (RB_MAGIC_HARD != magic) {
  652. ret = -EINVAL;
  653. goto fail;
  654. }
  655. /* Skip magic */
  656. buf = hc_buf + sizeof(magic);
  657. buflen = hc_buflen - sizeof(magic);
  658. /* Populate sysfs */
  659. ret = -ENOMEM;
  660. hc_kobj = kobject_create_and_add(RB_MTD_HARD_CONFIG, rb_kobj);
  661. if (!hc_kobj)
  662. goto fail;
  663. /* Locate and publish all known tags */
  664. for (i = 0; i < ARRAY_SIZE(hc_attrs); i++) {
  665. ret = routerboot_tag_find(buf, buflen, hc_attrs[i].tag_id,
  666. &hc_attrs[i].pld_ofs, &hc_attrs[i].pld_len);
  667. if (ret) {
  668. hc_attrs[i].pld_ofs = hc_attrs[i].pld_len = 0;
  669. continue;
  670. }
  671. /* Account for skipped magic */
  672. hc_attrs[i].pld_ofs += sizeof(magic);
  673. /*
  674. * Special case RB_ID_WLAN_DATA to prep and create the binary attribute.
  675. * We first check if the data is "old style" within a single tag (or no tag at all):
  676. * If it is we publish this single blob as a binary attribute child of hc_kobj to
  677. * preserve backward compatibility.
  678. * If it isn't and instead uses multiple ERD tags, we create a subfolder and
  679. * publish the known ones there.
  680. */
  681. if ((RB_ID_WLAN_DATA == hc_attrs[i].tag_id) && hc_attrs[i].pld_len) {
  682. outlen = RB_ART_SIZE;
  683. outbuf = kmalloc(outlen, GFP_KERNEL);
  684. if (!outbuf) {
  685. pr_warn(RB_HC_PR_PFX "Out of memory parsing WLAN tag\n");
  686. continue;
  687. }
  688. /* Test ID_SOLO first, if found: done */
  689. ret = hc_wlan_data_unpack(RB_WLAN_ERD_ID_SOLO, hc_attrs[i].pld_ofs, hc_attrs[i].pld_len, outbuf, &outlen);
  690. if (!ret) {
  691. hc_wd_solo_battr.pld_ofs = hc_attrs[i].pld_ofs;
  692. hc_wd_solo_battr.pld_len = hc_attrs[i].pld_len;
  693. ret = sysfs_create_bin_file(hc_kobj, &hc_wd_solo_battr.battr);
  694. if (ret)
  695. pr_warn(RB_HC_PR_PFX "Could not create %s sysfs entry (%d)\n",
  696. hc_wd_solo_battr.battr.attr.name, ret);
  697. }
  698. /* Otherwise, create "wlan_data" subtree and publish known data */
  699. else {
  700. hc_wlan_kobj = kobject_create_and_add("wlan_data", hc_kobj);
  701. if (!hc_wlan_kobj) {
  702. kfree(outbuf);
  703. pr_warn(RB_HC_PR_PFX "Could not create wlan_data sysfs folder\n");
  704. continue;
  705. }
  706. for (j = 0; j < ARRAY_SIZE(hc_wd_multi_battrs); j++) {
  707. outlen = RB_ART_SIZE;
  708. ret = hc_wlan_data_unpack(hc_wd_multi_battrs[j].erd_tag_id,
  709. hc_attrs[i].pld_ofs, hc_attrs[i].pld_len, outbuf, &outlen);
  710. if (ret) {
  711. hc_wd_multi_battrs[j].pld_ofs = hc_wd_multi_battrs[j].pld_len = 0;
  712. continue;
  713. }
  714. hc_wd_multi_battrs[j].pld_ofs = hc_attrs[i].pld_ofs;
  715. hc_wd_multi_battrs[j].pld_len = hc_attrs[i].pld_len;
  716. ret = sysfs_create_bin_file(hc_wlan_kobj, &hc_wd_multi_battrs[j].battr);
  717. if (ret)
  718. pr_warn(RB_HC_PR_PFX "Could not create wlan_data/%s sysfs entry (%d)\n",
  719. hc_wd_multi_battrs[j].battr.attr.name, ret);
  720. }
  721. }
  722. kfree(outbuf);
  723. }
  724. /* All other tags are published via standard attributes */
  725. else {
  726. ret = sysfs_create_file(hc_kobj, &hc_attrs[i].kattr.attr);
  727. if (ret)
  728. pr_warn(RB_HC_PR_PFX "Could not create %s sysfs entry (%d)\n",
  729. hc_attrs[i].kattr.attr.name, ret);
  730. }
  731. }
  732. pr_info("MikroTik RouterBOARD hardware configuration sysfs driver v" RB_HARDCONFIG_VER "\n");
  733. return 0;
  734. fail:
  735. kfree(hc_buf);
  736. hc_buf = NULL;
  737. return ret;
  738. }
  739. void rb_hardconfig_exit(void)
  740. {
  741. kobject_put(hc_kobj);
  742. hc_kobj = NULL;
  743. kfree(hc_buf);
  744. hc_buf = NULL;
  745. }