Skip to content

Commit

Permalink
Add exported syscalls: vitabrightReload(), vitabrightOledGetLevel(), …
Browse files Browse the repository at this point in the history
…vitabrightOledSetLevel(), vitabrightOledGetLut(), vitabrightOledSetLut() (#25)

* Export vitabrightReload()

* Remove unused vitabright.yml file

* Build stubs

* Fix vitabrightReload()

* Add int vitabrightLoadOledLut(unsigned char oledLut[357])

* Add vitabrightGetOledLut

* Fix exported syscalls

* Add vitabrightReload back

* Add vitabrightOledGetLevel/vitabrightOledSetLevel
  • Loading branch information
devnoname120 authored Dec 20, 2020
1 parent 66cd473 commit 6157d40
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 17 deletions.
17 changes: 16 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,22 @@ set_target_properties(vitabright
)

vita_create_self(vitabright.skprx vitabright
CONFIG ${CMAKE_SOURCE_DIR}/module.yml
UNSAFE
CONFIG ${CMAKE_SOURCE_DIR}/vitabright.yml
)

vita_create_stubs(vitabright-stubs vitabright module.yml
KERNEL
)

install(DIRECTORY ${CMAKE_BINARY_DIR}/vitabright-stubs/
DESTINATION lib
FILES_MATCHING PATTERN "*.a"
)

set(PSVITAIP 192.168.1.10)

add_custom_target(send
COMMAND curl --ftp-method nocwd -T vitabright.skprx ftp://${PSVITAIP}:1337/ur0:/tai/vitabright.skprx
DEPENDS vitabright.skprx
)
19 changes: 17 additions & 2 deletions main.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include <psp2kern/io/fcntl.h>
#include <psp2kern/kernel/cpu.h>
#include <psp2kern/kernel/modulemgr.h>
#include <psp2kern/kernel/sysmem.h>
#include <taihen.h>

#include "lcd/hooks.h"
Expand Down Expand Up @@ -37,6 +36,22 @@ int module_start(SceSize argc, const void *args) {
return SCE_KERNEL_START_SUCCESS;
}

// Exported as syscall.
int vitabrightReload() {
int state;
ENTER_SYSCALL(state);

oled_disable_hooks();
lcd_disable_hooks();

oled_enable_hooks();
lcd_enable_hooks();

EXIT_SYSCALL(state);

return 0;
}

int module_stop(SceSize argc, const void *args) {
oled_disable_hooks();
lcd_disable_hooks();
Expand Down
17 changes: 17 additions & 0 deletions module.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
vitabright:
attributes: 0
version:
major: 1
minor: 1
main:
start: module_start
stop: module_stop
modules:
vitabright:
syscall: true
functions:
- vitabrightReload
- vitabrightOledGetLevel
- vitabrightOledSetLevel
- vitabrightOledGetLut
- vitabrightOledSetLut
96 changes: 90 additions & 6 deletions oled/hooks.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
#include "../log.h"
#include "lut.h"
#include "parser.h"
#include <psp2kern/kernel/cpu.h>
#include <psp2kern/kernel/modulemgr.h>
#include <psp2kern/kernel/sysmem.h>
#include <taihen.h>

// Required in order to jump to code that is in thumb mode
#define THUMB_BIT 1

static unsigned char lookupNew[LUT_SIZE] = {0};
unsigned char lookupNew[LUT_SIZE] = {0};
static SceUID lut_inject = -1;
static SceUID oled_set_brightness_hook = -1;

Expand All @@ -20,6 +22,9 @@ static tai_hook_ref_t oled_set_brightness_ref = -1;
int module_get_offset(SceUID pid, SceUID modid, int segidx, size_t offset, uintptr_t *addr);

int ksceKernelSysrootGetSystemSwVersion(void);
int oled_apply_lut();

int isDimmingWorkAround = 1;

int hook_ksceOledSetBrightness(unsigned int brightness) {
// Trying to dim screen after inactivity
Expand All @@ -28,7 +33,7 @@ int hook_ksceOledSetBrightness(unsigned int brightness) {

// HACK: In modified vitabright_lut.txt, it corresponds to the line of screen dimmed after
// inactivity
if (old_level <= 16384) {
if (isDimmingWorkAround && old_level <= 16384) {
// Do nothing because it would increase brightness
return TAI_CONTINUE(int, oled_set_brightness_ref, old_level);
}
Expand All @@ -44,18 +49,22 @@ void oled_enable_hooks() {
return;
}

oled_apply_lut();
}

int oled_apply_lut() {
tai_module_info_t info;
info.size = sizeof(tai_module_info_t);
ret = taiGetModuleInfoForKernel(KERNEL_PID, "SceOled", &info);
int ret = taiGetModuleInfoForKernel(KERNEL_PID, "SceOled", &info);
LOG("[OLED] getmodninfo: 0x%08X\n", ret);
LOG("[OLED] modid: 0x%08X\n", info.modid);

if (ret < 0)
return;
return ret;

if (sizeof(lookupNew) != LUT_SIZE) {
LOG("[OLED] size mismatch! Skipping...\n");
return;
return -1;
}

LOG("[OLED] Size ok, hooking...\n");
Expand All @@ -79,7 +88,7 @@ void oled_enable_hooks() {
}
default: // Not supported
LOG("[OLED] Unsupported OS version: 0x%08X\n", (unsigned int)sw_version);
return;
return -2;
}

LOG("[OLED] OS version: 0x%08X\n, table offset: 0x%08X, ksceOledGetBrightness_addr: 0x%08X, "
Expand Down Expand Up @@ -122,6 +131,8 @@ void oled_enable_hooks() {
if (oled_set_brightness_hook < 0) {
LOG("[OLED] taiHookFunctionExportForKernel: 0x%08X\n", oled_set_brightness_hook);
}

return 0;
}

void oled_disable_hooks() {
Expand All @@ -130,4 +141,77 @@ void oled_disable_hooks() {

if (oled_set_brightness_hook >= 0)
taiHookReleaseForKernel(oled_set_brightness_hook, oled_set_brightness_ref);
}

/*
* Syscall exports.
*/

int vitabrightOledGetLevel() {
int state;
ENTER_SYSCALL(state);
int brightness = ksceOledGetBrightness();

int level;
if (brightness == 0) {
level = -1;
} else if (brightness == 1) {
level = 16;
} else if (brightness < 0x1000) {
level = 15;
} else if (brightness >= 0x10000) {
level = 0;
} else {
level = 16 - ((brightness + 0x1000) / 0x1000);
}

EXIT_SYSCALL(state);
return level;
}

int vitabrightOledSetLevel(unsigned int level) {
int state;
ENTER_SYSCALL(state);

unsigned int brightness = 0;
if (level > 16) {
brightness = 0;
} else if (level == 16) {
brightness = 1;
} else if (level == 15) {
brightness = 0xfff;
} else if (level == 0) {
brightness = 0x10000;
} else {
brightness = 0x1000 * (16 - level) - 0x1000;
}

isDimmingWorkAround = 0;
ksceOledSetBrightness(brightness);
isDimmingWorkAround = 1;

EXIT_SYSCALL(state);
return level;
}

int vitabrightOledGetLut(unsigned char oledLut[LUT_SIZE]) {
int state;
ENTER_SYSCALL(state);
ksceKernelMemcpyKernelToUser((uintptr_t)oledLut, lookupNew, LUT_SIZE);

EXIT_SYSCALL(state);
return 0;
}

int vitabrightOledSetLut(unsigned char oledLut[LUT_SIZE]) {
int state;
ENTER_SYSCALL(state);

oled_disable_hooks();
ksceKernelMemcpyUserToKernel(lookupNew, (uintptr_t)oledLut, LUT_SIZE);

oled_apply_lut();

EXIT_SYSCALL(state);
return 0;
}
8 changes: 0 additions & 8 deletions vitabright.yml

This file was deleted.

0 comments on commit 6157d40

Please sign in to comment.