blob: 516d667635d6d27fc89d5e9479f85559bdeee449 [file] [log] [blame]
barfab@chromium.orgb6d29932012-04-11 09:46:43 +02001# Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Tom Wai-Hong Tamb8a58ef2011-10-11 23:53:10 -07002# 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
ctchang38ae4922012-09-03 17:01:16 +080012import functools, os, shutil, sys, tempfile
Tom Wai-Hong Tamb8a58ef2011-10-11 23:53:10 -070013from optparse import OptionParser
14from SimpleXMLRPCServer import SimpleXMLRPCServer
15
Tom Wai-Hong Tamc0168912012-09-13 13:24:02 +080016from saft import cgpt_state, chromeos_interface, flashrom_handler
17from saft import kernel_handler, saft_flashrom_util, tpm_handler
Tom Wai-Hong Tamb8a58ef2011-10-11 23:53:10 -070018
19
Tom Wai-Hong Tam0e680af2011-10-26 14:32:55 +080020def allow_multiple_section_input(image_operator):
21 @functools.wraps(image_operator)
22 def wrapper(self, section):
23 if type(section) in (tuple, list):
24 for sec in section:
25 image_operator(self, sec)
26 else:
27 image_operator(self, section)
28 return wrapper
29
30
Vic Yang13d22ec2012-06-18 15:12:47 +080031class LazyFlashromHandlerProxy:
32 _loaded = False
33 _obj = None
34
35 def __init__(self, *args, **kargs):
36 self._args = args
37 self._kargs = kargs
38
39 def _load(self):
40 self._obj = flashrom_handler.FlashromHandler()
41 self._obj.init(*self._args, **self._kargs)
42 self._obj.new_image()
43 self._loaded = True
44
45 def __getattr__(self, name):
46 if not self._loaded:
47 self._load()
48 return getattr(self._obj, name)
49
Tom Wai-Hong Tamc1c4deb2012-07-26 14:28:11 +080050 def reload(self):
51 self._loaded = False
52
Vic Yang13d22ec2012-06-18 15:12:47 +080053
Tom Wai-Hong Tamb8a58ef2011-10-11 23:53:10 -070054class FAFTClient(object):
55 """A class of FAFT client which aggregates some useful functions of SAFT.
56
57 This class can be exposed via a XMLRPC server such that its functions can
58 be accessed remotely.
59
60 Attributes:
61 _chromeos_interface: An object to encapsulate OS services functions.
Vic Yang59cac9c2012-05-21 15:28:42 +080062 _bios_handler: An object to automate BIOS flashrom testing.
63 _ec_handler: An object to automate EC flashrom testing.
Tom Wai-Hong Tam23870e02012-08-24 16:15:34 +080064 _ec_image: An object to automate EC image for autest.
Tom Wai-Hong Tamb8a58ef2011-10-11 23:53:10 -070065 _kernel_handler: An object to provide kernel related actions.
66 _tpm_handler: An object to control TPM device.
ctchangc7e55ea2012-08-09 16:19:14 +080067 _temp_path: Path of a temp directory.
68 _keys_path: Path of a directory, keys/, in temp directory.
69 _work_path: Path of a directory, work/, in temp directory.
Tom Wai-Hong Tamb8a58ef2011-10-11 23:53:10 -070070 """
71
72 def __init__(self):
73 """Initialize the data attributes of this class."""
Tom Wai-Hong Tamb8a58ef2011-10-11 23:53:10 -070074 # TODO(waihong): Move the explicit object.init() methods to the
75 # objects' constructors (ChromeOSInterface, FlashromHandler,
76 # KernelHandler, and TpmHandler).
77 self._chromeos_interface = chromeos_interface.ChromeOSInterface(False)
Tom Wai-Hong Tam48958832011-12-30 10:16:57 +080078 # We keep the state of FAFT test in a permanent directory over reboots.
Tom Wai-Hong Tam07278c22012-02-08 16:53:00 +080079 state_dir = '/var/tmp/faft'
80 self._chromeos_interface.init(state_dir, log_file='/tmp/faft_log.txt')
81 os.chdir(state_dir)
Tom Wai-Hong Tamb8a58ef2011-10-11 23:53:10 -070082
Vic Yang13d22ec2012-06-18 15:12:47 +080083 self._bios_handler = LazyFlashromHandlerProxy(
84 saft_flashrom_util,
Vic Yang59cac9c2012-05-21 15:28:42 +080085 self._chromeos_interface,
86 None,
87 '/usr/share/vboot/devkeys',
88 'bios')
Vic Yang59cac9c2012-05-21 15:28:42 +080089
Todd Brochf2b1d012012-06-14 12:55:21 -070090 self._ec_handler = None
91 if not os.system("mosys ec info"):
Vic Yang13d22ec2012-06-18 15:12:47 +080092 self._ec_handler = LazyFlashromHandlerProxy(
93 saft_flashrom_util,
Todd Brochf2b1d012012-06-14 12:55:21 -070094 self._chromeos_interface,
Vic Yang13d22ec2012-06-18 15:12:47 +080095 'ec_root_key.vpubk',
Todd Brochf2b1d012012-06-14 12:55:21 -070096 '/usr/share/vboot/devkeys',
97 'ec')
Todd Brochf2b1d012012-06-14 12:55:21 -070098
Tom Wai-Hong Tamb8a58ef2011-10-11 23:53:10 -070099
100 self._kernel_handler = kernel_handler.KernelHandler()
Tom Wai-Hong Tam07278c22012-02-08 16:53:00 +0800101 # TODO(waihong): The dev_key_path is a new argument. We do that in
102 # order not to break the old image and still be able to run.
103 try:
104 self._kernel_handler.init(self._chromeos_interface,
ctchangd60030f2012-08-29 15:39:56 +0800105 dev_key_path='/usr/share/vboot/devkeys',
106 internal_disk=True)
Tom Wai-Hong Tam07278c22012-02-08 16:53:00 +0800107 except:
108 # Copy the key to the current working directory.
109 shutil.copy('/usr/share/vboot/devkeys/kernel_data_key.vbprivk', '.')
ctchangd60030f2012-08-29 15:39:56 +0800110 self._kernel_handler.init(self._chromeos_interface,
111 internal_disk=True)
Tom Wai-Hong Tamb8a58ef2011-10-11 23:53:10 -0700112
113 self._tpm_handler = tpm_handler.TpmHandler()
114 self._tpm_handler.init(self._chromeos_interface)
115
Tom Wai-Hong Tam48958832011-12-30 10:16:57 +0800116 self._cgpt_state = cgpt_state.CgptState(
Tom Wai-Hong Tamed2231c2012-07-27 14:39:46 +0800117 'SHORT', self._chromeos_interface, self.get_root_dev())
Tom Wai-Hong Tam48958832011-12-30 10:16:57 +0800118
ctchangc7e55ea2012-08-09 16:19:14 +0800119 # Initialize temporary directory path
120 self._temp_path = '/var/tmp/faft/autest'
121 self._keys_path = os.path.join(self._temp_path, 'keys')
122 self._work_path = os.path.join(self._temp_path, 'work')
123
Tom Wai-Hong Tamb8a58ef2011-10-11 23:53:10 -0700124
Tom Wai-Hong Tame8f291a2011-12-08 22:03:53 +0800125 def _dispatch(self, method, params):
126 """This _dispatch method handles string conversion especially.
127
128 Since we turn off allow_dotted_names option. So any string conversion,
129 like str(FAFTClient.method), i.e. FAFTClient.method.__str__, failed
130 via XML RPC call.
131 """
132 is_str = method.endswith('.__str__')
133 if is_str:
134 method = method.rsplit('.', 1)[0]
135 try:
136 func = getattr(self, method)
137 except AttributeError:
138 raise Exception('method "%s" is not supported' % method)
139 else:
140 if is_str:
141 return str(func)
142 else:
143 return func(*params)
144
145
Tom Wai-Hong Tambea57b32011-09-02 18:27:47 +0800146 def is_available(self):
147 """Function for polling the RPC server availability.
148
149 Returns:
150 Always True.
151 """
152 return True
153
154
Tom Wai-Hong Tamb8a58ef2011-10-11 23:53:10 -0700155 def run_shell_command(self, command):
156 """Run shell command.
157
158 Args:
159 command: A shell command to be run.
160 """
161 self._chromeos_interface.log('Requesting run shell command')
162 self._chromeos_interface.run_shell_command(command)
163
164
165 def run_shell_command_get_output(self, command):
166 """Run shell command and get its console output.
167
168 Args:
169 command: A shell command to be run.
170
171 Returns:
172 A list of strings stripped of the newline characters.
173 """
174 self._chromeos_interface.log(
175 'Requesting run shell command and get its console output')
176 return self._chromeos_interface.run_shell_command_get_output(command)
177
178
179 def software_reboot(self):
180 """Request software reboot."""
181 self._chromeos_interface.log('Requesting software reboot')
182 self._chromeos_interface.run_shell_command('reboot')
183
184
Tom Wai-Hong Tam678ab152011-12-14 15:27:24 +0800185 def get_platform_name(self):
186 """Get the platform name of the current system.
187
188 Returns:
189 A string of the platform name.
190 """
191 self._chromeos_interface.log('Requesting get platform name')
192 return self._chromeos_interface.run_shell_command_get_output(
193 'mosys platform name')[0]
194
195
Tom Wai-Hong Tamb8a58ef2011-10-11 23:53:10 -0700196 def get_crossystem_value(self, key):
197 """Get crossystem value of the requested key.
198
199 Args:
200 key: A crossystem key.
201
202 Returns:
203 A string of the requested crossystem value.
204 """
205 self._chromeos_interface.log('Requesting get crossystem value')
206 return self._chromeos_interface.run_shell_command_get_output(
207 'crossystem %s' % key)[0]
208
209
Tom Wai-Hong Tamcfda61f2011-11-02 17:41:01 +0800210 def get_root_dev(self):
211 """Get the name of root device without partition number.
212
213 Returns:
214 A string of the root device without partition number.
215 """
216 self._chromeos_interface.log('Requesting get root device')
217 return self._chromeos_interface.get_root_dev()
218
219
220 def get_root_part(self):
221 """Get the name of root device with partition number.
222
223 Returns:
224 A string of the root device with partition number.
225 """
226 self._chromeos_interface.log('Requesting get root part')
227 return self._chromeos_interface.get_root_part()
228
229
Tom Wai-Hong Tamb8a58ef2011-10-11 23:53:10 -0700230 def set_try_fw_b(self):
231 """Set 'Try Frimware B' flag in crossystem."""
232 self._chromeos_interface.log('Requesting restart with firmware B')
233 self._chromeos_interface.cs.fwb_tries = 1
234
235
Tom Wai-Hong Tam76c75072011-10-25 18:00:12 +0800236 def request_recovery_boot(self):
237 """Request running in recovery mode on the restart."""
238 self._chromeos_interface.log('Requesting restart in recovery mode')
239 self._chromeos_interface.cs.request_recovery()
240
241
Tom Wai-Hong Tam8c9eed62011-12-28 15:05:05 +0800242 def get_gbb_flags(self):
243 """Get the GBB flags.
244
245 Returns:
246 An integer of the GBB flags.
247 """
248 self._chromeos_interface.log('Getting GBB flags')
Vic Yang59cac9c2012-05-21 15:28:42 +0800249 return self._bios_handler.get_gbb_flags()
Tom Wai-Hong Tam8c9eed62011-12-28 15:05:05 +0800250
251
Tom Wai-Hong Tam81f70002011-12-13 12:29:46 +0800252 def get_firmware_flags(self, section):
253 """Get the preamble flags of a firmware section.
254
255 Args:
256 section: A firmware section, either 'a' or 'b'.
257
258 Returns:
259 An integer of the preamble flags.
260 """
261 self._chromeos_interface.log('Getting preamble flags of firmware %s' %
262 section)
Vic Yang59cac9c2012-05-21 15:28:42 +0800263 return self._bios_handler.get_section_flags(section)
264
265
Vic Yang91b73cf2012-07-31 17:18:11 +0800266 def set_firmware_flags(self, section, flags):
267 """Set the preamble flags of a firmware section.
268
269 Args:
270 section: A firmware section, either 'a' or 'b'.
271 flags: An integer of preamble flags.
272 """
273 self._chromeos_interface.log(
274 'Setting preamble flags of firmware %s to %s' % (section, flags))
275 version = self.get_firmware_version(section)
276 self._bios_handler.set_section_version(section, version, flags,
277 write_through=True)
278
279
Tom Wai-Hong Tam4e10b9f2012-09-06 16:23:02 +0800280 def get_firmware_sha(self, section):
281 """Get SHA1 hash of BIOS RW firmware section.
282
283 Args:
284 section: A firmware section, either 'a' or 'b'.
285 flags: An integer of preamble flags.
286 """
287 return self._bios_handler.get_section_sha(section)
288
289
ctchang38ae4922012-09-03 17:01:16 +0800290 def get_firmware_sig_sha(self, section):
291 """Get SHA1 hash of firmware vblock in section."""
292 return self._bios_handler.get_section_sig_sha(section)
293
294
Vic Yang91b73cf2012-07-31 17:18:11 +0800295 def get_EC_firmware_sha(self):
Tom Wai-Hong Tam4e10b9f2012-09-06 16:23:02 +0800296 """Get SHA1 hash of EC RW firmware section."""
Vic Yang91b73cf2012-07-31 17:18:11 +0800297 return self._ec_handler.get_section_sha('rw')
298
299
Tom Wai-Hong Tamc1c4deb2012-07-26 14:28:11 +0800300 def reload_firmware(self):
301 """Reload the firmware image that may be changed."""
302 self._bios_handler.reload()
303
304
Vic Yang59cac9c2012-05-21 15:28:42 +0800305 @allow_multiple_section_input
306 def corrupt_EC(self, section):
307 """Corrupt the requested EC section signature.
308
309 Args:
310 section: A EC section, either 'a' or 'b'.
311 """
312 self._chromeos_interface.log('Corrupting EC signature %s' %
313 section)
Vic Yang37a55462012-06-29 14:00:28 +0800314 self._ec_handler.corrupt_firmware(section, corrupt_all=True)
Vic Yang59cac9c2012-05-21 15:28:42 +0800315
316
317 @allow_multiple_section_input
318 def corrupt_EC_body(self, section):
319 """Corrupt the requested EC section body.
320
321 Args:
322 section: An EC section, either 'a' or 'b'.
323 """
324 self._chromeos_interface.log('Corrupting EC body %s' %
325 section)
Vic Yang37a55462012-06-29 14:00:28 +0800326 self._ec_handler.corrupt_firmware_body(section, corrupt_all=True)
Vic Yang59cac9c2012-05-21 15:28:42 +0800327
328
329 @allow_multiple_section_input
330 def restore_EC(self, section):
331 """Restore the previously corrupted EC section signature.
332
333 Args:
334 section: An EC section, either 'a' or 'b'.
335 """
336 self._chromeos_interface.log('Restoring EC signature %s' %
337 section)
Vic Yang37a55462012-06-29 14:00:28 +0800338 self._ec_handler.restore_firmware(section, restore_all=True)
Vic Yang59cac9c2012-05-21 15:28:42 +0800339
340
341 @allow_multiple_section_input
342 def restore_EC_body(self, section):
343 """Restore the previously corrupted EC section body.
344
345 Args:
346 section: An EC section, either 'a' or 'b'.
347 """
348 self._chromeos_interface.log('Restoring EC body %s' %
349 section)
Vic Yang37a55462012-06-29 14:00:28 +0800350 self._ec_handler.restore_firmware_body(section, restore_all=True)
Tom Wai-Hong Tam81f70002011-12-13 12:29:46 +0800351
352
Tom Wai-Hong Tam0e680af2011-10-26 14:32:55 +0800353 @allow_multiple_section_input
Tom Wai-Hong Tamb8a58ef2011-10-11 23:53:10 -0700354 def corrupt_firmware(self, section):
Tom Wai-Hong Tam9aea8212011-12-12 15:08:45 +0800355 """Corrupt the requested firmware section signature.
Tom Wai-Hong Tamb8a58ef2011-10-11 23:53:10 -0700356
357 Args:
358 section: A firmware section, either 'a' or 'b'.
359 """
Tom Wai-Hong Tam9aea8212011-12-12 15:08:45 +0800360 self._chromeos_interface.log('Corrupting firmware signature %s' %
361 section)
Vic Yang59cac9c2012-05-21 15:28:42 +0800362 self._bios_handler.corrupt_firmware(section)
Tom Wai-Hong Tamb8a58ef2011-10-11 23:53:10 -0700363
364
Tom Wai-Hong Tam0e680af2011-10-26 14:32:55 +0800365 @allow_multiple_section_input
Tom Wai-Hong Tam81f70002011-12-13 12:29:46 +0800366 def corrupt_firmware_body(self, section):
367 """Corrupt the requested firmware section body.
368
369 Args:
370 section: A firmware section, either 'a' or 'b'.
371 """
372 self._chromeos_interface.log('Corrupting firmware body %s' %
373 section)
Vic Yang59cac9c2012-05-21 15:28:42 +0800374 self._bios_handler.corrupt_firmware_body(section)
Tom Wai-Hong Tam81f70002011-12-13 12:29:46 +0800375
376
377 @allow_multiple_section_input
Tom Wai-Hong Tamb8a58ef2011-10-11 23:53:10 -0700378 def restore_firmware(self, section):
Tom Wai-Hong Tam9aea8212011-12-12 15:08:45 +0800379 """Restore the previously corrupted firmware section signature.
Tom Wai-Hong Tamb8a58ef2011-10-11 23:53:10 -0700380
381 Args:
382 section: A firmware section, either 'a' or 'b'.
383 """
Tom Wai-Hong Tam9aea8212011-12-12 15:08:45 +0800384 self._chromeos_interface.log('Restoring firmware signature %s' %
385 section)
Vic Yang59cac9c2012-05-21 15:28:42 +0800386 self._bios_handler.restore_firmware(section)
Tom Wai-Hong Tamb8a58ef2011-10-11 23:53:10 -0700387
388
Tom Wai-Hong Tamcfda61f2011-11-02 17:41:01 +0800389 @allow_multiple_section_input
Tom Wai-Hong Tam81f70002011-12-13 12:29:46 +0800390 def restore_firmware_body(self, section):
391 """Restore the previously corrupted firmware section body.
392
393 Args:
394 section: A firmware section, either 'a' or 'b'.
395 """
396 self._chromeos_interface.log('Restoring firmware body %s' %
397 section)
Vic Yang59cac9c2012-05-21 15:28:42 +0800398 self._bios_handler.restore_firmware_body(section)
Tom Wai-Hong Tam81f70002011-12-13 12:29:46 +0800399
400
Vic Yang91b73cf2012-07-31 17:18:11 +0800401 def get_firmware_version(self, section):
402 """Retrieve firmware version of a section."""
403 return self._bios_handler.get_section_version(section)
404
405
Tom Wai-Hong Tam46d03b12012-02-08 12:02:17 +0800406 def _modify_firmware_version(self, section, delta):
407 """Modify firmware version for the requested section, by adding delta.
408
409 The passed in delta, a positive or a negative number, is added to the
410 original firmware version.
411 """
Vic Yang91b73cf2012-07-31 17:18:11 +0800412 original_version = self.get_firmware_version(section)
Tom Wai-Hong Tam46d03b12012-02-08 12:02:17 +0800413 new_version = original_version + delta
Vic Yang59cac9c2012-05-21 15:28:42 +0800414 flags = self._bios_handler.get_section_flags(section)
Tom Wai-Hong Tam46d03b12012-02-08 12:02:17 +0800415 self._chromeos_interface.log(
416 'Setting firmware section %s version from %d to %d' % (
417 section, original_version, new_version))
Vic Yang59cac9c2012-05-21 15:28:42 +0800418 self._bios_handler.set_section_version(section, new_version, flags,
419 write_through=True)
Tom Wai-Hong Tam46d03b12012-02-08 12:02:17 +0800420
421 @allow_multiple_section_input
422 def move_firmware_backward(self, section):
423 """Decrement firmware version for the requested section."""
424 self._modify_firmware_version(section, -1)
425
426
427 @allow_multiple_section_input
428 def move_firmware_forward(self, section):
429 """Increase firmware version for the requested section."""
430 self._modify_firmware_version(section, 1)
431
ctchangc7e55ea2012-08-09 16:19:14 +0800432 def retrieve_firmware_version(self, section):
433 """Return firmware version."""
434 return self._bios_handler.get_section_version(section)
435
436 def retrieve_firmware_datakey_version(self, section):
437 """Return firmware data key version."""
438 return self._bios_handler.get_section_datakey_version(section)
439
440 def retrieve_kernel_subkey_version(self,section):
441 """Return kernel subkey version."""
442 return self._bios_handler.get_section_kernel_subkey_version(section)
Tom Wai-Hong Tam46d03b12012-02-08 12:02:17 +0800443
Tom Wai-Hong Tam81f70002011-12-13 12:29:46 +0800444 @allow_multiple_section_input
Tom Wai-Hong Tamcfda61f2011-11-02 17:41:01 +0800445 def corrupt_kernel(self, section):
446 """Corrupt the requested kernel section.
447
448 Args:
449 section: A kernel section, either 'a' or 'b'.
450 """
451 self._chromeos_interface.log('Corrupting kernel %s' % section)
452 self._kernel_handler.corrupt_kernel(section)
453
454
455 @allow_multiple_section_input
456 def restore_kernel(self, section):
457 """Restore the requested kernel section (previously corrupted).
458
459 Args:
460 section: A kernel section, either 'a' or 'b'.
461 """
Tom Wai-Hong Tamcfda61f2011-11-02 17:41:01 +0800462 self._kernel_handler.restore_kernel(section)
463
464
Tom Wai-Hong Tam07278c22012-02-08 16:53:00 +0800465 def _modify_kernel_version(self, section, delta):
466 """Modify kernel version for the requested section, by adding delta.
467
468 The passed in delta, a positive or a negative number, is added to the
469 original kernel version.
470 """
471 original_version = self._kernel_handler.get_version(section)
472 new_version = original_version + delta
473 self._chromeos_interface.log(
474 'Setting kernel section %s version from %d to %d' % (
475 section, original_version, new_version))
476 self._kernel_handler.set_version(section, new_version)
477
478
479 @allow_multiple_section_input
480 def move_kernel_backward(self, section):
481 """Decrement kernel version for the requested section."""
482 self._modify_kernel_version(section, -1)
483
484
485 @allow_multiple_section_input
486 def move_kernel_forward(self, section):
487 """Increase kernel version for the requested section."""
488 self._modify_kernel_version(section, 1)
489
ctchangcc88d112012-08-23 17:56:15 +0800490
491 def retrieve_kernel_version(self, section):
492 """Return kernel version."""
493 return self._kernel_handler.get_version(section)
494
495
496 def retrieve_kernel_datakey_version(self, section):
497 """Return kernel datakey version."""
498 return self._kernel_handler.get_datakey_version(section)
499
500
Tom Wai-Hong Tam622d0ba2012-08-15 16:29:05 +0800501 def diff_kernel_a_b(self):
502 """Compare kernel A with B.
503
504 Returns:
505 True: if kernel A is different with B.
506 False: if kernel A is the same as B.
507 """
508 rootdev = self._chromeos_interface.get_root_dev()
509 kernel_a = self._chromeos_interface.join_part(rootdev, '3')
510 kernel_b = self._chromeos_interface.join_part(rootdev, '5')
511
512 # The signature (some kind of hash) for the kernel body is stored in
513 # the beginning. So compare the first 64KB (including header, preamble,
514 # and signature) should be enough to check them identical.
515 header_a = self._chromeos_interface.read_partition(kernel_a, 0x10000)
516 header_b = self._chromeos_interface.read_partition(kernel_b, 0x10000)
517
518 return header_a != header_b
Tom Wai-Hong Tam07278c22012-02-08 16:53:00 +0800519
Tom Wai-Hong Tam23870e02012-08-24 16:15:34 +0800520
521 def setup_EC_image(self, ec_path):
522 """Setup the new EC image for later update.
523
524 Args:
525 ec_path: The path of the EC image to be updated.
526 """
527 self._ec_image = flashrom_handler.FlashromHandler()
528 self._ec_image.init(saft_flashrom_util,
529 self._chromeos_interface,
530 'ec_root_key.vpubk',
531 '/usr/share/vboot/devkeys',
532 'ec')
533 self._ec_image.new_image(ec_path)
534
535
536 def get_EC_image_sha(self):
537 """Get SHA1 hash of RW firmware section of the EC autest image."""
538 return self._ec_image.get_section_sha('rw')
539
540
541 def update_EC_from_image(self, section, flags):
542 """Update EC via software sync design.
543
544 It copys the RW section from the EC image, which is loaded by calling
545 setup_EC_image(), to the EC area of the specified RW section on the
546 current AP firmware.
547
548 Args:
549 section: A firmware section on current BIOS, either 'a' or 'b'.
550 flags: An integer of preamble flags.
551 """
552 blob = self._ec_image.get_section_body('rw')
553 self._bios_handler.set_section_ecbin(section, blob,
554 write_through=True)
555 self.set_firmware_flags(section, flags)
556
557
Tom Wai-Hong Tamb63bc742012-08-30 20:41:30 +0800558 def dump_firmware(self, bios_path):
559 """Dump the current BIOS firmware to a file, specified by bios_path.
560
561 Args:
562 bios_path: The path of the BIOS image to be written.
563 """
564 self._bios_handler.dump_whole(bios_path)
565
566
ctchang38ae4922012-09-03 17:01:16 +0800567 def dump_firmware_rw(self, dir_path):
568 """Dump the current BIOS firmware RW to dir_path.
569
570 VBOOTA, VBOOTB, FVMAIN, FVMAINB need to be dumped.
571
572 Args:
573 dir_path: The path of directory which contains files to be written.
574 """
575 if not os.path.isdir(dir_path):
576 raise Exception("%s doesn't exist" % dir_path)
577
578 VBOOTA_blob = self._bios_handler.get_section_sig('a')
579 VBOOTB_blob = self._bios_handler.get_section_sig('b')
580 FVMAIN_blob = self._bios_handler.get_section_body('a')
581 FVMAINB_blob = self._bios_handler.get_section_body('b')
582
583 open(os.path.join(dir_path, 'VBOOTA'), 'w').write(VBOOTA_blob)
584 open(os.path.join(dir_path, 'VBOOTB'), 'w').write(VBOOTB_blob)
585 open(os.path.join(dir_path, 'FVMAIN'), 'w').write(FVMAIN_blob)
586 open(os.path.join(dir_path, 'FVMAINB'), 'w').write(FVMAINB_blob)
587
588
Tom Wai-Hong Tamb63bc742012-08-30 20:41:30 +0800589 def write_firmware(self, bios_path):
590 """Write the firmware from bios_path to the current system.
591
592 Args:
593 bios_path: The path of the source BIOS image.
594 """
595 self._bios_handler.new_image(bios_path)
596 self._bios_handler.write_whole()
597
598
ctchang38ae4922012-09-03 17:01:16 +0800599 def write_firmware_rw(self, dir_path):
600 """Write the firmware RW from dir_path to the current system.
601
602 VBOOTA, VBOOTB, FVMAIN, FVMAINB need to be written.
603
604 Args:
605 dir_path: The path of directory which contains the source files.
606 """
607 if not os.path.exists(os.path.join(dir_path, 'VBOOTA')) or \
608 not os.path.exists(os.path.join(dir_path, 'VBOOTB')) or \
609 not os.path.exists(os.path.join(dir_path, 'FVMAIN')) or \
610 not os.path.exists(os.path.join(dir_path, 'FVMAINB')):
611 raise Exception("Source firmware file(s) doesn't exist.")
612
613 VBOOTA_blob = open(os.path.join(dir_path, 'VBOOTA'), 'rb').read()
614 VBOOTB_blob = open(os.path.join(dir_path, 'VBOOTB'), 'rb').read()
615 FVMAIN_blob = open(os.path.join(dir_path, 'FVMAIN'), 'rb').read()
616 FVMAINB_blob = open(os.path.join(dir_path, 'FVMAINB'), 'rb').read()
617
618 self._bios_handler.set_section_sig('a', VBOOTA_blob,
619 write_through=True)
620 self._bios_handler.set_section_sig('b', VBOOTB_blob,
621 write_through=True)
622 self._bios_handler.set_section_body('a', FVMAIN_blob,
623 write_through=True)
624 self._bios_handler.set_section_body('b', FVMAINB_blob,
625 write_through=True)
626
627
Tom Wai-Hong Tam23870e02012-08-24 16:15:34 +0800628 def dump_EC_firmware(self, ec_path):
629 """Dump the current EC firmware to a file, specified by ec_path.
630
631 Args:
632 ec_path: The path of the EC image to be written.
633 """
634 self._ec_handler.dump_whole(ec_path)
635
636
Tom Wai-Hong Tam48958832011-12-30 10:16:57 +0800637 def run_cgpt_test_loop(self):
638 """Run the CgptState test loop. The tst logic is handled in the client.
639
640 Returns:
641 0: there are more cgpt tests to execute.
642 1: no more CgptState test, finished.
643 """
644 return self._cgpt_state.test_loop()
645
646
647 def set_cgpt_test_step(self, step):
648 """Set the CgptState test step.
649
650 Args:
651 step: A test step number.
652 """
653 self._cgpt_state.set_step(step)
654
655
656 def get_cgpt_test_step(self):
657 """Get the CgptState test step.
658
659 Returns:
660 A test step number.
661 """
662 return self._cgpt_state.get_step()
663
664
Chun-ting Changf91ee0f2012-09-17 18:31:54 +0800665 def setup_firmwareupdate_temp_dir(self, shellball=None):
ctchangc7e55ea2012-08-09 16:19:14 +0800666 """Setup temporary directory.
667
Chun-ting Changf91ee0f2012-09-17 18:31:54 +0800668 Devkeys are copied to _key_path. Then, shellball (default:
669 /usr/sbin/chromeos-firmwareupdate) is extracted to _work_path.
670
671 Args:
672 shellball: Path of shellball.
ctchangc7e55ea2012-08-09 16:19:14 +0800673 """
ctchang6b700df2012-08-20 13:48:07 +0800674
675 self.cleanup_firmwareupdate_temp_dir()
676
ctchangc7e55ea2012-08-09 16:19:14 +0800677 os.mkdir(self._temp_path)
678 os.chdir(self._temp_path)
679
680 os.mkdir(self._work_path)
681 shutil.copytree('/usr/share/vboot/devkeys/', self._keys_path)
Chun-ting Changf91ee0f2012-09-17 18:31:54 +0800682
683 shellball_path = os.path.join(self._temp_path,
684 'chromeos-firmwareupdate')
685
686 if shellball:
687 shutil.copyfile(shellball, shellball_path)
688 else:
689 shutil.copyfile('/usr/sbin/chromeos-firmwareupdate',
690 shellball_path)
ctchangc7e55ea2012-08-09 16:19:14 +0800691 self.run_shell_command(
Chun-ting Changf91ee0f2012-09-17 18:31:54 +0800692 'sh %s --sb_extract %s' % (shellball_path, self._work_path))
ctchangc7e55ea2012-08-09 16:19:14 +0800693
694
695 def retrieve_shellball_fwid(self):
696 """Retrieve shellball's fwid.
697
698 This method should be called after setup_firmwareupdate_temp_dir.
699
700 Returns:
701 Shellball's fwid.
702 """
703 self.run_shell_command('dump_fmap -x %s %s' %
704 (os.path.join(self._work_path, 'bios.bin'),
705 'RW_FWID_A'))
706
707 [fwid] = self.run_shell_command_get_output(
ctchangcc88d112012-08-23 17:56:15 +0800708 "cat RW_FWID_A | tr '\\0' '\\t' | cut -f1")
ctchangc7e55ea2012-08-09 16:19:14 +0800709
710 return fwid
711
712
713 def cleanup_firmwareupdate_temp_dir(self):
714 """Cleanup temporary directory."""
ctchang6b700df2012-08-20 13:48:07 +0800715 if os.path.isdir(self._temp_path):
716 shutil.rmtree(self._temp_path)
ctchangc7e55ea2012-08-09 16:19:14 +0800717
718
719 def repack_firmwareupdate_shellball(self, append):
720 """Repack shellball with new fwid.
721
722 New fwid follows the rule: [orignal_fwid]-[append].
723
724 Args:
725 append: use for new fwid naming.
726 """
727 shutil.copy('/usr/sbin/chromeos-firmwareupdate', '%s' %
728 os.path.join(self._temp_path,
729 'chromeos-firmwareupdate-%s' % append))
730
ctchang6b700df2012-08-20 13:48:07 +0800731 self.run_shell_command('sh %s --sb_repack %s' % (
732 os.path.join(self._temp_path,
733 'chromeos-firmwareupdate-%s' % append),
734 self._work_path))
ctchangc7e55ea2012-08-09 16:19:14 +0800735
736 args = ['-i']
737 args.append('"s/TARGET_FWID=\\"\\(.*\\)\\"/TARGET_FWID=\\"\\1.%s\\"/g"'
738 % append)
739 args.append('%s'
740 % os.path.join(self._temp_path,
741 'chromeos-firmwareupdate-%s' % append))
742 cmd = 'sed %s' % ' '.join(args)
743 self.run_shell_command(cmd)
744
745 args = ['-i']
746 args.append('"s/TARGET_UNSTABLE=\\".*\\"/TARGET_UNSTABLE=\\"\\"/g"')
747 args.append('%s'
748 % os.path.join(self._temp_path,
749 'chromeos-firmwareupdate-%s' % append))
750 cmd = 'sed %s' % ' '.join(args)
751 self.run_shell_command(cmd)
752
753
754 def resign_firmware(self, version):
755 """Resign firmware with version.
756
757 Args:
758 version: new firmware version number.
759 """
760 args = [os.path.join(self._work_path, 'bios.bin')]
761 args.append(os.path.join(self._temp_path, 'output.bin'))
762 args.append(os.path.join(self._keys_path, 'firmware_data_key.vbprivk'))
763 args.append(os.path.join(self._keys_path, 'firmware.keyblock'))
764 args.append(os.path.join(self._keys_path,
765 'dev_firmware_data_key.vbprivk'))
766 args.append(os.path.join(self._keys_path, 'dev_firmware.keyblock'))
767 args.append(os.path.join(self._keys_path, 'kernel_subkey.vbpubk'))
768 args.append('%d' % version)
769 args.append('1')
770 cmd = '/usr/share/vboot/bin/resign_firmwarefd.sh %s' % ' '.join(args)
771 self.run_shell_command(cmd)
772
773 shutil.copyfile('%s' % os.path.join(self._temp_path, 'output.bin'),
774 '%s' % os.path.join(self._work_path, 'bios.bin'))
775
776
777 def run_firmware_autoupdate(self, append):
778 """Do firmwareupdate with autoupdate mode using new shellball.
779
780 Args:
781 append: decide which shellball to use with format
782 chromeos-firmwareupdate-[append]
783 """
784 self.run_shell_command(
785 '/bin/sh %s --mode autoupdate --noupdate_ec'
786 % os.path.join(self._temp_path,
787 'chromeos-firmwareupdate-%s' % append))
788
789
Chun-ting Changf91ee0f2012-09-17 18:31:54 +0800790 def run_firmware_factory_install(self):
791 """ Do firmwareupdate with factory_install mode using new shellball."""
792 self.run_shell_command(
793 '/bin/sh %s --mode factory_install --noupdate_ec'
794 % os.path.join(self._temp_path, 'chromeos-firmwareupdate'))
795
796
ctchangc7e55ea2012-08-09 16:19:14 +0800797 def run_firmware_bootok(self, append):
798 """Do bootok mode using new shellball.
799
800 Copy firmware B to firmware A if reboot success.
801 """
802 self.run_shell_command(
803 '/bin/sh %s --mode bootok' % os.path.join(self._temp_path,
804 'chromeos-firmwareupdate-%s' % append))
805
806
807 def run_firmware_recovery(self):
808 """Recovery to original shellball."""
Chun-ting Changf91ee0f2012-09-17 18:31:54 +0800809 self.run_shell_command(
810 '/bin/sh %s --mode recovery --noupdate_ec' % os.path.join(
811 self._temp_path, 'chromeos-firmwareupdate'))
ctchangc7e55ea2012-08-09 16:19:14 +0800812
813
814 def get_temp_path(self):
815 """Get temporary directory path."""
816 return self._temp_path
817
818
ctchangcc88d112012-08-23 17:56:15 +0800819 def get_keys_path(self):
820 """Get keys path in temporary directory."""
821 return self._keys_path
822
823
824 def resign_kernel_with_keys(self, section, key_path=None):
825 """Resign kernel with temporary key."""
826 self._kernel_handler.resign_kernel(section, key_path)
827
828
ctchang38ae4922012-09-03 17:01:16 +0800829 def create_temp_dir(self, prefix='backup_'):
830 """Create a temporary directory and return the path."""
831 return tempfile.mkdtemp(prefix=prefix)
832
833
Tom Wai-Hong Tambea57b32011-09-02 18:27:47 +0800834 def cleanup(self):
835 """Cleanup for the RPC server. Currently nothing."""
836 pass
837
838
Tom Wai-Hong Tamb8a58ef2011-10-11 23:53:10 -0700839def main():
840 parser = OptionParser(usage='Usage: %prog [options]')
841 parser.add_option('--port', type='int', dest='port', default=9990,
842 help='port number of XMLRPC server')
843 (options, args) = parser.parse_args()
844
845 faft_client = FAFTClient()
846
847 # Launch the XMLRPC server to provide FAFTClient commands.
Tom Wai-Hong Tam4a257e52011-11-12 08:36:22 +0800848 server = SimpleXMLRPCServer(('localhost', options.port), allow_none=True,
849 logRequests=False)
Tom Wai-Hong Tamb8a58ef2011-10-11 23:53:10 -0700850 server.register_introspection_functions()
851 server.register_instance(faft_client)
852 print 'XMLRPC Server: Serving FAFTClient on port %s' % options.port
853 server.serve_forever()
854
855
856if __name__ == '__main__':
857 main()