wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 1 | /* |
| 2 | * NFS support driver - based on etherboot and U-BOOT's tftp.c |
| 3 | * |
| 4 | * Masami Komiya <mkomiya@sonare.it> 2004 |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | /* NOTE: the NFS code is heavily inspired by the NetBSD netboot code (read: |
| 9 | * large portions are copied verbatim) as distributed in OSKit 0.97. A few |
| 10 | * changes were necessary to adapt the code to Etherboot and to fix several |
| 11 | * inconsistencies. Also the RPC message preparation is done "by hand" to |
| 12 | * avoid adding netsprintf() which I find hard to understand and use. */ |
| 13 | |
| 14 | /* NOTE 2: Etherboot does not care about things beyond the kernel image, so |
| 15 | * it loads the kernel image off the boot server (ARP_SERVER) and does not |
| 16 | * access the client root disk (root-path in dhcpd.conf), which would use |
| 17 | * ARP_ROOTSERVER. The root disk is something the operating system we are |
| 18 | * about to load needs to use. This is different from the OSKit 0.97 logic. */ |
| 19 | |
| 20 | /* NOTE 3: Symlink handling introduced by Anselm M Hoffmeister, 2003-July-14 |
| 21 | * If a symlink is encountered, it is followed as far as possible (recursion |
| 22 | * possible, maximum 16 steps). There is no clearing of ".."'s inside the |
| 23 | * path, so please DON'T DO THAT. thx. */ |
| 24 | |
| 25 | #include <common.h> |
| 26 | #include <command.h> |
| 27 | #include <net.h> |
| 28 | #include <malloc.h> |
Joe Hershberger | ed19185 | 2015-03-22 17:09:08 -0500 | [diff] [blame] | 29 | #include <mapmem.h> |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 30 | #include "nfs.h" |
| 31 | #include "bootp.h" |
| 32 | |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 33 | #define HASHES_PER_LINE 65 /* Number of "loading" hashes per line */ |
Hiroshi Ito | 3702cd7 | 2008-01-31 18:35:04 +0900 | [diff] [blame] | 34 | #define NFS_RETRY_COUNT 30 |
Tetsuyuki Kobayashi | 147e390 | 2012-07-03 22:25:21 +0000 | [diff] [blame] | 35 | #ifndef CONFIG_NFS_TIMEOUT |
| 36 | # define NFS_TIMEOUT 2000UL |
| 37 | #else |
| 38 | # define NFS_TIMEOUT CONFIG_NFS_TIMEOUT |
| 39 | #endif |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 40 | |
Matthias Brugger | b8d5b8c | 2012-12-11 19:14:16 +0100 | [diff] [blame] | 41 | #define NFS_RPC_ERR 1 |
| 42 | #define NFS_RPC_DROP 124 |
| 43 | |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 44 | static int fs_mounted; |
| 45 | static unsigned long rpc_id; |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 46 | static int nfs_offset = -1; |
| 47 | static int nfs_len; |
Matthias Brugger | b8d5b8c | 2012-12-11 19:14:16 +0100 | [diff] [blame] | 48 | static ulong nfs_timeout = NFS_TIMEOUT; |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 49 | |
| 50 | static char dirfh[NFS_FHSIZE]; /* file handle of directory */ |
| 51 | static char filefh[NFS_FHSIZE]; /* file handle of kernel image */ |
| 52 | |
Joe Hershberger | d4bb76a | 2012-05-23 07:59:14 +0000 | [diff] [blame] | 53 | static enum net_loop_state nfs_download_state; |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 54 | static struct in_addr nfs_server_ip; |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 55 | static int nfs_server_mount_port; |
| 56 | static int nfs_server_port; |
| 57 | static int nfs_our_port; |
| 58 | static int nfs_timeout_count; |
| 59 | static int nfs_state; |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 60 | #define STATE_PRCLOOKUP_PROG_MOUNT_REQ 1 |
| 61 | #define STATE_PRCLOOKUP_PROG_NFS_REQ 2 |
| 62 | #define STATE_MOUNT_REQ 3 |
| 63 | #define STATE_UMOUNT_REQ 4 |
| 64 | #define STATE_LOOKUP_REQ 5 |
| 65 | #define STATE_READ_REQ 6 |
| 66 | #define STATE_READLINK_REQ 7 |
| 67 | |
| 68 | static char default_filename[64]; |
| 69 | static char *nfs_filename; |
| 70 | static char *nfs_path; |
| 71 | static char nfs_path_buff[2048]; |
| 72 | |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 73 | static inline int store_block(uchar *src, unsigned offset, unsigned len) |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 74 | { |
wdenk | 77ef4c6 | 2004-02-24 22:33:21 +0000 | [diff] [blame] | 75 | ulong newsize = offset + len; |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 76 | #ifdef CONFIG_SYS_DIRECT_FLASH_NFS |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 77 | int i, rc = 0; |
| 78 | |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 79 | for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) { |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 80 | /* start address in flash? */ |
| 81 | if (load_addr + offset >= flash_info[i].start[0]) { |
| 82 | rc = 1; |
| 83 | break; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | if (rc) { /* Flash is destination for this packet */ |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 88 | rc = flash_write((uchar *)src, (ulong)(load_addr+offset), len); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 89 | if (rc) { |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 90 | flash_perror(rc); |
Wolfgang Denk | b077b50 | 2005-08-06 01:11:12 +0200 | [diff] [blame] | 91 | return -1; |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 92 | } |
| 93 | } else |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 94 | #endif /* CONFIG_SYS_DIRECT_FLASH_NFS */ |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 95 | { |
Joe Hershberger | ed19185 | 2015-03-22 17:09:08 -0500 | [diff] [blame] | 96 | void *ptr = map_sysmem(load_addr + offset, len); |
| 97 | |
| 98 | memcpy(ptr, src, len); |
| 99 | unmap_sysmem(ptr); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 100 | } |
wdenk | 77ef4c6 | 2004-02-24 22:33:21 +0000 | [diff] [blame] | 101 | |
Joe Hershberger | 290c899 | 2015-04-08 01:41:02 -0500 | [diff] [blame] | 102 | if (net_boot_file_size < (offset + len)) |
| 103 | net_boot_file_size = newsize; |
Wolfgang Denk | b077b50 | 2005-08-06 01:11:12 +0200 | [diff] [blame] | 104 | return 0; |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 107 | static char *basename(char *path) |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 108 | { |
| 109 | char *fname; |
| 110 | |
| 111 | fname = path + strlen(path) - 1; |
| 112 | while (fname >= path) { |
| 113 | if (*fname == '/') { |
| 114 | fname++; |
| 115 | break; |
| 116 | } |
| 117 | fname--; |
| 118 | } |
| 119 | return fname; |
| 120 | } |
| 121 | |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 122 | static char *dirname(char *path) |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 123 | { |
| 124 | char *fname; |
| 125 | |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 126 | fname = basename(path); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 127 | --fname; |
| 128 | *fname = '\0'; |
| 129 | return path; |
| 130 | } |
| 131 | |
| 132 | /************************************************************************** |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 133 | RPC_ADD_CREDENTIALS - Add RPC authentication/verifier entries |
| 134 | **************************************************************************/ |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 135 | static long *rpc_add_credentials(long *p) |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 136 | { |
| 137 | int hl; |
| 138 | int hostnamelen; |
| 139 | char hostname[256]; |
| 140 | |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 141 | strcpy(hostname, ""); |
| 142 | hostnamelen = strlen(hostname); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 143 | |
| 144 | /* Here's the executive summary on authentication requirements of the |
| 145 | * various NFS server implementations: Linux accepts both AUTH_NONE |
| 146 | * and AUTH_UNIX authentication (also accepts an empty hostname field |
| 147 | * in the AUTH_UNIX scheme). *BSD refuses AUTH_NONE, but accepts |
| 148 | * AUTH_UNIX (also accepts an empty hostname field in the AUTH_UNIX |
| 149 | * scheme). To be safe, use AUTH_UNIX and pass the hostname if we have |
| 150 | * it (if the BOOTP/DHCP reply didn't give one, just use an empty |
| 151 | * hostname). */ |
| 152 | |
| 153 | hl = (hostnamelen + 3) & ~3; |
| 154 | |
| 155 | /* Provide an AUTH_UNIX credential. */ |
| 156 | *p++ = htonl(1); /* AUTH_UNIX */ |
| 157 | *p++ = htonl(hl+20); /* auth length */ |
| 158 | *p++ = htonl(0); /* stamp */ |
| 159 | *p++ = htonl(hostnamelen); /* hostname string */ |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 160 | if (hostnamelen & 3) |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 161 | *(p + hostnamelen / 4) = 0; /* add zero padding */ |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 162 | memcpy(p, hostname, hostnamelen); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 163 | p += hl / 4; |
| 164 | *p++ = 0; /* uid */ |
| 165 | *p++ = 0; /* gid */ |
| 166 | *p++ = 0; /* auxiliary gid list */ |
| 167 | |
| 168 | /* Provide an AUTH_NONE verifier. */ |
| 169 | *p++ = 0; /* AUTH_NONE */ |
| 170 | *p++ = 0; /* auth length */ |
| 171 | |
| 172 | return p; |
| 173 | } |
| 174 | |
| 175 | /************************************************************************** |
| 176 | RPC_LOOKUP - Lookup RPC Port numbers |
| 177 | **************************************************************************/ |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 178 | static void rpc_req(int rpc_prog, int rpc_proc, uint32_t *data, int datalen) |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 179 | { |
| 180 | struct rpc_t pkt; |
| 181 | unsigned long id; |
| 182 | uint32_t *p; |
| 183 | int pktlen; |
| 184 | int sport; |
| 185 | |
wdenk | acd9b10 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 186 | id = ++rpc_id; |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 187 | pkt.u.call.id = htonl(id); |
| 188 | pkt.u.call.type = htonl(MSG_CALL); |
| 189 | pkt.u.call.rpcvers = htonl(2); /* use RPC version 2 */ |
| 190 | pkt.u.call.prog = htonl(rpc_prog); |
| 191 | pkt.u.call.vers = htonl(2); /* portmapper is version 2 */ |
| 192 | pkt.u.call.proc = htonl(rpc_proc); |
| 193 | p = (uint32_t *)&(pkt.u.call.data); |
| 194 | |
| 195 | if (datalen) |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 196 | memcpy((char *)p, (char *)data, datalen*sizeof(uint32_t)); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 197 | |
| 198 | pktlen = (char *)p + datalen*sizeof(uint32_t) - (char *)&pkt; |
| 199 | |
Joe Hershberger | a8ca4f6 | 2015-04-08 01:41:05 -0500 | [diff] [blame] | 200 | memcpy((char *)net_tx_packet + net_eth_hdr_size() + IP_UDP_HDR_SIZE, |
| 201 | (char *)&pkt, pktlen); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 202 | |
| 203 | if (rpc_prog == PROG_PORTMAP) |
| 204 | sport = SUNRPC_PORT; |
| 205 | else if (rpc_prog == PROG_MOUNT) |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 206 | sport = nfs_server_mount_port; |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 207 | else |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 208 | sport = nfs_server_port; |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 209 | |
Joe Hershberger | a8ca4f6 | 2015-04-08 01:41:05 -0500 | [diff] [blame] | 210 | net_send_udp_packet(net_server_ethaddr, nfs_server_ip, sport, |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 211 | nfs_our_port, pktlen); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | /************************************************************************** |
| 215 | RPC_LOOKUP - Lookup RPC Port numbers |
| 216 | **************************************************************************/ |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 217 | static void rpc_lookup_req(int prog, int ver) |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 218 | { |
| 219 | uint32_t data[16]; |
| 220 | |
| 221 | data[0] = 0; data[1] = 0; /* auth credential */ |
| 222 | data[2] = 0; data[3] = 0; /* auth verifier */ |
| 223 | data[4] = htonl(prog); |
| 224 | data[5] = htonl(ver); |
| 225 | data[6] = htonl(17); /* IP_UDP */ |
| 226 | data[7] = 0; |
| 227 | |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 228 | rpc_req(PROG_PORTMAP, PORTMAP_GETPORT, data, 8); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | /************************************************************************** |
| 232 | NFS_MOUNT - Mount an NFS Filesystem |
| 233 | **************************************************************************/ |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 234 | static void nfs_mount_req(char *path) |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 235 | { |
| 236 | uint32_t data[1024]; |
| 237 | uint32_t *p; |
| 238 | int len; |
| 239 | int pathlen; |
| 240 | |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 241 | pathlen = strlen(path); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 242 | |
| 243 | p = &(data[0]); |
| 244 | p = (uint32_t *)rpc_add_credentials((long *)p); |
| 245 | |
| 246 | *p++ = htonl(pathlen); |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 247 | if (pathlen & 3) |
| 248 | *(p + pathlen / 4) = 0; |
| 249 | memcpy(p, path, pathlen); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 250 | p += (pathlen + 3) / 4; |
| 251 | |
| 252 | len = (uint32_t *)p - (uint32_t *)&(data[0]); |
| 253 | |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 254 | rpc_req(PROG_MOUNT, MOUNT_ADDENTRY, data, len); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | /************************************************************************** |
| 258 | NFS_UMOUNTALL - Unmount all our NFS Filesystems on the Server |
| 259 | **************************************************************************/ |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 260 | static void nfs_umountall_req(void) |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 261 | { |
| 262 | uint32_t data[1024]; |
| 263 | uint32_t *p; |
| 264 | int len; |
| 265 | |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 266 | if ((nfs_server_mount_port == -1) || (!fs_mounted)) |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 267 | /* Nothing mounted, nothing to umount */ |
| 268 | return; |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 269 | |
| 270 | p = &(data[0]); |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 271 | p = (uint32_t *)rpc_add_credentials((long *)p); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 272 | |
| 273 | len = (uint32_t *)p - (uint32_t *)&(data[0]); |
| 274 | |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 275 | rpc_req(PROG_MOUNT, MOUNT_UMOUNTALL, data, len); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | /*************************************************************************** |
| 279 | * NFS_READLINK (AH 2003-07-14) |
| 280 | * This procedure is called when read of the first block fails - |
| 281 | * this probably happens when it's a directory or a symlink |
| 282 | * In case of successful readlink(), the dirname is manipulated, |
| 283 | * so that inside the nfs() function a recursion can be done. |
| 284 | **************************************************************************/ |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 285 | static void nfs_readlink_req(void) |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 286 | { |
| 287 | uint32_t data[1024]; |
| 288 | uint32_t *p; |
| 289 | int len; |
| 290 | |
| 291 | p = &(data[0]); |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 292 | p = (uint32_t *)rpc_add_credentials((long *)p); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 293 | |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 294 | memcpy(p, filefh, NFS_FHSIZE); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 295 | p += (NFS_FHSIZE / 4); |
| 296 | |
| 297 | len = (uint32_t *)p - (uint32_t *)&(data[0]); |
| 298 | |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 299 | rpc_req(PROG_NFS, NFS_READLINK, data, len); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | /************************************************************************** |
| 303 | NFS_LOOKUP - Lookup Pathname |
| 304 | **************************************************************************/ |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 305 | static void nfs_lookup_req(char *fname) |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 306 | { |
| 307 | uint32_t data[1024]; |
| 308 | uint32_t *p; |
| 309 | int len; |
| 310 | int fnamelen; |
| 311 | |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 312 | fnamelen = strlen(fname); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 313 | |
| 314 | p = &(data[0]); |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 315 | p = (uint32_t *)rpc_add_credentials((long *)p); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 316 | |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 317 | memcpy(p, dirfh, NFS_FHSIZE); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 318 | p += (NFS_FHSIZE / 4); |
| 319 | *p++ = htonl(fnamelen); |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 320 | if (fnamelen & 3) |
| 321 | *(p + fnamelen / 4) = 0; |
| 322 | memcpy(p, fname, fnamelen); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 323 | p += (fnamelen + 3) / 4; |
| 324 | |
| 325 | len = (uint32_t *)p - (uint32_t *)&(data[0]); |
| 326 | |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 327 | rpc_req(PROG_NFS, NFS_LOOKUP, data, len); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | /************************************************************************** |
| 331 | NFS_READ - Read File on NFS Server |
| 332 | **************************************************************************/ |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 333 | static void nfs_read_req(int offset, int readlen) |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 334 | { |
| 335 | uint32_t data[1024]; |
| 336 | uint32_t *p; |
| 337 | int len; |
| 338 | |
| 339 | p = &(data[0]); |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 340 | p = (uint32_t *)rpc_add_credentials((long *)p); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 341 | |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 342 | memcpy(p, filefh, NFS_FHSIZE); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 343 | p += (NFS_FHSIZE / 4); |
| 344 | *p++ = htonl(offset); |
| 345 | *p++ = htonl(readlen); |
| 346 | *p++ = 0; |
| 347 | |
| 348 | len = (uint32_t *)p - (uint32_t *)&(data[0]); |
| 349 | |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 350 | rpc_req(PROG_NFS, NFS_READ, data, len); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | /************************************************************************** |
| 354 | RPC request dispatcher |
| 355 | **************************************************************************/ |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 356 | static void nfs_send(void) |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 357 | { |
Robin Getz | 9e0a4d6 | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 358 | debug("%s\n", __func__); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 359 | |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 360 | switch (nfs_state) { |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 361 | case STATE_PRCLOOKUP_PROG_MOUNT_REQ: |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 362 | rpc_lookup_req(PROG_MOUNT, 1); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 363 | break; |
| 364 | case STATE_PRCLOOKUP_PROG_NFS_REQ: |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 365 | rpc_lookup_req(PROG_NFS, 2); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 366 | break; |
| 367 | case STATE_MOUNT_REQ: |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 368 | nfs_mount_req(nfs_path); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 369 | break; |
| 370 | case STATE_UMOUNT_REQ: |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 371 | nfs_umountall_req(); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 372 | break; |
| 373 | case STATE_LOOKUP_REQ: |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 374 | nfs_lookup_req(nfs_filename); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 375 | break; |
| 376 | case STATE_READ_REQ: |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 377 | nfs_read_req(nfs_offset, nfs_len); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 378 | break; |
| 379 | case STATE_READLINK_REQ: |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 380 | nfs_readlink_req(); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 381 | break; |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | /************************************************************************** |
| 386 | Handlers for the reply from server |
| 387 | **************************************************************************/ |
| 388 | |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 389 | static int rpc_lookup_reply(int prog, uchar *pkt, unsigned len) |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 390 | { |
| 391 | struct rpc_t rpc_pkt; |
| 392 | |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 393 | memcpy((unsigned char *)&rpc_pkt, pkt, len); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 394 | |
Robin Getz | 9e0a4d6 | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 395 | debug("%s\n", __func__); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 396 | |
Matthias Brugger | b8d5b8c | 2012-12-11 19:14:16 +0100 | [diff] [blame] | 397 | if (ntohl(rpc_pkt.u.reply.id) > rpc_id) |
| 398 | return -NFS_RPC_ERR; |
| 399 | else if (ntohl(rpc_pkt.u.reply.id) < rpc_id) |
| 400 | return -NFS_RPC_DROP; |
wdenk | acd9b10 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 401 | |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 402 | if (rpc_pkt.u.reply.rstatus || |
| 403 | rpc_pkt.u.reply.verifier || |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 404 | rpc_pkt.u.reply.astatus) |
wdenk | acd9b10 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 405 | return -1; |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 406 | |
| 407 | switch (prog) { |
| 408 | case PROG_MOUNT: |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 409 | nfs_server_mount_port = ntohl(rpc_pkt.u.reply.data[0]); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 410 | break; |
| 411 | case PROG_NFS: |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 412 | nfs_server_port = ntohl(rpc_pkt.u.reply.data[0]); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 413 | break; |
| 414 | } |
| 415 | |
| 416 | return 0; |
| 417 | } |
| 418 | |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 419 | static int nfs_mount_reply(uchar *pkt, unsigned len) |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 420 | { |
| 421 | struct rpc_t rpc_pkt; |
| 422 | |
Robin Getz | 9e0a4d6 | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 423 | debug("%s\n", __func__); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 424 | |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 425 | memcpy((unsigned char *)&rpc_pkt, pkt, len); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 426 | |
Matthias Brugger | b8d5b8c | 2012-12-11 19:14:16 +0100 | [diff] [blame] | 427 | if (ntohl(rpc_pkt.u.reply.id) > rpc_id) |
| 428 | return -NFS_RPC_ERR; |
| 429 | else if (ntohl(rpc_pkt.u.reply.id) < rpc_id) |
| 430 | return -NFS_RPC_DROP; |
wdenk | acd9b10 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 431 | |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 432 | if (rpc_pkt.u.reply.rstatus || |
| 433 | rpc_pkt.u.reply.verifier || |
| 434 | rpc_pkt.u.reply.astatus || |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 435 | rpc_pkt.u.reply.data[0]) |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 436 | return -1; |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 437 | |
| 438 | fs_mounted = 1; |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 439 | memcpy(dirfh, rpc_pkt.u.reply.data + 1, NFS_FHSIZE); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 440 | |
| 441 | return 0; |
| 442 | } |
| 443 | |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 444 | static int nfs_umountall_reply(uchar *pkt, unsigned len) |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 445 | { |
| 446 | struct rpc_t rpc_pkt; |
| 447 | |
Robin Getz | 9e0a4d6 | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 448 | debug("%s\n", __func__); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 449 | |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 450 | memcpy((unsigned char *)&rpc_pkt, pkt, len); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 451 | |
Matthias Brugger | b8d5b8c | 2012-12-11 19:14:16 +0100 | [diff] [blame] | 452 | if (ntohl(rpc_pkt.u.reply.id) > rpc_id) |
| 453 | return -NFS_RPC_ERR; |
| 454 | else if (ntohl(rpc_pkt.u.reply.id) < rpc_id) |
| 455 | return -NFS_RPC_DROP; |
wdenk | acd9b10 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 456 | |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 457 | if (rpc_pkt.u.reply.rstatus || |
| 458 | rpc_pkt.u.reply.verifier || |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 459 | rpc_pkt.u.reply.astatus) |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 460 | return -1; |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 461 | |
| 462 | fs_mounted = 0; |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 463 | memset(dirfh, 0, sizeof(dirfh)); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 464 | |
| 465 | return 0; |
| 466 | } |
| 467 | |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 468 | static int nfs_lookup_reply(uchar *pkt, unsigned len) |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 469 | { |
| 470 | struct rpc_t rpc_pkt; |
| 471 | |
Robin Getz | 9e0a4d6 | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 472 | debug("%s\n", __func__); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 473 | |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 474 | memcpy((unsigned char *)&rpc_pkt, pkt, len); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 475 | |
Matthias Brugger | b8d5b8c | 2012-12-11 19:14:16 +0100 | [diff] [blame] | 476 | if (ntohl(rpc_pkt.u.reply.id) > rpc_id) |
| 477 | return -NFS_RPC_ERR; |
| 478 | else if (ntohl(rpc_pkt.u.reply.id) < rpc_id) |
| 479 | return -NFS_RPC_DROP; |
wdenk | acd9b10 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 480 | |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 481 | if (rpc_pkt.u.reply.rstatus || |
| 482 | rpc_pkt.u.reply.verifier || |
| 483 | rpc_pkt.u.reply.astatus || |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 484 | rpc_pkt.u.reply.data[0]) |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 485 | return -1; |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 486 | |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 487 | memcpy(filefh, rpc_pkt.u.reply.data + 1, NFS_FHSIZE); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 488 | |
| 489 | return 0; |
| 490 | } |
| 491 | |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 492 | static int nfs_readlink_reply(uchar *pkt, unsigned len) |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 493 | { |
| 494 | struct rpc_t rpc_pkt; |
| 495 | int rlen; |
| 496 | |
Robin Getz | 9e0a4d6 | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 497 | debug("%s\n", __func__); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 498 | |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 499 | memcpy((unsigned char *)&rpc_pkt, pkt, len); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 500 | |
Matthias Brugger | b8d5b8c | 2012-12-11 19:14:16 +0100 | [diff] [blame] | 501 | if (ntohl(rpc_pkt.u.reply.id) > rpc_id) |
| 502 | return -NFS_RPC_ERR; |
| 503 | else if (ntohl(rpc_pkt.u.reply.id) < rpc_id) |
| 504 | return -NFS_RPC_DROP; |
wdenk | acd9b10 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 505 | |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 506 | if (rpc_pkt.u.reply.rstatus || |
| 507 | rpc_pkt.u.reply.verifier || |
| 508 | rpc_pkt.u.reply.astatus || |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 509 | rpc_pkt.u.reply.data[0]) |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 510 | return -1; |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 511 | |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 512 | rlen = ntohl(rpc_pkt.u.reply.data[1]); /* new path length */ |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 513 | |
| 514 | if (*((char *)&(rpc_pkt.u.reply.data[2])) != '/') { |
| 515 | int pathlen; |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 516 | strcat(nfs_path, "/"); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 517 | pathlen = strlen(nfs_path); |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 518 | memcpy(nfs_path + pathlen, (uchar *)&(rpc_pkt.u.reply.data[2]), |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 519 | rlen); |
Ed Swarthout | 867d870 | 2009-11-19 02:47:28 -0600 | [diff] [blame] | 520 | nfs_path[pathlen + rlen] = 0; |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 521 | } else { |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 522 | memcpy(nfs_path, (uchar *)&(rpc_pkt.u.reply.data[2]), rlen); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 523 | nfs_path[rlen] = 0; |
| 524 | } |
| 525 | return 0; |
| 526 | } |
| 527 | |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 528 | static int nfs_read_reply(uchar *pkt, unsigned len) |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 529 | { |
| 530 | struct rpc_t rpc_pkt; |
| 531 | int rlen; |
| 532 | |
Robin Getz | 9e0a4d6 | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 533 | debug("%s\n", __func__); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 534 | |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 535 | memcpy((uchar *)&rpc_pkt, pkt, sizeof(rpc_pkt.u.reply)); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 536 | |
Matthias Brugger | b8d5b8c | 2012-12-11 19:14:16 +0100 | [diff] [blame] | 537 | if (ntohl(rpc_pkt.u.reply.id) > rpc_id) |
| 538 | return -NFS_RPC_ERR; |
| 539 | else if (ntohl(rpc_pkt.u.reply.id) < rpc_id) |
| 540 | return -NFS_RPC_DROP; |
wdenk | acd9b10 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 541 | |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 542 | if (rpc_pkt.u.reply.rstatus || |
| 543 | rpc_pkt.u.reply.verifier || |
| 544 | rpc_pkt.u.reply.astatus || |
| 545 | rpc_pkt.u.reply.data[0]) { |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 546 | if (rpc_pkt.u.reply.rstatus) |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 547 | return -9999; |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 548 | if (rpc_pkt.u.reply.astatus) |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 549 | return -9999; |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 550 | return -ntohl(rpc_pkt.u.reply.data[0]); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 551 | } |
| 552 | |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 553 | if ((nfs_offset != 0) && !((nfs_offset) % |
| 554 | (NFS_READ_SIZE / 2 * 10 * HASHES_PER_LINE))) |
| 555 | puts("\n\t "); |
| 556 | if (!(nfs_offset % ((NFS_READ_SIZE / 2) * 10))) |
| 557 | putc('#'); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 558 | |
| 559 | rlen = ntohl(rpc_pkt.u.reply.data[18]); |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 560 | if (store_block((uchar *)pkt + sizeof(rpc_pkt.u.reply), |
| 561 | nfs_offset, rlen)) |
Wolfgang Denk | b077b50 | 2005-08-06 01:11:12 +0200 | [diff] [blame] | 562 | return -9999; |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 563 | |
| 564 | return rlen; |
| 565 | } |
| 566 | |
| 567 | /************************************************************************** |
| 568 | Interfaces of U-BOOT |
| 569 | **************************************************************************/ |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 570 | static void nfs_timeout_handler(void) |
wdenk | d9ee50d | 2004-09-28 21:51:42 +0000 | [diff] [blame] | 571 | { |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 572 | if (++nfs_timeout_count > NFS_RETRY_COUNT) { |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 573 | puts("\nRetry count exceeded; starting again\n"); |
Joe Hershberger | c80b41b0 | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 574 | net_start_again(); |
Evan Samanas | b956fba | 2009-11-09 20:08:36 -0600 | [diff] [blame] | 575 | } else { |
| 576 | puts("T "); |
Joe Hershberger | c80b41b0 | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 577 | net_set_timeout_handler(nfs_timeout + |
| 578 | NFS_TIMEOUT * nfs_timeout_count, |
| 579 | nfs_timeout_handler); |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 580 | nfs_send(); |
Hiroshi Ito | 3702cd7 | 2008-01-31 18:35:04 +0900 | [diff] [blame] | 581 | } |
wdenk | d9ee50d | 2004-09-28 21:51:42 +0000 | [diff] [blame] | 582 | } |
| 583 | |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 584 | static void nfs_handler(uchar *pkt, unsigned dest, struct in_addr sip, |
| 585 | unsigned src, unsigned len) |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 586 | { |
| 587 | int rlen; |
Matthias Brugger | b8d5b8c | 2012-12-11 19:14:16 +0100 | [diff] [blame] | 588 | int reply; |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 589 | |
Robin Getz | 9e0a4d6 | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 590 | debug("%s\n", __func__); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 591 | |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 592 | if (dest != nfs_our_port) |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 593 | return; |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 594 | |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 595 | switch (nfs_state) { |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 596 | case STATE_PRCLOOKUP_PROG_MOUNT_REQ: |
Matthias Brugger | b8d5b8c | 2012-12-11 19:14:16 +0100 | [diff] [blame] | 597 | if (rpc_lookup_reply(PROG_MOUNT, pkt, len) == -NFS_RPC_DROP) |
| 598 | break; |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 599 | nfs_state = STATE_PRCLOOKUP_PROG_NFS_REQ; |
| 600 | nfs_send(); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 601 | break; |
| 602 | |
| 603 | case STATE_PRCLOOKUP_PROG_NFS_REQ: |
Matthias Brugger | b8d5b8c | 2012-12-11 19:14:16 +0100 | [diff] [blame] | 604 | if (rpc_lookup_reply(PROG_NFS, pkt, len) == -NFS_RPC_DROP) |
| 605 | break; |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 606 | nfs_state = STATE_MOUNT_REQ; |
| 607 | nfs_send(); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 608 | break; |
| 609 | |
| 610 | case STATE_MOUNT_REQ: |
Matthias Brugger | b8d5b8c | 2012-12-11 19:14:16 +0100 | [diff] [blame] | 611 | reply = nfs_mount_reply(pkt, len); |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 612 | if (reply == -NFS_RPC_DROP) { |
Matthias Brugger | b8d5b8c | 2012-12-11 19:14:16 +0100 | [diff] [blame] | 613 | break; |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 614 | } else if (reply == -NFS_RPC_ERR) { |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 615 | puts("*** ERROR: Cannot mount\n"); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 616 | /* just to be sure... */ |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 617 | nfs_state = STATE_UMOUNT_REQ; |
| 618 | nfs_send(); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 619 | } else { |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 620 | nfs_state = STATE_LOOKUP_REQ; |
| 621 | nfs_send(); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 622 | } |
| 623 | break; |
| 624 | |
| 625 | case STATE_UMOUNT_REQ: |
Matthias Brugger | b8d5b8c | 2012-12-11 19:14:16 +0100 | [diff] [blame] | 626 | reply = nfs_umountall_reply(pkt, len); |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 627 | if (reply == -NFS_RPC_DROP) { |
Matthias Brugger | b8d5b8c | 2012-12-11 19:14:16 +0100 | [diff] [blame] | 628 | break; |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 629 | } else if (reply == -NFS_RPC_ERR) { |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 630 | puts("*** ERROR: Cannot umount\n"); |
Joe Hershberger | d4bb76a | 2012-05-23 07:59:14 +0000 | [diff] [blame] | 631 | net_set_state(NETLOOP_FAIL); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 632 | } else { |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 633 | puts("\ndone\n"); |
Joe Hershberger | d4bb76a | 2012-05-23 07:59:14 +0000 | [diff] [blame] | 634 | net_set_state(nfs_download_state); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 635 | } |
| 636 | break; |
| 637 | |
| 638 | case STATE_LOOKUP_REQ: |
Matthias Brugger | b8d5b8c | 2012-12-11 19:14:16 +0100 | [diff] [blame] | 639 | reply = nfs_lookup_reply(pkt, len); |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 640 | if (reply == -NFS_RPC_DROP) { |
Matthias Brugger | b8d5b8c | 2012-12-11 19:14:16 +0100 | [diff] [blame] | 641 | break; |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 642 | } else if (reply == -NFS_RPC_ERR) { |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 643 | puts("*** ERROR: File lookup fail\n"); |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 644 | nfs_state = STATE_UMOUNT_REQ; |
| 645 | nfs_send(); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 646 | } else { |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 647 | nfs_state = STATE_READ_REQ; |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 648 | nfs_offset = 0; |
| 649 | nfs_len = NFS_READ_SIZE; |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 650 | nfs_send(); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 651 | } |
| 652 | break; |
| 653 | |
| 654 | case STATE_READLINK_REQ: |
Matthias Brugger | b8d5b8c | 2012-12-11 19:14:16 +0100 | [diff] [blame] | 655 | reply = nfs_readlink_reply(pkt, len); |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 656 | if (reply == -NFS_RPC_DROP) { |
Matthias Brugger | b8d5b8c | 2012-12-11 19:14:16 +0100 | [diff] [blame] | 657 | break; |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 658 | } else if (reply == -NFS_RPC_ERR) { |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 659 | puts("*** ERROR: Symlink fail\n"); |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 660 | nfs_state = STATE_UMOUNT_REQ; |
| 661 | nfs_send(); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 662 | } else { |
Robin Getz | 9e0a4d6 | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 663 | debug("Symlink --> %s\n", nfs_path); |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 664 | nfs_filename = basename(nfs_path); |
| 665 | nfs_path = dirname(nfs_path); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 666 | |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 667 | nfs_state = STATE_MOUNT_REQ; |
| 668 | nfs_send(); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 669 | } |
| 670 | break; |
| 671 | |
| 672 | case STATE_READ_REQ: |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 673 | rlen = nfs_read_reply(pkt, len); |
Joe Hershberger | c80b41b0 | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 674 | net_set_timeout_handler(nfs_timeout, nfs_timeout_handler); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 675 | if (rlen > 0) { |
| 676 | nfs_offset += rlen; |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 677 | nfs_send(); |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 678 | } else if ((rlen == -NFSERR_ISDIR) || (rlen == -NFSERR_INVAL)) { |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 679 | /* symbolic link */ |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 680 | nfs_state = STATE_READLINK_REQ; |
| 681 | nfs_send(); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 682 | } else { |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 683 | if (!rlen) |
Joe Hershberger | d4bb76a | 2012-05-23 07:59:14 +0000 | [diff] [blame] | 684 | nfs_download_state = NETLOOP_SUCCESS; |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 685 | nfs_state = STATE_UMOUNT_REQ; |
| 686 | nfs_send(); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 687 | } |
| 688 | break; |
| 689 | } |
| 690 | } |
| 691 | |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 692 | |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 693 | void nfs_start(void) |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 694 | { |
Robin Getz | 9e0a4d6 | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 695 | debug("%s\n", __func__); |
Joe Hershberger | d4bb76a | 2012-05-23 07:59:14 +0000 | [diff] [blame] | 696 | nfs_download_state = NETLOOP_FAIL; |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 697 | |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 698 | nfs_server_ip = net_server_ip; |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 699 | nfs_path = (char *)nfs_path_buff; |
| 700 | |
| 701 | if (nfs_path == NULL) { |
Joe Hershberger | d4bb76a | 2012-05-23 07:59:14 +0000 | [diff] [blame] | 702 | net_set_state(NETLOOP_FAIL); |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 703 | puts("*** ERROR: Fail allocate memory\n"); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 704 | return; |
| 705 | } |
| 706 | |
Joe Hershberger | 290c899 | 2015-04-08 01:41:02 -0500 | [diff] [blame] | 707 | if (net_boot_file_name[0] == '\0') { |
Matthias Weisser | 6cc6326 | 2011-12-03 03:29:44 +0000 | [diff] [blame] | 708 | sprintf(default_filename, "/nfsroot/%02X%02X%02X%02X.img", |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 709 | net_ip.s_addr & 0xFF, |
| 710 | (net_ip.s_addr >> 8) & 0xFF, |
| 711 | (net_ip.s_addr >> 16) & 0xFF, |
| 712 | (net_ip.s_addr >> 24) & 0xFF); |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 713 | strcpy(nfs_path, default_filename); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 714 | |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 715 | printf("*** Warning: no boot file name; using '%s'\n", |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 716 | nfs_path); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 717 | } else { |
Joe Hershberger | 290c899 | 2015-04-08 01:41:02 -0500 | [diff] [blame] | 718 | char *p = net_boot_file_name; |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 719 | |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 720 | p = strchr(p, ':'); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 721 | |
| 722 | if (p != NULL) { |
Joe Hershberger | 290c899 | 2015-04-08 01:41:02 -0500 | [diff] [blame] | 723 | nfs_server_ip = string_to_ip(net_boot_file_name); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 724 | ++p; |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 725 | strcpy(nfs_path, p); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 726 | } else { |
Joe Hershberger | 290c899 | 2015-04-08 01:41:02 -0500 | [diff] [blame] | 727 | strcpy(nfs_path, net_boot_file_name); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 728 | } |
| 729 | } |
| 730 | |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 731 | nfs_filename = basename(nfs_path); |
| 732 | nfs_path = dirname(nfs_path); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 733 | |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 734 | printf("Using %s device\n", eth_get_name()); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 735 | |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 736 | printf("File transfer via NFS from server %pI4; our IP address is %pI4", |
| 737 | &nfs_server_ip, &net_ip); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 738 | |
| 739 | /* Check if we need to send across this subnet */ |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 740 | if (net_gateway.s_addr && net_netmask.s_addr) { |
| 741 | struct in_addr our_net; |
| 742 | struct in_addr server_net; |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 743 | |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 744 | our_net.s_addr = net_ip.s_addr & net_netmask.s_addr; |
| 745 | server_net.s_addr = net_server_ip.s_addr & net_netmask.s_addr; |
| 746 | if (our_net.s_addr != server_net.s_addr) |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 747 | printf("; sending through gateway %pI4", |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 748 | &net_gateway); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 749 | } |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 750 | printf("\nFilename '%s/%s'.", nfs_path, nfs_filename); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 751 | |
Joe Hershberger | 290c899 | 2015-04-08 01:41:02 -0500 | [diff] [blame] | 752 | if (net_boot_file_expected_size_in_blocks) { |
| 753 | printf(" Size is 0x%x Bytes = ", |
| 754 | net_boot_file_expected_size_in_blocks << 9); |
| 755 | print_size(net_boot_file_expected_size_in_blocks << 9, ""); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 756 | } |
Joe Hershberger | 6ecfc6b | 2012-05-15 08:59:09 +0000 | [diff] [blame] | 757 | printf("\nLoad address: 0x%lx\n" |
wdenk | 42c0547 | 2004-03-23 22:14:11 +0000 | [diff] [blame] | 758 | "Loading: *\b", load_addr); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 759 | |
Joe Hershberger | c80b41b0 | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 760 | net_set_timeout_handler(nfs_timeout, nfs_timeout_handler); |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 761 | net_set_udp_handler(nfs_handler); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 762 | |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 763 | nfs_timeout_count = 0; |
| 764 | nfs_state = STATE_PRCLOOKUP_PROG_MOUNT_REQ; |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 765 | |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 766 | /*nfs_our_port = 4096 + (get_ticks() % 3072);*/ |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 767 | /*FIX ME !!!*/ |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 768 | nfs_our_port = 1000; |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 769 | |
| 770 | /* zero out server ether in case the server ip has changed */ |
Joe Hershberger | 8ecdbed | 2015-04-08 01:41:04 -0500 | [diff] [blame] | 771 | memset(net_server_ethaddr, 0, 6); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 772 | |
Joe Hershberger | 40d7ca9 | 2015-04-08 01:41:10 -0500 | [diff] [blame] | 773 | nfs_send(); |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 774 | } |