Home | History | Annotate | Download | only in targetprep
      1 /*
      2  * Copyright (C) 2014 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.tradefed.targetprep;
     18 
     19 import com.android.tradefed.build.IBuildInfo;
     20 import com.android.tradefed.config.Option;
     21 import com.android.tradefed.config.OptionClass;
     22 import com.android.tradefed.device.DeviceNotAvailableException;
     23 import com.android.tradefed.device.ITestDevice;
     24 import com.android.tradefed.log.LogUtil.CLog;
     25 import com.android.tradefed.util.CommandResult;
     26 import com.android.tradefed.util.CommandStatus;
     27 
     28 /**
     29  * A {@link ITargetPreparer} that wipes userdata
     30  */
     31 @OptionClass(alias = "device-wiper")
     32 public class DeviceWiper implements ITargetPreparer {
     33 
     34     @Option(name = "disable", description = "disables the device wiper")
     35     protected boolean mDisable = false;
     36 
     37     @Option(name = "use-erase", description =
     38             "instruct wiper to use fastboot erase instead of format")
     39     protected boolean mUseErase = false;
     40 
     41     /**
     42      * Return True if this target preparer has been disabled and will do nothing. False otherwise.
     43      */
     44     public boolean isDisabled() {
     45         return mDisable;
     46     }
     47 
     48     @Override
     49     public void setUp(ITestDevice device, IBuildInfo buildInfo) throws TargetSetupError,
     50             DeviceNotAvailableException {
     51         if (mDisable) {
     52             return;
     53         }
     54         CLog.i("Wiping device");
     55         device.rebootIntoBootloader();
     56         if (mUseErase) {
     57             doErase(device);
     58         } else {
     59             doFormat(device);
     60         }
     61         device.executeFastbootCommand("reboot");
     62         device.waitForDeviceAvailable();
     63     }
     64 
     65     private void doFormat(ITestDevice device) throws DeviceNotAvailableException, TargetSetupError {
     66         CLog.d("Attempting fastboot wiping");
     67         CommandResult r = device.executeLongFastbootCommand("-w");
     68         if (r.getStatus() != CommandStatus.SUCCESS) {
     69             throw new TargetSetupError(String.format("fastboot wiping failed: %s", r.getStderr()),
     70                     device.getDeviceDescriptor());
     71         }
     72     }
     73 
     74     private void doErase(ITestDevice device) throws DeviceNotAvailableException, TargetSetupError {
     75         performFastbootOp(device, "erase", "cache");
     76         performFastbootOp(device, "erase", "userdata");
     77     }
     78 
     79     private void performFastbootOp(ITestDevice device, String op, String partition)
     80             throws DeviceNotAvailableException, TargetSetupError {
     81         CLog.d("Attempting fastboot %s %s", op, partition);
     82         CommandResult r = device.executeLongFastbootCommand(op, partition);
     83         if (r.getStatus() != CommandStatus.SUCCESS) {
     84             throw new TargetSetupError(String.format("%s %s failed: %s", op, partition,
     85                     r.getStderr()), device.getDeviceDescriptor());
     86         }
     87     }
     88 }
     89