You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1671 lines
39 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2007-2010 by Øyvind Harboe *
  3. * *
  4. * This program is free software; you can redistribute it and/or modify *
  5. * it under the terms of the GNU General Public License as published by *
  6. * the Free Software Foundation; either version 2 of the License, or *
  7. * (at your option) any later version. *
  8. * *
  9. * This program is distributed in the hope that it will be useful, *
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  12. * GNU General Public License for more details. *
  13. * *
  14. * You should have received a copy of the GNU General Public License *
  15. * along with this program; if not, write to the *
  16. * Free Software Foundation, Inc., *
  17. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  18. ***************************************************************************/
  19. #ifdef HAVE_CONFIG_H
  20. #include "config.h"
  21. #endif
  22. #include <helper/types.h>
  23. #include <jtag/jtag.h>
  24. #include <helper/ioutil.h>
  25. #include <helper/util.h>
  26. #include <helper/configuration.h>
  27. #include <server/server.h>
  28. #include <server/telnet_server.h>
  29. #include <server/gdb_server.h>
  30. #include <openocd.h>
  31. #include <helper/time_support.h>
  32. #include <sys/time.h>
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include <string.h>
  36. #include <unistd.h>
  37. #include <errno.h>
  38. #include <cyg/io/flash.h>
  39. #include <pkgconf/fs_jffs2.h> // Address of JFFS2
  40. #include <network.h>
  41. #include <fcntl.h>
  42. #include <sys/stat.h>
  43. #include <cyg/fileio/fileio.h>
  44. #include <dirent.h>
  45. #include <cyg/athttpd/http.h>
  46. #include <cyg/athttpd/socket.h>
  47. #include <cyg/athttpd/handler.h>
  48. #include <cyg/athttpd/cgi.h>
  49. #include <cyg/athttpd/forms.h>
  50. #include <cyg/discover/discover.h>
  51. #include <cyg/io/io.h>
  52. #include <cyg/io/serialio.h>
  53. #include <netinet/tcp.h>
  54. #include <cyg/hal/hal_diag.h>
  55. #include "rom.h"
  56. #ifdef CYGPKG_HAL_NIOS2
  57. #include <cyg/hal/io.h>
  58. #define ZY1000_SER_DEV "/dev/uart_0"
  59. #else
  60. #define ZY1000_SER_DEV "/dev/ser0"
  61. #endif
  62. #define MAX_IFS 64
  63. #if defined(CYGPKG_NET_FREEBSD_STACK)
  64. #include <tftp_support.h>
  65. /* posix compatibility broken*/
  66. struct tftpd_fileops fileops =
  67. {
  68. (int (*)(const char *, int))open,
  69. close,
  70. (int (*)(int, const void *, int))write,
  71. (int (*)(int, void *, int))read
  72. };
  73. #endif
  74. void diag_write(char *buf, int len)
  75. {
  76. int j;
  77. for (j = 0; j < len; j++)
  78. {
  79. diag_printf("%c", buf[j]);
  80. }
  81. }
  82. static bool serialLog = true;
  83. static bool writeLog = true;
  84. char hwaddr[512];
  85. #ifdef CYGPKG_PROFILE_GPROF
  86. #include <cyg/profile/profile.h>
  87. extern char _stext, _etext; // Defined by the linker
  88. static char *start_of_code=&_stext;
  89. static char *end_of_code=&_etext;
  90. void start_profile(void)
  91. {
  92. // This starts up the system-wide profiling, gathering
  93. // profile information on all of the code, with a 16 byte
  94. // "bucket" size, at a rate of 100us/profile hit.
  95. // Note: a bucket size of 16 will give pretty good function
  96. // resolution. Much smaller and the buffer becomes
  97. // much too large for very little gain.
  98. // Note: a timer period of 100us is also a reasonable
  99. // compromise. Any smaller and the overhead of
  100. // handling the timter (profile) interrupt could
  101. // swamp the system. A fast processor might get
  102. // by with a smaller value, but a slow one could
  103. // even be swamped by this value. If the value is
  104. // too large, the usefulness of the profile is reduced.
  105. // no more interrupts than 1/10ms.
  106. //profile_on((void *)0, (void *)0x40000, 16, 10000); // SRAM
  107. // profile_on(0, &_etext, 16, 10000); // SRAM & DRAM
  108. profile_on(start_of_code, end_of_code, 16, 10000); // Nios DRAM
  109. }
  110. #endif
  111. static FILE *log;
  112. static char reboot_stack[2048];
  113. static void zylinjtag_reboot(cyg_addrword_t data)
  114. {
  115. serialLog = true;
  116. diag_printf("Rebooting in 500 ticks..\n");
  117. cyg_thread_delay(500);
  118. diag_printf("Unmounting /config..\n");
  119. umount("/config");
  120. diag_printf("Rebooting..\n");
  121. #ifdef CYGPKG_HAL_NIOS2
  122. /* This will reboot & reconfigure the FPGA from the bootloader
  123. * and on.
  124. */
  125. IOWR(REMOTE_UPDATE_BASE, 0x20, 0x1);
  126. #else
  127. HAL_PLATFORM_RESET();
  128. #endif
  129. }
  130. static cyg_thread zylinjtag_thread_object;
  131. static cyg_handle_t zylinjtag_thread_handle;
  132. void reboot(void)
  133. {
  134. cyg_thread_create(1, zylinjtag_reboot, (cyg_addrword_t) 0, "reboot Thread",
  135. (void *) reboot_stack, sizeof(reboot_stack),
  136. &zylinjtag_thread_handle, &zylinjtag_thread_object);
  137. cyg_thread_resume(zylinjtag_thread_handle);
  138. }
  139. static char zylinjtag_reboot_port_stack[2048];
  140. static cyg_thread zylinjtag_reboot_port_thread_object;
  141. static cyg_handle_t zylinjtag_reboot_port_thread_handle;
  142. static void zylinjtag_reboot_port_task(cyg_addrword_t data)
  143. {
  144. int so_reuseaddr_option = 1;
  145. int fd;
  146. if ((fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
  147. {
  148. LOG_ERROR("error creating socket: %s", strerror(errno));
  149. exit(-1);
  150. }
  151. setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void*) &so_reuseaddr_option,
  152. sizeof(int));
  153. struct sockaddr_in sin;
  154. unsigned int address_size;
  155. address_size = sizeof(sin);
  156. memset(&sin, 0, sizeof(sin));
  157. sin.sin_family = AF_INET;
  158. sin.sin_addr.s_addr = INADDR_ANY;
  159. sin.sin_port = htons(1234);
  160. if (bind(fd, (struct sockaddr *) &sin, sizeof(sin)) == -1)
  161. {
  162. LOG_ERROR("couldn't bind to socket: %s", strerror(errno));
  163. exit(-1);
  164. }
  165. if (listen(fd, 1) == -1)
  166. {
  167. LOG_ERROR("couldn't listen on socket: %s", strerror(errno));
  168. exit(-1);
  169. }
  170. // socket_nonblock(fd);
  171. accept(fd, (struct sockaddr *) &sin, &address_size);
  172. diag_printf("Got reboot signal on port 1234");
  173. reboot();
  174. }
  175. void reboot_port(void)
  176. {
  177. cyg_thread_create(1, zylinjtag_reboot_port_task, (cyg_addrword_t) 0, "wait for reboot signal on port 1234",
  178. (void *) zylinjtag_reboot_port_stack, sizeof(zylinjtag_reboot_port_stack),
  179. &zylinjtag_reboot_port_thread_handle, &zylinjtag_reboot_port_thread_object);
  180. cyg_thread_resume(zylinjtag_reboot_port_thread_handle);
  181. }
  182. int configuration_output_handler(struct command_context *context,
  183. const char* line)
  184. {
  185. diag_printf("%s", line);
  186. return ERROR_OK;
  187. }
  188. int zy1000_configuration_output_handler_log(struct command_context *context,
  189. const char* line)
  190. {
  191. LOG_USER_N("%s", line);
  192. return ERROR_OK;
  193. }
  194. #ifdef CYGPKG_PROFILE_GPROF
  195. //extern int64_t totaltime;
  196. static int zylinjtag_Jim_Command_profile(Jim_Interp *interp, int argc,
  197. Jim_Obj * const *argv)
  198. {
  199. if ((argc == 2) && (strcmp(Jim_GetString(argv[1], NULL), "stats")==0))
  200. {
  201. // profile_off();
  202. //LOG_USER("Stats %dms sleeping in select()", (int)totaltime);
  203. } else
  204. {
  205. LOG_USER("Profiling started");
  206. start_profile();
  207. //totaltime = 0;
  208. }
  209. return ERROR_OK;
  210. }
  211. #endif
  212. externC void phi_init_all_network_interfaces(void);
  213. struct command_context *cmd_ctx;
  214. static bool webRunning = false;
  215. void keep_webserver(void)
  216. {
  217. // Target initialisation is only attempted at startup, so we sleep forever and
  218. // let the http server bail us out(i.e. get config files set up).
  219. diag_printf("OpenOCD has invoked exit().\n"
  220. "Use web server to correct any configuration settings and reboot.\n");
  221. if (!webRunning)
  222. reboot();
  223. // exit() will terminate the current thread and we we'll then sleep eternally or
  224. // we'll have a reboot scheduled.
  225. }
  226. extern void printDccChar(char c);
  227. static char logBuffer[128 * 1024];
  228. static const int logSize = sizeof(logBuffer);
  229. int writePtr = 0;
  230. int logCount = 0;
  231. void _zylinjtag_diag_write_char(char c, void **param)
  232. {
  233. if (writeLog)
  234. {
  235. logBuffer[writePtr] = c;
  236. writePtr = (writePtr + 1) % logSize;
  237. logCount++;
  238. }
  239. if (serialLog)
  240. {
  241. if (c == '\n')
  242. {
  243. HAL_DIAG_WRITE_CHAR('\r');
  244. }
  245. HAL_DIAG_WRITE_CHAR(c);
  246. }
  247. #ifdef CYGPKG_HAL_ZYLIN_PHI
  248. printDccChar(c);
  249. #endif
  250. }
  251. void copyfile(char *name2, char *name1);
  252. void copydir(char *name, char *destdir);
  253. #if 0
  254. MTAB_ENTRY(romfs_mte1,
  255. "/rom",
  256. "romfs",
  257. "",
  258. (CYG_ADDRWORD) &filedata[0]);
  259. #endif
  260. void openocd_sleep_prelude(void)
  261. {
  262. cyg_mutex_unlock(&httpstate.jim_lock);
  263. }
  264. void openocd_sleep_postlude(void)
  265. {
  266. cyg_mutex_lock(&httpstate.jim_lock);
  267. }
  268. void format(void)
  269. {
  270. #ifdef CYGDAT_IO_FLASH_BLOCK_DEVICE_NAME_1
  271. diag_printf("Formatting JFFS2...\n");
  272. cyg_io_handle_t handle;
  273. Cyg_ErrNo err;
  274. err = cyg_io_lookup(CYGDAT_IO_FLASH_BLOCK_DEVICE_NAME_1, &handle);
  275. if (err != ENOERR)
  276. {
  277. diag_printf("Flash Error cyg_io_lookup: %d\n", err);
  278. reboot();
  279. }
  280. cyg_uint32 len;
  281. cyg_io_flash_getconfig_devsize_t ds;
  282. len = sizeof(ds);
  283. err = cyg_io_get_config(handle, CYG_IO_GET_CONFIG_FLASH_DEVSIZE, &ds, &len);
  284. if (err != ENOERR)
  285. {
  286. diag_printf("Flash error cyg_io_get_config %d\n", err);
  287. reboot();
  288. }
  289. cyg_io_flash_getconfig_erase_t e;
  290. len = sizeof(e);
  291. e.offset = 0;
  292. e.len = ds.dev_size;
  293. diag_printf("Formatting 0x%08x bytes\n", (int)ds.dev_size);
  294. err = cyg_io_get_config(handle, CYG_IO_GET_CONFIG_FLASH_ERASE, &e, &len);
  295. if (err != ENOERR)
  296. {
  297. diag_printf("Flash erase error %d offset 0x%08x\n", err, e.err_address);
  298. reboot();
  299. }
  300. diag_printf("Flash formatted successfully\n");
  301. #endif
  302. reboot();
  303. }
  304. static int zylinjtag_Jim_Command_format_jffs2(Jim_Interp *interp, int argc,
  305. Jim_Obj * const *argv)
  306. {
  307. if (argc != 1)
  308. {
  309. return JIM_ERR;
  310. }
  311. format();
  312. for (;;)
  313. ;
  314. }
  315. static int zylinjtag_Jim_Command_threads(Jim_Interp *interp, int argc,
  316. Jim_Obj * const *argv)
  317. {
  318. cyg_handle_t thread = 0;
  319. cyg_uint16 id = 0;
  320. Jim_Obj *threads = Jim_NewListObj(interp, NULL, 0);
  321. /* Loop over the threads, and generate a table row for
  322. * each.
  323. */
  324. while (cyg_thread_get_next(&thread, &id))
  325. {
  326. Jim_Obj *threadObj = Jim_NewListObj(interp, NULL, 0);
  327. cyg_thread_info info;
  328. char *state_string;
  329. cyg_thread_get_info(thread, id, &info);
  330. if (info.name == NULL)
  331. info.name = "<no name>";
  332. Jim_ListAppendElement(interp, threadObj, Jim_NewStringObj(interp,
  333. info.name, strlen(info.name)));
  334. /* Translate the state into a string.
  335. */
  336. if (info.state == 0)
  337. state_string = "RUN";
  338. else if (info.state & 0x04)
  339. state_string = "SUSP";
  340. else
  341. switch (info.state & 0x1b)
  342. {
  343. case 0x01:
  344. state_string = "SLEEP";
  345. break;
  346. case 0x02:
  347. state_string = "CNTSLEEP";
  348. break;
  349. case 0x08:
  350. state_string = "CREATE";
  351. break;
  352. case 0x10:
  353. state_string = "EXIT";
  354. break;
  355. default:
  356. state_string = "????";
  357. break;
  358. }
  359. Jim_ListAppendElement(interp, threadObj, Jim_NewStringObj(interp,
  360. state_string, strlen(state_string)));
  361. Jim_ListAppendElement(interp, threadObj, Jim_NewIntObj(interp, id));
  362. Jim_ListAppendElement(interp, threadObj, Jim_NewIntObj(interp,
  363. info.set_pri));
  364. Jim_ListAppendElement(interp, threadObj, Jim_NewIntObj(interp,
  365. info.cur_pri));
  366. Jim_ListAppendElement(interp, threads, threadObj);
  367. }
  368. Jim_SetResult(interp, threads);
  369. return JIM_OK;
  370. }
  371. static int zylinjtag_Jim_Command_log(Jim_Interp *interp, int argc,
  372. Jim_Obj * const *argv)
  373. {
  374. Jim_Obj *tclOutput = Jim_NewStringObj(interp, "", 0);
  375. if (logCount >= logSize)
  376. {
  377. Jim_AppendString(httpstate.jim_interp, tclOutput, logBuffer + logCount
  378. % logSize, logSize - logCount % logSize);
  379. }
  380. Jim_AppendString(httpstate.jim_interp, tclOutput, logBuffer, writePtr);
  381. Jim_SetResult(interp, tclOutput);
  382. return JIM_OK;
  383. }
  384. static int zylinjtag_Jim_Command_reboot(Jim_Interp *interp, int argc,
  385. Jim_Obj * const *argv)
  386. {
  387. reboot();
  388. return JIM_OK;
  389. }
  390. static void zylinjtag_startNetwork(void)
  391. {
  392. // Bring TCP/IP up immediately before we're ready to accept commands.
  393. //
  394. // That is as soon as a PING responds, we're accepting telnet sessions.
  395. #if defined(CYGPKG_NET_FREEBSD_STACK)
  396. phi_init_all_network_interfaces();
  397. #else
  398. lwip_init();
  399. #endif
  400. if (!eth0_up)
  401. {
  402. diag_printf("Network not up and running\n");
  403. exit(-1);
  404. }
  405. /* very first thing we want is a reboot capability */
  406. reboot_port();
  407. #if defined(CYGPKG_NET_FREEBSD_STACK)
  408. /*start TFTP*/
  409. tftpd_start(69, &fileops);
  410. #endif
  411. cyg_httpd_init_tcl_interpreter();
  412. // Kludge! Why can't I do this from httpd.c??? I get linker errors...
  413. // some of that --start/end-group stuff?
  414. Jim_InitStaticExtensions(httpstate.jim_interp);
  415. Jim_CreateCommand(httpstate.jim_interp, "log", zylinjtag_Jim_Command_log,
  416. NULL, NULL);
  417. Jim_CreateCommand(httpstate.jim_interp, "zy1000_reboot",
  418. zylinjtag_Jim_Command_reboot, NULL, NULL);
  419. Jim_CreateCommand(httpstate.jim_interp, "threads",
  420. zylinjtag_Jim_Command_threads, NULL, NULL);
  421. Jim_CreateCommand(httpstate.jim_interp, "format_jffs2",
  422. zylinjtag_Jim_Command_format_jffs2, NULL, NULL);
  423. cyg_httpd_start();
  424. webRunning = true;
  425. diag_printf("Web server running\n");
  426. int s;
  427. struct ifreq ifr;
  428. s = socket(AF_INET, SOCK_DGRAM, 0);
  429. if (s >= 0)
  430. {
  431. strcpy(ifr.ifr_name, "eth0");
  432. int res;
  433. res = ioctl(s, SIOCGIFHWADDR, &ifr);
  434. close(s);
  435. if (res < 0)
  436. {
  437. diag_printf("Can't obtain MAC address\n");
  438. reboot();
  439. }
  440. }
  441. sprintf(hwaddr, "%02x:%02x:%02x:%02x:%02x:%02x",
  442. (int) ((unsigned char *) &ifr.ifr_hwaddr.sa_data)[0],
  443. (int) ((unsigned char *) &ifr.ifr_hwaddr.sa_data)[1],
  444. (int) ((unsigned char *) &ifr.ifr_hwaddr.sa_data)[2],
  445. (int) ((unsigned char *) &ifr.ifr_hwaddr.sa_data)[3],
  446. (int) ((unsigned char *) &ifr.ifr_hwaddr.sa_data)[4],
  447. (int) ((unsigned char *) &ifr.ifr_hwaddr.sa_data)[5]);
  448. discover_message
  449. = alloc_printf("ZY1000 Zylin JTAG debugger MAC %s", hwaddr);
  450. discover_launch();
  451. }
  452. static void print_exception_handler(cyg_addrword_t data, cyg_code_t exception,
  453. cyg_addrword_t info)
  454. {
  455. writeLog = false;
  456. serialLog = true;
  457. char *infoStr = "unknown";
  458. switch (exception)
  459. {
  460. #ifdef CYGNUM_HAL_VECTOR_UNDEF_INSTRUCTION
  461. case CYGNUM_HAL_VECTOR_UNDEF_INSTRUCTION:
  462. infoStr = "undefined instruction";
  463. break;
  464. case CYGNUM_HAL_VECTOR_SOFTWARE_INTERRUPT:
  465. infoStr = "software interrupt";
  466. break;
  467. case CYGNUM_HAL_VECTOR_ABORT_PREFETCH:
  468. infoStr = "abort prefetch";
  469. break;
  470. case CYGNUM_HAL_VECTOR_ABORT_DATA:
  471. infoStr = "abort data";
  472. break;
  473. #endif
  474. default:
  475. break;
  476. }
  477. diag_printf("Exception: %08x(%s) %08x\n", exception, infoStr, info);
  478. diag_printf("Dumping log\n---\n");
  479. if (logCount >= logSize)
  480. {
  481. diag_write(logBuffer + logCount % logSize, logSize - logCount % logSize);
  482. }
  483. diag_write(logBuffer, writePtr);
  484. diag_printf("---\nLogdump complete.\n");
  485. diag_printf("Exception: %08x(%s) %08x\n", exception, infoStr, info);
  486. diag_printf("\n---\nRebooting\n");
  487. HAL_PLATFORM_RESET();
  488. }
  489. #ifdef CYGNUM_HAL_VECTOR_UNDEF_INSTRUCTION
  490. static void setHandler(cyg_code_t exception)
  491. {
  492. cyg_exception_handler_t *old_handler;
  493. cyg_addrword_t old_data;
  494. cyg_exception_set_handler(exception, print_exception_handler, 0,
  495. &old_handler, &old_data);
  496. }
  497. #endif
  498. static cyg_thread zylinjtag_uart_thread_object;
  499. static cyg_handle_t zylinjtag_uart_thread_handle;
  500. static char uart_stack[4096];
  501. static char forwardBuffer[1024]; // NB! must be smaller than a TCP/IP packet!!!!!
  502. static char backwardBuffer[1024];
  503. void setNoDelay(int session, int flag)
  504. {
  505. #if 1
  506. // This decreases latency dramatically for e.g. GDB load which
  507. // does not have a sliding window protocol
  508. //
  509. // Can cause *lots* of TCP/IP packets to be sent and it would have
  510. // to be enabled/disabled on the fly to avoid the CPU being
  511. // overloaded...
  512. setsockopt(session, /* socket affected */
  513. IPPROTO_TCP, /* set option at TCP level */
  514. TCP_NODELAY, /* name of option */
  515. (char *) &flag, /* the cast is historical
  516. cruft */
  517. sizeof(int)); /* length of option value */
  518. #endif
  519. }
  520. #define TEST_TCPIP() 0
  521. #if TEST_TCPIP
  522. struct
  523. {
  524. int req;
  525. int actual;
  526. int req2;
  527. int actual2;
  528. } tcpipSent[512 * 1024];
  529. int cur;
  530. #endif
  531. static void zylinjtag_uart(cyg_addrword_t data)
  532. {
  533. int so_reuseaddr_option = 1;
  534. int fd;
  535. if ((fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
  536. {
  537. LOG_ERROR("error creating socket: %s", strerror(errno));
  538. exit(-1);
  539. }
  540. setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void*) &so_reuseaddr_option,
  541. sizeof(int));
  542. struct sockaddr_in sin;
  543. unsigned int address_size;
  544. address_size = sizeof(sin);
  545. memset(&sin, 0, sizeof(sin));
  546. sin.sin_family = AF_INET;
  547. sin.sin_addr.s_addr = INADDR_ANY;
  548. sin.sin_port = htons(5555);
  549. if (bind(fd, (struct sockaddr *) &sin, sizeof(sin)) == -1)
  550. {
  551. LOG_ERROR("couldn't bind to socket: %s", strerror(errno));
  552. exit(-1);
  553. }
  554. if (listen(fd, 1) == -1)
  555. {
  556. LOG_ERROR("couldn't listen on socket: %s", strerror(errno));
  557. exit(-1);
  558. }
  559. // socket_nonblock(fd);
  560. for (;;)
  561. {
  562. int session = accept(fd, (struct sockaddr *) &sin, &address_size);
  563. if (session < 0)
  564. {
  565. continue;
  566. }
  567. setNoDelay(session, 1);
  568. int oldopts = fcntl(session, F_GETFL, 0);
  569. fcntl(session, F_SETFL, oldopts | O_NONBLOCK); //
  570. int serHandle = open(ZY1000_SER_DEV, O_RDWR | O_NONBLOCK);
  571. if (serHandle < 0)
  572. {
  573. close(session);
  574. continue;
  575. }
  576. #ifdef CYGPKG_PROFILE_GPROF
  577. start_profile();
  578. #endif
  579. size_t actual = 0;
  580. size_t actual2 = 0;
  581. size_t pos, pos2;
  582. pos = 0;
  583. pos2 = 0;
  584. #if TEST_TCPIP
  585. cur = 0;
  586. #endif
  587. for (;;)
  588. {
  589. fd_set write_fds;
  590. fd_set read_fds;
  591. FD_ZERO(&write_fds);
  592. FD_ZERO(&read_fds);
  593. int fd_max = -1;
  594. FD_SET(session, &read_fds);
  595. fd_max = session;
  596. FD_SET(serHandle, &read_fds);
  597. if (serHandle > fd_max)
  598. {
  599. fd_max = serHandle;
  600. }
  601. /* Wait... */
  602. cyg_thread_delay(5); // 50ms fixed delay to wait for data to be sent/received
  603. if ((actual == 0) && (actual2 == 0))
  604. {
  605. int retval = select(fd_max + 1, &read_fds, NULL, NULL, NULL);
  606. if (retval <= 0)
  607. {
  608. break;
  609. }
  610. }
  611. if (actual2 <= 0)
  612. {
  613. memset(backwardBuffer, 's', sizeof(backwardBuffer));
  614. int t;
  615. t = read(serHandle, backwardBuffer,
  616. sizeof(backwardBuffer));
  617. actual2 = t;
  618. if (t < 0)
  619. {
  620. if (errno != EAGAIN)
  621. {
  622. goto closeSession;
  623. }
  624. actual2 = 0;
  625. }
  626. pos2 = 0;
  627. }
  628. size_t y = 0;
  629. if (actual2 > 0)
  630. {
  631. int written = write(session, backwardBuffer + pos2, actual2);
  632. if (written <= 0)
  633. goto closeSession;
  634. actual2 -= written;
  635. pos2 += written;
  636. y = written;
  637. }
  638. if (FD_ISSET(session, &read_fds)
  639. && (sizeof(forwardBuffer) > actual))
  640. {
  641. // NB! Here it is important that we empty the TCP/IP read buffer
  642. // to make transmission tick right
  643. memmove(forwardBuffer, forwardBuffer + pos, actual);
  644. pos = 0;
  645. int t;
  646. // this will block if there is no data at all
  647. t = read_socket(session, forwardBuffer + actual,
  648. sizeof(forwardBuffer) - actual);
  649. if (t <= 0)
  650. {
  651. goto closeSession;
  652. }
  653. actual += t;
  654. }
  655. int y2 = 0;
  656. if (actual > 0)
  657. {
  658. /* Do not put things into the serial buffer if it has something to send
  659. * as that can cause a single byte to be sent at the time.
  660. *
  661. *
  662. */
  663. int written = write(serHandle, forwardBuffer + pos, actual);
  664. if (written < 0)
  665. {
  666. if (errno != EAGAIN)
  667. {
  668. goto closeSession;
  669. }
  670. // The serial buffer is full
  671. written = 0;
  672. }
  673. else
  674. {
  675. actual -= written;
  676. pos += written;
  677. }
  678. y2 = written;
  679. }
  680. #if TEST_TCPIP
  681. if (cur < 1024)
  682. {
  683. tcpipSent[cur].req = x;
  684. tcpipSent[cur].actual = y;
  685. tcpipSent[cur].req2 = x2;
  686. tcpipSent[cur].actual2 = y2;
  687. cur++;
  688. }
  689. #endif
  690. }
  691. closeSession: close(session);
  692. close(serHandle);
  693. #if TEST_TCPIP
  694. int i;
  695. for (i = 0; i < 1024; i++)
  696. {
  697. diag_printf("%d %d %d %d\n", tcpipSent[i].req, tcpipSent[i].actual,
  698. tcpipSent[i].req2, tcpipSent[i].actual2);
  699. }
  700. #endif
  701. }
  702. close(fd);
  703. }
  704. void startUart(void)
  705. {
  706. cyg_thread_create(1, zylinjtag_uart, (cyg_addrword_t) 0, "uart thread",
  707. (void *) uart_stack, sizeof(uart_stack),
  708. &zylinjtag_uart_thread_handle, &zylinjtag_uart_thread_object);
  709. cyg_thread_set_priority(zylinjtag_uart_thread_handle, 1); // low priority as it sits in a busy loop
  710. cyg_thread_resume(zylinjtag_uart_thread_handle);
  711. }
  712. static int zylinjtag_Jim_Command_uart(Jim_Interp *interp, int argc,
  713. Jim_Obj * const *argv)
  714. {
  715. static int current_baud = 38400;
  716. if (argc == 1)
  717. {
  718. Jim_SetResult(interp, Jim_NewIntObj(interp, current_baud));
  719. return JIM_OK;
  720. }
  721. else if (argc != 2)
  722. {
  723. return JIM_ERR;
  724. }
  725. long new_baudrate;
  726. if (Jim_GetLong(interp, argv[1], &new_baudrate) != JIM_OK)
  727. return JIM_ERR;
  728. current_baud = new_baudrate;
  729. int baud;
  730. switch (current_baud)
  731. {
  732. case 9600:
  733. baud = CYGNUM_SERIAL_BAUD_9600;
  734. break;
  735. case 19200:
  736. baud = CYGNUM_SERIAL_BAUD_19200;
  737. break;
  738. case 38400:
  739. baud = CYGNUM_SERIAL_BAUD_38400;
  740. break;
  741. case 57600:
  742. baud = CYGNUM_SERIAL_BAUD_57600;
  743. break;
  744. case 115200:
  745. baud = CYGNUM_SERIAL_BAUD_115200;
  746. break;
  747. case 230400:
  748. baud = CYGNUM_SERIAL_BAUD_230400;
  749. break;
  750. default:
  751. Jim_SetResult(interp, Jim_NewStringObj(interp, "unsupported baudrate", -1));
  752. return JIM_ERR;
  753. }
  754. cyg_serial_info_t buf;
  755. cyg_uint32 len = 1;
  756. //get existing serial configuration
  757. len = sizeof(cyg_serial_info_t);
  758. int err;
  759. cyg_io_handle_t serial_handle;
  760. err = cyg_io_lookup(ZY1000_SER_DEV, &serial_handle);
  761. if (err != ENOERR)
  762. {
  763. Jim_SetResult(interp, Jim_NewStringObj(interp, "Could not open serial port", -1));
  764. return JIM_ERR;
  765. }
  766. err = cyg_io_get_config(serial_handle,
  767. CYG_IO_GET_CONFIG_SERIAL_OUTPUT_DRAIN, &buf, &len);
  768. err = cyg_io_get_config(serial_handle, CYG_IO_GET_CONFIG_SERIAL_INFO, &buf,
  769. &len);
  770. if (err != ENOERR)
  771. {
  772. Jim_SetResult(interp, Jim_NewStringObj(interp, "Failed to get serial port settings", -1));
  773. return JIM_ERR;
  774. }
  775. buf.baud = baud;
  776. err = cyg_io_set_config(serial_handle, CYG_IO_SET_CONFIG_SERIAL_INFO, &buf,
  777. &len);
  778. if (err != ENOERR)
  779. {
  780. Jim_SetResult(interp, Jim_NewStringObj(interp, "Failed to set serial port settings", -1));
  781. return JIM_ERR;
  782. }
  783. return JIM_OK;
  784. }
  785. bool logAllToSerial = false;
  786. int boolParam(char *var);
  787. static const char *zylin_config_dir="/config/settings";
  788. static int add_default_dirs(void)
  789. {
  790. add_script_search_dir(zylin_config_dir);
  791. add_script_search_dir("/rom/lib/openocd");
  792. add_script_search_dir("/rom");
  793. return ERROR_OK;
  794. }
  795. int main(int argc, char *argv[])
  796. {
  797. /* ramblockdevice will be the same address every time. The deflate app uses a buffer 16mBytes out, so we
  798. * need to allocate towards the end of the heap. */
  799. #ifdef CYGNUM_HAL_VECTOR_UNDEF_INSTRUCTION
  800. setHandler(CYGNUM_HAL_VECTOR_UNDEF_INSTRUCTION);
  801. setHandler(CYGNUM_HAL_VECTOR_ABORT_PREFETCH);
  802. setHandler(CYGNUM_HAL_VECTOR_ABORT_DATA);
  803. #endif
  804. int err;
  805. atexit(keep_webserver);
  806. diag_init_putc(_zylinjtag_diag_write_char);
  807. // We want this in the log.
  808. #ifdef CYGPKG_HAL_NIOS2
  809. diag_printf("Zylin ZY1000 PCB revc.\n");
  810. #else
  811. diag_printf("Zylin ZY1000 PCB revb.\n");
  812. #endif
  813. err = mount("", "/ram", "ramfs");
  814. if (err < 0)
  815. {
  816. diag_printf("unable to mount ramfs\n");
  817. }
  818. chdir("/ram");
  819. char address[16];
  820. sprintf(address, "%p", &filedata[0]);
  821. err = mount(address, "/rom", "romfs");
  822. if (err < 0)
  823. {
  824. diag_printf("unable to mount /rom\n");
  825. }
  826. err = mount("", "/log", "logfs");
  827. if (err < 0)
  828. {
  829. diag_printf("unable to mount logfs\n");
  830. }
  831. err = mount("", "/tftp", "tftpfs");
  832. if (err < 0)
  833. {
  834. diag_printf("unable to mount logfs\n");
  835. }
  836. log = fopen("/log/log", "w");
  837. if (log == NULL)
  838. {
  839. diag_printf("Could not open log file /ram/log\n");
  840. exit(-1);
  841. }
  842. copydir("/rom", "/ram/cgi");
  843. #ifdef CYGPKG_HAL_NIOS2
  844. cyg_flashaddr_t err_address;
  845. #define UNCACHED_EXT_FLASH_BASE (0x80000000 + EXT_FLASH_BASE)
  846. /* The revc flash is locked upon reset, unlock it */
  847. #ifdef CYGHWR_IO_FLASH_BLOCK_LOCKING
  848. if ((err = flash_unlock((void *) UNCACHED_EXT_FLASH_BASE, EXT_FLASH_SPAN,
  849. (void **) &err_address)) != 0)
  850. {
  851. diag_printf("Error: could not unlock flash\n");
  852. }
  853. #endif
  854. #endif
  855. err = mount("/dev/flash1", "/config", "jffs2");
  856. if (err < 0)
  857. {
  858. diag_printf("unable to mount jffs2, falling back to ram disk..\n");
  859. err = mount("", "/config", "ramfs");
  860. if (err < 0)
  861. {
  862. diag_printf("unable to mount /config as ramdisk.\n");
  863. reboot();
  864. }
  865. }
  866. else
  867. {
  868. /* are we using a ram disk instead of a flash disk? This is used
  869. * for ZY1000 live demo...
  870. *
  871. * copy over flash disk to ram block device
  872. */
  873. if (boolParam("ramdisk"))
  874. {
  875. diag_printf("Unmounting /config from flash and using ram instead\n");
  876. err = umount("/config");
  877. if (err < 0)
  878. {
  879. diag_printf("unable to unmount jffs\n");
  880. reboot();
  881. }
  882. err = mount("/dev/flash1", "/config2", "jffs2");
  883. if (err < 0)
  884. {
  885. diag_printf("unable to mount jffs\n");
  886. reboot();
  887. }
  888. err = mount("", "/config", "ramfs");
  889. if (err < 0)
  890. {
  891. diag_printf("unable to mount ram block device\n");
  892. reboot();
  893. }
  894. // copydir("/config2", "/config");
  895. copyfile("/config2/ip", "/config/ip");
  896. copydir("/config2/settings", "/config/settings");
  897. umount("/config2");
  898. }
  899. }
  900. mkdir(zylin_config_dir, 0777);
  901. char *dirname = alloc_printf("%s/target", zylin_config_dir);
  902. mkdir(dirname, 0777);
  903. free(dirname);
  904. dirname = alloc_printf("%s/board", zylin_config_dir);
  905. mkdir(dirname, 0777);
  906. free(dirname);
  907. dirname = alloc_printf("%s/event", zylin_config_dir);
  908. mkdir(dirname, 0777);
  909. free(dirname);
  910. logAllToSerial = boolParam("logserial");
  911. // We need the network & web server in case there is something wrong with
  912. // the config files that invoke exit()
  913. zylinjtag_startNetwork();
  914. /* we're going to access the jim interpreter from here on... */
  915. openocd_sleep_postlude();
  916. startUart();
  917. add_default_dirs();
  918. /* initialize commandline interface */
  919. struct command_context * cmd_ctx;
  920. struct command_context *setup_command_handler(Jim_Interp *interp);
  921. cmd_ctx = setup_command_handler(httpstate.jim_interp);
  922. command_set_output_handler(cmd_ctx, configuration_output_handler, NULL);
  923. command_context_mode(cmd_ctx, COMMAND_CONFIG);
  924. if (util_init(cmd_ctx) != ERROR_OK)
  925. return EXIT_FAILURE;
  926. if (ioutil_init(cmd_ctx) != ERROR_OK)
  927. return EXIT_FAILURE;
  928. #ifdef CYGPKG_PROFILE_GPROF
  929. Jim_CreateCommand(httpstate.jim_interp, "zy1000_profile", zylinjtag_Jim_Command_profile,
  930. NULL, NULL);
  931. #endif
  932. Jim_CreateCommand(httpstate.jim_interp, "zy1000_uart", zylinjtag_Jim_Command_uart, NULL, NULL);
  933. log_init();
  934. set_log_output(cmd_ctx, log);
  935. LOG_DEBUG("log init complete");
  936. // diag_printf("Executing config files\n");
  937. if (logAllToSerial)
  938. {
  939. diag_printf(
  940. "%s/logserial = 1 => sending log output to serial port using \"debug_level 3\" as default.\n", zylin_config_dir);
  941. command_run_line(cmd_ctx, "debug_level 3");
  942. }
  943. command_run_linef(cmd_ctx, "script /rom/openocd.cfg");
  944. int ret;
  945. ret = server_init(cmd_ctx);
  946. if (ERROR_OK != ret)
  947. return EXIT_FAILURE;
  948. /* we MUST always run the init command as it will launch telnet sessions */
  949. command_run_line(cmd_ctx, "init");
  950. // FIX!!! Yuk!
  951. // diag_printf() is really invoked from many more places than we trust it
  952. // not to cause instabilities(e.g. invoking fputc() from an interrupt is *BAD*).
  953. //
  954. // Disabling it here is safe and gives us enough logged debug output for now. Crossing
  955. // fingers that it doesn't cause any crashes.
  956. diag_printf("Init complete, GDB & telnet servers launched.\n");
  957. command_set_output_handler(cmd_ctx,
  958. zy1000_configuration_output_handler_log, NULL);
  959. if (!logAllToSerial)
  960. {
  961. serialLog = false;
  962. }
  963. /* handle network connections */
  964. server_loop(cmd_ctx);
  965. openocd_sleep_prelude();
  966. /* shut server down */
  967. server_quit();
  968. /* free commandline interface */
  969. command_done(cmd_ctx);
  970. umount("/config");
  971. exit(0);
  972. for (;;)
  973. ;
  974. }
  975. cyg_int32 cyg_httpd_exec_cgi_tcl(char *file_name);
  976. cyg_int32 homeForm(CYG_HTTPD_STATE *p)
  977. {
  978. cyg_httpd_exec_cgi_tcl("/ram/cgi/index.tcl");
  979. return 0;
  980. }
  981. CYG_HTTPD_HANDLER_TABLE_ENTRY(root_label, "/", homeForm);
  982. CYG_HTTPD_MIME_TABLE_ENTRY(text_mime_label, "text", "text/plain");
  983. CYG_HTTPD_MIME_TABLE_ENTRY(bin_mime_label, "bin", "application/octet-stream");
  984. #include <pkgconf/system.h>
  985. #include <pkgconf/hal.h>
  986. #include <pkgconf/kernel.h>
  987. #include <pkgconf/io_fileio.h>
  988. #include <pkgconf/fs_rom.h>
  989. #include <cyg/kernel/ktypes.h> // base kernel types
  990. #include <cyg/infra/cyg_trac.h> // tracing macros
  991. #include <cyg/infra/cyg_ass.h> // assertion macros
  992. #include <cyg/fileio/fileio.h>
  993. #include <cyg/kernel/kapi.h>
  994. #include <cyg/infra/diag.h>
  995. //==========================================================================
  996. // Eventually we want to eXecute In Place from the ROM in a protected
  997. // environment, so we'll need executables to be aligned to a boundary
  998. // suitable for MMU protection. A suitable boundary would be the 4k
  999. // boundary in all the CPU architectures I am currently aware of.
  1000. // Forward definitions
  1001. // Filesystem operations
  1002. static int tftpfs_mount(cyg_fstab_entry *fste, cyg_mtab_entry *mte);
  1003. static int tftpfs_umount(cyg_mtab_entry *mte);
  1004. static int tftpfs_open(cyg_mtab_entry *mte, cyg_dir dir, const char *name,
  1005. int mode, cyg_file *fte);
  1006. static int tftpfs_fo_read(struct CYG_FILE_TAG *fp, struct CYG_UIO_TAG *uio);
  1007. static int tftpfs_fo_write(struct CYG_FILE_TAG *fp, struct CYG_UIO_TAG *uio);
  1008. // File operations
  1009. static int tftpfs_fo_fsync(struct CYG_FILE_TAG *fp, int mode);
  1010. static int tftpfs_fo_close(struct CYG_FILE_TAG *fp);
  1011. static int tftpfs_fo_lseek(struct CYG_FILE_TAG *fp, off_t *apos, int whence);
  1012. //==========================================================================
  1013. // Filesystem table entries
  1014. // -------------------------------------------------------------------------
  1015. // Fstab entry.
  1016. // This defines the entry in the filesystem table.
  1017. // For simplicity we use _FILESYSTEM synchronization for all accesses since
  1018. // we should never block in any filesystem operations.
  1019. #if 1
  1020. FSTAB_ENTRY(tftpfs_fste, "tftpfs", 0,
  1021. CYG_SYNCMODE_NONE,
  1022. tftpfs_mount,
  1023. tftpfs_umount,
  1024. tftpfs_open,
  1025. (cyg_fsop_unlink *)cyg_fileio_erofs,
  1026. (cyg_fsop_mkdir *)cyg_fileio_erofs,
  1027. (cyg_fsop_rmdir *)cyg_fileio_erofs,
  1028. (cyg_fsop_rename *)cyg_fileio_erofs,
  1029. (cyg_fsop_link *)cyg_fileio_erofs,
  1030. (cyg_fsop_opendir *)cyg_fileio_erofs,
  1031. (cyg_fsop_chdir *)cyg_fileio_erofs,
  1032. (cyg_fsop_stat *)cyg_fileio_erofs,
  1033. (cyg_fsop_getinfo *)cyg_fileio_erofs,
  1034. (cyg_fsop_setinfo *)cyg_fileio_erofs);
  1035. #endif
  1036. // -------------------------------------------------------------------------
  1037. // mtab entry.
  1038. // This defines a single ROMFS loaded into ROM at the configured address
  1039. //
  1040. // MTAB_ENTRY(rom_mte, // structure name
  1041. // "/rom", // mount point
  1042. // "romfs", // FIlesystem type
  1043. // "", // hardware device
  1044. // (CYG_ADDRWORD) CYGNUM_FS_ROM_BASE_ADDRESS // Address in ROM
  1045. //);
  1046. // -------------------------------------------------------------------------
  1047. // File operations.
  1048. // This set of file operations are used for normal open files.
  1049. static cyg_fileops tftpfs_fileops =
  1050. { tftpfs_fo_read, tftpfs_fo_write, tftpfs_fo_lseek,
  1051. (cyg_fileop_ioctl *) cyg_fileio_erofs, cyg_fileio_seltrue,
  1052. tftpfs_fo_fsync, tftpfs_fo_close,
  1053. (cyg_fileop_fstat *) cyg_fileio_erofs,
  1054. (cyg_fileop_getinfo *) cyg_fileio_erofs,
  1055. (cyg_fileop_setinfo *) cyg_fileio_erofs, };
  1056. // -------------------------------------------------------------------------
  1057. // tftpfs_mount()
  1058. // Process a mount request. This mainly finds root for the
  1059. // filesystem.
  1060. static int tftpfs_mount(cyg_fstab_entry *fste, cyg_mtab_entry *mte)
  1061. {
  1062. return ENOERR;
  1063. }
  1064. static int tftpfs_umount(cyg_mtab_entry *mte)
  1065. {
  1066. return ENOERR;
  1067. }
  1068. struct Tftp
  1069. {
  1070. int write;
  1071. int readFile;
  1072. cyg_uint8 *mem;
  1073. int actual;
  1074. char *server;
  1075. int port;
  1076. char *file;
  1077. };
  1078. static void freeTftp(struct Tftp *t)
  1079. {
  1080. if (t == NULL)
  1081. return;
  1082. if (t->mem)
  1083. free(t->mem);
  1084. if (t->server)
  1085. free(t->server);
  1086. if (t->file)
  1087. free(t->file);
  1088. free(t);
  1089. }
  1090. static const int tftpMaxSize = 8192 * 1024;
  1091. static int tftpfs_open(cyg_mtab_entry *mte, cyg_dir dir, const char *name,
  1092. int mode, cyg_file *file)
  1093. {
  1094. struct Tftp *tftp;
  1095. tftp = malloc(sizeof(struct Tftp));
  1096. if (tftp == NULL)
  1097. return EMFILE;
  1098. memset(tftp, 0, sizeof(struct Tftp));
  1099. file->f_flag |= mode & CYG_FILE_MODE_MASK;
  1100. file->f_type = CYG_FILE_TYPE_FILE;
  1101. file->f_ops = &tftpfs_fileops;
  1102. file->f_offset = 0;
  1103. file->f_data = 0;
  1104. file->f_xops = 0;
  1105. tftp->mem = malloc(tftpMaxSize);
  1106. if (tftp->mem == NULL)
  1107. {
  1108. freeTftp(tftp);
  1109. return EMFILE;
  1110. }
  1111. char *server = strchr(name, '/');
  1112. if (server == NULL)
  1113. {
  1114. freeTftp(tftp);
  1115. return EMFILE;
  1116. }
  1117. tftp->server = malloc(server - name + 1);
  1118. if (tftp->server == NULL)
  1119. {
  1120. freeTftp(tftp);
  1121. return EMFILE;
  1122. }
  1123. strncpy(tftp->server, name, server - name);
  1124. tftp->server[server - name] = 0;
  1125. tftp->port = 0; /* default port 69 */
  1126. char *port;
  1127. port = strchr(tftp->server, ':');
  1128. if (port != NULL)
  1129. {
  1130. tftp->port = atoi(port + 1);
  1131. *port = 0;
  1132. }
  1133. tftp->file = strdup(server + 1);
  1134. if (tftp->file == NULL)
  1135. {
  1136. freeTftp(tftp);
  1137. return EMFILE;
  1138. }
  1139. file->f_data = (CYG_ADDRWORD) tftp;
  1140. return ENOERR;
  1141. }
  1142. static int fetchTftp(struct Tftp *tftp)
  1143. {
  1144. if (!tftp->readFile)
  1145. {
  1146. int err;
  1147. tftp->actual = tftp_client_get(tftp->file, tftp->server, tftp->port, tftp->mem,
  1148. tftpMaxSize, TFTP_OCTET, &err);
  1149. if (tftp->actual < 0)
  1150. {
  1151. return EMFILE;
  1152. }
  1153. tftp->readFile = 1;
  1154. }
  1155. return ENOERR;
  1156. }
  1157. // -------------------------------------------------------------------------
  1158. // tftpfs_fo_write()
  1159. // Read data from file.
  1160. static int tftpfs_fo_read(struct CYG_FILE_TAG *fp, struct CYG_UIO_TAG *uio)
  1161. {
  1162. struct Tftp *tftp = (struct Tftp *) fp->f_data;
  1163. if (fetchTftp(tftp) != ENOERR)
  1164. return EMFILE;
  1165. int i;
  1166. off_t pos = fp->f_offset;
  1167. int resid = 0;
  1168. for (i = 0; i < uio->uio_iovcnt; i++)
  1169. {
  1170. cyg_iovec *iov = &uio->uio_iov[i];
  1171. char *buf = (char *) iov->iov_base;
  1172. off_t len = iov->iov_len;
  1173. if (len + pos > tftp->actual)
  1174. {
  1175. len = tftp->actual - pos;
  1176. }
  1177. resid += iov->iov_len - len;
  1178. memcpy(buf, tftp->mem + pos, len);
  1179. pos += len;
  1180. }
  1181. uio->uio_resid = resid;
  1182. fp->f_offset = pos;
  1183. return ENOERR;
  1184. }
  1185. static int tftpfs_fo_write(struct CYG_FILE_TAG *fp, struct CYG_UIO_TAG *uio)
  1186. {
  1187. struct Tftp *tftp = (struct Tftp *) fp->f_data;
  1188. int i;
  1189. off_t pos = fp->f_offset;
  1190. int resid = 0;
  1191. for (i = 0; i < uio->uio_iovcnt; i++)
  1192. {
  1193. cyg_iovec *iov = &uio->uio_iov[i];
  1194. char *buf = (char *) iov->iov_base;
  1195. off_t len = iov->iov_len;
  1196. if (len + pos > tftpMaxSize)
  1197. {
  1198. len = tftpMaxSize - pos;
  1199. }
  1200. resid += iov->iov_len - len;
  1201. memcpy(tftp->mem + pos, buf, len);
  1202. pos += len;
  1203. }
  1204. uio->uio_resid = resid;
  1205. fp->f_offset = pos;
  1206. tftp->write = 1;
  1207. return ENOERR;
  1208. }
  1209. static int tftpfs_fo_fsync(struct CYG_FILE_TAG *fp, int mode)
  1210. {
  1211. int error = ENOERR;
  1212. return error;
  1213. }
  1214. // -------------------------------------------------------------------------
  1215. // romfs_fo_close()
  1216. // Close a file. We just clear out the data pointer.
  1217. static int tftpfs_fo_close(struct CYG_FILE_TAG *fp)
  1218. {
  1219. struct Tftp *tftp = (struct Tftp *) fp->f_data;
  1220. int error = ENOERR;
  1221. if (tftp->write)
  1222. {
  1223. tftp_client_put(tftp->file, tftp->server, 0, tftp->mem, fp->f_offset,
  1224. TFTP_OCTET, &error);
  1225. }
  1226. freeTftp(tftp);
  1227. fp->f_data = 0;
  1228. return error;
  1229. }
  1230. // -------------------------------------------------------------------------
  1231. // romfs_fo_lseek()
  1232. // Seek to a new file position.
  1233. static int tftpfs_fo_lseek(struct CYG_FILE_TAG *fp, off_t *apos, int whence)
  1234. {
  1235. struct Tftp *tftp = (struct Tftp *) fp->f_data;
  1236. off_t pos = *apos;
  1237. if (fetchTftp(tftp) != ENOERR)
  1238. return EMFILE;
  1239. switch (whence)
  1240. {
  1241. case SEEK_SET:
  1242. // Pos is already where we want to be.
  1243. break;
  1244. case SEEK_CUR:
  1245. // Add pos to current offset.
  1246. pos += fp->f_offset;
  1247. break;
  1248. case SEEK_END:
  1249. // Add pos to file size.
  1250. pos += tftp->actual;
  1251. break;
  1252. default:
  1253. return EINVAL;
  1254. }
  1255. // Check that pos is still within current file size, or at the
  1256. // very end.
  1257. if (pos < 0 || pos > tftp->actual)
  1258. return EINVAL;
  1259. // All OK, set fp offset and return new position.
  1260. *apos = fp->f_offset = pos;
  1261. return ENOERR;
  1262. }
  1263. void usleep(int us)
  1264. {
  1265. if (us > 10000)
  1266. cyg_thread_delay(us / 10000 + 1);
  1267. else
  1268. HAL_DELAY_US(us);
  1269. }
  1270. // Chunked version.
  1271. cyg_int32 show_log_entry(CYG_HTTPD_STATE *phttpstate)
  1272. {
  1273. cyg_httpd_start_chunked("text");
  1274. if (logCount >= logSize)
  1275. {
  1276. cyg_httpd_write_chunked(logBuffer + logCount % logSize, logSize
  1277. - logCount % logSize);
  1278. }
  1279. cyg_httpd_write_chunked(logBuffer, writePtr);
  1280. cyg_httpd_end_chunked();
  1281. return -1;
  1282. }
  1283. CYG_HTTPD_HANDLER_TABLE_ENTRY(show_log, "/ram/log", show_log_entry);
  1284. // Filesystem operations
  1285. static int logfs_mount(cyg_fstab_entry *fste, cyg_mtab_entry *mte);
  1286. static int logfs_umount(cyg_mtab_entry *mte);
  1287. static int logfs_open(cyg_mtab_entry *mte, cyg_dir dir, const char *name,
  1288. int mode, cyg_file *fte);
  1289. static int logfs_fo_write(struct CYG_FILE_TAG *fp, struct CYG_UIO_TAG *uio);
  1290. // File operations
  1291. static int logfs_fo_fsync(struct CYG_FILE_TAG *fp, int mode);
  1292. static int logfs_fo_close(struct CYG_FILE_TAG *fp);
  1293. #include <cyg/io/devtab.h>
  1294. //==========================================================================
  1295. // Filesystem table entries
  1296. // -------------------------------------------------------------------------
  1297. // Fstab entry.
  1298. // This defines the entry in the filesystem table.
  1299. // For simplicity we use _FILESYSTEM synchronization for all accesses since
  1300. // we should never block in any filesystem operations.
  1301. FSTAB_ENTRY(logfs_fste, "logfs", 0,
  1302. CYG_SYNCMODE_FILE_FILESYSTEM | CYG_SYNCMODE_IO_FILESYSTEM,
  1303. logfs_mount,
  1304. logfs_umount,
  1305. logfs_open,
  1306. (cyg_fsop_unlink *)cyg_fileio_erofs,
  1307. (cyg_fsop_mkdir *)cyg_fileio_erofs,
  1308. (cyg_fsop_rmdir *)cyg_fileio_erofs,
  1309. (cyg_fsop_rename *)cyg_fileio_erofs,
  1310. (cyg_fsop_link *)cyg_fileio_erofs,
  1311. (cyg_fsop_opendir *)cyg_fileio_erofs,
  1312. (cyg_fsop_chdir *)cyg_fileio_erofs,
  1313. (cyg_fsop_stat *)cyg_fileio_erofs,
  1314. (cyg_fsop_getinfo *)cyg_fileio_erofs,
  1315. (cyg_fsop_setinfo *)cyg_fileio_erofs);
  1316. // -------------------------------------------------------------------------
  1317. // File operations.
  1318. // This set of file operations are used for normal open files.
  1319. static cyg_fileops logfs_fileops =
  1320. { (cyg_fileop_read *) cyg_fileio_erofs, (cyg_fileop_write *) logfs_fo_write,
  1321. (cyg_fileop_lseek *) cyg_fileio_erofs,
  1322. (cyg_fileop_ioctl *) cyg_fileio_erofs, cyg_fileio_seltrue,
  1323. logfs_fo_fsync, logfs_fo_close, (cyg_fileop_fstat *) cyg_fileio_erofs,
  1324. (cyg_fileop_getinfo *) cyg_fileio_erofs,
  1325. (cyg_fileop_setinfo *) cyg_fileio_erofs, };
  1326. // -------------------------------------------------------------------------
  1327. // logfs_mount()
  1328. // Process a mount request. This mainly finds root for the
  1329. // filesystem.
  1330. static int logfs_mount(cyg_fstab_entry *fste, cyg_mtab_entry *mte)
  1331. {
  1332. return ENOERR;
  1333. }
  1334. static int logfs_umount(cyg_mtab_entry *mte)
  1335. {
  1336. return ENOERR;
  1337. }
  1338. static int logfs_open(cyg_mtab_entry *mte, cyg_dir dir, const char *name,
  1339. int mode, cyg_file *file)
  1340. {
  1341. file->f_flag |= mode & CYG_FILE_MODE_MASK;
  1342. file->f_type = CYG_FILE_TYPE_FILE;
  1343. file->f_ops = &logfs_fileops;
  1344. file->f_offset = 0;
  1345. file->f_data = 0;
  1346. file->f_xops = 0;
  1347. return ENOERR;
  1348. }
  1349. // -------------------------------------------------------------------------
  1350. // logfs_fo_write()
  1351. // Write data to file.
  1352. static int logfs_fo_write(struct CYG_FILE_TAG *fp, struct CYG_UIO_TAG *uio)
  1353. {
  1354. int i;
  1355. for (i = 0; i < uio->uio_iovcnt; i++)
  1356. {
  1357. cyg_iovec *iov = &uio->uio_iov[i];
  1358. char *buf = (char *) iov->iov_base;
  1359. off_t len = iov->iov_len;
  1360. diag_write(buf, len);
  1361. }
  1362. uio->uio_resid = 0;
  1363. return ENOERR;
  1364. }
  1365. static int logfs_fo_fsync(struct CYG_FILE_TAG *fp, int mode)
  1366. {
  1367. return ENOERR;
  1368. }
  1369. // -------------------------------------------------------------------------
  1370. // romfs_fo_close()
  1371. // Close a file. We just clear out the data pointer.
  1372. static int logfs_fo_close(struct CYG_FILE_TAG *fp)
  1373. {
  1374. return ENOERR;
  1375. }
  1376. int loadFile(const char *fileName, void **data, int *len);
  1377. /* boolean parameter stored on config */
  1378. int boolParam(char *var)
  1379. {
  1380. bool result = false;
  1381. char *name = alloc_printf("%s/%s", zylin_config_dir, var);
  1382. if (name == NULL)
  1383. return result;
  1384. void *data;
  1385. int len;
  1386. if (loadFile(name, &data, &len) == ERROR_OK)
  1387. {
  1388. if (len > 1)
  1389. len = 1;
  1390. result = strncmp((char *) data, "1", len) == 0;
  1391. free(data);
  1392. }
  1393. free(name);
  1394. return result;
  1395. }