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.
 
 
 
 
 
 

1639 lines
38 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2007-2009 by Øyvind Harboe *
  3. * *
  4. * This program is free software; you can redistribute it and/or modify *
  5. * it under the terms of the GNU General Public License as published by *
  6. * the Free Software Foundation; either version 2 of the License, or *
  7. * (at your option) any later version. *
  8. * *
  9. * This program is distributed in the hope that it will be useful, *
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  12. * GNU General Public License for more details. *
  13. * *
  14. * You should have received a copy of the GNU General Public License *
  15. * along with this program; if not, write to the *
  16. * Free Software Foundation, Inc., *
  17. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  18. ***************************************************************************/
  19. #ifdef HAVE_CONFIG_H
  20. #include "config.h"
  21. #endif
  22. #include <helper/types.h>
  23. #include <jtag/jtag.h>
  24. #include <helper/ioutil.h>
  25. #include <helper/configuration.h>
  26. #include <server/server.h>
  27. #include <server/telnet_server.h>
  28. #include <server/gdb_server.h>
  29. #include <openocd.h>
  30. #include <helper/time_support.h>
  31. #include <sys/time.h>
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <string.h>
  35. #include <unistd.h>
  36. #include <errno.h>
  37. #include <cyg/io/flash.h>
  38. #include <pkgconf/fs_jffs2.h> // Address of JFFS2
  39. #include <network.h>
  40. #include <fcntl.h>
  41. #include <sys/stat.h>
  42. #include <cyg/fileio/fileio.h>
  43. #include <dirent.h>
  44. #include <cyg/athttpd/http.h>
  45. #include <cyg/athttpd/socket.h>
  46. #include <cyg/athttpd/handler.h>
  47. #include <cyg/athttpd/cgi.h>
  48. #include <cyg/athttpd/forms.h>
  49. #include <cyg/discover/discover.h>
  50. #include <cyg/hal/hal_diag.h>
  51. #include <cyg/kernel/kapi.h>
  52. #include <cyg/io/serialio.h>
  53. #include <cyg/io/io.h>
  54. #include <netinet/tcp.h>
  55. #include "rom.h"
  56. #include <sys/ioctl.h>
  57. #include <sys/socket.h>
  58. #include <netinet/in.h>
  59. #include <net/if.h>
  60. #include <arpa/inet.h>
  61. #include <sys/types.h>
  62. #include <sys/socket.h>
  63. #include <netdb.h>
  64. #include <netinet/in.h>
  65. #include <unistd.h>
  66. #include <arpa/inet.h>
  67. #include <stdio.h>
  68. #include <ifaddrs.h>
  69. #include <string.h>
  70. #include <unistd.h>
  71. #include <stdio.h>
  72. #include <openocd.h>
  73. #ifdef CYGPKG_HAL_NIOS2
  74. #define ZY1000_SER_DEV "/dev/uart_0"
  75. #else
  76. #define ZY1000_SER_DEV "/dev/ser0"
  77. #endif
  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 struct flash_driver *flash_drivers[];
  102. extern struct target_type *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 *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 *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 *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. struct command_context *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. Jim_CreateCommand(httpstate.jim_interp, "log", zylinjtag_Jim_Command_log,
  414. NULL, NULL);
  415. Jim_CreateCommand(httpstate.jim_interp, "zy1000_reboot",
  416. zylinjtag_Jim_Command_reboot, NULL, NULL);
  417. Jim_CreateCommand(httpstate.jim_interp, "threads",
  418. zylinjtag_Jim_Command_threads, NULL, NULL);
  419. Jim_CreateCommand(httpstate.jim_interp, "format_jffs2",
  420. zylinjtag_Jim_Command_format_jffs2, NULL, NULL);
  421. cyg_httpd_start();
  422. webRunning = true;
  423. diag_printf("Web server running\n");
  424. int s;
  425. struct ifreq ifr;
  426. s = socket(AF_INET, SOCK_DGRAM, 0);
  427. if (s >= 0)
  428. {
  429. strcpy(ifr.ifr_name, "eth0");
  430. int res;
  431. res = ioctl(s, SIOCGIFHWADDR, &ifr);
  432. close(s);
  433. if (res < 0)
  434. {
  435. diag_printf("Can't obtain MAC address\n");
  436. reboot();
  437. }
  438. }
  439. sprintf(hwaddr, "%02x:%02x:%02x:%02x:%02x:%02x",
  440. (int) ((unsigned char *) &ifr.ifr_hwaddr.sa_data)[0],
  441. (int) ((unsigned char *) &ifr.ifr_hwaddr.sa_data)[1],
  442. (int) ((unsigned char *) &ifr.ifr_hwaddr.sa_data)[2],
  443. (int) ((unsigned char *) &ifr.ifr_hwaddr.sa_data)[3],
  444. (int) ((unsigned char *) &ifr.ifr_hwaddr.sa_data)[4],
  445. (int) ((unsigned char *) &ifr.ifr_hwaddr.sa_data)[5]);
  446. discover_message
  447. = alloc_printf("ZY1000 Zylin JTAG debugger MAC %s", hwaddr);
  448. discover_launch();
  449. }
  450. static void print_exception_handler(cyg_addrword_t data, cyg_code_t exception,
  451. cyg_addrword_t info)
  452. {
  453. writeLog = false;
  454. serialLog = true;
  455. char *infoStr = "unknown";
  456. switch (exception)
  457. {
  458. #ifdef CYGNUM_HAL_VECTOR_UNDEF_INSTRUCTION
  459. case CYGNUM_HAL_VECTOR_UNDEF_INSTRUCTION:
  460. infoStr = "undefined instruction";
  461. break;
  462. case CYGNUM_HAL_VECTOR_SOFTWARE_INTERRUPT:
  463. infoStr = "software interrupt";
  464. break;
  465. case CYGNUM_HAL_VECTOR_ABORT_PREFETCH:
  466. infoStr = "abort prefetch";
  467. break;
  468. case CYGNUM_HAL_VECTOR_ABORT_DATA:
  469. infoStr = "abort data";
  470. break;
  471. #endif
  472. default:
  473. break;
  474. }
  475. diag_printf("Exception: %08x(%s) %08x\n", exception, infoStr, info);
  476. diag_printf("Dumping log\n---\n");
  477. if (logCount >= logSize)
  478. {
  479. diag_write(logBuffer + logCount % logSize, logSize - logCount % logSize);
  480. }
  481. diag_write(logBuffer, writePtr);
  482. diag_printf("---\nLogdump complete.\n");
  483. diag_printf("Exception: %08x(%s) %08x\n", exception, infoStr, info);
  484. diag_printf("\n---\nRebooting\n");
  485. HAL_PLATFORM_RESET();
  486. }
  487. #ifdef CYGNUM_HAL_VECTOR_UNDEF_INSTRUCTION
  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. #endif
  496. static cyg_thread zylinjtag_uart_thread_object;
  497. static cyg_handle_t zylinjtag_uart_thread_handle;
  498. static char uart_stack[4096];
  499. static char forwardBuffer[1024]; // NB! must be smaller than a TCP/IP packet!!!!!
  500. static char backwardBuffer[1024];
  501. void setNoDelay(int session, int flag)
  502. {
  503. #if 1
  504. // This decreases latency dramatically for e.g. GDB load which
  505. // does not have a sliding window protocol
  506. //
  507. // Can cause *lots* of TCP/IP packets to be sent and it would have
  508. // to be enabled/disabled on the fly to avoid the CPU being
  509. // overloaded...
  510. setsockopt(session, /* socket affected */
  511. IPPROTO_TCP, /* set option at TCP level */
  512. TCP_NODELAY, /* name of option */
  513. (char *) &flag, /* the cast is historical
  514. cruft */
  515. sizeof(int)); /* length of option value */
  516. #endif
  517. }
  518. #define TEST_TCPIP() 0
  519. #if TEST_TCPIP
  520. struct
  521. {
  522. int req;
  523. int actual;
  524. int req2;
  525. int actual2;
  526. } tcpipSent[512 * 1024];
  527. int cur;
  528. #endif
  529. static void zylinjtag_uart(cyg_addrword_t data)
  530. {
  531. int so_reuseaddr_option = 1;
  532. int fd;
  533. if ((fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
  534. {
  535. LOG_ERROR("error creating socket: %s", strerror(errno));
  536. exit(-1);
  537. }
  538. setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void*) &so_reuseaddr_option,
  539. sizeof(int));
  540. struct sockaddr_in sin;
  541. unsigned int address_size;
  542. address_size = sizeof(sin);
  543. memset(&sin, 0, sizeof(sin));
  544. sin.sin_family = AF_INET;
  545. sin.sin_addr.s_addr = INADDR_ANY;
  546. sin.sin_port = htons(5555);
  547. if (bind(fd, (struct sockaddr *) &sin, sizeof(sin)) == -1)
  548. {
  549. LOG_ERROR("couldn't bind to socket: %s", strerror(errno));
  550. exit(-1);
  551. }
  552. if (listen(fd, 1) == -1)
  553. {
  554. LOG_ERROR("couldn't listen on socket: %s", strerror(errno));
  555. exit(-1);
  556. }
  557. // socket_nonblock(fd);
  558. for (;;)
  559. {
  560. int session = accept(fd, (struct sockaddr *) &sin, &address_size);
  561. if (session < 0)
  562. {
  563. continue;
  564. }
  565. setNoDelay(session, 1);
  566. int oldopts = fcntl(session, F_GETFL, 0);
  567. fcntl(session, F_SETFL, oldopts | O_NONBLOCK); //
  568. int serHandle = open(ZY1000_SER_DEV, O_RDWR | O_NONBLOCK);
  569. if (serHandle < 0)
  570. {
  571. close(session);
  572. continue;
  573. }
  574. #ifdef CYGPKG_PROFILE_GPROF
  575. start_profile();
  576. #endif
  577. size_t actual = 0;
  578. size_t actual2 = 0;
  579. size_t pos, pos2;
  580. pos = 0;
  581. pos2 = 0;
  582. #if TEST_TCPIP
  583. cur = 0;
  584. #endif
  585. for (;;)
  586. {
  587. fd_set write_fds;
  588. fd_set read_fds;
  589. FD_ZERO(&write_fds);
  590. FD_ZERO(&read_fds);
  591. int fd_max = -1;
  592. FD_SET(session, &read_fds);
  593. fd_max = session;
  594. FD_SET(serHandle, &read_fds);
  595. if (serHandle > fd_max)
  596. {
  597. fd_max = serHandle;
  598. }
  599. /* Wait... */
  600. cyg_thread_delay(5); // 50ms fixed delay to wait for data to be sent/received
  601. if ((actual == 0) && (actual2 == 0))
  602. {
  603. int retval = select(fd_max + 1, &read_fds, NULL, NULL, NULL);
  604. if (retval <= 0)
  605. {
  606. break;
  607. }
  608. }
  609. if (actual2 <= 0)
  610. {
  611. memset(backwardBuffer, 's', sizeof(backwardBuffer));
  612. int t;
  613. t = read(serHandle, backwardBuffer,
  614. sizeof(backwardBuffer));
  615. actual2 = t;
  616. if (t < 0)
  617. {
  618. if (errno != EAGAIN)
  619. {
  620. goto closeSession;
  621. }
  622. actual2 = 0;
  623. }
  624. pos2 = 0;
  625. }
  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 y2 = 0;
  654. if (actual > 0)
  655. {
  656. /* Do not put things into the serial buffer if it has something to send
  657. * as that can cause a single byte to be sent at the time.
  658. *
  659. *
  660. */
  661. int written = write(serHandle, forwardBuffer + pos, actual);
  662. if (written < 0)
  663. {
  664. if (errno != EAGAIN)
  665. {
  666. goto closeSession;
  667. }
  668. // The serial buffer is full
  669. written = 0;
  670. }
  671. else
  672. {
  673. actual -= written;
  674. pos += written;
  675. }
  676. y2 = written;
  677. }
  678. #if TEST_TCPIP
  679. if (cur < 1024)
  680. {
  681. tcpipSent[cur].req = x;
  682. tcpipSent[cur].actual = y;
  683. tcpipSent[cur].req2 = x2;
  684. tcpipSent[cur].actual2 = y2;
  685. cur++;
  686. }
  687. #endif
  688. }
  689. closeSession: close(session);
  690. close(serHandle);
  691. #if TEST_TCPIP
  692. int i;
  693. for (i = 0; i < 1024; i++)
  694. {
  695. diag_printf("%d %d %d %d\n", tcpipSent[i].req, tcpipSent[i].actual,
  696. tcpipSent[i].req2, tcpipSent[i].actual2);
  697. }
  698. #endif
  699. }
  700. close(fd);
  701. }
  702. void startUart(void)
  703. {
  704. cyg_thread_create(1, zylinjtag_uart, (cyg_addrword_t) 0, "uart thread",
  705. (void *) uart_stack, sizeof(uart_stack),
  706. &zylinjtag_uart_thread_handle, &zylinjtag_uart_thread_object);
  707. cyg_thread_set_priority(zylinjtag_uart_thread_handle, 1); // low priority as it sits in a busy loop
  708. cyg_thread_resume(zylinjtag_uart_thread_handle);
  709. }
  710. static int zylinjtag_Jim_Command_uart(Jim_Interp *interp, int argc,
  711. Jim_Obj * const *argv)
  712. {
  713. static int current_baud = 38400;
  714. if (argc == 1)
  715. {
  716. command_print(cmd_ctx, "%d", current_baud);
  717. return JIM_OK;
  718. }
  719. else if (argc != 2)
  720. {
  721. return JIM_ERR;
  722. }
  723. long new_baudrate;
  724. if (Jim_GetLong(interp, argv[1], &new_baudrate) != JIM_OK)
  725. return JIM_ERR;
  726. current_baud = new_baudrate;
  727. int baud;
  728. switch (current_baud)
  729. {
  730. case 9600:
  731. baud = CYGNUM_SERIAL_BAUD_9600;
  732. break;
  733. case 19200:
  734. baud = CYGNUM_SERIAL_BAUD_19200;
  735. break;
  736. case 38400:
  737. baud = CYGNUM_SERIAL_BAUD_38400;
  738. break;
  739. case 57600:
  740. baud = CYGNUM_SERIAL_BAUD_57600;
  741. break;
  742. case 115200:
  743. baud = CYGNUM_SERIAL_BAUD_115200;
  744. break;
  745. case 230400:
  746. baud = CYGNUM_SERIAL_BAUD_230400;
  747. break;
  748. default:
  749. command_print(cmd_ctx, "unsupported baudrate");
  750. return ERROR_INVALID_ARGUMENTS;
  751. }
  752. cyg_serial_info_t buf;
  753. cyg_uint32 len = 1;
  754. //get existing serial configuration
  755. len = sizeof(cyg_serial_info_t);
  756. int err;
  757. cyg_io_handle_t serial_handle;
  758. err = cyg_io_lookup(ZY1000_SER_DEV, &serial_handle);
  759. if (err != ENOERR)
  760. {
  761. LOG_ERROR("Could not open serial port\n");
  762. return JIM_ERR;
  763. }
  764. err = cyg_io_get_config(serial_handle,
  765. CYG_IO_GET_CONFIG_SERIAL_OUTPUT_DRAIN, &buf, &len);
  766. err = cyg_io_get_config(serial_handle, CYG_IO_GET_CONFIG_SERIAL_INFO, &buf,
  767. &len);
  768. if (err != ENOERR)
  769. {
  770. LOG_ERROR("Failed to get serial port settings %d", err);
  771. return JIM_ERR;
  772. }
  773. buf.baud = baud;
  774. err = cyg_io_set_config(serial_handle, CYG_IO_SET_CONFIG_SERIAL_INFO, &buf,
  775. &len);
  776. if (err != ENOERR)
  777. {
  778. LOG_ERROR("Failed to set serial port settings %d", err);
  779. return JIM_ERR;
  780. }
  781. return JIM_OK;
  782. }
  783. bool logAllToSerial = false;
  784. int boolParam(char *var);
  785. static const char *zylin_config_dir="/config/settings";
  786. static int add_default_dirs(void)
  787. {
  788. add_script_search_dir(zylin_config_dir);
  789. add_script_search_dir("/rom/lib/openocd");
  790. add_script_search_dir("/rom");
  791. return ERROR_OK;
  792. }
  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. struct command_context * cmd_ctx;
  902. struct command_context *setup_command_handler(Jim_Interp *interp);
  903. cmd_ctx = setup_command_handler(httpstate.jim_interp);
  904. command_set_output_handler(cmd_ctx, configuration_output_handler, NULL);
  905. command_context_mode(cmd_ctx, COMMAND_CONFIG);
  906. if (ioutil_init(cmd_ctx) != ERROR_OK)
  907. return EXIT_FAILURE;
  908. #ifdef CYGPKG_PROFILE_GPROF
  909. COMMAND_REGISTER(cmd_ctx, NULL, "ecosboard_profile", eCosBoard_handle_eCosBoard_profile_command,
  910. COMMAND_ANY, NULL);
  911. #endif
  912. Jim_CreateCommand(httpstate.jim_interp, "uart", zylinjtag_Jim_Command_uart, NULL, NULL);
  913. log_init();
  914. set_log_output(cmd_ctx, log);
  915. LOG_DEBUG("log init complete");
  916. // diag_printf("Executing config files\n");
  917. if (logAllToSerial)
  918. {
  919. diag_printf(
  920. "%s/logserial = 1 => sending log output to serial port using \"debug_level 3\" as default.\n", zylin_config_dir);
  921. command_run_line(cmd_ctx, "debug_level 3");
  922. }
  923. command_run_linef(cmd_ctx, "script /rom/openocd.cfg");
  924. int ret;
  925. ret = server_init(cmd_ctx);
  926. if (ERROR_OK != ret)
  927. return EXIT_FAILURE;
  928. /* we MUST always run the init command as it will launch telnet sessions */
  929. command_run_line(cmd_ctx, "init");
  930. // FIX!!! Yuk!
  931. // diag_printf() is really invoked from many more places than we trust it
  932. // not to cause instabilities(e.g. invoking fputc() from an interrupt is *BAD*).
  933. //
  934. // Disabling it here is safe and gives us enough logged debug output for now. Crossing
  935. // fingers that it doesn't cause any crashes.
  936. diag_printf("Init complete, GDB & telnet servers launched.\n");
  937. command_set_output_handler(cmd_ctx,
  938. zy1000_configuration_output_handler_log, NULL);
  939. if (!logAllToSerial)
  940. {
  941. serialLog = false;
  942. }
  943. /* handle network connections */
  944. server_loop(cmd_ctx);
  945. openocd_sleep_prelude();
  946. /* shut server down */
  947. server_quit();
  948. /* free commandline interface */
  949. command_done(cmd_ctx);
  950. umount("/config");
  951. exit(0);
  952. for (;;)
  953. ;
  954. }
  955. cyg_int32 cyg_httpd_exec_cgi_tcl(char *file_name);
  956. cyg_int32 homeForm(CYG_HTTPD_STATE *p)
  957. {
  958. cyg_httpd_exec_cgi_tcl("/ram/cgi/index.tcl");
  959. return 0;
  960. }
  961. CYG_HTTPD_HANDLER_TABLE_ENTRY(root_label, "/", homeForm);
  962. CYG_HTTPD_MIME_TABLE_ENTRY(text_mime_label, "text", "text/plain");
  963. CYG_HTTPD_MIME_TABLE_ENTRY(bin_mime_label, "bin", "application/octet-stream");
  964. #include <pkgconf/system.h>
  965. #include <pkgconf/hal.h>
  966. #include <pkgconf/kernel.h>
  967. #include <pkgconf/io_fileio.h>
  968. #include <pkgconf/fs_rom.h>
  969. #include <cyg/kernel/ktypes.h> // base kernel types
  970. #include <cyg/infra/cyg_trac.h> // tracing macros
  971. #include <cyg/infra/cyg_ass.h> // assertion macros
  972. #include <cyg/fileio/fileio.h>
  973. #include <cyg/kernel/kapi.h>
  974. #include <cyg/infra/diag.h>
  975. //==========================================================================
  976. // Eventually we want to eXecute In Place from the ROM in a protected
  977. // environment, so we'll need executables to be aligned to a boundary
  978. // suitable for MMU protection. A suitable boundary would be the 4k
  979. // boundary in all the CPU architectures I am currently aware of.
  980. // Forward definitions
  981. // Filesystem operations
  982. static int tftpfs_mount(cyg_fstab_entry *fste, cyg_mtab_entry *mte);
  983. static int tftpfs_umount(cyg_mtab_entry *mte);
  984. static int tftpfs_open(cyg_mtab_entry *mte, cyg_dir dir, const char *name,
  985. int mode, cyg_file *fte);
  986. static int tftpfs_fo_read(struct CYG_FILE_TAG *fp, struct CYG_UIO_TAG *uio);
  987. static int tftpfs_fo_write(struct CYG_FILE_TAG *fp, struct CYG_UIO_TAG *uio);
  988. // File operations
  989. static int tftpfs_fo_fsync(struct CYG_FILE_TAG *fp, int mode);
  990. static int tftpfs_fo_close(struct CYG_FILE_TAG *fp);
  991. static int tftpfs_fo_lseek(struct CYG_FILE_TAG *fp, off_t *apos, int whence);
  992. //==========================================================================
  993. // Filesystem table entries
  994. // -------------------------------------------------------------------------
  995. // Fstab entry.
  996. // This defines the entry in the filesystem table.
  997. // For simplicity we use _FILESYSTEM synchronization for all accesses since
  998. // we should never block in any filesystem operations.
  999. #if 1
  1000. FSTAB_ENTRY(tftpfs_fste, "tftpfs", 0,
  1001. CYG_SYNCMODE_NONE,
  1002. tftpfs_mount,
  1003. tftpfs_umount,
  1004. tftpfs_open,
  1005. (cyg_fsop_unlink *)cyg_fileio_erofs,
  1006. (cyg_fsop_mkdir *)cyg_fileio_erofs,
  1007. (cyg_fsop_rmdir *)cyg_fileio_erofs,
  1008. (cyg_fsop_rename *)cyg_fileio_erofs,
  1009. (cyg_fsop_link *)cyg_fileio_erofs,
  1010. (cyg_fsop_opendir *)cyg_fileio_erofs,
  1011. (cyg_fsop_chdir *)cyg_fileio_erofs,
  1012. (cyg_fsop_stat *)cyg_fileio_erofs,
  1013. (cyg_fsop_getinfo *)cyg_fileio_erofs,
  1014. (cyg_fsop_setinfo *)cyg_fileio_erofs);
  1015. #endif
  1016. // -------------------------------------------------------------------------
  1017. // mtab entry.
  1018. // This defines a single ROMFS loaded into ROM at the configured address
  1019. //
  1020. // MTAB_ENTRY(rom_mte, // structure name
  1021. // "/rom", // mount point
  1022. // "romfs", // FIlesystem type
  1023. // "", // hardware device
  1024. // (CYG_ADDRWORD) CYGNUM_FS_ROM_BASE_ADDRESS // Address in ROM
  1025. //);
  1026. // -------------------------------------------------------------------------
  1027. // File operations.
  1028. // This set of file operations are used for normal open files.
  1029. static cyg_fileops tftpfs_fileops =
  1030. { tftpfs_fo_read, tftpfs_fo_write, tftpfs_fo_lseek,
  1031. (cyg_fileop_ioctl *) cyg_fileio_erofs, cyg_fileio_seltrue,
  1032. tftpfs_fo_fsync, tftpfs_fo_close,
  1033. (cyg_fileop_fstat *) cyg_fileio_erofs,
  1034. (cyg_fileop_getinfo *) cyg_fileio_erofs,
  1035. (cyg_fileop_setinfo *) cyg_fileio_erofs, };
  1036. // -------------------------------------------------------------------------
  1037. // tftpfs_mount()
  1038. // Process a mount request. This mainly finds root for the
  1039. // filesystem.
  1040. static int tftpfs_mount(cyg_fstab_entry *fste, cyg_mtab_entry *mte)
  1041. {
  1042. return ENOERR;
  1043. }
  1044. static int tftpfs_umount(cyg_mtab_entry *mte)
  1045. {
  1046. return ENOERR;
  1047. }
  1048. struct Tftp
  1049. {
  1050. int write;
  1051. int readFile;
  1052. cyg_uint8 *mem;
  1053. int actual;
  1054. char *server;
  1055. char *file;
  1056. };
  1057. static void freeTftp(struct Tftp *t)
  1058. {
  1059. if (t == NULL)
  1060. return;
  1061. if (t->mem)
  1062. free(t->mem);
  1063. if (t->server)
  1064. free(t->server);
  1065. if (t->file)
  1066. free(t->file);
  1067. free(t);
  1068. }
  1069. static const int tftpMaxSize = 8192 * 1024;
  1070. static int tftpfs_open(cyg_mtab_entry *mte, cyg_dir dir, const char *name,
  1071. int mode, cyg_file *file)
  1072. {
  1073. struct Tftp *tftp;
  1074. tftp = malloc(sizeof(struct Tftp));
  1075. if (tftp == NULL)
  1076. return EMFILE;
  1077. memset(tftp, 0, sizeof(struct Tftp));
  1078. file->f_flag |= mode & CYG_FILE_MODE_MASK;
  1079. file->f_type = CYG_FILE_TYPE_FILE;
  1080. file->f_ops = &tftpfs_fileops;
  1081. file->f_offset = 0;
  1082. file->f_data = 0;
  1083. file->f_xops = 0;
  1084. tftp->mem = malloc(tftpMaxSize);
  1085. if (tftp->mem == NULL)
  1086. {
  1087. freeTftp(tftp);
  1088. return EMFILE;
  1089. }
  1090. char *server = strchr(name, '/');
  1091. if (server == NULL)
  1092. {
  1093. freeTftp(tftp);
  1094. return EMFILE;
  1095. }
  1096. tftp->server = malloc(server - name + 1);
  1097. if (tftp->server == NULL)
  1098. {
  1099. freeTftp(tftp);
  1100. return EMFILE;
  1101. }
  1102. strncpy(tftp->server, name, server - name);
  1103. tftp->server[server - name] = 0;
  1104. tftp->file = strdup(server + 1);
  1105. if (tftp->file == NULL)
  1106. {
  1107. freeTftp(tftp);
  1108. return EMFILE;
  1109. }
  1110. file->f_data = (CYG_ADDRWORD) tftp;
  1111. return ENOERR;
  1112. }
  1113. static int fetchTftp(struct Tftp *tftp)
  1114. {
  1115. if (!tftp->readFile)
  1116. {
  1117. int err;
  1118. tftp->actual = tftp_client_get(tftp->file, tftp->server, 0, tftp->mem,
  1119. tftpMaxSize, TFTP_OCTET, &err);
  1120. if (tftp->actual < 0)
  1121. {
  1122. return EMFILE;
  1123. }
  1124. tftp->readFile = 1;
  1125. }
  1126. return ENOERR;
  1127. }
  1128. // -------------------------------------------------------------------------
  1129. // tftpfs_fo_write()
  1130. // Read data from file.
  1131. static int tftpfs_fo_read(struct CYG_FILE_TAG *fp, struct CYG_UIO_TAG *uio)
  1132. {
  1133. struct Tftp *tftp = (struct Tftp *) fp->f_data;
  1134. if (fetchTftp(tftp) != ENOERR)
  1135. return EMFILE;
  1136. int i;
  1137. off_t pos = fp->f_offset;
  1138. int resid = 0;
  1139. for (i = 0; i < uio->uio_iovcnt; i++)
  1140. {
  1141. cyg_iovec *iov = &uio->uio_iov[i];
  1142. char *buf = (char *) iov->iov_base;
  1143. off_t len = iov->iov_len;
  1144. if (len + pos > tftp->actual)
  1145. {
  1146. len = tftp->actual - pos;
  1147. }
  1148. resid += iov->iov_len - len;
  1149. memcpy(buf, tftp->mem + pos, len);
  1150. pos += len;
  1151. }
  1152. uio->uio_resid = resid;
  1153. fp->f_offset = pos;
  1154. return ENOERR;
  1155. }
  1156. static int tftpfs_fo_write(struct CYG_FILE_TAG *fp, struct CYG_UIO_TAG *uio)
  1157. {
  1158. struct Tftp *tftp = (struct Tftp *) fp->f_data;
  1159. int i;
  1160. off_t pos = fp->f_offset;
  1161. int resid = 0;
  1162. for (i = 0; i < uio->uio_iovcnt; i++)
  1163. {
  1164. cyg_iovec *iov = &uio->uio_iov[i];
  1165. char *buf = (char *) iov->iov_base;
  1166. off_t len = iov->iov_len;
  1167. if (len + pos > tftpMaxSize)
  1168. {
  1169. len = tftpMaxSize - pos;
  1170. }
  1171. resid += iov->iov_len - len;
  1172. memcpy(tftp->mem + pos, buf, len);
  1173. pos += len;
  1174. }
  1175. uio->uio_resid = resid;
  1176. fp->f_offset = pos;
  1177. tftp->write = 1;
  1178. return ENOERR;
  1179. }
  1180. static int tftpfs_fo_fsync(struct CYG_FILE_TAG *fp, int mode)
  1181. {
  1182. int error = ENOERR;
  1183. return error;
  1184. }
  1185. // -------------------------------------------------------------------------
  1186. // romfs_fo_close()
  1187. // Close a file. We just clear out the data pointer.
  1188. static int tftpfs_fo_close(struct CYG_FILE_TAG *fp)
  1189. {
  1190. struct Tftp *tftp = (struct Tftp *) fp->f_data;
  1191. int error = ENOERR;
  1192. if (tftp->write)
  1193. {
  1194. tftp_client_put(tftp->file, tftp->server, 0, tftp->mem, fp->f_offset,
  1195. TFTP_OCTET, &error);
  1196. }
  1197. freeTftp(tftp);
  1198. fp->f_data = 0;
  1199. return error;
  1200. }
  1201. // -------------------------------------------------------------------------
  1202. // romfs_fo_lseek()
  1203. // Seek to a new file position.
  1204. static int tftpfs_fo_lseek(struct CYG_FILE_TAG *fp, off_t *apos, int whence)
  1205. {
  1206. struct Tftp *tftp = (struct Tftp *) fp->f_data;
  1207. off_t pos = *apos;
  1208. if (fetchTftp(tftp) != ENOERR)
  1209. return EMFILE;
  1210. switch (whence)
  1211. {
  1212. case SEEK_SET:
  1213. // Pos is already where we want to be.
  1214. break;
  1215. case SEEK_CUR:
  1216. // Add pos to current offset.
  1217. pos += fp->f_offset;
  1218. break;
  1219. case SEEK_END:
  1220. // Add pos to file size.
  1221. pos += tftp->actual;
  1222. break;
  1223. default:
  1224. return EINVAL;
  1225. }
  1226. // Check that pos is still within current file size, or at the
  1227. // very end.
  1228. if (pos < 0 || pos > tftp->actual)
  1229. return EINVAL;
  1230. // All OK, set fp offset and return new position.
  1231. *apos = fp->f_offset = pos;
  1232. return ENOERR;
  1233. }
  1234. void usleep(int us)
  1235. {
  1236. if (us > 10000)
  1237. cyg_thread_delay(us / 10000 + 1);
  1238. else
  1239. HAL_DELAY_US(us);
  1240. }
  1241. // Chunked version.
  1242. cyg_int32 show_log_entry(CYG_HTTPD_STATE *phttpstate)
  1243. {
  1244. cyg_httpd_start_chunked("text");
  1245. if (logCount >= logSize)
  1246. {
  1247. cyg_httpd_write_chunked(logBuffer + logCount % logSize, logSize
  1248. - logCount % logSize);
  1249. }
  1250. cyg_httpd_write_chunked(logBuffer, writePtr);
  1251. cyg_httpd_end_chunked();
  1252. return -1;
  1253. }
  1254. CYG_HTTPD_HANDLER_TABLE_ENTRY(show_log, "/ram/log", show_log_entry);
  1255. // Filesystem operations
  1256. static int logfs_mount(cyg_fstab_entry *fste, cyg_mtab_entry *mte);
  1257. static int logfs_umount(cyg_mtab_entry *mte);
  1258. static int logfs_open(cyg_mtab_entry *mte, cyg_dir dir, const char *name,
  1259. int mode, cyg_file *fte);
  1260. static int logfs_fo_write(struct CYG_FILE_TAG *fp, struct CYG_UIO_TAG *uio);
  1261. // File operations
  1262. static int logfs_fo_fsync(struct CYG_FILE_TAG *fp, int mode);
  1263. static int logfs_fo_close(struct CYG_FILE_TAG *fp);
  1264. #include <cyg/io/devtab.h>
  1265. //==========================================================================
  1266. // Filesystem table entries
  1267. // -------------------------------------------------------------------------
  1268. // Fstab entry.
  1269. // This defines the entry in the filesystem table.
  1270. // For simplicity we use _FILESYSTEM synchronization for all accesses since
  1271. // we should never block in any filesystem operations.
  1272. FSTAB_ENTRY(logfs_fste, "logfs", 0,
  1273. CYG_SYNCMODE_FILE_FILESYSTEM | CYG_SYNCMODE_IO_FILESYSTEM,
  1274. logfs_mount,
  1275. logfs_umount,
  1276. logfs_open,
  1277. (cyg_fsop_unlink *)cyg_fileio_erofs,
  1278. (cyg_fsop_mkdir *)cyg_fileio_erofs,
  1279. (cyg_fsop_rmdir *)cyg_fileio_erofs,
  1280. (cyg_fsop_rename *)cyg_fileio_erofs,
  1281. (cyg_fsop_link *)cyg_fileio_erofs,
  1282. (cyg_fsop_opendir *)cyg_fileio_erofs,
  1283. (cyg_fsop_chdir *)cyg_fileio_erofs,
  1284. (cyg_fsop_stat *)cyg_fileio_erofs,
  1285. (cyg_fsop_getinfo *)cyg_fileio_erofs,
  1286. (cyg_fsop_setinfo *)cyg_fileio_erofs);
  1287. // -------------------------------------------------------------------------
  1288. // File operations.
  1289. // This set of file operations are used for normal open files.
  1290. static cyg_fileops logfs_fileops =
  1291. { (cyg_fileop_read *) cyg_fileio_erofs, (cyg_fileop_write *) logfs_fo_write,
  1292. (cyg_fileop_lseek *) cyg_fileio_erofs,
  1293. (cyg_fileop_ioctl *) cyg_fileio_erofs, cyg_fileio_seltrue,
  1294. logfs_fo_fsync, logfs_fo_close, (cyg_fileop_fstat *) cyg_fileio_erofs,
  1295. (cyg_fileop_getinfo *) cyg_fileio_erofs,
  1296. (cyg_fileop_setinfo *) cyg_fileio_erofs, };
  1297. // -------------------------------------------------------------------------
  1298. // logfs_mount()
  1299. // Process a mount request. This mainly finds root for the
  1300. // filesystem.
  1301. static int logfs_mount(cyg_fstab_entry *fste, cyg_mtab_entry *mte)
  1302. {
  1303. return ENOERR;
  1304. }
  1305. static int logfs_umount(cyg_mtab_entry *mte)
  1306. {
  1307. return ENOERR;
  1308. }
  1309. static int logfs_open(cyg_mtab_entry *mte, cyg_dir dir, const char *name,
  1310. int mode, cyg_file *file)
  1311. {
  1312. file->f_flag |= mode & CYG_FILE_MODE_MASK;
  1313. file->f_type = CYG_FILE_TYPE_FILE;
  1314. file->f_ops = &logfs_fileops;
  1315. file->f_offset = 0;
  1316. file->f_data = 0;
  1317. file->f_xops = 0;
  1318. return ENOERR;
  1319. }
  1320. // -------------------------------------------------------------------------
  1321. // logfs_fo_write()
  1322. // Write data to file.
  1323. static int logfs_fo_write(struct CYG_FILE_TAG *fp, struct CYG_UIO_TAG *uio)
  1324. {
  1325. int i;
  1326. for (i = 0; i < uio->uio_iovcnt; i++)
  1327. {
  1328. cyg_iovec *iov = &uio->uio_iov[i];
  1329. char *buf = (char *) iov->iov_base;
  1330. off_t len = iov->iov_len;
  1331. diag_write(buf, len);
  1332. }
  1333. uio->uio_resid = 0;
  1334. return ENOERR;
  1335. }
  1336. static int logfs_fo_fsync(struct CYG_FILE_TAG *fp, int mode)
  1337. {
  1338. return ENOERR;
  1339. }
  1340. // -------------------------------------------------------------------------
  1341. // romfs_fo_close()
  1342. // Close a file. We just clear out the data pointer.
  1343. static int logfs_fo_close(struct CYG_FILE_TAG *fp)
  1344. {
  1345. return ENOERR;
  1346. }
  1347. int loadFile(const char *fileName, void **data, int *len);
  1348. /* boolean parameter stored on config */
  1349. int boolParam(char *var)
  1350. {
  1351. bool result = false;
  1352. char *name = alloc_printf("%s/%s", zylin_config_dir, var);
  1353. if (name == NULL)
  1354. return result;
  1355. void *data;
  1356. int len;
  1357. if (loadFile(name, &data, &len) == ERROR_OK)
  1358. {
  1359. if (len > 1)
  1360. len = 1;
  1361. result = strncmp((char *) data, "1", len) == 0;
  1362. free(data);
  1363. }
  1364. free(name);
  1365. return result;
  1366. }