Merge remote-tracking branch 'upstream/master'

This commit is contained in:
B3n30 2017-11-13 10:33:57 +01:00
commit b6dd8ef874
35 changed files with 1568 additions and 265 deletions

View File

@ -44,3 +44,8 @@ deploy:
skip_cleanup: true
on:
tags: true
notifications:
webhooks:
urls:
- https://api.citra-emu.org/code/travis/notify

View File

@ -6,115 +6,150 @@
<rect>
<x>0</x>
<y>0</y>
<width>752</width>
<height>300</height>
<width>616</width>
<height>261</height>
</rect>
</property>
<property name="windowTitle">
<string>About Citra</string>
</property>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="geometry">
<rect>
<x>630</x>
<y>250</y>
<width>101</width>
<height>32</height>
</rect>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Ok</set>
</property>
</widget>
<widget class="QLabel" name="labelLogo">
<property name="geometry">
<rect>
<x>20</x>
<y>20</y>
<width>241</width>
<height>251</height>
</rect>
</property>
<property name="text">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;&lt;img src=&quot;:/icons/citra.png&quot;/&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
</widget>
<widget class="QLabel" name="labelCitra">
<property name="geometry">
<rect>
<x>270</x>
<y>10</y>
<width>100</width>
<height>50</height>
</rect>
</property>
<property name="text">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;&lt;span style=&quot; font-size:28pt;&quot;&gt;Citra&lt;/span&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
</widget>
<widget class="QLabel" name="labelBuildInfo">
<property name="geometry">
<rect>
<x>270</x>
<y>70</y>
<width>451</width>
<height>21</height>
</rect>
</property>
<property name="text">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;%1 | %2-%3&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
</widget>
<widget class="QLabel" name="labelAbout">
<property name="geometry">
<rect>
<x>270</x>
<y>90</y>
<width>461</width>
<height>131</height>
</rect>
</property>
<property name="text">
<string>&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0//EN&quot; &quot;http://www.w3.org/TR/REC-html40/strict.dtd&quot;&gt;
<layout class="QVBoxLayout" name="verticalLayout_3">
<item>
<layout class="QHBoxLayout" name="horizontalLayout" stretch="0,1">
<item>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QLabel" name="labelLogo">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;&lt;img src=&quot;:/icons/citra.png&quot;/&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
</widget>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
<item>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="labelCitra">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;&lt;span style=&quot; font-size:28pt;&quot;&gt;Citra&lt;/span&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="labelBuildInfo">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;%1 | %2-%3&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="labelAbout">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0//EN&quot; &quot;http://www.w3.org/TR/REC-html40/strict.dtd&quot;&gt;
&lt;html&gt;&lt;head&gt;&lt;meta name=&quot;qrichtext&quot; content=&quot;1&quot; /&gt;&lt;style type=&quot;text/css&quot;&gt;
p, li { white-space: pre-wrap; }
&lt;/style&gt;&lt;/head&gt;&lt;body style=&quot; font-family:'MS Shell Dlg 2'; font-size:8pt; font-weight:400; font-style:normal;&quot;&gt;
&lt;p style=&quot; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;span style=&quot; font-size:12pt;&quot;&gt;Citra is a free and open source 3DS emulator &lt;/span&gt;&lt;/p&gt;
&lt;p style=&quot; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;span style=&quot; font-size:12pt;&quot;&gt;licensed under GPLv2.0 or any later version.&lt;/span&gt;&lt;/p&gt;
&lt;p style=&quot;-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;br /&gt;&lt;/p&gt;
&lt;p style=&quot; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;span style=&quot; font-size:12pt;&quot;&gt;This software should not be used to play games &lt;/span&gt;&lt;/p&gt;
&lt;p style=&quot; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;span style=&quot; font-size:12pt;&quot;&gt;you have not legally obtained.&lt;/span&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
</widget>
<widget class="QLabel" name="labelLinks">
<property name="geometry">
<rect>
<x>270</x>
<y>230</y>
<width>361</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;&lt;a href=&quot;https://citra-emu.org/&quot;&gt;&lt;span style=&quot; text-decoration: underline; color:#0000ff;&quot;&gt;Website&lt;/span&gt;&lt;/a&gt; | &lt;a href=&quot;https://community.citra-emu.org/&quot;&gt;&lt;span style=&quot; text-decoration: underline; color:#0000ff;&quot;&gt;Forum&lt;/span&gt;&lt;/a&gt; | &lt;a href=&quot;https://github.com/citra-emu&quot;&gt;&lt;span style=&quot; text-decoration: underline; color:#0000ff;&quot;&gt;Source Code&lt;/span&gt;&lt;/a&gt; | &lt;a href=&quot;//github.com/citra-emu/citra/graphs/contributors&quot;&gt;&lt;span style=&quot; text-decoration: underline; color:#0000ff;&quot;&gt;Contributors&lt;/span&gt;&lt;/a&gt; | &lt;a href=&quot;https://github.com/citra-emu/citra/blob/master/license.txt&quot;&gt;&lt;span style=&quot; text-decoration: underline; color:#0000ff;&quot;&gt;License&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
</widget>
<widget class="QLabel" name="labelLiability">
<property name="geometry">
<rect>
<x>150</x>
<y>270</y>
<width>450</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;&lt;span style=&quot; font-size:7pt;&quot;&gt;&amp;quot;3DS&amp;quot; is a trademark of Nintendo. Citra is not affiliated with Nintendo in any way.&lt;/span&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
</widget>
&lt;/style&gt;&lt;/head&gt;&lt;body style=&quot; font-family:'Ubuntu'; font-size:11pt; font-weight:400; font-style:normal;&quot;&gt;
&lt;p style=&quot; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;span style=&quot; font-family:'MS Shell Dlg 2'; font-size:12pt;&quot;&gt;Citra is a free and open source 3DS emulator licensed under GPLv2.0 or any later version.&lt;/span&gt;&lt;/p&gt;
&lt;p style=&quot;-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-family:'MS Shell Dlg 2'; font-size:8pt;&quot;&gt;&lt;br /&gt;&lt;/p&gt;
&lt;p style=&quot; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;span style=&quot; font-family:'MS Shell Dlg 2'; font-size:12pt;&quot;&gt;This software should not be used to play games you have not legally obtained.&lt;/span&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
<property name="alignment">
<set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter</set>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<spacer name="verticalSpacer_2">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QLabel" name="labelLinks">
<property name="text">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;&lt;a href=&quot;https://citra-emu.org/&quot;&gt;&lt;span style=&quot; text-decoration: underline; color:#0000ff;&quot;&gt;Website&lt;/span&gt;&lt;/a&gt; | &lt;a href=&quot;https://community.citra-emu.org/&quot;&gt;&lt;span style=&quot; text-decoration: underline; color:#0000ff;&quot;&gt;Forum&lt;/span&gt;&lt;/a&gt; | &lt;a href=&quot;https://github.com/citra-emu&quot;&gt;&lt;span style=&quot; text-decoration: underline; color:#0000ff;&quot;&gt;Source Code&lt;/span&gt;&lt;/a&gt; | &lt;a href=&quot;//github.com/citra-emu/citra/graphs/contributors&quot;&gt;&lt;span style=&quot; text-decoration: underline; color:#0000ff;&quot;&gt;Contributors&lt;/span&gt;&lt;/a&gt; | &lt;a href=&quot;https://github.com/citra-emu/citra/blob/master/license.txt&quot;&gt;&lt;span style=&quot; text-decoration: underline; color:#0000ff;&quot;&gt;License&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
<property name="openExternalLinks">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="labelLiability">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;&lt;span style=&quot; font-size:7pt;&quot;&gt;&amp;quot;3DS&amp;quot; is a trademark of Nintendo. Citra is not affiliated with Nintendo in any way.&lt;/span&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
</layout>
</widget>
<resources>
<include location="../../dist/icons/icons.qrc"/>

View File

