3DSident/source/kernel.cpp

149 lines
4.4 KiB
C++
Raw Permalink Normal View History

2024-09-20 14:50:21 +00:00
#include <3ds.h>
2024-11-07 12:31:45 +00:00
#include <cstring>
2024-09-20 14:50:21 +00:00
#include "fs.h"
#include "kernel.h"
#include "log.h"
2024-09-20 14:50:21 +00:00
#include "system.h"
#include "utils.h"
namespace Kernel {
const char *GetInitalVersion(void) {
Result ret = 0;
FS_Archive archive;
2024-11-07 12:31:45 +00:00
if (R_FAILED(ret = FS::OpenArchive(std::addressof(archive), ARCHIVE_NAND_TWL_FS))) {
Log::Error("%s(FS::OpenArchive) failed: 0x%x\n", __func__, ret);
2024-11-07 12:31:45 +00:00
return "unknown";
}
2024-09-20 14:50:21 +00:00
Handle handle;
if (R_FAILED(ret = FSUSER_OpenFileDirectly(&handle, ARCHIVE_NAND_TWL_FS, fsMakePath(PATH_EMPTY, ""), fsMakePath(PATH_ASCII, "/sys/log/product.log"), FS_OPEN_READ, 0))) {
Log::Error("%s(FSUSER_OpenFileDirectly) failed: 0x%x\n", __func__, ret);
return "unknown";
2024-09-20 14:50:21 +00:00
}
u64 size = 0;
if (R_FAILED(ret = FSFILE_GetSize(handle, std::addressof(size)))) {
Log::Error("%s(FSFILE_GetSize) failed: 0x%x\n", __func__, ret);
return "unknown";
2024-09-20 14:50:21 +00:00
}
char *buf = new char[size + 1];
u32 bytesRead = 0;
if (R_FAILED(ret = FSFILE_Read(handle, std::addressof(bytesRead), 0, reinterpret_cast<u32 *>(buf), static_cast<u32>(size)))) {
Log::Error("%s(FSFILE_Read) failed: 0x%x\n", __func__, ret);
return "unknown";
2024-09-20 14:50:21 +00:00
}
buf[size] = '\0';
2024-11-07 12:31:45 +00:00
// New 3DS/2DS only
static char version[11];
std::strcpy(version, Utils::GetSubstring(buf, "cup:", " preInstall:").c_str());
if (std::strlen(version) == 0) {
std::strcpy(version, Utils::GetSubstring(buf, "cup:", ",").c_str());
2024-09-20 14:50:21 +00:00
}
2024-11-07 12:31:45 +00:00
std::strcat(version, "-");
std::strcat(version, Utils::GetSubstring(buf, "nup:", " cup:").c_str());
2024-09-20 14:50:21 +00:00
if (R_FAILED(ret = FSFILE_Close(handle))) {
Log::Error("%s(FSFILE_Close) failed: 0x%x\n", __func__, ret);
return "unknown";
2024-09-20 14:50:21 +00:00
}
delete[] buf;
FS::CloseArchive(archive);
2024-11-07 12:31:45 +00:00
return version;
2024-09-20 14:50:21 +00:00
}
const char *GetVersion(VersionInfo info) {
Result ret = 0;
u32 osVersion = osGetKernelVersion();
OS_VersionBin *nver = new OS_VersionBin[sizeof(OS_VersionBin)];
OS_VersionBin *cver = new OS_VersionBin[sizeof(OS_VersionBin)];
static char kernelString[128];
static char firmString[128];
static char systemVersionString[128];
std::snprintf(kernelString, 128, "%lu.%lu-%lu",
GET_VERSION_MAJOR(osVersion),
GET_VERSION_MINOR(osVersion),
GET_VERSION_REVISION(osVersion)
);
std::snprintf(firmString, 128, "%lu.%lu-%lu",
GET_VERSION_MAJOR(osVersion),
GET_VERSION_MINOR(osVersion),
GET_VERSION_REVISION(osVersion)
);
if (R_FAILED(ret = osGetSystemVersionDataString(nver, cver, systemVersionString, sizeof(systemVersionString)))) {
Log::Error("%s failed: 0x%x\n", __func__, ret);
std::snprintf(systemVersionString, 128, "unknown");
2024-09-20 14:50:21 +00:00
}
if (info == VERSION_INFO_KERNEL) {
return kernelString;
}
else if (info == VERSION_INFO_FIRM) {
return firmString;
}
return systemVersionString;
}
const char *GetSdmcCid(void) {
Result ret = 0;
u8 buf[16];
if (R_FAILED(ret = FSUSER_GetSdmcCid(buf, 0x10))) {
Log::Error("%s failed: 0x%x\n", __func__, ret);
return "unknown";
2024-09-20 14:50:21 +00:00
}
static char cid[33];
for (int i = 0; i < 16; ++i) {
std::snprintf(cid + i * 2, 3, "%02X", buf[i]);
}
cid[32] = '\0';
return cid;
}
const char *GetNandCid(void) {
Result ret = 0;
u8 buf[16];
if (R_FAILED(ret = FSUSER_GetNandCid(buf, 0x10))) {
Log::Error("%s failed: 0x%x\n", __func__, ret);
return "unknown";
2024-09-20 14:50:21 +00:00
}
static char cid[33];
for (int i = 0; i < 16; ++i) {
std::snprintf(cid + i * 2, 3, "%02X", buf[i]);
}
cid[32] = '\0';
return cid;
}
2024-11-10 21:36:15 +00:00
u32 GetDeviceId(void) {
2024-09-20 14:50:21 +00:00
Result ret = 0;
u32 id = 0;
if (R_FAILED(ret = AM_GetDeviceId(std::addressof(id)))) {
Log::Error("%s failed: 0x%x\n", __func__, ret);
2024-09-20 14:50:21 +00:00
return ret;
}
return id;
}
}