Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
CTCaer
GitHub Repository: CTCaer/hekate
Path: blob/master/bdk/utils/util.c
1476 views
1
/*
2
* Copyright (c) 2018 naehrwert
3
* Copyright (c) 2018-2025 CTCaer
4
*
5
* This program is free software; you can redistribute it and/or modify it
6
* under the terms and conditions of the GNU General Public License,
7
* version 2, as published by the Free Software Foundation.
8
*
9
* This program is distributed in the hope it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12
* more details.
13
*
14
* You should have received a copy of the GNU General Public License
15
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16
*/
17
18
#include <string.h>
19
20
#include <mem/heap.h>
21
#include <power/max77620.h>
22
#include <rtc/max77620-rtc.h>
23
#include <soc/bpmp.h>
24
#include <soc/hw_init.h>
25
#include <soc/i2c.h>
26
#include <soc/pmc.h>
27
#include <soc/timer.h>
28
#include <soc/t210.h>
29
#include <storage/sd.h>
30
#include <utils/util.h>
31
32
u8 bit_count(u32 val)
33
{
34
u8 cnt = 0;
35
for (u32 i = 0; i < 32; i++)
36
{
37
if ((val >> i) & 1)
38
cnt++;
39
}
40
41
return cnt;
42
}
43
44
u32 bit_count_mask(u8 bits)
45
{
46
u32 val = 0;
47
for (u32 i = 0; i < bits; i++)
48
val |= 1 << i;
49
50
return val;
51
}
52
53
char *strcpy_ns(char *dst, char *src)
54
{
55
if (!src || !dst)
56
return NULL;
57
58
// Remove starting space.
59
u32 len = strlen(src);
60
if (len && src[0] == ' ')
61
{
62
len--;
63
src++;
64
}
65
66
strcpy(dst, src);
67
68
// Remove trailing space.
69
if (len && dst[len - 1] == ' ')
70
dst[len - 1] = 0;
71
72
return dst;
73
}
74
75
// Approximate square root finder for a 64-bit number.
76
u64 sqrt64(u64 num)
77
{
78
u64 base = 0;
79
u64 limit = num;
80
u64 square_root = 0;
81
82
while (base <= limit)
83
{
84
u64 tmp_sqrt = (base + limit) / 2;
85
86
if (tmp_sqrt * tmp_sqrt == num) {
87
square_root = tmp_sqrt;
88
break;
89
}
90
91
if (tmp_sqrt * tmp_sqrt < num)
92
{
93
square_root = base;
94
base = tmp_sqrt + 1;
95
}
96
else
97
limit = tmp_sqrt - 1;
98
}
99
100
return square_root;
101
}
102
103
#define TULONG_MAX ((unsigned long)((unsigned long)(~0L)))
104
#define TLONG_MAX ((long)(((unsigned long)(~0L)) >> 1))
105
#define TLONG_MIN ((long)(~TLONG_MAX))
106
#define ISSPACE(ch) ((ch >= '\t' && ch <= '\r') || (ch == ' '))
107
#define ISDIGIT(ch) ( ch >= '0' && ch <= '9' )
108
#define ISALPHA(ch) ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
109
#define ISUPPER(ch) ( ch >= 'A' && ch <= 'Z' )
110
111
/*
112
* Avoid using reentrant newlib version of strol. It's only used for errno.
113
*
114
* strol/atoi:
115
* Copyright (c) 1990 The Regents of the University of California.
116
*/
117
long strtol(const char *nptr, char **endptr, register int base)
118
{
119
register const char *s = nptr;
120
register unsigned long acc;
121
register int c;
122
register unsigned long cutoff;
123
register int neg = 0, any, cutlim;
124
125
/*
126
* Skip white space and pick up leading +/- sign if any.
127
* If base is 0, allow 0x for hex and 0 for octal, else
128
* assume decimal; if base is already 16, allow 0x.
129
*/
130
do {
131
c = *s++;
132
} while (ISSPACE(c));
133
if (c == '-') {
134
neg = 1;
135
c = *s++;
136
} else if (c == '+')
137
c = *s++;
138
if ((base == 0 || base == 16) &&
139
c == '0' && (*s == 'x' || *s == 'X')) {
140
c = s[1];
141
s += 2;
142
base = 16;
143
}
144
if (base == 0)
145
base = c == '0' ? 8 : 10;
146
147
/*
148
* Compute the cutoff value between legal numbers and illegal
149
* numbers. That is the largest legal value, divided by the
150
* base. An input number that is greater than this value, if
151
* followed by a legal input character, is too big. One that
152
* is equal to this value may be valid or not; the limit
153
* between valid and invalid numbers is then based on the last
154
* digit. For instance, if the range for longs is
155
* [-2147483648..2147483647] and the input base is 10,
156
* cutoff will be set to 214748364 and cutlim to either
157
* 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated
158
* a value > 214748364, or equal but the next digit is > 7 (or 8),
159
* the number is too big, and we will return a range error.
160
*
161
* Set any if any `digits' consumed; make it negative to indicate
162
* overflow.
163
*/
164
cutoff = neg ? -(unsigned long)TLONG_MIN : (base == 16 ? TULONG_MAX : TLONG_MAX);
165
cutlim = cutoff % (unsigned long)base;
166
cutoff /= (unsigned long)base;
167
for (acc = 0, any = 0;; c = *s++) {
168
if (ISDIGIT(c))
169
c -= '0';
170
else if (ISALPHA(c))
171
c -= ISUPPER(c) ? 'A' - 10 : 'a' - 10;
172
else
173
break;
174
if (c >= base)
175
break;
176
if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
177
any = -1;
178
else {
179
any = 1;
180
acc *= base;
181
acc += c;
182
}
183
}
184
if (any < 0) {
185
acc = neg ? TLONG_MIN : TLONG_MAX;
186
} else if (neg)
187
acc = -acc;
188
if (endptr != 0)
189
*endptr = (char *) (any ? s - 1 : nptr);
190
return (acc);
191
}
192
193
int atoi(const char *nptr)
194
{
195
return (int)strtol(nptr, (char **)NULL, 10);
196
}
197
198
void reg_write_array(u32 *base, const reg_cfg_t *cfg, u32 num_cfg)
199
{
200
// Expected register offset is a u32 array index.
201
for (u32 i = 0; i < num_cfg; i++)
202
base[cfg[i].idx] = cfg[i].val;
203
}
204
205
u32 crc32_calc(u32 crc, const u8 *buf, u32 len)
206
{
207
const u8 *p, *q;
208
static u32 *table = NULL;
209
210
// Calculate CRC table.
211
if (!table)
212
{
213
table = zalloc(256 * sizeof(u32));
214
for (u32 i = 0; i < 256; i++)
215
{
216
u32 rem = i;
217
for (u32 j = 0; j < 8; j++)
218
{
219
if (rem & 1)
220
{
221
rem >>= 1;
222
rem ^= 0xedb88320;
223
}
224
else
225
rem >>= 1;
226
}
227
table[i] = rem;
228
}
229
}
230
231
crc = ~crc;
232
q = buf + len;
233
for (p = buf; p < q; p++)
234
{
235
u8 oct = *p;
236
crc = (crc >> 8) ^ table[(crc & 0xff) ^ oct];
237
}
238
239
return ~crc;
240
}
241
242
int qsort_compare_int(const void *a, const void *b)
243
{
244
return (*(int *)a - *(int *)b);
245
}
246
247
int qsort_compare_char(const void *a, const void *b)
248
{
249
return strcmp(*(const char **)a, *(const char **)b);
250
}
251
252
int qsort_compare_char_case(const void *a, const void *b)
253
{
254
return strcasecmp(*(const char **)a, *(const char **)b);
255
}
256
257
void panic(u32 val)
258
{
259
// Set panic code.
260
PMC(APBDEV_PMC_SCRATCH200) = val;
261
262
// Disable SE.
263
//PMC(APBDEV_PMC_CRYPTO_OP) = PMC_CRYPTO_OP_SE_DISABLE;
264
265
// Immediately cause a full system reset.
266
watchdog_start(0, TIMER_PMCRESET_EN);
267
268
while (true);
269
}
270
271
void power_set_state(power_state_t state)
272
{
273
u8 reg;
274
275
// Unmount and power down sd card.
276
sd_end();
277
278
// De-initialize and power down various hardware.
279
hw_deinit(false, 0);
280
281
// Set power state.
282
switch (state)
283
{
284
case REBOOT_RCM:
285
PMC(APBDEV_PMC_SCRATCH0) = PMC_SCRATCH0_MODE_RCM; // Enable RCM path.
286
PMC(APBDEV_PMC_CNTRL) |= PMC_CNTRL_MAIN_RST; // PMC reset.
287
break;
288
289
case REBOOT_BYPASS_FUSES:
290
panic(0x21); // Bypass fuse programming in package1.
291
break;
292
293
case POWER_OFF:
294
// Initiate power down sequence and do not generate a reset (regulators retain state after POR).
295
i2c_send_byte(I2C_5, MAX77620_I2C_ADDR, MAX77620_REG_ONOFFCNFG1, MAX77620_ONOFFCNFG1_PWR_OFF);
296
break;
297
298
case POWER_OFF_RESET:
299
case POWER_OFF_REBOOT:
300
default:
301
// Enable/Disable soft reset wake event.
302
reg = i2c_recv_byte(I2C_5, MAX77620_I2C_ADDR, MAX77620_REG_ONOFFCNFG2);
303
if (state == POWER_OFF_RESET) // Do not wake up after power off.
304
reg &= ~(MAX77620_ONOFFCNFG2_SFT_RST_WK | MAX77620_ONOFFCNFG2_WK_ALARM1 | MAX77620_ONOFFCNFG2_WK_ALARM2);
305
else // POWER_OFF_REBOOT. Wake up after power off.
306
reg |= MAX77620_ONOFFCNFG2_SFT_RST_WK;
307
i2c_send_byte(I2C_5, MAX77620_I2C_ADDR, MAX77620_REG_ONOFFCNFG2, reg);
308
309
// Initiate power down sequence and generate a reset (regulators' state resets after POR).
310
i2c_send_byte(I2C_5, MAX77620_I2C_ADDR, MAX77620_REG_ONOFFCNFG1, MAX77620_ONOFFCNFG1_SFT_RST);
311
break;
312
}
313
314
while (true)
315
bpmp_halt();
316
}
317
318
void power_set_state_ex(void *param)
319
{
320
power_state_t *state = (power_state_t *)param;
321
power_set_state(*state);
322
}
323
324