cs: implement logic for main (linker error paradise, for now)

This commit is contained in:
Michael Scire 2021-03-02 13:35:47 -08:00
parent 0060c1fb49
commit 070e9fd855
20 changed files with 618 additions and 5 deletions

View file

@ -45,6 +45,7 @@
#include <stratosphere/capsrv.hpp>
#include <stratosphere/cfg.hpp>
#include <stratosphere/clkrst.hpp>
#include <stratosphere/cs.hpp>
#include <stratosphere/ddsf.hpp>
#include <stratosphere/dmnt.hpp>
#include <stratosphere/erpt.hpp>
@ -77,6 +78,7 @@
#include <stratosphere/regulator.hpp>
#include <stratosphere/ro.hpp>
#include <stratosphere/settings.hpp>
#include <stratosphere/scs.hpp>
#include <stratosphere/sf.hpp>
#include <stratosphere/sm.hpp>
#include <stratosphere/socket.hpp>

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/cs/cs_audio_server.hpp>
#include <stratosphere/cs/cs_hid_server.hpp>
#include <stratosphere/cs/cs_remote_video_server.hpp>
#include <stratosphere/cs/cs_target_io_server.hpp>
#include <stratosphere/cs/cs_command_processor.hpp>

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 <vapours.hpp>
namespace ams::cs {
void InitializeAudioServer();
}

View file

@ -0,0 +1,30 @@
/*
* 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 <vapours.hpp>
#include <stratosphere/scs/scs_command_processor.hpp>
namespace ams::cs {
using CommandHeader = scs::CommandHeader;
using ResponseHeader = scs::ResponseHeader;
class CommandProcessor : public scs::CommandProcessor {
public:
virtual bool ProcessCommand(const CommandHeader &header, const u8 *body, s32 socket) override;
};
}

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 <vapours.hpp>
namespace ams::cs {
void InitializeHidServer();
}

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 <vapours.hpp>
namespace ams::cs {
void InitializeRemoteVideoServer();
}

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 <vapours.hpp>
namespace ams::cs {
void InitializeTargetIoServer();
}

View file

@ -17,3 +17,7 @@
#include <stratosphere/htc/server/htc_htcmisc_hipc_server.hpp>
#include <stratosphere/htc/server/htc_htcmisc_channel_ids.hpp>
#include <stratosphere/htc/tenv/htc_tenv_types.hpp>
#include <stratosphere/htc/tenv/htc_tenv.hpp>
#include <stratosphere/htc/tenv/htc_tenv_service_manager.hpp>

View file

@ -0,0 +1,25 @@
/*
* 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 <vapours.hpp>
namespace ams::htc::tenv {
void Initialize(AllocateFunction allocate, DeallocateFunction deallocate);
Result RegisterDefinitionFilePath(os::ProcessId process_id, const char *path, size_t size);
void UnregisterDefinitionFilePath(os::ProcessId process_id);
}

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/>.
*/
#pragma once
#include <vapours.hpp>
#include <stratosphere/sf.hpp>
#include <stratosphere/htc/tenv/htc_tenv_types.hpp>
#define AMS_HTC_TENV_I_SERVICE_INTERFACE_INFO(C, H) \
AMS_SF_METHOD_INFO(C, H, 0, Result, GetVariable, (sf::Out<s64> out_size, const sf::OutBuffer &out_buffer, const htc::tenv::VariableName &name), (out_size, out_buffer, name)) \
AMS_SF_METHOD_INFO(C, H, 1, Result, GetVariableLength, (sf::Out<s64> out_size,const htc::tenv::VariableName &name), (out_size, name)) \
AMS_SF_METHOD_INFO(C, H, 2, Result, WaitUntilVariableAvailable, (s64 timeout_ms), (timeout_ms))
AMS_SF_DEFINE_INTERFACE(ams::htc::tenv, IService, AMS_HTC_TENV_I_SERVICE_INTERFACE_INFO)

View file

