-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOemLock.cpp
More file actions
354 lines (287 loc) · 13.2 KB
/
Copy pathOemLock.cpp
File metadata and controls
354 lines (287 loc) · 13.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#include "OemLock.h"
#include <android-base/properties.h>
#include <android-base/logging.h>
#include <cstring>
namespace aidl {
namespace android {
namespace hardware {
namespace oemlock {
const char* OemLock::kCarrierUnlockProp = "ro.boot.carrier_unlock_allowed";
const char* OemLock::kDeviceUnlockProp = "persist.vendor.oemlock.device_unlock_allowed";
const char* OemLock::kUnlockAbilityProp = "fastboot.unlock_ability";
OemLock::OemLock() : mAllowedByCarrier(false), mAllowedByDevice(false), mTeeInitialized(false) {
// Initialize TEE context for TA AVB communication first
if (!initTeeContext()) {
LOG(WARNING) << "Failed to initialize TEE context, using fallback mode";
}
// Initialize from persistent properties
mAllowedByCarrier = isCarrierUnlockAllowed();
mAllowedByDevice = isDeviceUnlockAllowed();
// Verify consistency with TA AVB persist storage
uint32_t unlock_ability_value;
if (readPersistValue(UNLOCK_ABILITY_KEY, &unlock_ability_value)) {
bool combined_unlock_ability = (unlock_ability_value == 1);
bool expected_combined = mAllowedByCarrier && mAllowedByDevice;
if (combined_unlock_ability != expected_combined) {
LOG(INFO) << "Syncing unlock ability: TA AVB=" << unlock_ability_value
<< ", expected=" << (expected_combined ? "1" : "0")
<< " (carrier=" << (mAllowedByCarrier ? "true" : "false")
<< ", device=" << (mAllowedByDevice ? "true" : "false") << ")";
// Sync the TA AVB value with the current state
writePersistValue(UNLOCK_ABILITY_KEY, expected_combined ? 1 : 0);
}
} else {
// Initialize TA AVB with current state if not found
bool expected_combined = mAllowedByCarrier && mAllowedByDevice;
writePersistValue(UNLOCK_ABILITY_KEY, expected_combined ? 1 : 0);
LOG(INFO) << "Initialized TA AVB unlock ability to " << (expected_combined ? "1" : "0");
}
LOG(INFO) << "OPTEE OemLock HAL initialized (carrier="
<< (mAllowedByCarrier ? "true" : "false")
<< ", device=" << (mAllowedByDevice ? "true" : "false") << ")";
}
OemLock::~OemLock() {
cleanupTeeContext();
}
::ndk::ScopedAStatus OemLock::getName(std::string* out_name) {
*out_name = "OPTEE OemLock HAL";
return ::ndk::ScopedAStatus::ok();
}
::ndk::ScopedAStatus OemLock::setOemUnlockAllowedByCarrier(bool in_allowed, const std::vector<uint8_t>& in_signature, OemLockSecureStatus* _aidl_return) {
// For Optee implementation, we don't verify carrier signature
// In production, this should verify the carrier signature
LOG(INFO) << "setOemUnlockAllowedByCarrier: " << (in_allowed ? "true" : "false");
if (!setCarrierUnlockAllowed(in_allowed)) {
LOG(ERROR) << "Failed to set carrier unlock allowed";
*_aidl_return = OemLockSecureStatus::FAILED;
return ::ndk::ScopedAStatus::ok();
}
mAllowedByCarrier = in_allowed;
*_aidl_return = OemLockSecureStatus::OK;
return ::ndk::ScopedAStatus::ok();
}
::ndk::ScopedAStatus OemLock::isOemUnlockAllowedByCarrier(bool* out_allowed) {
*out_allowed = mAllowedByCarrier;
LOG(INFO) << "isOemUnlockAllowedByCarrier: " << (*out_allowed ? "true" : "false");
return ::ndk::ScopedAStatus::ok();
}
::ndk::ScopedAStatus OemLock::setOemUnlockAllowedByDevice(bool in_allowed) {
LOG(INFO) << "setOemUnlockAllowedByDevice: " << (in_allowed ? "true" : "false");
mAllowedByDevice = in_allowed;
// Calculate final unlock state
bool combined_allowed = mAllowedByCarrier && mAllowedByDevice;
// Write unlock ability directly to TA AVB persist storage
uint32_t unlock_ability_value = combined_allowed ? 1 : 0;
if (!writePersistValue(UNLOCK_ABILITY_KEY, unlock_ability_value)) {
LOG(ERROR) << "Failed to set unlock ability in TA AVB persist storage";
return ::ndk::ScopedAStatus::fromServiceSpecificError(-1);
}
// Also write to legacy lock state for backward compatibility
uint32_t lock_state = combined_allowed ? 0 : 1;
if (!invokeAvbCommand(TA_AVB_CMD_WRITE_LOCK_STATE, &lock_state, true)) {
LOG(WARNING) << "Failed to set legacy lock state in TA AVB";
// Don't fail here, persist value is the primary mechanism
}
// Persist in Android property for consistency
if (!setDeviceUnlockAllowed(in_allowed)) {
LOG(WARNING) << "Failed to persist device unlock property";
}
// Remove dependency on environment variable - U-Boot now reads from TA AVB
LOG(INFO) << "TA AVB unlock_ability set to: " << unlock_ability_value
<< " (combined: carrier=" << (mAllowedByCarrier ? "true" : "false")
<< ", device=" << (mAllowedByDevice ? "true" : "false") << ")";
return ::ndk::ScopedAStatus::ok();
}
::ndk::ScopedAStatus OemLock::isOemUnlockAllowedByDevice(bool* out_allowed) {
// Read unlock ability directly from TA AVB persist storage
uint32_t unlock_ability_value;
if (readPersistValue(UNLOCK_ABILITY_KEY, &unlock_ability_value)) {
// Extract device state from combined unlock ability
// Since unlock_ability = carrier_allowed && device_allowed
// we need to verify against our cached state
bool combined_unlock_ability = (unlock_ability_value == 1);
// For device unlock, return cached device state
// The persist value shows the combined result
*out_allowed = mAllowedByDevice;
LOG(INFO) << "TA AVB unlock_ability: " << unlock_ability_value
<< ", device_allowed: " << (*out_allowed ? "true" : "false")
<< ", carrier_allowed: " << (mAllowedByCarrier ? "true" : "false")
<< ", combined_result: " << (combined_unlock_ability ? "true" : "false");
// Verify consistency
bool expected_combined = mAllowedByCarrier && mAllowedByDevice;
if (combined_unlock_ability != expected_combined) {
LOG(WARNING) << "Inconsistency detected: persist value says "
<< (combined_unlock_ability ? "unlocked" : "locked")
<< " but local state suggests "
<< (expected_combined ? "unlocked" : "locked");
}
} else {
// Fallback on cached value
*out_allowed = mAllowedByDevice;
LOG(WARNING) << "Failed to read unlock ability from TA AVB, using cached value";
}
return ::ndk::ScopedAStatus::ok();
}
bool OemLock::isCarrierUnlockAllowed() {
return ::android::base::GetBoolProperty(kCarrierUnlockProp, true);
}
bool OemLock::isDeviceUnlockAllowed() {
return ::android::base::GetBoolProperty(kDeviceUnlockProp, false);
}
bool OemLock::setCarrierUnlockAllowed(bool allowed) {
// For Production implementation, carrier unlock property is read-only
// It should be set during manufacturing/provisioning
LOG(WARNING) << "Carrier unlock property is read-only";
return true;
}
bool OemLock::setDeviceUnlockAllowed(bool allowed) {
std::string value = allowed ? "true" : "false";
return ::android::base::SetProperty(kDeviceUnlockProp, value);
}
bool OemLock::getDeviceLockState(bool* locked) {
// Read device lock state from bootloader/TA AVB
// This implementation reads from a property that should be set
// by the bootloader based on TA AVB state
std::string lock_state = ::android::base::GetProperty("ro.boot.flash.locked", "1");
*locked = (lock_state == "1");
LOG(INFO) << "Device lock state: " << (*locked ? "LOCKED" : "UNLOCKED");
return true;
}
bool OemLock::setDeviceLockState(bool locked) {
// Device lock state is controlled by bootloader via fastboot commands
// This HAL doesn't directly control the device lock state
LOG(WARNING) << "Device lock state is controlled by bootloader";
return true;
}
bool OemLock::initTeeContext() {
if (mTeeInitialized) return true;
TEEC_Result result = TEEC_InitializeContext(nullptr, &mTeeContext);
if (result != TEEC_SUCCESS) {
LOG(ERROR) << "Failed to initialize TEE context: " << std::hex << result;
return false;
}
mTeeInitialized = true;
LOG(INFO) << "TEE context initialized successfully";
return true;
}
void OemLock::cleanupTeeContext() {
if (mTeeInitialized) {
TEEC_FinalizeContext(&mTeeContext);
mTeeInitialized = false;
LOG(INFO) << "TEE context finalized";
}
}
bool OemLock::invokeAvbCommand(uint32_t cmd, uint32_t *lock_state, bool write) {
if (!initTeeContext()) return false;
TEEC_Session session;
TEEC_Operation operation;
TEEC_Result result;
// Open session with TA AVB
memset(&operation, 0, sizeof(operation));
result = TEEC_OpenSession(&mTeeContext, &session, &TA_AVB_UUID,
TEEC_LOGIN_PUBLIC, nullptr, nullptr, nullptr);
if (result != TEEC_SUCCESS) {
LOG(ERROR) << "Failed to open TA AVB session: " << std::hex << result;
return false;
}
// Prepare operation
memset(&operation, 0, sizeof(operation));
operation.paramTypes = TEEC_PARAM_TYPES(
write ? TEEC_VALUE_INPUT : TEEC_VALUE_OUTPUT,
TEEC_NONE, TEEC_NONE, TEEC_NONE);
if (write) {
operation.params[0].value.a = *lock_state;
LOG(INFO) << "Writing lock_state " << *lock_state << " to TA AVB";
}
// Invoke TA command
result = TEEC_InvokeCommand(&session, cmd, &operation, nullptr);
if (result == TEEC_SUCCESS) {
if (!write) {
*lock_state = operation.params[0].value.a;
LOG(INFO) << "Read lock_state " << *lock_state << " from TA AVB";
}
LOG(INFO) << "TA AVB command " << cmd << " executed successfully";
} else {
LOG(ERROR) << "TA AVB command " << cmd << " failed: " << std::hex << result;
}
TEEC_CloseSession(&session);
return (result == TEEC_SUCCESS);
}
bool OemLock::readPersistValue(const char* key, uint32_t* value) {
if (!initTeeContext()) return false;
TEEC_Session session;
TEEC_Operation operation;
TEEC_Result result;
// Open session with TA AVB
memset(&operation, 0, sizeof(operation));
result = TEEC_OpenSession(&mTeeContext, &session, &TA_AVB_UUID,
TEEC_LOGIN_PUBLIC, nullptr, nullptr, nullptr);
if (result != TEEC_SUCCESS) {
LOG(ERROR) << "Failed to open TA AVB session: " << std::hex << result;
return false;
}
// Prepare operation for reading persist value
memset(&operation, 0, sizeof(operation));
operation.paramTypes = TEEC_PARAM_TYPES(
TEEC_MEMREF_TEMP_INPUT, // key name
TEEC_MEMREF_TEMP_INOUT, // value buffer
TEEC_NONE, TEEC_NONE);
// Set key name
operation.params[0].tmpref.buffer = (void*)key;
operation.params[0].tmpref.size = strlen(key);
// Set value buffer
operation.params[1].tmpref.buffer = value;
operation.params[1].tmpref.size = sizeof(*value);
// Invoke TA command
result = TEEC_InvokeCommand(&session, TA_AVB_CMD_READ_PERSIST_VALUE, &operation, nullptr);
if (result == TEEC_SUCCESS) {
LOG(INFO) << "Read persist value '" << key << "': " << *value;
} else if (result == TEEC_ERROR_ITEM_NOT_FOUND) {
LOG(INFO) << "Persist value '" << key << "' not found, using default";
*value = 0; // Default value
result = TEEC_SUCCESS;
} else {
LOG(ERROR) << "Failed to read persist value '" << key << "': " << std::hex << result;
}
TEEC_CloseSession(&session);
return (result == TEEC_SUCCESS);
}
bool OemLock::writePersistValue(const char* key, uint32_t value) {
if (!initTeeContext()) return false;
TEEC_Session session;
TEEC_Operation operation;
TEEC_Result result;
// Open session with TA AVB
memset(&operation, 0, sizeof(operation));
result = TEEC_OpenSession(&mTeeContext, &session, &TA_AVB_UUID,
TEEC_LOGIN_PUBLIC, nullptr, nullptr, nullptr);
if (result != TEEC_SUCCESS) {
LOG(ERROR) << "Failed to open TA AVB session: " << std::hex << result;
return false;
}
// Prepare operation for writing persist value
memset(&operation, 0, sizeof(operation));
operation.paramTypes = TEEC_PARAM_TYPES(
TEEC_MEMREF_TEMP_INPUT, // key name
TEEC_MEMREF_TEMP_INPUT, // value to write
TEEC_NONE, TEEC_NONE);
// Set key name
operation.params[0].tmpref.buffer = (void*)key;
operation.params[0].tmpref.size = strlen(key);
// Set value to write
operation.params[1].tmpref.buffer = &value;
operation.params[1].tmpref.size = sizeof(value);
// Invoke TA command
result = TEEC_InvokeCommand(&session, TA_AVB_CMD_WRITE_PERSIST_VALUE, &operation, nullptr);
if (result == TEEC_SUCCESS) {
LOG(INFO) << "Wrote persist value '" << key << "': " << value;
} else {
LOG(ERROR) << "Failed to write persist value '" << key << "': " << std::hex << result;
}
TEEC_CloseSession(&session);
return (result == TEEC_SUCCESS);
}
} // namespace oemlock
} // namespace hardware
} // namespace android
} // namespace aidl