|
@@ -0,0 +1,191 @@
|
|
1
|
+/** @file
|
|
2
|
+ *
|
|
3
|
+ * PXE FILE API
|
|
4
|
+ *
|
|
5
|
+ */
|
|
6
|
+
|
|
7
|
+#include <stdlib.h>
|
|
8
|
+#include <stdio.h>
|
|
9
|
+#include <errno.h>
|
|
10
|
+#include <byteswap.h>
|
|
11
|
+#include <gpxe/uaccess.h>
|
|
12
|
+#include <gpxe/posix_io.h>
|
|
13
|
+#include <gpxe/features.h>
|
|
14
|
+#include <pxe.h>
|
|
15
|
+
|
|
16
|
+/*
|
|
17
|
+ * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
|
|
18
|
+ *
|
|
19
|
+ * This program is free software; you can redistribute it and/or
|
|
20
|
+ * modify it under the terms of the GNU General Public License as
|
|
21
|
+ * published by the Free Software Foundation; either version 2 of the
|
|
22
|
+ * License, or any later version.
|
|
23
|
+ *
|
|
24
|
+ * This program is distributed in the hope that it will be useful, but
|
|
25
|
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
26
|
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
27
|
+ * General Public License for more details.
|
|
28
|
+ *
|
|
29
|
+ * You should have received a copy of the GNU General Public License
|
|
30
|
+ * along with this program; if not, write to the Free Software
|
|
31
|
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
32
|
+ */
|
|
33
|
+
|
|
34
|
+FEATURE ( FEATURE_MISC, "PXEXT", DHCP_EB_FEATURE_PXE_EXT, 1 );
|
|
35
|
+
|
|
36
|
+/**
|
|
37
|
+ * FILE OPEN
|
|
38
|
+ *
|
|
39
|
+ * @v file_open Pointer to a struct s_PXENV_FILE_OPEN
|
|
40
|
+ * @v s_PXENV_FILE_OPEN::FileName URL of file to open
|
|
41
|
+ * @ret #PXENV_EXIT_SUCCESS File was opened
|
|
42
|
+ * @ret #PXENV_EXIT_FAILURE File was not opened
|
|
43
|
+ * @ret s_PXENV_FILE_OPEN::Status PXE status code
|
|
44
|
+ * @ret s_PXENV_FILE_OPEN::FileHandle Handle of opened file
|
|
45
|
+ *
|
|
46
|
+ */
|
|
47
|
+PXENV_EXIT_t pxenv_file_open ( struct s_PXENV_FILE_OPEN *file_open ) {
|
|
48
|
+ userptr_t filename;
|
|
49
|
+ size_t filename_len;
|
|
50
|
+ int fd;
|
|
51
|
+
|
|
52
|
+ DBG ( "PXENV_FILE_OPEN" );
|
|
53
|
+
|
|
54
|
+ /* Copy name from external program, and open it */
|
|
55
|
+ filename = real_to_user ( file_open->FileName.segment,
|
|
56
|
+ file_open->FileName.offset );
|
|
57
|
+ filename_len = strlen_user ( filename, 0 );
|
|
58
|
+ {
|
|
59
|
+ char uri_string[ filename_len + 1 ];
|
|
60
|
+
|
|
61
|
+ copy_from_user ( uri_string, filename, 0,
|
|
62
|
+ sizeof ( uri_string ) );
|
|
63
|
+ DBG ( " %s", uri_string );
|
|
64
|
+ fd = open ( uri_string );
|
|
65
|
+ }
|
|
66
|
+
|
|
67
|
+ if ( fd < 0 ) {
|
|
68
|
+ file_open->Status = PXENV_STATUS ( fd );
|
|
69
|
+ return PXENV_EXIT_FAILURE;
|
|
70
|
+ }
|
|
71
|
+
|
|
72
|
+ DBG ( " as file %d", fd );
|
|
73
|
+
|
|
74
|
+ file_open->FileHandle = fd;
|
|
75
|
+ file_open->Status = PXENV_STATUS_SUCCESS;
|
|
76
|
+ return PXENV_EXIT_SUCCESS;
|
|
77
|
+}
|
|
78
|
+
|
|
79
|
+/**
|
|
80
|
+ * FILE CLOSE
|
|
81
|
+ *
|
|
82
|
+ * @v file_close Pointer to a struct s_PXENV_FILE_CLOSE
|
|
83
|
+ * @v s_PXENV_FILE_CLOSE::FileHandle File handle
|
|
84
|
+ * @ret #PXENV_EXIT_SUCCESS File was closed
|
|
85
|
+ * @ret #PXENV_EXIT_FAILURE File was not closed
|
|
86
|
+ * @ret s_PXENV_FILE_CLOSE::Status PXE status code
|
|
87
|
+ *
|
|
88
|
+ */
|
|
89
|
+PXENV_EXIT_t pxenv_file_close ( struct s_PXENV_FILE_CLOSE *file_close ) {
|
|
90
|
+
|
|
91
|
+ DBG ( "PXENV_FILE_CLOSE %d", file_close->FileHandle );
|
|
92
|
+
|
|
93
|
+ close ( file_close->FileHandle );
|
|
94
|
+ file_close->Status = PXENV_STATUS_SUCCESS;
|
|
95
|
+ return PXENV_EXIT_SUCCESS;
|
|
96
|
+}
|
|
97
|
+
|
|
98
|
+/**
|
|
99
|
+ * FILE SELECT
|
|
100
|
+ *
|
|
101
|
+ * @v file_select Pointer to a struct s_PXENV_FILE_SELECT
|
|
102
|
+ * @v s_PXENV_FILE_SELECT::FileHandle File handle
|
|
103
|
+ * @ret #PXENV_EXIT_SUCCESS File has been checked for readiness
|
|
104
|
+ * @ret #PXENV_EXIT_FAILURE File has not been checked for readiness
|
|
105
|
+ * @ret s_PXENV_FILE_SELECT::Status PXE status code
|
|
106
|
+ * @ret s_PXENV_FILE_SELECT::Ready Indication of readiness
|
|
107
|
+ *
|
|
108
|
+ */
|
|
109
|
+PXENV_EXIT_t pxenv_file_select ( struct s_PXENV_FILE_SELECT *file_select ) {
|
|
110
|
+ fd_set fdset;
|
|
111
|
+ int ready;
|
|
112
|
+
|
|
113
|
+ DBG ( "PXENV_FILE_SELECT %d", file_select->FileHandle );
|
|
114
|
+
|
|
115
|
+ FD_ZERO ( &fdset );
|
|
116
|
+ FD_SET ( file_select->FileHandle, &fdset );
|
|
117
|
+ if ( ( ready = select ( &fdset, 0 ) ) < 0 ) {
|
|
118
|
+ file_select->Status = PXENV_STATUS ( ready );
|
|
119
|
+ return PXENV_EXIT_FAILURE;
|
|
120
|
+ }
|
|
121
|
+
|
|
122
|
+ file_select->Ready = ( ready ? RDY_READ : 0 );
|
|
123
|
+ file_select->Status = PXENV_STATUS_SUCCESS;
|
|
124
|
+ return PXENV_EXIT_SUCCESS;
|
|
125
|
+}
|
|
126
|
+
|
|
127
|
+/**
|
|
128
|
+ * FILE READ
|
|
129
|
+ *
|
|
130
|
+ * @v file_read Pointer to a struct s_PXENV_FILE_READ
|
|
131
|
+ * @v s_PXENV_FILE_READ::FileHandle File handle
|
|
132
|
+ * @v s_PXENV_FILE_READ::BufferSize Size of data buffer
|
|
133
|
+ * @v s_PXENV_FILE_READ::Buffer Data buffer
|
|
134
|
+ * @ret #PXENV_EXIT_SUCCESS Data has been read from file
|
|
135
|
+ * @ret #PXENV_EXIT_FAILURE Data has not been read from file
|
|
136
|
+ * @ret s_PXENV_FILE_READ::Status PXE status code
|
|
137
|
+ * @ret s_PXENV_FILE_READ::Ready Indication of readiness
|
|
138
|
+ * @ret s_PXENV_FILE_READ::BufferSize Length of data read
|
|
139
|
+ *
|
|
140
|
+ */
|
|
141
|
+PXENV_EXIT_t pxenv_file_read ( struct s_PXENV_FILE_READ *file_read ) {
|
|
142
|
+ userptr_t buffer;
|
|
143
|
+ ssize_t len;
|
|
144
|
+
|
|
145
|
+ DBG ( "PXENV_FILE_READ %d to %04x:%04x+%04x", file_read->FileHandle,
|
|
146
|
+ file_read->Buffer.segment, file_read->Buffer.offset,
|
|
147
|
+ file_read->BufferSize );
|
|
148
|
+
|
|
149
|
+ buffer = real_to_user ( file_read->Buffer.segment,
|
|
150
|
+ file_read->Buffer.offset );
|
|
151
|
+ if ( ( len = read_user ( file_read->FileHandle, buffer, 0,
|
|
152
|
+ file_read->BufferSize ) ) < 0 ) {
|
|
153
|
+ file_read->Status = PXENV_STATUS ( len );
|
|
154
|
+ return PXENV_EXIT_FAILURE;
|
|
155
|
+ }
|
|
156
|
+
|
|
157
|
+ DBG ( " read %04zx", ( ( size_t ) len ) );
|
|
158
|
+
|
|
159
|
+ file_read->BufferSize = len;
|
|
160
|
+ file_read->Status = PXENV_STATUS_SUCCESS;
|
|
161
|
+ return PXENV_EXIT_SUCCESS;
|
|
162
|
+}
|
|
163
|
+
|
|
164
|
+/**
|
|
165
|
+ * GET FILE SIZE
|
|
166
|
+ *
|
|
167
|
+ * @v get_file_size Pointer to a struct s_PXENV_GET_FILE_SIZE
|
|
168
|
+ * @v s_PXENV_GET_FILE_SIZE::FileHandle File handle
|
|
169
|
+ * @ret #PXENV_EXIT_SUCCESS File size has been determined
|
|
170
|
+ * @ret #PXENV_EXIT_FAILURE File size has not been determined
|
|
171
|
+ * @ret s_PXENV_GET_FILE_SIZE::Status PXE status code
|
|
172
|
+ * @ret s_PXENV_GET_FILE_SIZE::FileSize Size of file
|
|
173
|
+ */
|
|
174
|
+PXENV_EXIT_t pxenv_get_file_size ( struct s_PXENV_GET_FILE_SIZE
|
|
175
|
+ *get_file_size ) {
|
|
176
|
+ ssize_t filesize;
|
|
177
|
+
|
|
178
|
+ DBG ( "PXENV_GET_FILE_SIZE %d", get_file_size->FileHandle );
|
|
179
|
+
|
|
180
|
+ filesize = fsize ( get_file_size->FileHandle );
|
|
181
|
+ if ( filesize < 0 ) {
|
|
182
|
+ get_file_size->Status = PXENV_STATUS ( filesize );
|
|
183
|
+ return PXENV_EXIT_FAILURE;
|
|
184
|
+ }
|
|
185
|
+
|
|
186
|
+ DBG ( " is %zd", ( ( size_t ) filesize ) );
|
|
187
|
+
|
|
188
|
+ get_file_size->FileSize = filesize;
|
|
189
|
+ get_file_size->Status = PXENV_STATUS_SUCCESS;
|
|
190
|
+ return PXENV_EXIT_SUCCESS;
|
|
191
|
+}
|