Go Down

Topic: Burn hex with optiLoader (Read 481 times) previous topic - next topic

vsavic

Mar 14, 2018, 05:04 pm Last Edit: Mar 16, 2018, 03:56 am by vsavic Reason: Linked the same question on SE and Adafruit
Can optiLoader be used to burn custom hex?

For example, there's hex representation stored in optiLoader.ino in `image_328`, if that value is replaced with content of my pre-compiled hex, should that work right out of the box or some additional tweaking is needed?

Also some additional comments can be found on SE and Adafruit.

westfw

I think optiloader should be able to handle any .hex file as long as it's less than 512 bytes.  (You do have to edit them into the correct C-style strings.)

Some of the "derivative" implementations from Adafruit and Nick Gammon get away from the 512 byte restriction, but the may used a binary array format rather than .hex-style text.  (note that there are utilities "out there" that will input a .hex file and spit out a C array...)


vsavic

Thanks for your reply @westfw.

I tried using the tool from Nick's git repo. But it says:

Quote
Error: Unkown bootloader start address.
And if I modify it like:

Code: [Select]
import sys
import os
import hashlib
from intelhex import IntelHex

def main():
    if len(sys.argv) != 2:
        print "Usage:", sys.argv[0], "some-file.hex"
        sys.exit(1)

    hexfile = sys.argv[1]

    if not os.path.isfile(hexfile):
        print "Error: File does not exist."
        sys.exit(2)

    hexF_file = IntelHex(hexfile)
    hexF_file_bin = hexF_file.tobinarray()

    line = ""
    for i in range(len(hexF_file_bin)):
        line += hex(hexF_file_bin[i]) + ', '
        if i % 16 == 15:
            print line
            line = ''
    if line:
        print line

    print '};'

if __name__ == "__main__":
    main()


The code even for simple blink example end's up with more than 2k lines,most of them are just like this:
Quote
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
So I'm not 100% sure if the conversion from HEX file to that byte array is appropriate.

Do you maybe know which other tool I can use to get it in proper format?

westfw

Can you provide access to the .hex (and/or source/build instructions) for the file you're dealing with?
A .hex file contains an address for each line data; if you convert to binary, that information is lost.  If you have a combined application/bootloader, or a bootloader at the end of memory where the tool didn't figure out it's start address, a straight binary file may include a lot of 0xFF bytes for the areas that don't contain any code.  (0xFF is the value of unprogrammed memory.)

vsavic

#4
Mar 15, 2018, 02:32 am Last Edit: Mar 15, 2018, 02:38 am by vsavic Reason: Updated message
@westfw sure, this is what I have for example in Blink.ino.hex:

Source is just a Blink sample from Arduino IDE.

Code: [Select]
:100000000C945C000C946E000C946E000C946E00CA
:100010000C946E000C946E000C946E000C946E00A8
:100020000C946E000C946E000C946E000C946E0098
:100030000C946E000C946E000C946E000C946E0088
:100040000C9415010C946E000C946E000C946E00D0
:100050000C946E000C946E000C946E000C946E0068
:100060000C946E000C946E00000000002400270029
:100070002A0000000000250028002B0004040404CE
:100080000404040402020202020203030303030342
:10009000010204081020408001020408102001021F
:1000A00004081020000000080002010000030407FB
:1000B000000000000000000011241FBECFEFD8E0B8
:1000C000DEBFCDBF21E0A0E0B1E001C01D92A930AC
:1000D000B207E1F70E945F010C94CE010C9400007E
:1000E000E1EBF0E02491EDE9F0E09491E9E8F0E053
:1000F000E491EE2309F43BC0222339F1233091F03F
:1001000038F42130A9F0223001F524B52F7D12C03A
:10011000273091F02830A1F02430B9F420918000EC
:100120002F7D03C0209180002F77209380000DC089
:1001300024B52F7724BD09C02091B0002F7703C0CC
:100140002091B0002F7D2093B000F0E0EE0FFF1F54
:10015000EE58FF4FA591B4912FB7F894EC9181110F
:1001600003C090959E2301C09E2B9C932FBF0895A2
:100170003FB7F8948091050190910601A091070185
:10018000B091080126B5A89B05C02F3F19F0019634
:10019000A11DB11D3FBFBA2FA92F982F8827820F0D
:1001A000911DA11DB11DBC01CD0142E0660F771F5D
:1001B000881F991F4A95D1F708958F929F92AF9209
:1001C000BF92CF92DF92EF92FF920E94B8004B0154
:1001D0005C0188EEC82E83E0D82EE12CF12C0E9421
:1001E000B800DC01CB0188199909AA09BB09883E2E
:1001F0009340A105B10558F021E0C21AD108E108E9
:10020000F10888EE880E83E0981EA11CB11CC11471
:10021000D104E104F10419F7FF90EF90DF90CF9043
:10022000BF90AF909F908F9008951F920F920FB63E
:100230000F9211242F933F938F939F93AF93BF936C
:100240008091010190910201A0910301B0910401FC
:100250003091000123E0230F2D3720F40196A11DDA
:10026000B11D05C026E8230F0296A11DB11D2093E4
:1002700000018093010190930201A0930301B093C8
:1002800004018091050190910601A0910701B091B0
:1002900008010196A11DB11D8093050190930601EF
:1002A000A0930701B0930801BF91AF919F918F91E7
:1002B0003F912F910F900FBE0F901F90189578943B
:1002C00084B5826084BD84B5816084BD85B582605B
:1002D00085BD85B5816085BD80916E00816080930C
:1002E0006E00109281008091810082608093810075
:1002F0008091810081608093810080918000816085
:10030000809380008091B10084608093B1008091DF
:10031000B00081608093B00080917A008460809307
:100320007A0080917A00826080937A0080917A00CE
:10033000816080937A0080917A00806880937A004F
:100340001092C100EDE9F0E02491E9E8F0E0849139
:10035000882399F090E0880F991FFC01E859FF4F1E
:10036000A591B491FC01EE58FF4F859194918FB700
:10037000F894EC91E22BEC938FBFC0E0D0E081E0E9
:100380000E9470000E94DD0080E00E9470000E94C8
:10039000DD002097A1F30E940000F1CFF894FFCF79
:00000001FF