@ -0,0 +1,25 @@
/*
* 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 <vapours.hpp>
#include <stratosphere/sf.hpp>
#include <stratosphere/os/os_common_types.hpp>
#include <stratosphere/htc/tenv/htc_tenv_i_service.hpp>
#define AMS_HTC_TENV_I_SERVICE_MANAGER_INTERFACE_INFO(C, H) \
AMS_SF_METHOD_INFO(C, H, 0, Result, GetServiceInterface, (sf::Out<sf::SharedPointer<htc::tenv::IService>> out, const sf::ClientProcessId &process_id), (out, process_id))
AMS_SF_DEFINE_INTERFACE(ams::htc::tenv, IServiceManager, AMS_HTC_TENV_I_SERVICE_MANAGER_INTERFACE_INFO)

View file

@ -0,0 +1,31 @@
/*
* 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 <vapours.hpp>
#include <stratosphere/sm/sm_types.hpp>
#include <stratosphere/htc/tenv/htc_tenv_i_service_manager.hpp>
namespace ams::htc::tenv {
constexpr inline sm::ServiceName ServiceName = sm::ServiceName::Encode("htc:tenv");
class ServiceManager {
public:
Result GetServiceInterface(sf::Out<sf::SharedPointer<htc::tenv::IService>> out, const sf::ClientProcessId &process_id);
};
static_assert(htc::tenv::IsIServiceManager<ServiceManager>);
}

View file

@ -0,0 +1,25 @@
/*
* 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 <vapours.hpp>
namespace ams::htc::tenv {
struct VariableName {
char str[0x40];
};
}

View file

@ -0,0 +1,24 @@
/*
* 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/scs/scs_command_processor.hpp>
#include <stratosphere/scs/scs_shell_server.hpp>
#include <stratosphere/scs/scs_shell.hpp>
#include <stratosphere/scs/scs_tenv.hpp>
#include <stratosphere/scs/scs_server_manager.hpp>

View file

@ -0,0 +1,71 @@
/*
* 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 <vapours.hpp>
namespace ams::scs {
struct CommandHeader {
u64 id;
u32 command;
u32 body_size;
};
struct ResponseHeader {
u64 id;
u32 response;
u32 body_size;
};
class CommandProcessor {
protected:
enum Command {
Command_None = 0,
Command_LaunchProgramFromHost = 1,
Command_TerminateProcesses = 2,
Command_GetFirmwareVersion = 3,
Command_Reboot = 4,
Command_SetSafeMode = 5,
Command_RegisterTenvDefinitionFilePath = 6,
Command_TerminateApplication = 7,
Command_Shutdown = 8,
Command_SubscribeProcessEvent = 9,
Command_GetTitleName = 10,
Command_ControlVirtualTemperature = 11,
Command_LaunchInstalledApplication = 12,
Command_LaunchGameCardApplication = 13,
Command_LaunchInstalledSystemProcess = 14,
Command_TakeScreenShot = 15,
Command_TakeForegroundScreenShot = 16,
Command_SimulateGameCardDetection = 17,
Command_SimulateSdCardDetection = 18,
Command_DumpRunningApplication = 19,
};
enum Response {
Response_None = 0,
Response_Success = 1,
/* ... */
};
public:
constexpr CommandProcessor() = default;
void Initialize();
public:
virtual bool ProcessCommand(const CommandHeader &header, const u8 *body, s32 socket);
};
}

View file

@ -0,0 +1,52 @@
/*
* 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 <vapours.hpp>
#include <stratosphere/sf.hpp>
namespace ams::scs {
enum Port {
Port_HtcTenv,
Port_Count,
};
constexpr inline int SessionCount[Port_Count] = {
6,
};
constexpr inline auto MaxSessions = [] {
auto total = 0;
for (const auto sessions : SessionCount) {
total += sessions;
}
return total;
}();
struct ServerOptions {
static constexpr size_t PointerBufferSize = 0;
static constexpr size_t MaxDomains = 6;
static constexpr size_t MaxDomainObjects = 16;
};
class ServerManager final : public sf::hipc::ServerManager<Port_Count, ServerOptions, MaxSessions> {
/* ... */
};
ServerManager *GetServerManager();
void StartServer();
}

View file

