#ifndef __PSP_H__
#define __PSP_H__

#include <sys/syscall.h>
#include "syscall8.h"

typedef struct
{
	u32 tag;
//	u8 code;
	u8 *keys;
} PSPKey;

// Tags directly in little endian, no swap needed
// The tags the 3.55 emulator_drm directly supports
static u32 emulator_supported_tags355[] =
{
	0x00000008, /* 1.XX key_08000000 */
	0x7C16CBC0, /* 2.XX key_C0CB167C */
	0x03FD0480, /* 2.60-2.71 tag 00 u8 key_8004FD03[16] = {0xF4, 0xAE, 0xF4, 0xE1, 0x86, 0xDD, 0xD2, 0x9C, 0x7C, 0xC5, 0x42, 0xA6, 0x95, 0xA0, 0x83, 0x88};*/
	0xF00516D9, /* 2.80 tag 01 u8 key_D91605F0[16] = {0xB8, 0x8C, 0x45, 0x8B, 0xB6, 0xE7, 0x6E, 0xB8, 0x51, 0x59, 0xA6, 0x53, 0x7C, 0x5E, 0x86, 0x31};*/
	0xF00616D9, /* 3.00 tag 02 u8 key_D91606F0[16] = {0xED, 0x10, 0xE0, 0x36, 0xC4, 0xFE, 0x83, 0xF3, 0x75, 0x70, 0x5E, 0xF6, 0xA4, 0x40, 0x05, 0xF7};*/
	0xF00A16D9, /* 5.05 tag 05 u8 key_D9160AF0[16] = {0x10, 0xA9, 0xAC, 0x16, 0xAE, 0x19, 0xC0, 0x7E, 0x3B, 0x60, 0x77, 0x86, 0x01, 0x6F, 0xF2, 0x63};*/
	0xF00B16D9, /* 5.55 tag 06 u8 key_D9160BF0[16] = {0x83, 0x83, 0xF1, 0x37, 0x53, 0xD0, 0xBE, 0xFC, 0x8D, 0xA7, 0x32, 0x52, 0x46, 0x0A, 0xC2, 0xC2};*/
};

