2018-09-07 15:00:13 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018 Atmosphère-NX
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms and conditions of the GNU General Public License,
|
|
|
|
* version 2, as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
* more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2018-04-18 09:30:34 +00:00
|
|
|
#include <switch.h>
|
2019-03-28 22:06:50 +00:00
|
|
|
#include <stratosphere.hpp>
|
2018-04-18 09:30:34 +00:00
|
|
|
#include <algorithm>
|
2018-06-19 18:07:31 +00:00
|
|
|
#include <array>
|
2018-04-18 22:24:40 +00:00
|
|
|
#include <cstdio>
|
2018-04-17 23:41:57 +00:00
|
|
|
#include "ldr_launch_queue.hpp"
|
2018-06-19 18:07:31 +00:00
|
|
|
#include "meta_tools.hpp"
|
2018-04-17 23:41:57 +00:00
|
|
|
|
2019-04-05 04:05:41 +00:00
|
|
|
static std::array<LaunchQueue::LaunchItem, LAUNCH_QUEUE_SIZE> g_launch_queue = {};
|
2018-04-17 23:41:57 +00:00
|
|
|
|
2018-10-30 05:33:56 +00:00
|
|
|
Result LaunchQueue::Add(u64 tid, const char *args, u64 arg_size) {
|
2019-03-28 22:06:50 +00:00
|
|
|
if (arg_size > LAUNCH_QUEUE_ARG_SIZE_MAX) {
|
|
|
|
return ResultLoaderTooLongArgument;
|
2018-04-18 09:30:34 +00:00
|
|
|
}
|
|
|
|
|
2018-10-30 05:33:56 +00:00
|
|
|
int idx = GetFreeIndex(tid);
|
2019-03-28 22:06:50 +00:00
|
|
|
if (idx == LAUNCH_QUEUE_FULL) {
|
|
|
|
return ResultLoaderTooManyArguments;
|
|
|
|
}
|
2018-04-18 00:05:19 +00:00
|
|
|
|
2018-04-19 05:00:10 +00:00
|
|
|
g_launch_queue[idx].tid = tid;
|
|
|
|
g_launch_queue[idx].arg_size = arg_size;
|
2018-04-18 00:04:41 +00:00
|
|
|
|
2018-04-19 05:00:10 +00:00
|
|
|
std::copy(args, args + arg_size, g_launch_queue[idx].args);
|
2019-03-29 05:39:39 +00:00
|
|
|
return ResultSuccess;
|
2018-04-19 05:00:10 +00:00
|
|
|
}
|
|
|
|
|
2018-10-30 05:33:56 +00:00
|
|
|
Result LaunchQueue::AddCopy(u64 tid_base, u64 tid) {
|
|
|
|
int idx = GetIndex(tid_base);
|
2018-04-21 05:58:42 +00:00
|
|
|
if (idx == LAUNCH_QUEUE_FULL) {
|
2019-03-29 05:39:39 +00:00
|
|
|
return ResultSuccess;
|
2018-04-21 05:58:42 +00:00
|
|
|
}
|
|
|
|
|
2018-10-30 05:33:56 +00:00
|
|
|
return Add(tid, g_launch_queue[idx].args, g_launch_queue[idx].arg_size);
|
2018-04-21 05:58:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-10-30 05:33:56 +00:00
|
|
|
Result LaunchQueue::AddItem(const LaunchItem *item) {
|
2019-03-28 22:06:50 +00:00
|
|
|
if (item->arg_size > LAUNCH_QUEUE_ARG_SIZE_MAX) {
|
|
|
|
return ResultLoaderTooLongArgument;
|
2018-04-17 23:41:57 +00:00
|
|
|
}
|
|
|
|
|
2018-10-30 05:33:56 +00:00
|
|
|
int idx = GetFreeIndex(item->tid);
|
2019-03-28 22:06:50 +00:00
|
|
|
if (idx == LAUNCH_QUEUE_FULL) {
|
|
|
|
return ResultLoaderTooManyArguments;
|
|
|
|
}
|
2018-04-17 23:41:57 +00:00
|
|
|
|
2018-04-19 05:00:10 +00:00
|
|
|
g_launch_queue[idx] = *item;
|
2019-03-29 05:39:39 +00:00
|
|
|
return ResultSuccess;
|
2018-04-19 05:00:10 +00:00
|
|
|
}
|
|
|
|
|
2018-10-30 05:33:56 +00:00
|
|
|
int LaunchQueue::GetIndex(u64 tid) {
|
2018-06-19 18:07:31 +00:00
|
|
|
auto it = std::find_if(g_launch_queue.begin(), g_launch_queue.end(), member_equals_fn(&LaunchQueue::LaunchItem::tid, tid));
|
|
|
|
if (it == g_launch_queue.end()) {
|
|
|
|
return LAUNCH_QUEUE_FULL;
|
2018-04-19 05:00:10 +00:00
|
|
|
}
|
2018-06-19 18:07:31 +00:00
|
|
|
return std::distance(g_launch_queue.begin(), it);
|
2018-04-19 05:00:10 +00:00
|
|
|
}
|
|
|
|
|
2018-10-30 05:33:56 +00:00
|
|
|
int LaunchQueue::GetFreeIndex(u64 tid) {
|
2019-03-28 22:06:50 +00:00
|
|
|
for (unsigned int i = 0; i < LAUNCH_QUEUE_SIZE; i++) {
|
|
|
|
if (g_launch_queue[i].tid == tid || g_launch_queue[i].tid == 0x0) {
|
2018-04-19 05:00:10 +00:00
|
|
|
return i;
|
2018-04-18 00:04:41 +00:00
|
|
|
}
|
2018-04-17 23:41:57 +00:00
|
|
|
}
|
2018-04-19 05:00:10 +00:00
|
|
|
return LAUNCH_QUEUE_FULL;
|
|
|
|
}
|
|
|
|
|
2018-10-30 05:33:56 +00:00
|
|
|
bool LaunchQueue::Contains(u64 tid) {
|
|
|
|
return GetIndex(tid) != LAUNCH_QUEUE_FULL;
|
2018-04-19 05:00:10 +00:00
|
|
|
}
|
|
|
|
|
2018-10-30 05:33:56 +00:00
|
|
|
void LaunchQueue::Clear() {
|
2018-04-19 05:00:10 +00:00
|
|
|
for (unsigned int i = 0; i < LAUNCH_QUEUE_SIZE; i++) {
|
|
|
|
g_launch_queue[i].tid = 0;
|
|
|
|
}
|
|
|
|
}
|
2018-04-22 01:52:49 +00:00
|
|
|
|
|
|
|
|
2018-10-30 05:33:56 +00:00
|
|
|
LaunchQueue::LaunchItem *LaunchQueue::GetItem(u64 tid) {
|
|
|
|
int idx = GetIndex(tid);
|
2018-06-19 18:07:31 +00:00
|
|
|
if (idx == LAUNCH_QUEUE_FULL) {
|
2018-04-22 01:52:49 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return &g_launch_queue[idx];
|
2018-05-03 23:24:34 +00:00
|
|
|
}
|