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.
 
 
 
 
 
 

1653 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/io/io.h>
  51. #include <cyg/io/serialio.h>
  52. #include <netinet/tcp.h>
  53. #include <cyg/hal/hal_diag.h>
  54. #include "rom.h"
  55. #ifdef CYGPKG_HAL_NIOS2
  56. #include <cyg/hal/io.h>
  57. #define ZY1000_SER_DEV "/dev/uart_0"
  58. #else
  59. #define ZY1000_SER_DEV "/dev/ser0"
  60. #endif
  61. #define MAX_IFS 64
  62. #if defined(CYGPKG_NET_FREEBSD_STACK)
  63. #include <tftp_support.h>
  64. /* posix compatibility broken*/
  65. struct tftpd_fileops fileops =
  66. {
  67. (int (*)(const char *, int))open,
  68. close,
  69. (int (*)(int, const void *, int))write,
  70. (int (*)(int, void *, int))read
  71. };
  72. #endif
  73. void diag_write(char *buf, int len)
  74. {
  75. int j;
  76. for (j = 0; j < len; j++)
  77. {
  78. diag_printf("%c", buf[j]);
  79. }
  80. }
  81. static bool serialLog = true;
  82. static bool writeLog = true;
  83. char hwaddr[512];
  84. extern struct flash_driver *flash_drivers[];
  85. extern struct target_type *target_types[];
  86. #ifdef CYGPKG_PROFILE_GPROF
  87. #include <cyg/profile/profile.h>
  88. extern char _stext, _etext; // Defined by the linker
  89. static char *start_of_code=&_stext;
  90. static char *end_of_code=&_etext;
  91. void start_profile(void)
  92. {
  93. // This starts up the system-wide profiling, gathering
  94. // profile information on all of the code, with a 16 byte
  95. // "bucket" size, at a rate of 100us/profile hit.
  96. // Note: a bucket size of 16 will give pretty good function
  97. // resolution. Much smaller and the buffer becomes
  98. // much too large for very little gain.
  99. // Note: a timer period of 100us is also a reasonable
  100. // compromise. Any smaller and the overhead of
  101. // handling the timter (profile) interrupt could
  102. // swamp the system. A fast processor might get
  103. // by with a smaller value, but a slow one could
  104. // even be swamped by this value. If the value is
  105. // too large, the usefulness of the profile is reduced.
  106. // no more interrupts than 1/10ms.
  107. //profile_on((void *)0, (void *)0x40000, 16, 10000); // SRAM
  108. // profile_on(0, &_etext, 16, 10000); // SRAM & DRAM
  109. profile_on(start_of_code, end_of_code, 16, 10000); // Nios DRAM
  110. }
  111. #endif
  112. static FILE *log;
  113. static char reboot_stack[2048];
  114. static void zylinjtag_reboot(cyg_addrword_t data)
  115. {
  116. serialLog = true;
  117. diag_printf("Rebooting in 500 ticks..\n");
  118. cyg_thread_delay(500);
  119. diag_printf("Unmounting /config..\n");
  120. umount("/config");
  121. diag_printf("Rebooting..\n");
  122. #ifdef CYGPKG_HAL_NIOS2
  123. /* This will reboot & reconfigure the FPGA from the bootloader
  124. * and on.
  125. */
  126. IOWR(REMOTE_UPDATE_BASE, 0x20, 0x1);
  127. #else
  128. HAL_PLATFORM_RESET();
  129. #endif
  130. }
  131. static cyg_thread zylinjtag_thread_object;
  132. static cyg_handle_t zylinjtag_thread_handle;
  133. void reboot(void)
  134. {
  135. cyg_thread_create(1, zylinjtag_reboot, (cyg_addrword_t) 0, "reboot Thread",
  136. (void *) reboot_stack, sizeof(reboot_stack),
  137. &zylinjtag_thread_handle, &zylinjtag_thread_object);
  138. cyg_thread_resume(zylinjtag_thread_handle);
  139. }
  140. static char zylinjtag_reboot_port_stack[2048];
  141. static cyg_thread zylinjtag_reboot_port_thread_object;
  142. static cyg_handle_t zylinjtag_reboot_port_thread_handle;
  143. static void zylinjtag_reboot_port_task(cyg_addrword_t data)
  144. {
  145. int so_reuseaddr_option = 1;
  146. int fd;
  147. if ((fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
  148. {
  149. LOG_ERROR("error creating socket: %s", strerror(errno));
  150. exit(-1);
  151. }
  152. setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void*) &so_reuseaddr_option,
  153. sizeof(int));
  154. struct sockaddr_in sin;
  155. unsigned int address_size;
  156. address_size = sizeof(sin);
  157. memset(&sin, 0, sizeof(sin));
  158. sin.sin_family = AF_INET;
  159. sin.sin_addr.s_addr = INADDR_ANY;
  160. sin.sin_port = htons(1234);
  161. if (bind(fd, (struct sockaddr *) &sin, sizeof(sin)) == -1)
  162. {
  163. LOG_ERROR("couldn't bind to socket: %s", strerror(errno));
  164. exit(-1);
  165. }
  166. if (listen(fd, 1) == -1)
  167. {
  168. LOG_ERROR("couldn't listen on socket: %s", strerror(errno));
  169. exit(-1);
  170. }
  171. // socket_nonblock(fd);
  172. accept(fd, (struct sockaddr *) &sin, &address_size);
  173. diag_printf("Got reboot signal on port 1234");
  174. reboot();
  175. }
  176. void reboot_port(void)
  177. {
  178. cyg_thread_create(1, zylinjtag_reboot_port_task, (cyg_addrword_t) 0, "wait for reboot signal on port 1234",
  179. (void *) zylinjtag_reboot_port_stack, sizeof(zylinjtag_reboot_port_stack),
  180. &zylinjtag_reboot_port_thread_handle, &zylinjtag_reboot_port_thread_object);
  181. cyg_thread_resume(zylinjtag_reboot_port_thread_handle);
  182. }
  183. int configuration_output_handler(struct command_context *context,
  184. const char* line)
  185. {
  186. diag_printf("%s", line);
  187. return ERROR_OK;
  188. }
  189. int zy1000_configuration_output_handler_log(struct command_context *context,
  190. const char* line)
  191. {
  192. LOG_USER_N("%s", line);
  193. return ERROR_OK;
  194. }
  195. #ifdef CYGPKG_PROFILE_GPROF
  196. //extern int64_t totaltime;
  197. static int zylinjtag_Jim_Command_profile(Jim_Interp *interp, int argc,
  198. Jim_Obj * const *argv)
  199. {
  200. if ((argc == 2) && (strcmp(Jim_GetString(argv[1], NULL), "stats")==0))
  201. {
  202. // profile_off();
  203. //LOG_USER("Stats %dms sleeping in select()", (int)totaltime);
  204. } else
  205. {
  206. LOG_USER("Profiling started");
  207. start_profile();
  208. //totaltime = 0;
  209. }
  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. #ifdef CYGPKG_HAL_NIOS2
  838. cyg_flashaddr_t err_address;
  839. #define UNCACHED_EXT_FLASH_BASE (0x80000000 + EXT_FLASH_BASE)
  840. /* The revc flash is locked upon reset, unlock it */
  841. #ifdef CYGHWR_IO_FLASH_BLOCK_LOCKING
  842. if ((err = flash_unlock((void *) UNCACHED_EXT_FLASH_BASE, EXT_FLASH_SPAN,
  843. (void **) &err_address)) != 0)
  844. {
  845. diag_printf("Error: could not unlock flash\n");
  846. }
  847. #endif
  848. #endif
  849. err = mount("/dev/flash1", "/config", "jffs2");
  850. if (err < 0)
  851. {
  852. diag_printf("unable to mount jffs2, falling back to ram disk..\n");
  853. err = mount("", "/config", "ramfs");
  854. if (err < 0)
  855. {
  856. diag_printf("unable to mount /config as ramdisk.\n");
  857. reboot();
  858. }
  859. }
  860. else
  861. {
  862. /* are we using a ram disk instead of a flash disk? This is used
  863. * for ZY1000 live demo...
  864. *
  865. * copy over flash disk to ram block device
  866. */
  867. if (boolParam("ramdisk"))
  868. {
  869. diag_printf("Unmounting /config from flash and using ram instead\n");
  870. err = umount("/config");
  871. if (err < 0)
  872. {
  873. diag_printf("unable to unmount jffs\n");
  874. reboot();
  875. }
  876. err = mount("/dev/flash1", "/config2", "jffs2");
  877. if (err < 0)
  878. {
  879. diag_printf("unable to mount jffs\n");
  880. reboot();
  881. }
  882. err = mount("", "/config", "ramfs");
  883. if (err < 0)
  884. {
  885. diag_printf("unable to mount ram block device\n");
  886. reboot();
  887. }
  888. // copydir("/config2", "/config");
  889. copyfile("/config2/ip", "/config/ip");
  890. copydir("/config2/settings", "/config/settings");
  891. umount("/config2");
  892. }
  893. }
  894. mkdir(zylin_config_dir, 0777);
  895. char *dirname = alloc_printf("%s/target", zylin_config_dir);
  896. mkdir(dirname, 0777);
  897. free(dirname);
  898. dirname = alloc_printf("%s/board", zylin_config_dir);
  899. mkdir(dirname, 0777);
  900. free(dirname);
  901. dirname = alloc_printf("%s/event", zylin_config_dir);
  902. mkdir(dirname, 0777);
  903. free(dirname);
  904. logAllToSerial = boolParam("logserial");
  905. // We need the network & web server in case there is something wrong with
  906. // the config files that invoke exit()
  907. zylinjtag_startNetwork();
  908. /* we're going to access the jim interpreter from here on... */
  909. openocd_sleep_postlude();
  910. startUart();
  911. add_default_dirs();
  912. /* initialize commandline interface */
  913. struct command_context * cmd_ctx;
  914. struct command_context *setup_command_handler(Jim_Interp *interp);
  915. cmd_ctx = setup_command_handler(httpstate.jim_interp);
  916. command_set_output_handler(cmd_ctx, configuration_output_handler, NULL);
  917. command_context_mode(cmd_ctx, COMMAND_CONFIG);
  918. if (ioutil_init(cmd_ctx) != ERROR_OK)
  919. return EXIT_FAILURE;
  920. #ifdef CYGPKG_PROFILE_GPROF
  921. Jim_CreateCommand(httpstate.jim_interp, "zy1000_profile", zylinjtag_Jim_Command_profile,
  922. NULL, NULL);
  923. #endif
  924. Jim_CreateCommand(httpstate.jim_interp, "zy1000_uart", zylinjtag_Jim_Command_uart, NULL, NULL);
  925. log_init();
  926. set_log_output(cmd_ctx, log);
  927. LOG_DEBUG("log init complete");
  928. // diag_printf("Executing config files\n");
  929. if (logAllToSerial)
  930. {
  931. diag_printf(
  932. "%s/logserial = 1 => sending log output to serial port using \"debug_level 3\" as default.\n", zylin_config_dir);
  933. command_run_line(cmd_ctx, "debug_level 3");
  934. }
  935. command_run_linef(cmd_ctx, "script /rom/openocd.cfg");
  936. int ret;
  937. ret = server_init(cmd_ctx);
  938. if (ERROR_OK != ret)
  939. return EXIT_FAILURE;
  940. /* we MUST always run the init command as it will launch telnet sessions */
  941. command_run_line(cmd_ctx, "init");
  942. // FIX!!! Yuk!
  943. // diag_printf() is really invoked from many more places than we trust it
  944. // not to cause instabilities(e.g. invoking fputc() from an interrupt is *BAD*).
  945. //
  946. // Disabling it here is safe and gives us enough logged debug output for now. Crossing
  947. // fingers that it doesn't cause any crashes.
  948. diag_printf("Init complete, GDB & telnet servers launched.\n");
  949. command_set_output_handler(cmd_ctx,
  950. zy1000_configuration_output_handler_log, NULL);
  951. if (!logAllToSerial)
  952. {
  953. serialLog = false;
  954. }
  955. /* handle network connections */
  956. server_loop(cmd_ctx);
  957. openocd_sleep_prelude();
  958. /* shut server down */
  959. server_quit();
  960. /* free commandline interface */
  961. command_done(cmd_ctx);
  962. umount("/config");
  963. exit(0);
  964. for (;;)
  965. ;
  966. }
  967. cyg_int32 cyg_httpd_exec_cgi_tcl(char *file_name);
  968. cyg_int32 homeForm(CYG_HTTPD_STATE *p)
  969. {
  970. cyg_httpd_exec_cgi_tcl("/ram/cgi/index.tcl");
  971. return 0;
  972. }
  973. CYG_HTTPD_HANDLER_TABLE_ENTRY(root_label, "/", homeForm);
  974. CYG_HTTPD_MIME_TABLE_ENTRY(text_mime_label, "text", "text/plain");
  975. CYG_HTTPD_MIME_TABLE_ENTRY(bin_mime_label, "bin", "application/octet-stream");
  976. #include <pkgconf/system.h>
  977. #include <pkgconf/hal.h>
  978. #include <pkgconf/kernel.h>
  979. #include <pkgconf/io_fileio.h>
  980. #include <pkgconf/fs_rom.h>
  981. #include <cyg/kernel/ktypes.h> // base kernel types
  982. #include <cyg/infra/cyg_trac.h> // tracing macros
  983. #include <cyg/infra/cyg_ass.h> // assertion macros
  984. #include <cyg/fileio/fileio.h>
  985. #include <cyg/kernel/kapi.h>
  986. #include <cyg/infra/diag.h>
  987. //==========================================================================
  988. // Eventually we want to eXecute In Place from the ROM in a protected
  989. // environment, so we'll need executables to be aligned to a boundary
  990. // suitable for MMU protection. A suitable boundary would be the 4k
  991. // boundary in all the CPU architectures I am currently aware of.
  992. // Forward definitions
  993. // Filesystem operations
  994. static int tftpfs_mount(cyg_fstab_entry *fste, cyg_mtab_entry *mte);
  995. static int tftpfs_umount(cyg_mtab_entry *mte);
  996. static int tftpfs_open(cyg_mtab_entry *mte, cyg_dir dir, const char *name,
  997. int mode, cyg_file *fte);
  998. static int tftpfs_fo_read(struct CYG_FILE_TAG *fp, struct CYG_UIO_TAG *uio);
  999. static int tftpfs_fo_write(struct CYG_FILE_TAG *fp, struct CYG_UIO_TAG *uio);
  1000. // File operations
  1001. static int tftpfs_fo_fsync(struct CYG_FILE_TAG *fp, int mode);
  1002. static int tftpfs_fo_close(struct CYG_FILE_TAG *fp);
  1003. static int tftpfs_fo_lseek(struct CYG_FILE_TAG *fp, off_t *apos, int whence);
  1004. //==========================================================================
  1005. // Filesystem table entries
  1006. // -------------------------------------------------------------------------
  1007. // Fstab entry.
  1008. // This defines the entry in the filesystem table.
  1009. // For simplicity we use _FILESYSTEM synchronization for all accesses since
  1010. // we should never block in any filesystem operations.
  1011. #if 1
  1012. FSTAB_ENTRY(tftpfs_fste, "tftpfs", 0,
  1013. CYG_SYNCMODE_NONE,
  1014. tftpfs_mount,
  1015. tftpfs_umount,
  1016. tftpfs_open,
  1017. (cyg_fsop_unlink *)cyg_fileio_erofs,
  1018. (cyg_fsop_mkdir *)cyg_fileio_erofs,
  1019. (cyg_fsop_rmdir *)cyg_fileio_erofs,
  1020. (cyg_fsop_rename *)cyg_fileio_erofs,
  1021. (cyg_fsop_link *)cyg_fileio_erofs,
  1022. (cyg_fsop_opendir *)cyg_fileio_erofs,
  1023. (cyg_fsop_chdir *)cyg_fileio_erofs,
  1024. (cyg_fsop_stat *)cyg_fileio_erofs,
  1025. (cyg_fsop_getinfo *)cyg_fileio_erofs,
  1026. (cyg_fsop_setinfo *)cyg_fileio_erofs);
  1027. #endif
  1028. // -------------------------------------------------------------------------
  1029. // mtab entry.
  1030. // This defines a single ROMFS loaded into ROM at the configured address
  1031. //
  1032. // MTAB_ENTRY(rom_mte, // structure name
  1033. // "/rom", // mount point
  1034. // "romfs", // FIlesystem type
  1035. // "", // hardware device
  1036. // (CYG_ADDRWORD) CYGNUM_FS_ROM_BASE_ADDRESS // Address in ROM
  1037. //);
  1038. // -------------------------------------------------------------------------
  1039. // File operations.
  1040. // This set of file operations are used for normal open files.
  1041. static cyg_fileops tftpfs_fileops =
  1042. { tftpfs_fo_read, tftpfs_fo_write, tftpfs_fo_lseek,
  1043. (cyg_fileop_ioctl *) cyg_fileio_erofs, cyg_fileio_seltrue,
  1044. tftpfs_fo_fsync, tftpfs_fo_close,
  1045. (cyg_fileop_fstat *) cyg_fileio_erofs,
  1046. (cyg_fileop_getinfo *) cyg_fileio_erofs,
  1047. (cyg_fileop_setinfo *) cyg_fileio_erofs, };
  1048. // -------------------------------------------------------------------------
  1049. // tftpfs_mount()
  1050. // Process a mount request. This mainly finds root for the
  1051. // filesystem.
  1052. static int tftpfs_mount(cyg_fstab_entry *fste, cyg_mtab_entry *mte)
  1053. {
  1054. return ENOERR;
  1055. }
  1056. static int tftpfs_umount(cyg_mtab_entry *mte)
  1057. {
  1058. return ENOERR;
  1059. }
  1060. struct Tftp
  1061. {
  1062. int write;
  1063. int readFile;
  1064. cyg_uint8 *mem;
  1065. int actual;
  1066. char *server;
  1067. char *file;
  1068. };
  1069. static void freeTftp(struct Tftp *t)
  1070. {
  1071. if (t == NULL)
  1072. return;
  1073. if (t->mem)
  1074. free(t->mem);
  1075. if (t->server)
  1076. free(t->server);
  1077. if (t->file)
  1078. free(t->file);
  1079. free(t);
  1080. }
  1081. static const int tftpMaxSize = 8192 * 1024;
  1082. static int tftpfs_open(cyg_mtab_entry *mte, cyg_dir dir, const char *name,
  1083. int mode, cyg_file *file)
  1084. {
  1085. struct Tftp *tftp;
  1086. tftp = malloc(sizeof(struct Tftp));
  1087. if (tftp == NULL)
  1088. return EMFILE;
  1089. memset(tftp, 0, sizeof(struct Tftp));
  1090. file->f_flag |= mode & CYG_FILE_MODE_MASK;
  1091. file->f_type = CYG_FILE_TYPE_FILE;
  1092. file->f_ops = &tftpfs_fileops;
  1093. file->f_offset = 0;
  1094. file->f_data = 0;
  1095. file->f_xops = 0;
  1096. tftp->mem = malloc(tftpMaxSize);
  1097. if (tftp->mem == NULL)
  1098. {
  1099. freeTftp(tftp);
  1100. return EMFILE;
  1101. }
  1102. char *server = strchr(name, '/');
  1103. if (server == NULL)
  1104. {
  1105. freeTftp(tftp);
  1106. return EMFILE;
  1107. }
  1108. tftp->server = malloc(server - name + 1);
  1109. if (tftp->server == NULL)
  1110. {
  1111. freeTftp(tftp);
  1112. return EMFILE;
  1113. }
  1114. strncpy(tftp->server, name, server - name);
  1115. tftp->server[server - name] = 0;
  1116. tftp->file = strdup(server + 1);
  1117. if (tftp->file == NULL)
  1118. {
  1119. freeTftp(tftp);
  1120. return EMFILE;
  1121. }
  1122. file->f_data = (CYG_ADDRWORD) tftp;
  1123. return ENOERR;
  1124. }
  1125. static int fetchTftp(struct Tftp *tftp)
  1126. {
  1127. if (!tftp->readFile)
  1128. {
  1129. int err;
  1130. tftp->actual = tftp_client_get(tftp->file, tftp->server, 0, tftp->mem,
  1131. tftpMaxSize, TFTP_OCTET, &err);
  1132. if (tftp->actual < 0)
  1133. {
  1134. return EMFILE;
  1135. }
  1136. tftp->readFile = 1;
  1137. }
  1138. return ENOERR;
  1139. }
  1140. // -------------------------------------------------------------------------
  1141. // tftpfs_fo_write()
  1142. // Read data from file.
  1143. static int tftpfs_fo_read(struct CYG_FILE_TAG *fp, struct CYG_UIO_TAG *uio)
  1144. {
  1145. struct Tftp *tftp = (struct Tftp *) fp->f_data;
  1146. if (fetchTftp(tftp) != ENOERR)
  1147. return EMFILE;
  1148. int i;
  1149. off_t pos = fp->f_offset;
  1150. int resid = 0;
  1151. for (i = 0; i < uio->uio_iovcnt; i++)
  1152. {
  1153. cyg_iovec *iov = &uio->uio_iov[i];
  1154. char *buf = (char *) iov->iov_base;
  1155. off_t len = iov->iov_len;
  1156. if (len + pos > tftp->actual)
  1157. {
  1158. len = tftp->actual - pos;
  1159. }
  1160. resid += iov->iov_len - len;
  1161. memcpy(buf, tftp->mem + pos, len);
  1162. pos += len;
  1163. }
  1164. uio->uio_resid = resid;
  1165. fp->f_offset = pos;
  1166. return ENOERR;
  1167. }
  1168. static int tftpfs_fo_write(struct CYG_FILE_TAG *fp, struct CYG_UIO_TAG *uio)
  1169. {
  1170. struct Tftp *tftp = (struct Tftp *) fp->f_data;
  1171. int i;
  1172. off_t pos = fp->f_offset;
  1173. int resid = 0;
  1174. for (i = 0; i < uio->uio_iovcnt; i++)
  1175. {
  1176. cyg_iovec *iov = &uio->uio_iov[i];
  1177. char *buf = (char *) iov->iov_base;
  1178. off_t len = iov->iov_len;
  1179. if (len + pos > tftpMaxSize)
  1180. {
  1181. len = tftpMaxSize - pos;
  1182. }
  1183. resid += iov->iov_len - len;
  1184. memcpy(tftp->mem + pos, buf, len);
  1185. pos += len;
  1186. }
  1187. uio->uio_resid = resid;
  1188. fp->f_offset = pos;
  1189. tftp->write = 1;
  1190. return ENOERR;
  1191. }
  1192. static int tftpfs_fo_fsync(struct CYG_FILE_TAG *fp, int mode)
  1193. {
  1194. int error = ENOERR;
  1195. return error;
  1196. }
  1197. // -------------------------------------------------------------------------
  1198. // romfs_fo_close()
  1199. // Close a file. We just clear out the data pointer.
  1200. static int tftpfs_fo_close(struct CYG_FILE_TAG *fp)
  1201. {
  1202. struct Tftp *tftp = (struct Tftp *) fp->f_data;
  1203. int error = ENOERR;
  1204. if (tftp->write)
  1205. {
  1206. tftp_client_put(tftp->file, tftp->server, 0, tftp->mem, fp->f_offset,
  1207. TFTP_OCTET, &error);
  1208. }
  1209. freeTftp(tftp);
  1210. fp->f_data = 0;
  1211. return error;
  1212. }
  1213. // -------------------------------------------------------------------------
  1214. // romfs_fo_lseek()
  1215. // Seek to a new file position.
  1216. static int tftpfs_fo_lseek(struct CYG_FILE_TAG *fp, off_t *apos, int whence)
  1217. {
  1218. struct Tftp *tftp = (struct Tftp *) fp->f_data;
  1219. off_t pos = *apos;
  1220. if (fetchTftp(tftp) != ENOERR)
  1221. return EMFILE;
  1222. switch (whence)
  1223. {
  1224. case SEEK_SET:
  1225. // Pos is already where we want to be.
  1226. break;
  1227. case SEEK_CUR:
  1228. // Add pos to current offset.
  1229. pos += fp->f_offset;
  1230. break;
  1231. case SEEK_END:
  1232. // Add pos to file size.
  1233. pos += tftp->actual;
  1234. break;
  1235. default:
  1236. return EINVAL;
  1237. }
  1238. // Check that pos is still within current file size, or at the
  1239. // very end.
  1240. if (pos < 0 || pos > tftp->actual)
  1241. return EINVAL;
  1242. // All OK, set fp offset and return new position.
  1243. *apos = fp->f_offset = pos;
  1244. return ENOERR;
  1245. }
  1246. void usleep(int us)
  1247. {
  1248. if (us > 10000)
  1249. cyg_thread_delay(us / 10000 + 1);
  1250. else
  1251. HAL_DELAY_US(us);
  1252. }
  1253. // Chunked version.
  1254. cyg_int32 show_log_entry(CYG_HTTPD_STATE *phttpstate)
  1255. {
  1256. cyg_httpd_start_chunked("text");
  1257. if (logCount >= logSize)
  1258. {
  1259. cyg_httpd_write_chunked(logBuffer + logCount % logSize, logSize
  1260. - logCount % logSize);
  1261. }
  1262. cyg_httpd_write_chunked(logBuffer, writePtr);
  1263. cyg_httpd_end_chunked();
  1264. return -1;
  1265. }
  1266. CYG_HTTPD_HANDLER_TABLE_ENTRY(show_log, "/ram/log", show_log_entry);
  1267. // Filesystem operations
  1268. static int logfs_mount(cyg_fstab_entry *fste, cyg_mtab_entry *mte);
  1269. static int logfs_umount(cyg_mtab_entry *mte);
  1270. static int logfs_open(cyg_mtab_entry *mte, cyg_dir dir, const char *name,
  1271. int mode, cyg_file *fte);
  1272. static int logfs_fo_write(struct CYG_FILE_TAG *fp, struct CYG_UIO_TAG *uio);
  1273. // File operations
  1274. static int logfs_fo_fsync(struct CYG_FILE_TAG *fp, int mode);
  1275. static int logfs_fo_close(struct CYG_FILE_TAG *fp);
  1276. #include <cyg/io/devtab.h>
  1277. //==========================================================================
  1278. // Filesystem table entries
  1279. // -------------------------------------------------------------------------
  1280. // Fstab entry.
  1281. // This defines the entry in the filesystem table.
  1282. // For simplicity we use _FILESYSTEM synchronization for all accesses since
  1283. // we should never block in any filesystem operations.
  1284. FSTAB_ENTRY(logfs_fste, "logfs", 0,
  1285. CYG_SYNCMODE_FILE_FILESYSTEM | CYG_SYNCMODE_IO_FILESYSTEM,
  1286. logfs_mount,
  1287. logfs_umount,
  1288. logfs_open,
  1289. (cyg_fsop_unlink *)cyg_fileio_erofs,
  1290. (cyg_fsop_mkdir *)cyg_fileio_erofs,
  1291. (cyg_fsop_rmdir *)cyg_fileio_erofs,
  1292. (cyg_fsop_rename *)cyg_fileio_erofs,
  1293. (cyg_fsop_link *)cyg_fileio_erofs,
  1294. (cyg_fsop_opendir *)cyg_fileio_erofs,
  1295. (cyg_fsop_chdir *)cyg_fileio_erofs,
  1296. (cyg_fsop_stat *)cyg_fileio_erofs,
  1297. (cyg_fsop_getinfo *)cyg_fileio_erofs,
  1298. (cyg_fsop_setinfo *)cyg_fileio_erofs);
  1299. // -------------------------------------------------------------------------
  1300. // File operations.
  1301. // This set of file operations are used for normal open files.
  1302. static cyg_fileops logfs_fileops =
  1303. { (cyg_fileop_read *) cyg_fileio_erofs, (cyg_fileop_write *) logfs_fo_write,
  1304. (cyg_fileop_lseek *) cyg_fileio_erofs,
  1305. (cyg_fileop_ioctl *) cyg_fileio_erofs, cyg_fileio_seltrue,
  1306. logfs_fo_fsync, logfs_fo_close, (cyg_fileop_fstat *) cyg_fileio_erofs,
  1307. (cyg_fileop_getinfo *) cyg_fileio_erofs,
  1308. (cyg_fileop_setinfo *) cyg_fileio_erofs, };
  1309. // -------------------------------------------------------------------------
  1310. // logfs_mount()
  1311. // Process a mount request. This mainly finds root for the
  1312. // filesystem.
  1313. static int logfs_mount(cyg_fstab_entry *fste, cyg_mtab_entry *mte)
  1314. {
  1315. return ENOERR;
  1316. }
  1317. static int logfs_umount(cyg_mtab_entry *mte)
  1318. {
  1319. return ENOERR;
  1320. }
  1321. static int logfs_open(cyg_mtab_entry *mte, cyg_dir dir, const char *name,
  1322. int mode, cyg_file *file)
  1323. {
  1324. file->f_flag |= mode & CYG_FILE_MODE_MASK;
  1325. file->f_type = CYG_FILE_TYPE_FILE;
  1326. file->f_ops = &logfs_fileops;
  1327. file->f_offset = 0;
  1328. file->f_data = 0;
  1329. file->f_xops = 0;
  1330. return ENOERR;
  1331. }
  1332. // -------------------------------------------------------------------------
  1333. // logfs_fo_write()
  1334. // Write data to file.
  1335. static int logfs_fo_write(struct CYG_FILE_TAG *fp, struct CYG_UIO_TAG *uio)
  1336. {
  1337. int i;
  1338. for (i = 0; i < uio->uio_iovcnt; i++)
  1339. {
  1340. cyg_iovec *iov = &uio->uio_iov[i];
  1341. char *buf = (char *) iov->iov_base;
  1342. off_t len = iov->iov_len;
  1343. diag_write(buf, len);
  1344. }
  1345. uio->uio_resid = 0;
  1346. return ENOERR;
  1347. }
  1348. static int logfs_fo_fsync(struct CYG_FILE_TAG *fp, int mode)
  1349. {
  1350. return ENOERR;
  1351. }
  1352. // -------------------------------------------------------------------------
  1353. // romfs_fo_close()
  1354. // Close a file. We just clear out the data pointer.
  1355. static int logfs_fo_close(struct CYG_FILE_TAG *fp)
  1356. {
  1357. return ENOERR;
  1358. }
  1359. int loadFile(const char *fileName, void **data, int *len);
  1360. /* boolean parameter stored on config */
  1361. int boolParam(char *var)
  1362. {
  1363. bool result = false;
  1364. char *name = alloc_printf("%s/%s", zylin_config_dir, var);
  1365. if (name == NULL)
  1366. return result;
  1367. void *data;
  1368. int len;
  1369. if (loadFile(name, &data, &len) == ERROR_OK)
  1370. {
  1371. if (len > 1)
  1372. len = 1;
  1373. result = strncmp((char *) data, "1", len) == 0;
  1374. free(data);
  1375. }
  1376. free(name);
  1377. return result;
  1378. }