OpenCores
URL https://opencores.org/ocsvn/openrisc_2011-10-31/openrisc_2011-10-31/trunk

Subversion Repositories openrisc_2011-10-31

[/] [openrisc/] [trunk/] [bootloaders/] [orpmon/] [gencrc.c] - Blame information for rev 248

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 marcus.erl
/* Generates crc for specified binary file.
2
   We do very dirty hack here -- first occurrence of 0xccccccccdddddddd in output file designates where crc
3
   and size should be placed. We calculate the crc on the binary and then replace the occurences in the ELF
4
   file directly. This was done so, we don't need to */
5
 
6
#include <stdio.h>
7
 
8
#define MAX_SIZE 500000
9
#ifdef __BIG_ENDIAN__
10
#define SWAP32(x) (x)
11
#else /* !__BIG_ENDIAN__ */
12
#define SWAP32(x) ((((x) >> 24) & 0xff) << 0 \
13
                 | (((x) >> 16) & 0xff) << 8 \
14
                 | (((x) >> 8) & 0xff) << 16 \
15
                 | (((x) >> 0) & 0xff) << 24)
16
#endif /* __BIG_ENDIAN__ */
17
 
18
unsigned char buf[MAX_SIZE];
19
 
20
unsigned long crc32 (unsigned long crc, const unsigned char *buf, unsigned long len)
21
{
22
  /* Create bitwise CRC table first */
23
  unsigned long crc_table[256];
24
  int i, k;
25
  for (i = 0; i < 256; i++) {
26
    unsigned long c = (unsigned long)i;
27
    for (k = 0; k < 8; k++) c = c & 1 ? 0xedb88320 ^ (c >> 1) : c >> 1;
28
    crc_table[i] = c;
29
  }
30
 
31
  /* Calculate crc on buf */
32
  crc = crc ^ 0xffffffffL;
33
  while (len--) crc = crc_table[((int)crc ^ (*buf++)) & 0xff] ^ (crc >> 8);
34
  return crc ^ 0xffffffffL;
35
}
36
 
37
int main (int argc, char *argv[])
38
{
39
  FILE *fi, *fo;
40
  int size, i, tsize;
41
  unsigned long crc;
42
  if (argc != 3) return -1;
43
 
44
  fi = fopen (argv[1], "rb");
45
  fo = fopen (argv[2], "rb+");
46
  if (!fi || !fo) return 1;
47
 
48
  size = fread (buf, 1, MAX_SIZE, fi);
49
  fclose (fi);
50
 
51
  crc = crc32 (0, buf, size);
52
  tsize = fread (buf, 1, MAX_SIZE, fo);
53
 
54
  for (i = 0; i < tsize; i++)
55
    if (*((unsigned long *)&buf[i]) == SWAP32(0xcccccccc) && *((unsigned long *)&buf[i + 4]) == SWAP32(0xdddddddd)) {
56
      *(unsigned long *)&buf[i] = SWAP32(crc);
57
      *(unsigned long *)&buf[i + 4] = SWAP32(size);
58
      break;
59
    }
60
 
61
  if (i >= tsize - 8) return 2;
62
  fseek (fo, 0l, SEEK_SET);
63
  fwrite (buf, 1, tsize, fo);
64
  fclose (fo);
65
  return 0;
66
}

powered by: WebSVN 2.1.0

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