nfs.c 24 KB

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