1#!/usr/bin/env python3

2#

3# This file is part of Checkbox.

4#

5# Copyright 2021 Canonical Ltd.

6# Written by:

7# Jonathan Cave <jonathan.cave@canonical.com>

8#

9# Checkbox is free software: you can redistribute it and/or modify

10# it under the terms of the GNU General Public License version 3,

11# as published by the Free Software Foundation.

12#

13# Checkbox is distributed in the hope that it will be useful,

14# but WITHOUT ANY WARRANTY; without even the implied warranty of

15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the

16# GNU General Public License for more details.

17#

18# You should have received a copy of the GNU General Public License

19# along with Checkbox. If not, see <http://www.gnu.org/licenses/>.

20

21 import filecmp

22 import sys

23 from os import walk

24 from os.path import join, relpath

25

26 from checkbox_support.snap_utils.system import get_kernel_snap

27

28

29 if len(sys.argv) != 2:

30 raise SystemExit("ERROR: please specify the bootloader")

31

32 dtb_dir = "/var/lib/snapd/hostfs/boot/{}".format(sys.argv[1])

33 print("Bootloader DTB location: {}".format(dtb_dir))

34

35 kernel = get_kernel_snap()

36 if kernel is None:

37 raise SystemExit("ERROR: failed to get kernel snap")

38 snap_dtbs = "/snap/{}/current/dtbs".format(kernel)

39 print("Kernel snap DTB location: {}".format(snap_dtbs), end="\n\n", flush=True)

40

41 snap_files = []

42 for dirpath, dirs, files in walk(snap_dtbs):

43 if dirpath == snap_dtbs:

44 snap_files.extend(files)

45 else:

46 snap_files.extend(

47 [join(relpath(dirpath, snap_dtbs), f) for f in files]

48 )

49

50 match, mismatch, errors = filecmp.cmpfiles(

51 snap_dtbs, dtb_dir, snap_files, shallow=True

52)

53

54 if match:

55 print(

56 "{} matching DTB files found".format(len(match)),

57 end="\n\n",

58 flush=True,

59 )

60

61 if mismatch:

62 print(

63 "FAIL: DTB files shipped in kernel snap with mismatched versions"

64 " in bootloader dir:",

65 file=sys.stderr,

66 )

67 for f in mismatch:

68 print(f, file=sys.stderr)

69 print("", file=sys.stderr, flush=True)

70

71 if errors:

72 print(

73 "FAIL: DTB files shipped in kernel snap which could not be"

74 " found in bootloader dir:",

75 file=sys.stderr,

76 )

77 for f in errors:

78 print(f, file=sys.stderr)

79 print("", file=sys.stderr, flush=True)

80

81 if mismatch or errors:

82 raise SystemExit("Comparison failed")