summaryrefslogtreecommitdiffstats
path: root/upgrade/generate-data.rb
blob: c54f679ca0d006f63bece9746740983e41299e35 (plain)
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
require_relative "../ruby/micronucleus.rb"

data = HexProgram.new(open ARGV.first)

puts data.instance_variable_get(:@bytes).inspect

data = data.bytes

# find start address
start_address = 0
start_address += 1 while data[start_address] == 0xFF

# trim blank padding data from start of data
start_address.times { data.shift }

# if data is an odd number of bytes make it even
data.push 0xFF while (data.length % 2) != 0

puts "Length: #{data.length}"
puts "Start address: #{start_address}"

File.open "bootloader_data.c", "w" do |file|
  file.puts "// This file contains the bootloader data itself and the address to install the bootloader at"
  file.puts "// Use generate-data.rb with ruby 1.9 to generate these values from a hex file"
  file.puts "// Generated from #{ARGV.first} at #{Time.now}"
  file.puts ""
  file.puts "uint16_t bootloader_data[#{data.length / 2}] PROGMEM = {"
  file.puts data.each_slice(2).map { |big_end, little_end|
    "0x#{ ((little_end * 256) + big_end).to_s(16).rjust(4, '0') }"
  }.join(', ')
  file.puts "};"
  file.puts ""
  file.puts "uint16_t bootloader_address = #{start_address};"
end