• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright (c) 2012 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 logging
6import os
7import re
8
9from autotest_lib.client.bin import test, utils
10from autotest_lib.client.common_lib import error
11
12class firmware_LockedME(test.test):
13    # Needed by autotest
14    version = 1
15
16    # Temporary file to read BIOS image into. We run in a tempdir anyway, so it
17    # doesn't need a path.
18    BIOS_FILE = 'bios.bin'
19
20    def flashrom(self, ignore_status=False, args=()):
21        """Run flashrom, expect it to work. Fail if it doesn't"""
22        extra = ['-p', 'host'] + list(args)
23        return utils.run('flashrom', ignore_status=ignore_status, args=extra)
24
25    def has_ME(self):
26        """See if we can detect an ME.
27        FREG* is printed only when HSFS_FDV is set, which means the descriptor
28        table is valid. If we're running a BIOS without a valid descriptor this
29        step will fail. Unfortunately, we don't know of a simple and reliable
30        way to identify systems that have ME hardware.
31        """
32        logging.info('See if we have an ME...')
33        r = self.flashrom(args=('-V',))
34        return r.stdout.find("FREG0") >= 0
35
36    def try_to_rewrite(self, sectname):
37        """If we can modify the ME section, restore it and raise an error."""
38        logging.info('Try to write section %s...', sectname)
39        size = os.stat(sectname).st_size
40        utils.run('dd', args=('if=/dev/urandom', 'of=newdata',
41                              'count=1', 'bs=%d' % (size)))
42        r = self.flashrom(args=('-w', self.BIOS_FILE,
43                                '-i' , '%s:newdata' % (sectname),
44                                '--fast-verify'),
45                          ignore_status=True)
46        if not r.exit_status:
47            logging.info('Oops, it worked! Put it back...')
48            self.flashrom(args=('-w', self.BIOS_FILE,
49                                '-i', '%s:%s' % (sectname, sectname),
50                                '--fast-verify'),
51                          ignore_status=True)
52            raise error.TestFail('%s is writable, ME is unlocked' % sectname)
53
54    def run_once(self, expect_me_present=True):
55        """Fail unless the ME is locked.
56
57        @param expect_me_present: False means the system has no ME.
58        """
59
60        # See if the system even has an ME, and whether we expected that.
61        if self.has_ME():
62            if not expect_me_present:
63                raise error.TestFail('We expected no ME, but found one anyway')
64        else:
65            if expect_me_present:
66                raise error.TestNAError("No ME found. That's probably wrong.")
67            else:
68                logging.info('We expected no ME and we have no ME, so pass.')
69                return
70
71        # See if coreboot told us that the ME is still in Manufacturing Mode.
72        # It shouldn't be. We have to look only at the last thing it reports
73        # because it reports the values twice and the first one isn't always
74        # reliable.
75        logging.info('Check for Manufacturing Mode...')
76        last = None
77        with open('/sys/firmware/log') as infile:
78            for line in infile:
79                if re.search('ME: Manufacturing Mode', line):
80                    last = line
81        if last is not None and last.find("YES") >= 0:
82            raise error.TestFail("The ME is still in Manufacturing Mode")
83
84        # Get the bios image and extract the ME components
85        logging.info('Pull the ME components from the BIOS...')
86        self.flashrom(args=('-r', self.BIOS_FILE))
87        utils.run('dump_fmap', args=('-x', self.BIOS_FILE, 'SI_ME', 'SI_DESC'))
88
89        # flashrom should have read the SI_ME section as all 0xff's. If not,
90        # the ME is not locked.
91        logging.info('SI_ME should be all 0xff...')
92        with open('SI_ME', 'rb') as f:
93            for c in f.read():
94                if c != chr(0xff):
95                    raise error.TestFail("SI_ME was readable by flashrom")
96
97        # So far, so good, but we need to be certain. Rather than parse what
98        # flashrom tells us about the ME-related registers, we'll just try to
99        # change the ME components. We shouldn't be able to.
100        self.try_to_rewrite('SI_DESC')
101        self.try_to_rewrite('SI_ME')
102
103        # Okay, that's about all we can try. Looks like it's locked.
104