os.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*
  2. * Copyright (c) 2011 The Chromium OS Authors.
  3. * See file CREDITS for list of people who contributed to this
  4. * project.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation; either version 2 of
  9. * the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  19. * MA 02111-1307 USA
  20. */
  21. #include <errno.h>
  22. #include <fcntl.h>
  23. #include <getopt.h>
  24. #include <stdlib.h>
  25. #include <termios.h>
  26. #include <time.h>
  27. #include <unistd.h>
  28. #include <sys/mman.h>
  29. #include <sys/stat.h>
  30. #include <sys/time.h>
  31. #include <sys/types.h>
  32. #include <linux/types.h>
  33. #include <asm/getopt.h>
  34. #include <asm/sections.h>
  35. #include <asm/state.h>
  36. #include <os.h>
  37. /* Operating System Interface */
  38. ssize_t os_read(int fd, void *buf, size_t count)
  39. {
  40. return read(fd, buf, count);
  41. }
  42. ssize_t os_read_no_block(int fd, void *buf, size_t count)
  43. {
  44. const int flags = fcntl(fd, F_GETFL, 0);
  45. fcntl(fd, F_SETFL, flags | O_NONBLOCK);
  46. return os_read(fd, buf, count);
  47. }
  48. ssize_t os_write(int fd, const void *buf, size_t count)
  49. {
  50. return write(fd, buf, count);
  51. }
  52. off_t os_lseek(int fd, off_t offset, int whence)
  53. {
  54. if (whence == OS_SEEK_SET)
  55. whence = SEEK_SET;
  56. else if (whence == OS_SEEK_CUR)
  57. whence = SEEK_CUR;
  58. else if (whence == OS_SEEK_END)
  59. whence = SEEK_END;
  60. else
  61. os_exit(1);
  62. return lseek(fd, offset, whence);
  63. }
  64. int os_open(const char *pathname, int os_flags)
  65. {
  66. int flags;
  67. switch (os_flags & OS_O_MASK) {
  68. case OS_O_RDONLY:
  69. default:
  70. flags = O_RDONLY;
  71. break;
  72. case OS_O_WRONLY:
  73. flags = O_WRONLY;
  74. break;
  75. case OS_O_RDWR:
  76. flags = O_RDWR;
  77. break;
  78. }
  79. if (os_flags & OS_O_CREAT)
  80. flags |= O_CREAT;
  81. return open(pathname, flags, 0777);
  82. }
  83. int os_close(int fd)
  84. {
  85. return close(fd);
  86. }
  87. void os_exit(int exit_code)
  88. {
  89. exit(exit_code);
  90. }
  91. /* Restore tty state when we exit */
  92. static struct termios orig_term;
  93. static void os_fd_restore(void)
  94. {
  95. tcsetattr(0, TCSANOW, &orig_term);
  96. }
  97. /* Put tty into raw mode so <tab> and <ctrl+c> work */
  98. void os_tty_raw(int fd)
  99. {
  100. static int setup = 0;
  101. struct termios term;
  102. if (setup)
  103. return;
  104. setup = 1;
  105. /* If not a tty, don't complain */
  106. if (tcgetattr(fd, &orig_term))
  107. return;
  108. term = orig_term;
  109. term.c_iflag = IGNBRK | IGNPAR;
  110. term.c_oflag = OPOST | ONLCR;
  111. term.c_cflag = CS8 | CREAD | CLOCAL;
  112. term.c_lflag = 0;
  113. if (tcsetattr(fd, TCSANOW, &term))
  114. return;
  115. atexit(os_fd_restore);
  116. }
  117. void *os_malloc(size_t length)
  118. {
  119. return mmap(NULL, length, PROT_READ | PROT_WRITE,
  120. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  121. }
  122. void os_usleep(unsigned long usec)
  123. {
  124. usleep(usec);
  125. }
  126. u64 os_get_nsec(void)
  127. {
  128. #if defined(CLOCK_MONOTONIC) && defined(_POSIX_MONOTONIC_CLOCK)
  129. struct timespec tp;
  130. if (EINVAL == clock_gettime(CLOCK_MONOTONIC, &tp)) {
  131. struct timeval tv;
  132. gettimeofday(&tv, NULL);
  133. tp.tv_sec = tv.tv_sec;
  134. tp.tv_nsec = tv.tv_usec * 1000;
  135. }
  136. return tp.tv_sec * 1000000000ULL + tp.tv_nsec;
  137. #else
  138. struct timeval tv;
  139. gettimeofday(&tv, NULL);
  140. return tv.tv_sec * 1000000000ULL + tv.tv_usec * 1000;
  141. #endif
  142. }
  143. static char *short_opts;
  144. static struct option *long_opts;
  145. int os_parse_args(struct sandbox_state *state, int argc, char *argv[])
  146. {
  147. struct sb_cmdline_option **sb_opt = __u_boot_sandbox_option_start;
  148. size_t num_options = __u_boot_sandbox_option_count();
  149. size_t i;
  150. int hidden_short_opt;
  151. size_t si;
  152. int c;
  153. if (short_opts || long_opts)
  154. return 1;
  155. state->argc = argc;
  156. state->argv = argv;
  157. /* dynamically construct the arguments to the system getopt_long */
  158. short_opts = os_malloc(sizeof(*short_opts) * num_options * 2 + 1);
  159. long_opts = os_malloc(sizeof(*long_opts) * num_options);
  160. if (!short_opts || !long_opts)
  161. return 1;
  162. /*
  163. * getopt_long requires "val" to be unique (since that is what the
  164. * func returns), so generate unique values automatically for flags
  165. * that don't have a short option. pick 0x100 as that is above the
  166. * single byte range (where ASCII/ISO-XXXX-X charsets live).
  167. */
  168. hidden_short_opt = 0x100;
  169. si = 0;
  170. for (i = 0; i < num_options; ++i) {
  171. long_opts[i].name = sb_opt[i]->flag;
  172. long_opts[i].has_arg = sb_opt[i]->has_arg ?
  173. required_argument : no_argument;
  174. long_opts[i].flag = NULL;
  175. if (sb_opt[i]->flag_short) {
  176. short_opts[si++] = long_opts[i].val = sb_opt[i]->flag_short;
  177. if (long_opts[i].has_arg == required_argument)
  178. short_opts[si++] = ':';
  179. } else
  180. long_opts[i].val = sb_opt[i]->flag_short = hidden_short_opt++;
  181. }
  182. short_opts[si] = '\0';
  183. /* we need to handle output ourselves since u-boot provides printf */
  184. opterr = 0;
  185. /*
  186. * walk all of the options the user gave us on the command line,
  187. * figure out what u-boot option structure they belong to (via
  188. * the unique short val key), and call the appropriate callback.
  189. */
  190. while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) {
  191. for (i = 0; i < num_options; ++i) {
  192. if (sb_opt[i]->flag_short == c) {
  193. if (sb_opt[i]->callback(state, optarg)) {
  194. state->parse_err = sb_opt[i]->flag;
  195. return 0;
  196. }
  197. break;
  198. }
  199. }
  200. if (i == num_options) {
  201. /*
  202. * store the faulting flag for later display. we have to
  203. * store the flag itself as the getopt parsing itself is
  204. * tricky: need to handle the following flags (assume all
  205. * of the below are unknown):
  206. * -a optopt='a' optind=<next>
  207. * -abbbb optopt='a' optind=<this>
  208. * -aaaaa optopt='a' optind=<this>
  209. * --a optopt=0 optind=<this>
  210. * as you can see, it is impossible to determine the exact
  211. * faulting flag without doing the parsing ourselves, so
  212. * we just report the specific flag that failed.
  213. */
  214. if (optopt) {
  215. static char parse_err[3] = { '-', 0, '\0', };
  216. parse_err[1] = optopt;
  217. state->parse_err = parse_err;
  218. } else
  219. state->parse_err = argv[optind - 1];
  220. break;
  221. }
  222. }
  223. return 0;
  224. }