Skip to content

Commit

Permalink
add QTI eSE power manager patch
Browse files Browse the repository at this point in the history
  • Loading branch information
cinit committed Dec 28, 2021
1 parent dc898c8 commit 44a86d6
Show file tree
Hide file tree
Showing 8 changed files with 181 additions and 0 deletions.
1 change: 1 addition & 0 deletions app/src/main/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ add_subdirectory(libnciclient)
add_subdirectory(ncihostd)
add_subdirectory(libbasehalpatch)
add_subdirectory(libnxphalpatch)
add_subdirectory(libqtiesepmpatch)
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "../../libbasehalpatch/hook/hook_proc_symbols.h"

using namespace halpatch;
using namespace halpatch::nxphal;

static volatile bool sInitialized = false;
static volatile int sNciDeviceFd = -1;
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/cpp/libnxphalpatch/ipc/ipc_requests.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

#include <cstdint>

namespace halpatch::nxphal {

enum class RequestId : uint32_t {
GET_VERSION = 0x1,
GET_HOOK_STATUS = 0x40,
Expand Down Expand Up @@ -43,4 +45,6 @@ struct DeviceWriteRequest {
};
// no size static assert...

}

#endif //NCI_HOST_NATIVES_IPC_REQUESTS_H
33 changes: 33 additions & 0 deletions app/src/main/cpp/libqtiesepmpatch/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
cmake_minimum_required(VERSION 3.10)

project(qtiesepmpatch)

set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 17)

add_library(qtiesepmpatch SHARED
inject_entry.c ipc/ipc_request_handler.cpp)

if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
SET(CLANG_CXX_EXTRA_OPT "-Werror=unknown-warning-option -Werror=format-invalid-specifier -Werror=call-to-pure-virtual-from-ctor-dtor")
SET(CLANG_C_EXTRA_OPT "-Werror=format-invalid-specifier")
else ()
SET(CLANG_CXX_EXTRA_OPT "")
SET(CLANG_C_EXTRA_OPT "")
endif ()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CLANG_CXX_EXTRA_OPT} -fPIC -Werror=delete-non-virtual-dtor -Werror=return-type -Werror=non-virtual-dtor -Wno-invalid-offsetof")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${CLANG_C_EXTRA_OPT} -fPIC -Werror=return-type")

set(CMAKE_CXX_IMPLICIT_LINK_LIBRARIES "")
set(CMAKE_CXX_IMPLICIT_LINK_DIRECTORIES "")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--no-allow-shlib-undefined,--no-undefined")

set_target_properties(qtiesepmpatch PROPERTIES
LINKER_LANGUAGE C
CXX_EXTENSIONS OFF
POSITION_INDEPENDENT_CODE ON
)

