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