forked from microsoft/devicescript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinpatch.ts
More file actions
333 lines (294 loc) · 10.1 KB
/
binpatch.ts
File metadata and controls
333 lines (294 loc) · 10.1 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
import {
DeviceConfig,
ArchConfig,
serializeDcfg,
decodeDcfg,
decompileDcfg,
DcfgSettings,
parseAnyInt,
UF2File,
DCFG_MAGIC0,
DCFG_MAGIC1,
expandDcfgJSON,
jsonToDcfg,
boardInfos,
RepoInfo,
pinsInfo,
} from "@devicescript/compiler"
import { JSON5TryParse } from "@devicescript/interop"
import { HexInt } from "@devicescript/srvcfg"
import { readFile, writeFile } from "fs/promises"
import { read32, toHex } from "jacdac-ts"
import { basename, dirname, join, resolve } from "path"
import { error, isVerbose, log, verboseLog } from "./command"
export interface FileTypes {
uf2?: string
bin?: string
}
export interface BinPatchArgs {
binary: Uint8Array // UF2 or BIN file
type: keyof FileTypes
arch: ArchConfig
board: DeviceConfig
dcfg?: DcfgSettings
fstor?: Uint8Array
}
export interface BinPatchOptions extends FileTypes {
slug?: string
outdir?: string
elf?: string
generic?: boolean
fake?: boolean
}
function fatal(msg: string) {
error("fatal: " + msg)
process.exit(1)
}
function isDCFG(data: Uint8Array, off = 0) {
return (
read32(data, off) == DCFG_MAGIC0 && read32(data, off + 4) == DCFG_MAGIC1
)
}
async function patchUF2File(args: BinPatchArgs) {
const { binary, arch, board, fstor, dcfg } = args
if (!UF2File.isUF2(binary)) throw new Error("not a UF2 file")
const f = UF2File.fromFile(binary)
// needed for RP2040-E14
const align = parseAnyInt(arch.uf2Align)
if (align) {
const amask = align - 1
let maxAddr = 0
for (const b of f.fromBlocks) {
maxAddr = Math.max(b.targetAddr + b.payloadSize, maxAddr)
}
if (maxAddr & amask) {
const left = align - (maxAddr & amask)
const filler = new Uint8Array(left)
filler.fill(0xff)
f.writeBytes(maxAddr, filler)
}
}
if (dcfg) {
const dcfgOff = parseAnyInt(arch.dcfgOffset)
let dcfgBuf = serializeDcfg(dcfg)
const tmp = f.readBytes(dcfgOff, 12)
if (tmp) {
if (isDCFG(tmp)) {
log(`patching existing config`)
dcfgBuf = padCfg(dcfgBuf, read32(tmp, 8))
} else {
throw new Error(`data already at DCFG`)
}
}
f.writeBytes(dcfgOff, dcfgBuf)
}
if (fstor) {
const fstorOff = parseAnyInt(board.fstorOffset ?? arch.fstorOffset)
if (f.readBytes(fstorOff, 4))
throw new Error(`data already at flash offset`)
f.writeBytes(fstorOff, fstor)
}
return f.serialize()
}
function padCfg(cfg: Uint8Array, minsz: number) {
if (minsz > 1024 * 1024) throw new Error("too large config!")
const res = new Uint8Array(Math.max(cfg.length, minsz))
res.set(cfg)
return res
}
async function patchBinFile(args: BinPatchArgs) {
const { binary, arch, dcfg, board, fstor } = args
const shift = parseAnyInt(arch.binFlashOffset) ?? 0
const dcfgOff = parseAnyInt(arch.dcfgOffset) - shift
const fstorOff = parseAnyInt(board.fstorOffset ?? arch.fstorOffset) - shift
const pageSize = board.flashPageSize ?? arch.flashPageSize ?? 4096
let dcfgBuf = dcfg ? serializeDcfg(dcfg) : null
if (UF2File.isUF2(binary)) throw new Error("expecting BIN, not UF2 file")
if (dcfgOff > 16 * 1024 * 1024) throw new Error("offset too large for BIN")
if (fstorOff > 16 * 1024 * 1024)
throw new Error("fstor offset too large for BIN")
let minSize = binary.length
if (dcfg) minSize = Math.max(minSize, dcfgOff + dcfgBuf.length)
if (fstor) minSize = Math.max(minSize, fstorOff + fstor.length)
minSize = (minSize + (pageSize - 1)) & ~(pageSize - 1)
verboseLog(`size: ${binary.length} -> ${minSize} bytes`)
const output = new Uint8Array(minSize)
output.fill(0xff)
output.set(binary)
if (dcfg) {
if (isDCFG(output, dcfgOff)) {
verboseLog("patching existing")
dcfgBuf = padCfg(dcfgBuf, read32(output, dcfgOff + 8))
} else {
const slice = toHex(output.slice(dcfgOff, dcfgOff + 8))
if (slice == "0000000000000000" || slice == "ffffffffffffffff")
verboseLog("patching 00 or ff")
else throw new Error("data already at patch point!")
}
output.set(dcfgBuf, dcfgOff)
}
if (fstor) {
for (let i = 0; i < fstor.length; ++i)
if (output[fstorOff + i] != 0x00 && output[fstorOff + i] != 0xff)
throw new Error("data already at fstor patch point!")
output.set(fstor, fstorOff)
}
return output
}
function compileBoard(arch: ArchConfig, devcfg: DeviceConfig) {
const dcfg = jsonToDcfg(devcfg, true)
if (!dcfg["devName"]) throw new Error(`no devName`)
if (!dcfg["productId"]) throw new Error(`no productId`)
const { desc, errors } = pinsInfo(arch, devcfg)
if (errors.length) throw new Error(errors.join("\n"))
if (isVerbose) {
verboseLog(desc)
verboseLog(JSON.stringify(dcfg, null, 4))
const ser = serializeDcfg(dcfg)
const dec = decodeDcfg(ser)
verboseLog(dec.errors.join("\n"))
verboseLog(JSON.stringify(decompileDcfg(dec.settings), null, 4))
} else if (devcfg.$custom) {
log(desc)
}
return dcfg
}
export async function compileDcfgFile(fn: string) {
if (!fn.endsWith(".board.json"))
throw new Error("board file has to match *.board.json")
const folder = dirname(fn)
const readF = (f: string) => readFile(resolve(folder, f), "utf-8")
const arch: ArchConfig = JSON5TryParse(await readF("arch.json"))
if (arch?.dcfgOffset === undefined || arch?.id === undefined)
throw new Error(`no dcfgOffset or id in arch.json`)
const json: DeviceConfig = await expandDcfgJSON(basename(fn), readF)
for (const s of json.$services ?? []) {
if (!s.name) s.name = s.service
}
for (const s of json.services ?? []) {
if (!s.name) s.name = s.service
}
json.id = basename(fn, ".board.json")
json.archId = arch.id
try {
const dcfg = compileBoard(arch, json)
return { json, dcfg, arch }
} catch (e) {
throw new Error(`${fn}: ${e.message}`)
}
}
const patch: Record<
keyof FileTypes,
(args: BinPatchArgs) => Promise<Uint8Array>
> = {
bin: patchBinFile,
uf2: patchUF2File,
}
export async function patchCustomBoard(
fn: string,
board: DeviceConfig,
arch: ArchConfig
) {
board.$custom = true
const dcfg = compileBoard(arch, board)
const type = fn.replace(/.*\./, "") as keyof FileTypes
if (!patch[type]) throw new Error(`unknown file format: ${type}`)
const binary = await readFile(fn)
return await patchFile({ type, binary, arch, board, dcfg })
}
export async function patchFstorToBoard(
fn: string,
board: DeviceConfig,
arch: ArchConfig,
fstor: Uint8Array
) {
const type = fn.replace(/.*\./, "") as keyof FileTypes
if (!patch[type]) throw new Error(`unknown file format: ${type}`)
const binary = await readFile(fn)
return await patchFile({ type, binary, arch, board, fstor })
}
async function patchFile(args: BinPatchArgs) {
const f = patch[args.type]
return f(args)
}
export async function binPatch(files: string[], options: BinPatchOptions) {
let binFn = ""
let ft: keyof FileTypes = undefined
for (const k of Object.keys(patch) as (keyof FileTypes)[]) {
const opt = options[k]
if (!opt) continue
if (ft) fatal(`both --${ft} and --${k} provided`)
ft = k
binFn = opt
}
if (!ft) fatal("no file type provided")
const binary = options.fake ? null : await readFile(binFn)
const outpath = options.outdir || "dist"
const outext = options.uf2 ? ".uf2" : ".bin"
const binext = (off: HexInt) => {
const v = parseAnyInt(off)
return v == undefined ? outext : `-0x${v.toString(16)}${outext}`
}
const infoPath = join(outpath, "info.json")
const info: RepoInfo = {
archs: {},
boards: {},
}
if (options.slug) info.repoUrl = "https://github.com/" + options.slug
const ex: RepoInfo = JSON5TryParse(
await readFile(infoPath, "utf-8").then(
r => r,
_ => ""
)
)
if (ex) Object.assign(info, ex)
try {
for (const fn of files) {
log(`processing ${fn}...`)
const { dcfg, json, arch } = await compileDcfgFile(fn)
const suff = binext(arch.binFlashOffset)
const outname = (devid: string, ext = suff) =>
join(outpath, `devicescript-${arch.id}-${devid}${ext}`)
const outp = outname(json.id)
if (info.repoUrl)
json.$fwUrl =
info.repoUrl + "/releases/latest/download/" + basename(outp)
info.archs[arch.id] = arch
info.boards[json.id] = json
if (options.fake) continue
const patched = await patchFile({
type: ft,
binary,
arch,
board: json,
dcfg,
})
log(`writing ${outp}: ${patched.length} bytes`)
await writeFile(outp, patched)
if (options.generic || arch.binGenericFlashOffset !== undefined) {
const offpatched = parseAnyInt(arch.binFlashOffset)
const offgen = parseAnyInt(arch.binGenericFlashOffset)
let off = 0
if (offgen !== undefined) off = offgen - offpatched
await writeFile(
outname("generic", binext(offgen ?? offpatched)),
binary.slice(off)
)
const elfFileBuf = await readFile(
options.elf ?? binFn.replace(/\.[^\.]+$/, ".elf")
)
await writeFile(outname("generic", ".elf"), elfFileBuf)
}
}
} catch (e) {
verboseLog(e.stack)
fatal(e.message)
}
await writeFile(infoPath, JSON.stringify(info))
await writeFile(
join(outpath, "info.md"),
boardInfos(info)
.map(b => b.markdown)
.join("\n\n")
)
}