Skip to content

Instantly share code, notes, and snippets.

@mouseos
Last active July 31, 2023 15:16
Show Gist options
  • Save mouseos/572e24712acda6bc67a376d5228c4ce5 to your computer and use it in GitHub Desktop.
Save mouseos/572e24712acda6bc67a376d5228c4ce5 to your computer and use it in GitHub Desktop.
clova deskの/sysyem/bin/keyeventの逆コンパイル(binaryninja)
int64_t sub_1458()
{
/* jump -> data_4e50 */
}
void __libc_init(void* raw_args, void (* onexit)(),
int32_t (* main)(int32_t argc, char** argv, char** envp),
void const* structors) __noreturn
{
/* tailcall */
return __libc_init(raw_args, onexit, main, structors);
}
int32_t __cxa_atexit(void (* func)(void*), void* arg, void* dso_handle)
{
/* tailcall */
return __cxa_atexit(func, arg, dso_handle);
}
int64_t __register_atfork(void (* arg1)(), void (* arg2)(), void (* arg3)(),
int64_t arg4)
{
/* tailcall */
return __register_atfork(arg1, arg2, arg3, arg4);
}
int64_t SpecialCharacterFilter()
{
/* tailcall */
return SpecialCharacterFilter();
}
int64_t open()
{
/* tailcall */
return open();
}
int64_t __android_log_print()
{
/* tailcall */
return __android_log_print();
}
int64_t __write_chk()
{
/* tailcall */
return __write_chk();
}
int64_t close(int32_t arg1)
{
/* tailcall */
return close(arg1);
}
void __stack_chk_fail() __noreturn
{
/* tailcall */
return __stack_chk_fail();
}
int64_t fopen(char* arg1, char* arg2)
{
/* tailcall */
return fopen(arg1, arg2);
}
int64_t fprintf()
{
/* tailcall */
return fprintf();
}
int64_t fclose(FILE* arg1)
{
/* tailcall */
return fclose(arg1);
}
int64_t sync()
{
/* tailcall */
return sync();
}
int64_t property_set()
{
/* tailcall */
return property_set();
}
int64_t property_get()
{
/* tailcall */
return property_get();
}
int64_t strcmp()
{
/* tailcall */
return strcmp();
}
int64_t __errno()
{
/* tailcall */
return __errno();
}
int64_t strerror(int32_t arg1)
{
/* tailcall */
return strerror(arg1);
}
int64_t execl()
{
/* tailcall */
return execl();
}
int64_t __snprintf_chk()
{
/* tailcall */
return __snprintf_chk();
}
int64_t mixer_open()
{
/* tailcall */
return mixer_open();
}
int64_t usleep(useconds_t arg1)
{
/* tailcall */
return usleep(arg1);
}
int64_t mixer_get_ctl_by_name()
{
/* tailcall */
return mixer_get_ctl_by_name();
}
int64_t mixer_ctl_set_value()
{
/* tailcall */
return mixer_ctl_set_value();
}
int64_t mixer_close()
{
/* tailcall */
return mixer_close();
}
int64_t memset(int64_t arg1, int32_t arg2, size_t arg3)
{
/* tailcall */
return memset(arg1, arg2, arg3);
}
int64_t strstr(char* arg1, char* arg2)
{
/* tailcall */
return strstr(arg1, arg2);
}
int64_t sigaddset(sigset_t* arg1, int32_t arg2)
{
/* tailcall */
return sigaddset(arg1, arg2);
}
int64_t sigaction(int32_t arg1, struct sigaction* arg2, struct sigaction* arg3)
{
/* tailcall */
return sigaction(arg1, arg2, arg3);
}
int64_t pthread_mutex_lock(union pthread_mutex_t* arg1)
{
/* tailcall */
return pthread_mutex_lock(arg1);
}
int64_t send(int32_t arg1, int64_t arg2, size_t arg3, int32_t arg4)
{
/* tailcall */
return send(arg1, arg2, arg3, arg4);
}
int64_t pthread_mutex_unlock(union pthread_mutex_t* arg1)
{
/* tailcall */
return pthread_mutex_unlock(arg1);
}
int64_t malloc(size_t arg1)
{
/* tailcall */
return malloc(arg1);
}
int64_t free(int64_t arg1)
{
/* tailcall */
return free(arg1);
}
int64_t getenv(char* arg1)
{
/* tailcall */
return getenv(arg1);
}
int64_t strtol(char* arg1, char** arg2, int32_t arg3)
{
/* tailcall */
return strtol(arg1, arg2, arg3);
}
int64_t listen()
{
/* tailcall */
return listen();
}
int64_t accept(int32_t arg1, union __SOCKADDR_ARG arg2, socklen_t* arg3)
{
/* tailcall */
return accept(arg1, arg2, arg3);
}
int64_t pthread_mutex_init(union pthread_mutex_t* arg1,
union pthread_mutexattr_t* arg2)
{
/* tailcall */
return pthread_mutex_init(arg1, arg2);
}
int64_t ioctl()
{
/* tailcall */
return ioctl();
}
int64_t realloc(int64_t arg1, size_t arg2)
{
/* tailcall */
return realloc(arg1, arg2);
}
int64_t strdup(char* arg1)
{
/* tailcall */
return strdup(arg1);
}
int64_t __read_chk(int32_t arg1, int64_t arg2, size_t arg3, size_t arg4)
{
/* tailcall */
return __read_chk(arg1, arg2, arg3, arg4);
}
int64_t strncpy(char* arg1, char* arg2, size_t arg3)
{
/* tailcall */
return strncpy(arg1, arg2, arg3);
}
int64_t strnlen()
{
/* tailcall */
return strnlen();
}
int64_t memmove(int64_t arg1, int64_t arg2, size_t arg3)
{
/* tailcall */
return memmove(arg1, arg2, arg3);
}
int64_t opendir(char* arg1)
{
/* tailcall */
return opendir(arg1);
}
int64_t readdir(DIR* arg1)
{
/* tailcall */
return readdir(arg1);
}
int64_t closedir(DIR* arg1)
{
/* tailcall */
return closedir(arg1);
}
int64_t calloc(int64_t arg1, int64_t arg2)
{
/* tailcall */
return calloc(arg1, arg2);
}
int64_t inotify_init()
{
/* tailcall */
return inotify_init();
}
int64_t inotify_add_watch()
{
/* tailcall */
return inotify_add_watch();
}
int64_t __poll_chk(struct pollfd* arg1, nfds_t arg2, int32_t arg3,
uint64_t arg4)
{
/* tailcall */
return __poll_chk(arg1, arg2, arg3, arg4);
}
int64_t sub_17c8(void* arg1) __noreturn
{
int64_t var_18 = 0x4b28;
int64_t var_10 = 0x4b38;
int64_t var_8 = 0x4b48;
__libc_init(arg1, nullptr, main, &var_18);
/* no return */
}
int64_t _start() __noreturn
{
/* tailcall */
return sub_17c8(&arg_0);
}
void func(void* arg1)
{
if (arg1 != 0)
{
arg1();
}
}
int64_t sub_184c(void* arg1)
{
return __cxa_atexit(func, arg1, &data_5000);
}
int64_t sub_1880(void (* arg1)(), void (* arg2)(), void (* arg3)())
{
return __register_atfork(arg1, arg2, arg3, &data_5000);
}
int64_t sub_18b8(int32_t arg1)
{
int64_t x21 = *__stack_chk_guard;
int128_t var_51 = (*"n-1/timer_reset")[0];
int128_t var_70 = (*"/sys/devices/soc.0/qpnp-power-on…")[0];
int128_t var_60 = (*".0/qpnp-power-on-1/timer_reset")[0];
int64_t var_79 = 0;
int64_t var_88 = 0;
int64_t var_80 = 0;
int64_t var_98 = 0;
int64_t var_90 = 0;
int64_t var_a0 = 0;
SpecialCharacterFilter(&var_70, &var_a0, 0x2f);
int32_t x0_1 = open();
int64_t x0_2;
if ((x0_1 & 0x80000000) != 0)
{
x0_2 = __android_log_print(6, "KeyEvent", "Factory Reset File open Error");
}
else
{
char* const x1_2;
if ((arg1 & 1) == 0)
{
__android_log_print(4);
x1_2 = "0";
}
else
{
__android_log_print(4);
x1_2 = "1";
}
__write_chk(x0_1, x1_2, 1, 2);
x0_2 = close(x0_1);
}
if ((x21 - x21) != 0)
{
__stack_chk_fail();
/* no return */
}
return x0_2;
}
int64_t run_factory_reset()
{
int64_t x21 = *__stack_chk_guard;
FILE* recovery_cmd = fopen("/cache/recovery/command", &data_2f4c);
int64_t result;
if (recovery_cmd == 0)
{
int32_t* x0_9 = __errno();
__android_log_print(6, "KeyEvent", "fopen() failed -- %d(%s)\n", *x0_9, strerror(*x0_9));
__android_log_print(6, "KeyEvent", "write_recovery_command() failed\n");
result = 0xffffffff;
}
else
{
fprintf();
fclose(recovery_cmd);
sync();
int64_t var_48_1 = 0;
int64_t var_40_1 = 0;
int64_t var_58 = 0;
int64_t var_50_1 = 0;
int32_t set_reboot_recovery = property_set("sys.powerctl", "reboot,recovery");
int32_t x0_4;
if (set_reboot_recovery == 0)
{
property_get("sys.powerctl", &var_58, &data_2ea3);
x0_4 = strcmp("reboot,recovery", &var_58);
}
if ((set_reboot_recovery != 0 || (set_reboot_recovery == 0 && x0_4 != 0)))
{
int32_t* error_number = __errno();
__android_log_print(6, "KeyEvent", "property_set_verified() failed -…", "sys.powerctl", *error_number, strerror(*error_number));
execl();
}
result = 0;
}
if ((x21 - x21) != 0)
{
__stack_chk_fail();
/* no return */
}
return result;
}
int64_t sub_1b40(int32_t arg1)
{
int64_t x22 = *__stack_chk_guard;
__snprintf_chk();
int64_t mixer_open_result = mixer_open(0);
if (mixer_open_result == 0)
{
int32_t x20_1 = 0;
do
{
__android_log_print(6, 0, "mixer open error try -- %d", x20_1);
usleep(0x7a120);
mixer_open_result = mixer_open(0);
if (mixer_open_result != 0)
{
break;
}
x20_1 = (x20_1 + 1);
} while (x20_1 < 0xa);
__android_log_print(6, 0, "mixer open error");
}
void var_9c;
int64_t x0_3 = mixer_get_ctl_by_name(mixer_open_result, &var_9c);
int64_t result;
if (x0_3 == 0)
{
__android_log_print(6, 0, "%s: could not get %s ctl", "mix_off", &var_9c);
mixer_close(mixer_open_result);
result = 0xffffffea;
}
else if (mixer_ctl_set_value(x0_3, 0, 0) == 0)
{
mixer_close(mixer_open_result);
result = 0;
}
else
{
__android_log_print(6, 0, "error setting value %d on %s ", "mix_off", &var_9c);
mixer_close(mixer_open_result);
result = 0xffffffff;
}
if ((x22 - x22) != 0)
{
__stack_chk_fail();
/* no return */
}
return result;
}
int64_t check_shutdown_or_reboot()
{
int64_t x19 = *__stack_chk_guard;
void var_84;
memset(&var_84, 0, 0x5c);
property_get("sys.powerctl", &var_84, "no value");
int64_t x0_2 = strstr(&var_84, "reboot");
int64_t result;
if (x0_2 == 0)
{
result = strstr(&var_84, "shutdown");
}
if ((x0_2 != 0 || (x0_2 == 0 && result != 0)))
{
sub_1b40(1);
result = sub_1b40(2);
}
if ((x19 - x19) != 0)
{
__stack_chk_fail();
/* no return */
}
return result;
}
int64_t set_shutdownreceiver()
{
int64_t x19 = *__stack_chk_guard;
int64_t var_38 = 0;
int64_t var_30 = 0;
int64_t var_48 = 0;
int64_t var_40 = 0;
int64_t var_40_1 = 0x1cb0;
sigaddset(&var_38, 0xf);
sigaction(0xf, &var_48, nullptr);
sigaction(2, &var_48, nullptr);
int64_t result = property_set("ro.infr.shutdownreceiver", "keyevent");
if ((x19 - x19) != 0)
{
__stack_chk_fail();
/* no return */
}
return result;
}
int64_t notify_keyevent_client_cmd(int32_t* arg1, int32_t arg2, int32_t arg3)
{
int64_t result = 0xffffffff;
if ((arg1 != 0 && arg2 != 0))
{
void* const cmd;
if (*arg1 == 0x65)
{
cmd = "CMD_DEMO_MODE";
}
else
{
cmd = nullptr;
}
__android_log_print(4, "KeyEvent", "notify to client: cmd:%s\n", cmd);
pthread_mutex_lock(&data_5048);
int32_t send_result = send(arg3, arg1, 4, 0);
pthread_mutex_unlock(&data_5048);
if ((send_result & 0x80000000) != 0)
{
int32_t* x0_3 = __errno();
__android_log_print(6, "KeyEvent", "send failed %d -- errno=%d (%s)", arg3, *x0_3, strerror(*x0_3));
x0 = 0xffffffff;
}
else
{
__android_log_print(4, "KeyEvent", "notify to sock[%d] OK", arg3);
result = 0;
}
}
return result;
}
int64_t sub_1ed8(int32_t arg1, int32_t arg2)
{
int32_t* x0 = malloc(4);
int64_t result;
if (x0 == 0)
{
__android_log_print(6, "KeyEvent", "notify_keyword_detected: buffer …");
result = 0xffffffff;
}
else
{
*x0 = arg2;
if ((notify_keyevent_client_cmd(x0, 4, arg1) & 0x80000000) != 0)
{
result = 0xffffffff;
}
else
{
free(x0);
result = 0;
}
}
return result;
}
uint64_t init_keyevent_socket()()
{
int64_t x22 = *__stack_chk_guard;
__android_log_print(3, "KeyEvent", "broadcast_init()");
data_5008;
__snprintf_chk();
void var_88;
char* x0_1 = getenv(&var_88);
int32_t* error_number;
int32_t x0_4;
void* const error_message;
int32_t result;
if (x0_1 != 0)
{
error_number = __errno();
*error_number = 0;
x0_4 = strtol(x0_1, nullptr, 0xa);
if ((*error_number == 0 && (x0_4 & 0x80000000) == 0))
{
if (listen(x0_4, 0x80) != 0)
{
__android_log_print(6, "KeyEvent", "listen() failed - errno:%d, %s", *error_number, strerror(*error_number));
close(x0_4);
result = -1;
}
else
{
socklen_t var_fc = 0x6e;
void var_f8;
result = accept(x0_4, &var_f8, &var_fc);
if ((result & 0x80000000) != 0)
{
error_message = "accept() failed";
goto label_2064;
}
if ((pthread_mutex_init(&data_5048, nullptr) & 0x80000000) != 0)
{
error_message = "pthread_mutex_init() failed";
goto label_2064;
}
__android_log_print(3, "KeyEvent", "broadcast_init() OK");
}
}
}
if (((x0_1 == 0 || (x0_1 != 0 && *error_number != 0)) || ((x0_1 != 0 && *error_number == 0) && (x0_4 & 0x80000000) != 0)))
{
error_message = "android_get_control_socket() fai…";
label_2064:
__android_log_print(6, "KeyEvent", error_message);
result = -1;
}
if ((x22 - x22) != 0)
{
__stack_chk_fail();
/* no return */
}
return result;
}
int64_t sub_2108()
{
uint32_t x8 = key_power_pressed;
if ((((((x8 == 1 && key_mute_pressed == 1) && (key_play_pause_pressed & 1) == 0) && (key_vol_up_pressed & 1) == 0) && (key_vol_down_pressed & 1) == 0) && (is_factory_reset & 1) == 0))
{
is_factory_reset = 1;
}
if (((((x8 != 1 || (x8 == 1 && key_mute_pressed != 1)) || ((x8 == 1 && key_mute_pressed == 1) && (key_play_pause_pressed & 1) != 0)) || (((x8 == 1 && key_mute_pressed == 1) && (key_play_pause_pressed & 1) == 0) && (key_vol_up_pressed & 1) != 0)) || ((((x8 == 1 && key_mute_pressed == 1) && (key_play_pause_pressed & 1) == 0) && (key_vol_up_pressed & 1) == 0) && (key_vol_down_pressed & 1) != 0)))
{
if (is_factory_reset != 1)
{
goto mode_processor;
}
if (x8 == 0)
{
label_21b4:
is_factory_reset = 0;
mode_processor:
uint32_t x9_10 = key_vol_up_pressed;
uint32_t x11 = data_5026;
if (x9_10 != 1)
{
if (x11 != 0)
{
data_5026 = 0;
data_5027 = 0;
data_5018 = 0;
data_501c = 0;
}
}
else if ((x11 & 1) == 0)
{
data_5026 = 1;
}
uint32_t x10_2 = key_mute_pressed;
if (((x10_2 == 1 && key_play_pause_pressed == 1) && (is_demo_mode & 1) == 0))
{
is_demo_mode = 1;
}
if ((((x10_2 != 1 || (x10_2 == 1 && key_play_pause_pressed != 1)) && is_demo_mode == 1) || (x10_2 == 1 && key_play_pause_pressed == 1)))
{
if (((x8 | (x10_2 ^ 1)) & 1) != 0)
{
label_2284:
is_demo_mode = 0;
}
else
{
if ((((x9_10 | (!key_play_pause_pressed)) & 1) & 1) != 0)
{
goto label_2284;
}
if (key_vol_down_pressed == 1)
{
goto label_2284;
}
}
}
return;
}
}
if ((((((x8 == 1 && key_mute_pressed == 1) && (key_play_pause_pressed & 1) == 0) && (key_vol_up_pressed & 1) == 0) && (key_vol_down_pressed & 1) == 0) || (((((x8 != 1 || (x8 == 1 && key_mute_pressed != 1)) || ((x8 == 1 && key_mute_pressed == 1) && (key_play_pause_pressed & 1) != 0)) || (((x8 == 1 && key_mute_pressed == 1) && (key_play_pause_pressed & 1) == 0) && (key_vol_up_pressed & 1) != 0)) || ((((x8 == 1 && key_mute_pressed == 1) && (key_play_pause_pressed & 1) == 0) && (key_vol_up_pressed & 1) == 0) && (key_vol_down_pressed & 1) != 0)) && is_factory_reset == 1)))
{
if (key_mute_pressed != 1)
{
goto label_21b4;
}
if ((key_play_pause_pressed & 1) != 0)
{
goto label_21b4;
}
if ((key_vol_up_pressed & 1) != 0)
{
goto label_21b4;
}
if (key_vol_down_pressed == 1)
{
goto label_21b4;
}
goto mode_processor;
}
}
void sub_228c()
{
int64_t x19 = *__stack_chk_guard;
int32_t x9_5;
if ((key_vol_down_pressed == 1 && (data_5027 & 1) == 0))
{
int32_t x9_4 = data_5018;
data_5027 = 1;
int32_t x13_1 = data_501c;
x9_5 = (x9_4 + 1);
data_5018 = x9_5;
data_501c = ((1 << (x9_4 << 2)) | x13_1);
}
if ((key_vol_down_pressed != 1 || (key_vol_down_pressed == 1 && (data_5027 & 1) != 0)))
{
int32_t x11_1;
if ((key_play_pause_pressed == 1 && (data_5027 & 1) == 0))
{
x11_1 = 2;
}
if ((key_play_pause_pressed != 1 || (key_play_pause_pressed == 1 && (data_5027 & 1) != 0)))
{
if ((key_mute_pressed != 1 || (key_mute_pressed == 1 && (data_5027 & 1) != 0)))
{
x9_5 = data_5018;
data_5027 = 0;
}
if ((key_mute_pressed == 1 && (data_5027 & 1) == 0))
{
x11_1 = 3;
}
}
if (((key_play_pause_pressed == 1 && (data_5027 & 1) == 0) || (((key_play_pause_pressed != 1 || (key_play_pause_pressed == 1 && (data_5027 & 1) != 0)) && key_mute_pressed == 1) && (data_5027 & 1) == 0)))
{
int32_t x9_6 = data_5018;
data_5027 = 1;
int32_t x14_3 = data_501c;
x9_5 = (x9_6 + 1);
data_5018 = x9_5;
data_501c = ((x11_1 << (x9_6 << 2)) | x14_3);
}
}
if (x9_5 == 8)
{
void product_name;
property_get("ro.product.name", &product_name, &data_2ea3);
int32_t x8_3 = data_501c;
int32_t x9_7;
if (strcmp(&product_name, "if_s700n") == 0)
{
x9_7 = 0x13312123;
}
else
{
x9_7 = 0;
}
if (x8_3 == x9_7)
{
char infr_usb;
property_get("sys.infr.usb", &infr_usb, "0");
is_infr_usb_enable = infr_usb;
property_set();
}
data_501c = 0;
data_5018 = 0;
}
if ((x19 - x19) != 0)
{
__stack_chk_fail();
/* no return */
}
}
int64_t open_keyboard(char* arg1)
{
int64_t x21 = *__stack_chk_guard;
int32_t var_1150 = 1;
void var_1148;
memset(&var_1148, 0, 0x1000);
SpecialCharacterFilter(arg1, &var_1148, 0x1000);
int32_t input = open();
int32_t x0_4;
int32_t x0_12;
int64_t result;
void* const error_message;
int64_t x4_1;
if ((input & 0x80000000) != 0)
{
x4_1 = strerror(*__errno());
error_message = "could not open %s, %s\n";
}
else
{
void var_114c;
x0_4 = ioctl(input, 0x80044501, &var_114c);
if (x0_4 == 0)
{
void var_1158;
x0_12 = ioctl(input, 0x80084502, &var_1158);
if (x0_12 != 0)
{
x4_1 = strerror(*__errno());
error_message = "could not get driver id for %s, …";
}
else
{
char var_59_1 = 0;
char var_a9_1 = 0;
char var_f9_1 = 0;
char var_a8;
if (ioctl(input, 0x804f4506, &var_a8) <= 0)
{
var_a8 = 0;
}
char var_f8;
if (ioctl(input, 0x804f4507, &var_f8) <= 0)
{
var_f8 = 0;
}
char var_148;
if (ioctl(input, 0x804f4508, &var_148) <= 0)
{
var_148 = 0;
}
if (ioctl(input, 0x400445a0, &var_1150) != 0)
{
__android_log_print(6, "KeyEvent", "Can't enable monotonic clock rep…", strerror(*__errno()));
}
int64_t x0_29 = realloc(data_5030, ((data_5038 << 3) + 8));
int64_t x0_31;
if (x0_29 != 0)
{
int64_t x9_1 = data_5038;
int64_t x8_5 = data_5040;
data_5030 = x0_29;
x0_31 = realloc(x8_5, ((x9_1 << 3) + 8));
if (x0_31 != 0)
{
int64_t x8_6 = data_5038;
int64_t x9_3 = data_5030;
data_5040 = x0_31;
int32_t* x8_7 = (x9_3 + (x8_6 << 3));
*x8_7 = input;
x8_7[1] = 1;
int64_t x9_4 = data_5038;
result = 0;
*(data_5040 + (x9_4 << 3)) = strdup(arg1);
data_5038 = (x9_4 + 1);
}
}
if ((x0_29 == 0 || (x0_29 != 0 && x0_31 == 0)))
{
__android_log_print(6, "KeyEvent", "out of memory\n");
result = 0xffffffff;
}
}
}
else
{
x4_1 = strerror(*__errno());
error_message = "could not get driver version for…";
}
}
if ((((input & 0x80000000) != 0 || ((input & 0x80000000) == 0 && x0_4 != 0)) || (((input & 0x80000000) == 0 && x0_4 == 0) && x0_12 != 0)))
{
__android_log_print(6, "KeyEvent", error_message, arg1, x4_1);
result = 0xffffffff;
}
if ((x21 - x21) != 0)
{
__stack_chk_fail();
/* no return */
}
return result;
}
int64_t read_keyboard_events(char* arg1, int32_t arg2)
{
int64_t x21 = *__stack_chk_guard;
int64_t var_68 = x21;
void var_1268;
int32_t x19 = __read_chk(arg2, &var_1268, 0x200, 0x200);
int64_t result;
if (x19 > 0xf)
{
void var_1068;
strncpy(&var_1068, arg1, 0x1000);
void* x20_1 = (&var_1068 + strnlen(&var_1068, 0x1000));
int32_t x25_1 = 0;
*x20_1 = 0x2f;
void* x20_2 = (x20_1 + 1);
do
{
void* x28_1 = (&var_1268 + x25_1);
if (*(x28_1 + 0xc) != 0)
{
strncpy(x20_2, (x28_1 + 0x10), (0x1000 - strnlen(&var_1068, 0x1000)));
if ((*(x28_1 + 5) & 1) != 0)
{
open_keyboard(&var_1068);
}
else
{
int64_t x22_1 = data_5038;
if (x22_1 >= 2)
{
int64_t x26_1 = data_5040;
int64_t x27_1 = 1;
do
{
int64_t x21_2 = *(x26_1 + (x27_1 << 3));
if (strcmp(x21_2, &var_1068) == 0)
{
free(x21_2);
int64_t x26_2 = (x27_1 << 3);
int64_t x21_3 = (((x22_1 + (!x27_1)) << 0x20) >> 0x1d);
int64_t x0_14 = (data_5040 + x26_2);
memmove(x0_14, (x0_14 + 8), x21_3);
int64_t x0_15 = (data_5030 + x26_2);
memmove(x0_15, (x0_15 + 8), x21_3);
data_5038 = (data_5038 - 1);
break;
}
x27_1 = (x27_1 + 1);
} while (x22_1 > x27_1);
}
}
}
x19 = (x19 - (*(x28_1 + 0xc) + 0x10));
x25_1 = ((*(x28_1 + 0xc) + 0x10) + x25_1);
} while (x19 > 0xf);
result = 0;
x21 = *__stack_chk_guard;
}
else
{
int32_t* error_number = __errno();
if (*error_number != 4)
{
__android_log_print(6, "KeyEvent", "could not get event, %s\n", strerror(*error_number));
result = 1;
}
else
{
result = 0;
}
}
if ((x21 - var_68) != 0)
{
__stack_chk_fail();
/* no return */
}
return result;
}
int64_t open_dir(char* arg1)
{
int64_t x22 = *__stack_chk_guard;
DIR* x0 = opendir(arg1);
int64_t result;
if (x0 == 0)
{
result = 0xffffffff;
}
else
{
void var_1048;
strncpy(&var_1048, arg1, 0x1000);
void* x20_1 = (&var_1048 + strnlen(&var_1048, 0x1000));
*x20_1 = 0x2f;
void* x20_2 = (x20_1 + 1);
void* x0_5 = readdir(x0);
if (x0_5 != 0)
{
do
{
char* x21_2 = (x0_5 + 0x13);
if (*x21_2 != 0x2e)
{
label_29cc:
strncpy(x20_2, x21_2, (0x1000 - strnlen(&var_1048, 0x1000)));
open_keyboard(&var_1048);
}
else
{
uint32_t x8_2 = *(x0_5 + 0x14);
if (x8_2 != 0)
{
if (x8_2 != 0x2e)
{
goto label_29cc;
}
if (*(x0_5 + 0x15) != 0)
{
goto label_29cc;
}
}
}
x0_5 = readdir(x0);
} while (x0_5 != 0);
}
closedir(x0);
result = 0;
}
if ((x22 - x22) != 0)
{
__stack_chk_fail();
/* no return */
}
return result;
}
int32_t main(int32_t argc, char** argv, char** envp)
{
int64_t x27 = *__stack_chk_guard;
int64_t var_68 = x27;
set_shutdownreceiver();
__bss_start = init_keyevent_socket();
data_5027 = 0;
data_5018 = 0;
key_power_pressed = 0;
key_mute_pressed = 0;
key_play_pause_pressed = 0;
key_vol_up_pressed = 0;
key_vol_down_pressed = 0;
is_factory_reset = 0;
is_demo_mode = 0;
data_5026 = 0;
factory_event_count = 0;
data_5038 = 1;
data_5030 = calloc(1, 8);
//keycode (デバイスツリーからhexに変換)
key_vol_up = 0x73;
key_vol_down = 0x72;
key_play_pause = 0xA4;
key_mute = 0x5b;
key_power = 0x74;
int64_t x8 = data_5030;
*x8 = inotify_init();
*(x8 + 4) = 1;
if ((inotify_add_watch() & 0x80000000) != 0)
{
__android_log_print(6, "KeyEvent", "could not add watch for %s, %s\n", "/dev/input", strerror(*__errno()));
}
else if ((open_dir("/dev/input") & 0x80000000) != 0)
{
__android_log_print(6, "KeyEvent", "scan dir failed for %s\n", "/dev/input");
}
else
{
while (true)
{
int32_t x2;
if ((is_factory_reset & 1) != 0)
{
x2 = 0x1388;
}
else if ((is_demo_mode & 1) == 0)
{
x2 = -1;
factory_event_count = 0;
}
else
{
x2 = 0x1388;
}
if (__poll_chk(data_5030, data_5038, x2, -1) == 0)
{
if (is_factory_reset == 1)
{
__android_log_print(4, "KeyEvent", "Factory Event Count :%d\n", factory_event_count);
sub_18b8(1);
usleep(0x64);//sleep 100ms
sub_18b8(0);
int32_t factory_event_count_tmp = factory_event_count;
factory_event_count = (factory_event_count_tmp + 1);
if (factory_event_count_tmp >= 1)
{
__android_log_print(4, "KeyEvent", "Factory Reset!!!");
run_factory_reset();
}
}
if (is_demo_mode == 1)
{
__android_log_print(4, "KeyEvent", "=========================DEMO MO…");
if ((sub_1ed8(__bss_start, 0x65) & 0x80000000) != 0)
{
int32_t x0_9 = init_keyevent_socket();
__bss_start = x0_9;
sub_1ed8(x0_9, 0x65);
}
}
}
int64_t x8_6 = data_5030;
if ((*(x8_6 + 6) & 1) != 0)
{
read_keyboard_events("/dev/input", *x8_6);
}
int64_t x28_1 = 0;
int64_t x21_1 = 1;
if (data_5038 >= 2)
{
int32_t x0_11;
void* x8_9;
do
{
x8_9 = (data_5030 + x28_1);
if ((*(x8_9 + 0xe) & 1) != 0)
{
void var_80;
x0_11 = __read_chk(*(x8_9 + 8), &var_80, 0x18, 0x18);
if (x0_11 <= 0x17)
{
break;
}
int16_t var_6e;
uint32_t x9_4 = var_6e;
int32_t var_6c;
if (x9_4 <= key_vol_down)
{
if (x9_4 == key_mute)
{
if (var_6c == 0)
{
key_mute_pressed = 0;
}
else
{
key_mute_pressed = 1;
}
}
else if (x9_4 == key_vol_down)
{
if (var_6c == 0)
{
key_vol_down_pressed = 0;
}
else
{
key_vol_down_pressed = 1;
}
}
}
else if (x9_4 == key_vol_up)
{
if (var_6c == 0)
{
key_vol_up_pressed = 0;
}
else
{
key_vol_up_pressed = 1;
}
}
else if (x9_4 == key_play_pause)
{
if (var_6c == 0)
{
key_play_pause_pressed = 0;
}
else
{
key_play_pause_pressed = 1;
}
}
else if (x9_4 == key_power)
{
if (var_6c == 0)
{
key_power_pressed = 0;
}
else
{
key_power_pressed = 1;
}
}
sub_2108();
if (data_5026 == 1)
{
sub_228c();
}
}
x21_1 = (x21_1 + 1);
x28_1 = (x28_1 + 8);
} while (x21_1 < data_5038);
if (((*(x8_9 + 0xe) & 1) != 0 && x0_11 <= 0x17))
{
break;
}
}
}
__android_log_print(6, "KeyEvent", "could not get event\n");
x27 = *__stack_chk_guard;
}
if ((x27 - var_68) != 0)
{
__stack_chk_fail();
/* no return */
}
return 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment