generic: platform/mikrotik: disambiguate SPDX-License-Identifier
[openwrt/openwrt.git] / target / linux / generic / files / drivers / platform / mikrotik / routerboot.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Driver for MikroTik RouterBoot flash data. Common routines.
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
12 #include <linux/types.h>
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/sysfs.h>
16
17 #include "routerboot.h"
18
19 static struct kobject *rb_kobj;
20
21 /**
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
28 *
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.
38 *
39 * Return: 0 on success (tag found) or errno
40 */
41 int routerboot_tag_find(const u8 *bufhead, const size_t buflen, const u16 tag_id,
42 u16 *pld_ofs, u16 *pld_len)
43 {
44 const u32 *datum, *bufend;
45 u32 node;
46 u16 id, len;
47 int ret;
48
49 if (!bufhead || !tag_id)
50 return -EINVAL;
51
52 ret = -ENOENT;
53 datum = (const u32 *)bufhead;
54 bufend = (const u32 *)(bufhead + buflen);
55
56 while (datum < bufend) {
57 node = *datum++;
58
59 /* Tag list ends with null node */
60 if (!node)
61 break;
62
63 id = node & 0xFFFF;
64 len = node >> 16;
65
66 if (tag_id == id) {
67 if (datum >= bufend)
68 break;
69
70 if (pld_ofs)
71 *pld_ofs = (u16)((u8 *)datum - bufhead);
72 if (pld_len)
73 *pld_len = len;
74
75 ret = 0;
76 break;
77 }
78
79 /*
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
84 * performance path.
85 */
86 len = ALIGN(len, sizeof(*datum));
87 datum += len / sizeof(*datum);
88 }
89
90 return ret;
91 }
92
93 /**
94 * routerboot_rle_decode() - Simple RLE (MikroTik variant) decoding routine.
95 * @in: input buffer to decode
96 * @inlen: size of in
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
100 *
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.
105 *
106 * NB: The output buffer cannot overlap with the input buffer.
107 *
108 * Return: 0 on success or errno
109 */
110 int routerboot_rle_decode(const u8 *in, size_t inlen, u8 *out, size_t *outlen)
111 {
112 int ret, run, nbytes; // use native types for speed
113 u8 byte;
114
115 if (!in || (inlen < 2) || !out)
116 return -EINVAL;
117
118 ret = -ENOSPC;
119 nbytes = 0;
120 while (inlen >= 2) {
121 run = *in++;
122 inlen--;
123
124 /* Verbatim copies */
125 if (run & 0x80) {
126 /* Invert run byte sign */
127 run = ~run & 0xFF;
128 run++;
129
130 if (run > inlen)
131 goto fail;
132
133 inlen -= run;
134
135 nbytes += run;
136 if (nbytes > *outlen)
137 goto fail;
138
139 /* Basic memcpy */
140 while (run-- > 0)
141 *out++ = *in++;
142 }
143 /* Stream of half-words RLE: <run><byte>. run == 0 is ignored */
144 else {
145 byte = *in++;
146 inlen--;
147
148 nbytes += run;
149 if (nbytes > *outlen)
150 goto fail;
151
152 while (run-- > 0)
153 *out++ = byte;
154 }
155 }
156
157 ret = 0;
158 fail:
159 *outlen = nbytes;
160 return ret;
161 }
162
163 static int __init routerboot_init(void)
164 {
165 rb_kobj = kobject_create_and_add("mikrotik", firmware_kobj);
166 if (!rb_kobj)
167 return -ENOMEM;
168
169 return rb_hardconfig_init(rb_kobj);
170 }
171
172 static void __exit routerboot_exit(void)
173 {
174 rb_hardconfig_exit();
175 kobject_put(rb_kobj); // recursive afaict
176 }
177
178 module_init(routerboot_init);
179 module_exit(routerboot_exit);
180
181 MODULE_LICENSE("GPL v2");
182 MODULE_DESCRIPTION("MikroTik RouterBoot sysfs support");
183 MODULE_AUTHOR("Thibaut VARENE");