1 /*
2  * Copyright (C) 2020 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 package com.android.tradefed.util;
17 
18 import com.android.tradefed.build.BuildInfoKey;
19 import com.android.tradefed.build.BuildRetrievalError;
20 import com.android.tradefed.build.IBuildInfo;
21 import com.android.tradefed.build.IDeviceBuildInfo;
22 import com.android.tradefed.config.remote.ExtendedFile;
23 import com.android.tradefed.invoker.logger.CurrentInvocation;
24 import com.android.tradefed.log.LogUtil.CLog;
25 import com.android.tradefed.result.error.InfraErrorIdentifier;
26 import com.android.tradefed.targetprep.DeviceFlashPreparer;
27 import com.android.tradefed.targetprep.FlashingResourcesParser;
28 import com.android.tradefed.targetprep.IFlashingResourcesParser;
29 import com.android.tradefed.targetprep.TargetSetupError;
30 
31 import com.google.common.base.Strings;
32 
33 import java.io.File;
34 import java.util.Map;
35 
36 /**
37  * This utility helps setting the appropriate version of artifacts so they can be flashed via {@link
38  * DeviceFlashPreparer}.
39  */
40 public class FlashingResourceUtil {
41 
42     /**
43      * Handle the files needed for flashing and setting their appropriate value in the BuildInfo
44      *
45      * @param info The {IBuildInfo} constructed
46      * @param keyAndFiles The files downloaded considered
47      * @return True if flashing was setup
48      * @throws BuildRetrievalError
49      */
setUpFlashingResources(IBuildInfo info, Map<String, File> keyAndFiles)50     public static boolean setUpFlashingResources(IBuildInfo info, Map<String, File> keyAndFiles)
51             throws BuildRetrievalError {
52         IFlashingResourcesParser flashingResourcesParser = null;
53         String deviceImageFlavor = null;
54         String buildId = "downloaded";
55 
56         // Handle device image first
57         File deviceImage = keyAndFiles.get(BuildInfoKey.BuildInfoFileKey.DEVICE_IMAGE.getFileKey());
58         if (deviceImage == null) {
59             return false;
60         }
61         if (!(deviceImage instanceof ExtendedFile)) {
62             return false;
63         }
64         String bid = ((ExtendedFile) deviceImage).getBuildId();
65         if (!Strings.isNullOrEmpty(bid)) {
66             buildId = bid;
67         }
68         try {
69             flashingResourcesParser = new FlashingResourcesParser(deviceImage);
70             deviceImageFlavor = ((ExtendedFile) deviceImage).getBuildTarget();
71         } catch (TargetSetupError e) {
72             throw new BuildRetrievalError(
73                     "Failed to get image info from android-info.txt",
74                     e,
75                     InfraErrorIdentifier.ARTIFACT_DOWNLOAD_ERROR);
76         }
77         info.setFile(BuildInfoKey.BuildInfoFileKey.DEVICE_IMAGE, deviceImage, buildId);
78         if (CurrentInvocation.getInvocationFiles() != null) {
79             CurrentInvocation.getInvocationFiles()
80                     .put(BuildInfoKey.BuildInfoFileKey.DEVICE_IMAGE.getFileKey(), deviceImage);
81         }
82         keyAndFiles.remove(BuildInfoKey.BuildInfoFileKey.DEVICE_IMAGE.getFileKey());
83         // Handle bootloader
84         File bootloader =
85                 keyAndFiles.get(BuildInfoKey.BuildInfoFileKey.BOOTLOADER_IMAGE.getFileKey());
86         handleBootloader(info, bootloader, flashingResourcesParser);
87         keyAndFiles.remove(BuildInfoKey.BuildInfoFileKey.BOOTLOADER_IMAGE.getFileKey());
88 
89         // Handle baseband
90         File baseband = keyAndFiles.get(BuildInfoKey.BuildInfoFileKey.BASEBAND_IMAGE.getFileKey());
91         handleBaseband(info, baseband, flashingResourcesParser);
92         keyAndFiles.remove(BuildInfoKey.BuildInfoFileKey.BASEBAND_IMAGE.getFileKey());
93 
94         if (info instanceof IDeviceBuildInfo && deviceImageFlavor != null) {
95             ((IDeviceBuildInfo) info).setDeviceBuildFlavor(deviceImageFlavor);
96         }
97         return true;
98     }
99 
handleBaseband( IBuildInfo info, File baseband, IFlashingResourcesParser flashingResourcesParser)100     private static void handleBaseband(
101             IBuildInfo info, File baseband, IFlashingResourcesParser flashingResourcesParser) {
102         if (baseband == null) {
103             return;
104         }
105         String buildId = flashingResourcesParser.getRequiredBasebandVersion();
106         info.setFile(BuildInfoKey.BuildInfoFileKey.BASEBAND_IMAGE, baseband, buildId);
107         if (CurrentInvocation.getInvocationFiles() != null) {
108             CurrentInvocation.getInvocationFiles()
109                     .put(BuildInfoKey.BuildInfoFileKey.BASEBAND_IMAGE.getFileKey(), baseband);
110         }
111     }
112 
handleBootloader( IBuildInfo info, File bootloader, IFlashingResourcesParser flashingResourcesParser)113     private static void handleBootloader(
114             IBuildInfo info, File bootloader, IFlashingResourcesParser flashingResourcesParser) {
115         if (bootloader == null) {
116             return;
117         }
118         String buildId = flashingResourcesParser.getRequiredBootloaderVersion();
119         CLog.d("Bootloader from file: %s with version: %s", bootloader, buildId);
120         info.setFile(BuildInfoKey.BuildInfoFileKey.BOOTLOADER_IMAGE, bootloader, buildId);
121         if (CurrentInvocation.getInvocationFiles() != null) {
122             CurrentInvocation.getInvocationFiles()
123                     .put(BuildInfoKey.BuildInfoFileKey.BOOTLOADER_IMAGE.getFileKey(), bootloader);
124         }
125     }
126 }
127