From 076e0ef3f5751960eafff2748d3f646981f5f23c Mon Sep 17 00:00:00 2001 From: Andrew Scull Date: Thu, 28 Sep 2017 18:07:02 +0100 Subject: Implement a libnos_datagram device for Citadel on Android. Ports manual tests towards this implementation as it will be the official datagram communication library on Android. Test: mm Change-Id: I5cd34db40f1f5c40d3ef1adca45cc2cb5f7147a0 --- citadel/Android.bp | 19 +++ citadel/libnos_datagram/Android.bp | 25 ++++ citadel/libnos_datagram/citadel.c | 159 +++++++++++++++++++++ .../libnos_datagram/include/nos/android/citadel.h | 42 ++++++ 4 files changed, 245 insertions(+) create mode 100644 citadel/Android.bp create mode 100644 citadel/libnos_datagram/Android.bp create mode 100644 citadel/libnos_datagram/citadel.c create mode 100644 citadel/libnos_datagram/include/nos/android/citadel.h (limited to 'citadel') diff --git a/citadel/Android.bp b/citadel/Android.bp new file mode 100644 index 0000000..8c2ad75 --- /dev/null +++ b/citadel/Android.bp @@ -0,0 +1,19 @@ +// +// Copyright (C) 2017 The Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +subdirs = [ + "libnos_datagram", +] diff --git a/citadel/libnos_datagram/Android.bp b/citadel/libnos_datagram/Android.bp new file mode 100644 index 0000000..dcaf0dd --- /dev/null +++ b/citadel/libnos_datagram/Android.bp @@ -0,0 +1,25 @@ +// +// Copyright (C) 2017 The Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +cc_library { + name: "libnos_datagram_citadel", + srcs: ["citadel.c"], + defaults: ["nos_shared_cc_defaults"], + shared_libs: [ + "libnos_datagram", + ], + export_include_dirs: ["include"], +} diff --git a/citadel/libnos_datagram/citadel.c b/citadel/libnos_datagram/citadel.c new file mode 100644 index 0000000..6bc77c5 --- /dev/null +++ b/citadel/libnos_datagram/citadel.c @@ -0,0 +1,159 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/*****************************************************************************/ +/* TODO: #include */ +#define CITADEL_IOC_MAGIC 'c' +struct citadel_ioc_tpm_datagram { + __u64 buf; + __u32 len; + __u32 command; +}; +#define CITADEL_IOC_TPM_DATAGRAM _IOW(CITADEL_IOC_MAGIC, 1, \ + struct citadel_ioc_tpm_datagram) +/*****************************************************************************/ + +#define DEV_CITADEL "/dev/citadel0" + +static uint8_t in_buf[MAX_DEVICE_TRANSFER]; +static int read_datagram(void *ctx, uint32_t command, uint8_t *buf, uint32_t len) { + struct citadel_ioc_tpm_datagram dg = { + .buf = (unsigned long)in_buf, + .len = len, + .command = command, + }; + int ret; + int fd; + + if (!ctx) { + fprintf(stderr, "%s: invalid (NULL) device\n", __func__); + return -1; + } + fd = *(int *)ctx; + if (fd < 0) { + fprintf(stderr, "%s: invalid device\n", __func__); + return -2; + } + + if (len > MAX_DEVICE_TRANSFER) { + fprintf(stderr, "%s: invalid len (%d > %d)\n", __func__, + len, MAX_DEVICE_TRANSFER); + return -3; + } + + ret = ioctl(fd, CITADEL_IOC_TPM_DATAGRAM, &dg); + if (ret < 0) { + perror("can't send spi message"); + return ret; + } + + memcpy(buf, in_buf, len); + + return 0; +} + +static uint8_t out_buf[MAX_DEVICE_TRANSFER]; +static int write_datagram(void *ctx, uint32_t command, const uint8_t *buf, uint32_t len) { + struct citadel_ioc_tpm_datagram dg = { + .buf = (unsigned long)out_buf, + .len = len, + .command = command, + }; + int ret; + int fd; + + if (!ctx) { + fprintf(stderr, "%s: invalid (NULL) device\n", __func__); + return -1; + } + fd = *(int *)ctx; + if (fd < 0) { + fprintf(stderr, "%s: invalid device\n", __func__); + return -2; + } + + if (len > MAX_DEVICE_TRANSFER) { + fprintf(stderr, "%s: invalid len (%d > %d)\n", __func__, + len, MAX_DEVICE_TRANSFER); + return -3; + } + + memcpy(out_buf, buf, len); + + ret = ioctl(fd, CITADEL_IOC_TPM_DATAGRAM, &dg); + if (ret < 0) { + perror("can't send spi message"); + return ret; + } + + return 0; +} + +int nos_android_citadel_device_open(const char *device_name, struct nos_device *dev) { + int fd, *new_fd; + + fd = open(device_name ? device_name : DEV_CITADEL, O_RDWR); + if (fd < 0) { + perror("can't open device"); + return -1; + } + + new_fd = (int *)malloc(sizeof(int)); + if (!new_fd) { + perror("can't malloc new fd"); + close(fd); + return -1; + } + *new_fd = fd; + + dev->ctx = new_fd; + dev->ops.read = read_datagram; + dev->ops.write = write_datagram; + return 0; +} + +void nos_android_citadel_device_close(struct nos_device *dev) { + int fd; + + if (!dev || !dev->ctx) { + fprintf(stderr, "%s: invalid (NULL) device\n", __func__); + return; + } + fd = *(int *)dev->ctx; + if (fd < 0) { + fprintf(stderr, "%s: invalid device\n", __func__); + return; + } + + if (close(fd) < 0) + perror("Problem closing device (ignored)"); + free(dev->ctx); +} diff --git a/citadel/libnos_datagram/include/nos/android/citadel.h b/citadel/libnos_datagram/include/nos/android/citadel.h new file mode 100644 index 0000000..6c1925c --- /dev/null +++ b/citadel/libnos_datagram/include/nos/android/citadel.h @@ -0,0 +1,42 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef NOS_ANDROID_CITADEL_H +#define NOS_ANDROID_CITADEL_H + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/* + * Open a connection to a Nugget device. + * + * Returns 0 on success or negative on failure. + */ +int nos_android_citadel_device_open(const char* device_name, struct nos_device *dev); + +/* + * Close the connection to a Nugget device. + */ +void nos_android_citadel_device_close(struct nos_device *dev); + +#ifdef __cplusplus +} +#endif + +#endif /* NOS_ANDROID_CITADEL_H */ -- cgit v1.2.3