Openwall GNU/*/Linux - a small security-enhanced Linux distro for servers
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Date: Mon, 28 Sep 2015 14:39:45 +0300
From: Aleksey Cherepanov <lyosha@...nwall.com>
To: john-dev@...ts.openwall.com
Subject: Re: Re: ztex 1.15y boards, pre-development

Katja,

On Sun, Sep 27, 2015 at 10:41:21PM +0300, Aleksey Cherepanov wrote:
> On Mon, Jul 27, 2015 at 03:03:34AM +0300, Aleksey Cherepanov wrote:
> > I think, I got how to bypass it: read 5 times and skip results from
> > first 4 times. Also there is a difference: with wrong data I got 32
> > bytes back, while good data come in 34 bytes "packet".
> 
> I found why there is additional word that causes 34 bytes instead of
> 32: you check cnt >= 16 while you need cnt >= 15 because you already
> read 16 words at cnt == 15.

I played a bit and made a slightly different bitstream: it reads only
once and writes back only once too. The code is in attach.

There are problems:

- first time after power cycle it gives zeros, I use workaround: I
upload bitstream twice, then it works correctly even for the first run
of transfer_data

- I tried to make reads to return without data after the first one,
but it is not stable: sometimes I get "got 0 bytes back" (desired),
but sometimes it sticks with error "Error reading data: No error".


I removed cnt <= 0 on RESET. Otherwise the work is repeated on each
write even in 1 execution of transfer_data.


I think you messed up work with SLOE, SLRD, SLWR and PKTEND control
signals. I found a document that describes them:

http://www.cypress.com/file/4455144551

"SLRD pin should be asserted by the master to read data from the FIFO."
"SLWR pin should be asserted by the master to write data to the FIFO."
"SLOE: This is the enable signal for the FIFO's output driver."
"FIFOADR[1:0] These signals select the active endpoint."

I changed this a bit in your code. My code does not really match
states described in the document. Also I modified meaning of cnt:
0-14: write into fifo
  15: write into fifo and set PKTEND, bump cnt to 16
  16: disable sending, do nothing

I don't fully understand all signals yet: I've found some values for
signals by trial and error while they differ from my expectations
based on the documentation. Anyway I hope it gives ideas how to
proceed.

Thanks!

-- 
Regards,
Aleksey Cherepanov

/* modified; may be broken; see thread: http://openwall.com/lists/john-dev/2015/07/26/2 */

module intraffic (
	input        RESET,
	input        CS,
	input        RW,
	input        IFCLK,
	input        FLAGB,
	input        FLAGC,
	inout [15:0] FD,
	output       SLOE,
	output       SLRD,
	output       SLWR,
	output       FIFOADR0,
	output       FIFOADR1,
	output       PKTEND
);

reg [255:0] data;
reg [255:0] data_out;

reg SLOE_R, SLRD_R, SLWR_R, PKTEND_R;
reg [15:0] FD_R;
reg fr_or_sec;
reg [4:0] cnt;

