1/* 2 * Copyright (C) 2016 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#define LOG_TAG "TrustyNVRAM" 18 19#include "trusty_nvram_implementation.h" 20 21#include <errno.h> 22#include <string.h> 23#include <unistd.h> 24 25#include <hardware/nvram.h> 26#include <log/log.h> 27#include <trusty/tipc.h> 28 29#include <nvram/messages/blob.h> 30 31namespace nvram { 32namespace { 33 34// Character device to open for Trusty IPC connections. 35const char kTrustyDeviceName[] = "/dev/trusty-ipc-dev0"; 36 37// App identifier of the NVRAM app. 38const char kTrustyNvramAppId[] = "com.android.trusty.nvram"; 39 40} // namespace 41 42TrustyNvramImplementation::~TrustyNvramImplementation() { 43 if (tipc_nvram_fd_ != -1) { 44 tipc_close(tipc_nvram_fd_); 45 tipc_nvram_fd_ = -1; 46 } 47} 48 49void TrustyNvramImplementation::Execute(const nvram::Request& request, 50 nvram::Response* response) { 51 if (!SendRequest(request, response)) { 52 response->result = NV_RESULT_INTERNAL_ERROR; 53 } 54} 55 56bool TrustyNvramImplementation::Connect() { 57 if (tipc_nvram_fd_ != -1) { 58 return true; 59 } 60 61 int rc = tipc_connect(kTrustyDeviceName, kTrustyNvramAppId); 62 if (rc < 0) { 63 ALOGE("Failed to connect to Trusty NVRAM app: %s\n", strerror(-rc)); 64 return false; 65 } 66 67 tipc_nvram_fd_ = rc; 68 return true; 69} 70 71bool TrustyNvramImplementation::SendRequest(const nvram::Request& request, 72 nvram::Response* response) { 73 if (!Connect()) { 74 return false; 75 } 76 77 nvram::Blob request_buffer; 78 if (!nvram::Encode(request, &request_buffer)) { 79 ALOGE("Failed to encode NVRAM request.\n"); 80 return false; 81 } 82 83 ssize_t rc = 84 write(tipc_nvram_fd_, request_buffer.data(), request_buffer.size()); 85 if (rc < 0) { 86 ALOGE("Failed to send NVRAM request: %s\n", strerror(-rc)); 87 return false; 88 } 89 if (static_cast<size_t>(rc) != request_buffer.size()) { 90 ALOGE("Failed to send full request buffer: %zd\n", rc); 91 return false; 92 } 93 94 rc = read(tipc_nvram_fd_, response_buffer_, sizeof(response_buffer_)); 95 if (rc < 0) { 96 ALOGE("Failed to read NVRAM response: %s\n", strerror(-rc)); 97 return false; 98 } 99 100 if (static_cast<size_t>(rc) >= sizeof(response_buffer_)) { 101 ALOGE("NVRAM response exceeds response buffer size.\n"); 102 return false; 103 } 104 105 if (!nvram::Decode(response_buffer_, static_cast<size_t>(rc), response)) { 106 ALOGE("Failed to decode NVRAM response.\n"); 107 return false; 108 } 109 110 return true; 111} 112 113} // namespace nvram 114