/*
 * diag_flash.c
 *
 * Matthew Millman
 */


#include <common.h>
#include <diag.h>

#ifdef DIAG_FLASH

int diag_erase_all_flash(int doit, int full)
{
	char key = 0;
	int quit = 0;

	if (!doit)
	{
		serial_puts( "\n  WARNING: This option will destroy the contents of the FLASH file system."
			"\n  Press Y to proceed, or any other key to return to the previous menu ==> " );
	
		key = serial_getc();
		
		if ((key >= 0x20) && (key <= 0x7E))		/*Printable ASCII character*/
		{
			serial_putc( key );
		}
	}

	if ( key == 'y' || key == 'Y' || doit )
	{
		diag_printf("\n\n");

		diag_do_flash_erase(full, &quit);
	}
	
	return 0;
}

void diag_flash_test( int full )
{
	char    key;
	u8	testStatus;
	int	quit;
	u32	testCount;
			
	serial_puts( "\n  WARNING: This option will destroy the contents of the FLASH file system."
	             "\n  Press Y to proceed, or any other key to return to the previous menu ==> " );

	key = serial_getc();

	if ((key >= 0x20) && (key <= 0x7E))
	{
		serial_putc( key );
	}

	if (key == 'y' || key == 'Y')
	{
		diag_printf("\n\n  %s FLASH test in progress - press Q to quit...\r\n", full ? "Full" : "Filesystem");

		testCount  = 0;
		quit = 0;
		
		while (1)
		{
			testStatus = 0;
			testCount++;
			
			diag_printf("\n  Performing test 1/2 with pseudorandom data...\n\n");

			if (diag_do_one_flash_test(MEMORY_TEST_RANDOM, full, &quit))
			{
				testStatus = 1;
				break;
			}
			
			if (quit)
			{
				testStatus = 0;
				break;
			}
			
			if (!testStatus)
			{
				diag_printf("\n  Result for test 1/2 (pass %u): PASS\n", testCount);
			}
			else
				break;
			
			diag_printf("\n  Performing test 2/2 with pseudorandom complemented data...\n\n");

			if (diag_do_one_flash_test(MEMORY_TEST_RANDOM_COMPLEMENTED, full, &quit))
			{
				testStatus = 1;
				break;
			}
				
			if (quit)
			{
				testStatus = 0;
				break;
			}
			
			if (!testStatus)
			{
				diag_printf("\n  Result for test 2/2 (pass %u): PASS\n", testCount);
			}
			else
				break;
			
		}
		if (testStatus)
		{
			diag_printf("  Result for test pass %u: FAIL\n", testCount);
			diag_printf("  Exiting flash test.\n\n");
		}
	}
}

#endif /* DIAG_FLASH */
