nfs.c 24 KB

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