Tom Wai-Hong Tam | b8a58ef | 2011-10-11 23:53:10 -0700 | [diff] [blame] | 1 | # 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 | |
| 7 | It launches a XMLRPC server and exposes the interface of FAFTClient object. |
| 8 | The FAFTClient object aggreates some useful functions of exisintg SAFT |
| 9 | libraries. |
| 10 | """ |
| 11 | |
Tom Wai-Hong Tam | 0e680af | 2011-10-26 14:32:55 +0800 | [diff] [blame] | 12 | import functools |
Tom Wai-Hong Tam | b8a58ef | 2011-10-11 23:53:10 -0700 | [diff] [blame] | 13 | import os |
| 14 | import sys |
| 15 | import tempfile |
| 16 | from optparse import OptionParser |
| 17 | from SimpleXMLRPCServer import SimpleXMLRPCServer |
| 18 | |
| 19 | # Import libraries from SAFT. |
Tom Wai-Hong Tam | c1576b7 | 2011-11-08 11:36:16 +0800 | [diff] [blame] | 20 | sys.path.append('/usr/local/sbin/firmware/saft') |
Tom Wai-Hong Tam | b8a58ef | 2011-10-11 23:53:10 -0700 | [diff] [blame] | 21 | import chromeos_interface |
| 22 | import flashrom_handler |
| 23 | import kernel_handler |
| 24 | import saft_flashrom_util |
| 25 | import tpm_handler |
| 26 | |
| 27 | |
Tom Wai-Hong Tam | 0e680af | 2011-10-26 14:32:55 +0800 | [diff] [blame] | 28 | def 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 Tam | b8a58ef | 2011-10-11 23:53:10 -0700 | [diff] [blame] | 39 | class 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 Tam | e8f291a | 2011-12-08 22:03:53 +0800 | [diff] [blame] | 74 | def _dispatch(self, method, params): |
| 75 | """This _dispatch method handles string conversion especially. |
| 76 | |
| 77 | Since we turn off allow_dotted_names option. So any string conversion, |
| 78 | like str(FAFTClient.method), i.e. FAFTClient.method.__str__, failed |
| 79 | via XML RPC call. |
| 80 | """ |
| 81 | is_str = method.endswith('.__str__') |
| 82 | if is_str: |
| 83 | method = method.rsplit('.', 1)[0] |
| 84 | try: |
| 85 | func = getattr(self, method) |
| 86 | except AttributeError: |
| 87 | raise Exception('method "%s" is not supported' % method) |
| 88 | else: |
| 89 | if is_str: |
| 90 | return str(func) |
| 91 | else: |
| 92 | return func(*params) |
| 93 | |
| 94 | |
Tom Wai-Hong Tam | bea57b3 | 2011-09-02 18:27:47 +0800 | [diff] [blame] | 95 | def is_available(self): |
| 96 | """Function for polling the RPC server availability. |
| 97 | |
| 98 | Returns: |
| 99 | Always True. |
| 100 | """ |
| 101 | return True |
| 102 | |
| 103 | |
Tom Wai-Hong Tam | b8a58ef | 2011-10-11 23:53:10 -0700 | [diff] [blame] | 104 | def run_shell_command(self, command): |
| 105 | """Run shell command. |
| 106 | |
| 107 | Args: |
| 108 | command: A shell command to be run. |
| 109 | """ |
| 110 | self._chromeos_interface.log('Requesting run shell command') |
| 111 | self._chromeos_interface.run_shell_command(command) |
| 112 | |
| 113 | |
| 114 | def run_shell_command_get_output(self, command): |
| 115 | """Run shell command and get its console output. |
| 116 | |
| 117 | Args: |
| 118 | command: A shell command to be run. |
| 119 | |
| 120 | Returns: |
| 121 | A list of strings stripped of the newline characters. |
| 122 | """ |
| 123 | self._chromeos_interface.log( |
| 124 | 'Requesting run shell command and get its console output') |
| 125 | return self._chromeos_interface.run_shell_command_get_output(command) |
| 126 | |
| 127 | |
| 128 | def software_reboot(self): |
| 129 | """Request software reboot.""" |
| 130 | self._chromeos_interface.log('Requesting software reboot') |
| 131 | self._chromeos_interface.run_shell_command('reboot') |
| 132 | |
| 133 | |
| 134 | def get_crossystem_value(self, key): |
| 135 | """Get crossystem value of the requested key. |
| 136 | |
| 137 | Args: |
| 138 | key: A crossystem key. |
| 139 | |
| 140 | Returns: |
| 141 | A string of the requested crossystem value. |
| 142 | """ |
| 143 | self._chromeos_interface.log('Requesting get crossystem value') |
| 144 | return self._chromeos_interface.run_shell_command_get_output( |
| 145 | 'crossystem %s' % key)[0] |
| 146 | |
| 147 | |
Tom Wai-Hong Tam | cfda61f | 2011-11-02 17:41:01 +0800 | [diff] [blame] | 148 | def get_root_dev(self): |
| 149 | """Get the name of root device without partition number. |
| 150 | |
| 151 | Returns: |
| 152 | A string of the root device without partition number. |
| 153 | """ |
| 154 | self._chromeos_interface.log('Requesting get root device') |
| 155 | return self._chromeos_interface.get_root_dev() |
| 156 | |
| 157 | |
| 158 | def get_root_part(self): |
| 159 | """Get the name of root device with partition number. |
| 160 | |
| 161 | Returns: |
| 162 | A string of the root device with partition number. |
| 163 | """ |
| 164 | self._chromeos_interface.log('Requesting get root part') |
| 165 | return self._chromeos_interface.get_root_part() |
| 166 | |
| 167 | |
Tom Wai-Hong Tam | b8a58ef | 2011-10-11 23:53:10 -0700 | [diff] [blame] | 168 | def set_try_fw_b(self): |
| 169 | """Set 'Try Frimware B' flag in crossystem.""" |
| 170 | self._chromeos_interface.log('Requesting restart with firmware B') |
| 171 | self._chromeos_interface.cs.fwb_tries = 1 |
| 172 | |
| 173 | |
Tom Wai-Hong Tam | 76c7507 | 2011-10-25 18:00:12 +0800 | [diff] [blame] | 174 | def request_recovery_boot(self): |
| 175 | """Request running in recovery mode on the restart.""" |
| 176 | self._chromeos_interface.log('Requesting restart in recovery mode') |
| 177 | self._chromeos_interface.cs.request_recovery() |
| 178 | |
| 179 | |
Tom Wai-Hong Tam | 81f7000 | 2011-12-13 12:29:46 +0800 | [diff] [blame^] | 180 | def get_firmware_flags(self, section): |
| 181 | """Get the preamble flags of a firmware section. |
| 182 | |
| 183 | Args: |
| 184 | section: A firmware section, either 'a' or 'b'. |
| 185 | |
| 186 | Returns: |
| 187 | An integer of the preamble flags. |
| 188 | """ |
| 189 | self._chromeos_interface.log('Getting preamble flags of firmware %s' % |
| 190 | section) |
| 191 | return self._flashrom_handler.get_section_flags(section) |
| 192 | |
| 193 | |
Tom Wai-Hong Tam | 0e680af | 2011-10-26 14:32:55 +0800 | [diff] [blame] | 194 | @allow_multiple_section_input |
Tom Wai-Hong Tam | b8a58ef | 2011-10-11 23:53:10 -0700 | [diff] [blame] | 195 | def corrupt_firmware(self, section): |
Tom Wai-Hong Tam | 9aea821 | 2011-12-12 15:08:45 +0800 | [diff] [blame] | 196 | """Corrupt the requested firmware section signature. |
Tom Wai-Hong Tam | b8a58ef | 2011-10-11 23:53:10 -0700 | [diff] [blame] | 197 | |
| 198 | Args: |
| 199 | section: A firmware section, either 'a' or 'b'. |
| 200 | """ |
Tom Wai-Hong Tam | 9aea821 | 2011-12-12 15:08:45 +0800 | [diff] [blame] | 201 | self._chromeos_interface.log('Corrupting firmware signature %s' % |
| 202 | section) |
Tom Wai-Hong Tam | b8a58ef | 2011-10-11 23:53:10 -0700 | [diff] [blame] | 203 | self._flashrom_handler.corrupt_firmware(section) |
| 204 | |
| 205 | |
Tom Wai-Hong Tam | 0e680af | 2011-10-26 14:32:55 +0800 | [diff] [blame] | 206 | @allow_multiple_section_input |
Tom Wai-Hong Tam | 81f7000 | 2011-12-13 12:29:46 +0800 | [diff] [blame^] | 207 | def corrupt_firmware_body(self, section): |
| 208 | """Corrupt the requested firmware section body. |
| 209 | |
| 210 | Args: |
| 211 | section: A firmware section, either 'a' or 'b'. |
| 212 | """ |
| 213 | self._chromeos_interface.log('Corrupting firmware body %s' % |
| 214 | section) |
| 215 | self._flashrom_handler.corrupt_firmware_body(section) |
| 216 | |
| 217 | |
| 218 | @allow_multiple_section_input |
Tom Wai-Hong Tam | b8a58ef | 2011-10-11 23:53:10 -0700 | [diff] [blame] | 219 | def restore_firmware(self, section): |
Tom Wai-Hong Tam | 9aea821 | 2011-12-12 15:08:45 +0800 | [diff] [blame] | 220 | """Restore the previously corrupted firmware section signature. |
Tom Wai-Hong Tam | b8a58ef | 2011-10-11 23:53:10 -0700 | [diff] [blame] | 221 | |
| 222 | Args: |
| 223 | section: A firmware section, either 'a' or 'b'. |
| 224 | """ |
Tom Wai-Hong Tam | 9aea821 | 2011-12-12 15:08:45 +0800 | [diff] [blame] | 225 | self._chromeos_interface.log('Restoring firmware signature %s' % |
| 226 | section) |
Tom Wai-Hong Tam | b8a58ef | 2011-10-11 23:53:10 -0700 | [diff] [blame] | 227 | self._flashrom_handler.restore_firmware(section) |
| 228 | |
| 229 | |
Tom Wai-Hong Tam | cfda61f | 2011-11-02 17:41:01 +0800 | [diff] [blame] | 230 | @allow_multiple_section_input |
Tom Wai-Hong Tam | 81f7000 | 2011-12-13 12:29:46 +0800 | [diff] [blame^] | 231 | def restore_firmware_body(self, section): |
| 232 | """Restore the previously corrupted firmware section body. |
| 233 | |
| 234 | Args: |
| 235 | section: A firmware section, either 'a' or 'b'. |
| 236 | """ |
| 237 | self._chromeos_interface.log('Restoring firmware body %s' % |
| 238 | section) |
| 239 | self._flashrom_handler.restore_firmware_body(section) |
| 240 | |
| 241 | |
| 242 | @allow_multiple_section_input |
Tom Wai-Hong Tam | cfda61f | 2011-11-02 17:41:01 +0800 | [diff] [blame] | 243 | def corrupt_kernel(self, section): |
| 244 | """Corrupt the requested kernel section. |
| 245 | |
| 246 | Args: |
| 247 | section: A kernel section, either 'a' or 'b'. |
| 248 | """ |
| 249 | self._chromeos_interface.log('Corrupting kernel %s' % section) |
| 250 | self._kernel_handler.corrupt_kernel(section) |
| 251 | |
| 252 | |
| 253 | @allow_multiple_section_input |
| 254 | def restore_kernel(self, section): |
| 255 | """Restore the requested kernel section (previously corrupted). |
| 256 | |
| 257 | Args: |
| 258 | section: A kernel section, either 'a' or 'b'. |
| 259 | """ |
| 260 | self._chromeos_interface.log('restoring kernel %s' % section) |
| 261 | self._kernel_handler.restore_kernel(section) |
| 262 | |
| 263 | |
Tom Wai-Hong Tam | bea57b3 | 2011-09-02 18:27:47 +0800 | [diff] [blame] | 264 | def cleanup(self): |
| 265 | """Cleanup for the RPC server. Currently nothing.""" |
| 266 | pass |
| 267 | |
| 268 | |
Tom Wai-Hong Tam | b8a58ef | 2011-10-11 23:53:10 -0700 | [diff] [blame] | 269 | def main(): |
| 270 | parser = OptionParser(usage='Usage: %prog [options]') |
| 271 | parser.add_option('--port', type='int', dest='port', default=9990, |
| 272 | help='port number of XMLRPC server') |
| 273 | (options, args) = parser.parse_args() |
| 274 | |
| 275 | faft_client = FAFTClient() |
| 276 | |
| 277 | # Launch the XMLRPC server to provide FAFTClient commands. |
Tom Wai-Hong Tam | 4a257e5 | 2011-11-12 08:36:22 +0800 | [diff] [blame] | 278 | server = SimpleXMLRPCServer(('localhost', options.port), allow_none=True, |
| 279 | logRequests=False) |
Tom Wai-Hong Tam | b8a58ef | 2011-10-11 23:53:10 -0700 | [diff] [blame] | 280 | server.register_introspection_functions() |
| 281 | server.register_instance(faft_client) |
| 282 | print 'XMLRPC Server: Serving FAFTClient on port %s' % options.port |
| 283 | server.serve_forever() |
| 284 | |
| 285 | |
| 286 | if __name__ == '__main__': |
| 287 | main() |