1 /*
2  * Copyright (C) 2023 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 "guest/hals/oemlock/remote/remote_oemlock.h"
18 
19 #include <android-base/logging.h>
20 
21 namespace aidl {
22 namespace android {
23 namespace hardware {
24 namespace oemlock {
25 namespace {
26 
27 enum {
28     CUSTOM_ERROR_TRANSPORT_IS_FAILED = 0,
29 };
30 
resultToStatus(Result<void> r)31 ::ndk::ScopedAStatus resultToStatus(Result<void> r) {
32     if (r.ok()) {
33         return ::ndk::ScopedAStatus::ok();
34     } else {
35         return ::ndk::ScopedAStatus::fromServiceSpecificErrorWithMessage(
36                 CUSTOM_ERROR_TRANSPORT_IS_FAILED, r.error().Message().c_str());
37     }
38 }
39 
40 }
41 
OemLock(transport::Channel & channel)42 OemLock::OemLock(transport::Channel& channel) : channel_(channel) {}
43 
getName(std::string * out_name)44 ::ndk::ScopedAStatus OemLock::getName(std::string *out_name) {
45     *out_name = "CF Remote Implementation";
46     return ::ndk::ScopedAStatus::ok();
47 }
48 
setOemUnlockAllowedByCarrier(bool in_allowed,const std::vector<uint8_t> &,OemLockSecureStatus * _aidl_return)49 ::ndk::ScopedAStatus OemLock::setOemUnlockAllowedByCarrier(bool in_allowed,
50                                                            const std::vector<uint8_t> &,
51                                                            OemLockSecureStatus *_aidl_return) {
52     *_aidl_return = OemLockSecureStatus::OK;
53     return resultToStatus(setValue(secure_env::OemLockField::ALLOWED_BY_CARRIER, in_allowed));
54 }
55 
isOemUnlockAllowedByCarrier(bool * out_allowed)56 ::ndk::ScopedAStatus OemLock::isOemUnlockAllowedByCarrier(bool *out_allowed) {
57     return resultToStatus(requestValue(secure_env::OemLockField::ALLOWED_BY_CARRIER, out_allowed));
58 }
59 
setOemUnlockAllowedByDevice(bool in_allowed)60 ::ndk::ScopedAStatus OemLock::setOemUnlockAllowedByDevice(bool in_allowed) {
61     return resultToStatus(setValue(secure_env::OemLockField::ALLOWED_BY_DEVICE, in_allowed));
62 }
63 
isOemUnlockAllowedByDevice(bool * out_allowed)64 ::ndk::ScopedAStatus OemLock::isOemUnlockAllowedByDevice(bool *out_allowed) {
65     return resultToStatus(requestValue(secure_env::OemLockField::ALLOWED_BY_DEVICE, out_allowed));
66 }
67 
requestValue(secure_env::OemLockField field,bool * out)68 Result<void> OemLock::requestValue(secure_env::OemLockField field, bool *out) {
69     auto message = CF_EXPECT(transport::CreateMessage(static_cast<uint32_t>(field), 0),
70                              "Cannot allocate message for oemlock request: " <<
71                              static_cast<uint32_t>(field));
72     CF_EXPECT(channel_.SendRequest(*message),
73               "Can't send get value request for field: " << static_cast<uint32_t>(field));
74     auto response = CF_EXPECT(channel_.ReceiveMessage(),
75                               "Haven't received an answer for getting the field: " <<
76                               static_cast<uint32_t>(field));
77     *out = *reinterpret_cast<bool*>(response->payload);
78     return {};
79 }
80 
setValue(secure_env::OemLockField field,bool value)81 Result<void> OemLock::setValue(secure_env::OemLockField field, bool value) {
82     auto message = CF_EXPECT(transport::CreateMessage(static_cast<uint32_t>(field), sizeof(bool)),
83                              "Cannot allocate message for oemlock request: " <<
84                              static_cast<uint32_t>(field));
85     memcpy(message->payload, &value, sizeof(bool));
86     CF_EXPECT(channel_.SendRequest(*message),
87               "Can't send set value request for field: " << static_cast<uint32_t>(field));
88     auto response = CF_EXPECT(channel_.ReceiveMessage(),
89                               "Haven't received an answer for setting the field: " <<
90                               static_cast<uint32_t>(field));
91     auto updated_value = *reinterpret_cast<bool*>(response->payload);
92     CF_EXPECT(value == updated_value,
93               "Updated value for the field " << static_cast<uint32_t>(field) <<
94               " is different from what we wated to set");
95     return {};
96 }
97 
98 } // namespace oemlock
99 } // namespace hardware
100 } // namespace android
101 } // aidl
102