]> git.sur5r.net Git - u-boot/blobdiff - arch/sandbox/cpu/os.c
Merge tag 'signed-efi-next' of git://github.com/agraf/u-boot
[u-boot] / arch / sandbox / cpu / os.c
index 4d5f8057533e89f2ed6edd0298ac4c69038360eb..5839932b00571538d3e8291694f3e6a692e14dae 100644 (file)
@@ -1,12 +1,13 @@
+// SPDX-License-Identifier: GPL-2.0+
 /*
  * Copyright (c) 2011 The Chromium OS Authors.
- * SPDX-License-Identifier:    GPL-2.0+
  */
 
 #include <dirent.h>
 #include <errno.h>
 #include <fcntl.h>
 #include <getopt.h>
+#include <setjmp.h>
 #include <stdio.h>
 #include <stdint.h>
 #include <stdlib.h>
@@ -24,6 +25,7 @@
 #include <asm/sections.h>
 #include <asm/state.h>
 #include <os.h>
+#include <rtc_def.h>
 
 /* Operating System Interface */
 
@@ -106,10 +108,12 @@ void os_exit(int exit_code)
 static struct termios orig_term;
 static bool term_setup;
 
-static void os_fd_restore(void)
+void os_fd_restore(void)
 {
-       if (term_setup)
+       if (term_setup) {
                tcsetattr(0, TCSANOW, &orig_term);
+               term_setup = false;
+       }
 }
 
 /* Put tty into raw mode so <tab> and <ctrl+c> work */
@@ -119,7 +123,6 @@ void os_tty_raw(int fd, bool allow_sigs)
 
        if (term_setup)
                return;
-       term_setup = true;
 
        /* If not a tty, don't complain */
        if (tcgetattr(fd, &orig_term))
@@ -133,6 +136,7 @@ void os_tty_raw(int fd, bool allow_sigs)
        if (tcsetattr(fd, TCSANOW, &term))
                return;
 
+       term_setup = true;
        atexit(os_fd_restore);
 }
 
