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