diff options
Diffstat (limited to 'board/isee/common/igep_test.c')
-rw-r--r-- | board/isee/common/igep_test.c | 332 |
1 files changed, 332 insertions, 0 deletions
diff --git a/board/isee/common/igep_test.c b/board/isee/common/igep_test.c new file mode 100644 index 0000000..f49fcfa --- /dev/null +++ b/board/isee/common/igep_test.c @@ -0,0 +1,332 @@ +/* + * Copyright (C) 2016 ISEE 2007 SL - http://www.isee.biz + * + * Source file for Test functions to help board code + * + * Author: Jose Miguel Sanchez Sanabria <jsanabria@iseebcn.com> + * + * SPDX-License-Identifier: GPL-2.0+ + */ + +#include <common.h> +#include <i2c.h> +#include <asm/arch/sys_proto.h> +#include "igep_eeprom.h" +#include "igep_common.h" +#include "igep_test.h" + +int test_eeprom(void) +{ + u32 crc_value = 0; + u32 crc_save_value = 0; + //u32 *ptrt; + /* This is the TEST STRUCT INITIALIZED WITH KNOWN VALUES, KNOWN CRC TO TEST EEPROM */ + /* CRC was calcualted with same struct using 0 value in crc field, now set to 541599072*/ + struct igep_mf_setup test_struct = { + 1234567890, + 541599072, + "0b9e485e-dcfc-11e8-8f39-080027541785", + "120311D4EA9C35AC", + "IGEP1234", + "TEST-TEST", + "PROMAX", + "2019-01-24 11:59:31", + "02:00:00:00:00:01", + "02:de:fa:00:00:77" + }; + /* Auxiliar struct */ + struct igep_mf_setup aux_config; + + /* Here we will act different from normal u-boot, first we will write TEST structure and then we will read CRC and compare it with + known CRC */ + /* But first of all we need to read it and calculate it */ + if(check_eeprom() != 0){ + puts("EEPROM: not found\n"); + return 1; + }else{ + /* Write IGEP TEST STRUCT */ + if(eeprom_write_setup(EEPROM_TEST_STRUCT_OFF*32, (char*) &test_struct, sizeof(struct igep_mf_setup))){ + puts("EEPROM: write fail\n"); + return 1; + }else{ + /* Let's Read TEST STRUCT and put it in aux config to see how it went */ + if(eeprom_read_setup(EEPROM_TEST_STRUCT_OFF*32, (char*) &aux_config, sizeof(struct igep_mf_setup))){ + puts("EEPROM: read fail\n"); + return 1; + }else{ + + /* Store read crc32 */ + crc_save_value = aux_config.crc32; + //printf("Original crc32: 0x%x \n", test_struct.crc32); + //printf("Readed crc32: 0x%x \n", aux_config.crc32); + aux_config.crc32 = 0; + /* Fine now let's calculate the CRC32 of readed config and print it by screen */ + crc_value = crc32(0, (const unsigned char*) &aux_config, sizeof(struct igep_mf_setup)); + //printf("Calculated crc32: 0x%x \n", crc_value); + /* Now lets compare both CRC, in this TEST IT SHOULD ALWAYS BE OK! OTHERWISE EEPROM TEST FAILED */ + if(crc_save_value != crc_value){ + puts("EEPROM: CRC32 failed, EEPROM FAIL\n"); + return 1; + } + } + } + } + return 0; +} + +int burn_test(char* to_burn, uint64_t count, int offset) +{ + struct igep_bootloaders_test aux_config; + + if(check_eeprom() != 0){ + puts("EEPROM: not found\n"); + return 1; + }else{ + if(eeprom_write_setup((offset+count)*32, to_burn, sizeof(struct igep_bootloaders_test))){ + puts("EEPROM: write fail\n"); + return 1; + }else{ + if(eeprom_read_setup((offset+count)*32, (char*) &aux_config, sizeof(struct igep_bootloaders_test))){ + printf("EEPROM: read fail\n"); + }else{ + printf("EEPROM: read %d bytes \n", sizeof(struct igep_bootloaders_test)); + printf("---------------------------- |!| TEST STRUCT |!| ----------------------------\n"); + printf("Test Magic_id: 0x%x \n", aux_config.magic_id); + printf("Test Result: 0x%x \n", aux_config.test); + printf("CPUID: 0x%llx \n", aux_config.cpuid); + printf("crc32: 0x%x \n", aux_config.crc32); + printf("-----------------------------------------------------------------------------\n"); + } + } + } + return 0; +} + +int burn_test_counter(char* to_burn, int offset) +{ + struct igep_bootloaders_counter aux_config; + + if(check_eeprom() != 0){ + puts("EEPROM: not found\n"); + return 1; + }else{ + if(eeprom_write_setup(offset*32, to_burn, sizeof(struct igep_bootloaders_counter))){ + puts("EEPROM: write fail\n"); + return 1; + }else{ + if(eeprom_read_setup(offset*32, (char*) &aux_config, sizeof(struct igep_bootloaders_counter))){ + printf("EEPROM: read fail\n"); + }else{ + printf("EEPROM: read %d bytes \n", sizeof(struct igep_bootloaders_counter)); + printf("---------------------------- |!| TEST COUNTER STRUCT |!| ----------------------------\n"); + printf("Test Magic_id: 0x%x \n", aux_config.magic_id); + printf("Number of Tests: 0x%llx \n", aux_config.count); + printf("crc32: 0x%x \n", aux_config.crc32); + printf("-----------------------------------------------------------------------------\n"); + } + } + } + return 0; +} + +int test_counter_check(int offset) +{ + u32 crc_value = 0; + u32 crc_save_value = 0; + struct igep_bootloaders_counter aux_config; + + if(check_eeprom() != 0){ + puts("EEPROM: not found\n"); + return 1; + }else{ + if(eeprom_read_setup(offset*32, (char*) &aux_config, sizeof(struct igep_bootloaders_counter))){ + printf("EEPROM: read fail\n"); + return 1; + }else{ + printf("EEPROM: read %d bytes \n", sizeof(struct igep_bootloaders_counter)); + printf("---------------------------- |!| TEST COUNTER STRUCT |!| ----------------------------\n"); + printf("Counter Magic_id: 0x%x \n", aux_config.magic_id); + printf("Number of Tests: 0x%llx \n", aux_config.count); + printf("crc32: 0x%x \n", aux_config.crc32); + printf("-----------------------------------------------------------------------------\n"); + crc_save_value = aux_config.crc32; + aux_config.crc32 = 0; + crc_value = crc32(0, (const unsigned char*) &aux_config, sizeof(struct igep_bootloaders_counter)); + if(crc_save_value != crc_value){ + puts("EEPROM: CRC32 failed, EEPROM FAIL\n"); + return 1; + } + } + } + return 0; +} + +uint64_t test_counter_get(int offset) +{ + struct igep_bootloaders_counter aux_config; + check_eeprom(); + eeprom_read_setup(offset*32, (char*) &aux_config, sizeof(struct igep_bootloaders_counter)); + printf("EEPROM: read %d bytes \n", sizeof(struct igep_bootloaders_counter)); + printf("---------------------------- |!| TEST COUNTER STRUCT |!| ----------------------------\n"); + printf("Counter magic_id: 0x%x \n", aux_config.magic_id); + printf("Number of Tests: 0x%llx \n", aux_config.count); + printf("crc32: 0x%x \n", aux_config.crc32); + printf("-----------------------------------------------------------------------------\n"); + return aux_config.count; +} + +int save_test(uint16_t test, uint32_t id, uint64_t cpuid) +{ + struct igep_bootloaders_test test_to_burn; + struct igep_bootloaders_counter test_counter_to_burn; + int crc_value = 0; + uint64_t counter = 0; + uint32_t starting_off = 0; + uint32_t counter_off = 0; + uint32_t counter_id = 0; + uint64_t offset = 0; + + if (id == SPL_MAGIC_ID){ + /* SPL CASE */ + starting_off = SPL_START_TEST_OFF; + counter_off = SPL_TEST_COUNTER_OFF; + counter_id = SPL_MAGIC_ID_COUNTER; + }else { + /* UB CASE */ + starting_off = UB_START_TEST_OFF; + counter_off = UB_TEST_COUNTER_OFF; + counter_id = UB_MAGIC_ID_COUNTER; + } + + /* Prepare final struct (16 bytes) */ + test_to_burn.magic_id = id; + test_to_burn.test = test; + test_to_burn.cpuid = cpuid; + test_to_burn.crc32 = 0; + /* Calculate crc and final set up */ + crc_value = crc32(0, (const unsigned char*) &test_to_burn, sizeof(struct igep_bootloaders_test)); + test_to_burn.crc32 = crc_value; + + /* Erase counter if needed */ + //erase_eeprom_counter(counter_id, counter_off); + + /* Check SPL Test Counter struct integrity */ + if (test_counter_check(counter_off)){ + puts("COUNTER INTEGRITY FAIL.\n"); + erase_eeprom_counter(counter_id, counter_off); + } + /* Get counter */ + puts("COUNTER INTEGRITY OK.\n"); + counter = test_counter_get(counter_off); + /* Do module to get circular offset */ + offset = counter % UB_START_TEST_OFF; + + /*if UB add natural offset + if (id == UB_MAGIC_ID){ + offset = counter + UB_START_TEST_OFF; + } + */ + + printf("Counter: 0x%llx\n",counter); + printf("Offset: 0x%llx\n",offset); + + /* Burn TEST in offset addr */ + if (burn_test((char*)&test_to_burn, offset, starting_off)){ + puts("BURN TEST FAIL.\n"); + return 1; + }else{ + puts("BURN TEST OK.\n"); + /* add 1 to counter and burn spl test counter */ + counter = counter + 1; + printf("Now Counter is: 0x%llx\n",counter); + + test_counter_to_burn.magic_id = counter_id; + test_counter_to_burn.count = counter; + test_counter_to_burn.crc32 = 0; + /* Calculate crc and final set up */ + crc_value = crc32(0, (const unsigned char*) &test_counter_to_burn, sizeof(struct igep_bootloaders_counter)); + test_counter_to_burn.crc32 = crc_value; + /* Burn updated counter */ + if (burn_test_counter((char*)&test_counter_to_burn, counter_off)){ + puts("ISEE TEST FAIL.\n"); + return 1; + } + } + return 0; +} + +uint16_t load_test(uint32_t id) +{ + uint64_t counter = 0; + uint32_t counter_off = 0; + struct igep_bootloaders_test aux_config; + uint64_t offset = 0; + + if (id == SPL_MAGIC_ID_COUNTER){ + /* SPL CASE */ + counter_off = SPL_TEST_COUNTER_OFF; + }else { + /* UB CASE */ + counter_off = UB_TEST_COUNTER_OFF; + } + + /* Check SPL Test Counter struct integrity */ + if (test_counter_check(counter_off)){ + puts("COUNTER INTEGRITY FAIL.\n"); + return 0x4000; + }else{ + + /* Get counter */ + puts("COUNTER INTEGRITY OK.\n"); + counter = test_counter_get(counter_off); + + /* Do module to get circular offset */ + offset = counter % UB_START_TEST_OFF; + + if (offset!=0) + { + offset = offset - 1; + } + + if (id == UB_MAGIC_ID_COUNTER){ + /* Add UB natural offset */ + offset = offset + UB_START_TEST_OFF; + } + + printf("Counter: 0x%llx\n",counter); + printf("Offset: 0x%llx\n",offset); + + /* Obtain test at offset counter */ + if(eeprom_read_setup((offset)*32, (char*) &aux_config, sizeof(struct igep_bootloaders_test))){ + printf("EEPROM: read fail\n"); + return 0x4000; + }else{ + printf("EEPROM: read %d bytes \n", sizeof(struct igep_bootloaders_test)); + printf("---------------------------- |!| TEST STRUCT |!| ----------------------------\n"); + printf("Test Magic_id: 0x%x \n", aux_config.magic_id); + printf("Test Result: 0x%x \n", aux_config.test); + printf("CPUID: 0x%llx \n", aux_config.cpuid); + printf("crc32: 0x%x \n", aux_config.crc32); + printf("-----------------------------------------------------------------------------\n"); + return aux_config.test; + } + } +} + + + +int erase_eeprom_counter(uint32_t id, int counter_off){ + + int crc_value = 0; + struct igep_bootloaders_counter test_counter_to_burn; + /* This will erase counter and enable a default one of 0, uncomment it to test */ + puts("Creating default counter struct.\n"); + test_counter_to_burn.magic_id = id; + test_counter_to_burn.count = 0; + test_counter_to_burn.crc32 = 0; + crc_value = crc32(0, (const unsigned char*) &test_counter_to_burn, sizeof(struct igep_bootloaders_counter)); + test_counter_to_burn.crc32 = crc_value; + burn_test_counter((char*)&test_counter_to_burn,counter_off); + puts("Creation OK.\n"); + return 0; +}
\ No newline at end of file |