@@ -310,21 +314,24 @@ void os_dirent_free(struct os_dirent_node *node)
 
 int os_dirent_ls(const char *dirname, struct os_dirent_node **headp)
 {
-       struct dirent entry, *result;
+       struct dirent *entry;
        struct os_dirent_node *head, *node, *next;
        struct stat buf;
        DIR *dir;
        int ret;
        char *fname;
+       char *old_fname;
        int len;
+       int dirlen;
 
        *headp = NULL;
        dir = opendir(dirname);
        if (!dir)
                return -1;
 
-       /* Create a buffer for the maximum filename length */
-       len = sizeof(entry.d_name) + strlen(dirname) + 2;
+       /* Create a buffer upfront, with typically sufficient size */
+       dirlen = strlen(dirname) + 2;
+       len = dirlen + 256;
        fname = malloc(len);
        if (!fname) {
                ret = -ENOMEM;
@@ -332,18 +339,33 @@ int os_dirent_ls(const char *dirname, struct os_dirent_node **headp)
        }
 
        for (node = head = NULL;; node = next) {
-               ret = readdir_r(dir, &entry, &result);
-               if (ret || !result)
+               errno = 0;
+               entry = readdir(dir);
+               if (!entry) {
+                       ret = errno;
                        break;
-               next = malloc(sizeof(*node) + strlen(entry.d_name) + 1);
+               }
+               next = malloc(sizeof(*node) + strlen(entry->d_name) + 1);
                if (!next) {
                        os_dirent_free(head);
                        ret = -ENOMEM;
                        goto done;
                }
+               if (dirlen + strlen(entry->d_name) > len) {
+                       len = dirlen + strlen(entry->d_name);
+                       old_fname = fname;
+                       fname = realloc(fname, len);
+                       if (!fname) {
+                               free(old_fname);
+                               free(next);
+                               os_dirent_free(head);
+                               ret = -ENOMEM;
+                               goto done;
+                       }
+               }
                next->next = NULL;
-               strcpy(next->name, entry.d_name);
-               switch (entry.d_type) {
+               strcpy(next->name, entry->d_name);
+               switch (entry->d_type) {
                case DT_REG:
                        next->type = OS_FILET_REG;
                        break;
@@ -353,6 +375,8 @@ int os_dirent_ls(const char *dirname, struct os_dirent_node **headp)
                case DT_LNK:
                        next->type = OS_FILET_LNK;
                        break;
+               default:
+                       next->type = OS_FILET_UNKNOWN;
                }
                next->size = 0;
                snprintf(fname, len, "%s/%s", dirname, next->name);
@@ -360,8 +384,8 @@ int os_dirent_ls(const char *dirname, struct os_dirent_node **headp)
                        next->size = buf.st_size;
                if (node)
                        node->next = next;
-               if (!head)
-                       head = node;
+               else
+                       head = next;
        }
        *headp = head;
 
@@ -380,7 +404,7 @@ const char *os_dirent_typename[OS_FILET_COUNT] = {
 
 const char *os_dirent_get_typename(enum os_dirent_t type)
 {
-       if (type >= 0 && type < OS_FILET_COUNT)
+       if (type >= OS_FILET_REG && type < OS_FILET_COUNT)
                return os_dirent_typename[type];
 
        return os_dirent_typename[OS_FILET_UNKNOWN];
@@ -537,3 +561,93 @@ int os_jump_to_image(const void *dest, int size)
 
        return unlink(fname);
 }
+
+int os_find_u_boot(char *fname, int maxlen)
+{
+       struct sandbox_state *state = state_get_current();
+       const char *progname = state->argv[0];
+       int len = strlen(progname);
+       char *p;
+       int fd;
+
+       if (len >= maxlen || len < 4)
+               return -ENOSPC;
+
+       /* Look for 'u-boot' in the same directory as 'u-boot-spl' */
+       strcpy(fname, progname);
+       if (!strcmp(fname + len - 4, "-spl")) {
+               fname[len - 4] = '\0';
+               fd = os_open(fname, O_RDONLY);
+               if (fd >= 0) {
+                       close(fd);
+                       return 0;
+               }
+       }
+
+       /* Look for 'u-boot' in the parent directory of spl/ */
+       p = strstr(fname, "/spl/");
+       if (p) {
+               strcpy(p, p + 4);
+               fd = os_open(fname, O_RDONLY);
+               if (fd >= 0) {
+                       close(fd);
+                       return 0;
+               }
+       }
+
+       return -ENOENT;
+}
+
+int os_spl_to_uboot(const char *fname)
+{
+       struct sandbox_state *state = state_get_current();
+       char *argv[state->argc + 1];
+       int ret;
+
+       memcpy(argv, state->argv, sizeof(char *) * (state->argc + 1));
+       argv[0] = (char *)fname;
+       ret = execv(fname, argv);
+       if (ret)
+               return ret;
+
+       return unlink(fname);
+}
+
+void os_localtime(struct rtc_time *rt)
+{
+       time_t t = time(NULL);
+       struct tm *tm;
+
+       tm = localtime(&t);
+       rt->tm_sec = tm->tm_sec;
+       rt->tm_min = tm->tm_min;
+       rt->tm_hour = tm->tm_hour;
+       rt->tm_mday = tm->tm_mday;
+       rt->tm_mon = tm->tm_mon + 1;
+       rt->tm_year = tm->tm_year + 1900;
+       rt->tm_wday = tm->tm_wday;
+       rt->tm_yday = tm->tm_yday;
+       rt->tm_isdst = tm->tm_isdst;
+}
+
+int os_setjmp(ulong *jmp, int size)
+{
+       jmp_buf dummy;
+
+       /*
+        * We cannot rely on the struct name that jmp_buf uses, so use a
+        * local variable here
+        */
+       if (size < sizeof(dummy)) {
+               printf("setjmp: jmpbuf is too small (%d bytes, need %d)\n",
+                      size, sizeof(jmp_buf));
+               return -ENOSPC;
+       }
+
+       return setjmp((struct __jmp_buf_tag *)jmp);
+}
+
+void os_longjmp(ulong *jmp, int ret)
+{
+       longjmp((struct __jmp_buf_tag *)jmp, ret);
+}