1# Copyright 2017 - The Android Open Source Project 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15"""Implementation of gsi_util flash_gsi command.""" 16 17from gsi_util.utils import cmd_utils 18from gsi_util.utils import fastboot_utils 19from gsi_util.utils import file_utils 20 21 22def do_flash_gsi(args): 23 """Flashes a GSI image (system.img). 24 25 Also erases userdata/metadata partition and disables 26 Android Verified Boot (AVB). 27 28 Args: 29 args: flash_gsi command arguments. 30 """ 31 32 fastboot_utils.erase() # erases userdata/cache partition 33 # Not every device has metadata partition, so allow_error is True. 34 fastboot_utils.erase('metadata', allow_error=True) 35 36 # Flashes GSI. 37 fastboot_utils.flash('system', args.image) 38 39 # Disables AVB. 40 with file_utils.UnopenedTemporaryFile() as vbmeta_image: 41 # vbmeta flag 2 means disable entire AVB verification. 42 cmd_utils.run_command(['avbtool', 'make_vbmeta_image', 43 '--flag', '2', 44 '--padding_size', '4096', 45 '--output', vbmeta_image]) 46 # Not every device uses AVB, so allow_error is True. 47 fastboot_utils.flash('vbmeta', vbmeta_image, allow_error=True) 48 49 # Reboots the device. 50 fastboot_utils.reboot() 51 52 53def setup_command_args(subparsers): 54 """Sets up command args for 'flash_gsi'.""" 55 parser = subparsers.add_parser( 56 'flash_gsi', help='flash a GSI image', 57 description=('Flash a GSI image - ' 58 'including erasing userdata, ' 59 'disabling AVB (if the device supports AVB) ' 60 'and erasing metadata partition (if the device has).')) 61 parser.add_argument('-i', '--image', 62 help='the GSI image to flash', type=str) 63 parser.set_defaults(func=do_flash_gsi) 64