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.
 
 
 
 
 
 

1657 lines
39 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. #ifdef CYGPKG_HAL_NIOS2
  807. diag_printf("Zylin ZY1000 PCB revc.\n");
  808. #else
  809. diag_printf("Zylin ZY1000 PCB revb.\n");
  810. #endif
  811. err = mount("", "/ram", "ramfs");
  812. if (err < 0)
  813. {
  814. diag_printf("unable to mount ramfs\n");
  815. }
  816. chdir("/ram");
  817. char address[16];
  818. sprintf(address, "%p", &filedata[0]);
  819. err = mount(address, "/rom", "romfs");
  820. if (err < 0)
  821. {
  822. diag_printf("unable to mount /rom\n");
  823. }
  824. err = mount("", "/log", "logfs");
  825. if (err < 0)
  826. {
  827. diag_printf("unable to mount logfs\n");
  828. }
  829. err = mount("", "/tftp", "tftpfs");
  830. if (err < 0)
  831. {
  832. diag_printf("unable to mount logfs\n");
  833. }
  834. log = fopen("/log/log", "w");
  835. if (log == NULL)
  836. {
  837. diag_printf("Could not open log file /ram/log\n");
  838. exit(-1);
  839. }
  840. copydir("/rom", "/ram/cgi");
  841. #ifdef CYGPKG_HAL_NIOS2
  842. cyg_flashaddr_t err_address;
  843. #define UNCACHED_EXT_FLASH_BASE (0x80000000 + EXT_FLASH_BASE)
  844. /* The revc flash is locked upon reset, unlock it */
  845. #ifdef CYGHWR_IO_FLASH_BLOCK_LOCKING
  846. if ((err = flash_unlock((void *) UNCACHED_EXT_FLASH_BASE, EXT_FLASH_SPAN,
  847. (void **) &err_address)) != 0)
  848. {
  849. diag_printf("Error: could not unlock flash\n");
  850. }
  851. #endif
  852. #endif
  853. err = mount("/dev/flash1", "/config", "jffs2");
  854. if (err < 0)
  855. {
  856. diag_printf("unable to mount jffs2, falling back to ram disk..\n");
  857. err = mount("", "/config", "ramfs");
  858. if (err < 0)
  859. {
  860. diag_printf("unable to mount /config as ramdisk.\n");
  861. reboot();
  862. }
  863. }
  864. else
  865. {
  866. /* are we using a ram disk instead of a flash disk? This is used
  867. * for ZY1000 live demo...
  868. *
  869. * copy over flash disk to ram block device
  870. */
  871. if (boolParam("ramdisk"))
  872. {
  873. diag_printf("Unmounting /config from flash and using ram instead\n");
  874. err = umount("/config");
  875. if (err < 0)
  876. {
  877. diag_printf("unable to unmount jffs\n");
  878. reboot();
  879. }
  880. err = mount("/dev/flash1", "/config2", "jffs2");
  881. if (err < 0)
  882. {
  883. diag_printf("unable to mount jffs\n");
  884. reboot();
  885. }
  886. err = mount("", "/config", "ramfs");
  887. if (err < 0)
  888. {
  889. diag_printf("unable to mount ram block device\n");
  890. reboot();
  891. }
  892. // copydir("/config2", "/config");
  893. copyfile("/config2/ip", "/config/ip");
  894. copydir("/config2/settings", "/config/settings");
  895. umount("/config2");
  896. }
  897. }
  898. mkdir(zylin_config_dir, 0777);
  899. char *dirname = alloc_printf("%s/target", zylin_config_dir);
  900. mkdir(dirname, 0777);
  901. free(dirname);
  902. dirname = alloc_printf("%s/board", zylin_config_dir);
  903. mkdir(dirname, 0777);
  904. free(dirname);
  905. dirname = alloc_printf("%s/event", zylin_config_dir);
  906. mkdir(dirname, 0777);
  907. free(dirname);
  908. logAllToSerial = boolParam("logserial");
  909. // We need the network & web server in case there is something wrong with
  910. // the config files that invoke exit()
  911. zylinjtag_startNetwork();
  912. /* we're going to access the jim interpreter from here on... */
  913. openocd_sleep_postlude();
  914. startUart();
  915. add_default_dirs();
  916. /* initialize commandline interface */
  917. struct command_context * cmd_ctx;
  918. struct command_context *setup_command_handler(Jim_Interp *interp);
  919. cmd_ctx = setup_command_handler(httpstate.jim_interp);
  920. command_set_output_handler(cmd_ctx, configuration_output_handler, NULL);
  921. command_context_mode(cmd_ctx, COMMAND_CONFIG);
  922. if (ioutil_init(cmd_ctx) != ERROR_OK)
  923. return EXIT_FAILURE;
  924. #ifdef CYGPKG_PROFILE_GPROF
  925. Jim_CreateCommand(httpstate.jim_interp, "zy1000_profile", zylinjtag_Jim_Command_profile,
  926. NULL, NULL);
  927. #endif
  928. Jim_CreateCommand(httpstate.jim_interp, "zy1000_uart", zylinjtag_Jim_Command_uart, NULL, NULL);
  929. log_init();
  930. set_log_output(cmd_ctx, log);
  931. LOG_DEBUG("log init complete");
  932. // diag_printf("Executing config files\n");
  933. if (logAllToSerial)
  934. {
  935. diag_printf(
  936. "%s/logserial = 1 => sending log output to serial port using \"debug_level 3\" as default.\n", zylin_config_dir);
  937. command_run_line(cmd_ctx, "debug_level 3");
  938. }
  939. command_run_linef(cmd_ctx, "script /rom/openocd.cfg");
  940. int ret;
  941. ret = server_init(cmd_ctx);
  942. if (ERROR_OK != ret)
  943. return EXIT_FAILURE;
  944. /* we MUST always run the init command as it will launch telnet sessions */
  945. command_run_line(cmd_ctx, "init");
  946. // FIX!!! Yuk!
  947. // diag_printf() is really invoked from many more places than we trust it
  948. // not to cause instabilities(e.g. invoking fputc() from an interrupt is *BAD*).
  949. //
  950. // Disabling it here is safe and gives us enough logged debug output for now. Crossing
  951. // fingers that it doesn't cause any crashes.
  952. diag_printf("Init complete, GDB & telnet servers launched.\n");
  953. command_set_output_handler(cmd_ctx,
  954. zy1000_configuration_output_handler_log, NULL);
  955. if (!logAllToSerial)
  956. {
  957. serialLog = false;
  958. }
  959. /* handle network connections */
  960. server_loop(cmd_ctx);
  961. openocd_sleep_prelude();
  962. /* shut server down */
  963. server_quit();
  964. /* free commandline interface */
  965. command_done(cmd_ctx);
  966. umount("/config");
  967. exit(0);
  968. for (;;)
  969. ;
  970. }
  971. cyg_int32 cyg_httpd_exec_cgi_tcl(char *file_name);
  972. cyg_int32 homeForm(CYG_HTTPD_STATE *p)
  973. {
  974. cyg_httpd_exec_cgi_tcl("/ram/cgi/index.tcl");
  975. return 0;
  976. }
  977. CYG_HTTPD_HANDLER_TABLE_ENTRY(root_label, "/", homeForm);
  978. CYG_HTTPD_MIME_TABLE_ENTRY(text_mime_label, "text", "text/plain");
  979. CYG_HTTPD_MIME_TABLE_ENTRY(bin_mime_label, "bin", "application/octet-stream");
  980. #include <pkgconf/system.h>
  981. #include <pkgconf/hal.h>
  982. #include <pkgconf/kernel.h>
  983. #include <pkgconf/io_fileio.h>
  984. #include <pkgconf/fs_rom.h>
  985. #include <cyg/kernel/ktypes.h> // base kernel types
  986. #include <cyg/infra/cyg_trac.h> // tracing macros
  987. #include <cyg/infra/cyg_ass.h> // assertion macros
  988. #include <cyg/fileio/fileio.h>
  989. #include <cyg/kernel/kapi.h>
  990. #include <cyg/infra/diag.h>
  991. //==========================================================================
  992. // Eventually we want to eXecute In Place from the ROM in a protected
  993. // environment, so we'll need executables to be aligned to a boundary
  994. // suitable for MMU protection. A suitable boundary would be the 4k
  995. // boundary in all the CPU architectures I am currently aware of.
  996. // Forward definitions
  997. // Filesystem operations
  998. static int tftpfs_mount(cyg_fstab_entry *fste, cyg_mtab_entry *mte);
  999. static int tftpfs_umount(cyg_mtab_entry *mte);
  1000. static int tftpfs_open(cyg_mtab_entry *mte, cyg_dir dir, const char *name,
  1001. int mode, cyg_file *fte);
  1002. static int tftpfs_fo_read(struct CYG_FILE_TAG *fp, struct CYG_UIO_TAG *uio);
  1003. static int tftpfs_fo_write(struct CYG_FILE_TAG *fp, struct CYG_UIO_TAG *uio);
  1004. // File operations
  1005. static int tftpfs_fo_fsync(struct CYG_FILE_TAG *fp, int mode);
  1006. static int tftpfs_fo_close(struct CYG_FILE_TAG *fp);
  1007. static int tftpfs_fo_lseek(struct CYG_FILE_TAG *fp, off_t *apos, int whence);
  1008. //==========================================================================
  1009. // Filesystem table entries
  1010. // -------------------------------------------------------------------------
  1011. // Fstab entry.
  1012. // This defines the entry in the filesystem table.
  1013. // For simplicity we use _FILESYSTEM synchronization for all accesses since
  1014. // we should never block in any filesystem operations.
  1015. #if 1
  1016. FSTAB_ENTRY(tftpfs_fste, "tftpfs", 0,
  1017. CYG_SYNCMODE_NONE,
  1018. tftpfs_mount,
  1019. tftpfs_umount,
  1020. tftpfs_open,
  1021. (cyg_fsop_unlink *)cyg_fileio_erofs,
  1022. (cyg_fsop_mkdir *)cyg_fileio_erofs,
  1023. (cyg_fsop_rmdir *)cyg_fileio_erofs,
  1024. (cyg_fsop_rename *)cyg_fileio_erofs,
  1025. (cyg_fsop_link *)cyg_fileio_erofs,
  1026. (cyg_fsop_opendir *)cyg_fileio_erofs,
  1027. (cyg_fsop_chdir *)cyg_fileio_erofs,
  1028. (cyg_fsop_stat *)cyg_fileio_erofs,
  1029. (cyg_fsop_getinfo *)cyg_fileio_erofs,
  1030. (cyg_fsop_setinfo *)cyg_fileio_erofs);
  1031. #endif
  1032. // -------------------------------------------------------------------------
  1033. // mtab entry.
  1034. // This defines a single ROMFS loaded into ROM at the configured address
  1035. //
  1036. // MTAB_ENTRY(rom_mte, // structure name
  1037. // "/rom", // mount point
  1038. // "romfs", // FIlesystem type
  1039. // "", // hardware device
  1040. // (CYG_ADDRWORD) CYGNUM_FS_ROM_BASE_ADDRESS // Address in ROM
  1041. //);
  1042. // -------------------------------------------------------------------------
  1043. // File operations.
  1044. // This set of file operations are used for normal open files.
  1045. static cyg_fileops tftpfs_fileops =
  1046. { tftpfs_fo_read, tftpfs_fo_write, tftpfs_fo_lseek,
  1047. (cyg_fileop_ioctl *) cyg_fileio_erofs, cyg_fileio_seltrue,
  1048. tftpfs_fo_fsync, tftpfs_fo_close,
  1049. (cyg_fileop_fstat *) cyg_fileio_erofs,
  1050. (cyg_fileop_getinfo *) cyg_fileio_erofs,
  1051. (cyg_fileop_setinfo *) cyg_fileio_erofs, };
  1052. // -------------------------------------------------------------------------
  1053. // tftpfs_mount()
  1054. // Process a mount request. This mainly finds root for the
  1055. // filesystem.
  1056. static int tftpfs_mount(cyg_fstab_entry *fste, cyg_mtab_entry *mte)
  1057. {
  1058. return ENOERR;
  1059. }
  1060. static int tftpfs_umount(cyg_mtab_entry *mte)
  1061. {
  1062. return ENOERR;
  1063. }
  1064. struct Tftp
  1065. {
  1066. int write;
  1067. int readFile;
  1068. cyg_uint8 *mem;
  1069. int actual;
  1070. char *server;
  1071. char *file;
  1072. };
  1073. static void freeTftp(struct Tftp *t)
  1074. {
  1075. if (t == NULL)
  1076. return;
  1077. if (t->mem)
  1078. free(t->mem);
  1079. if (t->server)
  1080. free(t->server);
  1081. if (t->file)
  1082. free(t->file);
  1083. free(t);
  1084. }
  1085. static const int tftpMaxSize = 8192 * 1024;
  1086. static int tftpfs_open(cyg_mtab_entry *mte, cyg_dir dir, const char *name,
  1087. int mode, cyg_file *file)
  1088. {
  1089. struct Tftp *tftp;
  1090. tftp = malloc(sizeof(struct Tftp));
  1091. if (tftp == NULL)
  1092. return EMFILE;
  1093. memset(tftp, 0, sizeof(struct Tftp));
  1094. file->f_flag |= mode & CYG_FILE_MODE_MASK;
  1095. file->f_type = CYG_FILE_TYPE_FILE;
  1096. file->f_ops = &tftpfs_fileops;
  1097. file->f_offset = 0;
  1098. file->f_data = 0;
  1099. file->f_xops = 0;
  1100. tftp->mem = malloc(tftpMaxSize);
  1101. if (tftp->mem == NULL)
  1102. {
  1103. freeTftp(tftp);
  1104. return EMFILE;
  1105. }
  1106. char *server = strchr(name, '/');
  1107. if (server == NULL)
  1108. {
  1109. freeTftp(tftp);
  1110. return EMFILE;
  1111. }
  1112. tftp->server = malloc(server - name + 1);
  1113. if (tftp->server == NULL)
  1114. {
  1115. freeTftp(tftp);
  1116. return EMFILE;
  1117. }
  1118. strncpy(tftp->server, name, server - name);
  1119. tftp->server[server - name] = 0;
  1120. tftp->file = strdup(server + 1);
  1121. if (tftp->file == NULL)
  1122. {
  1123. freeTftp(tftp);
  1124. return EMFILE;
  1125. }
  1126. file->f_data = (CYG_ADDRWORD) tftp;
  1127. return ENOERR;
  1128. }
  1129. static int fetchTftp(struct Tftp *tftp)
  1130. {
  1131. if (!tftp->readFile)
  1132. {
  1133. int err;
  1134. tftp->actual = tftp_client_get(tftp->file, tftp->server, 0, tftp->mem,
  1135. tftpMaxSize, TFTP_OCTET, &err);
  1136. if (tftp->actual < 0)
  1137. {
  1138. return EMFILE;
  1139. }
  1140. tftp->readFile = 1;
  1141. }
  1142. return ENOERR;
  1143. }
  1144. // -------------------------------------------------------------------------
  1145. // tftpfs_fo_write()
  1146. // Read data from file.
  1147. static int tftpfs_fo_read(struct CYG_FILE_TAG *fp, struct CYG_UIO_TAG *uio)
  1148. {
  1149. struct Tftp *tftp = (struct Tftp *) fp->f_data;
  1150. if (fetchTftp(tftp) != ENOERR)
  1151. return EMFILE;
  1152. int i;
  1153. off_t pos = fp->f_offset;
  1154. int resid = 0;
  1155. for (i = 0; i < uio->uio_iovcnt; i++)
  1156. {
  1157. cyg_iovec *iov = &uio->uio_iov[i];
  1158. char *buf = (char *) iov->iov_base;
  1159. off_t len = iov->iov_len;
  1160. if (len + pos > tftp->actual)
  1161. {
  1162. len = tftp->actual - pos;
  1163. }
  1164. resid += iov->iov_len - len;
  1165. memcpy(buf, tftp->mem + pos, len);
  1166. pos += len;
  1167. }
  1168. uio->uio_resid = resid;
  1169. fp->f_offset = pos;
  1170. return ENOERR;
  1171. }
  1172. static int tftpfs_fo_write(struct CYG_FILE_TAG *fp, struct CYG_UIO_TAG *uio)
  1173. {
  1174. struct Tftp *tftp = (struct Tftp *) fp->f_data;
  1175. int i;
  1176. off_t pos = fp->f_offset;
  1177. int resid = 0;
  1178. for (i = 0; i < uio->uio_iovcnt; i++)
  1179. {
  1180. cyg_iovec *iov = &uio->uio_iov[i];
  1181. char *buf = (char *) iov->iov_base;
  1182. off_t len = iov->iov_len;
  1183. if (len + pos > tftpMaxSize)
  1184. {
  1185. len = tftpMaxSize - pos;
  1186. }
  1187. resid += iov->iov_len - len;
  1188. memcpy(tftp->mem + pos, buf, len);
  1189. pos += len;
  1190. }
  1191. uio->uio_resid = resid;
  1192. fp->f_offset = pos;
  1193. tftp->write = 1;
  1194. return ENOERR;
  1195. }
  1196. static int tftpfs_fo_fsync(struct CYG_FILE_TAG *fp, int mode)
  1197. {
  1198. int error = ENOERR;
  1199. return error;
  1200. }
  1201. // -------------------------------------------------------------------------
  1202. // romfs_fo_close()
  1203. // Close a file. We just clear out the data pointer.
  1204. static int tftpfs_fo_close(struct CYG_FILE_TAG *fp)
  1205. {
  1206. struct Tftp *tftp = (struct Tftp *) fp->f_data;
  1207. int error = ENOERR;
  1208. if (tftp->write)
  1209. {
  1210. tftp_client_put(tftp->file, tftp->server, 0, tftp->mem, fp->f_offset,
  1211. TFTP_OCTET, &error);
  1212. }
  1213. freeTftp(tftp);
  1214. fp->f_data = 0;
  1215. return error;
  1216. }
  1217. // -------------------------------------------------------------------------
  1218. // romfs_fo_lseek()
  1219. // Seek to a new file position.
  1220. static int tftpfs_fo_lseek(struct CYG_FILE_TAG *fp, off_t *apos, int whence)
  1221. {
  1222. struct Tftp *tftp = (struct Tftp *) fp->f_data;
  1223. off_t pos = *apos;
  1224. if (fetchTftp(tftp) != ENOERR)
  1225. return EMFILE;
  1226. switch (whence)
  1227. {
  1228. case SEEK_SET:
  1229. // Pos is already where we want to be.
  1230. break;
  1231. case SEEK_CUR:
  1232. // Add pos to current offset.
  1233. pos += fp->f_offset;
  1234. break;
  1235. case SEEK_END:
  1236. // Add pos to file size.
  1237. pos += tftp->actual;
  1238. break;
  1239. default:
  1240. return EINVAL;
  1241. }
  1242. // Check that pos is still within current file size, or at the
  1243. // very end.
  1244. if (pos < 0 || pos > tftp->actual)
  1245. return EINVAL;
  1246. // All OK, set fp offset and return new position.
  1247. *apos = fp->f_offset = pos;
  1248. return ENOERR;
  1249. }
  1250. void usleep(int us)
  1251. {
  1252. if (us > 10000)
  1253. cyg_thread_delay(us / 10000 + 1);
  1254. else
  1255. HAL_DELAY_US(us);
  1256. }
  1257. // Chunked version.
  1258. cyg_int32 show_log_entry(CYG_HTTPD_STATE *phttpstate)
  1259. {
  1260. cyg_httpd_start_chunked("text");
  1261. if (logCount >= logSize)
  1262. {
  1263. cyg_httpd_write_chunked(logBuffer + logCount % logSize, logSize
  1264. - logCount % logSize);
  1265. }
  1266. cyg_httpd_write_chunked(logBuffer, writePtr);
  1267. cyg_httpd_end_chunked();
  1268. return -1;
  1269. }
  1270. CYG_HTTPD_HANDLER_TABLE_ENTRY(show_log, "/ram/log", show_log_entry);
  1271. // Filesystem operations
  1272. static int logfs_mount(cyg_fstab_entry *fste, cyg_mtab_entry *mte);
  1273. static int logfs_umount(cyg_mtab_entry *mte);
  1274. static int logfs_open(cyg_mtab_entry *mte, cyg_dir dir, const char *name,
  1275. int mode, cyg_file *fte);
  1276. static int logfs_fo_write(struct CYG_FILE_TAG *fp, struct CYG_UIO_TAG *uio);
  1277. // File operations
  1278. static int logfs_fo_fsync(struct CYG_FILE_TAG *fp, int mode);
  1279. static int logfs_fo_close(struct CYG_FILE_TAG *fp);
  1280. #include <cyg/io/devtab.h>
  1281. //==========================================================================
  1282. // Filesystem table entries
  1283. // -------------------------------------------------------------------------
  1284. // Fstab entry.
  1285. // This defines the entry in the filesystem table.
  1286. // For simplicity we use _FILESYSTEM synchronization for all accesses since
  1287. // we should never block in any filesystem operations.
  1288. FSTAB_ENTRY(logfs_fste, "logfs", 0,
  1289. CYG_SYNCMODE_FILE_FILESYSTEM | CYG_SYNCMODE_IO_FILESYSTEM,
  1290. logfs_mount,
  1291. logfs_umount,
  1292. logfs_open,
  1293. (cyg_fsop_unlink *)cyg_fileio_erofs,
  1294. (cyg_fsop_mkdir *)cyg_fileio_erofs,
  1295. (cyg_fsop_rmdir *)cyg_fileio_erofs,
  1296. (cyg_fsop_rename *)cyg_fileio_erofs,
  1297. (cyg_fsop_link *)cyg_fileio_erofs,
  1298. (cyg_fsop_opendir *)cyg_fileio_erofs,
  1299. (cyg_fsop_chdir *)cyg_fileio_erofs,
  1300. (cyg_fsop_stat *)cyg_fileio_erofs,
  1301. (cyg_fsop_getinfo *)cyg_fileio_erofs,
  1302. (cyg_fsop_setinfo *)cyg_fileio_erofs);
  1303. // -------------------------------------------------------------------------
  1304. // File operations.
  1305. // This set of file operations are used for normal open files.
  1306. static cyg_fileops logfs_fileops =
  1307. { (cyg_fileop_read *) cyg_fileio_erofs, (cyg_fileop_write *) logfs_fo_write,
  1308. (cyg_fileop_lseek *) cyg_fileio_erofs,
  1309. (cyg_fileop_ioctl *) cyg_fileio_erofs, cyg_fileio_seltrue,
  1310. logfs_fo_fsync, logfs_fo_close, (cyg_fileop_fstat *) cyg_fileio_erofs,
  1311. (cyg_fileop_getinfo *) cyg_fileio_erofs,
  1312. (cyg_fileop_setinfo *) cyg_fileio_erofs, };
  1313. // -------------------------------------------------------------------------
  1314. // logfs_mount()
  1315. // Process a mount request. This mainly finds root for the
  1316. // filesystem.
  1317. static int logfs_mount(cyg_fstab_entry *fste, cyg_mtab_entry *mte)
  1318. {
  1319. return ENOERR;
  1320. }
  1321. static int logfs_umount(cyg_mtab_entry *mte)
  1322. {
  1323. return ENOERR;
  1324. }
  1325. static int logfs_open(cyg_mtab_entry *mte, cyg_dir dir, const char *name,
  1326. int mode, cyg_file *file)
  1327. {
  1328. file->f_flag |= mode & CYG_FILE_MODE_MASK;
  1329. file->f_type = CYG_FILE_TYPE_FILE;
  1330. file->f_ops = &logfs_fileops;
  1331. file->f_offset = 0;
  1332. file->f_data = 0;
  1333. file->f_xops = 0;
  1334. return ENOERR;
  1335. }
  1336. // -------------------------------------------------------------------------
  1337. // logfs_fo_write()
  1338. // Write data to file.
  1339. static int logfs_fo_write(struct CYG_FILE_TAG *fp, struct CYG_UIO_TAG *uio)
  1340. {
  1341. int i;
  1342. for (i = 0; i < uio->uio_iovcnt; i++)
  1343. {
  1344. cyg_iovec *iov = &uio->uio_iov[i];
  1345. char *buf = (char *) iov->iov_base;
  1346. off_t len = iov->iov_len;
  1347. diag_write(buf, len);
  1348. }
  1349. uio->uio_resid = 0;
  1350. return ENOERR;
  1351. }
  1352. static int logfs_fo_fsync(struct CYG_FILE_TAG *fp, int mode)
  1353. {
  1354. return ENOERR;
  1355. }
  1356. // -------------------------------------------------------------------------
  1357. // romfs_fo_close()
  1358. // Close a file. We just clear out the data pointer.
  1359. static int logfs_fo_close(struct CYG_FILE_TAG *fp)
  1360. {
  1361. return ENOERR;
  1362. }
  1363. int loadFile(const char *fileName, void **data, int *len);
  1364. /* boolean parameter stored on config */
  1365. int boolParam(char *var)
  1366. {
  1367. bool result = false;
  1368. char *name = alloc_printf("%s/%s", zylin_config_dir, var);
  1369. if (name == NULL)
  1370. return result;
  1371. void *data;
  1372. int len;
  1373. if (loadFile(name, &data, &len) == ERROR_OK)
  1374. {
  1375. if (len > 1)
  1376. len = 1;
  1377. result = strncmp((char *) data, "1", len) == 0;
  1378. free(data);
  1379. }
  1380. free(name);
  1381. return result;
  1382. }