2 * Copyright (c) 2011 The Chromium OS Authors.
3 * SPDX-License-Identifier: GPL-2.0+
19 #include <sys/types.h>
20 #include <linux/types.h>
22 #include <asm/getopt.h>
23 #include <asm/sections.h>
24 #include <asm/state.h>
27 /* Operating System Interface */
29 ssize_t os_read(int fd, void *buf, size_t count)
31 return read(fd, buf, count);
34 ssize_t os_read_no_block(int fd, void *buf, size_t count)
36 const int flags = fcntl(fd, F_GETFL, 0);
38 fcntl(fd, F_SETFL, flags | O_NONBLOCK);
39 return os_read(fd, buf, count);
42 ssize_t os_write(int fd, const void *buf, size_t count)
44 return write(fd, buf, count);
47 off_t os_lseek(int fd, off_t offset, int whence)
49 if (whence == OS_SEEK_SET)
51 else if (whence == OS_SEEK_CUR)
53 else if (whence == OS_SEEK_END)
57 return lseek(fd, offset, whence);
60 int os_open(const char *pathname, int os_flags)
64 switch (os_flags & OS_O_MASK) {
79 if (os_flags & OS_O_CREAT)
82 return open(pathname, flags, 0777);
90 void os_exit(int exit_code)
95 /* Restore tty state when we exit */
96 static struct termios orig_term;
98 static void os_fd_restore(void)
100 tcsetattr(0, TCSANOW, &orig_term);
103 /* Put tty into raw mode so <tab> and <ctrl+c> work */
104 void os_tty_raw(int fd)
106 static int setup = 0;
113 /* If not a tty, don't complain */
114 if (tcgetattr(fd, &orig_term))
118 term.c_iflag = IGNBRK | IGNPAR;
119 term.c_oflag = OPOST | ONLCR;
120 term.c_cflag = CS8 | CREAD | CLOCAL;
122 if (tcsetattr(fd, TCSANOW, &term))
125 atexit(os_fd_restore);
128 void *os_malloc(size_t length)
130 return mmap(NULL, length, PROT_READ | PROT_WRITE,
131 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
134 void os_usleep(unsigned long usec)
139 u64 __attribute__((no_instrument_function)) os_get_nsec(void)
141 #if defined(CLOCK_MONOTONIC) && defined(_POSIX_MONOTONIC_CLOCK)
143 if (EINVAL == clock_gettime(CLOCK_MONOTONIC, &tp)) {
146 gettimeofday(&tv, NULL);
147 tp.tv_sec = tv.tv_sec;
148 tp.tv_nsec = tv.tv_usec * 1000;
150 return tp.tv_sec * 1000000000ULL + tp.tv_nsec;
153 gettimeofday(&tv, NULL);
154 return tv.tv_sec * 1000000000ULL + tv.tv_usec * 1000;
158 static char *short_opts;
159 static struct option *long_opts;
161 int os_parse_args(struct sandbox_state *state, int argc, char *argv[])
163 struct sb_cmdline_option **sb_opt = __u_boot_sandbox_option_start;
164 size_t num_options = __u_boot_sandbox_option_count();
167 int hidden_short_opt;
172 if (short_opts || long_opts)
178 /* dynamically construct the arguments to the system getopt_long */
179 short_opts = os_malloc(sizeof(*short_opts) * num_options * 2 + 1);
180 long_opts = os_malloc(sizeof(*long_opts) * num_options);
181 if (!short_opts || !long_opts)
185 * getopt_long requires "val" to be unique (since that is what the
186 * func returns), so generate unique values automatically for flags
187 * that don't have a short option. pick 0x100 as that is above the
188 * single byte range (where ASCII/ISO-XXXX-X charsets live).
190 hidden_short_opt = 0x100;
192 for (i = 0; i < num_options; ++i) {
193 long_opts[i].name = sb_opt[i]->flag;
194 long_opts[i].has_arg = sb_opt[i]->has_arg ?
195 required_argument : no_argument;
196 long_opts[i].flag = NULL;
198 if (sb_opt[i]->flag_short) {
199 short_opts[si++] = long_opts[i].val = sb_opt[i]->flag_short;
200 if (long_opts[i].has_arg == required_argument)
201 short_opts[si++] = ':';
203 long_opts[i].val = sb_opt[i]->flag_short = hidden_short_opt++;
205 short_opts[si] = '\0';
207 /* we need to handle output ourselves since u-boot provides printf */
211 * walk all of the options the user gave us on the command line,
212 * figure out what u-boot option structure they belong to (via
213 * the unique short val key), and call the appropriate callback.
215 while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) {
216 for (i = 0; i < num_options; ++i) {
217 if (sb_opt[i]->flag_short == c) {
218 if (sb_opt[i]->callback(state, optarg)) {
219 state->parse_err = sb_opt[i]->flag;
225 if (i == num_options) {
227 * store the faulting flag for later display. we have to
228 * store the flag itself as the getopt parsing itself is
229 * tricky: need to handle the following flags (assume all
230 * of the below are unknown):
231 * -a optopt='a' optind=<next>
232 * -abbbb optopt='a' optind=<this>
233 * -aaaaa optopt='a' optind=<this>
234 * --a optopt=0 optind=<this>
235 * as you can see, it is impossible to determine the exact
236 * faulting flag without doing the parsing ourselves, so
237 * we just report the specific flag that failed.
240 static char parse_err[3] = { '-', 0, '\0', };
241 parse_err[1] = optopt;
242 state->parse_err = parse_err;
244 state->parse_err = argv[optind - 1];
252 void os_dirent_free(struct os_dirent_node *node)
254 struct os_dirent_node *next;
263 int os_dirent_ls(const char *dirname, struct os_dirent_node **headp)
265 struct dirent entry, *result;
266 struct os_dirent_node *head, *node, *next;
274 dir = opendir(dirname);
278 /* Create a buffer for the maximum filename length */
279 len = sizeof(entry.d_name) + strlen(dirname) + 2;
286 for (node = head = NULL;; node = next) {
287 ret = readdir_r(dir, &entry, &result);
290 next = malloc(sizeof(*node) + strlen(entry.d_name) + 1);
292 os_dirent_free(head);
296 strcpy(next->name, entry.d_name);
297 switch (entry.d_type) {
299 next->type = OS_FILET_REG;
302 next->type = OS_FILET_DIR;
305 next->type = OS_FILET_LNK;
309 snprintf(fname, len, "%s/%s", dirname, next->name);
310 if (!stat(fname, &buf))
311 next->size = buf.st_size;
324 const char *os_dirent_typename[OS_FILET_COUNT] = {
331 const char *os_dirent_get_typename(enum os_dirent_t type)
333 if (type >= 0 && type < OS_FILET_COUNT)
334 return os_dirent_typename[type];
336 return os_dirent_typename[OS_FILET_UNKNOWN];
339 ssize_t os_get_filesize(const char *fname)
344 ret = stat(fname, &buf);