summaryrefslogtreecommitdiff
path: root/common/env_otp.c
blob: 98d7c25112582d93981880381872dbb5c0ad35eb (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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
#include <common.h>
#include <command.h>
#include <environment.h>
#include <serial.h>
#include <linux/stddef.h>
#include <asm/byteorder.h>
#include <linux/aes.h>
#include <linux/rsa/base64.h>
#include <search.h>
#include <errno.h>
#include "../common/wmt_efuse/wmt_efuse.h"

DECLARE_GLOBAL_DATA_PTR;

#define CIPHER_IV \
{ \
  'W', 'O', 'N', 'D', \
  'E', 'R', 'M', 'E', \
  'D', 'I', 'A',   3, \
    7,   6, 'A', 'F'  \
}


#define THE_KEY \
{ \
  'W', 'M', 'T', 'K', \
  'E', 'Y',  1,   7,  \
    4,   4,  0,   3, \
    7,   6, 'A', 'F'  \
}

#define MAX_OTP_VAL_SIZE 260
#define MAX_OTP_VAL_BASE64_SIZE 512
#define MAX_NAME_SIZE (256)
#define MAX_VALUE_SIZE (4*1024)

extern env_t *env_ptr;
extern env_t *flash_addr;

extern env_t *env_ptr2 ;
extern env_t *flash_addr2 ;
extern struct hsearch_data env_htab ;

int encdec(int mode, unsigned char *key, size_t keybytes, unsigned char * data, size_t databytes, unsigned char *output){
    aes_context ctx;
	unsigned char iv[16]=CIPHER_IV;
	
	if(mode != AES_ENCRYPT && mode != AES_DECRYPT)
		return -1;

    if(mode == AES_ENCRYPT){
        if(aes_setkey_enc( &ctx, key, keybytes * 8) != 0){
             printf("invalid key size\n");
             return -1;
        }
    }else{
        if(aes_setkey_dec( &ctx, key, keybytes * 8) != 0){
             printf("invalid key size\n");
             return -1;                 
        }
    }
	
    if(aes_crypt_cbc(&ctx, mode, databytes, iv, data, output) != 0){
		return -1;
	}

    return 0;
}

//aes128
int genkey(unsigned char key[16]){
	//unsigned char *chipid = (unsigned char*)otp_getenv("otp.chipid");
    unsigned char chipid[8]={0};
	unsigned char chipidstr[17]={0};
	unsigned char id[16]={0};
	unsigned char thekey[16]=THE_KEY;
	int i = 0;
	
	efuse_read_otp(OTP_CPUID, chipid, sizeof(chipid));
	for(i = 0; i < sizeof(chipid); i++){
	    sprintf(chipidstr+i*2, "%02X", chipid[i]);
	}
    printf("chipidstr is %s\n", chipidstr);

    //if(chipid == NULL) return -1;
	
	int len = strlen((char*)chipidstr);
	if(len > sizeof(id)) len = sizeof(id);
	strncpy((char*)id, (char*)chipidstr, len);
	return encdec(AES_ENCRYPT, thekey, sizeof(thekey), id, sizeof(id), key);
}


int otp_sec_encode(uchar *in, int ilen, uchar *out, size_t *olen)
{
    //aes128
    unsigned char key[16]={0};
	unsigned char val[MAX_OTP_VAL_SIZE]={0};
	unsigned char cypval[MAX_OTP_VAL_SIZE]={0};
	unsigned char *p,*s ;
	size_t  vallen=0;
    
	if(genkey(key) < 0){
		printf("fail to gen key\n");
		return 1;
	}

    if(ilen > MAX_OTP_VAL_SIZE-1){
        printf("env value is too long\n");
	    return 1;
    }
    
    p = in;
    s = val;
    while((*s++ = *p++) != '\0')
        ;
	
    vallen = ilen;
	//16 bytes aligned
	vallen = ((vallen + 15) / 16) * 16;

	printf("val is %s, vallen is %d\n",in, vallen);
	if(encdec(AES_ENCRYPT, key, sizeof(key), val, vallen, cypval) < 0){
		printf("encode value fail\n");
		return 1;
	}

	if(base64_encode(out, olen, cypval, vallen) != 0){
        printf("base64 encode fail\n");
		return 1;
	}
    
    return 0;
   
}

int otp_sec_decode(const char *val, uchar * theval)
{
	if( val != NULL && theval  != NULL){
		unsigned char key[16]={0};
		unsigned char cypval[MAX_OTP_VAL_SIZE]={0};
        size_t vallen = MAX_OTP_VAL_SIZE; 
        
		if(base64_decode(cypval, &vallen, val, strlen(val)) != 0){
			printf("base64 decode fail\n");
			return -1;			
		}		
        
		if(genkey(key) < 0){
			printf("fail to gen key\n");
			return -1;
		}

		if(vallen % 16){
            printf("invalid value len\n");
			return -1;
		}

		if(encdec(AES_DECRYPT, key, sizeof(key), cypval, vallen, theval) < 0){
			printf("encode value fail\n");
			return -1;
		}		
		return 0;
	}
	return -1;
}

static int write_env(int index, uchar* buf, int size)
{
	int rc;
	ulong end_addr,flash_sect_addr;
    ulong start,end;
	int rcode = 0;

    if(size > CFG_ENV_SIZE)
        size = CFG_ENV_SIZE;
    
	flash_sect_addr = (ulong)flash_addr;
	end_addr =(flash_sect_addr+0x20000-1);

    //protect 2 sectors
	if (flash_sect_protect (0, flash_sect_addr, end_addr))
		return 1;
    
    start = (ulong)flash_addr+(index*CFG_ENV_SIZE);
    end = (ulong)flash_addr+(index*CFG_ENV_SIZE)+CFG_ENV_SIZE-1;
    
	printf ("Erasing env%d...",index+1);
	if (flash_sect_erase (start, end)){
		rcode = 1;
		goto Done;
    }

	printf ("Writing to env%d... ",index+1);

	if (rc = flash_write(buf,start, size))
	{
		flash_perror (rc);
		rcode = 1;
	}else{
        printf("done\n");
	}

Done:
	/* try to re-protect */
	(void) flash_sect_protect (1, flash_sect_addr, end_addr);
	return rcode;
}

static int is_persist(char *name)
{
    int i, len;
    char *persistlist[]={"otp.", "ethaddr", "wmt.ethaddr.persist", "androidboot.serialno", 
                    "btaddr", "wmt.btaddr.persist","pcba.serialno","serialnum","persist.", NULL};

    for(i=0; persistlist[i] != NULL; i++){
        len = strlen(persistlist[i]);
        if(!strncmp(name, persistlist[i], len))
            return 0;       
    }

    return -1;    
}

static int sync_persist_env(struct hsearch_data *htab, uchar *env2)
{
   
    int len,i;
    int updated=0;
    uchar name[MAX_NAME_SIZE]={0};
    uchar *val=NULL,*valbuf=NULL;   
    uchar *s,*res;
	env_t env_new;
    ENTRY e, *ep;    

    if(!htab)
        return 1;

    valbuf = malloc(MAX_VALUE_SIZE);
    
    for(s=env2; s < (env2+ENV_SIZE) && *s!='\0'; ){
        
        if(is_persist(s) == 0){
            i=0;
            while(*s != '=' && *s != '\0' && i < (sizeof(name)-1)) 
                name[i++] = *s++;            

            name[i] = '\0';
            
            i=0;
            s++;//skip '='            
            val = valbuf;
            while(*s != '\0' ) 
                val[i++] = *s++;
            
            val[i] = '\0';
            s++;
            //printf("env2:%s=%s\n",name,val);
            
    		e.key = (char*)name;
    		e.data = (char*)NULL;
    		hsearch_r(e, FIND, &ep, htab);
            //if(ep) printf("env1:%s-%s\n",ep->key,ep->data);
            /* otp.xx exist in env2, but not exist in env1,copy it to env1 */
            if(!ep){
                e.key = (char*)name;
    		    e.data = (char*)val;
                printf("insert %s=%s to env1\n",e.key,e.data);
    		    hsearch_r(e, ENTER, &ep, htab);
                if (!ep) {
		            printf("## Error inserting \"%s\" variable\n",name);
                    free(valbuf);
		            return 1;
	            }
                updated ++;
            }
        }
        else{        
            len = strlen(s)+1;
            s += len;
        }
    }

    //printf("sync %d otps to env1\n",updated);
    res = (char *)&(env_new.data);
	len = hexport_r(htab, '\0', &res, ENV_SIZE, 0, NULL);
	if (len < 0) {
		printf("Cannot export environment\n");
        free(valbuf);
		return 1;
	}
	env_new.crc = crc32(0, env_new.data, ENV_SIZE);  
    write_env(0,(uchar*)&env_new, CFG_ENV_SIZE);
    write_env(1,(uchar*)&env_new, CFG_ENV_SIZE);
    free(valbuf);
    
    return 0;
}


/* buf shloud to be clean*/
static int cpyenv(uchar *buf, uchar *addr, int len)
{
    int blk=0;
    uchar *src,*dest;

    src = addr;
    dest = buf;

    memcpy(dest, src, 0x400);//1K
    while( (dest[0x3fe]|dest[0x3ff]) != '\0' && blk++ < 63){
        src += 0x400;
        dest += 0x400;
        memcpy(dest, src, 0x400);        
    }
    
    return 0;
}

int save_env( int idx)
{
    int len;
    uchar *res;
	env_t env_new;

   if (idx ==1||idx ==2){    
        res = (char *)&(env_new.data);
    	len = hexport_r(&env_htab, '\0', &res, ENV_SIZE, 0, NULL);
    	if (len < 0) {
    		printf("Cannot export environment\n");
    		return 1;
    	}
    	env_new.crc = crc32(0, env_new.data, ENV_SIZE);  

        write_env(idx-1, (uchar*)&env_new, CFG_ENV_SIZE);
    }else{
        uchar env2_buf[CFG_ENV_SIZE]={0};
        cpyenv(env2_buf, (uchar*)env_ptr2, CFG_ENV_SIZE);
        sync_persist_env(&env_htab,&env2_buf[4]);
    }

    return 0;
}


int esync(void)
{
    int ret;
    u32 crc1,crc2;
    uchar env1_buf[CFG_ENV_SIZE]={0};
    uchar env2_buf[CFG_ENV_SIZE]={0};

    /* sync SF env partitions */    

    cpyenv(env1_buf, (uchar*)env_ptr, CFG_ENV_SIZE);
    cpyenv(env2_buf, (uchar*)env_ptr2, CFG_ENV_SIZE);

    crc1 = crc32(0, &env1_buf[4], ENV_SIZE);
    crc2 = crc32(0, &env2_buf[4], ENV_SIZE);

    //printf("crc1:%08lx,%08lx; crc2:%08lx,%08lx\n", env_ptr->crc,crc1,env_ptr2->crc,crc2);
    /* env1 and env2 ok,and env1==env2 */
	if (crc1 == env_ptr->crc && crc2 == env_ptr2->crc && crc1 == crc2) {
        printf("env1==env2\n");        
	}
    /* env1 is invalid,env2 is ok */
    else if(crc1 != env_ptr->crc && crc2 == env_ptr2->crc){
        printf("env2->env1\n");
        //memcpy(env_buf,env_ptr2,CFG_ENV_SIZE);
        write_env(0, env2_buf, CFG_ENV_SIZE);
    }
    /* env2 is invalid, env1 is ok */
    else if(crc2 != env_ptr2->crc && crc1 == env_ptr->crc){
        printf("env1->env2\n");
        //memcpy(env_buf,env_ptr,CFG_ENV_SIZE);
        write_env(1, env1_buf, CFG_ENV_SIZE);
    }
    /* env2 env1 ok,but env1!=env2 */
    else if(crc2 == env_ptr2->crc && crc1 == env_ptr->crc && crc1 != crc2){
        printf("env1<-> env2\n");
        struct hsearch_data env_htab1 ={0};
        if (himport_r(&env_htab1, (char *)&env1_buf[4], ENV_SIZE, '\0', 0)==0) {
            printf("env1 hash table error!\n");
	        return 1;
        }
        sync_persist_env(&env_htab1, &env2_buf[4]);
        hdestroy_r(&env_htab1);
    }
    /* env2 is invalid, env1 is invalid */
    else{
        printf("crc1:%08lx,%08lx; crc2:%08lx,%08lx\n", env_ptr->crc,crc1,env_ptr2->crc,crc2);
        printf("both env invalid\n");

        return 1;
    }
    
    return 0;    

}

int do_esync(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
{
    int i=0;

    if(argc < 2){
        esync();
    }else{
        i = simple_strtoul(argv[1], NULL, 10);
        if(i==1){
            /* save cache to env1 */
            save_env(1);
        }else if(i==2){
            /* save cache to env2 */
            save_env(2);
        }else{
            ;
        }
    }
    return 0;
}

U_BOOT_CMD(
    esync,    3,  1,  do_esync,
	"esync     - sync uboot env\n",
	"esync  \n"
);