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