This repository has been archived by the owner on Jul 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 171
/
firmwareUpdate-repair.ts
236 lines (212 loc) · 7.45 KB
/
firmwareUpdate-repair.ts
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
import { log } from "@ledgerhq/logs";
import { MCUNotGenuineToDashboard } from "@ledgerhq/errors";
import { Observable, from, of, EMPTY, concat, throwError } from "rxjs";
import {
concatMap,
delay,
filter,
map,
mergeMap,
throttleTime,
} from "rxjs/operators";
import semver from "semver";
import ManagerAPI from "../api/Manager";
import { withDevicePolling, withDevice } from "./deviceAccess";
import { getProviderId } from "../manager/provider";
import getDeviceInfo from "./getDeviceInfo";
import {
mcuOutdated,
mcuNotGenuine,
followDeviceRepair,
followDeviceUpdate,
} from "../deviceWordings";
import { DeviceVersion, FinalFirmware } from "../types/manager";
const wait2s = of({
type: "wait",
}).pipe(delay(2000));
export const repairChoices = [
{
id: "mcuOutdated",
label: mcuOutdated,
forceMCU: "0.7",
},
{
id: "mcuNotGenuine",
label: mcuNotGenuine,
forceMCU: "0.7",
},
{
id: "followDeviceRepair",
label: followDeviceRepair,
forceMCU: "0.9",
},
{
id: "followDeviceUpdate",
label: followDeviceUpdate,
forceMCU: "0.9",
},
];
const filterMCUForDeviceInfo = (deviceInfo) => {
const provider = getProviderId(deviceInfo);
return (mcu) => mcu.providers.includes(provider);
};
const repair = (
deviceId: string,
forceMCU_: string | null | undefined
): Observable<{
progress: number;
}> => {
log("hw", "firmwareUpdate-repair");
const mcusPromise = ManagerAPI.getMcus();
const withDeviceInfo = withDevicePolling(deviceId)(
(transport) => from(getDeviceInfo(transport)),
() => true // accept all errors. we're waiting forever condition that make getDeviceInfo work
);
const waitForBootloader = withDeviceInfo.pipe(
concatMap((deviceInfo) =>
deviceInfo.isBootloader ? EMPTY : concat(wait2s, waitForBootloader)
)
);
const loop = (forceMCU?: string | null | undefined) =>
withDeviceInfo.pipe(
concatMap((deviceInfo) => {
const installMcu = (version: string) =>
withDevice(deviceId)((transport) =>
ManagerAPI.installMcu(transport, "mcu", {
targetId: deviceInfo.targetId,
version,
})
);
if (!deviceInfo.isBootloader) {
// finish earlier
return EMPTY;
}
// This is a special case where user is in firmware 1.3.1
// and the device shows MCU Not Genuine.
// User needs to press both keys three times to go back to dashboard
// and continue the update process
if (
forceMCU &&
forceMCU === "0.7" &&
(deviceInfo.majMin === "0.6" || deviceInfo.majMin === "0.7")
) {
// finish earlier
return throwError(new MCUNotGenuineToDashboard());
}
if (forceMCU) {
return concat(installMcu(forceMCU), wait2s, loop());
}
switch (deviceInfo.majMin) {
case "0.0":
return concat(installMcu("0.6"), wait2s, loop());
case "0.6":
return installMcu("1.5");
case "0.7":
return installMcu("1.6");
case "0.9":
return installMcu("1.7");
default:
return from(mcusPromise).pipe(
concatMap((mcus) => {
let next;
const { seVersion, seTargetId, mcuBlVersion } = deviceInfo;
// This is a special case where a user with LNX version >= 2.0.0
// comes back with a broken updated device. We need to be able
// to patch MCU or Bootloader if needed
if (seVersion && seTargetId) {
log(
"hw",
"firmwareUpdate-repair seVersion and seTargetId found",
{ seVersion, seTargetId }
);
const validMcusForDeviceInfo = mcus
.filter(filterMCUForDeviceInfo(deviceInfo))
.filter((mcu) => mcu.from_bootloader_version !== "none");
log("hw", "firmwareUpdate-repair valid mcus for device", {
validMcusForDeviceInfo,
});
return from(
ManagerAPI.getDeviceVersion(
seTargetId,
getProviderId(deviceInfo)
)
).pipe(
mergeMap((deviceVersion: DeviceVersion) =>
from(
ManagerAPI.getCurrentFirmware({
deviceId: deviceVersion.id,
version: seVersion,
provider: getProviderId(deviceInfo),
})
)
),
mergeMap((finalFirmware: FinalFirmware) => {
log("hw", "firmwareUpdate-repair got final firmware", {
finalFirmware,
});
const mcu = ManagerAPI.findBestMCU(
finalFirmware.mcu_versions
.map((id) =>
validMcusForDeviceInfo.find((mcu) => mcu.id === id)
)
.filter(Boolean)
);
log("hw", "firmwareUpdate-repair got mcu", { mcu });
if (!mcu) return EMPTY;
const expectedBootloaderVersion = semver.coerce(
mcu.from_bootloader_version
).version;
const currentBootloaderVersion =
semver.coerce(mcuBlVersion).version;
log("hw", "firmwareUpdate-repair bootloader versions", {
currentBootloaderVersion,
expectedBootloaderVersion,
});
if (
expectedBootloaderVersion === currentBootloaderVersion
) {
next = mcu;
log(
"hw",
"firmwareUpdate-repair bootloader versions are the same",
{ next }
);
} else {
next = {
name: mcu.from_bootloader_version,
};
log(
"hw",
"firmwareUpdate-repair bootloader versions are different",
{ next }
);
}
return installMcu(next.name);
})
);
} else {
next = ManagerAPI.findBestMCU(
ManagerAPI.compatibleMCUForDeviceInfo(
mcus,
deviceInfo,
getProviderId(deviceInfo)
)
);
if (next) return installMcu(next.name);
}
return EMPTY;
})
);
}
})
);
// TODO ideally we should race waitForBootloader with an event "display-bootloader-reboot", it should be a delayed event that is not emitted if waitForBootloader is fast enough..
return concat(waitForBootloader, loop(forceMCU_)).pipe(
filter((e: any) => e.type === "bulk-progress"),
map((e) => ({
progress: e.progress,
})),
throttleTime(100)
);
};
export default repair;