Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
CTCaer
GitHub Repository: CTCaer/hekate
Path: blob/master/bootloader/frontend/fe_info.c
3693 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 <bdk.h>
21
22
#include "fe_info.h"
23
#include "../config.h"
24
#include "../hos/hos.h"
25
#include "../hos/pkg1.h"
26
#include <libs/fatfs/ff.h>
27
28
#pragma GCC push_options
29
#pragma GCC optimize ("Os")
30
31
void print_fuseinfo()
32
{
33
u32 fuse_size = h_cfg.t210b01 ? 0x368 : 0x300;
34
u32 fuse_address = h_cfg.t210b01 ? 0x7000F898 : 0x7000F900;
35
36
gfx_clear_partial_grey(0x1B, 0, 1256);
37
gfx_con_setpos(0, 0);
38
39
gfx_printf("\nSKU: %X - ", FUSE(FUSE_SKU_INFO));
40
switch (h_cfg.devmode)
41
{
42
case FUSE_NX_HW_STATE_PROD:
43
gfx_printf("Retail\n");
44
break;
45
case FUSE_NX_HW_STATE_DEV:
46
gfx_printf("Dev\n");
47
break;
48
}
49
gfx_printf("Sdram ID: %d\n", fuse_read_dramid(true));
50
gfx_printf("Burnt fuses: %d / 64\n", bit_count(fuse_read_odm(7)));
51
gfx_printf("Secure key: %08X%08X%08X%08X\n\n\n",
52
byte_swap_32(FUSE(FUSE_PRIVATE_KEY0)), byte_swap_32(FUSE(FUSE_PRIVATE_KEY1)),
53
byte_swap_32(FUSE(FUSE_PRIVATE_KEY2)), byte_swap_32(FUSE(FUSE_PRIVATE_KEY3)));
54
55
gfx_printf("%kFuse cache:\n\n%k", TXT_CLR_CYAN_L, TXT_CLR_DEFAULT);
56
gfx_hexdump(fuse_address, (u8 *)fuse_address, fuse_size);
57
58
btn_wait();
59
}
60
61
void print_mmc_info()
62
{
63
gfx_clear_partial_grey(0x1B, 0, 1256);
64
gfx_con_setpos(0, 0);
65
66
static const u32 SECTORS_TO_MIB_COEFF = 11;
67
68
if (emmc_initialize(false))
69
{
70
EPRINTF("Failed to init eMMC.");
71
goto out;
72
}
73
else
74
{
75
u16 card_type;
76
u32 speed = 0;
77
78
gfx_printf("%kCID:%k\n", TXT_CLR_CYAN_L, TXT_CLR_DEFAULT);
79
switch (emmc_storage.csd.mmca_vsn)
80
{
81
case 2: /* MMC v2.0 - v2.2 */
82
case 3: /* MMC v3.1 - v3.3 */
83
case 4: /* MMC v4 */
84
gfx_printf(
85
" Vendor ID: %X\n"
86
" OEM ID: %02X\n"
87
" Model: %c%c%c%c%c%c\n"
88
" Prd Rev: %X\n"
89
" S/N: %04X\n"
90
" Month/Year: %02d/%04d\n\n",
91
emmc_storage.cid.manfid, emmc_storage.cid.oemid,
92
emmc_storage.cid.prod_name[0], emmc_storage.cid.prod_name[1], emmc_storage.cid.prod_name[2],
93
emmc_storage.cid.prod_name[3], emmc_storage.cid.prod_name[4], emmc_storage.cid.prod_name[5],
94
emmc_storage.cid.prv, emmc_storage.cid.serial, emmc_storage.cid.month, emmc_storage.cid.year);
95
break;
96
default:
97
break;
98
}
99
100
if (emmc_storage.csd.structure == 0)
101
EPRINTF("Unknown CSD structure.");
102
else
103
{
104
gfx_printf("%kExtended CSD V1.%d:%k\n",
105
TXT_CLR_CYAN_L, emmc_storage.ext_csd.ext_struct, TXT_CLR_DEFAULT);
106
card_type = emmc_storage.ext_csd.card_type;
107
char card_type_support[96];
108
card_type_support[0] = 0;
109
if (card_type & EXT_CSD_CARD_TYPE_HS_26)
110
{
111
strcat(card_type_support, "HS26");
112
speed = (26 << 16) | 26;
113
}
114
if (card_type & EXT_CSD_CARD_TYPE_HS_52)
115
{
116
strcat(card_type_support, ", HS52");
117
speed = (52 << 16) | 52;
118
}
119
if (card_type & EXT_CSD_CARD_TYPE_DDR_1_8V)
120
{
121
strcat(card_type_support, ", DDR52_1.8V");
122
speed = (52 << 16) | 104;
123
}
124
if (card_type & EXT_CSD_CARD_TYPE_HS200_1_8V)
125
{
126
strcat(card_type_support, ", HS200_1.8V");
127
speed = (200 << 16) | 200;
128
}
129
if (card_type & EXT_CSD_CARD_TYPE_HS400_1_8V)
130
{
131
strcat(card_type_support, ", HS400_1.8V");
132
speed = (200 << 16) | 400;
133
}
134
135
gfx_printf(
136
" Spec Version: %02X\n"
137
" Extended Rev: 1.%d\n"
138
" Dev Version: %d\n"
139
" Cmd Classes: %02X\n"
140
" Capacity: %s\n"
141
" Max Rate: %d MB/s (%d MHz)\n"
142
" Current Rate: %d MB/s\n"
143
" Type Support: ",
144
emmc_storage.csd.mmca_vsn, emmc_storage.ext_csd.rev, emmc_storage.ext_csd.dev_version, emmc_storage.csd.cmdclass,
145
emmc_storage.csd.capacity == (4096 * EMMC_BLOCKSIZE) ? "High" : "Low", speed & 0xFFFF, (speed >> 16) & 0xFFFF,
146
emmc_storage.csd.busspeed);
147
gfx_con.fntsz = 8;
148
gfx_printf("%s", card_type_support);
149
gfx_con.fntsz = 16;
150
gfx_printf("\n\n", card_type_support);
151
152
u32 boot_size = emmc_storage.ext_csd.boot_mult << 17;
153
u32 rpmb_size = emmc_storage.ext_csd.rpmb_mult << 17;
154
gfx_printf("%keMMC Partitions:%k\n", TXT_CLR_CYAN_L, TXT_CLR_DEFAULT);
155
gfx_printf(" 1: %kBOOT0 %k\n Size: %5d KiB (LBA Sectors: 0x%07X)\n", TXT_CLR_GREENISH, TXT_CLR_DEFAULT,
156
boot_size / 1024, boot_size / EMMC_BLOCKSIZE);
157
gfx_put_small_sep();
158
gfx_printf(" 2: %kBOOT1 %k\n Size: %5d KiB (LBA Sectors: 0x%07X)\n", TXT_CLR_GREENISH, TXT_CLR_DEFAULT,
159
boot_size / 1024, boot_size / EMMC_BLOCKSIZE);
160
gfx_put_small_sep();
161
gfx_printf(" 3: %kRPMB %k\n Size: %5d KiB (LBA Sectors: 0x%07X)\n", TXT_CLR_GREENISH, TXT_CLR_DEFAULT,
162
rpmb_size / 1024, rpmb_size / EMMC_BLOCKSIZE);
163
gfx_put_small_sep();
164
gfx_printf(" 0: %kGPP (USER) %k\n Size: %5d MiB (LBA Sectors: 0x%07X)\n\n", TXT_CLR_GREENISH, TXT_CLR_DEFAULT,
165
emmc_storage.sec_cnt >> SECTORS_TO_MIB_COEFF, emmc_storage.sec_cnt);
166
gfx_put_small_sep();
167
gfx_printf("%kGPP (eMMC USER) partition table:%k\n", TXT_CLR_CYAN_L, TXT_CLR_DEFAULT);
168
169
emmc_set_partition(EMMC_GPP);
170
LIST_INIT(gpt);
171
emmc_gpt_parse(&gpt);
172
int gpp_idx = 0;
173
LIST_FOREACH_ENTRY(emmc_part_t, part, &gpt, link)
174
{
175
gfx_printf(" %02d: %k%s%k\n Size: % 5d MiB (LBA Sectors 0x%07X)\n LBA Range: %08X-%08X\n",
176
gpp_idx++, TXT_CLR_GREENISH, part->name, TXT_CLR_DEFAULT, (part->lba_end - part->lba_start + 1) >> SECTORS_TO_MIB_COEFF,
177
part->lba_end - part->lba_start + 1, part->lba_start, part->lba_end);
178
gfx_put_small_sep();
179
}
180
emmc_gpt_free(&gpt);
181
}
182
}
183
184
out:
185
emmc_end();
186
187
btn_wait();
188
}
189
190
void print_sdcard_info()
191
{
192
static const u32 SECTORS_TO_MIB_COEFF = 11;
193
194
gfx_clear_partial_grey(0x1B, 0, 1256);
195
gfx_con_setpos(0, 0);
196
197
if (!sd_initialize(false))
198
{
199
gfx_printf("%kCard IDentification:%k\n", TXT_CLR_CYAN_L, TXT_CLR_DEFAULT);
200
gfx_printf(
201
" Vendor ID: %02x\n"
202
" OEM ID: %c%c\n"
203
" Model: %c%c%c%c%c\n"
204
" HW rev: %X\n"
205
" FW rev: %X\n"
206
" S/N: %08x\n"
207
" Month/Year: %02d/%04d\n\n",
208
sd_storage.cid.manfid, (sd_storage.cid.oemid >> 8) & 0xFF, sd_storage.cid.oemid & 0xFF,
209
sd_storage.cid.prod_name[0], sd_storage.cid.prod_name[1], sd_storage.cid.prod_name[2],
210
sd_storage.cid.prod_name[3], sd_storage.cid.prod_name[4],
211
sd_storage.cid.hwrev, sd_storage.cid.fwrev, sd_storage.cid.serial,
212
sd_storage.cid.month, sd_storage.cid.year);
213
214
u16 *sd_errors = sd_get_error_count();
215
gfx_printf("%kCard-Specific Data V%d.0:%k\n", TXT_CLR_CYAN_L, sd_storage.csd.structure + 1, TXT_CLR_DEFAULT);
216
gfx_printf(
217
" Cmd Classes: %02X\n"
218
" Capacity: %d MiB\n"
219
" Bus Width: %d\n"
220
" Current Rate: %d MB/s (%d MHz)\n"
221
" Speed Class: %d\n"
222
" UHS Grade: U%d\n"
223
" Video Class: V%d\n"
224
" App perf class: A%d\n"
225
" Write Protect: %d\n"
226
" SDMMC Errors: %d %d %d\n\n",
227
sd_storage.csd.cmdclass, sd_storage.sec_cnt >> 11,
228
sd_storage.ssr.bus_width, sd_storage.csd.busspeed, sd_storage.csd.busspeed * 2,
229
sd_storage.ssr.speed_class, sd_storage.ssr.uhs_grade, sd_storage.ssr.video_class,
230
sd_storage.ssr.app_class, sd_storage.csd.write_protect,
231
sd_errors[0], sd_errors[1], sd_errors[2]); // SD_ERROR_INIT_FAIL, SD_ERROR_RW_FAIL, SD_ERROR_RW_RETRY.
232
233
int res = f_mount(&sd_fs, "", 1);
234
if (!res)
235
{
236
gfx_puts("Acquiring FAT volume info...\n\n");
237
gfx_printf("%kFound %s volume:%k\n Free: %d MiB\n Cluster: %d KiB\n",
238
TXT_CLR_CYAN_L, sd_fs.fs_type == FS_EXFAT ? "exFAT" : "FAT32", TXT_CLR_DEFAULT,
239
sd_fs.free_clst * sd_fs.csize >> SECTORS_TO_MIB_COEFF, (sd_fs.csize > 1) ? (sd_fs.csize >> 1) : 512);
240
f_unmount("");
241
}
242
else
243
{
244
EPRINTFARGS("Failed to mount SD card (FatFS Error %d).\n"
245
"Make sure that a FAT partition exists..", res);
246
}
247
248
sd_end();
249
}
250
else
251
{
252
EPRINTF("Failed to init SD card.");
253
if (!sdmmc_get_sd_inserted())
254
EPRINTF("Make sure that it is inserted.");
255
else
256
EPRINTF("SD Card Reader is not properly seated!");
257
sd_end();
258
}
259
260
btn_wait();
261
}
262
263
void print_fuel_gauge_info()
264
{
265
int value = 0;
266
267
gfx_printf("%kFuel Gauge Info:\n%k", TXT_CLR_CYAN_L, TXT_CLR_DEFAULT);
268
269
max17050_get_property(MAX17050_RepSOC, &value);
270
gfx_printf("Capacity now: %3d%\n", value >> 8);
271
272
max17050_get_property(MAX17050_RepCap, &value);
273
gfx_printf("Capacity now: %4d mAh\n", value);
274
275
max17050_get_property(MAX17050_FullCAP, &value);
276
gfx_printf("Capacity full: %4d mAh\n", value);
277
278
max17050_get_property(MAX17050_DesignCap, &value);
279
gfx_printf("Capacity (design): %4d mAh\n", value);
280
281
max17050_get_property(MAX17050_Current, &value);
282
gfx_printf("Current now: %d mA\n", value / 1000);
283
284
max17050_get_property(MAX17050_AvgCurrent, &value);
285
gfx_printf("Current average: %d mA\n", value / 1000);
286
287
max17050_get_property(MAX17050_VCELL, &value);
288
gfx_printf("Voltage now: %4d mV\n", value);
289
290
max17050_get_property(MAX17050_OCVInternal, &value);
291
gfx_printf("Voltage open-circuit: %4d mV\n", value);
292
293
max17050_get_property(MAX17050_MinVolt, &value);
294
gfx_printf("Min voltage reached: %4d mV\n", value);
295
296
max17050_get_property(MAX17050_MaxVolt, &value);
297
gfx_printf("Max voltage reached: %4d mV\n", value);
298
299
max17050_get_property(MAX17050_V_empty, &value);
300
gfx_printf("Empty voltage (design): %4d mV\n", value);
301
302
max17050_get_property(MAX17050_TEMP, &value);
303
gfx_printf("Battery temperature: %d.%d oC\n", value / 10,
304
(value >= 0 ? value : (~value)) % 10);
305
}
306
307
void print_battery_charger_info()
308
{
309
int value = 0;
310
311
gfx_printf("%k\n\nBattery Charger Info:\n%k", TXT_CLR_CYAN_L, TXT_CLR_DEFAULT);
312
313
bq24193_get_property(BQ24193_InputCurrentLimit, &value);
314
gfx_printf("Input current limit: %4d mA\n", value);
315
316
bq24193_get_property(BQ24193_SystemMinimumVoltage, &value);
317
gfx_printf("System voltage limit: %4d mV\n", value);
318
319
bq24193_get_property(BQ24193_FastChargeCurrentLimit, &value);
320
gfx_printf("Charge current limit: %4d mA\n", value);
321
322
bq24193_get_property(BQ24193_ChargeVoltageLimit, &value);
323
gfx_printf("Charge voltage limit: %4d mV\n", value);
324
325
bq24193_get_property(BQ24193_ChargeStatus, &value);
326
gfx_printf("Charge status: ");
327
switch (value)
328
{
329
case 0:
330
gfx_printf("Not charging\n");
331
break;
332
case 1:
333
gfx_printf("Pre-charging\n");
334
break;
335
case 2:
336
gfx_printf("Fast charging\n");
337
break;
338
case 3:
339
gfx_printf("Charge terminated\n");
340
break;
341
default:
342
gfx_printf("Unknown (%d)\n", value);
343
break;
344
}
345
bq24193_get_property(BQ24193_TempStatus, &value);
346
gfx_printf("Temperature status: ");
347
switch (value)
348
{
349
case 0:
350
gfx_printf("Normal\n");
351
break;
352
case 2:
353
gfx_printf("Warm\n");
354
break;
355
case 3:
356
gfx_printf("Cool\n");
357
break;
358
case 5:
359
gfx_printf("Cold\n");
360
break;
361
case 6:
362
gfx_printf("Hot\n");
363
break;
364
default:
365
gfx_printf("Unknown (%d)\n", value);
366
break;
367
}
368
}
369
370
void print_battery_info()
371
{
372
gfx_clear_partial_grey(0x1B, 0, 1256);
373
gfx_con_setpos(0, 0);
374
375
print_fuel_gauge_info();
376
377
print_battery_charger_info();
378
379
u8 *buf = (u8 *)malloc(0x100 * 2);
380
381
gfx_printf("%k\n\nBattery Fuel Gauge Registers:\n%k", TXT_CLR_CYAN_L, TXT_CLR_DEFAULT);
382
383
for (int i = 0; i < 0x200; i += 2)
384
{
385
i2c_recv_buf_small(buf + i, 2, I2C_1, MAXIM17050_I2C_ADDR, i >> 1);
386
usleep(2500);
387
}
388
389
gfx_hexdump(0, (u8 *)buf, 0x200);
390
391
btn_wait();
392
}
393
394
#pragma GCC pop_options
395
396