@onekeyfe/hd-core 1.2.0-alpha.95 → 1.2.0-alpha.97
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/__tests__/check-all-firmware-release-protocol-v2.test.ts +84 -404
- package/__tests__/firmware-update/device-update-bootloader.test.ts +2 -6
- package/__tests__/firmware-update/firmware-host-binding.test.ts +0 -9
- package/__tests__/firmware-update/firmware-update-bootloader-poll.test.ts +112 -0
- package/__tests__/firmware-update/firmware-update-plan.test.ts +70 -131
- package/__tests__/firmware-update/firmware-update-prepared-plan.test.ts +1 -53
- package/__tests__/protocol-v2-resources.test.ts +232 -148
- package/__tests__/protocol-v2.test.ts +517 -1106
- package/dist/api/CheckAllFirmwareRelease.d.ts +3 -2
- package/dist/api/CheckAllFirmwareRelease.d.ts.map +1 -1
- package/dist/api/CheckBLEFirmwareRelease.d.ts +2 -3
- package/dist/api/CheckBLEFirmwareRelease.d.ts.map +1 -1
- package/dist/api/CheckBootloaderRelease.d.ts +1 -2
- package/dist/api/CheckBootloaderRelease.d.ts.map +1 -1
- package/dist/api/CheckFirmwareRelease.d.ts +2 -3
- package/dist/api/CheckFirmwareRelease.d.ts.map +1 -1
- package/dist/api/FirmwareUpdateV2.d.ts +0 -1
- package/dist/api/FirmwareUpdateV2.d.ts.map +1 -1
- package/dist/api/FirmwareUpdateV3.d.ts.map +1 -1
- package/dist/api/FirmwareUpdateV4.d.ts +8 -6
- package/dist/api/FirmwareUpdateV4.d.ts.map +1 -1
- package/dist/api/UploadPortfolio.d.ts.map +1 -1
- package/dist/api/device/DeviceUpdateBootloader.d.ts.map +1 -1
- package/dist/api/firmware/FirmwareHostBinding.d.ts +1 -1
- package/dist/api/firmware/FirmwareHostBinding.d.ts.map +1 -1
- package/dist/api/firmware/FirmwareUpdateBaseMethod.d.ts.map +1 -1
- package/dist/api/firmware/FirmwareUpdatePlan.d.ts +1 -48
- package/dist/api/firmware/FirmwareUpdatePlan.d.ts.map +1 -1
- package/dist/api/firmware/FirmwareUpdatePreparedPlan.d.ts.map +1 -1
- package/dist/api/firmware/getBinary.d.ts +1 -0
- package/dist/api/firmware/getBinary.d.ts.map +1 -1
- package/dist/data-manager/DataManager.d.ts +3 -4
- package/dist/data-manager/DataManager.d.ts.map +1 -1
- package/dist/index.d.ts +111 -103
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +849 -1301
- package/dist/protocols/protocol-v2/resources.d.ts +28 -10
- package/dist/protocols/protocol-v2/resources.d.ts.map +1 -1
- package/dist/types/api/checkAllFirmwareRelease.d.ts +8 -20
- package/dist/types/api/checkAllFirmwareRelease.d.ts.map +1 -1
- package/dist/types/api/checkBLEFirmwareRelease.d.ts +13 -2
- package/dist/types/api/checkBLEFirmwareRelease.d.ts.map +1 -1
- package/dist/types/api/checkBootloaderRelease.d.ts +6 -2
- package/dist/types/api/checkBootloaderRelease.d.ts.map +1 -1
- package/dist/types/api/checkFirmwareRelease.d.ts +13 -2
- package/dist/types/api/checkFirmwareRelease.d.ts.map +1 -1
- package/dist/types/api/checkFirmwareTypeAvailable.d.ts +2 -2
- package/dist/types/api/checkFirmwareTypeAvailable.d.ts.map +1 -1
- package/dist/types/api/export.d.ts +1 -1
- package/dist/types/api/export.d.ts.map +1 -1
- package/dist/types/api/firmwareUpdate.d.ts +11 -3
- package/dist/types/api/firmwareUpdate.d.ts.map +1 -1
- package/dist/types/settings.d.ts +36 -35
- package/dist/types/settings.d.ts.map +1 -1
- package/package.json +4 -4
- package/src/api/CheckAllFirmwareRelease.ts +80 -108
- package/src/api/CheckBLEFirmwareRelease.ts +5 -37
- package/src/api/CheckBootloaderRelease.ts +3 -35
- package/src/api/CheckFirmwareRelease.ts +7 -35
- package/src/api/FirmwareUpdateV2.ts +3 -15
- package/src/api/FirmwareUpdateV3.ts +6 -13
- package/src/api/FirmwareUpdateV4.ts +407 -824
- package/src/api/UploadPortfolio.ts +0 -18
- package/src/api/device/DeviceUpdateBootloader.ts +5 -26
- package/src/api/firmware/FirmwareHostBinding.ts +3 -16
- package/src/api/firmware/FirmwareUpdateBaseMethod.ts +2 -0
- package/src/api/firmware/FirmwareUpdatePlan.ts +88 -226
- package/src/api/firmware/FirmwareUpdatePreparedPlan.ts +3 -10
- package/src/data-manager/DataManager.ts +19 -35
- package/src/index.ts +0 -10
- package/src/protocols/protocol-v2/resources.ts +341 -157
- package/src/types/api/checkAllFirmwareRelease.ts +12 -27
- package/src/types/api/checkBLEFirmwareRelease.ts +13 -4
- package/src/types/api/checkBootloaderRelease.ts +6 -2
- package/src/types/api/checkFirmwareRelease.ts +13 -2
- package/src/types/api/checkFirmwareTypeAvailable.ts +2 -2
- package/src/types/api/export.ts +1 -6
- package/src/types/api/firmwareUpdate.ts +16 -10
- package/src/types/settings.ts +61 -35
- package/src/utils/deviceFeaturesUtils.ts +2 -2
- package/__tests__/check-firmware-release-protocol-v2.test.ts +0 -199
- package/__tests__/firmware-memory-host.test.ts +0 -112
- package/__tests__/firmware-update/firmware-prepared-host-digest.test.ts +0 -448
- package/dist/api/firmware/FirmwareMemoryHost.d.ts +0 -22
- package/dist/api/firmware/FirmwareMemoryHost.d.ts.map +0 -1
- package/dist/api/firmware/protocolV2Release.d.ts +0 -33
- package/dist/api/firmware/protocolV2Release.d.ts.map +0 -1
- package/src/api/firmware/FirmwareMemoryHost.ts +0 -142
- package/src/api/firmware/protocolV2Release.ts +0 -168
|
@@ -1,16 +1,210 @@
|
|
|
1
|
+
import { sha256 } from '@noble/hashes/sha256';
|
|
2
|
+
import { PROTOCOL_V2_BLE_FILE_READ_CHUNK_SIZE } from '@onekeyfe/hd-transport';
|
|
3
|
+
|
|
1
4
|
import type {
|
|
2
|
-
|
|
3
|
-
|
|
5
|
+
IProtocolV2BootResources,
|
|
6
|
+
IProtocolV2Resource,
|
|
7
|
+
IProtocolV2ResourceFile,
|
|
8
|
+
IProtocolV2ResourceType,
|
|
4
9
|
IProtocolV2Resources,
|
|
5
10
|
} from '../../types';
|
|
6
|
-
import type {
|
|
11
|
+
import type { DeviceCommands } from '../../device/DeviceCommands';
|
|
12
|
+
|
|
13
|
+
export const PROTOCOL_V2_RESOURCE_TYPES = [
|
|
14
|
+
'images',
|
|
15
|
+
'animation',
|
|
16
|
+
'wallpaper',
|
|
17
|
+
'translations',
|
|
18
|
+
'roobert',
|
|
19
|
+
'noto',
|
|
20
|
+
'firmware_logo',
|
|
21
|
+
] as const satisfies readonly IProtocolV2ResourceType[];
|
|
7
22
|
|
|
8
|
-
export const
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
23
|
+
export const PROTOCOL_V2_RESOURCE_DEVICE_PATHS: Readonly<Record<IProtocolV2ResourceType, string>> =
|
|
24
|
+
{
|
|
25
|
+
images: 'vol0:/bundles/images/images.okpkg',
|
|
26
|
+
animation: 'vol0:/bundles/images/animation.okpkg',
|
|
27
|
+
wallpaper: 'vol0:/bundles/images/wallpaper.okpkg',
|
|
28
|
+
translations: 'vol0:/bundles/translations/translations.okpkg',
|
|
29
|
+
roobert: 'vol0:/bundles/font/roobert.okpkg',
|
|
30
|
+
noto: 'vol0:/bundles/font/noto.okpkg',
|
|
31
|
+
firmware_logo: 'vol0:/bundles/firmware_logo.okpkg',
|
|
32
|
+
};
|
|
12
33
|
|
|
34
|
+
const RESOURCE_TYPE_SET = new Set<string>(PROTOCOL_V2_RESOURCE_TYPES);
|
|
13
35
|
const SHA256_HEX_LENGTH = 64;
|
|
36
|
+
const SHA3_512_HEX_LENGTH = 128;
|
|
37
|
+
const PROTOCOL_V2_OKPP_HEADER_SIZE = 0x52a0;
|
|
38
|
+
const PROTOCOL_V2_OKPP_TYPE_OFFSET = 0x08;
|
|
39
|
+
const PROTOCOL_V2_OKPP_HEADER_LENGTH_OFFSET = 0x0c;
|
|
40
|
+
const PROTOCOL_V2_OKPP_HEADER_HASH_OFFSET = 0x240;
|
|
41
|
+
const PROTOCOL_V2_OKPP_HASH_SIZE = 64;
|
|
42
|
+
const PROTOCOL_V2_RESOURCE_IDENTITY_READ_SIZE =
|
|
43
|
+
PROTOCOL_V2_OKPP_HEADER_HASH_OFFSET + PROTOCOL_V2_OKPP_HASH_SIZE;
|
|
44
|
+
const PROTOCOL_V2_MIN_FILE_READ_CHUNK_SIZE = 64;
|
|
45
|
+
export const PROTOCOL_V2_RESOURCE_INVENTORY_TIMEOUT_MS = 5 * 1000;
|
|
46
|
+
|
|
47
|
+
export type ProtocolV2ResourceInventoryItem = {
|
|
48
|
+
type: IProtocolV2ResourceType;
|
|
49
|
+
size: number;
|
|
50
|
+
headerHash: string;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
export type ProtocolV2ResourceUpdateMode = 'application' | 'bootloader-recovery';
|
|
54
|
+
|
|
55
|
+
export type ProtocolV2ResourceUpdatePlan = {
|
|
56
|
+
status: 'valid' | 'outdated' | 'unknown';
|
|
57
|
+
resources: IProtocolV2Resource[];
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
function toFiniteNumber(value: unknown): number | undefined {
|
|
61
|
+
if (typeof value === 'number' && Number.isFinite(value)) return value;
|
|
62
|
+
if (typeof value === 'string') {
|
|
63
|
+
const numeric = Number(value);
|
|
64
|
+
return Number.isFinite(numeric) ? numeric : undefined;
|
|
65
|
+
}
|
|
66
|
+
if (value && typeof value === 'object') {
|
|
67
|
+
const longLike = value as { toNumber?: () => number };
|
|
68
|
+
if (typeof longLike.toNumber === 'function') {
|
|
69
|
+
const numeric = longLike.toNumber();
|
|
70
|
+
return Number.isFinite(numeric) ? numeric : undefined;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return undefined;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function toUint8Array(value: unknown): Uint8Array {
|
|
77
|
+
if (value instanceof Uint8Array) return value;
|
|
78
|
+
if (value instanceof ArrayBuffer) return new Uint8Array(value);
|
|
79
|
+
if (ArrayBuffer.isView(value)) {
|
|
80
|
+
return new Uint8Array(value.buffer, value.byteOffset, value.byteLength);
|
|
81
|
+
}
|
|
82
|
+
if (typeof value === 'string') {
|
|
83
|
+
const hex = value.replace(/^0x/i, '');
|
|
84
|
+
if (!hex || hex.length % 2 !== 0 || /[^0-9a-f]/i.test(hex)) {
|
|
85
|
+
return new Uint8Array(0);
|
|
86
|
+
}
|
|
87
|
+
const bytes = new Uint8Array(hex.length / 2);
|
|
88
|
+
for (let index = 0; index < bytes.length; index += 1) {
|
|
89
|
+
bytes[index] = Number.parseInt(hex.slice(index * 2, index * 2 + 2), 16);
|
|
90
|
+
}
|
|
91
|
+
return bytes;
|
|
92
|
+
}
|
|
93
|
+
return new Uint8Array(0);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function readAscii(bytes: Uint8Array, offset: number, length: number): string {
|
|
97
|
+
return Array.from(bytes.slice(offset, offset + length), byte => String.fromCharCode(byte)).join(
|
|
98
|
+
''
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function parseProtocolV2ResourceHeaderHash(bytes: Uint8Array): string | undefined {
|
|
103
|
+
if (bytes.byteLength < PROTOCOL_V2_RESOURCE_IDENTITY_READ_SIZE) return undefined;
|
|
104
|
+
if (readAscii(bytes, 0, 4) !== 'OKPP') return undefined;
|
|
105
|
+
if (readAscii(bytes, PROTOCOL_V2_OKPP_TYPE_OFFSET, 4) !== 'RESC') return undefined;
|
|
106
|
+
|
|
107
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
108
|
+
if (
|
|
109
|
+
view.getUint32(PROTOCOL_V2_OKPP_HEADER_LENGTH_OFFSET, true) !== PROTOCOL_V2_OKPP_HEADER_SIZE
|
|
110
|
+
) {
|
|
111
|
+
return undefined;
|
|
112
|
+
}
|
|
113
|
+
return bytesToHex(
|
|
114
|
+
bytes.slice(
|
|
115
|
+
PROTOCOL_V2_OKPP_HEADER_HASH_OFFSET,
|
|
116
|
+
PROTOCOL_V2_OKPP_HEADER_HASH_OFFSET + PROTOCOL_V2_OKPP_HASH_SIZE
|
|
117
|
+
)
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
async function readProtocolV2ResourceIdentity({
|
|
122
|
+
commands,
|
|
123
|
+
resource,
|
|
124
|
+
chunkSize,
|
|
125
|
+
timeoutMs = PROTOCOL_V2_RESOURCE_INVENTORY_TIMEOUT_MS,
|
|
126
|
+
}: {
|
|
127
|
+
commands: Pick<DeviceCommands, 'typedCall'>;
|
|
128
|
+
resource: IProtocolV2Resource;
|
|
129
|
+
chunkSize: number;
|
|
130
|
+
timeoutMs?: number;
|
|
131
|
+
}): Promise<ProtocolV2ResourceInventoryItem | undefined> {
|
|
132
|
+
const path = PROTOCOL_V2_RESOURCE_DEVICE_PATHS[resource.type];
|
|
133
|
+
const pathInfo = await commands.typedCall(
|
|
134
|
+
'FilesystemPathInfoQuery',
|
|
135
|
+
'FilesystemPathInfo',
|
|
136
|
+
{ path },
|
|
137
|
+
{ timeoutMs }
|
|
138
|
+
);
|
|
139
|
+
const size = toFiniteNumber(pathInfo.message?.size);
|
|
140
|
+
if (
|
|
141
|
+
!pathInfo.message?.exist ||
|
|
142
|
+
pathInfo.message?.directory ||
|
|
143
|
+
!Number.isSafeInteger(size) ||
|
|
144
|
+
size !== resource.size ||
|
|
145
|
+
size < PROTOCOL_V2_OKPP_HEADER_SIZE
|
|
146
|
+
) {
|
|
147
|
+
return undefined;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
const header = new Uint8Array(PROTOCOL_V2_RESOURCE_IDENTITY_READ_SIZE);
|
|
151
|
+
let offset = 0;
|
|
152
|
+
while (offset < header.byteLength) {
|
|
153
|
+
const readLength = Math.min(chunkSize, header.byteLength - offset);
|
|
154
|
+
const response = await commands.typedCall(
|
|
155
|
+
'FilesystemFileRead',
|
|
156
|
+
'FilesystemFile',
|
|
157
|
+
{
|
|
158
|
+
file: { path, offset, total_size: 0 },
|
|
159
|
+
chunk_len: readLength,
|
|
160
|
+
},
|
|
161
|
+
{ timeoutMs }
|
|
162
|
+
);
|
|
163
|
+
const data = toUint8Array(response.message?.data);
|
|
164
|
+
if (data.byteLength === 0) return undefined;
|
|
165
|
+
const copied = Math.min(data.byteLength, header.byteLength - offset);
|
|
166
|
+
header.set(data.subarray(0, copied), offset);
|
|
167
|
+
offset += copied;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
const headerHash = parseProtocolV2ResourceHeaderHash(header);
|
|
171
|
+
return headerHash ? { type: resource.type, size, headerHash } : undefined;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* 使用已发布 Pro2 固件支持的文件系统消息构建资源清单。
|
|
176
|
+
* 缺失、无法读取或格式错误的文件不会进入清单,因此会被选中重写。
|
|
177
|
+
*/
|
|
178
|
+
export async function readProtocolV2ResourceInventory({
|
|
179
|
+
commands,
|
|
180
|
+
resources,
|
|
181
|
+
chunkSize = PROTOCOL_V2_BLE_FILE_READ_CHUNK_SIZE,
|
|
182
|
+
timeoutMs = PROTOCOL_V2_RESOURCE_INVENTORY_TIMEOUT_MS,
|
|
183
|
+
}: {
|
|
184
|
+
commands: Pick<DeviceCommands, 'typedCall'>;
|
|
185
|
+
resources: readonly IProtocolV2Resource[];
|
|
186
|
+
chunkSize?: number;
|
|
187
|
+
timeoutMs?: number;
|
|
188
|
+
}): Promise<ProtocolV2ResourceInventoryItem[]> {
|
|
189
|
+
const normalizedChunkSize = Number.isFinite(chunkSize)
|
|
190
|
+
? Math.max(Math.floor(chunkSize), PROTOCOL_V2_MIN_FILE_READ_CHUNK_SIZE)
|
|
191
|
+
: PROTOCOL_V2_BLE_FILE_READ_CHUNK_SIZE;
|
|
192
|
+
const inventory: ProtocolV2ResourceInventoryItem[] = [];
|
|
193
|
+
for (const resource of resources) {
|
|
194
|
+
try {
|
|
195
|
+
const item = await readProtocolV2ResourceIdentity({
|
|
196
|
+
commands,
|
|
197
|
+
resource,
|
|
198
|
+
chunkSize: normalizedChunkSize,
|
|
199
|
+
timeoutMs,
|
|
200
|
+
});
|
|
201
|
+
if (item) inventory.push(item);
|
|
202
|
+
} catch {
|
|
203
|
+
// 单个资源无法读取时按缺失处理,不阻断其余资源的增量检查。
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
return inventory;
|
|
207
|
+
}
|
|
14
208
|
|
|
15
209
|
function normalizeHex(value: unknown, expectedLength: number, field: string): string {
|
|
16
210
|
if (typeof value !== 'string') {
|
|
@@ -25,186 +219,176 @@ function normalizeHex(value: unknown, expectedLength: number, field: string): st
|
|
|
25
219
|
return normalized;
|
|
26
220
|
}
|
|
27
221
|
|
|
28
|
-
|
|
29
|
-
export function parseProtocolV2Resources(value: unknown): IProtocolV2Resources | undefined {
|
|
30
|
-
if (value === undefined) return undefined;
|
|
222
|
+
function validateResource(value: unknown, index: number): IProtocolV2Resource {
|
|
31
223
|
if (!value || typeof value !== 'object') {
|
|
32
|
-
throw new Error(
|
|
224
|
+
throw new Error(`Invalid Pro2 resource at stable[${index}]`);
|
|
33
225
|
}
|
|
34
|
-
const
|
|
35
|
-
if (
|
|
36
|
-
throw new Error(
|
|
226
|
+
const resource = value as Partial<IProtocolV2Resource>;
|
|
227
|
+
if (typeof resource.type !== 'string' || !RESOURCE_TYPE_SET.has(resource.type)) {
|
|
228
|
+
throw new Error(`Invalid Pro2 resource type at stable[${index}]`);
|
|
37
229
|
}
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
archiveSha256?: unknown;
|
|
41
|
-
archiveSize?: unknown;
|
|
42
|
-
};
|
|
43
|
-
if (typeof archiveUrl !== 'string' || !archiveUrl.startsWith('https://')) {
|
|
44
|
-
throw new Error('Invalid Pro2 resources config: source.archiveUrl must use HTTPS');
|
|
45
|
-
}
|
|
46
|
-
if (typeof archiveSha256 !== 'string' || !/^[0-9a-fA-F]{64}$/.test(archiveSha256)) {
|
|
47
|
-
throw new Error('Invalid Pro2 resources config: source.archiveSha256 must be a SHA-256 digest');
|
|
230
|
+
if (typeof resource.url !== 'string' || !resource.url.startsWith('https://')) {
|
|
231
|
+
throw new Error(`Invalid Pro2 resource url at stable[${index}]`);
|
|
48
232
|
}
|
|
49
|
-
if (!Number.isSafeInteger(
|
|
50
|
-
throw new Error(
|
|
233
|
+
if (!Number.isSafeInteger(resource.size) || Number(resource.size) <= 0) {
|
|
234
|
+
throw new Error(`Invalid Pro2 resource size at stable[${index}]`);
|
|
51
235
|
}
|
|
52
236
|
return {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
237
|
+
type: resource.type,
|
|
238
|
+
url: resource.url,
|
|
239
|
+
size: Number(resource.size),
|
|
240
|
+
fileHash: normalizeHex(resource.fileHash, SHA256_HEX_LENGTH, 'fileHash'),
|
|
241
|
+
headerHash: normalizeHex(resource.headerHash, SHA3_512_HEX_LENGTH, 'headerHash'),
|
|
58
242
|
};
|
|
59
243
|
}
|
|
60
244
|
|
|
61
|
-
|
|
62
|
-
'
|
|
63
|
-
|
|
64
|
-
] as const;
|
|
65
|
-
|
|
66
|
-
function isAllowedManifestDevicePath(path: string): boolean {
|
|
67
|
-
if (
|
|
68
|
-
!path.endsWith('.okpkg') ||
|
|
69
|
-
path.includes('\\') ||
|
|
70
|
-
path.includes('//') ||
|
|
71
|
-
path.split('/').some(part => part === '.' || part === '..')
|
|
72
|
-
) {
|
|
73
|
-
return false;
|
|
245
|
+
function validateBootResources(value: unknown): IProtocolV2BootResources {
|
|
246
|
+
if (!value || typeof value !== 'object') {
|
|
247
|
+
throw new Error('Invalid Pro2 boot resources config');
|
|
74
248
|
}
|
|
75
|
-
|
|
76
|
-
|
|
249
|
+
const resource = value as Partial<IProtocolV2BootResources>;
|
|
250
|
+
if (resource.required !== false) {
|
|
251
|
+
throw new Error('Invalid Pro2 boot resources required flag: expected false');
|
|
77
252
|
}
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
function assertManifestString(value: unknown, field: string): string {
|
|
82
|
-
if (typeof value !== 'string' || value.length === 0) {
|
|
83
|
-
throw new Error(`Invalid Pro2 resource manifest ${field}`);
|
|
253
|
+
if (resource.target !== 'RES') {
|
|
254
|
+
throw new Error('Invalid Pro2 boot resources target: expected RES');
|
|
84
255
|
}
|
|
85
|
-
return value;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
function assertManifestRelativePath(value: unknown, field: string): string {
|
|
89
|
-
const path = assertManifestString(value, field);
|
|
90
256
|
if (
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
path.includes(':') ||
|
|
94
|
-
path.split('/').some(part => !part || part === '.' || part === '..')
|
|
257
|
+
resource.manifestUrl !== undefined &&
|
|
258
|
+
(typeof resource.manifestUrl !== 'string' || !resource.manifestUrl.startsWith('https://'))
|
|
95
259
|
) {
|
|
96
|
-
throw new Error(
|
|
260
|
+
throw new Error('Invalid Pro2 boot resources manifestUrl');
|
|
97
261
|
}
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
function parseProtocolV2ResourceManifestFile(
|
|
102
|
-
value: unknown,
|
|
103
|
-
index: number
|
|
104
|
-
): IProtocolV2ResourceManifestFile {
|
|
105
|
-
if (!value || typeof value !== 'object' || Array.isArray(value)) {
|
|
106
|
-
throw new Error(`Invalid Pro2 resource manifest files[${index}]`);
|
|
107
|
-
}
|
|
108
|
-
const file = value as Partial<IProtocolV2ResourceManifestFile>;
|
|
109
|
-
const archivePath = assertManifestRelativePath(file.archive_path, `files[${index}].archive_path`);
|
|
110
|
-
const originalName = assertManifestRelativePath(
|
|
111
|
-
file.original_name,
|
|
112
|
-
`files[${index}].original_name`
|
|
113
|
-
);
|
|
114
|
-
if (originalName.includes('/')) {
|
|
115
|
-
throw new Error(`Invalid Pro2 resource manifest files[${index}].original_name`);
|
|
116
|
-
}
|
|
117
|
-
const devicePath = assertManifestString(file.device_path, `files[${index}].device_path`);
|
|
118
|
-
if (!isAllowedManifestDevicePath(devicePath)) {
|
|
119
|
-
throw new Error(`Invalid Pro2 resource manifest files[${index}].device_path`);
|
|
120
|
-
}
|
|
121
|
-
if (!Number.isSafeInteger(file.size) || Number(file.size) <= 0) {
|
|
122
|
-
throw new Error(`Invalid Pro2 resource manifest files[${index}].size`);
|
|
123
|
-
}
|
|
124
|
-
const digest = normalizeHex(file.sha256, SHA256_HEX_LENGTH, `files[${index}].sha256`);
|
|
125
|
-
if (file.signed !== true) {
|
|
126
|
-
throw new Error(`Invalid Pro2 resource manifest files[${index}].signed`);
|
|
262
|
+
if (!Array.isArray(resource.files) || resource.files.length === 0) {
|
|
263
|
+
throw new Error('Invalid Pro2 boot resources files');
|
|
127
264
|
}
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
265
|
+
const files = resource.files.map((value, index): IProtocolV2ResourceFile => {
|
|
266
|
+
if (!value || typeof value !== 'object') {
|
|
267
|
+
throw new Error(`Invalid Pro2 boot resource file at files[${index}]`);
|
|
268
|
+
}
|
|
269
|
+
const file = value as Partial<IProtocolV2ResourceFile>;
|
|
270
|
+
if (typeof file.url !== 'string' || !file.url.startsWith('https://')) {
|
|
271
|
+
throw new Error(`Invalid Pro2 boot resource url at files[${index}]`);
|
|
272
|
+
}
|
|
273
|
+
if (
|
|
274
|
+
typeof file.devicePath !== 'string' ||
|
|
275
|
+
!file.devicePath.startsWith('vol0:/') ||
|
|
276
|
+
file.devicePath.includes('..') ||
|
|
277
|
+
file.devicePath.includes('\\')
|
|
278
|
+
) {
|
|
279
|
+
throw new Error(`Invalid Pro2 boot resource devicePath at files[${index}]`);
|
|
280
|
+
}
|
|
281
|
+
if (!Number.isSafeInteger(file.size) || Number(file.size) <= 0) {
|
|
282
|
+
throw new Error(`Invalid Pro2 boot resource size at files[${index}]`);
|
|
283
|
+
}
|
|
284
|
+
return {
|
|
285
|
+
...(typeof file.name === 'string' && file.name ? { name: file.name } : undefined),
|
|
286
|
+
url: file.url,
|
|
287
|
+
devicePath: file.devicePath,
|
|
288
|
+
size: Number(file.size),
|
|
289
|
+
fileHash: normalizeHex(file.fileHash, SHA256_HEX_LENGTH, `boot files[${index}].fileHash`),
|
|
290
|
+
};
|
|
291
|
+
});
|
|
292
|
+
if (new Set(files.map(file => file.devicePath)).size !== files.length) {
|
|
293
|
+
throw new Error('Invalid Pro2 boot resources files: duplicate devicePath');
|
|
136
294
|
}
|
|
137
295
|
return {
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
sha256: digest,
|
|
143
|
-
signed: true,
|
|
144
|
-
sig_algo: file.sig_algo,
|
|
145
|
-
payload_version: file.payload_version ?? null,
|
|
296
|
+
required: false,
|
|
297
|
+
target: 'RES',
|
|
298
|
+
...(resource.manifestUrl ? { manifestUrl: resource.manifestUrl } : undefined),
|
|
299
|
+
files,
|
|
146
300
|
};
|
|
147
301
|
}
|
|
148
302
|
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
}
|
|
153
|
-
const manifest = value as Partial<IProtocolV2ResourceManifest>;
|
|
303
|
+
/** Validate a complete Pro2 stable resource set from remote configuration. */
|
|
304
|
+
export function parseProtocolV2Resources(value: unknown): IProtocolV2Resources | undefined {
|
|
305
|
+
if (value === undefined) return undefined;
|
|
154
306
|
if (
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
manifest.restore_mode !== 'bootloader_update' ||
|
|
159
|
-
!Array.isArray(manifest.trees) ||
|
|
160
|
-
!Array.isArray(manifest.files)
|
|
307
|
+
!value ||
|
|
308
|
+
typeof value !== 'object' ||
|
|
309
|
+
!Array.isArray((value as { stable?: unknown }).stable)
|
|
161
310
|
) {
|
|
162
|
-
throw new Error('Invalid Pro2
|
|
311
|
+
throw new Error('Invalid Pro2 resources config: stable must be an array');
|
|
163
312
|
}
|
|
164
|
-
|
|
165
|
-
const
|
|
166
|
-
const
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
) {
|
|
174
|
-
|
|
313
|
+
|
|
314
|
+
const config = value as { stable: unknown[]; boot?: unknown };
|
|
315
|
+
const stable = config.stable.map(validateResource);
|
|
316
|
+
const types = new Set(stable.map(resource => resource.type));
|
|
317
|
+
if (stable.length !== PROTOCOL_V2_RESOURCE_TYPES.length || types.size !== stable.length) {
|
|
318
|
+
throw new Error(
|
|
319
|
+
`Invalid Pro2 resources config: stable must contain ${PROTOCOL_V2_RESOURCE_TYPES.length} unique resource types`
|
|
320
|
+
);
|
|
321
|
+
}
|
|
322
|
+
for (const type of PROTOCOL_V2_RESOURCE_TYPES) {
|
|
323
|
+
if (!types.has(type)) {
|
|
324
|
+
throw new Error(`Invalid Pro2 resources config: stable is missing ${type}`);
|
|
325
|
+
}
|
|
175
326
|
}
|
|
327
|
+
const boot = config.boot === undefined ? undefined : validateBootResources(config.boot);
|
|
176
328
|
return {
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
commit: assertManifestString(manifest.commit, 'commit'),
|
|
182
|
-
short_sha: assertManifestString(manifest.short_sha, 'short_sha'),
|
|
183
|
-
timestamp_utc: assertManifestString(manifest.timestamp_utc, 'timestamp_utc'),
|
|
184
|
-
core_version: assertManifestString(manifest.core_version, 'core_version'),
|
|
185
|
-
key_set: assertManifestString(manifest.key_set, 'key_set'),
|
|
186
|
-
device_root: 'vol0:',
|
|
187
|
-
restore_mode: 'bootloader_update',
|
|
188
|
-
trees: manifest.trees.map((tree, index) => {
|
|
189
|
-
if (!tree || typeof tree !== 'object') {
|
|
190
|
-
throw new Error(`Invalid Pro2 resource manifest trees[${index}]`);
|
|
329
|
+
stable: PROTOCOL_V2_RESOURCE_TYPES.map(type => {
|
|
330
|
+
const resource = stable.find(item => item.type === type);
|
|
331
|
+
if (!resource) {
|
|
332
|
+
throw new Error(`Invalid Pro2 resources config: stable is missing ${type}`);
|
|
191
333
|
}
|
|
192
|
-
|
|
193
|
-
return {
|
|
194
|
-
path: assertManifestRelativePath(item.path, `trees[${index}].path`),
|
|
195
|
-
device: assertManifestString(item.device, `trees[${index}].device`),
|
|
196
|
-
};
|
|
334
|
+
return resource;
|
|
197
335
|
}),
|
|
198
|
-
|
|
336
|
+
...(boot ? { boot } : undefined),
|
|
199
337
|
};
|
|
200
338
|
}
|
|
201
339
|
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
340
|
+
/** 比较文件系统资源清单;恢复模式无法取得清单时回退到全量更新。 */
|
|
341
|
+
export function buildProtocolV2ResourceUpdatePlan({
|
|
342
|
+
resources,
|
|
343
|
+
inventory,
|
|
344
|
+
mode,
|
|
345
|
+
forced = false,
|
|
205
346
|
}: {
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
347
|
+
resources: readonly IProtocolV2Resource[];
|
|
348
|
+
inventory?: readonly ProtocolV2ResourceInventoryItem[];
|
|
349
|
+
mode: ProtocolV2ResourceUpdateMode;
|
|
350
|
+
forced?: boolean;
|
|
351
|
+
}): ProtocolV2ResourceUpdatePlan {
|
|
352
|
+
if (forced) {
|
|
353
|
+
return {
|
|
354
|
+
status: resources.length > 0 ? 'outdated' : 'valid',
|
|
355
|
+
resources: [...resources],
|
|
356
|
+
};
|
|
357
|
+
}
|
|
358
|
+
if (!inventory) {
|
|
359
|
+
return mode === 'bootloader-recovery'
|
|
360
|
+
? {
|
|
361
|
+
status: resources.length > 0 ? 'outdated' : 'valid',
|
|
362
|
+
resources: [...resources],
|
|
363
|
+
}
|
|
364
|
+
: { status: 'unknown', resources: [] };
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
const inventoryByType = new Map(inventory.map(item => [item.type, item]));
|
|
368
|
+
const changedResources = resources.filter(resource => {
|
|
369
|
+
const current = inventoryByType.get(resource.type);
|
|
370
|
+
return (
|
|
371
|
+
!current ||
|
|
372
|
+
current.size !== resource.size ||
|
|
373
|
+
current.headerHash.toLowerCase() !== resource.headerHash.toLowerCase()
|
|
374
|
+
);
|
|
375
|
+
});
|
|
376
|
+
|
|
377
|
+
return {
|
|
378
|
+
status: changedResources.length === 0 ? 'valid' : 'outdated',
|
|
379
|
+
resources: changedResources,
|
|
380
|
+
};
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
function bytesToHex(bytes: Uint8Array): string {
|
|
384
|
+
return Array.from(bytes, byte => byte.toString(16).padStart(2, '0')).join('');
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
/** Verify the complete downloaded file before any device mutation. */
|
|
388
|
+
export function isProtocolV2ResourceFileValid(
|
|
389
|
+
binary: ArrayBuffer,
|
|
390
|
+
resource: Pick<IProtocolV2ResourceFile, 'size' | 'fileHash'>
|
|
391
|
+
): boolean {
|
|
392
|
+
if (binary.byteLength !== resource.size) return false;
|
|
393
|
+
return bytesToHex(sha256(new Uint8Array(binary))) === resource.fileHash.toLowerCase();
|
|
210
394
|
}
|
|
@@ -1,43 +1,30 @@
|
|
|
1
1
|
import type { EFirmwareType } from '@onekeyfe/hd-shared';
|
|
2
2
|
import type { CommonParams, Response } from '../params';
|
|
3
|
-
import type {
|
|
3
|
+
import type {
|
|
4
|
+
DeviceStateVersions,
|
|
5
|
+
Features,
|
|
6
|
+
IDeviceBLEFirmwareStatus,
|
|
7
|
+
IDeviceFirmwareStatus,
|
|
8
|
+
} from '../device';
|
|
4
9
|
import type {
|
|
5
10
|
IBLEFirmwareReleaseInfo,
|
|
6
11
|
IFirmwareReleaseInfo,
|
|
7
|
-
IProtocolV2FirmwareComponent,
|
|
8
12
|
IProtocolV2FirmwareComponentTarget,
|
|
9
|
-
IProtocolV2ResourceSource,
|
|
10
13
|
} from '../settings';
|
|
11
14
|
import type { FirmwareUpdateV4Target } from './firmwareUpdate';
|
|
12
15
|
import type { FirmwareUpdatePlan, FirmwareUpdatePlanForceTarget } from './firmwareUpdatePlan';
|
|
13
16
|
|
|
14
|
-
export type
|
|
15
|
-
protocol: 'V2';
|
|
16
|
-
configKey: string;
|
|
17
|
-
componentTarget: IProtocolV2FirmwareComponentTarget;
|
|
18
|
-
required: boolean;
|
|
19
|
-
changelog: IFirmwareReleaseInfo['changelog'];
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
export type FirmwareReleaseCheckResult = {
|
|
17
|
+
export type FirmwareRelease = {
|
|
23
18
|
shouldUpdate?: boolean;
|
|
24
19
|
status: IDeviceFirmwareStatus;
|
|
25
20
|
changelog?: {
|
|
26
21
|
'zh-CN': string;
|
|
27
22
|
'en-US': string;
|
|
28
23
|
}[];
|
|
29
|
-
release:
|
|
30
|
-
| IBLEFirmwareReleaseInfo
|
|
31
|
-
| IFirmwareReleaseInfo
|
|
32
|
-
| ProtocolV2ComponentReleaseInfo
|
|
33
|
-
| undefined;
|
|
24
|
+
release: IDeviceBLEFirmwareStatus | IBLEFirmwareReleaseInfo | IFirmwareReleaseInfo;
|
|
34
25
|
bootloaderMode?: boolean;
|
|
35
26
|
};
|
|
36
27
|
|
|
37
|
-
export type BridgeReleaseCheckResult = Omit<FirmwareReleaseCheckResult, 'release'> & {
|
|
38
|
-
release: string | undefined;
|
|
39
|
-
};
|
|
40
|
-
|
|
41
28
|
export type ProtocolV2FirmwareComponentReleaseStatus =
|
|
42
29
|
| 'valid'
|
|
43
30
|
| 'outdated'
|
|
@@ -62,10 +49,10 @@ export type ProtocolV2FirmwareComponentRelease = {
|
|
|
62
49
|
};
|
|
63
50
|
|
|
64
51
|
export type AllFirmwareRelease = {
|
|
65
|
-
firmware:
|
|
66
|
-
ble:
|
|
67
|
-
bootloader?:
|
|
68
|
-
bridge?:
|
|
52
|
+
firmware: FirmwareRelease;
|
|
53
|
+
ble: FirmwareRelease;
|
|
54
|
+
bootloader?: FirmwareRelease;
|
|
55
|
+
bridge?: FirmwareRelease;
|
|
69
56
|
features?: Features;
|
|
70
57
|
protocol?: 'V1' | 'V2';
|
|
71
58
|
deviceType?: 'pro2' | 'neo';
|
|
@@ -74,7 +61,6 @@ export type AllFirmwareRelease = {
|
|
|
74
61
|
hasUpgrade?: boolean;
|
|
75
62
|
required?: boolean;
|
|
76
63
|
resourceStatus?: 'valid' | 'outdated' | 'unknown';
|
|
77
|
-
resourceArchive?: IProtocolV2ResourceSource;
|
|
78
64
|
currentVersions?: DeviceStateVersions;
|
|
79
65
|
components?: ProtocolV2FirmwareComponentRelease[];
|
|
80
66
|
targetsToUpdate?: FirmwareUpdateV4Target[];
|
|
@@ -88,7 +74,6 @@ export type CheckAllFirmwareReleaseParams = {
|
|
|
88
74
|
firmwareType?: EFirmwareType;
|
|
89
75
|
platform?: 'native' | 'desktop' | 'ext' | 'web' | 'web-embed';
|
|
90
76
|
forceUpdateTargets?: FirmwareUpdatePlanForceTarget[];
|
|
91
|
-
protocolV2ForceUpdateTargets?: FirmwareUpdateV4Target[];
|
|
92
77
|
};
|
|
93
78
|
|
|
94
79
|
export declare function checkAllFirmwareRelease(
|
|
@@ -1,6 +1,15 @@
|
|
|
1
|
+
import type { IBLEFirmwareReleaseInfo } from '../settings';
|
|
1
2
|
import type { Response } from '../params';
|
|
2
|
-
import type {
|
|
3
|
+
import type { IDeviceBLEFirmwareStatus } from '../device';
|
|
3
4
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
type BleFirmwareRelease = {
|
|
6
|
+
status: IDeviceBLEFirmwareStatus;
|
|
7
|
+
changelog: {
|
|
8
|
+
'zh-CN': string;
|
|
9
|
+
'en-US': string;
|
|
10
|
+
}[];
|
|
11
|
+
release: IBLEFirmwareReleaseInfo;
|
|
12
|
+
bootloaderMode: boolean;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export declare function checkBLEFirmwareRelease(connectId?: string): Response<BleFirmwareRelease>;
|
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
import type { EFirmwareType } from '@onekeyfe/hd-shared';
|
|
2
2
|
import type { CommonParams, Response } from '../params';
|
|
3
|
-
import type { FirmwareReleaseCheckResult } from './checkAllFirmwareRelease';
|
|
4
3
|
|
|
5
|
-
export type CheckBootloaderReleaseResponse =
|
|
4
|
+
export type CheckBootloaderReleaseResponse = {
|
|
5
|
+
shouldUpdate: boolean;
|
|
6
|
+
status: 'outdated' | 'valid';
|
|
7
|
+
release: string | undefined;
|
|
8
|
+
bootloaderMode: boolean;
|
|
9
|
+
} | null;
|
|
6
10
|
|
|
7
11
|
export type CheckBootloaderReleaseParams = {
|
|
8
12
|
willUpdateFirmwareVersion?: string;
|