2 * Copyright (c) 2011 The Chromium OS Authors.
3 * See file CREDITS for list of people who contributed to this
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.
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.
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,
35 #include <sys/types.h>
36 #include <linux/types.h>
38 #include <asm/getopt.h>
39 #include <asm/sections.h>
40 #include <asm/state.h>
43 /* Operating System Interface */
45 ssize_t os_read(int fd, void *buf, size_t count)
47 return read(fd, buf, count);
50 ssize_t os_read_no_block(int fd, void *buf, size_t count)
52 const int flags = fcntl(fd, F_GETFL, 0);
54 fcntl(fd, F_SETFL, flags | O_NONBLOCK);
55 return os_read(fd, buf, count);
58 ssize_t os_write(int fd, const void *buf, size_t count)
60 return write(fd, buf, count);
63 off_t os_lseek(int fd, off_t offset, int whence)
65 if (whence == OS_SEEK_SET)
67 else if (whence == OS_SEEK_CUR)
69 else if (whence == OS_SEEK_END)
73 return lseek(fd, offset, whence);
76 int os_open(const char *pathname, int os_flags)
80 switch (os_flags & OS_O_MASK) {
95 if (os_flags & OS_O_CREAT)
98 return open(pathname, flags, 0777);
106 void os_exit(int exit_code)
111 /* Restore tty state when we exit */
112 static struct termios orig_term;
114 static void os_fd_restore(void)
116 tcsetattr(0, TCSANOW, &orig_term);
119 /* Put tty into raw mode so <tab> and <ctrl+c> work */
120 void os_tty_raw(int fd)
122 static int setup = 0;
129 /* If not a tty, don't complain */
130 if (tcgetattr(fd, &orig_term))
134 term.c_iflag = IGNBRK | IGNPAR;
135 term.c_oflag = OPOST | ONLCR;
136 term.c_cflag = CS8 | CREAD | CLOCAL;
138 if (tcsetattr(fd, TCSANOW, &term))
141 atexit(os_fd_restore);
144 void *os_malloc(size_t length)
146 return mmap(NULL, length, PROT_READ | PROT_WRITE,
147 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
150 void os_usleep(unsigned long usec)
155 u64 os_get_nsec(void)
157 #if defined(CLOCK_MONOTONIC) && defined(_POSIX_MONOTONIC_CLOCK)
159 if (EINVAL == clock_gettime(CLOCK_MONOTONIC, &tp)) {
162 gettimeofday(&tv, NULL);
163 tp.tv_sec = tv.tv_sec;
164 tp.tv_nsec = tv.tv_usec * 1000;
166 return tp.tv_sec * 1000000000ULL + tp.tv_nsec;
169 gettimeofday(&tv, NULL);
170 return tv.tv_sec * 1000000000ULL + tv.tv_usec * 1000;
174 static char *short_opts;
175 static struct option *long_opts;
177 int os_parse_args(struct sandbox_state *state, int argc, char *argv[])
179 struct sb_cmdline_option **sb_opt = __u_boot_sandbox_option_start;
180 size_t num_options = __u_boot_sandbox_option_count();
183 int hidden_short_opt;
188 if (short_opts || long_opts)
194 /* dynamically construct the arguments to the system getopt_long */
195 short_opts = os_malloc(sizeof(*short_opts) * num_options * 2 + 1);
196 long_opts = os_malloc(sizeof(*long_opts) * num_options);
197 if (!short_opts || !long_opts)
201 * getopt_long requires "val" to be unique (since that is what the
202 * func returns), so generate unique values automatically for flags
203 * that don't have a short option. pick 0x100 as that is above the
204 * single byte range (where ASCII/ISO-XXXX-X charsets live).
206 hidden_short_opt = 0x100;
208 for (i = 0; i < num_options; ++i) {
209 long_opts[i].name = sb_opt[i]->flag;
210 long_opts[i].has_arg = sb_opt[i]->has_arg ?
211 required_argument : no_argument;
212 long_opts[i].flag = NULL;
214 if (sb_opt[i]->flag_short) {
215 short_opts[si++] = long_opts[i].val = sb_opt[i]->flag_short;
216 if (long_opts[i].has_arg == required_argument)
217 short_opts[si++] = ':';
219 long_opts[i].val = sb_opt[i]->flag_short = hidden_short_opt++;
221 short_opts[si] = '\0';
223 /* we need to handle output ourselves since u-boot provides printf */
227 * walk all of the options the user gave us on the command line,
228 * figure out what u-boot option structure they belong to (via
229 * the unique short val key), and call the appropriate callback.
231 while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) {
232 for (i = 0; i < num_options; ++i) {
233 if (sb_opt[i]->flag_short == c) {
234 if (sb_opt[i]->callback(state, optarg)) {
235 state->parse_err = sb_opt[i]->flag;
241 if (i == num_options) {
243 * store the faulting flag for later display. we have to
244 * store the flag itself as the getopt parsing itself is
245 * tricky: need to handle the following flags (assume all
246 * of the below are unknown):
247 * -a optopt='a' optind=<next>
248 * -abbbb optopt='a' optind=<this>
249 * -aaaaa optopt='a' optind=<this>
250 * --a optopt=0 optind=<this>
251 * as you can see, it is impossible to determine the exact
252 * faulting flag without doing the parsing ourselves, so
253 * we just report the specific flag that failed.
256 static char parse_err[3] = { '-', 0, '\0', };
257 parse_err[1] = optopt;
258 state->parse_err = parse_err;
260 state->parse_err = argv[optind - 1];
268 void os_dirent_free(struct os_dirent_node *node)
270 struct os_dirent_node *next;
279 int os_dirent_ls(const char *dirname, struct os_dirent_node **headp)
281 struct dirent entry, *result;
282 struct os_dirent_node *head, *node, *next;
290 dir = opendir(dirname);
294 /* Create a buffer for the maximum filename length */
295 len = sizeof(entry.d_name) + strlen(dirname) + 2;
302 for (node = head = NULL;; node = next) {
303 ret = readdir_r(dir, &entry, &result);
306 next = malloc(sizeof(*node) + strlen(entry.d_name) + 1);
308 os_dirent_free(head);
312 strcpy(next->name, entry.d_name);
313 switch (entry.d_type) {
315 next->type = OS_FILET_REG;
318 next->type = OS_FILET_DIR;
321 next->type = OS_FILET_LNK;
325 snprintf(fname, len, "%s/%s", dirname, next->name);
326 if (!stat(fname, &buf))
327 next->size = buf.st_size;
340 const char *os_dirent_typename[OS_FILET_COUNT] = {
347 const char *os_dirent_get_typename(enum os_dirent_t type)
349 if (type >= 0 && type < OS_FILET_COUNT)
350 return os_dirent_typename[type];
352 return os_dirent_typename[OS_FILET_UNKNOWN];
355 ssize_t os_get_filesize(const char *fname)
360 ret = stat(fname, &buf);