nfs.c 24 KB

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