• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
OemLock(secure_env::Channel & channel)41 OemLock::OemLock(secure_env::Channel& channel) : channel_(channel) {}
42 
getName(std::string * out_name)43 ::ndk::ScopedAStatus OemLock::getName(std::string *out_name) {
44     *out_name = "CF Remote Implementation";
45     return ::ndk::ScopedAStatus::ok();
46 }
47 
setOemUnlockAllowedByCarrier(bool in_allowed,const std::vector<uint8_t> &,OemLockSecureStatus * _aidl_return)48 ::ndk::ScopedAStatus OemLock::setOemUnlockAllowedByCarrier(bool in_allowed,
49                                                            const std::vector<uint8_t> &,
50                                                            OemLockSecureStatus *_aidl_return) {
51     *_aidl_return = OemLockSecureStatus::OK;
52     return resultToStatus(setValue(secure_env::OemLockField::ALLOWED_BY_CARRIER, in_allowed));
53 }
54 
isOemUnlockAllowedByCarrier(bool * out_allowed)55 ::ndk::ScopedAStatus OemLock::isOemUnlockAllowedByCarrier(bool *out_allowed) {
56     return resultToStatus(requestValue(secure_env::OemLockField::ALLOWED_BY_CARRIER, out_allowed));
57 }
58 
setOemUnlockAllowedByDevice(bool in_allowed)59 ::ndk::ScopedAStatus OemLock::setOemUnlockAllowedByDevice(bool in_allowed) {
60     return resultToStatus(setValue(secure_env::OemLockField::ALLOWED_BY_DEVICE, in_allowed));
61 }
62 
isOemUnlockAllowedByDevice(bool * out_allowed)63 ::ndk::ScopedAStatus OemLock::isOemUnlockAllowedByDevice(bool *out_allowed) {
64     return resultToStatus(requestValue(secure_env::OemLockField::ALLOWED_BY_DEVICE, out_allowed));
65 }
66 
requestValue(secure_env::OemLockField field,bool * out)67 Result<void> OemLock::requestValue(secure_env::OemLockField field, bool *out) {
68     CF_EXPECT(channel_.SendRequest(static_cast<uint32_t>(field), nullptr, 0),
69               "Can't send get value request for field: " << static_cast<uint32_t>(field));
70     auto response = CF_EXPECT(channel_.ReceiveMessage(),
71                               "Haven't received an answer for getting the field: " <<
72                               static_cast<uint32_t>(field));
73     *out = *reinterpret_cast<bool*>(response->payload);
74     return {};
75 }
76 
setValue(secure_env::OemLockField field,bool value)77 Result<void> OemLock::setValue(secure_env::OemLockField field, bool value) {
78     CF_EXPECT(channel_.SendRequest(static_cast<uint32_t>(field), &value, sizeof(bool)),
79               "Can't send set value request for field: " << static_cast<uint32_t>(field));
80     auto response = CF_EXPECT(channel_.ReceiveMessage(),
81                               "Haven't received an answer for setting the field: " <<
82                               static_cast<uint32_t>(field));
83     auto updated_value = *reinterpret_cast<bool*>(response->payload);
84     CF_EXPECT(value == updated_value,
85               "Updated value for the field " << static_cast<uint32_t>(field) <<
86               " is different from what we wated to set");
87     return {};
88 }
89 
90 } // namespace oemlock
91 } // namespace hardware
92 } // namespace android
93 } // aidl
94