iPXE
linux_api.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2010 Piotr JaroszyƄski <p.jaroszynski@gmail.com>
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
00017  */
00018 
00019 FILE_LICENCE ( GPL2_OR_LATER );
00020 
00021 /** @file
00022  *
00023  * Implementation of most of the linux API.
00024  */
00025 
00026 #include <linux_api.h>
00027 
00028 #include <stdarg.h>
00029 #include <asm/unistd.h>
00030 #include <string.h>
00031 
00032 int linux_open ( const char *pathname, int flags ) {
00033         return linux_syscall ( __NR_open, pathname, flags );
00034 }
00035 
00036 int linux_close ( int fd ) {
00037         return linux_syscall ( __NR_close, fd );
00038 }
00039 
00040 off_t linux_lseek ( int fd, off_t offset, int whence ) {
00041         return linux_syscall ( __NR_lseek, fd, offset, whence );
00042 }
00043 
00044 __kernel_ssize_t linux_read ( int fd, void *buf, __kernel_size_t count ) {
00045         return linux_syscall ( __NR_read, fd, buf, count );
00046 }
00047 
00048 __kernel_ssize_t linux_write ( int fd, const void *buf,
00049                                __kernel_size_t count ) {
00050         return linux_syscall  (  __NR_write, fd, buf, count );
00051 }
00052 
00053 int linux_fcntl ( int fd, int cmd, ... ) {
00054         long arg;
00055         va_list list;
00056 
00057         va_start ( list, cmd );
00058         arg = va_arg ( list, long );
00059         va_end ( list );
00060 
00061         return linux_syscall ( __NR_fcntl, fd, cmd, arg );
00062 }
00063 
00064 int linux_ioctl ( int fd, int request, ... ) {
00065         void *arg;
00066         va_list list;
00067 
00068         va_start ( list, request );
00069         arg = va_arg ( list, void * );
00070         va_end ( list );
00071 
00072         return linux_syscall ( __NR_ioctl, fd, request, arg );
00073 }
00074 
00075 int linux_poll ( struct pollfd *fds, nfds_t nfds, int timeout ) {
00076         return linux_syscall ( __NR_poll, fds, nfds, timeout );
00077 }
00078 
00079 int linux_nanosleep ( const struct timespec *req, struct timespec *rem ) {
00080         return linux_syscall ( __NR_nanosleep, req, rem );
00081 }
00082 
00083 int linux_usleep ( useconds_t usec ) {
00084         struct timespec ts = {
00085                 .tv_sec = ( ( long ) ( usec / 1000000 ) ),
00086                 .tv_nsec = ( ( long ) ( usec % 1000000 ) * 1000UL ),
00087         };
00088 
00089         return linux_nanosleep ( &ts, NULL );
00090 }
00091 
00092 int linux_gettimeofday ( struct timeval *tv, struct timezone *tz ) {
00093         return linux_syscall ( __NR_gettimeofday, tv, tz );
00094 }
00095 
00096 void * linux_mmap ( void *addr, __kernel_size_t length, int prot, int flags,
00097                     int fd, __kernel_off_t offset ) {
00098         return ( void * ) linux_syscall ( __SYSCALL_mmap, addr, length, prot,
00099                                           flags, fd, offset );
00100 }
00101 
00102 void * linux_mremap ( void *old_address, __kernel_size_t old_size,
00103                       __kernel_size_t new_size, int flags ) {
00104         return ( void * ) linux_syscall ( __NR_mremap, old_address, old_size,
00105                                           new_size, flags );
00106 }
00107 
00108 int linux_munmap ( void *addr, __kernel_size_t length ) {
00109         return linux_syscall ( __NR_munmap, addr, length );
00110 }
00111 
00112 int linux_socket ( int domain, int type_, int protocol ) {
00113 #ifdef __NR_socket
00114         return linux_syscall ( __NR_socket, domain, type_, protocol );
00115 #else
00116 #ifndef SOCKOP_socket
00117 # define SOCKOP_socket 1
00118 #endif
00119         unsigned long sc_args[] = { domain, type_, protocol };
00120         return linux_syscall ( __NR_socketcall, SOCKOP_socket, sc_args );
00121 #endif
00122 }
00123 
00124 int linux_bind ( int fd, const struct sockaddr *addr, socklen_t addrlen ) {
00125 #ifdef __NR_bind
00126         return linux_syscall ( __NR_bind, fd, addr, addrlen );
00127 #else
00128 #ifndef SOCKOP_bind
00129 # define SOCKOP_bind 2
00130 #endif
00131         unsigned long sc_args[] = { fd, (unsigned long)addr, addrlen };
00132         return linux_syscall ( __NR_socketcall, SOCKOP_bind, sc_args );
00133 #endif
00134 }
00135 
00136 ssize_t linux_sendto ( int fd, const void *buf, size_t len, int flags,
00137                        const struct sockaddr *daddr, socklen_t addrlen ) {
00138 #ifdef __NR_sendto
00139         return linux_syscall ( __NR_sendto, fd, buf, len, flags,
00140                                daddr, addrlen );
00141 #else
00142 #ifndef SOCKOP_sendto
00143 # define SOCKOP_sendto 11
00144 #endif
00145         unsigned long sc_args[] = { fd, (unsigned long)buf, len,
00146                                     flags, (unsigned long)daddr, addrlen };
00147         return linux_syscall ( __NR_socketcall, SOCKOP_sendto, sc_args );
00148 #endif
00149 }