mt_scp_excep.c 14 KB

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