nfs.c 17 KB

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