• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.build.IBuildInfo;
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.result.error.InfraErrorIdentifier;
25 import com.android.tradefed.util.CommandResult;
26 import java.io.File;
27 import java.util.ArrayList;
28 import java.util.List;
29 import java.util.regex.Matcher;
30 import java.util.regex.Pattern;
31 
32 /** Target preparer that triggers fastboot and sends fastboot commands. */
33 @OptionClass(alias = "fastboot-command-preparer")
34 public final class FastbootCommandPreparer extends BaseTargetPreparer {
35 
36     /** Placeholder to be replaced with real file path in commands */
37     private static final Pattern EXTRA_FILE_PATTERSTRING =
38             Pattern.compile("\\$EXTRA_FILE\\(([^()]+)\\)");
39 
40     private enum FastbootMode {
41         BOOTLOADER,
42         FASTBOOTD,
43     }
44 
45     @Option(
46             name = "fastboot-mode",
47             description =
48                     "'BOOTLOADER' to boot the device into bootloader mode, "
49                             + "'FASTBOOTD' for fastbootd mode.")
50     private FastbootMode mFastbootMode = FastbootMode.BOOTLOADER;
51 
52     @Option(
53             name = "stay-fastboot",
54             description =
55                     "True to keep the device in bootloader or fastbootd mode after the commands"
56                             + " executed.")
57     private boolean mStayFastboot = false;
58 
59     @Option(
60             name = "command",
61             description =
62                     "Fastboot commands to run in setup. Device will be rebooted after the commands"
63                             + " executed.")
64     private List<String> mFastbootCommands = new ArrayList<String>();
65 
66     @Option(
67             name = "teardown-command",
68             description =
69                     "Fastboot commands to run in teardown. Device will be rebooted after the"
70                             + " commands executed.")
71     private List<String> mFastbootTearDownCommands = new ArrayList<String>();
72 
73     @Override
setUp(TestInformation testInformation)74     public void setUp(TestInformation testInformation)
75             throws TargetSetupError, BuildError, DeviceNotAvailableException {
76         if (!mFastbootCommands.isEmpty()) {
77             final IBuildInfo buildInfo = testInformation.getBuildInfo();
78             final ITestDevice device = testInformation.getDevice();
79             enterFastboot(device);
80             replaceExtraFile(mFastbootCommands, buildInfo);
81             for (String cmd : mFastbootCommands) {
82                 final CommandResult result = device.executeFastbootCommand(cmd.split("\\s+"));
83                 if (result.getExitCode() != 0) {
84                     throw new TargetSetupError(
85                             String.format(
86                                     "Command %s failed, stdout = [%s], stderr = [%s].",
87                                     cmd, result.getStdout(), result.getStderr()),
88                             device.getDeviceDescriptor(),
89                             InfraErrorIdentifier.OPTION_CONFIGURATION_ERROR);
90                 }
91             }
92             exitFastboot(device);
93         }
94     }
95 
96     /** {@inheritDoc} */
97     @Override
tearDown(TestInformation testInformation, Throwable e)98     public void tearDown(TestInformation testInformation, Throwable e)
99             throws DeviceNotAvailableException {
100         if (!mFastbootTearDownCommands.isEmpty()) {
101             final ITestDevice device = testInformation.getDevice();
102             final IBuildInfo buildInfo = testInformation.getBuildInfo();
103             enterFastboot(device);
104             replaceExtraFile(mFastbootTearDownCommands, buildInfo);
105             for (String cmd : mFastbootTearDownCommands) {
106                 device.executeFastbootCommand(cmd.split("\\s+"));
107             }
108             exitFastboot(device);
109         }
110     }
111 
enterFastboot(ITestDevice device)112     private void enterFastboot(ITestDevice device) throws DeviceNotAvailableException {
113         if (mFastbootMode == FastbootMode.BOOTLOADER) {
114             device.rebootIntoBootloader();
115         } else {
116             device.rebootIntoFastbootd();
117         }
118     }
119 
exitFastboot(ITestDevice device)120     private void exitFastboot(ITestDevice device) throws DeviceNotAvailableException {
121         if (!mStayFastboot) {
122             device.reboot();
123         }
124     }
125 
126     /**
127      * For each command in the list, replace placeholder (if any) with the file name indicated in
128      * the build information
129      *
130      * @param commands list of host commands
131      * @param buildInfo build artifact information
132      */
replaceExtraFile(final List<String> commands, IBuildInfo buildInfo)133     private void replaceExtraFile(final List<String> commands, IBuildInfo buildInfo) {
134         for (int i = 0; i < commands.size(); i++) {
135             Matcher matcher = EXTRA_FILE_PATTERSTRING.matcher(commands.get(i));
136             StringBuffer command = new StringBuffer();
137 
138             while (matcher.find()) {
139                 String fileName = matcher.group(1);
140                 File file = buildInfo.getFile(fileName);
141                 if (file == null || !file.exists()) {
142                     continue;
143                 }
144                 matcher.appendReplacement(command, file.getPath());
145             }
146             matcher.appendTail(command);
147             commands.set(i, command.toString());
148         }
149     }
150 }