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.
 
 
 
 
 
 

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