17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * GRUB -- GRand Unified Bootloader
37c478bd9Sstevel@tonic-gate * Copyright (C) 2000,2001,2002,2004 Free Software Foundation, Inc.
47c478bd9Sstevel@tonic-gate *
57c478bd9Sstevel@tonic-gate * This program is free software; you can redistribute it and/or modify
67c478bd9Sstevel@tonic-gate * it under the terms of the GNU General Public License as published by
77c478bd9Sstevel@tonic-gate * the Free Software Foundation; either version 2 of the License, or
87c478bd9Sstevel@tonic-gate * (at your option) any later version.
97c478bd9Sstevel@tonic-gate *
107c478bd9Sstevel@tonic-gate * This program is distributed in the hope that it will be useful,
117c478bd9Sstevel@tonic-gate * but WITHOUT ANY WARRANTY; without even the implied warranty of
127c478bd9Sstevel@tonic-gate * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
137c478bd9Sstevel@tonic-gate * GNU General Public License for more details.
147c478bd9Sstevel@tonic-gate *
157c478bd9Sstevel@tonic-gate * You should have received a copy of the GNU General Public License
167c478bd9Sstevel@tonic-gate * along with this program; if not, write to the Free Software
177c478bd9Sstevel@tonic-gate * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
187c478bd9Sstevel@tonic-gate */
197c478bd9Sstevel@tonic-gate
207c478bd9Sstevel@tonic-gate /* Based on "src/main.c" in etherboot-4.5.8. */
217c478bd9Sstevel@tonic-gate /**************************************************************************
227c478bd9Sstevel@tonic-gate ETHERBOOT - BOOTP/TFTP Bootstrap Program
237c478bd9Sstevel@tonic-gate
247c478bd9Sstevel@tonic-gate Author: Martin Renters
257c478bd9Sstevel@tonic-gate Date: Dec/93
267c478bd9Sstevel@tonic-gate
277c478bd9Sstevel@tonic-gate **************************************************************************/
287c478bd9Sstevel@tonic-gate
297c478bd9Sstevel@tonic-gate /* #define TFTP_DEBUG 1 */
307c478bd9Sstevel@tonic-gate
317c478bd9Sstevel@tonic-gate #include <filesys.h>
327c478bd9Sstevel@tonic-gate #include <shared.h>
337c478bd9Sstevel@tonic-gate
347c478bd9Sstevel@tonic-gate #include "grub.h"
357c478bd9Sstevel@tonic-gate #include "tftp.h"
367c478bd9Sstevel@tonic-gate #include "nic.h"
377c478bd9Sstevel@tonic-gate
3899ed6083Sszhou static int tftp_file_read_undi(const char *name,
3999ed6083Sszhou int (*fnc)(unsigned char *, unsigned int, unsigned int, int));
4099ed6083Sszhou static int tftp_read_undi(char *addr, int size);
4199ed6083Sszhou static int tftp_dir_undi(char *dirname);
4299ed6083Sszhou static void tftp_close_undi(void);
4399ed6083Sszhou static int buf_fill_undi(int abort);
4499ed6083Sszhou
4599ed6083Sszhou extern int use_bios_pxe;
4699ed6083Sszhou
477c478bd9Sstevel@tonic-gate static int retry;
487c478bd9Sstevel@tonic-gate static unsigned short iport = 2000;
497c478bd9Sstevel@tonic-gate static unsigned short oport = 0;
507c478bd9Sstevel@tonic-gate static unsigned short block, prevblock;
517c478bd9Sstevel@tonic-gate static int bcounter;
527c478bd9Sstevel@tonic-gate static struct tftp_t tp, saved_tp;
537c478bd9Sstevel@tonic-gate static int packetsize;
547c478bd9Sstevel@tonic-gate static int buf_eof, buf_read;
557c478bd9Sstevel@tonic-gate static int saved_filepos;
567c478bd9Sstevel@tonic-gate static unsigned short len, saved_len;
5799ed6083Sszhou static char *buf, *saved_name;
587c478bd9Sstevel@tonic-gate
597c478bd9Sstevel@tonic-gate /**
607c478bd9Sstevel@tonic-gate * tftp_read
617c478bd9Sstevel@tonic-gate *
627c478bd9Sstevel@tonic-gate * Read file with _name_, data handled by _fnc_. In fact, grub never
637c478bd9Sstevel@tonic-gate * use it, we just use it to read dhcp config file.
647c478bd9Sstevel@tonic-gate */
await_tftp(int ival,void * ptr __unused,unsigned short ptype __unused,struct iphdr * ip,struct udphdr * udp)657c478bd9Sstevel@tonic-gate static int await_tftp(int ival, void *ptr __unused,
667c478bd9Sstevel@tonic-gate unsigned short ptype __unused, struct iphdr *ip,
677c478bd9Sstevel@tonic-gate struct udphdr *udp)
687c478bd9Sstevel@tonic-gate {
697c478bd9Sstevel@tonic-gate static int tftp_count = 0;
707c478bd9Sstevel@tonic-gate
717c478bd9Sstevel@tonic-gate if (!udp) {
727c478bd9Sstevel@tonic-gate return 0;
737c478bd9Sstevel@tonic-gate }
747c478bd9Sstevel@tonic-gate if (arptable[ARP_CLIENT].ipaddr.s_addr != ip->dest.s_addr)
757c478bd9Sstevel@tonic-gate return 0;
767c478bd9Sstevel@tonic-gate if (ntohs(udp->dest) != ival)
777c478bd9Sstevel@tonic-gate return 0;
787c478bd9Sstevel@tonic-gate tftp_count++; /* show progress */
797c478bd9Sstevel@tonic-gate if ((tftp_count % 1000) == 0)
807c478bd9Sstevel@tonic-gate printf(".");
817c478bd9Sstevel@tonic-gate return 1;
827c478bd9Sstevel@tonic-gate }
837c478bd9Sstevel@tonic-gate
tftp_file_read(const char * name,int (* fnc)(unsigned char *,unsigned int,unsigned int,int))847c478bd9Sstevel@tonic-gate int tftp_file_read(const char *name, int (*fnc)(unsigned char *, unsigned int, unsigned int, int))
857c478bd9Sstevel@tonic-gate {
867c478bd9Sstevel@tonic-gate struct tftpreq_t tp;
877c478bd9Sstevel@tonic-gate struct tftp_t *tr;
887c478bd9Sstevel@tonic-gate int rc;
897c478bd9Sstevel@tonic-gate
9099ed6083Sszhou if (use_bios_pxe)
9199ed6083Sszhou return (tftp_file_read_undi(name, fnc));
9299ed6083Sszhou
937c478bd9Sstevel@tonic-gate retry = 0;
947c478bd9Sstevel@tonic-gate block = 0;
957c478bd9Sstevel@tonic-gate prevblock = 0;
967c478bd9Sstevel@tonic-gate bcounter = 0;
977c478bd9Sstevel@tonic-gate
987c478bd9Sstevel@tonic-gate
997c478bd9Sstevel@tonic-gate rx_qdrain();
1007c478bd9Sstevel@tonic-gate
1017c478bd9Sstevel@tonic-gate tp.opcode = htons(TFTP_RRQ);
1027c478bd9Sstevel@tonic-gate /* Warning: the following assumes the layout of bootp_t.
1037c478bd9Sstevel@tonic-gate But that's fixed by the IP, UDP and BOOTP specs. */
1047c478bd9Sstevel@tonic-gate len = sizeof(tp.ip) + sizeof(tp.udp) + sizeof(tp.opcode) +
1057c478bd9Sstevel@tonic-gate sprintf((char *)tp.u.rrq, "%s%coctet%cblksize%c%d",
1067c478bd9Sstevel@tonic-gate name, 0, 0, 0, TFTP_MAX_PACKET) + 1;
1077c478bd9Sstevel@tonic-gate if (!udp_transmit(arptable[ARP_SERVER].ipaddr.s_addr, ++iport,
1087c478bd9Sstevel@tonic-gate TFTP_PORT, len, &tp))
1097c478bd9Sstevel@tonic-gate return (0);
1107c478bd9Sstevel@tonic-gate for (;;)
1117c478bd9Sstevel@tonic-gate {
1127c478bd9Sstevel@tonic-gate long timeout;
1137c478bd9Sstevel@tonic-gate #ifdef CONGESTED
1147c478bd9Sstevel@tonic-gate timeout = rfc2131_sleep_interval(block?TFTP_REXMT: TIMEOUT, retry);
1157c478bd9Sstevel@tonic-gate #else
1167c478bd9Sstevel@tonic-gate timeout = rfc2131_sleep_interval(TIMEOUT, retry);
1177c478bd9Sstevel@tonic-gate #endif
1187c478bd9Sstevel@tonic-gate if (!await_reply(await_tftp, iport, NULL, timeout))
1197c478bd9Sstevel@tonic-gate {
1207c478bd9Sstevel@tonic-gate if (!block && retry++ < MAX_TFTP_RETRIES)
1217c478bd9Sstevel@tonic-gate { /* maybe initial request was lost */
1227c478bd9Sstevel@tonic-gate if (!udp_transmit(arptable[ARP_SERVER].ipaddr.s_addr,
1237c478bd9Sstevel@tonic-gate ++iport, TFTP_PORT, len, &tp))
1247c478bd9Sstevel@tonic-gate return (0);
1257c478bd9Sstevel@tonic-gate continue;
1267c478bd9Sstevel@tonic-gate }
1277c478bd9Sstevel@tonic-gate #ifdef CONGESTED
1287c478bd9Sstevel@tonic-gate if (block && ((retry += TFTP_REXMT) < TFTP_TIMEOUT))
1297c478bd9Sstevel@tonic-gate { /* we resend our last ack */
1307c478bd9Sstevel@tonic-gate #ifdef MDEBUG
1317c478bd9Sstevel@tonic-gate printf("<REXMT>\n");
1327c478bd9Sstevel@tonic-gate #endif
1337c478bd9Sstevel@tonic-gate udp_transmit(arptable[ARP_SERVER].ipaddr.s_addr,
1347c478bd9Sstevel@tonic-gate iport, oport,
1357c478bd9Sstevel@tonic-gate TFTP_MIN_PACKET, &tp);
1367c478bd9Sstevel@tonic-gate continue;
1377c478bd9Sstevel@tonic-gate }
1387c478bd9Sstevel@tonic-gate #endif
1397c478bd9Sstevel@tonic-gate break; /* timeout */
1407c478bd9Sstevel@tonic-gate }
1417c478bd9Sstevel@tonic-gate tr = (struct tftp_t *)&nic.packet[ETH_HLEN];
1427c478bd9Sstevel@tonic-gate if (tr->opcode == ntohs(TFTP_ERROR))
1437c478bd9Sstevel@tonic-gate {
1447c478bd9Sstevel@tonic-gate printf("TFTP error %d (%s)\n",
1457c478bd9Sstevel@tonic-gate ntohs(tr->u.err.errcode),
1467c478bd9Sstevel@tonic-gate tr->u.err.errmsg);
1477c478bd9Sstevel@tonic-gate break;
1487c478bd9Sstevel@tonic-gate }
1497c478bd9Sstevel@tonic-gate
1507c478bd9Sstevel@tonic-gate if (tr->opcode == ntohs(TFTP_OACK)) {
1517c478bd9Sstevel@tonic-gate char *p = tr->u.oack.data, *e;
1527c478bd9Sstevel@tonic-gate
1537c478bd9Sstevel@tonic-gate if (prevblock) /* shouldn't happen */
1547c478bd9Sstevel@tonic-gate continue; /* ignore it */
1557c478bd9Sstevel@tonic-gate len = ntohs(tr->udp.len) - sizeof(struct udphdr) - 2;
1567c478bd9Sstevel@tonic-gate if (len > TFTP_MAX_PACKET)
1577c478bd9Sstevel@tonic-gate goto noak;
1587c478bd9Sstevel@tonic-gate e = p + len;
1597c478bd9Sstevel@tonic-gate while (*p != '\0' && p < e) {
1607c478bd9Sstevel@tonic-gate /* if (!strcasecmp("blksize", p)) { */
1617c478bd9Sstevel@tonic-gate if (!grub_strcmp("blksize", p)) {
1627c478bd9Sstevel@tonic-gate p += 8;
1637c478bd9Sstevel@tonic-gate /* if ((packetsize = strtoul(p, &p, 10)) < */
1647c478bd9Sstevel@tonic-gate if ((packetsize = getdec(&p)) < TFTP_DEFAULTSIZE_PACKET)
1657c478bd9Sstevel@tonic-gate goto noak;
1667c478bd9Sstevel@tonic-gate while (p < e && *p) p++;
1677c478bd9Sstevel@tonic-gate if (p < e)
1687c478bd9Sstevel@tonic-gate p++;
1697c478bd9Sstevel@tonic-gate }
1707c478bd9Sstevel@tonic-gate else {
1717c478bd9Sstevel@tonic-gate noak:
1727c478bd9Sstevel@tonic-gate tp.opcode = htons(TFTP_ERROR);
1737c478bd9Sstevel@tonic-gate tp.u.err.errcode = 8;
1747c478bd9Sstevel@tonic-gate /*
1757c478bd9Sstevel@tonic-gate * Warning: the following assumes the layout of bootp_t.
1767c478bd9Sstevel@tonic-gate * But that's fixed by the IP, UDP and BOOTP specs.
1777c478bd9Sstevel@tonic-gate */
1787c478bd9Sstevel@tonic-gate len = sizeof(tp.ip) + sizeof(tp.udp) + sizeof(tp.opcode) + sizeof(tp.u.err.errcode) +
1797c478bd9Sstevel@tonic-gate /*
1807c478bd9Sstevel@tonic-gate * Normally bad form to omit the format string, but in this case
1817c478bd9Sstevel@tonic-gate * the string we are copying from is fixed. sprintf is just being
1827c478bd9Sstevel@tonic-gate * used as a strcpy and strlen.
1837c478bd9Sstevel@tonic-gate */
1847c478bd9Sstevel@tonic-gate sprintf((char *)tp.u.err.errmsg,
1857c478bd9Sstevel@tonic-gate "RFC1782 error") + 1;
1867c478bd9Sstevel@tonic-gate udp_transmit(arptable[ARP_SERVER].ipaddr.s_addr,
1877c478bd9Sstevel@tonic-gate iport, ntohs(tr->udp.src),
1887c478bd9Sstevel@tonic-gate len, &tp);
1897c478bd9Sstevel@tonic-gate return (0);
1907c478bd9Sstevel@tonic-gate }
1917c478bd9Sstevel@tonic-gate }
1927c478bd9Sstevel@tonic-gate if (p > e)
1937c478bd9Sstevel@tonic-gate goto noak;
1947c478bd9Sstevel@tonic-gate block = tp.u.ack.block = 0; /* this ensures, that */
1957c478bd9Sstevel@tonic-gate /* the packet does not get */
1967c478bd9Sstevel@tonic-gate /* processed as data! */
1977c478bd9Sstevel@tonic-gate }
1987c478bd9Sstevel@tonic-gate else if (tr->opcode == htons(TFTP_DATA)) {
1997c478bd9Sstevel@tonic-gate len = ntohs(tr->udp.len) - sizeof(struct udphdr) - 4;
2007c478bd9Sstevel@tonic-gate if (len > packetsize) /* shouldn't happen */
2017c478bd9Sstevel@tonic-gate continue; /* ignore it */
2027c478bd9Sstevel@tonic-gate block = ntohs(tp.u.ack.block = tr->u.data.block); }
2037c478bd9Sstevel@tonic-gate else {/* neither TFTP_OACK nor TFTP_DATA */
2047c478bd9Sstevel@tonic-gate break;
2057c478bd9Sstevel@tonic-gate }
2067c478bd9Sstevel@tonic-gate
2077c478bd9Sstevel@tonic-gate if ((block || bcounter) && (block != (unsigned short)(prevblock+1))) {
2087c478bd9Sstevel@tonic-gate /* Block order should be continuous */
2097c478bd9Sstevel@tonic-gate tp.u.ack.block = htons(block = prevblock);
2107c478bd9Sstevel@tonic-gate }
2117c478bd9Sstevel@tonic-gate tp.opcode = htons(TFTP_ACK);
2127c478bd9Sstevel@tonic-gate oport = ntohs(tr->udp.src);
2137c478bd9Sstevel@tonic-gate udp_transmit(arptable[ARP_SERVER].ipaddr.s_addr, iport,
2147c478bd9Sstevel@tonic-gate oport, TFTP_MIN_PACKET, &tp); /* ack */
2157c478bd9Sstevel@tonic-gate if ((unsigned short)(block-prevblock) != 1) {
2167c478bd9Sstevel@tonic-gate /* Retransmission or OACK, don't process via callback
2177c478bd9Sstevel@tonic-gate * and don't change the value of prevblock. */
2187c478bd9Sstevel@tonic-gate continue;
2197c478bd9Sstevel@tonic-gate }
2207c478bd9Sstevel@tonic-gate prevblock = block;
2217c478bd9Sstevel@tonic-gate retry = 0; /* It's the right place to zero the timer? */
2227c478bd9Sstevel@tonic-gate if ((rc = fnc(tr->u.data.download,
2237c478bd9Sstevel@tonic-gate ++bcounter, len, len < packetsize)) <= 0)
2247c478bd9Sstevel@tonic-gate return(rc);
2257c478bd9Sstevel@tonic-gate if (len < packetsize) { /* End of data --- fnc should not have returned */
2267c478bd9Sstevel@tonic-gate printf("tftp download complete, but\n");
2277c478bd9Sstevel@tonic-gate return (1);
2287c478bd9Sstevel@tonic-gate }
2297c478bd9Sstevel@tonic-gate }
2307c478bd9Sstevel@tonic-gate return (0);
2317c478bd9Sstevel@tonic-gate }
2327c478bd9Sstevel@tonic-gate
2337c478bd9Sstevel@tonic-gate /* Fill the buffer by receiving the data via the TFTP protocol. */
2347c478bd9Sstevel@tonic-gate static int
buf_fill(int abort)2357c478bd9Sstevel@tonic-gate buf_fill (int abort)
2367c478bd9Sstevel@tonic-gate {
2377c478bd9Sstevel@tonic-gate #ifdef TFTP_DEBUG
2387c478bd9Sstevel@tonic-gate grub_printf ("buf_fill (%d)\n", abort);
2397c478bd9Sstevel@tonic-gate #endif
2407c478bd9Sstevel@tonic-gate
24199ed6083Sszhou if (use_bios_pxe)
24299ed6083Sszhou return (buf_fill_undi(abort));
24399ed6083Sszhou
2447c478bd9Sstevel@tonic-gate while (! buf_eof && (buf_read + packetsize <= FSYS_BUFLEN))
2457c478bd9Sstevel@tonic-gate {
2467c478bd9Sstevel@tonic-gate struct tftp_t *tr;
2477c478bd9Sstevel@tonic-gate long timeout;
2487c478bd9Sstevel@tonic-gate
2497c478bd9Sstevel@tonic-gate #ifdef CONGESTED
2507c478bd9Sstevel@tonic-gate timeout = rfc2131_sleep_interval (block ? TFTP_REXMT : TIMEOUT, retry);
2517c478bd9Sstevel@tonic-gate #else
2527c478bd9Sstevel@tonic-gate timeout = rfc2131_sleep_interval (TIMEOUT, retry);
2537c478bd9Sstevel@tonic-gate #endif
2547c478bd9Sstevel@tonic-gate
2557c478bd9Sstevel@tonic-gate if (! await_reply (await_tftp, iport, NULL, timeout))
2567c478bd9Sstevel@tonic-gate {
2577c478bd9Sstevel@tonic-gate if (user_abort)
2587c478bd9Sstevel@tonic-gate return 0;
2597c478bd9Sstevel@tonic-gate
2607c478bd9Sstevel@tonic-gate if (! block && retry++ < MAX_TFTP_RETRIES)
2617c478bd9Sstevel@tonic-gate {
2627c478bd9Sstevel@tonic-gate /* Maybe initial request was lost. */
2637c478bd9Sstevel@tonic-gate #ifdef TFTP_DEBUG
2647c478bd9Sstevel@tonic-gate grub_printf ("Maybe initial request was lost.\n");
2657c478bd9Sstevel@tonic-gate #endif
2667c478bd9Sstevel@tonic-gate if (! udp_transmit (arptable[ARP_SERVER].ipaddr.s_addr,
2677c478bd9Sstevel@tonic-gate ++iport, TFTP_PORT, len, &tp))
2687c478bd9Sstevel@tonic-gate return 0;
2697c478bd9Sstevel@tonic-gate
2707c478bd9Sstevel@tonic-gate continue;
2717c478bd9Sstevel@tonic-gate }
2727c478bd9Sstevel@tonic-gate
2737c478bd9Sstevel@tonic-gate #ifdef CONGESTED
2747c478bd9Sstevel@tonic-gate if (block && ((retry += TFTP_REXMT) < TFTP_TIMEOUT))
2757c478bd9Sstevel@tonic-gate {
2767c478bd9Sstevel@tonic-gate /* We resend our last ack. */
2777c478bd9Sstevel@tonic-gate # ifdef TFTP_DEBUG
2787c478bd9Sstevel@tonic-gate grub_printf ("<REXMT>\n");
2797c478bd9Sstevel@tonic-gate # endif
2807c478bd9Sstevel@tonic-gate udp_transmit (arptable[ARP_SERVER].ipaddr.s_addr,
2817c478bd9Sstevel@tonic-gate iport, oport,
2827c478bd9Sstevel@tonic-gate TFTP_MIN_PACKET, &tp);
2837c478bd9Sstevel@tonic-gate continue;
2847c478bd9Sstevel@tonic-gate }
2857c478bd9Sstevel@tonic-gate #endif
2867c478bd9Sstevel@tonic-gate /* Timeout. */
2877c478bd9Sstevel@tonic-gate return 0;
2887c478bd9Sstevel@tonic-gate }
2897c478bd9Sstevel@tonic-gate
2907c478bd9Sstevel@tonic-gate tr = (struct tftp_t *) &nic.packet[ETH_HLEN];
2917c478bd9Sstevel@tonic-gate if (tr->opcode == ntohs (TFTP_ERROR))
2927c478bd9Sstevel@tonic-gate {
2937c478bd9Sstevel@tonic-gate grub_printf ("TFTP error %d (%s)\n",
2947c478bd9Sstevel@tonic-gate ntohs (tr->u.err.errcode),
2957c478bd9Sstevel@tonic-gate tr->u.err.errmsg);
2967c478bd9Sstevel@tonic-gate return 0;
2977c478bd9Sstevel@tonic-gate }
2987c478bd9Sstevel@tonic-gate
2997c478bd9Sstevel@tonic-gate if (tr->opcode == ntohs (TFTP_OACK))
3007c478bd9Sstevel@tonic-gate {
3017c478bd9Sstevel@tonic-gate char *p = tr->u.oack.data, *e;
3027c478bd9Sstevel@tonic-gate
3037c478bd9Sstevel@tonic-gate #ifdef TFTP_DEBUG
3047c478bd9Sstevel@tonic-gate grub_printf ("OACK ");
3057c478bd9Sstevel@tonic-gate #endif
3067c478bd9Sstevel@tonic-gate /* Shouldn't happen. */
3077c478bd9Sstevel@tonic-gate if (prevblock)
3087c478bd9Sstevel@tonic-gate {
3097c478bd9Sstevel@tonic-gate /* Ignore it. */
3107c478bd9Sstevel@tonic-gate grub_printf ("%s:%d: warning: PREVBLOCK != 0 (0x%x)\n",
3117c478bd9Sstevel@tonic-gate __FILE__, __LINE__, prevblock);
3127c478bd9Sstevel@tonic-gate continue;
3137c478bd9Sstevel@tonic-gate }
3147c478bd9Sstevel@tonic-gate
3157c478bd9Sstevel@tonic-gate len = ntohs (tr->udp.len) - sizeof (struct udphdr) - 2;
3167c478bd9Sstevel@tonic-gate if (len > TFTP_MAX_PACKET)
3177c478bd9Sstevel@tonic-gate goto noak;
3187c478bd9Sstevel@tonic-gate
3197c478bd9Sstevel@tonic-gate e = p + len;
3207c478bd9Sstevel@tonic-gate while (*p != '\000' && p < e)
3217c478bd9Sstevel@tonic-gate {
3227c478bd9Sstevel@tonic-gate if (! grub_strcmp ("blksize", p))
3237c478bd9Sstevel@tonic-gate {
3247c478bd9Sstevel@tonic-gate p += 8;
3257c478bd9Sstevel@tonic-gate if ((packetsize = getdec (&p)) < TFTP_DEFAULTSIZE_PACKET)
3267c478bd9Sstevel@tonic-gate goto noak;
3277c478bd9Sstevel@tonic-gate #ifdef TFTP_DEBUG
3287c478bd9Sstevel@tonic-gate grub_printf ("blksize = %d\n", packetsize);
3297c478bd9Sstevel@tonic-gate #endif
3307c478bd9Sstevel@tonic-gate }
3317c478bd9Sstevel@tonic-gate else if (! grub_strcmp ("tsize", p))
3327c478bd9Sstevel@tonic-gate {
3337c478bd9Sstevel@tonic-gate p += 6;
3347c478bd9Sstevel@tonic-gate if ((filemax = getdec (&p)) < 0)
3357c478bd9Sstevel@tonic-gate {
3367c478bd9Sstevel@tonic-gate filemax = -1;
3377c478bd9Sstevel@tonic-gate goto noak;
3387c478bd9Sstevel@tonic-gate }
3397c478bd9Sstevel@tonic-gate #ifdef TFTP_DEBUG
3407c478bd9Sstevel@tonic-gate grub_printf ("tsize = %d\n", filemax);
3417c478bd9Sstevel@tonic-gate #endif
3427c478bd9Sstevel@tonic-gate }
3437c478bd9Sstevel@tonic-gate else
3447c478bd9Sstevel@tonic-gate {
3457c478bd9Sstevel@tonic-gate noak:
3467c478bd9Sstevel@tonic-gate #ifdef TFTP_DEBUG
3477c478bd9Sstevel@tonic-gate grub_printf ("NOAK\n");
3487c478bd9Sstevel@tonic-gate #endif
3497c478bd9Sstevel@tonic-gate tp.opcode = htons (TFTP_ERROR);
3507c478bd9Sstevel@tonic-gate tp.u.err.errcode = 8;
3517c478bd9Sstevel@tonic-gate len = (grub_sprintf ((char *) tp.u.err.errmsg,
3527c478bd9Sstevel@tonic-gate "RFC1782 error")
3537c478bd9Sstevel@tonic-gate + sizeof (tp.ip) + sizeof (tp.udp)
3547c478bd9Sstevel@tonic-gate + sizeof (tp.opcode) + sizeof (tp.u.err.errcode)
3557c478bd9Sstevel@tonic-gate + 1);
3567c478bd9Sstevel@tonic-gate udp_transmit (arptable[ARP_SERVER].ipaddr.s_addr,
3577c478bd9Sstevel@tonic-gate iport, ntohs (tr->udp.src),
3587c478bd9Sstevel@tonic-gate len, &tp);
3597c478bd9Sstevel@tonic-gate return 0;
3607c478bd9Sstevel@tonic-gate }
3617c478bd9Sstevel@tonic-gate
3627c478bd9Sstevel@tonic-gate while (p < e && *p)
3637c478bd9Sstevel@tonic-gate p++;
3647c478bd9Sstevel@tonic-gate
3657c478bd9Sstevel@tonic-gate if (p < e)
3667c478bd9Sstevel@tonic-gate p++;
3677c478bd9Sstevel@tonic-gate }
3687c478bd9Sstevel@tonic-gate
3697c478bd9Sstevel@tonic-gate if (p > e)
3707c478bd9Sstevel@tonic-gate goto noak;
3717c478bd9Sstevel@tonic-gate
3727c478bd9Sstevel@tonic-gate /* This ensures that the packet does not get processed as
3737c478bd9Sstevel@tonic-gate data! */
3747c478bd9Sstevel@tonic-gate block = tp.u.ack.block = 0;
3757c478bd9Sstevel@tonic-gate }
3767c478bd9Sstevel@tonic-gate else if (tr->opcode == ntohs (TFTP_DATA))
3777c478bd9Sstevel@tonic-gate {
3787c478bd9Sstevel@tonic-gate #ifdef TFTP_DEBUG
3797c478bd9Sstevel@tonic-gate grub_printf ("DATA ");
3807c478bd9Sstevel@tonic-gate #endif
3817c478bd9Sstevel@tonic-gate len = ntohs (tr->udp.len) - sizeof (struct udphdr) - 4;
3827c478bd9Sstevel@tonic-gate
3837c478bd9Sstevel@tonic-gate /* Shouldn't happen. */
3847c478bd9Sstevel@tonic-gate if (len > packetsize)
3857c478bd9Sstevel@tonic-gate {
3867c478bd9Sstevel@tonic-gate /* Ignore it. */
3877c478bd9Sstevel@tonic-gate grub_printf ("%s:%d: warning: LEN > PACKETSIZE (0x%x > 0x%x)\n",
3887c478bd9Sstevel@tonic-gate __FILE__, __LINE__, len, packetsize);
3897c478bd9Sstevel@tonic-gate continue;
3907c478bd9Sstevel@tonic-gate }
3917c478bd9Sstevel@tonic-gate
3927c478bd9Sstevel@tonic-gate block = ntohs (tp.u.ack.block = tr->u.data.block);
3937c478bd9Sstevel@tonic-gate }
3947c478bd9Sstevel@tonic-gate else
3957c478bd9Sstevel@tonic-gate /* Neither TFTP_OACK nor TFTP_DATA. */
3967c478bd9Sstevel@tonic-gate break;
3977c478bd9Sstevel@tonic-gate
398*df05b9eeSGuoli Shu if ((block || bcounter) && (block != (unsigned short) (prevblock + 1)))
3997c478bd9Sstevel@tonic-gate /* Block order should be continuous */
4007c478bd9Sstevel@tonic-gate tp.u.ack.block = htons (block = prevblock);
4017c478bd9Sstevel@tonic-gate
4027c478bd9Sstevel@tonic-gate /* Should be continuous. */
4037c478bd9Sstevel@tonic-gate tp.opcode = abort ? htons (TFTP_ERROR) : htons (TFTP_ACK);
4047c478bd9Sstevel@tonic-gate oport = ntohs (tr->udp.src);
4057c478bd9Sstevel@tonic-gate
4067c478bd9Sstevel@tonic-gate #ifdef TFTP_DEBUG
4077c478bd9Sstevel@tonic-gate grub_printf ("ACK\n");
4087c478bd9Sstevel@tonic-gate #endif
4097c478bd9Sstevel@tonic-gate /* Ack. */
4107c478bd9Sstevel@tonic-gate udp_transmit (arptable[ARP_SERVER].ipaddr.s_addr, iport,
4117c478bd9Sstevel@tonic-gate oport, TFTP_MIN_PACKET, &tp);
4127c478bd9Sstevel@tonic-gate
4137c478bd9Sstevel@tonic-gate if (abort)
4147c478bd9Sstevel@tonic-gate {
4157c478bd9Sstevel@tonic-gate buf_eof = 1;
4167c478bd9Sstevel@tonic-gate break;
4177c478bd9Sstevel@tonic-gate }
4187c478bd9Sstevel@tonic-gate
4197c478bd9Sstevel@tonic-gate /* Retransmission or OACK. */
4207c478bd9Sstevel@tonic-gate if ((unsigned short) (block - prevblock) != 1)
4217c478bd9Sstevel@tonic-gate /* Don't process. */
4227c478bd9Sstevel@tonic-gate continue;
4237c478bd9Sstevel@tonic-gate
4247c478bd9Sstevel@tonic-gate prevblock = block;
4257c478bd9Sstevel@tonic-gate /* Is it the right place to zero the timer? */
4267c478bd9Sstevel@tonic-gate retry = 0;
4277c478bd9Sstevel@tonic-gate
4287c478bd9Sstevel@tonic-gate /* In GRUB, this variable doesn't play any important role at all,
4297c478bd9Sstevel@tonic-gate but use it for consistency with Etherboot. */
4307c478bd9Sstevel@tonic-gate bcounter++;
4317c478bd9Sstevel@tonic-gate
4327c478bd9Sstevel@tonic-gate /* Copy the downloaded data to the buffer. */
4337c478bd9Sstevel@tonic-gate grub_memmove (buf + buf_read, tr->u.data.download, len);
4347c478bd9Sstevel@tonic-gate buf_read += len;
4357c478bd9Sstevel@tonic-gate
4367c478bd9Sstevel@tonic-gate /* End of data. */
4377c478bd9Sstevel@tonic-gate if (len < packetsize)
4387c478bd9Sstevel@tonic-gate buf_eof = 1;
4397c478bd9Sstevel@tonic-gate }
4407c478bd9Sstevel@tonic-gate
4417c478bd9Sstevel@tonic-gate return 1;
4427c478bd9Sstevel@tonic-gate }
4437c478bd9Sstevel@tonic-gate
4447c478bd9Sstevel@tonic-gate /* Send the RRQ whose length is LEN. */
4457c478bd9Sstevel@tonic-gate static int
send_rrq(void)4467c478bd9Sstevel@tonic-gate send_rrq (void)
4477c478bd9Sstevel@tonic-gate {
4487c478bd9Sstevel@tonic-gate /* Initialize some variables. */
4497c478bd9Sstevel@tonic-gate retry = 0;
4507c478bd9Sstevel@tonic-gate block = 0;
4517c478bd9Sstevel@tonic-gate prevblock = 0;
4527c478bd9Sstevel@tonic-gate packetsize = TFTP_DEFAULTSIZE_PACKET;
4537c478bd9Sstevel@tonic-gate bcounter = 0;
4547c478bd9Sstevel@tonic-gate
4557c478bd9Sstevel@tonic-gate buf = (char *) FSYS_BUF;
4567c478bd9Sstevel@tonic-gate buf_eof = 0;
4577c478bd9Sstevel@tonic-gate buf_read = 0;
4587c478bd9Sstevel@tonic-gate saved_filepos = 0;
4597c478bd9Sstevel@tonic-gate
4607c478bd9Sstevel@tonic-gate rx_qdrain();
4617c478bd9Sstevel@tonic-gate
4627c478bd9Sstevel@tonic-gate #ifdef TFTP_DEBUG
4637c478bd9Sstevel@tonic-gate grub_printf ("send_rrq ()\n");
4647c478bd9Sstevel@tonic-gate {
4657c478bd9Sstevel@tonic-gate int i;
4667c478bd9Sstevel@tonic-gate char *p;
4677c478bd9Sstevel@tonic-gate
4687c478bd9Sstevel@tonic-gate for (i = 0, p = (char *) &tp; i < len; i++)
4697c478bd9Sstevel@tonic-gate if (p[i] >= ' ' && p[i] <= '~')
4707c478bd9Sstevel@tonic-gate grub_putchar (p[i]);
4717c478bd9Sstevel@tonic-gate else
4727c478bd9Sstevel@tonic-gate grub_printf ("\\%x", (unsigned) p[i]);
4737c478bd9Sstevel@tonic-gate
4747c478bd9Sstevel@tonic-gate grub_putchar ('\n');
4757c478bd9Sstevel@tonic-gate }
4767c478bd9Sstevel@tonic-gate #endif
4777c478bd9Sstevel@tonic-gate /* Send the packet. */
4787c478bd9Sstevel@tonic-gate return udp_transmit (arptable[ARP_SERVER].ipaddr.s_addr, ++iport,
4797c478bd9Sstevel@tonic-gate TFTP_PORT, len, &tp);
4807c478bd9Sstevel@tonic-gate }
4817c478bd9Sstevel@tonic-gate
4827c478bd9Sstevel@tonic-gate /* Mount the network drive. If the drive is ready, return one, otherwise
4837c478bd9Sstevel@tonic-gate return zero. */
4847c478bd9Sstevel@tonic-gate int
tftp_mount(void)4857c478bd9Sstevel@tonic-gate tftp_mount (void)
4867c478bd9Sstevel@tonic-gate {
4877c478bd9Sstevel@tonic-gate /* Check if the current drive is the network drive. */
4887c478bd9Sstevel@tonic-gate if (current_drive != NETWORK_DRIVE)
4897c478bd9Sstevel@tonic-gate return 0;
4907c478bd9Sstevel@tonic-gate
4917c478bd9Sstevel@tonic-gate /* If the drive is not initialized yet, abort. */
4927c478bd9Sstevel@tonic-gate if (! network_ready)
4937c478bd9Sstevel@tonic-gate return 0;
4947c478bd9Sstevel@tonic-gate
4957c478bd9Sstevel@tonic-gate return 1;
4967c478bd9Sstevel@tonic-gate }
4977c478bd9Sstevel@tonic-gate
4987c478bd9Sstevel@tonic-gate /* Read up to SIZE bytes, returned in ADDR. */
4997c478bd9Sstevel@tonic-gate int
tftp_read(char * addr,int size)5007c478bd9Sstevel@tonic-gate tftp_read (char *addr, int size)
5017c478bd9Sstevel@tonic-gate {
5027c478bd9Sstevel@tonic-gate /* How many bytes is read? */
5037c478bd9Sstevel@tonic-gate int ret = 0;
5047c478bd9Sstevel@tonic-gate
5057c478bd9Sstevel@tonic-gate #ifdef TFTP_DEBUG
5067c478bd9Sstevel@tonic-gate grub_printf ("tftp_read (0x%x, %d)\n", (int) addr, size);
5077c478bd9Sstevel@tonic-gate #endif
5087c478bd9Sstevel@tonic-gate
50999ed6083Sszhou if (use_bios_pxe)
51099ed6083Sszhou return (tftp_read_undi(addr, size));
51199ed6083Sszhou
5127c478bd9Sstevel@tonic-gate if (filepos < saved_filepos)
5137c478bd9Sstevel@tonic-gate {
5147c478bd9Sstevel@tonic-gate /* Uggh.. FILEPOS has been moved backwards. So reopen the file. */
5157c478bd9Sstevel@tonic-gate buf_read = 0;
5167c478bd9Sstevel@tonic-gate buf_fill (1);
5177c478bd9Sstevel@tonic-gate grub_memmove ((char *) &tp, (char *) &saved_tp, saved_len);
5187c478bd9Sstevel@tonic-gate len = saved_len;
5197c478bd9Sstevel@tonic-gate #ifdef TFTP_DEBUG
5207c478bd9Sstevel@tonic-gate {
5217c478bd9Sstevel@tonic-gate int i;
5227c478bd9Sstevel@tonic-gate grub_printf ("opcode = 0x%x, rrq = ", (unsigned long) tp.opcode);
5237c478bd9Sstevel@tonic-gate for (i = 0; i < TFTP_DEFAULTSIZE_PACKET; i++)
5247c478bd9Sstevel@tonic-gate {
5257c478bd9Sstevel@tonic-gate if (tp.u.rrq[i] >= ' ' && tp.u.rrq[i] <= '~')
5267c478bd9Sstevel@tonic-gate grub_putchar (tp.u.rrq[i]);
5277c478bd9Sstevel@tonic-gate else
5287c478bd9Sstevel@tonic-gate grub_putchar ('*');
5297c478bd9Sstevel@tonic-gate }
5307c478bd9Sstevel@tonic-gate grub_putchar ('\n');
5317c478bd9Sstevel@tonic-gate }
5327c478bd9Sstevel@tonic-gate #endif
5337c478bd9Sstevel@tonic-gate
5347c478bd9Sstevel@tonic-gate if (! send_rrq ())
5357c478bd9Sstevel@tonic-gate {
5367c478bd9Sstevel@tonic-gate errnum = ERR_WRITE;
5377c478bd9Sstevel@tonic-gate return 0;
5387c478bd9Sstevel@tonic-gate }
5397c478bd9Sstevel@tonic-gate }
5407c478bd9Sstevel@tonic-gate
5417c478bd9Sstevel@tonic-gate while (size > 0)
5427c478bd9Sstevel@tonic-gate {
5437c478bd9Sstevel@tonic-gate int amt = buf_read + saved_filepos - filepos;
5447c478bd9Sstevel@tonic-gate
5457c478bd9Sstevel@tonic-gate /* If the length that can be copied from the buffer is over the
5467c478bd9Sstevel@tonic-gate requested size, cut it down. */
5477c478bd9Sstevel@tonic-gate if (amt > size)
5487c478bd9Sstevel@tonic-gate amt = size;
5497c478bd9Sstevel@tonic-gate
5507c478bd9Sstevel@tonic-gate if (amt > 0)
5517c478bd9Sstevel@tonic-gate {
5527c478bd9Sstevel@tonic-gate /* Copy the buffer to the supplied memory space. */
5537c478bd9Sstevel@tonic-gate grub_memmove (addr, buf + filepos - saved_filepos, amt);
5547c478bd9Sstevel@tonic-gate size -= amt;
5557c478bd9Sstevel@tonic-gate addr += amt;
5567c478bd9Sstevel@tonic-gate filepos += amt;
5577c478bd9Sstevel@tonic-gate ret += amt;
5587c478bd9Sstevel@tonic-gate
5597c478bd9Sstevel@tonic-gate /* If the size of the empty space becomes small, move the unused
5607c478bd9Sstevel@tonic-gate data forwards. */
5617c478bd9Sstevel@tonic-gate if (filepos - saved_filepos > FSYS_BUFLEN / 2)
5627c478bd9Sstevel@tonic-gate {
5637c478bd9Sstevel@tonic-gate grub_memmove (buf, buf + FSYS_BUFLEN / 2, FSYS_BUFLEN / 2);
5647c478bd9Sstevel@tonic-gate buf_read -= FSYS_BUFLEN / 2;
5657c478bd9Sstevel@tonic-gate saved_filepos += FSYS_BUFLEN / 2;
5667c478bd9Sstevel@tonic-gate }
5677c478bd9Sstevel@tonic-gate }
5687c478bd9Sstevel@tonic-gate else
5697c478bd9Sstevel@tonic-gate {
5707c478bd9Sstevel@tonic-gate /* Skip the whole buffer. */
5717c478bd9Sstevel@tonic-gate saved_filepos += buf_read;
5727c478bd9Sstevel@tonic-gate buf_read = 0;
5737c478bd9Sstevel@tonic-gate }
5747c478bd9Sstevel@tonic-gate
5757c478bd9Sstevel@tonic-gate /* Read the data. */
5767c478bd9Sstevel@tonic-gate if (size > 0 && ! buf_fill (0))
5777c478bd9Sstevel@tonic-gate {
5787c478bd9Sstevel@tonic-gate errnum = ERR_READ;
5797c478bd9Sstevel@tonic-gate return 0;
5807c478bd9Sstevel@tonic-gate }
5817c478bd9Sstevel@tonic-gate
5827c478bd9Sstevel@tonic-gate /* Sanity check. */
5837c478bd9Sstevel@tonic-gate if (size > 0 && buf_read == 0)
5847c478bd9Sstevel@tonic-gate {
5857c478bd9Sstevel@tonic-gate errnum = ERR_READ;
5867c478bd9Sstevel@tonic-gate return 0;
5877c478bd9Sstevel@tonic-gate }
5887c478bd9Sstevel@tonic-gate }
5897c478bd9Sstevel@tonic-gate
5907c478bd9Sstevel@tonic-gate return ret;
5917c478bd9Sstevel@tonic-gate }
5927c478bd9Sstevel@tonic-gate
5937c478bd9Sstevel@tonic-gate /* Check if the file DIRNAME really exists. Get the size and save it in
5947c478bd9Sstevel@tonic-gate FILEMAX. */
5957c478bd9Sstevel@tonic-gate int
tftp_dir(char * dirname)5967c478bd9Sstevel@tonic-gate tftp_dir (char *dirname)
5977c478bd9Sstevel@tonic-gate {
5987c478bd9Sstevel@tonic-gate int ch;
5997c478bd9Sstevel@tonic-gate
6007c478bd9Sstevel@tonic-gate #ifdef TFTP_DEBUG
6017c478bd9Sstevel@tonic-gate grub_printf ("tftp_dir (%s)\n", dirname);
6027c478bd9Sstevel@tonic-gate #endif
6037c478bd9Sstevel@tonic-gate
60499ed6083Sszhou if (use_bios_pxe)
60599ed6083Sszhou return (tftp_dir_undi(dirname));
60699ed6083Sszhou
6077c478bd9Sstevel@tonic-gate /* In TFTP, there is no way to know what files exist. */
6087c478bd9Sstevel@tonic-gate if (print_possibilities)
6097c478bd9Sstevel@tonic-gate return 1;
6107c478bd9Sstevel@tonic-gate
6117c478bd9Sstevel@tonic-gate /* Don't know the size yet. */
6127c478bd9Sstevel@tonic-gate filemax = -1;
6137c478bd9Sstevel@tonic-gate
6147c478bd9Sstevel@tonic-gate reopen:
6157c478bd9Sstevel@tonic-gate /* Construct the TFTP request packet. */
6167c478bd9Sstevel@tonic-gate tp.opcode = htons (TFTP_RRQ);
6177c478bd9Sstevel@tonic-gate /* Terminate the filename. */
6187c478bd9Sstevel@tonic-gate ch = nul_terminate (dirname);
6197c478bd9Sstevel@tonic-gate /* Make the request string (octet, blksize and tsize). */
6207c478bd9Sstevel@tonic-gate len = (grub_sprintf ((char *) tp.u.rrq,
6217c478bd9Sstevel@tonic-gate "%s%coctet%cblksize%c%d%ctsize%c0",
6227c478bd9Sstevel@tonic-gate dirname, 0, 0, 0, TFTP_MAX_PACKET, 0, 0)
6237c478bd9Sstevel@tonic-gate + sizeof (tp.ip) + sizeof (tp.udp) + sizeof (tp.opcode) + 1);
6247c478bd9Sstevel@tonic-gate /* Restore the original DIRNAME. */
6257c478bd9Sstevel@tonic-gate dirname[grub_strlen (dirname)] = ch;
6267c478bd9Sstevel@tonic-gate /* Save the TFTP packet so that we can reopen the file later. */
6277c478bd9Sstevel@tonic-gate grub_memmove ((char *) &saved_tp, (char *) &tp, len);
6287c478bd9Sstevel@tonic-gate saved_len = len;
6297c478bd9Sstevel@tonic-gate if (! send_rrq ())
6307c478bd9Sstevel@tonic-gate {
6317c478bd9Sstevel@tonic-gate errnum = ERR_WRITE;
6327c478bd9Sstevel@tonic-gate return 0;
6337c478bd9Sstevel@tonic-gate }
6347c478bd9Sstevel@tonic-gate
6357c478bd9Sstevel@tonic-gate /* Read the data. */
6367c478bd9Sstevel@tonic-gate if (! buf_fill (0))
6377c478bd9Sstevel@tonic-gate {
6387c478bd9Sstevel@tonic-gate errnum = ERR_FILE_NOT_FOUND;
6397c478bd9Sstevel@tonic-gate return 0;
6407c478bd9Sstevel@tonic-gate }
6417c478bd9Sstevel@tonic-gate
6427c478bd9Sstevel@tonic-gate if (filemax == -1)
6437c478bd9Sstevel@tonic-gate {
6447c478bd9Sstevel@tonic-gate /* The server doesn't support the "tsize" option, so we must read
6457c478bd9Sstevel@tonic-gate the file twice... */
6467c478bd9Sstevel@tonic-gate
6477c478bd9Sstevel@tonic-gate /* Zero the size of the file. */
6487c478bd9Sstevel@tonic-gate filemax = 0;
6497c478bd9Sstevel@tonic-gate do
6507c478bd9Sstevel@tonic-gate {
6517c478bd9Sstevel@tonic-gate /* Add the length of the downloaded data. */
6527c478bd9Sstevel@tonic-gate filemax += buf_read;
6537c478bd9Sstevel@tonic-gate /* Reset the offset. Just discard the contents of the buffer. */
6547c478bd9Sstevel@tonic-gate buf_read = 0;
6557c478bd9Sstevel@tonic-gate /* Read the data. */
6567c478bd9Sstevel@tonic-gate if (! buf_fill (0))
6577c478bd9Sstevel@tonic-gate {
6587c478bd9Sstevel@tonic-gate errnum = ERR_READ;
6597c478bd9Sstevel@tonic-gate return 0;
6607c478bd9Sstevel@tonic-gate }
6617c478bd9Sstevel@tonic-gate }
6627c478bd9Sstevel@tonic-gate while (! buf_eof);
6637c478bd9Sstevel@tonic-gate
6647c478bd9Sstevel@tonic-gate /* Maybe a few amounts of data remains. */
6657c478bd9Sstevel@tonic-gate filemax += buf_read;
6667c478bd9Sstevel@tonic-gate
6677c478bd9Sstevel@tonic-gate /* Retry the open instruction. */
6687c478bd9Sstevel@tonic-gate goto reopen;
6697c478bd9Sstevel@tonic-gate }
6707c478bd9Sstevel@tonic-gate
6717c478bd9Sstevel@tonic-gate return 1;
6727c478bd9Sstevel@tonic-gate }
6737c478bd9Sstevel@tonic-gate
6747c478bd9Sstevel@tonic-gate /* Close the file. */
6757c478bd9Sstevel@tonic-gate void
tftp_close(void)6767c478bd9Sstevel@tonic-gate tftp_close (void)
6777c478bd9Sstevel@tonic-gate {
6787c478bd9Sstevel@tonic-gate #ifdef TFTP_DEBUG
6797c478bd9Sstevel@tonic-gate grub_printf ("tftp_close ()\n");
6807c478bd9Sstevel@tonic-gate #endif
6817c478bd9Sstevel@tonic-gate
68299ed6083Sszhou if (use_bios_pxe) {
68399ed6083Sszhou tftp_close_undi();
68499ed6083Sszhou return;
68599ed6083Sszhou }
68699ed6083Sszhou
6877c478bd9Sstevel@tonic-gate buf_read = 0;
6887c478bd9Sstevel@tonic-gate buf_fill (1);
6897c478bd9Sstevel@tonic-gate }
69099ed6083Sszhou
69199ed6083Sszhou /* tftp implementation using BIOS established PXE stack */
69299ed6083Sszhou
tftp_file_read_undi(const char * name,int (* fnc)(unsigned char *,unsigned int,unsigned int,int))69399ed6083Sszhou static int tftp_file_read_undi(const char *name,
69499ed6083Sszhou int (*fnc)(unsigned char *, unsigned int, unsigned int, int))
69599ed6083Sszhou {
69699ed6083Sszhou int rc;
69799ed6083Sszhou uint16_t len;
69899ed6083Sszhou
69999ed6083Sszhou buf = (char *)&nic.packet;
70099ed6083Sszhou /* open tftp session */
70199ed6083Sszhou if (eb_pxenv_tftp_open(name, arptable[ARP_SERVER].ipaddr,
70299ed6083Sszhou arptable[ARP_GATEWAY].ipaddr, &packetsize) == 0)
70399ed6083Sszhou return (0);
70499ed6083Sszhou
70599ed6083Sszhou /* read blocks and invoke fnc for each block */
70699ed6083Sszhou for (;;) {
70799ed6083Sszhou rc = eb_pxenv_tftp_read(buf, &len);
70899ed6083Sszhou if (rc == 0)
70999ed6083Sszhou break;
71099ed6083Sszhou rc = fnc(buf, ++block, len, len < packetsize);
71199ed6083Sszhou if (rc <= 0 || len < packetsize)
71299ed6083Sszhou break;
71399ed6083Sszhou }
71499ed6083Sszhou
71599ed6083Sszhou (void) eb_pxenv_tftp_close();
71699ed6083Sszhou return (rc > 0 ? 1 : 0);
71799ed6083Sszhou }
71899ed6083Sszhou
71999ed6083Sszhou /* Fill the buffer by reading the data via the TFTP protocol. */
72099ed6083Sszhou static int
buf_fill_undi(int abort)72199ed6083Sszhou buf_fill_undi(int abort)
72299ed6083Sszhou {
72399ed6083Sszhou int rc;
72499ed6083Sszhou uint8_t *tmpbuf;
72599ed6083Sszhou
72699ed6083Sszhou while (! buf_eof && (buf_read + packetsize <= FSYS_BUFLEN)) {
72799ed6083Sszhou poll_interruptions();
72899ed6083Sszhou if (user_abort)
72999ed6083Sszhou return 0;
73099ed6083Sszhou if (abort) {
73199ed6083Sszhou buf_eof = 1;
73299ed6083Sszhou break;
73399ed6083Sszhou }
73499ed6083Sszhou
73599ed6083Sszhou if (eb_pxenv_tftp_read(buf + buf_read, &len) == 0)
73699ed6083Sszhou return (0);
73799ed6083Sszhou
73899ed6083Sszhou buf_read += len;
73999ed6083Sszhou
74099ed6083Sszhou /* End of data. */
74199ed6083Sszhou if (len < packetsize)
74299ed6083Sszhou buf_eof = 1;
74399ed6083Sszhou }
74499ed6083Sszhou return 1;
74599ed6083Sszhou }
74699ed6083Sszhou
74799ed6083Sszhou static void
tftp_reopen_undi(void)74899ed6083Sszhou tftp_reopen_undi(void)
74999ed6083Sszhou {
75099ed6083Sszhou tftp_close();
75199ed6083Sszhou (void) eb_pxenv_tftp_open(saved_name, arptable[ARP_SERVER].ipaddr,
75299ed6083Sszhou arptable[ARP_GATEWAY].ipaddr, &packetsize);
75399ed6083Sszhou
75499ed6083Sszhou buf_eof = 0;
75599ed6083Sszhou buf_read = 0;
75699ed6083Sszhou saved_filepos = 0;
75799ed6083Sszhou }
75899ed6083Sszhou
75999ed6083Sszhou /* Read up to SIZE bytes, returned in ADDR. */
76099ed6083Sszhou static int
tftp_read_undi(char * addr,int size)76199ed6083Sszhou tftp_read_undi(char *addr, int size)
76299ed6083Sszhou {
76399ed6083Sszhou int ret = 0;
76499ed6083Sszhou
76599ed6083Sszhou if (filepos < saved_filepos) {
76699ed6083Sszhou /* Uggh.. FILEPOS has been moved backwards. reopen the file. */
76799ed6083Sszhou tftp_reopen_undi();
76899ed6083Sszhou }
76999ed6083Sszhou
77099ed6083Sszhou while (size > 0) {
77199ed6083Sszhou int amt = buf_read + saved_filepos - filepos;
77299ed6083Sszhou
77399ed6083Sszhou /* If the length that can be copied from the buffer is over
77499ed6083Sszhou the requested size, cut it down. */
77599ed6083Sszhou if (amt > size)
77699ed6083Sszhou amt = size;
77799ed6083Sszhou
77899ed6083Sszhou if (amt > 0) {
77999ed6083Sszhou /* Copy the buffer to the supplied memory space. */
78099ed6083Sszhou grub_memmove (addr, buf + filepos - saved_filepos, amt);
78199ed6083Sszhou size -= amt;
78299ed6083Sszhou addr += amt;
78399ed6083Sszhou filepos += amt;
78499ed6083Sszhou ret += amt;
78599ed6083Sszhou
78699ed6083Sszhou /* If the size of the empty space becomes small,
78799ed6083Sszhou * move the unused data forwards.
78899ed6083Sszhou */
78999ed6083Sszhou if (filepos - saved_filepos > FSYS_BUFLEN / 2) {
79099ed6083Sszhou grub_memmove (buf, buf + FSYS_BUFLEN / 2,
79199ed6083Sszhou FSYS_BUFLEN / 2);
79299ed6083Sszhou buf_read -= FSYS_BUFLEN / 2;
79399ed6083Sszhou saved_filepos += FSYS_BUFLEN / 2;
79499ed6083Sszhou }
79599ed6083Sszhou } else {
79699ed6083Sszhou /* Skip the whole buffer. */
79799ed6083Sszhou saved_filepos += buf_read;
79899ed6083Sszhou buf_read = 0;
79999ed6083Sszhou }
80099ed6083Sszhou
80199ed6083Sszhou /* Read the data. */
80299ed6083Sszhou if (size > 0 && ! buf_fill (0)) {
80399ed6083Sszhou errnum = ERR_READ;
80499ed6083Sszhou return 0;
80599ed6083Sszhou }
80699ed6083Sszhou
80799ed6083Sszhou /* Sanity check. */
80899ed6083Sszhou if (size > 0 && buf_read == 0) {
80999ed6083Sszhou errnum = ERR_READ;
81099ed6083Sszhou return 0;
81199ed6083Sszhou }
81299ed6083Sszhou }
81399ed6083Sszhou
81499ed6083Sszhou return ret;
81599ed6083Sszhou }
81699ed6083Sszhou
81799ed6083Sszhou static int
tftp_dir_undi(char * dirname)81899ed6083Sszhou tftp_dir_undi(char *dirname)
81999ed6083Sszhou {
82099ed6083Sszhou int rc, ch;
82199ed6083Sszhou uint16_t len;
82299ed6083Sszhou
82399ed6083Sszhou /* In TFTP, there is no way to know what files exist. */
82499ed6083Sszhou if (print_possibilities)
82599ed6083Sszhou return 1;
82699ed6083Sszhou
82799ed6083Sszhou /* name may be space terminated */
8285bb86dd8Ssethg ch = nul_terminate(dirname);
82999ed6083Sszhou saved_name = (char *)&saved_tp;
83099ed6083Sszhou sprintf(saved_name, "%s", dirname);
83199ed6083Sszhou
83299ed6083Sszhou /* Restore the original dirname */
83399ed6083Sszhou dirname[grub_strlen (dirname)] = ch;
83499ed6083Sszhou
83599ed6083Sszhou /* get the file size; must call before tftp_open */
83699ed6083Sszhou rc = eb_pxenv_tftp_get_fsize(saved_name, arptable[ARP_SERVER].ipaddr,
83799ed6083Sszhou arptable[ARP_GATEWAY].ipaddr, &filemax);
83899ed6083Sszhou
83999ed6083Sszhou /* open tftp session */
84099ed6083Sszhou if (eb_pxenv_tftp_open(saved_name, arptable[ARP_SERVER].ipaddr,
84199ed6083Sszhou arptable[ARP_GATEWAY].ipaddr, &packetsize) == 0)
84299ed6083Sszhou return (0);
84399ed6083Sszhou
84499ed6083Sszhou buf = (char *) FSYS_BUF;
84599ed6083Sszhou buf_eof = 0;
84699ed6083Sszhou buf_read = 0;
84799ed6083Sszhou saved_filepos = 0;
84899ed6083Sszhou
84999ed6083Sszhou if (rc == 0) {
85099ed6083Sszhou /* Read the entire file to get filemax */
85199ed6083Sszhou filemax = 0;
85299ed6083Sszhou do {
85399ed6083Sszhou /* Add the length of the downloaded data. */
85499ed6083Sszhou filemax += buf_read;
85599ed6083Sszhou buf_read = 0;
85699ed6083Sszhou if (! buf_fill (0)) {
85799ed6083Sszhou errnum = ERR_READ;
85899ed6083Sszhou return 0;
85999ed6083Sszhou }
86099ed6083Sszhou } while (! buf_eof);
86199ed6083Sszhou
86299ed6083Sszhou /* Maybe a few amounts of data remains. */
86399ed6083Sszhou filemax += buf_read;
86499ed6083Sszhou
86599ed6083Sszhou tftp_reopen_undi(); /* reopen file to read from beginning */
86699ed6083Sszhou }
86799ed6083Sszhou
86899ed6083Sszhou return (1);
86999ed6083Sszhou }
87099ed6083Sszhou
87199ed6083Sszhou static void
tftp_close_undi(void)87299ed6083Sszhou tftp_close_undi(void)
87399ed6083Sszhou {
87499ed6083Sszhou buf_read = 0;
87599ed6083Sszhou buf_fill (1);
87699ed6083Sszhou (void) eb_pxenv_tftp_close();
87799ed6083Sszhou }
878