/*
 * vm_linux/chiling/uboot/board/mt5398/mt5398_board.c
 *
 * Copyright (c) 2010-2012 MediaTek Inc.
 *      This file is based  ARM Realview platform
 *  Copyright (C) 2002 ARM Ltd.
 *  All Rights Reserved
 * $Author: xiaojun.zhu $
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 *
 */

#include <stdbool.h>
#include <common.h>
#include <exports.h>

#ifdef CC_SUPPORT_AB_UPDATE
#include <x_typedef.h>
#include <x_ldr_env.h>
#include "../include/configs/mt5893.h"
//#include <zlib.h>

//#define FLAG_RST_STRD_RB_INDEX_BASE (DRAM_START + 0x10000)

static UINT32 _flag_reset_storedRBindex = 0;


static UINT32 avb_calc_hash_flagResetSRI(LDR_RST_STRD_RB_INDEX_T *flag_reset_strdRBindx)
{
	UINT32 hash;

	hash = avb_crc32((const UINT8 *)&(flag_reset_strdRBindx->rFlgRstStrdRBIndx),
			 sizeof(LDR_RST_STRD_RB_INDEX_IN_T));

//	printf("Re-run RSRI: calclated hash is 0x%x\n", hash);
	return hash;
}

/* Check Flag reset stored rollback index validation
 * return value
 * true: Flag reset stored rollback index is valid
 * false: Flag reset stored rollback index is invalid
*/
static bool avb_is_valid_flagResetSRI(LDR_RST_STRD_RB_INDEX_T *flag_reset_strdRBindx)
{
	UINT32 hash_value;

	hash_value = avb_calc_hash_flagResetSRI(flag_reset_strdRBindx);
	if(hash_value == *((UINT32 *)(flag_reset_strdRBindx->hash)) &&
	   *((UINT32 *)(flag_reset_strdRBindx->rFlgRstStrdRBIndx.magic)) == FLAG_RST_STRD_RB_INDEX_MAGIC) {

		printf("Re-run RSRI: hash and magic is correct\n");
		return TRUE;
	}

	printf("Re-run RSRI: hash or magic is incorrect\n");
//	printf("Re-run RSRI: calc hash=0x%x, str hash=0x%x\n", 
//	       hash_value, *((UINT32 *)(flag_reset_strdRBindx->hash)));
//	printf("Re-run RSRI: strd magic=0x%x\n", *((UINT32 *)(flag_reset_strdRBindx->rFlgRstStrdRBIndx.magic)));

	return FALSE;
}

/* Read Flag of reset stored rollback index
 * If Flag of reset stored rollback index is enabled, set internal flag
 * of reset stored rollback index.
 */
int avb_read_flag_reset_StoredRBI(void)
{
	LDR_RST_STRD_RB_INDEX_T *flag_clr_strdRBindx = FLAG_RST_STRD_RB_INDEX_BASE;
	uint8_t strdRBflag;
	bool ret;

	ret = avb_is_valid_flagResetSRI(flag_clr_strdRBindx);
	if (ret == false) {
//		printf("Re-run RSRI: Hash or Magic of clear stored rollback index flag is incorrect.\n");
		memset(flag_clr_strdRBindx, 0x00, sizeof(LDR_RST_STRD_RB_INDEX_T));
		_flag_reset_storedRBindex = 0;
		return 1;
	}

	_flag_reset_storedRBindex = 0;

	if(flag_clr_strdRBindx->rFlgRstStrdRBIndx.flag == 0x01) {
		_flag_reset_storedRBindex = FLAG_RST_STRD_RB_INDEX_MAGIC;
		printf("Re-run RSRI: Set flag of reset stored rollback index enabled\n");
	}

//	printf("Re-run RSRI: read and store clear flag\n");
	memset(flag_clr_strdRBindx, 0x00, sizeof(LDR_RST_STRD_RB_INDEX_T));

	return 0;
}

int get_package_ignore_flag(void)
{
  /* if flag of reset stored rollback index is enabled, clear all rollback index */
  if(_flag_reset_storedRBindex == FLAG_RST_STRD_RB_INDEX_MAGIC) {
//    printf("Re-run RSRI: True flag of reset stored rollback index\n");
    return 1;
  }

//  printf("Re-run RSRI: False flag of reset stored rollback index\n");
  return 0;
}

int avb_clearPackageFlag(void)
{
  /* clear flag for safe */
   _flag_reset_storedRBindex = 0;

//  printf("Re-run RSRI: Clear flag of reset stored rollback index\n");
   return 0;
}
#endif /* CC_SUPPORT_AB_UPDATE */


