17a9219c1SSimon Glass /* 27a9219c1SSimon Glass * Copyright (c) 2011 The Chromium OS Authors. 31a459660SWolfgang Denk * SPDX-License-Identifier: GPL-2.0+ 47a9219c1SSimon Glass */ 57a9219c1SSimon Glass 662584db1SSimon Glass #include <dirent.h> 7e1012472SSimon Glass #include <errno.h> 87a9219c1SSimon Glass #include <fcntl.h> 970db4212SSimon Glass #include <getopt.h> 1062584db1SSimon Glass #include <stdio.h> 112a54d159SSimon Glass #include <stdint.h> 127a9219c1SSimon Glass #include <stdlib.h> 1362584db1SSimon Glass #include <string.h> 14ab06a758SMike Frysinger #include <termios.h> 15d99a6874SMatthias Weisser #include <time.h> 16e1012472SSimon Glass #include <unistd.h> 1721899b10SMatthias Weisser #include <sys/mman.h> 18e1012472SSimon Glass #include <sys/stat.h> 193bdf56b7SSimon Glass #include <sys/time.h> 20e1012472SSimon Glass #include <sys/types.h> 21d99a6874SMatthias Weisser #include <linux/types.h> 227a9219c1SSimon Glass 2370db4212SSimon Glass #include <asm/getopt.h> 2470db4212SSimon Glass #include <asm/sections.h> 2570db4212SSimon Glass #include <asm/state.h> 267a9219c1SSimon Glass #include <os.h> 277a9219c1SSimon Glass 287a9219c1SSimon Glass /* Operating System Interface */ 297a9219c1SSimon Glass 3077595c6dSSimon Glass struct os_mem_hdr { 3177595c6dSSimon Glass size_t length; /* number of bytes in the block */ 3277595c6dSSimon Glass }; 3377595c6dSSimon Glass 347a9219c1SSimon Glass ssize_t os_read(int fd, void *buf, size_t count) 357a9219c1SSimon Glass { 367a9219c1SSimon Glass return read(fd, buf, count); 377a9219c1SSimon Glass } 387a9219c1SSimon Glass 39e101550aSTaylor Hutt ssize_t os_read_no_block(int fd, void *buf, size_t count) 40e101550aSTaylor Hutt { 41e101550aSTaylor Hutt const int flags = fcntl(fd, F_GETFL, 0); 42e101550aSTaylor Hutt 43e101550aSTaylor Hutt fcntl(fd, F_SETFL, flags | O_NONBLOCK); 44e101550aSTaylor Hutt return os_read(fd, buf, count); 45e101550aSTaylor Hutt } 46e101550aSTaylor Hutt 477a9219c1SSimon Glass ssize_t os_write(int fd, const void *buf, size_t count) 487a9219c1SSimon Glass { 497a9219c1SSimon Glass return write(fd, buf, count); 507a9219c1SSimon Glass } 517a9219c1SSimon Glass 52e2dcefcbSMike Frysinger off_t os_lseek(int fd, off_t offset, int whence) 53e2dcefcbSMike Frysinger { 54e2dcefcbSMike Frysinger if (whence == OS_SEEK_SET) 55e2dcefcbSMike Frysinger whence = SEEK_SET; 56e2dcefcbSMike Frysinger else if (whence == OS_SEEK_CUR) 57e2dcefcbSMike Frysinger whence = SEEK_CUR; 58e2dcefcbSMike Frysinger else if (whence == OS_SEEK_END) 59e2dcefcbSMike Frysinger whence = SEEK_END; 60e2dcefcbSMike Frysinger else 61e2dcefcbSMike Frysinger os_exit(1); 62e2dcefcbSMike Frysinger return lseek(fd, offset, whence); 63e2dcefcbSMike Frysinger } 64e2dcefcbSMike Frysinger 65d9165153SSimon Glass int os_open(const char *pathname, int os_flags) 667a9219c1SSimon Glass { 67d9165153SSimon Glass int flags; 68d9165153SSimon Glass 69d9165153SSimon Glass switch (os_flags & OS_O_MASK) { 70d9165153SSimon Glass case OS_O_RDONLY: 71d9165153SSimon Glass default: 72d9165153SSimon Glass flags = O_RDONLY; 73d9165153SSimon Glass break; 74d9165153SSimon Glass 75d9165153SSimon Glass case OS_O_WRONLY: 76d9165153SSimon Glass flags = O_WRONLY; 77d9165153SSimon Glass break; 78d9165153SSimon Glass 79d9165153SSimon Glass case OS_O_RDWR: 80d9165153SSimon Glass flags = O_RDWR; 81d9165153SSimon Glass break; 82d9165153SSimon Glass } 83d9165153SSimon Glass 84d9165153SSimon Glass if (os_flags & OS_O_CREAT) 85d9165153SSimon Glass flags |= O_CREAT; 86d9165153SSimon Glass 87d9165153SSimon Glass return open(pathname, flags, 0777); 887a9219c1SSimon Glass } 897a9219c1SSimon Glass 907a9219c1SSimon Glass int os_close(int fd) 917a9219c1SSimon Glass { 927a9219c1SSimon Glass return close(fd); 937a9219c1SSimon Glass } 947a9219c1SSimon Glass 957a9219c1SSimon Glass void os_exit(int exit_code) 967a9219c1SSimon Glass { 977a9219c1SSimon Glass exit(exit_code); 987a9219c1SSimon Glass } 99ab06a758SMike Frysinger 100ab06a758SMike Frysinger /* Restore tty state when we exit */ 101ab06a758SMike Frysinger static struct termios orig_term; 102ab06a758SMike Frysinger 103ab06a758SMike Frysinger static void os_fd_restore(void) 104ab06a758SMike Frysinger { 105ab06a758SMike Frysinger tcsetattr(0, TCSANOW, &orig_term); 106ab06a758SMike Frysinger } 107ab06a758SMike Frysinger 108ab06a758SMike Frysinger /* Put tty into raw mode so <tab> and <ctrl+c> work */ 109ab06a758SMike Frysinger void os_tty_raw(int fd) 110ab06a758SMike Frysinger { 111ab06a758SMike Frysinger static int setup = 0; 112ab06a758SMike Frysinger struct termios term; 113ab06a758SMike Frysinger 114ab06a758SMike Frysinger if (setup) 115ab06a758SMike Frysinger return; 116ab06a758SMike Frysinger setup = 1; 117ab06a758SMike Frysinger 118ab06a758SMike Frysinger /* If not a tty, don't complain */ 119ab06a758SMike Frysinger if (tcgetattr(fd, &orig_term)) 120ab06a758SMike Frysinger return; 121ab06a758SMike Frysinger 122ab06a758SMike Frysinger term = orig_term; 123ab06a758SMike Frysinger term.c_iflag = IGNBRK | IGNPAR; 124ab06a758SMike Frysinger term.c_oflag = OPOST | ONLCR; 125ab06a758SMike Frysinger term.c_cflag = CS8 | CREAD | CLOCAL; 126ab06a758SMike Frysinger term.c_lflag = 0; 127ab06a758SMike Frysinger if (tcsetattr(fd, TCSANOW, &term)) 128ab06a758SMike Frysinger return; 129ab06a758SMike Frysinger 130ab06a758SMike Frysinger atexit(os_fd_restore); 131ab06a758SMike Frysinger } 13221899b10SMatthias Weisser 13321899b10SMatthias Weisser void *os_malloc(size_t length) 13421899b10SMatthias Weisser { 13577595c6dSSimon Glass struct os_mem_hdr *hdr; 13677595c6dSSimon Glass 13777595c6dSSimon Glass hdr = mmap(NULL, length + sizeof(*hdr), PROT_READ | PROT_WRITE, 13821899b10SMatthias Weisser MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); 13977595c6dSSimon Glass if (hdr == MAP_FAILED) 14077595c6dSSimon Glass return NULL; 14177595c6dSSimon Glass hdr->length = length; 14277595c6dSSimon Glass 14377595c6dSSimon Glass return hdr + 1; 14477595c6dSSimon Glass } 14577595c6dSSimon Glass 14677595c6dSSimon Glass void *os_free(void *ptr) 14777595c6dSSimon Glass { 14877595c6dSSimon Glass struct os_mem_hdr *hdr = ptr; 14977595c6dSSimon Glass 15077595c6dSSimon Glass hdr--; 15177595c6dSSimon Glass if (ptr) 15277595c6dSSimon Glass munmap(hdr, hdr->length + sizeof(*hdr)); 15377595c6dSSimon Glass } 15477595c6dSSimon Glass 15577595c6dSSimon Glass void *os_realloc(void *ptr, size_t length) 15677595c6dSSimon Glass { 15777595c6dSSimon Glass struct os_mem_hdr *hdr = ptr; 15877595c6dSSimon Glass void *buf = NULL; 15977595c6dSSimon Glass 16077595c6dSSimon Glass hdr--; 16177595c6dSSimon Glass if (length != 0) { 16277595c6dSSimon Glass buf = os_malloc(length); 16377595c6dSSimon Glass if (!buf) 16477595c6dSSimon Glass return buf; 16577595c6dSSimon Glass if (ptr) { 16677595c6dSSimon Glass if (length > hdr->length) 16777595c6dSSimon Glass length = hdr->length; 16877595c6dSSimon Glass memcpy(buf, ptr, length); 16977595c6dSSimon Glass } 17077595c6dSSimon Glass } 17177595c6dSSimon Glass os_free(ptr); 17277595c6dSSimon Glass 17377595c6dSSimon Glass return buf; 17421899b10SMatthias Weisser } 175d99a6874SMatthias Weisser 176d99a6874SMatthias Weisser void os_usleep(unsigned long usec) 177d99a6874SMatthias Weisser { 178d99a6874SMatthias Weisser usleep(usec); 179d99a6874SMatthias Weisser } 180d99a6874SMatthias Weisser 1812a54d159SSimon Glass uint64_t __attribute__((no_instrument_function)) os_get_nsec(void) 182d99a6874SMatthias Weisser { 183d99a6874SMatthias Weisser #if defined(CLOCK_MONOTONIC) && defined(_POSIX_MONOTONIC_CLOCK) 184d99a6874SMatthias Weisser struct timespec tp; 185d99a6874SMatthias Weisser if (EINVAL == clock_gettime(CLOCK_MONOTONIC, &tp)) { 186d99a6874SMatthias Weisser struct timeval tv; 187d99a6874SMatthias Weisser 188d99a6874SMatthias Weisser gettimeofday(&tv, NULL); 189d99a6874SMatthias Weisser tp.tv_sec = tv.tv_sec; 190d99a6874SMatthias Weisser tp.tv_nsec = tv.tv_usec * 1000; 191d99a6874SMatthias Weisser } 192d99a6874SMatthias Weisser return tp.tv_sec * 1000000000ULL + tp.tv_nsec; 193d99a6874SMatthias Weisser #else 194d99a6874SMatthias Weisser struct timeval tv; 195d99a6874SMatthias Weisser gettimeofday(&tv, NULL); 196d99a6874SMatthias Weisser return tv.tv_sec * 1000000000ULL + tv.tv_usec * 1000; 197d99a6874SMatthias Weisser #endif 198d99a6874SMatthias Weisser } 19970db4212SSimon Glass 20070db4212SSimon Glass static char *short_opts; 20170db4212SSimon Glass static struct option *long_opts; 20270db4212SSimon Glass 20370db4212SSimon Glass int os_parse_args(struct sandbox_state *state, int argc, char *argv[]) 20470db4212SSimon Glass { 2057b3efc66SSimon Glass struct sandbox_cmdline_option **sb_opt = __u_boot_sandbox_option_start; 20670db4212SSimon Glass size_t num_options = __u_boot_sandbox_option_count(); 20770db4212SSimon Glass size_t i; 20870db4212SSimon Glass 20970db4212SSimon Glass int hidden_short_opt; 21070db4212SSimon Glass size_t si; 21170db4212SSimon Glass 21270db4212SSimon Glass int c; 21370db4212SSimon Glass 21470db4212SSimon Glass if (short_opts || long_opts) 21570db4212SSimon Glass return 1; 21670db4212SSimon Glass 21770db4212SSimon Glass state->argc = argc; 21870db4212SSimon Glass state->argv = argv; 21970db4212SSimon Glass 22070db4212SSimon Glass /* dynamically construct the arguments to the system getopt_long */ 22170db4212SSimon Glass short_opts = os_malloc(sizeof(*short_opts) * num_options * 2 + 1); 22270db4212SSimon Glass long_opts = os_malloc(sizeof(*long_opts) * num_options); 22370db4212SSimon Glass if (!short_opts || !long_opts) 22470db4212SSimon Glass return 1; 22570db4212SSimon Glass 22670db4212SSimon Glass /* 22770db4212SSimon Glass * getopt_long requires "val" to be unique (since that is what the 22870db4212SSimon Glass * func returns), so generate unique values automatically for flags 22970db4212SSimon Glass * that don't have a short option. pick 0x100 as that is above the 23070db4212SSimon Glass * single byte range (where ASCII/ISO-XXXX-X charsets live). 23170db4212SSimon Glass */ 23270db4212SSimon Glass hidden_short_opt = 0x100; 23370db4212SSimon Glass si = 0; 23470db4212SSimon Glass for (i = 0; i < num_options; ++i) { 23570db4212SSimon Glass long_opts[i].name = sb_opt[i]->flag; 23670db4212SSimon Glass long_opts[i].has_arg = sb_opt[i]->has_arg ? 23770db4212SSimon Glass required_argument : no_argument; 23870db4212SSimon Glass long_opts[i].flag = NULL; 23970db4212SSimon Glass 24070db4212SSimon Glass if (sb_opt[i]->flag_short) { 24170db4212SSimon Glass short_opts[si++] = long_opts[i].val = sb_opt[i]->flag_short; 24270db4212SSimon Glass if (long_opts[i].has_arg == required_argument) 24370db4212SSimon Glass short_opts[si++] = ':'; 24470db4212SSimon Glass } else 24570db4212SSimon Glass long_opts[i].val = sb_opt[i]->flag_short = hidden_short_opt++; 24670db4212SSimon Glass } 24770db4212SSimon Glass short_opts[si] = '\0'; 24870db4212SSimon Glass 24970db4212SSimon Glass /* we need to handle output ourselves since u-boot provides printf */ 25070db4212SSimon Glass opterr = 0; 25170db4212SSimon Glass 25270db4212SSimon Glass /* 25370db4212SSimon Glass * walk all of the options the user gave us on the command line, 25470db4212SSimon Glass * figure out what u-boot option structure they belong to (via 25570db4212SSimon Glass * the unique short val key), and call the appropriate callback. 25670db4212SSimon Glass */ 25770db4212SSimon Glass while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) { 25870db4212SSimon Glass for (i = 0; i < num_options; ++i) { 25970db4212SSimon Glass if (sb_opt[i]->flag_short == c) { 26070db4212SSimon Glass if (sb_opt[i]->callback(state, optarg)) { 26170db4212SSimon Glass state->parse_err = sb_opt[i]->flag; 26270db4212SSimon Glass return 0; 26370db4212SSimon Glass } 26470db4212SSimon Glass break; 26570db4212SSimon Glass } 26670db4212SSimon Glass } 26770db4212SSimon Glass if (i == num_options) { 26870db4212SSimon Glass /* 26970db4212SSimon Glass * store the faulting flag for later display. we have to 27070db4212SSimon Glass * store the flag itself as the getopt parsing itself is 27170db4212SSimon Glass * tricky: need to handle the following flags (assume all 27270db4212SSimon Glass * of the below are unknown): 27370db4212SSimon Glass * -a optopt='a' optind=<next> 27470db4212SSimon Glass * -abbbb optopt='a' optind=<this> 27570db4212SSimon Glass * -aaaaa optopt='a' optind=<this> 27670db4212SSimon Glass * --a optopt=0 optind=<this> 27770db4212SSimon Glass * as you can see, it is impossible to determine the exact 27870db4212SSimon Glass * faulting flag without doing the parsing ourselves, so 27970db4212SSimon Glass * we just report the specific flag that failed. 28070db4212SSimon Glass */ 28170db4212SSimon Glass if (optopt) { 28270db4212SSimon Glass static char parse_err[3] = { '-', 0, '\0', }; 28370db4212SSimon Glass parse_err[1] = optopt; 28470db4212SSimon Glass state->parse_err = parse_err; 28570db4212SSimon Glass } else 28670db4212SSimon Glass state->parse_err = argv[optind - 1]; 28770db4212SSimon Glass break; 28870db4212SSimon Glass } 28970db4212SSimon Glass } 29070db4212SSimon Glass 29170db4212SSimon Glass return 0; 29270db4212SSimon Glass } 29362584db1SSimon Glass 29462584db1SSimon Glass void os_dirent_free(struct os_dirent_node *node) 29562584db1SSimon Glass { 29662584db1SSimon Glass struct os_dirent_node *next; 29762584db1SSimon Glass 29862584db1SSimon Glass while (node) { 29962584db1SSimon Glass next = node->next; 30062584db1SSimon Glass free(node); 30162584db1SSimon Glass node = next; 30262584db1SSimon Glass } 30362584db1SSimon Glass } 30462584db1SSimon Glass 30562584db1SSimon Glass int os_dirent_ls(const char *dirname, struct os_dirent_node **headp) 30662584db1SSimon Glass { 30762584db1SSimon Glass struct dirent entry, *result; 30862584db1SSimon Glass struct os_dirent_node *head, *node, *next; 30962584db1SSimon Glass struct stat buf; 31062584db1SSimon Glass DIR *dir; 31162584db1SSimon Glass int ret; 31262584db1SSimon Glass char *fname; 31362584db1SSimon Glass int len; 31462584db1SSimon Glass 31562584db1SSimon Glass *headp = NULL; 31662584db1SSimon Glass dir = opendir(dirname); 31762584db1SSimon Glass if (!dir) 31862584db1SSimon Glass return -1; 31962584db1SSimon Glass 32062584db1SSimon Glass /* Create a buffer for the maximum filename length */ 32162584db1SSimon Glass len = sizeof(entry.d_name) + strlen(dirname) + 2; 32262584db1SSimon Glass fname = malloc(len); 32362584db1SSimon Glass if (!fname) { 32462584db1SSimon Glass ret = -ENOMEM; 32562584db1SSimon Glass goto done; 32662584db1SSimon Glass } 32762584db1SSimon Glass 32862584db1SSimon Glass for (node = head = NULL;; node = next) { 32962584db1SSimon Glass ret = readdir_r(dir, &entry, &result); 33062584db1SSimon Glass if (ret || !result) 33162584db1SSimon Glass break; 33262584db1SSimon Glass next = malloc(sizeof(*node) + strlen(entry.d_name) + 1); 33362584db1SSimon Glass if (!next) { 33462584db1SSimon Glass os_dirent_free(head); 33562584db1SSimon Glass ret = -ENOMEM; 33662584db1SSimon Glass goto done; 33762584db1SSimon Glass } 33862584db1SSimon Glass strcpy(next->name, entry.d_name); 33962584db1SSimon Glass switch (entry.d_type) { 34062584db1SSimon Glass case DT_REG: 34162584db1SSimon Glass next->type = OS_FILET_REG; 34262584db1SSimon Glass break; 34362584db1SSimon Glass case DT_DIR: 34462584db1SSimon Glass next->type = OS_FILET_DIR; 34562584db1SSimon Glass break; 34662584db1SSimon Glass case DT_LNK: 34762584db1SSimon Glass next->type = OS_FILET_LNK; 34862584db1SSimon Glass break; 34962584db1SSimon Glass } 35062584db1SSimon Glass next->size = 0; 35162584db1SSimon Glass snprintf(fname, len, "%s/%s", dirname, next->name); 35262584db1SSimon Glass if (!stat(fname, &buf)) 35362584db1SSimon Glass next->size = buf.st_size; 35462584db1SSimon Glass if (node) 35562584db1SSimon Glass node->next = next; 35662584db1SSimon Glass if (!head) 35762584db1SSimon Glass head = node; 35862584db1SSimon Glass } 35962584db1SSimon Glass *headp = head; 36062584db1SSimon Glass 36162584db1SSimon Glass done: 36262584db1SSimon Glass closedir(dir); 36362584db1SSimon Glass return ret; 36462584db1SSimon Glass } 36562584db1SSimon Glass 36662584db1SSimon Glass const char *os_dirent_typename[OS_FILET_COUNT] = { 36762584db1SSimon Glass " ", 36862584db1SSimon Glass "SYM", 36962584db1SSimon Glass "DIR", 37062584db1SSimon Glass "???", 37162584db1SSimon Glass }; 37262584db1SSimon Glass 37362584db1SSimon Glass const char *os_dirent_get_typename(enum os_dirent_t type) 37462584db1SSimon Glass { 37562584db1SSimon Glass if (type >= 0 && type < OS_FILET_COUNT) 37662584db1SSimon Glass return os_dirent_typename[type]; 37762584db1SSimon Glass 37862584db1SSimon Glass return os_dirent_typename[OS_FILET_UNKNOWN]; 37962584db1SSimon Glass } 38062584db1SSimon Glass 38162584db1SSimon Glass ssize_t os_get_filesize(const char *fname) 38262584db1SSimon Glass { 38362584db1SSimon Glass struct stat buf; 38462584db1SSimon Glass int ret; 38562584db1SSimon Glass 38662584db1SSimon Glass ret = stat(fname, &buf); 38762584db1SSimon Glass if (ret) 38862584db1SSimon Glass return ret; 38962584db1SSimon Glass return buf.st_size; 39062584db1SSimon Glass } 39191b136c7SSimon Glass 39291b136c7SSimon Glass void os_putc(int ch) 39391b136c7SSimon Glass { 39491b136c7SSimon Glass putchar(ch); 39591b136c7SSimon Glass } 39691b136c7SSimon Glass 39791b136c7SSimon Glass void os_puts(const char *str) 39891b136c7SSimon Glass { 39991b136c7SSimon Glass while (*str) 40091b136c7SSimon Glass os_putc(*str++); 40191b136c7SSimon Glass } 402*5c2859cdSSimon Glass 403*5c2859cdSSimon Glass int os_write_ram_buf(const char *fname) 404*5c2859cdSSimon Glass { 405*5c2859cdSSimon Glass struct sandbox_state *state = state_get_current(); 406*5c2859cdSSimon Glass int fd, ret; 407*5c2859cdSSimon Glass 408*5c2859cdSSimon Glass fd = open(fname, O_CREAT | O_WRONLY, 0777); 409*5c2859cdSSimon Glass if (fd < 0) 410*5c2859cdSSimon Glass return -ENOENT; 411*5c2859cdSSimon Glass ret = write(fd, state->ram_buf, state->ram_size); 412*5c2859cdSSimon Glass close(fd); 413*5c2859cdSSimon Glass if (ret != state->ram_size) 414*5c2859cdSSimon Glass return -EIO; 415*5c2859cdSSimon Glass 416*5c2859cdSSimon Glass return 0; 417*5c2859cdSSimon Glass } 418*5c2859cdSSimon Glass 419*5c2859cdSSimon Glass int os_read_ram_buf(const char *fname) 420*5c2859cdSSimon Glass { 421*5c2859cdSSimon Glass struct sandbox_state *state = state_get_current(); 422*5c2859cdSSimon Glass int fd, ret; 423*5c2859cdSSimon Glass int size; 424*5c2859cdSSimon Glass 425*5c2859cdSSimon Glass size = os_get_filesize(fname); 426*5c2859cdSSimon Glass if (size < 0) 427*5c2859cdSSimon Glass return -ENOENT; 428*5c2859cdSSimon Glass if (size != state->ram_size) 429*5c2859cdSSimon Glass return -ENOSPC; 430*5c2859cdSSimon Glass fd = open(fname, O_RDONLY); 431*5c2859cdSSimon Glass if (fd < 0) 432*5c2859cdSSimon Glass return -ENOENT; 433*5c2859cdSSimon Glass 434*5c2859cdSSimon Glass ret = read(fd, state->ram_buf, state->ram_size); 435*5c2859cdSSimon Glass close(fd); 436*5c2859cdSSimon Glass if (ret != state->ram_size) 437*5c2859cdSSimon Glass return -EIO; 438*5c2859cdSSimon Glass 439*5c2859cdSSimon Glass return 0; 440*5c2859cdSSimon Glass } 441