blob: 5607b3071fe41dbb58005fb0865f3bf0ed59cd8a [file] [log] [blame]
Tom Wai-Hong Tamb8a58ef2011-10-11 23:53:10 -07001# Copyright (c) 2011 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
5"""Exposes the FAFTClient interface over XMLRPC.
6
7It launches a XMLRPC server and exposes the interface of FAFTClient object.
8The FAFTClient object aggreates some useful functions of exisintg SAFT
9libraries.
10"""
11
12import os
13import sys
14import tempfile
15from optparse import OptionParser
16from SimpleXMLRPCServer import SimpleXMLRPCServer
17
18# Import libraries from SAFT.
19sys.path.append('/usr/sbin/firmware/saft')
20import chromeos_interface
21import flashrom_handler
22import kernel_handler
23import saft_flashrom_util
24import tpm_handler
25
26
27class FAFTClient(object):
28 """A class of FAFT client which aggregates some useful functions of SAFT.
29
30 This class can be exposed via a XMLRPC server such that its functions can
31 be accessed remotely.
32
33 Attributes:
34 _chromeos_interface: An object to encapsulate OS services functions.
35 _flashrom_handler: An object to automate flashrom testing.
36 _kernel_handler: An object to provide kernel related actions.
37 _tpm_handler: An object to control TPM device.
38 """
39
40 def __init__(self):
41 """Initialize the data attributes of this class."""
42 tmp_dir = tempfile.mkdtemp()
43
44 # TODO(waihong): Move the explicit object.init() methods to the
45 # objects' constructors (ChromeOSInterface, FlashromHandler,
46 # KernelHandler, and TpmHandler).
47 self._chromeos_interface = chromeos_interface.ChromeOSInterface(False)
48 self._chromeos_interface.init(tmp_dir)
49
50 self._flashrom_handler = flashrom_handler.FlashromHandler()
51 self._flashrom_handler.init(saft_flashrom_util,
52 self._chromeos_interface)
53 self._flashrom_handler.new_image()
54
55 self._kernel_handler = kernel_handler.KernelHandler()
56 self._kernel_handler.init(self._chromeos_interface)
57
58 self._tpm_handler = tpm_handler.TpmHandler()
59 self._tpm_handler.init(self._chromeos_interface)
60
61
62 def run_shell_command(self, command):
63 """Run shell command.
64
65 Args:
66 command: A shell command to be run.
67 """
68 self._chromeos_interface.log('Requesting run shell command')
69 self._chromeos_interface.run_shell_command(command)
70
71
72 def run_shell_command_get_output(self, command):
73 """Run shell command and get its console output.
74
75 Args:
76 command: A shell command to be run.
77
78 Returns:
79 A list of strings stripped of the newline characters.
80 """
81 self._chromeos_interface.log(
82 'Requesting run shell command and get its console output')
83 return self._chromeos_interface.run_shell_command_get_output(command)
84
85
86 def software_reboot(self):
87 """Request software reboot."""
88 self._chromeos_interface.log('Requesting software reboot')
89 self._chromeos_interface.run_shell_command('reboot')
90
91
92 def get_crossystem_value(self, key):
93 """Get crossystem value of the requested key.
94
95 Args:
96 key: A crossystem key.
97
98 Returns:
99 A string of the requested crossystem value.
100 """
101 self._chromeos_interface.log('Requesting get crossystem value')
102 return self._chromeos_interface.run_shell_command_get_output(
103 'crossystem %s' % key)[0]
104
105
106 def set_try_fw_b(self):
107 """Set 'Try Frimware B' flag in crossystem."""
108 self._chromeos_interface.log('Requesting restart with firmware B')
109 self._chromeos_interface.cs.fwb_tries = 1
110
111
112 def corrupt_firmware(self, section):
113 """Corrupt the requested firmware section.
114
115 Args:
116 section: A firmware section, either 'a' or 'b'.
117 """
118 self._chromeos_interface.log('Corrupting firmware %s' % section)
119 self._flashrom_handler.corrupt_firmware(section)
120
121
122 def restore_firmware(self, section):
123 """Restore the requested firmware section (previously corrupted).
124
125 Args:
126 section: A firmware section, either 'a' or 'b'.
127 """
128 self._chromeos_interface.log('Restoring firmware %s' % section)
129 self._flashrom_handler.restore_firmware(section)
130
131
132def main():
133 parser = OptionParser(usage='Usage: %prog [options]')
134 parser.add_option('--port', type='int', dest='port', default=9990,
135 help='port number of XMLRPC server')
136 (options, args) = parser.parse_args()
137
138 faft_client = FAFTClient()
139
140 # Launch the XMLRPC server to provide FAFTClient commands.
141 server = SimpleXMLRPCServer(('localhost', options.port), allow_none=True)
142 server.register_introspection_functions()
143 server.register_instance(faft_client)
144 print 'XMLRPC Server: Serving FAFTClient on port %s' % options.port
145 server.serve_forever()
146
147
148if __name__ == '__main__':
149 main()