194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood/* Copyright 2008 The Android Open Source Project
294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood */
394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
4399b6c3bbc0d887ab016a8bb686ff16d36edc6c8Arve Hjønnevåg#include <inttypes.h>
594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood#include <stdio.h>
694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood#include <stdlib.h>
794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood#include <errno.h>
894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood#include <unistd.h>
994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood#include <fcntl.h>
1094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood#include <sys/mman.h>
1194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
1294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood#include "binder.h"
1394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
1494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood#define MAX_BIO_SIZE (1 << 30)
1594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
1694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood#define TRACE 0
1794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
1894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood#define LOG_TAG "Binder"
1994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood#include <cutils/log.h>
2094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
21bcf38880c65297da58194eb0c0ce8d6e2bab7d94Serban Constantinescuvoid bio_init_from_txn(struct binder_io *io, struct binder_transaction_data *txn);
2294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
2394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood#if TRACE
249b738bb4110926b85da65d36b2e6f1a50199ec4cSerban Constantinescuvoid hexdump(void *_data, size_t len)
2594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
2694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    unsigned char *data = _data;
279b738bb4110926b85da65d36b2e6f1a50199ec4cSerban Constantinescu    size_t count;
2894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
2994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    for (count = 0; count < len; count++) {
3094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if ((count & 15) == 0)
319b738bb4110926b85da65d36b2e6f1a50199ec4cSerban Constantinescu            fprintf(stderr,"%04zu:", count);
3294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        fprintf(stderr," %02x %c", *data,
3394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                (*data < 32) || (*data > 126) ? '.' : *data);
3494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        data++;
3594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if ((count & 15) == 15)
3694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            fprintf(stderr,"\n");
3794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
3894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if ((count & 15) != 0)
3994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        fprintf(stderr,"\n");
4094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
4194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
42bcf38880c65297da58194eb0c0ce8d6e2bab7d94Serban Constantinescuvoid binder_dump_txn(struct binder_transaction_data *txn)
4394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
44bcf38880c65297da58194eb0c0ce8d6e2bab7d94Serban Constantinescu    struct flat_binder_object *obj;
45399b6c3bbc0d887ab016a8bb686ff16d36edc6c8Arve Hjønnevåg    binder_size_t *offs = (binder_size_t *)(uintptr_t)txn->data.ptr.offsets;
46399b6c3bbc0d887ab016a8bb686ff16d36edc6c8Arve Hjønnevåg    size_t count = txn->offsets_size / sizeof(binder_size_t);
47399b6c3bbc0d887ab016a8bb686ff16d36edc6c8Arve Hjønnevåg
48399b6c3bbc0d887ab016a8bb686ff16d36edc6c8Arve Hjønnevåg    fprintf(stderr,"  target %016"PRIx64"  cookie %016"PRIx64"  code %08x  flags %08x\n",
49399b6c3bbc0d887ab016a8bb686ff16d36edc6c8Arve Hjønnevåg            (uint64_t)txn->target.ptr, (uint64_t)txn->cookie, txn->code, txn->flags);
50399b6c3bbc0d887ab016a8bb686ff16d36edc6c8Arve Hjønnevåg    fprintf(stderr,"  pid %8d  uid %8d  data %"PRIu64"  offs %"PRIu64"\n",
51399b6c3bbc0d887ab016a8bb686ff16d36edc6c8Arve Hjønnevåg            txn->sender_pid, txn->sender_euid, (uint64_t)txn->data_size, (uint64_t)txn->offsets_size);
52399b6c3bbc0d887ab016a8bb686ff16d36edc6c8Arve Hjønnevåg    hexdump((void *)(uintptr_t)txn->data.ptr.buffer, txn->data_size);
5394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    while (count--) {
54399b6c3bbc0d887ab016a8bb686ff16d36edc6c8Arve Hjønnevåg        obj = (struct flat_binder_object *) (((char*)(uintptr_t)txn->data.ptr.buffer) + *offs++);
55399b6c3bbc0d887ab016a8bb686ff16d36edc6c8Arve Hjønnevåg        fprintf(stderr,"  - type %08x  flags %08x  ptr %016"PRIx64"  cookie %016"PRIx64"\n",
56399b6c3bbc0d887ab016a8bb686ff16d36edc6c8Arve Hjønnevåg                obj->type, obj->flags, (uint64_t)obj->binder, (uint64_t)obj->cookie);
5794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
5894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
5994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
6094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood#define NAME(n) case n: return #n
6194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodconst char *cmd_name(uint32_t cmd)
6294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
6394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    switch(cmd) {
6494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        NAME(BR_NOOP);
6594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        NAME(BR_TRANSACTION_COMPLETE);
6694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        NAME(BR_INCREFS);
6794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        NAME(BR_ACQUIRE);
6894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        NAME(BR_RELEASE);
6994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        NAME(BR_DECREFS);
7094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        NAME(BR_TRANSACTION);
7194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        NAME(BR_REPLY);
7294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        NAME(BR_FAILED_REPLY);
7394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        NAME(BR_DEAD_REPLY);
7494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        NAME(BR_DEAD_BINDER);
7594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    default: return "???";
7694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
7794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
7894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood#else
7994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood#define hexdump(a,b) do{} while (0)
8094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood#define binder_dump_txn(txn)  do{} while (0)
8194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood#endif
8294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
8394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood#define BIO_F_SHARED    0x01  /* needs to be buffer freed */
8494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood#define BIO_F_OVERFLOW  0x02  /* ran out of space */
8594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood#define BIO_F_IOERROR   0x04
8694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood#define BIO_F_MALLOCED  0x08  /* needs to be free()'d */
8794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
8894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodstruct binder_state
8994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
9094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int fd;
9194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    void *mapped;
929b738bb4110926b85da65d36b2e6f1a50199ec4cSerban Constantinescu    size_t mapsize;
9394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood};
9494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
959b738bb4110926b85da65d36b2e6f1a50199ec4cSerban Constantinescustruct binder_state *binder_open(size_t mapsize)
9694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
9794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    struct binder_state *bs;
98a44542ca74b7da5b44ba30c205c3244805bb0600Serban Constantinescu    struct binder_version vers;
9994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
10094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    bs = malloc(sizeof(*bs));
10194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (!bs) {
10294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        errno = ENOMEM;
1039b738bb4110926b85da65d36b2e6f1a50199ec4cSerban Constantinescu        return NULL;
10494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
10594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
10694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    bs->fd = open("/dev/binder", O_RDWR);
10794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (bs->fd < 0) {
10894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        fprintf(stderr,"binder: cannot open device (%s)\n",
10994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                strerror(errno));
11094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        goto fail_open;
11194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
11294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
113a44542ca74b7da5b44ba30c205c3244805bb0600Serban Constantinescu    if ((ioctl(bs->fd, BINDER_VERSION, &vers) == -1) ||
114a44542ca74b7da5b44ba30c205c3244805bb0600Serban Constantinescu        (vers.protocol_version != BINDER_CURRENT_PROTOCOL_VERSION)) {
115a44542ca74b7da5b44ba30c205c3244805bb0600Serban Constantinescu        fprintf(stderr, "binder: driver version differs from user space\n");
116a44542ca74b7da5b44ba30c205c3244805bb0600Serban Constantinescu        goto fail_open;
117a44542ca74b7da5b44ba30c205c3244805bb0600Serban Constantinescu    }
118a44542ca74b7da5b44ba30c205c3244805bb0600Serban Constantinescu
11994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    bs->mapsize = mapsize;
12094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    bs->mapped = mmap(NULL, mapsize, PROT_READ, MAP_PRIVATE, bs->fd, 0);
12194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (bs->mapped == MAP_FAILED) {
12294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        fprintf(stderr,"binder: cannot map device (%s)\n",
12394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                strerror(errno));
12494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        goto fail_map;
12594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
12694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
12794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return bs;
12894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
12994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodfail_map:
13094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    close(bs->fd);
13194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodfail_open:
13294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    free(bs);
1339b738bb4110926b85da65d36b2e6f1a50199ec4cSerban Constantinescu    return NULL;
13494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
13594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
13694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodvoid binder_close(struct binder_state *bs)
13794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
13894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    munmap(bs->mapped, bs->mapsize);
13994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    close(bs->fd);
14094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    free(bs);
14194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
14294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
14394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodint binder_become_context_manager(struct binder_state *bs)
14494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
14594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return ioctl(bs->fd, BINDER_SET_CONTEXT_MGR, 0);
14694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
14794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
1489b738bb4110926b85da65d36b2e6f1a50199ec4cSerban Constantinescuint binder_write(struct binder_state *bs, void *data, size_t len)
14994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
15094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    struct binder_write_read bwr;
15194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int res;
1523a345f0df5f62d77e875a289e9aee89f0d1b526eSerban Constantinescu
15394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    bwr.write_size = len;
15494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    bwr.write_consumed = 0;
1553a345f0df5f62d77e875a289e9aee89f0d1b526eSerban Constantinescu    bwr.write_buffer = (uintptr_t) data;
15694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    bwr.read_size = 0;
15794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    bwr.read_consumed = 0;
15894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    bwr.read_buffer = 0;
15994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    res = ioctl(bs->fd, BINDER_WRITE_READ, &bwr);
16094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (res < 0) {
16194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        fprintf(stderr,"binder_write: ioctl failed (%s)\n",
16294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                strerror(errno));
16394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
16494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return res;
16594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
16694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
16794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodvoid binder_send_reply(struct binder_state *bs,
16894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                       struct binder_io *reply,
169399b6c3bbc0d887ab016a8bb686ff16d36edc6c8Arve Hjønnevåg                       binder_uintptr_t buffer_to_free,
17094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                       int status)
17194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
17294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    struct {
17394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        uint32_t cmd_free;
174399b6c3bbc0d887ab016a8bb686ff16d36edc6c8Arve Hjønnevåg        binder_uintptr_t buffer;
17594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        uint32_t cmd_reply;
176bcf38880c65297da58194eb0c0ce8d6e2bab7d94Serban Constantinescu        struct binder_transaction_data txn;
17794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    } __attribute__((packed)) data;
17894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
17994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    data.cmd_free = BC_FREE_BUFFER;
180399b6c3bbc0d887ab016a8bb686ff16d36edc6c8Arve Hjønnevåg    data.buffer = buffer_to_free;
18194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    data.cmd_reply = BC_REPLY;
182bcf38880c65297da58194eb0c0ce8d6e2bab7d94Serban Constantinescu    data.txn.target.ptr = 0;
18394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    data.txn.cookie = 0;
18494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    data.txn.code = 0;
18594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (status) {
18694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        data.txn.flags = TF_STATUS_CODE;
18794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        data.txn.data_size = sizeof(int);
188bcf38880c65297da58194eb0c0ce8d6e2bab7d94Serban Constantinescu        data.txn.offsets_size = 0;
189399b6c3bbc0d887ab016a8bb686ff16d36edc6c8Arve Hjønnevåg        data.txn.data.ptr.buffer = (uintptr_t)&status;
190bcf38880c65297da58194eb0c0ce8d6e2bab7d94Serban Constantinescu        data.txn.data.ptr.offsets = 0;
19194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    } else {
19294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        data.txn.flags = 0;
19394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        data.txn.data_size = reply->data - reply->data0;
194bcf38880c65297da58194eb0c0ce8d6e2bab7d94Serban Constantinescu        data.txn.offsets_size = ((char*) reply->offs) - ((char*) reply->offs0);
195399b6c3bbc0d887ab016a8bb686ff16d36edc6c8Arve Hjønnevåg        data.txn.data.ptr.buffer = (uintptr_t)reply->data0;
196399b6c3bbc0d887ab016a8bb686ff16d36edc6c8Arve Hjønnevåg        data.txn.data.ptr.offsets = (uintptr_t)reply->offs0;
19794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
19894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    binder_write(bs, &data, sizeof(data));
19994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
20094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
20194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodint binder_parse(struct binder_state *bs, struct binder_io *bio,
2023a345f0df5f62d77e875a289e9aee89f0d1b526eSerban Constantinescu                 uintptr_t ptr, size_t size, binder_handler func)
20394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
20494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int r = 1;
2053a345f0df5f62d77e875a289e9aee89f0d1b526eSerban Constantinescu    uintptr_t end = ptr + (uintptr_t) size;
20694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
20794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    while (ptr < end) {
2083a345f0df5f62d77e875a289e9aee89f0d1b526eSerban Constantinescu        uint32_t cmd = *(uint32_t *) ptr;
2093a345f0df5f62d77e875a289e9aee89f0d1b526eSerban Constantinescu        ptr += sizeof(uint32_t);
21094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood#if TRACE
21194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        fprintf(stderr,"%s:\n", cmd_name(cmd));
21294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood#endif
21394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        switch(cmd) {
21494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        case BR_NOOP:
21594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            break;
21694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        case BR_TRANSACTION_COMPLETE:
21794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            break;
21894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        case BR_INCREFS:
21994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        case BR_ACQUIRE:
22094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        case BR_RELEASE:
22194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        case BR_DECREFS:
22294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood#if TRACE
223399b6c3bbc0d887ab016a8bb686ff16d36edc6c8Arve Hjønnevåg            fprintf(stderr,"  %p, %p\n", (void *)ptr, (void *)(ptr + sizeof(void *)));
22494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood#endif
2253a345f0df5f62d77e875a289e9aee89f0d1b526eSerban Constantinescu            ptr += sizeof(struct binder_ptr_cookie);
22694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            break;
22794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        case BR_TRANSACTION: {
228bcf38880c65297da58194eb0c0ce8d6e2bab7d94Serban Constantinescu            struct binder_transaction_data *txn = (struct binder_transaction_data *) ptr;
2293a345f0df5f62d77e875a289e9aee89f0d1b526eSerban Constantinescu            if ((end - ptr) < sizeof(*txn)) {
23094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                ALOGE("parse: txn too small!\n");
23194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                return -1;
23294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
23394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            binder_dump_txn(txn);
23494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (func) {
23594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                unsigned rdata[256/4];
23694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                struct binder_io msg;
23794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                struct binder_io reply;
23894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                int res;
23994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
24094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                bio_init(&reply, rdata, sizeof(rdata), 4);
24194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                bio_init_from_txn(&msg, txn);
24294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                res = func(bs, txn, &msg, &reply);
243bcf38880c65297da58194eb0c0ce8d6e2bab7d94Serban Constantinescu                binder_send_reply(bs, &reply, txn->data.ptr.buffer, res);
24494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
2453a345f0df5f62d77e875a289e9aee89f0d1b526eSerban Constantinescu            ptr += sizeof(*txn);
24694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            break;
24794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
24894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        case BR_REPLY: {
249bcf38880c65297da58194eb0c0ce8d6e2bab7d94Serban Constantinescu            struct binder_transaction_data *txn = (struct binder_transaction_data *) ptr;
2503a345f0df5f62d77e875a289e9aee89f0d1b526eSerban Constantinescu            if ((end - ptr) < sizeof(*txn)) {
25194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                ALOGE("parse: reply too small!\n");
25294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                return -1;
25394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
25494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            binder_dump_txn(txn);
25594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (bio) {
25694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                bio_init_from_txn(bio, txn);
25794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                bio = 0;
25894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            } else {
2593a345f0df5f62d77e875a289e9aee89f0d1b526eSerban Constantinescu                /* todo FREE BUFFER */
26094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
2613a345f0df5f62d77e875a289e9aee89f0d1b526eSerban Constantinescu            ptr += sizeof(*txn);
26294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            r = 0;
26394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            break;
26494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
26594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        case BR_DEAD_BINDER: {
266399b6c3bbc0d887ab016a8bb686ff16d36edc6c8Arve Hjønnevåg            struct binder_death *death = (struct binder_death *)(uintptr_t) *(binder_uintptr_t *)ptr;
267399b6c3bbc0d887ab016a8bb686ff16d36edc6c8Arve Hjønnevåg            ptr += sizeof(binder_uintptr_t);
26894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            death->func(bs, death->ptr);
26994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            break;
27094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
27194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        case BR_FAILED_REPLY:
27294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            r = -1;
27394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            break;
27494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        case BR_DEAD_REPLY:
27594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            r = -1;
27694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            break;
27794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        default:
27894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            ALOGE("parse: OOPS %d\n", cmd);
27994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            return -1;
28094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
28194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
28294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
28394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return r;
28494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
28594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
2865fb1b8836aa5cf0f38b49bc7bfb8343b84fdf9bfSerban Constantinescuvoid binder_acquire(struct binder_state *bs, uint32_t target)
28794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
28894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    uint32_t cmd[2];
28994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    cmd[0] = BC_ACQUIRE;
2905fb1b8836aa5cf0f38b49bc7bfb8343b84fdf9bfSerban Constantinescu    cmd[1] = target;
29194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    binder_write(bs, cmd, sizeof(cmd));
29294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
29394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
2945fb1b8836aa5cf0f38b49bc7bfb8343b84fdf9bfSerban Constantinescuvoid binder_release(struct binder_state *bs, uint32_t target)
29594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
29694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    uint32_t cmd[2];
29794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    cmd[0] = BC_RELEASE;
2985fb1b8836aa5cf0f38b49bc7bfb8343b84fdf9bfSerban Constantinescu    cmd[1] = target;
29994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    binder_write(bs, cmd, sizeof(cmd));
30094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
30194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
3025fb1b8836aa5cf0f38b49bc7bfb8343b84fdf9bfSerban Constantinescuvoid binder_link_to_death(struct binder_state *bs, uint32_t target, struct binder_death *death)
30394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
3043a345f0df5f62d77e875a289e9aee89f0d1b526eSerban Constantinescu    struct {
3053a345f0df5f62d77e875a289e9aee89f0d1b526eSerban Constantinescu        uint32_t cmd;
3063a345f0df5f62d77e875a289e9aee89f0d1b526eSerban Constantinescu        struct binder_handle_cookie payload;
3073a345f0df5f62d77e875a289e9aee89f0d1b526eSerban Constantinescu    } __attribute__((packed)) data;
30894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
3093a345f0df5f62d77e875a289e9aee89f0d1b526eSerban Constantinescu    data.cmd = BC_REQUEST_DEATH_NOTIFICATION;
3103a345f0df5f62d77e875a289e9aee89f0d1b526eSerban Constantinescu    data.payload.handle = target;
3113a345f0df5f62d77e875a289e9aee89f0d1b526eSerban Constantinescu    data.payload.cookie = (uintptr_t) death;
3123a345f0df5f62d77e875a289e9aee89f0d1b526eSerban Constantinescu    binder_write(bs, &data, sizeof(data));
3133a345f0df5f62d77e875a289e9aee89f0d1b526eSerban Constantinescu}
31494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
31594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodint binder_call(struct binder_state *bs,
31694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                struct binder_io *msg, struct binder_io *reply,
3175fb1b8836aa5cf0f38b49bc7bfb8343b84fdf9bfSerban Constantinescu                uint32_t target, uint32_t code)
31894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
31994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int res;
32094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    struct binder_write_read bwr;
32194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    struct {
32294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        uint32_t cmd;
323bcf38880c65297da58194eb0c0ce8d6e2bab7d94Serban Constantinescu        struct binder_transaction_data txn;
3243a345f0df5f62d77e875a289e9aee89f0d1b526eSerban Constantinescu    } __attribute__((packed)) writebuf;
32594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    unsigned readbuf[32];
32694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
32794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (msg->flags & BIO_F_OVERFLOW) {
32894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        fprintf(stderr,"binder: txn buffer overflow\n");
32994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        goto fail;
33094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
33194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
33294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    writebuf.cmd = BC_TRANSACTION;
333bcf38880c65297da58194eb0c0ce8d6e2bab7d94Serban Constantinescu    writebuf.txn.target.handle = target;
33494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    writebuf.txn.code = code;
33594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    writebuf.txn.flags = 0;
33694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    writebuf.txn.data_size = msg->data - msg->data0;
337bcf38880c65297da58194eb0c0ce8d6e2bab7d94Serban Constantinescu    writebuf.txn.offsets_size = ((char*) msg->offs) - ((char*) msg->offs0);
338399b6c3bbc0d887ab016a8bb686ff16d36edc6c8Arve Hjønnevåg    writebuf.txn.data.ptr.buffer = (uintptr_t)msg->data0;
339399b6c3bbc0d887ab016a8bb686ff16d36edc6c8Arve Hjønnevåg    writebuf.txn.data.ptr.offsets = (uintptr_t)msg->offs0;
34094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
34194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    bwr.write_size = sizeof(writebuf);
34294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    bwr.write_consumed = 0;
3433a345f0df5f62d77e875a289e9aee89f0d1b526eSerban Constantinescu    bwr.write_buffer = (uintptr_t) &writebuf;
3449b738bb4110926b85da65d36b2e6f1a50199ec4cSerban Constantinescu
34594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    hexdump(msg->data0, msg->data - msg->data0);
34694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    for (;;) {
34794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        bwr.read_size = sizeof(readbuf);
34894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        bwr.read_consumed = 0;
3493a345f0df5f62d77e875a289e9aee89f0d1b526eSerban Constantinescu        bwr.read_buffer = (uintptr_t) readbuf;
35094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
35194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        res = ioctl(bs->fd, BINDER_WRITE_READ, &bwr);
35294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
35394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (res < 0) {
35494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            fprintf(stderr,"binder: ioctl failed (%s)\n", strerror(errno));
35594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            goto fail;
35694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
35794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
3583a345f0df5f62d77e875a289e9aee89f0d1b526eSerban Constantinescu        res = binder_parse(bs, reply, (uintptr_t) readbuf, bwr.read_consumed, 0);
35994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (res == 0) return 0;
36094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (res < 0) goto fail;
36194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
36294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
36394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodfail:
36494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    memset(reply, 0, sizeof(*reply));
36594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    reply->flags |= BIO_F_IOERROR;
36694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return -1;
36794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
36894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
36994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodvoid binder_loop(struct binder_state *bs, binder_handler func)
37094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
37194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int res;
37294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    struct binder_write_read bwr;
3733a345f0df5f62d77e875a289e9aee89f0d1b526eSerban Constantinescu    uint32_t readbuf[32];
37494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
37594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    bwr.write_size = 0;
37694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    bwr.write_consumed = 0;
37794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    bwr.write_buffer = 0;
3789b738bb4110926b85da65d36b2e6f1a50199ec4cSerban Constantinescu
37994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    readbuf[0] = BC_ENTER_LOOPER;
3803a345f0df5f62d77e875a289e9aee89f0d1b526eSerban Constantinescu    binder_write(bs, readbuf, sizeof(uint32_t));
38194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
38294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    for (;;) {
38394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        bwr.read_size = sizeof(readbuf);
38494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        bwr.read_consumed = 0;
3853a345f0df5f62d77e875a289e9aee89f0d1b526eSerban Constantinescu        bwr.read_buffer = (uintptr_t) readbuf;
38694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
38794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        res = ioctl(bs->fd, BINDER_WRITE_READ, &bwr);
38894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
38994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (res < 0) {
39094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            ALOGE("binder_loop: ioctl failed (%s)\n", strerror(errno));
39194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            break;
39294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
39394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
3943a345f0df5f62d77e875a289e9aee89f0d1b526eSerban Constantinescu        res = binder_parse(bs, 0, (uintptr_t) readbuf, bwr.read_consumed, func);
39594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (res == 0) {
39694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            ALOGE("binder_loop: unexpected reply?!\n");
39794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            break;
39894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
39994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (res < 0) {
40094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            ALOGE("binder_loop: io error %d %s\n", res, strerror(errno));
40194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            break;
40294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
40394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
40494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
40594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
406bcf38880c65297da58194eb0c0ce8d6e2bab7d94Serban Constantinescuvoid bio_init_from_txn(struct binder_io *bio, struct binder_transaction_data *txn)
40794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
408399b6c3bbc0d887ab016a8bb686ff16d36edc6c8Arve Hjønnevåg    bio->data = bio->data0 = (char *)(intptr_t)txn->data.ptr.buffer;
409399b6c3bbc0d887ab016a8bb686ff16d36edc6c8Arve Hjønnevåg    bio->offs = bio->offs0 = (binder_size_t *)(intptr_t)txn->data.ptr.offsets;
41094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    bio->data_avail = txn->data_size;
4113a345f0df5f62d77e875a289e9aee89f0d1b526eSerban Constantinescu    bio->offs_avail = txn->offsets_size / sizeof(size_t);
41294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    bio->flags = BIO_F_SHARED;
41394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
41494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
41594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodvoid bio_init(struct binder_io *bio, void *data,
4169b738bb4110926b85da65d36b2e6f1a50199ec4cSerban Constantinescu              size_t maxdata, size_t maxoffs)
41794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
4189b738bb4110926b85da65d36b2e6f1a50199ec4cSerban Constantinescu    size_t n = maxoffs * sizeof(size_t);
41994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
42094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (n > maxdata) {
42194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        bio->flags = BIO_F_OVERFLOW;
42294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        bio->data_avail = 0;
42394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        bio->offs_avail = 0;
42494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return;
42594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
42694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
42794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    bio->data = bio->data0 = (char *) data + n;
42894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    bio->offs = bio->offs0 = data;
42994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    bio->data_avail = maxdata - n;
43094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    bio->offs_avail = maxoffs;
43194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    bio->flags = 0;
43294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
43394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
4343a345f0df5f62d77e875a289e9aee89f0d1b526eSerban Constantinescustatic void *bio_alloc(struct binder_io *bio, size_t size)
43594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
43694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    size = (size + 3) & (~3);
43794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (size > bio->data_avail) {
43894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        bio->flags |= BIO_F_OVERFLOW;
4399b738bb4110926b85da65d36b2e6f1a50199ec4cSerban Constantinescu        return NULL;
44094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    } else {
44194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        void *ptr = bio->data;
44294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        bio->data += size;
44394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        bio->data_avail -= size;
44494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return ptr;
44594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
44694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
44794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
44894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodvoid binder_done(struct binder_state *bs,
44994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                 struct binder_io *msg,
45094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                 struct binder_io *reply)
45194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
4523a345f0df5f62d77e875a289e9aee89f0d1b526eSerban Constantinescu    struct {
4533a345f0df5f62d77e875a289e9aee89f0d1b526eSerban Constantinescu        uint32_t cmd;
4543a345f0df5f62d77e875a289e9aee89f0d1b526eSerban Constantinescu        uintptr_t buffer;
4553a345f0df5f62d77e875a289e9aee89f0d1b526eSerban Constantinescu    } __attribute__((packed)) data;
4563a345f0df5f62d77e875a289e9aee89f0d1b526eSerban Constantinescu
45794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (reply->flags & BIO_F_SHARED) {
4583a345f0df5f62d77e875a289e9aee89f0d1b526eSerban Constantinescu        data.cmd = BC_FREE_BUFFER;
4593a345f0df5f62d77e875a289e9aee89f0d1b526eSerban Constantinescu        data.buffer = (uintptr_t) reply->data0;
4603a345f0df5f62d77e875a289e9aee89f0d1b526eSerban Constantinescu        binder_write(bs, &data, sizeof(data));
46194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        reply->flags = 0;
46294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
46394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
46494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
465bcf38880c65297da58194eb0c0ce8d6e2bab7d94Serban Constantinescustatic struct flat_binder_object *bio_alloc_obj(struct binder_io *bio)
46694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
467bcf38880c65297da58194eb0c0ce8d6e2bab7d94Serban Constantinescu    struct flat_binder_object *obj;
46894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
46994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    obj = bio_alloc(bio, sizeof(*obj));
4709b738bb4110926b85da65d36b2e6f1a50199ec4cSerban Constantinescu
47194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (obj && bio->offs_avail) {
47294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        bio->offs_avail--;
47394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        *bio->offs++ = ((char*) obj) - ((char*) bio->data0);
47494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return obj;
47594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
47694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
47794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    bio->flags |= BIO_F_OVERFLOW;
4789b738bb4110926b85da65d36b2e6f1a50199ec4cSerban Constantinescu    return NULL;
47994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
48094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
48194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodvoid bio_put_uint32(struct binder_io *bio, uint32_t n)
48294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
48394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    uint32_t *ptr = bio_alloc(bio, sizeof(n));
48494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (ptr)
48594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        *ptr = n;
48694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
48794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
48894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodvoid bio_put_obj(struct binder_io *bio, void *ptr)
48994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
490bcf38880c65297da58194eb0c0ce8d6e2bab7d94Serban Constantinescu    struct flat_binder_object *obj;
49194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
49294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    obj = bio_alloc_obj(bio);
49394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (!obj)
49494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return;
49594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
49694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    obj->flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
49794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    obj->type = BINDER_TYPE_BINDER;
498399b6c3bbc0d887ab016a8bb686ff16d36edc6c8Arve Hjønnevåg    obj->binder = (uintptr_t)ptr;
49994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    obj->cookie = 0;
50094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
50194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
5025fb1b8836aa5cf0f38b49bc7bfb8343b84fdf9bfSerban Constantinescuvoid bio_put_ref(struct binder_io *bio, uint32_t handle)
50394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
504bcf38880c65297da58194eb0c0ce8d6e2bab7d94Serban Constantinescu    struct flat_binder_object *obj;
50594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
5065fb1b8836aa5cf0f38b49bc7bfb8343b84fdf9bfSerban Constantinescu    if (handle)
50794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        obj = bio_alloc_obj(bio);
50894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    else
50994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        obj = bio_alloc(bio, sizeof(*obj));
51094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
51194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (!obj)
51294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return;
51394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
51494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    obj->flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
51594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    obj->type = BINDER_TYPE_HANDLE;
5165fb1b8836aa5cf0f38b49bc7bfb8343b84fdf9bfSerban Constantinescu    obj->handle = handle;
51794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    obj->cookie = 0;
51894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
51994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
52094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodvoid bio_put_string16(struct binder_io *bio, const uint16_t *str)
52194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
5229b738bb4110926b85da65d36b2e6f1a50199ec4cSerban Constantinescu    size_t len;
52394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    uint16_t *ptr;
52494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
52594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (!str) {
52694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        bio_put_uint32(bio, 0xffffffff);
52794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return;
52894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
52994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
53094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    len = 0;
53194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    while (str[len]) len++;
53294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
53394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (len >= (MAX_BIO_SIZE / sizeof(uint16_t))) {
53494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        bio_put_uint32(bio, 0xffffffff);
53594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return;
53694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
53794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
5389b738bb4110926b85da65d36b2e6f1a50199ec4cSerban Constantinescu    /* Note: The payload will carry 32bit size instead of size_t */
5399b738bb4110926b85da65d36b2e6f1a50199ec4cSerban Constantinescu    bio_put_uint32(bio, (uint32_t) len);
54094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    len = (len + 1) * sizeof(uint16_t);
54194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    ptr = bio_alloc(bio, len);
54294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (ptr)
54394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        memcpy(ptr, str, len);
54494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
54594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
54694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodvoid bio_put_string16_x(struct binder_io *bio, const char *_str)
54794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
54894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    unsigned char *str = (unsigned char*) _str;
5499b738bb4110926b85da65d36b2e6f1a50199ec4cSerban Constantinescu    size_t len;
55094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    uint16_t *ptr;
55194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
55294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (!str) {
55394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        bio_put_uint32(bio, 0xffffffff);
55494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return;
55594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
55694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
55794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    len = strlen(_str);
55894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
55994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (len >= (MAX_BIO_SIZE / sizeof(uint16_t))) {
56094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        bio_put_uint32(bio, 0xffffffff);
56194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return;
56294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
56394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
5649b738bb4110926b85da65d36b2e6f1a50199ec4cSerban Constantinescu    /* Note: The payload will carry 32bit size instead of size_t */
56594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    bio_put_uint32(bio, len);
56694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    ptr = bio_alloc(bio, (len + 1) * sizeof(uint16_t));
56794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (!ptr)
56894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return;
56994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
57094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    while (*str)
57194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        *ptr++ = *str++;
57294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    *ptr++ = 0;
57394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
57494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
5759b738bb4110926b85da65d36b2e6f1a50199ec4cSerban Constantinescustatic void *bio_get(struct binder_io *bio, size_t size)
57694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
57794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    size = (size + 3) & (~3);
57894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
57994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (bio->data_avail < size){
58094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        bio->data_avail = 0;
58194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        bio->flags |= BIO_F_OVERFLOW;
5829b738bb4110926b85da65d36b2e6f1a50199ec4cSerban Constantinescu        return NULL;
58394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }  else {
58494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        void *ptr = bio->data;
58594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        bio->data += size;
58694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        bio->data_avail -= size;
58794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return ptr;
58894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
58994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
59094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
59194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwooduint32_t bio_get_uint32(struct binder_io *bio)
59294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
59394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    uint32_t *ptr = bio_get(bio, sizeof(*ptr));
59494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return ptr ? *ptr : 0;
59594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
59694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
5979b738bb4110926b85da65d36b2e6f1a50199ec4cSerban Constantinescuuint16_t *bio_get_string16(struct binder_io *bio, size_t *sz)
59894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
5999b738bb4110926b85da65d36b2e6f1a50199ec4cSerban Constantinescu    size_t len;
6009b738bb4110926b85da65d36b2e6f1a50199ec4cSerban Constantinescu
6019b738bb4110926b85da65d36b2e6f1a50199ec4cSerban Constantinescu    /* Note: The payload will carry 32bit size instead of size_t */
6029b738bb4110926b85da65d36b2e6f1a50199ec4cSerban Constantinescu    len = (size_t) bio_get_uint32(bio);
60394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (sz)
60494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        *sz = len;
60594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return bio_get(bio, (len + 1) * sizeof(uint16_t));
60694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
60794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
608bcf38880c65297da58194eb0c0ce8d6e2bab7d94Serban Constantinescustatic struct flat_binder_object *_bio_get_obj(struct binder_io *bio)
60994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
6109b738bb4110926b85da65d36b2e6f1a50199ec4cSerban Constantinescu    size_t n;
6119b738bb4110926b85da65d36b2e6f1a50199ec4cSerban Constantinescu    size_t off = bio->data - bio->data0;
61294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
6133a345f0df5f62d77e875a289e9aee89f0d1b526eSerban Constantinescu    /* TODO: be smarter about this? */
61494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    for (n = 0; n < bio->offs_avail; n++) {
61594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (bio->offs[n] == off)
616bcf38880c65297da58194eb0c0ce8d6e2bab7d94Serban Constantinescu            return bio_get(bio, sizeof(struct flat_binder_object));
61794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
61894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
61994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    bio->data_avail = 0;
62094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    bio->flags |= BIO_F_OVERFLOW;
6219b738bb4110926b85da65d36b2e6f1a50199ec4cSerban Constantinescu    return NULL;
62294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
62394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
6245fb1b8836aa5cf0f38b49bc7bfb8343b84fdf9bfSerban Constantinescuuint32_t bio_get_ref(struct binder_io *bio)
62594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
626bcf38880c65297da58194eb0c0ce8d6e2bab7d94Serban Constantinescu    struct flat_binder_object *obj;
62794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
62894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    obj = _bio_get_obj(bio);
62994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (!obj)
63094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return 0;
63194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
63294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (obj->type == BINDER_TYPE_HANDLE)
633bcf38880c65297da58194eb0c0ce8d6e2bab7d94Serban Constantinescu        return obj->handle;
63494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
63594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return 0;
63694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
637