Andrew Scull | 1c021e8 | 2017-09-20 17:04:55 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <nos/transport.h> |
| 18 | |
Anatol Pomazau | bc77563 | 2018-03-26 13:58:36 -0700 | [diff] [blame] | 19 | #include <errno.h> |
Andrew Scull | 1c021e8 | 2017-09-20 17:04:55 +0100 | [diff] [blame] | 20 | #include <stdarg.h> |
| 21 | #include <stdint.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
Allen Webb | 362e167 | 2017-12-14 16:14:13 -0800 | [diff] [blame] | 24 | #include <unistd.h> |
Andrew Scull | 1c021e8 | 2017-09-20 17:04:55 +0100 | [diff] [blame] | 25 | |
| 26 | #include <application.h> |
| 27 | |
| 28 | /* Note: evaluates expressions multiple times */ |
| 29 | #define MIN(a, b) (((a) < (b)) ? (a) : (b)) |
| 30 | |
| 31 | #define VERBOSE_LOG 0 |
| 32 | #define DEBUG_LOG 0 |
| 33 | |
| 34 | #ifdef ANDROID |
| 35 | /* Logging for Android */ |
Bill Richardson | 08c561d | 2018-03-06 13:13:16 -0800 | [diff] [blame] | 36 | #define LOG_TAG "libnos_transport" |
Andrew Scull | 1c021e8 | 2017-09-20 17:04:55 +0100 | [diff] [blame] | 37 | #include <sys/types.h> |
Bill Richardson | 1242dc1 | 2018-03-08 12:12:14 -0800 | [diff] [blame] | 38 | #include <log/log.h> |
Andrew Scull | 1c021e8 | 2017-09-20 17:04:55 +0100 | [diff] [blame] | 39 | |
| 40 | #define NLOGE(...) ALOGE(__VA_ARGS__) |
Bill Richardson | 08c561d | 2018-03-06 13:13:16 -0800 | [diff] [blame] | 41 | #define NLOGV(...) ALOGV(__VA_ARGS__) |
| 42 | #define NLOGD(...) ALOGD(__VA_ARGS__) |
Andrew Scull | 1c021e8 | 2017-09-20 17:04:55 +0100 | [diff] [blame] | 43 | |
Allen Webb | af290aa | 2017-12-21 09:25:04 -0800 | [diff] [blame] | 44 | extern int usleep (uint32_t usec); |
| 45 | |
Andrew Scull | 1c021e8 | 2017-09-20 17:04:55 +0100 | [diff] [blame] | 46 | #else |
| 47 | /* Logging for other platforms */ |
| 48 | #include <stdio.h> |
| 49 | |
Bill Richardson | e7f9c7a | 2017-09-28 14:38:26 +0800 | [diff] [blame] | 50 | #define NLOGE(...) do { fprintf(stderr, __VA_ARGS__); \ |
Andrew Scull | 36ebf2d | 2017-10-10 11:25:21 +0100 | [diff] [blame] | 51 | fprintf(stderr, "\n"); } while (0) |
Andrew Scull | 1c021e8 | 2017-09-20 17:04:55 +0100 | [diff] [blame] | 52 | #define NLOGV(...) do { if (VERBOSE_LOG) { \ |
Andrew Scull | 36ebf2d | 2017-10-10 11:25:21 +0100 | [diff] [blame] | 53 | printf(__VA_ARGS__); printf("\n"); } } while (0) |
Andrew Scull | 1c021e8 | 2017-09-20 17:04:55 +0100 | [diff] [blame] | 54 | #define NLOGD(...) do { if (DEBUG_LOG) { \ |
Andrew Scull | 36ebf2d | 2017-10-10 11:25:21 +0100 | [diff] [blame] | 55 | printf(__VA_ARGS__); printf("\n"); } } while (0) |
Andrew Scull | 1c021e8 | 2017-09-20 17:04:55 +0100 | [diff] [blame] | 56 | |
| 57 | #endif |
| 58 | |
Anatol Pomazau | 0ee610c | 2018-03-28 13:09:47 -0700 | [diff] [blame^] | 59 | /* Citadel might take up to 100ms to wake up */ |
| 60 | #define RETRY_COUNT 25 |
| 61 | #define RETRY_WAIT_TIME_US 5000 |
| 62 | |
| 63 | static int nos_device_read(const struct nos_device *dev, uint32_t command, |
| 64 | uint8_t *buf, uint32_t len) { |
| 65 | int retries = RETRY_COUNT; |
| 66 | while (retries--) { |
| 67 | int err = dev->ops.read(dev->ctx, command, buf, len); |
| 68 | |
| 69 | if (err == -EAGAIN) { |
| 70 | /* Linux driver returns EAGAIN error if Citadel chip is asleep. |
| 71 | * Give to the chip a little bit of time to awake and retry reading |
| 72 | * status again. */ |
| 73 | usleep(RETRY_WAIT_TIME_US); |
| 74 | continue; |
| 75 | } |
| 76 | |
| 77 | if (err) { |
| 78 | NLOGE("Failed to read: %s", strerror(-err)); |
| 79 | } |
| 80 | return -err; |
| 81 | } |
| 82 | |
| 83 | return ETIMEDOUT; |
| 84 | } |
| 85 | |
| 86 | static int nos_device_write(const struct nos_device *dev, uint32_t command, |
| 87 | uint8_t *buf, uint32_t len) { |
| 88 | int retries = RETRY_COUNT; |
| 89 | while (retries--) { |
| 90 | int err = dev->ops.write(dev->ctx, command, buf, len); |
| 91 | |
| 92 | if (err == -EAGAIN) { |
| 93 | /* Linux driver returns EAGAIN error if Citadel chip is asleep. |
| 94 | * Give to the chip a little bit of time to awake and retry reading |
| 95 | * status again. */ |
| 96 | usleep(RETRY_WAIT_TIME_US); |
| 97 | continue; |
| 98 | } |
| 99 | |
| 100 | if (err) { |
| 101 | NLOGE("Failed to write: %s", strerror(-err)); |
| 102 | } |
| 103 | return -err; |
| 104 | } |
| 105 | |
| 106 | return ETIMEDOUT; |
| 107 | } |
| 108 | |
Andrew Scull | 1c021e8 | 2017-09-20 17:04:55 +0100 | [diff] [blame] | 109 | /* status is non-zero on error */ |
Andrew Scull | 3935b18 | 2017-10-11 16:02:39 +0100 | [diff] [blame] | 110 | static int get_status(const struct nos_device *dev, |
| 111 | uint8_t app_id, uint32_t *status, uint16_t *ulen) |
Andrew Scull | 1c021e8 | 2017-09-20 17:04:55 +0100 | [diff] [blame] | 112 | { |
Andrew Scull | 36ebf2d | 2017-10-10 11:25:21 +0100 | [diff] [blame] | 113 | uint8_t buf[6]; |
| 114 | uint32_t command = CMD_ID(app_id) | CMD_IS_READ | CMD_TRANSPORT; |
Andrew Scull | 1c021e8 | 2017-09-20 17:04:55 +0100 | [diff] [blame] | 115 | |
Anatol Pomazau | 0ee610c | 2018-03-28 13:09:47 -0700 | [diff] [blame^] | 116 | if (0 != nos_device_read(dev, command, buf, sizeof(buf))) { |
| 117 | NLOGE("Failed to read device status"); |
Andrew Scull | 3935b18 | 2017-10-11 16:02:39 +0100 | [diff] [blame] | 118 | return -1; |
| 119 | } |
Andrew Scull | 1c021e8 | 2017-09-20 17:04:55 +0100 | [diff] [blame] | 120 | |
Anatol Pomazau | 0ee610c | 2018-03-28 13:09:47 -0700 | [diff] [blame^] | 121 | /* The read operation is successful */ |
| 122 | *status = *(uint32_t *)buf; |
| 123 | *ulen = *(uint16_t *)(buf + 4); |
| 124 | return 0; |
Andrew Scull | 1c021e8 | 2017-09-20 17:04:55 +0100 | [diff] [blame] | 125 | } |
| 126 | |
Andrew Scull | 3935b18 | 2017-10-11 16:02:39 +0100 | [diff] [blame] | 127 | static int clear_status(const struct nos_device *dev, uint8_t app_id) |
Andrew Scull | 1c021e8 | 2017-09-20 17:04:55 +0100 | [diff] [blame] | 128 | { |
Andrew Scull | 36ebf2d | 2017-10-10 11:25:21 +0100 | [diff] [blame] | 129 | uint32_t command = CMD_ID(app_id) | CMD_TRANSPORT; |
Andrew Scull | 1c021e8 | 2017-09-20 17:04:55 +0100 | [diff] [blame] | 130 | |
Anatol Pomazau | 0ee610c | 2018-03-28 13:09:47 -0700 | [diff] [blame^] | 131 | if (0 != nos_device_write(dev, command, 0, 0)) { |
Andrew Scull | 3935b18 | 2017-10-11 16:02:39 +0100 | [diff] [blame] | 132 | NLOGE("Failed to clear device status"); |
| 133 | return -1; |
| 134 | } |
| 135 | |
| 136 | return 0; |
Andrew Scull | 1c021e8 | 2017-09-20 17:04:55 +0100 | [diff] [blame] | 137 | } |
| 138 | |
Andrew Scull | 1c021e8 | 2017-09-20 17:04:55 +0100 | [diff] [blame] | 139 | uint32_t nos_call_application(const struct nos_device *dev, |
Andrew Scull | 36ebf2d | 2017-10-10 11:25:21 +0100 | [diff] [blame] | 140 | uint8_t app_id, uint16_t params, |
| 141 | const uint8_t *args, uint32_t arg_len, |
| 142 | uint8_t *reply, uint32_t *reply_len) |
Andrew Scull | 1c021e8 | 2017-09-20 17:04:55 +0100 | [diff] [blame] | 143 | { |
Andrew Scull | 36ebf2d | 2017-10-10 11:25:21 +0100 | [diff] [blame] | 144 | uint32_t command; |
| 145 | uint8_t buf[MAX_DEVICE_TRANSFER]; |
| 146 | uint32_t status; |
| 147 | uint16_t ulen; |
| 148 | uint32_t poll_count = 0; |
Andrew Scull | 1c021e8 | 2017-09-20 17:04:55 +0100 | [diff] [blame] | 149 | |
Anatol Pomazau | bc77563 | 2018-03-26 13:58:36 -0700 | [diff] [blame] | 150 | if (get_status(dev, app_id, &status, &ulen) != 0) { |
Andrew Scull | 3935b18 | 2017-10-11 16:02:39 +0100 | [diff] [blame] | 151 | return APP_ERROR_IO; |
| 152 | } |
Andrew Scull | 36ebf2d | 2017-10-10 11:25:21 +0100 | [diff] [blame] | 153 | NLOGV("%d: query status 0x%08x ulen 0x%04x", __LINE__, status, ulen); |
Andrew Scull | 1c021e8 | 2017-09-20 17:04:55 +0100 | [diff] [blame] | 154 | |
Andrew Scull | 36ebf2d | 2017-10-10 11:25:21 +0100 | [diff] [blame] | 155 | /* It's not idle, but we're the only ones telling it what to do, so it |
| 156 | * should be. */ |
| 157 | if (status != APP_STATUS_IDLE) { |
Andrew Scull | 1c021e8 | 2017-09-20 17:04:55 +0100 | [diff] [blame] | 158 | |
Andrew Scull | 36ebf2d | 2017-10-10 11:25:21 +0100 | [diff] [blame] | 159 | /* Try clearing the status */ |
| 160 | NLOGV("clearing previous status"); |
Andrew Scull | 3935b18 | 2017-10-11 16:02:39 +0100 | [diff] [blame] | 161 | if (clear_status(dev, app_id) != 0) { |
| 162 | return APP_ERROR_IO; |
| 163 | } |
Andrew Scull | 1c021e8 | 2017-09-20 17:04:55 +0100 | [diff] [blame] | 164 | |
Andrew Scull | 36ebf2d | 2017-10-10 11:25:21 +0100 | [diff] [blame] | 165 | /* Check again */ |
Andrew Scull | 3935b18 | 2017-10-11 16:02:39 +0100 | [diff] [blame] | 166 | if (get_status(dev, app_id, &status, &ulen) != 0) { |
| 167 | return APP_ERROR_IO; |
| 168 | } |
Bill Richardson | 08c561d | 2018-03-06 13:13:16 -0800 | [diff] [blame] | 169 | NLOGV("%d: query status 0x%08x ulen 0x%04x",__LINE__, status, ulen); |
Andrew Scull | 1c021e8 | 2017-09-20 17:04:55 +0100 | [diff] [blame] | 170 | |
Andrew Scull | 36ebf2d | 2017-10-10 11:25:21 +0100 | [diff] [blame] | 171 | /* It's ignoring us and is still not ready, so it's broken */ |
Andrew Scull | 3935b18 | 2017-10-11 16:02:39 +0100 | [diff] [blame] | 172 | if (status != APP_STATUS_IDLE) { |
| 173 | NLOGE("Device is not responding"); |
| 174 | return APP_ERROR_IO; |
| 175 | } |
Andrew Scull | 36ebf2d | 2017-10-10 11:25:21 +0100 | [diff] [blame] | 176 | } |
Andrew Scull | 1c021e8 | 2017-09-20 17:04:55 +0100 | [diff] [blame] | 177 | |
Andrew Scull | 36ebf2d | 2017-10-10 11:25:21 +0100 | [diff] [blame] | 178 | /* Send args data */ |
| 179 | command = CMD_ID(app_id) | CMD_TRANSPORT | CMD_IS_DATA; |
| 180 | do { |
| 181 | /* |
| 182 | * We can't send more than the device can take. For |
| 183 | * Citadel using the TPM Wait protocol on SPS, this is |
| 184 | * a constant. For other buses, it may not be. |
| 185 | * |
| 186 | * For each Write, Citadel requires that we send the length of |
| 187 | * what we're about to send in the params field. |
| 188 | */ |
| 189 | ulen = MIN(arg_len, MAX_DEVICE_TRANSFER); |
| 190 | CMD_SET_PARAM(command, ulen); |
| 191 | if (args && ulen) |
| 192 | memcpy(buf, args, ulen); |
Andrew Scull | 1c021e8 | 2017-09-20 17:04:55 +0100 | [diff] [blame] | 193 | |
Bill Richardson | 08c561d | 2018-03-06 13:13:16 -0800 | [diff] [blame] | 194 | NLOGV("Write command 0x%08x, bytes 0x%x", command, ulen); |
Andrew Scull | 1c021e8 | 2017-09-20 17:04:55 +0100 | [diff] [blame] | 195 | |
Anatol Pomazau | 0ee610c | 2018-03-28 13:09:47 -0700 | [diff] [blame^] | 196 | if (0 != nos_device_write(dev, command, buf, ulen)) { |
Andrew Scull | 3935b18 | 2017-10-11 16:02:39 +0100 | [diff] [blame] | 197 | NLOGE("Failed to send datagram to device"); |
| 198 | return APP_ERROR_IO; |
| 199 | } |
Andrew Scull | 1c021e8 | 2017-09-20 17:04:55 +0100 | [diff] [blame] | 200 | |
Andrew Scull | 36ebf2d | 2017-10-10 11:25:21 +0100 | [diff] [blame] | 201 | /* Additional data needs the additional flag set */ |
| 202 | command |= CMD_MORE_TO_COME; |
Andrew Scull | 1c021e8 | 2017-09-20 17:04:55 +0100 | [diff] [blame] | 203 | |
Andrew Scull | 36ebf2d | 2017-10-10 11:25:21 +0100 | [diff] [blame] | 204 | if (args) |
| 205 | args += ulen; |
| 206 | if (arg_len) |
| 207 | arg_len -= ulen; |
| 208 | } while (arg_len); |
Andrew Scull | 1c021e8 | 2017-09-20 17:04:55 +0100 | [diff] [blame] | 209 | |
Andrew Scull | 36ebf2d | 2017-10-10 11:25:21 +0100 | [diff] [blame] | 210 | /* See if we had any errors while sending the args */ |
Andrew Scull | 3935b18 | 2017-10-11 16:02:39 +0100 | [diff] [blame] | 211 | if (get_status(dev, app_id, &status, &ulen) != 0) { |
| 212 | return APP_ERROR_IO; |
| 213 | } |
Bill Richardson | 08c561d | 2018-03-06 13:13:16 -0800 | [diff] [blame] | 214 | NLOGV("%d: query status 0x%08x ulen 0x%04x", __LINE__, status, ulen); |
Andrew Scull | 36ebf2d | 2017-10-10 11:25:21 +0100 | [diff] [blame] | 215 | if (status & APP_STATUS_DONE) |
| 216 | /* Yep, problems. It should still be idle. */ |
| 217 | goto reply; |
Andrew Scull | 1c021e8 | 2017-09-20 17:04:55 +0100 | [diff] [blame] | 218 | |
Andrew Scull | 36ebf2d | 2017-10-10 11:25:21 +0100 | [diff] [blame] | 219 | /* Now tell the app to do whatever */ |
| 220 | command = CMD_ID(app_id) | CMD_PARAM(params); |
Bill Richardson | 08c561d | 2018-03-06 13:13:16 -0800 | [diff] [blame] | 221 | NLOGV("Write command 0x%08x", command); |
Anatol Pomazau | 0ee610c | 2018-03-28 13:09:47 -0700 | [diff] [blame^] | 222 | if (0 != nos_device_write(dev, command, 0, 0)) { |
Andrew Scull | 3935b18 | 2017-10-11 16:02:39 +0100 | [diff] [blame] | 223 | NLOGE("Failed to send command datagram to device"); |
| 224 | return APP_ERROR_IO; |
| 225 | } |
Andrew Scull | 1c021e8 | 2017-09-20 17:04:55 +0100 | [diff] [blame] | 226 | |
Andrew Scull | 36ebf2d | 2017-10-10 11:25:21 +0100 | [diff] [blame] | 227 | /* Poll the app status until it's done */ |
| 228 | do { |
Andrew Scull | 3935b18 | 2017-10-11 16:02:39 +0100 | [diff] [blame] | 229 | if (get_status(dev, app_id, &status, &ulen) != 0) { |
| 230 | return APP_ERROR_IO; |
| 231 | } |
Bill Richardson | 08c561d | 2018-03-06 13:13:16 -0800 | [diff] [blame] | 232 | NLOGD("%d: poll status 0x%08x ulen 0x%04x", __LINE__, status, ulen); |
Andrew Scull | 36ebf2d | 2017-10-10 11:25:21 +0100 | [diff] [blame] | 233 | poll_count++; |
| 234 | } while (!(status & APP_STATUS_DONE)); |
Bill Richardson | 08c561d | 2018-03-06 13:13:16 -0800 | [diff] [blame] | 235 | NLOGV("polled %d times, status 0x%08x ulen 0x%04x", poll_count, |
Andrew Scull | 36ebf2d | 2017-10-10 11:25:21 +0100 | [diff] [blame] | 236 | status, ulen); |
Andrew Scull | 1c021e8 | 2017-09-20 17:04:55 +0100 | [diff] [blame] | 237 | |
| 238 | reply: |
Andrew Scull | 36ebf2d | 2017-10-10 11:25:21 +0100 | [diff] [blame] | 239 | /* Read any result only if there's a place with room to put it */ |
| 240 | if (reply && reply_len && *reply_len) { |
| 241 | uint16_t left = MIN(*reply_len, ulen); |
| 242 | uint16_t gimme, got; |
Andrew Scull | 1c021e8 | 2017-09-20 17:04:55 +0100 | [diff] [blame] | 243 | |
Andrew Scull | 36ebf2d | 2017-10-10 11:25:21 +0100 | [diff] [blame] | 244 | command = CMD_ID(app_id) | CMD_IS_READ | |
| 245 | CMD_TRANSPORT | CMD_IS_DATA; |
Andrew Scull | 1c021e8 | 2017-09-20 17:04:55 +0100 | [diff] [blame] | 246 | |
Andrew Scull | 36ebf2d | 2017-10-10 11:25:21 +0100 | [diff] [blame] | 247 | got = 0 ; |
| 248 | while (left) { |
Andrew Scull | 1c021e8 | 2017-09-20 17:04:55 +0100 | [diff] [blame] | 249 | |
Andrew Scull | 36ebf2d | 2017-10-10 11:25:21 +0100 | [diff] [blame] | 250 | /* |
| 251 | * We can't read more than the device can send. For |
| 252 | * Citadel using the TPM Wait protocol on SPS, this is |
| 253 | * a constant. For other buses, it may not be. |
| 254 | */ |
| 255 | gimme = MIN(left, MAX_DEVICE_TRANSFER); |
Bill Richardson | 08c561d | 2018-03-06 13:13:16 -0800 | [diff] [blame] | 256 | NLOGV("Read command 0x%08x, bytes 0x%x", command, gimme); |
Anatol Pomazau | 0ee610c | 2018-03-28 13:09:47 -0700 | [diff] [blame^] | 257 | if (0 != nos_device_read(dev, command, buf, gimme)) { |
Andrew Scull | 3935b18 | 2017-10-11 16:02:39 +0100 | [diff] [blame] | 258 | NLOGE("Failed to receive datagram from device"); |
| 259 | return APP_ERROR_IO; |
| 260 | } |
Andrew Scull | 1c021e8 | 2017-09-20 17:04:55 +0100 | [diff] [blame] | 261 | |
Andrew Scull | 36ebf2d | 2017-10-10 11:25:21 +0100 | [diff] [blame] | 262 | memcpy(reply, buf, gimme); |
| 263 | reply += gimme; |
| 264 | left -= gimme; |
| 265 | got += gimme; |
| 266 | } |
| 267 | /* got it all */ |
| 268 | *reply_len = got; |
| 269 | } |
Andrew Scull | 1c021e8 | 2017-09-20 17:04:55 +0100 | [diff] [blame] | 270 | |
Andrew Scull | 36ebf2d | 2017-10-10 11:25:21 +0100 | [diff] [blame] | 271 | /* Clear the reply manually for the next caller */ |
| 272 | command = CMD_ID(app_id) | CMD_TRANSPORT; |
Anatol Pomazau | 0ee610c | 2018-03-28 13:09:47 -0700 | [diff] [blame^] | 273 | if (0 != nos_device_write(dev, command, 0, 0)) { |
Andrew Scull | 3935b18 | 2017-10-11 16:02:39 +0100 | [diff] [blame] | 274 | NLOGE("Failed to clear the reply"); |
| 275 | return APP_ERROR_IO; |
| 276 | } |
Andrew Scull | 1c021e8 | 2017-09-20 17:04:55 +0100 | [diff] [blame] | 277 | |
Andrew Scull | 36ebf2d | 2017-10-10 11:25:21 +0100 | [diff] [blame] | 278 | return APP_STATUS_CODE(status); |
Andrew Scull | 1c021e8 | 2017-09-20 17:04:55 +0100 | [diff] [blame] | 279 | } |