Skip to content

Commit

Permalink
Add support for different DDB types (#39)
Browse files Browse the repository at this point in the history
Co-authored-by: SKGleba <[email protected]>
  • Loading branch information
devnoname120 and SKGleba committed Mar 18, 2022
1 parent f5e7f46 commit dd2d75c
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 60 deletions.
5 changes: 3 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ if (ENABLE_LOGGING)
add_definitions(-DENABLE_LOGGING=1)
endif ()

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wl,-q -Wall -O3 -fno-pic -fno-PIC -std=gnu99")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wl,-q -Wall -Wextra -O3 -fno-pic -fno-PIC -std=gnu99")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -fno-rtti -fno-exceptions")

include_directories(
Expand All @@ -33,7 +33,7 @@ add_executable(vitabright
oled/parser.c
oled/hooks.c
lcd/hooks.c
)
main.h)


target_link_libraries(vitabright
Expand All @@ -45,6 +45,7 @@ target_link_libraries(vitabright
gcc
SceSysclibForDriver_stub
SceSysmemForDriver_stub
SceSysrootForKernel_stub
SceSysrootForDriver_stub
#SceLibKernel_stub
)
Expand Down
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
Vitabright is a plugin enabling you to alter the luminosity levels of your PS Vita. It thus allows you to decrease the brightness level below the minimum, and increase it above the maximum.
**This plugin is compatible with both OLED (PS Vita 1000) and LCD (PS Vita 2000) models. Tested to be functional on 3.60 Ensō, 3.65 h-encore, 3.67 h-encore and 3.68 h-encore**.

**Important note**: While most OLED screens work with vitabright, **some OLED screens** (PS Vita 1000) don't work with it. It seems that their gamma table isn't overridden properly. See issue [#13](https://github.com/devnoname120/vitabright/issues/13) for more information.

## Installation

- Download the latest version from the [Releases section](https://github.com/devnoname120/vitabright/releases).
Expand Down
39 changes: 28 additions & 11 deletions lcd/hooks.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "hooks.h"
#include "../main.h"
#include "../log.h"
#include <psp2kern/kernel/modulemgr.h>
#include <taihen.h>
Expand All @@ -14,7 +15,10 @@ static SceUID lcd_set_brightness_hook = -1;

static tai_hook_ref_t lcd_set_brightness_ref = -1;

// static uint8_t old_brightness_values[17] = {31, 37, 43, 50, 58, 67,
static SceUID power_set_max_brightness_hook = -1;
static tai_hook_ref_t power_set_max_brightness_ref = 0;

// static uint8_t original_brightness_values[17] = {31, 37, 43, 50, 58, 67,
// 77, 88, 100, 114, 129, 147,
// 166, 182, 203, 227, 255};

Expand All @@ -26,8 +30,6 @@ int (*ksceLcdSetBrightness)(unsigned int brightness);

int module_get_offset(SceUID pid, SceUID modid, int segidx, size_t offset, uintptr_t *addr);

int ksceKernelSysrootGetSystemSwVersion(void);

int lcd_brightness_to_index(unsigned int brightness) {
// 17 levels (0 to 16), brightness starts at 2, max 65536
return 16 * (brightness - 2) / 65534;
Expand All @@ -51,21 +53,36 @@ int hook_ksceLcdSetBrightness(unsigned int brightness) {
return TAI_CONTINUE(int, lcd_set_brightness_ref, new_brightness);
}

int hook_kscePowerSetDisplayMaxBrightnessForLcd(int limit) {
// Workaround for https://github.com/yifanlu/taiHEN/issues/12
if (power_set_max_brightness_ref == 0) {
return 0;
}

// Limits are not necessary on LCD.
return TAI_CONTINUE(int, power_set_max_brightness_ref, 0x10000);
}

void lcd_enable_hooks() {
// Completely remove max brightness limit when PS Vita is running in power mode C/D.
power_set_max_brightness_hook = taiHookFunctionExportForKernel(KERNEL_PID, &power_set_max_brightness_ref, "ScePower", TAI_ANY_LIBRARY, 0x77027B6B, hook_kscePowerSetDisplayMaxBrightnessForLcd);
LOG("[LCD] hooking kscePowerSetDisplayMaxBrightness: 0x%08X\n", power_set_max_brightness_hook);

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

if (ret < 0)
if (ret < 0) {
LOG("[LCD] Couldn't get SceLcd module id! Abandoning...\n");
return;
}

uint32_t lcd_table_off = 0;
size_t ksceLcdGetBrightness_addr = 0;
size_t ksceLcdSetBrightness_addr = 0;

unsigned int sw_version = ksceKernelSysrootGetSystemSwVersion();
switch (sw_version >> 16) {
case 0x360: {
lcd_table_off = 0x1B00;
Expand All @@ -86,7 +103,7 @@ void lcd_enable_hooks() {
}

default: // Not supported
LOG("[LCD] Unsupported OS version: 0x%08X\n", sw_version);
LOG("[LCD] Unsupported OS version: 0x%08X. Abandoning...\n", sw_version);
return;
}

Expand All @@ -108,13 +125,13 @@ void lcd_enable_hooks() {
int res_offset1 = module_get_offset(
KERNEL_PID, info.modid, 0, ksceLcdGetBrightness_addr, (uintptr_t *)&ksceLcdGetBrightness);
if (res_offset1 < 0) {
LOG("[LCD] module_get_offset1: 0x%08X\n", res_offset1);
LOG("[LCD] module_get_offset(\"ksceLcdGetBrightness\"): 0x%08X\n", res_offset1);
}

int res_offset2 = module_get_offset(
KERNEL_PID, info.modid, 0, ksceLcdSetBrightness_addr, (uintptr_t *)&ksceLcdSetBrightness);
if (res_offset2 < 0) {
LOG("[LCD] module_get_offset2: 0x%08X\n", res_offset2);
LOG("[LCD] module_get_offset(\"ksceLcdSetBrightness\"): 0x%08X\n", res_offset2);
}

if (ksceLcdGetBrightness != NULL && ksceLcdSetBrightness != NULL && res_offset1 >= 0 &&
Expand All @@ -132,7 +149,7 @@ void lcd_enable_hooks() {
0x581D3A87,
hook_ksceLcdSetBrightness);
if (lcd_set_brightness_hook < 0) {
LOG("[LCD] taiHookFunctionExportForKernel: 0x%08X\n", lcd_set_brightness_hook);
LOG("[LCD] taiHookFunctionExportForKernel: 0x%08X, abandoning...\n", lcd_set_brightness_hook);
}
}

Expand Down
38 changes: 19 additions & 19 deletions main.c
Original file line number Diff line number Diff line change
@@ -1,37 +1,34 @@
#include <psp2kern/kernel/cpu.h>
#include <psp2kern/kernel/modulemgr.h>
#include <taihen.h>
#include <psp2kern/kernel/sysroot.h>

#include "main.h"
#include "lcd/hooks.h"
#include "log.h"
#include "oled/hooks.h"

int ksceDisplaySetBrightness(int unk, unsigned int brightness);
static SceUID power_set_max_brightness_hook = -1;
static tai_hook_ref_t power_set_max_brightness_ref = 0;

static tai_hook_ref_t hook_power_get_max_brightness = 0;

unsigned int power_get_max_brightness() {
if (hook_power_get_max_brightness != 0) {
return TAI_CONTINUE(unsigned int, hook_power_get_max_brightness);
}

return 65536;
}
unsigned int sw_version = 0;

void _start() __attribute__((weak, alias("module_start")));
int module_start(SceSize argc, const void *args) {
LOG("vitabright started...\n");

// int ret = taiHookFunctionExportForKernel(KERNEL_PID,
// &hook_power_get_max_brightness,
// "ScePower",
// TAI_ANY_LIBRARY,
// 0xD8759B55,
// power_get_max_brightness);
// LOG("ret: 0x%08X\n", ret);
sw_version = ksceKernelSysrootGetSystemSwVersion();
LOG("[OS] version: %08X\n", sw_version);

oled_enable_hooks();
lcd_enable_hooks();
// Boot type indicator 1. See https://wiki.henkaku.xyz/vita/Sysroot#Boot_type_indicator_1
int is_lcd = *(uint8_t*)(ksceKernelSysrootGetKblParam() + 0xE8) & 9;
LOG("[OS] isLcd: %d", is_lcd);

if (is_lcd){
lcd_enable_hooks();
} else {
oled_enable_hooks();
}

return SCE_KERNEL_START_SUCCESS;
}
Expand All @@ -56,5 +53,8 @@ int module_stop(SceSize argc, const void *args) {
oled_disable_hooks();
lcd_disable_hooks();

if (power_set_max_brightness_hook >= 0)
taiHookReleaseForKernel(power_set_max_brightness_hook, power_set_max_brightness_ref);

return SCE_KERNEL_STOP_SUCCESS;
}
3 changes: 3 additions & 0 deletions main.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#pragma once

extern unsigned int sw_version;
Loading

0 comments on commit dd2d75c

Please sign in to comment.