/*-------------------------------------------------------------

libcios.c -- Custom IOS library

Copyright (C) 2008 (waninkoko)

This software is provided 'as-is', without any express or implied
warranty.  In no event will the authors be held liable for any
damages arising from the use of this software.

Permission is granted to anyone to use this software for any
purpose, including commercial applications, and to alter it and
redistribute it freely, subject to the following restrictions:

1.	The origin of this software must not be misrepresented; you
must not claim that you wrote the original software. If you use
this software in a product, an acknowledgment in the product
documentation would be appreciated but is not required.

2.	Altered source versions must be plainly marked as such, and
must not be misrepresented as being the original software.

3.	This notice may not be removed or altered from any source
distribution.

-------------------------------------------------------------*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include <ogcsys.h>
#include <wiiuse/wpad.h>

#include "libcios.h"
#include "config.h"
#include "tools.h"

/* Ticket structure */
typedef struct {
	sig_rsa2048 signature;
	tik tik_data;
} ATTRIBUTE_PACKED signed_tik;


#define CERTS_SIZE	0xA00

/* Variables */
static const char certs_fs[] ATTRIBUTE_ALIGN(32) = "/sys/cert.sys";



/*
 * CIOS_Load() - Load Custom IOS
 */

s32 Load_IOS(u32 version)
{
	WPAD_Shutdown();
	/* Load Custom IOS */
	int ret;
	ret = IOS_ReloadIOS(version);

	PAD_Init();
	WPAD_Init();
	WPAD_SetDataFormat(WPAD_CHAN_0, WPAD_FMT_BTNS_ACC_IR);

	return ret;
}


/*
 * CIOS_IsLoaded() - Check if Custom IOS is loaded
 */

s32 CIOS_IsLoaded(void)
{
	/* Check if Custom IOS is loaded */
	return (IOS_GetVersion() == CIOS_VERSION);
}


/*
 * __CIOS_GetCerts() - Reads the system certificates
 */

s32 __CIOS_GetCerts(signed_blob **out, u32 *len)
{
	signed_blob *p_certs = NULL;
	s32 fd, ret;

	/* Open certificates file */
	fd = IOS_Open(certs_fs, ISFS_OPEN_READ);
	if (fd < 0)
		return fd;

	/* Allocate memory */
	p_certs = (signed_blob *)memalign(32, CERTS_SIZE);
	if (!p_certs) {
		ret = IPC_ENOMEM;
		goto err;
	}

	/* Read certificates */
	ret = IOS_Read(fd, p_certs, CERTS_SIZE);
	if (ret < 0)
		goto err;

	/* Set values */
	*out = p_certs;
	*len = CERTS_SIZE;

	ret = 0;
	goto out;

err:
	/* Free memory */
	if (p_certs)
		free(p_certs);

out:
	/* Close file */
	if (fd >= 0)
		IOS_Close(fd);

	return ret;
}
