mrdump_usb.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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) 2017. 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. * The following software/firmware and/or related documentation ("MediaTek Software")
  32. * have been modified by MediaTek Inc. All revisions are subject to any receiver\'s
  33. * applicable license agreements with MediaTek Inc.
  34. */
  35. #include <dev/mrdump.h>
  36. #include <malloc.h>
  37. #include <printf.h>
  38. #include <platform/mt_gpt.h>
  39. #include <string.h>
  40. #include <sys/types.h>
  41. #include "aee.h"
  42. #include "mrdump_private.h"
  43. /*
  44. * USB Connectivity
  45. * Note: for usb transmission
  46. * QMU mode : MAX packet length: 63x1024 = 64512 byte. -> GPD_BUF_SIZE_ALIGN
  47. * ZLP issue: EXSPACE should not be multiple size of 512 byte.
  48. */
  49. #define EXSPACE 64256
  50. #define MAX_RSP_SIZE 64
  51. #define CON_TIMEOUT 5000
  52. /* Flow control */
  53. #define USB_RTS "_RTS"
  54. #define USB_CTS "_CTS"
  55. #define USB_FIN "_FIN"
  56. #define USBDONE "DONE"
  57. /*
  58. * Need to align 64 bytes to meet cache line length
  59. * 1. cmd[MAX_RSP_SIZE]
  60. * 2. data[EXSPACE]
  61. */
  62. struct mrdump_usb_handle {
  63. char cmd[MAX_RSP_SIZE];
  64. uint8_t data[EXSPACE];
  65. unsigned int zipsize;
  66. int idx;
  67. };
  68. /* flow control of usb connection */
  69. static bool usb_data_transfer(struct mrdump_usb_handle *handle, int length)
  70. {
  71. int len;
  72. /* send RTS */
  73. memset(handle->cmd, 0, MAX_RSP_SIZE);
  74. len = snprintf(handle->cmd, MAX_RSP_SIZE, "%s", USB_RTS);
  75. len = usb_write_with_timeout(handle->cmd, strlen(handle->cmd), CON_TIMEOUT);
  76. if (len > 0) {
  77. /* receive CTS */
  78. memset(handle->cmd, 0, MAX_RSP_SIZE);
  79. len = usb_read_with_timeout(handle->cmd, MAX_RSP_SIZE, CON_TIMEOUT);
  80. if ((len == (int)strlen(USB_CTS))&&
  81. (!strncmp(handle->cmd, USB_CTS, strlen(USB_CTS)))) {
  82. /* send DATA */
  83. len = usb_write_with_timeout(handle->data, length, CON_TIMEOUT);
  84. if (len > 0) {
  85. /* get FIN */
  86. memset(handle->cmd, 0, sizeof(handle->cmd));
  87. len = usb_read_with_timeout(handle->cmd, sizeof(handle->cmd), CON_TIMEOUT);
  88. if ((len == (int)strlen(USB_FIN)) &&
  89. (!strncmp(handle->cmd, USB_FIN, strlen(USB_FIN)))) {
  90. return true;
  91. } else {
  92. voprintf_error("%s: failed to get FIN.cmd<%p,%d><%x,%x,%x,%x>\n", __func__,
  93. handle->cmd, len, handle->cmd[0], handle->cmd[1], handle->cmd[2], handle->cmd[3]);
  94. }
  95. } else {
  96. voprintf_error("%s: send DATA error.\n", __func__);
  97. }
  98. } else {
  99. voprintf_error("%s: Not CTS after RTS.cmd<%p,%d><%x,%x,%x,%x>\n", __func__,
  100. handle->cmd, len, handle->cmd[0], handle->cmd[1], handle->cmd[2], handle->cmd[3]);
  101. }
  102. } else {
  103. voprintf_error("%s: send RTS error.\n", __func__);
  104. }
  105. return false;
  106. }
  107. /* store data in pool (EXSPACE) and write when pool is full */
  108. static int do_store_or_write(struct mrdump_usb_handle *handle, uint8_t *buf, uint32_t length)
  109. {
  110. int total;
  111. unsigned int leftspace, mylen, reval;
  112. /* count for leftspace */
  113. total = EXSPACE;
  114. leftspace = total - handle->idx;
  115. /* check length */
  116. if (length > leftspace) {
  117. mylen = leftspace;
  118. reval = length - leftspace;
  119. } else {
  120. mylen = length;
  121. reval = 0;
  122. }
  123. /* store */
  124. while (mylen > 0) {
  125. handle->data[handle->idx] = *buf;
  126. handle->idx++;
  127. buf++;
  128. mylen--;
  129. }
  130. /* write */
  131. if (handle->idx == total) {
  132. if (!usb_data_transfer(handle, handle->idx)) {
  133. voprintf_error("%s: connection failed.(error idx: %d)\n",
  134. __func__, handle->idx);
  135. return -1;
  136. }
  137. handle->idx = 0;
  138. }
  139. return reval;
  140. }
  141. static int usb_write_cb(void *opaque_handle, void *buf, int size)
  142. {
  143. unsigned int len, moves;
  144. int ret = 0;
  145. uint8_t *ptr;
  146. struct mrdump_usb_handle *handle = opaque_handle;
  147. handle->zipsize += size;
  148. /* EOF, write the left Data in handle data buffer... */
  149. if ((buf == NULL) && (size == 0)) {
  150. /* MUST: a delay for the last transmission */
  151. mdelay(10);
  152. if (!usb_data_transfer(handle, handle->idx)) {
  153. voprintf_error("%s: connection failed.(error idx: %d)\n",
  154. __func__, handle->idx);
  155. return -1;
  156. }
  157. /* send "MRDUMP ZLP" */
  158. memset((void *)handle->data, 0, sizeof(handle->data));
  159. size = snprintf((char *)handle->data, sizeof(handle->data), "%s_%s",
  160. MRDUMP_GO_DUMP, USBDONE);
  161. if (0 > usb_write_with_timeout(handle->data, strlen((char *)handle->data),
  162. CON_TIMEOUT)) {
  163. voprintf_error(" USB Dump: Write MRDUMP ZLP failed.\n");
  164. return -1;
  165. }
  166. return 0;
  167. }
  168. /* buf should not be NULL if not EOF */
  169. if (buf == NULL)
  170. return -1;
  171. /* process of Store and write */
  172. len = size;
  173. ptr = (uint8_t *)buf;
  174. while (1) {
  175. ret = do_store_or_write(handle, ptr, len);
  176. if (ret < 0) {
  177. voprintf_error(" USB Dump: store and write failed.\n");
  178. return -1;
  179. } else if (ret == 0) {
  180. break;
  181. } else {
  182. moves = len - ret;
  183. ptr += moves;
  184. len = ret;
  185. }
  186. }
  187. return size;
  188. }
  189. int mrdump_usb_output(const struct mrdump_control_block *mrdump_cb,
  190. const struct kzip_addlist *memlist,
  191. const struct kzip_addlist *memlist_cmm)
  192. {
  193. int i;
  194. struct kzip_addlist expdb_file[2];
  195. char *expdb_filename;
  196. unsigned int expdb_offset, expdb_filesize;
  197. voprintf_info("Output by USB\n");
  198. struct mrdump_usb_handle *handle = memalign(64, sizeof(struct mrdump_usb_handle));
  199. if (handle == NULL) {
  200. voprintf_error("No enough memory.");
  201. return -1;
  202. }
  203. memset(handle, 0, sizeof(struct mrdump_usb_handle));
  204. mdelay(100);
  205. bool ok = true;
  206. mtk_wdt_restart();
  207. struct kzip_file *zf = kzip_open(handle, usb_write_cb);
  208. if (zf != NULL) {
  209. /* add SYS_COREDUMP */
  210. if (!kzip_add_file(zf, memlist, "SYS_COREDUMP"))
  211. ok = false;
  212. if (ok && !kzip_add_file(zf, memlist_cmm, "kernel.cmm"))
  213. ok = false;
  214. if (!ok)
  215. voprintf_error("failed to generate cmm\n");
  216. /* add file on expdb */
  217. for(i = 0; i < IPANIC_NR_SECTIONS; i++) {
  218. if (kedump_get_data_info(i, &expdb_filename, &expdb_offset, &expdb_filesize) == 0) {
  219. expdb_file[0].address = expdb_offset;
  220. expdb_file[0].size = expdb_filesize;
  221. expdb_file[0].type = EXPDB_FILE;
  222. expdb_file[1].address = 0;
  223. expdb_file[1].size = 0;
  224. expdb_file[1].type = MEM_NO_MAP;
  225. if (!kzip_add_file(zf, expdb_file, expdb_filename))
  226. ok = false;
  227. }
  228. }
  229. /* close zipfile */
  230. kzip_close(zf);
  231. usb_write_cb(handle, NULL, 0); /* really write the last part */
  232. zf = NULL;
  233. } else {
  234. ok = false;
  235. }
  236. free(handle);
  237. mtk_wdt_restart();
  238. if (ok) {
  239. mrdump_status_ok("OUTPUT:%s\nMODE:%s\n", "USB DUMP",
  240. mrdump_mode2string(mrdump_cb->crash_record.reboot_mode));
  241. }
  242. return ok ? 0 : -1;
  243. }