-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparse-uart-socid.js
More file actions
348 lines (291 loc) · 11.2 KB
/
parse-uart-socid.js
File metadata and controls
348 lines (291 loc) · 11.2 KB
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
// UART SoC ID Parser JavaScript Implementation
// Converts Python parse_uart_socid.py functionality to web-based tool
// DOM Elements
let hexInput, parseButton, clearButton, exportButton;
let resultsSection, resultsContent;
// Parsed data storage
let parsedData = null;
// Initialize on DOM load
document.addEventListener('DOMContentLoaded', function() {
initializeElements();
attachEventListeners();
});
function initializeElements() {
hexInput = document.getElementById('hexInput');
parseButton = document.getElementById('parseButton');
clearButton = document.getElementById('clearButton');
exportButton = document.getElementById('exportButton');
resultsSection = document.getElementById('resultsSection');
resultsContent = document.getElementById('resultsContent');
}
function attachEventListeners() {
parseButton.addEventListener('click', handleParse);
clearButton.addEventListener('click', handleClear);
exportButton.addEventListener('click', handleExport);
hexInput.addEventListener('keypress', (e) => {
if (e.key === 'Enter' && e.ctrlKey) {
handleParse();
}
});
}
function handleParse() {
try {
const inputData = hexInput.value.trim();
if (!inputData) {
displayError('Please provide hex data to parse.');
return;
}
const binaryData = parseHexInput(inputData);
const parsed = parseSocIdData(binaryData);
parsedData = parsed;
displayResults(parsed);
resultsSection.style.display = 'block';
} catch (error) {
displayError('Parse error: ' + error.message);
console.error('Parse error:', error);
}
}
function handleClear() {
hexInput.value = '';
resultsSection.style.display = 'none';
parsedData = null;
clearError();
}
function handleExport() {
if (!parsedData) {
displayError('No data to export. Please parse SoC ID data first.');
return;
}
const jsonData = JSON.stringify(parsedData, null, 2);
const blob = new Blob([jsonData], { type: 'application/json' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'socid-parsed-data.json';
a.click();
URL.revokeObjectURL(url);
}
function parseHexInput(input) {
// Remove whitespace, newlines, and common prefixes
let cleanHex = input.replace(/\s+/g, '');
// Handle 0x prefixed hex values
cleanHex = cleanHex.replace(/0x/g, '');
// Validate hex characters
if (!/^[0-9A-Fa-f]*$/.test(cleanHex)) {
throw new Error('Invalid hex characters found. Please ensure input contains only hex digits (0-9, A-F).');
}
if (cleanHex.length === 0) {
throw new Error('No valid hex data found.');
}
if (cleanHex.length % 2 !== 0) {
throw new Error('Hex data length must be even (each byte requires 2 hex digits).');
}
// Convert to byte array
const bytes = [];
for (let i = 0; i < cleanHex.length; i += 2) {
bytes.push(parseInt(cleanHex.substr(i, 2), 16));
}
return new Uint8Array(bytes);
}
function parseSocIdData(binaryData) {
if (binaryData.length < 32) {
throw new Error('Insufficient data. SoC ID requires at least 32 bytes.');
}
const result = {
header: {},
publicRomInfo: {},
secureRomInfo: null
};
// Parse header (4 bytes)
const numBlocks = readUint32LE(binaryData, 0);
result.header.numBlocks = numBlocks;
// Parse public ROM info (28 bytes, starting at offset 4)
if (binaryData.length < 32) {
throw new Error('Insufficient data for public ROM info.');
}
result.publicRomInfo = parsePublicRomInfo(binaryData, 4);
// Parse secure ROM info if present (numBlocks > 1)
if (numBlocks > 1) {
if (binaryData.length < 200) {
throw new Error('Insufficient data for secure ROM info. Expected at least 200 bytes.');
}
result.secureRomInfo = parseSecureRomInfo(binaryData, 32);
}
return result;
}
function parsePublicRomInfo(data, offset) {
const info = {};
// struct SOCID_PubInfo_t
// uint8_t subBlockId;
info.subBlockId = data[offset];
// uint8_t size;
info.subBlockSize = data[offset + 1];
// uint8_t fixed[2]; - skip
// uint8_t devName[12];
const deviceNameBytes = data.slice(offset + 4, offset + 16);
info.deviceName = bytesToString(deviceNameBytes);
// uint32_t devType;
const deviceTypeBytes = data.slice(offset + 16, offset + 20);
info.deviceType = bytesToString(deviceTypeBytes);
// uint32_t dmscVersion;
const dmscVersionBytes = Array.from(data.slice(offset + 20, offset + 24));
dmscVersionBytes.reverse(); // Convert from little-endian
info.dmscRomVersion = dmscVersionBytes;
// uint32_t r5Version;
const r5VersionBytes = Array.from(data.slice(offset + 24, offset + 28));
r5VersionBytes.reverse(); // Convert from little-endian
info.r5RomVersion = r5VersionBytes;
return info;
}
function parseSecureRomInfo(data, offset) {
const info = {};
// struct SOCID_SecInfo_t
// uint8_t subBlockId;
info.secSubBlockId = data[offset];
// uint8_t size;
info.secSubBlockSize = data[offset + 1];
// uint16_t secPrime;
info.secPrime = readUint16LE(data, offset + 2);
// uint16_t keyRevision;
info.secKeyRevision = readUint16LE(data, offset + 4);
// uint16_t keyCount;
info.secKeyCount = readUint16LE(data, offset + 6);
// uint32_t tiRootKeyHash[16]; // 64 bytes
const tiMpkHashBytes = data.slice(offset + 8, offset + 72);
info.secTiMpkHash = bytesToHexString(tiMpkHashBytes);
// uint32_t custRootKeyHash[16]; // 64 bytes
const custMpkHashBytes = data.slice(offset + 72, offset + 136);
info.secCustMpkHash = bytesToHexString(custMpkHashBytes);
// uint32_t uniqueID[8]; // 32 bytes
const uniqueIdBytes = data.slice(offset + 136, offset + 168);
info.secUniqueId = bytesToHexString(uniqueIdBytes);
return info;
}
function readUint32LE(data, offset) {
return data[offset] |
(data[offset + 1] << 8) |
(data[offset + 2] << 16) |
(data[offset + 3] << 24);
}
function readUint16LE(data, offset) {
return data[offset] | (data[offset + 1] << 8);
}
function bytesToString(bytes) {
let result = '';
for (let i = 0; i < bytes.length; i++) {
if (bytes[i] === 0) break; // Stop at null terminator
if (bytes[i] >= 32 && bytes[i] <= 126) { // Printable ASCII
result += String.fromCharCode(bytes[i]);
}
}
return result;
}
function bytesToHexString(bytes) {
return Array.from(bytes)
.map(byte => byte.toString(16).padStart(2, '0'))
.join('');
}
function displayResults(data) {
let html = '';
// Header Information
html += `
<div class="result-group">
<h3>SoC ID Header Info</h3>
<div class="info-table">
<div class="info-row">
<span class="info-label">NumBlocks:</span>
<span class="info-value">${data.header.numBlocks}</span>
</div>
</div>
</div>
`;
// Public ROM Information
html += `
<div class="result-group">
<h3>SoC ID Public ROM Info</h3>
<div class="info-table">
<div class="info-row">
<span class="info-label">SubBlockId:</span>
<span class="info-value">${data.publicRomInfo.subBlockId}</span>
</div>
<div class="info-row">
<span class="info-label">SubBlockSize:</span>
<span class="info-value">${data.publicRomInfo.subBlockSize}</span>
</div>
<div class="info-row">
<span class="info-label">DeviceName:</span>
<span class="info-value device-name">${data.publicRomInfo.deviceName}</span>
</div>
<div class="info-row">
<span class="info-label">DeviceType:</span>
<span class="info-value device-type">${data.publicRomInfo.deviceType}</span>
</div>
<div class="info-row">
<span class="info-label">DMSC ROM Version:</span>
<span class="info-value version">[${data.publicRomInfo.dmscRomVersion.join(', ')}]</span>
</div>
<div class="info-row">
<span class="info-label">R5 ROM Version:</span>
<span class="info-value version">[${data.publicRomInfo.r5RomVersion.join(', ')}]</span>
</div>
</div>
</div>
`;
// Secure ROM Information (if present)
if (data.secureRomInfo) {
html += `
<div class="result-group">
<h3>SoC ID Secure ROM Info</h3>
<div class="info-table">
<div class="info-row">
<span class="info-label">Sec SubBlockId:</span>
<span class="info-value">${data.secureRomInfo.secSubBlockId}</span>
</div>
<div class="info-row">
<span class="info-label">Sec SubBlockSize:</span>
<span class="info-value">${data.secureRomInfo.secSubBlockSize}</span>
</div>
<div class="info-row">
<span class="info-label">Sec Prime:</span>
<span class="info-value">${data.secureRomInfo.secPrime}</span>
</div>
<div class="info-row">
<span class="info-label">Sec Key Revision:</span>
<span class="info-value">${data.secureRomInfo.secKeyRevision}</span>
</div>
<div class="info-row">
<span class="info-label">Sec Key Count:</span>
<span class="info-value">${data.secureRomInfo.secKeyCount}</span>
</div>
<div class="info-row">
<span class="info-label">Sec TI MPK Hash:</span>
<span class="info-value hash-value">${data.secureRomInfo.secTiMpkHash}</span>
</div>
<div class="info-row">
<span class="info-label">Sec Cust MPK Hash:</span>
<span class="info-value hash-value">${data.secureRomInfo.secCustMpkHash}</span>
</div>
<div class="info-row">
<span class="info-label">Sec Unique ID:</span>
<span class="info-value hash-value">${data.secureRomInfo.secUniqueId}</span>
</div>
</div>
</div>
`;
}
resultsContent.innerHTML = html;
clearError();
}
function displayError(message) {
resultsContent.innerHTML = `
<div class="error-message">
<h3>❌ Error</h3>
<p>${message}</p>
</div>
`;
resultsSection.style.display = 'block';
}
function clearError() {
const errorMessages = resultsContent.querySelectorAll('.error-message');
errorMessages.forEach(msg => msg.remove());
}