iPXE
linux_api.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2010 Piotr JaroszyƄski <p.jaroszynski@gmail.com>
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18 
19 FILE_LICENCE ( GPL2_OR_LATER );
20 
21 /** @file
22  *
23  * Implementation of most of the linux API.
24  */
25 
26 #include <linux_api.h>
27 
28 #include <stdarg.h>
29 #include <asm/unistd.h>
30 #include <string.h>
31 
32 int linux_open ( const char *pathname, int flags ) {
33  return linux_syscall ( __NR_open, pathname, flags );
34 }
35 
36 int linux_close ( int fd ) {
37  return linux_syscall ( __NR_close, fd );
38 }
39 
40 off_t linux_lseek ( int fd, off_t offset, int whence ) {
41  return linux_syscall ( __NR_lseek, fd, offset, whence );
42 }
43 
44 __kernel_ssize_t linux_read ( int fd, void *buf, __kernel_size_t count ) {
45  return linux_syscall ( __NR_read, fd, buf, count );
46 }
47 
48 __kernel_ssize_t linux_write ( int fd, const void *buf,
49  __kernel_size_t count ) {
50  return linux_syscall ( __NR_write, fd, buf, count );
51 }
52 
53 int linux_fcntl ( int fd, int cmd, ... ) {
54  long arg;
55  va_list list;
56 
57  va_start ( list, cmd );
58  arg = va_arg ( list, long );
59  va_end ( list );
60 
61  return linux_syscall ( __NR_fcntl, fd, cmd, arg );
62 }
63 
64 int linux_ioctl ( int fd, int request, ... ) {
65  void *arg;
66  va_list list;
67 
68  va_start ( list, request );
69  arg = va_arg ( list, void * );
70  va_end ( list );
71 
72  return linux_syscall ( __NR_ioctl, fd, request, arg );
73 }
74 
75 int linux_poll ( struct pollfd *fds, nfds_t nfds, int timeout ) {
76  return linux_syscall ( __NR_poll, fds, nfds, timeout );
77 }
78 
79 int linux_nanosleep ( const struct timespec *req, struct timespec *rem ) {
80  return linux_syscall ( __NR_nanosleep, req, rem );
81 }
82 
83 int linux_usleep ( useconds_t usec ) {
84  struct timespec ts = {
85  .tv_sec = ( ( long ) ( usec / 1000000 ) ),
86  .tv_nsec = ( ( long ) ( usec % 1000000 ) * 1000UL ),
87  };
88 
89  return linux_nanosleep ( &ts, NULL );
90 }
91 
92 int linux_gettimeofday ( struct timeval *tv, struct timezone *tz ) {
93  return linux_syscall ( __NR_gettimeofday, tv, tz );
94 }
95 
96 void * linux_mmap ( void *addr, __kernel_size_t length, int prot, int flags,
97  int fd, __kernel_off_t offset ) {
98  return ( void * ) linux_syscall ( __SYSCALL_mmap, addr, length, prot,
99  flags, fd, offset );
100 }
101 
102 void * linux_mremap ( void *old_address, __kernel_size_t old_size,
103  __kernel_size_t new_size, int flags ) {
104  return ( void * ) linux_syscall ( __NR_mremap, old_address, old_size,
105  new_size, flags );
106 }
107 
108 int linux_munmap ( void *addr, __kernel_size_t length ) {
109  return linux_syscall ( __NR_munmap, addr, length );
110 }
111 
112 int linux_socket ( int domain, int type_, int protocol ) {
113 #ifdef __NR_socket
114  return linux_syscall ( __NR_socket, domain, type_, protocol );
115 #else
116 #ifndef SOCKOP_socket
117 # define SOCKOP_socket 1
118 #endif
119  unsigned long sc_args[] = { domain, type_, protocol };
120  return linux_syscall ( __NR_socketcall, SOCKOP_socket, sc_args );
121 #endif
122 }
123 
124 int linux_bind ( int fd, const struct sockaddr *addr, socklen_t addrlen ) {
125 #ifdef __NR_bind
126  return linux_syscall ( __NR_bind, fd, addr, addrlen );
127 #else
128 #ifndef SOCKOP_bind
129 # define SOCKOP_bind 2
130 #endif
131  unsigned long sc_args[] = { fd, (unsigned long)addr, addrlen };
132  return linux_syscall ( __NR_socketcall, SOCKOP_bind, sc_args );
133 #endif
134 }
135 
136 ssize_t linux_sendto ( int fd, const void *buf, size_t len, int flags,
137  const struct sockaddr *daddr, socklen_t addrlen ) {
138 #ifdef __NR_sendto
139  return linux_syscall ( __NR_sendto, fd, buf, len, flags,
140  daddr, addrlen );
141 #else
142 #ifndef SOCKOP_sendto
143 # define SOCKOP_sendto 11
144 #endif
145  unsigned long sc_args[] = { fd, (unsigned long)buf, len,
146  flags, (unsigned long)daddr, addrlen };
147  return linux_syscall ( __NR_socketcall, SOCKOP_sendto, sc_args );
148 #endif
149 }
uint16_t length
Length.
Definition: intel.h:14
void * linux_mmap(void *addr, __kernel_size_t length, int prot, int flags, int fd, __kernel_off_t offset)
Definition: linux_api.c:96
#define va_end(ap)
Definition: stdarg.h:9
#define SOCKOP_socket
#define SOCKOP_bind
int linux_close(int fd)
Definition: linux_api.c:36
int linux_nanosleep(const struct timespec *req, struct timespec *rem)
Definition: linux_api.c:79
int linux_usleep(useconds_t usec)
Definition: linux_api.c:83
int linux_bind(int fd, const struct sockaddr *addr, socklen_t addrlen)
Definition: linux_api.c:124
uint32_t useconds_t
Definition: linux_api.h:48
void * linux_mremap(void *old_address, __kernel_size_t old_size, __kernel_size_t new_size, int flags)
Definition: linux_api.c:102
#define SOCKOP_sendto
int linux_gettimeofday(struct timeval *tv, struct timezone *tz)
Definition: linux_api.c:92
int linux_poll(struct pollfd *fds, nfds_t nfds, int timeout)
Definition: linux_api.c:75
uint32_t socklen_t
Definition: linux_api.h:49
long linux_syscall(int number,...)
int linux_ioctl(int fd, int request,...)
Definition: linux_api.c:64
__kernel_ssize_t linux_write(int fd, const void *buf, __kernel_size_t count)
Definition: linux_api.c:48
FILE_LICENCE(GPL2_OR_LATER)
#define va_arg(ap, type)
Definition: stdarg.h:8
int linux_open(const char *pathname, int flags)
Definition: linux_api.c:32
static userptr_t size_t offset
Offset of the first segment within the content.
Definition: deflate.h:259
ssize_t linux_sendto(int fd, const void *buf, size_t len, int flags, const struct sockaddr *daddr, socklen_t addrlen)
Definition: linux_api.c:136
Generalized socket address structure.
Definition: socket.h:96
u32 addr
Definition: sky2.h:8
off_t linux_lseek(int fd, off_t offset, int whence)
Definition: linux_api.c:40
int linux_munmap(void *addr, __kernel_size_t length)
Definition: linux_api.c:108
signed long off_t
Definition: stdint.h:8
uint32_t len
Length.
Definition: ena.h:14
__builtin_va_list va_list
Definition: stdarg.h:6
uint16_t count
Number of entries.
Definition: ena.h:22
int linux_fcntl(int fd, int cmd,...)
Definition: linux_api.c:53
u8 request[0]
List of IEs requested.
Definition: ieee80211.h:16
int linux_socket(int domain, int type_, int protocol)
Definition: linux_api.c:112
unsigned long nfds_t
Definition: linux_api.h:47
void timeout(int)
uint16_t protocol
Protocol ID.
Definition: stp.h:18
signed long ssize_t
Definition: stdint.h:7
#define va_start(ap, last)
Definition: stdarg.h:7
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362
struct golan_eqe_cmd cmd
Definition: CIB_PRM.h:29
String functions.
#define __SYSCALL_mmap
Definition: linux_api.h:4
struct ntlm_data domain
Domain name.
Definition: ntlm.h:16
__kernel_ssize_t linux_read(int fd, void *buf, __kernel_size_t count)
Definition: linux_api.c:44
uint8_t flags
Flags.
Definition: ena.h:18