1 /*
2  * Copyright (C) 2018 Google Inc.
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 package com.android.tradefed.targetprep;
17 
18 import com.android.tradefed.config.Option;
19 import com.android.tradefed.config.OptionClass;
20 import com.android.tradefed.device.DeviceNotAvailableException;
21 import com.android.tradefed.invoker.TestInformation;
22 
23 import java.util.ArrayList;
24 import java.util.List;
25 
26 /**
27  * Target preparer that triggers fastboot and sends fastboot commands.
28  *
29  * <p>TODO(b/122592575): Add tests for this preparer.
30  */
31 @OptionClass(alias = "fastboot-command-preparer")
32 public final class FastbootCommandPreparer extends BaseTargetPreparer {
33 
34     private enum FastbootMode {
35         BOOTLOADER,
36         FASTBOOTD,
37     }
38 
39     @Option(
40             name = "fastboot-mode",
41             description = "True to boot the device into bootloader mode, false for fastbootd mode.")
42     private FastbootMode mFastbootMode = FastbootMode.BOOTLOADER;
43 
44     @Option(
45             name = "stay-fastboot",
46             description = "True to keep the device in bootloader or fastbootd mode.")
47     private boolean mStayFastboot = false;
48 
49     @Option(name = "command", description = "Fastboot commands to run.")
50     private List<String> mFastbootCommands = new ArrayList<String>();
51 
52     @Override
setUp(TestInformation testInformation)53     public void setUp(TestInformation testInformation)
54             throws TargetSetupError, BuildError, DeviceNotAvailableException {
55         if (mFastbootMode == FastbootMode.BOOTLOADER) {
56             testInformation.getDevice().rebootIntoBootloader();
57         } else {
58             testInformation.getDevice().rebootIntoFastbootd();
59         }
60 
61         for (String cmd : mFastbootCommands) {
62             // Ignore reboots since we'll reboot in the end.
63             if (cmd.trim().equals("reboot")) {
64                 continue;
65             }
66 
67             testInformation.getDevice().executeFastbootCommand(cmd.split("\\s+"));
68         }
69 
70         if (!mStayFastboot) {
71             testInformation.getDevice().reboot();
72         }
73     }
74 
75     /** {@inheritDoc} */
76     @Override
tearDown(TestInformation testInformation, Throwable e)77     public void tearDown(TestInformation testInformation, Throwable e)
78             throws DeviceNotAvailableException {
79         testInformation.getDevice().reboot();
80     }
81 }
82