This source file includes following definitions.
- exec
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33 #include <sys/param.h>
34 #include <sys/exec.h>
35 #include <sys/reboot.h>
36 #ifndef INSECURE
37 #include <sys/stat.h>
38 #endif
39
40 #include "stand.h"
41
42 static char *ssym, *esym;
43
44 extern u_int opendev;
45
46 void
47 exec(char *path, void *loadaddr, int howto)
48 {
49 int io;
50 #ifndef INSECURE
51 struct stat sb;
52 #endif
53 struct exec x;
54 u_int i;
55 ssize_t sz;
56 char *addr;
57 #ifdef EXEC_DEBUG
58 char *daddr, *etxt;
59 #endif
60
61 io = open(path, 0);
62 if (io < 0)
63 return;
64
65 (void) fstat(io, &sb);
66 if (sb.st_mode & 2)
67 printf("non-secure file, check permissions!\n");
68
69 sz = read(io, (char *)&x, sizeof(x));
70 if (sz != sizeof(x) || N_BADMAG(x)) {
71 close(io);
72 errno = EFTYPE;
73 return;
74 }
75
76 #ifdef EXEC_DEBUG
77 printf("\nstruct exec {%x, %x, %x, %x, %x, %x, %x, %x}\n",
78 x.a_midmag, x.a_text, x.a_data, x.a_bss, x.a_syms,
79 x.a_entry, x.a_trsize, x.a_drsize);
80 #endif
81
82
83 printf("%u", x.a_text);
84 addr = loadaddr;
85 sz = x.a_text;
86 if (N_GETMAGIC(x) == ZMAGIC) {
87 bcopy((char *)&x, addr, sizeof x);
88 addr += sizeof x;
89 sz -= sizeof x;
90 }
91 if (read(io, (char *)addr, sz) != sz)
92 goto shread;
93 addr += sz;
94 #ifdef EXEC_DEBUG
95 printf("\ntext {%x, %x, %x, %x}\n",
96 addr[0], addr[1], addr[2], addr[3]);
97 etxt = addr;
98 #endif
99 if (N_GETMAGIC(x) == NMAGIC)
100 while ((long)addr & (N_PAGSIZ(x) - 1))
101 *addr++ = 0;
102
103
104 #ifdef EXEC_DEBUG
105 daddr = addr;
106 #endif
107 printf("+%u", x.a_data);
108 if (read(io, addr, x.a_data) != (ssize_t)x.a_data)
109 goto shread;
110 addr += x.a_data;
111
112
113 printf("+%u", x.a_bss);
114 for (i = 0; i < x.a_bss; i++)
115 *addr++ = 0;
116
117
118 if (x.a_syms) {
119 ssym = addr;
120 bcopy(&x.a_syms, addr, sizeof(x.a_syms));
121 addr += sizeof(x.a_syms);
122 printf("+[%u", x.a_syms);
123 if (read(io, addr, x.a_syms) != (ssize_t)x.a_syms)
124 goto shread;
125 addr += x.a_syms;
126
127 if (read(io, &i, sizeof(u_int)) != sizeof(u_int))
128 goto shread;
129
130 bcopy(&i, addr, sizeof(u_int));
131 if (i) {
132 sz = i - sizeof(int);
133 addr += sizeof(int);
134 if (read(io, addr, sz) != sz)
135 goto shread;
136 addr += sz;
137 }
138
139
140 printf("+%d]", sz);
141 esym = addr;
142 } else {
143 ssym = 0;
144 esym = 0;
145 }
146
147 close(io);
148
149
150 printf(" total=0x%lx", (u_long)addr);
151
152
153
154
155
156
157
158
159 printf(" start=0x%x\n", x.a_entry);
160
161 #ifdef EXEC_DEBUG
162 printf("loadaddr=%p etxt=%p daddr=%p ssym=%p esym=%p\n",
163 loadaddr, etxt, daddr, ssym, esym);
164 printf("\n\nReturn to boot...\n");
165 getchar();
166 #endif
167
168 machdep_start((char *)((register_t)x.a_entry), howto, loadaddr, ssym,
169 esym);
170
171
172 errno = ENOEXEC;
173 return;
174
175 shread:
176 close(io);
177 errno = EIO;
178 return;
179 }