forked from LedgerHQ/ledger-live-common
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflash.ts
82 lines (78 loc) · 2.83 KB
/
flash.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
import { log } from "@ledgerhq/logs";
import Transport from "@ledgerhq/hw-transport";
import { Observable, from, of, concat, EMPTY } from "rxjs";
import { mergeMap } from "rxjs/operators";
import ManagerAPI from "../api/Manager";
import { getProviderId } from "../manager/provider";
import getDeviceInfo from "./getDeviceInfo";
import type { FinalFirmware, DeviceInfo, McuVersion } from "../types/manager";
const blVersionAliases = {
"0.0": "0.6",
};
const filterMCUForDeviceInfo = (deviceInfo) => {
const provider = getProviderId(deviceInfo);
return (mcu) => mcu.providers.includes(provider);
};
export default (finalFirmware: FinalFirmware) =>
(transport: Transport): Observable<any> =>
from(getDeviceInfo(transport)).pipe(
mergeMap((deviceInfo: DeviceInfo) =>
(deviceInfo.majMin in blVersionAliases
? of(blVersionAliases[deviceInfo.majMin])
: from(
// we pick the best MCU to install in the context of the firmware
ManagerAPI.getMcus()
.then((mcus) => mcus.filter(filterMCUForDeviceInfo(deviceInfo)))
.then((mcus) =>
mcus.filter((mcu) => mcu.from_bootloader_version !== "none")
)
.then((mcus) =>
ManagerAPI.findBestMCU(
finalFirmware.mcu_versions
.map((id) => mcus.find((mcu) => mcu.id === id))
.filter(Boolean)
)
)
)
).pipe(
mergeMap((mcuVersion: (McuVersion | null | undefined) | string) => {
if (!mcuVersion) return EMPTY;
let version;
let isMCU = false;
if (typeof mcuVersion === "string") {
version = mcuVersion;
log("firmware-update", `flash ${version} from mcuVersion`);
} else {
const mcuFromBootloader = (
mcuVersion.from_bootloader_version || ""
)
.split(".")
.slice(0, 2)
.join(".");
isMCU = deviceInfo.majMin === mcuFromBootloader;
version = isMCU ? mcuVersion.name : mcuFromBootloader;
log(
"firmware-update",
`flash ${version} isMcu=${String(isMCU)}`,
{
blVersion: deviceInfo.majMin,
mcuFromBootloader,
version,
isMCU,
}
);
}
return concat(
of({
type: "install",
step: "flash-" + (isMCU ? "mcu" : "bootloader"),
}),
ManagerAPI.installMcu(transport, "mcu", {
targetId: deviceInfo.targetId,
version,
})
);
})
)
)
);