5 #include "sw/device/silicon_creator/lib/ownership/ownership_unlock.h"
9 #include "sw/device/silicon_creator/lib/boot_data.h"
10 #include "sw/device/silicon_creator/lib/boot_svc/boot_svc_msg.h"
11 #include "sw/device/silicon_creator/lib/drivers/hmac.h"
12 #include "sw/device/silicon_creator/lib/drivers/lifecycle.h"
13 #include "sw/device/silicon_creator/lib/error.h"
14 #include "sw/device/silicon_creator/lib/ownership/owner_block.h"
15 #include "sw/device/silicon_creator/lib/ownership/ownership_key.h"
18 if (ownership_state == kOwnershipStateLockedOwner ||
19 ownership_state == kOwnershipStateUnlockedSelf ||
20 ownership_state == kOwnershipStateUnlockedAny ||
21 ownership_state == kOwnershipStateUnlockedEndorsed) {
29 if (!nonce_equal(&msg->ownership_unlock_req.nonce, &bootdata->
nonce)) {
30 return kErrorOwnershipInvalidNonce;
35 lifecycle_device_id_get(&device_id);
36 if (lifecycle_din_eq(&device_id, msg->ownership_unlock_req.din) !=
38 return kErrorOwnershipInvalidDin;
41 if (msg->ownership_unlock_req.unlock_mode == kBootSvcUnlockEndorsed) {
43 hmac_sha256(&msg->ownership_unlock_req.next_owner_key,
44 sizeof(msg->ownership_unlock_req.next_owner_key), &digest);
47 }
else if (msg->ownership_unlock_req.unlock_mode == kBootSvcUnlockAny) {
49 }
else if (msg->ownership_unlock_req.unlock_mode == kBootSvcUnlockUpdate) {
52 return kErrorOwnershipInvalidMode;
54 nonce_new(&bootdata->
nonce);
55 return kErrorWriteBootdataThenReboot;
59 size_t len = (uintptr_t)&msg->ownership_unlock_req.signature -
60 (uintptr_t)&msg->ownership_unlock_req.unlock_mode;
62 switch (owner_page[0].update_mode) {
63 case kOwnershipUpdateModeOpen:
66 case kOwnershipUpdateModeNewVersion:
68 return kErrorOwnershipUnlockDenied;
69 case kOwnershipUpdateModeSelf:
70 case kOwnershipUpdateModeSelfVersion:
74 return kErrorOwnershipInvalidMode;
79 if (ownership_key_validate(
80 0, kOwnershipKeyUnlock | kOwnershipKeyRecovery,
81 &msg->ownership_unlock_req.signature,
82 &msg->ownership_unlock_req.unlock_mode,
84 return kErrorOwnershipInvalidSignature;
86 return do_unlock(msg, bootdata);
90 if (ownership_key_validate(0, kOwnershipKeyRecovery,
91 &msg->ownership_unlock_req.signature,
92 &msg->ownership_unlock_req.unlock_mode,
94 return kErrorOwnershipInvalidSignature;
96 return do_unlock(msg, bootdata);
98 return kErrorOwnershipInvalidState;
103 size_t len = (uintptr_t)&msg->ownership_unlock_req.signature -
104 (uintptr_t)&msg->ownership_unlock_req.unlock_mode;
106 switch (owner_page[0].update_mode) {
107 case kOwnershipUpdateModeNewVersion:
109 return kErrorOwnershipUnlockDenied;
110 case kOwnershipUpdateModeSelf:
111 case kOwnershipUpdateModeSelfVersion:
112 case kOwnershipUpdateModeOpen:
119 if (ownership_key_validate(0, kOwnershipKeyUnlock,
120 &msg->ownership_unlock_req.signature,
121 &msg->ownership_unlock_req.unlock_mode,
123 return kErrorOwnershipInvalidSignature;
125 return do_unlock(msg, bootdata);
127 return kErrorOwnershipInvalidState;
131 size_t len = (uintptr_t)&msg->ownership_unlock_req.signature -
132 (uintptr_t)&msg->ownership_unlock_req.unlock_mode;
137 if (ownership_key_validate(0, kOwnershipKeyUnlock,
138 &msg->ownership_unlock_req.signature,
139 &msg->ownership_unlock_req.unlock_mode,
141 return kErrorOwnershipInvalidSignature;
143 if (!nonce_equal(&msg->ownership_unlock_req.nonce, &bootdata->
nonce)) {
144 return kErrorOwnershipInvalidNonce;
149 lifecycle_device_id_get(&device_id);
150 if (lifecycle_din_eq(&device_id, msg->ownership_unlock_req.din) !=
152 return kErrorOwnershipInvalidDin;
156 nonce_new(&bootdata->
nonce);
157 return kErrorWriteBootdataThenReboot;
159 return kErrorOwnershipInvalidState;
164 rom_error_t error = kErrorOwnershipInvalidRequest;
165 switch (msg->ownership_unlock_req.unlock_mode) {
166 case kBootSvcUnlockAny:
167 error = unlock(msg, bootdata);
169 case kBootSvcUnlockEndorsed:
170 error = unlock(msg, bootdata);
172 case kBootSvcUnlockUpdate:
173 error = unlock_update(msg, bootdata);
175 case kBootSvcUnlockAbort:
176 error = unlock_abort(msg, bootdata);
181 boot_svc_ownership_unlock_res_init(
182 error == kErrorWriteBootdataThenReboot ? kErrorOk : error,
183 &msg->ownership_unlock_res);