/*      
  _____     ___ ____ 
   ____|   |    ____|      PSX2 OpenSource Project
  |     ___|   |____       (c) 2008 jMk0Nm 
------------------------------------------------------------------------
mcboot.h

*/

#ifndef _MCBOOT_H_
#define _MCBOOT_H_

#include <iopcontrol.h>
#include <iopheap.h>
#include <kernel.h>
#include <loadfile.h>
#include <libmc.h>
#include <libcdvd.h>
#include <libhdd.h>
#include <fileio.h>
#include <fileXio_rpc.h>
#include <malloc.h>
#include <ps2lib_err.h>
#include <libpad.h>
#include <sifrpc.h>
#include <stdio.h>
#include <string.h>
#include <gsKit.h>
#include <dmaKit.h>
#include <libjpg.h>
#include <tamtypes.h>
#include <debug.h>
#include <sbv_patches.h>
#include <smem.h>
#include <smod.h>


extern u32 size_mod_iomanx;
extern u32 size_mod_filexio;
extern u32 size_mod_usbd;
extern u32 size_mod_usb_mass;
extern u32 size_mod_poweroff;
extern u32 size_mod_mcsio2;
extern u32 size_mod_mcsp;
extern u32 size_icon_sys;
extern u32 size_icon_icn;
extern u32 size_boot_icon_sys;
extern u32 size_boot_icon_icn;
extern u32 size_cnf_icon_sys;
extern u32 size_cnf_icon_icn;
extern u32 size_background;
extern u32 size_launcher1;
extern u32 size_launcher2;
extern u32 size_freemcb_cnf;

extern void mod_iomanx;
extern void mod_filexio;
extern void mod_usbd;
extern void mod_usb_mass;
extern void mod_poweroff;
extern void mod_mcsio2;
extern void mod_mcsp;
extern void icon_sys;
extern void icon_icn;
extern void boot_icon_sys;
extern void boot_icon_icn;
extern void cnf_icon_sys;
extern void cnf_icon_icn;
extern void background;
extern void launcher1;
extern void launcher2;
extern void freemcb_cnf;


/* Encrypted file Data Block struct */
typedef struct {
	u32 size;               // Size of data block 
	u32 flags;				// Flags : 3 -> block to decrypt, 0 -> block to take in input file, 2 -> block to prepare.
    u8  checksum[8];
} Block_Data_t;

/* Encrypted file bit table struct */	
typedef struct {
	u32 headersize;          // header size (same as mg_ELF_Header_t->mg_header_size)
	u8 block_count;          // Number of encrypted blocks
	u8 pad1;
	u8 pad2;
	u8 pad3;
	Block_Data_t blocks[63]; // Block description, see Block_Data_t
} Bit_Data_t;

/* Encrypted file header struct */
typedef struct {
	u32 unknown1;             
    u32 unknown2;
    u16 unknown3_half;
    u16 version;
    u32 unknown4;
	u32 ELF_size;			 // Size of data blocks = Decrypted elf size
	u16 mg_header_size;      // header size
	u16 unknown5;
	u16 flags;               // ? for header purpose
	u16 BIT_count;           // ?  
	u32 mg_zones;
} mg_ELF_Header_t;

typedef struct {
	u32 unknown1;             
    u8  unknown2_1;
    u8  unknown2_2;
    u8  unknown2_3;
    u8  mg_region;
    u16 unknown3_half;
    u16 version;
    u32 unknown4;
} mg_ELF_region_t;


enum
{
	SCREEN_MARGIN = 16,
	FONT_WIDTH = 8,
	FONT_HEIGHT = 16,
	LINE_THICKNESS = 3,
	
	MAX_NAME = 256,
	MAX_PATH = 1025,
	MAX_ENTRY = 2048,
	MAX_PARTITIONS=500
};

#define TV_mode_AUTO 0
#define TV_mode_NTSC 1
#define TV_mode_PAL  2


