mt_scp_excep.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. /* Copyright Statement:
  2. *
  3. * This software/firmware and related documentation ("MediaTek Software") are
  4. * protected under relevant copyright laws. The information contained herein
  5. * is confidential and proprietary to MediaTek Inc. and/or its licensors.
  6. * Without the prior written permission of MediaTek inc. and/or its licensors,
  7. * any reproduction, modification, use or disclosure of MediaTek Software,
  8. * and information contained herein, in whole or in part, shall be strictly prohibited.
  9. */
  10. /* MediaTek Inc. (C) 2015. All rights reserved.
  11. *
  12. * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
  13. * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
  14. * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER ON
  15. * AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
  18. * NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
  19. * SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
  20. * SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES TO LOOK ONLY TO SUCH
  21. * THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. RECEIVER EXPRESSLY ACKNOWLEDGES
  22. * THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES
  23. * CONTAINED IN MEDIATEK SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK
  24. * SOFTWARE RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
  25. * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND
  26. * CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
  27. * AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
  28. * OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY RECEIVER TO
  29. * MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
  30. */
  31. #include <stdint.h>
  32. #include <string.h>
  33. #include <debug.h>
  34. #include <platform/boot_mode.h>
  35. #include <platform/partition.h>
  36. #include <platform/verified_boot.h>
  37. #include <platform/sec_policy.h>
  38. #ifdef MTK_PARTITION_COMMON
  39. #include <env.h>
  40. #else
  41. #include <platform/env.h>
  42. #endif
  43. #include <assert.h>
  44. #include <platform/mt_scp.h>
  45. #include <platform/errno.h>
  46. #include <mt_scp_excep.h>
  47. #include <reg.h>
  48. #include <malloc.h>
  49. #include <lib/crc.h>
  50. #include <lib/zutil.h>
  51. #include <lib/zlib.h>
  52. #include <platform/mtk_wdt.h>
  53. #ifdef MTK_3LEVEL_PAGETABLE
  54. #include <arch/arm/mmu.h>
  55. #include <stdlib.h>
  56. #include <err.h>
  57. #endif
  58. static inline void elf_setup_eident(unsigned char e_ident[EI_NIDENT], unsigned char elfclasz)
  59. {
  60. memcpy(e_ident, "\177ELF", 4); /* memcpy(e_ident, ELFMAG, SELFMAG) */
  61. e_ident[EI_CLASS] = elfclasz;
  62. e_ident[EI_DATA] = ELFDATA2LSB;
  63. e_ident[EI_VERSION] = EV_CURRENT;
  64. e_ident[EI_OSABI] = ELFOSABI_NONE;
  65. memset(e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD);
  66. }
  67. struct elf_siginfo {
  68. int si_signo;
  69. int si_code;
  70. int si_errno;
  71. };
  72. struct elf32_note_t {
  73. __u32 n_namesz; /* Name size */
  74. __u32 n_descsz; /* Content size */
  75. __u32 n_type; /* Content type */
  76. };
  77. struct elf32_timeval {
  78. int32_t tv_sec;
  79. int32_t tv_usec;
  80. };
  81. struct elf32_prstatus {
  82. struct elf_siginfo pr_info;
  83. short pr_cursig;
  84. uint32_t pr_sigpend;
  85. uint32_t pr_sighold;
  86. int32_t pr_pid;
  87. int32_t pr_ppid;
  88. int32_t pr_pgrp;
  89. int32_t pr_sid;
  90. struct elf32_timeval pr_utime;
  91. struct elf32_timeval pr_stime;
  92. struct elf32_timeval pr_cutime;
  93. struct elf32_timeval pr_cstime;
  94. uint32_t pr_reg[ELF_NGREGS];
  95. int32_t pr_fpvalid;
  96. };
  97. struct elf32_prpsinfo {
  98. char pr_state;
  99. char pr_sname;
  100. char pr_zomb;
  101. char pr_nice;
  102. uint32_t pr_flag;
  103. uint16_t pr_uid;
  104. uint16_t pr_gid;
  105. int32_t pr_pid;
  106. int32_t pr_ppid;
  107. int32_t pr_pgrp;
  108. int32_t pr_sid;
  109. char pr_fname[16];
  110. char pr_psargs[ELF_PRARGSZ];
  111. };
  112. struct scp_status_reg {
  113. unsigned int pc;
  114. unsigned int lr;
  115. unsigned int psp;
  116. unsigned int sp;
  117. unsigned int m2h;
  118. unsigned int h2m;
  119. };
  120. /* An ELF note in memory */
  121. struct memelfnote {
  122. const char *name;
  123. int type;
  124. unsigned int datasz;
  125. void *data;
  126. };
  127. static uint32_t roundup(uint32_t x, uint32_t y)
  128. {
  129. return ((x + y - 1) / y) * y;
  130. }
  131. static int notesize(struct memelfnote *en)
  132. {
  133. int sz;
  134. sz = sizeof(struct elf32_note);
  135. sz += roundup((strlen(en->name) + 1), 4);
  136. sz += roundup(en->datasz, 4);
  137. return sz;
  138. }
  139. static uint8_t *storenote(struct memelfnote *men, uint8_t *bufp)
  140. {
  141. struct elf32_note en;
  142. en.n_namesz = strlen(men->name) + 1;
  143. en.n_descsz = men->datasz;
  144. en.n_type = men->type;
  145. memcpy(bufp, &en, sizeof(en));
  146. bufp += sizeof(en);
  147. memcpy(bufp, men->name, en.n_namesz);
  148. bufp += en.n_namesz;
  149. bufp = (uint8_t *) roundup((unsigned long)bufp, 4);
  150. memcpy(bufp, men->data, men->datasz);
  151. bufp += men->datasz;
  152. bufp = (uint8_t *) roundup((unsigned long)bufp, 4);
  153. return bufp;
  154. }
  155. static uint8_t *core_write_cpu_note(int cpu, struct elf32_phdr *nhdr, uint8_t *bufp, enum scp_core_id id)
  156. {
  157. struct memelfnote notes;
  158. struct elf32_prstatus prstatus;
  159. char cpustr[16];
  160. unsigned int scp_A_task_context_addr;
  161. struct scp_region_info_st* scp_region_info = (void *)(SCP_SRAM_BASE + SCP_LOADER_SIZE);
  162. scp_A_task_context_addr = (unsigned int)scp_region_info->TaskContext_ptr;
  163. memset(&prstatus, 0, sizeof(struct elf32_prstatus));
  164. snprintf(cpustr, sizeof(cpustr), "CPU%d", cpu);
  165. /* set up the process status */
  166. notes.name = cpustr;
  167. notes.type = NT_PRSTATUS;
  168. notes.datasz = sizeof(struct elf32_prstatus);
  169. notes.data = &prstatus;
  170. prstatus.pr_pid = cpu + 1;
  171. /* if TaskContext_ptr with enable bit[31] */
  172. if ((scp_A_task_context_addr & 0x80000000) && (id == SCP_A_ID)) {
  173. memcpy((void *)&(prstatus.pr_reg),
  174. (void *)(SCP_SRAM_BASE + (scp_A_task_context_addr & 0x7fffffff)), sizeof(prstatus.pr_reg));
  175. }
  176. if (prstatus.pr_reg[15] == 0x0 && (id == SCP_A_ID))
  177. prstatus.pr_reg[15] = readl(SCP_WDT_PC);
  178. if (prstatus.pr_reg[14] == 0x0 && (id == SCP_A_ID))
  179. prstatus.pr_reg[14] = readl(SCP_WDT_LR);
  180. if (prstatus.pr_reg[13] == 0x0 && (id == SCP_A_ID))
  181. prstatus.pr_reg[13] = readl(SCP_WDT_PSP);
  182. nhdr->p_filesz += notesize(&notes);
  183. return storenote(&notes, bufp);
  184. }
  185. void exception_header_init(void *oldbufp, enum scp_core_id id)
  186. {
  187. struct elf32_phdr *nhdr, *phdr;
  188. struct elf32_hdr *elf;
  189. off_t offset = 0;
  190. uint8_t *bufp = oldbufp;
  191. uint32_t cpu;
  192. uint32_t dram_size = 0;
  193. struct scp_region_info_st* scp_region_info = (void *)(SCP_SRAM_BASE + SCP_LOADER_SIZE);
  194. /* NT_PRPSINFO */
  195. struct elf32_prpsinfo prpsinfo;
  196. struct memelfnote notes;
  197. elf = (struct elf32_hdr *) bufp;
  198. bufp += sizeof(struct elf32_hdr);
  199. offset += sizeof(struct elf32_hdr);
  200. elf_setup_eident(elf->e_ident, ELFCLASS32);
  201. /*setup elf header*/
  202. elf->e_type = ET_CORE;
  203. elf->e_machine = EM_ARM;
  204. elf->e_version = EV_CURRENT;
  205. elf->e_entry = 0;
  206. elf->e_phoff = sizeof(struct elf32_hdr);
  207. elf->e_shoff = 0;
  208. elf->e_flags = ELF_CORE_EFLAGS;
  209. elf->e_ehsize = sizeof(struct elf32_hdr);
  210. elf->e_phentsize = sizeof(struct elf32_phdr);
  211. elf->e_phnum = 2;
  212. elf->e_shentsize = 0;
  213. elf->e_shnum = 0;
  214. elf->e_shstrndx = 0;
  215. nhdr = (struct elf32_phdr *) bufp;
  216. bufp += sizeof(struct elf32_phdr);
  217. offset += sizeof(struct elf32_phdr);
  218. memset(nhdr, 0, sizeof(struct elf32_phdr));
  219. nhdr->p_type = 4; /* PT_NOTE */
  220. phdr = (struct elf32_phdr *) bufp;
  221. bufp += sizeof(struct elf32_phdr);
  222. offset += sizeof(struct elf32_phdr);
  223. phdr->p_flags = 0x4|0x2|0x1; /* PF_R|PF_W|PF_X */
  224. phdr->p_offset = CRASH_MEMORY_HEADER_SIZE;
  225. phdr->p_vaddr = CRASH_MEMORY_OFFSET;
  226. phdr->p_paddr = CRASH_MEMORY_OFFSET;
  227. if ((int)(scp_region_info->ap_dram_size) > 0)
  228. dram_size = scp_region_info->ap_dram_size;
  229. phdr->p_filesz = CRASH_MEMORY_LENGTH + roundup(dram_size, 4);
  230. phdr->p_memsz = CRASH_MEMORY_LENGTH + roundup(dram_size, 4);
  231. phdr->p_align = 0;
  232. phdr->p_type = 1; /* PT_LOAD */
  233. nhdr->p_offset = offset;
  234. /* set up the process info */
  235. notes.name = CORE_STR;
  236. notes.type = NT_PRPSINFO;
  237. notes.datasz = sizeof(struct elf32_prpsinfo);
  238. notes.data = &prpsinfo;
  239. memset(&prpsinfo, 0, sizeof(struct elf32_prpsinfo));
  240. prpsinfo.pr_state = 0;
  241. prpsinfo.pr_sname = 'R';
  242. prpsinfo.pr_zomb = 0;
  243. prpsinfo.pr_gid = prpsinfo.pr_uid = 0x0;
  244. strlcpy(prpsinfo.pr_fname, "freertos8", sizeof(prpsinfo.pr_fname));
  245. strlcpy(prpsinfo.pr_psargs, "freertos8", ELF_PRARGSZ);
  246. nhdr->p_filesz += notesize(&notes);
  247. bufp = storenote(&notes, bufp);
  248. /* Store pre-cpu backtrace */
  249. for (cpu = 0; cpu < 1; cpu++)
  250. bufp = core_write_cpu_note(cpu, nhdr, bufp, id);
  251. }
  252. /*dump scp reg*/
  253. void scp_reg_copy(void *bufp)
  254. {
  255. struct scp_reg_dump_list *scp_reg;
  256. uint32_t tmp;
  257. scp_reg = (struct scp_reg_dump_list *) bufp;
  258. /*setup scp reg*/
  259. scp_reg->scp_reg_magic = 0xDEADBEEF;
  260. scp_reg->ap_resource = readl(SCP_AP_RESOURCE);
  261. scp_reg->bus_resource = readl(SCP_BUS_RESOURCE);
  262. scp_reg->slp_protect = readl(SCP_SLP_PROTECT_CFG);
  263. scp_reg->cpu_sleep_status = readl(SCP_CPU_SLEEP_STATUS);
  264. scp_reg->clk_sw_sel = readl(SCP_CLK_SW_SEL);
  265. scp_reg->clk_enable = readl(SCP_CLK_ENABLE);
  266. scp_reg->clk_high_core = readl(SCP_CLK_HIGH_CORE_CG);
  267. scp_reg->debug_wdt_sp = readl(SCP_WDT_SP);
  268. scp_reg->debug_wdt_lr = readl(SCP_WDT_LR);
  269. scp_reg->debug_wdt_psp = readl(SCP_WDT_PSP);
  270. scp_reg->debug_wdt_pc = readl(SCP_WDT_PC);
  271. scp_reg->debug_addr_s2r = readl(SCP_DEBUG_ADDR_S2R);
  272. scp_reg->debug_addr_dma = readl(SCP_DEBUG_ADDR_DMA);
  273. scp_reg->debug_addr_spi0 = readl(SCP_DEBUG_ADDR_SPI0);
  274. scp_reg->debug_addr_spi1 = readl(SCP_DEBUG_ADDR_SPI1);
  275. scp_reg->debug_addr_spi2 = readl(SCP_DEBUG_ADDR_SPI2);
  276. scp_reg->debug_bus_status = readl(SCP_DEBUG_BUS_STATUS);
  277. /* scp_reg->debug_infra_mon = readl(SCP_SYS_INFRA_MON); */
  278. tmp = readl(SCP_BUS_CTRL)&(~dbg_irq_info_sel_mask);
  279. writel(tmp | (0 << dbg_irq_info_sel_shift) ,SCP_BUS_CTRL);
  280. scp_reg->infra_addr_latch = readl(SCP_DEBUG_IRQ_INFO);
  281. writel(tmp | (1 << dbg_irq_info_sel_shift) ,SCP_BUS_CTRL);
  282. scp_reg->ddebug_latch = readl(SCP_DEBUG_IRQ_INFO);
  283. writel(tmp | (2 << dbg_irq_info_sel_shift) ,SCP_BUS_CTRL);
  284. scp_reg->pdebug_latch = readl(SCP_DEBUG_IRQ_INFO);
  285. writel(tmp | (3 << dbg_irq_info_sel_shift) ,SCP_BUS_CTRL);
  286. scp_reg->pc_value = readl(SCP_DEBUG_IRQ_INFO);
  287. scp_reg->scp_reg_magic_end = 0xDEADBEEF;
  288. }
  289. /*dump scp l1c header*/
  290. void scp_sub_header_init(void *bufp)
  291. {
  292. struct scp_region_info_st* scp_region_info = (void *)(SCP_SRAM_BASE + SCP_LOADER_SIZE);
  293. struct scp_dump_header_list *scp_sub_head;
  294. scp_sub_head = (struct scp_dump_header_list *) bufp;
  295. /*setup scp reg*/
  296. scp_sub_head->scp_head_magic = 0xDEADBEEF;
  297. memcpy(&scp_sub_head->scp_region_info,
  298. scp_region_info, sizeof(struct scp_region_info_st));
  299. scp_sub_head->scp_head_magic_end = 0xDEADBEEF;
  300. }
  301. #define BUF_SIZE 65536
  302. int scp_zip_compress(unsigned char* input, unsigned char* output, unsigned int input_size)
  303. {
  304. int ret, flush;
  305. int have;
  306. z_stream strm;
  307. int i = 0;
  308. int j = 0;
  309. unsigned int avail_input_size = input_size;
  310. unsigned char *ibuf;
  311. unsigned char *obuf;
  312. ibuf = malloc(BUF_SIZE);
  313. if (ibuf == NULL)
  314. return Z_BUF_ERROR;
  315. obuf = malloc(BUF_SIZE);
  316. if (obuf == NULL) {
  317. free(ibuf);
  318. return Z_BUF_ERROR;
  319. }
  320. strm.zalloc = Z_NULL;
  321. strm.zfree = Z_NULL;
  322. strm.opaque = Z_NULL;
  323. /* allocate deflate state */
  324. memset(&strm, 0, sizeof(z_stream));
  325. ret = deflateInit2(&strm, Z_BEST_COMPRESSION, Z_DEFLATED, 31, 8, Z_DEFAULT_STRATEGY);
  326. if (ret != Z_OK) {
  327. free(ibuf);
  328. free(obuf);
  329. return ret;
  330. }
  331. /* compress until end of file */
  332. do {
  333. strm.avail_in = BUF_SIZE;
  334. strm.next_in = ibuf;
  335. strm.avail_out = BUF_SIZE;
  336. strm.next_out = obuf;
  337. flush = Z_NO_FLUSH;
  338. if (avail_input_size < BUF_SIZE) {
  339. strm.avail_in = avail_input_size;
  340. flush = Z_FINISH;
  341. }
  342. memcpy(ibuf, (void *)&input[i], strm.avail_in);
  343. i += strm.avail_in;
  344. avail_input_size -= strm.avail_in;
  345. strm.next_in = ibuf;
  346. /* run deflate() on input until output buffer not full, finish
  347. compression if all of source has been read in */
  348. do {
  349. strm.avail_out = BUF_SIZE;
  350. strm.next_out = obuf;
  351. ret = deflate(&strm, flush); /* no bad return value */
  352. assert(ret != Z_STREAM_ERROR); /* state not clobbered */
  353. have = BUF_SIZE - strm.avail_out;
  354. memcpy((void *)&output[j], obuf, have);
  355. j+=have;
  356. mtk_wdt_restart();
  357. } while (strm.avail_out == 0);
  358. assert(strm.avail_in == 0); /* all input will be used */
  359. /* done when last data in file processed */
  360. } while (flush != Z_FINISH);
  361. assert(ret == Z_STREAM_END); /* stream will be complete */
  362. free(ibuf);
  363. free(obuf);
  364. /* clean up and return */
  365. (void)deflateEnd(&strm);
  366. return j;
  367. }
  368. /*
  369. * this function need SCP to keeping awaken
  370. * scp_crash_dump: dump scp tcm info.
  371. * @param MemoryDump: scp dump struct
  372. * @param scp_core_id: core id
  373. * @return: scp dump size
  374. */
  375. #define SCP_EE_SIZE 0xA0000 //640 KB
  376. int scp_crash_dump(void *crash_buffer)
  377. {
  378. unsigned int scp_dump_size;
  379. int datasize;
  380. struct MemoryDump *pDump;
  381. void *tmp_ptr;
  382. uint32_t dram_start = 0;
  383. uint32_t dram_size = 0;
  384. struct scp_region_info_st* scp_region_info = (void *)(SCP_SRAM_BASE + SCP_LOADER_SIZE);
  385. /* L1C support? */
  386. if ((int)(scp_region_info->ap_dram_size) <= 0) {
  387. scp_dump_size = sizeof(struct MemoryDump);
  388. } else {
  389. scp_l1c_init(scp_region_info->Il1c_con, scp_region_info->Dl1c_con);
  390. dram_start = scp_region_info->ap_dram_start;
  391. dram_size = scp_region_info->ap_dram_size;
  392. if (arch_mmu_map((uint64_t)dram_start, (uint32_t)dram_start,
  393. MMU_MEMORY_TYPE_NORMAL_WRITE_BACK | MMU_MEMORY_AP_P_RW_U_NA, ROUNDUP(dram_size, PAGE_SIZE)) != NO_ERROR)
  394. dprintf(INFO, "scp_crash_dump: map error\n");
  395. scp_dump_size = sizeof(struct MemoryDump) + ROUNDUP(dram_size, 4);
  396. }
  397. tmp_ptr = pDump = malloc(scp_dump_size);
  398. if (tmp_ptr == NULL)
  399. return 0;
  400. /* todo confirm sram is power on? */
  401. exception_header_init(tmp_ptr, SCP_A_ID);
  402. /* init sub header*/
  403. scp_sub_header_init(&(pDump->scp_dump_header));
  404. if (dram_size) {
  405. /* can't flush due to we don't remap l1c yet*/
  406. /* must 4 byte alignment */
  407. memcpy((void *)&(pDump->memory),
  408. (void *)(SCP_SRAM_BASE + CRASH_MEMORY_OFFSET), CRASH_MEMORY_LENGTH);
  409. memcpy((void *)&(pDump->scp_reg_dump),
  410. (void *)(dram_start), dram_size);
  411. scp_reg_copy((void *)(&pDump->scp_reg_dump) + ROUNDUP(dram_size,4 ));
  412. } else {
  413. /* must 4 byte alignment */
  414. memcpy((void *)&(pDump->memory),
  415. (void *)(SCP_SRAM_BASE + CRASH_MEMORY_OFFSET), CRASH_MEMORY_LENGTH);
  416. scp_reg_copy(&(pDump->scp_reg_dump));
  417. }
  418. datasize = scp_zip_compress((void *)tmp_ptr, (void *)crash_buffer, scp_dump_size);
  419. free(tmp_ptr);
  420. return datasize;
  421. }