ppsp_impl.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319
  1. /*********************************************************************
  2. * INCLUDES
  3. */
  4. #include "ppsp_impl.h"
  5. #include "ppsp_serv.h"
  6. #include "error.h"
  7. #include "OSAL.h"
  8. #include "core_queu.h"
  9. #include "log.h"
  10. #include "tinycrypt/aes.h"
  11. //#include "ali_genie_profile.h"
  12. //#include "tbrsh_motr_pwms.h"
  13. //#include "tbrsh_leds_pwms.h"
  14. #define PPSP_IMPL_CFGS_BUSY_STAT_BITS 0x00000001
  15. #define PPSP_IMPL_CFGS_AUTH_STAT_BITS 0x00000002
  16. #define PPSP_IMPL_CFGS_OTAS_STAT_BITS 0x00000004 // ota bgns
  17. #define PPSP_IMPL_CFGS_OTAE_STAT_BITS 0x00000008 // ota ends
  18. #define PPSP_IMPL_CFGS_ALIS_PIDS_COUN 4
  19. #define PPSP_IMPL_CFGS_ALIS_MACS_COUN 6
  20. #define PPSP_IMPL_CFGS_ALIS_SCRT_COUN 16
  21. #define PPSP_IMPL_CFGS_MSGS_HDER_SIZE 4
  22. #define PPSP_IMPL_CFGS_OPCO_MISN_ISSU 0x02 // issu by master, resp by slave
  23. #define PPSP_IMPL_CFGS_OPCO_MISR_RESP 0x03 // resp by slave, issu by master
  24. #define PPSP_IMPL_CFGS_OPCO_MNSI_ISSU 0x04 // issu by slave, resp by master
  25. #define PPSP_IMPL_CFGS_OPCO_MRSI_RESP 0x05 // resp by master, issu by slave
  26. #define PPSP_IMPL_CFGS_OPCO_NRSP_ISSU 0x06 // issu W/O resp
  27. #define PPSP_IMPL_CFGS_OPCO_RAND_ISSU 0x10 // issu RAND by master, resp by slave
  28. #define PPSP_IMPL_CFGS_OPCO_CIPR_RESP 0x11 // resp RAND by slave, issu by master. CIPR = ENCR(RAND, BKEY(RAND,MACS,SCRT))
  29. #define PPSP_IMPL_CFGS_OPCO_VERF_ISSU 0x12 // issu VERF by master, resp by slave. verify result of encryption
  30. #define PPSP_IMPL_CFGS_OPCO_BKEY_RESP 0x13 // resp VERF by slave, issu by master
  31. #define PPSP_IMPL_CFGS_OPCO_NWRK_ISSU 0x14 // issu NWRK by master, resp by slave. networking rsult, unprov/proved
  32. #define PPSP_IMPL_CFGS_OPCO_NWRK_RESP 0x15 // resp NWRK by slave, issu by master. confirm of networking rsult
  33. #define PPSP_IMPL_CFGS_OPCO_VERS_ISSU 0x20 // issu VERS by master, resp by slave
  34. #define PPSP_IMPL_CFGS_OPCO_VERS_RESP 0x21 // resp VERS by slave, issu by master
  35. #define PPSP_IMPL_CFGS_OPCO_UPDA_ISSU 0x22 // issu UPDA by master, resp by slave
  36. #define PPSP_IMPL_CFGS_OPCO_UPDA_RESP 0x23 // resp UPDA by slave, issu by master. confirm, rcvd size, fast mode
  37. #define PPSP_IMPL_CFGS_OPCO_PACK_ISSU 0x2F // issu PACK by master, issu W/O resp.
  38. #define PPSP_IMPL_CFGS_OPCO_PACK_RESP 0x24 // issu PACK by slave, issu W/O resp. confirm of total frame
  39. #define PPSP_IMPL_CFGS_OPCO_COMP_ISSU 0x25 // issu COMP by master, resp by slave. complete of transfer
  40. #define PPSP_IMPL_CFGS_OPCO_COMP_RESP 0x26 // resp COMP by slave, issu by master. reply with crc
  41. #define PPSP_IMPL_CFGS_OPCO_USER_ISSU 0xFE // issu USER, resp by slave
  42. #define PPSP_IMPL_CFGS_OPCO_USER_RESP 0xFF // issu USER, resp by slave
  43. #define PPSP_IMPL_CFGS_PROG_ADDR_BASE (0x11000000)
  44. #define PPSP_IMPL_CFGS_PROG_SCTR_SIZE (0x1000) // program sector size in byte
  45. #define PPSP_IMPL_CFGS_PROG_ADDR_BGNS (0x50000) // program data bgn address of flash
  46. #define PPSP_IMPL_CFGS_PROG_FLSH_SIZE (0x30000) // program total size in byte
  47. #define PPSP_IMPL_CFGS_PROG_ADDR_ENDS (PPSP_IMPL_CFGS_PROG_ADDR_BGNS+PPSP_IMPL_CFGS_PROG_FLSH_SIZE) // program data end address of flash
  48. #define PPSP_IMPL_CFGS_PROG_VERS_REVI (1)
  49. #define PPSP_IMPL_CFGS_PROG_VERS_MINR (0)
  50. #define PPSP_IMPL_CFGS_PROG_VERS_MAJR (1)
  51. extern void
  52. ppsp_impl_serv_rcvd_hdlr(uint8 para, uint8 coun);
  53. static ppsp_serv_appl_CBs_t
  54. __ppsp_impl_hdlr_serv = {
  55. ppsp_impl_serv_rcvd_hdlr,
  56. };
  57. static uint32
  58. __ppsp_impl_stat_bits_flag = 0x00;
  59. static uint32
  60. __ppsp_impl_proc_tout_coun = 0x00;
  61. static core_sque_t*
  62. __ppsp_impl_msgs_queu_rcvd = NULL; // received msgs queue
  63. static core_sque_t*
  64. __ppsp_impl_msgs_queu_xfer = NULL; // transfer msgs queue
  65. static const uint8
  66. __ppsp_impl_opco_prim_list[] = {
  67. PPSP_IMPL_CFGS_OPCO_MISN_ISSU,
  68. PPSP_IMPL_CFGS_OPCO_MNSI_ISSU,
  69. PPSP_IMPL_CFGS_OPCO_MISR_RESP,
  70. PPSP_IMPL_CFGS_OPCO_MRSI_RESP,
  71. PPSP_IMPL_CFGS_OPCO_NRSP_ISSU,
  72. PPSP_IMPL_CFGS_OPCO_RAND_ISSU,
  73. PPSP_IMPL_CFGS_OPCO_CIPR_RESP,
  74. PPSP_IMPL_CFGS_OPCO_VERF_ISSU,
  75. PPSP_IMPL_CFGS_OPCO_BKEY_RESP,
  76. PPSP_IMPL_CFGS_OPCO_NWRK_ISSU,
  77. PPSP_IMPL_CFGS_OPCO_NWRK_RESP,
  78. PPSP_IMPL_CFGS_OPCO_VERS_ISSU,
  79. PPSP_IMPL_CFGS_OPCO_VERS_RESP,
  80. PPSP_IMPL_CFGS_OPCO_UPDA_ISSU,
  81. PPSP_IMPL_CFGS_OPCO_UPDA_RESP,
  82. PPSP_IMPL_CFGS_OPCO_PACK_ISSU,
  83. PPSP_IMPL_CFGS_OPCO_PACK_RESP,
  84. PPSP_IMPL_CFGS_OPCO_COMP_ISSU,
  85. PPSP_IMPL_CFGS_OPCO_COMP_RESP,
  86. PPSP_IMPL_CFGS_OPCO_USER_ISSU,
  87. PPSP_IMPL_CFGS_OPCO_USER_RESP,
  88. };
  89. static uint8
  90. __ppsp_impl_opco_prim_coun = sizeof(__ppsp_impl_opco_prim_list);
  91. static uint8
  92. __ppsp_impl_opco_user_coun = 0;
  93. static uint8*
  94. __ppsp_impl_opco_user_list = 0;
  95. static uint8
  96. __ppsp_impl_auth_keys_data[16];
  97. /*
  98. * private function prototype
  99. */
  100. // static uint8
  101. // ppsp_impl_psh_msgs_xfer(const uint8* data, uint16 leng);
  102. /*
  103. * private function implimentation
  104. */
  105. #define ppsp_impl_get_auth_rslt(rslt) \
  106. { \
  107. (rslt) = ((__ppsp_impl_stat_bits_flag & PPSP_IMPL_CFGS_AUTH_STAT_BITS)?1:0); \
  108. }
  109. #define ppsp_impl_set_auth_rslt(flag) \
  110. { \
  111. if ( 0 == (flag) ) \
  112. __ppsp_impl_stat_bits_flag &= ~PPSP_IMPL_CFGS_AUTH_STAT_BITS; \
  113. else \
  114. __ppsp_impl_stat_bits_flag |= PPSP_IMPL_CFGS_AUTH_STAT_BITS; \
  115. }
  116. #define ppsp_impl_get_msgs_numb(msgs, numb) \
  117. { \
  118. if ( 0 != msgs ) \
  119. (numb) = msgs[0]&0x0F; \
  120. }
  121. #define ppsp_impl_set_msgs_numb(msgs, numb) \
  122. { \
  123. if ( 0 != msgs ) \
  124. msgs[0] |= (numb)&0x0F; \
  125. }
  126. #define ppsp_impl_get_msgs_encr(msgs, numb) \
  127. { \
  128. if ( 0 != msgs ) \
  129. (numb) = ((msgs[0]&0x10)>>4); \
  130. }
  131. #define ppsp_impl_set_msgs_encr(msgs, flag) \
  132. { \
  133. if ( 0 != msgs ) \
  134. msgs[0] |= (((flag)&0x01)<<4); \
  135. }
  136. #define ppsp_impl_get_msgs_opco(msgs, opco) \
  137. { \
  138. if ( 0 != msgs ) \
  139. (opco) = msgs[1]&0xFF; \
  140. }
  141. #define ppsp_impl_set_msgs_opco(msgs, opco) \
  142. { \
  143. if ( 0 != msgs ) \
  144. msgs[1] = (opco)&0xFF; \
  145. }
  146. #define ppsp_impl_get_msgs_seqn(msgs, alln, seqn) \
  147. { \
  148. if ( 0 != msgs ) { \
  149. alln = (msgs[2]&0xF0)>>4; \
  150. seqn = (msgs[2]&0x0F)>>0; \
  151. } \
  152. }
  153. #define ppsp_impl_set_msgs_seqn(msgs, alln, seqn) \
  154. { \
  155. if ( 0 != msgs ) \
  156. msgs[2] = ((alln&0x0F))<<4 | ((seqn&0x0F)<<0); \
  157. }
  158. /*
  159. * desc: get msg payload length, byte 3 of header
  160. */
  161. #define ppsp_impl_get_msgs_frsz(msgs, frsz) \
  162. { \
  163. if ( 0 != msgs ) \
  164. (frsz) = msgs[3]&0xFF; \
  165. }
  166. #define ppsp_impl_set_msgs_frsz(msgs, frsz) \
  167. { \
  168. if ( 0 != msgs ) \
  169. msgs[3] = (frsz)&0xFF; \
  170. }
  171. /*
  172. * desc: get msg payload
  173. */
  174. #define ppsp_impl_get_msgs_plds(msgs, data) \
  175. { \
  176. if ( 0 != msgs ) \
  177. ((uint8*)(data)) = (((uint8*)(msgs))+PPSP_IMPL_CFGS_MSGS_HDER_SIZE); \
  178. }
  179. /*
  180. * desc: set msg payload
  181. */
  182. #define ppsp_impl_set_msgs_plds(msgs, data, coun) \
  183. { \
  184. if ( 0 != (uint8*)(msgs) ) \
  185. osal_memcpy(((uint8*)(msgs))+PPSP_IMPL_CFGS_MSGS_HDER_SIZE, (uint8*)(data), coun); \
  186. }
  187. /*
  188. * desc: PKCS#7 padding
  189. */
  190. #define ppsp_impl_get_pkcs_7pad(bksz, dasz, pval) \
  191. { \
  192. pval = bksz - (dasz % bksz); \
  193. }
  194. /*
  195. * desc: chk mesg package size
  196. */
  197. static uint8
  198. ppsp_impl_chk_msgs_leng(const uint8* mesg, uint16 coun)
  199. {
  200. return ( NULL != mesg && PPSP_IMPL_CFGS_MSGS_HDER_SIZE <= coun );
  201. }
  202. /*
  203. * desc: chk mesg id
  204. */
  205. static uint8
  206. ppsp_impl_chk_msgs_numb(const uint8* mesg, uint16 coun)
  207. {
  208. uint8 rslt = 0;
  209. if ( 0 != mesg ) {
  210. uint8 numb;
  211. ppsp_impl_get_msgs_numb(mesg, numb);
  212. rslt = ((0 <= numb) && (15 >= numb));
  213. } else {
  214. rslt = 0;
  215. }
  216. return ( rslt );
  217. }
  218. /*
  219. * desc: chk mesg opcode
  220. */
  221. static uint8
  222. ppsp_impl_chk_msgs_opco(const uint8* mesg, uint16 coun)
  223. {
  224. uint8 rslt = 0;
  225. if ( NULL != mesg ) {
  226. uint8 opco;
  227. opco = mesg[1] & 0xFF;
  228. for ( int itr0 = 0; itr0 < __ppsp_impl_opco_prim_coun; itr0 += 1 ) {
  229. if ( rslt = (opco == __ppsp_impl_opco_prim_list[itr0]) ) {
  230. break;
  231. }
  232. }
  233. if ( !rslt )
  234. for ( int itr0 = 0; itr0 < __ppsp_impl_opco_user_coun; itr0 += 1 ) {
  235. if ( rslt = (opco == __ppsp_impl_opco_user_list[itr0]) ) {
  236. break;
  237. }
  238. }
  239. }
  240. return ( rslt );
  241. }
  242. /*
  243. * desc: chk mesg segment number + total segment number
  244. */
  245. static uint8
  246. ppsp_impl_chk_msgs_seqn(const uint8* mesg, uint16 coun)
  247. {
  248. uint8 rslt = 1;
  249. if ( NULL != mesg ) {
  250. uint8 numb;
  251. /* numb of segment sequence */
  252. numb = (mesg[2] & 0x0F) >> 0;
  253. rslt = (0 <= numb && 15 >= numb);
  254. }
  255. if ( 1 == rslt ) {
  256. uint8 numb;
  257. /* numb of total segments */
  258. numb = (mesg[2] & 0xF0) >> 4;
  259. rslt = (0 <= numb && 15 >= numb);
  260. }
  261. return ( rslt );
  262. }
  263. /*
  264. * desc: create response message
  265. */
  266. static void*
  267. ppsp_impl_new_msgs_resp(uint8 numb, uint8 opco, uint8* data, uint16 leng)
  268. {
  269. uint8* msgs = 0;
  270. /* only consider unsegmented case */
  271. msgs = osal_mem_alloc(PPSP_IMPL_CFGS_MSGS_HDER_SIZE+leng);
  272. if ( 0 != msgs ) {
  273. osal_memset(msgs, 0, PPSP_IMPL_CFGS_MSGS_HDER_SIZE+leng);
  274. ppsp_impl_set_msgs_numb(msgs, numb);
  275. // ppsp_impl_set_msgs_encr(msgs, numb);
  276. // ppsp_impl_set_msgs_vers(msgs, numb);
  277. ppsp_impl_set_msgs_opco(msgs, opco);
  278. ppsp_impl_set_msgs_seqn(msgs, 0, 0); // frame numb, frame sequ
  279. ppsp_impl_set_msgs_frsz(msgs, leng); // frame size
  280. ppsp_impl_set_msgs_plds(msgs, data, leng); // payload
  281. // LOG("\r\nmsgs: >>> ");
  282. // for ( uint8 itr0 = 0; itr0 < PPSP_IMPL_CFGS_MSGS_HDER_SIZE+leng; itr0 += 1 ) {
  283. // LOG("%02x,", msgs[itr0]);
  284. // }
  285. // LOG("msgs: <<< \r\n");
  286. }
  287. return ( msgs );
  288. }
  289. static uint8
  290. ppsp_impl_psh_msgs_rcvd(const uint8* mesg, uint16 coun)
  291. {
  292. uint8 rslt = 1;
  293. if ( 0 == mesg ) {
  294. LOG("[PANDA][ERR] NULL POINTER !!!");
  295. rslt = 0;
  296. }
  297. // for (uint8 itr0 = 0; itr0 < coun; itr0 += 1)
  298. // LOG("[PANDA][INF] rcvd msg: data[%d]=%02x", itr0, mesg[itr0]);
  299. /* */
  300. if ( 1 == rslt ) {
  301. rslt = ppsp_impl_chk_msgs_leng(mesg, coun);
  302. // LOG("[PANDA][INF] ppsp_impl_chk_msgs_leng: %s\n\r", rslt ? "OK" :"NG");
  303. // printf("[PANDA][INF] ppsp_impl_chk_msgs_leng: %s\n\r", rslt ? "OK" :"NG");
  304. }
  305. /* */
  306. if ( 1 == rslt ) {
  307. rslt = ppsp_impl_chk_msgs_numb(mesg, coun);
  308. // LOG("[PANDA][ERR] ppsp_impl_chk_msgs_numb: %s\n\r", rslt ? "OK" :"NG");
  309. // printf("[PANDA][ERR] ppsp_impl_chk_msgs_numb: %s\n\r", rslt ? "OK" :"NG");
  310. }
  311. /* */
  312. if ( 1 == rslt ) {
  313. rslt = ppsp_impl_chk_msgs_opco(mesg, coun);
  314. // LOG("[PANDA][ERR] ppsp_impl_chk_msgs_opco: %s\n\r", rslt ? "OK" :"NG");
  315. // printf("[PANDA][ERR] ppsp_impl_chk_msgs_opco: %s\n\r", rslt ? "OK" :"NG");
  316. }
  317. /* */
  318. if ( 1 == rslt ) {
  319. rslt = ppsp_impl_chk_msgs_seqn(mesg, coun);
  320. // LOG("[PANDA][ERR] ppsp_impl_chk_msgs_seqn: %s\n\r", rslt ? "OK" :"NG");
  321. // printf("[PANDA][ERR] ppsp_impl_chk_msgs_seqn: %s\n\r", rslt ? "OK" :"NG");
  322. }
  323. /* */
  324. if ( 1 == rslt ) {
  325. uint8* vmsg = osal_mem_alloc(coun);
  326. osal_memcpy(vmsg, mesg, coun);
  327. core_sque_psh(__ppsp_impl_msgs_queu_rcvd, &vmsg);
  328. // if segment msg, enqueue();
  329. // else notify upper layer new msgs
  330. // __ppsp_impl_msgs_rcvd = (uint8*)data;
  331. }
  332. return ( rslt );
  333. }
  334. static uint8
  335. ppsp_impl_psh_msgs_xfer(const uint8* data, uint16 leng)
  336. {
  337. core_sque_psh(__ppsp_impl_msgs_queu_xfer, &data);
  338. }
  339. static void
  340. ppsp_impl_cvt_hex2Str(const unsigned char* const srcs, unsigned char* dest, int coun, int revs)
  341. {
  342. }
  343. static void
  344. ppsp_impl_prc_msgs_rand(uint8* msgs_rcvd)
  345. {
  346. uint8 itr0;
  347. uint8 msgn;
  348. uint8 rsiz;
  349. uint8* rand;
  350. ppsp_impl_get_msgs_numb(msgs_rcvd, msgn); // mesg id
  351. ppsp_impl_get_msgs_frsz(msgs_rcvd, rsiz); // rand size
  352. ppsp_impl_get_msgs_plds(msgs_rcvd, rand); // rand pointer
  353. uint8 pids[PPSP_IMPL_CFGS_ALIS_PIDS_COUN];
  354. uint8 macs[PPSP_IMPL_CFGS_ALIS_MACS_COUN];
  355. uint8 scrt[PPSP_IMPL_CFGS_ALIS_SCRT_COUN];
  356. //load PID
  357. ppsp_impl_get_pids(pids);
  358. //load MAC
  359. ppsp_impl_get_macs(macs);
  360. ppsp_impl_get_scrt(scrt);
  361. if ( 1 == ppsp_impl_cal_keys(rand, rsiz, pids, sizeof(pids), macs, sizeof(macs), scrt, sizeof(scrt)) ) {
  362. LOG("\n\rKEY:>>> "); for ( itr0 = 0; itr0 < sizeof(__ppsp_impl_auth_keys_data); itr0 ++ ) LOG("%02x,", __ppsp_impl_auth_keys_data[itr0]); LOG("KEY:<<< \n\r");
  363. uint8* msgs_xfer = 0;
  364. uint8 cipr[16] = { 0x00, }; // ciper
  365. ppsp_impl_enc_text(rand, cipr);
  366. LOG("\n\rCIP:>>> "); for ( itr0 = 0; itr0 < sizeof(cipr); itr0 ++ ) LOG("%02x,", cipr[itr0]); LOG("KEY:<<< \n\r");
  367. msgs_xfer = ppsp_impl_new_msgs_resp(msgn, PPSP_IMPL_CFGS_OPCO_CIPR_RESP, cipr, sizeof(cipr));
  368. if ( 0 != msgs_xfer ) ppsp_impl_psh_msgs_xfer(msgs_xfer, 20);
  369. } else {
  370. LOG("[PANDA][ERR] gen_aligenie_auth_key FAIL !!!");
  371. }
  372. }
  373. static void
  374. ppsp_impl_prc_msgs_verf(uint8* msgs_rcvd)
  375. {
  376. uint8 msgn;
  377. uint8 frsz;
  378. uint8* plds;
  379. ppsp_impl_get_msgs_numb(msgs_rcvd, msgn);
  380. ppsp_impl_get_msgs_frsz(msgs_rcvd, frsz);
  381. ppsp_impl_get_msgs_plds(msgs_rcvd, plds);
  382. if ( NULL != plds && 0 == plds[0] ) {
  383. uint8* msgs_xfer = 0;
  384. /* Auth Success */
  385. ppsp_impl_set_auth_rslt(1);
  386. msgs_xfer = ppsp_impl_new_msgs_resp(msgn, PPSP_IMPL_CFGS_OPCO_BKEY_RESP, plds, frsz);
  387. if ( 0 != msgs_xfer ) ppsp_impl_psh_msgs_xfer(msgs_xfer, 20);
  388. }
  389. }
  390. static void
  391. ppsp_impl_prc_msgs_nwrk(uint8* msgs_rcvd)
  392. {
  393. uint8 msgn;
  394. uint8 encr;
  395. uint8 frsz;
  396. uint8* plds;
  397. ppsp_impl_get_msgs_numb(msgs_rcvd, msgn);
  398. ppsp_impl_get_msgs_encr(msgs_rcvd, encr);
  399. ppsp_impl_get_msgs_frsz(msgs_rcvd, frsz);
  400. ppsp_impl_get_msgs_plds(msgs_rcvd, plds);
  401. uint8 text[16];
  402. if ( 0x01 == encr ) {
  403. ppsp_impl_dec_cipr(text, plds);
  404. }
  405. if ( 0 == text[0] ) {
  406. LOG("NETW EXIT DONE");
  407. } else
  408. if ( 1 == text[0] ) {
  409. LOG("NETW CFGS DONE");
  410. }
  411. uint8 padd;
  412. ppsp_impl_get_pkcs_7pad(16, 1, padd);
  413. osal_memset(text, padd, sizeof(text));
  414. text[0] = 0x01;
  415. uint8 cipr[16];
  416. if ( 0x01 == encr ) {
  417. ppsp_impl_enc_text(text, cipr);
  418. }
  419. uint8* xfer = ppsp_impl_new_msgs_resp(msgn, PPSP_IMPL_CFGS_OPCO_NWRK_RESP, cipr, sizeof(cipr));
  420. ppsp_impl_set_msgs_encr(xfer, encr);
  421. if ( 0 != xfer ) ppsp_impl_psh_msgs_xfer(xfer, 20);
  422. }
  423. /*****************************************************************************/
  424. // ota relative
  425. uint8 __ppsp_impl_upda_type;
  426. uint32 __ppsp_impl_upda_vers;
  427. uint32 __ppsp_impl_upda_fwsz;
  428. uint16 __ppsp_impl_upda_crcs; // crc from upstream
  429. uint8 __ppsp_impl_upda_flag;
  430. uint8 __ppsp_impl_upda_buff[256];
  431. uint16 __ppsp_impl_upda_offs; // offset of filled posi
  432. uint32 __ppsp_impl_upda_wrsz;
  433. uint16 __ppsp_impl_upda_crcd; // crc calc on local
  434. uint32 __ppsp_impl_upda_dnsz;
  435. static uint8
  436. ppsp_impl_era_prog_data(uint32 addr)
  437. {
  438. uint8 rslt = PPlus_ERR_FATAL;
  439. // flash address range check
  440. if ( PPSP_IMPL_CFGS_PROG_ADDR_BGNS > addr || PPSP_IMPL_CFGS_PROG_ADDR_ENDS <= addr ) {
  441. rslt = PPlus_ERR_INVALID_ADDR;
  442. goto RSLT_FAIL_ADDR;
  443. }
  444. flash_sector_erase(PPSP_IMPL_CFGS_PROG_ADDR_BASE + (addr&0xFFF000));
  445. rslt = PPlus_SUCCESS;
  446. RSLT_FAIL_ADDR:
  447. return ( rslt );
  448. }
  449. static uint8
  450. ppsp_impl_pul_prog_data(uint32 addr, uint32* valu)
  451. {
  452. uint8 rslt = PPlus_ERR_FATAL;
  453. // flash address range check
  454. if ( PPSP_IMPL_CFGS_PROG_ADDR_BGNS > addr || PPSP_IMPL_CFGS_PROG_ADDR_ENDS <= addr ) {
  455. rslt = PPlus_ERR_INVALID_ADDR;
  456. goto RSLT_FAIL_ADDR;
  457. }
  458. // 4 bytes aligned
  459. if ( addr & 0x000003 ) {
  460. rslt = PPlus_ERR_DATA_ALIGN;
  461. goto RSLT_FAIL_ADDR;
  462. }
  463. *valu = read_reg(PPSP_IMPL_CFGS_PROG_ADDR_BASE + addr);
  464. rslt = PPlus_SUCCESS;
  465. RSLT_FAIL_ADDR:
  466. return ( rslt );
  467. }
  468. static uint8
  469. ppsp_impl_psh_prog_data(uint32 addr, uint32 valu)
  470. {
  471. uint8 rslt = PPlus_ERR_FATAL;
  472. // flash address range check
  473. if ( PPSP_IMPL_CFGS_PROG_ADDR_BGNS > addr || PPSP_IMPL_CFGS_PROG_ADDR_ENDS <= addr ) {
  474. rslt = PPlus_ERR_INVALID_ADDR;
  475. goto RSLT_FAIL_ADDR;
  476. }
  477. // 4 bytes aligned
  478. if ( addr & 0x000003 ) {
  479. rslt = PPlus_ERR_DATA_ALIGN;
  480. goto RSLT_FAIL_ADDR;
  481. }
  482. if ( 0 == ProgramWord(PPSP_IMPL_CFGS_PROG_ADDR_BASE + addr, (uint8*)&valu, sizeof(valu)) ) {
  483. rslt = PPlus_ERR_FATAL;
  484. goto RSLT_FAIL_PROG;
  485. }
  486. rslt = PPlus_SUCCESS;
  487. RSLT_FAIL_PROG:
  488. RSLT_FAIL_ADDR:
  489. return ( rslt );
  490. }
  491. static uint16
  492. ppsp_impl_cal_crc16_CCITT_FALSE(uint16 crci, uint8* data, uint32 coun)
  493. {
  494. uint16 wCRCin = crci;
  495. uint16 wCPoly = 0x1021;
  496. while (coun--) {
  497. wCRCin ^= (*(data++) << 8);
  498. for (int i = 0; i < 8; i++) {
  499. if (wCRCin & 0x8000)
  500. wCRCin = (wCRCin << 1) ^ wCPoly;
  501. else
  502. wCRCin = wCRCin << 1;
  503. }
  504. }
  505. return (wCRCin);
  506. }
  507. static void
  508. ppsp_impl_prc_msgs_vers(uint8* msgs)
  509. {
  510. uint8 msgn;
  511. uint8 encr;
  512. uint8 frsz;
  513. uint8* plds;
  514. ppsp_impl_get_msgs_numb(msgs, msgn);
  515. ppsp_impl_get_msgs_encr(msgs, encr);
  516. ppsp_impl_get_msgs_frsz(msgs, frsz);
  517. ppsp_impl_get_msgs_plds(msgs, plds);
  518. if ( (0x01 == encr && 0x10 != frsz) || (0x00 == encr && 0x01 != frsz) ) {
  519. LOG("%s, !! INVALID MESG CONTENT !!");
  520. goto RSLT_FAIL_MESG;
  521. }
  522. uint8 text[16], size, offs, padd; // plain text, text size, offset, padding byte;
  523. if ( 0x01 == encr ) {
  524. ppsp_impl_dec_cipr(text, plds);
  525. } else {
  526. osal_memcpy(text, plds, frsz);
  527. }
  528. text[0] = (0x00==text[0]) ? 0x00 : 0xFF; // type
  529. text[1] = PPSP_IMPL_CFGS_PROG_VERS_REVI; // revision
  530. text[2] = PPSP_IMPL_CFGS_PROG_VERS_MINR; // minor
  531. text[3] = PPSP_IMPL_CFGS_PROG_VERS_MAJR; // major
  532. text[4] = 0x00; // reserved
  533. size = sizeof(text);
  534. offs = 5;
  535. uint8 cipr[16]; // ciper
  536. if ( 0x01 == encr ) {
  537. ppsp_impl_get_pkcs_7pad(size, offs, padd);
  538. osal_memset(text+offs, padd, size-offs);
  539. ppsp_impl_enc_text(text, cipr);
  540. }
  541. uint8* xfer;
  542. if ( 0x01 == encr ) {
  543. xfer = ppsp_impl_new_msgs_resp(msgn, PPSP_IMPL_CFGS_OPCO_VERS_RESP, cipr, sizeof(cipr));
  544. } else {
  545. xfer = ppsp_impl_new_msgs_resp(msgn, PPSP_IMPL_CFGS_OPCO_VERS_RESP, text, offs);
  546. }
  547. ppsp_impl_set_msgs_encr(xfer, encr);
  548. if ( 0 != xfer ) ppsp_impl_psh_msgs_xfer(xfer, 20);
  549. // if ( __ppsp_impl_stat_bits_flag & PPSP_IMPL_CFGS_OTAE_STAT_BITS ) {
  550. // __ppsp_impl_stat_bits_flag &= ~PPSP_IMPL_CFGS_OTAE_STAT_BITS;
  551. // write_reg(0x4000f034, 0); // flag as an OTAs auto reset
  552. // }
  553. RSLT_FAIL_MESG:
  554. }
  555. static void
  556. ppsp_impl_prc_msgs_upda(uint8* msgs)
  557. {
  558. uint8 msgn;
  559. uint8 encr;
  560. uint8 frsz;
  561. uint8* plds;
  562. ppsp_impl_get_msgs_numb(msgs, msgn);
  563. ppsp_impl_get_msgs_encr(msgs, encr);
  564. ppsp_impl_get_msgs_frsz(msgs, frsz);
  565. ppsp_impl_get_msgs_plds(msgs, plds);
  566. if ( (0x01 == encr && 0x10 != frsz) || (0x00 == encr && 0x0C != frsz) ) {
  567. LOG("%s, !! INVALID MESG CONTENT !! \r\n");
  568. goto RSLT_FAIL_MESG;
  569. }
  570. // plain text, text size, offset, padding byte
  571. uint8 cipr_text[16], text_size, padd_offs, padd_valu;
  572. if ( 0x01 == encr ) {
  573. ppsp_impl_dec_cipr(cipr_text, plds);
  574. } else {
  575. osal_memcpy(cipr_text, plds, frsz);
  576. }
  577. uint8 copy_offs = 0;
  578. osal_memcpy(&__ppsp_impl_upda_type, cipr_text+copy_offs, sizeof(__ppsp_impl_upda_type)); // fw type
  579. copy_offs += sizeof(__ppsp_impl_upda_type);
  580. osal_memcpy(&__ppsp_impl_upda_vers, cipr_text+copy_offs, sizeof(__ppsp_impl_upda_vers)); // fw version
  581. copy_offs += sizeof(__ppsp_impl_upda_vers);
  582. osal_memcpy(&__ppsp_impl_upda_fwsz, cipr_text+copy_offs, sizeof(__ppsp_impl_upda_fwsz)); // fw size in byte
  583. copy_offs += sizeof(__ppsp_impl_upda_fwsz);
  584. osal_memcpy(&__ppsp_impl_upda_crcs, cipr_text+copy_offs, sizeof(__ppsp_impl_upda_crcs)); // crc16
  585. copy_offs += sizeof(__ppsp_impl_upda_crcs);
  586. osal_memcpy(&__ppsp_impl_upda_flag, cipr_text+copy_offs, sizeof(__ppsp_impl_upda_flag)); // flag: 0:full update, 1:incr update
  587. LOG("%s, type:%x,vers:%x,size:%x,crcs:%x,flag:%x \r\n",
  588. __func__, __ppsp_impl_upda_type, __ppsp_impl_upda_vers, __ppsp_impl_upda_fwsz, __ppsp_impl_upda_crcs, __ppsp_impl_upda_flag);
  589. if ( PPSP_IMPL_CFGS_PROG_FLSH_SIZE < __ppsp_impl_upda_fwsz ) {
  590. LOG("%s, !! FIRMWARE SIZE EXCEED LIMIT OF %d BYTES !! \r\n", __func__, PPSP_IMPL_CFGS_PROG_FLSH_SIZE);
  591. goto RSLT_FAIL_FWSZ;
  592. }
  593. uint32 sctr = (__ppsp_impl_upda_fwsz/PPSP_IMPL_CFGS_PROG_SCTR_SIZE) + (__ppsp_impl_upda_fwsz%PPSP_IMPL_CFGS_PROG_SCTR_SIZE?1:0);
  594. for ( uint8 itr0 = 0; itr0 < sctr; itr0 += 1 ) {
  595. // PPSP_IMPL_CFGS_PROG_ADDR_BGNS + sctr * 0x1000
  596. ppsp_impl_era_prog_data(PPSP_IMPL_CFGS_PROG_ADDR_BGNS + (itr0<<12));
  597. }
  598. LOG("%s, total sctr:%x@size:%x erased!! \r\n", __func__, sctr, __ppsp_impl_upda_fwsz);
  599. /* resume of last trans point is not supported */
  600. // reset to initial
  601. __ppsp_impl_upda_offs = 0;
  602. __ppsp_impl_upda_wrsz = 0;
  603. __ppsp_impl_upda_dnsz = 0;
  604. __ppsp_impl_upda_crcd = 0xFFFF;
  605. // LOG("%s, pkcs#7=%x\n", __func__, padd);
  606. cipr_text[0] = 0x01; // 1: upda allow, 0: upda deny
  607. osal_memcpy(cipr_text+1, &__ppsp_impl_upda_dnsz, sizeof(__ppsp_impl_upda_dnsz));
  608. // text[1] = 0x00; // last upda posi
  609. // text[2] = 0x00; // last upda posi
  610. // text[3] = 0x00; // last upda posi
  611. // text[4] = 0x00; // last upda posi
  612. cipr_text[5] = 0x0F; // maxi frame in pack(0x00~0x0f:1~16)
  613. text_size = sizeof(cipr_text);
  614. padd_offs = 6;
  615. uint8 cipr[16];
  616. if ( 0x01 == encr ) {
  617. ppsp_impl_get_pkcs_7pad(text_size, padd_offs, padd_valu);
  618. osal_memset(cipr_text+padd_offs, padd_valu, text_size-padd_offs);
  619. ppsp_impl_enc_text(cipr_text, cipr);
  620. }
  621. uint8* xfer;
  622. if ( 0x01 == encr ) {
  623. xfer = ppsp_impl_new_msgs_resp(msgn, PPSP_IMPL_CFGS_OPCO_UPDA_RESP, cipr, sizeof(cipr));
  624. } else {
  625. xfer = ppsp_impl_new_msgs_resp(msgn, PPSP_IMPL_CFGS_OPCO_UPDA_RESP, cipr_text, padd_offs);
  626. }
  627. ppsp_impl_set_msgs_encr(xfer, encr);
  628. if ( 0 != xfer ) ppsp_impl_psh_msgs_xfer(xfer, 20);
  629. RSLT_FAIL_FWSZ:
  630. RSLT_FAIL_MESG:
  631. }
  632. static void
  633. ppsp_impl_prc_msgs_pack(uint8* msgs)
  634. {
  635. uint8 msgn;
  636. uint8 encr;
  637. uint8 alln;
  638. uint8 seqn;
  639. uint8 frsz; // payload size in byte
  640. uint8* plds; // payloads
  641. ppsp_impl_get_msgs_numb(msgs, msgn);
  642. ppsp_impl_get_msgs_encr(msgs, encr);
  643. ppsp_impl_get_msgs_seqn(msgs, alln, seqn);
  644. ppsp_impl_get_msgs_frsz(msgs, frsz);
  645. ppsp_impl_get_msgs_plds(msgs, plds);
  646. // if ( 0x01 == encr || 0x10 != frsz ) {
  647. // LOG("%s, !! INVALID MESG CONTENT !!");
  648. // goto RSLT_FAIL_MESG;
  649. // }
  650. // TODO: HANDLE EXCEPTION OF FRAME LOSS, IN THAT CASE, LAST FRAME NUMB SHOULD RESP FOR RETRANSMITION
  651. /*
  652. * copy offset of this received frame, data copy upto the offset would be copied to a buffer.
  653. * when the buffer is full, data in this buffer should write into flash right a way.
  654. * above procedure could repeat untill all data of the frame, being write into flash.
  655. */
  656. uint16 offs = 0;
  657. while ( offs < frsz ) {
  658. uint16 size = MIN(sizeof(__ppsp_impl_upda_buff)-__ppsp_impl_upda_offs, frsz-offs);
  659. osal_memcpy(__ppsp_impl_upda_buff+__ppsp_impl_upda_offs, plds+offs, size);
  660. __ppsp_impl_upda_offs += size;
  661. offs += size;
  662. if ( sizeof(__ppsp_impl_upda_buff) == __ppsp_impl_upda_offs ) {
  663. // flsh pack
  664. uint32 dwrd = (__ppsp_impl_upda_offs/sizeof(uint32)) + (__ppsp_impl_upda_offs%sizeof(uint32)?1:0);
  665. for ( int itr0 = 0; itr0 < dwrd; itr0 += 1 ) {
  666. uint32 valw, valr;
  667. // push for write
  668. ppsp_impl_psh_prog_data(
  669. PPSP_IMPL_CFGS_PROG_ADDR_BGNS + __ppsp_impl_upda_wrsz + (itr0 * sizeof(uint32)),
  670. valw = *((uint32*)__ppsp_impl_upda_buff + itr0));
  671. }
  672. // __ppsp_impl_upda_crcd = ppsp_impl_cal_crc16_CCITT_FALSE(__ppsp_impl_upda_crcd, __ppsp_impl_upda_buff, __ppsp_impl_upda_offs);
  673. __ppsp_impl_upda_wrsz += __ppsp_impl_upda_offs;
  674. __ppsp_impl_upda_offs = 0;
  675. }
  676. }
  677. __ppsp_impl_upda_dnsz += frsz;
  678. LOG("%s, crci:%04x, dnsz:%08x \r\n", __func__, __ppsp_impl_upda_crcd, __ppsp_impl_upda_dnsz);
  679. if ( alln != seqn ) {
  680. goto RSLT_SKIP_MESG;
  681. }
  682. /* complete of a package, should resp rcvd seqn, rcvd size */
  683. uint8 cipr_text[16], text_size, padd_offs, padd_valu; // plain text, text size, offset, padding byte;
  684. cipr_text[0] = ((alln&0x0F)<<4)|((seqn&0x0F)<<0); //
  685. osal_memcpy(cipr_text+1, &__ppsp_impl_upda_dnsz, sizeof(__ppsp_impl_upda_dnsz));
  686. // text[1] = 0x00; // last upda posi
  687. // text[2] = 0x20; // last upda posi
  688. // text[3] = 0x00; // last upda posi
  689. // text[4] = 0x00; // last upda posi
  690. text_size = sizeof(cipr_text);
  691. padd_offs = 5;
  692. uint8 cipr_data[16];
  693. uint8 auth;
  694. ppsp_impl_get_auth_rslt(auth);
  695. if ( auth ) {
  696. ppsp_impl_get_pkcs_7pad(text_size, padd_offs, padd_valu);
  697. osal_memset(cipr_text, padd_valu, text_size-padd_offs);
  698. ppsp_impl_enc_text(cipr_text, cipr_data);
  699. }
  700. uint8* xfer;
  701. if ( auth ) {
  702. xfer = ppsp_impl_new_msgs_resp(msgn, PPSP_IMPL_CFGS_OPCO_PACK_RESP, cipr_data, sizeof(cipr_data));
  703. } else {
  704. xfer = ppsp_impl_new_msgs_resp(msgn, PPSP_IMPL_CFGS_OPCO_PACK_RESP, cipr_text, padd_offs);
  705. }
  706. ppsp_impl_set_msgs_encr(xfer, auth);
  707. if ( 0 != xfer ) ppsp_impl_psh_msgs_xfer(xfer, 20);
  708. RSLT_FAIL_MESG:
  709. RSLT_SKIP_MESG:
  710. }
  711. static void
  712. ppsp_impl_prc_msgs_comp(uint8* msgs)
  713. {
  714. uint8 msgn;
  715. uint8 encr;
  716. uint8 frsz;
  717. uint8* plds;
  718. ppsp_impl_get_msgs_numb(msgs, msgn);
  719. ppsp_impl_get_msgs_encr(msgs, encr);
  720. ppsp_impl_get_msgs_frsz(msgs, frsz);
  721. ppsp_impl_get_msgs_plds(msgs, plds);
  722. if ( (0x01 == encr && 0x10 != frsz) || (0x00 == encr && 0x01 != frsz) ) {
  723. LOG("%s, !! INVALID MESG CONTENT !! \r\n", __func__);
  724. goto RSLT_FAIL_MESG;
  725. }
  726. uint8 cipr_text[16], text_size, padd_offs, padd_valu; // plain text, text size, offset, padding byte;
  727. if ( 0x01 == encr ) {
  728. ppsp_impl_dec_cipr(cipr_text, plds);
  729. } else {
  730. osal_memcpy(cipr_text, plds, frsz);
  731. }
  732. if ( 0x01 != cipr_text[0] ) {
  733. LOG("%s, !! INVALID MESG CONTENT !! \r\n", __func__);
  734. goto RSLT_FAIL_MESG;
  735. }
  736. // flsh remains in buffer
  737. if ( 0 < __ppsp_impl_upda_offs ) {
  738. // flsh pack
  739. uint32 dwrd = (__ppsp_impl_upda_offs/sizeof(uint32)) + (__ppsp_impl_upda_offs%sizeof(uint32)?1:0);
  740. for ( int itr0 = 0; itr0 < dwrd; itr0 += 1 ) {
  741. uint32 valw, valr;
  742. // push for write
  743. ppsp_impl_psh_prog_data(
  744. PPSP_IMPL_CFGS_PROG_ADDR_BGNS + __ppsp_impl_upda_wrsz + (itr0 * sizeof(uint32)),
  745. valw = *((uint32*)__ppsp_impl_upda_buff + itr0));
  746. }
  747. // __ppsp_impl_upda_crcd = ppsp_impl_cal_crc16_CCITT_FALSE(__ppsp_impl_upda_crcd, __ppsp_impl_upda_buff, __ppsp_impl_upda_offs);
  748. __ppsp_impl_upda_wrsz += __ppsp_impl_upda_offs;
  749. __ppsp_impl_upda_offs = 0;
  750. }
  751. /* complete of bins, should calc crc16 */
  752. __ppsp_impl_upda_crcd = 0xffff;
  753. for ( int itr0 = 0; itr0 < __ppsp_impl_upda_fwsz; itr0++ ) {
  754. uint8 valu;
  755. memcpy(&valu, (uint8_t*)(PPSP_IMPL_CFGS_PROG_ADDR_BASE+PPSP_IMPL_CFGS_PROG_ADDR_BGNS+itr0), 1);
  756. __ppsp_impl_upda_crcd = ppsp_impl_cal_crc16_CCITT_FALSE(__ppsp_impl_upda_crcd, &valu, 1);
  757. }
  758. LOG("%s, crci:%04x, dnsz:%08x, fwsz:%08x \r\n", __func__, __ppsp_impl_upda_crcd, __ppsp_impl_upda_dnsz, __ppsp_impl_upda_fwsz);
  759. //printf("%s, crci:%04x, dnsz:%08x, fwsz:%08x \n", __func__, __ppsp_impl_upda_crcd, __ppsp_impl_upda_dnsz, __ppsp_impl_upda_fwsz);
  760. /* complete of bins, should write tags for bldr */
  761. // uint32 tags;
  762. // osal_memcpy(&tags, "OTAF", 4);
  763. ppsp_impl_psh_prog_data(PPSP_IMPL_CFGS_PROG_ADDR_BGNS, 0x4641544f); // tag:"OTAF"
  764. // printf("%s, tags:%x \r\n", __func__, tags);
  765. // osal_memset(&tags, 0xFF, 4);
  766. // ppsp_impl_pul_prog_data(PPSP_IMPL_CFGS_PROG_ADDR_BGNS,&tags);
  767. // printf("%s, tags:%x \r\n", __func__, tags);
  768. /* complete of bins, should resp crcs chck */
  769. cipr_text[0] = ((__ppsp_impl_upda_crcd == __ppsp_impl_upda_crcs) ? 0x01 : 0x00); //1:succ, 0:fail
  770. LOG("%s, complete of bins: crcs comp:%s \r\n", __func__, cipr_text[0]?"SUCC": "FAIL");
  771. //printf("%s, complete of bins: crcs comp:%s \n", __func__, cipr_text[0]?"SUCC": "FAIL");
  772. text_size = sizeof(cipr_text);
  773. padd_offs = 1;
  774. uint8 cipr_data[16];
  775. if ( 0x01 == encr ) {
  776. ppsp_impl_get_pkcs_7pad(text_size, padd_offs, padd_valu);
  777. osal_memset(cipr_text, padd_valu, text_size-padd_offs);
  778. ppsp_impl_enc_text(cipr_text, cipr_data);
  779. }
  780. uint8* xfer;
  781. if ( 0x01 == encr ) {
  782. xfer = ppsp_impl_new_msgs_resp(msgn, PPSP_IMPL_CFGS_OPCO_COMP_RESP, cipr_data, sizeof(cipr_data));
  783. } else {
  784. xfer = ppsp_impl_new_msgs_resp(msgn, PPSP_IMPL_CFGS_OPCO_COMP_RESP, cipr_text, padd_offs);
  785. }
  786. ppsp_impl_set_msgs_encr(xfer, encr);
  787. if ( 0 != xfer ) ppsp_impl_psh_msgs_xfer(xfer, 20);
  788. __ppsp_impl_stat_bits_flag |= PPSP_IMPL_CFGS_OTAS_STAT_BITS;
  789. __ppsp_impl_proc_tout_coun = 10; // 1secs
  790. RSLT_FAIL_MESG:
  791. }
  792. static void
  793. ppsp_impl_prc_msgs_user(uint8* msgs_rcvd)
  794. {
  795. uint8 msgn;
  796. uint8 frsz;
  797. uint8* plds;
  798. ppsp_impl_get_msgs_numb(msgs_rcvd, msgn);
  799. ppsp_impl_get_msgs_frsz(msgs_rcvd, frsz);
  800. ppsp_impl_get_msgs_plds(msgs_rcvd, plds);
  801. if ( NULL == plds || 0 >= frsz ) goto RSLT_FAIL_NULL;
  802. /* do whatever user wants */
  803. //tbrsh_leds_gpio_set_mode(TBRSH_LEDS_ENUM_LED1, plds[0]);
  804. //tbrsh_leds_gpio_set_mode(TBRSH_LEDS_ENUM_LED2, plds[0]);
  805. //tbrsh_leds_gpio_set_mode(TBRSH_LEDS_ENUM_LED3, plds[0]);
  806. //tbrsh_motr_pwms_set_valu(30);
  807. //tbrsh_motr_pwms_set_mode(plds[0]);
  808. /* do whatever user wants */
  809. uint8* xfer = 0;
  810. plds[0] = 0x01; // set resp of true
  811. xfer = ppsp_impl_new_msgs_resp(msgn, PPSP_IMPL_CFGS_OPCO_BKEY_RESP, plds, frsz);
  812. if ( NULL == xfer ) goto RSLT_FAIL_NMEM;
  813. ppsp_impl_psh_msgs_xfer(xfer, 20);
  814. RSLT_FAIL_NMEM:
  815. RSLT_FAIL_NULL:
  816. }
  817. static uint8
  818. ppsp_impl_pop_msgs_rcvd(void)
  819. {
  820. uint8 rslt = 0;
  821. uint8* mesg = NULL;
  822. rslt = core_sque_pop(__ppsp_impl_msgs_queu_rcvd, &mesg);
  823. /* */
  824. if ( 1 == rslt ) {
  825. uint8 opco;
  826. ppsp_impl_get_msgs_opco(mesg, opco);
  827. LOG("[PANDA][ERR] ppsp_impl_get_msgs_opco: %02x\n\r", opco);
  828. if ( PPSP_IMPL_CFGS_OPCO_RAND_ISSU == opco ) {
  829. //printf("[PANDA][ERR] ppsp_impl_get_msgs_opco: %02x\n\r", opco);
  830. ppsp_impl_prc_msgs_rand(mesg);
  831. } else
  832. if ( PPSP_IMPL_CFGS_OPCO_VERF_ISSU == opco ) {
  833. //printf("[PANDA][ERR] ppsp_impl_get_msgs_opco: %02x\n\r", opco);
  834. ppsp_impl_prc_msgs_verf(mesg);
  835. } else
  836. if ( PPSP_IMPL_CFGS_OPCO_NWRK_ISSU == opco ) {
  837. //printf("[PANDA][ERR] ppsp_impl_get_msgs_opco: %02x\n\r", opco);
  838. ppsp_impl_prc_msgs_nwrk(mesg);
  839. } else
  840. if ( PPSP_IMPL_CFGS_OPCO_VERS_ISSU == opco ) {
  841. //printf("[PANDA][ERR] ppsp_impl_get_msgs_opco: %02x\n\r", opco);
  842. ppsp_impl_prc_msgs_vers(mesg);
  843. } else
  844. if ( PPSP_IMPL_CFGS_OPCO_UPDA_ISSU == opco ) {
  845. //printf("[PANDA][ERR] ppsp_impl_get_msgs_opco: %02x\n\r", opco);
  846. ppsp_impl_prc_msgs_upda(mesg);
  847. } else
  848. if ( PPSP_IMPL_CFGS_OPCO_PACK_ISSU == opco ) {
  849. ppsp_impl_prc_msgs_pack(mesg);
  850. } else
  851. if ( PPSP_IMPL_CFGS_OPCO_COMP_ISSU == opco ) {
  852. //printf("[PANDA][ERR] ppsp_impl_get_msgs_opco: %02x\n\r", opco);
  853. ppsp_impl_prc_msgs_comp(mesg);
  854. } else
  855. if ( PPSP_IMPL_CFGS_OPCO_USER_ISSU == opco ) {
  856. ppsp_impl_prc_msgs_user(mesg);
  857. }
  858. osal_mem_free(mesg);
  859. }
  860. return ( rslt );
  861. }
  862. static void
  863. ppsp_impl_serv_rcvd_hdlr(uint8 para, uint8 coun)
  864. {
  865. // LOG("[PANDA][ENT] %s para:%d, coun:%d \r\n", __func__, para, coun);
  866. if ( PPSP_SERV_CFGS_CHAR_FFD5_INDX == para ||
  867. PPSP_SERV_CFGS_CHAR_FFD7_INDX == para ) {
  868. void* data = NULL;
  869. if ( NULL == (data = osal_mem_alloc(coun)) ) return;
  870. ppsp_serv_get_para(para, data, coun);
  871. ppsp_impl_psh_msgs_rcvd(data, coun);
  872. ppsp_impl_pop_msgs_rcvd();
  873. if ( NULL != data ) osal_mem_free(data);
  874. }
  875. }
  876. /* alia: ppsp_impl_pop_msgs_xfer */
  877. static void
  878. ppsp_impl_serv_xfer_hdlr(void)
  879. {
  880. // LOG("[PANDA][ENT] %s \r\n", __func__);
  881. uint8 rslt = 1;
  882. uint8* mesg = NULL;
  883. uint8 opco = 0;
  884. uint8 frsz = 0;
  885. rslt = core_sque_pop(__ppsp_impl_msgs_queu_xfer, &mesg);
  886. if ( 1 == rslt ) {
  887. ppsp_impl_get_msgs_opco(mesg, opco);
  888. ppsp_impl_get_msgs_frsz(mesg, frsz);
  889. if ( PPSP_IMPL_CFGS_OPCO_VERS_ISSU <= opco && PPSP_IMPL_CFGS_OPCO_COMP_RESP >= opco )
  890. ppsp_serv_set_para(PPSP_SERV_CFGS_CHAR_FFD8_INDX, frsz + PPSP_IMPL_CFGS_MSGS_HDER_SIZE, (void*)mesg);
  891. else
  892. ppsp_serv_set_para(PPSP_SERV_CFGS_CHAR_FFD6_INDX, frsz + PPSP_IMPL_CFGS_MSGS_HDER_SIZE, (void*)mesg);
  893. osal_mem_free(mesg);
  894. }
  895. }
  896. void
  897. ppsp_impl_appl_timr_hdlr(void)
  898. {
  899. ppsp_impl_serv_xfer_hdlr();
  900. if ( __ppsp_impl_stat_bits_flag & PPSP_IMPL_CFGS_OTAS_STAT_BITS ) {
  901. if ( 0 < __ppsp_impl_proc_tout_coun ) {
  902. __ppsp_impl_proc_tout_coun --;
  903. if ( 0 == __ppsp_impl_proc_tout_coun ) {
  904. //printf("%s, hal_system_soft_reset for bgns of otas \r\n");
  905. write_reg(0x4000f034, 'O'); // flag as an OTAs auto reset
  906. hal_system_soft_reset();
  907. }
  908. }
  909. }/* else
  910. if ( __ppsp_impl_stat_bits_flag & PPSP_IMPL_CFGS_OTAE_STAT_BITS ) {
  911. if ( 0 < __ppsp_impl_proc_tout_coun ) {
  912. __ppsp_impl_proc_tout_coun --;
  913. if ( 0 == __ppsp_impl_proc_tout_coun ) {
  914. printf("%s, hal_system_soft_reset for ends of otas \r\n");
  915. // write_reg(0x4000f034, 'O'); // flag as an OTAs auto reset
  916. hal_system_soft_reset();
  917. }
  918. }
  919. } */
  920. }
  921. uint8
  922. ppsp_impl_ini(void)
  923. {
  924. __ppsp_impl_stat_bits_flag = 0x00;
  925. uint32 regs = read_reg(0x4000f034);
  926. if ( 'O' == regs ) {
  927. write_reg(0x4000f034, 0); // flag as an OTAs auto reset
  928. __ppsp_impl_stat_bits_flag |= PPSP_IMPL_CFGS_OTAE_STAT_BITS;
  929. // __ppsp_impl_proc_tout_coun = 300; // 30secs
  930. }
  931. __ppsp_impl_msgs_queu_rcvd = core_sque_new(sizeof(uint8*), 32);
  932. __ppsp_impl_msgs_queu_xfer = core_sque_new(sizeof(uint8*), 32);
  933. ppsp_serv_add_serv(PPSP_SERV_CFGS_SERV_FEB3_MASK);
  934. ppsp_serv_reg_appl(&__ppsp_impl_hdlr_serv);
  935. LOG("\r\n %s, ALIS VERS NUMB: %02d.%02d.%02d \r\n",
  936. __func__, PPSP_IMPL_CFGS_PROG_VERS_MAJR, PPSP_IMPL_CFGS_PROG_VERS_MINR, PPSP_IMPL_CFGS_PROG_VERS_REVI);
  937. }
  938. uint32
  939. ppsp_impl_get_stat(void)
  940. {
  941. return ( __ppsp_impl_stat_bits_flag );
  942. }
  943. uint8
  944. ppsp_impl_get_pids(uint8* pids)
  945. {
  946. uint8 rslt = 1;
  947. if ( NULL != pids ) {
  948. // load PID
  949. for ( uint8 itr0 = 0; itr0 < PPSP_IMPL_CFGS_ALIS_PIDS_COUN; itr0 += 1 ) {
  950. pids[PPSP_IMPL_CFGS_ALIS_PIDS_COUN-itr0-1] = (uint8_t)ReadFlash(0x4030+itr0);
  951. }
  952. }
  953. return ( rslt );
  954. }
  955. uint8
  956. ppsp_impl_get_macs(uint8* macs)
  957. {
  958. uint8 rslt = 1;
  959. uint32 addr = 0x4000;
  960. if ( NULL != macs ) {
  961. // load MAC
  962. macs [3] = (uint8)ReadFlash(addr ++);
  963. macs [2] = (uint8)ReadFlash(addr ++);
  964. macs [1] = (uint8)ReadFlash(addr ++);
  965. macs [0] = (uint8)ReadFlash(addr ++);
  966. macs [5] = (uint8)ReadFlash(addr ++);
  967. macs [4] = (uint8)ReadFlash(addr);
  968. }
  969. return ( rslt );
  970. }
  971. uint8
  972. ppsp_impl_get_scrt(uint8* scrt)
  973. {
  974. uint8 rslt = 1;
  975. if ( NULL != scrt ) {
  976. for ( uint8 itr0 = 0; itr0 < PPSP_IMPL_CFGS_ALIS_SCRT_COUN; itr0 ++ ) {
  977. scrt[itr0] = (uint8_t)ReadFlash(0x4010+itr0);
  978. }
  979. }
  980. return ( rslt );
  981. }
  982. /* calc auth keys */
  983. uint8
  984. ppsp_impl_cal_keys(const uint8* rand, uint8 rsiz, const uint8* pids, uint8 psiz, const uint8* macs, uint8 msiz, const uint8* scrt, uint8 ssiz)
  985. {
  986. uint8 rslt = 1;
  987. #if 0
  988. gen_aligenie_auth_key((uint8*)rand, rsiz, (uint8*)pids, psiz, (uint8*)macs, msiz, (uint8*)scrt, ssiz);
  989. cpy_aligenie_auth_key(__ppsp_impl_auth_keys_data);
  990. #endif
  991. return ( rslt );
  992. }
  993. uint8
  994. ppsp_impl_get_keys(uint8* keys)
  995. {
  996. uint8 rslt = 0;
  997. if ( NULL == keys ) {
  998. rslt = 0;
  999. goto RSLT_FAIL_NULL;
  1000. }
  1001. osal_memcpy(keys, __ppsp_impl_auth_keys_data, sizeof(__ppsp_impl_auth_keys_data));
  1002. rslt = 1;
  1003. RSLT_FAIL_NULL:
  1004. return ( rslt );
  1005. }
  1006. uint8
  1007. ppsp_impl_enc_text(uint8* text, uint8* cipr)
  1008. {
  1009. uint8 rslt = 1;
  1010. uint8 iv[16] = { 0x31, 0x32, 0x33, 0x61, 0x71, 0x77, 0x65, 0x64,
  1011. 0x23, 0x2a, 0x24, 0x21, 0x28, 0x34, 0x6a, 0x75, };
  1012. uint8 data[16];
  1013. uint8 itr0 = 0;
  1014. osal_memcpy(data, text, 16);
  1015. while ( itr0 < 16 ) {
  1016. data[itr0] ^= iv[itr0];
  1017. itr0 += 1;
  1018. }
  1019. struct tc_aes_key_sched_struct s;
  1020. if ( 0 == tc_aes128_set_encrypt_key(&s, __ppsp_impl_auth_keys_data) ) {
  1021. LOG("AES128 %s (NIST encr test) failed.\n", __func__);
  1022. rslt = 0;
  1023. goto RSLT_FAIL_ENCR;
  1024. }
  1025. if (tc_aes_encrypt(cipr, data, &s) == 0) {
  1026. LOG("AES128 %s (NIST encr test) failed.\n", __func__);
  1027. rslt = 0;
  1028. goto RSLT_FAIL_ENCR;
  1029. }
  1030. rslt = 1;
  1031. RSLT_FAIL_ENCR:
  1032. return ( rslt );
  1033. }
  1034. uint8
  1035. ppsp_impl_dec_cipr(uint8* text, uint8* cipr)
  1036. {
  1037. uint8 rslt = 1;
  1038. struct tc_aes_key_sched_struct s;
  1039. if (0 == tc_aes128_set_decrypt_key(&s, __ppsp_impl_auth_keys_data) ) {
  1040. LOG("AES128 %s (NIST decr test) failed.\n", __func__);
  1041. rslt = 0;
  1042. goto RSLT_FAIL_ENCR;
  1043. }
  1044. if (tc_aes_decrypt(text, cipr, &s) == 0) {
  1045. LOG("AES128 %s (NIST decr test) failed.\n", __func__);
  1046. rslt = 0;
  1047. goto RSLT_FAIL_ENCR;
  1048. }
  1049. uint8 aesiv[16] = { 0x31, 0x32, 0x33, 0x61, 0x71, 0x77, 0x65, 0x64,
  1050. 0x23, 0x2a, 0x24, 0x21, 0x28, 0x34, 0x6a, 0x75, };
  1051. uint8 itr0 = 0;
  1052. while ( itr0 < 16 ) {
  1053. text[itr0] ^= aesiv[itr0];
  1054. itr0 += 1;
  1055. }
  1056. rslt = 1;
  1057. RSLT_FAIL_ENCR:
  1058. return ( rslt );
  1059. }
  1060. /*
  1061. * callback of connection stat changes
  1062. */
  1063. void
  1064. ppsp_impl_ack_conn(uint8 flag)
  1065. {
  1066. // /* this likely a disconnection after OTAs, reset is required for complete */
  1067. // if ( __ppsp_impl_stat_bits_flag & PPSP_IMPL_CFGS_OTAS_STAT_BITS ) {
  1068. // write_reg(0x4000f034, 'O'); // flag as an OTAs auto reset
  1069. // hal_system_soft_reset();
  1070. // }
  1071. // // connection loss, shold reset all status
  1072. // __ppsp_impl_stat_bits_flag = 0x00;
  1073. }