forked from canonical/snapd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpiboot_test.go
696 lines (609 loc) · 20.6 KB
/
piboot_test.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2021 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package bootloader_test
import (
"io/ioutil"
"os"
"path/filepath"
"strings"
"time"
. "gopkg.in/check.v1"
"github.com/snapcore/snapd/boot"
"github.com/snapcore/snapd/bootloader"
"github.com/snapcore/snapd/bootloader/ubootenv"
"github.com/snapcore/snapd/osutil"
"github.com/snapcore/snapd/snap"
"github.com/snapcore/snapd/snap/snapfile"
"github.com/snapcore/snapd/snap/snaptest"
"github.com/snapcore/snapd/testutil"
)
type pibootTestSuite struct {
baseBootenvTestSuite
}
var _ = Suite(&pibootTestSuite{})
func (s *pibootTestSuite) TestNewPiboot(c *C) {
// no files means bl is not present, but we can still create the bl object
p := bootloader.NewPiboot(s.rootdir, nil)
c.Assert(p, NotNil)
c.Assert(p.Name(), Equals, "piboot")
present, err := p.Present()
c.Assert(err, IsNil)
c.Assert(present, Equals, false)
// now with files present, the bl is present
r := bootloader.MockPibootFiles(c, s.rootdir, nil)
defer r()
present, err = p.Present()
c.Assert(err, IsNil)
c.Assert(present, Equals, true)
}
func (s *pibootTestSuite) TestPibootGetEnvVar(c *C) {
// We need PrepareImageTime due to fixed reference to /run/mnt otherwise
opts := bootloader.Options{PrepareImageTime: true,
Role: bootloader.RoleRunMode, NoSlashBoot: true}
r := bootloader.MockPibootFiles(c, s.rootdir, &opts)
defer r()
p := bootloader.NewPiboot(s.rootdir, &opts)
c.Assert(p, NotNil)
err := p.SetBootVars(map[string]string{
"snap_mode": "",
"snap_core": "4",
})
c.Assert(err, IsNil)
m, err := p.GetBootVars("snap_mode", "snap_core")
c.Assert(err, IsNil)
c.Assert(m, DeepEquals, map[string]string{
"snap_mode": "",
"snap_core": "4",
})
}
func (s *pibootTestSuite) TestGetBootloaderWithPiboot(c *C) {
r := bootloader.MockPibootFiles(c, s.rootdir, nil)
defer r()
bootloader, err := bootloader.Find(s.rootdir, nil)
c.Assert(err, IsNil)
c.Assert(bootloader.Name(), Equals, "piboot")
}
func (s *pibootTestSuite) testPibootSetEnvWriteOnlyIfChanged(c *C, fromInitramfs bool) {
opts := bootloader.Options{PrepareImageTime: true,
Role: bootloader.RoleRunMode, NoSlashBoot: true}
r := bootloader.MockPibootFiles(c, s.rootdir, &opts)
defer r()
p := bootloader.NewPiboot(s.rootdir, &opts)
c.Assert(p, NotNil)
envFile := bootloader.PibootConfigFile(p)
env, err := ubootenv.OpenWithFlags(envFile, ubootenv.OpenBestEffort)
c.Assert(err, IsNil)
env.Set("snap_ab", "b")
env.Set("snap_mode", "")
err = env.Save()
c.Assert(err, IsNil)
st, err := os.Stat(envFile)
c.Assert(err, IsNil)
time.Sleep(100 * time.Millisecond)
// note that we set to the same var to the same value as above
if fromInitramfs {
nsbl, ok := p.(bootloader.NotScriptableBootloader)
c.Assert(ok, Equals, true)
err = nsbl.SetBootVarsFromInitramfs(map[string]string{"snap_ab": "b"})
} else {
err = p.SetBootVars(map[string]string{"snap_ab": "b"})
}
c.Assert(err, IsNil)
st2, err := os.Stat(envFile)
c.Assert(err, IsNil)
c.Assert(st.ModTime(), Equals, st2.ModTime())
}
func (s *pibootTestSuite) TestPibootSetEnvWriteOnlyIfChanged(c *C) {
// Run test from rootfs and from initramfs
fromInitramfs := false
s.testPibootSetEnvWriteOnlyIfChanged(c, fromInitramfs)
fromInitramfs = true
s.testPibootSetEnvWriteOnlyIfChanged(c, fromInitramfs)
}
func (s *pibootTestSuite) testExtractKernelAssets(c *C, dtbDir string) {
opts := bootloader.Options{PrepareImageTime: true,
Role: bootloader.RoleRunMode, NoSlashBoot: true}
r := bootloader.MockPibootFiles(c, s.rootdir, &opts)
defer r()
p := bootloader.NewPiboot(s.rootdir, &opts)
files := [][]string{
{"kernel.img", "I'm a kernel"},
{"initrd.img", "...and I'm an initrd"},
{filepath.Join(dtbDir, "foo.dtb"), "g'day, I'm foo.dtb"},
{"dtbs/overlays/bar.dtbo", "hello, I'm bar.dtbo"},
// must be last
{"meta/kernel.yaml", "version: 4.2"},
}
fn := snaptest.MakeTestSnapWithFiles(c, packageKernel, files)
snapf, err := snapfile.Open(fn)
c.Assert(err, IsNil)
assetsDir, err := ioutil.TempDir("", "kernel-assets")
c.Assert(err, IsNil)
defer os.RemoveAll(assetsDir)
err = bootloader.LayoutKernelAssetsToDir(p, snapf, assetsDir)
c.Assert(err, IsNil)
// Do again, as extracting might be called again for an
// already extracted kernel.
err = bootloader.LayoutKernelAssetsToDir(p, snapf, assetsDir)
c.Assert(err, IsNil)
// Extraction folders for files slice
destDirs := []string{
assetsDir, assetsDir, assetsDir, filepath.Join(assetsDir, "overlays"),
}
for i, dir := range destDirs {
fullFn := filepath.Join(dir, filepath.Base(files[i][0]))
c.Check(fullFn, testutil.FileEquals, files[i][1])
}
// Check that file required by piboot is created
readmeFn := filepath.Join(assetsDir, "overlays", "README")
c.Check(readmeFn, testutil.FilePresent)
}
func (s *pibootTestSuite) TestExtractKernelAssets(c *C) {
// armhf and arm64 kernel snaps store dtbs in different places
s.testExtractKernelAssets(c, "dtbs")
s.testExtractKernelAssets(c, "dtbs/broadcom")
}
func (s *pibootTestSuite) testExtractRecoveryKernelAssets(c *C, dtbDir string) {
opts := bootloader.Options{PrepareImageTime: true,
Role: bootloader.RoleRunMode, NoSlashBoot: true}
r := bootloader.MockPibootFiles(c, s.rootdir, &opts)
defer r()
p := bootloader.NewPiboot(s.rootdir, &opts)
files := [][]string{
{"kernel.img", "I'm a kernel"},
{"initrd.img", "...and I'm an initrd"},
{filepath.Join(dtbDir, "foo.dtb"), "g'day, I'm foo.dtb"},
{"dtbs/overlays/bar.dtbo", "hello, I'm bar.dtbo"},
// must be last
{"meta/kernel.yaml", "version: 4.2"},
}
si := &snap.SideInfo{
RealName: "ubuntu-kernel",
Revision: snap.R(42),
}
fn := snaptest.MakeTestSnapWithFiles(c, packageKernel, files)
snapf, err := snapfile.Open(fn)
c.Assert(err, IsNil)
info, err := snap.ReadInfoFromSnapFile(snapf, si)
c.Assert(err, IsNil)
// try with empty recovery dir first to check the errors
err = p.ExtractRecoveryKernelAssets("", info, snapf)
c.Assert(err, ErrorMatches, "internal error: recoverySystemDir unset")
// now the expected behavior
err = p.ExtractRecoveryKernelAssets("recovery-dir", info, snapf)
c.Assert(err, IsNil)
// Extraction folders for files slice
assetsDir := filepath.Join(s.rootdir, "recovery-dir", "kernel")
destDirs := []string{
assetsDir, assetsDir, assetsDir, filepath.Join(assetsDir, "overlays"),
}
for i, dir := range destDirs {
fullFn := filepath.Join(dir, filepath.Base(files[i][0]))
c.Check(fullFn, testutil.FileEquals, files[i][1])
}
// Check that file required by piboot is created
readmeFn := filepath.Join(assetsDir, "overlays", "README")
c.Check(readmeFn, testutil.FilePresent)
}
func (s *pibootTestSuite) TestExtractRecoveryKernelAssets(c *C) {
// armhf and arm64 kernel snaps store dtbs in different places
s.testExtractRecoveryKernelAssets(c, "dtbs")
s.testExtractRecoveryKernelAssets(c, "dtbs/broadcom")
}
func (s *pibootTestSuite) TestPibootUC20OptsPlacement(c *C) {
tt := []struct {
blOpts *bootloader.Options
expEnv string
comment string
}{
{
&bootloader.Options{PrepareImageTime: true,
Role: bootloader.RoleRunMode, NoSlashBoot: true},
"/piboot/ubuntu/piboot.conf",
"uc20 install mode piboot.conf",
},
{
&bootloader.Options{PrepareImageTime: true,
Role: bootloader.RoleRunMode},
"/boot/piboot/piboot.conf",
"uc20 run mode piboot.conf",
},
{
&bootloader.Options{PrepareImageTime: true,
Role: bootloader.RoleRecovery},
"/piboot/ubuntu/piboot.conf",
"uc20 recovery piboot.conf",
},
}
for _, t := range tt {
dir := c.MkDir()
restore := bootloader.MockPibootFiles(c, dir, t.blOpts)
p := bootloader.NewPiboot(dir, t.blOpts)
c.Assert(p, NotNil, Commentf(t.comment))
c.Assert(bootloader.PibootConfigFile(p), Equals,
filepath.Join(dir, t.expEnv), Commentf(t.comment))
// if we set boot vars on the piboot, we can open the config file and
// get the same variables
c.Assert(p.SetBootVars(map[string]string{"hello": "there"}), IsNil)
env, err := ubootenv.OpenWithFlags(filepath.Join(dir, t.expEnv),
ubootenv.OpenBestEffort)
c.Assert(err, IsNil)
c.Assert(env.Get("hello"), Equals, "there")
restore()
}
}
func (s *pibootTestSuite) TestCreateConfig(c *C) {
opts := bootloader.Options{PrepareImageTime: false,
Role: bootloader.RoleRunMode, NoSlashBoot: true}
r := bootloader.MockPibootFiles(c, s.rootdir, &opts)
defer r()
p := bootloader.NewPiboot(s.rootdir, &opts)
err := p.SetBootVars(map[string]string{
"snap_kernel": "pi-kernel_1",
"snapd_recovery_mode": "run",
"kernel_status": boot.DefaultStatus})
c.Assert(err, IsNil)
files := []struct {
path string
data string
}{
{
path: filepath.Join(s.rootdir, "config.txt"),
data: "\nos_prefix=/piboot/ubuntu/pi-kernel_1/\n",
},
{
path: filepath.Join(s.rootdir, "piboot/ubuntu/pi-kernel_1/cmdline.txt"),
data: " snapd_recovery_mode=run\n",
},
}
for _, fInfo := range files {
readData, err := ioutil.ReadFile(fInfo.path)
c.Assert(err, IsNil)
c.Assert(string(readData), Equals, fInfo.data)
}
}
func (s *pibootTestSuite) TestCreateTrybootCfg(c *C) {
opts := bootloader.Options{PrepareImageTime: false,
Role: bootloader.RoleRunMode, NoSlashBoot: true}
r := bootloader.MockPibootFiles(c, s.rootdir, &opts)
defer r()
p := bootloader.NewPiboot(s.rootdir, &opts)
err := p.SetBootVars(map[string]string{
"snap_kernel": "pi-kernel_1",
"snap_try_kernel": "pi-kernel_2",
"snapd_recovery_mode": "run",
"kernel_status": boot.TryStatus})
c.Assert(err, IsNil)
files := []struct {
path string
data string
}{
{
path: filepath.Join(s.rootdir, "tryboot.txt"),
data: "\nos_prefix=/piboot/ubuntu/pi-kernel_2/\n",
},
{
path: filepath.Join(s.rootdir, "piboot/ubuntu/pi-kernel_2/cmdline.txt"),
data: " snapd_recovery_mode=run kernel_status=trying\n",
},
}
for _, fInfo := range files {
readData, err := ioutil.ReadFile(fInfo.path)
c.Assert(err, IsNil)
c.Assert(string(readData), Equals, fInfo.data)
}
// Now set variables like in an after update reboot
err = p.SetBootVars(map[string]string{
"snap_kernel": "pi-kernel_2",
"snap_try_kernel": "",
"snapd_recovery_mode": "run",
"kernel_status": boot.DefaultStatus})
c.Assert(err, IsNil)
c.Assert(osutil.FileExists(filepath.Join(s.rootdir, "tryboot.txt")), Equals, false)
files = []struct {
path string
data string
}{
{
path: filepath.Join(s.rootdir, "config.txt"),
data: "\nos_prefix=/piboot/ubuntu/pi-kernel_2/\n",
},
{
path: filepath.Join(s.rootdir, "piboot/ubuntu/pi-kernel_2/cmdline.txt"),
data: " snapd_recovery_mode=run\n",
},
}
for _, fInfo := range files {
readData, err := ioutil.ReadFile(fInfo.path)
c.Assert(err, IsNil)
c.Assert(string(readData), Equals, fInfo.data)
}
}
func (s *pibootTestSuite) TestCreateConfigCurrentNotEmpty(c *C) {
opts := bootloader.Options{PrepareImageTime: false,
Role: bootloader.RoleRunMode, NoSlashBoot: true}
r := bootloader.MockPibootFiles(c, s.rootdir, &opts)
defer r()
// Get some extra kernel command line parameters
err := ioutil.WriteFile(filepath.Join(s.rootdir, "cmdline.txt"),
[]byte("opt1=foo bar\n"), 0644)
c.Assert(err, IsNil)
// Add some options to already existing config.txt
err = ioutil.WriteFile(filepath.Join(s.rootdir, "config.txt"),
[]byte("rpi.option1=val\nos_prefix=1\nrpi.option2=val\n"), 0644)
c.Assert(err, IsNil)
p := bootloader.NewPiboot(s.rootdir, &opts)
err = p.SetBootVars(map[string]string{
"snap_kernel": "pi-kernel_1",
"snapd_recovery_mode": "run",
"kernel_status": boot.DefaultStatus})
c.Assert(err, IsNil)
files := []struct {
path string
data string
}{
{
path: filepath.Join(s.rootdir, "config.txt"),
data: "rpi.option1=val\nos_prefix=/piboot/ubuntu/pi-kernel_1/\nrpi.option2=val\n",
},
{
path: filepath.Join(s.rootdir, "piboot/ubuntu/pi-kernel_1/cmdline.txt"),
data: "opt1=foo bar snapd_recovery_mode=run\n",
},
}
for _, fInfo := range files {
readData, err := ioutil.ReadFile(fInfo.path)
c.Assert(err, IsNil)
c.Assert(string(readData), Equals, fInfo.data)
}
// Now set variables like in an update
err = p.SetBootVars(map[string]string{
"snap_kernel": "pi-kernel_1",
"snap_try_kernel": "pi-kernel_2",
"snapd_recovery_mode": "run",
"kernel_status": boot.TryStatus})
c.Assert(err, IsNil)
files = []struct {
path string
data string
}{
{
path: filepath.Join(s.rootdir, "tryboot.txt"),
data: "rpi.option1=val\nos_prefix=/piboot/ubuntu/pi-kernel_2/\nrpi.option2=val\n",
},
{
path: filepath.Join(s.rootdir, "config.txt"),
data: "rpi.option1=val\nos_prefix=/piboot/ubuntu/pi-kernel_1/\nrpi.option2=val\n",
},
{
path: filepath.Join(s.rootdir, "piboot/ubuntu/pi-kernel_2/cmdline.txt"),
data: "opt1=foo bar snapd_recovery_mode=run kernel_status=trying\n",
},
}
for _, fInfo := range files {
readData, err := ioutil.ReadFile(fInfo.path)
c.Assert(err, IsNil)
c.Assert(string(readData), Equals, fInfo.data)
}
}
func (s *pibootTestSuite) TestOnlyOneOsPrefix(c *C) {
opts := bootloader.Options{PrepareImageTime: false,
Role: bootloader.RoleRunMode, NoSlashBoot: true}
r := bootloader.MockPibootFiles(c, s.rootdir, &opts)
defer r()
// Introuce two os_prefix lines
err := ioutil.WriteFile(filepath.Join(s.rootdir, "config.txt"),
[]byte("os_prefix=1\nos_prefix=2\n"), 0644)
c.Assert(err, IsNil)
p := bootloader.NewPiboot(s.rootdir, &opts)
err = p.SetBootVars(map[string]string{
"snap_kernel": "pi-kernel_1",
"snapd_recovery_mode": "run",
"kernel_status": boot.DefaultStatus})
c.Assert(err, IsNil)
files := []struct {
path string
data string
}{
{
path: filepath.Join(s.rootdir, "config.txt"),
data: "os_prefix=/piboot/ubuntu/pi-kernel_1/\n# os_prefix=2\n",
},
{
path: filepath.Join(s.rootdir, "piboot/ubuntu/pi-kernel_1/cmdline.txt"),
data: " snapd_recovery_mode=run\n",
},
}
for _, fInfo := range files {
readData, err := ioutil.ReadFile(fInfo.path)
c.Assert(err, IsNil)
c.Assert(string(readData), Equals, fInfo.data)
}
}
func (s *pibootTestSuite) TestGetRebootArguments(c *C) {
opts := bootloader.Options{PrepareImageTime: false,
Role: bootloader.RoleRunMode, NoSlashBoot: true}
r := bootloader.MockPibootFiles(c, s.rootdir, &opts)
defer r()
p := bootloader.NewPiboot(s.rootdir, &opts)
c.Assert(p, NotNil)
rbl, ok := p.(bootloader.RebootBootloader)
c.Assert(ok, Equals, true)
args, err := rbl.GetRebootArguments()
c.Assert(err, IsNil)
c.Assert(args, Equals, "")
err = p.SetBootVars(map[string]string{"kernel_status": "try"})
c.Assert(err, IsNil)
args, err = rbl.GetRebootArguments()
c.Assert(err, IsNil)
c.Assert(args, Equals, "0 tryboot")
err = p.SetBootVars(map[string]string{"kernel_status": ""})
c.Assert(err, IsNil)
}
func (s *pibootTestSuite) TestGetRebootArgumentsNoEnv(c *C) {
opts := bootloader.Options{PrepareImageTime: false,
Role: bootloader.RoleRunMode, NoSlashBoot: true}
p := bootloader.NewPiboot(s.rootdir, &opts)
c.Assert(p, NotNil)
rbl, ok := p.(bootloader.RebootBootloader)
c.Assert(ok, Equals, true)
args, err := rbl.GetRebootArguments()
c.Assert(err, ErrorMatches, "open .*/piboot.conf: no such file or directory")
c.Assert(args, Equals, "")
}
func (s *pibootTestSuite) TestSetBootVarsFromInitramfs(c *C) {
opts := bootloader.Options{PrepareImageTime: false,
Role: bootloader.RoleRunMode, NoSlashBoot: true}
r := bootloader.MockPibootFiles(c, s.rootdir, &opts)
defer r()
p := bootloader.NewPiboot(s.rootdir, &opts)
c.Assert(p, NotNil)
nsbl, ok := p.(bootloader.NotScriptableBootloader)
c.Assert(ok, Equals, true)
err := nsbl.SetBootVarsFromInitramfs(map[string]string{"kernel_status": "trying"})
c.Assert(err, IsNil)
m, err := p.GetBootVars("kernel_status")
c.Assert(err, IsNil)
c.Assert(m, DeepEquals, map[string]string{
"kernel_status": "trying",
})
}
func (s *pibootTestSuite) testExtractKernelAssetsAndRemove(c *C, dtbDir string) {
opts := bootloader.Options{PrepareImageTime: false,
Role: bootloader.RoleRunMode, NoSlashBoot: true}
r := bootloader.MockPibootFiles(c, s.rootdir, &opts)
defer r()
p := bootloader.NewPiboot(s.rootdir, &opts)
c.Assert(p, NotNil)
files := [][]string{
{"kernel.img", "I'm a kernel"},
{"initrd.img", "...and I'm an initrd"},
{filepath.Join(dtbDir, "foo.dtb"), "g'day, I'm foo.dtb"},
{"dtbs/overlays/bar.dtbo", "hello, I'm bar.dtbo"},
// must be last
{"meta/kernel.yaml", "version: 4.2"},
}
si := &snap.SideInfo{
RealName: "ubuntu-kernel",
Revision: snap.R(42),
}
fn := snaptest.MakeTestSnapWithFiles(c, packageKernel, files)
snapf, err := snapfile.Open(fn)
c.Assert(err, IsNil)
info, err := snap.ReadInfoFromSnapFile(snapf, si)
c.Assert(err, IsNil)
err = p.ExtractKernelAssets(info, snapf)
c.Assert(err, IsNil)
// this is where the kernel/initrd is unpacked
kernelAssetsDir := filepath.Join(s.rootdir, "piboot", "ubuntu", "ubuntu-kernel_42.snap")
for _, def := range files {
if def[0] == "meta/kernel.yaml" {
break
}
destPath := def[0]
if strings.HasPrefix(destPath, "dtbs/broadcom/") {
destPath = strings.TrimPrefix(destPath, "dtbs/broadcom/")
} else if strings.HasPrefix(destPath, "dtbs/") {
destPath = strings.TrimPrefix(destPath, "dtbs/")
}
fullFn := filepath.Join(kernelAssetsDir, destPath)
c.Check(fullFn, testutil.FileEquals, def[1])
}
// remove
err = p.RemoveKernelAssets(info)
c.Assert(err, IsNil)
c.Check(osutil.FileExists(kernelAssetsDir), Equals, false)
}
func (s *pibootTestSuite) TestExtractKernelAssetsAndRemove(c *C) {
// armhf and arm64 kernel snaps store dtbs in different places
s.testExtractKernelAssetsAndRemove(c, "dtbs")
s.testExtractKernelAssetsAndRemove(c, "dtbs/broadcom")
}
func (s *pibootTestSuite) testExtractKernelAssetsOnRPi4CheckEeprom(c *C, rpiRevisionCode, eepromTimeStamp []byte, errExpected bool) {
opts := bootloader.Options{PrepareImageTime: false,
Role: bootloader.RoleRunMode, NoSlashBoot: true}
r := bootloader.MockPibootFiles(c, s.rootdir, &opts)
defer r()
r = bootloader.MockRPi4Files(c, s.rootdir, rpiRevisionCode, eepromTimeStamp)
defer r()
p := bootloader.NewPiboot(s.rootdir, &opts)
c.Assert(p, NotNil)
files := [][]string{
{"kernel.img", "I'm a kernel"},
{"initrd.img", "...and I'm an initrd"},
{"dtbs/broadcom/foo.dtb", "g'day, I'm foo.dtb"},
{"dtbs/overlays/bar.dtbo", "hello, I'm bar.dtbo"},
// must be last
{"meta/kernel.yaml", "version: 4.2"},
}
si := &snap.SideInfo{
RealName: "ubuntu-kernel",
Revision: snap.R(42),
}
fn := snaptest.MakeTestSnapWithFiles(c, packageKernel, files)
snapf, err := snapfile.Open(fn)
c.Assert(err, IsNil)
info, err := snap.ReadInfoFromSnapFile(snapf, si)
c.Assert(err, IsNil)
err = p.ExtractKernelAssets(info, snapf)
if errExpected {
c.Check(err.Error(), Equals,
"your EEPROM does not support tryboot, please upgrade to a newer one before installing Ubuntu Core - see http://forum.snapcraft.io/t/29455 for more details")
return
}
c.Assert(err, IsNil)
// this is where the kernel/initrd is unpacked
kernelAssetsDir := filepath.Join(s.rootdir, "piboot", "ubuntu", "ubuntu-kernel_42.snap")
for _, def := range files {
if def[0] == "meta/kernel.yaml" {
break
}
destPath := def[0]
if strings.HasPrefix(destPath, "dtbs/broadcom/") {
destPath = strings.TrimPrefix(destPath, "dtbs/broadcom/")
} else if strings.HasPrefix(destPath, "dtbs/") {
destPath = strings.TrimPrefix(destPath, "dtbs/")
}
fullFn := filepath.Join(kernelAssetsDir, destPath)
c.Check(fullFn, testutil.FileEquals, def[1])
}
// remove
err = p.RemoveKernelAssets(info)
c.Assert(err, IsNil)
c.Check(osutil.FileExists(kernelAssetsDir), Equals, false)
}
func (s *pibootTestSuite) TestExtractKernelAssetsOnRPi4CheckEeprom(c *C) {
// Rev code is RPi4, eeprom supports tryboot
expectFailure := false
s.testExtractKernelAssetsOnRPi4CheckEeprom(c,
[]byte{0x00, 0xc0, 0x31, 0x11},
[]byte{0x61, 0xf0, 0x09, 0x91},
expectFailure)
// Rev code is RPi4, eeprom does not support tryboot
expectFailure = true
s.testExtractKernelAssetsOnRPi4CheckEeprom(c,
[]byte{0x00, 0xc0, 0x31, 0x11},
[]byte{0x60, 0x53, 0x15, 0x32},
expectFailure)
// Rev code is RPi3
expectFailure = false
s.testExtractKernelAssetsOnRPi4CheckEeprom(c,
[]byte{0x00, 0xa0, 0x20, 0x82},
[]byte{},
expectFailure)
}