-
Notifications
You must be signed in to change notification settings - Fork 303
/
lk2nd-device.c
392 lines (317 loc) · 9.66 KB
/
lk2nd-device.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
// SPDX-License-Identifier: GPL-2.0-only
#include <arch/arm.h>
#include <board.h>
#include <debug.h>
#include <libfdt.h>
#include <stdlib.h>
#include <string.h>
#include <lk2nd.h>
struct lk2nd_device lk2nd_dev = {0};
extern struct board_data board;
static void dump_board()
{
unsigned i;
dprintf(INFO, "Board: platform: %u, foundry: %#x, platform_version: %#x, "
"platform_hw: %#x, platform_subtype: %#x, target: %#x, "
"baseband: %#x, platform_hlos_subtype: %#x\n",
board.platform, board.foundry_id, board.platform_version,
board.platform_hw, board.platform_subtype, board.target,
board.baseband, board.platform_hlos_subtype);
for (i = 0; i < MAX_PMIC_DEVICES; ++i) {
if (board.pmic_info[i].pmic_type == PMIC_IS_INVALID)
continue;
dprintf(INFO, "pmic_info[%u]: type: %#x, version: %#x, target: %#x\n",
i, board.pmic_info[i].pmic_type,
board.pmic_info[i].pmic_version,
board.pmic_info[i].pmic_target);
}
}
static void update_board_id(struct board_id *board_id)
{
uint32_t hw_id = board_id->variant_id & 0xff;
uint32_t hw_subtype = board_id->platform_subtype & 0xff;
uint32_t hlos_subtype = board_id->platform_subtype & 0x700;
uint32_t target_id = board_id->variant_id & 0xffff00;
/* Some Huawei devices seem to use hw_id > 0xff, match entire variant */
if (board_hardware_id() > 0xff)
hw_id = board_id->variant_id;
/* See platform_dt_absolute_match() for the checks made here */
if (board_hardware_id() != hw_id) {
dprintf(INFO, "Updating board hardware id: 0x%x -> 0x%x\n",
board_hardware_id(), hw_id);
board.platform_hw = hw_id;
}
if (board_hardware_subtype() != hw_subtype) {
dprintf(INFO, "Updating board hardware subtype: 0x%x -> 0x%x\n",
board_hardware_subtype(), hw_subtype);
board.platform_subtype = hw_subtype;
}
if ((board_hlos_subtype() & 0x700) != hlos_subtype) {
hlos_subtype |= board_hlos_subtype() & ~0x700;
dprintf(INFO, "Updating board hlos subtype: 0x%x -> 0x%x\n",
board_hlos_subtype(), hlos_subtype);
board.platform_hlos_subtype = hlos_subtype;
}
if (!(target_id <= (board_target_id() & 0xffff00))) {
target_id |= board_target_id() & ~0xffff00;
dprintf(INFO, "Updating board target id: 0x%x -> 0x%x\n",
board_target_id(), target_id);
board.target = target_id;
}
}
static const char *strpresuf(const char *str, const char *pre)
{
int len = strlen(pre);
return strncmp(pre, str, len) == 0 ? str + len : NULL;
}
static inline void parse_arg(const char *str, const char *pre, const char **out)
{
const char *val = strpresuf(str, pre);
if (val)
*out = strdup(val);
}
static const char *parse_panel(const char *panel)
{
const char *panel_name;
char *end;
if (!panel)
return NULL;
/*
* Clean up a bit when arg looks like 1:dsi:0:<panel-name>:...
* It could look different, but I don't know how to handle that yet.
*/
panel_name = strpresuf(panel, "1:dsi:0:");
if (!panel_name) /* Some other format */
return NULL;
/* Cut off other garbage at the end of the string (e.g. :1:none) */
end = strchr(panel_name, ':');
if (end)
*end = 0;
/* If this isn't the main panel we don't really know how to deal with this */
if (strcmp(panel_name, "none") == 0)
return NULL;
return panel_name;
}
static void parse_boot_args(void)
{
char *saveptr;
char *args = strdup(lk2nd_dev.cmdline);
char *arg = strtok_r(args, " ", &saveptr);
while (arg) {
const char *aboot = strpresuf(arg, "androidboot.");
if (aboot) {
parse_arg(aboot, "device=", &lk2nd_dev.device);
parse_arg(aboot, "bootloader=", &lk2nd_dev.bootloader);
parse_arg(aboot, "serialno=", &lk2nd_dev.serialno);
parse_arg(aboot, "carrier=", &lk2nd_dev.carrier);
parse_arg(aboot, "radio=", &lk2nd_dev.radio);
} else {
parse_arg(arg, "mdss_mdp.panel=", &lk2nd_dev.panel.name);
}
arg = strtok_r(NULL, " ", &saveptr);
}
free(args);
lk2nd_dev.panel.name = parse_panel(lk2nd_dev.panel.name);
}
static const char *fdt_copyprop_str(const void *fdt, int offset, const char *prop)
{
int len;
const char *val;
char *result = NULL;
val = fdt_getprop(fdt, offset, prop, &len);
if (val && len > 0) {
result = malloc(len);
ASSERT(result);
strlcpy(result, val, len);
}
return result;
}
static bool match_string(const char *s, const char *match, size_t len)
{
len = strnlen(match, len);
if (len > 0) {
if (match[len-1] == '*') {
if (len > 1 && match[0] == '*')
// *contains*
return !!strstrl(s, match + 1, len - 2);
// prefix* (starts with)
len -= 2; // Don't match null terminator and '*'
} else if (match[0] == '*') {
// *suffix (ends with)
size_t slen = strlen(s);
if (slen < --len)
return false;
++match; // Skip '*'
s += slen - len; // Move to end of string
}
}
return strncmp(s, match, len + 1) == 0;
}
static bool match_panel(const void *fdt, int offset, const char *panel_name)
{
offset = fdt_subnode_offset(fdt, offset, "panel");
if (offset < 0) {
dprintf(CRITICAL, "No panels defined, cannot match\n");
return false;
}
return fdt_subnode_offset(fdt, offset, panel_name) >= 0;
}
static bool lk2nd_device_match(const void *fdt, int offset)
{
int len;
const char *val;
val = fdt_getprop(fdt, offset, "lk2nd,match-bootloader", &len);
if (val && len > 0) {
if (!lk2nd_dev.bootloader)
return false;
return match_string(lk2nd_dev.bootloader, val, len);
}
val = fdt_getprop(fdt, offset, "lk2nd,match-cmdline", &len);
if (val && len > 0) {
if (!lk2nd_dev.cmdline)
return false;
return match_string(lk2nd_dev.cmdline, val, len);
}
fdt_getprop(fdt, offset, "lk2nd,match-panel", &len);
if (len >= 0) {
if (!lk2nd_dev.panel.name)
return false;
return match_panel(fdt, offset, lk2nd_dev.panel.name);
}
return true; // No match property
}
static int lk2nd_find_device_offset(const void *fdt)
{
int offset;
offset = fdt_node_offset_by_compatible(fdt, -1, "lk2nd,device");
while (offset >= 0) {
if (lk2nd_device_match(fdt, offset))
return offset;
offset = fdt_node_offset_by_compatible(fdt, offset, "lk2nd,device");
}
return offset;
}
static const char *fdt_getprop_str(const void *fdt, int offset, const char *prop, int *len)
{
const char *val;
val = fdt_getprop(fdt, offset, prop, len);
if (!val || *len < 1)
return NULL;
*len = strnlen(val, *len);
return val;
}
static void lk2nd_parse_panels(const void *fdt, int offset)
{
struct lk2nd_panel *panel = &lk2nd_dev.panel;
const char *old, *new;
int old_len, new_len;
offset = fdt_subnode_offset(fdt, offset, "panel");
if (offset < 0)
return;
old = fdt_getprop_str(fdt, offset, "compatible", &old_len);
if (!old || old_len < 1)
return;
offset = fdt_subnode_offset(fdt, offset, panel->name);
if (offset < 0) {
dprintf(CRITICAL, "Unsupported panel: %s\n", panel->name);
return;
}
new = fdt_getprop_str(fdt, offset, "compatible", &new_len);
if (!new || new_len < 1)
return;
/* Include space required for null-terminators */
panel->compatible_size = ++new_len + ++old_len;
panel->compatible = malloc(panel->compatible_size);
ASSERT(panel->compatible);
panel->old_compatible = panel->compatible + new_len;
strlcpy((char*) panel->compatible, new, new_len);
strlcpy((char*) panel->old_compatible, old, old_len);
}
static void lk2nd_parse_device_node(const void *fdt)
{
int offset = lk2nd_find_device_offset(fdt);
if (offset < 0) {
dprintf(CRITICAL, "Failed to find matching lk2nd,device node: %d\n", offset);
return;
}
#ifdef GPIO_I2C_BUS_COUNT
lk2nd_samsung_muic_reset(fdt, offset);
#endif
lk2nd_motorola_smem_write_unit_info(fdt, offset);
lk2nd_dev.model = fdt_copyprop_str(fdt, offset, "model");
if (lk2nd_dev.model)
dprintf(INFO, "Device model: %s\n", lk2nd_dev.model);
else
dprintf(CRITICAL, "Device node is missing 'model' property\n");
if (dev_tree_get_board_id(fdt, offset, &lk2nd_dev.board_id) == 0)
update_board_id(&lk2nd_dev.board_id);
if (lk2nd_dev.panel.name)
lk2nd_parse_panels(fdt, offset);
}
int lk2nd_fdt_parse_early_uart(void)
{
int offset, len;
const uint32_t *val;
void *fdt = (void*) lk_boot_args[2];
if (!fdt || dev_tree_check_header(fdt))
return -1; // Will be reported later again. Hopefully.
offset = lk2nd_find_device_offset(fdt);
if (offset < 0)
return -1;
/* TODO: Change this to use chosen node */
val = fdt_getprop(fdt, offset, "lk2nd,uart", &len);
if (len > 0)
return fdt32_to_cpu(*val);
return -1;
}
static void lk2nd_fdt_parse(void)
{
void *fdt = (void*) lk_boot_args[2];
if (!fdt)
return;
if (dev_tree_check_header(fdt)) {
dprintf(INFO, "Invalid device tree provided by primary bootloader\n");
return;
}
lk2nd_dev.fdt = fdt;
if (dev_tree_get_board_id(fdt, 0, &lk2nd_dev.board_id) == 0)
update_board_id(&lk2nd_dev.board_id);
else
dprintf(INFO, "No valid (or multiple) qcom,board-id in device tree\n");
lk2nd_dev.cmdline = dev_tree_get_boot_args(fdt);
if (lk2nd_dev.cmdline) {
dprintf(INFO, "Command line from primary bootloader: ");
dputs(INFO, lk2nd_dev.cmdline);
dputc(INFO, '\n');
parse_boot_args();
}
lk2nd_parse_device_node(fdt);
}
void lk2nd_init(void)
{
dump_board();
lk2nd_fdt_parse();
}
static void lk2nd_update_panel_compatible(void *fdt)
{
struct lk2nd_panel *panel = &lk2nd_dev.panel;
int offset, ret;
/* Try to find panel node */
offset = fdt_node_offset_by_compatible(fdt, -1, panel->old_compatible);
if (offset < 0) {
dprintf(CRITICAL, "Failed to find panel node with compatible: %s\n",
panel->old_compatible);
return;
}
ret = fdt_setprop(fdt, offset, "compatible", panel->compatible, panel->compatible_size);
if (ret)
dprintf(CRITICAL, "Failed to update panel compatible: %d\n", ret);
}
void lk2nd_update_device_tree(void *fdt, const char *cmdline)
{
/* Don't touch lk2nd/downstream dtb */
if (strstr(cmdline, "lk2nd"))
return;
if (lk2nd_dev.panel.compatible)
lk2nd_update_panel_compatible(fdt);
}