// Keys we add supports
static u8 key_D91608F0[] = {0x5C, 0x77, 0x0C, 0xBB, 0xB4, 0xC2, 0x4F, 0xA2, 0x7E, 0x3B, 0x4E, 0xB4, 0xB4, 0xC8, 0x70, 0xAF}; // 3.10
static u8 key_D91609F0[] = {0xD0, 0x36, 0x12, 0x75, 0x80, 0x56, 0x20, 0x43, 0xC4, 0x30, 0x94, 0x3E, 0x1C, 0x75, 0xD1, 0xBF};
static u8 key_D91611F0[] = {0x61, 0xB0, 0xC0, 0x58, 0x71, 0x57, 0xD9, 0xFA, 0x74, 0x67, 0x0E, 0x5C, 0x7E, 0x6E, 0x95, 0xB9};
static u8 key_D91612F0[] = {0x9E, 0x20, 0xE1, 0xCD, 0xD7, 0x88, 0xDE, 0xC0, 0x31, 0x9B, 0x10, 0xAF, 0xC5, 0xB8, 0x73, 0x23};
static u8 key_D91613F0[] = {0xEB, 0xFF, 0x40, 0xD8, 0xB4, 0x1A, 0xE1, 0x66, 0x91, 0x3B, 0x8F, 0x64, 0xB6, 0xFC, 0xB7, 0x12};
static u8 key_D91614F0[] = {0xFD, 0xF7, 0xB7, 0x3C, 0x9F, 0xD1, 0x33, 0x95, 0x11, 0xB8, 0xB5, 0xBB, 0x54, 0x23, 0x73, 0x85};
static u8 key_D91615F0[] = {0xC8, 0x03, 0xE3, 0x44, 0x50, 0xF1, 0xE7, 0x2A, 0x6A, 0x0D, 0xC3, 0x61, 0xB6, 0x8E, 0x5F, 0x51};
static u8 key_D91616F0[] = {0x53, 0x03, 0xB8, 0x6A, 0x10, 0x19, 0x98, 0x49, 0x1C, 0xAF, 0x30, 0xE4, 0x25, 0x1B, 0x6B, 0x28};
static u8 key_D91617F0[] = {0x02, 0xFA, 0x48, 0x73, 0x75, 0xAF, 0xAE, 0x0A, 0x67, 0x89, 0x2B, 0x95, 0x4B, 0x09, 0x87, 0xA3};
static u8 key_D91618F0[] = {0x96, 0x96, 0x7C, 0xC3, 0xF7, 0x12, 0xDA, 0x62, 0x1B, 0xF6, 0x9A, 0x9A, 0x44, 0x44, 0xBC, 0x48};
static u8 key_D91619F0[] = {0xE0, 0x32, 0xA7, 0x08, 0x6B, 0x2B, 0x29, 0x2C, 0xD1, 0x4D, 0x5B, 0xEE, 0xA8, 0xC8, 0xB4, 0xE9};
static u8 key_D9161AF0[] = {0x27, 0xE5, 0xA7, 0x49, 0x52, 0xE1, 0x94, 0x67, 0x35, 0x66, 0x91, 0x0C, 0xE8, 0x9A, 0x25, 0x24};
static u8 key_D91620F0[] = {0x52, 0x1C, 0xB4, 0x5F, 0x40, 0x3B, 0x9A, 0xDD, 0xAC, 0xFC, 0xEA, 0x92, 0xFD, 0xDD, 0xF5, 0x90};
static u8 key_D91621F0[] = {0xD1, 0x91, 0x2E, 0xA6, 0x21, 0x14, 0x29, 0x62, 0xF6, 0xED, 0xAE, 0xCB, 0xDD, 0xA3, 0xBA, 0xFE};
static u8 key_D91622F0[] = {0x59, 0x5D, 0x78, 0x4D, 0x21, 0xB2, 0x01, 0x17, 0x6C, 0x9A, 0xB5, 0x1B, 0xDA, 0xB7, 0xF9, 0xE6};
static u8 key_D91623F0[] = {0xAA, 0x45, 0xEB, 0x4F, 0x62, 0xFB, 0xD1, 0x0D, 0x71, 0xD5, 0x62, 0xD2, 0xF5, 0xBF, 0xA5, 0x2F};
static u8 key_D91624F0[] = {0x61, 0xB7, 0x26, 0xAF, 0x8B, 0xF1, 0x41, 0x58, 0x83, 0x6A, 0xC4, 0x92, 0x12, 0xCB, 0xB1, 0xE9};
static u8 key_D91628F0[] = {0x49, 0xA4, 0xFC, 0x66, 0xDC, 0xE7, 0x62, 0x21, 0xDB, 0x18, 0xA7, 0x50, 0xD6, 0xA8, 0xC1, 0xB6};
static u8 key_D91680F0[] = {0x2C, 0x22, 0x9B, 0x12, 0x36, 0x74, 0x11, 0x67, 0x49, 0xD1, 0xD1, 0x88, 0x92, 0xF6, 0xA1, 0xD8};
static u8 key_D91681F0[] = {0x52, 0xB6, 0x36, 0x6C, 0x8C, 0x46, 0x7F, 0x7A, 0xCC, 0x11, 0x62, 0x99, 0xC1, 0x99, 0xBE, 0x98};

// Missing Keys (av 20190711)
static u8 key_D91610F0[] = {0x89, 0x07, 0x73, 0xB4, 0x09, 0x08, 0x3F, 0x54, 0x31, 0x87, 0x00, 0xF3, 0x35, 0x14, 0x55, 0xCC}; // tag 07 3.60
static u8 key_D9161EF0[] = {0x5B, 0x4A, 0xD2, 0xF6, 0x49, 0xD4, 0xEB, 0x0D, 0xC0, 0x0F, 0xCB, 0xA8, 0x15, 0x2F, 0x55, 0x08}; // tag 18 5.00 PSP-3000
static u8 key_D91690F0[] = {0x42, 0x61, 0xE2, 0x57, 0x94, 0x49, 0x42, 0xB5, 0xAA, 0x6D, 0x0D, 0x08, 0x3D, 0x24, 0xF7, 0x4B}; // tag 27 6.60

