2 * Copyright (c) 2011 The Chromium OS Authors.
3 * SPDX-License-Identifier: GPL-2.0+
20 #include <sys/types.h>
21 #include <linux/types.h>
23 #include <asm/getopt.h>
24 #include <asm/sections.h>
25 #include <asm/state.h>
28 /* Operating System Interface */
30 ssize_t os_read(int fd, void *buf, size_t count)
32 return read(fd, buf, count);
35 ssize_t os_read_no_block(int fd, void *buf, size_t count)
37 const int flags = fcntl(fd, F_GETFL, 0);
39 fcntl(fd, F_SETFL, flags | O_NONBLOCK);
40 return os_read(fd, buf, count);
43 ssize_t os_write(int fd, const void *buf, size_t count)
45 return write(fd, buf, count);
48 off_t os_lseek(int fd, off_t offset, int whence)
50 if (whence == OS_SEEK_SET)
52 else if (whence == OS_SEEK_CUR)
54 else if (whence == OS_SEEK_END)
58 return lseek(fd, offset, whence);
61 int os_open(const char *pathname, int os_flags)
65 switch (os_flags & OS_O_MASK) {
80 if (os_flags & OS_O_CREAT)
83 return open(pathname, flags, 0777);
91 void os_exit(int exit_code)
96 /* Restore tty state when we exit */
97 static struct termios orig_term;
99 static void os_fd_restore(void)
101 tcsetattr(0, TCSANOW, &orig_term);
104 /* Put tty into raw mode so <tab> and <ctrl+c> work */
105 void os_tty_raw(int fd)
107 static int setup = 0;
114 /* If not a tty, don't complain */
115 if (tcgetattr(fd, &orig_term))
119 term.c_iflag = IGNBRK | IGNPAR;
120 term.c_oflag = OPOST | ONLCR;
121 term.c_cflag = CS8 | CREAD | CLOCAL;
123 if (tcsetattr(fd, TCSANOW, &term))
126 atexit(os_fd_restore);
129 void *os_malloc(size_t length)
131 return mmap(NULL, length, PROT_READ | PROT_WRITE,
132 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
135 void os_usleep(unsigned long usec)
140 uint64_t __attribute__((no_instrument_function)) os_get_nsec(void)
142 #if defined(CLOCK_MONOTONIC) && defined(_POSIX_MONOTONIC_CLOCK)
144 if (EINVAL == clock_gettime(CLOCK_MONOTONIC, &tp)) {
147 gettimeofday(&tv, NULL);
148 tp.tv_sec = tv.tv_sec;
149 tp.tv_nsec = tv.tv_usec * 1000;
151 return tp.tv_sec * 1000000000ULL + tp.tv_nsec;
154 gettimeofday(&tv, NULL);
155 return tv.tv_sec * 1000000000ULL + tv.tv_usec * 1000;
159 static char *short_opts;
160 static struct option *long_opts;
162 int os_parse_args(struct sandbox_state *state, int argc, char *argv[])
164 struct sb_cmdline_option **sb_opt = __u_boot_sandbox_option_start;
165 size_t num_options = __u_boot_sandbox_option_count();
168 int hidden_short_opt;
173 if (short_opts || long_opts)
179 /* dynamically construct the arguments to the system getopt_long */
180 short_opts = os_malloc(sizeof(*short_opts) * num_options * 2 + 1);
181 long_opts = os_malloc(sizeof(*long_opts) * num_options);
182 if (!short_opts || !long_opts)
186 * getopt_long requires "val" to be unique (since that is what the
187 * func returns), so generate unique values automatically for flags
188 * that don't have a short option. pick 0x100 as that is above the
189 * single byte range (where ASCII/ISO-XXXX-X charsets live).
191 hidden_short_opt = 0x100;
193 for (i = 0; i < num_options; ++i) {
194 long_opts[i].name = sb_opt[i]->flag;
195 long_opts[i].has_arg = sb_opt[i]->has_arg ?
196 required_argument : no_argument;
197 long_opts[i].flag = NULL;
199 if (sb_opt[i]->flag_short) {
200 short_opts[si++] = long_opts[i].val = sb_opt[i]->flag_short;
201 if (long_opts[i].has_arg == required_argument)
202 short_opts[si++] = ':';
204 long_opts[i].val = sb_opt[i]->flag_short = hidden_short_opt++;
206 short_opts[si] = '\0';
208 /* we need to handle output ourselves since u-boot provides printf */
212 * walk all of the options the user gave us on the command line,
213 * figure out what u-boot option structure they belong to (via
214 * the unique short val key), and call the appropriate callback.
216 while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) {
217 for (i = 0; i < num_options; ++i) {
218 if (sb_opt[i]->flag_short == c) {
219 if (sb_opt[i]->callback(state, optarg)) {
220 state->parse_err = sb_opt[i]->flag;
226 if (i == num_options) {
228 * store the faulting flag for later display. we have to
229 * store the flag itself as the getopt parsing itself is
230 * tricky: need to handle the following flags (assume all
231 * of the below are unknown):
232 * -a optopt='a' optind=<next>
233 * -abbbb optopt='a' optind=<this>
234 * -aaaaa optopt='a' optind=<this>
235 * --a optopt=0 optind=<this>
236 * as you can see, it is impossible to determine the exact
237 * faulting flag without doing the parsing ourselves, so
238 * we just report the specific flag that failed.
241 static char parse_err[3] = { '-', 0, '\0', };
242 parse_err[1] = optopt;
243 state->parse_err = parse_err;
245 state->parse_err = argv[optind - 1];
253 void os_dirent_free(struct os_dirent_node *node)
255 struct os_dirent_node *next;
264 int os_dirent_ls(const char *dirname, struct os_dirent_node **headp)
266 struct dirent entry, *result;
267 struct os_dirent_node *head, *node, *next;
275 dir = opendir(dirname);
279 /* Create a buffer for the maximum filename length */
280 len = sizeof(entry.d_name) + strlen(dirname) + 2;
287 for (node = head = NULL;; node = next) {
288 ret = readdir_r(dir, &entry, &result);
291 next = malloc(sizeof(*node) + strlen(entry.d_name) + 1);
293 os_dirent_free(head);
297 strcpy(next->name, entry.d_name);
298 switch (entry.d_type) {
300 next->type = OS_FILET_REG;
303 next->type = OS_FILET_DIR;
306 next->type = OS_FILET_LNK;
310 snprintf(fname, len, "%s/%s", dirname, next->name);
311 if (!stat(fname, &buf))
312 next->size = buf.st_size;
325 const char *os_dirent_typename[OS_FILET_COUNT] = {
332 const char *os_dirent_get_typename(enum os_dirent_t type)
334 if (type >= 0 && type < OS_FILET_COUNT)
335 return os_dirent_typename[type];
337 return os_dirent_typename[OS_FILET_UNKNOWN];
340 ssize_t os_get_filesize(const char *fname)
345 ret = stat(fname, &buf);