Skip to content

Commit f4485f4

Browse files
deadprogramaykevl
authored andcommitted
uf2: extract target address from ELF to accomodate the differences with M0 vs M4 processors
Signed-off-by: Ron Evans <[email protected]>
1 parent 55144ad commit f4485f4

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

uf2.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,21 @@ import (
1515
// ConvertELFFileToUF2File converts an ELF file to a UF2 file.
1616
func ConvertELFFileToUF2File(infile, outfile string) error {
1717
// Read the .text segment.
18-
_, data, err := ExtractROM(infile)
18+
targetAddress, data, err := ExtractROM(infile)
1919
if err != nil {
2020
return err
2121
}
2222

23-
output, _ := ConvertBinToUF2(data)
23+
output, _ := ConvertBinToUF2(data, uint32(targetAddress))
2424
return ioutil.WriteFile(outfile, output, 0644)
2525
}
2626

2727
// ConvertBinToUF2 converts the binary bytes in input to UF2 formatted data.
28-
func ConvertBinToUF2(input []byte) ([]byte, int) {
28+
func ConvertBinToUF2(input []byte, targetAddr uint32) ([]byte, int) {
2929
blocks := split(input, 256)
3030
output := make([]byte, 0)
3131

32-
bl := NewUF2Block()
32+
bl := NewUF2Block(targetAddr)
3333
bl.SetNumBlocks(len(blocks))
3434

3535
for i := 0; i < len(blocks); i++ {
@@ -44,10 +44,9 @@ func ConvertBinToUF2(input []byte) ([]byte, int) {
4444
}
4545

4646
const (
47-
uf2MagicStart0 = 0x0A324655 // "UF2\n"
48-
uf2MagicStart1 = 0x9E5D5157 // Randomly selected
49-
uf2MagicEnd = 0x0AB16F30 // Ditto
50-
uf2StartAddress = 0x2000
47+
uf2MagicStart0 = 0x0A324655 // "UF2\n"
48+
uf2MagicStart1 = 0x9E5D5157 // Randomly selected
49+
uf2MagicEnd = 0x0AB16F30 // Ditto
5150
)
5251

5352
// UF2Block is the structure used for each UF2 code block sent to device.
@@ -65,11 +64,11 @@ type UF2Block struct {
6564
}
6665

6766
// NewUF2Block returns a new UF2Block struct that has been correctly populated
68-
func NewUF2Block() *UF2Block {
67+
func NewUF2Block(targetAddr uint32) *UF2Block {
6968
return &UF2Block{magicStart0: uf2MagicStart0,
7069
magicStart1: uf2MagicStart1,
7170
magicEnd: uf2MagicEnd,
72-
targetAddr: uf2StartAddress,
71+
targetAddr: targetAddr,
7372
flags: 0x0,
7473
familyID: 0x0,
7574
payloadSize: 256,

0 commit comments

Comments
 (0)