nfs.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925
  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 = (uint32_t *)&(rpc_pkt.u.call.data);
  173. if (datalen)
  174. memcpy((char *)p, (char *)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("*** Waring: 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. memcpy(filefh, rpc_pkt.u.reply.data + 1, NFS_FHSIZE);
  477. } else { /* NFSV3_FLAG */
  478. filefh3_length = ntohl(rpc_pkt.u.reply.data[1]);
  479. if (filefh3_length > NFS3_FHSIZE)
  480. filefh3_length = NFS3_FHSIZE;
  481. memcpy(filefh, rpc_pkt.u.reply.data + 2, filefh3_length);
  482. }
  483. return 0;
  484. }
  485. static int nfs3_get_attributes_offset(uint32_t *data)
  486. {
  487. if (ntohl(data[1]) != 0) {
  488. /* 'attributes_follow' flag is TRUE,
  489. * so we have attributes on 21 dwords */
  490. /* Skip unused values :
  491. type; 32 bits value,
  492. mode; 32 bits value,
  493. nlink; 32 bits value,
  494. uid; 32 bits value,
  495. gid; 32 bits value,
  496. size; 64 bits value,
  497. used; 64 bits value,
  498. rdev; 64 bits value,
  499. fsid; 64 bits value,
  500. fileid; 64 bits value,
  501. atime; 64 bits value,
  502. mtime; 64 bits value,
  503. ctime; 64 bits value,
  504. */
  505. return 22;
  506. } else {
  507. /* 'attributes_follow' flag is FALSE,
  508. * so we don't have any attributes */
  509. return 1;
  510. }
  511. }
  512. static int nfs_readlink_reply(uchar *pkt, unsigned len)
  513. {
  514. struct rpc_t rpc_pkt;
  515. int rlen;
  516. int nfsv3_data_offset = 0;
  517. debug("%s\n", __func__);
  518. memcpy((unsigned char *)&rpc_pkt, pkt, len);
  519. if (ntohl(rpc_pkt.u.reply.id) > rpc_id)
  520. return -NFS_RPC_ERR;
  521. else if (ntohl(rpc_pkt.u.reply.id) < rpc_id)
  522. return -NFS_RPC_DROP;
  523. if (rpc_pkt.u.reply.rstatus ||
  524. rpc_pkt.u.reply.verifier ||
  525. rpc_pkt.u.reply.astatus ||
  526. rpc_pkt.u.reply.data[0])
  527. return -1;
  528. if (!(supported_nfs_versions & NFSV2_FLAG)) { /* NFSV3_FLAG */
  529. nfsv3_data_offset =
  530. nfs3_get_attributes_offset(rpc_pkt.u.reply.data);
  531. }
  532. /* new path length */
  533. rlen = ntohl(rpc_pkt.u.reply.data[1 + nfsv3_data_offset]);
  534. if (*((char *)&(rpc_pkt.u.reply.data[2 + nfsv3_data_offset])) != '/') {
  535. int pathlen;
  536. strcat(nfs_path, "/");
  537. pathlen = strlen(nfs_path);
  538. memcpy(nfs_path + pathlen,
  539. (uchar *)&(rpc_pkt.u.reply.data[2 + nfsv3_data_offset]),
  540. rlen);
  541. nfs_path[pathlen + rlen] = 0;
  542. } else {
  543. memcpy(nfs_path,
  544. (uchar *)&(rpc_pkt.u.reply.data[2 + nfsv3_data_offset]),
  545. rlen);
  546. nfs_path[rlen] = 0;
  547. }
  548. return 0;
  549. }
  550. static int nfs_read_reply(uchar *pkt, unsigned len)
  551. {
  552. struct rpc_t rpc_pkt;
  553. int rlen;
  554. uchar *data_ptr;
  555. debug("%s\n", __func__);
  556. memcpy(&rpc_pkt.u.data[0], pkt, sizeof(rpc_pkt.u.reply));
  557. if (ntohl(rpc_pkt.u.reply.id) > rpc_id)
  558. return -NFS_RPC_ERR;
  559. else if (ntohl(rpc_pkt.u.reply.id) < rpc_id)
  560. return -NFS_RPC_DROP;
  561. if (rpc_pkt.u.reply.rstatus ||
  562. rpc_pkt.u.reply.verifier ||
  563. rpc_pkt.u.reply.astatus ||
  564. rpc_pkt.u.reply.data[0]) {
  565. if (rpc_pkt.u.reply.rstatus)
  566. return -9999;
  567. if (rpc_pkt.u.reply.astatus)
  568. return -9999;
  569. return -ntohl(rpc_pkt.u.reply.data[0]);
  570. }
  571. if ((nfs_offset != 0) && !((nfs_offset) %
  572. (NFS_READ_SIZE / 2 * 10 * HASHES_PER_LINE)))
  573. puts("\n\t ");
  574. if (!(nfs_offset % ((NFS_READ_SIZE / 2) * 10)))
  575. putc('#');
  576. if (supported_nfs_versions & NFSV2_FLAG) {
  577. rlen = ntohl(rpc_pkt.u.reply.data[18]);
  578. data_ptr = (uchar *)&(rpc_pkt.u.reply.data[19]);
  579. } else { /* NFSV3_FLAG */
  580. int nfsv3_data_offset =
  581. nfs3_get_attributes_offset(rpc_pkt.u.reply.data);
  582. /* count value */
  583. rlen = ntohl(rpc_pkt.u.reply.data[1 + nfsv3_data_offset]);
  584. /* Skip unused values :
  585. EOF: 32 bits value,
  586. data_size: 32 bits value,
  587. */
  588. data_ptr = (uchar *)
  589. &(rpc_pkt.u.reply.data[4 + nfsv3_data_offset]);
  590. }
  591. if (store_block(data_ptr, nfs_offset, rlen))
  592. return -9999;
  593. return rlen;
  594. }
  595. /**************************************************************************
  596. Interfaces of U-BOOT
  597. **************************************************************************/
  598. static void nfs_timeout_handler(void)
  599. {
  600. if (++nfs_timeout_count > NFS_RETRY_COUNT) {
  601. puts("\nRetry count exceeded; starting again\n");
  602. net_start_again();
  603. } else {
  604. puts("T ");
  605. net_set_timeout_handler(nfs_timeout +
  606. NFS_TIMEOUT * nfs_timeout_count,
  607. nfs_timeout_handler);
  608. nfs_send();
  609. }
  610. }
  611. static void nfs_handler(uchar *pkt, unsigned dest, struct in_addr sip,
  612. unsigned src, unsigned len)
  613. {
  614. int rlen;
  615. int reply;
  616. debug("%s\n", __func__);
  617. if (dest != nfs_our_port)
  618. return;
  619. switch (nfs_state) {
  620. case STATE_PRCLOOKUP_PROG_MOUNT_REQ:
  621. if (rpc_lookup_reply(PROG_MOUNT, pkt, len) == -NFS_RPC_DROP)
  622. break;
  623. nfs_state = STATE_PRCLOOKUP_PROG_NFS_REQ;
  624. nfs_send();
  625. break;
  626. case STATE_PRCLOOKUP_PROG_NFS_REQ:
  627. if (rpc_lookup_reply(PROG_NFS, pkt, len) == -NFS_RPC_DROP)
  628. break;
  629. nfs_state = STATE_MOUNT_REQ;
  630. nfs_send();
  631. break;
  632. case STATE_MOUNT_REQ:
  633. reply = nfs_mount_reply(pkt, len);
  634. if (reply == -NFS_RPC_DROP) {
  635. break;
  636. } else if (reply == -NFS_RPC_ERR) {
  637. puts("*** ERROR: Cannot mount\n");
  638. /* just to be sure... */
  639. nfs_state = STATE_UMOUNT_REQ;
  640. nfs_send();
  641. } else {
  642. nfs_state = STATE_LOOKUP_REQ;
  643. nfs_send();
  644. }
  645. break;
  646. case STATE_UMOUNT_REQ:
  647. reply = nfs_umountall_reply(pkt, len);
  648. if (reply == -NFS_RPC_DROP) {
  649. break;
  650. } else if (reply == -NFS_RPC_ERR) {
  651. debug("*** ERROR: Cannot umount\n");
  652. net_set_state(NETLOOP_FAIL);
  653. } else {
  654. puts("\ndone\n");
  655. net_set_state(nfs_download_state);
  656. }
  657. break;
  658. case STATE_LOOKUP_REQ:
  659. reply = nfs_lookup_reply(pkt, len);
  660. if (reply == -NFS_RPC_DROP) {
  661. break;
  662. } else if (reply == -NFS_RPC_ERR) {
  663. puts("*** ERROR: File lookup fail\n");
  664. nfs_state = STATE_UMOUNT_REQ;
  665. nfs_send();
  666. } else if (reply == -NFS_RPC_PROG_MISMATCH &&
  667. supported_nfs_versions != 0) {
  668. /* umount */
  669. nfs_state = STATE_UMOUNT_REQ;
  670. nfs_send();
  671. /* And retry with another supported version */
  672. nfs_state = STATE_PRCLOOKUP_PROG_MOUNT_REQ;
  673. nfs_send();
  674. } else {
  675. nfs_state = STATE_READ_REQ;
  676. nfs_offset = 0;
  677. nfs_len = NFS_READ_SIZE;
  678. nfs_send();
  679. }
  680. break;
  681. case STATE_READLINK_REQ:
  682. reply = nfs_readlink_reply(pkt, len);
  683. if (reply == -NFS_RPC_DROP) {
  684. break;
  685. } else if (reply == -NFS_RPC_ERR) {
  686. puts("*** ERROR: Symlink fail\n");
  687. nfs_state = STATE_UMOUNT_REQ;
  688. nfs_send();
  689. } else {
  690. debug("Symlink --> %s\n", nfs_path);
  691. nfs_filename = basename(nfs_path);
  692. nfs_path = dirname(nfs_path);
  693. nfs_state = STATE_MOUNT_REQ;
  694. nfs_send();
  695. }
  696. break;
  697. case STATE_READ_REQ:
  698. rlen = nfs_read_reply(pkt, len);
  699. net_set_timeout_handler(nfs_timeout, nfs_timeout_handler);
  700. if (rlen > 0) {
  701. nfs_offset += rlen;
  702. nfs_send();
  703. } else if ((rlen == -NFSERR_ISDIR) || (rlen == -NFSERR_INVAL)) {
  704. /* symbolic link */
  705. nfs_state = STATE_READLINK_REQ;
  706. nfs_send();
  707. } else {
  708. if (!rlen)
  709. nfs_download_state = NETLOOP_SUCCESS;
  710. if (rlen < 0)
  711. debug("NFS READ error (%d)\n", rlen);
  712. nfs_state = STATE_UMOUNT_REQ;
  713. nfs_send();
  714. }
  715. break;
  716. }
  717. }
  718. void nfs_start(void)
  719. {
  720. debug("%s\n", __func__);
  721. nfs_download_state = NETLOOP_FAIL;
  722. nfs_server_ip = net_server_ip;
  723. nfs_path = (char *)nfs_path_buff;
  724. if (nfs_path == NULL) {
  725. net_set_state(NETLOOP_FAIL);
  726. debug("*** ERROR: Fail allocate memory\n");
  727. return;
  728. }
  729. if (net_boot_file_name[0] == '\0') {
  730. sprintf(nfs_path, "/nfsroot/%02X%02X%02X%02X.img",
  731. net_ip.s_addr & 0xFF,
  732. (net_ip.s_addr >> 8) & 0xFF,
  733. (net_ip.s_addr >> 16) & 0xFF,
  734. (net_ip.s_addr >> 24) & 0xFF);
  735. debug("*** Warning: no boot file name; using '%s'\n",
  736. nfs_path);
  737. } else {
  738. char *p = net_boot_file_name;
  739. p = strchr(p, ':');
  740. if (p != NULL) {
  741. nfs_server_ip = string_to_ip(net_boot_file_name);
  742. ++p;
  743. strcpy(nfs_path, p);
  744. } else {
  745. strcpy(nfs_path, net_boot_file_name);
  746. }
  747. }
  748. nfs_filename = basename(nfs_path);
  749. nfs_path = dirname(nfs_path);
  750. debug("Using %s device\n", eth_get_name());
  751. debug("File transfer via NFS from server %pI4; our IP address is %pI4",
  752. &nfs_server_ip, &net_ip);
  753. /* Check if we need to send across this subnet */
  754. if (net_gateway.s_addr && net_netmask.s_addr) {
  755. struct in_addr our_net;
  756. struct in_addr server_net;
  757. our_net.s_addr = net_ip.s_addr & net_netmask.s_addr;
  758. server_net.s_addr = net_server_ip.s_addr & net_netmask.s_addr;
  759. if (our_net.s_addr != server_net.s_addr)
  760. debug("; sending through gateway %pI4",
  761. &net_gateway);
  762. }
  763. debug("\nFilename '%s/%s'.", nfs_path, nfs_filename);
  764. if (net_boot_file_expected_size_in_blocks) {
  765. debug(" Size is 0x%x Bytes = ",
  766. net_boot_file_expected_size_in_blocks << 9);
  767. print_size(net_boot_file_expected_size_in_blocks << 9, "");
  768. }
  769. debug("\nLoad address: 0x%lx\nLoading: *\b", load_addr);
  770. net_set_timeout_handler(nfs_timeout, nfs_timeout_handler);
  771. net_set_udp_handler(nfs_handler);
  772. nfs_timeout_count = 0;
  773. nfs_state = STATE_PRCLOOKUP_PROG_MOUNT_REQ;
  774. /*nfs_our_port = 4096 + (get_ticks() % 3072);*/
  775. /*FIX ME !!!*/
  776. nfs_our_port = 1000;
  777. /* zero out server ether in case the server ip has changed */
  778. memset(net_server_ethaddr, 0, 6);
  779. nfs_send();
  780. }