binder.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  1. /* Copyright 2008 The Android Open Source Project
  2. */
  3. #define LOG_TAG "Binder"
  4. #include <errno.h>
  5. #include <fcntl.h>
  6. #include <inttypes.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <sys/mman.h>
  11. #include <unistd.h>
  12. #include <log/log.h>
  13. #include "binder.h"
  14. #define MAX_BIO_SIZE (1 << 30)
  15. #define TRACE 0
  16. void bio_init_from_txn(struct binder_io *io, struct binder_transaction_data *txn);
  17. #if TRACE
  18. void hexdump(void *_data, size_t len)
  19. {
  20. unsigned char *data = _data;
  21. size_t count;
  22. for (count = 0; count < len; count++) {
  23. if ((count & 15) == 0)
  24. fprintf(stderr,"%04zu:", count);
  25. fprintf(stderr," %02x %c", *data,
  26. (*data < 32) || (*data > 126) ? '.' : *data);
  27. data++;
  28. if ((count & 15) == 15)
  29. fprintf(stderr,"\n");
  30. }
  31. if ((count & 15) != 0)
  32. fprintf(stderr,"\n");
  33. }
  34. void binder_dump_txn(struct binder_transaction_data *txn)
  35. {
  36. struct flat_binder_object *obj;
  37. binder_size_t *offs = (binder_size_t *)(uintptr_t)txn->data.ptr.offsets;
  38. size_t count = txn->offsets_size / sizeof(binder_size_t);
  39. fprintf(stderr," target %016"PRIx64" cookie %016"PRIx64" code %08x flags %08x\n",
  40. (uint64_t)txn->target.ptr, (uint64_t)txn->cookie, txn->code, txn->flags);
  41. fprintf(stderr," pid %8d uid %8d data %"PRIu64" offs %"PRIu64"\n",
  42. txn->sender_pid, txn->sender_euid, (uint64_t)txn->data_size, (uint64_t)txn->offsets_size);
  43. hexdump((void *)(uintptr_t)txn->data.ptr.buffer, txn->data_size);
  44. while (count--) {
  45. obj = (struct flat_binder_object *) (((char*)(uintptr_t)txn->data.ptr.buffer) + *offs++);
  46. fprintf(stderr," - type %08x flags %08x ptr %016"PRIx64" cookie %016"PRIx64"\n",
  47. obj->type, obj->flags, (uint64_t)obj->binder, (uint64_t)obj->cookie);
  48. }
  49. }
  50. #define NAME(n) case n: return #n
  51. const char *cmd_name(uint32_t cmd)
  52. {
  53. switch(cmd) {
  54. NAME(BR_NOOP);
  55. NAME(BR_TRANSACTION_COMPLETE);
  56. NAME(BR_INCREFS);
  57. NAME(BR_ACQUIRE);
  58. NAME(BR_RELEASE);
  59. NAME(BR_DECREFS);
  60. NAME(BR_TRANSACTION);
  61. NAME(BR_REPLY);
  62. NAME(BR_FAILED_REPLY);
  63. NAME(BR_DEAD_REPLY);
  64. NAME(BR_DEAD_BINDER);
  65. default: return "???";
  66. }
  67. }
  68. #else
  69. #define hexdump(a,b) do{} while (0)
  70. #define binder_dump_txn(txn) do{} while (0)
  71. #endif
  72. #define BIO_F_SHARED 0x01 /* needs to be buffer freed */
  73. #define BIO_F_OVERFLOW 0x02 /* ran out of space */
  74. #define BIO_F_IOERROR 0x04
  75. #define BIO_F_MALLOCED 0x08 /* needs to be free()'d */
  76. struct binder_state
  77. {
  78. int fd;
  79. void *mapped;
  80. size_t mapsize;
  81. };
  82. struct binder_state *binder_open(const char* driver, size_t mapsize)
  83. {
  84. struct binder_state *bs;
  85. struct binder_version vers;
  86. bs = malloc(sizeof(*bs));
  87. if (!bs) {
  88. errno = ENOMEM;
  89. return NULL;
  90. }
  91. bs->fd = open(driver, O_RDWR | O_CLOEXEC);
  92. if (bs->fd < 0) {
  93. fprintf(stderr,"binder: cannot open %s (%s)\n",
  94. driver, strerror(errno));
  95. goto fail_open;
  96. }
  97. if ((ioctl(bs->fd, BINDER_VERSION, &vers) == -1) ||
  98. (vers.protocol_version != BINDER_CURRENT_PROTOCOL_VERSION)) {
  99. fprintf(stderr,
  100. "binder: kernel driver version (%d) differs from user space version (%d)\n",
  101. vers.protocol_version, BINDER_CURRENT_PROTOCOL_VERSION);
  102. goto fail_open;
  103. }
  104. bs->mapsize = mapsize;
  105. bs->mapped = mmap(NULL, mapsize, PROT_READ, MAP_PRIVATE, bs->fd, 0);
  106. if (bs->mapped == MAP_FAILED) {
  107. fprintf(stderr,"binder: cannot map device (%s)\n",
  108. strerror(errno));
  109. goto fail_map;
  110. }
  111. return bs;
  112. fail_map:
  113. close(bs->fd);
  114. fail_open:
  115. free(bs);
  116. return NULL;
  117. }
  118. void binder_close(struct binder_state *bs)
  119. {
  120. munmap(bs->mapped, bs->mapsize);
  121. close(bs->fd);
  122. free(bs);
  123. }
  124. int binder_become_context_manager(struct binder_state *bs)
  125. {
  126. struct flat_binder_object obj;
  127. memset(&obj, 0, sizeof(obj));
  128. obj.flags = FLAT_BINDER_FLAG_TXN_SECURITY_CTX;
  129. int result = ioctl(bs->fd, BINDER_SET_CONTEXT_MGR_EXT, &obj);
  130. // fallback to original method
  131. if (result != 0) {
  132. android_errorWriteLog(0x534e4554, "121035042");
  133. result = ioctl(bs->fd, BINDER_SET_CONTEXT_MGR, 0);
  134. }
  135. return result;
  136. }
  137. int binder_write(struct binder_state *bs, void *data, size_t len)
  138. {
  139. struct binder_write_read bwr;
  140. int res;
  141. bwr.write_size = len;
  142. bwr.write_consumed = 0;
  143. bwr.write_buffer = (uintptr_t) data;
  144. bwr.read_size = 0;
  145. bwr.read_consumed = 0;
  146. bwr.read_buffer = 0;
  147. res = ioctl(bs->fd, BINDER_WRITE_READ, &bwr);
  148. if (res < 0) {
  149. fprintf(stderr,"binder_write: ioctl failed (%s)\n",
  150. strerror(errno));
  151. }
  152. return res;
  153. }
  154. void binder_free_buffer(struct binder_state *bs,
  155. binder_uintptr_t buffer_to_free)
  156. {
  157. struct {
  158. uint32_t cmd_free;
  159. binder_uintptr_t buffer;
  160. } __attribute__((packed)) data;
  161. data.cmd_free = BC_FREE_BUFFER;
  162. data.buffer = buffer_to_free;
  163. binder_write(bs, &data, sizeof(data));
  164. }
  165. void binder_send_reply(struct binder_state *bs,
  166. struct binder_io *reply,
  167. binder_uintptr_t buffer_to_free,
  168. int status)
  169. {
  170. struct {
  171. uint32_t cmd_free;
  172. binder_uintptr_t buffer;
  173. uint32_t cmd_reply;
  174. struct binder_transaction_data txn;
  175. } __attribute__((packed)) data;
  176. data.cmd_free = BC_FREE_BUFFER;
  177. data.buffer = buffer_to_free;
  178. data.cmd_reply = BC_REPLY;
  179. data.txn.target.ptr = 0;
  180. data.txn.cookie = 0;
  181. data.txn.code = 0;
  182. if (status) {
  183. data.txn.flags = TF_STATUS_CODE;
  184. data.txn.data_size = sizeof(int);
  185. data.txn.offsets_size = 0;
  186. data.txn.data.ptr.buffer = (uintptr_t)&status;
  187. data.txn.data.ptr.offsets = 0;
  188. } else {
  189. data.txn.flags = 0;
  190. data.txn.data_size = reply->data - reply->data0;
  191. data.txn.offsets_size = ((char*) reply->offs) - ((char*) reply->offs0);
  192. data.txn.data.ptr.buffer = (uintptr_t)reply->data0;
  193. data.txn.data.ptr.offsets = (uintptr_t)reply->offs0;
  194. }
  195. binder_write(bs, &data, sizeof(data));
  196. }
  197. int binder_parse(struct binder_state *bs, struct binder_io *bio,
  198. uintptr_t ptr, size_t size, binder_handler func)
  199. {
  200. int r = 1;
  201. uintptr_t end = ptr + (uintptr_t) size;
  202. while (ptr < end) {
  203. uint32_t cmd = *(uint32_t *) ptr;
  204. ptr += sizeof(uint32_t);
  205. #if TRACE
  206. fprintf(stderr,"%s:\n", cmd_name(cmd));
  207. #endif
  208. switch(cmd) {
  209. case BR_NOOP:
  210. break;
  211. case BR_TRANSACTION_COMPLETE:
  212. break;
  213. case BR_INCREFS:
  214. case BR_ACQUIRE:
  215. case BR_RELEASE:
  216. case BR_DECREFS:
  217. #if TRACE
  218. fprintf(stderr," %p, %p\n", (void *)ptr, (void *)(ptr + sizeof(void *)));
  219. #endif
  220. ptr += sizeof(struct binder_ptr_cookie);
  221. break;
  222. case BR_TRANSACTION_SEC_CTX:
  223. case BR_TRANSACTION: {
  224. struct binder_transaction_data_secctx txn;
  225. if (cmd == BR_TRANSACTION_SEC_CTX) {
  226. if ((end - ptr) < sizeof(struct binder_transaction_data_secctx)) {
  227. ALOGE("parse: txn too small (binder_transaction_data_secctx)!\n");
  228. return -1;
  229. }
  230. memcpy(&txn, (void*) ptr, sizeof(struct binder_transaction_data_secctx));
  231. ptr += sizeof(struct binder_transaction_data_secctx);
  232. } else /* BR_TRANSACTION */ {
  233. if ((end - ptr) < sizeof(struct binder_transaction_data)) {
  234. ALOGE("parse: txn too small (binder_transaction_data)!\n");
  235. return -1;
  236. }
  237. memcpy(&txn.transaction_data, (void*) ptr, sizeof(struct binder_transaction_data));
  238. ptr += sizeof(struct binder_transaction_data);
  239. txn.secctx = 0;
  240. }
  241. binder_dump_txn(&txn.transaction_data);
  242. if (func) {
  243. unsigned rdata[256/4];
  244. struct binder_io msg;
  245. struct binder_io reply;
  246. int res;
  247. bio_init(&reply, rdata, sizeof(rdata), 4);
  248. bio_init_from_txn(&msg, &txn.transaction_data);
  249. res = func(bs, &txn, &msg, &reply);
  250. if (txn.transaction_data.flags & TF_ONE_WAY) {
  251. binder_free_buffer(bs, txn.transaction_data.data.ptr.buffer);
  252. } else {
  253. binder_send_reply(bs, &reply, txn.transaction_data.data.ptr.buffer, res);
  254. }
  255. }
  256. break;
  257. }
  258. case BR_REPLY: {
  259. struct binder_transaction_data *txn = (struct binder_transaction_data *) ptr;
  260. if ((end - ptr) < sizeof(*txn)) {
  261. ALOGE("parse: reply too small!\n");
  262. return -1;
  263. }
  264. binder_dump_txn(txn);
  265. if (bio) {
  266. bio_init_from_txn(bio, txn);
  267. bio = 0;
  268. } else {
  269. /* todo FREE BUFFER */
  270. }
  271. ptr += sizeof(*txn);
  272. r = 0;
  273. break;
  274. }
  275. case BR_DEAD_BINDER: {
  276. struct binder_death *death = (struct binder_death *)(uintptr_t) *(binder_uintptr_t *)ptr;
  277. ptr += sizeof(binder_uintptr_t);
  278. death->func(bs, death->ptr);
  279. break;
  280. }
  281. case BR_FAILED_REPLY:
  282. r = -1;
  283. break;
  284. case BR_DEAD_REPLY:
  285. r = -1;
  286. break;
  287. default:
  288. ALOGE("parse: OOPS %d\n", cmd);
  289. return -1;
  290. }
  291. }
  292. return r;
  293. }
  294. void binder_acquire(struct binder_state *bs, uint32_t target)
  295. {
  296. uint32_t cmd[2];
  297. cmd[0] = BC_ACQUIRE;
  298. cmd[1] = target;
  299. binder_write(bs, cmd, sizeof(cmd));
  300. }
  301. void binder_release(struct binder_state *bs, uint32_t target)
  302. {
  303. uint32_t cmd[2];
  304. cmd[0] = BC_RELEASE;
  305. cmd[1] = target;
  306. binder_write(bs, cmd, sizeof(cmd));
  307. }
  308. void binder_link_to_death(struct binder_state *bs, uint32_t target, struct binder_death *death)
  309. {
  310. struct {
  311. uint32_t cmd;
  312. struct binder_handle_cookie payload;
  313. } __attribute__((packed)) data;
  314. data.cmd = BC_REQUEST_DEATH_NOTIFICATION;
  315. data.payload.handle = target;
  316. data.payload.cookie = (uintptr_t) death;
  317. binder_write(bs, &data, sizeof(data));
  318. }
  319. int binder_call(struct binder_state *bs,
  320. struct binder_io *msg, struct binder_io *reply,
  321. uint32_t target, uint32_t code)
  322. {
  323. int res;
  324. struct binder_write_read bwr;
  325. struct {
  326. uint32_t cmd;
  327. struct binder_transaction_data txn;
  328. } __attribute__((packed)) writebuf;
  329. unsigned readbuf[32];
  330. if (msg->flags & BIO_F_OVERFLOW) {
  331. fprintf(stderr,"binder: txn buffer overflow\n");
  332. goto fail;
  333. }
  334. writebuf.cmd = BC_TRANSACTION;
  335. writebuf.txn.target.handle = target;
  336. writebuf.txn.code = code;
  337. writebuf.txn.flags = 0;
  338. writebuf.txn.data_size = msg->data - msg->data0;
  339. writebuf.txn.offsets_size = ((char*) msg->offs) - ((char*) msg->offs0);
  340. writebuf.txn.data.ptr.buffer = (uintptr_t)msg->data0;
  341. writebuf.txn.data.ptr.offsets = (uintptr_t)msg->offs0;
  342. bwr.write_size = sizeof(writebuf);
  343. bwr.write_consumed = 0;
  344. bwr.write_buffer = (uintptr_t) &writebuf;
  345. hexdump(msg->data0, msg->data - msg->data0);
  346. for (;;) {
  347. bwr.read_size = sizeof(readbuf);
  348. bwr.read_consumed = 0;
  349. bwr.read_buffer = (uintptr_t) readbuf;
  350. res = ioctl(bs->fd, BINDER_WRITE_READ, &bwr);
  351. if (res < 0) {
  352. fprintf(stderr,"binder: ioctl failed (%s)\n", strerror(errno));
  353. goto fail;
  354. }
  355. res = binder_parse(bs, reply, (uintptr_t) readbuf, bwr.read_consumed, 0);
  356. if (res == 0) return 0;
  357. if (res < 0) goto fail;
  358. }
  359. fail:
  360. memset(reply, 0, sizeof(*reply));
  361. reply->flags |= BIO_F_IOERROR;
  362. return -1;
  363. }
  364. void binder_loop(struct binder_state *bs, binder_handler func)
  365. {
  366. int res;
  367. struct binder_write_read bwr;
  368. uint32_t readbuf[32];
  369. bwr.write_size = 0;
  370. bwr.write_consumed = 0;
  371. bwr.write_buffer = 0;
  372. readbuf[0] = BC_ENTER_LOOPER;
  373. binder_write(bs, readbuf, sizeof(uint32_t));
  374. for (;;) {
  375. bwr.read_size = sizeof(readbuf);
  376. bwr.read_consumed = 0;
  377. bwr.read_buffer = (uintptr_t) readbuf;
  378. res = ioctl(bs->fd, BINDER_WRITE_READ, &bwr);
  379. if (res < 0) {
  380. ALOGE("binder_loop: ioctl failed (%s)\n", strerror(errno));
  381. break;
  382. }
  383. res = binder_parse(bs, 0, (uintptr_t) readbuf, bwr.read_consumed, func);
  384. if (res == 0) {
  385. ALOGE("binder_loop: unexpected reply?!\n");
  386. break;
  387. }
  388. if (res < 0) {
  389. ALOGE("binder_loop: io error %d %s\n", res, strerror(errno));
  390. break;
  391. }
  392. }
  393. }
  394. void bio_init_from_txn(struct binder_io *bio, struct binder_transaction_data *txn)
  395. {
  396. bio->data = bio->data0 = (char *)(intptr_t)txn->data.ptr.buffer;
  397. bio->offs = bio->offs0 = (binder_size_t *)(intptr_t)txn->data.ptr.offsets;
  398. bio->data_avail = txn->data_size;
  399. bio->offs_avail = txn->offsets_size / sizeof(size_t);
  400. bio->flags = BIO_F_SHARED;
  401. }
  402. void bio_init(struct binder_io *bio, void *data,
  403. size_t maxdata, size_t maxoffs)
  404. {
  405. size_t n = maxoffs * sizeof(size_t);
  406. if (n > maxdata) {
  407. bio->flags = BIO_F_OVERFLOW;
  408. bio->data_avail = 0;
  409. bio->offs_avail = 0;
  410. return;
  411. }
  412. bio->data = bio->data0 = (char *) data + n;
  413. bio->offs = bio->offs0 = data;
  414. bio->data_avail = maxdata - n;
  415. bio->offs_avail = maxoffs;
  416. bio->flags = 0;
  417. }
  418. static void *bio_alloc(struct binder_io *bio, size_t size)
  419. {
  420. size = (size + 3) & (~3);
  421. if (size > bio->data_avail) {
  422. bio->flags |= BIO_F_OVERFLOW;
  423. return NULL;
  424. } else {
  425. void *ptr = bio->data;
  426. bio->data += size;
  427. bio->data_avail -= size;
  428. return ptr;
  429. }
  430. }
  431. void binder_done(struct binder_state *bs,
  432. __unused struct binder_io *msg,
  433. struct binder_io *reply)
  434. {
  435. struct {
  436. uint32_t cmd;
  437. uintptr_t buffer;
  438. } __attribute__((packed)) data;
  439. if (reply->flags & BIO_F_SHARED) {
  440. data.cmd = BC_FREE_BUFFER;
  441. data.buffer = (uintptr_t) reply->data0;
  442. binder_write(bs, &data, sizeof(data));
  443. reply->flags = 0;
  444. }
  445. }
  446. static struct flat_binder_object *bio_alloc_obj(struct binder_io *bio)
  447. {
  448. struct flat_binder_object *obj;
  449. obj = bio_alloc(bio, sizeof(*obj));
  450. if (obj && bio->offs_avail) {
  451. bio->offs_avail--;
  452. *bio->offs++ = ((char*) obj) - ((char*) bio->data0);
  453. return obj;
  454. }
  455. bio->flags |= BIO_F_OVERFLOW;
  456. return NULL;
  457. }
  458. void bio_put_uint32(struct binder_io *bio, uint32_t n)
  459. {
  460. uint32_t *ptr = bio_alloc(bio, sizeof(n));
  461. if (ptr)
  462. *ptr = n;
  463. }
  464. void bio_put_obj(struct binder_io *bio, void *ptr)
  465. {
  466. struct flat_binder_object *obj;
  467. obj = bio_alloc_obj(bio);
  468. if (!obj)
  469. return;
  470. obj->flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
  471. obj->hdr.type = BINDER_TYPE_BINDER;
  472. obj->binder = (uintptr_t)ptr;
  473. obj->cookie = 0;
  474. }
  475. void bio_put_ref(struct binder_io *bio, uint32_t handle)
  476. {
  477. struct flat_binder_object *obj;
  478. if (handle)
  479. obj = bio_alloc_obj(bio);
  480. else
  481. obj = bio_alloc(bio, sizeof(*obj));
  482. if (!obj)
  483. return;
  484. obj->flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
  485. obj->hdr.type = BINDER_TYPE_HANDLE;
  486. obj->handle = handle;
  487. obj->cookie = 0;
  488. }
  489. void bio_put_string16(struct binder_io *bio, const uint16_t *str)
  490. {
  491. size_t len;
  492. uint16_t *ptr;
  493. if (!str) {
  494. bio_put_uint32(bio, 0xffffffff);
  495. return;
  496. }
  497. len = 0;
  498. while (str[len]) len++;
  499. if (len >= (MAX_BIO_SIZE / sizeof(uint16_t))) {
  500. bio_put_uint32(bio, 0xffffffff);
  501. return;
  502. }
  503. /* Note: The payload will carry 32bit size instead of size_t */
  504. bio_put_uint32(bio, (uint32_t) len);
  505. len = (len + 1) * sizeof(uint16_t);
  506. ptr = bio_alloc(bio, len);
  507. if (ptr)
  508. memcpy(ptr, str, len);
  509. }
  510. void bio_put_string16_x(struct binder_io *bio, const char *_str)
  511. {
  512. unsigned char *str = (unsigned char*) _str;
  513. size_t len;
  514. uint16_t *ptr;
  515. if (!str) {
  516. bio_put_uint32(bio, 0xffffffff);
  517. return;
  518. }
  519. len = strlen(_str);
  520. if (len >= (MAX_BIO_SIZE / sizeof(uint16_t))) {
  521. bio_put_uint32(bio, 0xffffffff);
  522. return;
  523. }
  524. /* Note: The payload will carry 32bit size instead of size_t */
  525. bio_put_uint32(bio, len);
  526. ptr = bio_alloc(bio, (len + 1) * sizeof(uint16_t));
  527. if (!ptr)
  528. return;
  529. while (*str)
  530. *ptr++ = *str++;
  531. *ptr++ = 0;
  532. }
  533. static void *bio_get(struct binder_io *bio, size_t size)
  534. {
  535. size = (size + 3) & (~3);
  536. if (bio->data_avail < size){
  537. bio->data_avail = 0;
  538. bio->flags |= BIO_F_OVERFLOW;
  539. return NULL;
  540. } else {
  541. void *ptr = bio->data;
  542. bio->data += size;
  543. bio->data_avail -= size;
  544. return ptr;
  545. }
  546. }
  547. uint32_t bio_get_uint32(struct binder_io *bio)
  548. {
  549. uint32_t *ptr = bio_get(bio, sizeof(*ptr));
  550. return ptr ? *ptr : 0;
  551. }
  552. uint16_t *bio_get_string16(struct binder_io *bio, size_t *sz)
  553. {
  554. size_t len;
  555. /* Note: The payload will carry 32bit size instead of size_t */
  556. len = (size_t) bio_get_uint32(bio);
  557. if (sz)
  558. *sz = len;
  559. return bio_get(bio, (len + 1) * sizeof(uint16_t));
  560. }
  561. static struct flat_binder_object *_bio_get_obj(struct binder_io *bio)
  562. {
  563. size_t n;
  564. size_t off = bio->data - bio->data0;
  565. /* TODO: be smarter about this? */
  566. for (n = 0; n < bio->offs_avail; n++) {
  567. if (bio->offs[n] == off)
  568. return bio_get(bio, sizeof(struct flat_binder_object));
  569. }
  570. bio->data_avail = 0;
  571. bio->flags |= BIO_F_OVERFLOW;
  572. return NULL;
  573. }
  574. uint32_t bio_get_ref(struct binder_io *bio)
  575. {
  576. struct flat_binder_object *obj;
  577. obj = _bio_get_obj(bio);
  578. if (!obj)
  579. return 0;
  580. if (obj->hdr.type == BINDER_TYPE_HANDLE)
  581. return obj->handle;
  582. return 0;
  583. }