1 /*
2  * Copyright (C) 2019 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.build.IDeviceBuildInfo;
21 import com.android.tradefed.config.Option;
22 import com.android.tradefed.device.DeviceNotAvailableException;
23 import com.android.tradefed.device.ITestDevice;
24 import com.android.tradefed.invoker.TestInformation;
25 import com.android.tradefed.targetprep.IDeviceFlasher.UserDataFlashOption;
26 import com.android.tradefed.util.BuildInfoUtil;
27 
28 import java.io.File;
29 
30 /**
31  * An {@link ITargetPreparer} that stages specified files (bootloader, radio, device image zip) into
32  * {@link IDeviceBuildInfo} to get devices flashed with {@link FastbootDeviceFlasher}, then injects
33  * post-boot device attributes into the build info for result reporting purposes.
34  *
35  * <p>This is useful for using <code>fastboot update</code> as device image update mechanism from
36  * externally sourced devices and builds, to fit into existing automation infrastructure.
37  */
38 public class FastbootUpdateBootstrapPreparer extends DeviceFlashPreparer {
39 
40     @Option(name = "bootloader-image", description = "bootloader image file to be used for update")
41     private File mBootloaderImage = null;
42 
43     @Option(name = "baseband-image", description = "radio image file to be used for update")
44     private File mBasebandImage = null;
45 
46     @Option(name = "device-image", description = "device image file to be used for update")
47     private File mDeviceImage = null;
48 
49     @Option(
50         name = "bootstrap-build-info",
51         description =
52                 "whether build info should be"
53                         + "bootstrapped based on device attributes after flashing"
54     )
55     private boolean mBootStrapBuildInfo = true;
56 
57     // parameters below are the same as DeviceBuildInfoBootStrapper
58     @Option(name = "override-device-build-id", description = "the device buid id to inject.")
59     private String mOverrideDeviceBuildId = null;
60 
61     @Option(name = "override-device-build-alias", description = "the device buid alias to inject.")
62     private String mOverrideDeviceBuildAlias = null;
63 
64     @Option(
65         name = "override-device-build-flavor",
66         description = "the device build flavor to inject."
67     )
68     private String mOverrideDeviceBuildFlavor = null;
69 
70     @Option(
71         name = "override-device-build-branch",
72         description = "the device build branch to inject."
73     )
74     private String mOverrideDeviceBuildBranch = null;
75 
76     @Override
setUp(TestInformation testInfo)77     public void setUp(TestInformation testInfo)
78             throws TargetSetupError, BuildError, DeviceNotAvailableException {
79         ITestDevice device = testInfo.getDevice();
80         IBuildInfo buildInfo = testInfo.getBuildInfo();
81         if (!(buildInfo instanceof IDeviceBuildInfo)) {
82             throw new IllegalArgumentException("Provided build info must be a IDeviceBuildInfo");
83         }
84         // forcing the wipe mechanism to WIPE because the FLASH* based options are not feasible here
85         setUserDataFlashOption(UserDataFlashOption.WIPE);
86         IDeviceBuildInfo deviceBuildInfo = (IDeviceBuildInfo) buildInfo;
87         deviceBuildInfo.setBootloaderImageFile(mBootloaderImage, "0");
88         deviceBuildInfo.setBasebandImage(mBasebandImage, "0");
89         deviceBuildInfo.setDeviceImageFile(mDeviceImage, "0");
90         setSkipPostFlashBuildIdCheck(true);
91         // performs the actual flashing
92         super.setUp(testInfo);
93 
94         if (mBootStrapBuildInfo) {
95             BuildInfoUtil.bootstrapDeviceBuildAttributes(
96                     buildInfo,
97                     device,
98                     mOverrideDeviceBuildId,
99                     mOverrideDeviceBuildFlavor,
100                     mOverrideDeviceBuildBranch,
101                     mOverrideDeviceBuildAlias);
102         }
103     }
104 
105     @Override
createFlasher(ITestDevice device)106     protected IDeviceFlasher createFlasher(ITestDevice device) throws DeviceNotAvailableException {
107         return new FastbootDeviceFlasher();
108     }
109 }
110