Simon Glass | cd0684f | 2011-10-03 19:26:44 +0000 | [diff] [blame] | 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 | |
| 22 | #include <fcntl.h> |
| 23 | #include <stdlib.h> |
Mike Frysinger | a5baaee | 2011-10-26 00:21:29 +0000 | [diff] [blame] | 24 | #include <termios.h> |
Simon Glass | cd0684f | 2011-10-03 19:26:44 +0000 | [diff] [blame] | 25 | #include <unistd.h> |
| 26 | #include <sys/types.h> |
| 27 | #include <sys/stat.h> |
Matthias Weisser | b5f7b47 | 2011-11-05 11:40:34 +0100 | [diff] [blame^] | 28 | #include <sys/mman.h> |
Simon Glass | cd0684f | 2011-10-03 19:26:44 +0000 | [diff] [blame] | 29 | |
| 30 | #include <os.h> |
| 31 | |
| 32 | /* Operating System Interface */ |
| 33 | |
| 34 | ssize_t os_read(int fd, void *buf, size_t count) |
| 35 | { |
| 36 | return read(fd, buf, count); |
| 37 | } |
| 38 | |
| 39 | ssize_t os_write(int fd, const void *buf, size_t count) |
| 40 | { |
| 41 | return write(fd, buf, count); |
| 42 | } |
| 43 | |
| 44 | int os_open(const char *pathname, int flags) |
| 45 | { |
| 46 | return open(pathname, flags); |
| 47 | } |
| 48 | |
| 49 | int os_close(int fd) |
| 50 | { |
| 51 | return close(fd); |
| 52 | } |
| 53 | |
| 54 | void os_exit(int exit_code) |
| 55 | { |
| 56 | exit(exit_code); |
| 57 | } |
Mike Frysinger | a5baaee | 2011-10-26 00:21:29 +0000 | [diff] [blame] | 58 | |
| 59 | /* Restore tty state when we exit */ |
| 60 | static struct termios orig_term; |
| 61 | |
| 62 | static void os_fd_restore(void) |
| 63 | { |
| 64 | tcsetattr(0, TCSANOW, &orig_term); |
| 65 | } |
| 66 | |
| 67 | /* Put tty into raw mode so <tab> and <ctrl+c> work */ |
| 68 | void os_tty_raw(int fd) |
| 69 | { |
| 70 | static int setup = 0; |
| 71 | struct termios term; |
| 72 | |
| 73 | if (setup) |
| 74 | return; |
| 75 | setup = 1; |
| 76 | |
| 77 | /* If not a tty, don't complain */ |
| 78 | if (tcgetattr(fd, &orig_term)) |
| 79 | return; |
| 80 | |
| 81 | term = orig_term; |
| 82 | term.c_iflag = IGNBRK | IGNPAR; |
| 83 | term.c_oflag = OPOST | ONLCR; |
| 84 | term.c_cflag = CS8 | CREAD | CLOCAL; |
| 85 | term.c_lflag = 0; |
| 86 | if (tcsetattr(fd, TCSANOW, &term)) |
| 87 | return; |
| 88 | |
| 89 | atexit(os_fd_restore); |
| 90 | } |
Matthias Weisser | b5f7b47 | 2011-11-05 11:40:34 +0100 | [diff] [blame^] | 91 | |
| 92 | void *os_malloc(size_t length) |
| 93 | { |
| 94 | return mmap(NULL, length, PROT_READ | PROT_WRITE, |
| 95 | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
| 96 | } |