cs: implement GetFirmwareVersion command

This commit is contained in:
Michael Scire 2021-03-17 21:46:32 -07:00
parent 6ea5cf5f91
commit 6794c147dd
7 changed files with 171 additions and 18 deletions

View file

@ -25,6 +25,8 @@ namespace ams::cs {
class CommandProcessor : public scs::CommandProcessor { class CommandProcessor : public scs::CommandProcessor {
public: public:
virtual bool ProcessCommand(const CommandHeader &header, const u8 *body, s32 socket) override; virtual bool ProcessCommand(const CommandHeader &header, const u8 *body, s32 socket) override;
private:
static void SendFirmwareVersion(s32 socket, const CommandHeader &header);
}; };
} }

View file

@ -59,7 +59,12 @@ namespace ams::scs {
Response_None = 0, Response_None = 0,
Response_Success = 1, Response_Success = 1,
Response_Error = 2, Response_Error = 2,
/* ... */ Response_ProgramExited = 3,
Response_FirmwareVersion = 4,
Response_JitDebug = 5,
Response_ProgramLaunched = 6,
Response_TitleName = 7,
Response_ScreenShot = 8,
}; };
public: public:
constexpr CommandProcessor() = default; constexpr CommandProcessor() = default;
@ -68,16 +73,20 @@ namespace ams::scs {
public: public:
virtual bool ProcessCommand(const CommandHeader &header, const u8 *body, s32 socket); virtual bool ProcessCommand(const CommandHeader &header, const u8 *body, s32 socket);
protected: protected:
static std::scoped_lock<os::SdkMutex> MakeSendGuardBlock();
static void Send(s32 socket, const void *data, size_t size);
static void SendSuccess(s32 socket, const CommandHeader &header); static void SendSuccess(s32 socket, const CommandHeader &header);
static void SendErrorResult(s32 socket, const CommandHeader &header, Result result); static void SendErrorResult(s32 socket, const CommandHeader &header, Result result);
private: private:
static void SendErrorResult(s32 socket, u64 id, Result result); static void SendErrorResult(s32 socket, u64 id, Result result);
static void SendExited(s32 socket, u64 id, u64 process_id);
static void SendJitDebug(s32 socket, u64 id);
static void SendLaunched(s32 socket, u64 id, u64 process_id);
static void OnProcessStart(u64 id, s32 socket, os::ProcessId process_id); static void OnProcessStart(u64 id, s32 socket, os::ProcessId process_id);
static void OnProcessExit(u64 id, s32 socket, os::ProcessId process_id); static void OnProcessExit(u64 id, s32 socket, os::ProcessId process_id);
static void OnProcessJitDebug(u64 id, s32 socket, os::ProcessId process_id); static void OnProcessJitDebug(u64 id, s32 socket, os::ProcessId process_id);
}; };
os::SdkMutex &GetHtcsSendMutex();
} }

View file

@ -0,0 +1,26 @@
/*
* Copyright (c) 2018-2020 Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stratosphere.hpp>
#include "cs_command_impl.hpp"
namespace ams::cs {
Result DoGetFirmwareVersionCommand(settings::system::FirmwareVersion *out) {
settings::system::GetFirmwareVersion(out);
return ResultSuccess();
}
}

View file

@ -0,0 +1,23 @@
/*
* Copyright (c) 2018-2020 Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <stratosphere.hpp>
namespace ams::cs {
Result DoGetFirmwareVersionCommand(settings::system::FirmwareVersion *out);
}

View file

@ -14,11 +14,24 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <stratosphere.hpp> #include <stratosphere.hpp>
#include "cs_command_impl.hpp"
namespace ams::cs { namespace ams::cs {
namespace {
struct ResponseFirmwareVersion {
ResponseHeader header;
settings::system::FirmwareVersion firmware_version;
};
}
bool CommandProcessor::ProcessCommand(const CommandHeader &header, const u8 *body, s32 socket) { bool CommandProcessor::ProcessCommand(const CommandHeader &header, const u8 *body, s32 socket) {
switch (header.command) { switch (header.command) {
case Command_GetFirmwareVersion:
SendFirmwareVersion(socket, header);
break;
/* TODO: Command support. */ /* TODO: Command support. */
default: default:
scs::CommandProcessor::ProcessCommand(header, body, socket); scs::CommandProcessor::ProcessCommand(header, body, socket);
@ -28,4 +41,26 @@ namespace ams::cs {
return true; return true;
} }
void CommandProcessor::SendFirmwareVersion(s32 socket, const CommandHeader &header) {
/* Build the response. */
ResponseFirmwareVersion response = {
.header = {
.id = header.id,
.response = Response_FirmwareVersion,
.body_size = sizeof(response) - sizeof(response.header),
},
.firmware_version = {},
};
/* Get the firmware version. */
const Result result = DoGetFirmwareVersionCommand(std::addressof(response.firmware_version));
if (R_SUCCEEDED(result)) {
/* Send the response. */
auto lk = MakeSendGuardBlock();
Send(socket, std::addressof(response), sizeof(response));
} else {
SendErrorResult(socket, header, result);
}
}
} }

