Пример #1
0
//! \brief Sends a message of 5 bytes to the USBDM device over EP0.
//!
//!  An immediate response is expected
//!
//! @param data - Entry \n
//!    data[0]    = N, the number of bytes to receive from the device \n
//!    data[1]    = Command byte \n
//!    data[2..5] = parameter(s) for OSBDM command \n
//! @note data must be an array of at least 5 bytes even if there are no parameters!
//!
//! @param data - Exit \n
//!    data[0]      = cmd response from OSBDM\n
//!    data[1..N-1] = data response from the device (cleared on error)\n
//!
//! @return \n
//!    == BDM_RC_OK (0)     => Success, OK response from device\n
//!    == BDM_RC_USB_ERROR  => USB failure \n
//!    == else              => Error code from Device
//!
USBDM_ErrorCode bdm_usb_recv_ep0(unsigned char *data, unsigned *actualRxSize) {
    unsigned char size = data[0];   // Transfer size is the first byte
    unsigned char cmd  = data[1];   // OSBDM/TBDML Command byte
    int rc;
    int retry = 5;

    *actualRxSize = 0;

    if (usbDeviceHandle == NULL) {
        print("bdm_usb_recv_ep0() - ERROR : Device handle NULL!\n");
        data[0] = BDM_RC_DEVICE_NOT_OPEN;
        return BDM_RC_DEVICE_NOT_OPEN;
    }

#ifdef LOG_LOW_LEVEL
    print("============================\n");
    print("bdm_usb_recv_ep0(%s, size=%d) - \n", getCommandName(cmd), size);
    if (data[1] == CMD_USBDM_DEBUG)
        print("bdm_usb_recv_ep0() - Debug cmd = %s\n", getDebugCommandName(data[2]));
    printDump(data, 6);
#endif // LOG_LOW_LEVEL

    do {
        rc = libusb_control_transfer(usbDeviceHandle,
                                     LIBUSB_REQUEST_TYPE_VENDOR|EP_CONTROL_IN,      // bmRequestType
                                     cmd,                                           // bRequest
                                     data[2]+(data[3]<<8),                          // wValue
                                     data[4]+(data[5]<<8),                          // wIndex
                                     (unsigned char*)data,                          // ptr to data buffer (for Rx)
                                     size,                                          // wLength = size of transfer
                                     5*timeoutValue                                 // timeout
                                    );
        if (rc < 0) {
            print("bdm_usb_recv_ep0(size=%d) - Transfer error (USB error = %s) - retry %d \n", size, libusb_strerror((libusb_error)rc), retry);
            milliSleep(100); // So we don't monopolise the USB
        }
    } while ((rc < 0) && (--retry>0));

    if (rc < 0) {
        print("bdm_usb_recv_ep0() - Transfer failed (USB error = %s)\n", libusb_strerror((libusb_error)rc));
        data[0] = BDM_RC_USB_ERROR;
        *actualRxSize = 0;
    }
    else {
        *actualRxSize = (unsigned) rc;
    }
    if ((data[0] != BDM_RC_OK) && (data[0] != cmd)) { // Error at BDM?
        print("bdm_usb_recv_ep0() - Cmd Failed (%s):\n", getErrorName(data[0]));
        printDump(data,*actualRxSize);
        memset(&data[1], 0x00, size-1);
        return (USBDM_ErrorCode)data[0];
    }
#ifdef LOG_LOW_LEVEL
    print("bdm_usb_recv_ep0(size = %d, recvd = %d):\n", size, rc);
    printDump(data,rc);
#endif // LOG_LOW_LEVEL

    return(BDM_RC_OK);
}
Пример #2
0
/// The exception filter that will display the stack
static LONG WINAPI UnhandledExpFilter(PEXCEPTION_POINTERS pExceptionInfo)
{
    /// Handle to the process
    HANDLE hProcess;
    /// List of the loaded modules
    std::list<std::string> loadedModules;
    /// List of the elements on the call stack
    std::list<std::string> callStack;
    /// List of the elements on the file stack
    std::list<std::string> fileStack;

    SymSetOptions(SYMOPT_UNDNAME|SYMOPT_DEFERRED_LOADS|SYMOPT_LOAD_LINES);
    hProcess = GetCurrentProcess();

    if(SymInitialize(hProcess, NULL, TRUE))
    {
        LoadCallStack(pExceptionInfo, hProcess, callStack, fileStack);
        ::EnumerateLoadedModules64(hProcess, EnumerateLoadedModules, &loadedModules);
        ::SymCleanup(hProcess);
    }

    printDump(loadedModules, callStack, fileStack);

    return EXCEPTION_CONTINUE_SEARCH;
}
Пример #3
0
//! \brief Sends a command to the TBDML device over the Out Bulk Endpoint
//!
//! Since the EP transfer is unidirectional, data returned by the
//! device must be read in a separate transfer - this includes any status
//! from extended command execution.
//!
//! @param count = # of bytes to Tx (N)
//!
//! @param data                                 \n
//!    OUT                                      \n
//!    =======================================  \n
//!    data[0]      = reserved                  \n
//!    data[1]      = command byte              \n
//!    data[2..N-1] = parameters                \n
//!                                             \n
//!    IN                                       \n
//!    ======================================== \n
//!    data[0]      = error code (=rc)
//!
//! @return
//!   BDM_RC_OK        => USB transfer OK \n
//!   BDM_RC_USB_ERROR => USB transfer Failed
//!
USBDM_ErrorCode bdm_usb_send_epOut(unsigned int count, const unsigned char *data) {
    int rc;
    int transferCount;

    if (usbDeviceHandle==NULL) {
        print("bdm_usb_send_epOut(): device not open\n");
        return BDM_RC_DEVICE_NOT_OPEN;
    }

#ifdef LOG_LOW_LEVEL
    print("============================\n");
    if (data[0] == 0) { // Zero as first byte indicates split USBDM command
        print("bdm_usb_send_epOut() - USB EP0ut send (SPLIT, size=%d):\n", count);
    }
    else {
        print("bdm_usb_send_epOut() - USB EP0ut send (%s, size=%d):\n", getCommandName(data[1]), count);
        if (data[1] == CMD_USBDM_DEBUG)
            print("bdm_usb_send_epOut() - Debug cmd = %s\n", getDebugCommandName(data[2]));
    }
    printDump(data, count);
#endif // LOG_LOW_LEVEL

    rc = libusb_bulk_transfer(usbDeviceHandle,
                              EP_OUT,                  // Endpoint & direction
                              (unsigned char *)data,   // ptr to Tx data
                              count,                   // number of bytes to Tx
                              &transferCount,          // number of bytes actually Tx
                              timeoutValue             // timeout
                             );
    if (rc != LIBUSB_SUCCESS) {
        print("bdm_usb_send_epOut() - Transfer failed (USB error = %s, timeout=%d)\n", libusb_strerror((libusb_error)rc), timeoutValue);
        return BDM_RC_USB_ERROR;
    }
    return BDM_RC_OK;
}
Пример #4
0
void
LogNetDump(struct dump *dumpPtr)
{
    struct dump hostDump;
    extern buServerConfP globalConfPtr;

    dump_ntoh(dumpPtr, &hostDump);

    globalConfPtr->log = fopen(AFSDIR_SERVER_BUDBLOG_FILEPATH, "a");
    if (globalConfPtr->log != NULL) {
	printDump(globalConfPtr->log, &hostDump);
	fclose(globalConfPtr->log);
    }
}
Пример #5
0
//*****************************************************************************
//!
//! \brief Sends a message+data to the USBDM device over EP0
//!
//! No response is expected from device.
//!
//! Since the EP0 transfer is unidirectional in this case, data returned by the
//! device must be read in a separate transfer - this includes any status.
//!
//! @param data \n
//!    data[0]    = N, the number of bytes to send (excluding this byte)\n
//!    data[1]    = Command byte \n
//!    data[2..N] = parameter(s) for command \n
//!
//! @return \n
//!    == BDM_RC_OK (0)     => Success\n
//!    == BDM_RC_USB_ERROR  => USB failure
//!
USBDM_ErrorCode bdm_usb_send_ep0( const unsigned char * data ) {
    unsigned char setupPkt[] = {0,0,0,0,0,0,0};
    unsigned char size       = data[0];   // Size is the first byte
    int rc;
    unsigned index;

    if (usbDeviceHandle == NULL) {
        print("bdm_usb_send_ep0() - Device handle NULL!\n");
        return BDM_RC_DEVICE_NOT_OPEN;
    }
#ifdef LOG_LOW_LEVEL
    print("============================\n");
    print("bdm_usb_send_ep0() - USB EP0 send (%s, size=%d):\n", getCommandName(data[1]), size);
    if (data[1] == CMD_USBDM_DEBUG)
        print("bdm_usb_send_ep0() - Debug cmd = %s\n", getDebugCommandName(data[2]));
    printDump(data, size+1);
#endif // LOG_LOW_LEVEL

    // Copy data in case <5 bytes to avoid possible illegal de-referencing
    for(index=0; index<(sizeof(setupPkt)/sizeof(setupPkt[0])); index++) {
        if (index > size)
            break;
        setupPkt[index] = data[index];
    }
    rc=libusb_control_transfer(usbDeviceHandle,
                               LIBUSB_REQUEST_TYPE_VENDOR|EP_CONTROL_OUT,      // requestType=Vendor
                               setupPkt[1],                                    // request
                               setupPkt[2]+(setupPkt[3]<<8),                   // value
                               setupPkt[4]+(setupPkt[5]<<8),                   // index
                               (unsigned char *)((size>5)?data+6:setupPkt),    // data bytes
                               (size>5)?(size-5):0,                            // size (# of data bytes)
                               timeoutValue);                                  // how long to wait for reply
    if (rc < 0) {
        print("bdm_usb_send_ep0() - USB EP0 send: Send failed (USB error = %d)\n", rc);
        return(BDM_RC_USB_ERROR);
    }
    return(BDM_RC_OK);
}
Пример #6
0
//! \brief Receives a response from the USBDM device over the In Bulk Endpoint
//! Responses are retried to allow for target execution
//!
//! @param count = Maximum number of bytes to receive
//!
//! @param data
//!    IN                                       \n
//!    ======================================== \n
//!    data[0]      = error code (= rc)         \n
//!    data[1..N]   = data received             \n
//!
//! @param actualCount = Actual number of bytes received
//!
//! @return                                                       \n
//!    == BDM_RC_OK (0)     => Success, OK response from device   \n
//!    == BDM_RC_USB_ERROR  => USB failure                        \n
//!    == else              => Error code from Device
//!
USBDM_ErrorCode bdm_usb_recv_epIn(unsigned count, unsigned char *data, unsigned *actualCount) {
    int rc;
    int retry = 40; // ~4s of retries
    int transferCount;

    *actualCount = 0; // Assume failure

    if (usbDeviceHandle==NULL) {
        print("bdm_usb_recv_epIn(): device not open\n");
        return BDM_RC_DEVICE_NOT_OPEN;
    }

#ifdef LOG_LOW_LEVEL
    print("bdm_usb_recv_epIn(%d, ...)\n", count);
#endif // LOG_LOW_LEVEL

#if OLD
    do {
        rc = libusb_bulk_transfer(usbDeviceHandle,
                                  EP_IN,                         // Endpoint & direction
                                  (unsigned char *)dummyBuffer,  // ptr to Rx data
                                  sizeof(dummyBuffer)-5,         // number of bytes to Rx
                                  &transferCount,                // number of bytes actually Rx
                                  timeoutValue                   // timeout
                                 );
        if (rc != LIBUSB_SUCCESS) {
            print("bdm_usb_recv_epIn(count=%d) - Transfer timeout (USB error = %s, timeout=%d) - retry\n", count, libusb_strerror((libusb_error)rc), timeoutValue);
            milliSleep(100); // So we don't monopolise the USB
        }
    } while ((rc!=LIBUSB_SUCCESS) && (--retry>0));
#else
    do {
        rc = libusb_bulk_transfer(usbDeviceHandle,
                                  EP_IN,                         // Endpoint & direction
                                  (unsigned char *)dummyBuffer,  // ptr to Rx data
                                  sizeof(dummyBuffer)-5,         // number of bytes to Rx
                                  &transferCount,                // number of bytes actually Rx
                                  timeoutValue                   // timeout
                                 );
        if ((rc == LIBUSB_SUCCESS)  && (dummyBuffer[0] == BDM_RC_BUSY)) {
            // The BDM has indicated it's busy for a while - try again in 1/10 second
            print("bdm_usb_recv_epIn(retry=%d) - BDM Busy - retry\n", retry);
            milliSleep(100); // So we don't monopolise the USB
        }
    } while ((rc == LIBUSB_SUCCESS) && (dummyBuffer[0] == BDM_RC_BUSY)  && (--retry>0));
#endif
    if (rc != LIBUSB_SUCCESS) {
        print("bdm_usb_recv_epIn(count=%d) - Transfer failed (USB error = %s)\n", count, libusb_strerror((libusb_error)rc));
        data[0] = BDM_RC_USB_ERROR;
        memset(&data[1], 0x00, count-1);
        return BDM_RC_USB_ERROR;
    }

    memcpy(data, dummyBuffer, transferCount);
    rc = data[0]&0x7F;

    if (rc != BDM_RC_OK) {
        print("bdm_usb_recv_epIn() - Error Return %d (%s):\n", rc, getErrorName(rc));
        print("bdm_usb_recv_epIn(size = %d, recvd = %d):\n", count, transferCount);
        printDump(data, transferCount);
        memset(&data[1], 0x00, count-1);
    }

#ifdef LOG_LOW_LEVEL
    printDump(data, transferCount);
#endif // LOG_LOW_LEVEL

    *actualCount = transferCount;

    return (USBDM_ErrorCode)(rc);
}