I even tried this by just modifying it like this with adaLoader, but there I'm getting error as well saying:

Quote
Reading signature:950F
Searching for image...
  Found "optiboot_atmega328.hex" for atmega328P

Erasing chip

Setting fuses
  Set Lock Fuse to: 3F -> E000  Set Low Fuse to: FF -> A000  Set High Fuse to: DE -> A800  Set Ext Fuse to: 5 -> A400
Verifying fuses...
   Lock Fuse: 3F is 3F   Low Fuse: 0xFF is 0xFF   High Fuse: 0xDE is 0xDE   Ext Fuse: 0x5 is 0x5
Fuses set & verified
Page size: 128
Chip size: 32768
Writing address $0
Flashing page 0
  Commit Page: 0x0 -> 0x0
Writing address $80
Flashing page 80
  Commit Page: 0x40 -> 0x40
Writing address $100
Flashing page 100
  Commit Page: 0x80 -> 0x80
Writing address $180
Flashing page 180
  Commit Page: 0xC0 -> 0xC0
Writing address $200
Flashing page 200
  Commit Page: 0x100 -> 0x100
Writing address $280
Flashing page 280
  Commit Page: 0x140 -> 0x140
Writing address $300
Flashing page 300
  Commit Page: 0x180 -> 0x180
Writing address $380
Flashing page 380
  Commit Page: 0x1C0 -> 0x1C0
Writing address $400
No colon?

