]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - arch/sandbox/cpu/os.c
sandbox: Improve sandbox serial port keyboard interface
[karo-tx-uboot.git] / arch / sandbox / cpu / os.c
1 /*
2  * Copyright (c) 2011 The Chromium OS Authors.
3  * See file CREDITS for list of people who contributed to this
4  * project.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
19  * MA 02111-1307 USA
20  */
21
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <getopt.h>
25 #include <stdlib.h>
26 #include <termios.h>
27 #include <time.h>
28 #include <unistd.h>
29 #include <sys/mman.h>
30 #include <sys/stat.h>
31 #include <sys/time.h>
32 #include <sys/types.h>
33 #include <linux/types.h>
34
35 #include <asm/getopt.h>
36 #include <asm/sections.h>
37 #include <asm/state.h>
38 #include <os.h>
39
40 /* Operating System Interface */
41
42 ssize_t os_read(int fd, void *buf, size_t count)
43 {
44         return read(fd, buf, count);
45 }
46
47 ssize_t os_read_no_block(int fd, void *buf, size_t count)
48 {
49         const int flags = fcntl(fd, F_GETFL, 0);
50
51         fcntl(fd, F_SETFL, flags | O_NONBLOCK);
52         return os_read(fd, buf, count);
53 }
54
55 ssize_t os_write(int fd, const void *buf, size_t count)
56 {
57         return write(fd, buf, count);
58 }
59
60 off_t os_lseek(int fd, off_t offset, int whence)
61 {
62         if (whence == OS_SEEK_SET)
63                 whence = SEEK_SET;
64         else if (whence == OS_SEEK_CUR)
65                 whence = SEEK_CUR;
66         else if (whence == OS_SEEK_END)
67                 whence = SEEK_END;
68         else
69                 os_exit(1);
70         return lseek(fd, offset, whence);
71 }
72
73 int os_open(const char *pathname, int os_flags)
74 {
75         int flags;
76
77         switch (os_flags & OS_O_MASK) {
78         case OS_O_RDONLY:
79         default:
80                 flags = O_RDONLY;
81                 break;
82
83         case OS_O_WRONLY:
84                 flags = O_WRONLY;
85                 break;
86
87         case OS_O_RDWR:
88                 flags = O_RDWR;
89                 break;
90         }
91
92         if (os_flags & OS_O_CREAT)
93                 flags |= O_CREAT;
94
95         return open(pathname, flags, 0777);
96 }
97
98 int os_close(int fd)
99 {
100         return close(fd);
101 }
102
103 void os_exit(int exit_code)
104 {
105         exit(exit_code);
106 }
107
108 /* Restore tty state when we exit */
109 static struct termios orig_term;
110
111 static void os_fd_restore(void)
112 {
113         tcsetattr(0, TCSANOW, &orig_term);
114 }
115
116 /* Put tty into raw mode so <tab> and <ctrl+c> work */
117 void os_tty_raw(int fd)
118 {
119         static int setup = 0;
120         struct termios term;
121
122         if (setup)
123                 return;
124         setup = 1;
125
126         /* If not a tty, don't complain */
127         if (tcgetattr(fd, &orig_term))
128                 return;
129
130         term = orig_term;
131         term.c_iflag = IGNBRK | IGNPAR;
132         term.c_oflag = OPOST | ONLCR;
133         term.c_cflag = CS8 | CREAD | CLOCAL;
134         term.c_lflag = 0;
135         if (tcsetattr(fd, TCSANOW, &term))
136                 return;
137
138         atexit(os_fd_restore);
139 }
140
141 void *os_malloc(size_t length)
142 {
143         return mmap(NULL, length, PROT_READ | PROT_WRITE,
144                         MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
145 }
146
147 void os_usleep(unsigned long usec)
148 {
149         usleep(usec);
150 }
151
152 u64 os_get_nsec(void)
153 {
154 #if defined(CLOCK_MONOTONIC) && defined(_POSIX_MONOTONIC_CLOCK)
155         struct timespec tp;
156         if (EINVAL == clock_gettime(CLOCK_MONOTONIC, &tp)) {
157                 struct timeval tv;
158
159                 gettimeofday(&tv, NULL);
160                 tp.tv_sec = tv.tv_sec;
161                 tp.tv_nsec = tv.tv_usec * 1000;
162         }
163         return tp.tv_sec * 1000000000ULL + tp.tv_nsec;
164 #else
165         struct timeval tv;
166         gettimeofday(&tv, NULL);
167         return tv.tv_sec * 1000000000ULL + tv.tv_usec * 1000;
168 #endif
169 }
170
171 static char *short_opts;
172 static struct option *long_opts;
173
174 int os_parse_args(struct sandbox_state *state, int argc, char *argv[])
175 {
176         struct sb_cmdline_option **sb_opt = __u_boot_sandbox_option_start;
177         size_t num_options = __u_boot_sandbox_option_count();
178         size_t i;
179
180         int hidden_short_opt;
181         size_t si;
182
183         int c;
184
185         if (short_opts || long_opts)
186                 return 1;
187
188         state->argc = argc;
189         state->argv = argv;
190
191         /* dynamically construct the arguments to the system getopt_long */
192         short_opts = os_malloc(sizeof(*short_opts) * num_options * 2 + 1);
193         long_opts = os_malloc(sizeof(*long_opts) * num_options);
194         if (!short_opts || !long_opts)
195                 return 1;
196
197         /*
198          * getopt_long requires "val" to be unique (since that is what the
199          * func returns), so generate unique values automatically for flags
200          * that don't have a short option.  pick 0x100 as that is above the
201          * single byte range (where ASCII/ISO-XXXX-X charsets live).
202          */
203         hidden_short_opt = 0x100;
204         si = 0;
205         for (i = 0; i < num_options; ++i) {
206                 long_opts[i].name = sb_opt[i]->flag;
207                 long_opts[i].has_arg = sb_opt[i]->has_arg ?
208                         required_argument : no_argument;
209                 long_opts[i].flag = NULL;
210
211                 if (sb_opt[i]->flag_short) {
212                         short_opts[si++] = long_opts[i].val = sb_opt[i]->flag_short;
213                         if (long_opts[i].has_arg == required_argument)
214                                 short_opts[si++] = ':';
215                 } else
216                         long_opts[i].val = sb_opt[i]->flag_short = hidden_short_opt++;
217         }
218         short_opts[si] = '\0';
219
220         /* we need to handle output ourselves since u-boot provides printf */
221         opterr = 0;
222
223         /*
224          * walk all of the options the user gave us on the command line,
225          * figure out what u-boot option structure they belong to (via
226          * the unique short val key), and call the appropriate callback.
227          */
228         while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) {
229                 for (i = 0; i < num_options; ++i) {
230                         if (sb_opt[i]->flag_short == c) {
231                                 if (sb_opt[i]->callback(state, optarg)) {
232                                         state->parse_err = sb_opt[i]->flag;
233                                         return 0;
234                                 }
235                                 break;
236                         }
237                 }
238                 if (i == num_options) {
239                         /*
240                          * store the faulting flag for later display.  we have to
241                          * store the flag itself as the getopt parsing itself is
242                          * tricky: need to handle the following flags (assume all
243                          * of the below are unknown):
244                          *   -a        optopt='a' optind=<next>
245                          *   -abbbb    optopt='a' optind=<this>
246                          *   -aaaaa    optopt='a' optind=<this>
247                          *   --a       optopt=0   optind=<this>
248                          * as you can see, it is impossible to determine the exact
249                          * faulting flag without doing the parsing ourselves, so
250                          * we just report the specific flag that failed.
251                          */
252                         if (optopt) {
253                                 static char parse_err[3] = { '-', 0, '\0', };
254                                 parse_err[1] = optopt;
255                                 state->parse_err = parse_err;
256                         } else
257                                 state->parse_err = argv[optind - 1];
258                         break;
259                 }
260         }
261
262         return 0;
263 }