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>
17 #include "routerboot.h"
19 static struct kobject
*rb_kobj
;
22 * routerboot_tag_find() - Locate a given tag in routerboot config data.
23 * @bufhead: the buffer to look into. Must start with a tag node.
24 * @buflen: size of bufhead
25 * @tag_id: the tag identifier to look for
26 * @pld_ofs: will be updated with tag payload offset in bufhead, if tag found
27 * @pld_len: will be updated with tag payload size, if tag found
29 * This incarnation of tag_find() does only that: it finds a specific routerboot
30 * tag node in the input buffer. Routerboot tag nodes are u32 values:
31 * - The low nibble is the tag identification number,
32 * - The high nibble is the tag payload length (node excluded) in bytes.
33 * The payload immediately follows the tag node. Tag nodes are 32bit-aligned.
34 * The returned pld_ofs will always be aligned. pld_len may not end on 32bit
35 * boundary (the only known case is when parsing ERD data).
36 * The nodes are cpu-endian on the flash media. The payload is cpu-endian when
37 * applicable. Tag nodes are not ordered (by ID) on flash.
39 * Return: 0 on success (tag found) or errno
41 int routerboot_tag_find(const u8
*bufhead
, const size_t buflen
, const u16 tag_id
,
42 u16
*pld_ofs
, u16
*pld_len
)
44 const u32
*datum
, *bufend
;
49 if (!bufhead
|| !tag_id
)
53 datum
= (const u32
*)bufhead
;
54 bufend
= (const u32
*)(bufhead
+ buflen
);
56 while (datum
< bufend
) {
59 /* Tag list ends with null node */
71 *pld_ofs
= (u16
)((u8
*)datum
- bufhead
);
80 * The only known situation where len may not end on 32bit
81 * boundary is within ERD data. Since we're only extracting
82 * one tag (the first and only one) from that data, we should
83 * never need to forcefully ALIGN(). Do it anyway, this is not a
86 len
= ALIGN(len
, sizeof(*datum
));
87 datum
+= len
/ sizeof(*datum
);
94 * routerboot_rle_decode() - Simple RLE (MikroTik variant) decoding routine.
95 * @in: input buffer to decode
97 * @out: output buffer to write decoded data to
98 * @outlen: pointer to out size when function is called, will be updated with
99 * size of decoded output on return
101 * MikroTik's variant of RLE operates as follows, considering a signed run byte:
102 * - positive run => classic RLE
103 * - negative run => the next -<run> bytes must be copied verbatim
104 * The API is matched to the lzo1x routines for convenience.
106 * NB: The output buffer cannot overlap with the input buffer.
108 * Return: 0 on success or errno
110 int routerboot_rle_decode(const u8
*in
, size_t inlen
, u8
*out
, size_t *outlen
)
112 int ret
, run
, nbytes
; // use native types for speed
115 if (!in
|| (inlen
< 2) || !out
)
124 /* Verbatim copies */
126 /* Invert run byte sign */
136 if (nbytes
> *outlen
)
143 /* Stream of half-words RLE: <run><byte>. run == 0 is ignored */
149 if (nbytes
> *outlen
)
163 static int __init
routerboot_init(void)
165 rb_kobj
= kobject_create_and_add("mikrotik", firmware_kobj
);
170 * We ignore the following return values and always register.
171 * These init() routines are designed so that their failed state is
172 * always manageable by the corresponding exit() calls.
174 rb_hardconfig_init(rb_kobj
);
175 rb_softconfig_init(rb_kobj
);
180 static void __exit
routerboot_exit(void)
182 rb_softconfig_exit();
183 rb_hardconfig_exit();
184 kobject_put(rb_kobj
); // recursive afaict
187 /* Common routines */
189 ssize_t
routerboot_tag_show_string(const u8
*pld
, u16 pld_len
, char *buf
)
191 return scnprintf(buf
, pld_len
+1, "%s\n", pld
);
194 module_init(routerboot_init
);
195 module_exit(routerboot_exit
);
197 MODULE_LICENSE("GPL v2");
198 MODULE_DESCRIPTION("MikroTik RouterBoot sysfs support");
199 MODULE_AUTHOR("Thibaut VARENE");