nfs.c 24 KB

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