1 | /*
|
---|
2 | * TOPPERS PROJECT Home Network Working Group Software
|
---|
3 | *
|
---|
4 | * Copyright (C) 2020 Cores Co., Ltd. Japan
|
---|
5 | *
|
---|
6 | * 上記著作権者は,以下の(1)~(4)の条件を満たす場合に限り,本ソフトウェ
|
---|
7 | * ア(本ソフトウェアを改変したものを含む.以下同じ)を使用・複製・改
|
---|
8 | * 変・再配布(以下,利用と呼ぶ)することを無償で許諾する.
|
---|
9 | * (1) 本ソフトウェアをソースコードの形で利用する場合には,上記の著作
|
---|
10 | * 権表示,この利用条件および下記の無保証規定が,そのままの形でソー
|
---|
11 | * スコード中に含まれていること.
|
---|
12 | * (2) 本ソフトウェアを,ライブラリ形式など,他のソフトウェア開発に使
|
---|
13 | * 用できる形で再配布する場合には,再配布に伴うドキュメント(利用
|
---|
14 | * 者マニュアルなど)に,上記の著作権表示,この利用条件および下記
|
---|
15 | * の無保証規定を掲載すること.
|
---|
16 | * (3) 本ソフトウェアを,機器に組み込むなど,他のソフトウェア開発に使
|
---|
17 | * 用できない形で再配布する場合には,次のいずれかの条件を満たすこ
|
---|
18 | * と.
|
---|
19 | * (a) 再配布に伴うドキュメント(利用者マニュアルなど)に,上記の著
|
---|
20 | * 作権表示,この利用条件および下記の無保証規定を掲載すること.
|
---|
21 | * (b) 再配布の形態を,別に定める方法によって,TOPPERSプロジェクトに
|
---|
22 | * 報告すること.
|
---|
23 | * (4) 本ソフトウェアの利用により直接的または間接的に生じるいかなる損
|
---|
24 | * 害からも,上記著作権者およびTOPPERSプロジェクトを免責すること.
|
---|
25 | * また,本ソフトウェアのユーザまたはエンドユーザからのいかなる理
|
---|
26 | * 由に基づく請求からも,上記著作権者およびTOPPERSプロジェクトを
|
---|
27 | * 免責すること.
|
---|
28 | *
|
---|
29 | * 本ソフトウェアは,無保証で提供されているものである.上記著作権者お
|
---|
30 | * よびTOPPERSプロジェクトは,本ソフトウェアに関して,特定の使用目的
|
---|
31 | * に対する適合性も含めて,いかなる保証も行わない.また,本ソフトウェ
|
---|
32 | * アの利用により直接的または間接的に生じたいかなる損害に関しても,そ
|
---|
33 | * の責任を負わない.
|
---|
34 | *
|
---|
35 | * @(#) $Id$
|
---|
36 | */
|
---|
37 |
|
---|
38 | #include "shellif.h"
|
---|
39 | #include "target_syssvc.h"
|
---|
40 | #include "kernel_cfg.h"
|
---|
41 |
|
---|
42 | extern int shellcmd_exit_code;
|
---|
43 | extern jmp_buf shellcmd_exit;
|
---|
44 |
|
---|
45 | void shell_abort()
|
---|
46 | {
|
---|
47 | shellcmd_exit_code = -1;
|
---|
48 | longjmp(shellcmd_exit, 1);
|
---|
49 | }
|
---|
50 |
|
---|
51 | void shell_exit(int exitcd)
|
---|
52 | {
|
---|
53 | shellcmd_exit_code = exitcd;
|
---|
54 | longjmp(shellcmd_exit, 1);
|
---|
55 | }
|
---|
56 |
|
---|
57 | void shell_exit_group(int exitcd)
|
---|
58 | {
|
---|
59 | shellcmd_exit_code = exitcd;
|
---|
60 | longjmp(shellcmd_exit, 1);
|
---|
61 | }
|
---|
62 |
|
---|
63 | const struct utsname host_name = {
|
---|
64 | "TOPPERS/ASP3",
|
---|
65 | TARGET_NAME,
|
---|
66 | "3.5.0",
|
---|
67 | "3.5.0",
|
---|
68 | TARGET_NAME,
|
---|
69 | "toppers.jp"
|
---|
70 | };
|
---|
71 |
|
---|
72 | int shell_uname(struct utsname *uts)
|
---|
73 | {
|
---|
74 | memcpy(uts, &host_name, sizeof(host_name));
|
---|
75 | return 0;
|
---|
76 | }
|
---|
77 |
|
---|
78 | int shell_clock_getres(clockid_t clk_id, struct timespec *res)
|
---|
79 | {
|
---|
80 | if ((clk_id != CLOCK_REALTIME) && (clk_id != CLOCK_MONOTONIC))
|
---|
81 | return -EINVAL;
|
---|
82 |
|
---|
83 | memset(&res->tv_sec, 0xFF, sizeof(res->tv_sec));
|
---|
84 | res->tv_nsec = 0;
|
---|
85 |
|
---|
86 | return 0;
|
---|
87 | }
|
---|
88 |
|
---|
89 | int shell_clock_gettime(clockid_t clk_id, struct timespec *tp)
|
---|
90 | {
|
---|
91 | SYSTIM now = 0;
|
---|
92 |
|
---|
93 | if ((clk_id != CLOCK_REALTIME) && (clk_id != CLOCK_MONOTONIC))
|
---|
94 | return -EINVAL;
|
---|
95 |
|
---|
96 | get_tim(&now);
|
---|
97 | tp->tv_sec = now / 1000000;
|
---|
98 | tp->tv_nsec = (now % 1000000) * 1000;
|
---|
99 |
|
---|
100 | return 0;
|
---|
101 | }
|
---|
102 |
|
---|
103 | int shell_clock_settime(clockid_t clk_id, const struct timespec *tp)
|
---|
104 | {
|
---|
105 | if ((clk_id != CLOCK_REALTIME) && (clk_id != CLOCK_MONOTONIC))
|
---|
106 | return -EINVAL;
|
---|
107 |
|
---|
108 | SYSTIM time;
|
---|
109 | ER ret;
|
---|
110 |
|
---|
111 | time = (tp->tv_sec * 1000000ll) + (tp->tv_nsec / 1000ll);
|
---|
112 |
|
---|
113 | ret = set_tim(time);
|
---|
114 | if (ret != E_OK) {
|
---|
115 | return -EPERM;
|
---|
116 | }
|
---|
117 |
|
---|
118 | return 0;
|
---|
119 | }
|
---|
120 |
|
---|
121 | sigset_t g_sigmask;
|
---|
122 |
|
---|
123 | int shell_sigprocmask(int how, const sigset_t *restrict set, sigset_t *restrict old)
|
---|
124 | {
|
---|
125 | if (old != NULL)
|
---|
126 | memcpy(old, &g_sigmask, sizeof(sigset_t));
|
---|
127 |
|
---|
128 | switch (how) {
|
---|
129 | case SIG_BLOCK:
|
---|
130 | for (int i = 0; i < sizeof(g_sigmask.__bits) / sizeof(g_sigmask.__bits[0]); i++) {
|
---|
131 | g_sigmask.__bits[i] |= set->__bits[i];
|
---|
132 | }
|
---|
133 | break;
|
---|
134 | case SIG_UNBLOCK:
|
---|
135 | for (int i = 0; i < sizeof(g_sigmask.__bits) / sizeof(g_sigmask.__bits[0]); i++) {
|
---|
136 | g_sigmask.__bits[i] &= ~set->__bits[i];
|
---|
137 | }
|
---|
138 | break;
|
---|
139 | case SIG_SETMASK:
|
---|
140 | memcpy(&g_sigmask, set, sizeof(sigset_t));
|
---|
141 | break;
|
---|
142 | default:
|
---|
143 | return -EINVAL;
|
---|
144 | }
|
---|
145 |
|
---|
146 | return 0;
|
---|
147 | }
|
---|
148 |
|
---|
149 | // musl-1.1.18\src\internal\ksigaction.h
|
---|
150 | struct k_sigaction {
|
---|
151 | void(*handler)(int);
|
---|
152 | unsigned long flags;
|
---|
153 | void(*restorer)(void);
|
---|
154 | unsigned mask[2];
|
---|
155 | };
|
---|
156 |
|
---|
157 | struct k_sigaction sigtable[7];
|
---|
158 |
|
---|
159 | int shell_sigaction(int sig, const struct k_sigaction *restrict sa,
|
---|
160 | struct k_sigaction *restrict old, size_t size)
|
---|
161 | {
|
---|
162 | struct k_sigaction *sat;
|
---|
163 |
|
---|
164 | switch(sig){
|
---|
165 | case SIGALRM:
|
---|
166 | sat = &sigtable[0];
|
---|
167 | break;
|
---|
168 | case SIGFPE:
|
---|
169 | sat = &sigtable[1];
|
---|
170 | break;
|
---|
171 | case SIGILL:
|
---|
172 | sat = &sigtable[2];
|
---|
173 | break;
|
---|
174 | case SIGSEGV:
|
---|
175 | sat = &sigtable[3];
|
---|
176 | break;
|
---|
177 | case SIGBUS:
|
---|
178 | sat = &sigtable[4];
|
---|
179 | break;
|
---|
180 | case SIGABRT:
|
---|
181 | sat = &sigtable[5];
|
---|
182 | break;
|
---|
183 | case SIGPIPE:
|
---|
184 | sat = &sigtable[6];
|
---|
185 | break;
|
---|
186 | default:
|
---|
187 | return -EINVAL;
|
---|
188 | }
|
---|
189 |
|
---|
190 | if (old != NULL)
|
---|
191 | memcpy(old, sat, offsetof(struct k_sigaction, mask) + size);
|
---|
192 |
|
---|
193 | memcpy(sat, sa, offsetof(struct k_sigaction, mask) + size);
|
---|
194 |
|
---|
195 | return 0;
|
---|
196 | }
|
---|
197 |
|
---|
198 | int shell_madvise(void *a, size_t b, int c)
|
---|
199 | {
|
---|
200 | return 0;
|
---|
201 | }
|
---|
202 |
|
---|
203 | int shell_gettid()
|
---|
204 | {
|
---|
205 | ID tskid;
|
---|
206 | ER ret;
|
---|
207 |
|
---|
208 | ret = get_tid(&tskid);
|
---|
209 | if (ret != E_OK)
|
---|
210 | return -1;
|
---|
211 |
|
---|
212 | return tskid;
|
---|
213 | }
|
---|
214 |
|
---|
215 | int shell_tkill(int tid, int sig)
|
---|
216 | {
|
---|
217 | if ((tid == SHELLCMD_TASK) && (sig == SIGABRT)) {
|
---|
218 | shell_abort();
|
---|
219 | }
|
---|
220 |
|
---|
221 | no_implement("tkill");
|
---|
222 | return -1;
|
---|
223 | }
|
---|
224 |
|
---|
225 | int shell_kill(int pid, int sig)
|
---|
226 | {
|
---|
227 | DebugBreak();
|
---|
228 | return -1;
|
---|
229 | }
|
---|
230 |
|
---|
231 | int shell_gettimeofday(struct timeval *tv, void *tzvp)
|
---|
232 | {
|
---|
233 | SYSTIM time;
|
---|
234 | if (!tv) return 0;
|
---|
235 | get_tim(&time);
|
---|
236 | tv->tv_sec = time / 1000000;
|
---|
237 | tv->tv_usec = time - (tv->tv_sec * 1000000);
|
---|
238 | return 0;
|
---|
239 | }
|
---|
240 |
|
---|
241 | int shell_nanosleep(const struct timespec *req, struct timespec *rem)
|
---|
242 | {
|
---|
243 | ER ret;
|
---|
244 | TMO tmo;
|
---|
245 | SYSTIM prev, now, diff;
|
---|
246 |
|
---|
247 | if ((req == NULL) || (req->tv_nsec < 0) || (req->tv_nsec >= 1000000000))
|
---|
248 | return -EINVAL;
|
---|
249 |
|
---|
250 | get_tim(&prev);
|
---|
251 |
|
---|
252 | tmo = req->tv_sec * 1000000 + req->tv_nsec / 1000;
|
---|
253 | ret = tslp_tsk(tmo);
|
---|
254 | if (ret == E_OK) {
|
---|
255 | if (rem != NULL) {
|
---|
256 | get_tim(&now);
|
---|
257 | diff = now - prev;
|
---|
258 | rem->tv_sec = diff / 1000000ll;
|
---|
259 | rem->tv_nsec = (diff - (rem->tv_sec * 1000000ll)) * 1000ll;
|
---|
260 | }
|
---|
261 | return 0;
|
---|
262 | }
|
---|
263 | else if (ret == E_TMOUT) {
|
---|
264 | if (rem != NULL) {
|
---|
265 | rem->tv_sec = 0;
|
---|
266 | rem->tv_nsec = 0;
|
---|
267 | }
|
---|
268 | return 0;
|
---|
269 | }
|
---|
270 |
|
---|
271 | return -EFAULT;
|
---|
272 | }
|
---|
273 |
|
---|
274 | ssize_t shell_getrandom(void *buf, size_t buflen, unsigned int flags)
|
---|
275 | {
|
---|
276 | SYSTIM now;
|
---|
277 | int32_t i;
|
---|
278 | int *output = (int *)buf;
|
---|
279 | size_t sz = buflen / 4;
|
---|
280 |
|
---|
281 | get_tim(&now);
|
---|
282 | srand(now);
|
---|
283 |
|
---|
284 | for (i = 0; i < sz; i++)
|
---|
285 | output[i] = rand();
|
---|
286 |
|
---|
287 | for (i = 4 * sz; i < buflen; i++)
|
---|
288 | ((char *)buf)[i] = rand();
|
---|
289 |
|
---|
290 | return buflen;
|
---|
291 | }
|
---|
292 |
|
---|
293 | extern uint32_t __CmdBase;
|
---|
294 | extern uint32_t __CmdLimit;
|
---|
295 |
|
---|
296 | void *shell_brk(void *addr)
|
---|
297 | {
|
---|
298 | if (addr == 0) {
|
---|
299 | return (void *)((intptr_t)&__CmdBase + 0x40000);
|
---|
300 | }
|
---|
301 | if ((addr >= (intptr_t)&__CmdBase + 0x40000) && (addr < &__CmdLimit)) {
|
---|
302 | return addr;
|
---|
303 | }
|
---|
304 | return (void *)-1;
|
---|
305 | }
|
---|
306 |
|
---|
307 | void *shell_mmap2(void *start, size_t length, int prot, int flags, int fd, off_t pgoffset)
|
---|
308 | {
|
---|
309 | if (fd != -1)
|
---|
310 | return -EINVAL;
|
---|
311 |
|
---|
312 | if ((length >= 0) && (length <= (intptr_t)&__CmdLimit - ((intptr_t)&__CmdBase + 0x40000))) {
|
---|
313 | return &__CmdBase + 0x40000;
|
---|
314 | }
|
---|
315 | return (void *)-1;
|
---|
316 | }
|
---|
317 |
|
---|
318 | int shell_mprotect(void *addr, size_t len, int prot)
|
---|
319 | {
|
---|
320 | //if ((addr >= (intptr_t)&__CmdBase + 0x40000) && ((intptr_t)addr + len < &__CmdLimit)) {
|
---|
321 | return 0;
|
---|
322 | //}
|
---|
323 | //return -1;
|
---|
324 | }
|
---|