nfs.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  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. /* NOTE: the NFS code is heavily inspired by the NetBSD netboot code (read:
  8. * large portions are copied verbatim) as distributed in OSKit 0.97. A few
  9. * changes were necessary to adapt the code to Etherboot and to fix several
  10. * inconsistencies. Also the RPC message preparation is done "by hand" to
  11. * avoid adding netsprintf() which I find hard to understand and use. */
  12. /* NOTE 2: Etherboot does not care about things beyond the kernel image, so
  13. * it loads the kernel image off the boot server (ARP_SERVER) and does not
  14. * access the client root disk (root-path in dhcpd.conf), which would use
  15. * ARP_ROOTSERVER. The root disk is something the operating system we are
  16. * about to load needs to use. This is different from the OSKit 0.97 logic. */
  17. /* NOTE 3: Symlink handling introduced by Anselm M Hoffmeister, 2003-July-14
  18. * If a symlink is encountered, it is followed as far as possible (recursion
  19. * possible, maximum 16 steps). There is no clearing of ".."'s inside the
  20. * path, so please DON'T DO THAT. thx. */
  21. #include <common.h>
  22. #include <command.h>
  23. #include <net.h>
  24. #include <malloc.h>
  25. #include "nfs.h"
  26. #include "bootp.h"
  27. /*#define NFS_DEBUG*/
  28. #if ((CONFIG_COMMANDS & CFG_CMD_NET) && (CONFIG_COMMANDS & CFG_CMD_NFS))
  29. #define HASHES_PER_LINE 65 /* Number of "loading" hashes per line */
  30. #define NFS_TIMEOUT 10
  31. static int fs_mounted = 0;
  32. static unsigned long rpc_id = 0;
  33. static int nfs_offset = -1;
  34. static int nfs_len;
  35. static char dirfh[NFS_FHSIZE]; /* file handle of directory */
  36. static char filefh[NFS_FHSIZE]; /* file handle of kernel image */
  37. static IPaddr_t NfsServerIP;
  38. static int NfsSrvMountPort;
  39. static int NfsSrvNfsPort;
  40. static int NfsOurPort;
  41. static int NfsTimeoutCount;
  42. static int NfsState;
  43. #define STATE_PRCLOOKUP_PROG_MOUNT_REQ 1
  44. #define STATE_PRCLOOKUP_PROG_NFS_REQ 2
  45. #define STATE_MOUNT_REQ 3
  46. #define STATE_UMOUNT_REQ 4
  47. #define STATE_LOOKUP_REQ 5
  48. #define STATE_READ_REQ 6
  49. #define STATE_READLINK_REQ 7
  50. static char default_filename[64];
  51. static char *nfs_filename;
  52. static char *nfs_path;
  53. static char nfs_path_buff[2048];
  54. static __inline__ void
  55. store_block (uchar * src, unsigned offset, unsigned len)
  56. {
  57. ulong newsize = offset + len;
  58. #ifdef CFG_DIRECT_FLASH_NFS
  59. int i, rc = 0;
  60. for (i=0; i<CFG_MAX_FLASH_BANKS; i++) {
  61. /* start address in flash? */
  62. if (load_addr + offset >= flash_info[i].start[0]) {
  63. rc = 1;
  64. break;
  65. }
  66. }
  67. if (rc) { /* Flash is destination for this packet */
  68. rc = flash_write ((uchar *)src, (ulong)(load_addr+offset), len);
  69. if (rc) {
  70. flash_perror (rc);
  71. NetState = NETLOOP_FAIL;
  72. return;
  73. }
  74. } else
  75. #endif /* CFG_DIRECT_FLASH_NFS */
  76. {
  77. (void)memcpy ((void *)(load_addr + offset), src, len);
  78. }
  79. if (NetBootFileXferSize < (offset+len))
  80. NetBootFileXferSize = newsize;
  81. }
  82. static char*
  83. basename (char *path)
  84. {
  85. char *fname;
  86. fname = path + strlen(path) - 1;
  87. while (fname >= path) {
  88. if (*fname == '/') {
  89. fname++;
  90. break;
  91. }
  92. fname--;
  93. }
  94. return fname;
  95. }
  96. static char*
  97. dirname (char *path)
  98. {
  99. char *fname;
  100. fname = basename (path);
  101. --fname;
  102. *fname = '\0';
  103. return path;
  104. }
  105. /**************************************************************************
  106. RPC_ADD_CREDENTIALS - Add RPC authentication/verifier entries
  107. **************************************************************************/
  108. static long *rpc_add_credentials (long *p)
  109. {
  110. int hl;
  111. int hostnamelen;
  112. char hostname[256];
  113. strcpy (hostname, "");
  114. hostnamelen=strlen (hostname);
  115. /* Here's the executive summary on authentication requirements of the
  116. * various NFS server implementations: Linux accepts both AUTH_NONE
  117. * and AUTH_UNIX authentication (also accepts an empty hostname field
  118. * in the AUTH_UNIX scheme). *BSD refuses AUTH_NONE, but accepts
  119. * AUTH_UNIX (also accepts an empty hostname field in the AUTH_UNIX
  120. * scheme). To be safe, use AUTH_UNIX and pass the hostname if we have
  121. * it (if the BOOTP/DHCP reply didn't give one, just use an empty
  122. * hostname). */
  123. hl = (hostnamelen + 3) & ~3;
  124. /* Provide an AUTH_UNIX credential. */
  125. *p++ = htonl(1); /* AUTH_UNIX */
  126. *p++ = htonl(hl+20); /* auth length */
  127. *p++ = htonl(0); /* stamp */
  128. *p++ = htonl(hostnamelen); /* hostname string */
  129. if (hostnamelen & 3) {
  130. *(p + hostnamelen / 4) = 0; /* add zero padding */
  131. }
  132. memcpy (p, hostname, hostnamelen);
  133. p += hl / 4;
  134. *p++ = 0; /* uid */
  135. *p++ = 0; /* gid */
  136. *p++ = 0; /* auxiliary gid list */
  137. /* Provide an AUTH_NONE verifier. */
  138. *p++ = 0; /* AUTH_NONE */
  139. *p++ = 0; /* auth length */
  140. return p;
  141. }
  142. /**************************************************************************
  143. RPC_LOOKUP - Lookup RPC Port numbers
  144. **************************************************************************/
  145. static void
  146. rpc_req (int rpc_prog, int rpc_proc, uint32_t *data, int datalen)
  147. {
  148. struct rpc_t pkt;
  149. unsigned long id;
  150. uint32_t *p;
  151. int pktlen;
  152. int sport;
  153. id = ++rpc_id;
  154. pkt.u.call.id = htonl(id);
  155. pkt.u.call.type = htonl(MSG_CALL);
  156. pkt.u.call.rpcvers = htonl(2); /* use RPC version 2 */
  157. pkt.u.call.prog = htonl(rpc_prog);
  158. pkt.u.call.vers = htonl(2); /* portmapper is version 2 */
  159. pkt.u.call.proc = htonl(rpc_proc);
  160. p = (uint32_t *)&(pkt.u.call.data);
  161. if (datalen)
  162. memcpy ((char *)p, (char *)data, datalen*sizeof(uint32_t));
  163. pktlen = (char *)p + datalen*sizeof(uint32_t) - (char *)&pkt;
  164. memcpy ((char *)NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE, (char *)&pkt, pktlen);
  165. if (rpc_prog == PROG_PORTMAP)
  166. sport = SUNRPC_PORT;
  167. else if (rpc_prog == PROG_MOUNT)
  168. sport = NfsSrvMountPort;
  169. else
  170. sport = NfsSrvNfsPort;
  171. NetSendUDPPacket (NetServerEther, NfsServerIP, sport, NfsOurPort, pktlen);
  172. }
  173. /**************************************************************************
  174. RPC_LOOKUP - Lookup RPC Port numbers
  175. **************************************************************************/
  176. static void
  177. rpc_lookup_req (int prog, int ver)
  178. {
  179. uint32_t data[16];
  180. data[0] = 0; data[1] = 0; /* auth credential */
  181. data[2] = 0; data[3] = 0; /* auth verifier */
  182. data[4] = htonl(prog);
  183. data[5] = htonl(ver);
  184. data[6] = htonl(17); /* IP_UDP */
  185. data[7] = 0;
  186. rpc_req (PROG_PORTMAP, PORTMAP_GETPORT, data, 8);
  187. }
  188. /**************************************************************************
  189. NFS_MOUNT - Mount an NFS Filesystem
  190. **************************************************************************/
  191. static void
  192. nfs_mount_req (char *path)
  193. {
  194. uint32_t data[1024];
  195. uint32_t *p;
  196. int len;
  197. int pathlen;
  198. pathlen = strlen (path);
  199. p = &(data[0]);
  200. p = (uint32_t *)rpc_add_credentials((long *)p);
  201. *p++ = htonl(pathlen);
  202. if (pathlen & 3) *(p + pathlen / 4) = 0;
  203. memcpy (p, path, pathlen);
  204. p += (pathlen + 3) / 4;
  205. len = (uint32_t *)p - (uint32_t *)&(data[0]);
  206. rpc_req (PROG_MOUNT, MOUNT_ADDENTRY, data, len);
  207. }
  208. /**************************************************************************
  209. NFS_UMOUNTALL - Unmount all our NFS Filesystems on the Server
  210. **************************************************************************/
  211. static void
  212. nfs_umountall_req (void)
  213. {
  214. uint32_t data[1024];
  215. uint32_t *p;
  216. int len;
  217. if ((NfsSrvMountPort == -1) || (!fs_mounted)) {
  218. /* Nothing mounted, nothing to umount */
  219. return;
  220. }
  221. p = &(data[0]);
  222. p = (uint32_t *)rpc_add_credentials ((long *)p);
  223. len = (uint32_t *)p - (uint32_t *)&(data[0]);
  224. rpc_req (PROG_MOUNT, MOUNT_UMOUNTALL, data, len);
  225. }
  226. /***************************************************************************
  227. * NFS_READLINK (AH 2003-07-14)
  228. * This procedure is called when read of the first block fails -
  229. * this probably happens when it's a directory or a symlink
  230. * In case of successful readlink(), the dirname is manipulated,
  231. * so that inside the nfs() function a recursion can be done.
  232. **************************************************************************/
  233. static void
  234. nfs_readlink_req (void)
  235. {
  236. uint32_t data[1024];
  237. uint32_t *p;
  238. int len;
  239. p = &(data[0]);
  240. p = (uint32_t *)rpc_add_credentials ((long *)p);
  241. memcpy (p, filefh, NFS_FHSIZE);
  242. p += (NFS_FHSIZE / 4);
  243. len = (uint32_t *)p - (uint32_t *)&(data[0]);
  244. rpc_req (PROG_NFS, NFS_READLINK, data, len);
  245. }
  246. /**************************************************************************
  247. NFS_LOOKUP - Lookup Pathname
  248. **************************************************************************/
  249. static void
  250. nfs_lookup_req (char *fname)
  251. {
  252. uint32_t data[1024];
  253. uint32_t *p;
  254. int len;
  255. int fnamelen;
  256. fnamelen = strlen (fname);
  257. p = &(data[0]);
  258. p = (uint32_t *)rpc_add_credentials ((long *)p);
  259. memcpy (p, dirfh, NFS_FHSIZE);
  260. p += (NFS_FHSIZE / 4);
  261. *p++ = htonl(fnamelen);
  262. if (fnamelen & 3) *(p + fnamelen / 4) = 0;
  263. memcpy (p, fname, fnamelen);
  264. p += (fnamelen + 3) / 4;
  265. len = (uint32_t *)p - (uint32_t *)&(data[0]);
  266. rpc_req (PROG_NFS, NFS_LOOKUP, data, len);
  267. }
  268. /**************************************************************************
  269. NFS_READ - Read File on NFS Server
  270. **************************************************************************/
  271. static void
  272. nfs_read_req (int offset, int readlen)
  273. {
  274. uint32_t data[1024];
  275. uint32_t *p;
  276. int len;
  277. p = &(data[0]);
  278. p = (uint32_t *)rpc_add_credentials ((long *)p);
  279. memcpy (p, filefh, NFS_FHSIZE);
  280. p += (NFS_FHSIZE / 4);
  281. *p++ = htonl(offset);
  282. *p++ = htonl(readlen);
  283. *p++ = 0;
  284. len = (uint32_t *)p - (uint32_t *)&(data[0]);
  285. rpc_req (PROG_NFS, NFS_READ, data, len);
  286. }
  287. /**************************************************************************
  288. RPC request dispatcher
  289. **************************************************************************/
  290. static void
  291. NfsSend (void)
  292. {
  293. #ifdef NFS_DEBUG
  294. printf ("%s\n", __FUNCTION__);
  295. #endif
  296. switch (NfsState) {
  297. case STATE_PRCLOOKUP_PROG_MOUNT_REQ:
  298. rpc_lookup_req (PROG_MOUNT, 1);
  299. break;
  300. case STATE_PRCLOOKUP_PROG_NFS_REQ:
  301. rpc_lookup_req (PROG_NFS, 2);
  302. break;
  303. case STATE_MOUNT_REQ:
  304. nfs_mount_req (nfs_path);
  305. break;
  306. case STATE_UMOUNT_REQ:
  307. nfs_umountall_req ();
  308. break;
  309. case STATE_LOOKUP_REQ:
  310. nfs_lookup_req (nfs_filename);
  311. break;
  312. case STATE_READ_REQ:
  313. nfs_read_req (nfs_offset, nfs_len);
  314. break;
  315. case STATE_READLINK_REQ:
  316. nfs_readlink_req ();
  317. break;
  318. }
  319. }
  320. /**************************************************************************
  321. Handlers for the reply from server
  322. **************************************************************************/
  323. static int
  324. rpc_lookup_reply (int prog, uchar *pkt, unsigned len)
  325. {
  326. struct rpc_t rpc_pkt;
  327. memcpy ((unsigned char *)&rpc_pkt, pkt, len);
  328. #ifdef NFS_DEBUG
  329. printf ("%s\n", __FUNCTION__);
  330. #endif
  331. if (ntohl(rpc_pkt.u.reply.id) != rpc_id)
  332. return -1;
  333. if (rpc_pkt.u.reply.rstatus ||
  334. rpc_pkt.u.reply.verifier ||
  335. rpc_pkt.u.reply.astatus ||
  336. rpc_pkt.u.reply.astatus) {
  337. return -1;
  338. }
  339. switch (prog) {
  340. case PROG_MOUNT:
  341. NfsSrvMountPort = ntohl(rpc_pkt.u.reply.data[0]);
  342. break;
  343. case PROG_NFS:
  344. NfsSrvNfsPort = ntohl(rpc_pkt.u.reply.data[0]);
  345. break;
  346. }
  347. return 0;
  348. }
  349. static int
  350. nfs_mount_reply (uchar *pkt, unsigned len)
  351. {
  352. struct rpc_t rpc_pkt;
  353. #ifdef NFS_DEBUG
  354. printf ("%s\n", __FUNCTION__);
  355. #endif
  356. memcpy ((unsigned char *)&rpc_pkt, pkt, len);
  357. if (ntohl(rpc_pkt.u.reply.id) != rpc_id)
  358. return -1;
  359. if (rpc_pkt.u.reply.rstatus ||
  360. rpc_pkt.u.reply.verifier ||
  361. rpc_pkt.u.reply.astatus ||
  362. rpc_pkt.u.reply.data[0]) {
  363. return -1;
  364. }
  365. fs_mounted = 1;
  366. memcpy (dirfh, rpc_pkt.u.reply.data + 1, NFS_FHSIZE);
  367. return 0;
  368. }
  369. static int
  370. nfs_umountall_reply (uchar *pkt, unsigned len)
  371. {
  372. struct rpc_t rpc_pkt;
  373. #ifdef NFS_DEBUG
  374. printf ("%s\n", __FUNCTION__);
  375. #endif
  376. memcpy ((unsigned char *)&rpc_pkt, pkt, len);
  377. if (ntohl(rpc_pkt.u.reply.id) != rpc_id)
  378. return -1;
  379. if (rpc_pkt.u.reply.rstatus ||
  380. rpc_pkt.u.reply.verifier ||
  381. rpc_pkt.u.reply.astatus) {
  382. return -1;
  383. }
  384. fs_mounted = 0;
  385. memset (dirfh, 0, sizeof(dirfh));
  386. return 0;
  387. }
  388. static int
  389. nfs_lookup_reply (uchar *pkt, unsigned len)
  390. {
  391. struct rpc_t rpc_pkt;
  392. #ifdef NFS_DEBUG
  393. printf ("%s\n", __FUNCTION__);
  394. #endif
  395. memcpy ((unsigned char *)&rpc_pkt, pkt, len);
  396. if (ntohl(rpc_pkt.u.reply.id) != rpc_id)
  397. return -1;
  398. if (rpc_pkt.u.reply.rstatus ||
  399. rpc_pkt.u.reply.verifier ||
  400. rpc_pkt.u.reply.astatus ||
  401. rpc_pkt.u.reply.data[0]) {
  402. return -1;
  403. }
  404. memcpy (filefh, rpc_pkt.u.reply.data + 1, NFS_FHSIZE);
  405. return 0;
  406. }
  407. static int
  408. nfs_readlink_reply (uchar *pkt, unsigned len)
  409. {
  410. struct rpc_t rpc_pkt;
  411. int rlen;
  412. #ifdef NFS_DEBUG
  413. printf ("%s\n", __FUNCTION__);
  414. #endif
  415. memcpy ((unsigned char *)&rpc_pkt, pkt, len);
  416. if (ntohl(rpc_pkt.u.reply.id) != rpc_id)
  417. return -1;
  418. if (rpc_pkt.u.reply.rstatus ||
  419. rpc_pkt.u.reply.verifier ||
  420. rpc_pkt.u.reply.astatus ||
  421. rpc_pkt.u.reply.data[0]) {
  422. return -1;
  423. }
  424. rlen = ntohl (rpc_pkt.u.reply.data[1]); /* new path length */
  425. if (*((char *)&(rpc_pkt.u.reply.data[2])) != '/') {
  426. int pathlen;
  427. strcat (nfs_path, "/");
  428. pathlen = strlen(nfs_path);
  429. memcpy (nfs_path+pathlen, (uchar *)&(rpc_pkt.u.reply.data[2]), rlen);
  430. nfs_path[pathlen+rlen+1] = 0;
  431. } else {
  432. memcpy (nfs_path, (uchar *)&(rpc_pkt.u.reply.data[2]), rlen);
  433. nfs_path[rlen] = 0;
  434. }
  435. return 0;
  436. }
  437. static int
  438. nfs_read_reply (uchar *pkt, unsigned len)
  439. {
  440. struct rpc_t rpc_pkt;
  441. int rlen;
  442. #ifdef NFS_DEBUG_nop
  443. printf ("%s\n", __FUNCTION__);
  444. #endif
  445. memcpy ((uchar *)&rpc_pkt, pkt, sizeof(rpc_pkt.u.reply));
  446. if (ntohl(rpc_pkt.u.reply.id) != rpc_id)
  447. return -1;
  448. if (rpc_pkt.u.reply.rstatus ||
  449. rpc_pkt.u.reply.verifier ||
  450. rpc_pkt.u.reply.astatus ||
  451. rpc_pkt.u.reply.data[0]) {
  452. if (rpc_pkt.u.reply.rstatus) {
  453. return -9999;
  454. }
  455. if (rpc_pkt.u.reply.astatus) {
  456. return -9999;
  457. }
  458. return -ntohl(rpc_pkt.u.reply.data[0]);;
  459. }
  460. if ((nfs_offset!=0) && !((nfs_offset) % (NFS_READ_SIZE/2*10*HASHES_PER_LINE))) {
  461. puts ("\n\t ");
  462. }
  463. if (!(nfs_offset % ((NFS_READ_SIZE/2)*10))) {
  464. putc ('#');
  465. }
  466. rlen = ntohl(rpc_pkt.u.reply.data[18]);
  467. store_block ((uchar *)pkt+sizeof(rpc_pkt.u.reply), nfs_offset, rlen);
  468. return rlen;
  469. }
  470. /**************************************************************************
  471. Interfaces of U-BOOT
  472. **************************************************************************/
  473. static void
  474. NfsHandler (uchar *pkt, unsigned dest, unsigned src, unsigned len)
  475. {
  476. int rlen;
  477. #ifdef NFS_DEBUG
  478. printf ("%s\n", __FUNCTION__);
  479. #endif
  480. if (dest != NfsOurPort) return;
  481. switch (NfsState) {
  482. case STATE_PRCLOOKUP_PROG_MOUNT_REQ:
  483. rpc_lookup_reply (PROG_MOUNT, pkt, len);
  484. NfsState = STATE_PRCLOOKUP_PROG_NFS_REQ;
  485. NfsSend ();
  486. break;
  487. case STATE_PRCLOOKUP_PROG_NFS_REQ:
  488. rpc_lookup_reply (PROG_NFS, pkt, len);
  489. NfsState = STATE_MOUNT_REQ;
  490. NfsSend ();
  491. break;
  492. case STATE_MOUNT_REQ:
  493. if (nfs_mount_reply(pkt, len)) {
  494. puts ("*** ERROR: Cannot mount\n");
  495. /* just to be sure... */
  496. NfsState = STATE_UMOUNT_REQ;
  497. NfsSend ();
  498. } else {
  499. NfsState = STATE_LOOKUP_REQ;
  500. NfsSend ();
  501. }
  502. break;
  503. case STATE_UMOUNT_REQ:
  504. if (nfs_umountall_reply(pkt, len)) {
  505. puts ("*** ERROR: Cannot umount\n");
  506. NetState = NETLOOP_FAIL;
  507. } else {
  508. puts ("\ndone\n");
  509. NetState = NETLOOP_SUCCESS;
  510. }
  511. break;
  512. case STATE_LOOKUP_REQ:
  513. if (nfs_lookup_reply(pkt, len)) {
  514. puts ("*** ERROR: File lookup fail\n");
  515. NfsState = STATE_UMOUNT_REQ;
  516. NfsSend ();
  517. } else {
  518. NfsState = STATE_READ_REQ;
  519. nfs_offset = 0;
  520. nfs_len = NFS_READ_SIZE;
  521. NfsSend ();
  522. }
  523. break;
  524. case STATE_READLINK_REQ:
  525. if (nfs_readlink_reply(pkt, len)) {
  526. puts ("*** ERROR: Symlink fail\n");
  527. NfsState = STATE_UMOUNT_REQ;
  528. NfsSend ();
  529. } else {
  530. #ifdef NFS_DEBUG
  531. printf ("Symlink --> %s\n", nfs_path);
  532. #endif
  533. nfs_filename = basename (nfs_path);
  534. nfs_path = dirname (nfs_path);
  535. NfsState = STATE_MOUNT_REQ;
  536. NfsSend ();
  537. }
  538. break;
  539. case STATE_READ_REQ:
  540. rlen = nfs_read_reply (pkt, len);
  541. if (rlen > 0) {
  542. nfs_offset += rlen;
  543. NfsSend ();
  544. }
  545. else if ((rlen == -NFSERR_ISDIR)||(rlen == -NFSERR_INVAL)) {
  546. /* symbolic link */
  547. NfsState = STATE_READLINK_REQ;
  548. NfsSend ();
  549. } else {
  550. NfsState = STATE_UMOUNT_REQ;
  551. NfsSend ();
  552. }
  553. break;
  554. }
  555. }
  556. static void
  557. NfsTimeout (void)
  558. {
  559. puts ("Timeout\n");
  560. NetState = NETLOOP_FAIL;
  561. return;
  562. }
  563. void
  564. NfsStart (void)
  565. {
  566. #ifdef NFS_DEBUG
  567. printf ("%s\n", __FUNCTION__);
  568. #endif
  569. NfsServerIP = NetServerIP;
  570. nfs_path = (char *)nfs_path_buff;
  571. if (nfs_path == NULL) {
  572. NetState = NETLOOP_FAIL;
  573. puts ("*** ERROR: Fail allocate memory\n");
  574. return;
  575. }
  576. if (BootFile[0] == '\0') {
  577. IPaddr_t OurIP = ntohl (NetOurIP);
  578. sprintf (default_filename, "/nfsroot/%02lX%02lX%02lX%02lX.img",
  579. OurIP & 0xFF,
  580. (OurIP >> 8) & 0xFF,
  581. (OurIP >> 16) & 0xFF,
  582. (OurIP >> 24) & 0xFF );
  583. strcpy (nfs_path, default_filename);
  584. printf ("*** Warning: no boot file name; using '%s'\n",
  585. nfs_path);
  586. } else {
  587. char *p=BootFile;
  588. p = strchr (p, ':');
  589. if (p != NULL) {
  590. NfsServerIP = string_to_ip (BootFile);
  591. ++p;
  592. strcpy (nfs_path, p);
  593. } else {
  594. strcpy (nfs_path, BootFile);
  595. }
  596. }
  597. nfs_filename = basename (nfs_path);
  598. nfs_path = dirname (nfs_path);
  599. #if defined(CONFIG_NET_MULTI)
  600. printf ("Using %s device\n", eth_get_name());
  601. #endif
  602. puts ("File transfer via NFS from server "); print_IPaddr (NfsServerIP);
  603. puts ("; our IP address is "); print_IPaddr (NetOurIP);
  604. /* Check if we need to send across this subnet */
  605. if (NetOurGatewayIP && NetOurSubnetMask) {
  606. IPaddr_t OurNet = NetOurIP & NetOurSubnetMask;
  607. IPaddr_t ServerNet = NetServerIP & NetOurSubnetMask;
  608. if (OurNet != ServerNet) {
  609. puts ("; sending through gateway ");
  610. print_IPaddr (NetOurGatewayIP) ;
  611. }
  612. }
  613. printf ("\nFilename '%s/%s'.", nfs_path, nfs_filename);
  614. if (NetBootFileSize) {
  615. printf (" Size is 0x%x Bytes = ", NetBootFileSize<<9);
  616. print_size (NetBootFileSize<<9, "");
  617. }
  618. printf ("\nLoad address: 0x%lx\n"
  619. "Loading: *\b", load_addr);
  620. NetSetTimeout (NFS_TIMEOUT * CFG_HZ, NfsTimeout);
  621. NetSetHandler (NfsHandler);
  622. NfsTimeoutCount = 0;
  623. NfsState = STATE_PRCLOOKUP_PROG_MOUNT_REQ;
  624. /*NfsOurPort = 4096 + (get_ticks() % 3072);*/
  625. /*FIX ME !!!*/
  626. NfsOurPort = 1000;
  627. /* zero out server ether in case the server ip has changed */
  628. memset (NetServerEther, 0, 6);
  629. NfsSend ();
  630. }
  631. #endif /* CONFIG_COMMANDS & CFG_CMD_NFS */