/[dynamips]/upstream/dynamips-0.2.5/rom2c.c
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /upstream/dynamips-0.2.5/rom2c.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1 - (show annotations)
Sat Oct 6 16:01:44 2007 UTC (12 years, 3 months ago) by dpavlin
File MIME type: text/plain
File size: 2211 byte(s)
import 0.2.5 from upstream

1 /*
2 * Cisco 7200 (Predator) simulation platform.
3 * Copyright (c) 2006 Christophe Fillot (cf@utc.fr)
4 */
5
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <unistd.h>
9 #include <string.h>
10 #include <sys/types.h>
11 #include <sys/stat.h>
12 #include <sys/mman.h>
13 #include <signal.h>
14 #include <fcntl.h>
15 #include <assert.h>
16 #include <libelf.h>
17
18 #include "utils.h"
19
20 /* Extract ROM code+data from an ELF file and convert it into a C array */
21 int main(int argc,char *argv[])
22 {
23 unsigned char buffer[8];
24 m_uint32_t vaddr;
25 Elf32_Ehdr *ehdr;
26 Elf32_Phdr *phdr;
27 Elf *img_elf;
28 size_t len,clen;
29 int i,j,fd;
30 FILE *bfd,*fd_out;
31
32 if (argc != 3) {
33 fprintf(stderr,"Usage: %s <input_file> <output_file>\n",argv[0]);
34 exit(EXIT_FAILURE);
35 }
36
37 if ((fd = open(argv[1],O_RDONLY)) == -1)
38 return(-1);
39
40 if (elf_version(EV_CURRENT) == EV_NONE) {
41 fprintf(stderr,"load_elf_image: library out of date\n");
42 return(-1);
43 }
44
45 if (!(img_elf = elf_begin(fd,ELF_C_READ,NULL))) {
46 fprintf(stderr,"load_elf_image: elf_begin: %s\n",
47 elf_errmsg(elf_errno()));
48 return(-1);
49 }
50
51 if (!(phdr = elf32_getphdr(img_elf))) {
52 fprintf(stderr,"load_elf_image: elf32_getphdr: %s\n",
53 elf_errmsg(elf_errno()));
54 return(-1);
55 }
56
57 if (!(fd_out = fopen(argv[2],"w"))) {
58 fprintf(stderr,"Unable to create file \"%s\"\n",argv[2]);
59 exit(EXIT_FAILURE);
60 }
61
62 ehdr = elf32_getehdr(img_elf);
63 phdr = elf32_getphdr(img_elf);
64
65 printf("Extracting ROM from ELF file '%s'...\n",argv[1]);
66 bfd = fdopen(fd,"r");
67
68 if (!bfd) {
69 perror("load_elf_image: fdopen");
70 return(-1);
71 }
72
73 for(i=0;i<ehdr->e_phnum;i++,phdr++)
74 {
75 fseek(bfd,phdr->p_offset,SEEK_SET);
76
77 vaddr = (m_uint64_t)phdr->p_vaddr;
78 len = phdr->p_filesz;
79
80 if (vaddr != 0xbfc00000)
81 continue;
82
83 while(len > 0)
84 {
85 clen = fread(buffer,1,sizeof(buffer),bfd);
86
87 if (clen == 0)
88 break;
89
90 fprintf(fd_out," ");
91
92 for(j=0;j<clen;j++)
93 fprintf(fd_out,"0x%2.2x, ",buffer[j]);
94
95 fprintf(fd_out,"\n");
96 len -= clen;
97 }
98 }
99
100 fclose(fd_out);
101 return(0);
102 }

  ViewVC Help
Powered by ViewVC 1.1.26