ia64/xen-unstable

view tools/libxc/xc_load_aout9.c @ 6552:a9873d384da4

Merge.
author adsharma@los-vmm.sc.intel.com
date Thu Aug 25 12:24:48 2005 -0700 (2005-08-25)
parents 112d44270733 819726e7dd2b
children dfaf788ab18c
line source
2 #include "xg_private.h"
3 #include "xc_aout9.h"
5 #if defined(__i386__)
6 #define A9_MAGIC I_MAGIC
7 #elif defined(__x86_64__)
8 #define A9_MAGIC S_MAGIC
9 #elif defined(__ia64__)
10 #define A9_MAGIC 0
11 #else
12 #error "Unsupported architecture"
13 #endif
16 #define round_pgup(_p) (((_p)+(PAGE_SIZE-1))&PAGE_MASK)
17 #define round_pgdown(_p) ((_p)&PAGE_MASK)
19 static int parseaout9image(char *, unsigned long, struct domain_setup_info *);
20 static int loadaout9image(char *, unsigned long, int, u32, unsigned long *, struct domain_setup_info *);
21 static void copyout(int, u32, unsigned long *, unsigned long, void *, int);
22 struct Exec *get_header(char *, unsigned long, struct Exec *);
25 int
26 probe_aout9(
27 char *image,
28 unsigned long image_size,
29 struct load_funcs *load_funcs)
30 {
31 struct Exec ehdr;
33 if (!get_header(image, image_size, &ehdr)) {
34 ERROR("Kernel image does not have a a.out9 header.");
35 return -EINVAL;
36 }
38 load_funcs->parseimage = parseaout9image;
39 load_funcs->loadimage = loadaout9image;
40 return 0;
41 }
43 static int
44 parseaout9image(
45 char *image,
46 unsigned long image_size,
47 struct domain_setup_info *dsi)
48 {
49 struct Exec ehdr;
50 unsigned long start, txtsz, end;
52 if (!get_header(image, image_size, &ehdr)) {
53 ERROR("Kernel image does not have a a.out9 header.");
54 return -EINVAL;
55 }
57 if (sizeof ehdr + ehdr.text + ehdr.data > image_size) {
58 ERROR("a.out program extends past end of image.");
59 return -EINVAL;
60 }
62 start = round_pgdown(ehdr.entry);
63 txtsz = round_pgup(ehdr.text);
64 end = start + txtsz + ehdr.data + ehdr.bss;
66 dsi->v_start = start;
67 dsi->v_kernstart = start;
68 dsi->v_kernend = end;
69 dsi->v_kernentry = ehdr.entry;
70 dsi->v_end = end;
72 /* XXX load symbols */
74 return 0;
75 }
77 static int
78 loadaout9image(
79 char *image,
80 unsigned long image_size,
81 int xch, u32 dom,
82 unsigned long *parray,
83 struct domain_setup_info *dsi)
84 {
85 struct Exec ehdr;
86 unsigned long txtsz;
88 if (!get_header(image, image_size, &ehdr)) {
89 ERROR("Kernel image does not have a a.out9 header.");
90 return -EINVAL;
91 }
93 txtsz = round_pgup(ehdr.text);
94 copyout(xch, dom, parray,
95 0, image, sizeof ehdr + ehdr.text);
96 copyout(xch, dom, parray,
97 txtsz, image + sizeof ehdr + ehdr.text, ehdr.data);
98 /* XXX zeroing of BSS needed? */
100 /* XXX load symbols */
102 return 0;
103 }
105 /*
106 * copyout data to the domain given an offset to the start
107 * of its memory region described by parray.
108 */
109 static void
110 copyout(
111 int xch, u32 dom,
112 unsigned long *parray,
113 unsigned long off,
114 void *buf,
115 int sz)
116 {
117 unsigned long pgoff, chunksz;
118 void *pg;
120 while (sz > 0) {
121 pgoff = off & (PAGE_SIZE-1);
122 chunksz = sz;
123 if(chunksz > PAGE_SIZE - pgoff)
124 chunksz = PAGE_SIZE - pgoff;
126 pg = xc_map_foreign_range(xch, dom, PAGE_SIZE, PROT_WRITE,
127 parray[off>>PAGE_SHIFT]);
128 memcpy(pg + pgoff, buf, chunksz);
129 munmap(pg, PAGE_SIZE);
131 off += chunksz;
132 buf += chunksz;
133 sz -= chunksz;
134 }
135 }
137 #define swap16(_v) ((((u16)(_v)>>8)&0xff)|(((u16)(_v)&0xff)<<8))
138 #define swap32(_v) (((u32)swap16((u16)(_v))<<16)|(u32)swap16((u32)((_v)>>16)))
140 /*
141 * Decode the header from the start of image and return it.
142 */
143 struct Exec *
144 get_header(
145 char *image,
146 unsigned long image_size,
147 struct Exec *ehdr)
148 {
149 u32 *v, x;
150 int i;
152 if (A9_MAGIC == 0)
153 return 0;
155 if (image_size < sizeof ehdr)
156 return 0;
158 /* ... all big endian words */
159 v = (u32 *)ehdr;
160 for (i = 0; i < sizeof(*ehdr); i += 4) {
161 x = *(u32 *)&image[i];
162 v[i/4] = swap32(x);
163 }
165 if(ehdr->magic != A9_MAGIC)
166 return 0;
167 return ehdr;
168 }