forked from LedgerHQ/ledger-live-common
-
Notifications
You must be signed in to change notification settings - Fork 0
/
getVersion.ts
93 lines (79 loc) · 2.37 KB
/
getVersion.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
import Transport from "@ledgerhq/hw-transport";
import { FirmwareInfo } from "../types/manager";
/**
* Retrieve targetId and firmware version from device
*/
export default async function getVersion(
transport: Transport
): Promise<FirmwareInfo> {
const res = await transport.send(0xe0, 0x01, 0x00, 0x00);
const data = res.slice(0, res.length - 2);
let i = 0;
// parse the target id of either BL or SE
const targetId = data.readUIntBE(0, 4);
i += 4;
// parse the version of either BL or SE
const rawVersionLength = data[i++];
let rawVersion = data.slice(i, i + rawVersionLength).toString();
i += rawVersionLength;
// flags. gives information about manager allowed in SE mode.
const flagsLength = data[i++];
let flags = data.slice(i, i + flagsLength);
i += flagsLength;
if (!rawVersionLength) {
// To support old firmware like bootloader of 1.3.1
rawVersion = "0.0.0";
flags = Buffer.allocUnsafeSlow(0);
}
let mcuVersion = "";
let mcuBlVersion: string | undefined;
let seVersion: string | undefined;
let mcuTargetId: number | undefined;
let seTargetId: number | undefined;
const isBootloader = (targetId & 0xf0000000) !== 0x30000000;
if (isBootloader) {
mcuBlVersion = rawVersion;
mcuTargetId = targetId;
if (i < data.length) {
// se part 1
const part1Length = data[i++];
const part1 = data.slice(i, i + part1Length);
i += part1Length;
// at this time, this is how we branch old & new format
if (part1Length >= 5) {
seVersion = part1.toString();
// se part 2
const part2Length = data[i++];
const part2 = data.slice(i, i + part2Length);
i += flagsLength;
seTargetId = part2.readUIntBE(0, 4);
} else {
seTargetId = part1.readUIntBE(0, 4);
}
}
} else {
seVersion = rawVersion;
seTargetId = targetId;
// if SE: mcu version
const mcuVersionLength = data[i++];
let mcuVersionBuf: Buffer = Buffer.from(
data.slice(i, i + mcuVersionLength)
);
i += mcuVersionLength;
if (mcuVersionBuf[mcuVersionBuf.length - 1] === 0) {
mcuVersionBuf = mcuVersionBuf.slice(0, mcuVersionBuf.length - 1);
}
mcuVersion = mcuVersionBuf.toString();
}
return {
isBootloader,
rawVersion,
targetId,
seVersion,
mcuVersion,
mcuBlVersion,
mcuTargetId,
seTargetId,
flags,
};
}