Browse Source

[linux] Add most of the linux api

Signed-off-by: Piotr Jaroszyński <p.jaroszynski@gmail.com>
Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Piotr Jaroszyński 14 years ago
parent
commit
1812bfd5d0
2 changed files with 283 additions and 0 deletions
  1. 114
    0
      src/arch/x86/core/linux/linux_api.c
  2. 169
    0
      src/arch/x86/core/linux/linux_strerror.c

+ 114
- 0
src/arch/x86/core/linux/linux_api.c View File

@@ -0,0 +1,114 @@
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
+{
34
+	return linux_syscall(__NR_open, pathname, flags);
35
+}
36
+
37
+int linux_close(int fd)
38
+{
39
+	return linux_syscall(__NR_close, fd);
40
+}
41
+
42
+ssize_t linux_read(int fd, void *buf, size_t count)
43
+{
44
+	return linux_syscall(__NR_read, fd, buf, count);
45
+}
46
+
47
+ssize_t linux_write(int fd, const void *buf, size_t count)
48
+{
49
+	return linux_syscall(__NR_write, fd, buf, count);
50
+}
51
+
52
+int linux_fcntl(int fd, int cmd, ...)
53
+{
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
+{
66
+	void *arg;
67
+	va_list list;
68
+
69
+	va_start(list, request);
70
+	arg = va_arg(list, void *);
71
+	va_end(list);
72
+
73
+	return linux_syscall(__NR_ioctl, fd, request, arg);
74
+}
75
+
76
+int linux_poll(struct pollfd *fds, nfds_t nfds, int timeout)
77
+{
78
+	return linux_syscall(__NR_poll, fds, nfds, timeout);
79
+}
80
+
81
+int linux_nanosleep(const struct timespec *req, struct timespec *rem)
82
+{
83
+	return linux_syscall(__NR_nanosleep, req, rem);
84
+}
85
+
86
+int linux_usleep(useconds_t usec)
87
+{
88
+	struct timespec ts = {
89
+		.tv_sec = (long) (usec / 1000000),
90
+		.tv_nsec = (long) (usec % 1000000) * 1000ul
91
+	};
92
+
93
+	return linux_nanosleep(&ts, NULL);
94
+}
95
+
96
+int linux_gettimeofday(struct timeval *tv, struct timezone *tz)
97
+{
98
+	return linux_syscall(__NR_gettimeofday, tv, tz);
99
+}
100
+
101
+void *linux_mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset)
102
+{
103
+	return (void *)linux_syscall(__SYSCALL_mmap, addr, length, prot, flags, fd, offset);
104
+}
105
+
106
+void *linux_mremap(void *old_address, size_t old_size, size_t new_size, int flags)
107
+{
108
+	return (void *)linux_syscall(__NR_mremap, old_address, old_size, new_size, flags);
109
+}
110
+
111
+int linux_munmap(void *addr, size_t length)
112
+{
113
+	return linux_syscall(__NR_munmap, addr, length);
114
+}

+ 169
- 0
src/arch/x86/core/linux/linux_strerror.c View File

