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.config.Option;
20 import com.android.tradefed.config.OptionClass;
21 import com.android.tradefed.device.DeviceNotAvailableException;
22 import com.android.tradefed.device.ITestDevice;
23 import com.android.tradefed.invoker.TestInformation;
24 import com.android.tradefed.log.LogUtil.CLog;
25 import com.android.tradefed.util.CommandResult;
26 import com.android.tradefed.util.CommandStatus;
27 
28 /** A {@link ITargetPreparer} that wipes userdata */
29 @OptionClass(alias = "device-wiper")
30 public class DeviceWiper extends BaseTargetPreparer {
31 
32     @Option(name = "use-erase", description =
33             "instruct wiper to use fastboot erase instead of format")
34     protected boolean mUseErase = false;
35 
36     @Override
setUp(TestInformation testInfo)37     public void setUp(TestInformation testInfo)
38             throws TargetSetupError, BuildError, DeviceNotAvailableException {
39         ITestDevice device = testInfo.getDevice();
40         CLog.i("Wiping device");
41         device.rebootIntoBootloader();
42         if (mUseErase) {
43             doErase(device);
44         } else {
45             doFormat(device);
46         }
47         device.executeFastbootCommand("reboot");
48         device.waitForDeviceAvailable();
49     }
50 
doFormat(ITestDevice device)51     private void doFormat(ITestDevice device) throws DeviceNotAvailableException, TargetSetupError {
52         CLog.d("Attempting fastboot wiping");
53         CommandResult r = device.executeLongFastbootCommand("-w");
54         if (r.getStatus() != CommandStatus.SUCCESS) {
55             throw new TargetSetupError(String.format("fastboot wiping failed: %s", r.getStderr()),
56                     device.getDeviceDescriptor());
57         }
58     }
59 
doErase(ITestDevice device)60     private void doErase(ITestDevice device) throws DeviceNotAvailableException, TargetSetupError {
61         performFastbootOp(device, "erase", "cache");
62         performFastbootOp(device, "erase", "userdata");
63     }
64 
performFastbootOp(ITestDevice device, String op, String partition)65     private void performFastbootOp(ITestDevice device, String op, String partition)
66             throws DeviceNotAvailableException, TargetSetupError {
67         CLog.d("Attempting fastboot %s %s", op, partition);
68         CommandResult r = device.executeLongFastbootCommand(op, partition);
69         if (r.getStatus() != CommandStatus.SUCCESS) {
70             throw new TargetSetupError(String.format("%s %s failed: %s", op, partition,
71                     r.getStderr()), device.getDeviceDescriptor());
72         }
73     }
74 }
75