]> git.zerfleddert.de Git - proxmark3-svn/blobdiff - client/flash.c
Comms refactor (prerequisite of libproxmark work) (#371)
[proxmark3-svn] / client / flash.c
index 6670d637dca187bff1c7944961dbe20101198620..bf01b911f116b7d2a89cfd2a792f43dcfdde5af7 100644 (file)
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
-#include "sleep.h"
-#include "proxusb.h"
+#include <inttypes.h>
+#include <unistd.h>
+
+#include "proxmark3.h"
+#include "util.h"
+#include "util_posix.h"
 #include "flash.h"
 #include "elf.h"
 #include "proxendian.h"
+#include "usb_cmd.h"
+#include "comms.h"
 
 // FIXME: what the fuckity fuck
 unsigned int current_command = CMD_UNKNOWN;
@@ -26,7 +32,7 @@ unsigned int current_command = CMD_UNKNOWN;
 #define BOOTLOADER_SIZE        0x2000
 #define BOOTLOADER_END         (FLASH_START + BOOTLOADER_SIZE)
 
-#define BLOCK_SIZE             0x100
+#define BLOCK_SIZE             0x200
 
 static const uint8_t elf_ident[] = {
        0x7f, 'E', 'L', 'F',
@@ -35,6 +41,32 @@ static const uint8_t elf_ident[] = {
        EV_CURRENT
 };
 
+void CloseProxmark(receiver_arg* conn, char* serial_port_name) {
+       pthread_mutex_lock(&conn->recv_lock);
+
+       // Block the port from being used by anything
+       serial_port* my_port = GetSerialPort();
+       SetSerialPort(NULL);
+
+       // Then close the port.
+       uart_close(my_port);
+       pthread_mutex_unlock(&conn->recv_lock);
+
+       // Fix for linux, it seems that it is extremely slow to release the serial port file descriptor /dev/*
+       unlink(serial_port_name);
+}
+
+bool OpenProxmark(char* serial_port_name) {
+       serial_port *new_port = uart_open(serial_port_name);
+       if (new_port == INVALID_SERIAL_PORT || new_port == CLAIMED_SERIAL_PORT) {
+               //poll once a second
+               return false;
+       }
+       
+       SetSerialPort(new_port);
+       return true;
+}
+
 // Turn PHDRs into flasher segments, checking for PHDR sanity and merging adjacent
 // unaligned segments if needed
 static int build_segs_from_phdrs(flash_file_t *ctx, FILE *fd, Elf32_Phdr *phdrs, int num_phdrs)
@@ -270,9 +302,11 @@ static int get_proxmark_state(uint32_t *state)
        UsbCommand c;
        c.cmd = CMD_DEVICE_INFO;
        SendCommand(&c);
-
        UsbCommand resp;
-       ReceiveCommand(&resp);
+       while (!WaitForResponse(CMD_ANY, &resp)) {
+               // Keep waiting for a response
+               msleep(100);
+       }
 
        // Three outcomes:
        // 1. The old bootrom code will ignore CMD_DEVICE_INFO, but respond with an ACK
@@ -290,7 +324,7 @@ static int get_proxmark_state(uint32_t *state)
                        *state = resp.arg[0];
                        break;
                default:
-                       fprintf(stderr, "Error: Couldn't get proxmark state, bad response type: 0x%04x\n", resp.cmd);
+                       fprintf(stderr, "Error: Couldn't get proxmark state, bad response type: 0x%04" PRIx64 "\n", resp.cmd);
                        return -1;
                        break;
        }
@@ -299,7 +333,7 @@ static int get_proxmark_state(uint32_t *state)
 }
 
 // Enter the bootloader to be able to start flashing
-static int enter_bootloader(void)
+static int enter_bootloader(receiver_arg* conn, char *serial_port_name)
 {
        uint32_t state;
 
@@ -330,16 +364,17 @@ static int enter_bootloader(void)
                        SendCommand(&c);
                        fprintf(stderr,"Press and hold down button NOW if your bootloader requires it.\n");
                }
-               fprintf(stderr,"Waiting for Proxmark to reappear on USB...");
+               
+               msleep(100);
+               CloseProxmark(conn, serial_port_name);
 
-               CloseProxmark();
-               sleep(1);
-               while (!OpenProxmark(0)) {
+               fprintf(stderr,"Waiting for Proxmark to reappear on %s",serial_port_name);
+               do {
                        sleep(1);
                        fprintf(stderr, ".");
-               }
+               } while (!OpenProxmark(serial_port_name));
+               
                fprintf(stderr," Found.\n");
-
                return 0;
        }
 
@@ -347,23 +382,25 @@ static int enter_bootloader(void)
        return -1;
 }
 
-static int wait_for_ack(void)
+static int wait_for_ack()
 {
-       UsbCommand ack;
-       ReceiveCommand(&ack);
-       if (ack.cmd != CMD_ACK) {
-               printf("Error: Unexpected reply 0x%04x (expected ACK)\n", ack.cmd);
+       UsbCommand resp;
+       while (!WaitForResponse(CMD_ANY, &resp)) {
+               msleep(100);
+       }
+       if (resp.cmd != CMD_ACK) {
+               printf("Error: Unexpected reply 0x%04" PRIx64 " (expected ACK)\n", resp.cmd);
                return -1;
        }
        return 0;
 }
 
 // Go into flashing mode
-int flash_start_flashing(int enable_bl_writes)
+int flash_start_flashing(receiver_arg* conn, int enable_bl_writes,char *serial_port_name)
 {
        uint32_t state;
 
-       if (enter_bootloader() < 0)
+       if (enter_bootloader(conn, serial_port_name) < 0)
                return -1;
 
        if (get_proxmark_state(&state) < 0)
@@ -399,21 +436,12 @@ static int write_block(uint32_t address, uint8_t *data, uint32_t length)
 
        memset(block_buf, 0xFF, BLOCK_SIZE);
        memcpy(block_buf, data, length);
-
-       UsbCommand c = {CMD_SETUP_WRITE};
-       for (int i = 0; i < 240; i += 48) {
-               memcpy(c.d.asBytes, block_buf + i, 48);
-               c.arg[0] = i / 4;
-               SendCommand(&c);
-               if (wait_for_ack() < 0)
-                       return -1;
-       }
-
+  UsbCommand c;
        c.cmd = CMD_FINISH_WRITE;
        c.arg[0] = address;
-       memcpy(c.d.asBytes, block_buf+240, 16);
-       SendCommand(&c);
-       return wait_for_ack();
+       memcpy(c.d.asBytes, block_buf, length);
+  SendCommand(&c);
+  return wait_for_ack();
 }
 
 // Write a file's segments to Flash
@@ -471,8 +499,9 @@ void flash_free(flash_file_t *ctx)
 }
 
 // just reset the unit
-int flash_stop_flashing(void) {
+int flash_stop_flashing() {
        UsbCommand c = {CMD_HARDWARE_RESET};
        SendCommand(&c);
+       msleep(100);
        return 0;
 }
Impressum, Datenschutz