#define MainTextColor GS_SETREG_RGBAQ(0xFF,0xFF,0xFF,0x80,0x00) // white
#define TextGreen     GS_SETREG_RGBAQ(0x00,0xFF,0x00,0x80,0x00)
#define TextRed       GS_SETREG_RGBAQ(0xFF,0x00,0x00,0x80,0x00)
#define TextTrans     GS_SETREG_RGBAQ(0x00,0x00,0x40,0x40,0x00) // some sort of blue night with transparancy
#define TextBlack     GS_SETREG_RGBAQ(0x00,0x00,0x40,0x80,0x00) // some sort of blue night without transparancy
#define ColorBlack    GS_SETREG_RGBAQ(0x00,0x00,0x00,0x00,0x00) // black
#define ColorTrans    GS_SETREG_RGBAQ(0x80,0x80,0x80,0xA0,0x00) // 0xA0 in alpha channel give some more color to icons

extern GSGLOBAL  *gsGlobal;
extern GSFONT    *gsFont;
extern GSTEXTURE TexSkin;
extern int       SCREEN_WIDTH;
extern int       SCREEN_HEIGHT;
extern int       SCREEN_X;
extern int       SCREEN_Y;

#define BUTTON_CROSS    1
#define BUTTON_SQUARE   2
#define BUTTON_CIRCLE   3
#define BUTTON_TRIANGLE 4
#define BUTTON_START    5
#define BUTTON_SELECT   6


#define SCANRATE (gsKit_detect_signal()==GS_MODE_NTSC ? 60:50)



/* -------- embed.c -------- */

int embed(u8 *decrypted_dvdelf, u8 *boot_elf, int sizebootelf, u8 *launcher2_elf, int sizelauncher2elf, u8 *output_elf);
int verify_blocks(u8 *decrypted_dvdelf, u8 *injected_dvdelf);


/* -------- build_osd.c -------- */

int build_OSD(u8 *original_dvdelf, u8 *injected_dvdelf, u8 *hacked_dvdelf);



/* -------- osdname.c -------- */

u8 *Get_OSD_Name(void);


/* -------- mcsp_rpc.c -------- */

#define MCSP_IRX 0x0A0A0A0

typedef struct {
	char name [16];
    u16 size_name;
    int page;
} fileentry;

typedef struct {
	u16 attr;                 
	u32 size;                 
	u16 entry_cluster;
	char name [16];          
} returnentry;


int mcsp_rpc_Init(void);
int MC_OSD_Scan(u16 port, u16 slot, char *file, u16 size);
int MC_Dummies_Patch(u16 port, u16 slot, void *file_entry, int num_entries, int linking_cluster, int filesize, void *return_entry);
int MC_Dummies_UnPatch(u16 port, u16 slot, void* uninstall_entry, u16 num_entries, u32 linked_filesize, u16 linked_cluster);


/* -------- mcid.c -------- */

int bit_offset(u8 *inbuf);
int mcsio2_rpc_Init(void);
int Card_Auth(void);
void *Encrypt_Disk_File(void *buf);
void *Decrypt_Disk_File(void *buf);
void *Decrypt_Card_File(void *buf);


/* -------- pad.c -------- */

#define PAD_R3_V0 0x010000
#define PAD_R3_V1 0x020000
#define PAD_R3_H0 0x040000
#define PAD_R3_H1 0x080000
#define PAD_L3_V0 0x100000
#define PAD_L3_V1 0x200000
#define PAD_L3_H0 0x400000
#define PAD_L3_H1 0x800000

extern u32 joy_value;
extern u32 Button;

int  Setup_Pad(void);
int  Read_Pad(void);
void Wait_Pad_Ready(void);



/* -------- draw.c -------- */

void Setup_GS(int gs_vmode);
void Update_Screen_Mode(void);
void Clear_Screen(void);
void Load_Skin(void);
void Draw_Screen(void);
void Draw_Sprite( u64 Color, int x1, int y1, int x2, int y2 );
void Print_XY(const unsigned char *s, int x, int y, u64 colour, float scale, float space);


/* -------- timer.c -------- */

void TimerInit(void);
u64  Timer(void);
void TimerEnd(void);

u64 WaitTime;


#endif

