nfs.c 24 KB

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