nfs.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927
  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. /* NOTE 4: NFSv3 support added by Guillaume GARDET, 2016-June-20.
  22. * NFSv2 is still used by default. But if server does not support NFSv2, then
  23. * NFSv3 is used, if available on NFS server. */
  24. #include <common.h>
  25. #include <command.h>
  26. #include <net.h>
  27. #include <malloc.h>
  28. #include <mapmem.h>
  29. #include "nfs.h"
  30. #include "bootp.h"
  31. #define HASHES_PER_LINE 65 /* Number of "loading" hashes per line */
  32. #define NFS_RETRY_COUNT 30
  33. #ifndef CONFIG_NFS_TIMEOUT
  34. # define NFS_TIMEOUT 2000UL
  35. #else
  36. # define NFS_TIMEOUT CONFIG_NFS_TIMEOUT
  37. #endif
  38. #define NFS_RPC_ERR 1
  39. #define NFS_RPC_DROP 124
  40. static int fs_mounted;
  41. static unsigned long rpc_id;
  42. static int nfs_offset = -1;
  43. static int nfs_len;
  44. static ulong nfs_timeout = NFS_TIMEOUT;
  45. static char dirfh[NFS_FHSIZE]; /* NFSv2 / NFSv3 file handle of directory */
  46. static char filefh[NFS3_FHSIZE]; /* NFSv2 / NFSv3 file handle */
  47. static int filefh3_length; /* (variable) length of filefh when NFSv3 */
  48. static enum net_loop_state nfs_download_state;
  49. static struct in_addr nfs_server_ip;
  50. static int nfs_server_mount_port;
  51. static int nfs_server_port;
  52. static int nfs_our_port;
  53. static int nfs_timeout_count;
  54. static int nfs_state;
  55. #define STATE_PRCLOOKUP_PROG_MOUNT_REQ 1
  56. #define STATE_PRCLOOKUP_PROG_NFS_REQ 2
  57. #define STATE_MOUNT_REQ 3
  58. #define STATE_UMOUNT_REQ 4
  59. #define STATE_LOOKUP_REQ 5
  60. #define STATE_READ_REQ 6
  61. #define STATE_READLINK_REQ 7
  62. static char *nfs_filename;
  63. static char *nfs_path;
  64. static char nfs_path_buff[2048];
  65. #define NFSV2_FLAG 1
  66. #define NFSV3_FLAG 1 << 1
  67. static char supported_nfs_versions = NFSV2_FLAG | NFSV3_FLAG;
  68. static inline int store_block(uchar *src, unsigned offset, unsigned len)
  69. {
  70. ulong newsize = offset + len;
  71. #ifdef CONFIG_SYS_DIRECT_FLASH_NFS
  72. int i, rc = 0;
  73. for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
  74. /* start address in flash? */
  75. if (load_addr + offset >= flash_info[i].start[0]) {
  76. rc = 1;
  77. break;
  78. }
  79. }
  80. if (rc) { /* Flash is destination for this packet */
  81. rc = flash_write((uchar *)src, (ulong)(load_addr+offset), len);
  82. if (rc) {
  83. flash_perror(rc);
  84. return -1;
  85. }
  86. } else
  87. #endif /* CONFIG_SYS_DIRECT_FLASH_NFS */
  88. {
  89. void *ptr = map_sysmem(load_addr + offset, len);
  90. memcpy(ptr, src, len);
  91. unmap_sysmem(ptr);
  92. }
  93. if (net_boot_file_size < (offset + len))
  94. net_boot_file_size = newsize;
  95. return 0;
  96. }
  97. static char *basename(char *path)
  98. {
  99. char *fname;
  100. fname = path + strlen(path) - 1;
  101. while (fname >= path) {
  102. if (*fname == '/') {
  103. fname++;
  104. break;
  105. }
  106. fname--;
  107. }
  108. return fname;
  109. }
  110. static char *dirname(char *path)
  111. {
  112. char *fname;
  113. fname = basename(path);
  114. --fname;
  115. *fname = '\0';
  116. return path;
  117. }
  118. /**************************************************************************
  119. RPC_ADD_CREDENTIALS - Add RPC authentication/verifier entries
  120. **************************************************************************/
  121. static uint32_t *rpc_add_credentials(uint32_t *p)
  122. {
  123. /* Here's the executive summary on authentication requirements of the
  124. * various NFS server implementations: Linux accepts both AUTH_NONE
  125. * and AUTH_UNIX authentication (also accepts an empty hostname field
  126. * in the AUTH_UNIX scheme). *BSD refuses AUTH_NONE, but accepts
  127. * AUTH_UNIX (also accepts an empty hostname field in the AUTH_UNIX
  128. * scheme). To be safe, use AUTH_UNIX and pass the hostname if we have
  129. * it (if the BOOTP/DHCP reply didn't give one, just use an empty
  130. * hostname). */
  131. /* Provide an AUTH_UNIX credential. */
  132. *p++ = htonl(1); /* AUTH_UNIX */
  133. *p++ = htonl(20); /* auth length */
  134. *p++ = 0; /* stamp */
  135. *p++ = 0; /* hostname string */
  136. *p++ = 0; /* uid */
  137. *p++ = 0; /* gid */
  138. *p++ = 0; /* auxiliary gid list */
  139. /* Provide an AUTH_NONE verifier. */
  140. *p++ = 0; /* AUTH_NONE */
  141. *p++ = 0; /* auth length */
  142. return p;
  143. }
  144. /**************************************************************************
  145. RPC_LOOKUP - Lookup RPC Port numbers
  146. **************************************************************************/
  147. static struct rpc_t *rpc_req_prep(void)
  148. {
  149. return (struct rpc_t *)(net_tx_packet + net_eth_hdr_size() +
  150. IP_UDP_HDR_SIZE);
  151. }
  152. static void rpc_req(int rpc_prog, int rpc_proc, struct rpc_t *rpc_pkt,
  153. int datalen)
  154. {
  155. unsigned long id;
  156. int pktlen;
  157. int sport;
  158. id = ++rpc_id;
  159. rpc_pkt->u.call.id = htonl(id);
  160. rpc_pkt->u.call.type = htonl(MSG_CALL);
  161. rpc_pkt->u.call.rpcvers = htonl(2); /* use RPC version 2 */
  162. rpc_pkt->u.call.prog = htonl(rpc_prog);
  163. switch (rpc_prog) {
  164. case PROG_NFS:
  165. if (supported_nfs_versions & NFSV2_FLAG)
  166. rpc_pkt->u.call.vers = htonl(2); /* NFS v2 */
  167. else /* NFSV3_FLAG */
  168. rpc_pkt->u.call.vers = htonl(3); /* NFS v3 */
  169. break;
  170. case PROG_PORTMAP:
  171. case PROG_MOUNT:
  172. default:
  173. /* portmapper is version 2 */
  174. rpc_pkt->u.call.vers = htonl(2);
  175. }
  176. rpc_pkt->u.call.proc = htonl(rpc_proc);
  177. pktlen = ((char *)&rpc_pkt->u.call.data - (char *)&rpc_pkt) +
  178. datalen * sizeof(uint32_t);
  179. if (rpc_prog == PROG_PORTMAP)
  180. sport = SUNRPC_PORT;
  181. else if (rpc_prog == PROG_MOUNT)
  182. sport = nfs_server_mount_port;
  183. else
  184. sport = nfs_server_port;
  185. net_send_udp_packet(net_server_ethaddr, nfs_server_ip, sport,
  186. nfs_our_port, pktlen);
  187. }
  188. /**************************************************************************
  189. RPC_LOOKUP - Lookup RPC Port numbers
  190. **************************************************************************/
  191. static void rpc_lookup_req(int prog, int ver)
  192. {
  193. uint32_t *data;
  194. struct rpc_t *rpc_pkt = rpc_req_prep();
  195. data = rpc_pkt->u.call.data;
  196. data[0] = 0; data[1] = 0; /* auth credential */
  197. data[2] = 0; data[3] = 0; /* auth verifier */
  198. data[4] = htonl(prog);
  199. data[5] = htonl(ver);
  200. data[6] = htonl(17); /* IP_UDP */
  201. data[7] = 0;
  202. rpc_req(PROG_PORTMAP, PORTMAP_GETPORT, rpc_pkt, 8);
  203. }
  204. /**************************************************************************
  205. NFS_MOUNT - Mount an NFS Filesystem
  206. **************************************************************************/
  207. static void nfs_mount_req(char *path)
  208. {
  209. uint32_t *p;
  210. int len;
  211. int pathlen;
  212. struct rpc_t *rpc_pkt = rpc_req_prep();
  213. pathlen = strlen(path);
  214. p = rpc_pkt->u.call.data;
  215. p = rpc_add_credentials(p);
  216. *p++ = htonl(pathlen);
  217. if (pathlen & 3)
  218. *(p + pathlen / 4) = 0;
  219. memcpy(p, path, pathlen);
  220. p += (pathlen + 3) / 4;
  221. len = (uint32_t *)p - (uint32_t *)&(rpc_pkt->u.call.data);
  222. rpc_req(PROG_MOUNT, MOUNT_ADDENTRY, rpc_pkt, len);
  223. }
  224. /**************************************************************************
  225. NFS_UMOUNTALL - Unmount all our NFS Filesystems on the Server
  226. **************************************************************************/
  227. static void nfs_umountall_req(void)
  228. {
  229. uint32_t *p;
  230. int len;
  231. struct rpc_t *rpc_pkt = rpc_req_prep();
  232. if ((nfs_server_mount_port == -1) || (!fs_mounted))
  233. /* Nothing mounted, nothing to umount */
  234. return;
  235. p = rpc_pkt->u.call.data;
  236. p = rpc_add_credentials(p);
  237. len = (uint32_t *)p - (uint32_t *)&(rpc_pkt->u.call.data);
  238. rpc_req(PROG_MOUNT, MOUNT_UMOUNTALL, rpc_pkt, len);
  239. }
  240. /***************************************************************************
  241. * NFS_READLINK (AH 2003-07-14)
  242. * This procedure is called when read of the first block fails -
  243. * this probably happens when it's a directory or a symlink
  244. * In case of successful readlink(), the dirname is manipulated,
  245. * so that inside the nfs() function a recursion can be done.
  246. **************************************************************************/
  247. static void nfs_readlink_req(void)
  248. {
  249. uint32_t *p;
  250. int len;
  251. struct rpc_t *rpc_pkt = rpc_req_prep();
  252. p = rpc_pkt->u.call.data;
  253. p = rpc_add_credentials(p);
  254. if (supported_nfs_versions & NFSV2_FLAG) {
  255. memcpy(p, filefh, NFS_FHSIZE);
  256. p += (NFS_FHSIZE / 4);
  257. } else { /* NFSV3_FLAG */
  258. *p++ = htonl(filefh3_length);
  259. memcpy(p, filefh, filefh3_length);
  260. p += (filefh3_length / 4);
  261. }
  262. len = (uint32_t *)p - (uint32_t *)&(rpc_pkt->u.call.data);
  263. rpc_req(PROG_NFS, NFS_READLINK, rpc_pkt, len);
  264. }
  265. /**************************************************************************
  266. NFS_LOOKUP - Lookup Pathname
  267. **************************************************************************/
  268. static void nfs_lookup_req(char *fname)
  269. {
  270. uint32_t *p;
  271. int len;
  272. int fnamelen;
  273. struct rpc_t *rpc_pkt = rpc_req_prep();
  274. fnamelen = strlen(fname);
  275. p = rpc_pkt->u.call.data;
  276. p = rpc_add_credentials(p);
  277. if (supported_nfs_versions & NFSV2_FLAG) {
  278. memcpy(p, dirfh, NFS_FHSIZE);
  279. p += (NFS_FHSIZE / 4);
  280. *p++ = htonl(fnamelen);
  281. if (fnamelen & 3)
  282. *(p + fnamelen / 4) = 0;
  283. memcpy(p, fname, fnamelen);
  284. p += (fnamelen + 3) / 4;
  285. len = (uint32_t *)p - (uint32_t *)&(rpc_pkt->u.call.data);
  286. rpc_req(PROG_NFS, NFS_LOOKUP, rpc_pkt, len);
  287. } else { /* NFSV3_FLAG */
  288. *p++ = htonl(NFS_FHSIZE); /* Dir handle length */
  289. memcpy(p, dirfh, NFS_FHSIZE);
  290. p += (NFS_FHSIZE / 4);
  291. *p++ = htonl(fnamelen);
  292. if (fnamelen & 3)
  293. *(p + fnamelen / 4) = 0;
  294. memcpy(p, fname, fnamelen);
  295. p += (fnamelen + 3) / 4;
  296. len = (uint32_t *)p - (uint32_t *)&(rpc_pkt->u.call.data);
  297. rpc_req(PROG_NFS, NFS3PROC_LOOKUP, rpc_pkt, len);
  298. }
  299. }
  300. /**************************************************************************
  301. NFS_READ - Read File on NFS Server
  302. **************************************************************************/
  303. static void nfs_read_req(int offset, int readlen)
  304. {
  305. uint32_t *p;
  306. int len;
  307. struct rpc_t *rpc_pkt = rpc_req_prep();
  308. p = rpc_pkt->u.call.data;
  309. p = rpc_add_credentials(p);
  310. if (supported_nfs_versions & NFSV2_FLAG) {
  311. memcpy(p, filefh, NFS_FHSIZE);
  312. p += (NFS_FHSIZE / 4);
  313. *p++ = htonl(offset);
  314. *p++ = htonl(readlen);
  315. *p++ = 0;
  316. } else { /* NFSV3_FLAG */
  317. *p++ = htonl(filefh3_length);
  318. memcpy(p, filefh, filefh3_length);
  319. p += (filefh3_length / 4);
  320. *p++ = htonl(0); /* offset is 64-bit long, so fill with 0 */
  321. *p++ = htonl(offset);
  322. *p++ = htonl(readlen);
  323. *p++ = 0;
  324. }
  325. len = (uint32_t *)p - (uint32_t *)&(rpc_pkt->u.call.data);
  326. rpc_req(PROG_NFS, NFS_READ, rpc_pkt, len);
  327. }
  328. /**************************************************************************
  329. RPC request dispatcher
  330. **************************************************************************/
  331. static void nfs_send(void)
  332. {
  333. debug("%s\n", __func__);
  334. switch (nfs_state) {
  335. case STATE_PRCLOOKUP_PROG_MOUNT_REQ:
  336. if (supported_nfs_versions & NFSV2_FLAG)
  337. rpc_lookup_req(PROG_MOUNT, 1);
  338. else /* NFSV3_FLAG */
  339. rpc_lookup_req(PROG_MOUNT, 3);
  340. break;
  341. case STATE_PRCLOOKUP_PROG_NFS_REQ:
  342. if (supported_nfs_versions & NFSV2_FLAG)
  343. rpc_lookup_req(PROG_NFS, 2);
  344. else /* NFSV3_FLAG */
  345. rpc_lookup_req(PROG_NFS, 3);
  346. break;
  347. case STATE_MOUNT_REQ:
  348. nfs_mount_req(nfs_path);
  349. break;
  350. case STATE_UMOUNT_REQ:
  351. nfs_umountall_req();
  352. break;
  353. case STATE_LOOKUP_REQ:
  354. nfs_lookup_req(nfs_filename);
  355. break;
  356. case STATE_READ_REQ:
  357. nfs_read_req(nfs_offset, nfs_len);
  358. break;
  359. case STATE_READLINK_REQ:
  360. nfs_readlink_req();
  361. break;
  362. }
  363. }
  364. /**************************************************************************
  365. Handlers for the reply from server
  366. **************************************************************************/
  367. static int rpc_lookup_reply(int prog, uchar *pkt, unsigned len)
  368. {
  369. struct rpc_t rpc_pkt;
  370. memcpy(&rpc_pkt.u.data[0], pkt, len);
  371. debug("%s\n", __func__);
  372. if (ntohl(rpc_pkt.u.reply.id) > rpc_id)
  373. return -NFS_RPC_ERR;
  374. else if (ntohl(rpc_pkt.u.reply.id) < rpc_id)
  375. return -NFS_RPC_DROP;
  376. if (rpc_pkt.u.reply.rstatus ||
  377. rpc_pkt.u.reply.verifier ||
  378. rpc_pkt.u.reply.astatus)
  379. return -1;
  380. switch (prog) {
  381. case PROG_MOUNT:
  382. nfs_server_mount_port = ntohl(rpc_pkt.u.reply.data[0]);
  383. break;
  384. case PROG_NFS:
  385. nfs_server_port = ntohl(rpc_pkt.u.reply.data[0]);
  386. break;
  387. }
  388. return 0;
  389. }
  390. static int nfs_mount_reply(uchar *pkt, unsigned len)
  391. {
  392. struct rpc_t rpc_pkt;
  393. debug("%s\n", __func__);
  394. memcpy(&rpc_pkt.u.data[0], pkt, len);
  395. if (ntohl(rpc_pkt.u.reply.id) > rpc_id)
  396. return -NFS_RPC_ERR;
  397. else if (ntohl(rpc_pkt.u.reply.id) < rpc_id)
  398. return -NFS_RPC_DROP;
  399. if (rpc_pkt.u.reply.rstatus ||
  400. rpc_pkt.u.reply.verifier ||
  401. rpc_pkt.u.reply.astatus ||
  402. rpc_pkt.u.reply.data[0])
  403. return -1;
  404. fs_mounted = 1;
  405. /* NFSv2 and NFSv3 use same structure */
  406. memcpy(dirfh, rpc_pkt.u.reply.data + 1, NFS_FHSIZE);
  407. return 0;
  408. }
  409. static int nfs_umountall_reply(uchar *pkt, unsigned len)
  410. {
  411. struct rpc_t rpc_pkt;
  412. debug("%s\n", __func__);
  413. memcpy(&rpc_pkt.u.data[0], pkt, len);
  414. if (ntohl(rpc_pkt.u.reply.id) > rpc_id)
  415. return -NFS_RPC_ERR;
  416. else if (ntohl(rpc_pkt.u.reply.id) < rpc_id)
  417. return -NFS_RPC_DROP;
  418. if (rpc_pkt.u.reply.rstatus ||
  419. rpc_pkt.u.reply.verifier ||
  420. rpc_pkt.u.reply.astatus)
  421. return -1;
  422. fs_mounted = 0;
  423. memset(dirfh, 0, sizeof(dirfh));
  424. return 0;
  425. }
  426. static int nfs_lookup_reply(uchar *pkt, unsigned len)
  427. {
  428. struct rpc_t rpc_pkt;
  429. debug("%s\n", __func__);
  430. memcpy(&rpc_pkt.u.data[0], pkt, len);
  431. if (ntohl(rpc_pkt.u.reply.id) > rpc_id)
  432. return -NFS_RPC_ERR;
  433. else if (ntohl(rpc_pkt.u.reply.id) < rpc_id)
  434. return -NFS_RPC_DROP;
  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. switch (ntohl(rpc_pkt.u.reply.astatus)) {
  440. case NFS_RPC_SUCCESS: /* Not an error */
  441. break;
  442. case NFS_RPC_PROG_MISMATCH:
  443. /* Remote can't support NFS version */
  444. switch (ntohl(rpc_pkt.u.reply.data[0])) {
  445. /* Minimal supported NFS version */
  446. case 3:
  447. debug("*** Waring: NFS version not supported: Requested: V%d, accepted: min V%d - max V%d\n",
  448. (supported_nfs_versions & NFSV2_FLAG) ?
  449. 2 : 3,
  450. ntohl(rpc_pkt.u.reply.data[0]),
  451. ntohl(rpc_pkt.u.reply.data[1]));
  452. debug("Will retry with NFSv3\n");
  453. /* Clear NFSV2_FLAG from supported versions */
  454. supported_nfs_versions &= ~NFSV2_FLAG;
  455. return -NFS_RPC_PROG_MISMATCH;
  456. case 4:
  457. default:
  458. puts("*** ERROR: NFS version not supported");
  459. debug(": Requested: V%d, accepted: min V%d - max V%d\n",
  460. (supported_nfs_versions & NFSV2_FLAG) ?
  461. 2 : 3,
  462. ntohl(rpc_pkt.u.reply.data[0]),
  463. ntohl(rpc_pkt.u.reply.data[1]));
  464. puts("\n");
  465. }
  466. break;
  467. case NFS_RPC_PROG_UNAVAIL:
  468. case NFS_RPC_PROC_UNAVAIL:
  469. case NFS_RPC_GARBAGE_ARGS:
  470. case NFS_RPC_SYSTEM_ERR:
  471. default: /* Unknown error on 'accept state' flag */
  472. debug("*** ERROR: accept state error (%d)\n",
  473. ntohl(rpc_pkt.u.reply.astatus));
  474. break;
  475. }
  476. return -1;
  477. }
  478. if (supported_nfs_versions & NFSV2_FLAG) {
  479. memcpy(filefh, rpc_pkt.u.reply.data + 1, NFS_FHSIZE);
  480. } else { /* NFSV3_FLAG */
  481. filefh3_length = ntohl(rpc_pkt.u.reply.data[1]);
  482. if (filefh3_length > NFS3_FHSIZE)
  483. filefh3_length = NFS3_FHSIZE;
  484. memcpy(filefh, rpc_pkt.u.reply.data + 2, filefh3_length);
  485. }
  486. return 0;
  487. }
  488. static int nfs3_get_attributes_offset(uint32_t *data)
  489. {
  490. if (ntohl(data[1]) != 0) {
  491. /* 'attributes_follow' flag is TRUE,
  492. * so we have attributes on 21 dwords */
  493. /* Skip unused values :
  494. type; 32 bits value,
  495. mode; 32 bits value,
  496. nlink; 32 bits value,
  497. uid; 32 bits value,
  498. gid; 32 bits value,
  499. size; 64 bits value,
  500. used; 64 bits value,
  501. rdev; 64 bits value,
  502. fsid; 64 bits value,
  503. fileid; 64 bits value,
  504. atime; 64 bits value,
  505. mtime; 64 bits value,
  506. ctime; 64 bits value,
  507. */
  508. return 22;
  509. } else {
  510. /* 'attributes_follow' flag is FALSE,
  511. * so we don't have any attributes */
  512. return 1;
  513. }
  514. }
  515. static int nfs_readlink_reply(uchar *pkt, unsigned len)
  516. {
  517. struct rpc_t rpc_pkt;
  518. int rlen;
  519. int nfsv3_data_offset = 0;
  520. debug("%s\n", __func__);
  521. memcpy((unsigned char *)&rpc_pkt, pkt, len);
  522. if (ntohl(rpc_pkt.u.reply.id) > rpc_id)
  523. return -NFS_RPC_ERR;
  524. else if (ntohl(rpc_pkt.u.reply.id) < rpc_id)
  525. return -NFS_RPC_DROP;
  526. if (rpc_pkt.u.reply.rstatus ||
  527. rpc_pkt.u.reply.verifier ||
  528. rpc_pkt.u.reply.astatus ||
  529. rpc_pkt.u.reply.data[0])
  530. return -1;
  531. if (!(supported_nfs_versions & NFSV2_FLAG)) { /* NFSV3_FLAG */
  532. nfsv3_data_offset =
  533. nfs3_get_attributes_offset(rpc_pkt.u.reply.data);
  534. }
  535. /* new path length */
  536. rlen = ntohl(rpc_pkt.u.reply.data[1 + nfsv3_data_offset]);
  537. if (*((char *)&(rpc_pkt.u.reply.data[2 + nfsv3_data_offset])) != '/') {
  538. int pathlen;
  539. strcat(nfs_path, "/");
  540. pathlen = strlen(nfs_path);
  541. memcpy(nfs_path + pathlen,
  542. (uchar *)&(rpc_pkt.u.reply.data[2 + nfsv3_data_offset]),
  543. rlen);
  544. nfs_path[pathlen + rlen] = 0;
  545. } else {
  546. memcpy(nfs_path,
  547. (uchar *)&(rpc_pkt.u.reply.data[2 + nfsv3_data_offset]),
  548. rlen);
  549. nfs_path[rlen] = 0;
  550. }
  551. return 0;
  552. }
  553. static int nfs_read_reply(uchar *pkt, unsigned len)
  554. {
  555. struct rpc_t rpc_pkt;
  556. int rlen;
  557. uchar *data_ptr;
  558. debug("%s\n", __func__);
  559. memcpy(&rpc_pkt.u.data[0], pkt, sizeof(rpc_pkt.u.reply));
  560. if (ntohl(rpc_pkt.u.reply.id) > rpc_id)
  561. return -NFS_RPC_ERR;
  562. else if (ntohl(rpc_pkt.u.reply.id) < rpc_id)
  563. return -NFS_RPC_DROP;
  564. if (rpc_pkt.u.reply.rstatus ||
  565. rpc_pkt.u.reply.verifier ||
  566. rpc_pkt.u.reply.astatus ||
  567. rpc_pkt.u.reply.data[0]) {
  568. if (rpc_pkt.u.reply.rstatus)
  569. return -9999;
  570. if (rpc_pkt.u.reply.astatus)
  571. return -9999;
  572. return -ntohl(rpc_pkt.u.reply.data[0]);
  573. }
  574. if ((nfs_offset != 0) && !((nfs_offset) %
  575. (NFS_READ_SIZE / 2 * 10 * HASHES_PER_LINE)))
  576. puts("\n\t ");
  577. if (!(nfs_offset % ((NFS_READ_SIZE / 2) * 10)))
  578. putc('#');
  579. if (supported_nfs_versions & NFSV2_FLAG) {
  580. rlen = ntohl(rpc_pkt.u.reply.data[18]);
  581. data_ptr = (uchar *)&(rpc_pkt.u.reply.data[19]);
  582. } else { /* NFSV3_FLAG */
  583. int nfsv3_data_offset =
  584. nfs3_get_attributes_offset(rpc_pkt.u.reply.data);
  585. /* count value */
  586. rlen = ntohl(rpc_pkt.u.reply.data[1 + nfsv3_data_offset]);
  587. /* Skip unused values :
  588. EOF: 32 bits value,
  589. data_size: 32 bits value,
  590. */
  591. data_ptr = (uchar *)
  592. &(rpc_pkt.u.reply.data[4 + nfsv3_data_offset]);
  593. }
  594. if (store_block(data_ptr, nfs_offset, rlen))
  595. return -9999;
  596. return rlen;
  597. }
  598. /**************************************************************************
  599. Interfaces of U-BOOT
  600. **************************************************************************/
  601. static void nfs_timeout_handler(void)
  602. {
  603. if (++nfs_timeout_count > NFS_RETRY_COUNT) {
  604. puts("\nRetry count exceeded; starting again\n");
  605. net_start_again();
  606. } else {
  607. puts("T ");
  608. net_set_timeout_handler(nfs_timeout +
  609. NFS_TIMEOUT * nfs_timeout_count,
  610. nfs_timeout_handler);
  611. nfs_send();
  612. }
  613. }
  614. static void nfs_handler(uchar *pkt, unsigned dest, struct in_addr sip,
  615. unsigned src, unsigned len)
  616. {
  617. int rlen;
  618. int reply;
  619. debug("%s\n", __func__);
  620. if (dest != nfs_our_port)
  621. return;
  622. switch (nfs_state) {
  623. case STATE_PRCLOOKUP_PROG_MOUNT_REQ:
  624. if (rpc_lookup_reply(PROG_MOUNT, pkt, len) == -NFS_RPC_DROP)
  625. break;
  626. nfs_state = STATE_PRCLOOKUP_PROG_NFS_REQ;
  627. nfs_send();
  628. break;
  629. case STATE_PRCLOOKUP_PROG_NFS_REQ:
  630. if (rpc_lookup_reply(PROG_NFS, pkt, len) == -NFS_RPC_DROP)
  631. break;
  632. nfs_state = STATE_MOUNT_REQ;
  633. nfs_send();
  634. break;
  635. case STATE_MOUNT_REQ:
  636. reply = nfs_mount_reply(pkt, len);
  637. if (reply == -NFS_RPC_DROP) {
  638. break;
  639. } else if (reply == -NFS_RPC_ERR) {
  640. puts("*** ERROR: Cannot mount\n");
  641. /* just to be sure... */
  642. nfs_state = STATE_UMOUNT_REQ;
  643. nfs_send();
  644. } else {
  645. nfs_state = STATE_LOOKUP_REQ;
  646. nfs_send();
  647. }
  648. break;
  649. case STATE_UMOUNT_REQ:
  650. reply = nfs_umountall_reply(pkt, len);
  651. if (reply == -NFS_RPC_DROP) {
  652. break;
  653. } else if (reply == -NFS_RPC_ERR) {
  654. debug("*** ERROR: Cannot umount\n");
  655. net_set_state(NETLOOP_FAIL);
  656. } else {
  657. puts("\ndone\n");
  658. net_set_state(nfs_download_state);
  659. }
  660. break;
  661. case STATE_LOOKUP_REQ:
  662. reply = nfs_lookup_reply(pkt, len);
  663. if (reply == -NFS_RPC_DROP) {
  664. break;
  665. } else if (reply == -NFS_RPC_ERR) {
  666. puts("*** ERROR: File lookup fail\n");
  667. nfs_state = STATE_UMOUNT_REQ;
  668. nfs_send();
  669. } else if (reply == -NFS_RPC_PROG_MISMATCH &&
  670. supported_nfs_versions != 0) {
  671. /* umount */
  672. nfs_state = STATE_UMOUNT_REQ;
  673. nfs_send();
  674. /* And retry with another supported version */
  675. nfs_state = STATE_PRCLOOKUP_PROG_MOUNT_REQ;
  676. nfs_send();
  677. } else {
  678. nfs_state = STATE_READ_REQ;
  679. nfs_offset = 0;
  680. nfs_len = NFS_READ_SIZE;
  681. nfs_send();
  682. }
  683. break;
  684. case STATE_READLINK_REQ:
  685. reply = nfs_readlink_reply(pkt, len);
  686. if (reply == -NFS_RPC_DROP) {
  687. break;
  688. } else if (reply == -NFS_RPC_ERR) {
  689. puts("*** ERROR: Symlink fail\n");
  690. nfs_state = STATE_UMOUNT_REQ;
  691. nfs_send();
  692. } else {
  693. debug("Symlink --> %s\n", nfs_path);
  694. nfs_filename = basename(nfs_path);
  695. nfs_path = dirname(nfs_path);
  696. nfs_state = STATE_MOUNT_REQ;
  697. nfs_send();
  698. }
  699. break;
  700. case STATE_READ_REQ:
  701. rlen = nfs_read_reply(pkt, len);
  702. net_set_timeout_handler(nfs_timeout, nfs_timeout_handler);
  703. if (rlen > 0) {
  704. nfs_offset += rlen;
  705. nfs_send();
  706. } else if ((rlen == -NFSERR_ISDIR) || (rlen == -NFSERR_INVAL)) {
  707. /* symbolic link */
  708. nfs_state = STATE_READLINK_REQ;
  709. nfs_send();
  710. } else {
  711. if (!rlen)
  712. nfs_download_state = NETLOOP_SUCCESS;
  713. if (rlen < 0)
  714. debug("NFS READ error (%d)\n", rlen);
  715. nfs_state = STATE_UMOUNT_REQ;
  716. nfs_send();
  717. }
  718. break;
  719. }
  720. }
  721. void nfs_start(void)
  722. {
  723. debug("%s\n", __func__);
  724. nfs_download_state = NETLOOP_FAIL;
  725. nfs_server_ip = net_server_ip;
  726. nfs_path = (char *)nfs_path_buff;
  727. if (nfs_path == NULL) {
  728. net_set_state(NETLOOP_FAIL);
  729. debug("*** ERROR: Fail allocate memory\n");
  730. return;
  731. }
  732. if (net_boot_file_name[0] == '\0') {
  733. sprintf(nfs_path, "/nfsroot/%02X%02X%02X%02X.img",
  734. net_ip.s_addr & 0xFF,
  735. (net_ip.s_addr >> 8) & 0xFF,
  736. (net_ip.s_addr >> 16) & 0xFF,
  737. (net_ip.s_addr >> 24) & 0xFF);
  738. debug("*** Warning: no boot file name; using '%s'\n",
  739. nfs_path);
  740. } else {
  741. char *p = net_boot_file_name;
  742. p = strchr(p, ':');
  743. if (p != NULL) {
  744. nfs_server_ip = string_to_ip(net_boot_file_name);
  745. ++p;
  746. strcpy(nfs_path, p);
  747. } else {
  748. strcpy(nfs_path, net_boot_file_name);
  749. }
  750. }
  751. nfs_filename = basename(nfs_path);
  752. nfs_path = dirname(nfs_path);
  753. debug("Using %s device\n", eth_get_name());
  754. debug("File transfer via NFS from server %pI4; our IP address is %pI4",
  755. &nfs_server_ip, &net_ip);
  756. /* Check if we need to send across this subnet */
  757. if (net_gateway.s_addr && net_netmask.s_addr) {
  758. struct in_addr our_net;
  759. struct in_addr server_net;
  760. our_net.s_addr = net_ip.s_addr & net_netmask.s_addr;
  761. server_net.s_addr = net_server_ip.s_addr & net_netmask.s_addr;
  762. if (our_net.s_addr != server_net.s_addr)
  763. debug("; sending through gateway %pI4",
  764. &net_gateway);
  765. }
  766. debug("\nFilename '%s/%s'.", nfs_path, nfs_filename);
  767. if (net_boot_file_expected_size_in_blocks) {
  768. debug(" Size is 0x%x Bytes = ",
  769. net_boot_file_expected_size_in_blocks << 9);
  770. print_size(net_boot_file_expected_size_in_blocks << 9, "");
  771. }
  772. debug("\nLoad address: 0x%lx\nLoading: *\b", load_addr);
  773. net_set_timeout_handler(nfs_timeout, nfs_timeout_handler);
  774. net_set_udp_handler(nfs_handler);
  775. nfs_timeout_count = 0;
  776. nfs_state = STATE_PRCLOOKUP_PROG_MOUNT_REQ;
  777. /*nfs_our_port = 4096 + (get_ticks() % 3072);*/
  778. /*FIX ME !!!*/
  779. nfs_our_port = 1000;
  780. /* zero out server ether in case the server ip has changed */
  781. memset(net_server_ethaddr, 0, 6);
  782. nfs_send();
  783. }