AdaBootLoader Bootstrap programmer (originally OptiLoader Bill Westfield (WestfW))
Code: [Select]
":100000000C945C000C946E000C946E000C946E00CA\n"
":100010000C946E000C946E000C946E000C946E00A8\n"
":100020000C946E000C946E000C946E000C946E0098\n"
":100030000C946E000C946E000C946E000C946E0088\n"
":100040000C9415010C946E000C946E000C946E00D0\n"
":100050000C946E000C946E000C946E000C946E0068\n"
":100060000C946E000C946E00000000002400270029\n"
":100070002A0000000000250028002B0004040404CE\n"
":100080000404040402020202020203030303030342\n"
":10009000010204081020408001020408102001021F\n"
":1000A00004081020000000080002010000030407FB\n"
":1000B000000000000000000011241FBECFEFD8E0B8\n"
":1000C000DEBFCDBF21E0A0E0B1E001C01D92A930AC\n"
":1000D000B207E1F70E945F010C94CE010C9400007E\n"
":1000E000E1EBF0E02491EDE9F0E09491E9E8F0E053\n"
":1000F000E491EE2309F43BC0222339F1233091F03F\n"
":1001000038F42130A9F0223001F524B52F7D12C03A\n"
":10011000273091F02830A1F02430B9F420918000EC\n"
":100120002F7D03C0209180002F77209380000DC089\n"
":1001300024B52F7724BD09C02091B0002F7703C0CC\n"
":100140002091B0002F7D2093B000F0E0EE0FFF1F54\n"
":10015000EE58FF4FA591B4912FB7F894EC9181110F\n"
":1001600003C090959E2301C09E2B9C932FBF0895A2\n"
":100170003FB7F8948091050190910601A091070185\n"
":10018000B091080126B5A89B05C02F3F19F0019634\n"
":10019000A11DB11D3FBFBA2FA92F982F8827820F0D\n"
":1001A000911DA11DB11DBC01CD0142E0660F771F5D\n"
":1001B000881F991F4A95D1F708958F929F92AF9209\n"
":1001C000BF92CF92DF92EF92FF920E94B8004B0154\n"
":1001D0005C0188EEC82E83E0D82EE12CF12C0E9421\n"
":1001E000B800DC01CB0188199909AA09BB09883E2E\n"
":1001F0009340A105B10558F021E0C21AD108E108E9\n"
":10020000F10888EE880E83E0981EA11CB11CC11471\n"
":10021000D104E104F10419F7FF90EF90DF90CF9043\n"
":10022000BF90AF909F908F9008951F920F920FB63E\n"
":100230000F9211242F933F938F939F93AF93BF936C\n"
":100240008091010190910201A0910301B0910401FC\n"
":100250003091000123E0230F2D3720F40196A11DDA\n"
":10026000B11D05C026E8230F0296A11DB11D2093E4\n"
":1002700000018093010190930201A0930301B093C8\n"
":1002800004018091050190910601A0910701B091B0\n"
":1002900008010196A11DB11D8093050190930601EF\n"
":1002A000A0930701B0930801BF91AF919F918F91E7\n"
":1002B0003F912F910F900FBE0F901F90189578943B\n"
":1002C00084B5826084BD84B5816084BD85B582605B\n"
":1002D00085BD85B5816085BD80916E00816080930C\n"
":1002E0006E00109281008091810082608093810075\n"
":1002F0008091810081608093810080918000816085\n"
":10030000809380008091B10084608093B1008091DF\n"
":10031000B00081608093B00080917A008460809307\n"
":100320007A0080917A00826080937A0080917A00CE\n"
":10033000816080937A0080917A00806880937A004F\n"
":100340001092C100EDE9F0E02491E9E8F0E0849139\n"
":10035000882399F090E0880F991FFC01E859FF4F1E\n"
":10036000A591B491FC01EE58FF4F859194918FB700\n"
":10037000F894EC91E22BEC938FBFC0E0D0E081E0E9\n"
":100380000E9470000E94DD0080E00E9470000E94C8\n"
":10039000DD002097A1F30E940000F1CFF894FFCF79\n"
":00000001FF\n"

vsavic

@westfw is it possible that the way how Arduino IDE generates this Blink hex is maybe different than the one which supposed to be generated and used as image with this Optiboot \ Adaboot sketch, and that's why there's an error while reading from it, ...or I am missing something else?

westfw

It sort-of looks like the Adaloader wants the .hex file to contain full pages of code (for whatever the page size of the chip happens to be.)  That seems like a surprising limitation; it would be true of bootloaders like optiboot that have a version number in the last possible bytes, but I don't think that most regular images will be like that.

Delta_G

Also on SE:  https://arduino.stackexchange.com/questions/50746/burn-hex-with-optiloader

OP, if you're going to sprinkle the same question all over the internet, please at least be considerate enough to link them together so nobody has to waste time giving you answers you already have. 
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

westfw

Meh.  I find it difficult to be upset with "cross posting" that is broader than a single forum provider.
Cross posting to the Microcontroller, Bootloader, and hardware developement topics of the arduino forums - bad.
Cross posting to one Arduino forum, and stackexchange, and AVRFreaks, Adafruit, Sparkfun...  that's OK with me.  Presumably there are lots of people that follow at least one such forum and are NOT interested in following all of them.


Delta_G

Presumably there are lots of people that follow at least one such forum and are NOT interested in following all of them.
Exactly.  I'm not saying he shouldn't cross post them there.  Just that it is would be considerate to give links to prevent wasting people's time.  How about if the poster gets his answer on SE and then you spend a while writing up some well thought out beautiful response the he'll never see because he already has an answer from there and isn't going to revisit the question. 
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

vsavic

@Delta_G I get what you are saying, but you can not just assume that someone will not revisit the question and share the joy of finding the solution for his problem. Anyway, you are right about linking them all together. So I put link to here as well.

Go Up