flasher implement(80%)

This commit is contained in:
dxl
2020-03-12 18:29:15 +08:00
parent a61096d30c
commit aa45f9b803
10 changed files with 93 additions and 87 deletions
@@ -3,9 +3,11 @@ package cn.rrg.rdv.activities.main;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.Nullable;
@@ -30,13 +32,12 @@ public class PM3FlasherMainActivity extends BaseActivity implements DevCallback<
private UsbSerialControl control = UsbSerialControl.get();
private Proxmark3Flasher flasher = Proxmark3Flasher.getFlasher();
private AlertDialog usbConnectWaitDialog;
//在程序执行读卡,写卡操作时的程序后台交互性提醒对话框
private AlertDialog mDialogWorkingState = null;
private TextView txtShowLog;
private Button btnFlash;
private volatile boolean startLabel = false;
private MODE mode = MODE.BOOT;
@Override
@@ -50,25 +51,9 @@ public class PM3FlasherMainActivity extends BaseActivity implements DevCallback<
initViews();
initActions();
if (!control.connect(null)) {
usbConnectWaitDialog.show();
}
}
private void initViews() {
usbConnectWaitDialog = new AlertDialog.Builder(context, R.style.CustomerDialogStyle)
.setView(View.inflate(context, R.layout.item_usb_device_connecting, null))
.create();
usbConnectWaitDialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
ImageView imgUsbConnect = usbConnectWaitDialog.findViewById(R.id.imgUsbConnect);
Glide.with(context).load(R.drawable.usb_connect).into(imgUsbConnect);
}
});
usbConnectWaitDialog.setCancelable(false);
mDialogWorkingState = new AlertDialog.Builder(context).create();
View _workingStateMsgView = View.inflate(context, R.layout.dialog_working_msg, null);
mDialogWorkingState.setView(_workingStateMsgView);
@@ -76,39 +61,34 @@ public class PM3FlasherMainActivity extends BaseActivity implements DevCallback<
mDialogWorkingState.setCancelable(false);
btnFlash = findViewById(R.id.btnFlash);
txtShowLog = findViewById(R.id.txtShowLog);
}
private void initActions() {
btnFlash.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
btnFlash.setEnabled(false);
startLabel = true;
if (!flasher.isPM3Opened()) {
if (!flasher.openProxmark3()) {
return;
}
}
mode = MODE.BOOT;
if (control.connect(null)) {
flashDefaultFW();
} else {
ToastUtil.show(context, getString(R.string.tips_device_no_found), false);
usbConnectWaitDialog.show();
}
}
});
}
private void showWorkingDialog() {
private void appendLog(String log) {
runOnUiThread(new Runnable() {
@Override
public void run() {
mDialogWorkingState.show();
}
});
}
private void dissmissWorkingDialog() {
runOnUiThread(new Runnable() {
@Override
public void run() {
mDialogWorkingState.dismiss();
txtShowLog.append("\n");
txtShowLog.append(log);
}
});
}
@@ -117,77 +97,74 @@ public class PM3FlasherMainActivity extends BaseActivity implements DevCallback<
new Thread(new Runnable() {
@Override
public void run() {
if (!flasher.isPM3Opened()) {
if (!flasher.openProxmark3()) {
LogUtils.d("PM3打开失败!");
return;
} else {
LogUtils.d("PM3打开成功!");
}
}
LogUtils.d("开始刷机!");
showWorkingDialog();
// 如果没有在Bootloader模式下,我们需要先重启设备使其进入Bootloader模式下!
appendLog("开始判断当前是否是BOOTROM模式");
if (flasher.isBootloaderMode()) {
LogUtils.d("当前处于Bootloader模式下,将会开始刷机!");
appendLog("当前是BOOTROM模式");
switch (mode) {
case BOOT:
if (flasher.flashBootRom(Paths.PM3_IMAGE_BOOT_FILE)) {
LogUtils.d("PM3刷写BootRom成功!");
// ToastUtil.show(context, getString(R.string.tips_boot_flash_success), false);
appendLog(getString(R.string.tips_boot_flash_success));
} else {
LogUtils.d("PM3刷写BootRom失败!");
// ToastUtil.show(context, getString(R.string.tips_boot_flash_failed), false);
appendLog(getString(R.string.tips_boot_flash_failed));
}
finishFlash();
// 开启下一轮!
mode = MODE.OS;
break;
case OS:
if (flasher.flashFullImg(Paths.PM3_IMAGE_OS_FILE)) {
LogUtils.d("PM3刷写BootRom成功!");
// ToastUtil.show(context, getString(R.string.tips_os_flash_success), false);
appendLog(getString(R.string.tips_os_flash_success));
} else {
LogUtils.d("PM3刷写BootRom失败!");
// ToastUtil.show(context, getString(R.string.tips_os_flash_failed), false);
appendLog(getString(R.string.tips_os_flash_failed));
}
finishFlash();
startLabel = false;
ToastUtil.show(context, getString(R.string.finish), false);
mode = MODE.BOOT;
break;
}
dissmissWorkingDialog();
} else {
appendLog("当前不是BOOTROM模式,将会重启设备进入BOOTROM模式");
if (!flasher.enterBootloader()) {
LogUtils.d("PM3进入刷写模式失败!");
appendLog("PM3进入刷写模式失败!");
}
}
dissmissWorkingDialog();
}
}).start();
}
private void finishFlash() {
flasher.flashModeClose();
flasher.closeProxmark3();
}
@Override
protected void onDestroy() {
super.onDestroy();
control.unregister(this);
usbConnectWaitDialog.dismiss();
flasher.closeProxmark3();
}
@Override
public void onAttach(String dev) {
// 自动连接设备!
control.connect(dev);
usbConnectWaitDialog.dismiss();
if (startLabel) {
flashDefaultFW();
}
flashDefaultFW();
}
@Override
public void onDetach(String dev) {
flasher.closeProxmark3();
usbConnectWaitDialog.show();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// LogUtils.d("X: " + event.getRawX());
// LogUtils.d("Y: " + event.getRawY());
return super.onTouchEvent(event);
}
}
@@ -15,11 +15,18 @@
android:layout_height="wrap_content"
android:src="@drawable/rdv4_2" />
<TextView
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:text="@string/tips_flash_pm3" />
android:padding="16dp">
<TextView
android:id="@+id/txtShowLog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone" />
</ScrollView>
</LinearLayout>
@@ -31,6 +38,7 @@
android:layout_centerHorizontal="true"
android:padding="16dp"
android:text="@string/flash"
android:textAllCaps="false"
android:textColor="@color/colorPrimary" />
</RelativeLayout>
@@ -272,5 +272,9 @@
<string name="tips_permission_request_failed">Some permission requests failed.</string>
<string name="tips_device_no_found">Device no found(Proxmark3 CDC)</string>
<string name="tips_flash_pm3">1. Firmware flash only support proxmark3 driver by usb-cdc\n \n 2. Only rdv4 is supported (RRG firmware supported by app is needed)\n \n 3. In order to be compatible, flasher need to write boot first and then OS\n \n 4. Please make sure the USB link is stable work during the process of writing\n \n 5. PM3 will be restarted repeatedly during the flash process. In case of usb permission request, please click allow.\n \n 6. This Activity will be automatically closed after all flash are completed</string>
<string name="tips_boot_flash_success">BootRom flash successful.</string>
<string name="tips_boot_flash_failed">BootRom flash failed.</string>
<string name="tips_os_flash_success">OS flash successful.</string>
<string name="tips_os_flash_failed">OS flash failed.</string>
</resources>
@@ -278,5 +278,9 @@
5、刷写过程中会反复重启PM3,如果出现授权申请请点击允许。\n
\n
6、全部刷写完成后将会自动关闭此页面\n</string>
<string name="tips_boot_flash_success">引导器刷写成功.</string>
<string name="tips_boot_flash_failed">引导器刷写失败.</string>
<string name="tips_os_flash_success">操作系统刷写成功</string>
<string name="tips_os_flash_failed">操作系统刷写失败</string>
</resources>
+4
View File
@@ -271,5 +271,9 @@
<string name="tips_permission_request_failed">Some permission requests failed.</string>
<string name="tips_device_no_found">Device no found(Proxmark3 CDC)</string>
<string name="tips_flash_pm3">1. Firmware flash only support proxmark3 driver by usb-cdc\n \n 2. Only rdv4 is supported (RRG firmware supported by app is needed)\n \n 3. In order to be compatible, flasher need to write boot first and then OS\n \n 4. Please make sure the USB link is stable work during the process of writing\n \n 5. PM3 will be restarted repeatedly during the flash process. In case of usb permission request, please click allow.\n \n 6. This Activity will be automatically closed after all flash are completed</string>
<string name="tips_boot_flash_success">BootRom flash successful.</string>
<string name="tips_boot_flash_failed">BootRom flash failed.</string>
<string name="tips_os_flash_success">OS flash successful.</string>
<string name="tips_os_flash_failed">OS flash failed.</string>
</resources>
@@ -75,6 +75,7 @@ public final class Com implements Serializable {
* @throws IOException IOException in write error.
*/
private static int write() throws IOException {
if (mCommunication == null) return -1;
//加上偏移值
//try {
return mCommunication.write(send_buffer.array(),
@@ -92,6 +93,7 @@ public final class Com implements Serializable {
* @throws IOException IOException in read error.
*/
private static int read() throws IOException {
if (mCommunication == null) return -1;
//try {
return mCommunication.read(recv_buffer.array(),
recv_buffer.arrayOffset(), syncLength() +
@@ -105,6 +107,7 @@ public final class Com implements Serializable {
* flush data, clear buffer!
*/
private static void flush() throws IOException {
if (mCommunication == null) return;
//try {
mCommunication.flush();
//} catch (IOException e) {
@@ -114,7 +117,7 @@ public final class Com implements Serializable {
/**
* close devices, clear buffer and break communication!
* TODO don't need close, because some communication can't close() in c layer.
* TODO don't close, because some communication can't close() in c layer.
*/
private static void close() throws IOException {
//try {
@@ -43,7 +43,7 @@ public class UsbSerialControl implements DriverInterface<String, UsbManager> {
//设备名称!
public static final String NAME_DRIVER_USB_UART = "OTGToUartSerial(OTG转串口)";
//串口对象
private static UsbSerialDevice mPort = null;
private volatile UsbSerialDevice mPort = null;
//单例模式
private static UsbSerialControl mThiz = null;
//回调接口
@@ -109,9 +109,7 @@ public class UsbSerialControl implements DriverInterface<String, UsbManager> {
}
}
//回调设备移除接口
if (mPort != null)
if (mCallback != null)
mCallback.onDetach(NAME_DRIVER_USB_UART);
if (mCallback != null) mCallback.onDetach(NAME_DRIVER_USB_UART);
}
}
}
@@ -259,7 +257,7 @@ public class UsbSerialControl implements DriverInterface<String, UsbManager> {
return;
}
mPort.close();
mPort = null;
}
@Override
@@ -291,8 +289,7 @@ public class UsbSerialControl implements DriverInterface<String, UsbManager> {
private boolean connect1(String addr) {
if (mPort != null) {
mPort.close();
mPort = null;
return true;
}
//得到Usb管理器
UsbManager usbManager = (UsbManager) mContext.getSystemService(Context.USB_SERVICE);
@@ -358,7 +355,7 @@ public class UsbSerialControl implements DriverInterface<String, UsbManager> {
@Override
public String getDevice() {
return mPort != null ? mPort.getClass().getSimpleName() : null;
return mPort != null ? mPort.getClass().getSimpleName() : NAME_DRIVER_USB_UART;
}
@Override
+8 -10
View File
@@ -348,10 +348,6 @@ __attribute__((force_align_arg_pointer))
PacketResponseNG rx;
PacketResponseNGRaw rx_raw;
#if defined(__MACH__) && defined(__APPLE__)
disableAppNap("Proxmark3 polling UART");
#endif
// is this connection->run a cross thread call?
while (connection->run) {
rxlen = 0;
@@ -556,10 +552,6 @@ __attribute__((force_align_arg_pointer))
uart_close(sp);
sp = NULL;
#if defined(__MACH__) && defined(__APPLE__)
enableAppNap();
#endif
pthread_exit(NULL);
return NULL;
}
@@ -693,21 +685,27 @@ int TestProxmark(void) {
void CloseProxmark(void) {
conn.run = false;
LOGD("尝试释放资源!");
#ifdef __BIONIC__
if (communication_thread != 0) {
pthread_join(communication_thread, NULL);
}
#else
pthread_join(communication_thread, NULL);
pthread_join(communication_thread, NULL);
#endif
LOGD("释放线程资源完成!");
if (sp) {
uart_close(sp);
}
// Clean up our state
sp = NULL;
memset(&communication_thread, 0, sizeof(pthread_t));
if (communication_thread != 0) {
memset(&communication_thread, 0, sizeof(pthread_t));
}
session.pm3_present = false;
}
+9 -6
View File
@@ -325,7 +325,7 @@ static int get_proxmark_state(uint32_t *state) {
SendCommandBL(CMD_DEVICE_INFO, 0, 0, 0, NULL, 0);
PacketResponseNG resp;
// WaitForResponse(CMD_UNKNOWN, &resp); // wait for any response. No timeout.
WaitForResponseTimeout(CMD_UNKNOWN, &resp, 1000);
WaitForResponseTimeout(CMD_UNKNOWN, &resp, 3000);
// Three outcomes:
// 1. The old bootrom code will ignore CMD_DEVICE_INFO, but respond with an ACK
@@ -346,6 +346,7 @@ static int get_proxmark_state(uint32_t *state) {
return PM3_EFATAL;
}
LOGD("get_proxmark_state() Current state%d", *state);
if (*state == 0) return get_proxmark_state(state);
return PM3_SUCCESS;
}
@@ -448,7 +449,9 @@ void flash_free(flash_file_t *ctx) {
// just reset the unit
int flash_stop_flashing(void) {
SendCommandBL(CMD_HARDWARE_RESET, 0, 0, 0, NULL, 0);
if (conn.run) {
SendCommandBL(CMD_HARDWARE_RESET, 0, 0, 0, NULL, 0);
}
return PM3_SUCCESS;
}
@@ -577,7 +580,7 @@ jboolean flash_implement(JNIEnv *env, jobject thiz, jstring file, jboolean is_bo
jclass iae_clz = (*env)->FindClass(env, "java/lang/IllegalArgumentException");
if (!conn.run) {
(*env)->ThrowNew(env, iae_clz, "The pm3 client is closed, please open first.");
return false;
goto finish;
}
const char *file_path = (*env)->GetStringUTFChars(env, file, false);
@@ -620,10 +623,9 @@ jboolean flash_implement(JNIEnv *env, jobject thiz, jstring file, jboolean is_bo
void close_proxmkar3(JNIEnv *env, jobject thiz) {
if (conn.run) {
clearCommandBuffer();
CloseProxmark();
deatchThread();
}
LOGD("关闭完成.");
}
jboolean enter_bootloader_mode(JNIEnv *env, jobject thiz) {
@@ -672,8 +674,9 @@ jboolean open_proxmark3(JNIEnv *env, jobject thiz) {
if (conn.run) {
LOGD("设备已经是打开状态,将会跳过打开!");
return true;
} else {
LOGD("开启完成.");
}
clearCommandBuffer();
return (jboolean) OpenProxmark("dxl", false, 233, true, FLASHMODE_SPEED);
}
@@ -24,6 +24,14 @@ public class Proxmark3Flasher {
private static Proxmark3Flasher flasher;
private static String LOG = "Proxmark3Flasher";
/**
* 警告
* 使用刷写器的时候不要频繁关闭Client
* openProxmark3() 请在Activity -> onCreate() 中执行
* closeProxmark3() 请求Activity -> onDestroy() 中执行
* 频繁关闭客户端将会导致JNI Crash
* */
static {
//在静态块加载对应的模块
System.loadLibrary("pm3_flasher");