os.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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 <dirent.h>
  22. #include <errno.h>
  23. #include <fcntl.h>
  24. #include <getopt.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <termios.h>
  29. #include <time.h>
  30. #include <unistd.h>
  31. #include <sys/mman.h>
  32. #include <sys/stat.h>
  33. #include <sys/time.h>
  34. #include <sys/types.h>
  35. #include <linux/types.h>
  36. #include <asm/getopt.h>
  37. #include <asm/sections.h>
  38. #include <asm/state.h>
  39. #include <os.h>
  40. /* Operating System Interface */
  41. ssize_t os_read(int fd, void *buf, size_t count)
  42. {
  43. return read(fd, buf, count);
  44. }
  45. ssize_t os_read_no_block(int fd, void *buf, size_t count)
  46. {
  47. const int flags = fcntl(fd, F_GETFL, 0);
  48. fcntl(fd, F_SETFL, flags | O_NONBLOCK);
  49. return os_read(fd, buf, count);
  50. }
  51. ssize_t os_write(int fd, const void *buf, size_t count)
  52. {
  53. return write(fd, buf, count);
  54. }
  55. off_t os_lseek(int fd, off_t offset, int whence)
  56. {
  57. if (whence == OS_SEEK_SET)
  58. whence = SEEK_SET;
  59. else if (whence == OS_SEEK_CUR)
  60. whence = SEEK_CUR;
  61. else if (whence == OS_SEEK_END)
  62. whence = SEEK_END;
  63. else
  64. os_exit(1);
  65. return lseek(fd, offset, whence);
  66. }
  67. int os_open(const char *pathname, int os_flags)
  68. {
  69. int flags;
  70. switch (os_flags & OS_O_MASK) {
  71. case OS_O_RDONLY:
  72. default:
  73. flags = O_RDONLY;
  74. break;
  75. case OS_O_WRONLY:
  76. flags = O_WRONLY;
  77. break;
  78. case OS_O_RDWR:
  79. flags = O_RDWR;
  80. break;
  81. }
  82. if (os_flags & OS_O_CREAT)
  83. flags |= O_CREAT;
  84. return open(pathname, flags, 0777);
  85. }
  86. int os_close(int fd)
  87. {
  88. return close(fd);
  89. }
  90. void os_exit(int exit_code)
  91. {
  92. exit(exit_code);
  93. }
  94. /* Restore tty state when we exit */
  95. static struct termios orig_term;
  96. static void os_fd_restore(void)
  97. {
  98. tcsetattr(0, TCSANOW, &orig_term);
  99. }
  100. /* Put tty into raw mode so <tab> and <ctrl+c> work */
  101. void os_tty_raw(int fd)
  102. {
  103. static int setup = 0;
  104. struct termios term;
  105. if (setup)
  106. return;
  107. setup = 1;
  108. /* If not a tty, don't complain */
  109. if (tcgetattr(fd, &orig_term))
  110. return;
  111. term = orig_term;
  112. term.c_iflag = IGNBRK | IGNPAR;
  113. term.c_oflag = OPOST | ONLCR;
  114. term.c_cflag = CS8 | CREAD | CLOCAL;
  115. term.c_lflag = 0;
  116. if (tcsetattr(fd, TCSANOW, &term))
  117. return;
  118. atexit(os_fd_restore);
  119. }
  120. void *os_malloc(size_t length)
  121. {
  122. return mmap(NULL, length, PROT_READ | PROT_WRITE,
  123. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  124. }
  125. void os_usleep(unsigned long usec)
  126. {
  127. usleep(usec);
  128. }
  129. u64 os_get_nsec(void)
  130. {
  131. #if defined(CLOCK_MONOTONIC) && defined(_POSIX_MONOTONIC_CLOCK)
  132. struct timespec tp;
  133. if (EINVAL == clock_gettime(CLOCK_MONOTONIC, &tp)) {
  134. struct timeval tv;
  135. gettimeofday(&tv, NULL);
  136. tp.tv_sec = tv.tv_sec;
  137. tp.tv_nsec = tv.tv_usec * 1000;
  138. }
  139. return tp.tv_sec * 1000000000ULL + tp.tv_nsec;
  140. #else
  141. struct timeval tv;
  142. gettimeofday(&tv, NULL);
  143. return tv.tv_sec * 1000000000ULL + tv.tv_usec * 1000;
  144. #endif
  145. }
  146. static char *short_opts;
  147. static struct option *long_opts;
  148. int os_parse_args(struct sandbox_state *state, int argc, char *argv[])
  149. {
  150. struct sb_cmdline_option **sb_opt = __u_boot_sandbox_option_start;
  151. size_t num_options = __u_boot_sandbox_option_count();
  152. size_t i;
  153. int hidden_short_opt;
  154. size_t si;
  155. int c;
  156. if (short_opts || long_opts)
  157. return 1;
  158. state->argc = argc;
  159. state->argv = argv;
  160. /* dynamically construct the arguments to the system getopt_long */
  161. short_opts = os_malloc(sizeof(*short_opts) * num_options * 2 + 1);
  162. long_opts = os_malloc(sizeof(*long_opts) * num_options);
  163. if (!short_opts || !long_opts)
  164. return 1;
  165. /*
  166. * getopt_long requires "val" to be unique (since that is what the
  167. * func returns), so generate unique values automatically for flags
  168. * that don't have a short option. pick 0x100 as that is above the
  169. * single byte range (where ASCII/ISO-XXXX-X charsets live).
  170. */
  171. hidden_short_opt = 0x100;
  172. si = 0;
  173. for (i = 0; i < num_options; ++i) {
  174. long_opts[i].name = sb_opt[i]->flag;
  175. long_opts[i].has_arg = sb_opt[i]->has_arg ?
  176. required_argument : no_argument;
  177. long_opts[i].flag = NULL;
  178. if (sb_opt[i]->flag_short) {
  179. short_opts[si++] = long_opts[i].val = sb_opt[i]->flag_short;
  180. if (long_opts[i].has_arg == required_argument)
  181. short_opts[si++] = ':';
  182. } else
  183. long_opts[i].val = sb_opt[i]->flag_short = hidden_short_opt++;
  184. }
  185. short_opts[si] = '\0';
  186. /* we need to handle output ourselves since u-boot provides printf */
  187. opterr = 0;
  188. /*
  189. * walk all of the options the user gave us on the command line,
  190. * figure out what u-boot option structure they belong to (via
  191. * the unique short val key), and call the appropriate callback.
  192. */
  193. while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) {
  194. for (i = 0; i < num_options; ++i) {
  195. if (sb_opt[i]->flag_short == c) {
  196. if (sb_opt[i]->callback(state, optarg)) {
  197. state->parse_err = sb_opt[i]->flag;
  198. return 0;
  199. }
  200. break;
  201. }
  202. }
  203. if (i == num_options) {
  204. /*
  205. * store the faulting flag for later display. we have to
  206. * store the flag itself as the getopt parsing itself is
  207. * tricky: need to handle the following flags (assume all
  208. * of the below are unknown):
  209. * -a optopt='a' optind=<next>
  210. * -abbbb optopt='a' optind=<this>
  211. * -aaaaa optopt='a' optind=<this>
  212. * --a optopt=0 optind=<this>
  213. * as you can see, it is impossible to determine the exact
  214. * faulting flag without doing the parsing ourselves, so
  215. * we just report the specific flag that failed.
  216. */
  217. if (optopt) {
  218. static char parse_err[3] = { '-', 0, '\0', };
  219. parse_err[1] = optopt;
  220. state->parse_err = parse_err;
  221. } else
  222. state->parse_err = argv[optind - 1];
  223. break;
  224. }
  225. }
  226. return 0;
  227. }
  228. void os_dirent_free(struct os_dirent_node *node)
  229. {
  230. struct os_dirent_node *next;
  231. while (node) {
  232. next = node->next;
  233. free(node);
  234. node = next;
  235. }
  236. }
  237. int os_dirent_ls(const char *dirname, struct os_dirent_node **headp)
  238. {
  239. struct dirent entry, *result;
  240. struct os_dirent_node *head, *node, *next;
  241. struct stat buf;
  242. DIR *dir;
  243. int ret;
  244. char *fname;
  245. int len;
  246. *headp = NULL;
  247. dir = opendir(dirname);
  248. if (!dir)
  249. return -1;
  250. /* Create a buffer for the maximum filename length */
  251. len = sizeof(entry.d_name) + strlen(dirname) + 2;
  252. fname = malloc(len);
  253. if (!fname) {
  254. ret = -ENOMEM;
  255. goto done;
  256. }
  257. for (node = head = NULL;; node = next) {
  258. ret = readdir_r(dir, &entry, &result);
  259. if (ret || !result)
  260. break;
  261. next = malloc(sizeof(*node) + strlen(entry.d_name) + 1);
  262. if (!next) {
  263. os_dirent_free(head);
  264. ret = -ENOMEM;
  265. goto done;
  266. }
  267. strcpy(next->name, entry.d_name);
  268. switch (entry.d_type) {
  269. case DT_REG:
  270. next->type = OS_FILET_REG;
  271. break;
  272. case DT_DIR:
  273. next->type = OS_FILET_DIR;
  274. break;
  275. case DT_LNK:
  276. next->type = OS_FILET_LNK;
  277. break;
  278. }
  279. next->size = 0;
  280. snprintf(fname, len, "%s/%s", dirname, next->name);
  281. if (!stat(fname, &buf))
  282. next->size = buf.st_size;
  283. if (node)
  284. node->next = next;
  285. if (!head)
  286. head = node;
  287. }
  288. *headp = head;
  289. done:
  290. closedir(dir);
  291. return ret;
  292. }
  293. const char *os_dirent_typename[OS_FILET_COUNT] = {
  294. " ",
  295. "SYM",
  296. "DIR",
  297. "???",
  298. };
  299. const char *os_dirent_get_typename(enum os_dirent_t type)
  300. {
  301. if (type >= 0 && type < OS_FILET_COUNT)
  302. return os_dirent_typename[type];
  303. return os_dirent_typename[OS_FILET_UNKNOWN];
  304. }
  305. ssize_t os_get_filesize(const char *fname)
  306. {
  307. struct stat buf;
  308. int ret;
  309. ret = stat(fname, &buf);
  310. if (ret)
  311. return ret;
  312. return buf.st_size;
  313. }