blob: a9a6305f05cc6131d27821169a9104e6244ce1a9 [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
Tom Wai-Hong Tambea57b32011-09-02 18:27:47 +080062 def is_available(self):
63 """Function for polling the RPC server availability.
64
65 Returns:
66 Always True.
67 """
68 return True
69
70
Tom Wai-Hong Tamb8a58ef2011-10-11 23:53:10 -070071 def run_shell_command(self, command):
72 """Run shell command.
73
74 Args:
75 command: A shell command to be run.
76 """
77 self._chromeos_interface.log('Requesting run shell command')
78 self._chromeos_interface.run_shell_command(command)
79
80
81 def run_shell_command_get_output(self, command):
82 """Run shell command and get its console output.
83
84 Args:
85 command: A shell command to be run.
86
87 Returns:
88 A list of strings stripped of the newline characters.
89 """
90 self._chromeos_interface.log(
91 'Requesting run shell command and get its console output')
92 return self._chromeos_interface.run_shell_command_get_output(command)
93
94
95 def software_reboot(self):
96 """Request software reboot."""
97 self._chromeos_interface.log('Requesting software reboot')
98 self._chromeos_interface.run_shell_command('reboot')
99
100
101 def get_crossystem_value(self, key):
102 """Get crossystem value of the requested key.
103
104 Args:
105 key: A crossystem key.
106
107 Returns:
108 A string of the requested crossystem value.
109 """
110 self._chromeos_interface.log('Requesting get crossystem value')
111 return self._chromeos_interface.run_shell_command_get_output(
112 'crossystem %s' % key)[0]
113
114
115 def set_try_fw_b(self):
116 """Set 'Try Frimware B' flag in crossystem."""
117 self._chromeos_interface.log('Requesting restart with firmware B')
118 self._chromeos_interface.cs.fwb_tries = 1
119
120
Tom Wai-Hong Tam76c75072011-10-25 18:00:12 +0800121 def request_recovery_boot(self):
122 """Request running in recovery mode on the restart."""
123 self._chromeos_interface.log('Requesting restart in recovery mode')
124 self._chromeos_interface.cs.request_recovery()
125
126
Tom Wai-Hong Tamb8a58ef2011-10-11 23:53:10 -0700127 def corrupt_firmware(self, section):
128 """Corrupt the requested firmware section.
129
130 Args:
131 section: A firmware section, either 'a' or 'b'.
132 """
133 self._chromeos_interface.log('Corrupting firmware %s' % section)
134 self._flashrom_handler.corrupt_firmware(section)
135
136
137 def restore_firmware(self, section):
138 """Restore the requested firmware section (previously corrupted).
139
140 Args:
141 section: A firmware section, either 'a' or 'b'.
142 """
143 self._chromeos_interface.log('Restoring firmware %s' % section)
144 self._flashrom_handler.restore_firmware(section)
145
146
Tom Wai-Hong Tambea57b32011-09-02 18:27:47 +0800147 def cleanup(self):
148 """Cleanup for the RPC server. Currently nothing."""
149 pass
150
151
Tom Wai-Hong Tamb8a58ef2011-10-11 23:53:10 -0700152def main():
153 parser = OptionParser(usage='Usage: %prog [options]')
154 parser.add_option('--port', type='int', dest='port', default=9990,
155 help='port number of XMLRPC server')
156 (options, args) = parser.parse_args()
157
158 faft_client = FAFTClient()
159
160 # Launch the XMLRPC server to provide FAFTClient commands.
161 server = SimpleXMLRPCServer(('localhost', options.port), allow_none=True)
162 server.register_introspection_functions()
163 server.register_instance(faft_client)
164 print 'XMLRPC Server: Serving FAFTClient on port %s' % options.port
165 server.serve_forever()
166
167
168if __name__ == '__main__':
169 main()