-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
106 lines (95 loc) · 2.49 KB
/
main.go
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Copyright 2023 The Embedded Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"bufio"
"encoding/binary"
"flag"
"fmt"
"os"
)
func fatalErr(err error) {
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
func usage() {
fmt.Fprint(
os.Stderr,
"\nUsage:\n imxrtmbr [options] MBR_FILE\n\nOptions:\n",
)
flag.PrintDefaults()
}
const mbrSize = 8192
func main() {
var flashSize, imageSize, flexRAMCfg uint
flag.UintVar(
&flashSize, "flash", 0,
"flash size (KiB)",
)
flag.UintVar(
&imageSize, "image", 0,
"program image size (KiB), 0 means all the remaining flash space",
)
flag.UintVar(
&flexRAMCfg, "flexram", 0,
"FlexRAM configuration (the value to write to the GPR17)",
)
flag.Usage = usage
flag.Parse()
if flag.NArg() != 1 {
usage()
return
}
if flashSize == 0 {
fmt.Fprintln(os.Stderr, "flash size not set")
usage()
os.Exit(1)
}
flashSize *= KiB
imageSize *= KiB
if imageSize == 0 {
imageSize = flashSize - mbrSize
}
flashConfig.MemCfg.SFlashA1Size = uint32(flashSize)
f, err := os.Create(flag.Arg(0))
fatalErr(err)
w := bufio.NewWriter(f)
fatalErr(binary.Write(w, binary.LittleEndian, flashConfig))
for a := baseAddr + flashConfigSize; a < ivtAddr; a++ {
fatalErr(w.WriteByte(0xff))
}
if flexRAMCfg == 0 {
bootData.Length = uint32(imageSize)
fatalErr(binary.Write(w, binary.LittleEndian, regularIVT))
fatalErr(binary.Write(w, binary.LittleEndian, bootData))
for a := bootDataAddr + bootDataSize; a < mbrEndAddr; a++ {
fatalErr(w.WriteByte(0xff))
}
} else {
bootData.Length = uint32(pluginAddr + len(plugin)*2 - baseAddr)
bootData.Plugin = 1
imageSize -= stage2IVTAddr - baseAddr
pluginImageSize[0] = uint16(imageSize)
pluginImageSize[1] = uint16(imageSize >> 16)
pluginFlexRAMCfg[0] = uint16(flexRAMCfg)
pluginFlexRAMCfg[1] = uint16(flexRAMCfg >> 16)
fatalErr(binary.Write(w, binary.LittleEndian, pluginIVT))
fatalErr(binary.Write(w, binary.LittleEndian, bootData))
for a := bootDataAddr + bootDataSize; a < pluginAddr; a++ {
fatalErr(w.WriteByte(0xff))
}
fatalErr(binary.Write(w, binary.LittleEndian, plugin))
for a := pluginAddr + len(plugin)*2; a < stage2IVTAddr; a++ {
fatalErr(w.WriteByte(0xff))
}
fatalErr(binary.Write(w, binary.LittleEndian, stage2IVT))
for a := stage2IVTAddr + ivtSize; a < mbrEndAddr; a++ {
fatalErr(w.WriteByte(0xff))
}
}
fatalErr(w.Flush())
fatalErr(f.Close())
}