target_compile_definitions(qtiesepmpatch PRIVATE NCI_HOST_VERSION=\"${NCI_HOST_VERSION}\")

target_link_libraries(qtiesepmpatch basehalpatch ${ANDROID_LIBS})
24 changes: 24 additions & 0 deletions app/src/main/cpp/libqtiesepmpatch/inject_entry.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef NCI_HOST_VERSION
#error Please define macro NCI_HOST_VERSION in CMakeLists.txt
#endif

#include <stddef.h>
#include <errno.h>

#include "../libbasehalpatch/ipc/inject_io_init.h"

__attribute__((used, section("NCI_HOST_VERSION"), visibility("default")))
const char g_nci_host_version[] = NCI_HOST_VERSION;

// called by daemon with ptrace
__attribute__((noinline, visibility("default")))
void *qti_esepm_patch_inject_init(int fd) {
(void) g_nci_host_version;
if (fd < 0) {
return (void *) -EINVAL;
}
if (initElfHeaderInfo("libqtiesepmpatch.so", &qti_esepm_patch_inject_init) == 0) {
return (void *) -EBADE;
}
return (void *) (size_t) BaseHalPatchInitSocket(fd);
}
96 changes: 96 additions & 0 deletions app/src/main/cpp/libqtiesepmpatch/ipc/ipc_request_handler.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
//
// Created by kinit on 2021-11-18.
//
#include <cstring>
#include <unistd.h>

#include "ipc_requests.h"
#include "../../libbasehalpatch/ipc/request_handler.h"
#include "../../libbasehalpatch/hook/hook_proc_symbols.h"

using namespace halpatch;
using namespace halpatch::qtiesepm;

static volatile bool sInitialized = false;
static volatile int sNciDeviceFd = -1;

const char *const NCI_DEVICE_NAME = "/dev/nq-nci";

namespace halpatchhook::callback {

void afterHook_read(ssize_t result, int fd, const void *buffer, size_t size) {}

void afterHook_write(ssize_t result, int fd, const void *buffer, size_t size) {}

void afterHook_open(int result, const char *name, int flags, uint32_t mode) {
if (result > 0 && name != nullptr && strncmp(NCI_DEVICE_NAME, name, strlen(NCI_DEVICE_NAME)) == 0) {
sNciDeviceFd = result;
}
}

void afterHook_close(int result, int fd) {
if (fd == sNciDeviceFd) {
sNciDeviceFd = -1;
}
}

void afterHook_ioctl(int result, int fd, unsigned long int request, uint64_t arg) {}

void afterHook_select(int result, int nfds, void *readfds, void *writefds, void *exceptfds, void *timeout) {}

}

void handleGetVersionRequest(uint32_t requestId, const void *, uint32_t) {
const char *version = NCI_HOST_VERSION;
HalResponse response = {requestId, 0, 0, static_cast<uint32_t>(strlen(version))};
sendResponsePacket(response, version);
}

void handleGetHookStatusRequest(uint32_t requestId, const void *payload, uint32_t payloadSize) {
HalResponse response = {requestId, sInitialized, 0, 0};
sendResponsePacket(response, nullptr);
}

void handleGetNciFdRequest(uint32_t requestId, const void *payload, uint32_t payloadSize) {
HalResponse response = {requestId, uint32_t(sNciDeviceFd), 0, 0};
sendResponsePacket(response, nullptr);
}

void handleInitPltHookRequest(uint32_t requestId, const void *payload, uint32_t payloadSize) {
HalResponse response = {requestId, 0, 0, 0};
const auto *originHookProcedure = reinterpret_cast<const OriginHookProcedure *>(payload);
if (payloadSize != sizeof(OriginHookProcedure) || originHookProcedure == nullptr
|| originHookProcedure->struct_size != sizeof(OriginHookProcedure)) {
sendResponseError(requestId, HalRequestErrorCode::ERR_INVALID_ARGUMENT,
"invalid payload OriginHookProcedure");
} else {
int result = hook_sym_init(originHookProcedure);
if (result == 0) {
sInitialized = true;
}
response.result = result;
sendResponsePacket(response, nullptr);
}
}

int handleRequestPacket(const HalRequest &request, const void *payload) {
uint32_t requestId = request.id;
auto requestCode = static_cast<RequestId>(request.requestCode);
uint32_t payloadSize = request.payloadSize;
switch (requestCode) {
case RequestId::GET_VERSION:
handleGetVersionRequest(requestId, payload, payloadSize);
return 0;
case RequestId::GET_HOOK_STATUS:
handleGetHookStatusRequest(requestId, payload, payloadSize);
return 0;
case RequestId::INIT_PLT_HOOK:
handleInitPltHookRequest(requestId, payload, payloadSize);
return 0;
case RequestId::GET_NCI_FD:
handleGetNciFdRequest(requestId, payload, payloadSize);
return 0;
default:
return -1;
}
}
21 changes: 21 additions & 0 deletions app/src/main/cpp/libqtiesepmpatch/ipc/ipc_requests.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// Created by kinit on 2021-11-18.
//

#ifndef NCI_HOST_NATIVES_IPC_REQUESTS_H
#define NCI_HOST_NATIVES_IPC_REQUESTS_H

#include <cstdint>

namespace halpatch::qtiesepm {

enum class RequestId : uint32_t {
GET_VERSION = 0x1,
GET_HOOK_STATUS = 0x40,
INIT_PLT_HOOK = 0x41,
GET_NCI_FD = 0x61,
};

}

#endif //NCI_HOST_NATIVES_IPC_REQUESTS_H
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

using namespace hwhal;
using namespace halpatch;
using namespace halpatch::nxphal;
using namespace ipcprotocol;

static const char *const LOG_TAG = "NxpHalHandler";
Expand Down

0 comments on commit 44a86d6

Please sign in to comment.