forked from hybridgroup/gort
/
chip.go
256 lines (216 loc) · 6.54 KB
/
chip.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package commands
import (
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"runtime"
"strings"
"github.com/codegangsta/cli"
)
// Chip command implementation
func Chip() cli.Command {
return cli.Command{
Name: "chip",
Usage: "Download and install device tree components for your C.H.I.P",
Action: func(c *cli.Context) {
if runtime.GOOS != "linux" {
fmt.Println("C.H.I.P commands must be run on the device, current OS not supported")
return
}
valid := true
if c.Args().First() != "install" {
valid = false
} else if len(c.Args()) < 2 {
valid = false
}
usage := func() {
fmt.Println("Invalid/no subcommand supplied.")
fmt.Println()
fmt.Println("Usage:")
fmt.Println(" # downloads and installs the required 'dtc' tool")
fmt.Println(" gort chip install dtc")
fmt.Println()
fmt.Println(" # installs the device tree overlay for PWM0")
fmt.Println(" # requires an installed 'dtc' tool, and most likely 'sudo'")
fmt.Println(" gort chip install pwm")
fmt.Println()
fmt.Println(" # installs the device tree overlay for SPI2")
fmt.Println(" # requires an installed 'dtc' tool, and most likely 'sudo'")
fmt.Println(" gort chip install spi")
}
if valid == false {
usage()
return
}
subCommand := c.Args()[1]
switch subCommand {
case "pwm":
info := chipOverlayInfo{"pwm", "chip-pwm0.dtbo", "chip-pwm", "support/chip/chip-pwm0.dts"}
err := chipBuildAndInstallOverlay(info)
if err != nil {
log.Fatal(err)
}
err = chipUpdateInitscript(info)
if err != nil {
log.Fatal(err)
}
fmt.Println("PWM0 overlay installation complete.")
fmt.Println("Now reboot to make sure init scripts load the overlay as expected.")
case "spi":
info := chipOverlayInfo{"spi", "chip-spi2.dtbo", "chip-spi", "support/chip/chip-spi2.dts"}
err := chipBuildAndInstallOverlay(info)
if err != nil {
log.Fatal(err)
}
err = chipUpdateInitscript(info)
if err != nil {
log.Fatal(err)
}
fmt.Println("SPI2 overlay installation complete.")
fmt.Println("Now reboot to make sure init scripts load the overlay as expected.")
case "dtc":
chipInstallDTC()
default:
usage()
}
},
}
}
const chipOverlayInstallPath = "/lib/firmware/gort.io"
const chipOverlayConfigPath = "/sys/kernel/config/device-tree/overlays"
type chipOverlayInfo struct {
id string
dtbo string
folder string
source string
}
func chipBuildOverlay(tmpDir string, source string) (err error) {
path, err := exec.LookPath("dtc")
if err != nil {
return fmt.Errorf("Failed to find 'dtc' command on path")
}
sourcePath := tmpDir + "/overlay.dts"
blobPath := tmpDir + "/overlay.dtbo"
if err = ioutil.WriteFile(sourcePath, []byte(source), 0666); err != nil {
return err
}
dtc := exec.Command(path, "-O", "dtb", "-o", blobPath, "-b", "o", "-@", sourcePath)
if err = dtc.Run(); err != nil {
return err
}
return nil
}
func chipInstallOverlay(tmpDir string, blobFile string) (err error) {
err = os.MkdirAll(chipOverlayInstallPath, 0777)
if err == nil {
blobSource := tmpDir + "/overlay.dtbo"
blobTarget := chipOverlayInstallPath + "/" + blobFile
err = copyFile(blobSource, blobTarget)
}
if os.IsPermission(err) {
return fmt.Errorf("No permission to install overlay at %q, retry using 'sudo'", chipOverlayInstallPath)
}
return err
}
func chipBuildAndInstallOverlay(info chipOverlayInfo) (err error) {
blobPath := chipOverlayInstallPath + "/" + info.dtbo
if _, err = os.Stat(blobPath); err == nil {
// this blob is in place, return
return
}
if os.IsNotExist(err) {
var tmpDir string
tmpDir, err = ioutil.TempDir("", "dtbo")
defer os.RemoveAll(tmpDir)
code, _ := Asset(info.source)
err = chipBuildOverlay(tmpDir, string(code))
if err != nil {
return fmt.Errorf("Failed to build overlay: %v", err)
}
err = chipInstallOverlay(tmpDir, info.dtbo)
if err != nil {
return fmt.Errorf("Failed to install overlay: %v", err)
}
} else {
return fmt.Errorf("Failed to check for installed overlay: %v", err)
}
return
}
func chipInstallDTC() (err error) {
dir, _ := createGortDirectory()
fmt.Println("Attempting to install dev tools with apt-get.")
cmd := exec.Command("sudo", "apt-get", "-y", "install", "flex", "bison", "git")
cmd.Dir = dir
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
log.Fatal(err)
}
fmt.Println("Attempting to fetch 'dtc' from github.")
cmd = exec.Command("git", "clone", "https://github.com/atenart/dtc")
cmd.Dir = dir
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
log.Fatal(err)
}
fmt.Println("Attempting to build 'dtc'.")
cmd = exec.Command("make")
cmd.Dir = fmt.Sprintf("%v/%v", dir, "dtc")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
log.Fatal(err)
}
fmt.Println("Attempting to install 'dtc'.")
cmd = exec.Command("sudo", "make", "install", "PREFIX=/usr")
cmd.Dir = fmt.Sprintf("%v/%v", dir, "dtc")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
log.Fatal(err)
}
return
}
func chipUpdateInitscript(info chipOverlayInfo) (err error) {
const initScript = "/etc/rc.local"
configPath := chipOverlayConfigPath + "/" + info.folder
overlayPath := chipOverlayInstallPath + "/" + info.dtbo
prefix := fmt.Sprintf("# GORT BEGIN %s\n", info.id)
action := fmt.Sprintf("mkdir -p %s\ncp %s %s/dtbo\n", configPath, overlayPath, configPath)
suffix := fmt.Sprintf("# GORT END %s\n", info.id)
initSegment := prefix + action + suffix
scriptBytes, err := ioutil.ReadFile(initScript)
if err != nil {
return
}
script := string(scriptBytes)
if strings.Contains(script, prefix) {
// assume previously edited script
headAndRest := strings.Split(script, prefix)
head, rest := headAndRest[0], headAndRest[1]
if strings.Contains(rest, suffix) {
midTail := strings.Split(rest, suffix)
_, tail := midTail[0], midTail[1]
script = head + initSegment + tail
} else {
return fmt.Errorf("%q init script GORT section malformed. Manual editing required", initScript)
}
} else {
// assume unedited script, should end with "exit 0"
const exitLine = "exit 0\n"
if strings.HasSuffix(script, exitLine) {
script = strings.TrimSuffix(script, exitLine)
script = script + initSegment + exitLine
} else {
return fmt.Errorf("%q init script editing failed. Manual editing required", initScript)
}
}
err = ioutil.WriteFile(initScript, []byte(script), 0755)
if err != nil {
return fmt.Errorf("Failed to write edited init script: %v", err)
}
return
}