Merge lp:~mvo/snappy/snappy-more-uboot-fiddling into lp:~snappy-dev/snappy/snappy-moved-to-github

Proposed by Michael Vogt on 2015-07-23
Status: Merged
Approved by: Ricardo Salveti on 2015-07-23
Approved revision: 603
Merged at revision: 602
Proposed branch: lp:~mvo/snappy/snappy-more-uboot-fiddling
Merge into: lp:~snappy-dev/snappy/snappy-moved-to-github
Diff against target: 322 lines (+113/-128)
2 files modified
partition/bootloader_uboot.go (+110/-127)
partition/bootloader_uboot_test.go (+3/-1)
To merge this branch: bzr merge lp:~mvo/snappy/snappy-more-uboot-fiddling
Reviewer Review Type Date Requested Status
Ricardo Salveti (community) Approve on 2015-07-23
Oliver Grawert 2015-07-23 Approve on 2015-07-23
Review via email: mp+265715@code.launchpad.net

Commit Message

Do all modifications in uboot via setEnvVar/getEnvVar and set the methods for this in the uboot init.

Description of the Change

Do all modifications in uboot via setEnvVar/getEnvVar and set the methods for this in the uboot init.

To post a comment you must log in.
Oliver Grawert (ogra) wrote :

tested and it works ... i cant really judge the code itself though

review: Approve
Ricardo Salveti (rsalveti) wrote :

