blob: e17f16685c8d27d30d6316e299b3c158c5799fe9 [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
Tom Wai-Hong Tam0e680af2011-10-26 14:32:55 +080012import functools
Tom Wai-Hong Tamb8a58ef2011-10-11 23:53:10 -070013import os
14import sys
15import tempfile
16from optparse import OptionParser
17from SimpleXMLRPCServer import SimpleXMLRPCServer
18
19# Import libraries from SAFT.
20sys.path.append('/usr/sbin/firmware/saft')
21import chromeos_interface
22import flashrom_handler
23import kernel_handler
24import saft_flashrom_util
25import tpm_handler
26
27
Tom Wai-Hong Tam0e680af2011-10-26 14:32:55 +080028def allow_multiple_section_input(image_operator):
29 @functools.wraps(image_operator)
30 def wrapper(self, section):
31 if type(section) in (tuple, list):
32 for sec in section:
33 image_operator(self, sec)
34 else:
35 image_operator(self, section)
36 return wrapper
37
38
Tom Wai-Hong Tamb8a58ef2011-10-11 23:53:10 -070039class FAFTClient(object):
40 """A class of FAFT client which aggregates some useful functions of SAFT.
41
42 This class can be exposed via a XMLRPC server such that its functions can
43 be accessed remotely.
44
45 Attributes:
46 _chromeos_interface: An object to encapsulate OS services functions.
47 _flashrom_handler: An object to automate flashrom testing.
48 _kernel_handler: An object to provide kernel related actions.
49 _tpm_handler: An object to control TPM device.
50 """
51
52 def __init__(self):
53 """Initialize the data attributes of this class."""
54 tmp_dir = tempfile.mkdtemp()
55
56 # TODO(waihong): Move the explicit object.init() methods to the
57 # objects' constructors (ChromeOSInterface, FlashromHandler,
58 # KernelHandler, and TpmHandler).
59 self._chromeos_interface = chromeos_interface.ChromeOSInterface(False)
60 self._chromeos_interface.init(tmp_dir)
61
62 self._flashrom_handler = flashrom_handler.FlashromHandler()
63 self._flashrom_handler.init(saft_flashrom_util,
64 self._chromeos_interface)
65 self._flashrom_handler.new_image()
66
67 self._kernel_handler = kernel_handler.KernelHandler()
68 self._kernel_handler.init(self._chromeos_interface)
69
70 self._tpm_handler = tpm_handler.TpmHandler()
71 self._tpm_handler.init(self._chromeos_interface)
72
73
Tom Wai-Hong Tambea57b32011-09-02 18:27:47 +080074 def is_available(self):
75 """Function for polling the RPC server availability.
76
77 Returns:
78 Always True.
79 """
80 return True
81
82
Tom Wai-Hong Tamb8a58ef2011-10-11 23:53:10 -070083 def run_shell_command(self, command):
84 """Run shell command.
85
86 Args:
87 command: A shell command to be run.
88 """
89 self._chromeos_interface.log('Requesting run shell command')
90 self._chromeos_interface.run_shell_command(command)
91
92
93 def run_shell_command_get_output(self, command):
94 """Run shell command and get its console output.
95
96 Args:
97 command: A shell command to be run.
98
99 Returns:
100 A list of strings stripped of the newline characters.
101 """
102 self._chromeos_interface.log(
103 'Requesting run shell command and get its console output')
104 return self._chromeos_interface.run_shell_command_get_output(command)
105
106
107 def software_reboot(self):
108 """Request software reboot."""
109 self._chromeos_interface.log('Requesting software reboot')
110 self._chromeos_interface.run_shell_command('reboot')
111
112
113 def get_crossystem_value(self, key):
114 """Get crossystem value of the requested key.
115
116 Args:
117 key: A crossystem key.
118
119 Returns:
120 A string of the requested crossystem value.
121 """
122 self._chromeos_interface.log('Requesting get crossystem value')
123 return self._chromeos_interface.run_shell_command_get_output(
124 'crossystem %s' % key)[0]
125
126
127 def set_try_fw_b(self):
128 """Set 'Try Frimware B' flag in crossystem."""
129 self._chromeos_interface.log('Requesting restart with firmware B')
130 self._chromeos_interface.cs.fwb_tries = 1
131
132
Tom Wai-Hong Tam76c75072011-10-25 18:00:12 +0800133 def request_recovery_boot(self):
134 """Request running in recovery mode on the restart."""
135 self._chromeos_interface.log('Requesting restart in recovery mode')
136 self._chromeos_interface.cs.request_recovery()
137
138
Tom Wai-Hong Tam0e680af2011-10-26 14:32:55 +0800139 @allow_multiple_section_input
Tom Wai-Hong Tamb8a58ef2011-10-11 23:53:10 -0700140 def corrupt_firmware(self, section):
141 """Corrupt the requested firmware section.
142
143 Args:
144 section: A firmware section, either 'a' or 'b'.
145 """
146 self._chromeos_interface.log('Corrupting firmware %s' % section)
147 self._flashrom_handler.corrupt_firmware(section)
148
149
Tom Wai-Hong Tam0e680af2011-10-26 14:32:55 +0800150 @allow_multiple_section_input
Tom Wai-Hong Tamb8a58ef2011-10-11 23:53:10 -0700151 def restore_firmware(self, section):
152 """Restore the requested firmware section (previously corrupted).
153
154 Args:
155 section: A firmware section, either 'a' or 'b'.
156 """
157 self._chromeos_interface.log('Restoring firmware %s' % section)
158 self._flashrom_handler.restore_firmware(section)
159
160
Tom Wai-Hong Tambea57b32011-09-02 18:27:47 +0800161 def cleanup(self):
162 """Cleanup for the RPC server. Currently nothing."""
163 pass
164
165
Tom Wai-Hong Tamb8a58ef2011-10-11 23:53:10 -0700166def main():
167 parser = OptionParser(usage='Usage: %prog [options]')
168 parser.add_option('--port', type='int', dest='port', default=9990,
169 help='port number of XMLRPC server')
170 (options, args) = parser.parse_args()
171
172 faft_client = FAFTClient()
173
174 # Launch the XMLRPC server to provide FAFTClient commands.
175 server = SimpleXMLRPCServer(('localhost', options.port), allow_none=True)
176 server.register_introspection_functions()
177 server.register_instance(faft_client)
178 print 'XMLRPC Server: Serving FAFTClient on port %s' % options.port
179 server.serve_forever()
180
181
182if __name__ == '__main__':
183 main()