-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhammerd_api.cc
157 lines (143 loc) · 5.87 KB
/
hammerd_api.cc
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
// Copyright 2017 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "hammerd/hammerd_api.h"
#include <memory>
#include <string>
#include <chromeos/dbus/service_constants.h>
#include "hammerd/dbus_wrapper.h"
#include "hammerd/usb_utils.h"
// Because it returns std::string, which is not compatible with C, we move
// outside the extern "C" scope.
std::string ToString(const ByteString* s) {
return std::string(s->ptr, s->size);
}
extern "C" {
using hammerd::FirmwareUpdater;
using hammerd::SectionName;
using hammerd::UpdateExtraCommand;
using hammerd::UsbEndpoint;
BRILLO_EXPORT FirmwareUpdater* FirmwareUpdater_New(uint16_t vendor_id,
uint16_t product_id,
const char* path) {
return new FirmwareUpdater(
std::make_unique<UsbEndpoint>(vendor_id, product_id, std::string(path)));
}
BRILLO_EXPORT bool FirmwareUpdater_LoadEcImage(FirmwareUpdater* updater,
const ByteString* ec_image) {
return updater->LoadEcImage(ToString(ec_image));
}
BRILLO_EXPORT bool FirmwareUpdater_LoadTouchpadImage(
FirmwareUpdater* updater, const ByteString* touchpad_image) {
return updater->LoadTouchpadImage(ToString(touchpad_image));
}
BRILLO_EXPORT UsbConnectStatus
FirmwareUpdater_TryConnectUsb(FirmwareUpdater* updater) {
return updater->TryConnectUsb();
}
BRILLO_EXPORT void FirmwareUpdater_CloseUsb(FirmwareUpdater* updater) {
updater->CloseUsb();
}
BRILLO_EXPORT bool FirmwareUpdater_SendFirstPdu(FirmwareUpdater* updater) {
return updater->SendFirstPdu();
}
BRILLO_EXPORT void FirmwareUpdater_SendDone(FirmwareUpdater* updater) {
return updater->SendDone();
}
BRILLO_EXPORT bool FirmwareUpdater_InjectEntropy(FirmwareUpdater* updater) {
return updater->InjectEntropy();
}
BRILLO_EXPORT bool FirmwareUpdater_InjectEntropyWithPayload(
FirmwareUpdater* updater, const ByteString* payload) {
return updater->InjectEntropyWithPayload(ToString(payload));
}
BRILLO_EXPORT bool FirmwareUpdater_SendSubcommand(
FirmwareUpdater* updater, UpdateExtraCommand subcommand) {
return updater->SendSubcommand(subcommand);
}
BRILLO_EXPORT bool FirmwareUpdater_SendSubcommandWithPayload(
FirmwareUpdater* updater,
UpdateExtraCommand subcommand,
const ByteString* cmd_body) {
return updater->SendSubcommandWithPayload(subcommand, ToString(cmd_body));
}
BRILLO_EXPORT bool FirmwareUpdater_SendSubcommandReceiveResponse(
FirmwareUpdater* updater,
UpdateExtraCommand subcommand,
const ByteString* cmd_body,
void* resp,
size_t resp_size) {
return updater->SendSubcommandReceiveResponse(subcommand, ToString(cmd_body),
resp, resp_size);
}
BRILLO_EXPORT bool FirmwareUpdater_TransferImage(FirmwareUpdater* updater,
SectionName section_name) {
return updater->TransferImage(section_name);
}
BRILLO_EXPORT bool FirmwareUpdater_TransferTouchpadFirmware(
FirmwareUpdater* updater, uint32_t section_addr, size_t data_len) {
return updater->TransferTouchpadFirmware(section_addr, data_len);
}
BRILLO_EXPORT SectionName
FirmwareUpdater_CurrentSection(FirmwareUpdater* updater) {
return updater->CurrentSection();
}
BRILLO_EXPORT bool FirmwareUpdater_ValidKey(FirmwareUpdater* updater) {
return updater->ValidKey();
}
BRILLO_EXPORT int FirmwareUpdater_CompareRollback(FirmwareUpdater* updater) {
return updater->CompareRollback();
}
BRILLO_EXPORT bool FirmwareUpdater_VersionMismatch(FirmwareUpdater* updater,
SectionName section_name) {
return updater->VersionMismatch(section_name);
}
BRILLO_EXPORT bool FirmwareUpdater_IsSectionLocked(FirmwareUpdater* updater,
SectionName section_name) {
return updater->IsSectionLocked(section_name);
}
BRILLO_EXPORT bool FirmwareUpdater_UnlockRW(FirmwareUpdater* updater) {
return updater->UnlockRW();
}
BRILLO_EXPORT bool FirmwareUpdater_IsRollbackLocked(FirmwareUpdater* updater) {
return updater->IsRollbackLocked();
}
BRILLO_EXPORT bool FirmwareUpdater_UnlockRollback(FirmwareUpdater* updater) {
return updater->UnlockRollback();
}
BRILLO_EXPORT const FirstResponsePdu* FirmwareUpdater_GetFirstResponsePdu(
FirmwareUpdater* updater) {
return updater->GetFirstResponsePdu();
}
BRILLO_EXPORT const char* FirmwareUpdater_GetSectionVersion(
FirmwareUpdater* updater, SectionName section_name) {
// To avoid the string being freed from memory after exiting the function, we
// store as a static variable here. However, the return string should be
// copied before calling the function again (Python ctypes does this).
static std::string version = updater->GetSectionVersion(section_name);
return version.c_str();
}
BRILLO_EXPORT PairManager* PairManager_New() {
return new PairManager();
}
BRILLO_EXPORT int PairManager_PairChallenge(PairManager* self,
FirmwareUpdater* fw_updater,
uint8_t* public_key) {
hammerd::ChallengeStatus ret;
// We do not allow to send DBus signal from Hammerd API. Inject a dummy
// DBus wrapper here.
hammerd::DummyDBusWrapper dummy;
ret = self->PairChallenge(fw_updater, &dummy);
if (ret != ChallengeStatus::kChallengePassed)
return static_cast<int>(ret);
if (dummy.GetLastSignalName() != hammerd::kPairChallengeSucceededSignal)
return static_cast<int>(ChallengeStatus::kUnknownError);
if (public_key) {
std::string last_value = dummy.GetLastValue();
if (last_value.size() != kX25519PublicValueLen)
return static_cast<int>(ChallengeStatus::kUnknownError);
memcpy(public_key, last_value.c_str(), last_value.size());
}
return static_cast<int>(ret);
}
} // extern "C"