@ -0,0 +1,34 @@
/*
* 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 <vapours.hpp>
#include <stratosphere/os.hpp>
#include <stratosphere/ncm/ncm_ids.hpp>
namespace ams::scs {
using ProcessEventHandler = void(*)(s64 command_id, s32 socket, os::ProcessId process_id);
void InitializeShell();
void RegisterCommonProcessEventHandler(ProcessEventHandler on_start, ProcessEventHandler on_exit, ProcessEventHandler on_jit_debug);
bool RegisterSocket(s32 socket);
void UnregisterSocket(s32 socket);
Result SubscribeProcessEvent(s32 socket, bool is_register, s64 command_id);
}

View file

@ -0,0 +1,41 @@
/*
* 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 <vapours.hpp>
#include <stratosphere/os.hpp>
#include <stratosphere/htcs.hpp>
#include <stratosphere/scs/scs_command_processor.hpp>
namespace ams::scs {
class ShellServer {
private:
htcs::HtcsPortName m_port_name;
os::ThreadType m_thread;
u8 m_buffer[64_KB];
CommandProcessor *m_command_processor;
private:
static void ThreadEntry(void *arg) { reinterpret_cast<ShellServer *>(arg)->DoShellServer(); }
void DoShellServer();
public:
constexpr ShellServer() = default;
public:
void Initialize(const char *port_name, void *stack, size_t stack_size, CommandProcessor *command_processor);
void Start();
};
}

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 <vapours.hpp>
namespace ams::scs {
void InitializeTenvServiceManager();
}

View file

@ -65,7 +65,27 @@ namespace ams::cs {
namespace {
/* TODO: Heap? */
alignas(os::ThreadStackAlignment) u8 g_shell_stack[4_KB];
alignas(os::ThreadStackAlignment) u8 g_runner_stack[4_KB];
alignas(os::MemoryPageSize) u8 g_heap_memory[32_KB];
alignas(0x40) u8 g_htcs_buffer[1_KB];
lmem::HeapHandle g_heap_handle;
void *Allocate(size_t size) {
void *mem = lmem::AllocateFromExpHeap(g_heap_handle, size);
return mem;
}
void Deallocate(void *p, size_t size) {
lmem::FreeToExpHeap(g_heap_handle, p);
}
void InitializeHeap() {
g_heap_handle = lmem::CreateExpHeap(g_heap_memory, sizeof(g_heap_memory), lmem::CreateOption_ThreadSafe);
}
}
@ -81,22 +101,32 @@ void __libnx_initheap(void) {
fake_heap_start = (char*)addr;
fake_heap_end = (char*)addr + size;
cs::InitializeHeap();
}
void __appInit(void) {
hos::InitializeForStratosphere();
/* TODO */
fs::SetAllocator(cs::Allocate, cs::Deallocate);
sm::DoWithSession([&]() {
/* TODO */
R_ABORT_UNLESS(fsInitialize());
lr::Initialize();
R_ABORT_UNLESS(ldr::InitializeForShell());
R_ABORT_UNLESS(pgl::Initialize());
/* TODO: Other services? */
});
ams::CheckApiVersion();
}
void __appExit(void) {
/* TODO */
/* TODO: Other services? */
pgl::Finalize();
ldr::FinalizeForShell();
lr::Finalize();
fsExit();
}
namespace ams {
@ -131,13 +161,68 @@ void __libnx_free(void *mem) {
AMS_ABORT("__libnx_free was called");
}
namespace ams::cs {
namespace {
constinit ::ams::cs::CommandProcessor g_command_processor;
constinit ::ams::scs::ShellServer g_shell_server;
constinit ::ams::scs::ShellServer g_runner_server;
constinit sf::UnmanagedServiceObject<htc::tenv::IServiceManager, htc::tenv::ServiceManager> g_tenv_service_manager;
}
}
int main(int argc, char **argv)
{
using namespace ams::cs;
/* Set thread name. */
os::SetThreadNamePointer(os::GetCurrentThread(), AMS_GET_SYSTEM_THREAD_NAME(cs, Main));
AMS_ASSERT(os::GetThreadPriority(os::GetCurrentThread()) == AMS_GET_SYSTEM_THREAD_PRIORITY(cs, Main));
/* TODO */
/* Initialize htcs. */
constexpr auto HtcsSocketCountMax = 6;
const size_t buffer_size = htcs::GetWorkingMemorySize(2 * HtcsSocketCountMax);
AMS_ABORT_UNLESS(sizeof(g_htcs_buffer) >= buffer_size);
htcs::InitializeForSystem(g_htcs_buffer, buffer_size, HtcsSocketCountMax);
/* Initialize audio server. */
cs::InitializeAudioServer();
/* Initialize remote video server. */
cs::InitializeRemoteVideoServer();
/* Initialize hid server. */
cs::InitializeHidServer();
/* Initialize target io server. */
cs::InitializeTargetIoServer();
/* Initialize command processor. */
g_command_processor.Initialize();
/* Setup scs. */
scs::InitializeShell();
/* Setup target environment service. */
scs::InitializeTenvServiceManager();
/* Initialize the shell servers. */
g_shell_server.Initialize("iywys@$cs", g_shell_stack, sizeof(g_shell_stack), std::addressof(g_command_processor));
g_shell_server.Start();
g_runner_server.Initialize("iywys@$csForRunnerTools", g_runner_stack, sizeof(g_runner_stack), std::addressof(g_command_processor));
g_runner_server.Start();
/* Register htc:tenv. */
R_ABORT_UNLESS(scs::GetServerManager()->RegisterObjectForServer(g_tenv_service_manager.GetShared(), htc::tenv::ServiceName, scs::SessionCount[scs::Port_HtcTenv]));
/* Start the scs ipc server. */
scs::StartServer();
return 0;
}