blob: 7138303fa7df7e8185a053538935489fb98857e7 [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
121 def corrupt_firmware(self, section):
122 """Corrupt the requested firmware section.
123
124 Args:
125 section: A firmware section, either 'a' or 'b'.
126 """
127 self._chromeos_interface.log('Corrupting firmware %s' % section)
128 self._flashrom_handler.corrupt_firmware(section)
129
130
131 def restore_firmware(self, section):
132 """Restore the requested firmware section (previously corrupted).
133
134 Args:
135 section: A firmware section, either 'a' or 'b'.
136 """
137 self._chromeos_interface.log('Restoring firmware %s' % section)
138 self._flashrom_handler.restore_firmware(section)
139
140
Tom Wai-Hong Tambea57b32011-09-02 18:27:47 +0800141 def cleanup(self):
142 """Cleanup for the RPC server. Currently nothing."""
143 pass
144
145
Tom Wai-Hong Tamb8a58ef2011-10-11 23:53:10 -0700146def main():
147 parser = OptionParser(usage='Usage: %prog [options]')
148 parser.add_option('--port', type='int', dest='port', default=9990,
149 help='port number of XMLRPC server')
150 (options, args) = parser.parse_args()
151
152 faft_client = FAFTClient()
153
154 # Launch the XMLRPC server to provide FAFTClient commands.
155 server = SimpleXMLRPCServer(('localhost', options.port), allow_none=True)
156 server.register_introspection_functions()
157 server.register_instance(faft_client)
158 print 'XMLRPC Server: Serving FAFTClient on port %s' % options.port
159 server.serve_forever()
160
161
162if __name__ == '__main__':
163 main()