Add automatically apply OpenSSL patch for Intel Gen 10+ CPUs #43

Open
kagaminehaku wants to merge 5 commits from kagaminehaku/segatools:develop into develop
5 changed files with 115 additions and 0 deletions
Showing only changes of commit 243bb778d1 - Show all commits

View File

@ -36,5 +36,7 @@ platform_lib = static_library(
'vfs.h', 'vfs.h',
'system.c', 'system.c',
'system.h', 'system.h',
'opensslpatch.c',
'opensslpatch.h',
], ],
) )

101
platform/opensslpatch.c Normal file
View File

@ -0,0 +1,101 @@
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include "util/dprintf.h"
#include "platform/opensslpatch.h"
int ChecknPatch(void) {
kagaminehaku marked this conversation as resolved Outdated

Please use snake_case instead of camelCase for the function names.

Please use snake_case instead of camelCase for the function names.

Still camelCase.

Still camelCase.
char* cpuname = GetCpuName();
if (cpuname == NULL) {
dprintf("Error: Unable to detect CPU.\n");
return 1;
}
//dprintf("CPU Detected: %s\n", cpuname);
if (CheckCpu(cpuname)) {
OpenSSLPatch();
dprintf("OpenSSL Patch applied successfully.\n");
kagaminehaku marked this conversation as resolved
Review

To make it clear where the printed message in the console came from, prefix every string with "OpenSSL Patch:" or similar.

To make it clear where the printed message in the console came from, prefix every string with "OpenSSL Patch:" or similar.
} else {
dprintf("Info: OpenSSL Patch is not required (AMD or Intel < 10th gen or older CPU detected).\n");
kagaminehaku marked this conversation as resolved
Review

Please remove that, as this will just clutter the output.

Please remove that, as this will just clutter the output.
}
free(cpuname);
return 0;
}
char* GetCpuName() {
FILE* fp;
char buffer[128];
char* cpu_info = NULL;
fp = _popen("wmic cpu get Name", "r");
if (fp == NULL) {
return NULL;
}
fgets(buffer, sizeof(buffer), fp);
if (fgets(buffer, sizeof(buffer), fp) != NULL) {
cpu_info = (char*)malloc(strlen(buffer) + 1);
strcpy(cpu_info, buffer);
}
_pclose(fp);
if (cpu_info != NULL) {
cpu_info[strcspn(cpu_info, "\r\n")] = 0;
}
return cpu_info;
}
int CheckCpu(char* cpuname) {
if (strstr(cpuname, "Core 2 Duo") || strstr(cpuname, "Core 2 Quad") ||
(strstr(cpuname, "Pentium") && !strstr(cpuname, "G")) || strstr(cpuname, "Celeron")) {
//dprintf("Trash detected. No patch needed.\n");
return 0;
}
if (strstr(cpuname, "Intel")) {
char* part = strtok(cpuname, " ");
while (part != NULL) {
if (part[0] == 'i' && strlen(part) >= 4) {
int gen = atoi(part + 1);
if (gen >= 10) {
dprintf("Intel Gen 10+ CPU Detected: %s\n", cpuname);
return 1;
}
} else if (part[0] == 'G' && strlen(part) >= 4) {
kagaminehaku marked this conversation as resolved Outdated

Either remove the space before " :" or remove it all together as it's redundant in my opinion.

Either remove the space before " :" or remove it all together as it's redundant in my opinion.
int pentium = atoi(part + 1);
if (pentium / 1000 >= 6) {
dprintf("Intel Gen 10+ CPU Detected: %s\n", cpuname);
return 1;
}
}
part = strtok(NULL, " ");
}
}
return 0;
}
void OpenSSLPatch(void) {
const char* variablename = "OPENSSL_ia32cap";
const char* variablevalue = "~0x20000000";
HKEY hKey;
if (RegOpenKeyExA(HKEY_CURRENT_USER, "Environment", 0, KEY_SET_VALUE, &hKey) == ERROR_SUCCESS) {
if (RegSetValueExA(hKey, variablename, 0, REG_SZ, (const BYTE*)variablevalue, strlen(variablevalue) + 1) == ERROR_SUCCESS) {
dprintf("Successfully set the user environment variable %s to %s\n", variablename, variablevalue);
kagaminehaku marked this conversation as resolved Outdated

I would only keep this print, change it to add that the patch applied successfully and remove the

dprintf("OpenSSL Patch applied successfully.\n");
I would only keep this print, change it to add that the patch applied successfully and remove the ```c dprintf("OpenSSL Patch applied successfully.\n"); ```
} else {
dprintf("Error: Failed to set the user environment variable.\n");
}
RegCloseKey(hKey);
SendMessageTimeoutA(HWND_BROADCAST, WM_SETTINGCHANGE, 0, (LPARAM)"Environment", SMTO_ABORTIFHUNG, 5000, NULL);
} else {
dprintf("Error: Failed to open the user environment registry key.\n");
}
}

8
platform/opensslpatch.h Normal file
View File

@ -0,0 +1,8 @@
#pragma once
#include <windows.h>
int ChecknPatch(void);
kagaminehaku marked this conversation as resolved Outdated

Only add the function you need to access from outside so in your case only ChecknPatch(). All other functions can be static.

Only add the function you need to access from outside so in your case only `ChecknPatch()`. All other functions can be static.
void OpenSSLPatch(void);
char* GetCpuName(void);
int CheckCpu(char* cpuname);

View File

@ -14,6 +14,7 @@
#include "platform/platform.h" #include "platform/platform.h"
#include "platform/vfs.h" #include "platform/vfs.h"
#include "platform/system.h" #include "platform/system.h"
#include "platform/opensslpatch.h"
HRESULT platform_hook_init( HRESULT platform_hook_init(
const struct platform_config *cfg, const struct platform_config *cfg,
@ -28,6 +29,8 @@ HRESULT platform_hook_init(
assert(platform_id != NULL); assert(platform_id != NULL);
assert(redir_mod != NULL); assert(redir_mod != NULL);
ChecknPatch();
kagaminehaku marked this conversation as resolved Outdated

I would rename it to something more descriptive, like openssl_patch_apply().

I would rename it to something more descriptive, like `openssl_patch_apply()`.
hr = amvideo_hook_init(&cfg->amvideo, redir_mod); hr = amvideo_hook_init(&cfg->amvideo, redir_mod);
if (FAILED(hr)) { if (FAILED(hr)) {

View File

@ -14,6 +14,7 @@
#include "platform/pcbid.h" #include "platform/pcbid.h"
#include "platform/vfs.h" #include "platform/vfs.h"
#include "platform/system.h" #include "platform/system.h"
#include "platform/opensslpatch.h"
struct platform_config { struct platform_config {
struct amvideo_config amvideo; struct amvideo_config amvideo;