From cbd8a35c6dbaaca35494cbec319960887ad02dcf Mon Sep 17 00:00:00 2001 From: wdenk Date: Tue, 24 Feb 2004 02:00:03 +0000 Subject: [PATCH] * Patch by Masami Komiy, 22 Feb 2004: Add support for NFS for file download * Minor code cleanup --- CHANGELOG | 3 + common/cmd_bootm.c | 4 +- common/cmd_itest.c | 3 + common/cmd_mem.c | 1 + common/cmd_net.c | 13 + cpu/mpc5xxx/config.mk | 2 +- cpu/mpc5xxx/cpu_init.c | 2 +- cpu/mpc5xxx/fec.c | 4 +- cpu/mpc5xxx/start.S | 2 +- include/asm-ppc/global_data.h | 2 +- include/asm-ppc/u-boot.h | 4 +- include/bmp_logo.h | 2 +- include/cmd_confdefs.h | 1 + include/common.h | 3 +- include/configs/IceCube.h | 4 +- include/configs/TOP5200.h | 4 +- include/net.h | 8 +- include/ppc_asm.tmpl | 2 +- lib_ppc/board.c | 12 +- net/Makefile | 2 +- net/eth.c | 2 +- net/net.c | 24 ++ net/nfs.c | 772 ++++++++++++++++++++++++++++++++++ net/nfs.h | 74 ++++ 24 files changed, 921 insertions(+), 29 deletions(-) create mode 100644 net/nfs.c create mode 100644 net/nfs.h diff --git a/CHANGELOG b/CHANGELOG index f636d3d2ad..0dedea4fa8 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -2,6 +2,9 @@ Changes for U-Boot 1.0.2: ====================================================================== +* Patch by Masami Komiy, 22 Feb 2004: + Add support for NFS for file download + * Patch by Andrea Scian, 17 Feb 2004: Add support for S3C44B0 processor and DAVE B2 board diff --git a/common/cmd_bootm.c b/common/cmd_bootm.c index ac9c32e7b8..72553aa061 100644 --- a/common/cmd_bootm.c +++ b/common/cmd_bootm.c @@ -575,10 +575,10 @@ do_bootm_linux (cmd_tbl_t *cmdtp, int flag, kbd->bi_sccfreq /= 1000000L; kbd->bi_vco /= 1000000L; #endif /* CONFIG_8260 */ -#if defined(CONFIG_MPC5XXX) +#if defined(CONFIG_MPC5xxx) kbd->bi_ipbfreq /= 1000000L; kbd->bi_pcifreq /= 1000000L; -#endif /* CONFIG_MPC5XXX */ +#endif /* CONFIG_MPC5xxx */ } kernel = (void (*)(bd_t *, ulong, ulong, ulong, ulong))hdr->ih_ep; diff --git a/common/cmd_itest.c b/common/cmd_itest.c index 87260b02d6..7fba598d42 100644 --- a/common/cmd_itest.c +++ b/common/cmd_itest.c @@ -32,6 +32,8 @@ #include #include +#if (CONFIG_COMMANDS & CFG_CMD_ITEST) + #define EQ 0 #define NE 1 #define LT 2 @@ -195,3 +197,4 @@ U_BOOT_CMD( "itest - return true/false on integer compare\n", "[.b, .w, .l, .s] [*]value1 [*]value2\n" ); +#endif /* CONFIG_COMMANDS & CFG_CMD_ITEST */ diff --git a/common/cmd_mem.c b/common/cmd_mem.c index 0a5cca9a93..39fc1804f3 100644 --- a/common/cmd_mem.c +++ b/common/cmd_mem.c @@ -38,6 +38,7 @@ #if (CONFIG_COMMANDS & (CFG_CMD_MEMORY | \ CFG_CMD_I2C | \ + CFG_CMD_ITEST | \ CFG_CMD_PCI | \ CMD_CMD_PORTIO ) ) int cmd_get_data_size(char* arg, int default_size) diff --git a/common/cmd_net.c b/common/cmd_net.c index e9b54d2450..f13e9d493c 100644 --- a/common/cmd_net.c +++ b/common/cmd_net.c @@ -81,6 +81,19 @@ U_BOOT_CMD( ); #endif /* CFG_CMD_DHCP */ +#if (CONFIG_COMMANDS & CFG_CMD_NFS) +int do_nfs (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) +{ + return netboot_common(NFS, cmdtp, argc, argv); +} + +U_BOOT_CMD( + nfs, 3, 1, do_nfs, + "nfs - boot image via network using NFS protocol\n", + "[loadAddress] [host ip addr:bootfilename]\n" +); +#endif /* CFG_CMD_NFS */ + static void netboot_update_env(void) { char tmp[16] ; diff --git a/cpu/mpc5xxx/config.mk b/cpu/mpc5xxx/config.mk index 0bee6d1347..ecd94e9b34 100644 --- a/cpu/mpc5xxx/config.mk +++ b/cpu/mpc5xxx/config.mk @@ -23,5 +23,5 @@ PLATFORM_RELFLAGS += -fPIC -ffixed-r14 -meabi -PLATFORM_CPPFLAGS += -DCONFIG_MPC5XXX -ffixed-r2 -ffixed-r29 \ +PLATFORM_CPPFLAGS += -DCONFIG_MPC5xxx -ffixed-r2 -ffixed-r29 \ -mstring -mcpu=603e -mmultiple diff --git a/cpu/mpc5xxx/cpu_init.c b/cpu/mpc5xxx/cpu_init.c index c23d621ec0..ce562c1728 100644 --- a/cpu/mpc5xxx/cpu_init.c +++ b/cpu/mpc5xxx/cpu_init.c @@ -183,7 +183,7 @@ int cpu_init_r (void) /* route critical ints to normal ints */ *(vu_long *)MPC5XXX_ICTL_EXT |= 0x00000001; -#if (CONFIG_COMMANDS & CFG_CMD_NET) && defined(CONFIG_MPC5XXX_FEC) +#if (CONFIG_COMMANDS & CFG_CMD_NET) && defined(CONFIG_MPC5xxx_FEC) /* load FEC microcode */ loadtask(0, 2); #endif diff --git a/cpu/mpc5xxx/fec.c b/cpu/mpc5xxx/fec.c index e48fa88b5d..04e8001dfe 100644 --- a/cpu/mpc5xxx/fec.c +++ b/cpu/mpc5xxx/fec.c @@ -17,7 +17,7 @@ /* #define DEBUG 0x28 */ #if (CONFIG_COMMANDS & CFG_CMD_NET) && defined(CONFIG_NET_MULTI) && \ - defined(CONFIG_MPC5XXX_FEC) + defined(CONFIG_MPC5xxx_FEC) #if (DEBUG & 0x60) static void tfifo_print(mpc5xxx_fec_priv *fec); @@ -980,4 +980,4 @@ static uint32 local_crc32(char *string, unsigned int crc_value, int len) } #endif /* DEBUG */ -#endif /* CONFIG_MPC5XXX_FEC */ +#endif /* CONFIG_MPC5xxx_FEC */ diff --git a/cpu/mpc5xxx/start.S b/cpu/mpc5xxx/start.S index 6f0b269b46..a1631f49a4 100644 --- a/cpu/mpc5xxx/start.S +++ b/cpu/mpc5xxx/start.S @@ -29,7 +29,7 @@ #include #include -#define CONFIG_MPC5XXX 1 /* needed for Linux kernel header files */ +#define CONFIG_MPC5xxx 1 /* needed for Linux kernel header files */ #define _LINUX_CONFIG_H 1 /* avoid reading Linux autoconf.h file */ #include diff --git a/include/asm-ppc/global_data.h b/include/asm-ppc/global_data.h index 1a9808325a..e3ff631eb3 100644 --- a/include/asm-ppc/global_data.h +++ b/include/asm-ppc/global_data.h @@ -46,7 +46,7 @@ typedef struct global_data { unsigned long scc_clk; unsigned long brg_clk; #endif -#if defined(CONFIG_MPC5XXX) +#if defined(CONFIG_MPC5xxx) unsigned long ipb_clk; unsigned long pci_clk; #endif diff --git a/include/asm-ppc/u-boot.h b/include/asm-ppc/u-boot.h index e9d9dc55b4..8141222823 100644 --- a/include/asm-ppc/u-boot.h +++ b/include/asm-ppc/u-boot.h @@ -42,7 +42,7 @@ typedef struct bd_info { || defined(CONFIG_E500) unsigned long bi_immr_base; /* base of IMMR register */ #endif -#if defined(CONFIG_MPC5XXX) +#if defined(CONFIG_MPC5xxx) unsigned long bi_mbar_base; /* base of internal registers */ #endif unsigned long bi_bootflags; /* boot / reboot flag (for LynxOS) */ @@ -57,7 +57,7 @@ typedef struct bd_info { unsigned long bi_sccfreq; /* SCC_CLK Freq, in MHz */ unsigned long bi_vco; /* VCO Out from PLL, in MHz */ #endif -#if defined(CONFIG_MPC5XXX) +#if defined(CONFIG_MPC5xxx) unsigned long bi_ipbfreq; /* IPB Bus Freq, in MHz */ unsigned long bi_pcifreq; /* PCI Bus Freq, in MHz */ #endif diff --git a/include/bmp_logo.h b/include/bmp_logo.h index 9c924b8592..265f744d0e 100644 --- a/include/bmp_logo.h +++ b/include/bmp_logo.h @@ -18,7 +18,7 @@ unsigned short bmp_logo_palette[] = { 0x0343, 0x0454, 0x0565, 0x0565, 0x0676, 0x0787, 0x0898, 0x0999, 0x0AAA, 0x0ABA, 0x0BCB, 0x0CCC, 0x0DDD, 0x0EEE, 0x0FFF, 0x0FB3, 0x0FB4, 0x0FC4, 0x0FC5, 0x0FC6, 0x0FD7, 0x0FD8, 0x0FD9, 0x0FDA, - 0x0FEA, 0x0FEB, 0x0FEC, 0x0FFD, 0x0FFE, 0x0FFF, 0x0FFF, + 0x0FEA, 0x0FEB, 0x0FEC, 0x0FFD, 0x0FFE, 0x0FFF, 0x0FFF, }; unsigned char bmp_logo_bitmap[] = { diff --git a/include/cmd_confdefs.h b/include/cmd_confdefs.h index af5654cd67..6f8154c8ed 100644 --- a/include/cmd_confdefs.h +++ b/include/cmd_confdefs.h @@ -86,6 +86,7 @@ #define CFG_CMD_FAT 0x0010000000000000U /* FAT support */ #define CFG_CMD_IMLS 0x0020000000000000U /* List all found images */ #define CFG_CMD_ITEST 0x0040000000000000U /* Integer (and string) test */ +#define CFG_CMD_NFS 0x0080000000000000U /* NFS support */ #define CFG_CMD_ALL 0xFFFFFFFFFFFFFFFFU /* ALL commands */ diff --git a/include/common.h b/include/common.h index df58008be8..a1460f4c65 100644 --- a/include/common.h +++ b/include/common.h @@ -319,6 +319,7 @@ void relocate_code (ulong, gd_t *, ulong); ulong get_endaddr (void); void trap_init (ulong); #if defined (CONFIG_4xx) || \ + defined (CONFIG_5xxx) || \ defined (CONFIG_74xx_7xx) || \ defined (CONFIG_74x) || \ defined (CONFIG_75x) || \ @@ -361,7 +362,7 @@ int sdram_adjust_866 (void); #if defined(CONFIG_8260) int prt_8260_clks (void); #endif -#if defined(CONFIG_MPC5XXX) +#if defined(CONFIG_MPC5xxx) int prt_mpc5xxx_clks (void); #endif #ifdef CONFIG_4xx diff --git a/include/configs/IceCube.h b/include/configs/IceCube.h index 194f62faf2..888b165230 100644 --- a/include/configs/IceCube.h +++ b/include/configs/IceCube.h @@ -29,7 +29,7 @@ * (easy to change) */ -#define CONFIG_MPC5XXX 1 /* This is an MPC5xxx CPU */ +#define CONFIG_MPC5xxx 1 /* This is an MPC5xxx CPU */ #define CONFIG_ICECUBE 1 /* ... on IceCube board */ #define CFG_MPC5XXX_CLKIN 33000000 /* ... running at 33.000000MHz */ @@ -213,7 +213,7 @@ /* * Ethernet configuration */ -#define CONFIG_MPC5XXX_FEC 1 +#define CONFIG_MPC5xxx_FEC 1 #define CONFIG_FEC_10MBIT 1 /* Workaround for FEC 100Mbit problem */ #define CONFIG_PHY_ADDR 0x00 diff --git a/include/configs/TOP5200.h b/include/configs/TOP5200.h index bdb7a46d89..ae4677d6a8 100644 --- a/include/configs/TOP5200.h +++ b/include/configs/TOP5200.h @@ -41,7 +41,7 @@ * (easy to change) */ -#define CONFIG_MPC5XXX 1 /* This is an MPC5xxx CPU */ +#define CONFIG_MPC5xxx 1 /* This is an MPC5xxx CPU */ #define CONFIG_MPC5200 1 /* More exactly a MPC5200 */ #define CONFIG_TOP5200 1 /* ... on TOP5200 board - we need this for FEC.C */ @@ -281,7 +281,7 @@ /* * Ethernet configuration */ -#define CONFIG_MPC5XXX_FEC 1 +#define CONFIG_MPC5xxx_FEC 1 #define CONFIG_FEC_10MBIT 1 /* Workaround for FEC 100Mbit problem */ #define CONFIG_PHY_ADDR 0x1f #define CONFIG_PHY_TYPE 0x79c874 diff --git a/include/net.h b/include/net.h index 39debc673d..1b2b347f86 100644 --- a/include/net.h +++ b/include/net.h @@ -21,13 +21,13 @@ # endif #endif /* CONFIG_8xx */ -#if defined(CONFIG_MPC5XXX) +#if defined(CONFIG_MPC5xxx) # if !defined(CONFIG_NET_MULTI) -# if defined(CONFIG_MPC5XXX_FEC) +# if defined(CONFIG_MPC5xxx_FEC) # define CONFIG_NET_MULTI # endif # endif -#endif /* CONFIG_MPC5XXX */ +#endif /* CONFIG_MPC5xxx */ #if !defined(CONFIG_NET_MULTI) && (defined(CONFIG_8260) || defined(CONFIG_MPC8560)) #include @@ -306,7 +306,7 @@ extern int NetState; /* Network loop state */ extern int NetRestartWrap; /* Tried all network devices */ #endif -typedef enum { BOOTP, RARP, ARP, TFTP, DHCP, PING, DNS } proto_t; +typedef enum { BOOTP, RARP, ARP, TFTP, DHCP, PING, DNS, NFS } proto_t; /* from net/net.c */ extern char BootFile[128]; /* Boot File name */ diff --git a/include/ppc_asm.tmpl b/include/ppc_asm.tmpl index 799a726e7d..d4d3b74478 100644 --- a/include/ppc_asm.tmpl +++ b/include/ppc_asm.tmpl @@ -175,7 +175,7 @@ #define IM_IMMR (IM_REGBASE+0x01a8) #define IM_SCCR (IM_REGBASE+0x0c80) -#elif defined(CONFIG_MPC5XXX) +#elif defined(CONFIG_MPC5xxx) #define HID0_ICE_BITPOS 16 #define HID0_DCE_BITPOS 17 diff --git a/lib_ppc/board.c b/lib_ppc/board.c index a6305339ae..a85425c312 100644 --- a/lib_ppc/board.c +++ b/lib_ppc/board.c @@ -32,7 +32,7 @@ #ifdef CONFIG_5xx #include #endif -#ifdef CONFIG_MPC5XXX +#ifdef CONFIG_MPC5xxx #include #endif #if (CONFIG_COMMANDS & CFG_CMD_IDE) @@ -288,9 +288,9 @@ init_fnc_t *init_sequence[] = { prt_8260_clks, #endif /* CONFIG_8260 */ checkcpu, -#if defined(CONFIG_MPC5XXX) +#if defined(CONFIG_MPC5xxx) prt_mpc5xxx_clks, -#endif /* CONFIG_MPC5XXX */ +#endif /* CONFIG_MPC5xxx */ checkboard, INIT_FUNC_WATCHDOG_INIT #if defined(CONFIG_MISC_INIT_F) @@ -478,7 +478,7 @@ void board_init_f (ulong bootflag) defined(CONFIG_E500) bd->bi_immr_base = CFG_IMMR; /* base of IMMR register */ #endif -#if defined(CONFIG_MPC5XXX) +#if defined(CONFIG_MPC5xxx) bd->bi_mbar_base = CFG_MBAR; /* base of internal registers */ #endif @@ -493,10 +493,10 @@ void board_init_f (ulong bootflag) bd->bi_sccfreq = gd->scc_clk; bd->bi_vco = gd->vco_out; #endif /* CONFIG_8260 */ -#if defined(CONFIG_MPC5XXX) +#if defined(CONFIG_MPC5xxx) bd->bi_ipbfreq = gd->ipb_clk; bd->bi_pcifreq = gd->pci_clk; -#endif /* CONFIG_MPC5XXX */ +#endif /* CONFIG_MPC5xxx */ bd->bi_baudrate = gd->baudrate; /* Console Baudrate */ #ifdef CFG_EXTBDINFO diff --git a/net/Makefile b/net/Makefile index b899d7b2f2..cf25a0f33e 100644 --- a/net/Makefile +++ b/net/Makefile @@ -27,7 +27,7 @@ include $(TOPDIR)/config.mk LIB = libnet.a -OBJS = net.o tftp.o bootp.o rarp.o eth.o +OBJS = net.o tftp.o bootp.o rarp.o eth.o nfs.o all: $(LIB) $(LIB): $(START) $(OBJS) diff --git a/net/eth.c b/net/eth.c index 177fab8ba1..7eae469684 100644 --- a/net/eth.c +++ b/net/eth.c @@ -155,7 +155,7 @@ int eth_initialize(bd_t *bis) #if defined(FEC_ENET) || defined(CONFIG_ETHER_ON_FCC) fec_initialize(bis); #endif -#if defined(CONFIG_MPC5XXX_FEC) +#if defined(CONFIG_MPC5xxx_FEC) mpc5xxx_fec_initialize(bis); #endif #if defined(CONFIG_SK98) diff --git a/net/net.c b/net/net.c index 39ffe8c47b..47c2007696 100644 --- a/net/net.c +++ b/net/net.c @@ -55,6 +55,15 @@ * derived from our own IP address) * We want: - load the boot file * Next step: none + * + * NFS: + * + * Prerequisites: - own ethernet address + * - own IP address + * - name of bootfile (if unknown, we use a default name + * derived from our own IP address) + * We want: - load the boot file + * Next step: none */ @@ -65,6 +74,7 @@ #include "bootp.h" #include "tftp.h" #include "rarp.h" +#include "nfs.h" #ifdef CONFIG_STATUS_LED #include #include @@ -278,6 +288,9 @@ restart: */ switch (protocol) { +#if (CONFIG_COMMANDS & CFG_CMD_NFS) + case NFS: +#endif #if (CONFIG_COMMANDS & CFG_CMD_PING) case PING: #endif @@ -287,6 +300,9 @@ restart: NetOurSubnetMask= getenv_IPaddr ("netmask"); switch (protocol) { +#if (CONFIG_COMMANDS & CFG_CMD_NFS) + case NFS: +#endif case TFTP: NetServerIP = getenv_IPaddr ("serverip"); break; @@ -356,6 +372,11 @@ restart: case PING: PingStart(); break; +#endif +#if (CONFIG_COMMANDS & CFG_CMD_NFS) + case NFS: + NfsStart(); + break; #endif default: break; @@ -918,6 +939,9 @@ static int net_check_prereq (proto_t protocol) return (1); } goto common; +#endif +#if (CONFIG_COMMANDS & CFG_CMD_NFS) + case NFS: #endif case TFTP: if (NetServerIP == 0) { diff --git a/net/nfs.c b/net/nfs.c new file mode 100644 index 0000000000..6f4a06ad04 --- /dev/null +++ b/net/nfs.c @@ -0,0 +1,772 @@ +/* + * NFS support driver - based on etherboot and U-BOOT's tftp.c + * + * Masami Komiya 2004 + * + */ + +/* NOTE: the NFS code is heavily inspired by the NetBSD netboot code (read: + * large portions are copied verbatim) as distributed in OSKit 0.97. A few + * changes were necessary to adapt the code to Etherboot and to fix several + * inconsistencies. Also the RPC message preparation is done "by hand" to + * avoid adding netsprintf() which I find hard to understand and use. */ + +/* NOTE 2: Etherboot does not care about things beyond the kernel image, so + * it loads the kernel image off the boot server (ARP_SERVER) and does not + * access the client root disk (root-path in dhcpd.conf), which would use + * ARP_ROOTSERVER. The root disk is something the operating system we are + * about to load needs to use. This is different from the OSKit 0.97 logic. */ + +/* NOTE 3: Symlink handling introduced by Anselm M Hoffmeister, 2003-July-14 + * If a symlink is encountered, it is followed as far as possible (recursion + * possible, maximum 16 steps). There is no clearing of ".."'s inside the + * path, so please DON'T DO THAT. thx. */ + +#include +#include +#include +#include +#include "nfs.h" +#include "bootp.h" + +/*#define NFS_DEBUG*/ + +#if ((CONFIG_COMMANDS & CFG_CMD_NET) && (CONFIG_COMMANDS & CFG_CMD_NFS)) + +#define HASHES_PER_LINE 65 /* Number of "loading" hashes per line */ +#define NFS_TIMEOUT 10 + +static int fs_mounted = 0; +static unsigned long rpc_id; +static int nfs_offset = -1; +static int nfs_len; + +static char dirfh[NFS_FHSIZE]; /* file handle of directory */ +static char filefh[NFS_FHSIZE]; /* file handle of kernel image */ + +static IPaddr_t NfsServerIP; +static int NfsSrvMountPort; +static int NfsSrvNfsPort; +static int NfsOurPort; +static int NfsTimeoutCount; +static int NfsState; +#define STATE_PRCLOOKUP_PROG_MOUNT_REQ 1 +#define STATE_PRCLOOKUP_PROG_NFS_REQ 2 +#define STATE_MOUNT_REQ 3 +#define STATE_UMOUNT_REQ 4 +#define STATE_LOOKUP_REQ 5 +#define STATE_READ_REQ 6 +#define STATE_READLINK_REQ 7 + +static char default_filename[64]; +static char *nfs_filename; +static char *nfs_path; +static char nfs_path_buff[2048]; + +static __inline__ void +store_block (uchar * src, unsigned offset, unsigned len) +{ +#ifdef CFG_DIRECT_FLASH_NFS + int i, rc = 0; + + for (i=0; i= flash_info[i].start[0]) { + rc = 1; + break; + } + } + + if (rc) { /* Flash is destination for this packet */ + rc = flash_write ((uchar *)src, (ulong)(load_addr+offset), len); + if (rc) { + flash_perror (rc); + NetState = NETLOOP_FAIL; + return; + } + } else +#endif /* CFG_DIRECT_FLASH_NFS */ + + { + (void)memcpy ((void *)(load_addr + offset), src, len); + } +} + +static char* +basename (char *path) +{ + char *fname; + + fname = path + strlen(path) - 1; + while (fname >= path) { + if (*fname == '/') { + fname++; + break; + } + fname--; + } + return fname; +} + +static char* +dirname (char *path) +{ + char *fname; + + fname = basename (path); + --fname; + *fname = '\0'; + return path; +} + +/************************************************************************** +RPC_INIT - set up the ID counter to something fairly random +**************************************************************************/ +static void +rpc_init (void) +{ + unsigned long t; + t=get_ticks(); + rpc_id = t ^ (t << 8) ^ (t << 16); +} + +/************************************************************************** +RPC_ADD_CREDENTIALS - Add RPC authentication/verifier entries +**************************************************************************/ +static long *rpc_add_credentials (long *p) +{ + int hl; + int hostnamelen; + char hostname[256]; + + strcpy (hostname, ""); + hostnamelen=strlen (hostname); + + /* Here's the executive summary on authentication requirements of the + * various NFS server implementations: Linux accepts both AUTH_NONE + * and AUTH_UNIX authentication (also accepts an empty hostname field + * in the AUTH_UNIX scheme). *BSD refuses AUTH_NONE, but accepts + * AUTH_UNIX (also accepts an empty hostname field in the AUTH_UNIX + * scheme). To be safe, use AUTH_UNIX and pass the hostname if we have + * it (if the BOOTP/DHCP reply didn't give one, just use an empty + * hostname). */ + + hl = (hostnamelen + 3) & ~3; + + /* Provide an AUTH_UNIX credential. */ + *p++ = htonl(1); /* AUTH_UNIX */ + *p++ = htonl(hl+20); /* auth length */ + *p++ = htonl(0); /* stamp */ + *p++ = htonl(hostnamelen); /* hostname string */ + if (hostnamelen & 3) { + *(p + hostnamelen / 4) = 0; /* add zero padding */ + } + memcpy (p, hostname, hostnamelen); + p += hl / 4; + *p++ = 0; /* uid */ + *p++ = 0; /* gid */ + *p++ = 0; /* auxiliary gid list */ + + /* Provide an AUTH_NONE verifier. */ + *p++ = 0; /* AUTH_NONE */ + *p++ = 0; /* auth length */ + + return p; +} + +/************************************************************************** +RPC_LOOKUP - Lookup RPC Port numbers +**************************************************************************/ +static void +rpc_req (int rpc_prog, int rpc_proc, uint32_t *data, int datalen) +{ + struct rpc_t pkt; + unsigned long id; + uint32_t *p; + int pktlen; + int sport; + + id = rpc_id++; + pkt.u.call.id = htonl(id); + pkt.u.call.type = htonl(MSG_CALL); + pkt.u.call.rpcvers = htonl(2); /* use RPC version 2 */ + pkt.u.call.prog = htonl(rpc_prog); + pkt.u.call.vers = htonl(2); /* portmapper is version 2 */ + pkt.u.call.proc = htonl(rpc_proc); + p = (uint32_t *)&(pkt.u.call.data); + + if (datalen) + memcpy ((char *)p, (char *)data, datalen*sizeof(uint32_t)); + + pktlen = (char *)p + datalen*sizeof(uint32_t) - (char *)&pkt; + + memcpy ((char *)NetTxPacket+ETHER_HDR_SIZE+IP_HDR_SIZE, (char *)&pkt, pktlen); + + if (rpc_prog == PROG_PORTMAP) + sport = SUNRPC_PORT; + else if (rpc_prog == PROG_MOUNT) + sport = NfsSrvMountPort; + else + sport = NfsSrvNfsPort; + + NetSendUDPPacket (NetServerEther, NfsServerIP, sport, NfsOurPort, pktlen); +} + +/************************************************************************** +RPC_LOOKUP - Lookup RPC Port numbers +**************************************************************************/ +static void +rpc_lookup_req (int prog, int ver) +{ + uint32_t data[16]; + + data[0] = 0; data[1] = 0; /* auth credential */ + data[2] = 0; data[3] = 0; /* auth verifier */ + data[4] = htonl(prog); + data[5] = htonl(ver); + data[6] = htonl(17); /* IP_UDP */ + data[7] = 0; + + rpc_req (PROG_PORTMAP, PORTMAP_GETPORT, data, 8); +} + +/************************************************************************** +NFS_MOUNT - Mount an NFS Filesystem +**************************************************************************/ +static void +nfs_mount_req (char *path) +{ + uint32_t data[1024]; + uint32_t *p; + int len; + int pathlen; + + pathlen = strlen (path); + + p = &(data[0]); + p = (uint32_t *)rpc_add_credentials((long *)p); + + *p++ = htonl(pathlen); + if (pathlen & 3) *(p + pathlen / 4) = 0; + memcpy (p, path, pathlen); + p += (pathlen + 3) / 4; + + len = (uint32_t *)p - (uint32_t *)&(data[0]); + + rpc_req (PROG_MOUNT, MOUNT_ADDENTRY, data, len); +} + +/************************************************************************** +NFS_UMOUNTALL - Unmount all our NFS Filesystems on the Server +**************************************************************************/ +static void +nfs_umountall_req (void) +{ + uint32_t data[1024]; + uint32_t *p; + int len; + + if ((NfsSrvMountPort == -1) || (!fs_mounted)) { + /* Nothing mounted, nothing to umount */ + return; + } + + p = &(data[0]); + p = (uint32_t *)rpc_add_credentials ((long *)p); + + len = (uint32_t *)p - (uint32_t *)&(data[0]); + + rpc_req (PROG_MOUNT, MOUNT_UMOUNTALL, data, len); +} + +/*************************************************************************** + * NFS_READLINK (AH 2003-07-14) + * This procedure is called when read of the first block fails - + * this probably happens when it's a directory or a symlink + * In case of successful readlink(), the dirname is manipulated, + * so that inside the nfs() function a recursion can be done. + **************************************************************************/ +static void +nfs_readlink_req (void) +{ + uint32_t data[1024]; + uint32_t *p; + int len; + + p = &(data[0]); + p = (uint32_t *)rpc_add_credentials ((long *)p); + + memcpy (p, filefh, NFS_FHSIZE); + p += (NFS_FHSIZE / 4); + + len = (uint32_t *)p - (uint32_t *)&(data[0]); + + rpc_req (PROG_NFS, NFS_READLINK, data, len); +} + +/************************************************************************** +NFS_LOOKUP - Lookup Pathname +**************************************************************************/ +static void +nfs_lookup_req (char *fname) +{ + uint32_t data[1024]; + uint32_t *p; + int len; + int fnamelen; + + fnamelen = strlen (fname); + + p = &(data[0]); + p = (uint32_t *)rpc_add_credentials ((long *)p); + + memcpy (p, dirfh, NFS_FHSIZE); + p += (NFS_FHSIZE / 4); + *p++ = htonl(fnamelen); + if (fnamelen & 3) *(p + fnamelen / 4) = 0; + memcpy (p, fname, fnamelen); + p += (fnamelen + 3) / 4; + + len = (uint32_t *)p - (uint32_t *)&(data[0]); + + rpc_req (PROG_NFS, NFS_LOOKUP, data, len); +} + +/************************************************************************** +NFS_READ - Read File on NFS Server +**************************************************************************/ +static void +nfs_read_req (int offset, int readlen) +{ + uint32_t data[1024]; + uint32_t *p; + int len; + + p = &(data[0]); + p = (uint32_t *)rpc_add_credentials ((long *)p); + + memcpy (p, filefh, NFS_FHSIZE); + p += (NFS_FHSIZE / 4); + *p++ = htonl(offset); + *p++ = htonl(readlen); + *p++ = 0; + + len = (uint32_t *)p - (uint32_t *)&(data[0]); + + rpc_req (PROG_NFS, NFS_READ, data, len); +} + +/************************************************************************** +RPC request dispatcher +**************************************************************************/ + +static void +NfsSend (void) +{ +#ifdef NFS_DEBUG + printf ("%s\n", __FUNCTION__); +#endif + + switch (NfsState) { + case STATE_PRCLOOKUP_PROG_MOUNT_REQ: + rpc_lookup_req (PROG_MOUNT, 1); + break; + case STATE_PRCLOOKUP_PROG_NFS_REQ: + rpc_lookup_req (PROG_NFS, 2); + break; + case STATE_MOUNT_REQ: + nfs_mount_req (nfs_path); + break; + case STATE_UMOUNT_REQ: + nfs_umountall_req (); + break; + case STATE_LOOKUP_REQ: + nfs_lookup_req (nfs_filename); + break; + case STATE_READ_REQ: + nfs_read_req (nfs_offset, nfs_len); + break; + case STATE_READLINK_REQ: + nfs_readlink_req (); + break; + } +} + +/************************************************************************** +Handlers for the reply from server +**************************************************************************/ + +static int +rpc_lookup_reply (int prog, uchar *pkt, unsigned len) +{ + struct rpc_t rpc_pkt; + + memcpy ((unsigned char *)&rpc_pkt, pkt, len); + +#ifdef NFS_DEBUG + printf ("%s\n", __FUNCTION__); +#endif + + if (rpc_pkt.u.reply.rstatus || + rpc_pkt.u.reply.verifier || + rpc_pkt.u.reply.astatus || + rpc_pkt.u.reply.astatus) { + } + + switch (prog) { + case PROG_MOUNT: + NfsSrvMountPort = ntohl(rpc_pkt.u.reply.data[0]); + break; + case PROG_NFS: + NfsSrvNfsPort = ntohl(rpc_pkt.u.reply.data[0]); + break; + } + + return 0; +} + +static int +nfs_mount_reply (uchar *pkt, unsigned len) +{ + struct rpc_t rpc_pkt; + +#ifdef NFS_DEBUG + printf ("%s\n", __FUNCTION__); +#endif + + memcpy ((unsigned char *)&rpc_pkt, pkt, len); + + if (rpc_pkt.u.reply.rstatus || + rpc_pkt.u.reply.verifier || + rpc_pkt.u.reply.astatus || + rpc_pkt.u.reply.data[0]) { + return -1; + } + + fs_mounted = 1; + memcpy (dirfh, rpc_pkt.u.reply.data + 1, NFS_FHSIZE); + + return 0; +} + +static int +nfs_umountall_reply (uchar *pkt, unsigned len) +{ + struct rpc_t rpc_pkt; + +#ifdef NFS_DEBUG + printf ("%s\n", __FUNCTION__); +#endif + + memcpy ((unsigned char *)&rpc_pkt, pkt, len); + + if (rpc_pkt.u.reply.rstatus || + rpc_pkt.u.reply.verifier || + rpc_pkt.u.reply.astatus) { + return -1; + } + + fs_mounted = 0; + memset (dirfh, 0, sizeof(dirfh)); + + return 0; +} + +static int +nfs_lookup_reply (uchar *pkt, unsigned len) +{ + struct rpc_t rpc_pkt; + +#ifdef NFS_DEBUG + printf ("%s\n", __FUNCTION__); +#endif + + memcpy ((unsigned char *)&rpc_pkt, pkt, len); + + if (rpc_pkt.u.reply.rstatus || + rpc_pkt.u.reply.verifier || + rpc_pkt.u.reply.astatus || + rpc_pkt.u.reply.data[0]) { + return -1; + } + + memcpy (filefh, rpc_pkt.u.reply.data + 1, NFS_FHSIZE); + + return 0; +} + +static int +nfs_readlink_reply (uchar *pkt, unsigned len) +{ + struct rpc_t rpc_pkt; + int rlen; + +#ifdef NFS_DEBUG + printf ("%s\n", __FUNCTION__); +#endif + + memcpy ((unsigned char *)&rpc_pkt, pkt, len); + + if (rpc_pkt.u.reply.rstatus || + rpc_pkt.u.reply.verifier || + rpc_pkt.u.reply.astatus || + rpc_pkt.u.reply.data[0]) { + return -1; + } + + rlen = ntohl (rpc_pkt.u.reply.data[1]); /* new path length */ + + if (*((char *)&(rpc_pkt.u.reply.data[2])) != '/') { + int pathlen; + strcat (nfs_path, "/"); + pathlen = strlen(nfs_path); + memcpy (nfs_path+pathlen, (uchar *)&(rpc_pkt.u.reply.data[2]), rlen); + nfs_path[pathlen+rlen+1] = 0; + } else { + memcpy (nfs_path, (uchar *)&(rpc_pkt.u.reply.data[2]), rlen); + nfs_path[rlen] = 0; + } + return 0; +} + +static int +nfs_read_reply (uchar *pkt, unsigned len) +{ + struct rpc_t rpc_pkt; + int rlen; + +#ifdef NFS_DEBUG_nop + printf ("%s\n", __FUNCTION__); +#endif + + rlen = (uchar *)&(rpc_pkt.u.reply.data[19]) - (uchar *)&(rpc_pkt.u.reply.id); + memcpy ((uchar *)&rpc_pkt, pkt, rlen); + + if (rpc_pkt.u.reply.rstatus || + rpc_pkt.u.reply.verifier || + rpc_pkt.u.reply.astatus || + rpc_pkt.u.reply.data[0]) { + if (rpc_pkt.u.reply.rstatus) { + return -9999; + } + if (rpc_pkt.u.reply.astatus) { + return -9999; + } + return -ntohl(rpc_pkt.u.reply.data[0]);; + } + + if ((nfs_offset!=0) && !((nfs_offset) % (NFS_READ_SIZE/2*10*HASHES_PER_LINE))) { + puts ("\n\t "); + } + if (!(nfs_offset % ((NFS_READ_SIZE/2)*10))) { + putc ('#'); + } + + rlen = ntohl(rpc_pkt.u.reply.data[18]); + store_block ((uchar *)&(rpc_pkt.u.reply.data[19]), nfs_offset, rlen); + + return rlen; +} + +/************************************************************************** +Interfaces of U-BOOT +**************************************************************************/ + +static void +NfsHandler (uchar *pkt, unsigned dest, unsigned src, unsigned len) +{ + int rlen; + +#ifdef NFS_DEBUG + printf ("%s\n", __FUNCTION__); +#endif + + if (dest != NfsOurPort) return; + + switch (NfsState) { + case STATE_PRCLOOKUP_PROG_MOUNT_REQ: + rpc_lookup_reply (PROG_MOUNT, pkt, len); + NfsState = STATE_PRCLOOKUP_PROG_NFS_REQ; + NfsSend (); + break; + + case STATE_PRCLOOKUP_PROG_NFS_REQ: + rpc_lookup_reply (PROG_NFS, pkt, len); + NfsState = STATE_MOUNT_REQ; + NfsSend (); + break; + + case STATE_MOUNT_REQ: + if (nfs_mount_reply(pkt, len)) { + puts ("*** ERROR: Cannot mount\n"); + /* just to be sure... */ + NfsState = STATE_UMOUNT_REQ; + NfsSend (); + } else { + NfsState = STATE_LOOKUP_REQ; + NfsSend (); + } + break; + + case STATE_UMOUNT_REQ: + if (nfs_umountall_reply(pkt, len)) { + puts ("*** ERROR: Cannot umount\n"); + NetState = NETLOOP_FAIL; + } else { + NetState = NETLOOP_SUCCESS; + } + break; + + case STATE_LOOKUP_REQ: + if (nfs_lookup_reply(pkt, len)) { + puts ("*** ERROR: File lookup fail\n"); + NfsState = STATE_UMOUNT_REQ; + NfsSend (); + } else { + NfsState = STATE_READ_REQ; + nfs_offset = 0; + nfs_len = NFS_READ_SIZE; + NfsSend (); + } + break; + + case STATE_READLINK_REQ: + if (nfs_readlink_reply(pkt, len)) { + puts ("*** ERROR: Symlink fail\n"); + NfsState = STATE_UMOUNT_REQ; + NfsSend (); + } else { +#ifdef NFS_DEBUG + printf ("Symlink --> %s\n", nfs_path); +#endif + nfs_filename = basename (nfs_path); + nfs_path = dirname (nfs_path); + + NfsState = STATE_MOUNT_REQ; + NfsSend (); + } + break; + + case STATE_READ_REQ: + rlen = nfs_read_reply (pkt, len); + if (rlen > 0) { + nfs_offset += rlen; + NfsSend (); + } + else if ((rlen == -NFSERR_ISDIR)||(rlen == -NFSERR_INVAL)) { + /* symbolic link */ + NfsState = STATE_READLINK_REQ; + NfsSend (); + } else { + puts ("\ndone\n"); + printf ("Bytes transferred = %d (%x hex)\n", + nfs_offset, nfs_offset); + NfsState = STATE_UMOUNT_REQ; + NfsSend (); + } + break; + } +} + +static void +NfsTimeout (void) +{ + puts ("Timeout\n"); + NetState = NETLOOP_FAIL; + return; +} + +void +NfsStart (void) +{ +#ifdef NFS_DEBUG + printf ("%s\n", __FUNCTION__); +#endif + + NfsServerIP = NetServerIP; + nfs_path = (char *)nfs_path_buff; + + if (nfs_path == NULL) { + NetState = NETLOOP_FAIL; + puts ("*** ERROR: Fail allocate memory\n"); + return; + } + + if (BootFile[0] == '\0') { + IPaddr_t OurIP = ntohl (NetOurIP); + + sprintf (default_filename, "/nfsroot/%02lX%02lX%02lX%02lX.img", + OurIP & 0xFF, + (OurIP >> 8) & 0xFF, + (OurIP >> 16) & 0xFF, + (OurIP >> 24) & 0xFF ); + strcpy (nfs_path, default_filename); + + printf ("*** Warning: no boot file name; using '%s'\n", + nfs_path); + } else { + char *p=BootFile; + + p = strchr (p, ':'); + + if (p != NULL) { + NfsServerIP = string_to_ip (BootFile); + ++p; + strcpy (nfs_path, p); + } else { + strcpy (nfs_path, BootFile); + } + } + + nfs_filename = basename (nfs_path); + nfs_path = dirname (nfs_path); + +#if defined(CONFIG_NET_MULTI) + printf ("Using %s device\n", eth_get_name()); +#endif + + puts ("File transfer via NFS from server "); print_IPaddr (NfsServerIP); + puts ("; our IP address is "); print_IPaddr (NetOurIP); + + /* Check if we need to send across this subnet */ + if (NetOurGatewayIP && NetOurSubnetMask) { + IPaddr_t OurNet = NetOurIP & NetOurSubnetMask; + IPaddr_t ServerNet = NetServerIP & NetOurSubnetMask; + + if (OurNet != ServerNet) { + puts ("; sending through gateway "); + print_IPaddr (NetOurGatewayIP) ; + } + } + putc ('\n'); + + printf ("Filename '%s/%s'.", nfs_path, nfs_filename); + + if (NetBootFileSize) { + printf (" Size is 0x%x Bytes = ", NetBootFileSize<<9); + print_size (NetBootFileSize<<9, ""); + } + putc ('\n'); + + printf ("Load address: 0x%lx\n", load_addr); + + printf ("Loading: *\b"); + + NetSetTimeout (NFS_TIMEOUT * CFG_HZ, NfsTimeout); + NetSetHandler (NfsHandler); + + rpc_init (); + NfsTimeoutCount = 0; + NfsState = STATE_PRCLOOKUP_PROG_MOUNT_REQ; + + /*NfsOurPort = 4096 + (get_ticks() % 3072);*/ + /*FIX ME !!!*/ + NfsOurPort = 1000; + + /* zero out server ether in case the server ip has changed */ + memset (NetServerEther, 0, 6); + + NfsSend (); +} + +#endif /* CONFIG_COMMANDS & CFG_CMD_NFS */ diff --git a/net/nfs.h b/net/nfs.h new file mode 100644 index 0000000000..ebd4266857 --- /dev/null +++ b/net/nfs.h @@ -0,0 +1,74 @@ +/* + * (C) Masami Komiya 2004 + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2, or (at + * your option) any later version. + */ + +#ifndef __NFS_H__ +#define __NFS_H__ + +#define SUNRPC_PORT 111 + +#define PROG_PORTMAP 100000 +#define PROG_NFS 100003 +#define PROG_MOUNT 100005 + +#define MSG_CALL 0 +#define MSG_REPLY 1 + +#define PORTMAP_GETPORT 3 + +#define MOUNT_ADDENTRY 1 +#define MOUNT_UMOUNTALL 4 + +#define NFS_LOOKUP 4 +#define NFS_READLINK 5 +#define NFS_READ 6 + +#define NFS_FHSIZE 32 + +#define NFSERR_PERM 1 +#define NFSERR_NOENT 2 +#define NFSERR_ACCES 13 +#define NFSERR_ISDIR 21 +#define NFSERR_INVAL 22 + +/* Block size used for NFS read accesses. A RPC reply packet (including all + * headers) must fit within a single Ethernet frame to avoid fragmentation. + * Chosen to be a power of two, as most NFS servers are optimized for this. */ +#define NFS_READ_SIZE 1024 + +#define NFS_MAXLINKDEPTH 16 + +struct rpc_t { + union { + uint8_t data[2048]; + struct { + uint32_t id; + uint32_t type; + uint32_t rpcvers; + uint32_t prog; + uint32_t vers; + uint32_t proc; + uint32_t data[1]; + } call; + struct { + uint32_t id; + uint32_t type; + uint32_t rstatus; + uint32_t verifier; + uint32_t v2; + uint32_t astatus; + uint32_t data[1]; + } reply; + } u; +}; +extern void NfsStart (void); /* Begin NFS */ + + +/**********************************************************************/ + +#endif /* __NFS_H__ */ -- 2.39.5