From 9045c57d6f88b8c828e325e8db75b19c4b4097bc Mon Sep 17 00:00:00 2001
From: JamePeng <jame_peng@sina.com>
Date: Sat, 9 Apr 2016 03:44:00 +0800
Subject: [PATCH] update the code of AM service! (#1623)

---
 src/core/hle/service/am/am.cpp     | 144 ++++++++++++++++++++++++++---
 src/core/hle/service/am/am.h       | 126 ++++++++++++++++++++++---
 src/core/hle/service/am/am_app.cpp |  16 ++--
 src/core/hle/service/am/am_net.cpp |  18 ++--
 src/core/hle/service/am/am_sys.cpp |  22 +++--
 src/core/hle/service/am/am_u.cpp   |  18 ++--
 6 files changed, 291 insertions(+), 53 deletions(-)

diff --git a/src/core/hle/service/am/am.cpp b/src/core/hle/service/am/am.cpp
index 06be9940e..9591522e5 100644
--- a/src/core/hle/service/am/am.cpp
+++ b/src/core/hle/service/am/am.cpp
@@ -2,6 +2,8 @@
 // Licensed under GPLv2 or any later version
 // Refer to the license.txt file included.
 
+#include <cinttypes>
+
 #include "common/logging/log.h"
 
 #include "core/hle/service/service.h"
@@ -9,30 +11,119 @@
 #include "core/hle/service/am/am_app.h"
 #include "core/hle/service/am/am_net.h"
 #include "core/hle/service/am/am_sys.h"
+#include "core/hle/service/am/am_u.h"
 
 namespace Service {
 namespace AM {
 
-void TitleIDListGetTotal(Service::Interface* self) {
+static std::array<u32, 3> am_content_count = { 0, 0, 0 };
+static std::array<u32, 3> am_titles_count = { 0, 0, 0 };
+static std::array<u32, 3> am_titles_list_count = { 0, 0, 0 };
+static u32 am_ticket_count = 0;
+static u32 am_ticket_list_count = 0;
+
+void GetTitleCount(Service::Interface* self) {
     u32* cmd_buff = Kernel::GetCommandBuffer();
+
     u32 media_type = cmd_buff[1] & 0xFF;
 
     cmd_buff[1] = RESULT_SUCCESS.raw;
-    cmd_buff[2] = 0;
-
-    LOG_WARNING(Service_AM, "(STUBBED) media_type %u", media_type);
+    cmd_buff[2] = am_titles_count[media_type];
+    LOG_WARNING(Service_AM, "(STUBBED) media_type=%u, title_count=0x%08x", media_type, am_titles_count[media_type]);
 }
 
-void GetTitleIDList(Service::Interface* self) {
+void FindContentInfos(Service::Interface* self) {
     u32* cmd_buff = Kernel::GetCommandBuffer();
-    u32 num_titles = cmd_buff[1];
-    u32 media_type = cmd_buff[2] & 0xFF;
-    u32 addr       = cmd_buff[4];
+
+    u32 media_type = cmd_buff[1] & 0xFF;
+    u64 title_id = (static_cast<u64>(cmd_buff[3]) << 32) | cmd_buff[2];
+    u32 content_ids_pointer = cmd_buff[6];
+    u32 content_info_pointer = cmd_buff[8];
+
+    am_content_count[media_type] = cmd_buff[4];
 
     cmd_buff[1] = RESULT_SUCCESS.raw;
-    cmd_buff[2] = 0;
+    LOG_WARNING(Service_AM, "(STUBBED) media_type=%u, title_id=0x%016lx, content_cound=%u, content_ids_pointer=0x%08x, content_info_pointer=0x%08x",
+                media_type, title_id, am_content_count[media_type], content_ids_pointer, content_info_pointer);
+}
 
-    LOG_WARNING(Service_AM, "(STUBBED) Requested %u titles from media type %u. Address=0x%08X", num_titles, media_type, addr);
+void ListContentInfos(Service::Interface* self) {
+    u32* cmd_buff = Kernel::GetCommandBuffer();
+
+    u32 media_type = cmd_buff[2] & 0xFF;
+    u64 title_id = (static_cast<u64>(cmd_buff[4]) << 32) | cmd_buff[3];
+    u32 start_index = cmd_buff[5];
+    u32 content_info_pointer = cmd_buff[7];
+
+    am_content_count[media_type] = cmd_buff[1];
+
+    cmd_buff[1] = RESULT_SUCCESS.raw;
+    cmd_buff[2] = am_content_count[media_type];
+    LOG_WARNING(Service_AM, "(STUBBED) media_type=%u, content_count=%u, title_id=0x%016" PRIx64 ", start_index=0x%08x, content_info_pointer=0x%08X",
+                media_type, am_content_count[media_type], title_id, start_index, content_info_pointer);
+}
+
+void DeleteContents(Service::Interface* self) {
+    u32* cmd_buff = Kernel::GetCommandBuffer();
+
+    u32 media_type = cmd_buff[1] & 0xFF;
+    u64 title_id = (static_cast<u64>(cmd_buff[3]) << 32) | cmd_buff[2];
+    u32 content_ids_pointer = cmd_buff[6];
+
+    am_content_count[media_type] = cmd_buff[4];
+
+    cmd_buff[1] = RESULT_SUCCESS.raw;
+    LOG_WARNING(Service_AM, "(STUBBED) media_type=%u, title_id=0x%016" PRIx64 ", content_count=%u, content_ids_pointer=0x%08x",
+        media_type, title_id, am_content_count[media_type], content_ids_pointer);
+}
+
+void GetTitleList(Service::Interface* self) {
+    u32* cmd_buff = Kernel::GetCommandBuffer();
+
+    u32 media_type = cmd_buff[2] & 0xFF;
+    u32 title_ids_output_pointer = cmd_buff[4];
+
+    am_titles_list_count[media_type] = cmd_buff[1];
+
+    cmd_buff[1] = RESULT_SUCCESS.raw;
+    cmd_buff[2] = am_titles_list_count[media_type];
+    LOG_WARNING(Service_AM, "(STUBBED) media_type=%u, titles_list_count=0x%08X, title_ids_output_pointer=0x%08X",
+                media_type, am_titles_list_count[media_type], title_ids_output_pointer);
+}
+
+void GetTitleInfo(Service::Interface* self) {
+    u32* cmd_buff = Kernel::GetCommandBuffer();
+
+    u32 media_type = cmd_buff[1] & 0xFF;
+    u32 title_id_list_pointer = cmd_buff[4];
+    u32 title_list_pointer = cmd_buff[6];
+
+    am_titles_count[media_type] = cmd_buff[2];
+
+    cmd_buff[1] = RESULT_SUCCESS.raw;
+    LOG_WARNING(Service_AM, "(STUBBED) media_type=%u, total_titles=0x%08X, title_id_list_pointer=0x%08X, title_list_pointer=0x%08X",
+                media_type, am_titles_count[media_type], title_id_list_pointer, title_list_pointer);
+}
+
+void GetDataTitleInfos(Service::Interface* self) {
+    GetTitleInfo(self);
+
+    LOG_WARNING(Service_AM, "(STUBBED) called");
+}
+
+void ListDataTitleTicketInfos(Service::Interface* self) {
+    u32* cmd_buff = Kernel::GetCommandBuffer();
+
+    u64 title_id = (static_cast<u64>(cmd_buff[3]) << 32) | cmd_buff[2];
+    u32 start_index = cmd_buff[4];
+    u32 ticket_info_pointer = cmd_buff[6];
+
+    am_ticket_count = cmd_buff[1];
+
+    cmd_buff[1] = RESULT_SUCCESS.raw;
+    cmd_buff[2] = am_ticket_count;
+    LOG_WARNING(Service_AM, "(STUBBED) ticket_count=0x%08X, title_id=0x%016" PRIx64 ", start_index=0x%08X, ticket_info_pointer=0x%08X",
+                 am_ticket_count, title_id, start_index, ticket_info_pointer);
 }
 
 void GetNumContentInfos(Service::Interface* self) {
@@ -40,16 +131,47 @@ void GetNumContentInfos(Service::Interface* self) {
 
     cmd_buff[1] = RESULT_SUCCESS.raw;
     cmd_buff[2] = 1; // Number of content infos plus one
-
     LOG_WARNING(Service_AM, "(STUBBED) called");
 }
 
+void DeleteTicket(Service::Interface* self) {
+    u32* cmd_buff = Kernel::GetCommandBuffer();
+
+    u64 title_id = (static_cast<u64>(cmd_buff[2]) << 32) | cmd_buff[1];
+
+    cmd_buff[1] = RESULT_SUCCESS.raw;
+    LOG_WARNING(Service_AM, "(STUBBED) called title_id=0x%016" PRIx64 "",title_id);
+}
+
+void GetTicketCount(Service::Interface* self) {
+    u32* cmd_buff = Kernel::GetCommandBuffer();
+
+    cmd_buff[1] = RESULT_SUCCESS.raw;
+    cmd_buff[2] = am_ticket_count;
+    LOG_WARNING(Service_AM, "(STUBBED) called ticket_count=0x%08x",am_ticket_count);
+}
+
+void GetTicketList(Service::Interface* self) {
+    u32* cmd_buff = Kernel::GetCommandBuffer();
+
+    u32 num_of_skip = cmd_buff[2];
+    u32 ticket_list_pointer = cmd_buff[4];
+
+    am_ticket_list_count = cmd_buff[1];
+
+    cmd_buff[1] = RESULT_SUCCESS.raw;
+    cmd_buff[2] = am_ticket_list_count;
+    LOG_WARNING(Service_AM, "(STUBBED) ticket_list_count=0x%08x, num_of_skip=0x%08x, ticket_list_pointer=0x%08x",
+                am_ticket_list_count, num_of_skip, ticket_list_pointer);
+}
+
 void Init() {
     using namespace Kernel;
 
     AddService(new AM_APP_Interface);
     AddService(new AM_NET_Interface);
     AddService(new AM_SYS_Interface);
+    AddService(new AM_U_Interface);
 }
 
 void Shutdown() {
diff --git a/src/core/hle/service/am/am.h b/src/core/hle/service/am/am.h
index 15e63bc7b..5676cdd5f 100644
--- a/src/core/hle/service/am/am.h
+++ b/src/core/hle/service/am/am.h
@@ -11,7 +11,7 @@ class Interface;
 namespace AM {
 
 /**
- * AM::TitleIDListGetTotal service function
+ * AM::GetTitleCount service function
  * Gets the number of installed titles in the requested media type
  *  Inputs:
  *      0 : Command header (0x00010040)
@@ -20,36 +20,140 @@ namespace AM {
  *      1 : Result, 0 on success, otherwise error code
  *      2 : The number of titles in the requested media type
  */
-void TitleIDListGetTotal(Service::Interface* self);
+void GetTitleCount(Service::Interface* self);
 
 /**
- * AM::GetTitleIDList service function
+ * AM::FindContentInfos service function
+ *  Inputs:
+ *      1 : MediaType
+ *    2-3 : u64, Title ID
+ *      4 : Content count
+ *      6 : Content IDs pointer
+ *      8 : Content Infos pointer
+ *  Outputs:
+ *      1 : Result, 0 on success, otherwise error code
+ */
+void FindContentInfos(Service::Interface* self);
+
+/**
+ * AM::ListContentInfos service function
+ *  Inputs:
+ *      1 : Content count
+ *      2 : MediaType
+ *    3-4 : u64, Title ID
+ *      5 : Start Index
+ *      7 : Content Infos pointer
+ *  Outputs:
+ *      1 : Result, 0 on success, otherwise error code
+ *      2 : Number of content infos returned
+ */
+void ListContentInfos(Service::Interface* self);
+
+/**
+ * AM::DeleteContents service function
+ *  Inputs:
+ *      1 : MediaType
+ *    2-3 : u64, Title ID
+ *      4 : Content count
+ *      6 : Content IDs pointer
+ *  Outputs:
+ *      1 : Result, 0 on success, otherwise error code
+ */
+void DeleteContents(Service::Interface* self);
+
+/**
+ * AM::GetTitleList service function
  * Loads information about the desired number of titles from the desired media type into an array
  *  Inputs:
- *      0 : Command header (0x00020082)
- *      1 : The maximum number of titles to load
+ *      1 : Title count
  *      2 : Media type to load the titles from
- *      3 : Descriptor of the output buffer pointer
- *      4 : Address of the output buffer
+ *      4 : Title IDs output pointer
  *  Outputs:
  *      1 : Result, 0 on success, otherwise error code
  *      2 : The number of titles loaded from the requested media type
  */
-void GetTitleIDList(Service::Interface* self);
+void GetTitleList(Service::Interface* self);
+
+/**
+ * AM::GetTitleInfo service function
+ *  Inputs:
+ *      1 : u8 Mediatype
+ *      2 : Total titles
+ *      4 : TitleIDList pointer
+ *      6 : TitleList pointer
+ *  Outputs:
+ *      1 : Result, 0 on success, otherwise error code
+ */
+void GetTitleInfo(Service::Interface* self);
+
+/**
+ * AM::GetDataTitleInfos service function
+ * Wrapper for AM::GetTitleInfo
+ *  Inputs:
+ *      1 : u8 Mediatype
+ *      2 : Total titles
+ *      4 : TitleIDList pointer
+ *      6 : TitleList pointer
+ *  Outputs:
+ *      1 : Result, 0 on success, otherwise error code
+ */
+void GetDataTitleInfos(Service::Interface* self);
+
+/**
+ * AM::ListDataTitleTicketInfos service function
+ *  Inputs:
+ *      1 : Ticket count
+ *    2-3 : u64, Title ID
+ *      4 : Start Index?
+ *      5 : (TicketCount * 24) << 8 | 0x4
+ *      6 : Ticket Infos pointer
+ *  Outputs:
+ *      1 : Result, 0 on success, otherwise error code
+ *      2 : Number of ticket infos returned
+ */
+void ListDataTitleTicketInfos(Service::Interface* self);
 
 /**
  * AM::GetNumContentInfos service function
  *  Inputs:
  *      0 : Command header (0x100100C0)
- *      1 : Unknown
- *      2 : Unknown
- *      3 : Unknown
+ *      1 : MediaType
+ *    2-3 : u64, Title ID
  *  Outputs:
  *      1 : Result, 0 on success, otherwise error code
  *      2 : Number of content infos plus one
  */
 void GetNumContentInfos(Service::Interface* self);
 
+/**
+ * AM::DeleteTicket service function
+ *  Inputs:
+ *    1-2 : u64, Title ID
+ *  Outputs:
+ *      1 : Result, 0 on success, otherwise error code
+ */
+void DeleteTicket(Service::Interface* self);
+
+/**
+ * AM::GetTicketCount service function
+ *  Outputs:
+ *      1 : Result, 0 on success, otherwise error code
+ *      2 : Total titles
+ */
+void GetTicketCount(Service::Interface* self);
+
+/**
+ * AM::GetTicketList service function
+ *  Inputs:
+ *      1 : Number of TicketList
+ *      2 : Number to skip
+ *      4 : TicketList pointer
+ *  Outputs:
+ *      1 : Result, 0 on success, otherwise error code
+ *      2 : Total TicketList
+ */
+void GetTicketList(Service::Interface* self);
+
 /// Initialize AM service
 void Init();
 
diff --git a/src/core/hle/service/am/am_app.cpp b/src/core/hle/service/am/am_app.cpp
index 16c76a1eb..d27b3defd 100644
--- a/src/core/hle/service/am/am_app.cpp
+++ b/src/core/hle/service/am/am_app.cpp
@@ -9,14 +9,14 @@ namespace Service {
 namespace AM {
 
 const Interface::FunctionInfo FunctionTable[] = {
-    {0x100100C0, GetNumContentInfos, "GetNumContentInfos"},
-    {0x10020104, nullptr, "FindContentInfos"},
-    {0x10030142, nullptr, "ListContentInfos"},
-    {0x10040102, nullptr, "DeleteContents"},
-    {0x10050084, nullptr, "GetDataTitleInfos"},
-    {0x10070102, nullptr, "ListDataTitleTicketInfos"},
-    {0x100900C0, nullptr, "IsDataTitleInUse"},
-    {0x100A0000, nullptr, "IsExternalTitleDatabaseInitialized"},
+    {0x100100C0, GetNumContentInfos,        "GetNumContentInfos"},
+    {0x10020104, FindContentInfos,          "FindContentInfos"},
+    {0x10030142, ListContentInfos,          "ListContentInfos"},
+    {0x10040102, DeleteContents,            "DeleteContents"},
+    {0x10050084, GetDataTitleInfos,         "GetDataTitleInfos"},
+    {0x10070102, ListDataTitleTicketInfos,  "ListDataTitleTicketInfos"},
+    {0x100900C0, nullptr,                   "IsDataTitleInUse"},
+    {0x100A0000, nullptr,                   "IsExternalTitleDatabaseInitialized"},
 };
 
 AM_APP_Interface::AM_APP_Interface() {
diff --git a/src/core/hle/service/am/am_net.cpp b/src/core/hle/service/am/am_net.cpp
index 065e04118..e75755245 100644
--- a/src/core/hle/service/am/am_net.cpp
+++ b/src/core/hle/service/am/am_net.cpp
@@ -9,16 +9,20 @@ namespace Service {
 namespace AM {
 
 const Interface::FunctionInfo FunctionTable[] = {
-    {0x00010040, TitleIDListGetTotal,   "TitleIDListGetTotal"},
-    {0x00020082, GetTitleIDList,        "GetTitleIDList"},
-    {0x00030084, nullptr,               "ListTitles"},
+    {0x00010040, GetTitleCount,         "GetTitleCount"},
+    {0x00020082, GetTitleList,          "GetTitleList"},
+    {0x00030084, GetTitleInfo,          "GetTitleInfo"},
     {0x000400C0, nullptr,               "DeleteApplicationTitle"},
     {0x000500C0, nullptr,               "GetTitleProductCode"},
-    {0x00080000, nullptr,               "TitleIDListGetTotal3"},
-    {0x00090082, nullptr,               "GetTitleIDList3"},
+    {0x000600C0, nullptr,               "GetTitleExtDataId"},
+    {0x00070080, DeleteTicket,          "DeleteTicket"},
+    {0x00080000, GetTicketCount,        "GetTicketCount"},
+    {0x00090082, GetTicketList,         "GetTicketList"},
     {0x000A0000, nullptr,               "GetDeviceID"},
-    {0x000D0084, nullptr,               "ListTitles2"},
-    {0x00140040, nullptr,               "FinishInstallToMedia"},
+    {0x000D0084, nullptr,               "GetPendingTitleInfo"},
+    {0x000E00C0, nullptr,               "DeletePendingTitle"},
+    {0x00140040, nullptr,               "FinalizePendingTitles"},
+    {0x00150040, nullptr,               "DeleteAllPendingTitles"},
     {0x00180080, nullptr,               "InitializeTitleDatabase"},
     {0x00190040, nullptr,               "ReloadDBS"},
     {0x001A00C0, nullptr,               "GetDSiWareExportSize"},
diff --git a/src/core/hle/service/am/am_sys.cpp b/src/core/hle/service/am/am_sys.cpp
index e38812297..8bad5e1c9 100644
--- a/src/core/hle/service/am/am_sys.cpp
+++ b/src/core/hle/service/am/am_sys.cpp
@@ -9,23 +9,27 @@ namespace Service {
 namespace AM {
 
 const Interface::FunctionInfo FunctionTable[] = {
-    {0x00010040, TitleIDListGetTotal,         "TitleIDListGetTotal"},
-    {0x00020082, GetTitleIDList,              "GetTitleIDList"},
-    {0x00030084, nullptr,                     "ListTitles"},
+    {0x00010040, GetTitleCount,               "GetTitleCount"},
+    {0x00020082, GetTitleList,                "GetTitleList"},
+    {0x00030084, GetTitleInfo,                "GetTitleInfo"},
     {0x000400C0, nullptr,                     "DeleteApplicationTitle"},
     {0x000500C0, nullptr,                     "GetTitleProductCode"},
-    {0x00080000, nullptr,                     "TitleIDListGetTotal3"},
-    {0x00090082, nullptr,                     "GetTitleIDList3"},
+    {0x000600C0, nullptr,                     "GetTitleExtDataId"},
+    {0x00070080, DeleteTicket,                "DeleteTicket"},
+    {0x00080000, GetTicketCount,              "GetTicketCount"},
+    {0x00090082, GetTicketList,               "GetTicketList"},
     {0x000A0000, nullptr,                     "GetDeviceID"},
-    {0x000D0084, nullptr,                     "ListTitles2"},
-    {0x00140040, nullptr,                     "FinishInstallToMedia"},
+    {0x000D0084, nullptr,                     "GetPendingTitleInfo"},
+    {0x000E00C0, nullptr,                     "DeletePendingTitle"},
+    {0x00140040, nullptr,                     "FinalizePendingTitles"},
+    {0x00150040, nullptr,                     "DeleteAllPendingTitles"},
     {0x00180080, nullptr,                     "InitializeTitleDatabase"},
     {0x00190040, nullptr,                     "ReloadDBS"},
     {0x001A00C0, nullptr,                     "GetDSiWareExportSize"},
     {0x001B0144, nullptr,                     "ExportDSiWare"},
     {0x001C0084, nullptr,                     "ImportDSiWare"},
-    {0x00230080, nullptr,                     "TitleIDListGetTotal2"},
-    {0x002400C2, nullptr,                     "GetTitleIDList2"}
+    {0x00230080, nullptr,                     "GetPendingTitleCount"},
+    {0x002400C2, nullptr,                     "GetPendingTitleList"}
 };
 
 AM_SYS_Interface::AM_SYS_Interface() {
diff --git a/src/core/hle/service/am/am_u.cpp b/src/core/hle/service/am/am_u.cpp
index c0392b754..d583dd9e6 100644
--- a/src/core/hle/service/am/am_u.cpp
+++ b/src/core/hle/service/am/am_u.cpp
@@ -9,16 +9,20 @@ namespace Service {
 namespace AM {
 
 const Interface::FunctionInfo FunctionTable[] = {
-    {0x00010040, TitleIDListGetTotal,         "TitleIDListGetTotal"},
-    {0x00020082, GetTitleIDList,              "GetTitleIDList"},
-    {0x00030084, nullptr,                     "ListTitles"},
+    {0x00010040, GetTitleCount,               "GetTitleCount"},
+    {0x00020082, GetTitleList,                "GetTitleList"},
+    {0x00030084, GetTitleInfo,                "GetTitleInfo"},
     {0x000400C0, nullptr,                     "DeleteApplicationTitle"},
     {0x000500C0, nullptr,                     "GetTitleProductCode"},
-    {0x00080000, nullptr,                     "TitleIDListGetTotal3"},
-    {0x00090082, nullptr,                     "GetTitleIDList3"},
+    {0x000600C0, nullptr,                     "GetTitleExtDataId"},
+    {0x00070080, DeleteTicket,                "DeleteTicket"},
+    {0x00080000, GetTicketCount,              "GetTicketCount"},
+    {0x00090082, GetTicketList,               "GetTicketList"},
     {0x000A0000, nullptr,                     "GetDeviceID"},
-    {0x000D0084, nullptr,                     "ListTitles2"},
-    {0x00140040, nullptr,                     "FinishInstallToMedia"},
+    {0x000D0084, nullptr,                     "GetPendingTitleInfo"},
+    {0x000E00C0, nullptr,                     "DeletePendingTitle"},
+    {0x00140040, nullptr,                     "FinalizePendingTitles"},
+    {0x00150040, nullptr,                     "DeleteAllPendingTitles"},
     {0x00180080, nullptr,                     "InitializeTitleDatabase"},
     {0x00190040, nullptr,                     "ReloadDBS"},
     {0x001A00C0, nullptr,                     "GetDSiWareExportSize"},