os.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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_write(int fd, const void *buf, size_t count)
  43. {
  44. return write(fd, buf, count);
  45. }
  46. off_t os_lseek(int fd, off_t offset, int whence)
  47. {
  48. if (whence == OS_SEEK_SET)
  49. whence = SEEK_SET;
  50. else if (whence == OS_SEEK_CUR)
  51. whence = SEEK_CUR;
  52. else if (whence == OS_SEEK_END)
  53. whence = SEEK_END;
  54. else
  55. os_exit(1);
  56. return lseek(fd, offset, whence);
  57. }
  58. int os_open(const char *pathname, int os_flags)
  59. {
  60. int flags;
  61. switch (os_flags & OS_O_MASK) {
  62. case OS_O_RDONLY:
  63. default:
  64. flags = O_RDONLY;
  65. break;
  66. case OS_O_WRONLY:
  67. flags = O_WRONLY;
  68. break;
  69. case OS_O_RDWR:
  70. flags = O_RDWR;
  71. break;
  72. }
  73. if (os_flags & OS_O_CREAT)
  74. flags |= O_CREAT;
  75. return open(pathname, flags, 0777);
  76. }
  77. int os_close(int fd)
  78. {
  79. return close(fd);
  80. }
  81. void os_exit(int exit_code)
  82. {
  83. exit(exit_code);
  84. }
  85. /* Restore tty state when we exit */
  86. static struct termios orig_term;
  87. static void os_fd_restore(void)
  88. {
  89. tcsetattr(0, TCSANOW, &orig_term);
  90. }
  91. /* Put tty into raw mode so <tab> and <ctrl+c> work */
  92. void os_tty_raw(int fd)
  93. {
  94. static int setup = 0;
  95. struct termios term;
  96. if (setup)
  97. return;
  98. setup = 1;
  99. /* If not a tty, don't complain */
  100. if (tcgetattr(fd, &orig_term))
  101. return;
  102. term = orig_term;
  103. term.c_iflag = IGNBRK | IGNPAR;
  104. term.c_oflag = OPOST | ONLCR;
  105. term.c_cflag = CS8 | CREAD | CLOCAL;
  106. term.c_lflag = 0;
  107. if (tcsetattr(fd, TCSANOW, &term))
  108. return;
  109. atexit(os_fd_restore);
  110. }
  111. void *os_malloc(size_t length)
  112. {
  113. return mmap(NULL, length, PROT_READ | PROT_WRITE,
  114. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  115. }
  116. void os_usleep(unsigned long usec)
  117. {
  118. usleep(usec);
  119. }
  120. u64 os_get_nsec(void)
  121. {
  122. #if defined(CLOCK_MONOTONIC) && defined(_POSIX_MONOTONIC_CLOCK)
  123. struct timespec tp;
  124. if (EINVAL == clock_gettime(CLOCK_MONOTONIC, &tp)) {
  125. struct timeval tv;
  126. gettimeofday(&tv, NULL);
  127. tp.tv_sec = tv.tv_sec;
  128. tp.tv_nsec = tv.tv_usec * 1000;
  129. }
  130. return tp.tv_sec * 1000000000ULL + tp.tv_nsec;
  131. #else
  132. struct timeval tv;
  133. gettimeofday(&tv, NULL);
  134. return tv.tv_sec * 1000000000ULL + tv.tv_usec * 1000;
  135. #endif
  136. }
  137. static char *short_opts;
  138. static struct option *long_opts;
  139. int os_parse_args(struct sandbox_state *state, int argc, char *argv[])
  140. {
  141. struct sb_cmdline_option **sb_opt = __u_boot_sandbox_option_start;
  142. size_t num_options = __u_boot_sandbox_option_count();
  143. size_t i;
  144. int hidden_short_opt;
  145. size_t si;
  146. int c;
  147. if (short_opts || long_opts)
  148. return 1;
  149. state->argc = argc;
  150. state->argv = argv;
  151. /* dynamically construct the arguments to the system getopt_long */
  152. short_opts = os_malloc(sizeof(*short_opts) * num_options * 2 + 1);
  153. long_opts = os_malloc(sizeof(*long_opts) * num_options);
  154. if (!short_opts || !long_opts)
  155. return 1;
  156. /*
  157. * getopt_long requires "val" to be unique (since that is what the
  158. * func returns), so generate unique values automatically for flags
  159. * that don't have a short option. pick 0x100 as that is above the
  160. * single byte range (where ASCII/ISO-XXXX-X charsets live).
  161. */
  162. hidden_short_opt = 0x100;
  163. si = 0;
  164. for (i = 0; i < num_options; ++i) {
  165. long_opts[i].name = sb_opt[i]->flag;
  166. long_opts[i].has_arg = sb_opt[i]->has_arg ?
  167. required_argument : no_argument;
  168. long_opts[i].flag = NULL;
  169. if (sb_opt[i]->flag_short) {
  170. short_opts[si++] = long_opts[i].val = sb_opt[i]->flag_short;
  171. if (long_opts[i].has_arg == required_argument)
  172. short_opts[si++] = ':';
  173. } else
  174. long_opts[i].val = sb_opt[i]->flag_short = hidden_short_opt++;
  175. }
  176. short_opts[si] = '\0';
  177. /* we need to handle output ourselves since u-boot provides printf */
  178. opterr = 0;
  179. /*
  180. * walk all of the options the user gave us on the command line,
  181. * figure out what u-boot option structure they belong to (via
  182. * the unique short val key), and call the appropriate callback.
  183. */
  184. while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) {
  185. for (i = 0; i < num_options; ++i) {
  186. if (sb_opt[i]->flag_short == c) {
  187. if (sb_opt[i]->callback(state, optarg)) {
  188. state->parse_err = sb_opt[i]->flag;
  189. return 0;
  190. }
  191. break;
  192. }
  193. }
  194. if (i == num_options) {
  195. /*
  196. * store the faulting flag for later display. we have to
  197. * store the flag itself as the getopt parsing itself is
  198. * tricky: need to handle the following flags (assume all
  199. * of the below are unknown):
  200. * -a optopt='a' optind=<next>
  201. * -abbbb optopt='a' optind=<this>
  202. * -aaaaa optopt='a' optind=<this>
  203. * --a optopt=0 optind=<this>
  204. * as you can see, it is impossible to determine the exact
  205. * faulting flag without doing the parsing ourselves, so
  206. * we just report the specific flag that failed.
  207. */
  208. if (optopt) {
  209. static char parse_err[3] = { '-', 0, '\0', };
  210. parse_err[1] = optopt;
  211. state->parse_err = parse_err;
  212. } else
  213. state->parse_err = argv[optind - 1];
  214. break;
  215. }
  216. }
  217. return 0;
  218. }