blob: 33edd8c8fbaf61f1d63ce61d686bbfc4fd3720fe [file] [log] [blame]
Tom Wai-Hong Tamc1e0b9a2012-11-01 13:52:39 +08001#!/usr/bin/python -u
barfab@chromium.orgb6d29932012-04-11 09:46:43 +02002# Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Tom Wai-Hong Tamb8a58ef2011-10-11 23:53:10 -07003# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6"""Exposes the FAFTClient interface over XMLRPC.
7
8It launches a XMLRPC server and exposes the interface of FAFTClient object.
9The FAFTClient object aggreates some useful functions of exisintg SAFT
10libraries.
11"""
12
ctchang38ae4922012-09-03 17:01:16 +080013import functools, os, shutil, sys, tempfile
Tom Wai-Hong Tamb8a58ef2011-10-11 23:53:10 -070014from optparse import OptionParser
15from SimpleXMLRPCServer import SimpleXMLRPCServer
16
Tom Wai-Hong Tamc0168912012-09-13 13:24:02 +080017from saft import cgpt_state, chromeos_interface, flashrom_handler
18from saft import kernel_handler, saft_flashrom_util, tpm_handler
Tom Wai-Hong Tamb8a58ef2011-10-11 23:53:10 -070019
20
Tom Wai-Hong Tam0e680af2011-10-26 14:32:55 +080021def allow_multiple_section_input(image_operator):
22 @functools.wraps(image_operator)
23 def wrapper(self, section):
24 if type(section) in (tuple, list):
25 for sec in section:
26 image_operator(self, sec)
27 else:
28 image_operator(self, section)
29 return wrapper
30
31
Vic Yang13d22ec2012-06-18 15:12:47 +080032class LazyFlashromHandlerProxy:
33 _loaded = False
34 _obj = None
35
36 def __init__(self, *args, **kargs):
37 self._args = args
38 self._kargs = kargs
39
40 def _load(self):
41 self._obj = flashrom_handler.FlashromHandler()
42 self._obj.init(*self._args, **self._kargs)
43 self._obj.new_image()
44 self._loaded = True
45
46 def __getattr__(self, name):
47 if not self._loaded:
48 self._load()
49 return getattr(self._obj, name)
50
Tom Wai-Hong Tamc1c4deb2012-07-26 14:28:11 +080051 def reload(self):
52 self._loaded = False
53
Vic Yang13d22ec2012-06-18 15:12:47 +080054
Tom Wai-Hong Tamb8a58ef2011-10-11 23:53:10 -070055class FAFTClient(object):
56 """A class of FAFT client which aggregates some useful functions of SAFT.
57
58 This class can be exposed via a XMLRPC server such that its functions can
59 be accessed remotely.
60
61 Attributes:
62 _chromeos_interface: An object to encapsulate OS services functions.
Vic Yang59cac9c2012-05-21 15:28:42 +080063 _bios_handler: An object to automate BIOS flashrom testing.
64 _ec_handler: An object to automate EC flashrom testing.
Tom Wai-Hong Tam23870e02012-08-24 16:15:34 +080065 _ec_image: An object to automate EC image for autest.
Tom Wai-Hong Tamb8a58ef2011-10-11 23:53:10 -070066 _kernel_handler: An object to provide kernel related actions.
67 _tpm_handler: An object to control TPM device.
ctchangc7e55ea2012-08-09 16:19:14 +080068 _temp_path: Path of a temp directory.
69 _keys_path: Path of a directory, keys/, in temp directory.
70 _work_path: Path of a directory, work/, in temp directory.
Tom Wai-Hong Tamb8a58ef2011-10-11 23:53:10 -070071 """
72
73 def __init__(self):
74 """Initialize the data attributes of this class."""
Tom Wai-Hong Tamb8a58ef2011-10-11 23:53:10 -070075 # TODO(waihong): Move the explicit object.init() methods to the
76 # objects' constructors (ChromeOSInterface, FlashromHandler,
77 # KernelHandler, and TpmHandler).
78 self._chromeos_interface = chromeos_interface.ChromeOSInterface(False)
Tom Wai-Hong Tam48958832011-12-30 10:16:57 +080079 # We keep the state of FAFT test in a permanent directory over reboots.
Tom Wai-Hong Tam07278c22012-02-08 16:53:00 +080080 state_dir = '/var/tmp/faft'
81 self._chromeos_interface.init(state_dir, log_file='/tmp/faft_log.txt')
82 os.chdir(state_dir)
Tom Wai-Hong Tamb8a58ef2011-10-11 23:53:10 -070083
Vic Yang13d22ec2012-06-18 15:12:47 +080084 self._bios_handler = LazyFlashromHandlerProxy(
85 saft_flashrom_util,
Vic Yang59cac9c2012-05-21 15:28:42 +080086 self._chromeos_interface,
87 None,
88 '/usr/share/vboot/devkeys',
89 'bios')
Vic Yang59cac9c2012-05-21 15:28:42 +080090
Todd Brochf2b1d012012-06-14 12:55:21 -070091 self._ec_handler = None
92 if not os.system("mosys ec info"):
Vic Yang13d22ec2012-06-18 15:12:47 +080093 self._ec_handler = LazyFlashromHandlerProxy(
94 saft_flashrom_util,
Todd Brochf2b1d012012-06-14 12:55:21 -070095 self._chromeos_interface,
Vic Yang13d22ec2012-06-18 15:12:47 +080096 'ec_root_key.vpubk',
Todd Brochf2b1d012012-06-14 12:55:21 -070097 '/usr/share/vboot/devkeys',
98 'ec')
Todd Brochf2b1d012012-06-14 12:55:21 -070099
Tom Wai-Hong Tamb8a58ef2011-10-11 23:53:10 -0700100
101 self._kernel_handler = kernel_handler.KernelHandler()
Tom Wai-Hong Tam07278c22012-02-08 16:53:00 +0800102 # TODO(waihong): The dev_key_path is a new argument. We do that in
103 # order not to break the old image and still be able to run.
104 try:
105 self._kernel_handler.init(self._chromeos_interface,
ctchangd60030f2012-08-29 15:39:56 +0800106 dev_key_path='/usr/share/vboot/devkeys',
107 internal_disk=True)
Tom Wai-Hong Tam07278c22012-02-08 16:53:00 +0800108 except:
109 # Copy the key to the current working directory.
110 shutil.copy('/usr/share/vboot/devkeys/kernel_data_key.vbprivk', '.')
ctchangd60030f2012-08-29 15:39:56 +0800111 self._kernel_handler.init(self._chromeos_interface,
112 internal_disk=True)
Tom Wai-Hong Tamb8a58ef2011-10-11 23:53:10 -0700113
114 self._tpm_handler = tpm_handler.TpmHandler()
115 self._tpm_handler.init(self._chromeos_interface)
116
Tom Wai-Hong Tam48958832011-12-30 10:16:57 +0800117 self._cgpt_state = cgpt_state.CgptState(
Tom Wai-Hong Tamed2231c2012-07-27 14:39:46 +0800118 'SHORT', self._chromeos_interface, self.get_root_dev())
Tom Wai-Hong Tam48958832011-12-30 10:16:57 +0800119
ctchangc7e55ea2012-08-09 16:19:14 +0800120 # Initialize temporary directory path
121 self._temp_path = '/var/tmp/faft/autest'
122 self._keys_path = os.path.join(self._temp_path, 'keys')
123 self._work_path = os.path.join(self._temp_path, 'work')
124
Tom Wai-Hong Tamb8a58ef2011-10-11 23:53:10 -0700125
Tom Wai-Hong Tame8f291a2011-12-08 22:03:53 +0800126 def _dispatch(self, method, params):
127 """This _dispatch method handles string conversion especially.
128
129 Since we turn off allow_dotted_names option. So any string conversion,
130 like str(FAFTClient.method), i.e. FAFTClient.method.__str__, failed
131 via XML RPC call.
132 """
133 is_str = method.endswith('.__str__')
134 if is_str:
135 method = method.rsplit('.', 1)[0]
136 try:
137 func = getattr(self, method)
138 except AttributeError:
139 raise Exception('method "%s" is not supported' % method)
140 else:
141 if is_str:
142 return str(func)
143 else:
144 return func(*params)
145
146
Tom Wai-Hong Tambea57b32011-09-02 18:27:47 +0800147 def is_available(self):
148 """Function for polling the RPC server availability.
149
150 Returns:
151 Always True.
152 """
153 return True
154
155
Tom Wai-Hong Tamb8a58ef2011-10-11 23:53:10 -0700156 def run_shell_command(self, command):
157 """Run shell command.
158
159 Args:
160 command: A shell command to be run.
161 """
162 self._chromeos_interface.log('Requesting run shell command')
163 self._chromeos_interface.run_shell_command(command)
164
165
166 def run_shell_command_get_output(self, command):
167 """Run shell command and get its console output.
168
169 Args:
170 command: A shell command to be run.
171
172 Returns:
173 A list of strings stripped of the newline characters.
174 """
175 self._chromeos_interface.log(
176 'Requesting run shell command and get its console output')
177 return self._chromeos_interface.run_shell_command_get_output(command)
178
179
180 def software_reboot(self):
181 """Request software reboot."""
182 self._chromeos_interface.log('Requesting software reboot')
183 self._chromeos_interface.run_shell_command('reboot')
184
185
Tom Wai-Hong Tam678ab152011-12-14 15:27:24 +0800186 def get_platform_name(self):
187 """Get the platform name of the current system.
188
189 Returns:
190 A string of the platform name.
191 """
192 self._chromeos_interface.log('Requesting get platform name')
193 return self._chromeos_interface.run_shell_command_get_output(
194 'mosys platform name')[0]
195
196
Tom Wai-Hong Tamb8a58ef2011-10-11 23:53:10 -0700197 def get_crossystem_value(self, key):
198 """Get crossystem value of the requested key.
199
200 Args:
201 key: A crossystem key.
202
203 Returns:
204 A string of the requested crossystem value.
205 """
206 self._chromeos_interface.log('Requesting get crossystem value')
207 return self._chromeos_interface.run_shell_command_get_output(
208 'crossystem %s' % key)[0]
209
210
Tom Wai-Hong Tamcfda61f2011-11-02 17:41:01 +0800211 def get_root_dev(self):
212 """Get the name of root device without partition number.
213
214 Returns:
215 A string of the root device without partition number.
216 """
217 self._chromeos_interface.log('Requesting get root device')
218 return self._chromeos_interface.get_root_dev()
219
220
221 def get_root_part(self):
222 """Get the name of root device with partition number.
223
224 Returns:
225 A string of the root device with partition number.
226 """
227 self._chromeos_interface.log('Requesting get root part')
228 return self._chromeos_interface.get_root_part()
229
230
Tom Wai-Hong Tamb8a58ef2011-10-11 23:53:10 -0700231 def set_try_fw_b(self):
232 """Set 'Try Frimware B' flag in crossystem."""
233 self._chromeos_interface.log('Requesting restart with firmware B')
234 self._chromeos_interface.cs.fwb_tries = 1
235
236
Tom Wai-Hong Tam76c75072011-10-25 18:00:12 +0800237 def request_recovery_boot(self):
238 """Request running in recovery mode on the restart."""
239 self._chromeos_interface.log('Requesting restart in recovery mode')
240 self._chromeos_interface.cs.request_recovery()
241
242
Tom Wai-Hong Tam0a7b2be2012-10-15 16:44:12 +0800243 def get_dev_boot_usb(self):
244 """Get dev_boot_usb value which controls developer mode boot from USB.
245
246 Returns:
247 True if enable, False if disable.
248 """
249 self._chromeos_interface.log('Getting dev_boot_usb')
250 return self._chromeos_interface.cs.dev_boot_usb == '1'
251
252
253 def set_dev_boot_usb(self, value):
254 """Set dev_boot_usb value which controls developer mode boot from USB.
255
256 Args:
257 value: True to enable, False to disable.
258 """
259 self._chromeos_interface.log('Setting dev_boot_usb to %s' % str(value))
260 self._chromeos_interface.cs.dev_boot_usb = 1 if value else 0
261
262
Tom Wai-Hong Tam8c9eed62011-12-28 15:05:05 +0800263 def get_gbb_flags(self):
264 """Get the GBB flags.
265
266 Returns:
267 An integer of the GBB flags.
268 """
269 self._chromeos_interface.log('Getting GBB flags')
Vic Yang59cac9c2012-05-21 15:28:42 +0800270 return self._bios_handler.get_gbb_flags()
Tom Wai-Hong Tam8c9eed62011-12-28 15:05:05 +0800271
272
Tom Wai-Hong Tam81f70002011-12-13 12:29:46 +0800273 def get_firmware_flags(self, section):
274 """Get the preamble flags of a firmware section.
275
276 Args:
277 section: A firmware section, either 'a' or 'b'.
278
279 Returns:
280 An integer of the preamble flags.
281 """
282 self._chromeos_interface.log('Getting preamble flags of firmware %s' %
283 section)
Vic Yang59cac9c2012-05-21 15:28:42 +0800284 return self._bios_handler.get_section_flags(section)
285
286
Vic Yang91b73cf2012-07-31 17:18:11 +0800287 def set_firmware_flags(self, section, flags):
288 """Set the preamble flags of a firmware section.
289
290 Args:
291 section: A firmware section, either 'a' or 'b'.
292 flags: An integer of preamble flags.
293 """
294 self._chromeos_interface.log(
295 'Setting preamble flags of firmware %s to %s' % (section, flags))
296 version = self.get_firmware_version(section)
297 self._bios_handler.set_section_version(section, version, flags,
298 write_through=True)
299
300
Tom Wai-Hong Tam4e10b9f2012-09-06 16:23:02 +0800301 def get_firmware_sha(self, section):
302 """Get SHA1 hash of BIOS RW firmware section.
303
304 Args:
305 section: A firmware section, either 'a' or 'b'.
306 flags: An integer of preamble flags.
307 """
308 return self._bios_handler.get_section_sha(section)
309
310
ctchang38ae4922012-09-03 17:01:16 +0800311 def get_firmware_sig_sha(self, section):
312 """Get SHA1 hash of firmware vblock in section."""
313 return self._bios_handler.get_section_sig_sha(section)
314
315
Vic Yang91b73cf2012-07-31 17:18:11 +0800316 def get_EC_firmware_sha(self):
Tom Wai-Hong Tam4e10b9f2012-09-06 16:23:02 +0800317 """Get SHA1 hash of EC RW firmware section."""
Vic Yang91b73cf2012-07-31 17:18:11 +0800318 return self._ec_handler.get_section_sha('rw')
319
320
Tom Wai-Hong Tamc1c4deb2012-07-26 14:28:11 +0800321 def reload_firmware(self):
322 """Reload the firmware image that may be changed."""
323 self._bios_handler.reload()
324
325
Vic Yang59cac9c2012-05-21 15:28:42 +0800326 @allow_multiple_section_input
327 def corrupt_EC(self, section):
328 """Corrupt the requested EC section signature.
329
330 Args:
331 section: A EC section, either 'a' or 'b'.
332 """
333 self._chromeos_interface.log('Corrupting EC signature %s' %
334 section)
Vic Yang37a55462012-06-29 14:00:28 +0800335 self._ec_handler.corrupt_firmware(section, corrupt_all=True)
Vic Yang59cac9c2012-05-21 15:28:42 +0800336
337
338 @allow_multiple_section_input
339 def corrupt_EC_body(self, section):
340 """Corrupt the requested EC section body.
341
342 Args:
343 section: An EC section, either 'a' or 'b'.
344 """
345 self._chromeos_interface.log('Corrupting EC body %s' %
346 section)
Vic Yang37a55462012-06-29 14:00:28 +0800347 self._ec_handler.corrupt_firmware_body(section, corrupt_all=True)
Vic Yang59cac9c2012-05-21 15:28:42 +0800348
349
350 @allow_multiple_section_input
351 def restore_EC(self, section):
352 """Restore the previously corrupted EC section signature.
353
354 Args:
355 section: An EC section, either 'a' or 'b'.
356 """
357 self._chromeos_interface.log('Restoring EC signature %s' %
358 section)
Vic Yang37a55462012-06-29 14:00:28 +0800359 self._ec_handler.restore_firmware(section, restore_all=True)
Vic Yang59cac9c2012-05-21 15:28:42 +0800360
361
362 @allow_multiple_section_input
363 def restore_EC_body(self, section):
364 """Restore the previously corrupted EC section body.
365
366 Args:
367 section: An EC section, either 'a' or 'b'.
368 """
369 self._chromeos_interface.log('Restoring EC body %s' %
370 section)
Vic Yang37a55462012-06-29 14:00:28 +0800371 self._ec_handler.restore_firmware_body(section, restore_all=True)
Tom Wai-Hong Tam81f70002011-12-13 12:29:46 +0800372
373
Tom Wai-Hong Tam0e680af2011-10-26 14:32:55 +0800374 @allow_multiple_section_input
Tom Wai-Hong Tamb8a58ef2011-10-11 23:53:10 -0700375 def corrupt_firmware(self, section):
Tom Wai-Hong Tam9aea8212011-12-12 15:08:45 +0800376 """Corrupt the requested firmware section signature.
Tom Wai-Hong Tamb8a58ef2011-10-11 23:53:10 -0700377
378 Args:
379 section: A firmware section, either 'a' or 'b'.
380 """
Tom Wai-Hong Tam9aea8212011-12-12 15:08:45 +0800381 self._chromeos_interface.log('Corrupting firmware signature %s' %
382 section)
Vic Yang59cac9c2012-05-21 15:28:42 +0800383 self._bios_handler.corrupt_firmware(section)
Tom Wai-Hong Tamb8a58ef2011-10-11 23:53:10 -0700384
385
Tom Wai-Hong Tam0e680af2011-10-26 14:32:55 +0800386 @allow_multiple_section_input
Tom Wai-Hong Tam81f70002011-12-13 12:29:46 +0800387 def corrupt_firmware_body(self, section):
388 """Corrupt the requested firmware section body.
389
390 Args:
391 section: A firmware section, either 'a' or 'b'.
392 """
393 self._chromeos_interface.log('Corrupting firmware body %s' %
394 section)
Vic Yang59cac9c2012-05-21 15:28:42 +0800395 self._bios_handler.corrupt_firmware_body(section)
Tom Wai-Hong Tam81f70002011-12-13 12:29:46 +0800396
397
398 @allow_multiple_section_input
Tom Wai-Hong Tamb8a58ef2011-10-11 23:53:10 -0700399 def restore_firmware(self, section):
Tom Wai-Hong Tam9aea8212011-12-12 15:08:45 +0800400 """Restore the previously corrupted firmware section signature.
Tom Wai-Hong Tamb8a58ef2011-10-11 23:53:10 -0700401
402 Args:
403 section: A firmware section, either 'a' or 'b'.
404 """
Tom Wai-Hong Tam9aea8212011-12-12 15:08:45 +0800405 self._chromeos_interface.log('Restoring firmware signature %s' %
406 section)
Vic Yang59cac9c2012-05-21 15:28:42 +0800407 self._bios_handler.restore_firmware(section)
Tom Wai-Hong Tamb8a58ef2011-10-11 23:53:10 -0700408
409
Tom Wai-Hong Tamcfda61f2011-11-02 17:41:01 +0800410 @allow_multiple_section_input
Tom Wai-Hong Tam81f70002011-12-13 12:29:46 +0800411 def restore_firmware_body(self, section):
412 """Restore the previously corrupted firmware section body.
413
414 Args:
415 section: A firmware section, either 'a' or 'b'.
416 """
417 self._chromeos_interface.log('Restoring firmware body %s' %
418 section)
Vic Yang59cac9c2012-05-21 15:28:42 +0800419 self._bios_handler.restore_firmware_body(section)
Tom Wai-Hong Tam81f70002011-12-13 12:29:46 +0800420
421
Vic Yang91b73cf2012-07-31 17:18:11 +0800422 def get_firmware_version(self, section):
423 """Retrieve firmware version of a section."""
424 return self._bios_handler.get_section_version(section)
425
426
Chun-ting Changa4f65532012-10-17 16:57:28 +0800427 def get_tpm_firmware_version(self):
428 """Retrieve tpm firmware body version."""
429 return self._tpm_handler.get_fw_version()
430
431
Tom Wai-Hong Tam46d03b12012-02-08 12:02:17 +0800432 def _modify_firmware_version(self, section, delta):
433 """Modify firmware version for the requested section, by adding delta.
434
435 The passed in delta, a positive or a negative number, is added to the
436 original firmware version.
437 """
Vic Yang91b73cf2012-07-31 17:18:11 +0800438 original_version = self.get_firmware_version(section)
Tom Wai-Hong Tam46d03b12012-02-08 12:02:17 +0800439 new_version = original_version + delta
Vic Yang59cac9c2012-05-21 15:28:42 +0800440 flags = self._bios_handler.get_section_flags(section)
Tom Wai-Hong Tam46d03b12012-02-08 12:02:17 +0800441 self._chromeos_interface.log(
442 'Setting firmware section %s version from %d to %d' % (
443 section, original_version, new_version))
Vic Yang59cac9c2012-05-21 15:28:42 +0800444 self._bios_handler.set_section_version(section, new_version, flags,
445 write_through=True)
Tom Wai-Hong Tam46d03b12012-02-08 12:02:17 +0800446
447 @allow_multiple_section_input
448 def move_firmware_backward(self, section):
449 """Decrement firmware version for the requested section."""
450 self._modify_firmware_version(section, -1)
451
452
453 @allow_multiple_section_input
454 def move_firmware_forward(self, section):
455 """Increase firmware version for the requested section."""
456 self._modify_firmware_version(section, 1)
457
Chun-ting Changa4f65532012-10-17 16:57:28 +0800458 def get_firmware_datakey_version(self, section):
ctchangc7e55ea2012-08-09 16:19:14 +0800459 """Return firmware data key version."""
460 return self._bios_handler.get_section_datakey_version(section)
461
Chun-ting Changa4f65532012-10-17 16:57:28 +0800462 def get_tpm_firmware_datakey_version(self):
463 """Retrieve tpm firmware data key version."""
464 return self._tpm_handler.get_fw_body_version()
465
ctchangc7e55ea2012-08-09 16:19:14 +0800466 def retrieve_kernel_subkey_version(self,section):
467 """Return kernel subkey version."""
468 return self._bios_handler.get_section_kernel_subkey_version(section)
Tom Wai-Hong Tam46d03b12012-02-08 12:02:17 +0800469
Tom Wai-Hong Tam81f70002011-12-13 12:29:46 +0800470 @allow_multiple_section_input
Tom Wai-Hong Tamcfda61f2011-11-02 17:41:01 +0800471 def corrupt_kernel(self, section):
472 """Corrupt the requested kernel section.
473
474 Args:
475 section: A kernel section, either 'a' or 'b'.
476 """
477 self._chromeos_interface.log('Corrupting kernel %s' % section)
478 self._kernel_handler.corrupt_kernel(section)
479
480
481 @allow_multiple_section_input
482 def restore_kernel(self, section):
483 """Restore the requested kernel section (previously corrupted).
484
485 Args:
486 section: A kernel section, either 'a' or 'b'.
487 """
Tom Wai-Hong Tamcfda61f2011-11-02 17:41:01 +0800488 self._kernel_handler.restore_kernel(section)
489
490
Tom Wai-Hong Tam07278c22012-02-08 16:53:00 +0800491 def _modify_kernel_version(self, section, delta):
492 """Modify kernel version for the requested section, by adding delta.
493
494 The passed in delta, a positive or a negative number, is added to the
495 original kernel version.
496 """
497 original_version = self._kernel_handler.get_version(section)
498 new_version = original_version + delta
499 self._chromeos_interface.log(
500 'Setting kernel section %s version from %d to %d' % (
501 section, original_version, new_version))
502 self._kernel_handler.set_version(section, new_version)
503
504
505 @allow_multiple_section_input
506 def move_kernel_backward(self, section):
507 """Decrement kernel version for the requested section."""
508 self._modify_kernel_version(section, -1)
509
510
511 @allow_multiple_section_input
512 def move_kernel_forward(self, section):
513 """Increase kernel version for the requested section."""
514 self._modify_kernel_version(section, 1)
515
ctchangcc88d112012-08-23 17:56:15 +0800516
517 def retrieve_kernel_version(self, section):
518 """Return kernel version."""
519 return self._kernel_handler.get_version(section)
520
521
522 def retrieve_kernel_datakey_version(self, section):
523 """Return kernel datakey version."""
524 return self._kernel_handler.get_datakey_version(section)
525
526
Tom Wai-Hong Tam622d0ba2012-08-15 16:29:05 +0800527 def diff_kernel_a_b(self):
528 """Compare kernel A with B.
529
530 Returns:
531 True: if kernel A is different with B.
532 False: if kernel A is the same as B.
533 """
534 rootdev = self._chromeos_interface.get_root_dev()
535 kernel_a = self._chromeos_interface.join_part(rootdev, '3')
536 kernel_b = self._chromeos_interface.join_part(rootdev, '5')
537
538 # The signature (some kind of hash) for the kernel body is stored in
539 # the beginning. So compare the first 64KB (including header, preamble,
540 # and signature) should be enough to check them identical.
541 header_a = self._chromeos_interface.read_partition(kernel_a, 0x10000)
542 header_b = self._chromeos_interface.read_partition(kernel_b, 0x10000)
543
544 return header_a != header_b
Tom Wai-Hong Tam07278c22012-02-08 16:53:00 +0800545
Tom Wai-Hong Tam23870e02012-08-24 16:15:34 +0800546
Tom Wai-Hong Tam0a7b2be2012-10-15 16:44:12 +0800547 def is_removable_device_boot(self):
548 """Check the current boot device is removable.
549
550 Returns:
551 True: if a removable device boots.
552 False: if a non-removable device boots.
553 """
554 root_part = self._chromeos_interface.get_root_part()
555 return self._chromeos_interface.is_removable_device(root_part)
556
557
Tom Wai-Hong Tam23870e02012-08-24 16:15:34 +0800558 def setup_EC_image(self, ec_path):
559 """Setup the new EC image for later update.
560
561 Args:
562 ec_path: The path of the EC image to be updated.
563 """
564 self._ec_image = flashrom_handler.FlashromHandler()
565 self._ec_image.init(saft_flashrom_util,
566 self._chromeos_interface,
567 'ec_root_key.vpubk',
568 '/usr/share/vboot/devkeys',
569 'ec')
570 self._ec_image.new_image(ec_path)
571
572
573 def get_EC_image_sha(self):
574 """Get SHA1 hash of RW firmware section of the EC autest image."""
575 return self._ec_image.get_section_sha('rw')
576
577
578 def update_EC_from_image(self, section, flags):
579 """Update EC via software sync design.
580
581 It copys the RW section from the EC image, which is loaded by calling
582 setup_EC_image(), to the EC area of the specified RW section on the
583 current AP firmware.
584
585 Args:
586 section: A firmware section on current BIOS, either 'a' or 'b'.
587 flags: An integer of preamble flags.
588 """
589 blob = self._ec_image.get_section_body('rw')
590 self._bios_handler.set_section_ecbin(section, blob,
591 write_through=True)
592 self.set_firmware_flags(section, flags)
593
594
Tom Wai-Hong Tamb63bc742012-08-30 20:41:30 +0800595 def dump_firmware(self, bios_path):
596 """Dump the current BIOS firmware to a file, specified by bios_path.
597
598 Args:
599 bios_path: The path of the BIOS image to be written.
600 """
601 self._bios_handler.dump_whole(bios_path)
602
603
ctchang38ae4922012-09-03 17:01:16 +0800604 def dump_firmware_rw(self, dir_path):
605 """Dump the current BIOS firmware RW to dir_path.
606
607 VBOOTA, VBOOTB, FVMAIN, FVMAINB need to be dumped.
608
609 Args:
610 dir_path: The path of directory which contains files to be written.
611 """
612 if not os.path.isdir(dir_path):
613 raise Exception("%s doesn't exist" % dir_path)
614
615 VBOOTA_blob = self._bios_handler.get_section_sig('a')
616 VBOOTB_blob = self._bios_handler.get_section_sig('b')
617 FVMAIN_blob = self._bios_handler.get_section_body('a')
618 FVMAINB_blob = self._bios_handler.get_section_body('b')
619
620 open(os.path.join(dir_path, 'VBOOTA'), 'w').write(VBOOTA_blob)
621 open(os.path.join(dir_path, 'VBOOTB'), 'w').write(VBOOTB_blob)
622 open(os.path.join(dir_path, 'FVMAIN'), 'w').write(FVMAIN_blob)
623 open(os.path.join(dir_path, 'FVMAINB'), 'w').write(FVMAINB_blob)
624
625
Tom Wai-Hong Tamb63bc742012-08-30 20:41:30 +0800626 def write_firmware(self, bios_path):
627 """Write the firmware from bios_path to the current system.
628
629 Args:
630 bios_path: The path of the source BIOS image.
631 """
632 self._bios_handler.new_image(bios_path)
633 self._bios_handler.write_whole()
634
635
ctchang38ae4922012-09-03 17:01:16 +0800636 def write_firmware_rw(self, dir_path):
637 """Write the firmware RW from dir_path to the current system.
638
639 VBOOTA, VBOOTB, FVMAIN, FVMAINB need to be written.
640
641 Args:
642 dir_path: The path of directory which contains the source files.
643 """
644 if not os.path.exists(os.path.join(dir_path, 'VBOOTA')) or \
645 not os.path.exists(os.path.join(dir_path, 'VBOOTB')) or \
646 not os.path.exists(os.path.join(dir_path, 'FVMAIN')) or \
647 not os.path.exists(os.path.join(dir_path, 'FVMAINB')):
648 raise Exception("Source firmware file(s) doesn't exist.")
649
650 VBOOTA_blob = open(os.path.join(dir_path, 'VBOOTA'), 'rb').read()
651 VBOOTB_blob = open(os.path.join(dir_path, 'VBOOTB'), 'rb').read()
652 FVMAIN_blob = open(os.path.join(dir_path, 'FVMAIN'), 'rb').read()
653 FVMAINB_blob = open(os.path.join(dir_path, 'FVMAINB'), 'rb').read()
654
655 self._bios_handler.set_section_sig('a', VBOOTA_blob,
656 write_through=True)
657 self._bios_handler.set_section_sig('b', VBOOTB_blob,
658 write_through=True)
659 self._bios_handler.set_section_body('a', FVMAIN_blob,
660 write_through=True)
661 self._bios_handler.set_section_body('b', FVMAINB_blob,
662 write_through=True)
663
664
Tom Wai-Hong Tam23870e02012-08-24 16:15:34 +0800665 def dump_EC_firmware(self, ec_path):
666 """Dump the current EC firmware to a file, specified by ec_path.
667
668 Args:
669 ec_path: The path of the EC image to be written.
670 """
671 self._ec_handler.dump_whole(ec_path)
672
673
Tom Wai-Hong Tam48958832011-12-30 10:16:57 +0800674 def run_cgpt_test_loop(self):
675 """Run the CgptState test loop. The tst logic is handled in the client.
676
677 Returns:
678 0: there are more cgpt tests to execute.
679 1: no more CgptState test, finished.
680 """
681 return self._cgpt_state.test_loop()
682
683
684 def set_cgpt_test_step(self, step):
685 """Set the CgptState test step.
686
687 Args:
688 step: A test step number.
689 """
690 self._cgpt_state.set_step(step)
691
692
693 def get_cgpt_test_step(self):
694 """Get the CgptState test step.
695
696 Returns:
697 A test step number.
698 """
699 return self._cgpt_state.get_step()
700
701
Chun-ting Changf91ee0f2012-09-17 18:31:54 +0800702 def setup_firmwareupdate_temp_dir(self, shellball=None):
ctchangc7e55ea2012-08-09 16:19:14 +0800703 """Setup temporary directory.
704
Chun-ting Changf91ee0f2012-09-17 18:31:54 +0800705 Devkeys are copied to _key_path. Then, shellball (default:
706 /usr/sbin/chromeos-firmwareupdate) is extracted to _work_path.
707
708 Args:
709 shellball: Path of shellball.
ctchangc7e55ea2012-08-09 16:19:14 +0800710 """
ctchang6b700df2012-08-20 13:48:07 +0800711
712 self.cleanup_firmwareupdate_temp_dir()
713
ctchangc7e55ea2012-08-09 16:19:14 +0800714 os.mkdir(self._temp_path)
715 os.chdir(self._temp_path)
716
717 os.mkdir(self._work_path)
718 shutil.copytree('/usr/share/vboot/devkeys/', self._keys_path)
Chun-ting Changf91ee0f2012-09-17 18:31:54 +0800719
720 shellball_path = os.path.join(self._temp_path,
721 'chromeos-firmwareupdate')
722
723 if shellball:
724 shutil.copyfile(shellball, shellball_path)
725 else:
726 shutil.copyfile('/usr/sbin/chromeos-firmwareupdate',
727 shellball_path)
ctchangc7e55ea2012-08-09 16:19:14 +0800728 self.run_shell_command(
Chun-ting Changf91ee0f2012-09-17 18:31:54 +0800729 'sh %s --sb_extract %s' % (shellball_path, self._work_path))
ctchangc7e55ea2012-08-09 16:19:14 +0800730
731
732 def retrieve_shellball_fwid(self):
733 """Retrieve shellball's fwid.
734
735 This method should be called after setup_firmwareupdate_temp_dir.
736
737 Returns:
738 Shellball's fwid.
739 """
740 self.run_shell_command('dump_fmap -x %s %s' %
741 (os.path.join(self._work_path, 'bios.bin'),
742 'RW_FWID_A'))
743
744 [fwid] = self.run_shell_command_get_output(
ctchangcc88d112012-08-23 17:56:15 +0800745 "cat RW_FWID_A | tr '\\0' '\\t' | cut -f1")
ctchangc7e55ea2012-08-09 16:19:14 +0800746
747 return fwid
748
749
750 def cleanup_firmwareupdate_temp_dir(self):
751 """Cleanup temporary directory."""
ctchang6b700df2012-08-20 13:48:07 +0800752 if os.path.isdir(self._temp_path):
753 shutil.rmtree(self._temp_path)
ctchangc7e55ea2012-08-09 16:19:14 +0800754
755
756 def repack_firmwareupdate_shellball(self, append):
757 """Repack shellball with new fwid.
758
759 New fwid follows the rule: [orignal_fwid]-[append].
760
761 Args:
762 append: use for new fwid naming.
763 """
764 shutil.copy('/usr/sbin/chromeos-firmwareupdate', '%s' %
765 os.path.join(self._temp_path,
766 'chromeos-firmwareupdate-%s' % append))
767
ctchang6b700df2012-08-20 13:48:07 +0800768 self.run_shell_command('sh %s --sb_repack %s' % (
769 os.path.join(self._temp_path,
770 'chromeos-firmwareupdate-%s' % append),
771 self._work_path))
ctchangc7e55ea2012-08-09 16:19:14 +0800772
773 args = ['-i']
774 args.append('"s/TARGET_FWID=\\"\\(.*\\)\\"/TARGET_FWID=\\"\\1.%s\\"/g"'
775 % append)
776 args.append('%s'
777 % os.path.join(self._temp_path,
778 'chromeos-firmwareupdate-%s' % append))
779 cmd = 'sed %s' % ' '.join(args)
780 self.run_shell_command(cmd)
781
782 args = ['-i']
783 args.append('"s/TARGET_UNSTABLE=\\".*\\"/TARGET_UNSTABLE=\\"\\"/g"')
784 args.append('%s'
785 % os.path.join(self._temp_path,
786 'chromeos-firmwareupdate-%s' % append))
787 cmd = 'sed %s' % ' '.join(args)
788 self.run_shell_command(cmd)
789
790
791 def resign_firmware(self, version):
792 """Resign firmware with version.
793
794 Args:
795 version: new firmware version number.
796 """
797 args = [os.path.join(self._work_path, 'bios.bin')]
798 args.append(os.path.join(self._temp_path, 'output.bin'))
799 args.append(os.path.join(self._keys_path, 'firmware_data_key.vbprivk'))
800 args.append(os.path.join(self._keys_path, 'firmware.keyblock'))
801 args.append(os.path.join(self._keys_path,
802 'dev_firmware_data_key.vbprivk'))
803 args.append(os.path.join(self._keys_path, 'dev_firmware.keyblock'))
804 args.append(os.path.join(self._keys_path, 'kernel_subkey.vbpubk'))
805 args.append('%d' % version)
806 args.append('1')
807 cmd = '/usr/share/vboot/bin/resign_firmwarefd.sh %s' % ' '.join(args)
808 self.run_shell_command(cmd)
809
810 shutil.copyfile('%s' % os.path.join(self._temp_path, 'output.bin'),
811 '%s' % os.path.join(self._work_path, 'bios.bin'))
812
813
814 def run_firmware_autoupdate(self, append):
815 """Do firmwareupdate with autoupdate mode using new shellball.
816
817 Args:
818 append: decide which shellball to use with format
819 chromeos-firmwareupdate-[append]
820 """
821 self.run_shell_command(
Chun-ting Changa4f65532012-10-17 16:57:28 +0800822 '/bin/sh %s --mode autoupdate '
823 '--noupdate_ec --nocheck_rw_compatible'
824 % os.path.join(self._temp_path,
ctchangc7e55ea2012-08-09 16:19:14 +0800825 'chromeos-firmwareupdate-%s' % append))
826
827
Chun-ting Changf91ee0f2012-09-17 18:31:54 +0800828 def run_firmware_factory_install(self):
829 """ Do firmwareupdate with factory_install mode using new shellball."""
830 self.run_shell_command(
831 '/bin/sh %s --mode factory_install --noupdate_ec'
832 % os.path.join(self._temp_path, 'chromeos-firmwareupdate'))
833
834
ctchangc7e55ea2012-08-09 16:19:14 +0800835 def run_firmware_bootok(self, append):
836 """Do bootok mode using new shellball.
837
838 Copy firmware B to firmware A if reboot success.
839 """
840 self.run_shell_command(
841 '/bin/sh %s --mode bootok' % os.path.join(self._temp_path,
842 'chromeos-firmwareupdate-%s' % append))
843
844
845 def run_firmware_recovery(self):
846 """Recovery to original shellball."""
Chun-ting Changf91ee0f2012-09-17 18:31:54 +0800847 self.run_shell_command(
Chun-ting Changa4f65532012-10-17 16:57:28 +0800848 '/bin/sh %s --mode recovery --noupdate_ec --nocheck_rw_compatible'
849 % os.path.join(self._temp_path, 'chromeos-firmwareupdate'))
ctchangc7e55ea2012-08-09 16:19:14 +0800850
851
852 def get_temp_path(self):
853 """Get temporary directory path."""
854 return self._temp_path
855
856
ctchangcc88d112012-08-23 17:56:15 +0800857 def get_keys_path(self):
858 """Get keys path in temporary directory."""
859 return self._keys_path
860
861
862 def resign_kernel_with_keys(self, section, key_path=None):
863 """Resign kernel with temporary key."""
864 self._kernel_handler.resign_kernel(section, key_path)
865
866
ctchang38ae4922012-09-03 17:01:16 +0800867 def create_temp_dir(self, prefix='backup_'):
868 """Create a temporary directory and return the path."""
869 return tempfile.mkdtemp(prefix=prefix)
870
871
Tom Wai-Hong Tambea57b32011-09-02 18:27:47 +0800872 def cleanup(self):
873 """Cleanup for the RPC server. Currently nothing."""
874 pass
875
876
Tom Wai-Hong Tamb8a58ef2011-10-11 23:53:10 -0700877def main():
878 parser = OptionParser(usage='Usage: %prog [options]')
879 parser.add_option('--port', type='int', dest='port', default=9990,
880 help='port number of XMLRPC server')
881 (options, args) = parser.parse_args()
882
883 faft_client = FAFTClient()
884
885 # Launch the XMLRPC server to provide FAFTClient commands.
Tom Wai-Hong Tam4a257e52011-11-12 08:36:22 +0800886 server = SimpleXMLRPCServer(('localhost', options.port), allow_none=True,
Tom Wai-Hong Tamc1e0b9a2012-11-01 13:52:39 +0800887 logRequests=True)
Tom Wai-Hong Tamb8a58ef2011-10-11 23:53:10 -0700888 server.register_introspection_functions()
889 server.register_instance(faft_client)
890 print 'XMLRPC Server: Serving FAFTClient on port %s' % options.port
891 server.serve_forever()
892
893
894if __name__ == '__main__':
895 main()