assign SLOE = (CS == 1'b1) ? SLOE_R : 1'bZ;
assign SLRD = (CS == 1'b1) ? SLRD_R : 1'bZ;
assign SLWR = (CS == 1'b1) ? SLWR_R : 1'bZ;
assign FIFOADR0 = (CS == 1'b1) ? 1'b0 : 1'bZ;
assign FIFOADR1 = (CS == 1'b1) ? ((RW == 1'b1) ? 1'b0 : 1'b1) : 1'bZ;
assign PKTEND = (CS == 1'b1) ? PKTEND_R : 1'bZ;
// assign PKTEND = (CS == 1'b1) ? (RESET == 1 ? 1'b1 : PKTEND_R) : 1'bZ;
// assign FD = (RW == 1'b1 && CS == 1'b1) ? FD_R : 16'bZ;
// assign FD = (RW == 1'b1 && CS == 1'b1 && cnt <= 16 && RESET == 0) ? FD_R : 16'bZ;
assign FD = (RW == 1'b1 && CS == 1'b1 && cnt <= 16) ? FD_R : 16'bZ;

always @(posedge IFCLK)
begin

   if (RESET == 1) begin

   //    // cnt <= 0;
      fr_or_sec <= 0;

      SLRD_R <= 0;
      SLOE_R <= 0;
      SLWR_R <= 1;
      PKTEND_R <= 1;

   end else begin

      if (cnt < 5'd16) begin

         // PKTEND_R <= 1'b0;
         if (RW == 0) begin
            // reading; flagc - got data
            SLOE_R <= 1'b0;
            if (FLAGC == 1) begin
               if (fr_or_sec == 1'b0) begin
                  SLRD_R <= 1'b1;
                  fr_or_sec <= 1'b1;
                  data[255:240] <= FD;
               end else begin
                  data[255:240] <= FD;
                  data[239:0] <= data[255:16];
                  SLRD_R <= 1'b0;
                  fr_or_sec <= 1'b0;
               end
            end
            data_out <= data;
            // data_out <= 256'hd1310ba698dfb5ac2ffd72dbd01adfb7b8e1afed6a267e96ba7c9045f12c7f99;
            SLWR_R <= 1'b1;
            // PKTEND_R <= 1'b0;
            PKTEND_R <= 1'b1;
         end else begin
            // writing; flagb - got room
            if (FLAGB == 1) begin
               if (fr_or_sec == 0) begin
                  FD_R <= data_out[15:0];
                  SLWR_R <= 1'b0;
                  fr_or_sec <= 1'b1;
               end else begin
                  FD_R <= FD_R;
                  data_out[239:0] <= data_out[255:16];
                  SLWR_R <= 1'b1;
                  fr_or_sec <= 0;
                  cnt <= cnt + 5'd1;
               end
            end
            PKTEND_R <= 1'b0;
            SLRD_R <= 1'b1;
            SLOE_R <= 1'b1;
         end
         if (cnt == 5'd15) begin
            PKTEND_R <= 1'b1;
            // cnt <= 5'd0;
            cnt <= cnt + 5'd1;
            SLRD_R <= 1'b1;
            fr_or_sec <= 0;
         end

      end else begin
         PKTEND_R <= 1'b0;
         SLRD_R <= 1'b0;
         SLWR_R <= 1'b1;
         SLOE_R <= 1'b0;
         fr_or_sec <= 0;
      end

   end

end

endmodule

/* modified; may be broken; see thread: http://openwall.com/lists/john-dev/2015/07/26/2 */
/*!
   UCEcho -- C host software for ucecho examples
   Copyright (C) 2009-2010 ZTEX e.K.
   http://www.ztex.de

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License version 3 as
   published by the Free Software Foundation.

   This program 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
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, see http://www.gnu.org/licenses/.
!*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <usb.h>

#define BUFSIZE  256

struct usb_device *device;
usb_dev_handle *handle;
char buf[BUFSIZE];

// find the first ucecho device
struct usb_device *find_device ()
{
    struct usb_bus *bus_search;
    struct usb_device *device_search;

    bus_search = usb_busses;
    while (bus_search != NULL){
        device_search = bus_search->devices;
        while (device_search != NULL){
            if ((device_search->descriptor.idVendor == 0x221a) && (device_search->descriptor.idProduct == 0x100)) {
                handle = usb_open(device_search);
                usb_get_string_simple(handle, device_search->descriptor.iProduct, buf, BUFSIZE);

                if (! strncmp("intraffic", buf , 9 ) )
                    return device_search;

                usb_close(handle);
            }
            device_search = device_search->next;
        }
        bus_search = bus_search->next;
    }

    return NULL;
}

// main
int main(int argc, char *argv[])
{
    unsigned int sent[64] = {0xd1310ba6, 0x98dfb5ac, 0x2ffd72db, 0xd01adfb7,
                             0xb8e1afed, 0x6a267e96, 0xba7c9045, 0xf12c7f99,
                             0};
    unsigned int received[8 * 1024 / sizeof(unsigned int)] = {0};
    int i;

    pid_t pid = getpid();

    printf("\npid == %zd\n", pid);

    /* to make unique data for each run */
    for (i = 0; i < 8; i++) {
        sent[i] += pid;
    }

    sent[2] += 7;

    usb_init();                        // initializing libusb
    usb_find_busses();                    // ... finding busses
    usb_find_devices();                    // ... and devices

    device = find_device();                // find the device (hopefully the correct one)

    if ( device == NULL ) {                // nothing found
        fprintf(stderr, "Cannot find ucecho device\n");
        return 1;
    }

    if (usb_claim_interface(handle, 0) < 0) {
        fprintf(stderr, "Error claiming interface 0: %s\n", usb_strerror());
        return 1;
    }

    /* int ep_out = interface.endpoint[6].bEndpointAddress; */
    /* int ep_in = interface.endpoint[2].bEndpointAddress; */

    /* int ep_out = 6; */
    /* int ep_in = 2; */

    /* ep_out is used for usb_bulk_read, ep_in is for usb_bulk_write */
    int ep_out = 2;
    int ep_in = 6;

    int t;
    for (t = 0; t < 5; t++) {

        sent[0] ^= t;
        sent[1] ^= sent[0];

        //write mode
        i = usb_control_msg(handle, 0x40, 0x60, 0, 0, NULL, 0, 1000);
        if ( i < 0 ) {
            fprintf(stderr, "Error sending data: %s\n", usb_strerror());
            return 1;
        } else {
            printf("write mode control msg, %d\n", i);
        }

        printf("Written S[0] to FPGA...\n");
        /* for(i = 0; i < 32; i++) */
        /*     printf("S[0][%d] = 0x%08x\n", i, sent[i]); */

        // write string to ucecho device
        i = usb_bulk_write(handle, ep_in, (const char *)(sent), sizeof(unsigned int) * 8, 1000);
        if (i < 0) {
            fprintf(stderr, "Error sending data: %s\n", usb_strerror());
            return 1;
        } else {
            printf("wrote %d bytes\n", i);
        }

        //read mode
        i = usb_control_msg(handle, 0x40, 0x60, 1, 0, NULL, 0, 1000);
        if (i < 0) {
            fprintf(stderr, "Error sending data: %s\n", usb_strerror());
            return 1;
        } else {
            printf("read mode control msg, %d\n", i);
        }


        printf("Read S[0] from FPGA: \n");

        int j;
        for (j = 0; j < 8; j++) {

            // read string back from ucecho device
            /* i = usb_bulk_read(handle, 2, (char *)(received), 1024, 1000); */
            i = usb_bulk_read(handle, ep_out, (char *)(received), 1024, 1000);
            if (i < 0) {
                fprintf(stderr, "Error reading data: %s\n", usb_strerror());
                return 1;
            } else {
                printf("got %d bytes back (read #%d)\n", i, j);
            }

            /* for(i = 0; i < 8; i++) */
            /*     printf("S[0][%d] = 0x%08x\n", i, received[i]); */

            if (i == 34) {
                printf("33,34: %08x\n", received[8]);
            }
            if (i) {
                for(i = 0; i < 8; i++)
                    printf("S 0 %d = 0x%08x 0x%08x %s\n", i, received[i], sent[i], received[i] == sent[i] ? "ok": "<<< failure");
            }

        }

    }

    usb_release_interface(handle, 0);
    usb_close(handle);
    return 0;
}

Powered by blists - more mailing lists

Your e-mail address:

Powered by Openwall GNU/*/Linux - Powered by OpenVZ