//----------------------------------------------------------------------------------
// <copyright file="BootEEPROM.h" company="LiquidRobotics">
//	Copyright (c) Liquid Robotics Corporation.  All rights reserved.
// </copyright>
//
// <summary>
// 	Header file for items related to the boot section of the EEPROM
// </summary>
//
// <owner>Mike Cookson</owner>
//---------------------------------------------------------------------------------

#ifndef __BOOTEEPROM_H__
# define __BOOTEEPROM_H__

typedef struct s_boot_image_info
{
	char fileName[13];
	uint32_t imageLength;
	uint16_t imageCRC;
	uint8_t imageMD5[16];
} BOOT_IMAGE_INFO;

typedef struct s_boot_eeprom_vars
{
	BOOT_IMAGE_INFO selectedImage;			// info about the selected image
	BOOT_IMAGE_INFO fallbackImage;			// info about the fallback image
	uint8_t remainingFailuresAllowed;		// if tentative, the number of failures allowed before falling back
	uint8_t useFallback : 1,				// 0: boot loader should use selected image, 1: use  fallback image
			intentionalRestart : 1,			// 0: did not intend to restart, 1: restart was intentional
			activatedFallback : 1,			// 0: boot loader did not just activate fallback, 1: it did
			restartedByWatchdog : 1,		// 0: boot loader did not detect reset by watchdog or intentional, 1: it did
			tentativeImage : 1,				// 0: if selected image, it has not been verified yet, 1: it has been
			imageBurned : 1;				// 0: boot loader did not just reload the file, 1: it did
} BOOT_EEPROM_VARS;

typedef struct s_boot_eeprom
{
	BOOT_EEPROM_VARS bootVars;
	uint8_t space[128 - sizeof(BOOT_EEPROM_VARS) - sizeof(unsigned short)];
	uint16_t crc;
} BOOT_EEPROM;

extern uint8_t __eeprom_start;

#define BOOT_VARS(idx) ((BOOT_EEPROM*) 0x810000L)[(idx)]

int ReadBootEEPROM(BOOT_EEPROM_VARS* pVars);
void WriteBootEEPROM(BOOT_EEPROM_VARS* pVars);
uint16_t ComputeCRC(BOOT_EEPROM* pEEPROM);

#ifdef EXPAND_RAW_BOOT_FUNCTIONS
void ReadRawBootEEPROM(uint8_t idx, BOOT_EEPROM* pBuffer);
void WriteRawBootEEPROM(uint8_t idx, BOOT_EEPROM* pBuffer);
#endif

#endif
