OpenCores
URL https://opencores.org/ocsvn/eco32/eco32/trunk

Subversion Repositories eco32

[/] [eco32/] [trunk/] [stdalone/] [dskchk2/] [chkimg.c] - Blame information for rev 198

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 198 hellwig
/*
2
 * chkimg.c -- check test image
3
 */
4
 
5
 
6
#include <stdio.h>
7
#include <stdlib.h>
8
#include <string.h>
9
#include <stdarg.h>
10
 
11
 
12
#define NUM_SECTORS     262144          /* sync with image generator */
13
#define SECTOR_SIZE     512
14
 
15
 
16
/**************************************************************/
17
 
18
 
19
void error(char *fmt, ...) {
20
  va_list ap;
21
 
22
  va_start(ap, fmt);
23
  printf("Error: ");
24
  vprintf(fmt, ap);
25
  printf("\n");
26
  va_end(ap);
27
  exit(1);
28
}
29
 
30
 
31
/**************************************************************/
32
 
33
 
34
static unsigned int randomState = 0;
35
 
36
 
37
void setRandomSeed(unsigned int seed) {
38
  randomState = seed;
39
}
40
 
41
 
42
unsigned int nextRandomNumber(void) {
43
  unsigned int retVal;
44
 
45
  retVal = randomState;
46
  randomState = randomState * 1103515245 + 12345;
47
  return retVal;
48
}
49
 
50
 
51
/**************************************************************/
52
 
53
 
54
static unsigned int randomSector = 0xDEADBEEF;
55
 
56
 
57
unsigned int nextRandomSector(unsigned int numSectors) {
58
  randomSector = randomSector * 1103515245 + 12345;
59
  return randomSector % numSectors;
60
}
61
 
62
 
63
/**************************************************************/
64
 
65
 
66
static void usage(char *myself) {
67
  printf("Usage: %s <image file name> <number of checks>\n", myself);
68
  exit(1);
69
}
70
 
71
 
72
int main(int argc, char *argv[]) {
73
  char *imageFileName;
74
  unsigned int numChecks;
75
  char *endptr;
76
  FILE *imageFile;
77
  unsigned int check;
78
  unsigned int sectorRequ;
79
  unsigned char buffer[SECTOR_SIZE];
80
  int i;
81
  unsigned int sectorRead;
82
  unsigned int number;
83
  unsigned int wrong, corrupted;
84
 
85
  printf("\nIDE disk check\n\n");
86
  if (argc != 3) {
87
    usage(argv[0]);
88
  }
89
  imageFileName = argv[1];
90
  imageFile = fopen(imageFileName, "r");
91
  if (imageFile == NULL) {
92
    error("cannot open image file '%s'", imageFileName);
93
  }
94
  numChecks = strtoul(argv[2], &endptr, 0);
95
  if (*endptr != '\0') {
96
    error("illegal number of checks");
97
  }
98
  if (numChecks < 1) {
99
    error("number of checks too small");
100
  }
101
  wrong = 0;
102
  corrupted = 0;
103
  for (check = 0; check < numChecks; check++) {
104
    sectorRequ = nextRandomSector(NUM_SECTORS);
105
    fseek(imageFile, sectorRequ * SECTOR_SIZE, SEEK_SET);
106
    if (fread(buffer, 1, SECTOR_SIZE, imageFile) != SECTOR_SIZE) {
107
      error("cannot read image file '%s'", imageFileName);
108
    }
109
    sectorRead = (buffer[0] << 24) |
110
                 (buffer[1] << 16) |
111
                 (buffer[2] <<  8) |
112
                 (buffer[3] <<  0);
113
    if (sectorRead != sectorRequ) {
114
      wrong++;
115
    }
116
    setRandomSeed(sectorRead);
117
    for (i = 0; i < SECTOR_SIZE / 4; i++) {
118
      number = (buffer[4 * i + 0] << 24) |
119
               (buffer[4 * i + 1] << 16) |
120
               (buffer[4 * i + 2] <<  8) |
121
               (buffer[4 * i + 3] <<  0);
122
      if (number != nextRandomNumber()) {
123
        corrupted++;
124
        break;
125
      }
126
    }
127
    printf("check #%06d: requ 0x%08x, read 0x%08x, sector %s\n",
128
           check, sectorRequ, sectorRead,
129
           i == SECTOR_SIZE / 4 ? "ok" : "corrupted");
130
  }
131
  fclose(imageFile);
132
  printf("\nTotal number of sectors: %u read, %u wrong, %u corrupted\n\n",
133
         numChecks, wrong, corrupted);
134
  return 0;
135
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.