1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-24 10:56:50 +01:00

Improved PineTime/InfiniTime firmware DFU metadata parsing and checks

This commit is contained in:
TaaviE 2021-05-03 00:07:20 +03:00
parent da997dc3c0
commit e4d70ade2a
4 changed files with 106 additions and 23 deletions

View File

@ -78,6 +78,7 @@ dependencies {
implementation "androidx.palette:palette:1.0.0" implementation "androidx.palette:palette:1.0.0"
implementation "com.google.android.material:material:1.3.0" implementation "com.google.android.material:material:1.3.0"
implementation "com.google.code.gson:gson:2.8.6"
implementation "no.nordicsemi.android:dfu:1.11.1" implementation "no.nordicsemi.android:dfu:1.11.1"
implementation("com.github.tony19:logback-android-classic:1.1.1-6") { implementation("com.github.tony19:logback-android-classic:1.1.1-6") {

View File

@ -0,0 +1,43 @@
/* Copyright (C) 2021 Taavi Eomäe
This file is part of Gadgetbridge.
Gadgetbridge is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Gadgetbridge is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.devices.pinetime;
import java.math.BigInteger;
import java.util.List;
public class InfiniTimeDFUPackage {
InfiniTimeDFUPackageManifest manifest;
}
class InfiniTimeDFUPackageManifest {
InfiniTimeDFUPackageApplication application;
Float dfu_version;
}
class InfiniTimeDFUPackageApplication {
String bin_file;
String dat_file;
InfiniTimeDFUPackagePacketData init_packet_data;
}
class InfiniTimeDFUPackagePacketData {
BigInteger application_version;
BigInteger device_revision;
BigInteger device_type;
BigInteger firmware_crc16;
List<Integer> softdevice_req;
}

View File

@ -17,17 +17,18 @@
package nodomain.freeyourgadget.gadgetbridge.devices.pinetime; package nodomain.freeyourgadget.gadgetbridge.devices.pinetime;
import android.content.Context; import android.content.Context;
import android.content.Intent;
import android.net.Uri; import android.net.Uri;
import android.os.Build;
import androidx.localbroadcastmanager.content.LocalBroadcastManager; import com.google.gson.Gson;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.io.BufferedInputStream; import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import nodomain.freeyourgadget.gadgetbridge.R; import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.activities.InstallActivity; import nodomain.freeyourgadget.gadgetbridge.activities.InstallActivity;
@ -35,9 +36,10 @@ import nodomain.freeyourgadget.gadgetbridge.devices.InstallHandler;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.model.DeviceType; import nodomain.freeyourgadget.gadgetbridge.model.DeviceType;
import nodomain.freeyourgadget.gadgetbridge.model.GenericItem; import nodomain.freeyourgadget.gadgetbridge.model.GenericItem;
import nodomain.freeyourgadget.gadgetbridge.util.GB;
import nodomain.freeyourgadget.gadgetbridge.util.UriHelper; import nodomain.freeyourgadget.gadgetbridge.util.UriHelper;
import static java.nio.charset.StandardCharsets.UTF_8;
public class PineTimeInstallHandler implements InstallHandler { public class PineTimeInstallHandler implements InstallHandler {
private static final Logger LOG = LoggerFactory.getLogger(PineTimeInstallHandler.class); private static final Logger LOG = LoggerFactory.getLogger(PineTimeInstallHandler.class);
@ -47,49 +49,86 @@ public class PineTimeInstallHandler implements InstallHandler {
public PineTimeInstallHandler(Uri uri, Context context) { public PineTimeInstallHandler(Uri uri, Context context) {
this.context = context; this.context = context;
UriHelper uriHelper; UriHelper uriHelper;
InputStream inputStream;
ZipInputStream zipInputStream;
InfiniTimeDFUPackage metadata = null;
try { try {
uriHelper = UriHelper.get(uri, this.context); uriHelper = UriHelper.get(uri, this.context);
} catch (IOException e) { inputStream = new BufferedInputStream(uriHelper.openInputStream());
valid = false; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
return; zipInputStream = new ZipInputStream(inputStream, UTF_8);
} } else {
zipInputStream = new ZipInputStream(inputStream);
try (InputStream in = new BufferedInputStream(uriHelper.openInputStream())) {
byte[] bytes = new byte[32];
int read = in.read(bytes);
if (read < 32) {
valid = false;
return;
} }
ZipEntry entry;
while ((entry = zipInputStream.getNextEntry()) != null) {
if (entry.isDirectory()) {
continue;
}
if (entry.getName().equals("manifest.json")) {
LOG.debug("Found manifest.json in DFU zip");
StringBuilder json = new StringBuilder();
final byte[] buffer = new byte[1024];
while (zipInputStream.read(buffer, 0, buffer.length) != -1) {
json.append(new String(buffer));
}
Gson gson = new Gson();
metadata = gson.fromJson(json.toString().trim(), InfiniTimeDFUPackage.class);
continue;
}
}
zipInputStream.close();
inputStream.close();
} catch (Exception e) { } catch (Exception e) {
valid = false; valid = false;
return; return;
} }
valid = true;
if (metadata != null) {
valid = true;
version = metadata.manifest.application.bin_file;
}
} }
@Override @Override
public void validateInstallation(InstallActivity installActivity, GBDevice device) { public void validateInstallation(InstallActivity installActivity, GBDevice device) {
installActivity.setInstallEnabled(true);
if (device.isBusy()) { if (device.isBusy()) {
LOG.error("Firmware cannot be installed (device busy)");
installActivity.setInfoText("Firmware cannot be installed (device busy)");
installActivity.setInfoText(device.getBusyTask()); installActivity.setInfoText(device.getBusyTask());
installActivity.setInstallEnabled(false); installActivity.setInstallEnabled(false);
return; return;
} }
if (device.getType() != DeviceType.PINETIME_JF || !device.isConnected()) { if (device.getType() != DeviceType.PINETIME_JF || !device.isConnected()) {
installActivity.setInfoText("Firmware cannot be installed"); LOG.error("Firmware cannot be installed (not connected or wrong device)");
installActivity.setInfoText("Firmware cannot be installed (not connected or wrong device)");
installActivity.setInstallEnabled(false); installActivity.setInstallEnabled(false);
return; return;
} }
if (!valid) {
LOG.error("Firmware cannot be installed (not valid)");
installActivity.setInfoText("Firmware cannot be installed (not valid)");
installActivity.setInstallEnabled(false);
}
GenericItem installItem = new GenericItem(); GenericItem installItem = new GenericItem();
installItem.setIcon(R.drawable.ic_firmware); installItem.setIcon(R.drawable.ic_firmware);
installItem.setName("PineTime firmware"); installItem.setName("PineTime firmware");
installItem.setDetails(version); installItem.setDetails(version);
installActivity.setInfoText(context.getString(R.string.firmware_install_warning, "(unknown)")); installActivity.setInfoText(context.getString(R.string.firmware_install_warning, "(unknown)"));
installActivity.setInstallEnabled(true);
installActivity.setInstallItem(installItem); installActivity.setInstallItem(installItem);
LOG.debug("Initialized PineTimeInstallHandler"); LOG.debug("Initialized PineTimeInstallHandler");
} }

View File

@ -326,8 +326,8 @@ public class PineTimeJFSupport extends AbstractBTLEDeviceSupport implements DfuL
try { try {
handler = new PineTimeInstallHandler(uri, getContext()); handler = new PineTimeInstallHandler(uri, getContext());
// TODO: Check validity more closely if (handler.isValid()) {
if (true) { gbDevice.setBusyTask("firmware upgrade");
DfuServiceInitiator starter = new DfuServiceInitiator(getDevice().getAddress()) DfuServiceInitiator starter = new DfuServiceInitiator(getDevice().getAddress())
.setDeviceName(getDevice().getName()) .setDeviceName(getDevice().getName())
.setKeepBond(true) .setKeepBond(true)
@ -346,13 +346,13 @@ public class PineTimeJFSupport extends AbstractBTLEDeviceSupport implements DfuL
LocalBroadcastManager.getInstance(getContext()).sendBroadcast(new Intent(GB.ACTION_SET_PROGRESS_TEXT) LocalBroadcastManager.getInstance(getContext()).sendBroadcast(new Intent(GB.ACTION_SET_PROGRESS_TEXT)
.putExtra(GB.DISPLAY_MESSAGE_MESSAGE, getContext().getString(R.string.devicestatus_upload_starting)) .putExtra(GB.DISPLAY_MESSAGE_MESSAGE, getContext().getString(R.string.devicestatus_upload_starting))
); );
gbDevice.setBusyTask("firmware upgrade");
} else { } else {
// TODO: Handle invalid firmware files LocalBroadcastManager.getInstance(getContext()).sendBroadcast(new Intent(GB.ACTION_SET_PROGRESS_TEXT)
.putExtra(GB.DISPLAY_MESSAGE_MESSAGE, getContext().getString(R.string.fwinstaller_firmware_not_compatible_to_device)));
} }
} catch (Exception ex) { } catch (Exception ex) {
GB.toast(getContext(), getContext().getString(R.string.updatefirmwareoperation_write_failed) + ":" + ex.getMessage(), Toast.LENGTH_LONG, GB.ERROR, ex); GB.toast(getContext(), getContext().getString(R.string.updatefirmwareoperation_write_failed) + ":" + ex.getMessage(), Toast.LENGTH_LONG, GB.ERROR, ex);
if (gbDevice.isBusy()) { if (gbDevice.isBusy() && gbDevice.getBusyTask().equals("firmware upgrade")) {
gbDevice.unsetBusyTask(); gbDevice.unsetBusyTask();
} }
} }