1 // SPDX-License-Identifier: GPL-2.0-only
3 * Driver for MikroTik RouterBoot flash data. Common routines.
5 * Copyright (C) 2020 Thibaut VARĂˆNE <hacks+kernel@slashdirt.org>
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.
12 #include <linux/types.h>
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/sysfs.h>
16 #include <linux/mtd/mtd.h>
18 #include "routerboot.h"
20 static struct kobject
*rb_kobj
;
23 * routerboot_tag_find() - Locate a given tag in routerboot config data.
24 * @bufhead: the buffer to look into. Must start with a tag node.
25 * @buflen: size of bufhead
26 * @tag_id: the tag identifier to look for
27 * @pld_ofs: will be updated with tag payload offset in bufhead, if tag found
28 * @pld_len: will be updated with tag payload size, if tag found
30 * This incarnation of tag_find() does only that: it finds a specific routerboot
31 * tag node in the input buffer. Routerboot tag nodes are u32 values:
32 * - The low nibble is the tag identification number,
33 * - The high nibble is the tag payload length (node excluded) in bytes.
34 * The payload immediately follows the tag node. Tag nodes are 32bit-aligned.
35 * The returned pld_ofs will always be aligned. pld_len may not end on 32bit
36 * boundary (the only known case is when parsing ERD data).
37 * The nodes are cpu-endian on the flash media. The payload is cpu-endian when
38 * applicable. Tag nodes are not ordered (by ID) on flash.
40 * Return: 0 on success (tag found) or errno
42 int routerboot_tag_find(const u8
*bufhead
, const size_t buflen
, const u16 tag_id
,
43 u16
*pld_ofs
, u16
*pld_len
)
45 const u32
*datum
, *bufend
;
50 if (!bufhead
|| !tag_id
)
54 datum
= (const u32
*)bufhead
;
55 bufend
= (const u32
*)(bufhead
+ buflen
);
57 while (datum
< bufend
) {
60 /* Tag list ends with null node */
72 *pld_ofs
= (u16
)((u8
*)datum
- bufhead
);
81 * The only known situation where len may not end on 32bit
82 * boundary is within ERD data. Since we're only extracting
83 * one tag (the first and only one) from that data, we should
84 * never need to forcefully ALIGN(). Do it anyway, this is not a
87 len
= ALIGN(len
, sizeof(*datum
));
88 datum
+= len
/ sizeof(*datum
);
95 * routerboot_rle_decode() - Simple RLE (MikroTik variant) decoding routine.
96 * @in: input buffer to decode
98 * @out: output buffer to write decoded data to
99 * @outlen: pointer to out size when function is called, will be updated with
100 * size of decoded output on return
102 * MikroTik's variant of RLE operates as follows, considering a signed run byte:
103 * - positive run => classic RLE
104 * - negative run => the next -<run> bytes must be copied verbatim
105 * The API is matched to the lzo1x routines for convenience.
107 * NB: The output buffer cannot overlap with the input buffer.
109 * Return: 0 on success or errno
111 int routerboot_rle_decode(const u8
*in
, size_t inlen
, u8
*out
, size_t *outlen
)
113 int ret
, run
, nbytes
; // use native types for speed
116 if (!in
|| (inlen
< 2) || !out
)
125 /* Verbatim copies */
127 /* Invert run byte sign */
137 if (nbytes
> *outlen
)
144 /* Stream of half-words RLE: <run><byte>. run == 0 is ignored */
150 if (nbytes
> *outlen
)
164 static void routerboot_mtd_notifier_add(struct mtd_info
*mtd
)
166 /* Currently routerboot is only known to live on NOR flash */
167 if (mtd
->type
!= MTD_NORFLASH
)
171 * We ignore the following return values and always register.
172 * These init() routines are designed so that their failed state is
173 * always manageable by the corresponding exit() calls.
174 * Notifier is called with MTD mutex held: use __get/__put variants.
175 * TODO: allow partition names override
177 if (!strcmp(mtd
->name
, RB_MTD_HARD_CONFIG
))
178 rb_hardconfig_init(rb_kobj
, mtd
);
179 else if (!strcmp(mtd
->name
, RB_MTD_SOFT_CONFIG
))
180 rb_softconfig_init(rb_kobj
, mtd
);
183 static void routerboot_mtd_notifier_remove(struct mtd_info
*mtd
)
185 if (mtd
->type
!= MTD_NORFLASH
)
188 if (!strcmp(mtd
->name
, RB_MTD_HARD_CONFIG
))
189 rb_hardconfig_exit();
190 else if (!strcmp(mtd
->name
, RB_MTD_SOFT_CONFIG
))
191 rb_softconfig_exit();
194 /* Note: using a notifier prevents qualifying init()/exit() functions with __init/__exit */
195 static struct mtd_notifier routerboot_mtd_notifier
= {
196 .add
= routerboot_mtd_notifier_add
,
197 .remove
= routerboot_mtd_notifier_remove
,
200 static int __init
routerboot_init(void)
202 rb_kobj
= kobject_create_and_add("mikrotik", firmware_kobj
);
206 register_mtd_user(&routerboot_mtd_notifier
);
211 static void __exit
routerboot_exit(void)
213 unregister_mtd_user(&routerboot_mtd_notifier
);
214 /* Exit routines are idempotent */
215 rb_softconfig_exit();
216 rb_hardconfig_exit();
217 kobject_put(rb_kobj
); // recursive afaict
220 /* Common routines */
222 ssize_t
routerboot_tag_show_string(const u8
*pld
, u16 pld_len
, char *buf
)
224 return scnprintf(buf
, pld_len
+1, "%s\n", pld
);
227 ssize_t
routerboot_tag_show_u32s(const u8
*pld
, u16 pld_len
, char *buf
)
230 u32
*data
; // cpu-endian
232 /* Caller ensures pld_len > 0 */
233 if (pld_len
% sizeof(*data
))
239 out
+= sprintf(out
, "0x%08x\n", *data
);
241 } while ((pld_len
-= sizeof(*data
)));
246 module_init(routerboot_init
);
247 module_exit(routerboot_exit
);
249 MODULE_LICENSE("GPL v2");
250 MODULE_DESCRIPTION("MikroTik RouterBoot sysfs support");
251 MODULE_AUTHOR("Thibaut VARENE");