1#!/usr/bin/python 2# 3# Copyright (c) 2014 The Chromium OS Authors. All rights reserved. 4# Use of this source code is governed by a BSD-style license that can be 5# found in the LICENSE file. 6 7import logging, os 8from autotest_lib.client.bin import utils, test 9from autotest_lib.client.common_lib import error 10 11class kernel_CrosECSysfs(test.test): 12 '''Make sure the EC sysfs interface provides meaningful output''' 13 version = 1 14 15 cros_ec = '/dev/cros_ec' 16 sysfs_path = '/sys/devices/virtual/chromeos/cros_ec' 17 kernel_ver = os.uname()[2] 18 if utils.compare_versions(kernel_ver, "3.14") >= 0: 19 sysfs_path = '/sys/class/chromeos/cros_ec' 20 21 def _read_file(self, filename): 22 """ 23 Return the contents of the given file or fail. 24 25 @param filename Full path to the file to be read 26 """ 27 try: 28 content = utils.read_file(filename) 29 except Exception as err: 30 raise error.TestFail('sysfs file problem: %s' % err) 31 return content 32 33 def _read_sysfs(self, filename): 34 """ 35 Read the contents of the given sysfs file or fail 36 37 @param filename Name of the file within the sysfs interface directory 38 """ 39 fullpath = os.path.join(self.sysfs_path, filename) 40 return self._read_file(fullpath) 41 42 def _read_field(self, filename, field): 43 """ 44 Return the given field from the sysfs file or fail 45 46 @param filename Name of the file within the sysfs interface directory 47 @param field Name of field to match in the file content 48 """ 49 fullpath = os.path.join(self.sysfs_path, filename) 50 content = self._read_file(fullpath) 51 match = utils.get_field(content, 0, field) 52 if match is None: 53 raise error.TestFail("no '%s' field in %s" % (field, fullpath)) 54 return match 55 56 def run_once(self): 57 """ 58 Quick check for the existence of the basic sysfs files 59 """ 60 # If /dev/cros_ec isn't present, then the MFD_CROS_EC_DEV driver isn't 61 # present, so there's no point to looking for the sysfs interface to it. 62 if not os.path.exists(self.cros_ec): 63 raise error.TestFail("%s not found. No driver?" % self.cros_ec) 64 65 flashsize = self._read_field('flashinfo', 'FlashSize') 66 logging.info("flashsize is %s", flashsize) 67 68 build = self._read_field('version', 'Build info:') 69 logging.info("build is %s", build) 70 71 reboot = self._read_sysfs('reboot') 72 if reboot.find("ro") < 0: 73 raise error.TestFail('reboot help is weird: %s' % reboot) 74 logging.info("reboot is %s", reboot) 75