1 // Copyright (c) 2012- PPSSPP Project.
2 
3 // This program is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, version 2.0 or later versions.
6 
7 // This program is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10 // GNU General Public License 2.0 for more details.
11 
12 // A copy of the GPL 2.0 should have been included with the program.
13 // If not, see http://www.gnu.org/licenses/
14 
15 // Official git repository and contact information can be found at
16 // https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
17 
18 #pragma once
19 
20 #ifdef _MSC_VER
21 #pragma pack(push,1)
22 #endif
23 
24 // Based on https://playstationdev.wiki/psvitadevwiki/index.php?title=Error_Codes
25 #define	SCE_NP_ERROR_ALREADY_INITIALIZED				0x80550001
26 #define	SCE_NP_ERROR_NOT_INITIALIZED					0x80550002
27 #define	SCE_NP_ERROR_INVALID_ARGUMENT					0x80550003
28 
29 #define	SCE_NP_AUTH_ERROR_ALREADY_INITIALIZED			0x80550301
30 #define	SCE_NP_AUTH_ERROR_NOT_INITIALIZED				0x80550302
31 #define	SCE_NP_AUTH_ERROR_EINVAL						0x80550303
32 #define	SCE_NP_AUTH_ERROR_ENOMEM						0x80550304
33 #define	SCE_NP_AUTH_ERROR_ESRCH							0x80550305
34 #define	SCE_NP_AUTH_ERROR_EBUSY							0x80550306
35 #define	SCE_NP_AUTH_ERROR_ABORTED						0x80550307
36 #define	SCE_NP_AUTH_ERROR_INVALID_SERVICE_ID			0x80550308
37 #define	SCE_NP_AUTH_ERROR_INVALID_CREDENTIAL			0x80550309
38 #define	SCE_NP_AUTH_ERROR_INVALID_ENTITLEMENT_ID		0x8055030a
39 #define	SCE_NP_AUTH_ERROR_INVALID_DATA_LENGTH			0x8055030b
40 #define	SCE_NP_AUTH_ERROR_UNSUPPORTED_TICKET_VERSION	0x8055030c
41 #define	SCE_NP_AUTH_ERROR_STACKSIZE_TOO_SHORT			0x8055030d
42 #define	SCE_NP_AUTH_ERROR_TICKET_STATUS_CODE_INVALID	0x8055030e
43 #define	SCE_NP_AUTH_ERROR_TICKET_PARAM_NOT_FOUND		0x8055030f
44 #define	SCE_NP_AUTH_ERROR_INVALID_TICKET_VERSION		0x80550310
45 #define	SCE_NP_AUTH_ERROR_INVALID_ARGUMENT				0x80550311
46 
47 #define	SCE_NP_AUTH_ERROR_SERVICE_END					0x80550400
48 #define	SCE_NP_AUTH_ERROR_SERVICE_DOWN					0x80550401
49 #define	SCE_NP_AUTH_ERROR_SERVICE_BUSY					0x80550402
50 
51 // Based on https://github.com/RPCS3/rpcs3/blob/psp2/rpcs3/Emu/PSP2/Modules/sceNpCommon.h
52 enum SceNpServiceState : s32
53 {
54 	SCE_NP_SERVICE_STATE_UNKNOWN = 0,
55 	SCE_NP_SERVICE_STATE_SIGNED_OUT,
56 	SCE_NP_SERVICE_STATE_SIGNED_IN,
57 	SCE_NP_SERVICE_STATE_ONLINE
58 };
59 
60 struct SceNpCommunicationId
61 {
62 	char data[9];
63 	char term;
64 	u8 num;
65 	char dummy;
66 };
67 
68 struct SceNpCommunicationPassphrase
69 {
70 	u8 data[128];
71 };
72 
73 struct SceNpCommunicationSignature
74 {
75 	u8 data[160];
76 };
77 
78 struct SceNpCommunicationConfig
79 {
80 	PSPPointer<SceNpCommunicationId> commId;
81 	PSPPointer<SceNpCommunicationPassphrase> commPassphrase;
82 	PSPPointer<SceNpCommunicationSignature> commSignature;
83 };
84 
85 struct SceNpCountryCode
86 {
87 	char data[2];
88 	char term;
89 	char padding[1];
90 };
91 
92 // Username?
93 struct SceNpOnlineId
94 {
95 	char data[16];
96 	char term;
97 	char dummy[3];
98 };
99 
100 struct SceNpId
101 {
102 	SceNpOnlineId handle;
103 	u8 opt[8];
104 	u8 reserved[8];
105 };
106 
107 struct SceNpAvatarUrl
108 {
109 	char data[127];
110 	char term;
111 };
112 
113 struct SceNpUserInformation
114 {
115 	SceNpId userId;
116 	SceNpAvatarUrl icon;
117 	u8 reserved[52];
118 };
119 
120 struct SceNpMyLanguages
121 {
122 	s32_le language1;
123 	s32_le language2;
124 	s32_le language3;
125 	u8 padding[4];
126 };
127 
128 struct SceNpAvatarImage
129 {
130 	u8 data[200 * 1024];
131 	u32_le size;
132 	u8 reserved[12];
133 };
134 
135 enum SceNpAvatarSizeType : s32
136 {
137 	SCE_NP_AVATAR_SIZE_LARGE,
138 	SCE_NP_AVATAR_SIZE_MIDDLE,
139 	SCE_NP_AVATAR_SIZE_SMALL
140 };
141 
142 struct SceNpAboutMe
143 {
144 	char data[64];
145 };
146 
147 struct SceNpDate
148 {
149 	u16_le year;
150 	u8 month;
151 	u8 day;
152 };
153 
154 union SceNpTicketParam
155 {
156 	s32_le _s32;
157 	s64_le _s64;
158 	u32_le _u32;
159 	u64_le _u64;
160 	SceNpDate date;
161 	u8 data[256];
162 };
163 
164 struct SceNpTicketVersion
165 {
166 	u16_le major;
167 	u16_le minor;
168 };
169 
170 struct NpAuthHandler {
171 	u32 entryPoint;
172 	u32 argument;
173 };
174 
175 struct NpAuthArgs {
176 	u32_le data[3]; // id, result, ArgAddr
177 };
178 
179 using SceNpAuthCallback = s32(s32 id, s32 result, PSPPointer<void> arg);
180 
181 struct SceNpAuthRequestParameter
182 {
183 	u32_le size; // Size of this struct
184 	SceNpTicketVersion version; // Highest ticket version supported by this game/device? so PSN server can return supported ticket
185 	u32_le serviceIdAddr; //PSPPointer<char> serviceId; // null-terminated string
186 	u32_le cookieAddr; //PSPPointer<char> cookie; // null-terminated string?
187 	u32_le cookieSize;
188 	u32_le entitlementIdAddr; //PSPPointer<char> entitlementId; // null-terminated string
189 	u32_le consumedCount; // related to entitlement?
190 	u32 ticketCbAddr; //PSPPointer<SceNpAuthCallback> ticketCb
191 	u32_le cbArgAddr; //PSPPointer<void> cbArg
192 };
193 
194 struct SceNpEntitlementId
195 {
196 	u8 data[32];
197 };
198 
199 struct SceNpEntitlement
200 {
201 	SceNpEntitlementId id;
202 	u64_le createdDate;
203 	u64_le expireDate;
204 	u32_le type;
205 	s32_le remainingCount;
206 	u32_le consumedCount;
207 	u8 padding[4];
208 };
209 
210 #define	TICKET_VER_2_0	0x21000000;
211 #define	TICKET_VER_2_1	0x21010000;
212 #define	TICKET_VER_3_0	0x31000000;
213 #define	TICKET_VER_4_0	0x41000000;
214 
215 #define	NUMBER_PARAMETERS			12
216 
217 #define	PARAM_TYPE_NULL				0
218 #define	PARAM_TYPE_INT				1
219 #define	PARAM_TYPE_LONG				2
220 #define	PARAM_TYPE_STRING			4 // PSP returns maximum 255 bytes
221 #define	PARAM_TYPE_DATE				7
222 #define	PARAM_TYPE_STRING_ASCII		8 // PSP returns maximum 255 bytes, can contains control code
223 
224 #define	SECTION_TYPE_BODY			0x3000
225 #define	SECTION_TYPE_FOOTER			0x3002
226 
227 // Tickets data are in big-endian based on captured packets
228 struct SceNpTicketParamData
229 {
230 	u16_be type;
231 	u16_be length;
232 	//u8 value[]; // optional data
233 };
234 
235 struct SceNpTicketHeader
236 {
237 	u32_be version; // Version contents byte are: V1 0M 00 00, where V = major version, M = minor version according to https://www.psdevwiki.com/ps3/X-I-5-Ticket
238 	s32_be size; // total ticket size (excluding this 8-bytes header struct)
239 };
240 
241 // Section contents byte are: 30 XX 00 YY, where XX = section type, YY = section size according to https://www.psdevwiki.com/ps3/X-I-5-Ticket
242 // A section can contain other sections or param data, thus sharing their enum/def?
243 struct SceNpTicketSection
244 {
245 	u16_be type; // section type? ie. 30 XX where known XX are 00, 02, 10, 11
246 	u16_be size; // total section size (excluding this 4-bytes section delimiter struct)
247 };
248 
249 struct SceNpTicket
250 {
251 	SceNpTicketHeader header;
252 	SceNpTicketSection section; // Body or Parameter sections?
253 	//SceNpTicketParamData parameters[]; // a list of TicketParamData
254 	//u8 unknownBytes[]; // optional data?
255 };
256 
257 #define	PARENTAL_CONTROL_DISABLED	0
258 #define	PARENTAL_CONTROL_ENABLED	1
259 
260 #define	STATUS_ACCOUNT_SUSPENDED					0x80
261 #define	STATUS_ACCOUNT_CHAT_RESTRICTED				0x100
262 #define	STATUS_ACCOUNT_PARENTAL_CONTROL_ENABLED		0x200
263 
264 struct SceNpAuthMemoryStat {
265 	int npMemSize;     // Memory allocated by the NP utility.
266 	int npMaxMemSize;  // Maximum memory used by the NP utility.
267 	int npFreeMemSize; // Free memory available to use by the NP utility.
268 };
269 
270 #ifdef _MSC_VER
271 #pragma pack(pop)
272 #endif
273 
274 
275 extern std::recursive_mutex npAuthEvtMtx;
276 extern std::deque<NpAuthArgs> npAuthEvents;
277 extern std::map<int, NpAuthHandler> npAuthHandlers;
278 
279 void Register_sceNp();
280 void Register_sceNpCommerce2();
281 void Register_sceNpService();
282 void Register_sceNpAuth();
283