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