1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2011 The Chromium OS Authors.
21 #include <sys/types.h>
22 #include <linux/types.h>
24 #include <asm/getopt.h>
25 #include <asm/sections.h>
26 #include <asm/state.h>
30 /* Operating System Interface */
33 size_t length; /* number of bytes in the block */
36 ssize_t os_read(int fd, void *buf, size_t count)
38 return read(fd, buf, count);
41 ssize_t os_write(int fd, const void *buf, size_t count)
43 return write(fd, buf, count);
46 off_t os_lseek(int fd, off_t offset, int whence)
48 if (whence == OS_SEEK_SET)
50 else if (whence == OS_SEEK_CUR)
52 else if (whence == OS_SEEK_END)
56 return lseek(fd, offset, whence);
59 int os_open(const char *pathname, int os_flags)
63 switch (os_flags & OS_O_MASK) {
78 if (os_flags & OS_O_CREAT)
80 if (os_flags & OS_O_TRUNC)
83 return open(pathname, flags, 0777);
91 int os_unlink(const char *pathname)
93 return unlink(pathname);
96 void os_exit(int exit_code)
101 int os_write_file(const char *name, const void *buf, int size)
106 fd = os_open(fname, OS_O_WRONLY | OS_O_CREAT | OS_O_TRUNC);
108 printf("Cannot open file '%s'\n", fname);
111 if (os_write(fd, buf, size) != size) {
112 printf("Cannot write to file '%s'\n", fname);
116 printf("Write '%s', size %#x (%d)\n", name, size, size);
121 /* Restore tty state when we exit */
122 static struct termios orig_term;
123 static bool term_setup;
124 static bool term_nonblock;
126 void os_fd_restore(void)
131 tcsetattr(0, TCSANOW, &orig_term);
133 flags = fcntl(0, F_GETFL, 0);
134 fcntl(0, F_SETFL, flags & ~O_NONBLOCK);
140 /* Put tty into raw mode so <tab> and <ctrl+c> work */
141 void os_tty_raw(int fd, bool allow_sigs)
149 /* If not a tty, don't complain */
150 if (tcgetattr(fd, &orig_term))
154 term.c_iflag = IGNBRK | IGNPAR;
155 term.c_oflag = OPOST | ONLCR;
156 term.c_cflag = CS8 | CREAD | CLOCAL;
157 term.c_lflag = allow_sigs ? ISIG : 0;
158 if (tcsetattr(fd, TCSANOW, &term))
161 flags = fcntl(fd, F_GETFL, 0);
162 if (!(flags & O_NONBLOCK)) {
163 if (fcntl(fd, F_SETFL, flags | O_NONBLOCK))
165 term_nonblock = true;
169 atexit(os_fd_restore);
172 void *os_malloc(size_t length)
174 struct os_mem_hdr *hdr;
175 int page_size = getpagesize();
177 hdr = mmap(NULL, length + page_size,
178 PROT_READ | PROT_WRITE | PROT_EXEC,
179 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
180 if (hdr == MAP_FAILED)
182 hdr->length = length;
184 return (void *)hdr + page_size;
187 void os_free(void *ptr)
189 struct os_mem_hdr *hdr = ptr;
193 munmap(hdr, hdr->length + sizeof(*hdr));
196 void *os_realloc(void *ptr, size_t length)
198 struct os_mem_hdr *hdr = ptr;
203 buf = os_malloc(length);
207 if (length > hdr->length)
208 length = hdr->length;
209 memcpy(buf, ptr, length);
217 void os_usleep(unsigned long usec)
222 uint64_t __attribute__((no_instrument_function)) os_get_nsec(void)
224 #if defined(CLOCK_MONOTONIC) && defined(_POSIX_MONOTONIC_CLOCK)
226 if (EINVAL == clock_gettime(CLOCK_MONOTONIC, &tp)) {
229 gettimeofday(&tv, NULL);
230 tp.tv_sec = tv.tv_sec;
231 tp.tv_nsec = tv.tv_usec * 1000;
233 return tp.tv_sec * 1000000000ULL + tp.tv_nsec;
236 gettimeofday(&tv, NULL);
237 return tv.tv_sec * 1000000000ULL + tv.tv_usec * 1000;
241 static char *short_opts;
242 static struct option *long_opts;
244 int os_parse_args(struct sandbox_state *state, int argc, char *argv[])
246 struct sandbox_cmdline_option **sb_opt = __u_boot_sandbox_option_start;
247 size_t num_options = __u_boot_sandbox_option_count();
250 int hidden_short_opt;
255 if (short_opts || long_opts)
261 /* dynamically construct the arguments to the system getopt_long */
262 short_opts = os_malloc(sizeof(*short_opts) * num_options * 2 + 1);
263 long_opts = os_malloc(sizeof(*long_opts) * num_options);
264 if (!short_opts || !long_opts)
268 * getopt_long requires "val" to be unique (since that is what the
269 * func returns), so generate unique values automatically for flags
270 * that don't have a short option. pick 0x100 as that is above the
271 * single byte range (where ASCII/ISO-XXXX-X charsets live).
273 hidden_short_opt = 0x100;
275 for (i = 0; i < num_options; ++i) {
276 long_opts[i].name = sb_opt[i]->flag;
277 long_opts[i].has_arg = sb_opt[i]->has_arg ?
278 required_argument : no_argument;
279 long_opts[i].flag = NULL;
281 if (sb_opt[i]->flag_short) {
282 short_opts[si++] = long_opts[i].val = sb_opt[i]->flag_short;
283 if (long_opts[i].has_arg == required_argument)
284 short_opts[si++] = ':';
286 long_opts[i].val = sb_opt[i]->flag_short = hidden_short_opt++;
288 short_opts[si] = '\0';
290 /* we need to handle output ourselves since u-boot provides printf */
294 * walk all of the options the user gave us on the command line,
295 * figure out what u-boot option structure they belong to (via
296 * the unique short val key), and call the appropriate callback.
298 while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) {
299 for (i = 0; i < num_options; ++i) {
300 if (sb_opt[i]->flag_short == c) {
301 if (sb_opt[i]->callback(state, optarg)) {
302 state->parse_err = sb_opt[i]->flag;
308 if (i == num_options) {
310 * store the faulting flag for later display. we have to
311 * store the flag itself as the getopt parsing itself is
312 * tricky: need to handle the following flags (assume all
313 * of the below are unknown):
314 * -a optopt='a' optind=<next>
315 * -abbbb optopt='a' optind=<this>
316 * -aaaaa optopt='a' optind=<this>
317 * --a optopt=0 optind=<this>
318 * as you can see, it is impossible to determine the exact
319 * faulting flag without doing the parsing ourselves, so
320 * we just report the specific flag that failed.
323 static char parse_err[3] = { '-', 0, '\0', };
324 parse_err[1] = optopt;
325 state->parse_err = parse_err;
327 state->parse_err = argv[optind - 1];
335 void os_dirent_free(struct os_dirent_node *node)
337 struct os_dirent_node *next;
346 int os_dirent_ls(const char *dirname, struct os_dirent_node **headp)
348 struct dirent *entry;
349 struct os_dirent_node *head, *node, *next;
359 dir = opendir(dirname);
363 /* Create a buffer upfront, with typically sufficient size */
364 dirlen = strlen(dirname) + 2;
372 for (node = head = NULL;; node = next) {
374 entry = readdir(dir);
379 next = malloc(sizeof(*node) + strlen(entry->d_name) + 1);
381 os_dirent_free(head);
385 if (dirlen + strlen(entry->d_name) > len) {
386 len = dirlen + strlen(entry->d_name);
388 fname = realloc(fname, len);
392 os_dirent_free(head);
398 strcpy(next->name, entry->d_name);
399 switch (entry->d_type) {
401 next->type = OS_FILET_REG;
404 next->type = OS_FILET_DIR;
407 next->type = OS_FILET_LNK;
410 next->type = OS_FILET_UNKNOWN;
413 snprintf(fname, len, "%s/%s", dirname, next->name);
414 if (!stat(fname, &buf))
415 next->size = buf.st_size;
429 const char *os_dirent_typename[OS_FILET_COUNT] = {
436 const char *os_dirent_get_typename(enum os_dirent_t type)
438 if (type >= OS_FILET_REG && type < OS_FILET_COUNT)
439 return os_dirent_typename[type];
441 return os_dirent_typename[OS_FILET_UNKNOWN];
444 int os_get_filesize(const char *fname, loff_t *size)
449 ret = stat(fname, &buf);
461 void os_puts(const char *str)
467 int os_write_ram_buf(const char *fname)
469 struct sandbox_state *state = state_get_current();
472 fd = open(fname, O_CREAT | O_WRONLY, 0777);
475 ret = write(fd, state->ram_buf, state->ram_size);
477 if (ret != state->ram_size)
483 int os_read_ram_buf(const char *fname)
485 struct sandbox_state *state = state_get_current();
489 ret = os_get_filesize(fname, &size);
492 if (size != state->ram_size)
494 fd = open(fname, O_RDONLY);
498 ret = read(fd, state->ram_buf, state->ram_size);
500 if (ret != state->ram_size)
506 static int make_exec(char *fname, const void *data, int size)
510 strcpy(fname, "/tmp/u-boot.jump.XXXXXX");
514 if (write(fd, data, size) < 0)
517 if (chmod(fname, 0777))
523 static int add_args(char ***argvp, const char *add_args[], int count)
528 for (argv = *argvp, argc = 0; (*argvp)[argc]; argc++)
531 argv = malloc((argc + count + 1) * sizeof(char *));
533 printf("Out of memory for %d argv\n", count);
536 memcpy(argv, *argvp, argc * sizeof(char *));
537 memcpy(argv + argc, add_args, count * sizeof(char *));
538 argv[argc + count] = NULL;
544 int os_jump_to_image(const void *dest, int size)
546 struct sandbox_state *state = state_get_current();
547 char fname[30], mem_fname[30];
549 const char *extra_args[5];
550 char **argv = state->argv;
555 err = make_exec(fname, dest, size);
559 strcpy(mem_fname, "/tmp/u-boot.mem.XXXXXX");
560 fd = mkstemp(mem_fname);
564 err = os_write_ram_buf(mem_fname);
570 extra_args[0] = "-j";
571 extra_args[1] = fname;
572 extra_args[2] = "-m";
573 extra_args[3] = mem_fname;
574 extra_args[4] = "--rm_memory";
575 err = add_args(&argv, extra_args,
576 sizeof(extra_args) / sizeof(extra_args[0]));
581 for (i = 0; argv[i]; i++)
582 printf("%d %s\n", i, argv[i]);
588 err = execv(fname, argv);
593 return unlink(fname);
596 int os_find_u_boot(char *fname, int maxlen)
598 struct sandbox_state *state = state_get_current();
599 const char *progname = state->argv[0];
600 int len = strlen(progname);
605 if (len >= maxlen || len < 4)
608 strcpy(fname, progname);
609 suffix = fname + len - 4;
611 /* If we are TPL, boot to SPL */
612 if (!strcmp(suffix, "-tpl")) {
613 fname[len - 3] = 's';
614 fd = os_open(fname, O_RDONLY);
620 /* Look for 'u-boot-tpl' in the tpl/ directory */
621 p = strstr(fname, "/tpl/");
624 fd = os_open(fname, O_RDONLY);
633 /* Look for 'u-boot' in the same directory as 'u-boot-spl' */
634 if (!strcmp(suffix, "-spl")) {
635 fname[len - 4] = '\0';
636 fd = os_open(fname, O_RDONLY);
643 /* Look for 'u-boot' in the parent directory of spl/ */
644 p = strstr(fname, "/spl/");
647 fd = os_open(fname, O_RDONLY);
657 int os_spl_to_uboot(const char *fname)
659 struct sandbox_state *state = state_get_current();
660 char *argv[state->argc + 1];
663 memcpy(argv, state->argv, sizeof(char *) * (state->argc + 1));
664 argv[0] = (char *)fname;
665 ret = execv(fname, argv);
669 return unlink(fname);
672 void os_localtime(struct rtc_time *rt)
674 time_t t = time(NULL);
678 rt->tm_sec = tm->tm_sec;
679 rt->tm_min = tm->tm_min;
680 rt->tm_hour = tm->tm_hour;
681 rt->tm_mday = tm->tm_mday;
682 rt->tm_mon = tm->tm_mon + 1;
683 rt->tm_year = tm->tm_year + 1900;
684 rt->tm_wday = tm->tm_wday;
685 rt->tm_yday = tm->tm_yday;
686 rt->tm_isdst = tm->tm_isdst;
694 int os_mprotect_allow(void *start, size_t len)
696 int page_size = getpagesize();
698 /* Move start to the start of a page, len to the end */
699 start = (void *)(((ulong)start) & ~(page_size - 1));
700 len = (len + page_size * 2) & ~(page_size - 1);
702 return mprotect(start, len, PROT_READ | PROT_WRITE);