Rajesh Chenna | d7511d4 | 2012-01-17 23:06:36 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
Gilad Arnold | abb352e | 2012-09-23 08:24:27 | [diff] [blame] | 3 | # Copyright (c) 2009-2012 The Chromium OS Authors. All rights reserved. |
Rajesh Chenna | d7511d4 | 2012-01-17 23:06:36 | [diff] [blame] | 4 | # Use of this source code is governed by a BSD-style license that can be |
Gilad Arnold | abb352e | 2012-09-23 08:24:27 | [diff] [blame] | 5 | # found in the LICENSE file. |
Rajesh Chenna | d7511d4 | 2012-01-17 23:06:36 | [diff] [blame] | 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__ = '[email protected] (Rajesh Chenna)' |
| 25 | |
Gilad Arnold | abb352e | 2012-09-23 08:24:27 | [diff] [blame] | 26 | # TODO(garnold) deprecated module, switch to using subprocess. |
Rajesh Chenna | d7511d4 | 2012-01-17 23:06:36 | [diff] [blame] | 27 | import commands |
| 28 | import logging |
| 29 | import optparse |
| 30 | import os |
| 31 | import re |
| 32 | import sys |
| 33 | |
Gilad Arnold | abb352e | 2012-09-23 08:24:27 | [diff] [blame] | 34 | |
Rajesh Chenna | d7511d4 | 2012-01-17 23:06:36 | [diff] [blame] | 35 | # Constants |
| 36 | dev_keys = '$HOME/trunk/src/platform/vboot_reference/tests/devkeys' |
| 37 | mount_gpt_image = '$HOME/trunk/src/scripts/mount_gpt_image.sh' |
| 38 | image_signing_dir = ('$HOME/trunk/src/platform/vboot_reference/scripts/' |
| 39 | 'image_signing') |
| 40 | |
| 41 | def main(): |
| 42 | |
| 43 | parser = optparse.OptionParser() |
| 44 | parser.add_option('-b', '--bios', help='bios name including path') |
| 45 | parser.add_option('-d', '--imagedir', help='image directory') |
| 46 | parser.add_option('-i', '--image', help='image name') |
| 47 | |
| 48 | (options, args) = parser.parse_args() |
| 49 | # Checking whether running inside chroot or not. |
| 50 | if not os.path.exists('/etc/debian_chroot'): |
| 51 | logging.fatal("Make sure you are inside chroot") |
| 52 | sys.exit(0) |
| 53 | # Conditions to check all arguments. |
| 54 | if not all([options.bios, |
| 55 | options.imagedir, |
| 56 | options.image]): |
| 57 | logging.fatal('Missing arguments.') |
| 58 | logging.fatal('Please provide bios, imagedir and image') |
| 59 | sys.exit(0) |
| 60 | |
| 61 | # Verify bios.bin is passing. |
| 62 | #If not, copy the supplied bios to bios.bin. |
| 63 | if 'bios.bin' not in options.bios: |
| 64 | os.system('cp %s %s/bios.bin' %(options.bios, |
| 65 | options.bios[:options.bios.rfind('/')])) |
| 66 | # Step1: Mount the image. |
| 67 | os.system('sudo %s --from %s --image %s' |
| 68 | % (mount_gpt_image, options.imagedir, |
| 69 | options.image)) |
| 70 | |
| 71 | # Step2: copy shellball. |
| 72 | os.system('sudo cp /tmp/m/usr/sbin/chromeos-firmwareupdate /tmp/') |
| 73 | # Step3: Extract shellball. |
| 74 | extract = commands.getoutput('sudo /tmp/chromeos-firmwareupdate ' |
| 75 | '--sb_extract') |
| 76 | extract_dir = re.match('Extracting to: (.+)', extract) |
| 77 | # Step4: copy bios.bin to extracted directory. |
| 78 | os.system('sudo cp %s/bios.bin %s/' |
| 79 | %(options.bios[:options.bios.rfind('/')], extract_dir.group(1))) |
| 80 | # Step5: repack shellball. |
| 81 | os.system('sudo /tmp/chromeos-firmwareupdate --sb_repack %s' |
| 82 | %(extract_dir.group(1))) |
| 83 | # Step6: copy shellball back to /tmp/m location. |
| 84 | os.system('sudo mv /tmp/chromeos-firmwareupdate /tmp/m/usr/sbin/') |
| 85 | # Step7: Unmount the image. |
| 86 | os.system('%s -u' %mount_gpt_image) |
| 87 | # Step 8: enable firmware update. |
| 88 | os.system('sudo %s/tag_image.sh --from=%s/%s --update_firmware=1' |
| 89 | %(image_signing_dir, |
| 90 | options.imagedir, |
| 91 | options.image)) |
| 92 | # Step 9: Re-sign the image. |
| 93 | os.system('sudo %s/sign_official_build.sh usb %s/%s %s %s/resigned_%s' |
| 94 | %(image_signing_dir, |
| 95 | options.imagedir, |
| 96 | options.image, |
| 97 | dev_keys, |
| 98 | options.imagedir, |
| 99 | options.image)) |
| 100 | |
| 101 | if __name__ == '__main__': |
| 102 | main() |