@ -154,6 +154,9 @@ QString WaitTreeThread::GetText() const {
case THREADSTATUS_WAIT_SLEEP:
status = tr("sleeping");
break;
case THREADSTATUS_WAIT_IPC:
status = tr("waiting for IPC response");
break;
case THREADSTATUS_WAIT_SYNCH_ALL:
case THREADSTATUS_WAIT_SYNCH_ANY:
status = tr("waiting for objects");
@ -182,6 +185,8 @@ QColor WaitTreeThread::GetColor() const {
return QColor(Qt::GlobalColor::darkRed);
case THREADSTATUS_WAIT_SLEEP:
return QColor(Qt::GlobalColor::darkYellow);
case THREADSTATUS_WAIT_IPC:
return QColor(Qt::GlobalColor::darkCyan);
case THREADSTATUS_WAIT_SYNCH_ALL:
case THREADSTATUS_WAIT_SYNCH_ANY:
return QColor(Qt::GlobalColor::red);

View File

@ -24,6 +24,7 @@ set(SRCS
file_sys/archive_selfncch.cpp
file_sys/archive_source_sd_savedata.cpp
file_sys/archive_systemsavedata.cpp
file_sys/cia_container.cpp
file_sys/disk_archive.cpp
file_sys/ivfc_archive.cpp
file_sys/ncch_container.cpp
@ -61,6 +62,7 @@ set(SRCS
hle/kernel/timer.cpp
hle/kernel/vm_manager.cpp
hle/kernel/wait_object.cpp
hle/kernel/ipc.cpp
hle/lock.cpp
hle/romfs.cpp
hle/service/ac/ac.cpp
@ -217,13 +219,16 @@ set(HEADERS
file_sys/archive_selfncch.h
file_sys/archive_source_sd_savedata.h
file_sys/archive_systemsavedata.h
file_sys/cia_container.h
file_sys/directory_backend.h
file_sys/disk_archive.h
file_sys/errors.h
file_sys/file_backend.h
file_sys/ivfc_archive.h
file_sys/ncch_container.h
file_sys/path_parser.h
file_sys/savedata_archive.h
file_sys/title_metadata.h
frontend/camera/blank_camera.h
frontend/camera/factory.h
frontend/camera/interface.h
@ -247,6 +252,7 @@ set(HEADERS
hle/kernel/event.h
hle/kernel/handle_table.h
hle/kernel/hle_ipc.h
hle/kernel/ipc.h
hle/kernel/kernel.h
hle/kernel/memory.h
hle/kernel/mutex.h

View File

@ -35,8 +35,7 @@ public:
return false;
}
ResultVal<size_t> Write(u64 offset, size_t length, bool flush,
const u8* buffer) const override {
ResultVal<size_t> Write(u64 offset, size_t length, bool flush, const u8* buffer) override {
if (offset > size) {
return ERR_WRITE_BEYOND_END;
} else if (offset == size) {

View File

@ -206,7 +206,7 @@ ResultVal<size_t> NCCHFile::Read(const u64 offset, const size_t length, u8* buff
}
ResultVal<size_t> NCCHFile::Write(const u64 offset, const size_t length, const bool flush,
const u8* buffer) const {
const u8* buffer) {
LOG_ERROR(Service_FS, "Attempted to write to NCCH file");
// TODO(shinyquagsire23): Find error code
return MakeResult<size_t>(0);

View File

@ -54,7 +54,7 @@ public:
NCCHFile(std::vector<u8> buffer) : file_buffer(buffer) {}
ResultVal<size_t> Read(u64 offset, size_t length, u8* buffer) const override;
ResultVal<size_t> Write(u64 offset, size_t length, bool flush, const u8* buffer) const override;
ResultVal<size_t> Write(u64 offset, size_t length, bool flush, const u8* buffer) override;
u64 GetSize() const override;
bool SetSize(u64 size) const override;
bool Close() const override {

View File

@ -51,8 +51,7 @@ public:
return MakeResult<size_t>(data->size());
}
ResultVal<size_t> Write(u64 offset, size_t length, bool flush,
const u8* buffer) const override {
ResultVal<size_t> Write(u64 offset, size_t length, bool flush, const u8* buffer) override {
LOG_ERROR(Service_FS, "The file is read-only!");
return ERROR_UNSUPPORTED_OPEN_FLAGS;
}

View File

@ -0,0 +1,228 @@
// Copyright 2017 Citra Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
#include <cinttypes>
#include <cryptopp/sha.h>
#include "common/alignment.h"
#include "common/file_util.h"
#include "common/logging/log.h"
#include "core/file_sys/cia_container.h"
#include "core/file_sys/file_backend.h"
#include "core/loader/loader.h"
////////////////////////////////////////////////////////////////////////////////////////////////////
// FileSys namespace
namespace FileSys {
constexpr u32 CIA_SECTION_ALIGNMENT = 0x40;
Loader::ResultStatus CIAContainer::Load(const FileBackend& backend) {
std::vector<u8> header_data(sizeof(Header));
// Load the CIA Header
ResultVal<size_t> read_result = backend.Read(0, sizeof(Header), header_data.data());
if (read_result.Failed() || *read_result != sizeof(Header))
return Loader::ResultStatus::Error;
Loader::ResultStatus result = LoadHeader(header_data);
if (result != Loader::ResultStatus::Success)
return result;
// Load Title Metadata
std::vector<u8> tmd_data(cia_header.tmd_size);
read_result = backend.Read(GetTitleMetadataOffset(), cia_header.tmd_size, tmd_data.data());
if (read_result.Failed() || *read_result != cia_header.tmd_size)
return Loader::ResultStatus::Error;
result = LoadTitleMetadata(tmd_data);
if (result != Loader::ResultStatus::Success)
return result;
// Load CIA Metadata
if (cia_header.meta_size) {
std::vector<u8> meta_data(sizeof(Metadata));
read_result = backend.Read(GetMetadataOffset(), sizeof(Metadata), meta_data.data());
if (read_result.Failed() || *read_result != sizeof(Metadata))
return Loader::ResultStatus::Error;
result = LoadMetadata(meta_data);
if (result != Loader::ResultStatus::Success)
return result;
}
return Loader::ResultStatus::Success;
}
Loader::ResultStatus CIAContainer::Load(const std::string& filepath) {
FileUtil::IOFile file(filepath, "rb");
if (!file.IsOpen())
return Loader::ResultStatus::Error;
// Load CIA Header
std::vector<u8> header_data(sizeof(Header));
if (file.ReadBytes(header_data.data(), sizeof(Header)) != sizeof(Header))
return Loader::ResultStatus::Error;
Loader::ResultStatus result = LoadHeader(header_data);
if (result != Loader::ResultStatus::Success)
return result;
// Load Title Metadata
std::vector<u8> tmd_data(cia_header.tmd_size);
file.Seek(GetTitleMetadataOffset(), SEEK_SET);
if (file.ReadBytes(tmd_data.data(), cia_header.tmd_size) != cia_header.tmd_size)
return Loader::ResultStatus::Error;
result = LoadTitleMetadata(tmd_data);
if (result != Loader::ResultStatus::Success)
return result;
// Load CIA Metadata
if (cia_header.meta_size) {
std::vector<u8> meta_data(sizeof(Metadata));
file.Seek(GetMetadataOffset(), SEEK_SET);
if (file.ReadBytes(meta_data.data(), sizeof(Metadata)) != sizeof(Metadata))
return Loader::ResultStatus::Error;
result = LoadMetadata(meta_data);
if (result != Loader::ResultStatus::Success)
return result;
}
return Loader::ResultStatus::Success;
}
Loader::ResultStatus CIAContainer::Load(const std::vector<u8>& file_data) {
Loader::ResultStatus result = LoadHeader(file_data);
if (result != Loader::ResultStatus::Success)
return result;
// Load Title Metadata
result = LoadTitleMetadata(file_data, GetTitleMetadataOffset());
if (result != Loader::ResultStatus::Success)
return result;
// Load CIA Metadata
if (cia_header.meta_size) {
result = LoadMetadata(file_data, GetMetadataOffset());
if (result != Loader::ResultStatus::Success)
return result;
}
return Loader::ResultStatus::Success;
}
Loader::ResultStatus CIAContainer::LoadHeader(const std::vector<u8>& header_data, size_t offset) {
if (header_data.size() - offset < sizeof(Header))
return Loader::ResultStatus::Error;
std::memcpy(&cia_header, header_data.data(), sizeof(Header));
return Loader::ResultStatus::Success;
}
Loader::ResultStatus CIAContainer::LoadTitleMetadata(const std::vector<u8>& tmd_data,
size_t offset) {
return cia_tmd.Load(tmd_data, offset);
}
Loader::ResultStatus CIAContainer::LoadMetadata(const std::vector<u8>& meta_data, size_t offset) {
if (meta_data.size() - offset < sizeof(Metadata))
return Loader::ResultStatus::Error;
std::memcpy(&cia_metadata, meta_data.data(), sizeof(Metadata));
return Loader::ResultStatus::Success;
}
const TitleMetadata& CIAContainer::GetTitleMetadata() const {
return cia_tmd;
}
std::array<u64, 0x30>& CIAContainer::GetDependencies() {
return cia_metadata.dependencies;
}
u32 CIAContainer::GetCoreVersion() const {
return cia_metadata.core_version;
}
u64 CIAContainer::GetCertificateOffset() const {
return Common::AlignUp(cia_header.header_size, CIA_SECTION_ALIGNMENT);
}
u64 CIAContainer::GetTicketOffset() const {
return Common::AlignUp(GetCertificateOffset() + cia_header.cert_size, CIA_SECTION_ALIGNMENT);
}
u64 CIAContainer::GetTitleMetadataOffset() const {
return Common::AlignUp(GetTicketOffset() + cia_header.tik_size, CIA_SECTION_ALIGNMENT);
}
u64 CIAContainer::GetMetadataOffset() const {
u64 tmd_end_offset = GetContentOffset();
// Meta exists after all content in the CIA
u64 offset = Common::AlignUp(tmd_end_offset + cia_header.content_size, CIA_SECTION_ALIGNMENT);
return offset;
}
u64 CIAContainer::GetContentOffset(u16 index) const {
u64 offset =
Common::AlignUp(GetTitleMetadataOffset() + cia_header.tmd_size, CIA_SECTION_ALIGNMENT);
for (u16 i = 0; i < index; i++) {
offset += GetContentSize(i);
}
return offset;
}
u32 CIAContainer::GetCertificateSize() const {
return cia_header.cert_size;
}
u32 CIAContainer::GetTicketSize() const {
return cia_header.tik_size;
}
u32 CIAContainer::GetTitleMetadataSize() const {
return cia_header.tmd_size;
}
u32 CIAContainer::GetMetadataSize() const {
return cia_header.meta_size;
}
u64 CIAContainer::GetTotalContentSize() const {
return cia_header.content_size;
}
u64 CIAContainer::GetContentSize(u16 index) const {
// If the content doesn't exist in the CIA, it doesn't have a size.
if (!cia_header.isContentPresent(index))
return 0;
return cia_tmd.GetContentSizeByIndex(index);
}
void CIAContainer::Print() const {
LOG_DEBUG(Service_FS, "Type: %u", static_cast<u32>(cia_header.type));
LOG_DEBUG(Service_FS, "Version: %u\n", static_cast<u32>(cia_header.version));
LOG_DEBUG(Service_FS, "Certificate Size: 0x%08x bytes", GetCertificateSize());
LOG_DEBUG(Service_FS, "Ticket Size: 0x%08x bytes", GetTicketSize());
LOG_DEBUG(Service_FS, "TMD Size: 0x%08x bytes", GetTitleMetadataSize());
LOG_DEBUG(Service_FS, "Meta Size: 0x%08x bytes", GetMetadataSize());
LOG_DEBUG(Service_FS, "Content Size: 0x%08" PRIx64 " bytes\n", GetTotalContentSize());
LOG_DEBUG(Service_FS, "Certificate Offset: 0x%08" PRIx64 " bytes", GetCertificateOffset());
LOG_DEBUG(Service_FS, "Ticket Offset: 0x%08" PRIx64 " bytes", GetTicketOffset());
LOG_DEBUG(Service_FS, "TMD Offset: 0x%08" PRIx64 " bytes", GetTitleMetadataOffset());
LOG_DEBUG(Service_FS, "Meta Offset: 0x%08" PRIx64 " bytes", GetMetadataOffset());
for (u16 i = 0; i < cia_tmd.GetContentCount(); i++) {
LOG_DEBUG(Service_FS, "Content %x Offset: 0x%08" PRIx64 " bytes", i, GetContentOffset(i));
}
}
}

View File

@ -0,0 +1,105 @@
// Copyright 2017 Citra Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
#pragma once
#include <array>
#include <memory>
#include <string>
#include <vector>
#include "common/common_types.h"
#include "common/swap.h"
#include "core/file_sys/title_metadata.h"
namespace Loader {
enum class ResultStatus;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// FileSys namespace
namespace FileSys {
class FileBackend;
constexpr size_t CIA_CONTENT_MAX_COUNT = 0x10000;
constexpr size_t CIA_CONTENT_BITS_SIZE = (CIA_CONTENT_MAX_COUNT / 8);
constexpr size_t CIA_HEADER_SIZE = 0x2020;
constexpr size_t CIA_DEPENDENCY_SIZE = 0x300;
constexpr size_t CIA_METADATA_SIZE = 0x400;
/**
* Helper which implements an interface to read and write CTR Installable Archive (CIA) files.
* Data can either be loaded from a FileBackend, a string path, or from a data array. Data can
* also be partially loaded for CIAs which are downloading/streamed in and need some metadata
* read out.
*/
class CIAContainer {
public:
// Load whole CIAs outright
Loader::ResultStatus Load(const FileBackend& backend);
Loader::ResultStatus Load(const std::string& filepath);
Loader::ResultStatus Load(const std::vector<u8>& header_data);
// Load parts of CIAs (for CIAs streamed in)
Loader::ResultStatus LoadHeader(const std::vector<u8>& header_data, size_t offset = 0);
Loader::ResultStatus LoadTitleMetadata(const std::vector<u8>& tmd_data, size_t offset = 0);
Loader::ResultStatus LoadMetadata(const std::vector<u8>& meta_data, size_t offset = 0);
const TitleMetadata& GetTitleMetadata() const;
std::array<u64, 0x30>& GetDependencies();
u32 GetCoreVersion() const;
u64 GetCertificateOffset() const;
u64 GetTicketOffset() const;
u64 GetTitleMetadataOffset() const;
u64 GetMetadataOffset() const;
u64 GetContentOffset(u16 index = 0) const;
u32 GetCertificateSize() const;
u32 GetTicketSize() const;
u32 GetTitleMetadataSize() const;
u32 GetMetadataSize() const;
u64 GetTotalContentSize() const;
u64 GetContentSize(u16 index = 0) const;
void Print() const;
private:
struct Header {
u32_le header_size;
u16_le type;
u16_le version;
u32_le cert_size;
u32_le tik_size;
u32_le tmd_size;
u32_le meta_size;
u64_le content_size;
std::array<u8, CIA_CONTENT_BITS_SIZE> content_present;
bool isContentPresent(u16 index) const {
// The content_present is a bit array which defines which content in the TMD
// is included in the CIA, so check the bit for this index and add if set.
// The bits in the content index are arranged w/ index 0 as the MSB, 7 as the LSB, etc.
return (content_present[index >> 3] & (0x80 >> (index & 7)));
}
};
static_assert(sizeof(Header) == CIA_HEADER_SIZE, "CIA Header structure size is wrong");
struct Metadata {
std::array<u64_le, 0x30> dependencies;
std::array<u8, 0x180> reserved;
u32_le core_version;
std::array<u8, 0xfc> reserved_2;
};
static_assert(sizeof(Metadata) == CIA_METADATA_SIZE, "CIA Metadata structure size is wrong");
Header cia_header;
Metadata cia_metadata;
TitleMetadata cia_tmd;
};
} // namespace FileSys

View File

@ -25,7 +25,7 @@ ResultVal<size_t> DiskFile::Read(const u64 offset, const size_t length, u8* buff
}
ResultVal<size_t> DiskFile::Write(const u64 offset, const size_t length, const bool flush,
const u8* buffer) const {
const u8* buffer) {
if (!mode.write_flag)
return ERROR_INVALID_OPEN_FLAGS;

View File

@ -28,7 +28,7 @@ public:
}
ResultVal<size_t> Read(u64 offset, size_t length, u8* buffer) const override;
ResultVal<size_t> Write(u64 offset, size_t length, bool flush, const u8* buffer) const override;
ResultVal<size_t> Write(u64 offset, size_t length, bool flush, const u8* buffer) override;
u64 GetSize() const override;
bool SetSize(u64 size) const override;
bool Close() const override;

View File

@ -19,6 +19,7 @@ enum {
FileAlreadyExists = 180,
DirectoryAlreadyExists = 185,
AlreadyExists = 190,
InsufficientSpace = 210,
InvalidOpenFlags = 230,
DirectoryNotEmpty = 240,
NotAFile = 250,
@ -74,6 +75,8 @@ constexpr ResultCode ERROR_COMMAND_NOT_ALLOWED(ErrCodes::CommandNotAllowed, Erro
ErrorSummary::WrongArgument, ErrorLevel::Permanent);
constexpr ResultCode ERROR_EXEFS_SECTION_NOT_FOUND(ErrCodes::ExeFSSectionNotFound, ErrorModule::FS,
ErrorSummary::NotFound, ErrorLevel::Status);
constexpr ResultCode ERROR_INSUFFICIENT_SPACE(ErrCodes::InsufficientSpace, ErrorModule::FS,
ErrorSummary::OutOfResource, ErrorLevel::Status);
/// Returned when a function is passed an invalid archive handle.
constexpr ResultCode ERR_INVALID_ARCHIVE_HANDLE(ErrCodes::ArchiveNotMounted, ErrorModule::FS,

View File

@ -35,8 +35,7 @@ public:
* @param buffer Buffer to read data from
* @return Number of bytes written, or error code
*/
virtual ResultVal<size_t> Write(u64 offset, size_t length, bool flush,
const u8* buffer) const = 0;
virtual ResultVal<size_t> Write(u64 offset, size_t length, bool flush, const u8* buffer) = 0;
/**
* Get the size of the file in bytes

View File

@ -94,7 +94,7 @@ ResultVal<size_t> IVFCFile::Read(const u64 offset, const size_t length, u8* buff
}
ResultVal<size_t> IVFCFile::Write(const u64 offset, const size_t length, const bool flush,
const u8* buffer) const {
const u8* buffer) {
LOG_ERROR(Service_FS, "Attempted to write to IVFC file");
// TODO(Subv): Find error code
return MakeResult<size_t>(0);

View File

@ -56,7 +56,7 @@ public:
: romfs_file(file), data_offset(offset), data_size(size) {}
ResultVal<size_t> Read(u64 offset, size_t length, u8* buffer) const override;
ResultVal<size_t> Write(u64 offset, size_t length, bool flush, const u8* buffer) const override;
ResultVal<size_t> Write(u64 offset, size_t length, bool flush, const u8* buffer) override;
u64 GetSize() const override;
bool SetSize(u64 size) const override;
bool Close() const override {

View File

@ -29,47 +29,70 @@ static u32 GetSignatureSize(u32 signature_type) {
case EcdsaSha256:
return 0x3C;
}
return 0;
}
Loader::ResultStatus TitleMetadata::Load() {
FileUtil::IOFile file(filepath, "rb");
Loader::ResultStatus TitleMetadata::Load(const std::string& file_path) {
FileUtil::IOFile file(file_path, "rb");
if (!file.IsOpen())
return Loader::ResultStatus::Error;
if (!file.ReadBytes(&signature_type, sizeof(u32_be)))
std::vector<u8> file_data(file.GetSize());
if (!file.ReadBytes(file_data.data(), file.GetSize()))
return Loader::ResultStatus::Error;
Loader::ResultStatus result = Load(file_data);
if (result != Loader::ResultStatus::Success)
LOG_ERROR(Service_FS, "Failed to load TMD from file %s!", file_path.c_str());
return result;
}
Loader::ResultStatus TitleMetadata::Load(const std::vector<u8> file_data, size_t offset) {
size_t total_size = static_cast<size_t>(file_data.size() - offset);
if (total_size < sizeof(u32_be))
return Loader::ResultStatus::Error;
memcpy(&signature_type, &file_data[offset], sizeof(u32_be));
// Signature lengths are variable, and the body follows the signature
u32 signature_size = GetSignatureSize(signature_type);
tmd_signature.resize(signature_size);
if (!file.ReadBytes(&tmd_signature[0], signature_size))
return Loader::ResultStatus::Error;
// The TMD body start position is rounded to the nearest 0x40 after the signature
size_t body_start = Common::AlignUp(signature_size + sizeof(u32), 0x40);
file.Seek(body_start, SEEK_SET);
size_t body_end = body_start + sizeof(Body);
// Read our TMD body, then load the amount of ContentChunks specified
if (file.ReadBytes(&tmd_body, sizeof(TitleMetadata::Body)) != sizeof(TitleMetadata::Body))
if (total_size < body_end)
return Loader::ResultStatus::Error;
// Read signature + TMD body, then load the amount of ContentChunks specified
tmd_signature.resize(signature_size);
memcpy(tmd_signature.data(), &file_data[offset + sizeof(u32_be)], signature_size);
memcpy(&tmd_body, &file_data[offset + body_start], sizeof(TitleMetadata::Body));
size_t expected_size =
body_start + sizeof(Body) + tmd_body.content_count * sizeof(ContentChunk);
if (total_size < expected_size) {
LOG_ERROR(Service_FS, "Malformed TMD, expected size 0x%zx, got 0x%zx!", expected_size,
total_size);
return Loader::ResultStatus::ErrorInvalidFormat;
}
for (u16 i = 0; i < tmd_body.content_count; i++) {
ContentChunk chunk;
if (file.ReadBytes(&chunk, sizeof(ContentChunk)) == sizeof(ContentChunk)) {
tmd_chunks.push_back(chunk);
} else {
LOG_ERROR(Service_FS, "Malformed TMD %s, failed to load content chunk index %u!",
filepath.c_str(), i);
return Loader::ResultStatus::ErrorInvalidFormat;
}
memcpy(&chunk, &file_data[offset + body_end + (i * sizeof(ContentChunk))],
sizeof(ContentChunk));
tmd_chunks.push_back(chunk);
}
return Loader::ResultStatus::Success;
}
Loader::ResultStatus TitleMetadata::Save() {
FileUtil::IOFile file(filepath, "wb");
Loader::ResultStatus TitleMetadata::Save(const std::string& file_path) {
FileUtil::IOFile file(file_path, "wb");
if (!file.IsOpen())
return Loader::ResultStatus::Error;
@ -186,8 +209,7 @@ void TitleMetadata::AddContentChunk(const ContentChunk& chunk) {
}
void TitleMetadata::Print() const {
LOG_DEBUG(Service_FS, "%s - %u chunks", filepath.c_str(),
static_cast<u32>(tmd_body.content_count));
LOG_DEBUG(Service_FS, "%u chunks", static_cast<u32>(tmd_body.content_count));
// Content info describes ranges of content chunks
LOG_DEBUG(Service_FS, "Content info:");

View File

@ -4,6 +4,7 @@
#pragma once
#include <array>
#include <string>
#include <vector>
#include "common/common_types.h"
@ -92,9 +93,9 @@ public:
#pragma pack(pop)
explicit TitleMetadata(std::string& path) : filepath(std::move(path)) {}
Loader::ResultStatus Load();
Loader::ResultStatus Save();
Loader::ResultStatus Load(const std::string& file_path);
Loader::ResultStatus Load(const std::vector<u8> file_data, size_t offset = 0);
Loader::ResultStatus Save(const std::string& file_path);
u64 GetTitleID() const;
u32 GetTitleType() const;
@ -121,8 +122,6 @@ private:
u32_be signature_type;
std::vector<u8> tmd_signature;
std::vector<ContentChunk> tmd_chunks;
std::string filepath;
};
} // namespace FileSys

View File

@ -7,7 +7,6 @@
#include "core/hle/result.h"
namespace Kernel {
namespace ErrCodes {
enum {
OutOfHandles = 19,
@ -18,6 +17,7 @@ enum {
WrongPermission = 46,
InvalidBufferDescriptor = 48,
MaxConnectionsReached = 52,
CommandTooLarge = 54,
};
}

View File

@ -0,0 +1,93 @@
// Copyright 2017 Citra Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
#include "core/hle/ipc.h"
#include "core/hle/kernel/handle_table.h"
#include "core/hle/kernel/ipc.h"
#include "core/hle/kernel/kernel.h"
#include "core/hle/kernel/memory.h"
#include "core/hle/kernel/process.h"
#include "core/hle/kernel/thread.h"
#include "core/memory.h"
namespace Kernel {
ResultCode TranslateCommandBuffer(SharedPtr<Thread> src_thread, SharedPtr<Thread> dst_thread,
VAddr src_address, VAddr dst_address) {
auto& src_process = src_thread->owner_process;
IPC::Header header;
// TODO(Subv): Replace by Memory::Read32 when possible.
Memory::ReadBlock(*src_process, src_address, &header.raw, sizeof(header.raw));
size_t untranslated_size = 1u + header.normal_params_size;
size_t command_size = untranslated_size + header.translate_params_size;
// Note: The real kernel does not check that the command length fits into the IPC buffer area.
ASSERT(command_size <= IPC::COMMAND_BUFFER_LENGTH);
std::array<u32, IPC::COMMAND_BUFFER_LENGTH> cmd_buf;
Memory::ReadBlock(*src_process, src_address, cmd_buf.data(), command_size * sizeof(u32));
size_t i = untranslated_size;
while (i < command_size) {
u32 descriptor = cmd_buf[i];
i += 1;
switch (IPC::GetDescriptorType(descriptor)) {
case IPC::DescriptorType::CopyHandle:
case IPC::DescriptorType::MoveHandle: {
u32 num_handles = IPC::HandleNumberFromDesc(descriptor);
// Note: The real kernel does not check that the number of handles fits into the command
// buffer before writing them, only after finishing.
if (i + num_handles > command_size) {
return ResultCode(ErrCodes::CommandTooLarge, ErrorModule::OS,
ErrorSummary::InvalidState, ErrorLevel::Status);
}
for (u32 j = 0; j < num_handles; ++j) {
Handle handle = cmd_buf[i];
SharedPtr<Object> object = nullptr;
// Perform pseudo-handle detection here because by the time this function is called,
// the current thread and process are no longer the ones which created this IPC
// request, but the ones that are handling it.
if (handle == CurrentThread) {
object = src_thread;
} else if (handle == CurrentProcess) {
object = src_process;
} else if (handle != 0) {
object = g_handle_table.GetGeneric(handle);
if (descriptor == IPC::DescriptorType::MoveHandle) {
g_handle_table.Close(handle);
}
}
if (object == nullptr) {
// Note: The real kernel sets invalid translated handles to 0 in the target
// command buffer.
cmd_buf[i++] = 0;
continue;
}
auto result = g_handle_table.Create(std::move(object));
cmd_buf[i++] = result.ValueOr(0);
}
break;
}
case IPC::DescriptorType::CallingPid: {
cmd_buf[i++] = src_process->process_id;
break;
}
default:
UNIMPLEMENTED_MSG("Unsupported handle translation: 0x%08X", descriptor);
}
}
Memory::WriteBlock(*dst_thread->owner_process, dst_address, cmd_buf.data(),
command_size * sizeof(u32));
return RESULT_SUCCESS;
}
} // namespace Kernel

14
src/core/hle/kernel/ipc.h Normal file
View File

@ -0,0 +1,14 @@
// Copyright 2017 Citra Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
#pragma once
#include "common/common_types.h"
#include "core/hle/kernel/thread.h"
namespace Kernel {
/// Performs IPC command buffer translation from one process to another.
ResultCode TranslateCommandBuffer(SharedPtr<Thread> src_thread, SharedPtr<Thread> dst_thread,
VAddr src_address, VAddr dst_address);
} // namespace Kernel

View File

@ -88,6 +88,8 @@ ResultCode Mutex::Release(Thread* thread) {
WakeupAllWaitingThreads();
Core::System::GetInstance().PrepareReschedule();
}
return RESULT_SUCCESS;
}
void Mutex::AddWaitingThread(SharedPtr<Thread> thread) {

View File

@ -61,13 +61,11 @@ ResultCode ServerSession::HandleSyncRequest(SharedPtr<Thread> thread) {
// If this ServerSession has an associated HLE handler, forward the request to it.
if (hle_handler != nullptr) {
// Attempt to translate the incoming request's command buffer.
ResultCode result = TranslateHLERequest(this);
if (result.IsError())
return result;
hle_handler->HandleSyncRequest(SharedPtr<ServerSession>(this));
// TODO(Subv): Translate the response command buffer.
} else {
// Put the thread to sleep until the server replies, it will be awoken in
// svcReplyAndReceive.
thread->status = THREADSTATUS_WAIT_IPC;
// Add the thread to the list of threads that have issued a sync request with this
// server.
pending_requesting_threads.push_back(std::move(thread));
@ -96,8 +94,4 @@ ServerSession::SessionPair ServerSession::CreateSessionPair(const std::string& n
return std::make_tuple(std::move(server_session), std::move(client_session));
}
ResultCode TranslateHLERequest(ServerSession* server_session) {
// TODO(Subv): Implement this function once multiple concurrent processes are supported.
return RESULT_SUCCESS;
}
} // namespace Kernel

View File

@ -36,6 +36,9 @@ class Thread;
*/
class ServerSession final : public WaitObject {
public:
std::string GetName() const override {
return name;
}
std::string GetTypeName() const override {
return "ServerSession";
}
@ -104,13 +107,4 @@ private:
static ResultVal<SharedPtr<ServerSession>> Create(std::string name = "Unknown");
};
/**
* Performs command buffer translation for an HLE IPC request.
* The command buffer from the ServerSession thread's TLS is copied into a
* buffer and all descriptors in the buffer are processed.
* TODO(Subv): Implement this function, currently we do not support multiple processes running at
* once, but once that is implemented we'll need to properly translate all descriptors
* in the command buffer.
*/
ResultCode TranslateHLERequest(ServerSession* server_session);
}
} // namespace Kernel

View File

@ -278,6 +278,7 @@ void Thread::ResumeFromWait() {
case THREADSTATUS_WAIT_SYNCH_ANY:
case THREADSTATUS_WAIT_ARB:
case THREADSTATUS_WAIT_SLEEP:
case THREADSTATUS_WAIT_IPC:
break;
case THREADSTATUS_READY:
@ -496,8 +497,9 @@ void Thread::BoostPriority(u32 priority) {
SharedPtr<Thread> SetupMainThread(u32 entry_point, u32 priority, SharedPtr<Process> owner_process) {
// Initialize new "main" thread
auto thread_res = Thread::Create("main", entry_point, priority, 0, THREADPROCESSORID_0,
Memory::HEAP_VADDR_END, owner_process);
auto thread_res =
Thread::Create("main", entry_point, priority, 0, owner_process->ideal_processor,
Memory::HEAP_VADDR_END, owner_process);
SharedPtr<Thread> thread = std::move(thread_res).Unwrap();
@ -570,4 +572,4 @@ const std::vector<SharedPtr<Thread>>& GetThreadList() {
return thread_list;
}
} // namespace
} // namespace Kernel

View File

@ -35,6 +35,7 @@ enum ThreadStatus {
THREADSTATUS_READY, ///< Ready to run
THREADSTATUS_WAIT_ARB, ///< Waiting on an address arbiter
THREADSTATUS_WAIT_SLEEP, ///< Waiting due to a SleepThread SVC
THREADSTATUS_WAIT_IPC, ///< Waiting for the reply from an IPC request
THREADSTATUS_WAIT_SYNCH_ANY, ///< Waiting due to WaitSynch1 or WaitSynchN with wait_all = false
THREADSTATUS_WAIT_SYNCH_ALL, ///< Waiting due to WaitSynchronizationN with wait_all = true
THREADSTATUS_DORMANT, ///< Created but not yet made ready

View File

@ -7,8 +7,7 @@
namespace Service {
namespace AC {
// TODO(Subv): Find out the correct number of concurrent sessions allowed
AC_I::AC_I(std::shared_ptr<Module> ac) : Module::Interface(std::move(ac), "ac:i", 1) {
AC_I::AC_I(std::shared_ptr<Module> ac) : Module::Interface(std::move(ac), "ac:i", 10) {
static const FunctionInfo functions[] = {
{0x00010000, &AC_I::CreateDefaultConfig, "CreateDefaultConfig"},
{0x00040006, &AC_I::ConnectAsync, "ConnectAsync"},

View File

@ -7,8 +7,7 @@
namespace Service {
namespace AC {
// TODO(Subv): Find out the correct number of concurrent sessions allowed
AC_U::AC_U(std::shared_ptr<Module> ac) : Module::Interface(std::move(ac), "ac:u", 1) {
AC_U::AC_U(std::shared_ptr<Module> ac) : Module::Interface(std::move(ac), "ac:u", 10) {
static const FunctionInfo functions[] = {
{0x00010000, &AC_U::CreateDefaultConfig, "CreateDefaultConfig"},
{0x00040006, &AC_U::ConnectAsync, "ConnectAsync"},

View File

@ -2,15 +2,27 @@
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
#include <algorithm>
#include <array>
#include <cinttypes>
#include <cstddef>
#include <cstring>
#include "common/file_util.h"
#include "common/logging/log.h"
#include "common/string_util.h"
#include "core/file_sys/cia_container.h"
#include "core/file_sys/errors.h"
#include "core/file_sys/file_backend.h"
#include "core/file_sys/ncch_container.h"
#include "core/file_sys/title_metadata.h"
#include "core/hle/ipc.h"
#include "core/hle/ipc_helpers.h"
#include "core/hle/kernel/client_port.h"
#include "core/hle/kernel/client_session.h"
#include "core/hle/kernel/errors.h"
#include "core/hle/kernel/handle_table.h"
#include "core/hle/kernel/server_session.h"
#include "core/hle/kernel/session.h"
#include "core/hle/result.h"
#include "core/hle/service/am/am.h"
#include "core/hle/service/am/am_app.h"
@ -20,6 +32,7 @@
#include "core/hle/service/fs/archive.h"
#include "core/hle/service/service.h"
#include "core/loader/loader.h"
#include "core/loader/smdh.h"
namespace Service {
namespace AM {
@ -27,6 +40,11 @@ namespace AM {
constexpr u32 TID_HIGH_UPDATE = 0x0004000E;
constexpr u32 TID_HIGH_DLC = 0x0004008C;
// CIA installation static context variables
static bool cia_installing = false;
static u64 cia_installing_tid;
static Service::FS::MediaType cia_installing_media_type;
static bool lists_initialized = false;
static std::array<std::vector<u64_le>, 3> am_title_list;
@ -60,7 +78,192 @@ struct TicketInfo {
static_assert(sizeof(TicketInfo) == 0x18, "Ticket info structure size is wrong");
std::string GetTitleMetadataPath(Service::FS::MediaType media_type, u64 tid) {
// A file handled returned for CIAs to be written into and subsequently installed.
class CIAFile final : public FileSys::FileBackend {
public:
explicit CIAFile(Service::FS::MediaType media_type) : media_type(media_type) {}
ResultVal<size_t> Read(u64 offset, size_t length, u8* buffer) const override {
UNIMPLEMENTED();
return MakeResult<size_t>(length);
}
ResultVal<size_t> WriteTitleMetadata(u64 offset, size_t length, const u8* buffer) {
container.LoadTitleMetadata(data, container.GetTitleMetadataOffset());
FileSys::TitleMetadata tmd = container.GetTitleMetadata();
cia_installing_tid = tmd.GetTitleID();
tmd.Print();
// If a TMD already exists for this app (ie 00000000.tmd), the incoming TMD
// will be the same plus one, (ie 00000001.tmd), both will be kept until
// the install is finalized and old contents can be discarded.
if (FileUtil::Exists(GetTitleMetadataPath(media_type, tmd.GetTitleID())))
is_update = true;
std::string tmd_path = GetTitleMetadataPath(media_type, tmd.GetTitleID(), is_update);
// Create content/ folder if it doesn't exist
std::string tmd_folder;
Common::SplitPath(tmd_path, &tmd_folder, nullptr, nullptr);
FileUtil::CreateFullPath(tmd_folder);
// Save TMD so that we can start getting new .app paths
if (tmd.Save(tmd_path) != Loader::ResultStatus::Success)
return FileSys::ERROR_INSUFFICIENT_SPACE;
// Create any other .app folders which may not exist yet
std::string app_folder;
Common::SplitPath(GetTitleContentPath(media_type, tmd.GetTitleID(),
FileSys::TMDContentIndex::Main, is_update),
&app_folder, nullptr, nullptr);
FileUtil::CreateFullPath(app_folder);
content_written.resize(container.GetTitleMetadata().GetContentCount());
install_state = CIAInstallState::TMDLoaded;
return MakeResult<size_t>(length);
}
ResultVal<size_t> WriteContentData(u64 offset, size_t length, const u8* buffer) {
// Data is not being buffered, so we have to keep track of how much of each <ID>.app
// has been written since we might get a written buffer which contains multiple .app
// contents or only part of a larger .app's contents.
u64 offset_max = offset + length;
for (int i = 0; i < container.GetTitleMetadata().GetContentCount(); i++) {
if (content_written[i] < container.GetContentSize(i)) {
// The size, minimum unwritten offset, and maximum unwritten offset of this content
u64 size = container.GetContentSize(i);
u64 range_min = container.GetContentOffset(i) + content_written[i];
u64 range_max = container.GetContentOffset(i) + size;
// The unwritten range for this content is beyond the buffered data we have
// or comes before the buffered data we have, so skip this content ID.
if (range_min > offset_max || range_max < offset)
continue;
// Figure out how much of this content ID we have just recieved/can write out
u64 available_to_write = std::min(offset_max, range_max) - range_min;
// Since the incoming TMD has already been written, we can use GetTitleContentPath
// to get the content paths to write to.
FileSys::TitleMetadata tmd = container.GetTitleMetadata();
FileUtil::IOFile file(
GetTitleContentPath(media_type, tmd.GetTitleID(), i, is_update),
content_written[i] ? "a" : "w");
if (!file.IsOpen())
return FileSys::ERROR_INSUFFICIENT_SPACE;
file.WriteBytes(buffer + (range_min - offset), available_to_write);
// Keep tabs on how much of this content ID has been written so new range_min
// values can be calculated.
content_written[i] += available_to_write;
LOG_DEBUG(Service_AM, "Wrote %" PRIx64 " to content %u, total %" PRIx64,
available_to_write, i, content_written[i]);
}
}
return MakeResult<size_t>(length);
}
ResultVal<size_t> Write(u64 offset, size_t length, bool flush, const u8* buffer) override {
written += length;
// TODO(shinyquagsire23): Can we assume that things will only be written in sequence?
// Does AM send an error if we write to things out of order?
// Or does it just ignore offsets and assume a set sequence of incoming data?
// The data in CIAs is always stored CIA Header > Cert > Ticket > TMD > Content > Meta.
// The CIA Header describes Cert, Ticket, TMD, total content sizes, and TMD is needed for
// content sizes so it ends up becoming a problem of keeping track of how much has been
// written and what we have been able to pick up.
if (install_state == CIAInstallState::InstallStarted) {
size_t buf_copy_size = std::min(length, FileSys::CIA_HEADER_SIZE);
size_t buf_max_size =
std::min(static_cast<size_t>(offset + length), FileSys::CIA_HEADER_SIZE);
data.resize(buf_max_size);
memcpy(data.data() + offset, buffer, buf_copy_size);
// We have enough data to load a CIA header and parse it.
if (written >= FileSys::CIA_HEADER_SIZE) {
container.LoadHeader(data);
container.Print();
install_state = CIAInstallState::HeaderLoaded;
}
}
// If we don't have a header yet, we can't pull offsets of other sections
if (install_state == CIAInstallState::InstallStarted)
return MakeResult<size_t>(length);
// If we have been given data before (or including) .app content, pull it into
// our buffer, but only pull *up to* the content offset, no further.
if (offset < container.GetContentOffset()) {
size_t buf_loaded = data.size();
size_t copy_offset = std::max(static_cast<size_t>(offset), buf_loaded);
size_t buf_offset = buf_loaded - offset;
size_t buf_copy_size =
std::min(length, static_cast<size_t>(container.GetContentOffset() - offset)) -
buf_loaded;
size_t buf_max_size = std::min(offset + length, container.GetContentOffset());
data.resize(buf_max_size);
memcpy(data.data() + copy_offset, buffer + buf_offset, buf_copy_size);
}
// TODO(shinyquagsire23): Write out .tik files to nand?
// The end of our TMD is at the beginning of Content data, so ensure we have that much
// buffered before trying to parse.
if (written >= container.GetContentOffset() &&
install_state != CIAInstallState::TMDLoaded) {
auto result = WriteTitleMetadata(offset, length, buffer);
if (result.Failed())
return result;
}
// Content data sizes can only be retrieved from TMD data
if (install_state != CIAInstallState::TMDLoaded)
return MakeResult<size_t>(length);
// From this point forward, data will no longer be buffered in data
auto result = WriteContentData(offset, length, buffer);
if (result.Failed())
return result;
return MakeResult<size_t>(length);
}
u64 GetSize() const override {
return written;
}
bool SetSize(u64 size) const override {
return false;
}
bool Close() const override {
return true;
}
void Flush() const override {}
private:
// Whether it's installing an update, and what step of installation it is at
bool is_update = false;
CIAInstallState install_state = CIAInstallState::InstallStarted;
// How much has been written total, CIAContainer for the installing CIA, buffer of all data
// prior to content data, how much of each content index has been written, and where the CIA
// is being installed to
u64 written = 0;
FileSys::CIAContainer container;
std::vector<u8> data;
std::vector<u64> content_written;
Service::FS::MediaType media_type;
};
std::string GetTitleMetadataPath(Service::FS::MediaType media_type, u64 tid, bool update) {
std::string content_path = GetTitlePath(media_type, tid) + "content/";
if (media_type == Service::FS::MediaType::GameCard) {
@ -69,23 +272,37 @@ std::string GetTitleMetadataPath(Service::FS::MediaType media_type, u64 tid) {
}
// The TMD ID is usually held in the title databases, which we don't implement.
// For now, just scan for any .tmd files which exist and use the first .tmd
// found (there should only really be one unless the directories are meddled with)
// For now, just scan for any .tmd files which exist, the smallest will be the
// base ID and the largest will be the (currently installing) update ID.
constexpr u32 MAX_TMD_ID = 0xFFFFFFFF;
u32 base_id = MAX_TMD_ID;
u32 update_id = 0;
FileUtil::FSTEntry entries;
FileUtil::ScanDirectoryTree(content_path, entries);
for (const FileUtil::FSTEntry& entry : entries.children) {
std::string filename_filename, filename_extension;
Common::SplitPath(entry.virtualName, nullptr, &filename_filename, &filename_extension);
if (filename_extension == ".tmd")
return content_path + entry.virtualName;
if (filename_extension == ".tmd") {
u32 id = std::stoul(filename_filename.c_str(), nullptr, 16);
base_id = std::min(base_id, id);
update_id = std::max(update_id, id);
}
}
// If we can't find an existing .tmd, return a path for one to be created.
return content_path + "00000000.tmd";
// If we didn't find anything, default to 00000000.tmd for it to be created.
if (base_id == MAX_TMD_ID)
base_id = 0;
// Update ID should be one more than the last, if it hasn't been created yet.
if (base_id == update_id)
update_id++;
return content_path + Common::StringFromFormat("%08x.tmd", (update ? update_id : base_id));
}
std::string GetTitleContentPath(Service::FS::MediaType media_type, u64 tid, u16 index) {
std::string GetTitleContentPath(Service::FS::MediaType media_type, u64 tid, u16 index,
bool update) {
std::string content_path = GetTitlePath(media_type, tid) + "content/";
if (media_type == Service::FS::MediaType::GameCard) {
@ -95,11 +312,11 @@ std::string GetTitleContentPath(Service::FS::MediaType media_type, u64 tid, u16
return "";
}
std::string tmd_path = GetTitleMetadataPath(media_type, tid);
std::string tmd_path = GetTitleMetadataPath(media_type, tid, update);
u32 content_id = 0;
FileSys::TitleMetadata tmd(tmd_path);
if (tmd.Load() == Loader::ResultStatus::Success) {
FileSys::TitleMetadata tmd;
if (tmd.Load(tmd_path) == Loader::ResultStatus::Success) {
content_id = tmd.GetContentIDByIndex(index);
// TODO(shinyquagsire23): how does DLC actually get this folder on hardware?
@ -184,14 +401,28 @@ void GetNumPrograms(Service::Interface* self) {
rb.Push<u32>(am_title_list[media_type].size());
}
void FindContentInfos(Service::Interface* self) {
void FindDLCContentInfos(Service::Interface* self) {
IPC::RequestParser rp(Kernel::GetCommandBuffer(), 0x1002, 4, 4); // 0x10020104
auto media_type = static_cast<Service::FS::MediaType>(rp.Pop<u8>());
u64 title_id = rp.Pop<u64>();
u32 content_count = rp.Pop<u32>();
VAddr content_requested_in = rp.PopMappedBuffer();
VAddr content_info_out = rp.PopMappedBuffer();
size_t input_buffer_size, output_buffer_size;
IPC::MappedBufferPermissions input_buffer_perms, output_buffer_perms;
VAddr content_requested_in = rp.PopMappedBuffer(&input_buffer_size, &input_buffer_perms);
VAddr content_info_out = rp.PopMappedBuffer(&output_buffer_size, &output_buffer_perms);
// Validate that only DLC TIDs are passed in
u32 tid_high = static_cast<u32>(title_id >> 32);
if (tid_high != TID_HIGH_DLC) {
IPC::RequestBuilder rb = rp.MakeBuilder(1, 4);
rb.Push(ResultCode(ErrCodes::InvalidTIDInList, ErrorModule::AM,
ErrorSummary::InvalidArgument, ErrorLevel::Usage));
rb.PushMappedBuffer(content_requested_in, input_buffer_size, input_buffer_perms);
rb.PushMappedBuffer(content_info_out, output_buffer_size, output_buffer_perms);
return;
}
std::vector<u16_le> content_requested(content_count);
Memory::ReadBlock(content_requested_in, content_requested.data(), content_count * sizeof(u16));
@ -199,8 +430,8 @@ void FindContentInfos(Service::Interface* self) {
std::string tmd_path = GetTitleMetadataPath(media_type, title_id);
u32 content_read = 0;
FileSys::TitleMetadata tmd(tmd_path);
if (tmd.Load() == Loader::ResultStatus::Success) {
FileSys::TitleMetadata tmd;
if (tmd.Load(tmd_path) == Loader::ResultStatus::Success) {
// Get info for each content index requested
for (size_t i = 0; i < content_count; i++) {
std::shared_ptr<FileUtil::IOFile> romfs_file;
@ -223,23 +454,40 @@ void FindContentInfos(Service::Interface* self) {
}
}
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
IPC::RequestBuilder rb = rp.MakeBuilder(1, 4);
rb.Push(RESULT_SUCCESS);
rb.PushMappedBuffer(content_requested_in, input_buffer_size, input_buffer_perms);
rb.PushMappedBuffer(content_info_out, output_buffer_size, output_buffer_perms);
}
void ListContentInfos(Service::Interface* self) {
void ListDLCContentInfos(Service::Interface* self) {
IPC::RequestParser rp(Kernel::GetCommandBuffer(), 0x1003, 5, 2); // 0x10030142
u32 content_count = rp.Pop<u32>();
auto media_type = static_cast<Service::FS::MediaType>(rp.Pop<u8>());
u64 title_id = rp.Pop<u64>();
u32 start_index = rp.Pop<u32>();
VAddr content_info_out = rp.PopMappedBuffer();
size_t output_buffer_size;
IPC::MappedBufferPermissions output_buffer_perms;
VAddr content_info_out = rp.PopMappedBuffer(&output_buffer_size, &output_buffer_perms);
// Validate that only DLC TIDs are passed in
u32 tid_high = static_cast<u32>(title_id >> 32);
if (tid_high != TID_HIGH_DLC) {
IPC::RequestBuilder rb = rp.MakeBuilder(2, 2);
rb.Push(ResultCode(ErrCodes::InvalidTIDInList, ErrorModule::AM,
ErrorSummary::InvalidArgument, ErrorLevel::Usage));
rb.Push<u32>(0);
rb.PushMappedBuffer(content_info_out, output_buffer_size, output_buffer_perms);
return;
}
std::string tmd_path = GetTitleMetadataPath(media_type, title_id);
u32 copied = 0;
FileSys::TitleMetadata tmd(tmd_path);
if (tmd.Load() == Loader::ResultStatus::Success) {
FileSys::TitleMetadata tmd;
if (tmd.Load(tmd_path) == Loader::ResultStatus::Success) {
copied = std::min(content_count, static_cast<u32>(tmd.GetContentCount()));
for (u32 i = start_index; i < copied; i++) {
std::shared_ptr<FileUtil::IOFile> romfs_file;
@ -261,9 +509,10 @@ void ListContentInfos(Service::Interface* self) {
}
}
IPC::RequestBuilder rb = rp.MakeBuilder(2, 0);
IPC::RequestBuilder rb = rp.MakeBuilder(2, 2);
rb.Push(RESULT_SUCCESS);
rb.Push(copied);
rb.PushMappedBuffer(content_info_out, output_buffer_size, output_buffer_perms);
}
void DeleteContents(Service::Interface* self) {
@ -313,8 +562,8 @@ ResultCode GetTitleInfoFromList(const std::vector<u64>& title_id_list,
TitleInfo title_info = {};
title_info.tid = title_id_list[i];
FileSys::TitleMetadata tmd(tmd_path);
if (tmd.Load() == Loader::ResultStatus::Success) {
FileSys::TitleMetadata tmd;
if (tmd.Load(tmd_path) == Loader::ResultStatus::Success) {
// TODO(shinyquagsire23): This is the total size of all files this process owns,
// including savefiles and other content. This comes close but is off.
title_info.size = tmd.GetContentSizeByIndex(FileSys::TMDContentIndex::Main);
@ -452,23 +701,33 @@ void ListDataTitleTicketInfos(Service::Interface* self) {
ticket_count, title_id, start_index, ticket_info_out);
}
void GetNumContentInfos(Service::Interface* self) {
void GetDLCContentInfoCount(Service::Interface* self) {
IPC::RequestParser rp(Kernel::GetCommandBuffer(), 0x1001, 3, 0); // 0x100100C0
auto media_type = static_cast<Service::FS::MediaType>(rp.Pop<u8>());
u64 title_id = rp.Pop<u64>();
// Validate that only DLC TIDs are passed in
u32 tid_high = static_cast<u32>(title_id >> 32);
if (tid_high != TID_HIGH_DLC) {
IPC::RequestBuilder rb = rp.MakeBuilder(2, 2);
rb.Push(ResultCode(ErrCodes::InvalidTID, ErrorModule::AM, ErrorSummary::InvalidArgument,
ErrorLevel::Usage));
rb.Push<u32>(0);
return;
}
IPC::RequestBuilder rb = rp.MakeBuilder(2, 0);
rb.Push(RESULT_SUCCESS); // No error
std::string tmd_path = GetTitleMetadataPath(media_type, title_id);
FileSys::TitleMetadata tmd(tmd_path);
if (tmd.Load() == Loader::ResultStatus::Success) {
FileSys::TitleMetadata tmd;
if (tmd.Load(tmd_path) == Loader::ResultStatus::Success) {
rb.Push<u32>(tmd.GetContentCount());
} else {
rb.Push<u32>(1); // Number of content infos plus one
LOG_WARNING(Service_AM, "(STUBBED) called media_type=%u, title_id=0x%016" PRIx64,
media_type, title_id);
static_cast<u32>(media_type), title_id);
}
}
@ -513,7 +772,7 @@ void QueryAvailableTitleDatabase(Service::Interface* self) {
rb.Push(RESULT_SUCCESS); // No error
rb.Push(true);
LOG_WARNING(Service_APT, "(STUBBED) media_type=%u", media_type);
LOG_WARNING(Service_AM, "(STUBBED) media_type=%u", media_type);
}
void CheckContentRights(Service::Interface* self) {
@ -529,7 +788,7 @@ void CheckContentRights(Service::Interface* self) {
rb.Push(RESULT_SUCCESS); // No error
rb.Push(has_rights);
LOG_WARNING(Service_APT, "(STUBBED) tid=%016" PRIx64 ", content_index=%u", tid, content_index);
LOG_WARNING(Service_AM, "(STUBBED) tid=%016" PRIx64 ", content_index=%u", tid, content_index);
}
void CheckContentRightsIgnorePlatform(Service::Interface* self) {
@ -545,7 +804,376 @@ void CheckContentRightsIgnorePlatform(Service::Interface* self) {
rb.Push(RESULT_SUCCESS); // No error
rb.Push(has_rights);
LOG_WARNING(Service_APT, "(STUBBED) tid=%016" PRIx64 ", content_index=%u", tid, content_index);
LOG_WARNING(Service_AM, "(STUBBED) tid=%016" PRIx64 ", content_index=%u", tid, content_index);
}
void BeginImportProgram(Service::Interface* self) {
IPC::RequestParser rp(Kernel::GetCommandBuffer(), 0x0402, 1, 0); // 0x04020040
auto media_type = static_cast<Service::FS::MediaType>(rp.Pop<u8>());
if (cia_installing) {
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
rb.Push(ResultCode(ErrCodes::CIACurrentlyInstalling, ErrorModule::AM,
ErrorSummary::InvalidState, ErrorLevel::Permanent));
return;
}
// Create our CIAFile handle for the app to write to, and while the app writes
// Citra will store contents out to sdmc/nand
const FileSys::Path cia_path = {};
auto file =
std::make_shared<Service::FS::File>(std::make_unique<CIAFile>(media_type), cia_path);
auto sessions = Kernel::ServerSession::CreateSessionPair(file->GetName());
file->ClientConnected(std::get<Kernel::SharedPtr<Kernel::ServerSession>>(sessions));
cia_installing = true;
cia_installing_media_type = media_type;
IPC::RequestBuilder rb = rp.MakeBuilder(1, 2);
rb.Push(RESULT_SUCCESS); // No error
rb.PushCopyHandles(
Kernel::g_handle_table.Create(std::get<Kernel::SharedPtr<Kernel::ClientSession>>(sessions))
.Unwrap());
LOG_WARNING(Service_AM, "(STUBBED) media_type=%u", static_cast<u32>(media_type));
}
void EndImportProgram(Service::Interface* self) {
IPC::RequestParser rp(Kernel::GetCommandBuffer(), 0x0405, 0, 2); // 0x04050002
auto cia_handle = rp.PopHandle();
// Clean up older content data if we installed newer content on top
std::string old_tmd_path =
GetTitleMetadataPath(cia_installing_media_type, cia_installing_tid, false);
std::string new_tmd_path =
GetTitleMetadataPath(cia_installing_media_type, cia_installing_tid, true);
if (FileUtil::Exists(new_tmd_path) && old_tmd_path != new_tmd_path) {
FileSys::TitleMetadata old_tmd;
FileSys::TitleMetadata new_tmd;
old_tmd.Load(old_tmd_path);
new_tmd.Load(new_tmd_path);
// For each content ID in the old TMD, check if there is a matching ID in the new
// TMD. If a CIA contains (and wrote to) an identical ID, it should be kept while
// IDs which only existed for the old TMD should be deleted.
for (u16 old_index = 0; old_index < old_tmd.GetContentCount(); old_index++) {
bool abort = false;
for (u16 new_index = 0; new_index < new_tmd.GetContentCount(); new_index++) {
if (old_tmd.GetContentIDByIndex(old_index) ==
new_tmd.GetContentIDByIndex(new_index)) {
abort = true;
}
}
if (abort)
break;
FileUtil::Delete(
GetTitleContentPath(cia_installing_media_type, old_tmd.GetTitleID(), old_index));
}
FileUtil::Delete(old_tmd_path);
}
ScanForAllTitles();
cia_installing = false;
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
rb.Push(RESULT_SUCCESS);
}
ResultVal<std::shared_ptr<Service::FS::File>> GetFileFromHandle(Kernel::Handle handle) {
// Step up the chain from Handle->ClientSession->ServerSession and then
// cast to File. For AM on 3DS, invalid handles actually hang the system.
auto file_session = Kernel::g_handle_table.Get<Kernel::ClientSession>(handle);
if (file_session == nullptr || file_session->parent == nullptr) {
LOG_WARNING(Service_AM, "Invalid file handle!");
return Kernel::ERR_INVALID_HANDLE;
}
Kernel::SharedPtr<Kernel::ServerSession> server = file_session->parent->server;
if (server == nullptr) {
LOG_WARNING(Service_AM, "File handle ServerSession disconnected!");
return Kernel::ERR_SESSION_CLOSED_BY_REMOTE;
}
if (server->hle_handler != nullptr) {
auto file = std::dynamic_pointer_cast<Service::FS::File>(server->hle_handler);
// TODO(shinyquagsire23): This requires RTTI, use service calls directly instead?
if (file != nullptr)
return MakeResult<std::shared_ptr<Service::FS::File>>(file);
LOG_ERROR(Service_AM, "Failed to cast handle to FSFile!");
return Kernel::ERR_INVALID_HANDLE;
}
// Probably the best bet if someone is LLEing the fs service is to just have them LLE AM
// while they're at it, so not implemented.
LOG_ERROR(Service_AM, "Given file handle does not have an HLE handler!");
return Kernel::ERR_NOT_IMPLEMENTED;
}
void GetProgramInfoFromCia(Service::Interface* self) {
IPC::RequestParser rp(Kernel::GetCommandBuffer(), 0x0408, 1, 2); // 0x04080042
auto media_type = static_cast<Service::FS::MediaType>(rp.Pop<u8>());
// Get a File from our Handle
auto file_res = GetFileFromHandle(rp.PopHandle());
if (!file_res.Succeeded()) {
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
rb.Push(file_res.Code());
return;
}
auto file = file_res.Unwrap();
FileSys::CIAContainer container;
if (container.Load(*file->backend) != Loader::ResultStatus::Success) {
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
rb.Push(ResultCode(ErrCodes::InvalidCIAHeader, ErrorModule::AM,
ErrorSummary::InvalidArgument, ErrorLevel::Permanent));
return;
}
FileSys::TitleMetadata tmd = container.GetTitleMetadata();
TitleInfo title_info = {};
container.Print();
// TODO(shinyquagsire23): Sizes allegedly depend on the mediatype, and will double
// on some mediatypes. Since this is more of a required install size we'll report
// what Citra needs, but it would be good to be more accurate here.
title_info.tid = tmd.GetTitleID();
title_info.size = tmd.GetContentSizeByIndex(FileSys::TMDContentIndex::Main);
title_info.version = tmd.GetTitleVersion();
title_info.type = tmd.GetTitleType();
IPC::RequestBuilder rb = rp.MakeBuilder(8, 0);
rb.Push(RESULT_SUCCESS);
rb.PushRaw<TitleInfo>(title_info);
}
void GetSystemMenuDataFromCia(Service::Interface* self) {
IPC::RequestParser rp(Kernel::GetCommandBuffer(), 0x0409, 0, 4); // 0x04090004
// Get a File from our Handle
auto file_res = GetFileFromHandle(rp.PopHandle());
if (!file_res.Succeeded()) {
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
rb.Push(file_res.Code());
return;
}
size_t output_buffer_size;
IPC::MappedBufferPermissions output_buffer_perms;
VAddr output_buffer = rp.PopMappedBuffer(&output_buffer_size, &output_buffer_perms);
output_buffer_size = std::min(output_buffer_size, sizeof(Loader::SMDH));
auto file = file_res.Unwrap();
FileSys::CIAContainer container;
if (container.Load(*file->backend) != Loader::ResultStatus::Success) {
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
rb.Push(ResultCode(ErrCodes::InvalidCIAHeader, ErrorModule::AM,
ErrorSummary::InvalidArgument, ErrorLevel::Permanent));
return;
}
std::vector<u8> temp(output_buffer_size);
// Read from the Meta offset + 0x400 for the 0x36C0-large SMDH
auto read_result =
file->backend->Read(container.GetMetadataOffset() + FileSys::CIA_METADATA_SIZE,
output_buffer_size, temp.data());
if (read_result.Failed() || *read_result != output_buffer_size) {
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
rb.Push(ResultCode(ErrCodes::InvalidCIAHeader, ErrorModule::AM,
ErrorSummary::InvalidArgument, ErrorLevel::Permanent));
return;
}
Memory::WriteBlock(output_buffer, temp.data(), output_buffer_size);
IPC::RequestBuilder rb = rp.MakeBuilder(1, 2);
rb.PushMappedBuffer(output_buffer, output_buffer_size, output_buffer_perms);
rb.Push(RESULT_SUCCESS);
}
void GetDependencyListFromCia(Service::Interface* self) {
IPC::RequestParser rp(Kernel::GetCommandBuffer(), 0x040A, 0, 2); // 0x040A0002
// Get a File from our Handle
auto file_res = GetFileFromHandle(rp.PopHandle());
if (!file_res.Succeeded()) {
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
rb.Push(file_res.Code());
return;
}
size_t output_buffer_size;
VAddr output_buffer = rp.PeekStaticBuffer(0, &output_buffer_size);
output_buffer_size = std::min(output_buffer_size, FileSys::CIA_DEPENDENCY_SIZE);
auto file = file_res.Unwrap();
FileSys::CIAContainer container;
if (container.Load(*file->backend) != Loader::ResultStatus::Success) {
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
rb.Push(ResultCode(ErrCodes::InvalidCIAHeader, ErrorModule::AM,
ErrorSummary::InvalidArgument, ErrorLevel::Permanent));
return;
}
Memory::WriteBlock(output_buffer, container.GetDependencies().data(), output_buffer_size);
IPC::RequestBuilder rb = rp.MakeBuilder(1, 2);
rb.Push(RESULT_SUCCESS);
rb.PushStaticBuffer(output_buffer, output_buffer_size, 0);
}
void GetTransferSizeFromCia(Service::Interface* self) {
IPC::RequestParser rp(Kernel::GetCommandBuffer(), 0x040B, 0, 2); // 0x040B0002
// Get a File from our Handle
auto file_res = GetFileFromHandle(rp.PopHandle());
if (!file_res.Succeeded()) {
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
rb.Push(file_res.Code());
return;
}
auto file = file_res.Unwrap();
FileSys::CIAContainer container;
if (container.Load(*file->backend) != Loader::ResultStatus::Success) {
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
rb.Push(ResultCode(ErrCodes::InvalidCIAHeader, ErrorModule::AM,
ErrorSummary::InvalidArgument, ErrorLevel::Permanent));
return;
}
IPC::RequestBuilder rb = rp.MakeBuilder(3, 0);
rb.Push(RESULT_SUCCESS);
rb.Push(container.GetMetadataOffset());
}
void GetCoreVersionFromCia(Service::Interface* self) {
IPC::RequestParser rp(Kernel::GetCommandBuffer(), 0x040C, 0, 2); // 0x040C0002
// Get a File from our Handle
auto file_res = GetFileFromHandle(rp.PopHandle());
if (!file_res.Succeeded()) {
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
rb.Push(file_res.Code());
return;
}
auto file = file_res.Unwrap();
FileSys::CIAContainer container;
if (container.Load(*file->backend) != Loader::ResultStatus::Success) {
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
rb.Push(ResultCode(ErrCodes::InvalidCIAHeader, ErrorModule::AM,
ErrorSummary::InvalidArgument, ErrorLevel::Permanent));
return;
}
IPC::RequestBuilder rb = rp.MakeBuilder(2, 0);
rb.Push(RESULT_SUCCESS);
rb.Push(container.GetCoreVersion());
}
void GetRequiredSizeFromCia(Service::Interface* self) {
IPC::RequestParser rp(Kernel::GetCommandBuffer(), 0x040D, 1, 2); // 0x040D0042
auto media_type = static_cast<Service::FS::MediaType>(rp.Pop<u8>());
// Get a File from our Handle
auto file_res = GetFileFromHandle(rp.PopHandle());
if (!file_res.Succeeded()) {
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
rb.Push(file_res.Code());
return;
}
auto file = file_res.Unwrap();
FileSys::CIAContainer container;
if (container.Load(*file->backend) != Loader::ResultStatus::Success) {
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
rb.Push(ResultCode(ErrCodes::InvalidCIAHeader, ErrorModule::AM,
ErrorSummary::InvalidArgument, ErrorLevel::Permanent));
return;
}
// TODO(shinyquagsire23): Sizes allegedly depend on the mediatype, and will double
// on some mediatypes. Since this is more of a required install size we'll report
// what Citra needs, but it would be good to be more accurate here.
IPC::RequestBuilder rb = rp.MakeBuilder(3, 0);
rb.Push(RESULT_SUCCESS);
rb.Push(container.GetTitleMetadata().GetContentSizeByIndex(FileSys::TMDContentIndex::Main));
}
void GetMetaSizeFromCia(Service::Interface* self) {
IPC::RequestParser rp(Kernel::GetCommandBuffer(), 0x0413, 0, 2); // 0x04130002
// Get a File from our Handle
auto file_res = GetFileFromHandle(rp.PopHandle());
if (!file_res.Succeeded()) {
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
rb.Push(file_res.Code());
return;
}
auto file = file_res.Unwrap();
FileSys::CIAContainer container;
if (container.Load(*file->backend) != Loader::ResultStatus::Success) {
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
rb.Push(ResultCode(ErrCodes::InvalidCIAHeader, ErrorModule::AM,
ErrorSummary::InvalidArgument, ErrorLevel::Permanent));
return;
}
IPC::RequestBuilder rb = rp.MakeBuilder(2, 0);
rb.Push(RESULT_SUCCESS);
rb.Push(container.GetMetadataSize());
}
void GetMetaDataFromCia(Service::Interface* self) {
IPC::RequestParser rp(Kernel::GetCommandBuffer(), 0x0414, 0, 2); // 0x04140044
u32 output_size = rp.Pop<u32>();
// Get a File from our Handle
auto file_res = GetFileFromHandle(rp.PopHandle());
if (!file_res.Succeeded()) {
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
rb.Push(file_res.Code());
return;
}
size_t output_buffer_size;
VAddr output_buffer = rp.PeekStaticBuffer(0, &output_buffer_size);
// Don't write beyond the actual static buffer size.
output_size = std::min(static_cast<u32>(output_buffer_size), output_size);
auto file = file_res.Unwrap();
FileSys::CIAContainer container;
if (container.Load(*file->backend) != Loader::ResultStatus::Success) {
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
rb.Push(ResultCode(ErrCodes::InvalidCIAHeader, ErrorModule::AM,
ErrorSummary::InvalidArgument, ErrorLevel::Permanent));
return;
}
// Read from the Meta offset for the specified size
std::vector<u8> temp(output_size);
auto read_result = file->backend->Read(container.GetMetadataOffset(), output_size, temp.data());
if (read_result.Failed() || *read_result != output_size) {
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
rb.Push(ResultCode(ErrCodes::InvalidCIAHeader, ErrorModule::AM,
ErrorSummary::InvalidArgument, ErrorLevel::Permanent));
return;
}
Memory::WriteBlock(output_buffer, temp.data(), output_size);
IPC::RequestBuilder rb = rp.MakeBuilder(1, 2);
rb.Push(RESULT_SUCCESS);
rb.PushStaticBuffer(output_buffer, output_buffer_size, 0);
}
void Init() {
@ -557,7 +1185,9 @@ void Init() {
ScanForAllTitles();
}
void Shutdown() {}
void Shutdown() {
cia_installing = false;
}
} // namespace AM

View File

@ -21,26 +21,42 @@ namespace AM {
namespace ErrCodes {
enum {
CIACurrentlyInstalling = 4,
InvalidTID = 31,
EmptyCIA = 32,
InvalidTIDInList = 60,
InvalidCIAHeader = 104,
};
} // namespace ErrCodes
enum class CIAInstallState : u32 {
InstallStarted,
HeaderLoaded,
CertLoaded,
TicketLoaded,
TMDLoaded,
ContentWritten,
};
/**
* Get the .tmd path for a title
* @param media_type the media the title exists on
* @param tid the title ID to get
* @param update set true if the incoming TMD should be used instead of the current TMD
* @returns string path to the .tmd file if it exists, otherwise a path to create one is given.
*/
std::string GetTitleMetadataPath(Service::FS::MediaType media_type, u64 tid);
std::string GetTitleMetadataPath(Service::FS::MediaType media_type, u64 tid, bool update = false);
/**
* Get the .app path for a title's installed content index.
* @param media_type the media the title exists on
* @param tid the title ID to get
* @param index the content index to get
* @param update set true if the incoming TMD should be used instead of the current TMD
* @returns string path to the .app file
*/
std::string GetTitleContentPath(Service::FS::MediaType media_type, u64 tid, u16 index = 0);
std::string GetTitleContentPath(Service::FS::MediaType media_type, u64 tid, u16 index = 0,
bool update = false);
/**
* Get the folder for a title's installed content.
@ -81,7 +97,8 @@ void ScanForAllTitles();
void GetNumPrograms(Service::Interface* self);
/**
* AM::FindContentInfos service function
* AM::FindDLCContentInfos service function
* Explicitly checks that TID high value is 0004008C or an error is returned.
* Inputs:
* 1 : MediaType
* 2-3 : u64, Title ID
@ -91,10 +108,11 @@ void GetNumPrograms(Service::Interface* self);
* Outputs:
* 1 : Result, 0 on success, otherwise error code
*/
void FindContentInfos(Service::Interface* self);
void FindDLCContentInfos(Service::Interface* self);
/**
* AM::ListContentInfos service function
* AM::ListDLCContentInfos service function
* Explicitly checks that TID high value is 0004008C or an error is returned.
* Inputs:
* 1 : Content count
* 2 : MediaType
@ -105,7 +123,7 @@ void FindContentInfos(Service::Interface* self);
* 1 : Result, 0 on success, otherwise error code
* 2 : Number of content infos returned
*/
void ListContentInfos(Service::Interface* self);
void ListDLCContentInfos(Service::Interface* self);
/**
* AM::DeleteContents service function
@ -187,7 +205,8 @@ void GetPatchTitleInfos(Service::Interface* self);
void ListDataTitleTicketInfos(Service::Interface* self);
/**
* AM::GetNumContentInfos service function
* AM::GetDLCContentInfoCount service function
* Explicitly checks that TID high value is 0004008C or an error is returned.
* Inputs:
* 0 : Command header (0x100100C0)
* 1 : MediaType
@ -196,7 +215,7 @@ void ListDataTitleTicketInfos(Service::Interface* self);
* 1 : Result, 0 on success, otherwise error code
* 2 : Number of content infos plus one
*/
void GetNumContentInfos(Service::Interface* self);
void GetDLCContentInfoCount(Service::Interface* self);
/**
* AM::DeleteTicket service function
@ -259,6 +278,127 @@ void CheckContentRights(Service::Interface* self);
*/
void CheckContentRightsIgnorePlatform(Service::Interface* self);
/**
* AM::BeginImportProgram service function
* Begin importing from a CTR Installable Archive
* Inputs:
* 0 : Command header (0x04020040)
* 1 : Media type to install title to
* Outputs:
* 1 : Result, 0 on success, otherwise error code
* 2-3 : CIAFile handle for application to write to
*/
void BeginImportProgram(Service::Interface* self);
/**
* AM::EndImportProgram service function
* Finish importing from a CTR Installable Archive
* Inputs:
* 0 : Command header (0x04050002)
* 1-2 : CIAFile handle application wrote to
* Outputs:
* 1 : Result, 0 on success, otherwise error code
*/
void EndImportProgram(Service::Interface* self);
/**
* AM::GetProgramInfoFromCia service function
* Get TitleInfo from a CIA file handle
* Inputs:
* 0 : Command header (0x04080042)
* 1 : Media type of the title
* 2-3 : File handle CIA data can be read from
* Outputs:
* 1 : Result, 0 on success, otherwise error code
* 2-8: TitleInfo structure
*/
void GetProgramInfoFromCia(Service::Interface* self);
/**
* AM::GetSystemMenuDataFromCia service function
* Loads a CIA file's SMDH data into a specified buffer
* Inputs:
* 0 : Command header (0x04090004)
* 1-2 : File handle CIA data can be read from
* 3-4 : Output buffer
* Outputs:
* 1 : Result, 0 on success, otherwise error code
*/
void GetSystemMenuDataFromCia(Service::Interface* self);
/**
* AM::GetDependencyListFromCia service function
* Loads a CIA's dependency list into a specified buffer
* Inputs:
* 0 : Command header (0x040A0002)
* 1-2 : File handle CIA data can be read from
* 64-65 : Output buffer
* Outputs:
* 1 : Result, 0 on success, otherwise error code
*/
void GetDependencyListFromCia(Service::Interface* self);
/**
* AM::GetTransferSizeFromCia service function
* Returns the total expected transfer size up to the CIA meta offset from a CIA
* Inputs:
* 0 : Command header (0x040B0002)
* 1-2 : File handle CIA data can be read from
* Outputs:
* 1 : Result, 0 on success, otherwise error code
* 2-3 : Transfer size
*/
void GetTransferSizeFromCia(Service::Interface* self);
/**
* AM::GetCoreVersionFromCia service function
* Returns the core version from a CIA
* Inputs:
* 0 : Command header (0x040C0002)
* 1-2 : File handle CIA data can be read from
* Outputs:
* 1 : Result, 0 on success, otherwise error code
* 2 : Core version
*/
void GetCoreVersionFromCia(Service::Interface* self);
/**
* AM::GetRequiredSizeFromCia service function
* Returns the required amount of free space required to install a given CIA file
* Inputs:
* 0 : Command header (0x040D0042)
* 1 : Media type to install title to
* 2-3 : File handle CIA data can be read from
* Outputs:
* 1 : Result, 0 on success, otherwise error code
* 2-3 : Required free space for CIA
*/
void GetRequiredSizeFromCia(Service::Interface* self);
/**
* AM::GetMetaSizeFromCia service function
* Returns the size of a given CIA's meta section
* Inputs:
* 0 : Command header (0x04130002)
* 1-2 : File handle CIA data can be read from
* Outputs:
* 1 : Result, 0 on success, otherwise error code
* 2 : Meta section size
*/
void GetMetaSizeFromCia(Service::Interface* self);
/**
* AM::GetMetaDataFromCia service function
* Loads meta section data from a CIA file into a given buffer
* Inputs:
* 0 : Command header (0x04140044)
* 1-2 : File handle CIA data can be read from
* 3-4 : Output buffer
* Outputs:
* 1 : Result, 0 on success, otherwise error code
*/
void GetMetaDataFromCia(Service::Interface* self);
/// Initialize AM service
void Init();

View File

@ -9,9 +9,9 @@ namespace Service {
namespace AM {
const Interface::FunctionInfo FunctionTable[] = {
{0x100100C0, GetNumContentInfos, "GetNumContentInfos"},
{0x10020104, FindContentInfos, "FindContentInfos"},
{0x10030142, ListContentInfos, "ListContentInfos"},
{0x100100C0, GetDLCContentInfoCount, "GetDLCContentInfoCount"},
{0x10020104, FindDLCContentInfos, "FindDLCContentInfos"},
{0x10030142, ListDLCContentInfos, "ListDLCContentInfos"},
{0x10040102, DeleteContents, "DeleteContents"},
{0x10050084, GetDLCTitleInfos, "GetDLCTitleInfos"},
{0x10060080, nullptr, "GetNumDataTitleTickets"},

View File

@ -55,25 +55,25 @@ const Interface::FunctionInfo FunctionTable[] = {
{0x002C0084, nullptr, "GetProgramInfosIgnorePlatform"},
{0x002D00C0, nullptr, "CheckContentRightsIgnorePlatform"},
{0x04010080, nullptr, "UpdateFirmwareTo"},
{0x04020040, nullptr, "BeginImportProgram"},
{0x04020040, BeginImportProgram, "BeginImportProgram"},
{0x04030000, nullptr, "BeginImportProgramTemporarily"},
{0x04040002, nullptr, "CancelImportProgram"},
{0x04050002, nullptr, "EndImportProgram"},
{0x04050002, EndImportProgram, "EndImportProgram"},
{0x04060002, nullptr, "EndImportProgramWithoutCommit"},
{0x040700C2, nullptr, "CommitImportPrograms"},
{0x04080042, nullptr, "GetProgramInfoFromCia"},
{0x04090004, nullptr, "GetSystemMenuDataFromCia"},
{0x040A0002, nullptr, "GetDependencyListFromCia"},
{0x040B0002, nullptr, "GetTransferSizeFromCia"},
{0x040C0002, nullptr, "GetCoreVersionFromCia"},
{0x040D0042, nullptr, "GetRequiredSizeFromCia"},
{0x04080042, GetProgramInfoFromCia, "GetProgramInfoFromCia"},
{0x04090004, GetSystemMenuDataFromCia, "GetSystemMenuDataFromCia"},
{0x040A0002, GetDependencyListFromCia, "GetDependencyListFromCia"},
{0x040B0002, GetTransferSizeFromCia, "GetTransferSizeFromCia"},
{0x040C0002, GetCoreVersionFromCia, "GetCoreVersionFromCia"},
{0x040D0042, GetRequiredSizeFromCia, "GetRequiredSizeFromCia"},
{0x040E00C2, nullptr, "CommitImportProgramsAndUpdateFirmwareAuto"},
{0x040F0000, nullptr, "UpdateFirmwareAuto"},
{0x041000C0, nullptr, "DeleteProgram"},
{0x04110044, nullptr, "GetTwlProgramListForReboot"},
{0x04120000, nullptr, "GetSystemUpdaterMutex"},
{0x04130002, nullptr, "GetMetaSizeFromCia"},
{0x04140044, nullptr, "GetMetaDataFromCia"},
{0x04130002, GetMetaSizeFromCia, "GetMetaSizeFromCia"},
{0x04140044, GetMetaDataFromCia, "GetMetaDataFromCia"},
{0x04150080, nullptr, "CheckDemoLaunchRights"},
{0x041600C0, nullptr, "GetInternalTitleLocationInfo"},
{0x041700C0, nullptr, "PerpetuateAgbSaveData"},

View File

@ -54,9 +54,9 @@ const Interface::FunctionInfo FunctionTable[] = {
{0x002B0142, nullptr, "ListExistingContentInfosSystem"},
{0x002C0084, nullptr, "GetProgramInfosIgnorePlatform"},
{0x002D00C0, CheckContentRightsIgnorePlatform, "CheckContentRightsIgnorePlatform"},
{0x100100C0, GetNumContentInfos, "GetNumContentInfos"},
{0x10020104, FindContentInfos, "FindContentInfos"},
{0x10030142, ListContentInfos, "ListContentInfos"},
{0x100100C0, GetDLCContentInfoCount, "GetDLCContentInfoCount"},
{0x10020104, FindDLCContentInfos, "FindDLCContentInfos"},
{0x10030142, ListDLCContentInfos, "ListDLCContentInfos"},
{0x10040102, DeleteContents, "DeleteContents"},
{0x10050084, GetDLCTitleInfos, "GetDLCTitleInfos"},
{0x10060080, nullptr, "GetNumDataTitleTickets"},

View File

@ -55,25 +55,25 @@ const Interface::FunctionInfo FunctionTable[] = {
{0x002C0084, nullptr, "GetProgramInfosIgnorePlatform"},
{0x002D00C0, nullptr, "CheckContentRightsIgnorePlatform"},
{0x04010080, nullptr, "UpdateFirmwareTo"},
{0x04020040, nullptr, "BeginImportProgram"},
{0x04020040, BeginImportProgram, "BeginImportProgram"},
{0x04030000, nullptr, "BeginImportProgramTemporarily"},
{0x04040002, nullptr, "CancelImportProgram"},
{0x04050002, nullptr, "EndImportProgram"},
{0x04050002, EndImportProgram, "EndImportProgram"},
{0x04060002, nullptr, "EndImportProgramWithoutCommit"},
{0x040700C2, nullptr, "CommitImportPrograms"},
{0x04080042, nullptr, "GetProgramInfoFromCia"},
{0x04090004, nullptr, "GetSystemMenuDataFromCia"},
{0x040A0002, nullptr, "GetDependencyListFromCia"},
{0x040B0002, nullptr, "GetTransferSizeFromCia"},
{0x040C0002, nullptr, "GetCoreVersionFromCia"},
{0x040D0042, nullptr, "GetRequiredSizeFromCia"},
{0x04080042, GetProgramInfoFromCia, "GetProgramInfoFromCia"},
{0x04090004, GetSystemMenuDataFromCia, "GetSystemMenuDataFromCia"},
{0x040A0002, GetDependencyListFromCia, "GetDependencyListFromCia"},
{0x040B0002, GetTransferSizeFromCia, "GetTransferSizeFromCia"},
{0x040C0002, GetCoreVersionFromCia, "GetCoreVersionFromCia"},
{0x040D0042, GetRequiredSizeFromCia, "GetRequiredSizeFromCia"},
{0x040E00C2, nullptr, "CommitImportProgramsAndUpdateFirmwareAuto"},
{0x040F0000, nullptr, "UpdateFirmwareAuto"},
{0x041000C0, nullptr, "DeleteProgram"},
{0x04110044, nullptr, "GetTwlProgramListForReboot"},
{0x04120000, nullptr, "GetSystemUpdaterMutex"},
{0x04130002, nullptr, "GetMetaSizeFromCia"},
{0x04140044, nullptr, "GetMetaDataFromCia"},
{0x04130002, GetMetaSizeFromCia, "GetMetaSizeFromCia"},
{0x04140044, GetMetaDataFromCia, "GetMetaDataFromCia"},
{0x04150080, nullptr, "CheckDemoLaunchRights"},
{0x041600C0, nullptr, "GetInternalTitleLocationInfo"},
{0x041700C0, nullptr, "PerpetuateAgbSaveData"},

View File

@ -18,6 +18,7 @@
#include "core/hle/kernel/errors.h"
#include "core/hle/kernel/event.h"
#include "core/hle/kernel/handle_table.h"
#include "core/hle/kernel/ipc.h"
#include "core/hle/kernel/memory.h"
#include "core/hle/kernel/mutex.h"
#include "core/hle/kernel/process.h"
@ -241,8 +242,6 @@ static ResultCode SendSyncRequest(Kernel::Handle handle) {
Core::System::GetInstance().PrepareReschedule();
// TODO(Subv): svcSendSyncRequest should put the caller thread to sleep while the server
// responds and cause a reschedule.
return session->SendSyncRequest(Kernel::GetCurrentThread());
}
@ -455,6 +454,33 @@ static ResultCode WaitSynchronizationN(s32* out, VAddr handles_address, s32 hand
}
}
static ResultCode ReceiveIPCRequest(Kernel::SharedPtr<Kernel::ServerSession> server_session,
Kernel::SharedPtr<Kernel::Thread> thread) {
if (server_session->parent->client == nullptr) {
return Kernel::ERR_SESSION_CLOSED_BY_REMOTE;
}
VAddr target_address = thread->GetCommandBufferAddress();
VAddr source_address = server_session->currently_handling->GetCommandBufferAddress();
ResultCode translation_result = Kernel::TranslateCommandBuffer(
server_session->currently_handling, thread, source_address, target_address);
// If a translation error occurred, immediately resume the client thread.
if (translation_result.IsError()) {
// Set the output of SendSyncRequest in the client thread to the translation output.
server_session->currently_handling->SetWaitSynchronizationResult(translation_result);
server_session->currently_handling->ResumeFromWait();
server_session->currently_handling = nullptr;
// TODO(Subv): This path should try to wait again on the same objects.
ASSERT_MSG(false, "ReplyAndReceive translation error behavior unimplemented");
}
return translation_result;
}
/// In a single operation, sends a IPC reply and waits for a new request.
static ResultCode ReplyAndReceive(s32* index, VAddr handles_address, s32 handle_count,
Kernel::Handle reply_target) {
@ -497,7 +523,15 @@ static ResultCode ReplyAndReceive(s32* index, VAddr handles_address, s32 handle_
return Kernel::ERR_SESSION_CLOSED_BY_REMOTE;
}
// TODO(Subv): Perform IPC translation from the current thread to request_thread.
VAddr source_address = Kernel::GetCurrentThread()->GetCommandBufferAddress();
VAddr target_address = request_thread->GetCommandBufferAddress();
ResultCode translation_result = Kernel::TranslateCommandBuffer(
Kernel::GetCurrentThread(), request_thread, source_address, target_address);
// Note: The real kernel seems to always panic if the Server->Client buffer translation
// fails for whatever reason.
ASSERT(translation_result.IsSuccess());
// Note: The scheduler is not invoked here.
request_thread->ResumeFromWait();
@ -526,14 +560,11 @@ static ResultCode ReplyAndReceive(s32* index, VAddr handles_address, s32 handle_
object->Acquire(thread);
*index = static_cast<s32>(std::distance(objects.begin(), itr));
if (object->GetHandleType() == Kernel::HandleType::ServerSession) {
auto server_session = static_cast<Kernel::ServerSession*>(object);
if (server_session->parent->client == nullptr)
return Kernel::ERR_SESSION_CLOSED_BY_REMOTE;
if (object->GetHandleType() != Kernel::HandleType::ServerSession)
return RESULT_SUCCESS;
// TODO(Subv): Perform IPC translation from the ServerSession to the current thread.
}
return RESULT_SUCCESS;
auto server_session = static_cast<Kernel::ServerSession*>(object);
return ReceiveIPCRequest(server_session, Kernel::GetCurrentThread());
}
// No objects were ready to be acquired, prepare to suspend the thread.
@ -556,10 +587,15 @@ static ResultCode ReplyAndReceive(s32* index, VAddr handles_address, s32 handle_
ASSERT(thread->status == THREADSTATUS_WAIT_SYNCH_ANY);
ASSERT(reason == ThreadWakeupReason::Signal);
thread->SetWaitSynchronizationResult(RESULT_SUCCESS);
thread->SetWaitSynchronizationOutput(thread->GetWaitObjectIndex(object.get()));
ResultCode result = RESULT_SUCCESS;
// TODO(Subv): Perform IPC translation upon wakeup.
if (object->GetHandleType() == Kernel::HandleType::ServerSession) {
auto server_session = Kernel::DynamicObjectCast<Kernel::ServerSession>(object);
result = ReceiveIPCRequest(server_session, thread);
}
thread->SetWaitSynchronizationResult(result);
thread->SetWaitSynchronizationOutput(thread->GetWaitObjectIndex(object.get()));
};
Core::System::GetInstance().PrepareReschedule();
@ -700,11 +736,22 @@ static ResultCode CreateThread(Kernel::Handle* out_handle, u32 priority, u32 ent
return Kernel::ERR_NOT_AUTHORIZED;
}
if (processor_id == THREADPROCESSORID_DEFAULT) {
// Set the target CPU to the one specified in the process' exheader.
processor_id = Kernel::g_current_process->ideal_processor;
ASSERT(processor_id != THREADPROCESSORID_DEFAULT);
}
switch (processor_id) {
case THREADPROCESSORID_ALL:
case THREADPROCESSORID_DEFAULT:
case THREADPROCESSORID_0:
break;
case THREADPROCESSORID_ALL:
LOG_INFO(Kernel_SVC,
"Newly created thread is allowed to be run in any Core, unimplemented.");
break;
case THREADPROCESSORID_1:
LOG_ERROR(Kernel_SVC,
"Newly created thread must run in the SysCore (Core1), unimplemented.");
break;
default:
// TODO(bunnei): Implement support for other processor IDs
@ -712,23 +759,6 @@ static ResultCode CreateThread(Kernel::Handle* out_handle, u32 priority, u32 ent
break;
}
if (processor_id == THREADPROCESSORID_ALL) {
LOG_INFO(Kernel_SVC,
"Newly created thread is allowed to be run in any Core, unimplemented.");
}
if (processor_id == THREADPROCESSORID_DEFAULT &&
Kernel::g_current_process->ideal_processor == THREADPROCESSORID_1) {
LOG_WARNING(
Kernel_SVC,
"Newly created thread is allowed to be run in the SysCore (Core1), unimplemented.");
}
if (processor_id == THREADPROCESSORID_1) {
LOG_ERROR(Kernel_SVC,
"Newly created thread must run in the SysCore (Core1), unimplemented.");
}
CASCADE_RESULT(SharedPtr<Thread> thread,
Kernel::Thread::Create(name, entry_point, priority, arg, processor_id, stack_top,
Kernel::g_current_process));