blob: 36637af6ce4c578a19e4133025822afe82ec6001 [file] [log] [blame]
Simon Glass7a9219c2011-10-03 19:26:44 +00001/*
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
Simon Glasse1012472012-01-10 15:54:05 -080022#include <errno.h>
Simon Glass7a9219c2011-10-03 19:26:44 +000023#include <fcntl.h>
Simon Glass70db4212012-02-15 15:51:16 -080024#include <getopt.h>
Simon Glass7a9219c2011-10-03 19:26:44 +000025#include <stdlib.h>
Mike Frysingerab06a752011-10-26 00:21:29 +000026#include <termios.h>
Matthias Weisserd99a6872011-11-29 12:16:40 +010027#include <time.h>
Simon Glasse1012472012-01-10 15:54:05 -080028#include <unistd.h>
Matthias Weisser21899b12011-11-05 11:40:34 +010029#include <sys/mman.h>
Simon Glasse1012472012-01-10 15:54:05 -080030#include <sys/stat.h>
Simon Glass3bdf56b2012-01-10 15:54:06 -080031#include <sys/time.h>
Simon Glasse1012472012-01-10 15:54:05 -080032#include <sys/types.h>
Matthias Weisserd99a6872011-11-29 12:16:40 +010033#include <linux/types.h>
Simon Glass7a9219c2011-10-03 19:26:44 +000034
Simon Glass70db4212012-02-15 15:51:16 -080035#include <asm/getopt.h>
36#include <asm/sections.h>
37#include <asm/state.h>
Simon Glass7a9219c2011-10-03 19:26:44 +000038#include <os.h>
39
40/* Operating System Interface */
41
42ssize_t os_read(int fd, void *buf, size_t count)
43{
44 return read(fd, buf, count);
45}
46
47ssize_t os_write(int fd, const void *buf, size_t count)
48{
49 return write(fd, buf, count);
50}
51
Mike Frysingere2dcefc2011-10-25 13:02:58 +020052off_t os_lseek(int fd, off_t offset, int whence)
53{
54 if (whence == OS_SEEK_SET)
55 whence = SEEK_SET;
56 else if (whence == OS_SEEK_CUR)
57 whence = SEEK_CUR;
58 else if (whence == OS_SEEK_END)
59 whence = SEEK_END;
60 else
61 os_exit(1);
62 return lseek(fd, offset, whence);
63}
64
Simon Glassd9165152012-02-20 23:56:58 -050065int os_open(const char *pathname, int os_flags)
Simon Glass7a9219c2011-10-03 19:26:44 +000066{
Simon Glassd9165152012-02-20 23:56:58 -050067 int flags;
68
69 switch (os_flags & OS_O_MASK) {
70 case OS_O_RDONLY:
71 default:
72 flags = O_RDONLY;
73 break;
74
75 case OS_O_WRONLY:
76 flags = O_WRONLY;
77 break;
78
79 case OS_O_RDWR:
80 flags = O_RDWR;
81 break;
82 }
83
84 if (os_flags & OS_O_CREAT)
85 flags |= O_CREAT;
86
87 return open(pathname, flags, 0777);
Simon Glass7a9219c2011-10-03 19:26:44 +000088}
89
90int os_close(int fd)
91{
92 return close(fd);
93}
94
95void os_exit(int exit_code)
96{
97 exit(exit_code);
98}
Mike Frysingerab06a752011-10-26 00:21:29 +000099
100/* Restore tty state when we exit */
101static struct termios orig_term;
102
103static void os_fd_restore(void)
104{
105 tcsetattr(0, TCSANOW, &orig_term);
106}
107
108/* Put tty into raw mode so <tab> and <ctrl+c> work */
109void os_tty_raw(int fd)
110{
111 static int setup = 0;
112 struct termios term;
113
114 if (setup)
115 return;
116 setup = 1;
117
118 /* If not a tty, don't complain */
119 if (tcgetattr(fd, &orig_term))
120 return;
121
122 term = orig_term;
123 term.c_iflag = IGNBRK | IGNPAR;
124 term.c_oflag = OPOST | ONLCR;
125 term.c_cflag = CS8 | CREAD | CLOCAL;
126 term.c_lflag = 0;
127 if (tcsetattr(fd, TCSANOW, &term))
128 return;
129
130 atexit(os_fd_restore);
131}
Matthias Weisser21899b12011-11-05 11:40:34 +0100132
133void *os_malloc(size_t length)
134{
135 return mmap(NULL, length, PROT_READ | PROT_WRITE,
136 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
137}
Matthias Weisserd99a6872011-11-29 12:16:40 +0100138
139void os_usleep(unsigned long usec)
140{
141 usleep(usec);
142}
143
144u64 os_get_nsec(void)
145{
146#if defined(CLOCK_MONOTONIC) && defined(_POSIX_MONOTONIC_CLOCK)
147 struct timespec tp;
148 if (EINVAL == clock_gettime(CLOCK_MONOTONIC, &tp)) {
149 struct timeval tv;
150
151 gettimeofday(&tv, NULL);
152 tp.tv_sec = tv.tv_sec;
153 tp.tv_nsec = tv.tv_usec * 1000;
154 }
155 return tp.tv_sec * 1000000000ULL + tp.tv_nsec;
156#else
157 struct timeval tv;
158 gettimeofday(&tv, NULL);
159 return tv.tv_sec * 1000000000ULL + tv.tv_usec * 1000;
160#endif
161}
Simon Glass70db4212012-02-15 15:51:16 -0800162
163static char *short_opts;
164static struct option *long_opts;
165
166int os_parse_args(struct sandbox_state *state, int argc, char *argv[])
167{
168 struct sb_cmdline_option **sb_opt = __u_boot_sandbox_option_start;
169 size_t num_options = __u_boot_sandbox_option_count();
170 size_t i;
171
172 int hidden_short_opt;
173 size_t si;
174
175 int c;
176
177 if (short_opts || long_opts)
178 return 1;
179
180 state->argc = argc;
181 state->argv = argv;
182
183 /* dynamically construct the arguments to the system getopt_long */
184 short_opts = os_malloc(sizeof(*short_opts) * num_options * 2 + 1);
185 long_opts = os_malloc(sizeof(*long_opts) * num_options);
186 if (!short_opts || !long_opts)
187 return 1;
188
189 /*
190 * getopt_long requires "val" to be unique (since that is what the
191 * func returns), so generate unique values automatically for flags
192 * that don't have a short option. pick 0x100 as that is above the
193 * single byte range (where ASCII/ISO-XXXX-X charsets live).
194 */
195 hidden_short_opt = 0x100;
196 si = 0;
197 for (i = 0; i < num_options; ++i) {
198 long_opts[i].name = sb_opt[i]->flag;
199 long_opts[i].has_arg = sb_opt[i]->has_arg ?
200 required_argument : no_argument;
201 long_opts[i].flag = NULL;
202
203 if (sb_opt[i]->flag_short) {
204 short_opts[si++] = long_opts[i].val = sb_opt[i]->flag_short;
205 if (long_opts[i].has_arg == required_argument)
206 short_opts[si++] = ':';
207 } else
208 long_opts[i].val = sb_opt[i]->flag_short = hidden_short_opt++;
209 }
210 short_opts[si] = '\0';
211
212 /* we need to handle output ourselves since u-boot provides printf */
213 opterr = 0;
214
215 /*
216 * walk all of the options the user gave us on the command line,
217 * figure out what u-boot option structure they belong to (via
218 * the unique short val key), and call the appropriate callback.
219 */
220 while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) {
221 for (i = 0; i < num_options; ++i) {
222 if (sb_opt[i]->flag_short == c) {
223 if (sb_opt[i]->callback(state, optarg)) {
224 state->parse_err = sb_opt[i]->flag;
225 return 0;
226 }
227 break;
228 }
229 }
230 if (i == num_options) {
231 /*
232 * store the faulting flag for later display. we have to
233 * store the flag itself as the getopt parsing itself is
234 * tricky: need to handle the following flags (assume all
235 * of the below are unknown):
236 * -a optopt='a' optind=<next>
237 * -abbbb optopt='a' optind=<this>
238 * -aaaaa optopt='a' optind=<this>
239 * --a optopt=0 optind=<this>
240 * as you can see, it is impossible to determine the exact
241 * faulting flag without doing the parsing ourselves, so
242 * we just report the specific flag that failed.
243 */
244 if (optopt) {
245 static char parse_err[3] = { '-', 0, '\0', };
246 parse_err[1] = optopt;
247 state->parse_err = parse_err;
248 } else
249 state->parse_err = argv[optind - 1];
250 break;
251 }
252 }
253
254 return 0;
255}