Skip to content

Improve hex merger #295

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Start introducing a proper hex merger
Uses github.com/marcinbor85/gohex as additiona dependency
  • Loading branch information
facchinm committed Sep 14, 2018
commit 1707aec9fcdceb7d7261a0cebcd838fc067de21a
90 changes: 37 additions & 53 deletions merge_sketch_with_bootloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@
package builder

import (
"os"
"path/filepath"

"github.com/arduino/arduino-builder/constants"
"github.com/arduino/arduino-builder/i18n"
"github.com/arduino/arduino-builder/types"
"github.com/arduino/arduino-builder/utils"
"os"
"path/filepath"
"strings"
"github.com/marcinbor85/gohex"
)

type MergeSketchWithBootloader struct{}
Expand Down Expand Up @@ -80,70 +80,54 @@ func (s *MergeSketchWithBootloader) Run(ctx *types.Context) error {

mergedSketchPath := filepath.Join(filepath.Dir(builtSketchPath), sketchFileName+".with_bootloader.hex")

err := merge(builtSketchPath, bootloaderPath, mergedSketchPath)
// Ignore merger errors for the first iteration
merge(builtSketchPath, bootloaderPath, mergedSketchPath)

return err
return nil
}

func hexLineOnlyContainsFF(line string) bool {
//:206FE000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFB1
if len(line) <= 11 {
return false
}
byteArray := []byte(line)
for _, char := range byteArray[9:(len(byteArray) - 2)] {
if char != 'F' {
return false
}
func merge(builtSketchPath, bootloaderPath, mergedSketchPath string) error {
bootFile, err := os.Open(bootloaderPath)
if err != nil {
return err
}
return true
}

func extractActualBootloader(bootloader []string) []string {

var realBootloader []string
defer bootFile.Close()

// skip until we find a line full of FFFFFF (except address and checksum)
for i, row := range bootloader {
if hexLineOnlyContainsFF(row) {
realBootloader = bootloader[i:len(bootloader)]
break
}
mem_boot := gohex.NewMemory()
err = mem_boot.ParseIntelHex(bootFile)
if err != nil {
return err
}

// drop all "empty" lines
for i, row := range realBootloader {
if !hexLineOnlyContainsFF(row) {
realBootloader = realBootloader[i:len(realBootloader)]
break
}
buildFile, err := os.Open(builtSketchPath)
if err != nil {
return err
}
defer buildFile.Close()

if len(realBootloader) == 0 {
// we didn't find any line full of FFFF, thus it's a standalone bootloader
realBootloader = bootloader
mem_sketch := gohex.NewMemory()
err = mem_sketch.ParseIntelHex(buildFile)
if err != nil {
return err
}

return realBootloader
}
mem_merge := gohex.NewMemory()

func merge(builtSketchPath, bootloaderPath, mergedSketchPath string) error {
sketch, err := utils.ReadFileToRows(builtSketchPath)
if err != nil {
return i18n.WrapError(err)
for _, segment := range mem_boot.GetDataSegments() {
err = mem_merge.AddBinary(segment.Address, segment.Data)
}
sketch = sketch[:len(sketch)-2]

bootloader, err := utils.ReadFileToRows(bootloaderPath)
if err != nil {
return i18n.WrapError(err)
for _, segment := range mem_sketch.GetDataSegments() {
err = mem_merge.AddBinary(segment.Address, segment.Data)
}

realBootloader := extractActualBootloader(bootloader)

for _, row := range realBootloader {
sketch = append(sketch, row)
mergeFile, err := os.Create(mergedSketchPath)
if err != nil {
return err
}
defer mergeFile.Close()

return utils.WriteFile(mergedSketchPath, strings.Join(sketch, "\n"))
mem_merge.DumpIntelHex(mergeFile, 32)
return nil
//bytes := mem_merge.ToBinary(0, len, 0xFF)
//return utils.WriteFile(mergedSketchPath, string(bytes))
}