Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    1) // SPDX-License-Identifier: GPL-2.0+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  * (C) Copyright 2008-2015 Fuzhou Rockchip Electronics Co., Ltd
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7) #include <memory.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8) #include <stdint.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11) #include <stdbool.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12) #include <sys/stat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13) #include <time.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16)  * \brief	   SHA-1 context structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18) typedef struct
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20)     unsigned long total[2];	/*!< number of bytes processed	*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21)     unsigned long state[5];	/*!< intermediate digest state	*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22)     unsigned char buffer[64];	/*!< data block being processed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) sha1_context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27)  * 32-bit integer manipulation macros (big endian)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) #ifndef GET_UINT32_BE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) #define GET_UINT32_BE(n,b,i) {				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) 	(n) = ( (unsigned long) (b)[(i)    ] << 24 )	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) 	    | ( (unsigned long) (b)[(i) + 1] << 16 )	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) 	    | ( (unsigned long) (b)[(i) + 2] <<  8 )	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) 	    | ( (unsigned long) (b)[(i) + 3]       );	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) #ifndef PUT_UINT32_BE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) #define PUT_UINT32_BE(n,b,i) {				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) 	(b)[(i)    ] = (unsigned char) ( (n) >> 24 );	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) 	(b)[(i) + 1] = (unsigned char) ( (n) >> 16 );	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) 	(b)[(i) + 2] = (unsigned char) ( (n) >>  8 );	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) 	(b)[(i) + 3] = (unsigned char) ( (n)       );	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47)  * SHA-1 context setup
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) static
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) void sha1_starts (sha1_context * ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) 	ctx->total[0] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) 	ctx->total[1] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) 	ctx->state[0] = 0x67452301;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) 	ctx->state[1] = 0xEFCDAB89;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) 	ctx->state[2] = 0x98BADCFE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) 	ctx->state[3] = 0x10325476;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) 	ctx->state[4] = 0xC3D2E1F0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) static void sha1_process(sha1_context *ctx, const unsigned char data[64])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) 	unsigned long temp, W[16], A, B, C, D, E;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) 	GET_UINT32_BE (W[0], data, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) 	GET_UINT32_BE (W[1], data, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) 	GET_UINT32_BE (W[2], data, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) 	GET_UINT32_BE (W[3], data, 12);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) 	GET_UINT32_BE (W[4], data, 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) 	GET_UINT32_BE (W[5], data, 20);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) 	GET_UINT32_BE (W[6], data, 24);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) 	GET_UINT32_BE (W[7], data, 28);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) 	GET_UINT32_BE (W[8], data, 32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) 	GET_UINT32_BE (W[9], data, 36);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) 	GET_UINT32_BE (W[10], data, 40);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) 	GET_UINT32_BE (W[11], data, 44);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) 	GET_UINT32_BE (W[12], data, 48);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) 	GET_UINT32_BE (W[13], data, 52);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) 	GET_UINT32_BE (W[14], data, 56);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) 	GET_UINT32_BE (W[15], data, 60);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) #define S(x,n)	((x << n) | ((x & 0xFFFFFFFF) >> (32 - n)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) #define R(t) (						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) 	temp = W[(t -  3) & 0x0F] ^ W[(t - 8) & 0x0F] ^	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) 	       W[(t - 14) & 0x0F] ^ W[ t      & 0x0F],	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) 	( W[t & 0x0F] = S(temp,1) )			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) #define P(a,b,c,d,e,x)	{				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) 	e += S(a,5) + F(b,c,d) + K + x; b = S(b,30);	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) 	A = ctx->state[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) 	B = ctx->state[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) 	C = ctx->state[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) 	D = ctx->state[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) 	E = ctx->state[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) #define F(x,y,z) (z ^ (x & (y ^ z)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) #define K 0x5A827999
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) 	P (A, B, C, D, E, W[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) 	P (E, A, B, C, D, W[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) 	P (D, E, A, B, C, W[2]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) 	P (C, D, E, A, B, W[3]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) 	P (B, C, D, E, A, W[4]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) 	P (A, B, C, D, E, W[5]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) 	P (E, A, B, C, D, W[6]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) 	P (D, E, A, B, C, W[7]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) 	P (C, D, E, A, B, W[8]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) 	P (B, C, D, E, A, W[9]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) 	P (A, B, C, D, E, W[10]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) 	P (E, A, B, C, D, W[11]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) 	P (D, E, A, B, C, W[12]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) 	P (C, D, E, A, B, W[13]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) 	P (B, C, D, E, A, W[14]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) 	P (A, B, C, D, E, W[15]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) 	P (E, A, B, C, D, R (16));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) 	P (D, E, A, B, C, R (17));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) 	P (C, D, E, A, B, R (18));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) 	P (B, C, D, E, A, R (19));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) #undef K
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) #undef F
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) #define F(x,y,z) (x ^ y ^ z)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) #define K 0x6ED9EBA1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) 	P (A, B, C, D, E, R (20));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) 	P (E, A, B, C, D, R (21));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) 	P (D, E, A, B, C, R (22));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) 	P (C, D, E, A, B, R (23));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) 	P (B, C, D, E, A, R (24));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) 	P (A, B, C, D, E, R (25));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) 	P (E, A, B, C, D, R (26));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) 	P (D, E, A, B, C, R (27));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) 	P (C, D, E, A, B, R (28));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 	P (B, C, D, E, A, R (29));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) 	P (A, B, C, D, E, R (30));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 	P (E, A, B, C, D, R (31));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) 	P (D, E, A, B, C, R (32));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) 	P (C, D, E, A, B, R (33));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 	P (B, C, D, E, A, R (34));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) 	P (A, B, C, D, E, R (35));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) 	P (E, A, B, C, D, R (36));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) 	P (D, E, A, B, C, R (37));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) 	P (C, D, E, A, B, R (38));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) 	P (B, C, D, E, A, R (39));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) #undef K
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) #undef F
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) #define F(x,y,z) ((x & y) | (z & (x | y)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) #define K 0x8F1BBCDC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) 	P (A, B, C, D, E, R (40));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 	P (E, A, B, C, D, R (41));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) 	P (D, E, A, B, C, R (42));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 	P (C, D, E, A, B, R (43));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) 	P (B, C, D, E, A, R (44));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) 	P (A, B, C, D, E, R (45));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 	P (E, A, B, C, D, R (46));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) 	P (D, E, A, B, C, R (47));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) 	P (C, D, E, A, B, R (48));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) 	P (B, C, D, E, A, R (49));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) 	P (A, B, C, D, E, R (50));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) 	P (E, A, B, C, D, R (51));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) 	P (D, E, A, B, C, R (52));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) 	P (C, D, E, A, B, R (53));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) 	P (B, C, D, E, A, R (54));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) 	P (A, B, C, D, E, R (55));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) 	P (E, A, B, C, D, R (56));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) 	P (D, E, A, B, C, R (57));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) 	P (C, D, E, A, B, R (58));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) 	P (B, C, D, E, A, R (59));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) #undef K
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) #undef F
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) #define F(x,y,z) (x ^ y ^ z)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) #define K 0xCA62C1D6
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 	P (A, B, C, D, E, R (60));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 	P (E, A, B, C, D, R (61));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) 	P (D, E, A, B, C, R (62));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 	P (C, D, E, A, B, R (63));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 	P (B, C, D, E, A, R (64));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 	P (A, B, C, D, E, R (65));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 	P (E, A, B, C, D, R (66));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) 	P (D, E, A, B, C, R (67));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 	P (C, D, E, A, B, R (68));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) 	P (B, C, D, E, A, R (69));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) 	P (A, B, C, D, E, R (70));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) 	P (E, A, B, C, D, R (71));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) 	P (D, E, A, B, C, R (72));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 	P (C, D, E, A, B, R (73));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) 	P (B, C, D, E, A, R (74));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) 	P (A, B, C, D, E, R (75));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) 	P (E, A, B, C, D, R (76));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) 	P (D, E, A, B, C, R (77));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) 	P (C, D, E, A, B, R (78));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) 	P (B, C, D, E, A, R (79));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) #undef K
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) #undef F
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) 	ctx->state[0] += A;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) 	ctx->state[1] += B;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) 	ctx->state[2] += C;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) 	ctx->state[3] += D;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) 	ctx->state[4] += E;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) #undef P
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) #undef R
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) #undef S
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221)  * SHA-1 process buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) static
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) void sha1_update(sha1_context *ctx, const unsigned char *input,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 		 unsigned int ilen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 	int fill;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 	unsigned long left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 	if (ilen <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) 	left = ctx->total[0] & 0x3F;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 	fill = 64 - left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 	ctx->total[0] += ilen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 	ctx->total[0] &= 0xFFFFFFFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 	if (ctx->total[0] < (unsigned long) ilen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 		ctx->total[1]++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 	if (left && ilen >= fill) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 		memcpy ((void *) (ctx->buffer + left), (void *) input, fill);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 		sha1_process (ctx, ctx->buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 		input += fill;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) 		ilen -= fill;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) 		left = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 	while (ilen >= 64) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 		sha1_process (ctx, input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 		input += 64;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) 		ilen -= 64;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) 	if (ilen > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 		memcpy ((void *) (ctx->buffer + left), (void *) input, ilen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) static const unsigned char sha1_padding[64] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 	0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 	   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 	   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) 	   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269)  * SHA-1 final digest
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) static
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) void sha1_finish (sha1_context * ctx, unsigned char output[20])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 	unsigned long last, padn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 	unsigned long high, low;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 	unsigned char msglen[8];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) 	high = (ctx->total[0] >> 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 		| (ctx->total[1] << 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) 	low = (ctx->total[0] << 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 	PUT_UINT32_BE (high, msglen, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 	PUT_UINT32_BE (low, msglen, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 	last = ctx->total[0] & 0x3F;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 	padn = (last < 56) ? (56 - last) : (120 - last);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 	sha1_update (ctx, (unsigned char *) sha1_padding, padn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 	sha1_update (ctx, msglen, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 	PUT_UINT32_BE (ctx->state[0], output, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 	PUT_UINT32_BE (ctx->state[1], output, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) 	PUT_UINT32_BE (ctx->state[2], output, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 	PUT_UINT32_BE (ctx->state[3], output, 12);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) 	PUT_UINT32_BE (ctx->state[4], output, 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299)  * Output = SHA-1( input buffer )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) static
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) void sha1_csum(const unsigned char *input, unsigned int ilen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 	       unsigned char *output)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 	sha1_context ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) 	sha1_starts (&ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 	sha1_update (&ctx, input, ilen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 	sha1_finish (&ctx, output);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) typedef struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 	uint32_t total[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 	uint32_t state[8];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) 	uint8_t buffer[64];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) } sha256_context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) static
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) void sha256_starts(sha256_context * ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 	ctx->total[0] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 	ctx->total[1] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 	ctx->state[0] = 0x6A09E667;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 	ctx->state[1] = 0xBB67AE85;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 	ctx->state[2] = 0x3C6EF372;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) 	ctx->state[3] = 0xA54FF53A;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) 	ctx->state[4] = 0x510E527F;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 	ctx->state[5] = 0x9B05688C;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) 	ctx->state[6] = 0x1F83D9AB;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 	ctx->state[7] = 0x5BE0CD19;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) static void sha256_process(sha256_context *ctx, const uint8_t data[64])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 	uint32_t temp1, temp2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 	uint32_t W[64];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) 	uint32_t A, B, C, D, E, F, G, H;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 	GET_UINT32_BE(W[0], data, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) 	GET_UINT32_BE(W[1], data, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 	GET_UINT32_BE(W[2], data, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 	GET_UINT32_BE(W[3], data, 12);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 	GET_UINT32_BE(W[4], data, 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 	GET_UINT32_BE(W[5], data, 20);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 	GET_UINT32_BE(W[6], data, 24);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 	GET_UINT32_BE(W[7], data, 28);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) 	GET_UINT32_BE(W[8], data, 32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 	GET_UINT32_BE(W[9], data, 36);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 	GET_UINT32_BE(W[10], data, 40);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 	GET_UINT32_BE(W[11], data, 44);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 	GET_UINT32_BE(W[12], data, 48);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 	GET_UINT32_BE(W[13], data, 52);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 	GET_UINT32_BE(W[14], data, 56);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 	GET_UINT32_BE(W[15], data, 60);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) #define SHR(x,n) ((x & 0xFFFFFFFF) >> n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) #define ROTR(x,n) (SHR(x,n) | (x << (32 - n)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) #define S0(x) (ROTR(x, 7) ^ ROTR(x,18) ^ SHR(x, 3))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) #define S1(x) (ROTR(x,17) ^ ROTR(x,19) ^ SHR(x,10))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) #define S2(x) (ROTR(x, 2) ^ ROTR(x,13) ^ ROTR(x,22))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) #define S3(x) (ROTR(x, 6) ^ ROTR(x,11) ^ ROTR(x,25))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) #define F0(x,y,z) ((x & y) | (z & (x | y)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) #define F1(x,y,z) (z ^ (x & (y ^ z)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) #define R(t)					\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) (						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 	W[t] = S1(W[t - 2]) + W[t - 7] +	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) 		S0(W[t - 15]) + W[t - 16]	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) #define P(a,b,c,d,e,f,g,h,x,K) {		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 	temp1 = h + S3(e) + F1(e,f,g) + K + x;	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) 	temp2 = S2(a) + F0(a,b,c);		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 	d += temp1; h = temp1 + temp2;		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 	A = ctx->state[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 	B = ctx->state[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 	C = ctx->state[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 	D = ctx->state[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 	E = ctx->state[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 	F = ctx->state[5];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 	G = ctx->state[6];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 	H = ctx->state[7];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 	P(A, B, C, D, E, F, G, H, W[0], 0x428A2F98);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 	P(H, A, B, C, D, E, F, G, W[1], 0x71374491);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 	P(G, H, A, B, C, D, E, F, W[2], 0xB5C0FBCF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 	P(F, G, H, A, B, C, D, E, W[3], 0xE9B5DBA5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 	P(E, F, G, H, A, B, C, D, W[4], 0x3956C25B);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 	P(D, E, F, G, H, A, B, C, W[5], 0x59F111F1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) 	P(C, D, E, F, G, H, A, B, W[6], 0x923F82A4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 	P(B, C, D, E, F, G, H, A, W[7], 0xAB1C5ED5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) 	P(A, B, C, D, E, F, G, H, W[8], 0xD807AA98);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 	P(H, A, B, C, D, E, F, G, W[9], 0x12835B01);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 	P(G, H, A, B, C, D, E, F, W[10], 0x243185BE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 	P(F, G, H, A, B, C, D, E, W[11], 0x550C7DC3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) 	P(E, F, G, H, A, B, C, D, W[12], 0x72BE5D74);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 	P(D, E, F, G, H, A, B, C, W[13], 0x80DEB1FE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 	P(C, D, E, F, G, H, A, B, W[14], 0x9BDC06A7);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 	P(B, C, D, E, F, G, H, A, W[15], 0xC19BF174);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) 	P(A, B, C, D, E, F, G, H, R(16), 0xE49B69C1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 	P(H, A, B, C, D, E, F, G, R(17), 0xEFBE4786);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 	P(G, H, A, B, C, D, E, F, R(18), 0x0FC19DC6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 	P(F, G, H, A, B, C, D, E, R(19), 0x240CA1CC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 	P(E, F, G, H, A, B, C, D, R(20), 0x2DE92C6F);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 	P(D, E, F, G, H, A, B, C, R(21), 0x4A7484AA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 	P(C, D, E, F, G, H, A, B, R(22), 0x5CB0A9DC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 	P(B, C, D, E, F, G, H, A, R(23), 0x76F988DA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 	P(A, B, C, D, E, F, G, H, R(24), 0x983E5152);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 	P(H, A, B, C, D, E, F, G, R(25), 0xA831C66D);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 	P(G, H, A, B, C, D, E, F, R(26), 0xB00327C8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 	P(F, G, H, A, B, C, D, E, R(27), 0xBF597FC7);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 	P(E, F, G, H, A, B, C, D, R(28), 0xC6E00BF3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) 	P(D, E, F, G, H, A, B, C, R(29), 0xD5A79147);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 	P(C, D, E, F, G, H, A, B, R(30), 0x06CA6351);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 	P(B, C, D, E, F, G, H, A, R(31), 0x14292967);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 	P(A, B, C, D, E, F, G, H, R(32), 0x27B70A85);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 	P(H, A, B, C, D, E, F, G, R(33), 0x2E1B2138);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 	P(G, H, A, B, C, D, E, F, R(34), 0x4D2C6DFC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 	P(F, G, H, A, B, C, D, E, R(35), 0x53380D13);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 	P(E, F, G, H, A, B, C, D, R(36), 0x650A7354);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 	P(D, E, F, G, H, A, B, C, R(37), 0x766A0ABB);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 	P(C, D, E, F, G, H, A, B, R(38), 0x81C2C92E);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 	P(B, C, D, E, F, G, H, A, R(39), 0x92722C85);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 	P(A, B, C, D, E, F, G, H, R(40), 0xA2BFE8A1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) 	P(H, A, B, C, D, E, F, G, R(41), 0xA81A664B);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 	P(G, H, A, B, C, D, E, F, R(42), 0xC24B8B70);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 	P(F, G, H, A, B, C, D, E, R(43), 0xC76C51A3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 	P(E, F, G, H, A, B, C, D, R(44), 0xD192E819);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 	P(D, E, F, G, H, A, B, C, R(45), 0xD6990624);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 	P(C, D, E, F, G, H, A, B, R(46), 0xF40E3585);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 	P(B, C, D, E, F, G, H, A, R(47), 0x106AA070);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 	P(A, B, C, D, E, F, G, H, R(48), 0x19A4C116);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 	P(H, A, B, C, D, E, F, G, R(49), 0x1E376C08);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 	P(G, H, A, B, C, D, E, F, R(50), 0x2748774C);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 	P(F, G, H, A, B, C, D, E, R(51), 0x34B0BCB5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 	P(E, F, G, H, A, B, C, D, R(52), 0x391C0CB3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 	P(D, E, F, G, H, A, B, C, R(53), 0x4ED8AA4A);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 	P(C, D, E, F, G, H, A, B, R(54), 0x5B9CCA4F);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 	P(B, C, D, E, F, G, H, A, R(55), 0x682E6FF3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 	P(A, B, C, D, E, F, G, H, R(56), 0x748F82EE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 	P(H, A, B, C, D, E, F, G, R(57), 0x78A5636F);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) 	P(G, H, A, B, C, D, E, F, R(58), 0x84C87814);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) 	P(F, G, H, A, B, C, D, E, R(59), 0x8CC70208);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) 	P(E, F, G, H, A, B, C, D, R(60), 0x90BEFFFA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 	P(D, E, F, G, H, A, B, C, R(61), 0xA4506CEB);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 	P(C, D, E, F, G, H, A, B, R(62), 0xBEF9A3F7);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) 	P(B, C, D, E, F, G, H, A, R(63), 0xC67178F2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 	ctx->state[0] += A;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) 	ctx->state[1] += B;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 	ctx->state[2] += C;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 	ctx->state[3] += D;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 	ctx->state[4] += E;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 	ctx->state[5] += F;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 	ctx->state[6] += G;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 	ctx->state[7] += H;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) #undef P
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) #undef R
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) #undef F1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) #undef F0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) #undef S3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) #undef S2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) #undef S1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) #undef S0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) #undef ROTR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) #undef SHR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) static
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) void sha256_update(sha256_context *ctx, const uint8_t *input, uint32_t length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) 	uint32_t left, fill;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 	if (!length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) 	left = ctx->total[0] & 0x3F;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 	fill = 64 - left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) 	ctx->total[0] += length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 	ctx->total[0] &= 0xFFFFFFFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) 	if (ctx->total[0] < length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) 		ctx->total[1]++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 	if (left && length >= fill) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) 		memcpy((void *) (ctx->buffer + left), (void *) input, fill);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) 		sha256_process(ctx, ctx->buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) 		length -= fill;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 		input += fill;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 		left = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 	while (length >= 64) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 		sha256_process(ctx, input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 		length -= 64;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 		input += 64;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 	if (length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 		memcpy((void *) (ctx->buffer + left), (void *) input, length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) static uint8_t sha256_padding[64] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 	0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 	   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 	   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 	   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) static
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) void sha256_finish(sha256_context * ctx, uint8_t digest[32])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 	uint32_t last, padn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 	uint32_t high, low;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 	uint8_t msglen[8];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 	high = ((ctx->total[0] >> 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 		| (ctx->total[1] << 3));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 	low = (ctx->total[0] << 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 	PUT_UINT32_BE(high, msglen, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 	PUT_UINT32_BE(low, msglen, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 	last = ctx->total[0] & 0x3F;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 	padn = (last < 56) ? (56 - last) : (120 - last);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 	sha256_update(ctx, sha256_padding, padn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 	sha256_update(ctx, msglen, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 	PUT_UINT32_BE(ctx->state[0], digest, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 	PUT_UINT32_BE(ctx->state[1], digest, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 	PUT_UINT32_BE(ctx->state[2], digest, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 	PUT_UINT32_BE(ctx->state[3], digest, 12);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 	PUT_UINT32_BE(ctx->state[4], digest, 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 	PUT_UINT32_BE(ctx->state[5], digest, 20);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 	PUT_UINT32_BE(ctx->state[6], digest, 24);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 	PUT_UINT32_BE(ctx->state[7], digest, 28);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549)  * Output = SHA-256( input buffer ).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) static
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) void sha256_csum(const unsigned char *input, unsigned int ilen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 		 unsigned char *output)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 	sha256_context ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 	sha256_starts(&ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 	sha256_update(&ctx, input, ilen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 	sha256_finish(&ctx, output);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) /* #define DEBUG */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) static bool g_debug =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) #ifdef DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566)         true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568)         false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) #endif /* DEBUG */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) #define LOGE(fmt, args...)                                                     \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572)   fprintf(stderr, "E/%s(%d): " fmt "\n", __func__, __LINE__, ##args)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) #define LOGD(fmt, args...)                                                     \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574)   do {                                                                         \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575)     if (g_debug)                                                               \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576)       fprintf(stderr, "D/%s(%d): " fmt "\n", __func__, __LINE__, ##args);      \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577)   } while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) /* sync with ./board/rockchip/rk30xx/rkloader.c #define FDT_PATH */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) #define FDT_PATH "rk-kernel.dtb"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) #define DTD_SUBFIX ".dtb"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) #define DEFAULT_IMAGE_PATH "resource.img"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) #define DEFAULT_UNPACK_DIR "out"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) #define BLOCK_SIZE 512
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) #define RESOURCE_PTN_HDR_SIZE 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) #define INDEX_TBL_ENTR_SIZE 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) #define RESOURCE_PTN_VERSION 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) #define INDEX_TBL_VERSION 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) #define RESOURCE_PTN_HDR_MAGIC "RSCE"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) typedef struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 	char magic[4]; /* tag, "RSCE" */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 	uint16_t resource_ptn_version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) 	uint16_t index_tbl_version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) 	uint8_t header_size;    /* blocks, size of ptn header. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 	uint8_t tbl_offset;     /* blocks, offset of index table. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 	uint8_t tbl_entry_size; /* blocks, size of index table's entry. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) 	uint32_t tbl_entry_num; /* numbers of index table's entry. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) } resource_ptn_header;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) #define INDEX_TBL_ENTR_TAG "ENTR"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) #define MAX_INDEX_ENTRY_PATH_LEN	220
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) #define MAX_HASH_LEN			32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) typedef struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 	char tag[4]; /* tag, "ENTR" */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 	char path[MAX_INDEX_ENTRY_PATH_LEN];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 	char hash[MAX_HASH_LEN]; /* hash data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 	uint32_t hash_size;	 /* 20 or 32 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 	uint32_t content_offset; /* blocks, offset of resource content. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 	uint32_t content_size;   /* bytes, size of resource content. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) } index_tbl_entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) #define OPT_VERBOSE "--verbose"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) #define OPT_HELP "--help"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) #define OPT_VERSION "--version"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) #define OPT_PRINT "--print"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) #define OPT_PACK "--pack"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) #define OPT_UNPACK "--unpack"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) #define OPT_TEST_LOAD "--test_load"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) #define OPT_TEST_CHARGE "--test_charge"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) #define OPT_IMAGE "--image="
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) #define OPT_ROOT "--root="
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) #define VERSION "2014-5-31 14:43:42"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) typedef struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 	char path[MAX_INDEX_ENTRY_PATH_LEN];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 	uint32_t content_offset; /* blocks, offset of resource content. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 	uint32_t content_size;   /* bytes, size of resource content. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 	void *load_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) } resource_content;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) typedef struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 	int max_level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 	int num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 	int delay;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 	char prefix[MAX_INDEX_ENTRY_PATH_LEN];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) } anim_level_conf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) #define DEF_CHARGE_DESC_PATH "charge_anim_desc.txt"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) #define OPT_CHARGE_ANIM_DELAY "delay="
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) #define OPT_CHARGE_ANIM_LOOP_CUR "only_current_level="
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) #define OPT_CHARGE_ANIM_LEVELS "levels="
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) #define OPT_CHARGE_ANIM_LEVEL_CONF "max_level="
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) #define OPT_CHARGE_ANIM_LEVEL_NUM "num="
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) #define OPT_CHARGE_ANIM_LEVEL_PFX "prefix="
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) static char image_path[MAX_INDEX_ENTRY_PATH_LEN] = "\0";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) static int fix_blocks(size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 	return (size + BLOCK_SIZE - 1) / BLOCK_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) static const char *fix_path(const char *path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 	if (!memcmp(path, "./", 2)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 		return path + 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 	return path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) static uint16_t switch_short(uint16_t x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 	uint16_t val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 	uint8_t *p = (uint8_t *)(&x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 	val = (*p++ & 0xff) << 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 	val |= (*p & 0xff) << 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 	return val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) static uint32_t switch_int(uint32_t x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 	uint32_t val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 	uint8_t *p = (uint8_t *)(&x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 	val = (*p++ & 0xff) << 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 	val |= (*p++ & 0xff) << 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 	val |= (*p++ & 0xff) << 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 	val |= (*p & 0xff) << 24;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 	return val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) static void fix_header(resource_ptn_header *header)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 	/* switch for be. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 	header->resource_ptn_version = switch_short(header->resource_ptn_version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 	header->index_tbl_version = switch_short(header->index_tbl_version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 	header->tbl_entry_num = switch_int(header->tbl_entry_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) static void fix_entry(index_tbl_entry *entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 	/* switch for be. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 	entry->content_offset = switch_int(entry->content_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 	entry->content_size = switch_int(entry->content_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) static int inline get_ptn_offset(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) static bool StorageWriteLba(int offset_block, void *data, int blocks)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 	bool ret = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 	FILE *file = fopen(image_path, "rb+");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 	if (!file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 		goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 	int offset = offset_block * BLOCK_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 	fseek(file, offset, SEEK_SET);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 	if (offset != ftell(file)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 		LOGE("Failed to seek %s to %d!", image_path, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 		goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 	if (!fwrite(data, blocks * BLOCK_SIZE, 1, file)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 		LOGE("Failed to write %s!", image_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 		goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 	ret = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) end:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 	if (file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 		fclose(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) static bool StorageReadLba(int offset_block, void *data, int blocks)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 	bool ret = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 	FILE *file = fopen(image_path, "rb");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 	if (!file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 		goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 	int offset = offset_block * BLOCK_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 	fseek(file, offset, SEEK_SET);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 	if (offset != ftell(file)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 		goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 	if (!fread(data, blocks * BLOCK_SIZE, 1, file)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 		goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 	ret = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) end:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 	if (file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 		fclose(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) static bool write_data(int offset_block, void *data, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 	bool ret = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 	if (!data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 		goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 	int blocks = len / BLOCK_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 	if (blocks && !StorageWriteLba(offset_block, data, blocks)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 		goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 	int left = len % BLOCK_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 	if (left) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 		char buf[BLOCK_SIZE] = "\0";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 		memcpy(buf, data + blocks * BLOCK_SIZE, left);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 		if (!StorageWriteLba(offset_block + blocks, buf, 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 			goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 	ret = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) end:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) /**********************load test************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) static int load_file(const char *file_path, int offset_block, int blocks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) static int test_load(int argc, char **argv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 	if (argc < 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 		LOGE("Nothing to load!");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 	const char *file_path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 	int offset_block = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 	int blocks = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 	if (argc > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 		file_path = (const char *)fix_path(argv[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 		argc--, argv++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 	if (argc > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 		offset_block = atoi(argv[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 		argc--, argv++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 	if (argc > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 		blocks = atoi(argv[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 	return load_file(file_path, offset_block, blocks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) static void free_content(resource_content *content)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) 	if (content->load_addr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 		free(content->load_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 		content->load_addr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) static void tests_dump_file(const char *path, void *data, int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 	FILE *file = fopen(path, "wb");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 	if (!file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 	fwrite(data, len, 1, file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 	fclose(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) static bool load_content(resource_content *content)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 	if (content->load_addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 	int blocks = fix_blocks(content->content_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 	content->load_addr = malloc(blocks * BLOCK_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 	if (!content->load_addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 	if (!StorageReadLba(get_ptn_offset() + content->content_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 	                    content->load_addr, blocks)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 		free_content(content);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 	tests_dump_file(content->path, content->load_addr, content->content_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) static bool load_content_data(resource_content *content, int offset_block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839)                               void *data, int blocks)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 	if (!StorageReadLba(get_ptn_offset() + content->content_offset + offset_block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 	                    data, blocks)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 	tests_dump_file(content->path, data, blocks * BLOCK_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) static bool get_entry(const char *file_path, index_tbl_entry *entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 	bool ret = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 	char buf[BLOCK_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 	resource_ptn_header header;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 	if (!StorageReadLba(get_ptn_offset(), buf, 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 		LOGE("Failed to read header!");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 		goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 	memcpy(&header, buf, sizeof(header));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 	if (memcmp(header.magic, RESOURCE_PTN_HDR_MAGIC, sizeof(header.magic))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 		LOGE("Not a resource image(%s)!", image_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 		goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 	/* test on pc, switch for be. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 	fix_header(&header);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 	/* TODO: support header_size & tbl_entry_size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 	if (header.resource_ptn_version != RESOURCE_PTN_VERSION ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 	    header.header_size != RESOURCE_PTN_HDR_SIZE ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 	    header.index_tbl_version != INDEX_TBL_VERSION ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 	    header.tbl_entry_size != INDEX_TBL_ENTR_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 		LOGE("Not supported in this version!");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 		goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 	for (i = 0; i < header.tbl_entry_num; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 		/* TODO: support tbl_entry_size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 		if (!StorageReadLba(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 		            get_ptn_offset() + header.header_size + i * header.tbl_entry_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 		            buf, 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 			LOGE("Failed to read index entry:%d!", i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 			goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 		memcpy(entry, buf, sizeof(*entry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 		if (memcmp(entry->tag, INDEX_TBL_ENTR_TAG, sizeof(entry->tag))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 			LOGE("Something wrong with index entry:%d!", i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 			goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 		if (!strncmp(entry->path, file_path, sizeof(entry->path)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 	if (i == header.tbl_entry_num) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 		LOGE("Cannot find %s!", file_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 		goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 	/* test on pc, switch for be. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 	fix_entry(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 	printf("Found entry:\n\tpath:%s\n\toffset:%d\tsize:%d\n", entry->path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) 	       entry->content_offset, entry->content_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 	ret = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) end:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) static bool get_content(resource_content *content)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 	bool ret = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 	index_tbl_entry entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 	if (!get_entry(content->path, &entry))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 		goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 	content->content_offset = entry.content_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 	content->content_size = entry.content_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 	ret = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) end:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) static int load_file(const char *file_path, int offset_block, int blocks)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 	printf("Try to load:%s", file_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 	if (blocks) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 		printf(", offset block:%d, blocks:%d\n", offset_block, blocks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 		printf("\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 	bool ret = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 	resource_content content;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 	snprintf(content.path, sizeof(content.path), "%s", file_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 	content.load_addr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 	if (!get_content(&content)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 		goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 	if (!blocks) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 		if (!load_content(&content)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 			goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 		void *data = malloc(blocks * BLOCK_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 		if (!data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 			goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 		if (!load_content_data(&content, offset_block, data, blocks)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 			goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 	ret = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) end:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 	free_content(&content);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) /**********************load test end************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) /**********************anim test************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) static bool parse_level_conf(const char *arg, anim_level_conf *level_conf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 	memset(level_conf, 0, sizeof(anim_level_conf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 	char *buf = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 	buf = strstr(arg, OPT_CHARGE_ANIM_LEVEL_CONF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 	if (buf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 		level_conf->max_level = atoi(buf + strlen(OPT_CHARGE_ANIM_LEVEL_CONF));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 		LOGE("Not found:%s", OPT_CHARGE_ANIM_LEVEL_CONF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 	buf = strstr(arg, OPT_CHARGE_ANIM_LEVEL_NUM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 	if (buf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 		level_conf->num = atoi(buf + strlen(OPT_CHARGE_ANIM_LEVEL_NUM));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 		if (level_conf->num <= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 			return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 		LOGE("Not found:%s", OPT_CHARGE_ANIM_LEVEL_NUM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 	buf = strstr(arg, OPT_CHARGE_ANIM_DELAY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 	if (buf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 		level_conf->delay = atoi(buf + strlen(OPT_CHARGE_ANIM_DELAY));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 	buf = strstr(arg, OPT_CHARGE_ANIM_LEVEL_PFX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 	if (buf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 		snprintf(level_conf->prefix, sizeof(level_conf->prefix), "%s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 		         buf + strlen(OPT_CHARGE_ANIM_LEVEL_PFX));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 		LOGE("Not found:%s", OPT_CHARGE_ANIM_LEVEL_PFX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 	LOGD("Found conf:\nmax_level:%d, num:%d, delay:%d, prefix:%s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 	     level_conf->max_level, level_conf->num, level_conf->delay,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 	     level_conf->prefix);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) static int test_charge(int argc, char **argv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 	const char *desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 	if (argc > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 		desc = argv[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 		desc = DEF_CHARGE_DESC_PATH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 	resource_content content;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 	snprintf(content.path, sizeof(content.path), "%s", desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 	content.load_addr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 	if (!get_content(&content)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 		goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 	if (!load_content(&content)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 		goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 	char *buf = (char *)content.load_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 	char *end = buf + content.content_size - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 	*end = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 	LOGD("desc:\n%s", buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 	int pos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 	while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 		char *line = (char *)memchr(buf + pos, '\n', strlen(buf + pos));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 		if (!line)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 		*line = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 		LOGD("splite:%s", buf + pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 		pos += (strlen(buf + pos) + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 	int delay = 900;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 	int only_current_level = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 	anim_level_conf *level_confs = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 	int level_conf_pos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 	int level_conf_num = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 	while (true) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 		if (buf >= end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 		const char *arg = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 		buf += (strlen(buf) + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 		LOGD("parse arg:%s", arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 		if (!memcmp(arg, OPT_CHARGE_ANIM_LEVEL_CONF,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 		            strlen(OPT_CHARGE_ANIM_LEVEL_CONF))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 			if (!level_confs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 				LOGE("Found level conf before levels!");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 				goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 			if (level_conf_pos >= level_conf_num) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 				LOGE("Too many level confs!(%d >= %d)", level_conf_pos, level_conf_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 				goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 			if (!parse_level_conf(arg, level_confs + level_conf_pos)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 				LOGE("Failed to parse level conf:%s", arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 				goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 			level_conf_pos++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) 		} else if (!memcmp(arg, OPT_CHARGE_ANIM_DELAY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 		                   strlen(OPT_CHARGE_ANIM_DELAY))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 			delay = atoi(arg + strlen(OPT_CHARGE_ANIM_DELAY));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) 			LOGD("Found delay:%d", delay);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 		} else if (!memcmp(arg, OPT_CHARGE_ANIM_LOOP_CUR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 		                   strlen(OPT_CHARGE_ANIM_LOOP_CUR))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) 			only_current_level =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 			        !memcmp(arg + strlen(OPT_CHARGE_ANIM_LOOP_CUR), "true", 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) 			LOGD("Found only_current_level:%d", only_current_level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 		} else if (!memcmp(arg, OPT_CHARGE_ANIM_LEVELS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 		                   strlen(OPT_CHARGE_ANIM_LEVELS))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 			if (level_conf_num) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) 				goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) 			level_conf_num = atoi(arg + strlen(OPT_CHARGE_ANIM_LEVELS));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) 			if (!level_conf_num) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) 				goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) 			level_confs =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) 			        (anim_level_conf *)malloc(level_conf_num * sizeof(anim_level_conf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 			LOGD("Found levels:%d", level_conf_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 			LOGE("Unknown arg:%s", arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 			goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 	if (level_conf_pos != level_conf_num || !level_conf_num) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 		LOGE("Something wrong with level confs!");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) 		goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) 	int i = 0, j = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) 	for (i = 0; i < level_conf_num; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) 		if (!level_confs[i].delay) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) 			level_confs[i].delay = delay;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) 		if (!level_confs[i].delay) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) 			LOGE("Missing delay in level conf:%d", i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 			goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) 		for (j = 0; j < i; j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) 			if (level_confs[j].max_level == level_confs[i].max_level) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 				LOGE("Dup level conf:%d", i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) 				goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) 			if (level_confs[j].max_level > level_confs[i].max_level) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) 				anim_level_conf conf = level_confs[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) 				memmove(level_confs + j + 1, level_confs + j,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) 				        (i - j) * sizeof(anim_level_conf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) 				level_confs[j] = conf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) 	printf("Parse anim desc(%s):\n", desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) 	printf("only_current_level=%d\n", only_current_level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) 	printf("level conf:\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) 	for (i = 0; i < level_conf_num; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) 		printf("\tmax=%d, delay=%d, num=%d, prefix=%s\n", level_confs[i].max_level,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) 		       level_confs[i].delay, level_confs[i].num, level_confs[i].prefix);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) end:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 	free_content(&content);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) /**********************anim test end************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) /**********************append file************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) static const char *PROG = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) static resource_ptn_header header;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) static bool just_print = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) static char root_path[MAX_INDEX_ENTRY_PATH_LEN] = "\0";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) static void version(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 	printf("%s (cjf@rock-chips.com)\t" VERSION "\n", PROG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) static void usage(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) 	printf("Usage: %s [options] [FILES]\n", PROG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) 	printf("Tools for Rockchip's resource image.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) 	version();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) 	printf("Options:\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 	printf("\t" OPT_PACK "\t\t\tPack image from given files.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) 	printf("\t" OPT_UNPACK "\t\tUnpack given image to current dir.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) 	printf("\t" OPT_IMAGE "path"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) 	       "\t\tSpecify input/output image path.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) 	printf("\t" OPT_PRINT "\t\t\tJust print informations.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) 	printf("\t" OPT_VERBOSE "\t\tDisplay more runtime informations.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) 	printf("\t" OPT_HELP "\t\t\tDisplay this information.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 	printf("\t" OPT_VERSION "\t\tDisplay version information.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) 	printf("\t" OPT_ROOT "path"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) 	       "\t\tSpecify resources' root dir.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) static int pack_image(int file_num, const char **files);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) static int unpack_image(const char *unpack_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) enum ACTION {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) 	ACTION_PACK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) 	ACTION_UNPACK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) 	ACTION_TEST_LOAD,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) 	ACTION_TEST_CHARGE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) int main(int argc, char **argv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) 	PROG = fix_path(argv[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) 	enum ACTION action = ACTION_PACK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) 	argc--, argv++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) 	while (argc > 0 && argv[0][0] == '-') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) 		/* it's a opt arg. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) 		const char *arg = argv[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) 		argc--, argv++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) 		if (!strcmp(OPT_VERBOSE, arg)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) 			g_debug = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) 		} else if (!strcmp(OPT_HELP, arg)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) 			usage();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) 		} else if (!strcmp(OPT_VERSION, arg)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) 			version();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) 		} else if (!strcmp(OPT_PRINT, arg)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) 			just_print = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) 		} else if (!strcmp(OPT_PACK, arg)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) 			action = ACTION_PACK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) 		} else if (!strcmp(OPT_UNPACK, arg)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) 			action = ACTION_UNPACK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) 		} else if (!strcmp(OPT_TEST_LOAD, arg)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) 			action = ACTION_TEST_LOAD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) 		} else if (!strcmp(OPT_TEST_CHARGE, arg)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) 			action = ACTION_TEST_CHARGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) 		} else if (!memcmp(OPT_IMAGE, arg, strlen(OPT_IMAGE))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) 			snprintf(image_path, sizeof(image_path), "%s", arg + strlen(OPT_IMAGE));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) 		} else if (!memcmp(OPT_ROOT, arg, strlen(OPT_ROOT))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) 			snprintf(root_path, sizeof(root_path), "%s", arg + strlen(OPT_ROOT));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) 			LOGE("Unknown opt:%s", arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) 			usage();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) 			return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) 	if (!image_path[0]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) 		snprintf(image_path, sizeof(image_path), "%s", DEFAULT_IMAGE_PATH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) 	switch (action) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) 	case ACTION_PACK: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) 		int file_num = argc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) 		const char **files = (const char **)argv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) 		if (!file_num) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) 			LOGE("No file to pack!");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) 		LOGD("try to pack %d files.", file_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) 		return pack_image(file_num, files);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) 	case ACTION_UNPACK: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) 		return unpack_image(argc > 0 ? argv[0] : DEFAULT_UNPACK_DIR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) 	case ACTION_TEST_LOAD: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) 		return test_load(argc, argv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) 	case ACTION_TEST_CHARGE: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) 		return test_charge(argc, argv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) 	/* not reach here. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) 	return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) /************unpack code****************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) static bool mkdirs(char *path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) 	char *tmp = path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) 	char *pos = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) 	char buf[MAX_INDEX_ENTRY_PATH_LEN];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) 	bool ret = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) 	while ((pos = memchr(tmp, '/', strlen(tmp)))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) 		strcpy(buf, path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) 		buf[pos - path] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) 		tmp = pos + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) 		LOGD("mkdir:%s", buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) 		if (!mkdir(buf, 0755)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) 			ret = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) 	if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) 		LOGD("Failed to mkdir(%s)!", path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) static bool dump_file(FILE *file, const char *unpack_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261)                       index_tbl_entry entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) 	LOGD("try to dump entry:%s", entry.path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) 	bool ret = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) 	FILE *out_file = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) 	long int pos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) 	char path[MAX_INDEX_ENTRY_PATH_LEN * 2 + 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) 	if (just_print) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) 		ret = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) 		goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) 	pos = ftell(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) 	snprintf(path, sizeof(path), "%s/%s", unpack_dir, entry.path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) 	mkdirs(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) 	out_file = fopen(path, "wb");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) 	if (!out_file) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) 		LOGE("Failed to create:%s", path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) 		goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) 	long int offset = entry.content_offset * BLOCK_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) 	fseek(file, offset, SEEK_SET);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) 	if (offset != ftell(file)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) 		LOGE("Failed to read content:%s", entry.path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) 		goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) 	char buf[BLOCK_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) 	int n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) 	int len = entry.content_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) 	while (len > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) 		n = len > BLOCK_SIZE ? BLOCK_SIZE : len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) 		if (!fread(buf, n, 1, file)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) 			LOGE("Failed to read content:%s", entry.path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) 			goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) 		if (!fwrite(buf, n, 1, out_file)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) 			LOGE("Failed to write:%s", entry.path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) 			goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) 		len -= n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) 	ret = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) end:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) 	if (out_file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) 		fclose(out_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) 	if (pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) 		fseek(file, pos, SEEK_SET);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) static int unpack_image(const char *dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) 	FILE *image_file = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) 	bool ret = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) 	char unpack_dir[MAX_INDEX_ENTRY_PATH_LEN];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) 	if (just_print)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) 		dir = ".";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) 	snprintf(unpack_dir, sizeof(unpack_dir), "%s", dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) 	if (!strlen(unpack_dir)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) 		goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) 	} else if (unpack_dir[strlen(unpack_dir) - 1] == '/') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) 		unpack_dir[strlen(unpack_dir) - 1] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) 	mkdir(unpack_dir, 0755);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) 	image_file = fopen(image_path, "rb");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) 	char buf[BLOCK_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) 	if (!image_file) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) 		LOGE("Failed to open:%s", image_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) 		goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) 	if (!fread(buf, BLOCK_SIZE, 1, image_file)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) 		LOGE("Failed to read header!");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) 		goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) 	memcpy(&header, buf, sizeof(header));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) 	if (memcmp(header.magic, RESOURCE_PTN_HDR_MAGIC, sizeof(header.magic))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) 		LOGE("Not a resource image(%s)!", image_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) 		goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) 	/* switch for be. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) 	fix_header(&header);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) 	printf("Dump header:\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) 	printf("partition version:%d.%d\n", header.resource_ptn_version,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) 	       header.index_tbl_version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) 	printf("header size:%d\n", header.header_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) 	printf("index tbl:\n\toffset:%d\tentry size:%d\tentry num:%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) 	       header.tbl_offset, header.tbl_entry_size, header.tbl_entry_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) 	/* TODO: support header_size & tbl_entry_size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) 	if (header.resource_ptn_version != RESOURCE_PTN_VERSION ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) 	    header.header_size != RESOURCE_PTN_HDR_SIZE ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) 	    header.index_tbl_version != INDEX_TBL_VERSION ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) 	    header.tbl_entry_size != INDEX_TBL_ENTR_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) 		LOGE("Not supported in this version!");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) 		goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) 	printf("Dump Index table:\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) 	index_tbl_entry entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) 	for (i = 0; i < header.tbl_entry_num; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) 		/* TODO: support tbl_entry_size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) 		if (!fread(buf, BLOCK_SIZE, 1, image_file)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) 			LOGE("Failed to read index entry:%d!", i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) 			goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) 		memcpy(&entry, buf, sizeof(entry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) 		if (memcmp(entry.tag, INDEX_TBL_ENTR_TAG, sizeof(entry.tag))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) 			LOGE("Something wrong with index entry:%d!", i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) 			goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) 		/* switch for be. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) 		fix_entry(&entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) 		printf("entry(%d):\n\tpath:%s\n\toffset:%d\tsize:%d\n", i, entry.path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) 		       entry.content_offset, entry.content_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) 		if (!dump_file(image_file, unpack_dir, entry)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) 			goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) 	printf("Unack %s to %s successed!\n", image_path, unpack_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) 	ret = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) end:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) 	if (image_file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) 		fclose(image_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) 	return ret ? 0 : -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) /************unpack code end****************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) /************pack code****************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) static inline size_t get_file_size(const char *path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) 	LOGD("try to get size(%s)...", path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) 	struct stat st;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) 	if (stat(path, &st) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) 		LOGE("Failed to get size:%s", path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) 	LOGD("path:%s, size:%ld", path, st.st_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) 	return st.st_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) static int write_file(int offset_block, const char *src_path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) 		      char hash[], int hash_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) 	LOGD("try to write file(%s) to offset:%d...", src_path, offset_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) 	char *buf = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) 	int ret = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) 	size_t file_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) 	FILE *src_file = fopen(src_path, "rb");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) 	if (!src_file) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) 		LOGE("Failed to open:%s", src_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) 		goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) 	file_size = get_file_size(src_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) 	if (file_size < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) 		goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) 	buf = calloc(file_size, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) 	if (!buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) 		goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) 	if (!fread(buf, file_size, 1, src_file))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) 		goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) 	if (!write_data(offset_block, buf, file_size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) 		goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) 	if (hash_size == 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) 		sha1_csum((const unsigned char *)buf, file_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) 			  (unsigned char *)hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) 	else if (hash_size == 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) 		sha256_csum((const unsigned char *)buf, file_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) 			    (unsigned char *)hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) 		goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) 	ret = file_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) end:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) 	if (src_file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) 		fclose(src_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) 	if (buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) 		free(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) static bool write_header(const int file_num)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) 	LOGD("try to write header...");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) 	memcpy(header.magic, RESOURCE_PTN_HDR_MAGIC, sizeof(header.magic));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) 	header.resource_ptn_version = RESOURCE_PTN_VERSION;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) 	header.index_tbl_version = INDEX_TBL_VERSION;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) 	header.header_size = RESOURCE_PTN_HDR_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) 	header.tbl_offset = header.header_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) 	header.tbl_entry_size = INDEX_TBL_ENTR_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) 	header.tbl_entry_num = file_num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) 	/* switch for le. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) 	resource_ptn_header hdr = header;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) 	fix_header(&hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) 	return write_data(0, &hdr, sizeof(hdr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) static bool write_index_tbl(const int file_num, const char **files)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) 	LOGD("try to write index table...");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) 	bool ret = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) 	bool foundFdt = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) 	int offset =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) 	        header.header_size + header.tbl_entry_size * header.tbl_entry_num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) 	index_tbl_entry entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) 	char hash[20];	/* sha1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) 	memcpy(entry.tag, INDEX_TBL_ENTR_TAG, sizeof(entry.tag));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) 	for (i = 0; i < file_num; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) 		size_t file_size = get_file_size(files[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) 		if (file_size < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) 			goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) 		entry.content_size = file_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) 		entry.content_offset = offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) 		if (write_file(offset, files[i], hash, sizeof(hash)) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) 			goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) 		memcpy(entry.hash, hash, sizeof(hash));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) 		entry.hash_size = sizeof(hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) 		LOGD("try to write index entry(%s)...", files[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) 		/* switch for le. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) 		fix_entry(&entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) 		memset(entry.path, 0, sizeof(entry.path));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) 		const char *path = files[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) 		if (root_path[0]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) 			if (!strncmp(path, root_path, strlen(root_path))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) 				path += strlen(root_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) 				if (path[0] == '/')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) 					path++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) 		path = fix_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) 		if (!strcmp(files[i] + strlen(files[i]) - strlen(DTD_SUBFIX), DTD_SUBFIX)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) 			if (!foundFdt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) 				/* use default path. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) 				LOGD("mod fdt path:%s -> %s...", files[i], FDT_PATH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) 				path = FDT_PATH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) 				foundFdt = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) 		snprintf(entry.path, sizeof(entry.path), "%s", path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) 		offset += fix_blocks(file_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) 		if (!write_data(header.header_size + i * header.tbl_entry_size, &entry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) 		                sizeof(entry)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) 			goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) 	ret = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) end:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) static int pack_image(int file_num, const char **files)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) 	bool ret = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) 	FILE *image_file = fopen(image_path, "wb");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) 	if (!image_file) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) 		LOGE("Failed to create:%s", image_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) 		goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) 	fclose(image_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) 	/* prepare files */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) 	int i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) 	int pos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) 	const char *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) 	for (i = 0; i < file_num; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) 		if (!strcmp(files[i] + strlen(files[i]) - strlen(DTD_SUBFIX), DTD_SUBFIX)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) 			/* dtb files for kernel. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) 			tmp = files[pos];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) 			files[pos] = files[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) 			files[i] = tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) 			pos++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) 		} else if (!strcmp(fix_path(image_path), fix_path(files[i]))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) 			/* not to pack image itself! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) 			tmp = files[file_num - 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) 			files[file_num - 1] = files[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) 			files[i] = tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) 			file_num--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) 	if (!write_header(file_num)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) 		LOGE("Failed to write header!");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) 		goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) 	if (!write_index_tbl(file_num, files)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) 		LOGE("Failed to write index table!");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) 		goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) 	printf("Pack to %s successed!\n", image_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) 	ret = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) end:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) 	return ret ? 0 : -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) /************pack code end****************/