Module: Meshtastic::Admin::Firmware::SerialBootloader

Defined in:
lib/meshtastic/admin/firmware/serial_bootloader.rb

Overview

Native ROM protocol only: never uploads an executable flasher stub.

Constant Summary collapse

CHIP_IDS =
{ esp32: 0, esp32s3: 9, esp32c3: 5 }.freeze

Class Method Summary collapse

Class Method Details

.authorsObject



208
209
210
# File 'lib/meshtastic/admin/firmware/serial_bootloader.rb', line 208

public_class_method def self.authors
  "AUTHOR(S):\n        0day Inc. <[email protected]>\n"
end

.helpObject



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/meshtastic/admin/firmware/serial_bootloader.rb', line 212

public_class_method def self.help
  puts "USAGE:
    # Install and verify native ESP ROM firmware.
    #{self}.install(
      protocol: 'optional - :esp_rom only; defaults to :esp_rom',
      port: 'required - dedicated UART device path, not an active PhoneAPI connection',
      chip: 'required - :esp32, :esp32s3 or :esp32c3; UART ROM only',
      bytes: 'optional - raw unmerged application image, exclusive with firmware',
      firmware: 'optional - application .bin file path, exclusive with bytes',
      offset: 'required - known application partition address, >= 0x10000, sector aligned',
      flash_size: 'required - known physical flash capacity, power of two from 1 to 16 MiB',
      reset: 'optional - :classic DTR/RTS reset (default) or :none for manual ROM entry',
      timeout: 'optional - positive command deadline in seconds, default 120'
    )
    # Print author contact information.
    #{self}.authors
  "
end

.install(opts = {}) ⇒ Object



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
# File 'lib/meshtastic/admin/firmware/serial_bootloader.rb', line 14

public_class_method def self.install(opts = {})
  bytes = validate(opts.merge({}))
  chip = opts.fetch(:chip)
  offset = opts.fetch(:offset)
  port = UART.open(opts.fetch(:port), 115_200, '8N1')
  attrs = Termios.tcgetattr(port)
  attrs.cflag &= ~(Termios::CRTSCTS | Termios::HUPCL)
  Termios.tcsetattr(port, Termios::TCSANOW, attrs)
  reset_lines(io: port, bootloader: true) if opts.fetch(:reset, :classic) == :classic
  context = { io: port, timeout: opts.fetch(:timeout, 120) }
  synchronize(context.merge({}))
  identify(context.merge(chip: chip))

  command(context.merge(op: 13, payload: [0, 0].pack('V2')))
  command(context.merge(op: 11, payload: [0, opts.fetch(:flash_size), 65_536, 4096, 256, 65_535].pack('V6')))
  blocks = (bytes.bytesize + 1023) / 1024
  params = [bytes.bytesize, blocks, 1024, offset]
  params << 0 unless chip == :esp32
  command(context.merge(op: 2, payload: params.pack('V*')))
  blocks.times do |sequence|
    block = bytes.byteslice(sequence * 1024, 1024).ljust(1024, "\xff".b)
    payload = [1024, sequence, 0, 0].pack('V4') + block
    command(context.merge(op: 3, payload: payload, checksum: block.bytes.reduce(0xef, :^)))
  end
  md5 = command(context.merge(op: 19, payload: [offset, bytes.bytesize, 0, 0].pack('V4'), response_size: 32)).fetch(:data)
  raise IOError, 'ROM flash MD5 mismatch' unless md5.downcase == Digest::MD5.hexdigest(bytes)

  command(context.merge(op: 4, payload: [0].pack('V')))
  reset_lines(io: port, bootloader: false) if opts.fetch(:reset, :classic) == :classic
  { status: :verified, chip: chip, bytes: bytes.bytesize, offset: offset, md5: md5.downcase, sha256: Digest::SHA256.hexdigest(bytes), reboot_requested: true, boot_verified: false }
ensure
  port&.close
end