Skip to content
Merged
4 changes: 2 additions & 2 deletions Apps/FirmwareUpdater/Sources/BackupView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ final class BackupModel {
let outcome = await UpdateModeStep(midi: CoreMIDISend(), apps: WorkspaceApps()).enter(typed: typed)
typed = ""
switch outcome {
case .sent: stepResult = "Sent. The display chip restarts in update mode: give it a few seconds, then back it up. It stays in update mode until it's flashed; whether a power cycle ends it isn't known yet. Denon's own updater flashes a chip in update mode, so it is the way back."
case .sent: stepResult = UpdateModeStep.sentText
case .refused(let reason): stepResult = reason
}
}
Expand Down Expand Up @@ -55,7 +55,7 @@ final class BackupModel {

static var known: KnownFirmware {
let numbers = (try? Catalog.builtIn().knownNumbers) ?? KnownNumbers.none
return KnownFirmware(display: numbers.display, topPanel: numbers.topPanel)
return KnownFirmware(display: numbers.display, topPanel: numbers.topPanel, displayOurs: numbers.displayOurs)
}

static func stamp() -> String {
Expand Down
51 changes: 50 additions & 1 deletion Apps/FirmwareUpdater/Sources/IOKitDFU.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,20 @@ enum IOKitDFU {
var found: [Found] = []
while case let service = IOIteratorNext(iterator), service != 0 {
var name = ""
// The interface only for its string index, then closed: an open interface keeps its device from opening
// (cos, da45140d on the owner's unit), and the raw read needs the device.
var index: UInt8 = 0
if let interface = try? IOUSBHostInterface(__ioService: service, options: [], queue: nil, interestHandler: nil) {
let index = interface.interfaceDescriptor.pointee.iInterface
index = interface.interfaceDescriptor.pointee.iInterface
interface.destroy()
}
// Raw first: the display chip's bootloader fails IOUSBHost's string read ("Failed to get descriptor") but
// answers a full 255-byte GET_DESCRIPTOR (cos, on the owner's unit). The top panel chip answers both.
if let raw = rawString(index: index, interface: service) {
name = raw
} else if let interface = try? IOUSBHostInterface(__ioService: service, options: [], queue: nil, interestHandler: nil) {
name = (try? interface.__string(with: Int(index), languageID: 0x0409)) as String? ?? ""
if name.isEmpty { FileLog.shared.log("update mode: alt name not read by IOUSBHost either") }
interface.destroy()
}
var location: UInt32 = 0
Expand All @@ -38,6 +49,44 @@ enum IOKitDFU {
}
return found
}

/// String descriptor `index` (US English) by a raw GET_DESCRIPTOR of 255 bytes to the interface's device. A read:
/// no DFU request, nothing sent to the chip's flash. Nil when it fails or isn't a string descriptor.
private static func rawString(index: UInt8, interface service: io_service_t) -> String? {
guard index != 0 else {
FileLog.shared.log("update mode: alt name, no string index")
return nil
}
var parent: io_registry_entry_t = 0
let parentCode = IORegistryEntryGetParentEntry(service, kIOServicePlane, &parent)
guard parentCode == KERN_SUCCESS else {
FileLog.shared.log("update mode: alt name, no parent device (code \(parentCode))")
return nil
}
defer { IOObjectRelease(parent) }
let device: IOUSBHostDevice
do {
device = try IOUSBHostDevice(__ioService: parent, options: [], queue: nil, interestHandler: nil)
} catch {
FileLog.shared.log("update mode: alt name, device open failed (code \((error as NSError).code))")
return nil
}
defer { device.destroy() }
let request = IOUSBDeviceRequest(bmRequestType: 0x80, bRequest: 6, wValue: 0x0300 | UInt16(index), wIndex: 0x0409,
wLength: StringDescriptor.requestLength)
let data = NSMutableData(length: Int(StringDescriptor.requestLength))!
var transferred = 0
do {
try device.__send(request, data: data, bytesTransferred: &transferred, completionTimeout: 2)
} catch {
FileLog.shared.log("update mode: alt name, raw read failed (code \((error as NSError).code))")
return nil
}
let bytes = [UInt8](Data(referencing: data).prefix(transferred))
let text = StringDescriptor.text(bytes)
FileLog.shared.log("update mode: alt name, raw read \(StringDescriptor.header(bytes)), " + (text == nil ? "not a string" : "read"))
return text
}
}

/// Control requests to a DFU interface over IOUSBHost. Wrapped in ReadOnlyTransport by the caller.
Expand Down
33 changes: 23 additions & 10 deletions Packages/FirmwareUpdaterKit/Sources/FirmwareCatalog/Catalog.swift
Original file line number Diff line number Diff line change
Expand Up @@ -165,21 +165,25 @@ public struct Catalog: Sendable {

/// The numbers the sets' stock firmware is known by (#692). A number that isn't one fails the load.
private static func numbers(_ sets: [File.Set]) throws -> KnownNumbers {
var display: [UInt32: String] = [:], topPanel: [String: String] = [:]
var display: [UInt32: String] = [:], topPanel: [String: String] = [:], displayOurs: [UInt32: String] = [:]
for set in sets {
guard let known = set.known else { continue }
if let crc = known.wd_crc {
guard crc.wholeMatch(of: /0x[0-9A-F]{8}/) != nil, let value = UInt32(crc.dropFirst(2), radix: 16) else {
throw CatalogError.badNumber(crc)
}
display[value] = set.wd
}
if let crc = known.wd_crc { display[try Self.crc(crc)] = set.wd }
for ours in known.wd_ours ?? [] { displayOurs[try Self.crc(ours.crc)] = ours.label }
if let hash = known.tp_sha256 {
guard hash.wholeMatch(of: /[0-9a-f]{64}/) != nil else { throw CatalogError.badNumber(hash) }
topPanel[hash] = set.tp
}
}
return KnownNumbers(display: display, topPanel: topPanel)
return KnownNumbers(display: display, topPanel: topPanel, displayOurs: displayOurs)
}

/// "0x" and 8 uppercase hex digits, or the load fails.
private static func crc(_ text: String) throws -> UInt32 {
guard text.wholeMatch(of: /0x[0-9A-F]{8}/) != nil, let value = UInt32(text.dropFirst(2), radix: 16) else {
throw CatalogError.badNumber(text)
}
return value
}

/// The SHA of every panel build the catalog lists, carried or not.
Expand All @@ -206,7 +210,13 @@ public struct Catalog: Sendable {
}
struct Set: Decodable { let wd: String; let tp: String; let entries: [Entry]; var known: Known? = nil }
/// The numbers this set's stock firmware is known by (#692): "0x" and 8 hex digits; 64 hex digits.
struct Known: Decodable { let wd_crc: String?; let tp_sha256: String? }
/// wd_ours: our own wd images (the drawn display), by trailer CRC, each with the label the screen shows.
struct Known: Decodable {
struct Ours: Decodable { let crc: String; let label: String }
let wd_crc: String?
let tp_sha256: String?
var wd_ours: [Ours]? = nil
}
struct Entry: Decodable {
let kind: LC6000Entry.Kind
let chip: Chip
Expand All @@ -224,12 +234,15 @@ public struct Catalog: Sendable {
public struct KnownNumbers: Sendable, Hashable {
public let display: [UInt32: String]
public let topPanel: [String: String]
/// Our own wd images on top of a stock one (the drawn display), by trailer CRC, to the label the screen shows.
public let displayOurs: [UInt32: String]
/// Nothing known: every version refused for updates, backups still allowed.
public static let none = KnownNumbers(display: [:], topPanel: [:])

public init(display: [UInt32: String], topPanel: [String: String]) {
public init(display: [UInt32: String], topPanel: [String: String], displayOurs: [UInt32: String] = [:]) {
self.display = display
self.topPanel = topPanel
self.displayOurs = displayOurs
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
{
"wd": "00.50",
"tp": "00.31",
"known": { "wd_crc": "0x3A7CE2CA", "tp_sha256": "1f97959902b2febae979e47ab9a9c09b5b362d17e5736ac110df5f1a761a76cc" },
"known": { "wd_crc": "0x3A7CE2CA", "tp_sha256": "1f97959902b2febae979e47ab9a9c09b5b362d17e5736ac110df5f1a761a76cc",
"wd_ours": [{ "crc": "0x33A7BEBB", "label": "00.50 with slot 16 and DeckBridge's drawn display (block A 07da7ec9)" }] },
"entries": [
{ "kind": "patch", "chip": "wd", "file": "lc6000/wd-00.50-show-slot-16.patch" },
{ "kind": "patch", "chip": "tp", "file": "lc6000/tp-00.31-no-deck-select-on-hello.patch" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ public enum PatchedImage {
/// every edit's new bytes are there, the display image's CRC is good, and with the old bytes back it's stock.
public static func state(of app: [UInt8], chip: Chip, edits: [PatchEdit], known: KnownNumbers) -> PatchState {
if let version = stockVersion(app, chip: chip, known: known) { return .stock(version) }
// One of our own images on top of stock (the drawn display), known by its CRC: already up to date.
if chip == .wd, let label = DisplayTrailer.crc(of: app).flatMap({ known.displayOurs[$0] }) { return .alreadyPatched(label) }
// apply checks the display CRC before it builds on the image, so a damaged one is refused here too.
let undone = edits.map { PatchEdit(address: $0.address, old: $0.new, new: $0.old) }
guard let stock = try? apply(undone, chip: chip, to: app),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/// A USB string descriptor from a raw GET_DESCRIPTOR (bLength, type 3, UTF-16LE text). The display chip's ROM bootloader
/// answers only the full 255-byte request, so the updater reads the alt name raw (cos, on the owner's unit).
public enum StringDescriptor {
/// The full-length request's wLength.
public static let requestLength: UInt16 = 255

/// The text, or nil when `bytes` isn't a whole string descriptor.
public static func text(_ bytes: [UInt8]) -> String? {
guard bytes.count >= 2, bytes[1] == 0x03 else { return nil }
let length = Int(bytes[0])
guard length >= 2 else { return nil }
// A bLength past what came is the bootloader's own count: the text is what came.
let body = bytes[2..<min(length, bytes.count)]
let units = stride(from: body.startIndex, to: body.endIndex - 1, by: 2).map { UInt16(bytes[$0]) | UInt16(bytes[$0 + 1]) << 8 }
return String(decoding: units, as: UTF16.self)
}

/// For the log when a read isn't a string: bLength, type and the count, never the text.
public static func header(_ bytes: [UInt8]) -> String {
guard bytes.count >= 2 else { return "\(bytes.count) bytes" }
return "bLength \(bytes[0]), type \(bytes[1]), \(bytes.count) bytes"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ public protocol MIDISend: Sendable {
/// A port of tools/lc6000-firmware/enter-update-mode.swift. It writes nothing to flash, but the chip stays in update
/// mode until it is flashed or power-cycled.
public struct UpdateModeStep: Sendable {
/// What the screen says once the step is sent. A power cycle ends update mode: the owner saw it (2026-11-22).
public static let sentText = "Sent. The display chip restarts in update mode: give it a few seconds, then back it up. "
+ "A power cycle ends update mode, and the chip starts its own firmware again."

public enum Outcome: Sendable, Hashable {
case sent
case refused(String)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,22 @@ struct KnownNumbersTests {
@Test("A known number that isn't one: the catalog doesn't load",
arguments: [#"{"wd_crc": "3A7CE2CA", "tp_sha256": null}"#,
#"{"wd_crc": "0x3A7CE2C", "tp_sha256": null}"#,
#"{"wd_crc": null, "tp_sha256": "1690"}"#])
#"{"wd_crc": null, "tp_sha256": "1690"}"#,
#"{"wd_crc": null, "tp_sha256": null, "wd_ours": [{"crc": "33A7BEBB", "label": "x"}]}"#])
func badNumber(known: String) throws {
let folder = FileManager.default.temporaryDirectory.appending(path: "known-\(UUID().uuidString)")
try FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true)
try #"{"panel": [], "lc6000": [{"wd": "00.50", "tp": "00.31", "entries": [], "known": \#(known)}]}"#
.write(to: folder.appending(path: "catalog.json"), atomically: true, encoding: .utf8)
#expect(throws: CatalogError.self) { try Catalog(directory: folder) }
}

/// The owner's wd chip, backed up 2026-11-22 (#692, cos): byte for byte the image flashed 11-19, stock 00.50 with the
/// slot-16 patch and the drawn display's block A (sha1 07da7ec9…). Its trailer CRC is ours, so it's up to date.
@Test("The built-in catalog knows our drawn-display wd image by its trailer CRC")
func drawnDisplayImage() throws {
let known = try Catalog.builtIn().knownNumbers
#expect(known.displayOurs == [0x33A7_BEBB: "00.50 with slot 16 and DeckBridge's drawn display (block A 07da7ec9)"])
#expect(known.display[0x33A7_BEBB] == nil)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,15 @@ struct PatchStateTests {
#expect(PatchedImage.state(of: patched, chip: .tp, edits: edits, known: known) == .alreadyPatched("00.31"))
#expect(PatchedImage.state(of: stock, chip: .tp, edits: edits, known: .none) == .unknown)
}

@Test("wd: an image of ours the catalog knows by its CRC (the drawn display) is up to date, by its label")
func displayOurs() throws {
let v = try Self.vector("wd 00.50 slot-16")
let stock = V.element(v), edits = try V.edits(v)
// Stand-in for the drawn image: any image with a good trailer whose stock the catalog doesn't know.
let ours = try PatchedImage.apply(edits, chip: .wd, to: stock)
let crc = try #require(DisplayTrailer.crc(of: ours))
let known = KnownNumbers(display: [:], topPanel: [:], displayOurs: [crc: "00.50 drawn"])
#expect(PatchedImage.state(of: ours, chip: .wd, edits: edits, known: known) == .alreadyPatched("00.50 drawn"))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import Testing
import UnitCheck

/// A USB string descriptor as a raw GET_DESCRIPTOR returns it: bLength, type 3, then UTF-16LE. The display chip's ROM
/// bootloader only answers the full 255-byte request (cos, the owner's unit: IOUSBHost's string read failed on it).
@Suite("Update mode: a raw string descriptor")
struct StringDescriptorTests {
static func descriptor(_ text: String, length: Int? = nil) -> [UInt8] {
let body = text.utf16.flatMap { [UInt8($0 & 0xFF), UInt8($0 >> 8)] }
return [UInt8(length ?? body.count + 2), 0x03] + body
}

@Test("The display chip's alt name, 112 bytes: read whole")
func displayChipName() {
let name = "@Internal Flash /0x08000000/04*016Kg,01*064Kg,07*128Kg"
let bytes = Self.descriptor(name)
#expect(bytes.count == 112)
#expect(StringDescriptor.text(bytes) == name)
}

@Test("Trailing bytes past bLength are ignored")
func pastLength() {
#expect(StringDescriptor.text(Self.descriptor("@Flash") + [0x41, 0x00]) == "@Flash")
}

@Test("Not a string descriptor, too short, or bLength past the bytes: nil",
arguments: [[], [0x02], [0x04, 0x02, 0x41, 0x00]] as [[UInt8]])
func notAString(bytes: [UInt8]) {
#expect(StringDescriptor.text(bytes) == nil)
}

@Test("bLength past the bytes that came (a ROM bootloader's own count): the text is what came")
func lengthPastBytes() {
#expect(StringDescriptor.text(Self.descriptor("@Flash", length: 0xFF)) == "@Flash")
}

@Test("For the log: bLength, type and the count, never the text")
func header() {
#expect(StringDescriptor.header(Self.descriptor("@Flash")) == "bLength 14, type 3, 14 bytes")
#expect(StringDescriptor.header([0x09]) == "1 bytes")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,12 @@ struct SysEx7Tests {
#expect(SysEx7.packets(try kitUpdateModeMessage()) == [[0x3016_0002, 0x0B01_1072], [0x3032_0000, 0x0000_0000]])
#expect(SysEx7.packets([0xF0, 0x7E, 0x00, 0x06, 0x01, 0xF7]) == [[0x3004_7E00, 0x0601_0000]])
}

/// The owner saw a power cycle end the display chip's update mode (2026-11-22): the screen says so, with no fear.
@Test("After the step: back it up now, and a power cycle ends update mode")
func sentText() {
#expect(UpdateModeStep.sentText.contains("A power cycle ends update mode"))
#expect(!UpdateModeStep.sentText.contains("isn't known"))
#expect(!UpdateModeStep.sentText.contains("way back"))
}
}
2 changes: 1 addition & 1 deletion docs/LC6000-RECOVERY.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Work down the steps, and stop at the first one that brings the unit back.

Switch the LC6000 off, wait 5 seconds, and switch it on with no buttons held.

Update mode only lasts until a power cycle. A chip whose update was written and checked starts its new image now. A chip that was only read starts its old one. This has been seen on hardware for both chips (2026-11-19).
Update mode only lasts until a power cycle. A chip whose update was written and checked starts its new image now. A chip that was only read starts its old one. This has been seen on hardware for both chips (2026-11-19), and again by the owner for the display chip after the updater's update-mode step (2026-11-22).

## 2. Restore your backup with the updater

Expand Down
Loading