summaryrefslogtreecommitdiff
path: root/board/isee/common/igep_test.c
blob: f49fcfa3464ea7e504cce1bf6096c1453926136a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
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;
}