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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [rtems/] [tools/] [build/] [binpatch.c] - Blame information for rev 778

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

Line No. Rev Author Line
1 30 unneback
/*
2
 *  $Id: binpatch.c,v 1.2 2001-09-27 12:02:53 chris Exp $
3
 */
4
 
5
 
6
#include <stdio.h>
7
#include <stdlib.h>
8
 
9
/*
10
 * This function will patch binary file
11
 */
12
 
13
 
14
static char buf[512];
15
 
16
static void
17
usage(void)
18
{
19
  printf("usage: binpatch [-h] <ofile> <ifile> <reloc> <off> <byte0> "
20
         "[<byte1> [<byte2> [<byte3>]]]\n");
21
  printf("this function patches binary file at specified offset with\n");
22
  printf("up to 4 bytes provided on command line \n");
23
  printf("-h       - prints this message\n\n");
24
  printf("<ofile>  - output file\n");
25
  printf("<ifile>  - input ifile\n");
26
  printf("<reloc>  - relocation address of image\n");
27
  printf("<off>    - offset of patch, offset in file is at off - reloc\n");
28
  printf("<byte0>  - byte 0 of patch\n");
29
  printf("<byte1>  - byte 1 of patch\n");
30
  printf("<byte2>  - byte 1 of patch\n");
31
  printf("<byte3>  - byte 1 of patch\n");
32
 
33
  return;
34
}
35
 
36
int
37
main(int argc, char **argv)
38
{
39
  int   c;
40
  FILE  *ofp, *ifp;
41
  char  patch[4], *end;
42
  int   patchLen, tmp, i, off, cnt, patched, len, reloc;
43
 
44
 
45
  /* parse command line options */
46
  while ((c = getopt(argc, argv, "h")) >= 0)
47
    {
48
      switch (c)
49
        {
50
        case 'h':
51
          usage();
52
          return 0;
53
        default:
54
          usage();
55
          return 1;
56
        }
57
    }
58
 
59
  if(argc < 6)
60
    {
61
      usage();
62
      return 1;
63
    }
64
 
65
  /* Let us get offset in file */
66
  reloc = strtol(argv[3], &end, 0);
67
  if(end == argv[3] || off < 0)
68
    {
69
      fprintf(stderr, "bad reloc value %s\n", argv[3]);
70
      return 1;
71
    }
72
 
73
  off = strtol(argv[4], &end, 0);
74
  if(end == argv[4] || off < 0 || off < reloc)
75
    {
76
      fprintf(stderr, "bad offset value %s\n", argv[4]);
77
      return 1;
78
    }
79
 
80
  off -= reloc;
81
 
82
  /* Let us get patch  */
83
  patchLen = argc - 5;
84
 
85
  for(i=0; i<patchLen; i++)
86
    {
87
      tmp = strtol(argv[5+i], &end, 0);
88
 
89
      if(end == argv[4+i] || tmp < 0 || tmp > 0xff)
90
        {
91
          fprintf(stderr, "bad byte value %s\n", argv[5+i]);
92
          return 1;
93
        }
94
      patch[i] = tmp;
95
    }
96
 
97
  ifp = fopen(argv[2], "r");
98
  if(ifp == NULL)
99
    {
100
      fprintf(stderr, "unable to open file %s\n", argv[2]);
101
      return 1;
102
    }
103
 
104
  ofp = fopen(argv[1], "w");
105
  if(ofp == NULL)
106
    {
107
      fprintf(stderr, "unable to open file %s\n", argv[1]);
108
      return 1;
109
    }
110
 
111
  cnt     = 0;
112
  patched = 0;
113
  for(;;)
114
    {
115
      len = fread(buf, 1, sizeof(buf), ifp);
116
 
117
      if(len == 0)
118
        {
119
          break;
120
        }
121
 
122
      if(cnt <= off && (cnt + len) > off)
123
        {
124
          /* Perform patch */
125
          for(i=0; i<patchLen && (off+i)<(cnt+len); i++)
126
            {
127
              buf[off-cnt+i] = patch[i];
128
            }
129
          patched = 1;
130
        }
131
      else if(cnt > off && cnt < (off + patchLen))
132
        {
133
          /* Perform patch */
134
          for(i=cnt-off; i<patchLen; i++)
135
            {
136
              buf[off-cnt+i] = patch[i];
137
            }
138
          patched = 1;
139
        }
140
 
141
      fwrite(buf, 1, len, ofp);
142
 
143
      cnt += len;
144
    }
145
 
146
  fclose(ifp);
147
  fclose(ofp);
148
 
149
  if(!patched)
150
    {
151
      fprintf(stderr, "warning: offset is beyond input file length\n");
152
      fprintf(stderr, "         no patch is performed\n");
153
    }
154
 
155
  return 0;
156
}

powered by: WebSVN 2.1.0

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