blob: 260f662229c8092a5fdd4b3e94534d7449d34c0b [file] [log] [blame]
Rajesh Chennad7511d42012-01-17 15:06:36 -08001#!/usr/bin/python
2
3# Copyright(c) 2012 The Chromium OS Author. All Rights Reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file
6
7"""This script updates given firmware in shellball and puts in an image.
8
9This scripts mounts given image and copies chromeos-firmwareupdate file. Then
10extracts chromeos-firmwareupdater and replaces given bios.bin in
11chromeos-firmwareupdater, re-packs the file and enables firmare update.
12At the end it unmounts the image.
13
14This is useful for test team to test new firmware/coreboot.
15
16Syntax:
17 update_firmware_image.py --imagedir <path to image> --image <image name>
18 --bios <path to bios>
19 e.g. update_firmware_image.py --imagedir /home/$USER/src/images/
20 --image chromiumos_test_image.bin
21 --bios /home/$USER/src/bios/bios.bin
22"""
23
24#__author__ = 'rchenna@google.com (Rajesh Chenna)'
25
26import commands
27import logging
28import optparse
29import os
30import re
31import sys
32
33# Constants
34dev_keys = '$HOME/trunk/src/platform/vboot_reference/tests/devkeys'
35mount_gpt_image = '$HOME/trunk/src/scripts/mount_gpt_image.sh'
36image_signing_dir = ('$HOME/trunk/src/platform/vboot_reference/scripts/'
37 'image_signing')
38
39def main():
40
41 parser = optparse.OptionParser()
42 parser.add_option('-b', '--bios', help='bios name including path')
43 parser.add_option('-d', '--imagedir', help='image directory')
44 parser.add_option('-i', '--image', help='image name')
45
46 (options, args) = parser.parse_args()
47 # Checking whether running inside chroot or not.
48 if not os.path.exists('/etc/debian_chroot'):
49 logging.fatal("Make sure you are inside chroot")
50 sys.exit(0)
51 # Conditions to check all arguments.
52 if not all([options.bios,
53 options.imagedir,
54 options.image]):
55 logging.fatal('Missing arguments.')
56 logging.fatal('Please provide bios, imagedir and image')
57 sys.exit(0)
58
59 # Verify bios.bin is passing.
60 #If not, copy the supplied bios to bios.bin.
61 if 'bios.bin' not in options.bios:
62 os.system('cp %s %s/bios.bin' %(options.bios,
63 options.bios[:options.bios.rfind('/')]))
64 # Step1: Mount the image.
65 os.system('sudo %s --from %s --image %s'
66 % (mount_gpt_image, options.imagedir,
67 options.image))
68
69 # Step2: copy shellball.
70 os.system('sudo cp /tmp/m/usr/sbin/chromeos-firmwareupdate /tmp/')
71 # Step3: Extract shellball.
72 extract = commands.getoutput('sudo /tmp/chromeos-firmwareupdate '
73 '--sb_extract')
74 extract_dir = re.match('Extracting to: (.+)', extract)
75 # Step4: copy bios.bin to extracted directory.
76 os.system('sudo cp %s/bios.bin %s/'
77 %(options.bios[:options.bios.rfind('/')], extract_dir.group(1)))
78 # Step5: repack shellball.
79 os.system('sudo /tmp/chromeos-firmwareupdate --sb_repack %s'
80 %(extract_dir.group(1)))
81 # Step6: copy shellball back to /tmp/m location.
82 os.system('sudo mv /tmp/chromeos-firmwareupdate /tmp/m/usr/sbin/')
83 # Step7: Unmount the image.
84 os.system('%s -u' %mount_gpt_image)
85 # Step 8: enable firmware update.
86 os.system('sudo %s/tag_image.sh --from=%s/%s --update_firmware=1'
87 %(image_signing_dir,
88 options.imagedir,
89 options.image))
90 # Step 9: Re-sign the image.
91 os.system('sudo %s/sign_official_build.sh usb %s/%s %s %s/resigned_%s'
92 %(image_signing_dir,
93 options.imagedir,
94 options.image,
95 dev_keys,
96 options.imagedir,
97 options.image))
98
99if __name__ == '__main__':
100 main()