adb.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. #include "includes.h"
  2. #include "asm/includes.h"
  3. #include "system/timer.h"
  4. #include "device/ioctl_cmds.h"
  5. #include "usb/host/usb_host.h"
  6. #include "usb_ctrl_transfer.h"
  7. #include "usb_bulk_transfer.h"
  8. #include "device_drive.h"
  9. #include "adb.h"
  10. #include "usb_config.h"
  11. #include "app_config.h"
  12. #if TCFG_ADB_ENABLE
  13. #define LOG_TAG_CONST USB
  14. #define LOG_TAG "[adb]"
  15. #define LOG_ERROR_ENABLE
  16. #define LOG_DEBUG_ENABLE
  17. #define LOG_INFO_ENABLE
  18. /* #define LOG_DUMP_ENABLE */
  19. #define LOG_CLI_ENABLE
  20. #include "debug.h"
  21. #include "adb_rsa_key.c"
  22. struct adb_device_t adb;
  23. struct device adb_device;
  24. void aoa_switch(struct usb_host_device *host_dev);
  25. static int set_adb_power(struct usb_host_device *host_dev, u32 value)
  26. {
  27. return DEV_ERR_NONE;
  28. }
  29. static int get_adb_power(struct usb_host_device *host_dev, u32 value)
  30. {
  31. return DEV_ERR_NONE;
  32. }
  33. static const struct interface_ctrl adb_ops = {
  34. .interface_class = USB_CLASS_ADB,
  35. .set_power = set_adb_power,
  36. .get_power = get_adb_power,
  37. .ioctl = NULL,
  38. };
  39. static const struct usb_interface_info adb_inf = {
  40. .ctrl = (struct interface_ctrl *) &adb_ops,
  41. .dev.adb = &adb,
  42. };
  43. u32 usb_adb_interface_ptp_mtp_parse(struct usb_host_device *host_dev, u8 interface_num, const u8 *pBuf)
  44. {
  45. struct usb_interface_descriptor *interface = (struct usb_interface_descriptor *)pBuf;
  46. pBuf += sizeof(struct usb_interface_descriptor);
  47. int len = 0;
  48. const usb_dev usb_id = host_device2id(host_dev);
  49. for (int i = 0; i < interface->bNumEndpoints; i++) {
  50. struct usb_endpoint_descriptor *endpoint = (struct usb_endpoint_descriptor *)pBuf;
  51. if (endpoint->bDescriptorType == USB_DT_ENDPOINT) {
  52. if (endpoint->bmAttributes == USB_ENDPOINT_XFER_BULK) {
  53. if (endpoint->bEndpointAddress & USB_DIR_IN) {
  54. adb.extr_in = endpoint->bEndpointAddress & 0xf;
  55. } else {
  56. adb.extr_out = endpoint->bEndpointAddress;
  57. }
  58. }
  59. pBuf += USB_DT_ENDPOINT_SIZE;
  60. } else {
  61. return 0;
  62. }
  63. }
  64. printf("%s() %x %x\n", __func__, adb.extr_in, adb.extr_out);
  65. return pBuf - (u8 *)interface ;
  66. }
  67. int usb_adb_parser(struct usb_host_device *host_dev, u8 interface_num, const u8 *pBuf)
  68. {
  69. struct usb_interface_descriptor *interface = (struct usb_interface_descriptor *)pBuf;
  70. pBuf += sizeof(struct usb_interface_descriptor);
  71. int len = 0;
  72. const usb_dev usb_id = host_device2id(host_dev);
  73. adb_device.private_data = host_dev;
  74. host_dev->interface_info[interface_num] = &adb_inf;
  75. for (int endnum = 0; endnum < interface->bNumEndpoints; endnum++) {
  76. struct usb_endpoint_descriptor *end_desc = (struct usb_endpoint_descriptor *)pBuf;
  77. if (end_desc->bDescriptorType != USB_DT_ENDPOINT ||
  78. end_desc->bLength != USB_DT_ENDPOINT_SIZE) {
  79. log_error("ep bDescriptorType = %d bLength = %d", end_desc->bDescriptorType, end_desc->bLength);
  80. return -USB_DT_ENDPOINT;
  81. }
  82. len += USB_DT_ENDPOINT_SIZE;
  83. pBuf += USB_DT_ENDPOINT_SIZE;
  84. if ((end_desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_BULK) {
  85. if (end_desc->bEndpointAddress & USB_DIR_IN) {
  86. adb.host_epin = usb_get_ep_num(usb_id, USB_DIR_IN, USB_ENDPOINT_XFER_BULK);
  87. adb.target_epin = end_desc->bEndpointAddress & 0x0f;
  88. #if HUSB_MODE
  89. adb.rxmaxp = end_desc->wMaxPacketSize;
  90. #endif
  91. log_debug("D(%d)->H(%d)", adb.target_epin, adb.host_epin);
  92. } else {
  93. adb.host_epout = usb_get_ep_num(usb_id, USB_DIR_OUT, USB_ENDPOINT_XFER_BULK);
  94. adb.target_epout = end_desc->bEndpointAddress & 0x0f;
  95. #if HUSB_MODE
  96. adb.txmaxp = end_desc->wMaxPacketSize;
  97. #endif
  98. log_debug("H(%d)->D(%d)", adb.host_epout, adb.target_epout);
  99. }
  100. }
  101. }
  102. u8 *ep_buffer = usb_h_get_ep_buffer(usb_id, adb.host_epin | USB_DIR_IN);
  103. usb_h_ep_config(usb_id, adb.host_epin | USB_DIR_IN, USB_ENDPOINT_XFER_BULK, 0, 0, ep_buffer, 64);
  104. ep_buffer = usb_h_get_ep_buffer(usb_id, adb.host_epout | USB_DIR_OUT);
  105. usb_h_ep_config(usb_id, adb.host_epout | USB_DIR_OUT, USB_ENDPOINT_XFER_BULK, 0, 0, ep_buffer, 64);
  106. return len;
  107. }
  108. static int adb_send_packet(struct amessage *msg, const u8 *data_ptr)
  109. {
  110. if (msg == NULL) {
  111. return usb_bulk_only_send(&adb_device, adb.host_epout, 64, adb.target_epout, NULL, 0);
  112. }
  113. u32 cnt;
  114. u32 count = msg->data_length;
  115. msg->magic = msg->command ^ 0xffffffff;
  116. const u8 *_data_ptr = data_ptr;
  117. msg->data_check = 0;
  118. while (count--) {
  119. msg->data_check += *_data_ptr++;
  120. }
  121. int ret = usb_bulk_only_send(&adb_device, adb.host_epout, 64, adb.target_epout, (u8 *)msg, sizeof(*msg));
  122. if (ret < 0) {
  123. return ret;
  124. }
  125. if (data_ptr != NULL) {
  126. return usb_bulk_only_send(&adb_device, adb.host_epout, 64, adb.target_epout, data_ptr, msg->data_length);
  127. } else {
  128. return 0;
  129. }
  130. }
  131. static int adb_recv(u8 *buffer, u32 len, u32 timeout)
  132. {
  133. return usb_bulk_only_receive(&adb_device, adb.host_epin, 64, adb.target_epin, buffer, len);
  134. }
  135. #define check_usb_status(ret) do{\
  136. if(ret < 0){\
  137. log_info("%s() @ %d %d\n", __func__, __LINE__, ret);\
  138. return ret;\
  139. }\
  140. }while(0);
  141. static u8 adb_signatrue_data_index ;
  142. u32 adb_auth()
  143. {
  144. log_info("%s() %d\n", __func__, __LINE__);
  145. struct amessage msg;
  146. int ret = 0;
  147. u8 *cmd_string;
  148. adb.local_id = 0x58525047;
  149. adb.remote_id = 0;
  150. msg.command = A_CNXN;
  151. msg.arg0 = A_VERSION;
  152. msg.arg1 = 0x00001000;
  153. cmd_string = (u8 *)"host::";
  154. msg.data_length = strlen((const char *)cmd_string) + 1;
  155. ret = adb_send_packet(&msg, cmd_string);
  156. check_usb_status(ret);
  157. memset(&msg, 0, 24);
  158. ret = adb_recv((u8 *)&msg, 24, 5);
  159. check_usb_status(ret);
  160. if (msg.command == A_CNXN) {
  161. if (adb_recv(adb.buffer, msg.data_length, 1 * 100)) {
  162. log_error("auth error 0\n");
  163. return 0;
  164. }
  165. log_info("auth not send rsa pub key\n");
  166. return 0;
  167. } else if (msg.command == A_AUTH) {
  168. } else {
  169. log_error("auth error 1\n");
  170. return 1;
  171. }
  172. ret = adb_recv(adb.buffer, msg.data_length, 1 * 100);
  173. check_usb_status(ret);
  174. msg.command = A_AUTH;
  175. msg.arg0 = ADB_AUTH_SIGNATURE;
  176. msg.data_length = sizeof(adb_signatrue_data[0]);
  177. if (adb_signatrue_data_index > 2) {
  178. adb_signatrue_data_index = 0;
  179. }
  180. adb_signatrue_data_index ++;
  181. cmd_string = (u8 *)&adb_signatrue_data[adb_signatrue_data_index][0];
  182. ret = adb_send_packet(&msg, cmd_string);
  183. check_usb_status(ret);
  184. ret = adb_send_packet(NULL, NULL);//zero packet
  185. check_usb_status(ret);
  186. memset(&msg, 0, 24);
  187. ret = adb_recv((u8 *)&msg, 24, 1 * 100);
  188. check_usb_status(ret);
  189. if (msg.command != A_AUTH) {
  190. log_error("auth error 2\n");
  191. return 1;
  192. }
  193. ret = adb_recv(adb.buffer, msg.data_length, 1 * 100);
  194. check_usb_status(ret);
  195. __RETRY:
  196. msg.command = A_AUTH;
  197. msg.arg0 = ADB_AUTH_RSAPUBLICKEY;
  198. msg.arg1 = 0;
  199. msg.data_length = sizeof(adb_rsa_pub_key);
  200. ret = adb_send_packet(&msg, (u8 *)adb_rsa_pub_key);
  201. check_usb_status(ret);
  202. ret = adb_recv((u8 *)&msg, 24, 30 * 100);
  203. if (ret < 0) {
  204. if (ret == -DEV_ERR_TIMEOUT) {
  205. goto __RETRY;
  206. }
  207. check_usb_status(ret);
  208. }
  209. ret = adb_recv(adb.buffer, msg.data_length, 1 * 100);//最长等待30s,等手机点击确认授权adb
  210. if (ret < 0) {
  211. if (ret == -DEV_ERR_TIMEOUT) {
  212. goto __RETRY;
  213. }
  214. check_usb_status(ret);
  215. }
  216. if (msg.command == A_AUTH) {
  217. goto __RETRY;
  218. }
  219. return 0;
  220. }
  221. u32 adb_shell_login()
  222. {
  223. log_info("%s() %d\n", __func__, __LINE__);
  224. struct amessage msg;
  225. u8 *cmd_string;
  226. /* __AUTH_SUCCESS: */
  227. cmd_string = (u8 *)"shell:";
  228. msg.command = A_OPEN;
  229. msg.arg0 = adb.local_id;
  230. msg.arg1 = 0;
  231. msg.data_length = strlen((char const *)cmd_string) + 1;
  232. int ret = adb_send_packet(&msg, cmd_string);
  233. check_usb_status(ret);
  234. memset(&msg, 0, 24);
  235. ret = adb_recv((u8 *)&msg, 24, 1 * 100);
  236. check_usb_status(ret);
  237. if (msg.command != A_OKAY) {
  238. log_error("A_OKAY error\n");
  239. return 4;
  240. }
  241. ret = adb_recv((u8 *)&msg, 24, 1 * 100);
  242. check_usb_status(ret);
  243. if (msg.command != A_WRTE) {
  244. log_error("A_WRTE error\n");
  245. return 5;
  246. }
  247. adb.remote_id = msg.arg0;
  248. ret = adb_recv(adb.buffer, msg.data_length, 1 * 100);
  249. check_usb_status(ret);
  250. msg.command = A_OKAY;
  251. msg.arg0 = adb.local_id;
  252. msg.arg1 = adb.remote_id;
  253. msg.data_length = 0;
  254. ret = adb_send_packet(&msg, NULL);
  255. check_usb_status(ret);
  256. return 0;
  257. }
  258. static u32 adb_ex_cmd(const char *cmd_string, u8 *echo_buffer, u32 max_len)
  259. {
  260. log_info("%s\n", cmd_string);
  261. int ret;
  262. struct amessage msg;
  263. msg.command = A_WRTE;
  264. msg.arg0 = adb.local_id;
  265. msg.arg1 = adb.remote_id;
  266. msg.data_length = strlen(cmd_string);
  267. ret = adb_send_packet(&msg, (u8 *)cmd_string);
  268. check_usb_status(ret);
  269. memset(&msg, 0, 24);
  270. memset(echo_buffer, 0, max_len);
  271. ret = adb_recv((u8 *)&msg, sizeof(msg), 3 * 100);
  272. check_usb_status(ret);
  273. if (msg.command != A_OKAY) {
  274. return true;
  275. }
  276. u32 offset = 0;
  277. do {
  278. ret = adb_recv((u8 *)&msg, sizeof(msg), 3 * 100);
  279. check_usb_status(ret);
  280. if (msg.command != A_WRTE) {
  281. log_info("command %x\n", msg.command);
  282. return true;
  283. }
  284. if ((offset + msg.data_length) > max_len) {
  285. log_info("%s", echo_buffer);
  286. echo_buffer[offset] = 0;
  287. offset = 0;
  288. }
  289. ret = adb_recv(&echo_buffer[offset], msg.data_length, 3 * 100);
  290. check_usb_status(ret);
  291. offset += msg.data_length;
  292. if (msg.data_length == 0) {
  293. log_info("no data_length\n");
  294. break;
  295. }
  296. if (offset >= max_len) {
  297. }
  298. /* echo_buffer[offset] = '\n'; */
  299. echo_buffer[offset] = 0;
  300. if (echo_buffer[offset - 2] == 0x24 && echo_buffer[offset - 1] == 0x20) {
  301. /* puts("end\n"); */
  302. break;
  303. } else if (echo_buffer[offset - 2] == 0x23 && echo_buffer[offset - 1] == 0x20) {
  304. /* puts("end 1\n"); */
  305. break;
  306. }
  307. msg.command = A_OKAY;
  308. msg.arg0 = adb.local_id;
  309. msg.arg1 = adb.remote_id;
  310. msg.data_length = 0;
  311. ret = adb_send_packet(&msg, NULL);
  312. check_usb_status(ret);
  313. } while (1);
  314. msg.command = A_OKAY;
  315. msg.arg0 = adb.local_id;
  316. msg.arg1 = adb.remote_id;
  317. msg.data_length = 0;
  318. /* puts("exit\n"); */
  319. return adb_send_packet(&msg, NULL);
  320. }
  321. #define APP_ACTIVITY_PATH "com.zh-jieli.gmaeCenter/com.zh-jieli.gameCenter.activity.guide.SplashActivity\n"
  322. #define APP_WEBSITE "http://www.zh-jieli.com\n"
  323. #define APP_BASH_IN_PATH "/sdcard/jilei/active.bash"
  324. #define APP_BASH_OUT_PATH "/data/local/tmp/active.bash"
  325. u32 adb_game_active()
  326. {
  327. log_info("%s() %d\n", __func__, __LINE__);
  328. u32 max_len = adb.max_len;;
  329. u8 *adb_buffer = adb.buffer;
  330. //1,启动app
  331. adb_ex_cmd("am start -n " APP_ACTIVITY_PATH, adb_buffer, max_len);
  332. puts((char *)adb_buffer);
  333. //查找Error字符串,如果找到跳转网页下载app,否则执行adb指令
  334. if (strstr((const char *)adb_buffer, "Error") != NULL) {
  335. adb_ex_cmd("am start -a android.intent.action.VIEW -d " APP_WEBSITE, adb_buffer, max_len);
  336. puts((char *)adb_buffer);
  337. } else {
  338. adb_ex_cmd("dd if=" APP_BASH_IN_PATH " of=" APP_BASH_OUT_PATH "\n", adb_buffer, max_len);
  339. puts((char *)adb_buffer);
  340. adb_ex_cmd("chown shell " APP_BASH_OUT_PATH";chmod 777 "APP_BASH_OUT_PATH "\n", adb_buffer, max_len);
  341. puts((char *)adb_buffer);
  342. adb_ex_cmd("trap \"\" HUP;sh "APP_BASH_OUT_PATH "&\n", adb_buffer, max_len);
  343. puts((char *)adb_buffer);
  344. }
  345. return 0;
  346. }
  347. static void mtp_ptp_open_session(u32 is_mtp)
  348. {
  349. /* usbh_bulk_send_blocking(ADB_HOST_EP, adb_device.extr_out, (u8 *)_open_session, 16); */
  350. /* usbh_request_bulk_blocking(ADB_HOST_EP, adb_device.extr_in, get_data_buffer(), 12, 3); */
  351. /* usbh_bulk_send_blocking(ADB_HOST_EP, adb_device.extr_out, (u8 *)get_device_info, sizeof(get_device_info)); */
  352. /* usbh_request_bulk_blocking(ADB_HOST_EP, adb_device.extr_in, get_data_buffer(), 512, 3); */
  353. /* usbh_request_bulk_blocking(ADB_HOST_EP, adb_device.extr_in, get_data_buffer(), 12, 3); */
  354. }
  355. static void adb_open_session()
  356. {
  357. struct usb_host_device *host_dev = adb_device.private_data;
  358. if (adb.extr_in && adb.extr_out) {
  359. get_ms_extended_compat_id(host_dev, adb.buffer);
  360. get_device_status(host_dev);
  361. get_config_descriptor(host_dev, adb.buffer, 0xff);
  362. /* mtp_ptp_open_session(ac6921_data_buffer[0x12] == 0x4d); */
  363. }
  364. }
  365. u32 adb_process()
  366. {
  367. adb.max_len = 1024;
  368. adb.buffer = malloc(adb.max_len);
  369. os_time_dly(20);
  370. do {
  371. adb_open_session();
  372. if (adb_auth()) {
  373. break;
  374. }
  375. if (adb_shell_login()) {
  376. break;
  377. }
  378. adb_game_active();
  379. log_info("adb active succ");
  380. return 0;
  381. } while (0);
  382. free(adb.buffer);
  383. log_info("adb active error");
  384. return 1;
  385. }
  386. void adb_switch_aoa(u32 id)
  387. {
  388. struct usb_host_device *host_dev = adb_device.private_data;
  389. aoa_switch(host_dev);
  390. /* usb_host_remount(id, 3, 30, 50, 1); */
  391. }
  392. #endif //TCFG_ADB_ENABLE