nfs.c 24 KB

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