From a140cfda112e0a480e75ed6c4af4759da507214b Mon Sep 17 00:00:00 2001 From: jdesprez Date: Tue, 27 Jun 2017 16:05:23 -0700 Subject: Add a check before flashing that device is matching build Ensure that the device and the build info presented are compatible and flashing could be attempted. Test: unit tests ./tradefed.sh run google/util/flash --branch git_master --build-flavor marlin-userdebug (against sailfish device) Bug: 63041448 Change-Id: Icd6d7d66fad7e7edd68b4795533221a94ed66d29 --- .../tradefed/targetprep/DeviceFlashPreparer.java | 34 ++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'src') diff --git a/src/com/android/tradefed/targetprep/DeviceFlashPreparer.java b/src/com/android/tradefed/targetprep/DeviceFlashPreparer.java index 96f4569ec..99a47fd8b 100644 --- a/src/com/android/tradefed/targetprep/DeviceFlashPreparer.java +++ b/src/com/android/tradefed/targetprep/DeviceFlashPreparer.java @@ -30,6 +30,8 @@ import com.android.tradefed.targetprep.IDeviceFlasher.UserDataFlashOption; import com.android.tradefed.util.IRunUtil; import com.android.tradefed.util.RunUtil; +import com.google.common.annotations.VisibleForTesting; + import java.util.ArrayList; import java.util.Collection; import java.util.concurrent.Semaphore; @@ -62,6 +64,12 @@ public abstract class DeviceFlashPreparer implements ITargetCleaner { "specify if system should always be flashed even if already running desired build.") private boolean mForceSystemFlash = false; + @Option( + name = "skip-pre-flash-product-check", + description = "Specify if device product type should be checked before flashing" + ) + private boolean mSkipPreFlashProductType = false; + /* * A temporary workaround for special builds. Should be removed after changes from build team. * Bug: 18078421 @@ -247,6 +255,7 @@ public abstract class DeviceFlashPreparer implements ITargetCleaner { getRunUtil().allowInterrupt(false); try { IDeviceBuildInfo deviceBuild = (IDeviceBuildInfo)buildInfo; + checkDeviceProductType(device, deviceBuild); device.setRecoveryMode(RecoveryMode.ONLINE); IDeviceFlasher flasher = createFlasher(device); flasher.setWipeTimeout(mWipeTimeout); @@ -302,6 +311,31 @@ public abstract class DeviceFlashPreparer implements ITargetCleaner { } } + /** Check the device allocated against the build to ensure this is compatible to be flashed. */ + @VisibleForTesting + void checkDeviceProductType(ITestDevice device, IDeviceBuildInfo deviceBuild) + throws BuildError, DeviceNotAvailableException { + if (mSkipPreFlashProductType) { + CLog.d("Skipping pre flash device product check."); + return; + } + String buildFlavor = deviceBuild.getBuildFlavor(); + String deviceTypeFromBuild = buildFlavor; + // handle a case where build flavor format is not really quite as expected. + // <'device board'>- + if (buildFlavor.indexOf("-") != -1) { + deviceTypeFromBuild = buildFlavor.substring(0, buildFlavor.indexOf("-")); + } + String deviceProduct = device.getProductType(); // ro.hardware of the device + if (!deviceTypeFromBuild.equals(deviceProduct)) { + throw new BuildError( + String.format( + "Device allocated is a '%s' while build is meant for a '%s'", + deviceProduct, deviceTypeFromBuild), + device.getDeviceDescriptor()); + } + } + /** * Verifies the expected build matches the actual build on device after flashing * @throws DeviceNotAvailableException -- cgit v1.2.3