-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
TitleContents.cpp
188 lines (153 loc) · 5.81 KB
/
TitleContents.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// Copyright 2017 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#include "Core/IOS/ES/ES.h"
#include <vector>
#include "Common/Logging/Log.h"
#include "Core/HW/Memmap.h"
#include "Core/IOS/ES/Formats.h"
#include "Core/IOS/FS/FileSystemProxy.h"
#include "Core/IOS/Uids.h"
#include "Core/System.h"
namespace IOS::HLE
{
s32 ESCore::OpenContent(const ES::TMDReader& tmd, u16 content_index, u32 uid, Ticks ticks)
{
const u64 title_id = tmd.GetTitleId();
ES::Content content;
if (!tmd.GetContent(content_index, &content))
return ES_EINVAL;
for (size_t i = 0; i < m_content_table.size(); ++i)
{
OpenedContent& entry = m_content_table[i];
if (entry.m_opened)
continue;
const std::string path = GetContentPath(title_id, content, ticks);
auto fd = m_ios.GetFSCore().Open(PID_KERNEL, PID_KERNEL, path, FS::Mode::Read, {}, ticks);
if (fd.Get() < 0)
return fd.Get();
entry.m_opened = true;
entry.m_fd = fd.Release();
entry.m_content = content;
entry.m_title_id = title_id;
entry.m_uid = uid;
INFO_LOG_FMT(IOS_ES,
"OpenContent: title ID {:016x}, UID {:#x}, content {:08x} (index {}) -> CFD {}",
title_id, uid, content.id, content_index, i);
return static_cast<s32>(i);
}
return FS_EFDEXHAUSTED;
}
IPCReply ESDevice::OpenContent(u32 uid, const IOCtlVRequest& request)
{
return MakeIPCReply(IPC_OVERHEAD_TICKS, [&](Ticks ticks) -> s32 {
if (!request.HasNumberOfValidVectors(3, 0) || request.in_vectors[0].size != sizeof(u64) ||
request.in_vectors[1].size != sizeof(ES::TicketView) ||
request.in_vectors[2].size != sizeof(u32))
{
return ES_EINVAL;
}
auto& system = GetSystem();
auto& memory = system.GetMemory();
const u64 title_id = memory.Read_U64(request.in_vectors[0].address);
const u32 content_index = memory.Read_U32(request.in_vectors[2].address);
// TODO: check the ticket view, check permissions.
const auto tmd = m_core.FindInstalledTMD(title_id, ticks);
if (!tmd.IsValid())
return FS_ENOENT;
return m_core.OpenContent(tmd, content_index, uid, ticks);
});
}
IPCReply ESDevice::OpenActiveTitleContent(u32 caller_uid, const IOCtlVRequest& request)
{
return MakeIPCReply(IPC_OVERHEAD_TICKS, [&](Ticks ticks) -> s32 {
if (!request.HasNumberOfValidVectors(1, 0) || request.in_vectors[0].size != sizeof(u32))
return ES_EINVAL;
auto& system = GetSystem();
auto& memory = system.GetMemory();
const u32 content_index = memory.Read_U32(request.in_vectors[0].address);
if (!m_core.m_title_context.active)
return ES_EINVAL;
ES::UIDSys uid_map{GetEmulationKernel().GetFSCore()};
const u32 uid = uid_map.GetOrInsertUIDForTitle(m_core.m_title_context.tmd.GetTitleId());
ticks.Add(uid_map.GetTicks());
if (caller_uid != 0 && caller_uid != uid)
return ES_EACCES;
return m_core.OpenContent(m_core.m_title_context.tmd, content_index, caller_uid, ticks);
});
}
s32 ESCore::ReadContent(u32 cfd, u8* buffer, u32 size, u32 uid, Ticks ticks)
{
if (cfd >= m_content_table.size())
return ES_EINVAL;
OpenedContent& entry = m_content_table[cfd];
if (entry.m_uid != uid)
return ES_EACCES;
if (!entry.m_opened)
return IPC_EINVAL;
return m_ios.GetFSCore().Read(entry.m_fd, buffer, size, {}, ticks);
}
IPCReply ESDevice::ReadContent(u32 uid, const IOCtlVRequest& request)
{
return MakeIPCReply(IPC_OVERHEAD_TICKS, [&](Ticks ticks) -> s32 {
if (!request.HasNumberOfValidVectors(1, 1) || request.in_vectors[0].size != sizeof(u32))
return ES_EINVAL;
auto& system = GetSystem();
auto& memory = system.GetMemory();
const u32 cfd = memory.Read_U32(request.in_vectors[0].address);
const u32 size = request.io_vectors[0].size;
const u32 addr = request.io_vectors[0].address;
INFO_LOG_FMT(IOS_ES, "ReadContent(uid={:#x}, cfd={}, size={}, addr={:08x})", uid, cfd, size,
addr);
return m_core.ReadContent(cfd, memory.GetPointerForRange(addr, size), size, uid, ticks);
});
}
s32 ESCore::CloseContent(u32 cfd, u32 uid, Ticks ticks)
{
if (cfd >= m_content_table.size())
return ES_EINVAL;
OpenedContent& entry = m_content_table[cfd];
if (entry.m_uid != uid)
return ES_EACCES;
if (!entry.m_opened)
return IPC_EINVAL;
m_ios.GetFSCore().Close(entry.m_fd, ticks);
entry = {};
INFO_LOG_FMT(IOS_ES, "CloseContent: CFD {}", cfd);
return IPC_SUCCESS;
}
IPCReply ESDevice::CloseContent(u32 uid, const IOCtlVRequest& request)
{
return MakeIPCReply(IPC_OVERHEAD_TICKS, [&](Ticks ticks) -> s32 {
if (!request.HasNumberOfValidVectors(1, 0) || request.in_vectors[0].size != sizeof(u32))
return ES_EINVAL;
auto& system = GetSystem();
auto& memory = system.GetMemory();
const u32 cfd = memory.Read_U32(request.in_vectors[0].address);
return m_core.CloseContent(cfd, uid, ticks);
});
}
s32 ESCore::SeekContent(u32 cfd, u32 offset, SeekMode mode, u32 uid, Ticks ticks)
{
if (cfd >= m_content_table.size())
return ES_EINVAL;
OpenedContent& entry = m_content_table[cfd];
if (entry.m_uid != uid)
return ES_EACCES;
if (!entry.m_opened)
return IPC_EINVAL;
return m_ios.GetFSCore().Seek(entry.m_fd, offset, static_cast<FS::SeekMode>(mode), ticks);
}
IPCReply ESDevice::SeekContent(u32 uid, const IOCtlVRequest& request)
{
return MakeIPCReply(IPC_OVERHEAD_TICKS, [&](Ticks ticks) -> s32 {
if (!request.HasNumberOfValidVectors(3, 0))
return ES_EINVAL;
auto& system = GetSystem();
auto& memory = system.GetMemory();
const u32 cfd = memory.Read_U32(request.in_vectors[0].address);
const u32 offset = memory.Read_U32(request.in_vectors[1].address);
const auto mode = static_cast<SeekMode>(memory.Read_U32(request.in_vectors[2].address));
return m_core.SeekContent(cfd, offset, mode, uid, ticks);
});
}
} // namespace IOS::HLE