nfs.c 19 KB

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