View file

@ -24,12 +24,26 @@ namespace ams::scs {
u32 result; u32 result;
}; };
struct ResponseProgramExited {
ResponseHeader header;
u64 process_id;
};
struct ResponseProgramLaunched {
ResponseHeader header;
u64 process_id;
};
constinit os::SdkMutex g_htcs_send_mutex; constinit os::SdkMutex g_htcs_send_mutex;
} }
os::SdkMutex &GetHtcsSendMutex() { std::scoped_lock<os::SdkMutex> CommandProcessor::MakeSendGuardBlock() {
return g_htcs_send_mutex; return std::scoped_lock<os::SdkMutex>{g_htcs_send_mutex};
}
void CommandProcessor::Send(s32 socket, const void *data, size_t size) {
htcs::Send(socket, data, size, 0);
} }
void CommandProcessor::SendSuccess(s32 socket, const CommandHeader &header) { void CommandProcessor::SendSuccess(s32 socket, const CommandHeader &header) {
@ -41,8 +55,8 @@ namespace ams::scs {
}; };
/* Send the response. */ /* Send the response. */
std::scoped_lock lk(GetHtcsSendMutex()); auto lk = MakeSendGuardBlock();
htcs::Send(socket, std::addressof(response), sizeof(response), 0); Send(socket, std::addressof(response), sizeof(response));
} }
void CommandProcessor::SendErrorResult(s32 socket, const CommandHeader &header, Result result) { void CommandProcessor::SendErrorResult(s32 socket, const CommandHeader &header, Result result) {
@ -61,23 +75,65 @@ namespace ams::scs {
}; };
/* Send the response. */ /* Send the response. */
std::scoped_lock lk(GetHtcsSendMutex()); auto lk = MakeSendGuardBlock();
htcs::Send(socket, std::addressof(response), sizeof(response), 0); Send(socket, std::addressof(response), sizeof(response));
}
void CommandProcessor::SendExited(s32 socket, u64 id, u64 process_id) {
/* Build the response. */
const ResponseProgramExited response = {
.header = {
.id = id,
.response = Response_ProgramExited,
.body_size = sizeof(response) - sizeof(response.header),
},
.process_id = process_id,
};
/* Send the response. */
auto lk = MakeSendGuardBlock();
Send(socket, std::addressof(response), sizeof(response));
}
void CommandProcessor::SendJitDebug(s32 socket, u64 id) {
/* Build the response. */
const ResponseHeader response = {
.id = id,
.response = Response_JitDebug,
.body_size = 0,
};
/* Send the response. */
auto lk = MakeSendGuardBlock();
Send(socket, std::addressof(response), sizeof(response));
}
void CommandProcessor::SendLaunched(s32 socket, u64 id, u64 process_id) {
/* Build the response. */
const ResponseProgramLaunched response = {
.header = {
.id = id,
.response = Response_ProgramLaunched,
.body_size = sizeof(response) - sizeof(response.header),
},
.process_id = process_id,
};
/* Send the response. */
auto lk = MakeSendGuardBlock();
Send(socket, std::addressof(response), sizeof(response));
} }
void CommandProcessor::OnProcessStart(u64 id, s32 socket, os::ProcessId process_id) { void CommandProcessor::OnProcessStart(u64 id, s32 socket, os::ProcessId process_id) {
/* TODO */ SendLaunched(socket, id, process_id.value);
AMS_ABORT("CommandProcessor::OnProcessStart");
} }
void CommandProcessor::OnProcessExit(u64 id, s32 socket, os::ProcessId process_id) { void CommandProcessor::OnProcessExit(u64 id, s32 socket, os::ProcessId process_id) {
/* TODO */ SendExited(socket, id, process_id.value);
AMS_ABORT("CommandProcessor::OnProcessExit");
} }
void CommandProcessor::OnProcessJitDebug(u64 id, s32 socket, os::ProcessId process_id) { void CommandProcessor::OnProcessJitDebug(u64 id, s32 socket, os::ProcessId process_id) {
/* TODO */ SendJitDebug(socket, id);
AMS_ABORT("CommandProcessor::OnProcessJitDebug");
} }
void CommandProcessor::Initialize() { void CommandProcessor::Initialize() {

View file

@ -120,6 +120,7 @@ void __appInit(void) {
lr::Initialize(); lr::Initialize();
R_ABORT_UNLESS(ldr::InitializeForShell()); R_ABORT_UNLESS(ldr::InitializeForShell());
R_ABORT_UNLESS(pgl::Initialize()); R_ABORT_UNLESS(pgl::Initialize());
R_ABORT_UNLESS(setsysInitialize());
/* TODO: Other services? */ /* TODO: Other services? */
ams::CheckApiVersion(); ams::CheckApiVersion();
@ -127,6 +128,7 @@ void __appInit(void) {
void __appExit(void) { void __appExit(void) {
/* TODO: Other services? */ /* TODO: Other services? */
setsysExit();
pgl::Finalize(); pgl::Finalize();
ldr::FinalizeForShell(); ldr::FinalizeForShell();
lr::Finalize(); lr::Finalize();