summaryrefslogtreecommitdiff
path: root/usb_stick_tester.pas
blob: f9ddd463c816a3b4ae9eff91a9e8cf4881e7d795 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
program usb_stick_tester;

{$mode objfpc}{$H+}

uses
  sysUtils; //,systemunit;

var
  f:               file;
  i,j,k,block_len: int64;
  a:               array[0..1023] of int64;
  fehler:          boolean;

begin

  if paramcount<>1 then begin
    writeln('usage: usb_stick_tester /dev/sdX');
    halt(1);
  end;

  assignFile(f,paramstr(1));
  try
    reset(f,1);
  except
    writeln('error: could not open "' + paramstr(1) + '"');
    halt(1);
  end;

  block_len:=length(a) * sizeof(a[0]);

  j:=0;
  while true do begin

    for i:=0 to length(a)-1 do
      a[i]:=j * block_len + i;

    seek(f,j * block_len);
    try
      blockwrite(f,a[0],block_len);
    except
      writeln('write failed at position ' + intToStr(j*block_len) + ' .. ' + intToStr((j+1)*block_len-1));
      break;
    end;

    if j>1 then begin
      k:=random(j);
      seek(f,k * block_len);
      blockread(f,a[0],block_len);
      fehler:=false;
      for i:=0 to length(a)-1 do
        if a[i] <> k * block_len + i then begin
          if (a[i]-i) mod block_len = 0 then
            writeln('Block ' + intToHex((a[i]-i) div block_len,2*sizeof(a[0])) + ' an Stelle ' + intToHex(k,2*sizeof(a[0])))
          else
            writeln('Byte ' + intToHex(a[i],2*sizeof(a[0])) + ' an Stelle ' + intToHex(k * block_len + i,2*sizeof(a[0])));
          fehler:=true;
          break;
        end;
      if fehler then
        break;
    end;

    inc(j);
    if j mod 1024 = 0 then
      write('.');
  end;

  closeFile(f);

end.