nfs.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  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. #define NFS_TIMEOUT 2000UL
  30. static int fs_mounted;
  31. static unsigned long rpc_id;
  32. static int nfs_offset = -1;
  33. static int nfs_len;
  34. static char dirfh[NFS_FHSIZE]; /* file handle of directory */
  35. static char filefh[NFS_FHSIZE]; /* file handle of kernel image */
  36. static enum net_loop_state nfs_download_state;
  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 int
  55. store_block(uchar *src, unsigned offset, unsigned len)
  56. {
  57. ulong newsize = offset + len;
  58. #ifdef CONFIG_SYS_DIRECT_FLASH_NFS
  59. int i, rc = 0;
  60. for (i = 0; i < CONFIG_SYS_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. return -1;
  72. }
  73. } else
  74. #endif /* CONFIG_SYS_DIRECT_FLASH_NFS */
  75. {
  76. (void)memcpy((void *)(load_addr + offset), src, len);
  77. }
  78. if (NetBootFileXferSize < (offset+len))
  79. NetBootFileXferSize = newsize;
  80. return 0;
  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. memcpy(p, hostname, hostnamelen);
  132. p += hl / 4;
  133. *p++ = 0; /* uid */
  134. *p++ = 0; /* gid */
  135. *p++ = 0; /* auxiliary gid list */
  136. /* Provide an AUTH_NONE verifier. */
  137. *p++ = 0; /* AUTH_NONE */
  138. *p++ = 0; /* auth length */
  139. return p;
  140. }
  141. /**************************************************************************
  142. RPC_LOOKUP - Lookup RPC Port numbers
  143. **************************************************************************/
  144. static void
  145. rpc_req(int rpc_prog, int rpc_proc, uint32_t *data, int datalen)
  146. {
  147. struct rpc_t pkt;
  148. unsigned long id;
  149. uint32_t *p;
  150. int pktlen;
  151. int sport;
  152. id = ++rpc_id;
  153. pkt.u.call.id = htonl(id);
  154. pkt.u.call.type = htonl(MSG_CALL);
  155. pkt.u.call.rpcvers = htonl(2); /* use RPC version 2 */
  156. pkt.u.call.prog = htonl(rpc_prog);
  157. pkt.u.call.vers = htonl(2); /* portmapper is version 2 */
  158. pkt.u.call.proc = htonl(rpc_proc);
  159. p = (uint32_t *)&(pkt.u.call.data);
  160. if (datalen)
  161. memcpy((char *)p, (char *)data, datalen*sizeof(uint32_t));
  162. pktlen = (char *)p + datalen*sizeof(uint32_t) - (char *)&pkt;
  163. memcpy((char *)NetTxPacket + NetEthHdrSize() + IP_UDP_HDR_SIZE,
  164. (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,
  172. pktlen);
  173. }
  174. /**************************************************************************
  175. RPC_LOOKUP - Lookup RPC Port numbers
  176. **************************************************************************/
  177. static void
  178. rpc_lookup_req(int prog, int ver)
  179. {
  180. uint32_t data[16];
  181. data[0] = 0; data[1] = 0; /* auth credential */
  182. data[2] = 0; data[3] = 0; /* auth verifier */
  183. data[4] = htonl(prog);
  184. data[5] = htonl(ver);
  185. data[6] = htonl(17); /* IP_UDP */
  186. data[7] = 0;
  187. rpc_req(PROG_PORTMAP, PORTMAP_GETPORT, data, 8);
  188. }
  189. /**************************************************************************
  190. NFS_MOUNT - Mount an NFS Filesystem
  191. **************************************************************************/
  192. static void
  193. nfs_mount_req(char *path)
  194. {
  195. uint32_t data[1024];
  196. uint32_t *p;
  197. int len;
  198. int pathlen;
  199. pathlen = strlen(path);
  200. p = &(data[0]);
  201. p = (uint32_t *)rpc_add_credentials((long *)p);
  202. *p++ = htonl(pathlen);
  203. if (pathlen & 3)
  204. *(p + pathlen / 4) = 0;
  205. memcpy(p, path, pathlen);
  206. p += (pathlen + 3) / 4;
  207. len = (uint32_t *)p - (uint32_t *)&(data[0]);
  208. rpc_req(PROG_MOUNT, MOUNT_ADDENTRY, data, len);
  209. }
  210. /**************************************************************************
  211. NFS_UMOUNTALL - Unmount all our NFS Filesystems on the Server
  212. **************************************************************************/
  213. static void
  214. nfs_umountall_req(void)
  215. {
  216. uint32_t data[1024];
  217. uint32_t *p;
  218. int len;
  219. if ((NfsSrvMountPort == -1) || (!fs_mounted))
  220. /* Nothing mounted, nothing to umount */
  221. return;
  222. p = &(data[0]);
  223. p = (uint32_t *)rpc_add_credentials((long *)p);
  224. len = (uint32_t *)p - (uint32_t *)&(data[0]);
  225. rpc_req(PROG_MOUNT, MOUNT_UMOUNTALL, data, len);
  226. }
  227. /***************************************************************************
  228. * NFS_READLINK (AH 2003-07-14)
  229. * This procedure is called when read of the first block fails -
  230. * this probably happens when it's a directory or a symlink
  231. * In case of successful readlink(), the dirname is manipulated,
  232. * so that inside the nfs() function a recursion can be done.
  233. **************************************************************************/
  234. static void
  235. nfs_readlink_req(void)
  236. {
  237. uint32_t data[1024];
  238. uint32_t *p;
  239. int len;
  240. p = &(data[0]);
  241. p = (uint32_t *)rpc_add_credentials((long *)p);
  242. memcpy(p, filefh, NFS_FHSIZE);
  243. p += (NFS_FHSIZE / 4);
  244. len = (uint32_t *)p - (uint32_t *)&(data[0]);
  245. rpc_req(PROG_NFS, NFS_READLINK, data, len);
  246. }
  247. /**************************************************************************
  248. NFS_LOOKUP - Lookup Pathname
  249. **************************************************************************/
  250. static void
  251. nfs_lookup_req(char *fname)
  252. {
  253. uint32_t data[1024];
  254. uint32_t *p;
  255. int len;
  256. int fnamelen;
  257. fnamelen = strlen(fname);
  258. p = &(data[0]);
  259. p = (uint32_t *)rpc_add_credentials((long *)p);
  260. memcpy(p, dirfh, NFS_FHSIZE);
  261. p += (NFS_FHSIZE / 4);
  262. *p++ = htonl(fnamelen);
  263. if (fnamelen & 3)
  264. *(p + fnamelen / 4) = 0;
  265. memcpy(p, fname, fnamelen);
  266. p += (fnamelen + 3) / 4;
  267. len = (uint32_t *)p - (uint32_t *)&(data[0]);
  268. rpc_req(PROG_NFS, NFS_LOOKUP, data, len);
  269. }
  270. /**************************************************************************
  271. NFS_READ - Read File on NFS Server
  272. **************************************************************************/
  273. static void
  274. nfs_read_req(int offset, int readlen)
  275. {
  276. uint32_t data[1024];
  277. uint32_t *p;
  278. int len;
  279. p = &(data[0]);
  280. p = (uint32_t *)rpc_add_credentials((long *)p);
  281. memcpy(p, filefh, NFS_FHSIZE);
  282. p += (NFS_FHSIZE / 4);
  283. *p++ = htonl(offset);
  284. *p++ = htonl(readlen);
  285. *p++ = 0;
  286. len = (uint32_t *)p - (uint32_t *)&(data[0]);
  287. rpc_req(PROG_NFS, NFS_READ, data, len);
  288. }
  289. /**************************************************************************
  290. RPC request dispatcher
  291. **************************************************************************/
  292. static void
  293. NfsSend(void)
  294. {
  295. debug("%s\n", __func__);
  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. debug("%s\n", __func__);
  329. if (ntohl(rpc_pkt.u.reply.id) != rpc_id)
  330. return -1;
  331. if (rpc_pkt.u.reply.rstatus ||
  332. rpc_pkt.u.reply.verifier ||
  333. rpc_pkt.u.reply.astatus)
  334. return -1;
  335. switch (prog) {
  336. case PROG_MOUNT:
  337. NfsSrvMountPort = ntohl(rpc_pkt.u.reply.data[0]);
  338. break;
  339. case PROG_NFS:
  340. NfsSrvNfsPort = ntohl(rpc_pkt.u.reply.data[0]);
  341. break;
  342. }
  343. return 0;
  344. }
  345. static int
  346. nfs_mount_reply(uchar *pkt, unsigned len)
  347. {
  348. struct rpc_t rpc_pkt;
  349. debug("%s\n", __func__);
  350. memcpy((unsigned char *)&rpc_pkt, pkt, len);
  351. if (ntohl(rpc_pkt.u.reply.id) != rpc_id)
  352. return -1;
  353. if (rpc_pkt.u.reply.rstatus ||
  354. rpc_pkt.u.reply.verifier ||
  355. rpc_pkt.u.reply.astatus ||
  356. rpc_pkt.u.reply.data[0])
  357. return -1;
  358. fs_mounted = 1;
  359. memcpy(dirfh, rpc_pkt.u.reply.data + 1, NFS_FHSIZE);
  360. return 0;
  361. }
  362. static int
  363. nfs_umountall_reply(uchar *pkt, unsigned len)
  364. {
  365. struct rpc_t rpc_pkt;
  366. debug("%s\n", __func__);
  367. memcpy((unsigned char *)&rpc_pkt, pkt, len);
  368. if (ntohl(rpc_pkt.u.reply.id) != rpc_id)
  369. return -1;
  370. if (rpc_pkt.u.reply.rstatus ||
  371. rpc_pkt.u.reply.verifier ||
  372. rpc_pkt.u.reply.astatus)
  373. return -1;
  374. fs_mounted = 0;
  375. memset(dirfh, 0, sizeof(dirfh));
  376. return 0;
  377. }
  378. static int
  379. nfs_lookup_reply(uchar *pkt, unsigned len)
  380. {
  381. struct rpc_t rpc_pkt;
  382. debug("%s\n", __func__);
  383. memcpy((unsigned char *)&rpc_pkt, pkt, len);
  384. if (ntohl(rpc_pkt.u.reply.id) != rpc_id)
  385. return -1;
  386. if (rpc_pkt.u.reply.rstatus ||
  387. rpc_pkt.u.reply.verifier ||
  388. rpc_pkt.u.reply.astatus ||
  389. rpc_pkt.u.reply.data[0])
  390. return -1;
  391. memcpy(filefh, rpc_pkt.u.reply.data + 1, NFS_FHSIZE);
  392. return 0;
  393. }
  394. static int
  395. nfs_readlink_reply(uchar *pkt, unsigned len)
  396. {
  397. struct rpc_t rpc_pkt;
  398. int rlen;
  399. debug("%s\n", __func__);
  400. memcpy((unsigned char *)&rpc_pkt, pkt, len);
  401. if (ntohl(rpc_pkt.u.reply.id) != rpc_id)
  402. return -1;
  403. if (rpc_pkt.u.reply.rstatus ||
  404. rpc_pkt.u.reply.verifier ||
  405. rpc_pkt.u.reply.astatus ||
  406. rpc_pkt.u.reply.data[0])
  407. return -1;
  408. rlen = ntohl(rpc_pkt.u.reply.data[1]); /* new path length */
  409. if (*((char *)&(rpc_pkt.u.reply.data[2])) != '/') {
  410. int pathlen;
  411. strcat(nfs_path, "/");
  412. pathlen = strlen(nfs_path);
  413. memcpy(nfs_path + pathlen, (uchar *)&(rpc_pkt.u.reply.data[2]),
  414. rlen);
  415. nfs_path[pathlen + rlen] = 0;
  416. } else {
  417. memcpy(nfs_path, (uchar *)&(rpc_pkt.u.reply.data[2]), rlen);
  418. nfs_path[rlen] = 0;
  419. }
  420. return 0;
  421. }
  422. static int
  423. nfs_read_reply(uchar *pkt, unsigned len)
  424. {
  425. struct rpc_t rpc_pkt;
  426. int rlen;
  427. debug("%s\n", __func__);
  428. memcpy((uchar *)&rpc_pkt, pkt, sizeof(rpc_pkt.u.reply));
  429. if (ntohl(rpc_pkt.u.reply.id) != rpc_id)
  430. return -1;
  431. if (rpc_pkt.u.reply.rstatus ||
  432. rpc_pkt.u.reply.verifier ||
  433. rpc_pkt.u.reply.astatus ||
  434. rpc_pkt.u.reply.data[0]) {
  435. if (rpc_pkt.u.reply.rstatus)
  436. return -9999;
  437. if (rpc_pkt.u.reply.astatus)
  438. return -9999;
  439. return -ntohl(rpc_pkt.u.reply.data[0]);
  440. }
  441. if ((nfs_offset != 0) && !((nfs_offset) %
  442. (NFS_READ_SIZE / 2 * 10 * HASHES_PER_LINE)))
  443. puts("\n\t ");
  444. if (!(nfs_offset % ((NFS_READ_SIZE / 2) * 10)))
  445. putc('#');
  446. rlen = ntohl(rpc_pkt.u.reply.data[18]);
  447. if (store_block((uchar *)pkt + sizeof(rpc_pkt.u.reply),
  448. nfs_offset, rlen))
  449. return -9999;
  450. return rlen;
  451. }
  452. /**************************************************************************
  453. Interfaces of U-BOOT
  454. **************************************************************************/
  455. static void
  456. NfsTimeout(void)
  457. {
  458. if (++NfsTimeoutCount > NFS_RETRY_COUNT) {
  459. puts("\nRetry count exceeded; starting again\n");
  460. NetStartAgain();
  461. } else {
  462. puts("T ");
  463. NetSetTimeout(NFS_TIMEOUT, NfsTimeout);
  464. NfsSend();
  465. }
  466. }
  467. static void
  468. NfsHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, unsigned len)
  469. {
  470. int rlen;
  471. debug("%s\n", __func__);
  472. if (dest != NfsOurPort)
  473. return;
  474. switch (NfsState) {
  475. case STATE_PRCLOOKUP_PROG_MOUNT_REQ:
  476. rpc_lookup_reply(PROG_MOUNT, pkt, len);
  477. NfsState = STATE_PRCLOOKUP_PROG_NFS_REQ;
  478. NfsSend();
  479. break;
  480. case STATE_PRCLOOKUP_PROG_NFS_REQ:
  481. rpc_lookup_reply(PROG_NFS, pkt, len);
  482. NfsState = STATE_MOUNT_REQ;
  483. NfsSend();
  484. break;
  485. case STATE_MOUNT_REQ:
  486. if (nfs_mount_reply(pkt, len)) {
  487. puts("*** ERROR: Cannot mount\n");
  488. /* just to be sure... */
  489. NfsState = STATE_UMOUNT_REQ;
  490. NfsSend();
  491. } else {
  492. NfsState = STATE_LOOKUP_REQ;
  493. NfsSend();
  494. }
  495. break;
  496. case STATE_UMOUNT_REQ:
  497. if (nfs_umountall_reply(pkt, len)) {
  498. puts("*** ERROR: Cannot umount\n");
  499. net_set_state(NETLOOP_FAIL);
  500. } else {
  501. puts("\ndone\n");
  502. net_set_state(nfs_download_state);
  503. }
  504. break;
  505. case STATE_LOOKUP_REQ:
  506. if (nfs_lookup_reply(pkt, len)) {
  507. puts("*** ERROR: File lookup fail\n");
  508. NfsState = STATE_UMOUNT_REQ;
  509. NfsSend();
  510. } else {
  511. NfsState = STATE_READ_REQ;
  512. nfs_offset = 0;
  513. nfs_len = NFS_READ_SIZE;
  514. NfsSend();
  515. }
  516. break;
  517. case STATE_READLINK_REQ:
  518. if (nfs_readlink_reply(pkt, len)) {
  519. puts("*** ERROR: Symlink fail\n");
  520. NfsState = STATE_UMOUNT_REQ;
  521. NfsSend();
  522. } else {
  523. debug("Symlink --> %s\n", nfs_path);
  524. nfs_filename = basename(nfs_path);
  525. nfs_path = dirname(nfs_path);
  526. NfsState = STATE_MOUNT_REQ;
  527. NfsSend();
  528. }
  529. break;
  530. case STATE_READ_REQ:
  531. rlen = nfs_read_reply(pkt, len);
  532. NetSetTimeout(NFS_TIMEOUT, NfsTimeout);
  533. if (rlen > 0) {
  534. nfs_offset += rlen;
  535. NfsSend();
  536. } else if ((rlen == -NFSERR_ISDIR) || (rlen == -NFSERR_INVAL)) {
  537. /* symbolic link */
  538. NfsState = STATE_READLINK_REQ;
  539. NfsSend();
  540. } else {
  541. if (!rlen)
  542. nfs_download_state = NETLOOP_SUCCESS;
  543. NfsState = STATE_UMOUNT_REQ;
  544. NfsSend();
  545. }
  546. break;
  547. }
  548. }
  549. void
  550. NfsStart(void)
  551. {
  552. debug("%s\n", __func__);
  553. nfs_download_state = NETLOOP_FAIL;
  554. NfsServerIP = NetServerIP;
  555. nfs_path = (char *)nfs_path_buff;
  556. if (nfs_path == NULL) {
  557. net_set_state(NETLOOP_FAIL);
  558. puts("*** ERROR: Fail allocate memory\n");
  559. return;
  560. }
  561. if (BootFile[0] == '\0') {
  562. sprintf(default_filename, "/nfsroot/%02X%02X%02X%02X.img",
  563. NetOurIP & 0xFF,
  564. (NetOurIP >> 8) & 0xFF,
  565. (NetOurIP >> 16) & 0xFF,
  566. (NetOurIP >> 24) & 0xFF);
  567. strcpy(nfs_path, default_filename);
  568. printf("*** Warning: no boot file name; using '%s'\n",
  569. nfs_path);
  570. } else {
  571. char *p = BootFile;
  572. p = strchr(p, ':');
  573. if (p != NULL) {
  574. NfsServerIP = string_to_ip(BootFile);
  575. ++p;
  576. strcpy(nfs_path, p);
  577. } else {
  578. strcpy(nfs_path, BootFile);
  579. }
  580. }
  581. nfs_filename = basename(nfs_path);
  582. nfs_path = dirname(nfs_path);
  583. printf("Using %s device\n", eth_get_name());
  584. printf("File transfer via NFS from server %pI4"
  585. "; our IP address is %pI4", &NfsServerIP, &NetOurIP);
  586. /* Check if we need to send across this subnet */
  587. if (NetOurGatewayIP && NetOurSubnetMask) {
  588. IPaddr_t OurNet = NetOurIP & NetOurSubnetMask;
  589. IPaddr_t ServerNet = NetServerIP & NetOurSubnetMask;
  590. if (OurNet != ServerNet)
  591. printf("; sending through gateway %pI4",
  592. &NetOurGatewayIP);
  593. }
  594. printf("\nFilename '%s/%s'.", nfs_path, nfs_filename);
  595. if (NetBootFileSize) {
  596. printf(" Size is 0x%x Bytes = ", NetBootFileSize<<9);
  597. print_size(NetBootFileSize<<9, "");
  598. }
  599. printf("\nLoad address: 0x%lx\n"
  600. "Loading: *\b", load_addr);
  601. NetSetTimeout(NFS_TIMEOUT, NfsTimeout);
  602. net_set_udp_handler(NfsHandler);
  603. NfsTimeoutCount = 0;
  604. NfsState = STATE_PRCLOOKUP_PROG_MOUNT_REQ;
  605. /*NfsOurPort = 4096 + (get_ticks() % 3072);*/
  606. /*FIX ME !!!*/
  607. NfsOurPort = 1000;
  608. /* zero out server ether in case the server ip has changed */
  609. memset(NetServerEther, 0, 6);
  610. NfsSend();
  611. }