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.
 
 
 
 
 
 

1633 lines
38 KiB

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