1# SPDX-License-Identifier:  GPL-2.0+
2# Copyright (c) 2018, Linaro Limited
3#
4# Android Verified Boot 2.0 Test
5
6"""
7This tests Android Verified Boot 2.0 support in U-Boot:
8
9For additional details about how to build proper vbmeta partition
10check doc/android/avb2.rst
11
12For configuration verification:
13- Corrupt boot partition and check for failure
14- Corrupt vbmeta partition and check for failure
15"""
16
17import pytest
18
19# defauld mmc id
20mmc_dev = 1
21temp_addr = 0x90000000
22temp_addr2 = 0x90002000
23
24@pytest.mark.buildconfigspec('cmd_avb')
25@pytest.mark.buildconfigspec('cmd_mmc')
26def test_avb_verify(ubman):
27    """Run AVB 2.0 boot verification chain with avb subset of commands
28    """
29
30    success_str = "Verification passed successfully"
31
32    response = ubman.run_command('avb init %s' %str(mmc_dev))
33    assert response == ''
34    response = ubman.run_command('avb verify')
35    assert response.find(success_str)
36
37
38@pytest.mark.buildconfigspec('cmd_avb')
39@pytest.mark.buildconfigspec('cmd_mmc')
40@pytest.mark.notbuildconfigspec('sandbox')
41def test_avb_mmc_uuid(ubman):
42    """Check if 'avb get_uuid' works, compare results with
43    'part list mmc 1' output
44    """
45
46    response = ubman.run_command('avb init %s' % str(mmc_dev))
47    assert response == ''
48
49    response = ubman.run_command('mmc rescan; mmc dev %s' %
50                                          str(mmc_dev))
51    assert response.find('is current device')
52
53    part_lines = ubman.run_command('mmc part').splitlines()
54    part_list = {}
55    cur_partname = ''
56
57    for line in part_lines:
58        if '"' in line:
59            start_pt = line.find('"')
60            end_pt = line.find('"', start_pt + 1)
61            cur_partname = line[start_pt + 1: end_pt]
62
63        if 'guid:' in line:
64            guid_to_check = line.split('guid:\t')
65            part_list[cur_partname] = guid_to_check[1]
66
67    # lets check all guids with avb get_guid
68    for part, guid in part_list.items():
69        avb_guid_resp = ubman.run_command('avb get_uuid %s' % part)
70        assert guid == avb_guid_resp.split('UUID: ')[1]
71
72
73@pytest.mark.buildconfigspec('cmd_avb')
74def test_avb_read_rb(ubman):
75    """Test reading rollback indexes
76    """
77
78    response = ubman.run_command('avb init %s' % str(mmc_dev))
79    assert response == ''
80
81    response = ubman.run_command('avb read_rb 1')
82    assert response == 'Rollback index: 0'
83
84
85@pytest.mark.buildconfigspec('cmd_avb')
86def test_avb_is_unlocked(ubman):
87    """Test if device is in the unlocked state
88    """
89
90    response = ubman.run_command('avb init %s' % str(mmc_dev))
91    assert response == ''
92
93    response = ubman.run_command('avb is_unlocked')
94    assert response == 'Unlocked = 1'
95
96
97@pytest.mark.buildconfigspec('cmd_avb')
98@pytest.mark.buildconfigspec('cmd_mmc')
99@pytest.mark.notbuildconfigspec('sandbox')
100def test_avb_mmc_read(ubman):
101    """Test mmc read operation
102    """
103
104    response = ubman.run_command('mmc rescan; mmc dev %s 0' %
105                                          str(mmc_dev))
106    assert response.find('is current device')
107
108    response = ubman.run_command('mmc read 0x%x 0x100 0x1' % temp_addr)
109    assert response.find('read: OK')
110
111    response = ubman.run_command('avb init %s' % str(mmc_dev))
112    assert response == ''
113
114    response = ubman.run_command('avb read_part xloader 0 100 0x%x' %
115                                           temp_addr2)
116    assert response.find('Read 512 bytes')
117
118    # Now lets compare two buffers
119    response = ubman.run_command('cmp 0x%x 0x%x 40' %
120                                          (temp_addr, temp_addr2))
121    assert response.find('64 word')
122
123
124@pytest.mark.buildconfigspec('cmd_avb')
125@pytest.mark.buildconfigspec('optee_ta_avb')
126def test_avb_persistent_values(ubman):
127    """Test reading/writing persistent storage to avb
128    """
129
130    response = ubman.run_command('avb init %s' % str(mmc_dev))
131    assert response == ''
132
133    response = ubman.run_command('avb write_pvalue test value_value')
134    assert response == 'Wrote 12 bytes'
135
136    response = ubman.run_command('avb read_pvalue test 12')
137    assert response == 'Read 12 bytes, value = value_value'
138