-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.c
620 lines (525 loc) · 19 KB
/
main.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
613
614
615
616
617
618
619
620
/*
* ShellSecBat plugin
* Copyright (c) 2017 OperationNT
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <psp2/kernel/modulemgr.h>
#include <psp2/kernel/processmgr.h>
#include <psp2/kernel/clib.h>
#include <psp2/kernel/sysmem.h>
#include <psp2/rtc.h>
#include <psp2/power.h>
#include <psp2/io/fcntl.h>
#include <taihen.h>
#include <psp2/registrymgr.h>
#include <psp2/system_param.h>
#include <psp2/ctrl.h>
#include <psp2/io/devctl.h>
static char *devices[] = {
"imc0:",
"ur0:",
"ux0:",
"uma0:",
"xmc0:",
"grw0:"
};
#define N_DEVICES (sizeof(devices) / sizeof(char **))
// Configuration format
// Features:[Drives display][Battery display]
// Time:[Seconds display][Date display][Year display][Date separator]
// Drives:[Skip unmounted][Free space display][Space decimal separator][imc0:][ur0:][ux0:][uma0:][xmc0:][grw0:]
// LeftKey:[Keys combination for previous drive display]
// RightKey:[Keys combination for next drive display]
// File content for default settings
// Features:11 Time:100/ Drives:11.111111 LeftKey:101 RightKey:201
// File content for ShellSecBat V4 behavior
// Features:11 Time:100/ Drives:01.011100 LeftKey:101 RightKey:201
// File content for ShellDateSecBat V4 behavior
// Features:11 Time:110/ Drives:01.011100 LeftKey:101 RightKey:201
static char *configPathes[] = {
"ux0:/data/ShellSecBat.txt",
"ux0:/tai/ShellSecBat.txt",
"ur0:/tai/ShellSecBat.txt",
"ur0:/plugins/ShellSecBat.txt"
};
#define N_PATHES (sizeof(configPathes) / sizeof(char **))
static int displayDrives = 1;
static int displayBattery = 1;
static int displaySeconds = 1;
static int displayDate = 0;
static int displayYear = 0;
static char dateSeparator = '/';
static int skipUnmounted = 1;
static int displayFree = 1;
static char spaceSeparator = '.';
static int displayedDrives[N_DEVICES] = {1,1,1,1,1,1};
static unsigned int driveKeys[2] = {SCE_CTRL_SELECT|SCE_CTRL_LTRIGGER, SCE_CTRL_SELECT|SCE_CTRL_RTRIGGER};
static char* seekChar(char* iStr, char iChar)
{
while (*iStr != iChar && *iStr != '\0')
iStr++;
return (*iStr != '\0') ? iStr : NULL;
}
static char* seekDoubleChar(char* iStr, char iChar1, char iChar2)
{
while ((iStr[0] != iChar1 || iStr[1] != iChar2) && *iStr != '\0')
iStr++;
return (*iStr != '\0') ? iStr : NULL;
}
static void getHexValue(char* iStr, unsigned int* oValue)
{
char* str = iStr;
while (((*str >= '0' && *str <= '9') || (*str >= 'a' && *str <= 'f') || (*str >= 'A' && *str <= 'F')) && str-iStr < 8)
str++;
char size = str-iStr;
if (size <= 0 || (*str != ' ' && *str != '\t' && *str != '\r' && *str != '\n' && *str != '\0'))
return;
*oValue = 0;
unsigned int factor = 1;
for (str -= 1; str >= iStr; str--, factor *= 16)
{
if (*str >= 'a' && *str <= 'f')
*str -= ('a' - 'A');
if (*str >= 'A' && *str <= 'F')
*oValue += (10 + *str - 'A') * factor;
else
*oValue += (*str - '0') * factor;
}
}
static char *units[] = {
"B ",
"KB",
"MB",
"GB",
"TB",
"PB"
};
#define N_UNITS (sizeof(units) / sizeof(char **))
static int unitType(SceOff iBytes, int* oConv, int* oCent)
{
SceOff convBytes = iBytes;
SceOff divider = 1;
int res = 0;
while (convBytes > 1023 && res < N_UNITS-1)
{
divider *= 1024;
convBytes /= 1024;
res++;
}
*oConv = (int)convBytes;
*oCent = (int)((iBytes%divider)/(divider/102));
return res;
}
static int displayed_disk = N_DEVICES;
/*
offset 0x1844f0:
int status_draw_battery_patched(int a1, uint8_t a2);
*/
static SceUID g_hooks[2];
uint32_t text_addr, text_size, data_addr, data_size;
// Functions
/**
* ScePafWidget_39B15B98
*
* @param widget Widget ptr
* @param size Font size (22.0 - standard font size in statusbar)
* @param unk0 Unknown, pass 1
* @param pos Start of part of text to set size
* @param len Length of part of text to set size from pos
*/
static int (*scePafWidgetSetFontSize)(void *widget, float size, int unk0, int pos, int len);
static void get_functions_retail_355()
{
scePafWidgetSetFontSize = (void*) text_addr + 0x45c950;
}
static void get_functions_retail_360()
{
scePafWidgetSetFontSize = (void*) text_addr + 0x45ce80;
}
static void get_functions_retail_365_368()
{
scePafWidgetSetFontSize = (void*) text_addr + 0x45D2C8;
}
static void get_functions_testkit_360()
{
scePafWidgetSetFontSize = (void*) text_addr + 0x453038;
}
static void get_functions_testkit_365()
{
scePafWidgetSetFontSize = (void*) text_addr + 0x453478;
}
static void get_functions_devkit_360()
{
scePafWidgetSetFontSize = (void*) text_addr + 0x44E5F8;
}
static void get_functions_devkit_365()
{
scePafWidgetSetFontSize = (void*) text_addr + 0x44EA68;
}
static int digit_len(int num)
{
if (num < 10) {
return 1;
} else if (num < 100) {
return 2;
} else {
return 3;
}
}
typedef struct {
int start;
int length;
float height;
} TextMod;
#define N_FONT_CHANGE 3
static TextMod fontChange[N_FONT_CHANGE] = { {-1, 2, 16.0} , {-1, 0, 20.0} , {-1, 1, 16.0} };
static int in_draw_time = 0;
static tai_hook_ref_t ref_hook0;
static int status_draw_time_patched(void *a1, int a2)
{
in_draw_time = 1;
int out = TAI_CONTINUE(int, ref_hook0, a1, a2);
in_draw_time = 0;
if (a1) {
for (int i = 0; i < N_FONT_CHANGE; i++)
{
TextMod* curFont = &fontChange[i];
if (curFont->start != -1)
scePafWidgetSetFontSize(a1, curFont->height, 1, curFont->start, curFont->length);
}
}
return out;
}
static tai_hook_ref_t ref_hook1;
static uint16_t **some_strdup_patched(uint16_t **a1, uint16_t *a2, int a2_size)
{
if (in_draw_time)
{
char buff[32];
int len;
int i;
if (displayDrives)
{
// Here, buttons state is checked every 1.001 seconds.
SceCtrlData pad;
sceCtrlPeekBufferPositive(0, &pad, 1);
int nextDrive = ((pad.buttons & driveKeys[1]) == driveKeys[1]) ? 1 : 0;
int prevDrive = ((pad.buttons & driveKeys[0]) == driveKeys[0]) ? 1 : 0;
SceIoDevInfo info;
int res = -1;
if (1 == nextDrive + prevDrive) // XOR on drive display commands
{
while (res < 0)
{
do
{
displayed_disk = (nextDrive ? (displayed_disk+1) : (displayed_disk+N_DEVICES))%(N_DEVICES+1);
}
while (displayed_disk < N_DEVICES && !displayedDrives[displayed_disk]);
if (displayed_disk == N_DEVICES)
break;
res = sceIoDevctl(devices[displayed_disk], 0x3001, NULL, 0, &info, sizeof(SceIoDevInfo));
if (!skipUnmounted)
break;
}
}
else if (displayed_disk < N_DEVICES)
res = sceIoDevctl(devices[displayed_disk], 0x3001, NULL, 0, &info, sizeof(SceIoDevInfo));
if (displayed_disk < N_DEVICES)
{
if (res >= 0)
{
int freeConv = 0;
int freeDec = 0;
int freeUnit = unitType(displayFree ? info.free_size : (info.max_size-info.free_size), &freeConv, &freeDec);
int maxConv = 0;
int maxDec = 0;
int maxUnit = unitType(info.max_size, &maxConv, &maxDec);
if (freeDec > 0)
{
len = sceClibSnprintf(buff, 32, "%s %d%c%02d%s/%d%c%02d%s", devices[displayed_disk],
freeConv, spaceSeparator, freeDec, units[freeUnit],
maxConv, spaceSeparator, maxDec, units[maxUnit]);
}
else
{
len = sceClibSnprintf(buff, 32, "%s %d%s/%d%c%02d%s", devices[displayed_disk],
freeConv, units[freeUnit],
maxConv, spaceSeparator, maxDec, units[maxUnit]);
}
i = 6;
while (i < len && buff[i] != '/') i++;
fontChange[0].start = i-2;
fontChange[0].length = 2;
fontChange[2].start = len-2;
fontChange[2].length = 2;
}
else
{
// The 2 spaces at string end avoid a font size issue.
len = sceClibSnprintf(buff, 32, "%s Not mounted ", devices[displayed_disk]);
fontChange[0].start = -1;
fontChange[2].start = -1;
}
for (i = 0; i < len; ++i) {
a2[i] = buff[i];
}
a2[len] = 0;
fontChange[1].start = 0;
fontChange[1].length = 4;
return TAI_CONTINUE(uint16_t**, ref_hook1, a1, a2, len);
}
}
SceDateTime time_local;
SceDateTime time_utc;
sceRtcGetCurrentClock(&time_utc, 0);
SceRtcTick tick;
sceRtcGetTick(&time_utc, &tick);
sceRtcConvertUtcToLocalTime(&tick, &tick);
sceRtcSetTick(&time_local, &tick);
if (displayDate)
{
int date_format = SCE_SYSTEM_PARAM_DATE_FORMAT_DDMMYYYY;
sceRegMgrGetKeyInt("/CONFIG/DATE", "date_format", &date_format);
if (displayYear)
{
if (SCE_SYSTEM_PARAM_DATE_FORMAT_DDMMYYYY == date_format)
len = sceClibSnprintf(buff, 32, "%02d%c%02d%c%04d ", time_local.day, dateSeparator, time_local.month, dateSeparator, time_local.year);
else if (SCE_SYSTEM_PARAM_DATE_FORMAT_YYYYMMDD == date_format)
len = sceClibSnprintf(buff, 32, "%04d%c%02d%c%02d ", time_local.year, dateSeparator, time_local.month, dateSeparator, time_local.day);
else
len = sceClibSnprintf(buff, 32, "%02d%c%02d%c%04d ", time_local.month, dateSeparator, time_local.day, dateSeparator, time_local.year);
}
else
{
if (SCE_SYSTEM_PARAM_DATE_FORMAT_DDMMYYYY == date_format)
len = sceClibSnprintf(buff, 32, "%02d%c%02d ", time_local.day, dateSeparator, time_local.month);
else
len = sceClibSnprintf(buff, 32, "%02d%c%02d ", time_local.month, dateSeparator, time_local.day);
}
uint16_t tmp[16];
for (i = 0; i < a2_size; ++i) {
tmp[i] = a2[i];
}
for (i = 0; i < len; ++i) {
a2[i] = buff[i];
}
for (i = 0; i < a2_size; ++i) {
a2[len+i] = tmp[i];
}
a2_size += len;
}
int is_ampm = (a2[a2_size - 1] == 'M');
if (displaySeconds)
{
len = sceClibSnprintf(buff, 32, ":%02d", time_local.second);
if (is_ampm)
{
for (i = 0; i < 3; ++i) {
a2[a2_size + len - 3 + i] = a2[a2_size - 3 + i];
}
for (i = 0; i < len; ++i) {
a2[a2_size - 3 + i] = buff[i];
}
}
else
{
for (i = 0; i < len; ++i) {
a2[a2_size + i] = buff[i];
}
}
a2_size += len;
}
if (displayBattery)
{
static int oldpercent = 0;
int percent = scePowerGetBatteryLifePercent();
if (percent < 0 || percent > 100) {
percent = oldpercent;
}
oldpercent = percent;
len = sceClibSnprintf(buff, 32, " %d%%", percent);
for (i = 0; i < len; ++i) {
a2[a2_size + i] = buff[i];
}
fontChange[0].start = is_ampm ? (a2_size - 2) : -1;
fontChange[1].start = a2_size + 2;
fontChange[1].length = digit_len(percent);
fontChange[2].start = fontChange[1].start + fontChange[1].length;
fontChange[2].length = 1;
a2_size += len;
}
else
{
fontChange[0].start = is_ampm ? (a2_size - 2) : -1;
fontChange[1].start = -1;
fontChange[2].start = -1;
}
a2[a2_size] = 0;
return TAI_CONTINUE(uint16_t**, ref_hook1, a1, a2, a2_size);
}
return TAI_CONTINUE(uint16_t**, ref_hook1, a1, a2, a2_size);
}
void _start() __attribute__ ((weak, alias ("module_start")));
int module_start(SceSize argc, const void *args)
{
tai_module_info_t info;
info.size = sizeof(info);
int ret = taiGetModuleInfo("SceShell", &info);
if (ret < 0) {
return SCE_KERNEL_START_FAILED;
}
// Modified from TheOfficialFloW/Adrenaline
SceKernelModuleInfo mod_info;
mod_info.size = sizeof(SceKernelModuleInfo);
ret = sceKernelGetModuleInfo(info.modid, &mod_info);
if (ret < 0) {
return SCE_KERNEL_START_FAILED;
}
text_addr = (uint32_t) mod_info.segments[0].vaddr;
text_size = (uint32_t) mod_info.segments[0].memsz;
data_addr = (uint32_t) mod_info.segments[1].vaddr;
data_size = (uint32_t) mod_info.segments[1].memsz;
uint32_t offsets[2];
switch (info.module_nid) {
case 0x8978D25D: // retail 3.55 SceShell
offsets[0] = 0x183EF0;
offsets[1] = 0x40DBD8;
get_functions_retail_355();
break;
case 0x0552F692: // retail 3.60 SceShell
offsets[0] = 0x183ea4;
offsets[1] = 0x40e0b4;
get_functions_retail_360();
break;
case 0x5549BF1F: // retail 3.65 SceShell
case 0x34B4D82E: // retail 3.67 SceShell
case 0x12DAC0F3: // retail 3.68 SceShell
offsets[0] = 0x183F6C;
offsets[1] = 0x40E4FC;
get_functions_retail_365_368();
break;
case 0xEAB89D5C: // PTEL 3.60 SceShell
offsets[0] = 0x17C2D8;
offsets[1] = 0x404828;
get_functions_testkit_360();
break;
case 0x587F9CED: // PTEL 3.65 SceShell
offsets[0] = 0x17C3A0;
offsets[1] = 0x404C68;
get_functions_testkit_365();
break;
case 0x6CB01295: // PDEL 3.60 SceShell
offsets[0] = 0x17B8DC;
offsets[1] = 0x400028;
get_functions_devkit_360();
break;
case 0xE6A02F2B: // PDEL 3.65 SceShell
offsets[0] = 0x17B9A4;
offsets[1] = 0x400498;
get_functions_devkit_365();
break;
default:
return SCE_KERNEL_START_FAILED;
}
g_hooks[0] = taiHookFunctionOffset(&ref_hook0,
info.modid,
0, // segidx
offsets[0], // offset
1, // thumb
status_draw_time_patched);
g_hooks[1] = taiHookFunctionOffset(&ref_hook1,
info.modid,
0, // segidx
offsets[1], // offset
1, // thumb
some_strdup_patched);
// Setup configuration from file
displayBattery = (sceKernelGetModel() != SCE_KERNEL_MODEL_VITATV);
SceUID fd = -1;
int i = 0;
int fileSize = 0;
while (i < N_PATHES && fd < 0)
{
fd = sceIoOpen(configPathes[i], SCE_O_RDONLY, 0666);
if (fd >= 0)
{
fileSize = sceIoLseek(fd, 0, SCE_SEEK_END);
if (fileSize > 116)
{
sceIoClose(fd);
fd = -1;
}
}
i++;
}
if (fd >= 0)
{
char configStr[128];
sceIoLseek(fd, 0, SCE_SEEK_SET);
sceIoRead(fd, configStr, fileSize);
sceIoClose(fd);
configStr[fileSize] = '\0';
sceClibMemset(&(configStr[fileSize+1]), ' ', 11);
// Look for features
char* seeked = configStr;
if ((seeked = seekDoubleChar(seeked, 'F', 'e')) && (seeked = seekChar(seeked, ':')))
{
displayDrives = ('1' == seeked[1]);
displayBattery = (displayBattery && '1' == seeked[2]);
}
seeked = configStr;
if ((seeked = seekDoubleChar(seeked, 'T', 'i')) && (seeked = seekChar(seeked, ':')))
{
displaySeconds = ('1' == seeked[1]);
displayDate = ('1' == seeked[2]);
if (displayDate)
{
displayYear = ('1' == seeked[3]);
if ('\0' != seeked[4])
dateSeparator = seeked[4];
}
}
if (displayDrives)
{
seeked = configStr;
if ((seeked = seekDoubleChar(seeked, 'D', 'r')) && (seeked = seekChar(seeked, ':')))
{
skipUnmounted = ('1' == seeked[1]);
displayFree = ('1' == seeked[2]);
if ('\0' != seeked[3])
spaceSeparator = seeked[3];
for (i = 0; i < N_DEVICES; i++)
displayedDrives[i] = ('1' == seeked[4+i]);
}
seeked = configStr;
if ((seeked = seekDoubleChar(seeked, 'L', 'e')) && (seeked = seekChar(seeked, ':')))
getHexValue(&seeked[1], &driveKeys[0]);
seeked = configStr;
if ((seeked = seekDoubleChar(seeked, 'R', 'i')) && (seeked = seekChar(seeked, ':')))
getHexValue(&seeked[1], &driveKeys[1]);
}
}
return SCE_KERNEL_START_SUCCESS;
}
int module_stop(SceSize argc, const void *args)
{
if (g_hooks[0] >= 0) taiHookRelease(g_hooks[0], ref_hook0);
if (g_hooks[1] >= 0) taiHookRelease(g_hooks[1], ref_hook1);
return SCE_KERNEL_STOP_SUCCESS;
}