@@ -0,0 +1,169 @@
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
+ * linux_strerror implementation
24
+ */
25
+
26
+#include <linux_api.h>
27
+#include <stdio.h>
28
+
29
+/** Error names from glibc */
30
+static const char *errors[] = {
31
+	"Success",
32
+	"Operation not permitted",
33
+	"No such file or directory",
34
+	"No such process",
35
+	"Interrupted system call",
36
+	"Input/output error",
37
+	"No such device or address",
38
+	"Argument list too long",
39
+	"Exec format error",
40
+	"Bad file descriptor",
41
+	"No child processes",
42
+	"Resource temporarily unavailable",
43
+	"Cannot allocate memory",
44
+	"Permission denied",
45
+	"Bad address",
46
+	"Block device required",
47
+	"Device or resource busy",
48
+	"File exists",
49
+	"Invalid cross-device link",
50
+	"No such device",
51
+	"Not a directory",
52
+	"Is a directory",
53
+	"Invalid argument",
54
+	"Too many open files in system",
55
+	"Too many open files",
56
+	"Inappropriate ioctl for device",
57
+	"Text file busy",
58
+	"File too large",
59
+	"No space left on device",
60
+	"Illegal seek",
61
+	"Read-only file system",
62
+	"Too many links",
63
+	"Broken pipe",
64
+	"Numerical argument out of domain",
65
+	"Numerical result out of range",
66
+	"Resource deadlock avoided",
67
+	"File name too long",
68
+	"No locks available",
69
+	"Function not implemented",
70
+	"Directory not empty",
71
+	"Too many levels of symbolic links",
72
+	"",
73
+	"No message of desired type",
74
+	"Identifier removed",
75
+	"Channel number out of range",
76
+	"Level 2 not synchronized",
77
+	"Level 3 halted",
78
+	"Level 3 reset",
79
+	"Link number out of range",
80
+	"Protocol driver not attached",
81
+	"No CSI structure available",
82
+	"Level 2 halted",
83
+	"Invalid exchange",
84
+	"Invalid request descriptor",
85
+	"Exchange full",
86
+	"No anode",
87
+	"Invalid request code",
88
+	"Invalid slot",
89
+	"",
90
+	"Bad font file format",
91
+	"Device not a stream",
92
+	"No data available",
93
+	"Timer expired",
94
+	"Out of streams resources",
95
+	"Machine is not on the network",
96
+	"Package not installed",
97
+	"Object is remote",
98
+	"Link has been severed",
99
+	"Advertise error",
100
+	"Srmount error",
101
+	"Communication error on send",
102
+	"Protocol error",
103
+	"Multihop attempted",
104
+	"RFS specific error",
105
+	"Bad message",
106
+	"Value too large for defined data type",
107
+	"Name not unique on network",
108
+	"File descriptor in bad state",
109
+	"Remote address changed",
110
+	"Can not access a needed shared library",
111
+	"Accessing a corrupted shared library",
112
+	".lib section in a.out corrupted",
113
+	"Attempting to link in too many shared libraries",
114
+	"Cannot exec a shared library directly",
115
+	"Invalid or incomplete multibyte or wide character",
116
+	"Interrupted system call should be restarted",
117
+	"Streams pipe error",
118
+	"Too many users",
119
+	"Socket operation on non-socket",
120
+	"Destination address required",
121
+	"Message too long",
122
+	"Protocol wrong type for socket",
123
+	"Protocol not available",
124
+	"Protocol not supported",
125
+	"Socket type not supported",
126
+	"Operation not supported",
127
+	"Protocol family not supported",
128
+	"Address family not supported by protocol",
129
+	"Address already in use",
130
+	"Cannot assign requested address",
131
+	"Network is down",
132
+	"Network is unreachable",
133
+	"Network dropped connection on reset",
134
+	"Software caused connection abort",
135
+	"Connection reset by peer",
136
+	"No buffer space available",
137
+	"Transport endpoint is already connected",
138
+	"Transport endpoint is not connected",
139
+	"Cannot send after transport endpoint shutdown",
140
+	"Too many references: cannot splice",
141
+	"Connection timed out",
142
+	"Connection refused",
143
+	"Host is down",
144
+	"No route to host",
145
+	"Operation already in progress",
146
+	"Operation now in progress",
147
+	"Stale NFS file handle",
148
+	"Structure needs cleaning",
149
+	"Not a XENIX named type file",
150
+	"No XENIX semaphores available",
151
+	"Is a named type file",
152
+	"Remote I/O error",
153
+	"Disk quota exceeded",
154
+	"No medium found",
155
+	"Wrong medium type",
156
+};
157
+
158
+const char *linux_strerror(int errnum)
159
+{
160
+	static char errbuf[64];
161
+	static int errors_size = sizeof(errors) / sizeof(*errors);
162
+	
163
+	if (errnum >= errors_size || errnum < 0) {
164
+		snprintf(errbuf, sizeof(errbuf), "Error %#08x", errnum);
165
+		return errbuf;
166
+	} else {
167
+		return errors[errnum];
168
+	}
169
+}

Loading…
Cancel
Save