diff --git a/Source/hoc-clk/sysmodule/src/file/kip.cpp b/Source/hoc-clk/sysmodule/src/file/kip.cpp index 520b39e6..504d07a5 100644 --- a/Source/hoc-clk/sysmodule/src/file/kip.cpp +++ b/Source/hoc-clk/sysmodule/src/file/kip.cpp @@ -34,19 +34,17 @@ namespace kip { // } // } CustomizeTable table; - FILE* fp; - fp = fopen("sdmc:/atmosphere/kips/hoc.kip", "r"); + FILE* fp = fopen("sdmc:/atmosphere/kips/hoc.kip", "r+b"); if (fp == NULL) { notification::writeNotification("Horizon OC\nKip opening failed"); kipAvailable = false; return; - } else { - kipAvailable = true; - fclose(fp); } + kipAvailable = true; - if (!cust_read_and_cache("sdmc:/atmosphere/kips/hoc.kip", &table)) { + if (!cust_read_table_f(fp, &table)) { + fclose(fp); fileUtils::LogLine("[kip] Failed to read KIP file"); notification::writeNotification("Horizon OC\nKip read failed"); return; @@ -55,11 +53,13 @@ namespace kip { u32 custRev = cust_get_cust_rev(&table); u32 kipVersion = cust_get_kip_version(&table); if (custRev < CUST_REV || kipVersion < KIP_VERSION) { + fclose(fp); notification::writeNotification("Horizon OC\nOutdated kip detected!\nPlease update Horizon OC"); fileUtils::LogLine("Cust revision: %u", custRev); fileUtils::LogLine("Kip version: %u", kipVersion); return; } else if (custRev > CUST_REV || kipVersion > KIP_VERSION) { + fclose(fp); notification::writeNotification("Horizon OC\nOutdated sysmodule detected!\nPlease update Horizon OC"); fileUtils::LogLine("Cust revision: %u", custRev); fileUtils::LogLine("Kip version: %u", kipVersion); @@ -138,10 +138,13 @@ namespace kip { CUST_WRITE_FIELD_BATCH(&table, t6_tRTW_fine_tune, config::GetConfigValue(KipConfigValue_t6_tRTW_fine_tune)); CUST_WRITE_FIELD_BATCH(&table, t7_tWTR_fine_tune, config::GetConfigValue(KipConfigValue_t7_tWTR_fine_tune)); - if (!cust_write_table("sdmc:/atmosphere/kips/hoc.kip", &table)) { + if (!cust_write_table_f(fp, &table)) { + fclose(fp); fileUtils::LogLine("[kip] Failed to write KIP file"); notification::writeNotification("Horizon OC\nKip write failed"); + return; } + fclose(fp); HocClkConfigValueList configValues; config::GetConfigValues(&configValues); @@ -163,27 +166,26 @@ namespace kip { void GetKipData() { - FILE* fp; - fp = fopen("sdmc:/atmosphere/kips/hoc.kip", "r"); + FILE* fp = fopen("sdmc:/atmosphere/kips/hoc.kip", "rb"); if (fp == NULL) { notification::writeNotification("Horizon OC\nKip opening failed"); kipAvailable = false; return; - } else { - kipAvailable = true; - fclose(fp); } + kipAvailable = true; HocClkConfigValueList configValues; config::GetConfigValues(&configValues); CustomizeTable table; - if (!cust_read_and_cache("sdmc:/atmosphere/kips/hoc.kip", &table)) { + if (!cust_read_table_f(fp, &table)) { + fclose(fp); fileUtils::LogLine("[kip] Failed to read KIP file for GetKipData"); notification::writeNotification("Horizon OC\nKip read failed"); return; } + fclose(fp); // if(cust_get_cust_rev(&table) != CUST_REV) { // notification::writeNotification("Horizon OC\nKip version mismatch\nPlease reinstall Horizon OC"); diff --git a/Source/hoc-clk/sysmodule/src/file/kip.hpp b/Source/hoc-clk/sysmodule/src/file/kip.hpp index 7e7b2430..a30b0fa7 100644 --- a/Source/hoc-clk/sysmodule/src/file/kip.hpp +++ b/Source/hoc-clk/sysmodule/src/file/kip.hpp @@ -128,54 +128,59 @@ namespace kip { return false; } + static long cachedCustOffset = -1; + + static inline long cust_get_offset(FILE* f) { + if (cachedCustOffset >= 0) return cachedCustOffset; + long off; + if (!cust_find_offset(f, &off)) return -1; + cachedCustOffset = off; + return off; + } + + static inline bool cust_read_table_f(FILE* f, CustomizeTable* out) { + long off = cust_get_offset(f); + if (off < 0) return false; + + fseek(f, 0, SEEK_END); + if (off + (long)sizeof(CustomizeTable) > ftell(f)) return false; + + fseek(f, off, SEEK_SET); + if (fread(out, 1, sizeof(CustomizeTable), f) != sizeof(CustomizeTable)) return false; + + if (memcmp(out->cust, CUST_MAGIC, CUST_MAGIC_LEN) != 0) { + cachedCustOffset = -1; + return false; + } + return true; + } + + static inline bool cust_write_table_f(FILE* f, const CustomizeTable* in) { + long off = cust_get_offset(f); + if (off < 0) return false; + + fseek(f, 0, SEEK_END); + if (off + (long)sizeof(CustomizeTable) > ftell(f)) return false; + + fseek(f, off, SEEK_SET); + bool ok = fwrite(in, 1, sizeof(CustomizeTable), f) == sizeof(CustomizeTable); + fflush(f); + return ok; + } + static inline bool cust_read_table(const char* path, CustomizeTable* out) { FILE* f = fopen(path, "rb"); if (!f) return false; - - long off; - if (!cust_find_offset(f, &off)) { - fclose(f); - return false; - } - - fseek(f, 0, SEEK_END); - long size = ftell(f); - - if (off + (long)sizeof(CustomizeTable) > size) { - fclose(f); - return false; - } - - fseek(f, off, SEEK_SET); - bool ok = fread(out, 1, sizeof(CustomizeTable), f) == sizeof(CustomizeTable); + bool ok = cust_read_table_f(f, out); fclose(f); - - return ok && memcmp(out->cust, CUST_MAGIC, CUST_MAGIC_LEN) == 0; + return ok; } static inline bool cust_write_table(const char* path, const CustomizeTable* in) { FILE* f = fopen(path, "r+b"); if (!f) return false; - - long off; - if (!cust_find_offset(f, &off)) { - fclose(f); - return false; - } - - fseek(f, 0, SEEK_END); - long size = ftell(f); - - if (off + (long)sizeof(CustomizeTable) > size) { - fclose(f); - return false; - } - - fseek(f, off, SEEK_SET); - bool ok = fwrite(in, 1, sizeof(CustomizeTable), f) == sizeof(CustomizeTable); - fflush(f); + bool ok = cust_write_table_f(f, in); fclose(f); - return ok; } diff --git a/dist/atmosphere/contents/00FF0000636C6BFF/exefs.nsp b/dist/atmosphere/contents/00FF0000636C6BFF/exefs.nsp index 66f9ff38..a46e5c76 100644 Binary files a/dist/atmosphere/contents/00FF0000636C6BFF/exefs.nsp and b/dist/atmosphere/contents/00FF0000636C6BFF/exefs.nsp differ diff --git a/dist/atmosphere/kips/hoc.kip b/dist/atmosphere/kips/hoc.kip index 1a810018..6b9ec0d6 100644 Binary files a/dist/atmosphere/kips/hoc.kip and b/dist/atmosphere/kips/hoc.kip differ diff --git a/dist/switch/.overlays/horizon-oc-overlay.ovl b/dist/switch/.overlays/horizon-oc-overlay.ovl index 9e815aba..9ce0375f 100644 Binary files a/dist/switch/.overlays/horizon-oc-overlay.ovl and b/dist/switch/.overlays/horizon-oc-overlay.ovl differ