1 | /* |
---|
2 | * redboot-syscalls.c -- provide system call support for RedBoot |
---|
3 | * |
---|
4 | * Copyright (c) 1997, 2001, 2002 Red Hat, Inc. |
---|
5 | * |
---|
6 | * The authors hereby grant permission to use, copy, modify, distribute, |
---|
7 | * and license this software and its documentation for any purpose, provided |
---|
8 | * that existing copyright notices are retained in all copies and that this |
---|
9 | * notice is included verbatim in any distributions. No written agreement, |
---|
10 | * license, or royalty fee is required for any of the authorized uses. |
---|
11 | * Modifications to this software may be copyrighted by their authors |
---|
12 | * and need not follow the licensing terms described here, provided that |
---|
13 | * the new terms are clearly indicated on the first page of each file where |
---|
14 | * they apply. |
---|
15 | * |
---|
16 | */ |
---|
17 | |
---|
18 | #include <stdlib.h> |
---|
19 | #include <sys/stat.h> |
---|
20 | #include <sys/times.h> |
---|
21 | #include <errno.h> |
---|
22 | #include "syscall.h" |
---|
23 | |
---|
24 | // Use "naked" attribute to suppress C prologue/epilogue |
---|
25 | static int __attribute__ ((naked)) __syscall(int func_no, ...) |
---|
26 | { |
---|
27 | asm ("mov r12, lr\n"); |
---|
28 | #ifdef __thumb__ |
---|
29 | asm ("swi 0x18\n"); |
---|
30 | #else |
---|
31 | asm ("swi 0x180001\n"); |
---|
32 | #endif |
---|
33 | asm ("mov pc, r12\n"); |
---|
34 | } |
---|
35 | |
---|
36 | int |
---|
37 | _close(int fd) |
---|
38 | { |
---|
39 | int err; |
---|
40 | err = __syscall(SYS_close, fd); |
---|
41 | if (err<0) |
---|
42 | { |
---|
43 | errno = -err; |
---|
44 | return -1; |
---|
45 | } |
---|
46 | return err; |
---|
47 | } |
---|
48 | |
---|
49 | |
---|
50 | void |
---|
51 | _exit(int stat) |
---|
52 | { |
---|
53 | while (1) |
---|
54 | __syscall(SYS_exit, stat); |
---|
55 | } |
---|
56 | |
---|
57 | |
---|
58 | int |
---|
59 | _stat (const char *filename, struct stat *st) |
---|
60 | { |
---|
61 | int err; |
---|
62 | err = __syscall(SYS_stat, filename, st); |
---|
63 | if (err<0) |
---|
64 | { |
---|
65 | errno = -err; |
---|
66 | return -1; |
---|
67 | } |
---|
68 | return err; |
---|
69 | } |
---|
70 | |
---|
71 | int |
---|
72 | _fstat (int file, struct stat *st) |
---|
73 | { |
---|
74 | int err; |
---|
75 | err = __syscall(SYS_fstat, file, st); |
---|
76 | if (err<0) |
---|
77 | { |
---|
78 | errno = -err; |
---|
79 | return -1; |
---|
80 | } |
---|
81 | return err; |
---|
82 | } |
---|
83 | |
---|
84 | int |
---|
85 | _getpid(void) |
---|
86 | { |
---|
87 | return 1; |
---|
88 | } |
---|
89 | |
---|
90 | |
---|
91 | int |
---|
92 | _gettimeofday (void * tp, void * tzp) |
---|
93 | { |
---|
94 | int err; |
---|
95 | err = __syscall(SYS_gettimeofday, tp, tzp); |
---|
96 | if (err<0) |
---|
97 | { |
---|
98 | errno = -err; |
---|
99 | return -1; |
---|
100 | } |
---|
101 | return err; |
---|
102 | } |
---|
103 | |
---|
104 | |
---|
105 | int |
---|
106 | _isatty(int fd) |
---|
107 | { |
---|
108 | int err; |
---|
109 | err = __syscall(SYS_isatty, fd); |
---|
110 | if (err<0) |
---|
111 | { |
---|
112 | errno = -err; |
---|
113 | return -1; |
---|
114 | } |
---|
115 | return err; |
---|
116 | } |
---|
117 | |
---|
118 | |
---|
119 | int |
---|
120 | _kill(int pid, int sig) |
---|
121 | { |
---|
122 | if(pid == 1) |
---|
123 | _exit(sig); |
---|
124 | return 0; |
---|
125 | } |
---|
126 | |
---|
127 | |
---|
128 | off_t |
---|
129 | _lseek(int fd, off_t offset, int whence) |
---|
130 | { |
---|
131 | int err; |
---|
132 | err = __syscall(SYS_lseek, fd, offset, whence); |
---|
133 | if (err<0) |
---|
134 | { |
---|
135 | errno = -err; |
---|
136 | return (off_t)-1; |
---|
137 | } |
---|
138 | return err; |
---|
139 | } |
---|
140 | |
---|
141 | |
---|
142 | int |
---|
143 | _open(const char *buf, int flags, int mode) |
---|
144 | { |
---|
145 | int err ; |
---|
146 | err = __syscall(SYS_open, buf, flags, mode); |
---|
147 | if (err<0) |
---|
148 | { |
---|
149 | errno = -err; |
---|
150 | return -1; |
---|
151 | } |
---|
152 | return err; |
---|
153 | } |
---|
154 | |
---|
155 | |
---|
156 | int |
---|
157 | _write(int fd, const char *buf, int nbytes) |
---|
158 | { |
---|
159 | int err; |
---|
160 | |
---|
161 | err = __syscall(SYS_write, fd, buf, nbytes); |
---|
162 | if (err<0) |
---|
163 | { |
---|
164 | errno = -err; |
---|
165 | return -1; |
---|
166 | } |
---|
167 | return err; |
---|
168 | } |
---|
169 | |
---|
170 | |
---|
171 | void |
---|
172 | print(char *ptr) |
---|
173 | { |
---|
174 | char *p = ptr; |
---|
175 | |
---|
176 | while (*p != '\0') |
---|
177 | p++; |
---|
178 | |
---|
179 | _write (1, ptr, p-ptr); |
---|
180 | } |
---|
181 | |
---|
182 | void |
---|
183 | _raise (void) |
---|
184 | { |
---|
185 | return; |
---|
186 | } |
---|
187 | |
---|
188 | |
---|
189 | int |
---|
190 | _read(int fd, char *buf, int nbytes) |
---|
191 | { |
---|
192 | int err; |
---|
193 | err = __syscall(SYS_read, fd, buf, nbytes); |
---|
194 | if (err<0) |
---|
195 | { |
---|
196 | errno = -err; |
---|
197 | return -1; |
---|
198 | } |
---|
199 | return err; |
---|
200 | } |
---|
201 | |
---|
202 | |
---|
203 | extern char end[]; /* end is set in the linker command file */ |
---|
204 | |
---|
205 | char *heap_ptr; |
---|
206 | |
---|
207 | char * |
---|
208 | _sbrk (int nbytes) |
---|
209 | { |
---|
210 | char *base; |
---|
211 | |
---|
212 | if (!heap_ptr) |
---|
213 | heap_ptr = (char *)&end; |
---|
214 | base = heap_ptr; |
---|
215 | heap_ptr += nbytes; |
---|
216 | |
---|
217 | return base; |
---|
218 | } |
---|
219 | |
---|
220 | |
---|
221 | clock_t |
---|
222 | _times(struct tms * tp) |
---|
223 | { |
---|
224 | clock_t utime; |
---|
225 | int err; |
---|
226 | err = __syscall(SYS_times, &utime); |
---|
227 | if (err) |
---|
228 | utime = 0; |
---|
229 | |
---|
230 | if (tp) { |
---|
231 | tp->tms_utime = utime; |
---|
232 | tp->tms_stime = 0; |
---|
233 | tp->tms_cutime = 0; |
---|
234 | tp->tms_cstime = 0; |
---|
235 | } |
---|
236 | |
---|
237 | return utime; |
---|
238 | } |
---|
239 | |
---|
240 | int |
---|
241 | _rename (const char *oldpath, const char *newpath) |
---|
242 | { |
---|
243 | int err ; |
---|
244 | err = __syscall(SYS_rename, oldpath, newpath); |
---|
245 | if (err<0) |
---|
246 | { |
---|
247 | errno = -err; |
---|
248 | return -1; |
---|
249 | } |
---|
250 | return err; |
---|
251 | } |
---|
252 | |
---|
253 | int |
---|
254 | _unlink (const char *pathname) |
---|
255 | { |
---|
256 | int err ; |
---|
257 | err = __syscall(SYS_unlink, pathname); |
---|
258 | if (err<0) |
---|
259 | { |
---|
260 | errno = -err; |
---|
261 | return -1; |
---|
262 | } |
---|
263 | return err; |
---|
264 | } |
---|
265 | |
---|
266 | int |
---|
267 | _system (const char *command) |
---|
268 | { |
---|
269 | int err ; |
---|
270 | err = __syscall(SYS_system, command); |
---|
271 | return err; |
---|
272 | } |
---|
273 | |
---|
274 | #define SYS_meminfo 1001 |
---|
275 | |
---|
276 | void * |
---|
277 | __get_memtop(void) |
---|
278 | { |
---|
279 | unsigned long totmem = 0, topmem = 0; |
---|
280 | int numbanks; |
---|
281 | |
---|
282 | __syscall(SYS_meminfo, (unsigned long)&totmem, (unsigned long)&topmem, 0); |
---|
283 | return (void*)topmem; |
---|
284 | } |
---|