xref: /rk3399_rockchip-uboot/arch/sandbox/cpu/os.c (revision 70db4212fcdb080444a23dccaf673b68a3ffc1fa)
17a9219c1SSimon Glass /*
27a9219c1SSimon Glass  * Copyright (c) 2011 The Chromium OS Authors.
37a9219c1SSimon Glass  * See file CREDITS for list of people who contributed to this
47a9219c1SSimon Glass  * project.
57a9219c1SSimon Glass  *
67a9219c1SSimon Glass  * This program is free software; you can redistribute it and/or
77a9219c1SSimon Glass  * modify it under the terms of the GNU General Public License as
87a9219c1SSimon Glass  * published by the Free Software Foundation; either version 2 of
97a9219c1SSimon Glass  * the License, or (at your option) any later version.
107a9219c1SSimon Glass  *
117a9219c1SSimon Glass  * This program is distributed in the hope that it will be useful,
127a9219c1SSimon Glass  * but WITHOUT ANY WARRANTY; without even the implied warranty of
137a9219c1SSimon Glass  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
147a9219c1SSimon Glass  * GNU General Public License for more details.
157a9219c1SSimon Glass  *
167a9219c1SSimon Glass  * You should have received a copy of the GNU General Public License
177a9219c1SSimon Glass  * along with this program; if not, write to the Free Software
187a9219c1SSimon Glass  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
197a9219c1SSimon Glass  * MA 02111-1307 USA
207a9219c1SSimon Glass  */
217a9219c1SSimon Glass 
22e1012472SSimon Glass #include <errno.h>
237a9219c1SSimon Glass #include <fcntl.h>
24*70db4212SSimon Glass #include <getopt.h>
257a9219c1SSimon Glass #include <stdlib.h>
26ab06a758SMike Frysinger #include <termios.h>
27d99a6874SMatthias Weisser #include <time.h>
28e1012472SSimon Glass #include <unistd.h>
2921899b10SMatthias Weisser #include <sys/mman.h>
30e1012472SSimon Glass #include <sys/stat.h>
313bdf56b7SSimon Glass #include <sys/time.h>
32e1012472SSimon Glass #include <sys/types.h>
33d99a6874SMatthias Weisser #include <linux/types.h>
347a9219c1SSimon Glass 
35*70db4212SSimon Glass #include <asm/getopt.h>
36*70db4212SSimon Glass #include <asm/sections.h>
37*70db4212SSimon Glass #include <asm/state.h>
387a9219c1SSimon Glass #include <os.h>
397a9219c1SSimon Glass 
407a9219c1SSimon Glass /* Operating System Interface */
417a9219c1SSimon Glass 
427a9219c1SSimon Glass ssize_t os_read(int fd, void *buf, size_t count)
437a9219c1SSimon Glass {
447a9219c1SSimon Glass 	return read(fd, buf, count);
457a9219c1SSimon Glass }
467a9219c1SSimon Glass 
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 {
13521899b10SMatthias Weisser 	return mmap(NULL, length, PROT_READ | PROT_WRITE,
13621899b10SMatthias Weisser 			MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
13721899b10SMatthias Weisser }
138d99a6874SMatthias Weisser 
139d99a6874SMatthias Weisser void os_usleep(unsigned long usec)
140d99a6874SMatthias Weisser {
141d99a6874SMatthias Weisser 	usleep(usec);
142d99a6874SMatthias Weisser }
143d99a6874SMatthias Weisser 
144d99a6874SMatthias Weisser u64 os_get_nsec(void)
145d99a6874SMatthias Weisser {
146d99a6874SMatthias Weisser #if defined(CLOCK_MONOTONIC) && defined(_POSIX_MONOTONIC_CLOCK)
147d99a6874SMatthias Weisser 	struct timespec tp;
148d99a6874SMatthias Weisser 	if (EINVAL == clock_gettime(CLOCK_MONOTONIC, &tp)) {
149d99a6874SMatthias Weisser 		struct timeval tv;
150d99a6874SMatthias Weisser 
151d99a6874SMatthias Weisser 		gettimeofday(&tv, NULL);
152d99a6874SMatthias Weisser 		tp.tv_sec = tv.tv_sec;
153d99a6874SMatthias Weisser 		tp.tv_nsec = tv.tv_usec * 1000;
154d99a6874SMatthias Weisser 	}
155d99a6874SMatthias Weisser 	return tp.tv_sec * 1000000000ULL + tp.tv_nsec;
156d99a6874SMatthias Weisser #else
157d99a6874SMatthias Weisser 	struct timeval tv;
158d99a6874SMatthias Weisser 	gettimeofday(&tv, NULL);
159d99a6874SMatthias Weisser 	return tv.tv_sec * 1000000000ULL + tv.tv_usec * 1000;
160d99a6874SMatthias Weisser #endif
161d99a6874SMatthias Weisser }
162*70db4212SSimon Glass 
163*70db4212SSimon Glass static char *short_opts;
164*70db4212SSimon Glass static struct option *long_opts;
165*70db4212SSimon Glass 
166*70db4212SSimon Glass int os_parse_args(struct sandbox_state *state, int argc, char *argv[])
167*70db4212SSimon Glass {
168*70db4212SSimon Glass 	struct sb_cmdline_option **sb_opt = __u_boot_sandbox_option_start;
169*70db4212SSimon Glass 	size_t num_options = __u_boot_sandbox_option_count();
170*70db4212SSimon Glass 	size_t i;
171*70db4212SSimon Glass 
172*70db4212SSimon Glass 	int hidden_short_opt;
173*70db4212SSimon Glass 	size_t si;
174*70db4212SSimon Glass 
175*70db4212SSimon Glass 	int c;
176*70db4212SSimon Glass 
177*70db4212SSimon Glass 	if (short_opts || long_opts)
178*70db4212SSimon Glass 		return 1;
179*70db4212SSimon Glass 
180*70db4212SSimon Glass 	state->argc = argc;
181*70db4212SSimon Glass 	state->argv = argv;
182*70db4212SSimon Glass 
183*70db4212SSimon Glass 	/* dynamically construct the arguments to the system getopt_long */
184*70db4212SSimon Glass 	short_opts = os_malloc(sizeof(*short_opts) * num_options * 2 + 1);
185*70db4212SSimon Glass 	long_opts = os_malloc(sizeof(*long_opts) * num_options);
186*70db4212SSimon Glass 	if (!short_opts || !long_opts)
187*70db4212SSimon Glass 		return 1;
188*70db4212SSimon Glass 
189*70db4212SSimon Glass 	/*
190*70db4212SSimon Glass 	 * getopt_long requires "val" to be unique (since that is what the
191*70db4212SSimon Glass 	 * func returns), so generate unique values automatically for flags
192*70db4212SSimon Glass 	 * that don't have a short option.  pick 0x100 as that is above the
193*70db4212SSimon Glass 	 * single byte range (where ASCII/ISO-XXXX-X charsets live).
194*70db4212SSimon Glass 	 */
195*70db4212SSimon Glass 	hidden_short_opt = 0x100;
196*70db4212SSimon Glass 	si = 0;
197*70db4212SSimon Glass 	for (i = 0; i < num_options; ++i) {
198*70db4212SSimon Glass 		long_opts[i].name = sb_opt[i]->flag;
199*70db4212SSimon Glass 		long_opts[i].has_arg = sb_opt[i]->has_arg ?
200*70db4212SSimon Glass 			required_argument : no_argument;
201*70db4212SSimon Glass 		long_opts[i].flag = NULL;
202*70db4212SSimon Glass 
203*70db4212SSimon Glass 		if (sb_opt[i]->flag_short) {
204*70db4212SSimon Glass 			short_opts[si++] = long_opts[i].val = sb_opt[i]->flag_short;
205*70db4212SSimon Glass 			if (long_opts[i].has_arg == required_argument)
206*70db4212SSimon Glass 				short_opts[si++] = ':';
207*70db4212SSimon Glass 		} else
208*70db4212SSimon Glass 			long_opts[i].val = sb_opt[i]->flag_short = hidden_short_opt++;
209*70db4212SSimon Glass 	}
210*70db4212SSimon Glass 	short_opts[si] = '\0';
211*70db4212SSimon Glass 
212*70db4212SSimon Glass 	/* we need to handle output ourselves since u-boot provides printf */
213*70db4212SSimon Glass 	opterr = 0;
214*70db4212SSimon Glass 
215*70db4212SSimon Glass 	/*
216*70db4212SSimon Glass 	 * walk all of the options the user gave us on the command line,
217*70db4212SSimon Glass 	 * figure out what u-boot option structure they belong to (via
218*70db4212SSimon Glass 	 * the unique short val key), and call the appropriate callback.
219*70db4212SSimon Glass 	 */
220*70db4212SSimon Glass 	while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) {
221*70db4212SSimon Glass 		for (i = 0; i < num_options; ++i) {
222*70db4212SSimon Glass 			if (sb_opt[i]->flag_short == c) {
223*70db4212SSimon Glass 				if (sb_opt[i]->callback(state, optarg)) {
224*70db4212SSimon Glass 					state->parse_err = sb_opt[i]->flag;
225*70db4212SSimon Glass 					return 0;
226*70db4212SSimon Glass 				}
227*70db4212SSimon Glass 				break;
228*70db4212SSimon Glass 			}
229*70db4212SSimon Glass 		}
230*70db4212SSimon Glass 		if (i == num_options) {
231*70db4212SSimon Glass 			/*
232*70db4212SSimon Glass 			 * store the faulting flag for later display.  we have to
233*70db4212SSimon Glass 			 * store the flag itself as the getopt parsing itself is
234*70db4212SSimon Glass 			 * tricky: need to handle the following flags (assume all
235*70db4212SSimon Glass 			 * of the below are unknown):
236*70db4212SSimon Glass 			 *   -a        optopt='a' optind=<next>
237*70db4212SSimon Glass 			 *   -abbbb    optopt='a' optind=<this>
238*70db4212SSimon Glass 			 *   -aaaaa    optopt='a' optind=<this>
239*70db4212SSimon Glass 			 *   --a       optopt=0   optind=<this>
240*70db4212SSimon Glass 			 * as you can see, it is impossible to determine the exact
241*70db4212SSimon Glass 			 * faulting flag without doing the parsing ourselves, so
242*70db4212SSimon Glass 			 * we just report the specific flag that failed.
243*70db4212SSimon Glass 			 */
244*70db4212SSimon Glass 			if (optopt) {
245*70db4212SSimon Glass 				static char parse_err[3] = { '-', 0, '\0', };
246*70db4212SSimon Glass 				parse_err[1] = optopt;
247*70db4212SSimon Glass 				state->parse_err = parse_err;
248*70db4212SSimon Glass 			} else
249*70db4212SSimon Glass 				state->parse_err = argv[optind - 1];
250*70db4212SSimon Glass 			break;
251*70db4212SSimon Glass 		}
252*70db4212SSimon Glass 	}
253*70db4212SSimon Glass 
254*70db4212SSimon Glass 	return 0;
255*70db4212SSimon Glass }
256