rb_softconfig.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Driver for MikroTik RouterBoot soft 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 "soft_config" flash segment of
  12. * MikroTik RouterBOARDs devices. It presents the data in a sysfs folder
  13. * named "soft_config". The data is presented in a user/machine-friendly way
  14. * with just as much parsing as can be generalized across mikrotik platforms
  15. * (as inferred from reverse-engineering).
  16. *
  17. * The known soft_config tags are presented in the "soft_config" sysfs folder,
  18. * with the addition of one specific file named "commit", which is only
  19. * available if the driver supports writes to the mtd device: no modifications
  20. * made to any of the other attributes are actually written back to flash media
  21. * until a true value is input into this file (e.g. [Yy1]). This is to avoid
  22. * unnecessary flash wear, and to permit to revert all changes by issuing a
  23. * false value ([Nn0]). Reading the content of this file shows the current
  24. * status of the driver: if the data in sysfs matches the content of the
  25. * soft_config partition, the file will read "clean". Otherwise, it will read
  26. * "dirty".
  27. *
  28. * The writeable sysfs files presented by this driver will accept only inputs
  29. * which are in a valid range for the given tag. As a design choice, the driver
  30. * will not assess whether the inputs are identical to the existing data.
  31. *
  32. * Note: PAGE_SIZE is assumed to be >= 4K, hence the device attribute show
  33. * routines need not check for output overflow.
  34. *
  35. * Some constant defines extracted from rbcfg.h by Gabor Juhos
  36. * <[email protected]>
  37. */
  38. #include <linux/types.h>
  39. #include <linux/init.h>
  40. #include <linux/kernel.h>
  41. #include <linux/slab.h>
  42. #include <linux/errno.h>
  43. #include <linux/kobject.h>
  44. #include <linux/string.h>
  45. #include <linux/mtd/mtd.h>
  46. #include <linux/sysfs.h>
  47. #include <linux/version.h>
  48. #include <linux/capability.h>
  49. #include <linux/spinlock.h>
  50. #include <linux/crc32.h>
  51. #ifdef CONFIG_ATH79
  52. #include <asm/mach-ath79/ath79.h>
  53. #endif
  54. #include "routerboot.h"
  55. #define RB_SOFTCONFIG_VER "0.05"
  56. #define RB_SC_PR_PFX "[rb_softconfig] "
  57. #define RB_SC_HAS_WRITE_SUPPORT true
  58. #define RB_SC_WMODE S_IWUSR
  59. #define RB_SC_RMODE S_IRUSR
  60. /* ID values for software settings */
  61. #define RB_SCID_UART_SPEED 0x01 // u32*1
  62. #define RB_SCID_BOOT_DELAY 0x02 // u32*1
  63. #define RB_SCID_BOOT_DEVICE 0x03 // u32*1
  64. #define RB_SCID_BOOT_KEY 0x04 // u32*1
  65. #define RB_SCID_CPU_MODE 0x05 // u32*1
  66. #define RB_SCID_BIOS_VERSION 0x06 // str
  67. #define RB_SCID_BOOT_PROTOCOL 0x09 // u32*1
  68. #define RB_SCID_CPU_FREQ_IDX 0x0C // u32*1
  69. #define RB_SCID_BOOTER 0x0D // u32*1
  70. #define RB_SCID_SILENT_BOOT 0x0F // u32*1
  71. /*
  72. * protected_routerboot seems to use tag 0x1F. It only works in combination with
  73. * RouterOS, resulting in a wiped board otherwise, so it's not implemented here.
  74. * The tag values are as follows:
  75. * - off: 0x0
  76. * - on: the lower halfword encodes the max value in s for the reset feature,
  77. * the higher halfword encodes the min value in s for the reset feature.
  78. * Default value when on: 0x00140258: 0x14 = 20s / 0x258= 600s
  79. * See details here: https://wiki.mikrotik.com/wiki/Manual:RouterBOARD_settings#Protected_bootloader
  80. */
  81. /* Tag values */
  82. #define RB_UART_SPEED_115200 0
  83. #define RB_UART_SPEED_57600 1
  84. #define RB_UART_SPEED_38400 2
  85. #define RB_UART_SPEED_19200 3
  86. #define RB_UART_SPEED_9600 4
  87. #define RB_UART_SPEED_4800 5
  88. #define RB_UART_SPEED_2400 6
  89. #define RB_UART_SPEED_1200 7
  90. #define RB_UART_SPEED_OFF 8
  91. /* valid boot delay: 1 - 9s in 1s increment */
  92. #define RB_BOOT_DELAY_MIN 1
  93. #define RB_BOOT_DELAY_MAX 9
  94. #define RB_BOOT_DEVICE_ETHER 0 // "boot over Ethernet"
  95. #define RB_BOOT_DEVICE_NANDETH 1 // "boot from NAND, if fail then Ethernet"
  96. #define RB_BOOT_DEVICE_CFCARD 2 // (not available in rbcfg)
  97. #define RB_BOOT_DEVICE_ETHONCE 3 // "boot Ethernet once, then NAND"
  98. #define RB_BOOT_DEVICE_NANDONLY 5 // "boot from NAND only"
  99. #define RB_BOOT_DEVICE_FLASHCFG 7 // "boot in flash configuration mode"
  100. #define RB_BOOT_DEVICE_FLSHONCE 8 // "boot in flash configuration mode once, then NAND"
  101. /*
  102. * ATH79 9xxx CPU frequency indices.
  103. * It is unknown if they apply to all ATH79 RBs, and some do not seem to feature
  104. * the upper levels (QCA955x), while F is presumably AR9344-only.
  105. */
  106. #define RB_CPU_FREQ_IDX_ATH79_9X_A (0 << 3)
  107. #define RB_CPU_FREQ_IDX_ATH79_9X_B (1 << 3) // 0x8
  108. #define RB_CPU_FREQ_IDX_ATH79_9X_C (2 << 3) // 0x10 - factory freq for many devices
  109. #define RB_CPU_FREQ_IDX_ATH79_9X_D (3 << 3) // 0x18
  110. #define RB_CPU_FREQ_IDX_ATH79_9X_E (4 << 3) // 0x20
  111. #define RB_CPU_FREQ_IDX_ATH79_9X_F (5 << 3) // 0x28
  112. #define RB_CPU_FREQ_IDX_ATH79_9X_MIN 0 // all devices support lowest setting
  113. #define RB_CPU_FREQ_IDX_ATH79_9X_AR9334_MAX 5 // stops at F
  114. #define RB_CPU_FREQ_IDX_ATH79_9X_QCA953X_MAX 4 // stops at E
  115. #define RB_CPU_FREQ_IDX_ATH79_9X_QCA9556_MAX 2 // stops at C
  116. #define RB_CPU_FREQ_IDX_ATH79_9X_QCA9558_MAX 3 // stops at D
  117. /* ATH79 7xxx CPU frequency indices. */
  118. #define RB_CPU_FREQ_IDX_ATH79_7X_A ((0 * 9) << 4)
  119. #define RB_CPU_FREQ_IDX_ATH79_7X_B ((1 * 9) << 4)
  120. #define RB_CPU_FREQ_IDX_ATH79_7X_C ((2 * 9) << 4)
  121. #define RB_CPU_FREQ_IDX_ATH79_7X_D ((3 * 9) << 4)
  122. #define RB_CPU_FREQ_IDX_ATH79_7X_E ((4 * 9) << 4)
  123. #define RB_CPU_FREQ_IDX_ATH79_7X_F ((5 * 9) << 4)
  124. #define RB_CPU_FREQ_IDX_ATH79_7X_G ((6 * 9) << 4)
  125. #define RB_CPU_FREQ_IDX_ATH79_7X_H ((7 * 9) << 4)
  126. #define RB_CPU_FREQ_IDX_ATH79_7X_MIN 0 // all devices support lowest setting
  127. #define RB_CPU_FREQ_IDX_ATH79_7X_AR724X_MAX 3 // stops at D
  128. #define RB_CPU_FREQ_IDX_ATH79_7X_AR7161_MAX 7 // stops at H - check if applies to all AR71xx devices
  129. #define RB_SC_CRC32_OFFSET 4 // located right after magic
  130. static struct kobject *sc_kobj;
  131. static u8 *sc_buf;
  132. static size_t sc_buflen;
  133. static rwlock_t sc_bufrwl; // rw lock to sc_buf
  134. /* MUST be used with lock held */
  135. #define RB_SC_CLRCRC() *(u32 *)(sc_buf + RB_SC_CRC32_OFFSET) = 0
  136. #define RB_SC_GETCRC() *(u32 *)(sc_buf + RB_SC_CRC32_OFFSET)
  137. #define RB_SC_SETCRC(_crc) *(u32 *)(sc_buf + RB_SC_CRC32_OFFSET) = (_crc)
  138. struct sc_u32tvs {
  139. const u32 val;
  140. const char *str;
  141. };
  142. #define RB_SC_TVS(_val, _str) { \
  143. .val = (_val), \
  144. .str = (_str), \
  145. }
  146. static ssize_t sc_tag_show_u32tvs(const u8 *pld, u16 pld_len, char *buf,
  147. const struct sc_u32tvs tvs[], const int tvselmts)
  148. {
  149. const char *fmt;
  150. char *out = buf;
  151. u32 data; // cpu-endian
  152. int i;
  153. // fallback to raw hex output if we can't handle the input
  154. if (tvselmts < 0)
  155. return routerboot_tag_show_u32s(pld, pld_len, buf);
  156. if (sizeof(data) != pld_len)
  157. return -EINVAL;
  158. read_lock(&sc_bufrwl);
  159. data = *(u32 *)pld; // pld aliases sc_buf
  160. read_unlock(&sc_bufrwl);
  161. for (i = 0; i < tvselmts; i++) {
  162. fmt = (tvs[i].val == data) ? "[%s] " : "%s ";
  163. out += sprintf(out, fmt, tvs[i].str);
  164. }
  165. out += sprintf(out, "\n");
  166. return out - buf;
  167. }
  168. static ssize_t sc_tag_store_u32tvs(const u8 *pld, u16 pld_len, const char *buf, size_t count,
  169. const struct sc_u32tvs tvs[], const int tvselmts)
  170. {
  171. int i;
  172. if (tvselmts < 0)
  173. return tvselmts;
  174. if (sizeof(u32) != pld_len)
  175. return -EINVAL;
  176. for (i = 0; i < tvselmts; i++) {
  177. if (sysfs_streq(buf, tvs[i].str)) {
  178. write_lock(&sc_bufrwl);
  179. *(u32 *)pld = tvs[i].val; // pld aliases sc_buf
  180. RB_SC_CLRCRC();
  181. write_unlock(&sc_bufrwl);
  182. return count;
  183. }
  184. }
  185. return -EINVAL;
  186. }
  187. struct sc_boolts {
  188. const char *strfalse;
  189. const char *strtrue;
  190. };
  191. static ssize_t sc_tag_show_boolts(const u8 *pld, u16 pld_len, char *buf,
  192. const struct sc_boolts *bts)
  193. {
  194. const char *fmt;
  195. char *out = buf;
  196. u32 data; // cpu-endian
  197. if (sizeof(data) != pld_len)
  198. return -EINVAL;
  199. read_lock(&sc_bufrwl);
  200. data = *(u32 *)pld; // pld aliases sc_buf
  201. read_unlock(&sc_bufrwl);
  202. fmt = (data) ? "%s [%s]\n" : "[%s] %s\n";
  203. out += sprintf(out, fmt, bts->strfalse, bts->strtrue);
  204. return out - buf;
  205. }
  206. static ssize_t sc_tag_store_boolts(const u8 *pld, u16 pld_len, const char *buf, size_t count,
  207. const struct sc_boolts *bts)
  208. {
  209. u32 data; // cpu-endian
  210. if (sizeof(data) != pld_len)
  211. return -EINVAL;
  212. if (sysfs_streq(buf, bts->strfalse))
  213. data = 0;
  214. else if (sysfs_streq(buf, bts->strtrue))
  215. data = 1;
  216. else
  217. return -EINVAL;
  218. write_lock(&sc_bufrwl);
  219. *(u32 *)pld = data; // pld aliases sc_buf
  220. RB_SC_CLRCRC();
  221. write_unlock(&sc_bufrwl);
  222. return count;
  223. }
  224. static struct sc_u32tvs const sc_uartspeeds[] = {
  225. RB_SC_TVS(RB_UART_SPEED_OFF, "off"),
  226. RB_SC_TVS(RB_UART_SPEED_1200, "1200"),
  227. RB_SC_TVS(RB_UART_SPEED_2400, "2400"),
  228. RB_SC_TVS(RB_UART_SPEED_4800, "4800"),
  229. RB_SC_TVS(RB_UART_SPEED_9600, "9600"),
  230. RB_SC_TVS(RB_UART_SPEED_19200, "19200"),
  231. RB_SC_TVS(RB_UART_SPEED_38400, "38400"),
  232. RB_SC_TVS(RB_UART_SPEED_57600, "57600"),
  233. RB_SC_TVS(RB_UART_SPEED_115200, "115200"),
  234. };
  235. /*
  236. * While the defines are carried over from rbcfg, use strings that more clearly
  237. * show the actual setting purpose (especially since the NAND* settings apply
  238. * to both nand- and nor-based devices). "cfcard" was disabled in rbcfg: disable
  239. * it here too.
  240. */
  241. static struct sc_u32tvs const sc_bootdevices[] = {
  242. RB_SC_TVS(RB_BOOT_DEVICE_ETHER, "eth"),
  243. RB_SC_TVS(RB_BOOT_DEVICE_NANDETH, "flasheth"),
  244. //RB_SC_TVS(RB_BOOT_DEVICE_CFCARD, "cfcard"),
  245. RB_SC_TVS(RB_BOOT_DEVICE_ETHONCE, "ethonce"),
  246. RB_SC_TVS(RB_BOOT_DEVICE_NANDONLY, "flash"),
  247. RB_SC_TVS(RB_BOOT_DEVICE_FLASHCFG, "cfg"),
  248. RB_SC_TVS(RB_BOOT_DEVICE_FLSHONCE, "cfgonce"),
  249. };
  250. static struct sc_boolts const sc_bootkey = {
  251. .strfalse = "any",
  252. .strtrue = "del",
  253. };
  254. static struct sc_boolts const sc_cpumode = {
  255. .strfalse = "powersave",
  256. .strtrue = "regular",
  257. };
  258. static struct sc_boolts const sc_bootproto = {
  259. .strfalse = "bootp",
  260. .strtrue = "dhcp",
  261. };
  262. static struct sc_boolts const sc_booter = {
  263. .strfalse = "regular",
  264. .strtrue = "backup",
  265. };
  266. static struct sc_boolts const sc_silent_boot = {
  267. .strfalse = "off",
  268. .strtrue = "on",
  269. };
  270. #define SC_TAG_SHOW_STORE_U32TVS_FUNCS(_name) \
  271. static ssize_t sc_tag_show_##_name(const u8 *pld, u16 pld_len, char *buf) \
  272. { \
  273. return sc_tag_show_u32tvs(pld, pld_len, buf, sc_##_name, ARRAY_SIZE(sc_##_name)); \
  274. } \
  275. static ssize_t sc_tag_store_##_name(const u8 *pld, u16 pld_len, const char *buf, size_t count) \
  276. { \
  277. return sc_tag_store_u32tvs(pld, pld_len, buf, count, sc_##_name, ARRAY_SIZE(sc_##_name)); \
  278. }
  279. #define SC_TAG_SHOW_STORE_BOOLTS_FUNCS(_name) \
  280. static ssize_t sc_tag_show_##_name(const u8 *pld, u16 pld_len, char *buf) \
  281. { \
  282. return sc_tag_show_boolts(pld, pld_len, buf, &sc_##_name); \
  283. } \
  284. static ssize_t sc_tag_store_##_name(const u8 *pld, u16 pld_len, const char *buf, size_t count) \
  285. { \
  286. return sc_tag_store_boolts(pld, pld_len, buf, count, &sc_##_name); \
  287. }
  288. SC_TAG_SHOW_STORE_U32TVS_FUNCS(uartspeeds)
  289. SC_TAG_SHOW_STORE_U32TVS_FUNCS(bootdevices)
  290. SC_TAG_SHOW_STORE_BOOLTS_FUNCS(bootkey)
  291. SC_TAG_SHOW_STORE_BOOLTS_FUNCS(cpumode)
  292. SC_TAG_SHOW_STORE_BOOLTS_FUNCS(bootproto)
  293. SC_TAG_SHOW_STORE_BOOLTS_FUNCS(booter)
  294. SC_TAG_SHOW_STORE_BOOLTS_FUNCS(silent_boot)
  295. static ssize_t sc_tag_show_bootdelays(const u8 *pld, u16 pld_len, char *buf)
  296. {
  297. const char *fmt;
  298. char *out = buf;
  299. u32 data; // cpu-endian
  300. int i;
  301. if (sizeof(data) != pld_len)
  302. return -EINVAL;
  303. read_lock(&sc_bufrwl);
  304. data = *(u32 *)pld; // pld aliases sc_buf
  305. read_unlock(&sc_bufrwl);
  306. for (i = RB_BOOT_DELAY_MIN; i <= RB_BOOT_DELAY_MAX; i++) {
  307. fmt = (i == data) ? "[%d] " : "%d ";
  308. out += sprintf(out, fmt, i);
  309. }
  310. out += sprintf(out, "\n");
  311. return out - buf;
  312. }
  313. static ssize_t sc_tag_store_bootdelays(const u8 *pld, u16 pld_len, const char *buf, size_t count)
  314. {
  315. u32 data; // cpu-endian
  316. int ret;
  317. if (sizeof(data) != pld_len)
  318. return -EINVAL;
  319. ret = kstrtou32(buf, 10, &data);
  320. if (ret)
  321. return ret;
  322. if ((data < RB_BOOT_DELAY_MIN) || (RB_BOOT_DELAY_MAX < data))
  323. return -EINVAL;
  324. write_lock(&sc_bufrwl);
  325. *(u32 *)pld = data; // pld aliases sc_buf
  326. RB_SC_CLRCRC();
  327. write_unlock(&sc_bufrwl);
  328. return count;
  329. }
  330. /* Support CPU frequency accessors only when the tag format has been asserted */
  331. #if defined(CONFIG_ATH79)
  332. /* Use the same letter-based nomenclature as RouterBOOT */
  333. static struct sc_u32tvs const sc_cpufreq_indexes_ath79_9x[] = {
  334. RB_SC_TVS(RB_CPU_FREQ_IDX_ATH79_9X_A, "a"),
  335. RB_SC_TVS(RB_CPU_FREQ_IDX_ATH79_9X_B, "b"),
  336. RB_SC_TVS(RB_CPU_FREQ_IDX_ATH79_9X_C, "c"),
  337. RB_SC_TVS(RB_CPU_FREQ_IDX_ATH79_9X_D, "d"),
  338. RB_SC_TVS(RB_CPU_FREQ_IDX_ATH79_9X_E, "e"),
  339. RB_SC_TVS(RB_CPU_FREQ_IDX_ATH79_9X_F, "f"),
  340. };
  341. static struct sc_u32tvs const sc_cpufreq_indexes_ath79_7x[] = {
  342. RB_SC_TVS(RB_CPU_FREQ_IDX_ATH79_7X_A, "a"),
  343. RB_SC_TVS(RB_CPU_FREQ_IDX_ATH79_7X_B, "b"),
  344. RB_SC_TVS(RB_CPU_FREQ_IDX_ATH79_7X_C, "c"),
  345. RB_SC_TVS(RB_CPU_FREQ_IDX_ATH79_7X_D, "d"),
  346. RB_SC_TVS(RB_CPU_FREQ_IDX_ATH79_7X_E, "e"),
  347. RB_SC_TVS(RB_CPU_FREQ_IDX_ATH79_7X_F, "f"),
  348. RB_SC_TVS(RB_CPU_FREQ_IDX_ATH79_7X_G, "g"),
  349. RB_SC_TVS(RB_CPU_FREQ_IDX_ATH79_7X_H, "h"),
  350. };
  351. static int sc_tag_cpufreq_ath79_arraysize(void)
  352. {
  353. int idx_max;
  354. if (ATH79_SOC_AR7161 == ath79_soc)
  355. idx_max = RB_CPU_FREQ_IDX_ATH79_7X_AR7161_MAX+1;
  356. else if (soc_is_ar724x())
  357. idx_max = RB_CPU_FREQ_IDX_ATH79_7X_AR724X_MAX+1;
  358. else if (soc_is_ar9344())
  359. idx_max = RB_CPU_FREQ_IDX_ATH79_9X_AR9334_MAX+1;
  360. else if (soc_is_qca953x())
  361. idx_max = RB_CPU_FREQ_IDX_ATH79_9X_QCA953X_MAX+1;
  362. else if (soc_is_qca9556())
  363. idx_max = RB_CPU_FREQ_IDX_ATH79_9X_QCA9556_MAX+1;
  364. else if (soc_is_qca9558())
  365. idx_max = RB_CPU_FREQ_IDX_ATH79_9X_QCA9558_MAX+1;
  366. else
  367. idx_max = -EOPNOTSUPP;
  368. return idx_max;
  369. }
  370. static ssize_t sc_tag_show_cpufreq_indexes(const u8 *pld, u16 pld_len, char *buf)
  371. {
  372. const struct sc_u32tvs *tvs;
  373. if (soc_is_ar71xx() || soc_is_ar724x())
  374. tvs = sc_cpufreq_indexes_ath79_7x;
  375. else
  376. tvs = sc_cpufreq_indexes_ath79_9x;
  377. return sc_tag_show_u32tvs(pld, pld_len, buf, tvs, sc_tag_cpufreq_ath79_arraysize());
  378. }
  379. static ssize_t sc_tag_store_cpufreq_indexes(const u8 *pld, u16 pld_len, const char *buf, size_t count)
  380. {
  381. const struct sc_u32tvs *tvs;
  382. if (soc_is_ar71xx() || soc_is_ar724x())
  383. tvs = sc_cpufreq_indexes_ath79_7x;
  384. else
  385. tvs = sc_cpufreq_indexes_ath79_9x;
  386. return sc_tag_store_u32tvs(pld, pld_len, buf, count, tvs, sc_tag_cpufreq_ath79_arraysize());
  387. }
  388. #else
  389. /* By default we only show the raw value to help with reverse-engineering */
  390. #define sc_tag_show_cpufreq_indexes routerboot_tag_show_u32s
  391. #define sc_tag_store_cpufreq_indexes NULL
  392. #endif
  393. static ssize_t sc_attr_show(struct kobject *kobj, struct kobj_attribute *attr,
  394. char *buf);
  395. static ssize_t sc_attr_store(struct kobject *kobj, struct kobj_attribute *attr,
  396. const char *buf, size_t count);
  397. /* Array of known tags to publish in sysfs */
  398. static struct sc_attr {
  399. const u16 tag_id;
  400. /* sysfs tag show attribute. Must lock sc_buf when dereferencing pld */
  401. ssize_t (* const tshow)(const u8 *pld, u16 pld_len, char *buf);
  402. /* sysfs tag store attribute. Must lock sc_buf when dereferencing pld */
  403. ssize_t (* const tstore)(const u8 *pld, u16 pld_len, const char *buf, size_t count);
  404. struct kobj_attribute kattr;
  405. u16 pld_ofs;
  406. u16 pld_len;
  407. } sc_attrs[] = {
  408. {
  409. .tag_id = RB_SCID_UART_SPEED,
  410. .tshow = sc_tag_show_uartspeeds,
  411. .tstore = sc_tag_store_uartspeeds,
  412. .kattr = __ATTR(uart_speed, RB_SC_RMODE|RB_SC_WMODE, sc_attr_show, sc_attr_store),
  413. }, {
  414. .tag_id = RB_SCID_BOOT_DELAY,
  415. .tshow = sc_tag_show_bootdelays,
  416. .tstore = sc_tag_store_bootdelays,
  417. .kattr = __ATTR(boot_delay, RB_SC_RMODE|RB_SC_WMODE, sc_attr_show, sc_attr_store),
  418. }, {
  419. .tag_id = RB_SCID_BOOT_DEVICE,
  420. .tshow = sc_tag_show_bootdevices,
  421. .tstore = sc_tag_store_bootdevices,
  422. .kattr = __ATTR(boot_device, RB_SC_RMODE|RB_SC_WMODE, sc_attr_show, sc_attr_store),
  423. }, {
  424. .tag_id = RB_SCID_BOOT_KEY,
  425. .tshow = sc_tag_show_bootkey,
  426. .tstore = sc_tag_store_bootkey,
  427. .kattr = __ATTR(boot_key, RB_SC_RMODE|RB_SC_WMODE, sc_attr_show, sc_attr_store),
  428. }, {
  429. .tag_id = RB_SCID_CPU_MODE,
  430. .tshow = sc_tag_show_cpumode,
  431. .tstore = sc_tag_store_cpumode,
  432. .kattr = __ATTR(cpu_mode, RB_SC_RMODE|RB_SC_WMODE, sc_attr_show, sc_attr_store),
  433. }, {
  434. .tag_id = RB_SCID_BIOS_VERSION,
  435. .tshow = routerboot_tag_show_string,
  436. .tstore = NULL,
  437. .kattr = __ATTR(bios_version, RB_SC_RMODE, sc_attr_show, NULL),
  438. }, {
  439. .tag_id = RB_SCID_BOOT_PROTOCOL,
  440. .tshow = sc_tag_show_bootproto,
  441. .tstore = sc_tag_store_bootproto,
  442. .kattr = __ATTR(boot_proto, RB_SC_RMODE|RB_SC_WMODE, sc_attr_show, sc_attr_store),
  443. }, {
  444. .tag_id = RB_SCID_CPU_FREQ_IDX,
  445. .tshow = sc_tag_show_cpufreq_indexes,
  446. .tstore = sc_tag_store_cpufreq_indexes,
  447. .kattr = __ATTR(cpufreq_index, RB_SC_RMODE|RB_SC_WMODE, sc_attr_show, sc_attr_store),
  448. }, {
  449. .tag_id = RB_SCID_BOOTER,
  450. .tshow = sc_tag_show_booter,
  451. .tstore = sc_tag_store_booter,
  452. .kattr = __ATTR(booter, RB_SC_RMODE|RB_SC_WMODE, sc_attr_show, sc_attr_store),
  453. }, {
  454. .tag_id = RB_SCID_SILENT_BOOT,
  455. .tshow = sc_tag_show_silent_boot,
  456. .tstore = sc_tag_store_silent_boot,
  457. .kattr = __ATTR(silent_boot, RB_SC_RMODE|RB_SC_WMODE, sc_attr_show, sc_attr_store),
  458. },
  459. };
  460. static ssize_t sc_attr_show(struct kobject *kobj, struct kobj_attribute *attr,
  461. char *buf)
  462. {
  463. const struct sc_attr *sc_attr;
  464. const u8 *pld;
  465. u16 pld_len;
  466. sc_attr = container_of(attr, typeof(*sc_attr), kattr);
  467. if (!sc_attr->pld_len)
  468. return -ENOENT;
  469. pld = sc_buf + sc_attr->pld_ofs; // pld aliases sc_buf -> lock!
  470. pld_len = sc_attr->pld_len;
  471. return sc_attr->tshow(pld, pld_len, buf);
  472. }
  473. static ssize_t sc_attr_store(struct kobject *kobj, struct kobj_attribute *attr,
  474. const char *buf, size_t count)
  475. {
  476. const struct sc_attr *sc_attr;
  477. const u8 *pld;
  478. u16 pld_len;
  479. if (!RB_SC_HAS_WRITE_SUPPORT)
  480. return -EOPNOTSUPP;
  481. if (!capable(CAP_SYS_ADMIN))
  482. return -EACCES;
  483. sc_attr = container_of(attr, typeof(*sc_attr), kattr);
  484. if (!sc_attr->tstore)
  485. return -EOPNOTSUPP;
  486. if (!sc_attr->pld_len)
  487. return -ENOENT;
  488. pld = sc_buf + sc_attr->pld_ofs; // pld aliases sc_buf -> lock!
  489. pld_len = sc_attr->pld_len;
  490. return sc_attr->tstore(pld, pld_len, buf, count);
  491. }
  492. /*
  493. * Shows the current buffer status:
  494. * "clean": the buffer is in sync with the mtd data
  495. * "dirty": the buffer is out of sync with the mtd data
  496. */
  497. static ssize_t sc_commit_show(struct kobject *kobj, struct kobj_attribute *attr,
  498. char *buf)
  499. {
  500. const char *str;
  501. char *out = buf;
  502. u32 crc;
  503. read_lock(&sc_bufrwl);
  504. crc = RB_SC_GETCRC();
  505. read_unlock(&sc_bufrwl);
  506. str = (crc) ? "clean" : "dirty";
  507. out += sprintf(out, "%s\n", str);
  508. return out - buf;
  509. }
  510. /*
  511. * Performs buffer flushing:
  512. * This routine expects an input compatible with kstrtobool().
  513. * - a "false" input discards the current changes and reads data back from mtd.
  514. * - a "true" input commits the current changes to mtd.
  515. * If there is no pending changes, this routine is a no-op.
  516. * Handling failures is left as an exercise to userspace.
  517. */
  518. static ssize_t sc_commit_store(struct kobject *kobj, struct kobj_attribute *attr,
  519. const char *buf, size_t count)
  520. {
  521. struct mtd_info *mtd;
  522. struct erase_info ei;
  523. size_t bytes_rw, ret = count;
  524. bool flush;
  525. u32 crc;
  526. if (!RB_SC_HAS_WRITE_SUPPORT)
  527. return -EOPNOTSUPP;
  528. read_lock(&sc_bufrwl);
  529. crc = RB_SC_GETCRC();
  530. read_unlock(&sc_bufrwl);
  531. if (crc)
  532. return count; // NO-OP
  533. ret = kstrtobool(buf, &flush);
  534. if (ret)
  535. return ret;
  536. mtd = get_mtd_device_nm(RB_MTD_SOFT_CONFIG); // TODO allow override
  537. if (IS_ERR(mtd))
  538. return -ENODEV;
  539. write_lock(&sc_bufrwl);
  540. if (!flush) // reread
  541. ret = mtd_read(mtd, 0, mtd->size, &bytes_rw, sc_buf);
  542. else { // crc32 + commit
  543. /*
  544. * CRC32 is computed on the entire buffer, excluding the CRC
  545. * value itself. CRC is already null when we reach this point,
  546. * so we can compute the CRC32 on the buffer as is.
  547. * The expected CRC32 is Ethernet FCS style, meaning the seed is
  548. * ~0 and the final result is also bitflipped.
  549. */
  550. crc = ~crc32(~0, sc_buf, sc_buflen);
  551. RB_SC_SETCRC(crc);
  552. /*
  553. * The soft_config partition is assumed to be entirely contained
  554. * in a single eraseblock.
  555. */
  556. ei.addr = 0;
  557. ei.len = mtd->size;
  558. ret = mtd_erase(mtd, &ei);
  559. if (!ret)
  560. ret = mtd_write(mtd, 0, mtd->size, &bytes_rw, sc_buf);
  561. /*
  562. * Handling mtd_write() failure here is a tricky situation. The
  563. * proposed approach is to let userspace deal with retrying,
  564. * with the caveat that it must try to flush the buffer again as
  565. * rereading the mtd contents could potentially read garbage.
  566. * The rationale is: even if we keep a shadow buffer of the
  567. * original content, there is no guarantee that we will ever be
  568. * able to write it anyway.
  569. * Regardless, it appears that RouterBOOT will ignore an invalid
  570. * soft_config (including a completely wiped segment) and will
  571. * write back factory defaults when it happens.
  572. */
  573. }
  574. write_unlock(&sc_bufrwl);
  575. put_mtd_device(mtd);
  576. if (ret)
  577. goto mtdfail;
  578. if (bytes_rw != sc_buflen) {
  579. ret = -EIO;
  580. goto mtdfail;
  581. }
  582. return count;
  583. mtdfail:
  584. RB_SC_CLRCRC(); // mark buffer content as dirty/invalid
  585. return ret;
  586. }
  587. static struct kobj_attribute sc_kattrcommit = __ATTR(commit, RB_SC_RMODE|RB_SC_WMODE, sc_commit_show, sc_commit_store);
  588. int rb_softconfig_init(struct kobject *rb_kobj, struct mtd_info *mtd)
  589. {
  590. size_t bytes_read, buflen;
  591. const u8 *buf;
  592. int i, ret;
  593. u32 magic;
  594. sc_buf = NULL;
  595. sc_kobj = NULL;
  596. ret = __get_mtd_device(mtd);
  597. if (ret)
  598. return -ENODEV;
  599. sc_buflen = mtd->size;
  600. sc_buf = kmalloc(sc_buflen, GFP_KERNEL);
  601. if (!sc_buf) {
  602. __put_mtd_device(mtd);
  603. return -ENOMEM;
  604. }
  605. ret = mtd_read(mtd, 0, sc_buflen, &bytes_read, sc_buf);
  606. __put_mtd_device(mtd);
  607. if (ret)
  608. goto fail;
  609. if (bytes_read != sc_buflen) {
  610. ret = -EIO;
  611. goto fail;
  612. }
  613. /* Check we have what we expect */
  614. magic = *(const u32 *)sc_buf;
  615. if (RB_MAGIC_SOFT != magic) {
  616. ret = -EINVAL;
  617. goto fail;
  618. }
  619. /* Skip magic and 32bit CRC located immediately after */
  620. buf = sc_buf + (sizeof(magic) + sizeof(u32));
  621. buflen = sc_buflen - (sizeof(magic) + sizeof(u32));
  622. /* Populate sysfs */
  623. ret = -ENOMEM;
  624. sc_kobj = kobject_create_and_add(RB_MTD_SOFT_CONFIG, rb_kobj);
  625. if (!sc_kobj)
  626. goto fail;
  627. rwlock_init(&sc_bufrwl);
  628. /* Locate and publish all known tags */
  629. for (i = 0; i < ARRAY_SIZE(sc_attrs); i++) {
  630. ret = routerboot_tag_find(buf, buflen, sc_attrs[i].tag_id,
  631. &sc_attrs[i].pld_ofs, &sc_attrs[i].pld_len);
  632. if (ret) {
  633. sc_attrs[i].pld_ofs = sc_attrs[i].pld_len = 0;
  634. continue;
  635. }
  636. /* Account for skipped magic and crc32 */
  637. sc_attrs[i].pld_ofs += sizeof(magic) + sizeof(u32);
  638. ret = sysfs_create_file(sc_kobj, &sc_attrs[i].kattr.attr);
  639. if (ret)
  640. pr_warn(RB_SC_PR_PFX "Could not create %s sysfs entry (%d)\n",
  641. sc_attrs[i].kattr.attr.name, ret);
  642. }
  643. /* Finally add the 'commit' attribute */
  644. if (RB_SC_HAS_WRITE_SUPPORT) {
  645. ret = sysfs_create_file(sc_kobj, &sc_kattrcommit.attr);
  646. if (ret) {
  647. pr_err(RB_SC_PR_PFX "Could not create %s sysfs entry (%d), aborting!\n",
  648. sc_kattrcommit.attr.name, ret);
  649. goto sysfsfail; // required attribute
  650. }
  651. }
  652. pr_info("MikroTik RouterBOARD software configuration sysfs driver v" RB_SOFTCONFIG_VER "\n");
  653. return 0;
  654. sysfsfail:
  655. kobject_put(sc_kobj);
  656. sc_kobj = NULL;
  657. fail:
  658. kfree(sc_buf);
  659. sc_buf = NULL;
  660. return ret;
  661. }
  662. void rb_softconfig_exit(void)
  663. {
  664. kobject_put(sc_kobj);
  665. sc_kobj = NULL;
  666. kfree(sc_buf);
  667. sc_buf = NULL;
  668. }