Home | History | Annotate | Download | only in hal
      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 #include <errno.h>
     18 #include <string.h>
     19 #include <sys/socket.h>
     20 #include <sys/types.h>
     21 #include <unistd.h>
     22 
     23 #include <android-base/logging.h>
     24 #include <cutils/sockets.h>
     25 
     26 #include <nvram/hal/nvram_device_adapter.h>
     27 #include <nvram/messages/nvram_messages.h>
     28 
     29 namespace {
     30 
     31 constexpr char kFakeNvramControlSocketName[] = "nvram";
     32 
     33 // This instantiates an |NvramManager| with the storage interface wired up with
     34 // an in-memory implementation. This *DOES NOT* meet the persistence and tamper
     35 // evidence requirements of the HAL, but is useful for demonstration and running
     36 // tests against the |NvramManager| implementation.
     37 class TestingNvramImplementation : public nvram::NvramImplementation {
     38  public:
     39   ~TestingNvramImplementation() override;
     40 
     41   void Execute(const nvram::Request& request,
     42                nvram::Response* response) override;
     43 
     44  private:
     45   // Connects the fake NVRAM control socket it it is not open already. Returns
     46   // true if the channel is open, false on errors.
     47   bool Connect();
     48 
     49   // Sends a request to the fake NVRAM daemon. Returns true if successful, false
     50   // on any I/O errors.
     51   bool SendRequest(const nvram::Request& request, nvram::Response* response);
     52 
     53   // A file descriptor of the socket connected to the fake NVRAM daemon.
     54   int nvram_socket_fd_ = -1;
     55 
     56   // The command buffer, used for encoding request and decoding responses.
     57   uint8_t command_buffer_[4096];
     58 };
     59 
     60 TestingNvramImplementation::~TestingNvramImplementation() {
     61   if (nvram_socket_fd_ != -1) {
     62     // No need to handle EINTR specially here as bionic filters it out.
     63     if (close(nvram_socket_fd_)) {
     64       PLOG(ERROR) << "Failed to close NVRAM command socket";
     65     }
     66     nvram_socket_fd_ = -1;
     67   }
     68 }
     69 
     70 void TestingNvramImplementation::Execute(const nvram::Request& request,
     71                                          nvram::Response* response) {
     72   if (!SendRequest(request, response)) {
     73     response->result = NV_RESULT_INTERNAL_ERROR;
     74   }
     75 }
     76 
     77 bool TestingNvramImplementation::Connect() {
     78   if (nvram_socket_fd_ != -1) {
     79     return true;
     80   }
     81 
     82   int rc =
     83       socket_local_client(kFakeNvramControlSocketName,
     84                           ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_SEQPACKET);
     85   if (rc < 0) {
     86     PLOG(ERROR) << "Failed to connect fake NVRAM control socket";
     87     return false;
     88   }
     89 
     90   nvram_socket_fd_ = rc;
     91   return true;
     92 }
     93 
     94 bool TestingNvramImplementation::SendRequest(const nvram::Request& request,
     95                                              nvram::Response* response) {
     96   if (!Connect()) {
     97     return false;
     98   }
     99 
    100   size_t request_size = sizeof(command_buffer_);
    101   if (!nvram::Encode(request, command_buffer_, &request_size)) {
    102     LOG(ERROR) << "Failed to encode NVRAM request.";
    103     return false;
    104   }
    105 
    106   ssize_t rc = TEMP_FAILURE_RETRY(
    107       write(nvram_socket_fd_, command_buffer_, request_size));
    108   if (rc < 0) {
    109     PLOG(ERROR) << "Failed to send request on NVRAM control socket";
    110     return false;
    111   }
    112 
    113   rc = TEMP_FAILURE_RETRY(
    114       read(nvram_socket_fd_, command_buffer_, sizeof(command_buffer_)));
    115   if (rc < 0 || static_cast<size_t>(rc) > sizeof(command_buffer_)) {
    116     PLOG(ERROR) << "Failed to read NVRAM response";
    117     return false;
    118   }
    119 
    120   if (!nvram::Decode(command_buffer_, static_cast<size_t>(rc), response)) {
    121     LOG(ERROR) << "Failed to decode NVRAM response.";
    122     return false;
    123   }
    124 
    125   return true;
    126 }
    127 
    128 }  // namespace
    129 
    130 extern "C" int testing_nvram_open(const hw_module_t* module,
    131                                   const char* device_id,
    132                                   hw_device_t** device_ptr) {
    133   if (strcmp(NVRAM_HARDWARE_DEVICE_ID, device_id) != 0) {
    134     return -EINVAL;
    135   }
    136 
    137   nvram::NvramDeviceAdapter* adapter =
    138       new nvram::NvramDeviceAdapter(module, new TestingNvramImplementation);
    139   *device_ptr = adapter->as_device();
    140   return 0;
    141 }
    142