mt_scp_excep.c 14 KB

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