rb_softconfig.c 22 KB

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