2012-10-24 17:45:23 +00:00
|
|
|
/* Copyright 2008 The Android Open Source Project
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
|
|
|
|
#include "binder.h"
|
|
|
|
|
|
|
|
#define MAX_BIO_SIZE (1 << 30)
|
|
|
|
|
|
|
|
#define TRACE 0
|
|
|
|
|
|
|
|
#define LOG_TAG "Binder"
|
|
|
|
#include <cutils/log.h>
|
|
|
|
|
2014-01-10 13:56:27 +00:00
|
|
|
void bio_init_from_txn(struct binder_io *io, struct binder_transaction_data *txn);
|
2012-10-24 17:45:23 +00:00
|
|
|
|
|
|
|
#if TRACE
|
2014-01-10 15:48:29 +00:00
|
|
|
void hexdump(void *_data, size_t len)
|
2012-10-24 17:45:23 +00:00
|
|
|
{
|
|
|
|
unsigned char *data = _data;
|
2014-01-10 15:48:29 +00:00
|
|
|
size_t count;
|
2012-10-24 17:45:23 +00:00
|
|
|
|
|
|
|
for (count = 0; count < len; count++) {
|
|
|
|
if ((count & 15) == 0)
|
2014-01-10 15:48:29 +00:00
|
|
|
fprintf(stderr,"%04zu:", count);
|
2012-10-24 17:45:23 +00:00
|
|
|
fprintf(stderr," %02x %c", *data,
|
|
|
|
(*data < 32) || (*data > 126) ? '.' : *data);
|
|
|
|
data++;
|
|
|
|
if ((count & 15) == 15)
|
|
|
|
fprintf(stderr,"\n");
|
|
|
|
}
|
|
|
|
if ((count & 15) != 0)
|
|
|
|
fprintf(stderr,"\n");
|
|
|
|
}
|
|
|
|
|
2014-01-10 13:56:27 +00:00
|
|
|
void binder_dump_txn(struct binder_transaction_data *txn)
|
2012-10-24 17:45:23 +00:00
|
|
|
{
|
2014-01-10 13:56:27 +00:00
|
|
|
struct flat_binder_object *obj;
|
2014-01-10 15:48:29 +00:00
|
|
|
size_t *offs = txn->data.ptr.offsets;
|
|
|
|
size_t count = txn->offsets_size / sizeof(size_t);
|
2012-10-24 17:45:23 +00:00
|
|
|
|
|
|
|
fprintf(stderr," target %p cookie %p code %08x flags %08x\n",
|
2014-01-10 13:56:27 +00:00
|
|
|
txn->target.ptr, txn->cookie, txn->code, txn->flags);
|
|
|
|
fprintf(stderr," pid %8d uid %8d data %zu offs %zu\n",
|
|
|
|
txn->sender_pid, txn->sender_euid, txn->data_size, txn->offsets_size);
|
|
|
|
hexdump(txn->data.ptr.buffer, txn->data_size);
|
2012-10-24 17:45:23 +00:00
|
|
|
while (count--) {
|
2014-01-10 13:56:27 +00:00
|
|
|
obj = (struct flat_binder_object *) (((char*) txn->data.ptr.buffer) + *offs++);
|
2012-10-24 17:45:23 +00:00
|
|
|
fprintf(stderr," - type %08x flags %08x ptr %p cookie %p\n",
|
2014-01-10 13:56:27 +00:00
|
|
|
obj->type, obj->flags, obj->binder, obj->cookie);
|
2012-10-24 17:45:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#define NAME(n) case n: return #n
|
|
|
|
const char *cmd_name(uint32_t cmd)
|
|
|
|
{
|
|
|
|
switch(cmd) {
|
|
|
|
NAME(BR_NOOP);
|
|
|
|
NAME(BR_TRANSACTION_COMPLETE);
|
|
|
|
NAME(BR_INCREFS);
|
|
|
|
NAME(BR_ACQUIRE);
|
|
|
|
NAME(BR_RELEASE);
|
|
|
|
NAME(BR_DECREFS);
|
|
|
|
NAME(BR_TRANSACTION);
|
|
|
|
NAME(BR_REPLY);
|
|
|
|
NAME(BR_FAILED_REPLY);
|
|
|
|
NAME(BR_DEAD_REPLY);
|
|
|
|
NAME(BR_DEAD_BINDER);
|
|
|
|
default: return "???";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#define hexdump(a,b) do{} while (0)
|
|
|
|
#define binder_dump_txn(txn) do{} while (0)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define BIO_F_SHARED 0x01 /* needs to be buffer freed */
|
|
|
|
#define BIO_F_OVERFLOW 0x02 /* ran out of space */
|
|
|
|
#define BIO_F_IOERROR 0x04
|
|
|
|
#define BIO_F_MALLOCED 0x08 /* needs to be free()'d */
|
|
|
|
|
|
|
|
struct binder_state
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
void *mapped;
|
2014-01-10 15:48:29 +00:00
|
|
|
size_t mapsize;
|
2012-10-24 17:45:23 +00:00
|
|
|
};
|
|
|
|
|
2014-01-10 15:48:29 +00:00
|
|
|
struct binder_state *binder_open(size_t mapsize)
|
2012-10-24 17:45:23 +00:00
|
|
|
{
|
|
|
|
struct binder_state *bs;
|
2014-01-30 15:16:45 +00:00
|
|
|
struct binder_version vers;
|
2012-10-24 17:45:23 +00:00
|
|
|
|
|
|
|
bs = malloc(sizeof(*bs));
|
|
|
|
if (!bs) {
|
|
|
|
errno = ENOMEM;
|
2014-01-10 15:48:29 +00:00
|
|
|
return NULL;
|
2012-10-24 17:45:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bs->fd = open("/dev/binder", O_RDWR);
|
|
|
|
if (bs->fd < 0) {
|
|
|
|
fprintf(stderr,"binder: cannot open device (%s)\n",
|
|
|
|
strerror(errno));
|
|
|
|
goto fail_open;
|
|
|
|
}
|
|
|
|
|
2014-01-30 15:16:45 +00:00
|
|
|
if ((ioctl(bs->fd, BINDER_VERSION, &vers) == -1) ||
|
|
|
|
(vers.protocol_version != BINDER_CURRENT_PROTOCOL_VERSION)) {
|
|
|
|
fprintf(stderr, "binder: driver version differs from user space\n");
|
|
|
|
goto fail_open;
|
|
|
|
}
|
|
|
|
|
2012-10-24 17:45:23 +00:00
|
|
|
bs->mapsize = mapsize;
|
|
|
|
bs->mapped = mmap(NULL, mapsize, PROT_READ, MAP_PRIVATE, bs->fd, 0);
|
|
|
|
if (bs->mapped == MAP_FAILED) {
|
|
|
|
fprintf(stderr,"binder: cannot map device (%s)\n",
|
|
|
|
strerror(errno));
|
|
|
|
goto fail_map;
|
|
|
|
}
|
|
|
|
|
|
|
|
return bs;
|
|
|
|
|
|
|
|
fail_map:
|
|
|
|
close(bs->fd);
|
|
|
|
fail_open:
|
|
|
|
free(bs);
|
2014-01-10 15:48:29 +00:00
|
|
|
return NULL;
|
2012-10-24 17:45:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void binder_close(struct binder_state *bs)
|
|
|
|
{
|
|
|
|
munmap(bs->mapped, bs->mapsize);
|
|
|
|
close(bs->fd);
|
|
|
|
free(bs);
|
|
|
|
}
|
|
|
|
|
|
|
|
int binder_become_context_manager(struct binder_state *bs)
|
|
|
|
{
|
|
|
|
return ioctl(bs->fd, BINDER_SET_CONTEXT_MGR, 0);
|
|
|
|
}
|
|
|
|
|
2014-01-10 15:48:29 +00:00
|
|
|
int binder_write(struct binder_state *bs, void *data, size_t len)
|
2012-10-24 17:45:23 +00:00
|
|
|
{
|
|
|
|
struct binder_write_read bwr;
|
|
|
|
int res;
|
|
|
|
bwr.write_size = len;
|
|
|
|
bwr.write_consumed = 0;
|
|
|
|
bwr.write_buffer = (unsigned) data;
|
|
|
|
bwr.read_size = 0;
|
|
|
|
bwr.read_consumed = 0;
|
|
|
|
bwr.read_buffer = 0;
|
|
|
|
res = ioctl(bs->fd, BINDER_WRITE_READ, &bwr);
|
|
|
|
if (res < 0) {
|
|
|
|
fprintf(stderr,"binder_write: ioctl failed (%s)\n",
|
|
|
|
strerror(errno));
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
void binder_send_reply(struct binder_state *bs,
|
|
|
|
struct binder_io *reply,
|
2014-01-10 15:48:29 +00:00
|
|
|
const void *buffer_to_free,
|
2012-10-24 17:45:23 +00:00
|
|
|
int status)
|
|
|
|
{
|
|
|
|
struct {
|
|
|
|
uint32_t cmd_free;
|
|
|
|
void *buffer;
|
|
|
|
uint32_t cmd_reply;
|
2014-01-10 13:56:27 +00:00
|
|
|
struct binder_transaction_data txn;
|
2012-10-24 17:45:23 +00:00
|
|
|
} __attribute__((packed)) data;
|
|
|
|
|
|
|
|
data.cmd_free = BC_FREE_BUFFER;
|
|
|
|
data.buffer = buffer_to_free;
|
|
|
|
data.cmd_reply = BC_REPLY;
|
2014-01-10 13:56:27 +00:00
|
|
|
data.txn.target.ptr = 0;
|
2012-10-24 17:45:23 +00:00
|
|
|
data.txn.cookie = 0;
|
|
|
|
data.txn.code = 0;
|
|
|
|
if (status) {
|
|
|
|
data.txn.flags = TF_STATUS_CODE;
|
|
|
|
data.txn.data_size = sizeof(int);
|
2014-01-10 13:56:27 +00:00
|
|
|
data.txn.offsets_size = 0;
|
|
|
|
data.txn.data.ptr.buffer = &status;
|
|
|
|
data.txn.data.ptr.offsets = 0;
|
2012-10-24 17:45:23 +00:00
|
|
|
} else {
|
|
|
|
data.txn.flags = 0;
|
|
|
|
data.txn.data_size = reply->data - reply->data0;
|
2014-01-10 13:56:27 +00:00
|
|
|
data.txn.offsets_size = ((char*) reply->offs) - ((char*) reply->offs0);
|
|
|
|
data.txn.data.ptr.buffer = reply->data0;
|
|
|
|
data.txn.data.ptr.offsets = reply->offs0;
|
2012-10-24 17:45:23 +00:00
|
|
|
}
|
|
|
|
binder_write(bs, &data, sizeof(data));
|
|
|
|
}
|
|
|
|
|
|
|
|
int binder_parse(struct binder_state *bs, struct binder_io *bio,
|
|
|
|
uint32_t *ptr, uint32_t size, binder_handler func)
|
|
|
|
{
|
|
|
|
int r = 1;
|
|
|
|
uint32_t *end = ptr + (size / 4);
|
|
|
|
|
|
|
|
while (ptr < end) {
|
|
|
|
uint32_t cmd = *ptr++;
|
|
|
|
#if TRACE
|
|
|
|
fprintf(stderr,"%s:\n", cmd_name(cmd));
|
|
|
|
#endif
|
|
|
|
switch(cmd) {
|
|
|
|
case BR_NOOP:
|
|
|
|
break;
|
|
|
|
case BR_TRANSACTION_COMPLETE:
|
|
|
|
break;
|
|
|
|
case BR_INCREFS:
|
|
|
|
case BR_ACQUIRE:
|
|
|
|
case BR_RELEASE:
|
|
|
|
case BR_DECREFS:
|
|
|
|
#if TRACE
|
|
|
|
fprintf(stderr," %08x %08x\n", ptr[0], ptr[1]);
|
|
|
|
#endif
|
|
|
|
ptr += 2;
|
|
|
|
break;
|
|
|
|
case BR_TRANSACTION: {
|
2014-01-10 13:56:27 +00:00
|
|
|
struct binder_transaction_data *txn = (struct binder_transaction_data *) ptr;
|
|
|
|
if ((end - ptr) * sizeof(uint32_t) < sizeof(*txn)) {
|
2012-10-24 17:45:23 +00:00
|
|
|
ALOGE("parse: txn too small!\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
binder_dump_txn(txn);
|
|
|
|
if (func) {
|
|
|
|
unsigned rdata[256/4];
|
|
|
|
struct binder_io msg;
|
|
|
|
struct binder_io reply;
|
|
|
|
int res;
|
|
|
|
|
|
|
|
bio_init(&reply, rdata, sizeof(rdata), 4);
|
|
|
|
bio_init_from_txn(&msg, txn);
|
|
|
|
res = func(bs, txn, &msg, &reply);
|
2014-01-10 13:56:27 +00:00
|
|
|
binder_send_reply(bs, &reply, txn->data.ptr.buffer, res);
|
2012-10-24 17:45:23 +00:00
|
|
|
}
|
|
|
|
ptr += sizeof(*txn) / sizeof(uint32_t);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case BR_REPLY: {
|
2014-01-10 13:56:27 +00:00
|
|
|
struct binder_transaction_data *txn = (struct binder_transaction_data *) ptr;
|
|
|
|
if ((end - ptr) * sizeof(uint32_t) < sizeof(*txn)) {
|
2012-10-24 17:45:23 +00:00
|
|
|
ALOGE("parse: reply too small!\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
binder_dump_txn(txn);
|
|
|
|
if (bio) {
|
|
|
|
bio_init_from_txn(bio, txn);
|
|
|
|
bio = 0;
|
|
|
|
} else {
|
|
|
|
/* todo FREE BUFFER */
|
|
|
|
}
|
|
|
|
ptr += (sizeof(*txn) / sizeof(uint32_t));
|
|
|
|
r = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case BR_DEAD_BINDER: {
|
|
|
|
struct binder_death *death = (void*) *ptr++;
|
|
|
|
death->func(bs, death->ptr);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case BR_FAILED_REPLY:
|
|
|
|
r = -1;
|
|
|
|
break;
|
|
|
|
case BR_DEAD_REPLY:
|
|
|
|
r = -1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ALOGE("parse: OOPS %d\n", cmd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
void binder_acquire(struct binder_state *bs, void *ptr)
|
|
|
|
{
|
|
|
|
uint32_t cmd[2];
|
|
|
|
cmd[0] = BC_ACQUIRE;
|
|
|
|
cmd[1] = (uint32_t) ptr;
|
|
|
|
binder_write(bs, cmd, sizeof(cmd));
|
|
|
|
}
|
|
|
|
|
|
|
|
void binder_release(struct binder_state *bs, void *ptr)
|
|
|
|
{
|
|
|
|
uint32_t cmd[2];
|
|
|
|
cmd[0] = BC_RELEASE;
|
|
|
|
cmd[1] = (uint32_t) ptr;
|
|
|
|
binder_write(bs, cmd, sizeof(cmd));
|
|
|
|
}
|
|
|
|
|
|
|
|
void binder_link_to_death(struct binder_state *bs, void *ptr, struct binder_death *death)
|
|
|
|
{
|
|
|
|
uint32_t cmd[3];
|
|
|
|
cmd[0] = BC_REQUEST_DEATH_NOTIFICATION;
|
|
|
|
cmd[1] = (uint32_t) ptr;
|
|
|
|
cmd[2] = (uint32_t) death;
|
|
|
|
binder_write(bs, cmd, sizeof(cmd));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int binder_call(struct binder_state *bs,
|
|
|
|
struct binder_io *msg, struct binder_io *reply,
|
|
|
|
void *target, uint32_t code)
|
|
|
|
{
|
|
|
|
int res;
|
|
|
|
struct binder_write_read bwr;
|
|
|
|
struct {
|
|
|
|
uint32_t cmd;
|
2014-01-10 13:56:27 +00:00
|
|
|
struct binder_transaction_data txn;
|
2012-10-24 17:45:23 +00:00
|
|
|
} writebuf;
|
|
|
|
unsigned readbuf[32];
|
|
|
|
|
|
|
|
if (msg->flags & BIO_F_OVERFLOW) {
|
|
|
|
fprintf(stderr,"binder: txn buffer overflow\n");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
writebuf.cmd = BC_TRANSACTION;
|
2014-01-10 13:56:27 +00:00
|
|
|
writebuf.txn.target.handle = target;
|
2012-10-24 17:45:23 +00:00
|
|
|
writebuf.txn.code = code;
|
|
|
|
writebuf.txn.flags = 0;
|
|
|
|
writebuf.txn.data_size = msg->data - msg->data0;
|
2014-01-10 13:56:27 +00:00
|
|
|
writebuf.txn.offsets_size = ((char*) msg->offs) - ((char*) msg->offs0);
|
|
|
|
writebuf.txn.data.ptr.buffer = msg->data0;
|
|
|
|
writebuf.txn.data.ptr.offsets = msg->offs0;
|
2012-10-24 17:45:23 +00:00
|
|
|
|
|
|
|
bwr.write_size = sizeof(writebuf);
|
|
|
|
bwr.write_consumed = 0;
|
|
|
|
bwr.write_buffer = (unsigned) &writebuf;
|
2014-01-10 15:48:29 +00:00
|
|
|
|
2012-10-24 17:45:23 +00:00
|
|
|
hexdump(msg->data0, msg->data - msg->data0);
|
|
|
|
for (;;) {
|
|
|
|
bwr.read_size = sizeof(readbuf);
|
|
|
|
bwr.read_consumed = 0;
|
|
|
|
bwr.read_buffer = (unsigned) readbuf;
|
|
|
|
|
|
|
|
res = ioctl(bs->fd, BINDER_WRITE_READ, &bwr);
|
|
|
|
|
|
|
|
if (res < 0) {
|
|
|
|
fprintf(stderr,"binder: ioctl failed (%s)\n", strerror(errno));
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
res = binder_parse(bs, reply, readbuf, bwr.read_consumed, 0);
|
|
|
|
if (res == 0) return 0;
|
|
|
|
if (res < 0) goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
fail:
|
|
|
|
memset(reply, 0, sizeof(*reply));
|
|
|
|
reply->flags |= BIO_F_IOERROR;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void binder_loop(struct binder_state *bs, binder_handler func)
|
|
|
|
{
|
|
|
|
int res;
|
|
|
|
struct binder_write_read bwr;
|
|
|
|
unsigned readbuf[32];
|
|
|
|
|
|
|
|
bwr.write_size = 0;
|
|
|
|
bwr.write_consumed = 0;
|
|
|
|
bwr.write_buffer = 0;
|
2014-01-10 15:48:29 +00:00
|
|
|
|
2012-10-24 17:45:23 +00:00
|
|
|
readbuf[0] = BC_ENTER_LOOPER;
|
|
|
|
binder_write(bs, readbuf, sizeof(unsigned));
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
bwr.read_size = sizeof(readbuf);
|
|
|
|
bwr.read_consumed = 0;
|
|
|
|
bwr.read_buffer = (unsigned) readbuf;
|
|
|
|
|
|
|
|
res = ioctl(bs->fd, BINDER_WRITE_READ, &bwr);
|
|
|
|
|
|
|
|
if (res < 0) {
|
|
|
|
ALOGE("binder_loop: ioctl failed (%s)\n", strerror(errno));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
res = binder_parse(bs, 0, readbuf, bwr.read_consumed, func);
|
|
|
|
if (res == 0) {
|
|
|
|
ALOGE("binder_loop: unexpected reply?!\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (res < 0) {
|
|
|
|
ALOGE("binder_loop: io error %d %s\n", res, strerror(errno));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-10 13:56:27 +00:00
|
|
|
void bio_init_from_txn(struct binder_io *bio, struct binder_transaction_data *txn)
|
2012-10-24 17:45:23 +00:00
|
|
|
{
|
2014-01-10 13:56:27 +00:00
|
|
|
bio->data = bio->data0 = txn->data.ptr.buffer;
|
|
|
|
bio->offs = bio->offs0 = txn->data.ptr.offsets;
|
2012-10-24 17:45:23 +00:00
|
|
|
bio->data_avail = txn->data_size;
|
2014-01-10 13:56:27 +00:00
|
|
|
bio->offs_avail = txn->offsets_size / 4;
|
2012-10-24 17:45:23 +00:00
|
|
|
bio->flags = BIO_F_SHARED;
|
|
|
|
}
|
|
|
|
|
|
|
|
void bio_init(struct binder_io *bio, void *data,
|
2014-01-10 15:48:29 +00:00
|
|
|
size_t maxdata, size_t maxoffs)
|
2012-10-24 17:45:23 +00:00
|
|
|
{
|
2014-01-10 15:48:29 +00:00
|
|
|
size_t n = maxoffs * sizeof(size_t);
|
2012-10-24 17:45:23 +00:00
|
|
|
|
|
|
|
if (n > maxdata) {
|
|
|
|
bio->flags = BIO_F_OVERFLOW;
|
|
|
|
bio->data_avail = 0;
|
|
|
|
bio->offs_avail = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bio->data = bio->data0 = (char *) data + n;
|
|
|
|
bio->offs = bio->offs0 = data;
|
|
|
|
bio->data_avail = maxdata - n;
|
|
|
|
bio->offs_avail = maxoffs;
|
|
|
|
bio->flags = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *bio_alloc(struct binder_io *bio, uint32_t size)
|
|
|
|
{
|
|
|
|
size = (size + 3) & (~3);
|
|
|
|
if (size > bio->data_avail) {
|
|
|
|
bio->flags |= BIO_F_OVERFLOW;
|
2014-01-10 15:48:29 +00:00
|
|
|
return NULL;
|
2012-10-24 17:45:23 +00:00
|
|
|
} else {
|
|
|
|
void *ptr = bio->data;
|
|
|
|
bio->data += size;
|
|
|
|
bio->data_avail -= size;
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void binder_done(struct binder_state *bs,
|
|
|
|
struct binder_io *msg,
|
|
|
|
struct binder_io *reply)
|
|
|
|
{
|
|
|
|
if (reply->flags & BIO_F_SHARED) {
|
|
|
|
uint32_t cmd[2];
|
|
|
|
cmd[0] = BC_FREE_BUFFER;
|
|
|
|
cmd[1] = (uint32_t) reply->data0;
|
|
|
|
binder_write(bs, cmd, sizeof(cmd));
|
|
|
|
reply->flags = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-10 13:56:27 +00:00
|
|
|
static struct flat_binder_object *bio_alloc_obj(struct binder_io *bio)
|
2012-10-24 17:45:23 +00:00
|
|
|
{
|
2014-01-10 13:56:27 +00:00
|
|
|
struct flat_binder_object *obj;
|
2012-10-24 17:45:23 +00:00
|
|
|
|
|
|
|
obj = bio_alloc(bio, sizeof(*obj));
|
2014-01-10 15:48:29 +00:00
|
|
|
|
2012-10-24 17:45:23 +00:00
|
|
|
if (obj && bio->offs_avail) {
|
|
|
|
bio->offs_avail--;
|
|
|
|
*bio->offs++ = ((char*) obj) - ((char*) bio->data0);
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
bio->flags |= BIO_F_OVERFLOW;
|
2014-01-10 15:48:29 +00:00
|
|
|
return NULL;
|
2012-10-24 17:45:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void bio_put_uint32(struct binder_io *bio, uint32_t n)
|
|
|
|
{
|
|
|
|
uint32_t *ptr = bio_alloc(bio, sizeof(n));
|
|
|
|
if (ptr)
|
|
|
|
*ptr = n;
|
|
|
|
}
|
|
|
|
|
|
|
|
void bio_put_obj(struct binder_io *bio, void *ptr)
|
|
|
|
{
|
2014-01-10 13:56:27 +00:00
|
|
|
struct flat_binder_object *obj;
|
2012-10-24 17:45:23 +00:00
|
|
|
|
|
|
|
obj = bio_alloc_obj(bio);
|
|
|
|
if (!obj)
|
|
|
|
return;
|
|
|
|
|
|
|
|
obj->flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
|
|
|
|
obj->type = BINDER_TYPE_BINDER;
|
2014-01-10 13:56:27 +00:00
|
|
|
obj->binder = ptr;
|
2012-10-24 17:45:23 +00:00
|
|
|
obj->cookie = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void bio_put_ref(struct binder_io *bio, void *ptr)
|
|
|
|
{
|
2014-01-10 13:56:27 +00:00
|
|
|
struct flat_binder_object *obj;
|
2012-10-24 17:45:23 +00:00
|
|
|
|
|
|
|
if (ptr)
|
|
|
|
obj = bio_alloc_obj(bio);
|
|
|
|
else
|
|
|
|
obj = bio_alloc(bio, sizeof(*obj));
|
|
|
|
|
|
|
|
if (!obj)
|
|
|
|
return;
|
|
|
|
|
|
|
|
obj->flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
|
|
|
|
obj->type = BINDER_TYPE_HANDLE;
|
2014-01-10 13:56:27 +00:00
|
|
|
obj->handle = ptr;
|
2012-10-24 17:45:23 +00:00
|
|
|
obj->cookie = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void bio_put_string16(struct binder_io *bio, const uint16_t *str)
|
|
|
|
{
|
2014-01-10 15:48:29 +00:00
|
|
|
size_t len;
|
2012-10-24 17:45:23 +00:00
|
|
|
uint16_t *ptr;
|
|
|
|
|
|
|
|
if (!str) {
|
|
|
|
bio_put_uint32(bio, 0xffffffff);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
len = 0;
|
|
|
|
while (str[len]) len++;
|
|
|
|
|
|
|
|
if (len >= (MAX_BIO_SIZE / sizeof(uint16_t))) {
|
|
|
|
bio_put_uint32(bio, 0xffffffff);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-01-10 15:48:29 +00:00
|
|
|
/* Note: The payload will carry 32bit size instead of size_t */
|
|
|
|
bio_put_uint32(bio, (uint32_t) len);
|
2012-10-24 17:45:23 +00:00
|
|
|
len = (len + 1) * sizeof(uint16_t);
|
|
|
|
ptr = bio_alloc(bio, len);
|
|
|
|
if (ptr)
|
|
|
|
memcpy(ptr, str, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
void bio_put_string16_x(struct binder_io *bio, const char *_str)
|
|
|
|
{
|
|
|
|
unsigned char *str = (unsigned char*) _str;
|
2014-01-10 15:48:29 +00:00
|
|
|
size_t len;
|
2012-10-24 17:45:23 +00:00
|
|
|
uint16_t *ptr;
|
|
|
|
|
|
|
|
if (!str) {
|
|
|
|
bio_put_uint32(bio, 0xffffffff);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
len = strlen(_str);
|
|
|
|
|
|
|
|
if (len >= (MAX_BIO_SIZE / sizeof(uint16_t))) {
|
|
|
|
bio_put_uint32(bio, 0xffffffff);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-01-10 15:48:29 +00:00
|
|
|
/* Note: The payload will carry 32bit size instead of size_t */
|
2012-10-24 17:45:23 +00:00
|
|
|
bio_put_uint32(bio, len);
|
|
|
|
ptr = bio_alloc(bio, (len + 1) * sizeof(uint16_t));
|
|
|
|
if (!ptr)
|
|
|
|
return;
|
|
|
|
|
|
|
|
while (*str)
|
|
|
|
*ptr++ = *str++;
|
|
|
|
*ptr++ = 0;
|
|
|
|
}
|
|
|
|
|
2014-01-10 15:48:29 +00:00
|
|
|
static void *bio_get(struct binder_io *bio, size_t size)
|
2012-10-24 17:45:23 +00:00
|
|
|
{
|
|
|
|
size = (size + 3) & (~3);
|
|
|
|
|
|
|
|
if (bio->data_avail < size){
|
|
|
|
bio->data_avail = 0;
|
|
|
|
bio->flags |= BIO_F_OVERFLOW;
|
2014-01-10 15:48:29 +00:00
|
|
|
return NULL;
|
2012-10-24 17:45:23 +00:00
|
|
|
} else {
|
|
|
|
void *ptr = bio->data;
|
|
|
|
bio->data += size;
|
|
|
|
bio->data_avail -= size;
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t bio_get_uint32(struct binder_io *bio)
|
|
|
|
{
|
|
|
|
uint32_t *ptr = bio_get(bio, sizeof(*ptr));
|
|
|
|
return ptr ? *ptr : 0;
|
|
|
|
}
|
|
|
|
|
2014-01-10 15:48:29 +00:00
|
|
|
uint16_t *bio_get_string16(struct binder_io *bio, size_t *sz)
|
2012-10-24 17:45:23 +00:00
|
|
|
{
|
2014-01-10 15:48:29 +00:00
|
|
|
size_t len;
|
|
|
|
|
|
|
|
/* Note: The payload will carry 32bit size instead of size_t */
|
|
|
|
len = (size_t) bio_get_uint32(bio);
|
2012-10-24 17:45:23 +00:00
|
|
|
if (sz)
|
|
|
|
*sz = len;
|
|
|
|
return bio_get(bio, (len + 1) * sizeof(uint16_t));
|
|
|
|
}
|
|
|
|
|
2014-01-10 13:56:27 +00:00
|
|
|
static struct flat_binder_object *_bio_get_obj(struct binder_io *bio)
|
2012-10-24 17:45:23 +00:00
|
|
|
{
|
2014-01-10 15:48:29 +00:00
|
|
|
size_t n;
|
|
|
|
size_t off = bio->data - bio->data0;
|
2012-10-24 17:45:23 +00:00
|
|
|
|
|
|
|
/* TODO: be smarter about this? */
|
|
|
|
for (n = 0; n < bio->offs_avail; n++) {
|
|
|
|
if (bio->offs[n] == off)
|
2014-01-10 13:56:27 +00:00
|
|
|
return bio_get(bio, sizeof(struct flat_binder_object));
|
2012-10-24 17:45:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bio->data_avail = 0;
|
|
|
|
bio->flags |= BIO_F_OVERFLOW;
|
2014-01-10 15:48:29 +00:00
|
|
|
return NULL;
|
2012-10-24 17:45:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void *bio_get_ref(struct binder_io *bio)
|
|
|
|
{
|
2014-01-10 13:56:27 +00:00
|
|
|
struct flat_binder_object *obj;
|
2012-10-24 17:45:23 +00:00
|
|
|
|
|
|
|
obj = _bio_get_obj(bio);
|
|
|
|
if (!obj)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (obj->type == BINDER_TYPE_HANDLE)
|
2014-01-10 13:56:27 +00:00
|
|
|
return obj->handle;
|
2012-10-24 17:45:23 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|