forked from fagci/uv-k5-firmware-fagci-mod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuart.c
612 lines (514 loc) · 13.3 KB
/
uart.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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
/* Copyright 2023 Dual Tachyon
* https://github.com/DualTachyon
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <string.h>
#if !defined(ENABLE_OVERLAY)
#include "ARMCM0.h"
#endif
#if defined(ENABLE_FMRADIO)
#include "app/fm.h"
#endif
#include "app/uart.h"
#include "board.h"
#include "bsp/dp32g030/dma.h"
#include "bsp/dp32g030/gpio.h"
#include "driver/aes.h"
#include "driver/bk4819.h"
#include "driver/crc.h"
#include "driver/eeprom.h"
#include "driver/gpio.h"
#include "driver/uart.h"
#include "functions.h"
#include "misc.h"
#include "settings.h"
#if defined(ENABLE_OVERLAY)
#include "sram-overlay.h"
#endif
#include "version.h"
#ifdef ENABLE_UART_CAT
#include "../driver/system.h"
#include "external/printf/printf.h"
#endif
#define DMA_INDEX(x, y) (((x) + (y)) % sizeof(UART_DMA_Buffer))
typedef struct {
uint16_t ID;
uint16_t Size;
} Header_t;
typedef struct {
uint8_t Padding[2];
uint16_t ID;
} Footer_t;
typedef struct {
Header_t Header;
uint32_t Timestamp;
} CMD_0514_t;
typedef struct {
Header_t Header;
struct {
char Version[16];
bool bHasCustomAesKey;
bool bIsInLockScreen;
uint8_t Padding[2];
uint32_t Challenge[4];
} Data;
} REPLY_0514_t;
typedef struct {
Header_t Header;
uint16_t Offset;
uint8_t Size;
uint8_t Padding;
uint32_t Timestamp;
} CMD_051B_t;
typedef struct {
Header_t Header;
struct {
uint16_t Offset;
uint8_t Size;
uint8_t Padding;
uint8_t Data[128];
} Data;
} REPLY_051B_t;
typedef struct {
Header_t Header;
uint16_t Offset;
uint8_t Size;
bool bAllowPassword;
uint32_t Timestamp;
uint8_t Data[0];
} CMD_051D_t;
typedef struct {
Header_t Header;
struct {
uint16_t Offset;
} Data;
} REPLY_051D_t;
typedef struct {
Header_t Header;
struct {
uint16_t RSSI;
uint8_t ExNoiseIndicator;
uint8_t GlitchIndicator;
} Data;
} REPLY_0527_t;
typedef struct {
Header_t Header;
struct {
uint16_t Voltage;
uint16_t Current;
} Data;
} REPLY_0529_t;
typedef struct {
Header_t Header;
uint32_t Response[4];
} CMD_052D_t;
typedef struct {
Header_t Header;
struct {
bool bIsLocked;
uint8_t Padding[3];
} Data;
} REPLY_052D_t;
typedef struct {
Header_t Header;
uint32_t Timestamp;
} CMD_052F_t;
typedef struct {
Header_t Header;
uint8_t RegNum;
} CMD_0601_t;
typedef struct {
Header_t Header;
struct {
uint16_t Val;
uint8_t v1;
uint8_t v2;
} Data;
} REPLY_0601_t;
typedef struct {
Header_t Header;
uint8_t RegNum;
uint16_t RegValue;
} CMD_0602_t;
typedef struct {
Header_t Header;
struct {
uint16_t Val;
uint8_t v1;
uint8_t v2;
} Data;
} REPLY_0602_t;
static const uint8_t Obfuscation[16] = {0x16, 0x6C, 0x14, 0xE6, 0x2E, 0x91,
0x0D, 0x40, 0x21, 0x35, 0xD5, 0x40,
0x13, 0x03, 0xE9, 0x80};
static union {
uint8_t Buffer[256];
struct {
Header_t Header;
uint8_t Data[252];
};
} UART_Command;
static uint32_t Timestamp;
static uint16_t gUART_WriteIndex;
static bool bIsEncrypted = true;
static void SendReply(void *pReply, uint16_t Size) {
Header_t Header;
Footer_t Footer;
uint8_t *pBytes;
uint16_t i;
if (bIsEncrypted) {
pBytes = (uint8_t *)pReply;
for (i = 0; i < Size; i++) {
pBytes[i] ^= Obfuscation[i % 16];
}
}
Header.ID = 0xCDAB;
Header.Size = Size;
UART_Send(&Header, sizeof(Header));
UART_Send(pReply, Size);
if (bIsEncrypted) {
Footer.Padding[0] = Obfuscation[(Size + 0) % 16] ^ 0xFF;
Footer.Padding[1] = Obfuscation[(Size + 1) % 16] ^ 0xFF;
} else {
Footer.Padding[0] = 0xFF;
Footer.Padding[1] = 0xFF;
}
Footer.ID = 0xBADC;
UART_Send(&Footer, sizeof(Footer));
}
static void SendVersion(void) {
REPLY_0514_t Reply;
Reply.Header.ID = 0x0515;
Reply.Header.Size = sizeof(Reply.Data);
strcpy(Reply.Data.Version, Version);
Reply.Data.bHasCustomAesKey = bHasCustomAesKey;
Reply.Data.bIsInLockScreen = bIsInLockScreen;
Reply.Data.Challenge[0] = gChallenge[0];
Reply.Data.Challenge[1] = gChallenge[1];
Reply.Data.Challenge[2] = gChallenge[2];
Reply.Data.Challenge[3] = gChallenge[3];
SendReply(&Reply, sizeof(Reply));
}
static bool IsBadChallenge(const uint32_t *pKey, const uint32_t *pIn,
const uint32_t *pResponse) {
uint8_t i;
uint32_t IV[4];
IV[0] = 0;
IV[1] = 0;
IV[2] = 0;
IV[3] = 0;
AES_Encrypt(pKey, IV, pIn, IV, true);
for (i = 0; i < 4; i++) {
if (IV[i] != pResponse[i]) {
return true;
}
}
return false;
}
static void CMD_0514(const uint8_t *pBuffer) {
const CMD_0514_t *pCmd = (const CMD_0514_t *)pBuffer;
Timestamp = pCmd->Timestamp;
#if defined(ENABLE_FMRADIO)
gFmRadioCountdown = 4;
#endif
GPIO_ClearBit(&GPIOB->DATA, GPIOB_PIN_BACKLIGHT);
SendVersion();
}
static void CMD_051B(const uint8_t *pBuffer) {
const CMD_051B_t *pCmd = (const CMD_051B_t *)pBuffer;
REPLY_051B_t Reply;
bool bLocked = false;
if (pCmd->Timestamp != Timestamp) {
return;
}
#if defined(ENABLE_FMRADIO)
gFmRadioCountdown = 4;
#endif
memset(&Reply, 0, sizeof(Reply));
Reply.Header.ID = 0x051C;
Reply.Header.Size = pCmd->Size + 4;
Reply.Data.Offset = pCmd->Offset;
Reply.Data.Size = pCmd->Size;
if (bHasCustomAesKey) {
bLocked = gIsLocked;
}
if (!bLocked) {
EEPROM_ReadBuffer(pCmd->Offset, Reply.Data.Data, pCmd->Size);
}
SendReply(&Reply, pCmd->Size + 8);
}
static void CMD_051D(const uint8_t *pBuffer) {
const CMD_051D_t *pCmd = (const CMD_051D_t *)pBuffer;
REPLY_051D_t Reply;
bool bReloadEeprom;
bool bIsLocked;
if (pCmd->Timestamp != Timestamp) {
return;
}
bReloadEeprom = false;
#if defined(ENABLE_FMRADIO)
gFmRadioCountdown = 4;
#endif
Reply.Header.ID = 0x051E;
Reply.Header.Size = sizeof(Reply.Data);
Reply.Data.Offset = pCmd->Offset;
bIsLocked = bHasCustomAesKey;
if (bHasCustomAesKey) {
bIsLocked = gIsLocked;
}
if (!bIsLocked) {
uint16_t i;
for (i = 0; i < (pCmd->Size / 8U); i++) {
uint16_t Offset = pCmd->Offset + (i * 8U);
if (Offset >= 0x0F30 && Offset < 0x0F40) {
if (!gIsLocked) {
bReloadEeprom = true;
}
}
if ((Offset < 0x0E98 || Offset >= 0x0EA0) || !bIsInLockScreen ||
pCmd->bAllowPassword) {
EEPROM_WriteBuffer(Offset, &pCmd->Data[i * 8U]);
}
}
if (bReloadEeprom) {
BOARD_EEPROM_Init();
}
}
SendReply(&Reply, sizeof(Reply));
}
static void CMD_0527(void) {
REPLY_0527_t Reply;
Reply.Header.ID = 0x0528;
Reply.Header.Size = sizeof(Reply.Data);
Reply.Data.RSSI = BK4819_ReadRegister(BK4819_REG_67) & 0x01FF;
Reply.Data.ExNoiseIndicator = BK4819_ReadRegister(BK4819_REG_65) & 0x007F;
Reply.Data.GlitchIndicator = BK4819_ReadRegister(BK4819_REG_63);
SendReply(&Reply, sizeof(Reply));
}
static void CMD_0529(void) {
REPLY_0529_t Reply;
Reply.Header.ID = 0x52A;
Reply.Header.Size = sizeof(Reply.Data);
// Original doesn't actually send current!
BOARD_ADC_GetBatteryInfo(&Reply.Data.Voltage, &Reply.Data.Current);
SendReply(&Reply, sizeof(Reply));
}
static void CMD_052D(const uint8_t *pBuffer) {
const CMD_052D_t *pCmd = (const CMD_052D_t *)pBuffer;
REPLY_052D_t Reply;
bool bIsLocked;
#if defined(ENABLE_FMRADIO)
gFmRadioCountdown = 4;
#endif
Reply.Header.ID = 0x052E;
Reply.Header.Size = sizeof(Reply.Data);
bIsLocked = bHasCustomAesKey;
if (!bIsLocked) {
bIsLocked = IsBadChallenge(gCustomAesKey, gChallenge, pCmd->Response);
}
if (!bIsLocked) {
bIsLocked = IsBadChallenge(gDefaultAesKey, gChallenge, pCmd->Response);
if (bIsLocked) {
gTryCount++;
}
}
if (gTryCount < 3) {
if (!bIsLocked) {
gTryCount = 0;
}
} else {
gTryCount = 3;
bIsLocked = true;
}
gIsLocked = bIsLocked;
Reply.Data.bIsLocked = bIsLocked;
SendReply(&Reply, sizeof(Reply));
}
static void CMD_052F(const uint8_t *pBuffer) {
const CMD_052F_t *pCmd = (const CMD_052F_t *)pBuffer;
gEeprom.DUAL_WATCH = DUAL_WATCH_OFF;
gEeprom.CROSS_BAND_RX_TX = CROSS_BAND_OFF;
gEeprom.RX_CHANNEL = 0;
gEeprom.DTMF_SIDE_TONE = false;
gEeprom.VfoInfo[0].FrequencyReverse = false;
gEeprom.VfoInfo[0].pRX = &gEeprom.VfoInfo[0].ConfigRX;
gEeprom.VfoInfo[0].pTX = &gEeprom.VfoInfo[0].ConfigTX;
gEeprom.VfoInfo[0].FREQUENCY_DEVIATION_SETTING = FREQUENCY_DEVIATION_OFF;
gEeprom.VfoInfo[0].DTMF_PTT_ID_TX_MODE = PTT_ID_OFF;
gEeprom.VfoInfo[0].DTMF_DECODING_ENABLE = false;
if (gCurrentFunction == FUNCTION_POWER_SAVE) {
FUNCTION_Select(FUNCTION_FOREGROUND);
}
Timestamp = pCmd->Timestamp;
GPIO_ClearBit(&GPIOB->DATA, GPIOB_PIN_BACKLIGHT);
SendVersion();
}
#ifdef ENABLE_UART_CAT
static void CMD_0601(const uint8_t *pBuffer) {
const CMD_0601_t *pCmd = (const CMD_0601_t *)pBuffer;
REPLY_0601_t Reply;
Reply.Header.ID = 0x0601;
Reply.Header.Size = sizeof(Reply.Data);
Reply.Data.Val = BK4819_ReadRegister(pCmd->RegNum);
Reply.Data.v1 = pCmd->RegNum;
SendReply(&Reply, sizeof(Reply));
}
static void CMD_0602(const uint8_t *pBuffer) {
const CMD_0602_t *pCmd = (const CMD_0602_t *)pBuffer;
REPLY_0602_t Reply;
Reply.Header.ID = 0x0602;
Reply.Header.Size = sizeof(Reply.Data);
BK4819_WriteRegister(pCmd->RegNum, pCmd->RegValue);
Reply.Data.Val = BK4819_ReadRegister(pCmd->RegNum);
Reply.Data.v1 = pCmd->RegNum;
SendReply(&Reply, sizeof(Reply));
}
#endif
uint64_t xtou64(const char *str) {
uint64_t res = 0;
char c;
while ((c = *str++)) {
char v = ((c & 0xF) + (c >> 6)) | ((c >> 3) & 0x8);
res = (res << 4) | (uint64_t)v;
}
return res;
}
bool UART_IsCommandAvailable(void) {
uint16_t DmaLength;
uint16_t CommandLength;
uint16_t Index;
uint16_t TailIndex;
uint16_t Size;
uint16_t CRC;
uint16_t i;
DmaLength = DMA_CH0->ST & 0xFFFU;
while (1) {
if (gUART_WriteIndex == DmaLength) {
return false;
}
while (gUART_WriteIndex != DmaLength &&
UART_DMA_Buffer[gUART_WriteIndex] != 0xABU) {
gUART_WriteIndex = DMA_INDEX(gUART_WriteIndex, 1);
}
if (gUART_WriteIndex == DmaLength) {
return false;
}
if (gUART_WriteIndex < DmaLength) {
CommandLength = DmaLength - gUART_WriteIndex;
} else {
CommandLength = (DmaLength + sizeof(UART_DMA_Buffer)) - gUART_WriteIndex;
}
if (CommandLength < 8) {
return 0;
}
if (UART_DMA_Buffer[DMA_INDEX(gUART_WriteIndex, 1)] == 0xCD) {
break;
}
gUART_WriteIndex = DMA_INDEX(gUART_WriteIndex, 1);
}
Index = DMA_INDEX(gUART_WriteIndex, 2);
Size = (UART_DMA_Buffer[DMA_INDEX(Index, 1)] << 8) | UART_DMA_Buffer[Index];
if (Size + 8 > sizeof(UART_DMA_Buffer)) {
gUART_WriteIndex = DmaLength;
return false;
}
if (CommandLength < Size + 8) {
return false;
}
Index = DMA_INDEX(Index, 2);
TailIndex = DMA_INDEX(Index, Size + 2);
if (UART_DMA_Buffer[TailIndex] != 0xDC ||
UART_DMA_Buffer[DMA_INDEX(TailIndex, 1)] != 0xBA) {
gUART_WriteIndex = DmaLength;
return false;
}
if (TailIndex < Index) {
uint16_t ChunkSize = sizeof(UART_DMA_Buffer) - Index;
memcpy(UART_Command.Buffer, UART_DMA_Buffer + Index, ChunkSize);
memcpy(UART_Command.Buffer + ChunkSize, UART_DMA_Buffer, TailIndex);
} else {
memcpy(UART_Command.Buffer, UART_DMA_Buffer + Index, TailIndex - Index);
}
TailIndex = DMA_INDEX(TailIndex, 2);
if (TailIndex < gUART_WriteIndex) {
memset(UART_DMA_Buffer + gUART_WriteIndex, 0,
sizeof(UART_DMA_Buffer) - gUART_WriteIndex);
memset(UART_DMA_Buffer, 0, TailIndex);
} else {
memset(UART_DMA_Buffer + gUART_WriteIndex, 0, TailIndex - gUART_WriteIndex);
}
gUART_WriteIndex = TailIndex;
if (UART_Command.Header.ID == 0x0514) {
bIsEncrypted = false;
}
if (UART_Command.Header.ID == 0x6902) {
bIsEncrypted = true;
}
if (bIsEncrypted) {
for (i = 0; i < Size + 2; i++) {
UART_Command.Buffer[i] ^= Obfuscation[i % 16];
}
}
CRC = UART_Command.Buffer[Size] | (UART_Command.Buffer[Size + 1] << 8);
if (CRC_Calculate(UART_Command.Buffer, Size) != CRC) {
return false;
}
return true;
}
void UART_HandleCommand(void) {
switch (UART_Command.Header.ID) {
case 0x0514:
CMD_0514(UART_Command.Buffer);
break;
case 0x051B:
CMD_051B(UART_Command.Buffer);
break;
case 0x051D:
CMD_051D(UART_Command.Buffer);
break;
case 0x051F:
// Not implementing non-authentic command
break;
case 0x0521:
// Not implementing non-authentic command
break;
case 0x0527:
CMD_0527();
break;
case 0x0529:
CMD_0529();
break;
case 0x052D:
CMD_052D(UART_Command.Buffer);
break;
case 0x052F:
CMD_052F(UART_Command.Buffer);
break;
case 0x05DD:
#if defined(ENABLE_OVERLAY)
overlay_FLASH_RebootToBootloader();
#else
NVIC_SystemReset();
#endif
break;
#ifdef ENABLE_UART_CAT
case 0x0601:
CMD_0601(UART_Command.Buffer);
break;
case 0x0602:
CMD_0602(UART_Command.Buffer);
break;
#endif
}
}