Rajesh Chenna | d7511d4 | 2012-01-17 15:06:36 -0800 | [diff] [blame] | 1 | #!/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 | |
| 9 | This scripts mounts given image and copies chromeos-firmwareupdate file. Then |
| 10 | extracts chromeos-firmwareupdater and replaces given bios.bin in |
| 11 | chromeos-firmwareupdater, re-packs the file and enables firmare update. |
| 12 | At the end it unmounts the image. |
| 13 | |
| 14 | This is useful for test team to test new firmware/coreboot. |
| 15 | |
| 16 | Syntax: |
| 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 | |
| 26 | import commands |
| 27 | import logging |
| 28 | import optparse |
| 29 | import os |
| 30 | import re |
| 31 | import sys |
| 32 | |
| 33 | # Constants |
| 34 | dev_keys = '$HOME/trunk/src/platform/vboot_reference/tests/devkeys' |
| 35 | mount_gpt_image = '$HOME/trunk/src/scripts/mount_gpt_image.sh' |
| 36 | image_signing_dir = ('$HOME/trunk/src/platform/vboot_reference/scripts/' |
| 37 | 'image_signing') |
| 38 | |
| 39 | def 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 | |
| 99 | if __name__ == '__main__': |
| 100 | main() |