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