Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / SLOF / tools / gen_reloc_table.c
1 /******************************************************************************
2  * Copyright (c) 2004, 2008 IBM Corporation
3  * All rights reserved.
4  * This program and the accompanying materials
5  * are made available under the terms of the BSD License
6  * which accompanies this distribution, and is available at
7  * http://www.opensource.org/licenses/bsd-license.php
8  *
9  * Contributors:
10  *     IBM Corporation - initial implementation
11  *****************************************************************************/
12
13 #include <stdio.h>
14 #include <stdlib.h>
15
16 static int reloc_64_cnt;
17
18 static int reloc_64[4096];
19
20 static void
21 output_int(FILE *output_file, int i)
22 {
23   fputc((i>>24) & 0xff, output_file);
24   fputc((i>>16) & 0xff, output_file);
25   fputc((i>>8) & 0xff, output_file);
26   fputc(i & 0xff, output_file);
27 }
28
29 static void
30 output_reloc_table(FILE * output_file, int reloc_cnt, int reloc[])
31 {
32   int i;
33   for (i=0; i < reloc_cnt; i++)
34     {
35 #ifdef DEBUG
36       printf ("reloc %x\n", reloc[i]);
37 #endif
38       output_int (output_file, reloc[i]);
39     }
40   if ((reloc_cnt & 1) == 0)
41     output_int (output_file, 0);
42 }
43
44 int
45 main(int argc, char *argv[])
46 {
47   int cnt_a, cnt_b, offset = -1;
48   unsigned char a, b;
49   FILE *orig, *other, *output_file;
50
51   if (argc != 4)
52     {
53       fprintf (stderr, "reloc_diff orig_file other_file output_file\n");
54       exit(-1);
55     }
56     
57   orig = fopen(argv[1], "rb");
58   other = fopen(argv[2], "rb");
59   output_file = fopen(argv[3], "wb");
60   if(orig == NULL || other == NULL || output_file == NULL) {
61     printf("Could not open file.\n");
62     return -1;
63   }
64
65   while (1)
66     {
67       cnt_a = fread(&a, 1, 1, orig);
68       cnt_b = fread(&b, 1, 1, other);
69       offset ++;
70       if (cnt_a != cnt_b)
71         {
72           fprintf (stderr, "Files >%s< and >%s< have not the same length\n",argv[1],argv[2]);
73           exit(-1);
74         }
75
76       if (cnt_a == 0)
77         break;
78       
79       if (a == b)       continue;
80
81       if (a + 0x40 == b)
82         {
83           reloc_64[reloc_64_cnt++] = offset;
84         }
85       else
86         {
87           fprintf(stderr, "Unknown relocation");
88           fprintf(stderr, "Offset %x: %02x %02x\n", offset, a, b);
89           break;
90         }
91     }
92
93   output_reloc_table(output_file, reloc_64_cnt, reloc_64);
94   return 0;
95 }