/* 0x5D Game EBOOT.BIN keys */
static const PSPKey psp_extra_keys[] =
{
	{ 0xF00816D9, /* 0x5D, */ key_D91608F0 }, // tag 03 3.10
	{ 0xF00916D9, /* 0x5D, */ key_D91609F0 }, // tag 04 5.00
	{ 0xF01116D9, /* 0x5D, */ key_D91611F0 }, // tag 08 6.00
	{ 0xF01216D9, /* 0x5D, */ key_D91612F0 }, // tag 09
	{ 0xF01316D9, /* 0x5D, */ key_D91613F0 }, // tag 10
	{ 0xF01416D9, /* 0x5D, */ key_D91614F0 }, // tag 11
	{ 0xF01516D9, /* 0x5D, */ key_D91615F0 }, // tag 12
	{ 0xF01616D9, /* 0x5D, */ key_D91616F0 }, // tag 13
	{ 0xF01716D9, /* 0x5D, */ key_D91617F0 }, // tag 14
	{ 0xF01816D9, /* 0x5D, */ key_D91618F0 }, // tag 15
	{ 0xF01916D9, /* 0x5D, */ key_D91619F0 }, // tag 16
	{ 0xF01A16D9, /* 0x5D, */ key_D9161AF0 }, // tag 17
	{ 0xF02016D9, /* 0x5D, */ key_D91620F0 }, // tag 19
	{ 0xF02116D9, /* 0x5D, */ key_D91621F0 }, // tag 20
	{ 0xF02216D9, /* 0x5D, */ key_D91622F0 }, // tag 21
	{ 0xF02316D9, /* 0x5D, */ key_D91623F0 }, // tag 22
	{ 0xF02416D9, /* 0x5D, */ key_D91624F0 }, // tag 23
	{ 0xF02816D9, /* 0x5D, */ key_D91628F0 }, // tag 24 5.70 PSPgo
	{ 0xF08016D9, /* 0x5D, */ key_D91680F0 }, // tag 25 6.30
	{ 0xF08116D9, /* 0x5D, */ key_D91681F0 }, // tag 26 6.50

	// Missing Keys (av 20190711)
	{ 0xF01016D9, /* 0x5D, */ key_D91610F0 }, // tag 07 3.60
	{ 0xF01E16D9, /* 0x5D, */ key_D9161EF0 }, // tag 18 5.00 PSP-3000
	{ 0xF09016D9, /* 0x5D, */ key_D91690F0 }, // tag 27 6.60
};


#define NUM_SUPPORTED_TAGS	(sizeof(emulator_supported_tags355) / sizeof(u32))
#define NUM_EXTRA_KEYS		(sizeof(psp_extra_keys) / sizeof(PSPKey))

static int sys_psp_set_umdfile(char *file, char *id, int prometheus)
{
	system_call_4(SC_COBRA_SYSCALL8, SYSCALL8_OPCODE_SET_PSP_UMDFILE, (u64)(u32)file, (u64)(u32)id, prometheus);
	return (int)p1;
}

static int sys_psp_set_decrypt_options(int decrypt_patch, u32 tag, u8 *keys, u8 code, u32 tag2, u8 *keys2, u8 code2)
{
	system_call_8(SC_COBRA_SYSCALL8, SYSCALL8_OPCODE_SET_PSP_DECRYPT_OPTIONS, decrypt_patch, tag, (u64)(u32)keys, code, tag2, (u64)(u32)keys2, code2);
	return (int)p1;
}
/*
static int sys_psp_change_emu_path(const char *path)
{
	system_call_2(SC_COBRA_SYSCALL8, SYSCALL8_OPCODE_PSP_CHANGE_EMU, (u64)(u32)path);
	return (int)p1;
}
*/
#endif /* __PSP_H__ */
