-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathvalidation.c
More file actions
120 lines (109 loc) · 3.69 KB
/
Copy pathvalidation.c
File metadata and controls
120 lines (109 loc) · 3.69 KB
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
/**
* @file validation.c
* @brief Implementation of validation.h
*
* DAPLink Interface Firmware
* Copyright (c) 2009-2019, ARM Limited, All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*
* 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>
#include "validation.h"
#include "target_config.h"
#include "target_family.h"
#include "target_board.h"
static inline uint32_t test_range(const uint32_t test, const uint32_t min, const uint32_t max)
{
return ((test < min) || (test > max)) ? 0 : 1;
}
uint8_t validate_bin_nvic(const uint8_t *buf)
{
if (g_target_family && g_target_family->validate_bin_nvic) {
return g_target_family && g_target_family->validate_bin_nvic(buf);
} else {
return validate_bin_nvic_base(buf);
}
}
uint8_t validate_bin_nvic_base(const uint8_t *buf)
{
if (g_board_info.target_cfg) {
uint32_t i = 4, nvic_val = 0;
uint8_t in_range = 0;
// test the initial SP value
memcpy(&nvic_val, buf + 0, sizeof(nvic_val));
const region_info_t * region = g_board_info.target_cfg->ram_regions;
for (; region->start != 0 || region->end != 0; ++region) {
if (1 == test_range(nvic_val, region->start, region->end)) {
in_range = 1;
break;
}
}
if (in_range == 0) {
return 0;
}
// Reset_Handler
// NMI_Handler
// HardFault_Handler
for (; i <= 12; i += 4) {
in_range = 0;
memcpy(&nvic_val, buf + i, sizeof(nvic_val));
const region_info_t * region = g_board_info.target_cfg->flash_regions;
for (; region->start != 0 || region->end != 0; ++region) {
if (1 == test_range(nvic_val, region->start, region->end)) {
in_range = 1;
break;
}
}
if (in_range == 0) {
return 0;
}
}
return 1;
} else {
return 0;
}
}
uint8_t validate_hexfile(const uint8_t *buf)
{
if (g_target_family && g_target_family->validate_hexfile) {
return g_target_family->validate_hexfile(buf);
} else {
// look here for known hex records
// add hex identifier b[0] == ':' && b[8] == {'0', '2', '3', '4', '5'}
return ((buf[0] == ':') && ((buf[8] == '0') || (buf[8] == '2') || (buf[8] == '3') || (buf[8] == '4') || (buf[8] == '5'))) ? 1 : 0;
}
}
uint8_t validate_uf2file(const uint8_t *buf)
{
#if 0
if (g_target_family && g_target_family->validate_uf2file) {
return g_target_family->validate_uf2file(buf);
}
#endif
// look here for known uf2 record
// First magic number, UF2 signature
if ((buf[0] != 0x55) || (buf[1] != 0x46) || (buf[2] != 0x32) || (buf[3] != 0x0A)) {
return 0;
}
// Second magic number, 0x9E5D5157
if ((buf[4] != 0x57) || (buf[5] != 0x51) || (buf[6] != 0x5D) || (buf[7] != 0x9E)) {
return 0;
}
// Final magic number, 0x0AB16F30
if ((buf[508] != 0x30) || (buf[509] != 0x6F) || (buf[510] != 0xB1) || (buf[511] != 0x0A)) {
return 0;
}
// TODO validate check sum , assuming 512-byte granularity .
return 1;
}