Looks fine.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'partition/bootloader_uboot.go'
2--- partition/bootloader_uboot.go 2015-07-23 11:54:14 +0000
3+++ partition/bootloader_uboot.go 2015-07-23 18:11:32 +0000
4@@ -72,7 +72,10 @@
5 Value string
6 }
7
8-// newUboot create a new Grub bootloader object
9+var setBootVar = func(name, value string) error { return nil }
10+var getBootVar = func(name string) (string, error) { return "", nil }
11+
12+// newUboot create a new Uboot bootloader object
13 func newUboot(partition *Partition) bootLoader {
14 if !helpers.FileExists(bootloaderUbootConfigFile) {
15 return nil
16@@ -84,6 +87,14 @@
17 }
18 u := uboot{bootloaderType: *b}
19
20+ if helpers.FileExists(bootloaderUbootFwEnvFile) {
21+ setBootVar = setBootVarFwEnv
22+ getBootVar = getBootVarFwEnv
23+ } else {
24+ setBootVar = setBootVarLegacy
25+ getBootVar = getBootVarLegacy
26+ }
27+
28 return &u
29 }
30
31@@ -91,36 +102,15 @@
32 return bootloaderNameUboot
33 }
34
35-// ToggleRootFS make the U-Boot bootloader switch rootfs's.
36-//
37-// Approach:
38-//
39-// - Assume the device's installed version of u-boot supports
40-// CONFIG_SUPPORT_RAW_INITRD (that allows u-boot to boot a
41-// standard initrd+kernel on the fat32 disk partition).
42-// - Copy the "other" rootfs's kernel+initrd to the boot partition,
43-// renaming them in the process to ensure the next boot uses the
44-// correct versions.
45 func (u *uboot) ToggleRootFS(otherRootfs string) (err error) {
46-
47- // If the file exists, update it. Otherwise create it.
48- //
49- // The file _should_ always exist, but since it's on a writable
50- // partition, it's possible the admin removed it by mistake. So
51- // recreate to allow the system to boot!
52- changes := []configFileChange{
53- configFileChange{Name: bootloaderRootfsVar,
54- Value: string(otherRootfs),
55- },
56- configFileChange{Name: bootloaderBootmodeVar,
57- Value: bootloaderBootmodeTry,
58- },
59+ if err := setBootVar(bootloaderRootfsVar, string(otherRootfs)); err != nil {
60+ return err
61 }
62
63- return modifyNameValueFile(bootloaderUbootEnvFile, changes)
64+ return setBootVar(bootloaderBootmodeVar, bootloaderBootmodeTry)
65 }
66
67-func (u *uboot) GetBootVar(name string) (value string, err error) {
68+func getBootVarLegacy(name string) (value string, err error) {
69 cfg := goconfigparser.New()
70 cfg.AllowNoSectionHeader = true
71 if err := cfg.ReadFile(bootloaderUbootEnvFile); err != nil {
72@@ -130,84 +120,23 @@
73 return cfg.Get("", name)
74 }
75
76-func (u *uboot) GetNextBootRootFSName() (label string, err error) {
77- value, err := u.GetBootVar(bootloaderRootfsVar)
78- if err != nil {
79- // should never happen
80- return "", err
81- }
82-
83- return value, nil
84-}
85-
86-// FIXME: put into utils package
87-func readLines(path string) (lines []string, err error) {
88-
89- file, err := os.Open(path)
90-
91- if err != nil {
92- return nil, err
93- }
94-
95- defer file.Close()
96-
97- scanner := bufio.NewScanner(file)
98- for scanner.Scan() {
99- lines = append(lines, scanner.Text())
100- }
101-
102- return lines, scanner.Err()
103-}
104-
105-// FIXME: put into utils package
106-func writeLines(lines []string, path string) (err error) {
107-
108- file, err := os.Create(path)
109-
110- if err != nil {
111- return err
112- }
113-
114- defer func() {
115- e := file.Close()
116- if err == nil {
117- err = e
118- }
119- }()
120-
121- writer := bufio.NewWriter(file)
122-
123- for _, line := range lines {
124- if _, err := fmt.Fprintln(writer, line); err != nil {
125- return err
126- }
127- }
128-
129- if err := writer.Flush(); err != nil {
130- return err
131- }
132-
133- return file.Sync()
134-}
135-
136-func (u *uboot) markCurrentBootSuccessfulLegacy(currentRootfs string) error {
137+func setBootVarLegacy(name, value string) error {
138+ curVal, err := getBootVarLegacy(name)
139+ if err == nil && curVal == value {
140+ return nil
141+ }
142+
143 changes := []configFileChange{
144- configFileChange{Name: bootloaderBootmodeVar,
145- Value: bootloaderBootmodeSuccess,
146- },
147- configFileChange{Name: bootloaderRootfsVar,
148- Value: string(currentRootfs),
149- },
150- }
151-
152- if err := modifyNameValueFile(bootloaderUbootEnvFile, changes); err != nil {
153- return err
154- }
155-
156- return os.RemoveAll(bootloaderUbootStampFile)
157+ configFileChange{
158+ Name: name,
159+ Value: value,
160+ },
161+ }
162+
163+ return modifyNameValueFile(bootloaderUbootEnvFile, changes)
164 }
165
166-func (u *uboot) setBootVar(name, value string) error {
167+func setBootVarFwEnv(name, value string) error {
168 env, err := uenv.Open(bootloaderUbootFwEnvFile)
169 if err != nil {
170 return err
171@@ -222,12 +151,7 @@
172 return env.Save()
173 }
174
175-func (u *uboot) hasBootVar(name string) (bool, error) {
176- v, err := u.getBootVar(name)
177- return v != "", err
178-}
179-
180-func (u *uboot) getBootVar(name string) (string, error) {
181+func getBootVarFwEnv(name string) (string, error) {
182 env, err := uenv.Open(bootloaderUbootFwEnvFile)
183 if err != nil {
184 return "", err
185@@ -236,29 +160,42 @@
186 return env.Get(name), nil
187 }
188
189+func (u *uboot) GetBootVar(name string) (value string, err error) {
190+ return getBootVar(name)
191+}
192+
193+func (u *uboot) GetNextBootRootFSName() (label string, err error) {
194+ value, err := u.GetBootVar(bootloaderRootfsVar)
195+ if err != nil {
196+ // should never happen
197+ return "", err
198+ }
199+
200+ return value, nil
201+}
202+
203 // FIXME: this is super similar to grub now, refactor to extract the
204 // common code
205-func (u *uboot) markCurrentBootSuccessfulFwEnv(currentRootfs string) error {
206+func (u *uboot) MarkCurrentBootSuccessful(currentRootfs string) error {
207 // Clear the variable set on boot to denote a good boot.
208- if err := u.setBootVar(bootloaderTrialBootVar, "0"); err != nil {
209- return err
210- }
211-
212- if err := u.setBootVar(bootloaderRootfsVar, currentRootfs); err != nil {
213- return err
214- }
215-
216- return u.setBootVar(bootloaderBootmodeVar, bootloaderBootmodeSuccess)
217+ if err := setBootVar(bootloaderTrialBootVar, "0"); err != nil {
218+ return err
219+ }
220+
221+ if err := setBootVar(bootloaderRootfsVar, currentRootfs); err != nil {
222+ return err
223+ }
224+
225+ if err := setBootVar(bootloaderBootmodeVar, bootloaderBootmodeSuccess); err != nil {
226+ return err
227+ }
228+
229+ // legacy support, does not error if the file is not there
230+ return os.RemoveAll(bootloaderUbootStampFile)
231 }
232
233-func (u *uboot) MarkCurrentBootSuccessful(currentRootfs string) error {
234- // modern system
235- if helpers.FileExists(bootloaderUbootFwEnvFile) {
236- return u.markCurrentBootSuccessfulFwEnv(currentRootfs)
237- }
238-
239- // legacy
240- return u.markCurrentBootSuccessfulLegacy(currentRootfs)
241+func (u *uboot) BootDir() string {
242+ return bootloaderUbootDir
243 }
244
245 // Write lines to file atomically. File does not have to preexist.
246@@ -349,6 +286,52 @@
247 return nil
248 }
249
250-func (u *uboot) BootDir() string {
251- return bootloaderUbootDir
252+// FIXME: put into utils package
253+func readLines(path string) (lines []string, err error) {
254+
255+ file, err := os.Open(path)
256+
257+ if err != nil {
258+ return nil, err
259+ }
260+
261+ defer file.Close()
262+
263+ scanner := bufio.NewScanner(file)
264+ for scanner.Scan() {
265+ lines = append(lines, scanner.Text())
266+ }
267+
268+ return lines, scanner.Err()
269+}
270+
271+// FIXME: put into utils package
272+func writeLines(lines []string, path string) (err error) {
273+
274+ file, err := os.Create(path)
275+
276+ if err != nil {
277+ return err
278+ }
279+
280+ defer func() {
281+ e := file.Close()
282+ if err == nil {
283+ err = e
284+ }
285+ }()
286+
287+ writer := bufio.NewWriter(file)
288+
289+ for _, line := range lines {
290+ if _, err := fmt.Fprintln(writer, line); err != nil {
291+ return err
292+ }
293+ }
294+
295+ if err := writer.Flush(); err != nil {
296+ return err
297+ }
298+
299+ return file.Sync()
300 }
301
302=== modified file 'partition/bootloader_uboot_test.go'
303--- partition/bootloader_uboot_test.go 2015-07-23 11:54:14 +0000
304+++ partition/bootloader_uboot_test.go 2015-07-23 18:11:32 +0000
305@@ -59,6 +59,8 @@
306 snappy_stamp=snappy-stamp.txt
307 # either "regular" (normal boot) or "try" when trying a new version
308 snappy_mode=regular
309+# compat
310+snappy_trial_boot=0
311 # if we are trying a new version, check if stamp file is already there to revert
312 # to other version
313 snappy_boot=if test "${snappy_mode}" = "try"; then if test -e mmc ${bootpart} ${snappy_stamp}; then if test "${snappy_ab}" = "a"; then setenv snappy_ab "b"; else setenv snappy_ab "a"; fi; else fatwrite mmc ${mmcdev}:${mmcpart} 0x0 ${snappy_stamp} 0; fi; fi; run loadfiles; setenv mmcroot /dev/disk/by-label/system-${snappy_ab} ${snappy_cmdline}; run mmcargs; bootz ${loadaddr} ${initrd_addr}:${initrd_size} ${fdtaddr}
314@@ -363,7 +365,7 @@
315 u := newUboot(partition)
316 c.Assert(u, NotNil)
317
318- err = u.(*uboot).setBootVar(bootloaderRootfsVar, "b")
319+ err = setBootVar(bootloaderRootfsVar, "b")
320 c.Assert(err, IsNil)
321
322 env, err = uenv.Open(bootloaderUbootFwEnvFile)

Subscribers

People subscribed via source and target branches