• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2021 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import subprocess
6
7from autotest_lib.client.bin import test
8from autotest_lib.client.bin import utils
9from autotest_lib.client.common_lib import error
10
11
12class firmware_CbfsMcache(test.test):
13    """Validates that the CBFS metadata cache didn't overflow."""
14    version = 1
15
16    MCACHE_MAGIC_FULL = b'FULL'
17    MCACHE_MAGIC_END = b'$END'
18
19    CBMEM_RO_MCACHE = b'524d5346'
20    CBMEM_RW_MCACHE = b'574d5346'
21
22    def cbmem(self, *args):
23        """Runs 'cbmem' utility with specified arguments and returns output."""
24        # Cannot use utils.run because it force-decodes stdout as UTF-8.
25        return subprocess.check_output(('cbmem', ) + args)
26
27    def has_mcache(self):
28        """Returns true iff there's an RO MCACHE section in CBMEM."""
29        return self.CBMEM_RO_MCACHE in self.cbmem('-l')
30
31    def check_mcache(self, cbmem_id, name):
32        """Fail if the cbmem_id mcache wasn't terminated with an END token."""
33        mcache = self.cbmem('-r', cbmem_id)
34        if mcache[-4:] == self.MCACHE_MAGIC_FULL:
35            raise error.TestFail("CBFS %s mcache overflowed!" % name)
36        if mcache[-4:] != self.MCACHE_MAGIC_END:
37            raise error.TestError(
38                    "CBFS %s mcache ends with invalid token (%s)!" %
39                    mcache[-4:].__repr__())
40
41    def run_once(self):
42        """Fail if mcaches exists and wasn't terminated with an END token."""
43        if utils.get_board() == 'volteer':
44            raise error.TestNAError("Skipped on Volteer, see b/187561710.")
45        if not self.has_mcache():
46            raise error.TestNAError("This platform doesn't use CBFS mcache.")
47        self.check_mcache(self.CBMEM_RO_MCACHE, "RO")
48        self.check_mcache(self.CBMEM_RW_MCACHE, "RW")
49