Print this page
NEX-16819 loader UEFI support
Includes work by Toomas Soome <tsoome@me.com>
Upstream commits:
loader: pxe receive cleanup
9475 libefi: Do not return only if ReceiveFilter
installboot: should support efi system partition
8931 boot1.efi: scan all display modes rather than
loader: spinconsole updates
loader: gfx experiment to try GOP Blt() function.
sha1 build test
loader: add sha1 hash calculation
common/sha1: update for loader build
loader: biosdisk rework
uts: 32-bit kernel FB needs mapping in low memory
uts: add diag-device
uts: boot console mirror with diag-device
uts: enable very early console on ttya
kmdb: add diag-device as input/output device
uts: test VGA memory exclusion from mapping
uts: clear boot mapping and protect boot pages test
uts: add dboot map debug printf
uts: need to release FB pages in release_bootstrap()
uts: add screenmap ioctl
uts: update sys/queue.h
loader: add illumos uts/common to include path
loader: tem/gfx font cleanup
loader: vbe checks
uts: gfx_private set KD_TEXT when KD_RESETTEXT is
uts: gfx 8-bit update
loader: gfx 8-bit fix
loader: always set media size from partition.
uts: MB2 support for 32-bit kernel
loader: x86 should have tem 80x25
uts: x86 should have tem 80x25
uts: font update
loader: font update
uts: tem attributes
loader: tem.c comment added
uts: use font module
loader: add font module
loader: build rules for new font setup
uts: gfx_private update for new font structure
uts: early boot update for new font structure
uts: font update
uts: font build rules update for new fonts
uts: tem update to new font structure
loader: module.c needs to include tem_impl.h
uts: gfx_private 8x16 font rework
uts: make font_lookup public
loader: font rework
uts: font rework
9259 libefi: efi_alloc_and_read should check for PMBR
uts: tem utf-8 support
loader: implement tem utf-8 support
loader: tem should be able to display UTF-8
7784 uts: console input should support utf-8
7796 uts: ldterm default to utf-8
uts: do not reset serial console
uts: set up colors even if tem is not console
uts: add type for early boot properties
uts: gfx_private experiment with drm and vga
uts: gfx_private should use setmode drm callback.
uts: identify FB types and set up gfx_private based
loader: replace gop and vesa with framebuffer
uts: boot needs simple tem to support mdb
uts: boot_keyboard should emit esc sequences for
uts: gfx_private FB showuld be written by line
kmdb: set terminal window size
uts: gfx_private needs to keep track of early boot FB
pnglite: move pnglite to usr/src/common
loader: gfx_fb
ficl-sys: add gfx primitives
loader: add illumos.png logo
ficl: add fb-putimage
loader: add png support
loader: add alpha blending for gfx_fb
loader: use term-drawrect for menu frame
ficl: add simple gfx words
uts: provide fb_info via fbgattr dev_specific array.
uts: gfx_private add alpha blending
uts: update sys/ascii.h
uts: tem OSC support (incomplete)
uts: implement env module support and use data from
uts: tem get colors from early boot data
loader: use crc32 from libstand (libz)
loader: optimize for size
loader: pass tem info to the environment
loader: import tem for loader console
loader: UEFI loader needs to set ISADIR based on
loader: need UEFI32 support
8918 loader.efi: add vesa edid support
uts: tem_safe_pix_clear_prom_output() should only
uts: tem_safe_pix_clear_entire_screen() should use
uts: tem_safe_check_first_time() should query cursor
uts: tem implement cls callback & visual_io v4
uts: gfx_vgatext use block cursor for vgatext
uts: gfx_private implement cls callback & visual_io
uts: gfx_private bitmap framebuffer implementation
uts: early start frame buffer console support
uts: font functions should check the input char
uts: font rendering should support 16/24/32bit depths
uts: use smallest font as fallback default.
uts: update terminal dimensions based on selected
7834 uts: vgatext should use gfx_private
uts: add spacing property to 8859-1.bdf
terminfo: add underline for sun-color
terminfo: sun-color has 16 colors
uts: add font load callback type
loader: do not repeat int13 calls with error 0x20 and
8905 loader: add skein/edonr support
8904 common/crypto: make skein and edonr loader
Reviewed by: Yuri Pankov <yuri.pankov@nexenta.com>
Reviewed by: Sanjay Nadkarni <sanjay.nadkarni@nexenta.com>
Reviewed by: Evan Layton <evan.layton@nexenta.com>
Revert "NEX-16819 loader UEFI support"
This reverts commit ec06b9fc617b99234e538bf2e7e4d02a24993e0c.
Reverting due to failures in the zfs-tests and the sharefs-tests
NEX-16819 loader UEFI support
Includes work by Toomas Soome <tsoome@me.com>
Upstream commits:
loader: pxe receive cleanup
9475 libefi: Do not return only if ReceiveFilter
installboot: should support efi system partition
8931 boot1.efi: scan all display modes rather than
loader: spinconsole updates
loader: gfx experiment to try GOP Blt() function.
sha1 build test
loader: add sha1 hash calculation
common/sha1: update for loader build
loader: biosdisk rework
uts: 32-bit kernel FB needs mapping in low memory
uts: add diag-device
uts: boot console mirror with diag-device
uts: enable very early console on ttya
kmdb: add diag-device as input/output device
uts: test VGA memory exclusion from mapping
uts: clear boot mapping and protect boot pages test
uts: add dboot map debug printf
uts: need to release FB pages in release_bootstrap()
uts: add screenmap ioctl
uts: update sys/queue.h
loader: add illumos uts/common to include path
loader: tem/gfx font cleanup
loader: vbe checks
uts: gfx_private set KD_TEXT when KD_RESETTEXT is
uts: gfx 8-bit update
loader: gfx 8-bit fix
loader: always set media size from partition.
uts: MB2 support for 32-bit kernel
loader: x86 should have tem 80x25
uts: x86 should have tem 80x25
uts: font update
loader: font update
uts: tem attributes
loader: tem.c comment added
uts: use font module
loader: add font module
loader: build rules for new font setup
uts: gfx_private update for new font structure
uts: early boot update for new font structure
uts: font update
uts: font build rules update for new fonts
uts: tem update to new font structure
loader: module.c needs to include tem_impl.h
uts: gfx_private 8x16 font rework
uts: make font_lookup public
loader: font rework
uts: font rework
libefi: efi_alloc_and_read should check for PMBR
uts: tem utf-8 support
loader: implement tem utf-8 support
loader: tem should be able to display UTF-8
7784 uts: console input should support utf-8
7796 uts: ldterm default to utf-8
uts: do not reset serial console
uts: set up colors even if tem is not console
uts: add type for early boot properties
uts: gfx_private experiment with drm and vga
uts: gfx_private should use setmode drm callback.
uts: identify FB types and set up gfx_private based
loader: replace gop and vesa with framebuffer
uts: boot needs simple tem to support mdb
uts: boot_keyboard should emit esc sequences for
uts: gfx_private FB showuld be written by line
kmdb: set terminal window size
uts: gfx_private needs to keep track of early boot FB
pnglite: move pnglite to usr/src/common
loader: gfx_fb
ficl-sys: add gfx primitives
loader: add illumos.png logo
ficl: add fb-putimage
loader: add png support
loader: add alpha blending for gfx_fb
loader: use term-drawrect for menu frame
ficl: add simple gfx words
uts: provide fb_info via fbgattr dev_specific array.
uts: gfx_private add alpha blending
uts: update sys/ascii.h
uts: tem OSC support (incomplete)
uts: implement env module support and use data from
uts: tem get colors from early boot data
loader: use crc32 from libstand (libz)
loader: optimize for size
loader: pass tem info to the environment
loader: import tem for loader console
loader: UEFI loader needs to set ISADIR based on
loader: need UEFI32 support
8918 loader.efi: add vesa edid support
uts: tem_safe_pix_clear_prom_output() should only
uts: tem_safe_pix_clear_entire_screen() should use
uts: tem_safe_check_first_time() should query cursor
uts: tem implement cls callback & visual_io v4
uts: gfx_vgatext use block cursor for vgatext
uts: gfx_private implement cls callback & visual_io
uts: gfx_private bitmap framebuffer implementation
uts: early start frame buffer console support
uts: font functions should check the input char
uts: font rendering should support 16/24/32bit depths
uts: use smallest font as fallback default.
uts: update terminal dimensions based on selected
7834 uts: vgatext should use gfx_private
uts: add spacing property to 8859-1.bdf
terminfo: add underline for sun-color
terminfo: sun-color has 16 colors
uts: add font load callback type
loader: do not repeat int13 calls with error 0x20 and
8905 loader: add skein/edonr support
8904 common/crypto: make skein and edonr loader
Reviewed by: Yuri Pankov <yuri.pankov@nexenta.com>
Reviewed by: Sanjay Nadkarni <sanjay.nadkarni@nexenta.com>
Reviewed by: Evan Layton <evan.layton@nexenta.com>
| Split |
Close |
| Expand all |
| Collapse all |
--- old/usr/src/boot/lib/libstand/tftp.c
+++ new/usr/src/boot/lib/libstand/tftp.c
1 1 /* $NetBSD: tftp.c,v 1.4 1997/09/17 16:57:07 drochner Exp $ */
2 2
3 3 /*
4 4 * Copyright (c) 1996
5 5 * Matthias Drochner. All rights reserved.
6 6 *
7 7 * Redistribution and use in source and binary forms, with or without
8 8 * modification, are permitted provided that the following conditions
9 9 * are met:
10 10 * 1. Redistributions of source code must retain the above copyright
11 11 * notice, this list of conditions and the following disclaimer.
12 12 * 2. Redistributions in binary form must reproduce the above copyright
13 13 * notice, this list of conditions and the following disclaimer in the
14 14 * documentation and/or other materials provided with the distribution.
15 15 * 3. All advertising materials mentioning features or use of this software
16 16 * must display the following acknowledgement:
17 17 * This product includes software developed for the NetBSD Project
18 18 * by Matthias Drochner.
19 19 * 4. The name of the author may not be used to endorse or promote products
20 20 * derived from this software without specific prior written permission.
21 21 *
22 22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 32 */
33 33
34 34 #include <sys/cdefs.h>
35 35
36 36 /*
37 37 * Simple TFTP implementation for libsa.
38 38 * Assumes:
39 39 * - socket descriptor (int) at open_file->f_devdata
40 40 * - server host IP in global servip
41 41 * Restrictions:
42 42 * - read only
43 43 * - lseek only with SEEK_SET or SEEK_CUR
44 44 * - no big time differences between transfers (<tftp timeout)
45 45 */
46 46
47 47 #include <sys/types.h>
48 48 #include <sys/stat.h>
49 49 #include <netinet/in.h>
50 50 #include <netinet/udp.h>
51 51 #include <netinet/in_systm.h>
52 52 #include <arpa/tftp.h>
53 53
54 54 #include <string.h>
55 55
56 56 #include "stand.h"
57 57 #include "net.h"
|
↓ open down ↓ |
57 lines elided |
↑ open up ↑ |
58 58 #include "netif.h"
59 59
60 60 #include "tftp.h"
61 61
62 62 struct tftp_handle;
63 63
64 64 static int tftp_open(const char *path, struct open_file *f);
65 65 static int tftp_close(struct open_file *f);
66 66 static int tftp_parse_oack(struct tftp_handle *h, char *buf, size_t len);
67 67 static int tftp_read(struct open_file *f, void *buf, size_t size, size_t *resid);
68 -static int tftp_write(struct open_file *f, void *buf, size_t size, size_t *resid);
69 68 static off_t tftp_seek(struct open_file *f, off_t offset, int where);
70 69 static int tftp_set_blksize(struct tftp_handle *h, const char *str);
71 70 static int tftp_stat(struct open_file *f, struct stat *sb);
72 71 static ssize_t sendrecv_tftp(struct tftp_handle *h,
73 72 ssize_t (*sproc)(struct iodesc *, void *, size_t),
74 73 void *sbuf, size_t ssize,
75 74 ssize_t (*rproc)(struct tftp_handle *h, void **, void **, time_t, unsigned short *),
76 75 void **, void **, unsigned short *rtype);
77 76
78 77 struct fs_ops tftp_fsops = {
79 78 "tftp",
80 79 tftp_open,
81 80 tftp_close,
82 81 tftp_read,
83 - tftp_write,
82 + null_write,
84 83 tftp_seek,
85 84 tftp_stat,
86 85 null_readdir
87 86 };
88 87
89 88 extern struct in_addr servip;
90 89
91 90 static int tftpport = 2000;
92 91 static int is_open = 0;
93 92
94 93 /*
95 94 * The legacy TFTP_BLKSIZE value was SEGSIZE(512).
96 95 * TFTP_REQUESTED_BLKSIZE of 1428 is (Ethernet MTU, less the TFTP, UDP and
97 96 * IP header lengths).
98 97 */
99 98 #define TFTP_REQUESTED_BLKSIZE 1428
100 99
101 100 /*
102 101 * Choose a blksize big enough so we can test with Ethernet
103 102 * Jumbo frames in the future.
104 103 */
105 104 #define TFTP_MAX_BLKSIZE 9008
106 105
107 106 struct tftp_handle {
108 107 struct iodesc *iodesc;
109 108 int currblock; /* contents of lastdata */
110 109 int islastblock; /* flag */
111 110 int validsize;
112 111 int off;
113 112 char *path; /* saved for re-requests */
114 113 unsigned int tftp_blksize;
115 114 unsigned long tftp_tsize;
116 115 void *pkt;
117 116 struct tftphdr *tftp_hdr;
118 117 };
119 118
120 119 #define TFTP_MAX_ERRCODE EOPTNEG
121 120 static const int tftperrors[TFTP_MAX_ERRCODE + 1] = {
122 121 0, /* ??? */
123 122 ENOENT,
124 123 EPERM,
125 124 ENOSPC,
126 125 EINVAL, /* ??? */
127 126 EINVAL, /* ??? */
128 127 EEXIST,
129 128 EINVAL, /* ??? */
130 129 EINVAL, /* Option negotiation failed. */
131 130 };
132 131
133 132 static int tftp_getnextblock(struct tftp_handle *h);
134 133
135 134 /* send error message back. */
136 135 static void
137 136 tftp_senderr(struct tftp_handle *h, u_short errcode, const char *msg)
138 137 {
139 138 struct {
140 139 u_char header[HEADER_SIZE];
141 140 struct tftphdr t;
142 141 u_char space[63]; /* +1 from t */
143 142 } __packed __aligned(4) wbuf;
144 143 char *wtail;
145 144 int len;
146 145
147 146 len = strlen(msg);
148 147 if (len > sizeof(wbuf.space))
149 148 len = sizeof(wbuf.space);
150 149
151 150 wbuf.t.th_opcode = htons((u_short) ERROR);
152 151 wbuf.t.th_code = htons(errcode);
153 152
154 153 wtail = wbuf.t.th_msg;
155 154 bcopy(msg, wtail, len);
156 155 wtail[len] = '\0';
157 156 wtail += len + 1;
158 157
159 158 sendudp(h->iodesc, &wbuf.t, wtail - (char *) &wbuf.t);
160 159 }
161 160
162 161 static void
163 162 tftp_sendack(struct tftp_handle *h)
164 163 {
165 164 struct {
166 165 u_char header[HEADER_SIZE];
167 166 struct tftphdr t;
168 167 } __packed __aligned(4) wbuf;
169 168 char *wtail;
170 169
171 170 wbuf.t.th_opcode = htons((u_short) ACK);
172 171 wtail = (char *) &wbuf.t.th_block;
173 172 wbuf.t.th_block = htons((u_short) h->currblock);
174 173 wtail += 2;
175 174
176 175 sendudp(h->iodesc, &wbuf.t, wtail - (char *) &wbuf.t);
177 176 }
178 177
179 178 static ssize_t
180 179 recvtftp(struct tftp_handle *h, void **pkt, void **payload, time_t tleft,
181 180 unsigned short *rtype)
182 181 {
183 182 struct iodesc *d = h->iodesc;
184 183 struct tftphdr *t;
185 184 void *ptr = NULL;
186 185 ssize_t len;
187 186
188 187 errno = 0;
189 188
190 189 len = readudp(d, &ptr, (void **)&t, tleft);
191 190
192 191 if (len < 4) {
193 192 free(ptr);
194 193 return (-1);
195 194 }
196 195
197 196 *rtype = ntohs(t->th_opcode);
198 197 switch (ntohs(t->th_opcode)) {
199 198 case DATA: {
200 199 int got;
201 200
202 201 if (htons(t->th_block) != (u_short) d->xid) {
203 202 /*
204 203 * Expected block?
205 204 */
206 205 free(ptr);
207 206 return (-1);
208 207 }
209 208 if (d->xid == 1) {
210 209 /*
211 210 * First data packet from new port.
212 211 */
213 212 struct udphdr *uh;
214 213 uh = (struct udphdr *) t - 1;
215 214 d->destport = uh->uh_sport;
216 215 } /* else check uh_sport has not changed??? */
217 216 got = len - (t->th_data - (char *)t);
218 217 *pkt = ptr;
219 218 *payload = t;
220 219 return (got);
221 220 }
222 221 case ERROR:
223 222 if ((unsigned) ntohs(t->th_code) > TFTP_MAX_ERRCODE) {
224 223 printf("illegal tftp error %d\n", ntohs(t->th_code));
225 224 errno = EIO;
226 225 } else {
227 226 #ifdef TFTP_DEBUG
228 227 printf("tftp-error %d\n", ntohs(t->th_code));
229 228 #endif
230 229 errno = tftperrors[ntohs(t->th_code)];
231 230 }
232 231 free(ptr);
233 232 return (-1);
234 233 case OACK: {
235 234 struct udphdr *uh;
236 235 int tftp_oack_len;
237 236
238 237 /*
239 238 * Unexpected OACK. TFTP transfer already in progress.
240 239 * Drop the pkt.
241 240 */
242 241 if (d->xid != 1) {
243 242 free(ptr);
244 243 return (-1);
245 244 }
246 245
247 246 /*
248 247 * Remember which port this OACK came from, because we need
249 248 * to send the ACK or errors back to it.
250 249 */
251 250 uh = (struct udphdr *) t - 1;
252 251 d->destport = uh->uh_sport;
253 252
254 253 /* Parse options ACK-ed by the server. */
255 254 tftp_oack_len = len - sizeof(t->th_opcode);
256 255 if (tftp_parse_oack(h, t->th_u.tu_stuff, tftp_oack_len) != 0) {
257 256 tftp_senderr(h, EOPTNEG, "Malformed OACK");
258 257 errno = EIO;
259 258 free(ptr);
260 259 return (-1);
261 260 }
262 261 *pkt = ptr;
263 262 *payload = t;
264 263 return (0);
265 264 }
266 265 default:
267 266 #ifdef TFTP_DEBUG
268 267 printf("tftp type %d not handled\n", ntohs(t->th_opcode));
269 268 #endif
270 269 free(ptr);
271 270 return (-1);
272 271 }
273 272 }
274 273
275 274 /* send request, expect first block (or error) */
276 275 static int
277 276 tftp_makereq(struct tftp_handle *h)
278 277 {
279 278 struct {
280 279 u_char header[HEADER_SIZE];
281 280 struct tftphdr t;
282 281 u_char space[FNAME_SIZE + 6];
283 282 } __packed __aligned(4) wbuf;
284 283 char *wtail;
285 284 int l;
286 285 ssize_t res;
287 286 void *pkt;
288 287 struct tftphdr *t;
289 288 char *tftp_blksize = NULL;
290 289 int blksize_l;
291 290 unsigned short rtype = 0;
292 291
293 292 /*
294 293 * Allow overriding default TFTP block size by setting
295 294 * a tftp.blksize environment variable.
296 295 */
297 296 if ((tftp_blksize = getenv("tftp.blksize")) != NULL) {
298 297 tftp_set_blksize(h, tftp_blksize);
299 298 }
300 299
301 300 wbuf.t.th_opcode = htons((u_short) RRQ);
302 301 wtail = wbuf.t.th_stuff;
303 302 l = strlen(h->path);
304 303 #ifdef TFTP_PREPEND_PATH
305 304 if (l > FNAME_SIZE - (sizeof(TFTP_PREPEND_PATH) - 1))
306 305 return (ENAMETOOLONG);
307 306 bcopy(TFTP_PREPEND_PATH, wtail, sizeof(TFTP_PREPEND_PATH) - 1);
308 307 wtail += sizeof(TFTP_PREPEND_PATH) - 1;
309 308 #else
310 309 if (l > FNAME_SIZE)
311 310 return (ENAMETOOLONG);
312 311 #endif
313 312 bcopy(h->path, wtail, l + 1);
314 313 wtail += l + 1;
315 314 bcopy("octet", wtail, 6);
316 315 wtail += 6;
317 316 bcopy("blksize", wtail, 8);
318 317 wtail += 8;
319 318 blksize_l = sprintf(wtail, "%d", h->tftp_blksize);
320 319 wtail += blksize_l + 1;
321 320 bcopy("tsize", wtail, 6);
322 321 wtail += 6;
323 322 bcopy("0", wtail, 2);
324 323 wtail += 2;
325 324
326 325 /* h->iodesc->myport = htons(--tftpport); */
327 326 h->iodesc->myport = htons(tftpport + (getsecs() & 0x3ff));
328 327 h->iodesc->destport = htons(IPPORT_TFTP);
329 328 h->iodesc->xid = 1; /* expected block */
330 329
331 330 h->currblock = 0;
332 331 h->islastblock = 0;
333 332 h->validsize = 0;
334 333
335 334 pkt = NULL;
336 335 res = sendrecv_tftp(h, &sendudp, &wbuf.t, wtail - (char *) &wbuf.t,
337 336 &recvtftp, &pkt, (void **)&t, &rtype);
338 337 if (res == -1) {
339 338 free(pkt);
340 339 return (errno);
341 340 }
342 341
343 342 free(h->pkt);
344 343 h->pkt = pkt;
345 344 h->tftp_hdr = t;
346 345
347 346 if (rtype == OACK)
348 347 return (tftp_getnextblock(h));
349 348
350 349 /* Server ignored our blksize request, revert to TFTP default. */
351 350 h->tftp_blksize = SEGSIZE;
352 351
353 352 switch (rtype) {
354 353 case DATA: {
355 354 h->currblock = 1;
356 355 h->validsize = res;
357 356 h->islastblock = 0;
358 357 if (res < h->tftp_blksize) {
359 358 h->islastblock = 1; /* very short file */
360 359 tftp_sendack(h);
361 360 }
362 361 return (0);
363 362 }
364 363 case ERROR:
365 364 default:
366 365 return (errno);
367 366 }
368 367
369 368 }
370 369
371 370 /* ack block, expect next */
372 371 static int
373 372 tftp_getnextblock(struct tftp_handle *h)
374 373 {
375 374 struct {
376 375 u_char header[HEADER_SIZE];
377 376 struct tftphdr t;
378 377 } __packed __aligned(4) wbuf;
379 378 char *wtail;
380 379 int res;
381 380 void *pkt;
382 381 struct tftphdr *t;
383 382 unsigned short rtype = 0;
384 383 wbuf.t.th_opcode = htons((u_short) ACK);
385 384 wtail = (char *) &wbuf.t.th_block;
386 385 wbuf.t.th_block = htons((u_short) h->currblock);
387 386 wtail += 2;
388 387
389 388 h->iodesc->xid = h->currblock + 1; /* expected block */
390 389
391 390 pkt = NULL;
392 391 res = sendrecv_tftp(h, &sendudp, &wbuf.t, wtail - (char *) &wbuf.t,
393 392 &recvtftp, &pkt, (void **)&t, &rtype);
394 393
395 394 if (res == -1) { /* 0 is OK! */
396 395 free(pkt);
397 396 return (errno);
398 397 }
399 398
400 399 free(h->pkt);
401 400 h->pkt = pkt;
402 401 h->tftp_hdr = t;
403 402 h->currblock++;
404 403 h->validsize = res;
405 404 if (res < h->tftp_blksize)
406 405 h->islastblock = 1; /* EOF */
407 406
408 407 if (h->islastblock == 1) {
409 408 /* Send an ACK for the last block */
410 409 wbuf.t.th_block = htons((u_short) h->currblock);
411 410 sendudp(h->iodesc, &wbuf.t, wtail - (char *)&wbuf.t);
412 411 }
413 412
414 413 return (0);
415 414 }
416 415
417 416 static int
418 417 tftp_open(const char *path, struct open_file *f)
419 418 {
420 419 struct tftp_handle *tftpfile;
421 420 struct iodesc *io;
422 421 int res;
423 422 size_t pathsize;
424 423 const char *extraslash;
425 424
426 425 if (netproto != NET_TFTP)
427 426 return (EINVAL);
428 427
429 428 if (f->f_dev->dv_type != DEVT_NET)
430 429 return (EINVAL);
431 430
432 431 if (is_open)
433 432 return (EBUSY);
434 433
435 434 tftpfile = (struct tftp_handle *) malloc(sizeof(*tftpfile));
436 435 if (!tftpfile)
437 436 return (ENOMEM);
438 437
439 438 memset(tftpfile, 0, sizeof(*tftpfile));
440 439 tftpfile->tftp_blksize = TFTP_REQUESTED_BLKSIZE;
441 440 tftpfile->iodesc = io = socktodesc(*(int *) (f->f_devdata));
442 441 if (io == NULL)
443 442 return (EINVAL);
444 443
445 444 io->destip = servip;
446 445 tftpfile->off = 0;
447 446 pathsize = (strlen(rootpath) + 1 + strlen(path) + 1) * sizeof(char);
448 447 tftpfile->path = malloc(pathsize);
449 448 if (tftpfile->path == NULL) {
450 449 free(tftpfile);
451 450 return(ENOMEM);
452 451 }
453 452 if (rootpath[strlen(rootpath) - 1] == '/' || path[0] == '/')
454 453 extraslash = "";
455 454 else
456 455 extraslash = "/";
457 456 res = snprintf(tftpfile->path, pathsize, "%s%s%s",
458 457 rootpath, extraslash, path);
459 458 if (res < 0 || res > pathsize) {
460 459 free(tftpfile->path);
461 460 free(tftpfile);
462 461 return(ENOMEM);
463 462 }
464 463
465 464 res = tftp_makereq(tftpfile);
466 465
467 466 if (res) {
468 467 free(tftpfile->path);
469 468 free(tftpfile->pkt);
470 469 free(tftpfile);
471 470 return (res);
472 471 }
473 472 f->f_fsdata = (void *) tftpfile;
474 473 is_open = 1;
475 474 return (0);
476 475 }
477 476
478 477 static int
479 478 tftp_read(struct open_file *f, void *addr, size_t size,
480 479 size_t *resid /* out */)
481 480 {
482 481 struct tftp_handle *tftpfile;
483 482 tftpfile = (struct tftp_handle *) f->f_fsdata;
484 483
485 484 while (size > 0) {
486 485 int needblock, count;
487 486
488 487 twiddle(32);
489 488
490 489 needblock = tftpfile->off / tftpfile->tftp_blksize + 1;
491 490
492 491 if (tftpfile->currblock > needblock) { /* seek backwards */
493 492 tftp_senderr(tftpfile, 0, "No error: read aborted");
494 493 tftp_makereq(tftpfile); /* no error check, it worked
495 494 * for open */
496 495 }
497 496
498 497 while (tftpfile->currblock < needblock) {
499 498 int res;
500 499
501 500 res = tftp_getnextblock(tftpfile);
502 501 if (res) { /* no answer */
503 502 #ifdef TFTP_DEBUG
504 503 printf("tftp: read error\n");
505 504 #endif
506 505 return (res);
507 506 }
508 507 if (tftpfile->islastblock)
509 508 break;
510 509 }
511 510
512 511 if (tftpfile->currblock == needblock) {
513 512 int offinblock, inbuffer;
514 513
515 514 offinblock = tftpfile->off % tftpfile->tftp_blksize;
516 515
517 516 inbuffer = tftpfile->validsize - offinblock;
518 517 if (inbuffer < 0) {
519 518 #ifdef TFTP_DEBUG
520 519 printf("tftp: invalid offset %d\n",
521 520 tftpfile->off);
522 521 #endif
523 522 return (EINVAL);
524 523 }
525 524 count = (size < inbuffer ? size : inbuffer);
526 525 bcopy(tftpfile->tftp_hdr->th_data + offinblock,
527 526 addr, count);
528 527
529 528 addr = (char *)addr + count;
530 529 tftpfile->off += count;
531 530 size -= count;
532 531
533 532 if ((tftpfile->islastblock) && (count == inbuffer))
534 533 break; /* EOF */
535 534 } else {
536 535 #ifdef TFTP_DEBUG
537 536 printf("tftp: block %d not found\n", needblock);
538 537 #endif
539 538 return (EINVAL);
540 539 }
541 540
542 541 }
543 542
544 543 if (resid)
545 544 *resid = size;
546 545 return (0);
547 546 }
548 547
549 548 static int
550 549 tftp_close(struct open_file *f)
551 550 {
552 551 struct tftp_handle *tftpfile;
553 552 tftpfile = (struct tftp_handle *) f->f_fsdata;
554 553
555 554 /* let it time out ... */
|
↓ open down ↓ |
462 lines elided |
↑ open up ↑ |
556 555
557 556 if (tftpfile) {
558 557 free(tftpfile->path);
559 558 free(tftpfile->pkt);
560 559 free(tftpfile);
561 560 }
562 561 is_open = 0;
563 562 return (0);
564 563 }
565 564
566 -static int
567 -tftp_write(struct open_file *f __unused, void *start __unused, size_t size __unused,
568 - size_t *resid __unused /* out */)
569 -{
570 - return (EROFS);
571 -}
572 -
573 565 static int
574 566 tftp_stat(struct open_file *f, struct stat *sb)
575 567 {
576 568 struct tftp_handle *tftpfile;
577 569 tftpfile = (struct tftp_handle *) f->f_fsdata;
578 570
579 571 sb->st_mode = 0444 | S_IFREG;
580 572 sb->st_nlink = 1;
581 573 sb->st_uid = 0;
582 574 sb->st_gid = 0;
583 575 sb->st_size = (off_t) tftpfile->tftp_tsize;
584 576 return (0);
585 577 }
586 578
587 579 static off_t
588 580 tftp_seek(struct open_file *f, off_t offset, int where)
589 581 {
590 582 struct tftp_handle *tftpfile;
591 583 tftpfile = (struct tftp_handle *) f->f_fsdata;
592 584
593 585 switch (where) {
594 586 case SEEK_SET:
595 587 tftpfile->off = offset;
596 588 break;
597 589 case SEEK_CUR:
598 590 tftpfile->off += offset;
599 591 break;
600 592 default:
601 593 errno = EOFFSET;
602 594 return (-1);
603 595 }
604 596 return (tftpfile->off);
605 597 }
606 598
607 599 static ssize_t
608 600 sendrecv_tftp(struct tftp_handle *h,
609 601 ssize_t (*sproc)(struct iodesc *, void *, size_t),
610 602 void *sbuf, size_t ssize,
611 603 ssize_t (*rproc)(struct tftp_handle *, void **, void **, time_t,
612 604 unsigned short *),
613 605 void **pkt, void **payload, unsigned short *rtype)
614 606 {
615 607 struct iodesc *d = h->iodesc;
616 608 ssize_t cc;
617 609 time_t t, t1, tleft;
618 610
619 611 #ifdef TFTP_DEBUG
620 612 if (debug)
621 613 printf("sendrecv: called\n");
622 614 #endif
623 615
624 616 tleft = MINTMO;
625 617 t = t1 = getsecs();
626 618 for (;;) {
627 619 if ((getsecs() - t) > MAXTMO) {
628 620 errno = ETIMEDOUT;
629 621 return -1;
630 622 }
631 623
632 624 cc = (*sproc)(d, sbuf, ssize);
633 625 if (cc != -1 && cc < ssize)
634 626 panic("sendrecv: short write! (%zd < %zu)",
635 627 cc, ssize);
636 628
637 629 if (cc == -1) {
638 630 /* Error on transmit; wait before retrying */
639 631 while ((getsecs() - t1) < tleft);
640 632 t1 = getsecs();
641 633 continue;
642 634 }
643 635
644 636 t = t1 = getsecs();
645 637 recvnext:
646 638 if ((getsecs() - t) > MAXTMO) {
647 639 errno = ETIMEDOUT;
648 640 return (-1);
649 641 }
650 642 /* Try to get a packet and process it. */
651 643 cc = (*rproc)(h, pkt, payload, tleft, rtype);
652 644 /* Return on data, EOF or real error. */
653 645 if (cc != -1 || (errno != 0 && errno != ETIMEDOUT))
654 646 return (cc);
655 647 if ((getsecs() - t1) < tleft) {
656 648 goto recvnext;
657 649 }
658 650
659 651 /* Timed out or didn't get the packet we're waiting for */
660 652 tleft += MINTMO;
661 653 if (tleft > (2 * MINTMO)) {
662 654 tleft = (2 * MINTMO);
663 655 }
664 656 t1 = getsecs();
665 657 }
666 658 }
667 659
668 660 static int
669 661 tftp_set_blksize(struct tftp_handle *h, const char *str)
670 662 {
671 663 char *endptr;
672 664 int new_blksize;
673 665 int ret = 0;
674 666
675 667 if (h == NULL || str == NULL)
676 668 return (ret);
677 669
678 670 new_blksize =
679 671 (unsigned int)strtol(str, &endptr, 0);
680 672
681 673 /*
682 674 * Only accept blksize value if it is numeric.
683 675 * RFC2348 specifies that acceptable values are 8-65464.
684 676 * Let's choose a limit less than MAXRSPACE.
685 677 */
686 678 if (*endptr == '\0' && new_blksize >= 8
687 679 && new_blksize <= TFTP_MAX_BLKSIZE) {
688 680 h->tftp_blksize = new_blksize;
689 681 ret = 1;
690 682 }
691 683
692 684 return (ret);
693 685 }
694 686
695 687 /*
696 688 * In RFC2347, the TFTP Option Acknowledgement package (OACK)
697 689 * is used to acknowledge a client's option negotiation request.
698 690 * The format of an OACK packet is:
699 691 * +-------+---~~---+---+---~~---+---+---~~---+---+---~~---+---+
700 692 * | opc | opt1 | 0 | value1 | 0 | optN | 0 | valueN | 0 |
701 693 * +-------+---~~---+---+---~~---+---+---~~---+---+---~~---+---+
702 694 *
703 695 * opc
704 696 * The opcode field contains a 6, for Option Acknowledgment.
705 697 *
706 698 * opt1
707 699 * The first option acknowledgment, copied from the original
708 700 * request.
709 701 *
710 702 * value1
711 703 * The acknowledged value associated with the first option. If
712 704 * and how this value may differ from the original request is
713 705 * detailed in the specification for the option.
714 706 *
715 707 * optN, valueN
716 708 * The final option/value acknowledgment pair.
717 709 */
718 710 static int
719 711 tftp_parse_oack(struct tftp_handle *h, char *buf, size_t len)
720 712 {
721 713 /*
722 714 * We parse the OACK strings into an array
723 715 * of name-value pairs.
724 716 */
725 717 char *tftp_options[128] = { 0 };
726 718 char *val = buf;
727 719 int i = 0;
728 720 int option_idx = 0;
729 721 int blksize_is_set = 0;
730 722 int tsize = 0;
731 723
732 724 unsigned int orig_blksize;
733 725
734 726 while (option_idx < 128 && i < len) {
735 727 if (buf[i] == '\0') {
736 728 if (&buf[i] > val) {
737 729 tftp_options[option_idx] = val;
738 730 val = &buf[i] + 1;
739 731 ++option_idx;
740 732 }
741 733 }
742 734 ++i;
743 735 }
744 736
745 737 /* Save the block size we requested for sanity check later. */
746 738 orig_blksize = h->tftp_blksize;
747 739
748 740 /*
749 741 * Parse individual TFTP options.
750 742 * * "blksize" is specified in RFC2348.
751 743 * * "tsize" is specified in RFC2349.
752 744 */
753 745 for (i = 0; i < option_idx; i += 2) {
754 746 if (strcasecmp(tftp_options[i], "blksize") == 0) {
755 747 if (i + 1 < option_idx)
756 748 blksize_is_set =
757 749 tftp_set_blksize(h, tftp_options[i + 1]);
758 750 } else if (strcasecmp(tftp_options[i], "tsize") == 0) {
759 751 if (i + 1 < option_idx)
760 752 tsize = strtol(tftp_options[i + 1], (char **)NULL, 10);
761 753 if (tsize != 0)
762 754 h->tftp_tsize = tsize;
763 755 } else {
764 756 /* Do not allow any options we did not expect to be ACKed. */
765 757 printf("unexpected tftp option '%s'\n", tftp_options[i]);
766 758 return (-1);
767 759 }
768 760 }
769 761
770 762 if (!blksize_is_set) {
771 763 /*
772 764 * If TFTP blksize was not set, try defaulting
773 765 * to the legacy TFTP blksize of SEGSIZE(512)
774 766 */
775 767 h->tftp_blksize = SEGSIZE;
776 768 } else if (h->tftp_blksize > orig_blksize) {
777 769 /*
778 770 * Server should not be proposing block sizes that
779 771 * exceed what we said we can handle.
780 772 */
781 773 printf("unexpected blksize %u\n", h->tftp_blksize);
782 774 return (-1);
783 775 }
784 776
785 777 #ifdef TFTP_DEBUG
786 778 printf("tftp_blksize: %u\n", h->tftp_blksize);
787 779 printf("tftp_tsize: %lu\n", h->tftp_tsize);
788 780 #endif
789 781 return 0;
790 782 }
|
↓ open down ↓ |
208 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX