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.
 
 
 
 
 
 

2594 lines
58 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2007-2008 by Øyvind Harboe *
  3. * *
  4. * This program is free software; you can redistribute it and/or modify *
  5. * it under the terms of the GNU General Public License as published by *
  6. * the Free Software Foundation; either version 2 of the License, or *
  7. * (at your option) any later version. *
  8. * *
  9. * This program is distributed in the hope that it will be useful, *
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  12. * GNU General Public License for more details. *
  13. * *
  14. * You should have received a copy of the GNU General Public License *
  15. * along with this program; if not, write to the *
  16. * Free Software Foundation, Inc., *
  17. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  18. ***************************************************************************/
  19. #ifdef HAVE_CONFIG_H
  20. #include "config.h"
  21. #endif
  22. #include "log.h"
  23. #include "types.h"
  24. #include "jtag.h"
  25. #include "configuration.h"
  26. #include "xsvf.h"
  27. #include "target.h"
  28. #include "flash.h"
  29. #include "nand.h"
  30. #include "pld.h"
  31. #include "command.h"
  32. #include "server.h"
  33. #include "telnet_server.h"
  34. #include "gdb_server.h"
  35. #include <time_support.h>
  36. #include <sys/time.h>
  37. #include <sys/types.h>
  38. #include <strings.h>
  39. #include <stdio.h>
  40. #include <stdlib.h>
  41. #include <string.h>
  42. #include <unistd.h>
  43. #include <errno.h>
  44. #include <cyg/io/flash.h>
  45. #include <pkgconf/fs_jffs2.h> // Address of JFFS2
  46. #include <network.h>
  47. #include <fcntl.h>
  48. #include <sys/stat.h>
  49. #include <cyg/fileio/fileio.h>
  50. #include <dirent.h>
  51. #include <cyg/athttpd/http.h>
  52. #include <cyg/athttpd/socket.h>
  53. #include <cyg/athttpd/handler.h>
  54. #include <cyg/athttpd/cgi.h>
  55. #include <cyg/athttpd/forms.h>
  56. #include <cyg/discover/discover.h>
  57. #include <cyg/hal/hal_diag.h>
  58. #include <cyg/kernel/kapi.h>
  59. #include <cyg/io/serialio.h>
  60. #include <cyg/io/io.h>
  61. #include <netinet/tcp.h>
  62. #include "rom.h"
  63. #include <sys/ioctl.h>
  64. #include <sys/socket.h>
  65. #include <netinet/in.h>
  66. #include <net/if.h>
  67. #include <arpa/inet.h>
  68. #include <sys/types.h>
  69. #include <sys/socket.h>
  70. #include <netdb.h>
  71. #include <netinet/in.h>
  72. #include <unistd.h>
  73. #include <arpa/inet.h>
  74. #include <stdio.h>
  75. #include <ifaddrs.h>
  76. #include <string.h>
  77. #include <unistd.h>
  78. #include <stdio.h>
  79. #define MAX_IFS 64
  80. #if defined(CYGPKG_NET_FREEBSD_STACK)
  81. #include <tftp_support.h>
  82. /* posix compatibility broken*/
  83. struct tftpd_fileops fileops =
  84. {
  85. (int (*)(const char *, int))open,
  86. close,
  87. (int (*)(int, const void *, int))write,
  88. ( int (*)(int, void *, int))read
  89. };
  90. #endif
  91. #define ZYLIN_VERSION "1.47"
  92. #define ZYLIN_DATE __DATE__
  93. #define ZYLIN_TIME __TIME__
  94. /* hmmm.... we can't pick up the right # during build if we've checked this out
  95. * in Eclipse... arrggghh...*/
  96. #define ZYLIN_OPENOCD "$Revision$"
  97. #define ZYLIN_OPENOCD_VERSION "Zylin JTAG ZY1000 " ZYLIN_VERSION " " ZYLIN_DATE " " ZYLIN_TIME
  98. #define ZYLIN_CONFIG_DIR "/config/settings"
  99. void diag_write(char *buf, int len)
  100. {
  101. int j;
  102. for (j = 0; j < len; j++)
  103. {
  104. diag_printf("%c", buf[j]);
  105. }
  106. }
  107. static bool serialLog = true;
  108. static bool writeLog = true;
  109. char hwaddr[512];
  110. struct FastLoad
  111. {
  112. u32 address;
  113. u8 *data;
  114. int length;
  115. };
  116. static int fastload_num;
  117. static struct FastLoad *fastload;
  118. static void free_fastload()
  119. {
  120. if (fastload!=NULL)
  121. {
  122. int i;
  123. for (i=0; i<fastload_num; i++)
  124. {
  125. if (fastload[i].data)
  126. free(fastload[i].data);
  127. }
  128. free(fastload);
  129. fastload=NULL;
  130. }
  131. }
  132. int handle_fast_load_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  133. {
  134. u8 *buffer;
  135. u32 buf_cnt;
  136. u32 image_size;
  137. u32 min_address=0;
  138. u32 max_address=0xffffffff;
  139. int i;
  140. int retval;
  141. image_t image;
  142. duration_t duration;
  143. char *duration_text;
  144. if ((argc < 1)||(argc > 5))
  145. {
  146. return ERROR_COMMAND_SYNTAX_ERROR;
  147. }
  148. /* a base address isn't always necessary, default to 0x0 (i.e. don't relocate) */
  149. if (argc >= 2)
  150. {
  151. image.base_address_set = 1;
  152. image.base_address = strtoul(args[1], NULL, 0);
  153. }
  154. else
  155. {
  156. image.base_address_set = 0;
  157. }
  158. image.start_address_set = 0;
  159. if (argc>=4)
  160. {
  161. min_address=strtoul(args[3], NULL, 0);
  162. }
  163. if (argc>=5)
  164. {
  165. max_address=strtoul(args[4], NULL, 0)+min_address;
  166. }
  167. if (min_address>max_address)
  168. {
  169. return ERROR_COMMAND_SYNTAX_ERROR;
  170. }
  171. duration_start_measure(&duration);
  172. if (image_open(&image, args[0], (argc >= 3) ? args[2] : NULL) != ERROR_OK)
  173. {
  174. return ERROR_OK;
  175. }
  176. image_size = 0x0;
  177. retval = ERROR_OK;
  178. fastload_num=image.num_sections;
  179. fastload=(struct FastLoad *)malloc(sizeof(struct FastLoad)*image.num_sections);
  180. if (fastload==NULL)
  181. {
  182. image_close(&image);
  183. return ERROR_FAIL;
  184. }
  185. memset(fastload, 0, sizeof(struct FastLoad)*image.num_sections);
  186. for (i = 0; i < image.num_sections; i++)
  187. {
  188. buffer = malloc(image.sections[i].size);
  189. if (buffer == NULL)
  190. {
  191. command_print(cmd_ctx, "error allocating buffer for section (%d bytes)", image.sections[i].size);
  192. break;
  193. }
  194. if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
  195. {
  196. free(buffer);
  197. break;
  198. }
  199. u32 offset=0;
  200. u32 length=buf_cnt;
  201. /* DANGER!!! beware of unsigned comparision here!!! */
  202. if ((image.sections[i].base_address+buf_cnt>=min_address)&&
  203. (image.sections[i].base_address<max_address))
  204. {
  205. if (image.sections[i].base_address<min_address)
  206. {
  207. /* clip addresses below */
  208. offset+=min_address-image.sections[i].base_address;
  209. length-=offset;
  210. }
  211. if (image.sections[i].base_address+buf_cnt>max_address)
  212. {
  213. length-=(image.sections[i].base_address+buf_cnt)-max_address;
  214. }
  215. fastload[i].address=image.sections[i].base_address+offset;
  216. fastload[i].data=malloc(length);
  217. if (fastload[i].data==NULL)
  218. {
  219. free(buffer);
  220. break;
  221. }
  222. memcpy(fastload[i].data, buffer+offset, length);
  223. fastload[i].length=length;
  224. image_size += length;
  225. command_print(cmd_ctx, "%u byte written at address 0x%8.8x", length, image.sections[i].base_address+offset);
  226. }
  227. free(buffer);
  228. }
  229. duration_stop_measure(&duration, &duration_text);
  230. if (retval==ERROR_OK)
  231. {
  232. command_print(cmd_ctx, "Loaded %u bytes in %s", image_size, duration_text);
  233. command_print(cmd_ctx, "NB!!! image has not been loaded to target, issue a subsequent 'fast_load' to do so.");
  234. }
  235. free(duration_text);
  236. image_close(&image);
  237. if (retval!=ERROR_OK)
  238. {
  239. free_fastload();
  240. }
  241. return retval;
  242. }
  243. int handle_fast_load_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  244. {
  245. if (argc>0)
  246. return ERROR_COMMAND_SYNTAX_ERROR;
  247. if (fastload==NULL)
  248. {
  249. LOG_ERROR("No image in memory");
  250. return ERROR_FAIL;
  251. }
  252. int i;
  253. int ms=timeval_ms();
  254. int size=0;
  255. for (i=0; i<fastload_num;i++)
  256. {
  257. int retval;
  258. target_t *target = get_current_target(cmd_ctx);
  259. if ((retval = target_write_buffer(target, fastload[i].address, fastload[i].length, fastload[i].data)) != ERROR_OK)
  260. {
  261. return retval;
  262. }
  263. size+=fastload[i].length;
  264. }
  265. int after=timeval_ms();
  266. command_print(cmd_ctx, "Loaded image %f kBytes/s", (float)(size/1024.0)/((float)(after-ms)/1000.0));
  267. return ERROR_OK;
  268. }
  269. /* Give TELNET a way to find out what version this is */
  270. int handle_zy1000_version_command(struct command_context_s *cmd_ctx, char *cmd,
  271. char **args, int argc)
  272. {
  273. if (argc > 1)
  274. {
  275. return ERROR_COMMAND_SYNTAX_ERROR;
  276. }
  277. if (argc == 0)
  278. {
  279. command_print(cmd_ctx, ZYLIN_OPENOCD_VERSION);
  280. } else if (strcmp("openocd", args[0])==0)
  281. {
  282. int revision;
  283. revision=atol(ZYLIN_OPENOCD+strlen("XRevision: "));
  284. command_print(cmd_ctx, "%d", revision);
  285. } else if (strcmp("zy1000", args[0])==0)
  286. {
  287. command_print(cmd_ctx, "%s", ZYLIN_VERSION);
  288. } else if (strcmp("date", args[0])==0)
  289. {
  290. command_print(cmd_ctx, "%s", ZYLIN_DATE);
  291. } else
  292. {
  293. return ERROR_COMMAND_SYNTAX_ERROR;
  294. }
  295. return ERROR_OK;
  296. }
  297. extern flash_driver_t *flash_drivers[];
  298. extern target_type_t *target_types[];
  299. #ifdef CYGPKG_PROFILE_GPROF
  300. #include <cyg/profile/profile.h>
  301. extern char _stext, _etext; // Defined by the linker
  302. void start_profile(void)
  303. {
  304. // This starts up the system-wide profiling, gathering
  305. // profile information on all of the code, with a 16 byte
  306. // "bucket" size, at a rate of 100us/profile hit.
  307. // Note: a bucket size of 16 will give pretty good function
  308. // resolution. Much smaller and the buffer becomes
  309. // much too large for very little gain.
  310. // Note: a timer period of 100us is also a reasonable
  311. // compromise. Any smaller and the overhead of
  312. // handling the timter (profile) interrupt could
  313. // swamp the system. A fast processor might get
  314. // by with a smaller value, but a slow one could
  315. // even be swamped by this value. If the value is
  316. // too large, the usefulness of the profile is reduced.
  317. // no more interrupts than 1/10ms.
  318. // profile_on(&_stext, &_etext, 16, 10000); // DRAM
  319. //profile_on((void *)0, (void *)0x40000, 16, 10000); // SRAM
  320. profile_on(0, &_etext, 16, 10000); // SRAM & DRAM
  321. }
  322. #endif
  323. // launch GDB server if a config file exists
  324. bool zylinjtag_parse_config_file(struct command_context_s *cmd_ctx, const char *config_file_name)
  325. {
  326. bool foundFile = false;
  327. FILE *config_file = NULL;
  328. command_print(cmd_ctx, "executing config file %s", config_file_name);
  329. config_file = fopen(config_file_name, "r");
  330. if (config_file)
  331. {
  332. fclose(config_file);
  333. int retval;
  334. retval = command_run_linef(cmd_ctx, "script %s", config_file_name);
  335. if (retval == ERROR_OK)
  336. {
  337. foundFile = true;
  338. }
  339. else
  340. {
  341. command_print(cmd_ctx, "Failed executing %s %d", config_file_name, retval);
  342. }
  343. }
  344. else
  345. {
  346. command_print(cmd_ctx, "No %s found", config_file_name);
  347. }
  348. return foundFile;
  349. }
  350. extern int eth0_up;
  351. static FILE *log;
  352. static char reboot_stack[2048];
  353. static void
  354. zylinjtag_reboot(cyg_addrword_t data)
  355. {
  356. serialLog = true;
  357. diag_printf("Rebooting in 100 ticks..\n");
  358. cyg_thread_delay(100);
  359. diag_printf("Unmounting /config..\n");
  360. umount("/config");
  361. diag_printf("Rebooting..\n");
  362. HAL_PLATFORM_RESET();
  363. }
  364. static cyg_thread zylinjtag_thread_object;
  365. static cyg_handle_t zylinjtag_thread_handle;
  366. void reboot(void)
  367. {
  368. cyg_thread_create(1,
  369. zylinjtag_reboot,
  370. (cyg_addrword_t)0,
  371. "reboot Thread",
  372. (void *)reboot_stack,
  373. sizeof(reboot_stack),
  374. &zylinjtag_thread_handle,
  375. &zylinjtag_thread_object);
  376. cyg_thread_resume(zylinjtag_thread_handle);
  377. }
  378. int configuration_output_handler(struct command_context_s *context, const char* line)
  379. {
  380. diag_printf("%s", line);
  381. return ERROR_OK;
  382. }
  383. int zy1000_configuration_output_handler_log(struct command_context_s *context, const char* line)
  384. {
  385. LOG_USER_N("%s", line);
  386. return ERROR_OK;
  387. }
  388. int handle_rm_command(struct command_context_s *cmd_ctx, char *cmd,
  389. char **args, int argc)
  390. {
  391. if (argc != 1)
  392. {
  393. command_print(cmd_ctx, "rm <filename>");
  394. return ERROR_INVALID_ARGUMENTS;
  395. }
  396. if (unlink(args[0]) != 0)
  397. {
  398. command_print(cmd_ctx, "failed: %d", errno);
  399. }
  400. return ERROR_OK;
  401. }
  402. int loadFile(const char *fileName, void **data, int *len);
  403. int handle_cat_command(struct command_context_s *cmd_ctx, char *cmd,
  404. char **args, int argc)
  405. {
  406. if (argc != 1)
  407. {
  408. command_print(cmd_ctx, "cat <filename>");
  409. return ERROR_INVALID_ARGUMENTS;
  410. }
  411. // NOTE!!! we only have line printing capability so we print the entire file as a single line.
  412. void *data;
  413. int len;
  414. int retval = loadFile(args[0], &data, &len);
  415. if (retval == ERROR_OK)
  416. {
  417. command_print(cmd_ctx, "%s", data);
  418. free(data);
  419. }
  420. else
  421. {
  422. command_print(cmd_ctx, "%s not found %d", args[0], retval);
  423. }
  424. return ERROR_OK;
  425. }
  426. int handle_trunc_command(struct command_context_s *cmd_ctx, char *cmd,
  427. char **args, int argc)
  428. {
  429. if (argc != 1)
  430. {
  431. command_print(cmd_ctx, "trunc <filename>");
  432. return ERROR_INVALID_ARGUMENTS;
  433. }
  434. FILE *config_file = NULL;
  435. config_file = fopen(args[0], "w");
  436. if (config_file != NULL)
  437. fclose(config_file);
  438. return ERROR_OK;
  439. }
  440. int handle_meminfo_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  441. {
  442. static int prev = 0;
  443. struct mallinfo info;
  444. if (argc != 0)
  445. {
  446. command_print(cmd_ctx, "meminfo");
  447. return ERROR_INVALID_ARGUMENTS;
  448. }
  449. info = mallinfo();
  450. if (prev > 0)
  451. {
  452. command_print(cmd_ctx, "Diff: %d", prev - info.fordblks);
  453. }
  454. prev = info.fordblks;
  455. command_print(cmd_ctx, "Available ram: %d", info.fordblks );
  456. return ERROR_OK;
  457. }
  458. static bool savePower;
  459. static void setPower(bool power)
  460. {
  461. savePower = power;
  462. if (power)
  463. {
  464. HAL_WRITE_UINT32(0x08000014, 0x8);
  465. } else
  466. {
  467. HAL_WRITE_UINT32(0x08000010, 0x8);
  468. }
  469. }
  470. int handle_power_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  471. {
  472. if (argc > 1)
  473. {
  474. return ERROR_INVALID_ARGUMENTS;
  475. }
  476. if (argc == 1)
  477. {
  478. if (strcmp(args[0], "on") == 0)
  479. {
  480. setPower(1);
  481. }
  482. else if (strcmp(args[0], "off") == 0)
  483. {
  484. setPower(0);
  485. } else
  486. {
  487. command_print(cmd_ctx, "arg is \"on\" or \"off\"");
  488. return ERROR_INVALID_ARGUMENTS;
  489. }
  490. }
  491. command_print(cmd_ctx, "Target power %s", savePower ? "on" : "off");
  492. return ERROR_OK;
  493. }
  494. int handle_append_command(struct command_context_s *cmd_ctx, char *cmd,
  495. char **args, int argc)
  496. {
  497. if (argc < 1)
  498. {
  499. command_print(cmd_ctx,
  500. "append <filename> [<string1>, [<string2>, ...]]");
  501. return ERROR_INVALID_ARGUMENTS;
  502. }
  503. FILE *config_file = NULL;
  504. config_file = fopen(args[0], "a");
  505. if (config_file != NULL)
  506. {
  507. int i;
  508. fseek(config_file, 0, SEEK_END);
  509. for (i = 1; i < argc; i++)
  510. {
  511. fwrite(args[i], strlen(args[i]), 1, config_file);
  512. if (i != argc - 1)
  513. {
  514. fwrite(" ", 1, 1, config_file);
  515. }
  516. }
  517. fwrite("\n", 1, 1, config_file);
  518. fclose(config_file);
  519. }
  520. return ERROR_OK;
  521. }
  522. extern int telnet_socket;
  523. int readMore(int fd, void *data, int length)
  524. {
  525. /* used in select() */
  526. fd_set read_fds;
  527. /* monitor sockets for acitvity */
  528. int fd_max = 1;
  529. FD_ZERO(&read_fds);
  530. /* listen for new connections */
  531. FD_SET(fd, &read_fds);
  532. // Maximum 5 seconds.
  533. struct timeval tv;
  534. tv.tv_sec = 5;
  535. tv.tv_usec = 0;
  536. int retval = select(fd_max + 1, &read_fds, NULL, NULL, &tv);
  537. if (retval == 0)
  538. {
  539. diag_printf("Timed out waiting for binary payload\n");
  540. return -1;
  541. }
  542. if (retval != 1)
  543. return -1;
  544. return read_socket(fd, data, length);
  545. }
  546. int readAll(int fd, void *data, int length)
  547. {
  548. int pos = 0;
  549. for (;;)
  550. {
  551. int actual = readMore(fd, ((char *) data) + pos, length - pos);
  552. // diag_printf("Read %d bytes(pos=%d, length=%d)\n", actual, pos, length);
  553. if (actual <= 0)
  554. return -1;
  555. pos += actual;
  556. if (pos == length)
  557. break;
  558. }
  559. return length;
  560. }
  561. int handle_peek_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  562. {
  563. cyg_uint32 value;
  564. if (argc != 1)
  565. {
  566. return ERROR_INVALID_ARGUMENTS;
  567. }
  568. HAL_READ_UINT32(strtoul(args[0], NULL, 0), value);
  569. command_print(cmd_ctx, "0x%x : 0x%x", strtoul(args[0], NULL, 0), value);
  570. return ERROR_OK;
  571. }
  572. int handle_poke_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  573. {
  574. if (argc != 2)
  575. {
  576. return ERROR_INVALID_ARGUMENTS;
  577. }
  578. HAL_WRITE_UINT32(strtoul(args[0], NULL, 0), strtoul(args[1], NULL, 0));
  579. return ERROR_OK;
  580. }
  581. int handle_cp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  582. {
  583. if (argc != 2)
  584. {
  585. return ERROR_INVALID_ARGUMENTS;
  586. }
  587. // NOTE!!! we only have line printing capability so we print the entire file as a single line.
  588. void *data;
  589. int len;
  590. int retval = loadFile(args[0], &data, &len);
  591. if (retval != ERROR_OK)
  592. return retval;
  593. FILE *f = fopen(args[1], "wb");
  594. if (f == NULL)
  595. retval = ERROR_INVALID_ARGUMENTS;
  596. int pos = 0;
  597. for (;;)
  598. {
  599. int chunk = len - pos;
  600. static const int maxChunk = 512 * 1024; // ~1/sec
  601. if (chunk > maxChunk)
  602. {
  603. chunk = maxChunk;
  604. }
  605. if ((retval==ERROR_OK)&&(fwrite(((char *)data)+pos, 1, chunk, f)!=chunk))
  606. retval = ERROR_INVALID_ARGUMENTS;
  607. if (retval != ERROR_OK)
  608. {
  609. break;
  610. }
  611. command_print(cmd_ctx, "%d", len - pos);
  612. pos += chunk;
  613. if (pos == len)
  614. break;
  615. }
  616. if (retval == ERROR_OK)
  617. {
  618. command_print(cmd_ctx, "Copied %s to %s", args[0], args[1]);
  619. } else
  620. {
  621. command_print(cmd_ctx, "Failed: %d", retval);
  622. }
  623. if (data != NULL)
  624. free(data);
  625. if (f != NULL)
  626. fclose(f);
  627. if (retval != ERROR_OK)
  628. unlink(args[1]);
  629. return retval;
  630. }
  631. #ifdef CYGPKG_PROFILE_GPROF
  632. extern void start_profile();
  633. int eCosBoard_handle_eCosBoard_profile_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  634. {
  635. command_print(cmd_ctx, "Profiling started");
  636. start_profile();
  637. return ERROR_OK;
  638. }
  639. #endif
  640. externC void phi_init_all_network_interfaces();
  641. command_context_t *cmd_ctx;
  642. static bool webRunning = false;
  643. void keep_webserver()
  644. {
  645. // Target initialisation is only attempted at startup, so we sleep forever and
  646. // let the http server bail us out(i.e. get config files set up).
  647. diag_printf("OpenOCD has invoked exit().\n"
  648. "Use web server to correct any configuration settings and reboot.\n");
  649. if (!webRunning)
  650. reboot();
  651. // exit() will terminate the current thread and we we'll then sleep eternally or
  652. // we'll have a reboot scheduled.
  653. }
  654. extern void printDccChar(char c);
  655. static char logBuffer[128 * 1024];
  656. static const int logSize = sizeof(logBuffer);
  657. int writePtr = 0;
  658. int logCount = 0;
  659. void _zylinjtag_diag_write_char(char c, void **param)
  660. {
  661. if (writeLog)
  662. {
  663. logBuffer[writePtr] = c;
  664. writePtr = (writePtr + 1) % logSize;
  665. logCount++;
  666. }
  667. if (serialLog)
  668. {
  669. if (c == '\n')
  670. {
  671. HAL_DIAG_WRITE_CHAR('\r');
  672. }
  673. HAL_DIAG_WRITE_CHAR(c);
  674. }
  675. #ifdef CYGPKG_HAL_ZYLIN_PHI
  676. printDccChar(c);
  677. #endif
  678. }
  679. #define SHOW_RESULT(a, b) diag_printf(#a " failed %d\n", (int)b)
  680. #define IOSIZE 512
  681. static void copyfile(char *name2, char *name1)
  682. {
  683. int err;
  684. char buf[IOSIZE];
  685. int fd1, fd2;
  686. ssize_t done, wrote;
  687. fd1 = open(name1, O_WRONLY | O_CREAT);
  688. if (fd1 < 0)
  689. SHOW_RESULT( open, fd1 );
  690. fd2 = open(name2, O_RDONLY);
  691. if (fd2 < 0)
  692. SHOW_RESULT( open, fd2 );
  693. for (;;)
  694. {
  695. done = read(fd2, buf, IOSIZE );
  696. if (done < 0)
  697. {
  698. SHOW_RESULT( read, done );
  699. break;
  700. }
  701. if( done == 0 ) break;
  702. wrote = write(fd1, buf, done);
  703. if( wrote != done ) SHOW_RESULT( write, wrote );
  704. if( wrote != done ) break;
  705. }
  706. err = close(fd1);
  707. if( err < 0 ) SHOW_RESULT( close, err );
  708. err = close(fd2);
  709. if( err < 0 ) SHOW_RESULT( close, err );
  710. }
  711. static void copydir(char *name, char *destdir)
  712. {
  713. int err;
  714. DIR *dirp;
  715. dirp = opendir(destdir);
  716. if (dirp==NULL)
  717. {
  718. mkdir(destdir, 0777);
  719. } else
  720. {
  721. err = closedir(dirp);
  722. }
  723. dirp = opendir(name);
  724. if( dirp == NULL ) SHOW_RESULT( opendir, -1 );
  725. for (;;)
  726. {
  727. struct dirent *entry = readdir(dirp);
  728. if (entry == NULL)
  729. break;
  730. if (strcmp(entry->d_name, ".") == 0)
  731. continue;
  732. if (strcmp(entry->d_name, "..") == 0)
  733. continue;
  734. bool isDir = false;
  735. struct stat buf;
  736. char fullPath[PATH_MAX];
  737. strncpy(fullPath, name, PATH_MAX);
  738. strcat(fullPath, "/");
  739. strncat(fullPath, entry->d_name, PATH_MAX - strlen(fullPath));
  740. if (stat(fullPath, &buf) == -1)
  741. {
  742. diag_printf("unable to read status from %s", fullPath);
  743. break;
  744. }
  745. isDir = S_ISDIR(buf.st_mode) != 0;
  746. if (isDir)
  747. continue;
  748. // diag_printf("<INFO>: entry %14s",entry->d_name);
  749. char fullname[PATH_MAX];
  750. char fullname2[PATH_MAX];
  751. strcpy(fullname, name);
  752. strcat(fullname, "/");
  753. strcat(fullname, entry->d_name);
  754. strcpy(fullname2, destdir);
  755. strcat(fullname2, "/");
  756. strcat(fullname2, entry->d_name);
  757. // diag_printf("from %s to %s\n", fullname, fullname2);
  758. copyfile(fullname, fullname2);
  759. // diag_printf("\n");
  760. }
  761. err = closedir(dirp);
  762. if( err < 0 ) SHOW_RESULT( stat, err );
  763. }
  764. #if 0
  765. MTAB_ENTRY( romfs_mte1,
  766. "/rom",
  767. "romfs",
  768. "",
  769. (CYG_ADDRWORD) &filedata[0] );
  770. #endif
  771. void openocd_sleep_prelude()
  772. {
  773. cyg_mutex_unlock(&httpstate.jim_lock);
  774. }
  775. void openocd_sleep_postlude()
  776. {
  777. cyg_mutex_lock(&httpstate.jim_lock);
  778. }
  779. static int
  780. zylinjtag_Jim_Command_rm(Jim_Interp *interp,
  781. int argc,
  782. Jim_Obj * const *argv)
  783. {
  784. int del;
  785. if (argc != 2)
  786. {
  787. Jim_WrongNumArgs(interp, 1, argv, "rm ?dirorfile?");
  788. return JIM_ERR;
  789. }
  790. del = 0;
  791. if (unlink(Jim_GetString(argv[1], NULL)) == 0)
  792. del = 1;
  793. if (rmdir(Jim_GetString(argv[1], NULL)) == 0)
  794. del = 1;
  795. return del ? JIM_OK : JIM_ERR;
  796. }
  797. static int zylinjtag_Jim_Command_threads(Jim_Interp *interp, int argc,
  798. Jim_Obj * const *argv)
  799. {
  800. cyg_handle_t thread = 0;
  801. cyg_uint16 id = 0;
  802. Jim_Obj *threads = Jim_NewListObj(interp, NULL, 0);
  803. /* Loop over the threads, and generate a table row for
  804. * each.
  805. */
  806. while (cyg_thread_get_next(&thread, &id))
  807. {
  808. Jim_Obj *threadObj = Jim_NewListObj(interp, NULL, 0);
  809. cyg_thread_info info;
  810. char *state_string;
  811. cyg_thread_get_info(thread, id, &info);
  812. if (info.name == NULL)
  813. info.name = "<no name>";
  814. Jim_ListAppendElement(interp, threadObj, Jim_NewStringObj(interp,
  815. info.name, strlen(info.name)));
  816. /* Translate the state into a string.
  817. */
  818. if (info.state == 0)
  819. state_string = "RUN";
  820. else if (info.state & 0x04)
  821. state_string = "SUSP";
  822. else
  823. switch (info.state & 0x1b)
  824. {
  825. case 0x01:
  826. state_string = "SLEEP";
  827. break;
  828. case 0x02:
  829. state_string = "CNTSLEEP";
  830. break;
  831. case 0x08:
  832. state_string = "CREATE";
  833. break;
  834. case 0x10:
  835. state_string = "EXIT";
  836. break;
  837. default:
  838. state_string = "????";
  839. break;
  840. }
  841. Jim_ListAppendElement(interp, threadObj, Jim_NewStringObj(interp,
  842. state_string, strlen(state_string)));
  843. Jim_ListAppendElement (interp, threadObj, Jim_NewIntObj(interp, id));
  844. Jim_ListAppendElement(interp, threadObj, Jim_NewIntObj(interp, info.set_pri));
  845. Jim_ListAppendElement(interp, threadObj, Jim_NewIntObj(interp, info.cur_pri));
  846. Jim_ListAppendElement(interp, threads, threadObj);
  847. }
  848. Jim_SetResult( interp, threads);
  849. return JIM_OK;
  850. }
  851. static int
  852. zylinjtag_Jim_Command_ls(Jim_Interp *interp,
  853. int argc,
  854. Jim_Obj * const *argv)
  855. {
  856. if (argc != 2)
  857. {
  858. Jim_WrongNumArgs(interp, 1, argv, "ls ?dir?");
  859. return JIM_ERR;
  860. }
  861. char *name = (char*) Jim_GetString(argv[1], NULL);
  862. DIR *dirp = NULL;
  863. dirp = opendir(name);
  864. if (dirp == NULL)
  865. {
  866. return JIM_ERR;
  867. }
  868. Jim_Obj *objPtr = Jim_NewListObj(interp, NULL, 0);
  869. for (;;)
  870. {
  871. struct dirent *entry = NULL;
  872. entry = readdir(dirp);
  873. if (entry == NULL)
  874. break;
  875. if ((strcmp(".", entry->d_name)==0)||(strcmp("..", entry->d_name)==0))
  876. continue;
  877. Jim_ListAppendElement(interp, objPtr, Jim_NewStringObj(interp, entry->d_name, strlen(entry->d_name)));
  878. }
  879. closedir(dirp);
  880. Jim_SetResult(interp, objPtr);
  881. return JIM_OK;
  882. }
  883. static int
  884. zylinjtag_Jim_Command_getmem(Jim_Interp *interp,
  885. int argc,
  886. Jim_Obj * const *argv)
  887. {
  888. if (argc != 3)
  889. {
  890. Jim_WrongNumArgs(interp, 1, argv, "ls ?dir?");
  891. return JIM_ERR;
  892. }
  893. long address;
  894. long length;
  895. if (Jim_GetLong(interp, argv[1], &address) != JIM_OK)
  896. return JIM_ERR;
  897. if (Jim_GetLong(interp, argv[2], &length) != JIM_OK)
  898. return JIM_ERR;
  899. if (length < 0 && length > (4096 * 1024))
  900. {
  901. Jim_WrongNumArgs(interp, 1, argv, "getmem ?dir?");
  902. return JIM_ERR;
  903. }
  904. void *mem = malloc(length);
  905. if (mem == NULL)
  906. return JIM_ERR;
  907. target_t *target = get_current_target(cmd_ctx);
  908. int retval;
  909. int size = 1;
  910. int count = length;
  911. if ((address % 4 == 0) && (count % 4 == 0))
  912. {
  913. size = 4;
  914. count /= 4;
  915. }
  916. if ((retval = target->type->read_memory(target, address, size, count, mem)) != ERROR_OK)
  917. {
  918. free(mem);
  919. return JIM_ERR;
  920. }
  921. Jim_Obj *objPtr = Jim_NewStringObj(interp, mem, length);
  922. Jim_SetResult(interp, objPtr);
  923. free(mem);
  924. return JIM_OK;
  925. }
  926. static int
  927. zylinjtag_Jim_Command_peek(Jim_Interp *interp,
  928. int argc,
  929. Jim_Obj * const *argv)
  930. {
  931. if (argc != 2)
  932. {
  933. Jim_WrongNumArgs(interp, 1, argv, "peek ?address?");
  934. return JIM_ERR;
  935. }
  936. long address;
  937. if (Jim_GetLong(interp, argv[1], &address) != JIM_OK)
  938. return JIM_ERR;
  939. int value = *((volatile int *) address);
  940. Jim_SetResult(interp, Jim_NewIntObj(interp, value));
  941. return JIM_OK;
  942. }
  943. static int
  944. zylinjtag_Jim_Command_poke(Jim_Interp *interp,
  945. int argc,
  946. Jim_Obj * const *argv)
  947. {
  948. if (argc != 3)
  949. {
  950. Jim_WrongNumArgs(interp, 1, argv, "poke ?address? ?value?");
  951. return JIM_ERR;
  952. }
  953. long address;
  954. if (Jim_GetLong(interp, argv[1], &address) != JIM_OK)
  955. return JIM_ERR;
  956. long value;
  957. if (Jim_GetLong(interp, argv[2], &value) != JIM_OK)
  958. return JIM_ERR;
  959. *((volatile int *) address) = value;
  960. return JIM_OK;
  961. }
  962. static int
  963. zylinjtag_Jim_Command_flash(Jim_Interp *interp,
  964. int argc,
  965. Jim_Obj * const *argv)
  966. {
  967. int retval;
  968. u32 base = 0;
  969. flash_bank_t *t = get_flash_bank_by_num_noprobe(0);
  970. if (t != NULL)
  971. {
  972. base = t->base;
  973. retval = JIM_OK;
  974. } else
  975. {
  976. retval = JIM_ERR;
  977. }
  978. if (retval == JIM_OK)
  979. {
  980. Jim_SetResult(interp, Jim_NewIntObj(interp, base));
  981. }
  982. return retval;
  983. }
  984. static int
  985. zylinjtag_Jim_Command_log(Jim_Interp *interp,
  986. int argc,
  987. Jim_Obj * const *argv)
  988. {
  989. Jim_Obj *tclOutput = Jim_NewStringObj(interp, "", 0);
  990. if (logCount >= logSize)
  991. {
  992. Jim_AppendString(httpstate.jim_interp, tclOutput, logBuffer+logCount%logSize, logSize-logCount%logSize);
  993. }
  994. Jim_AppendString(httpstate.jim_interp, tclOutput, logBuffer, writePtr);
  995. Jim_SetResult(interp, tclOutput);
  996. return JIM_OK;
  997. }
  998. static int
  999. zylinjtag_Jim_Command_reboot(Jim_Interp *interp,
  1000. int argc,
  1001. Jim_Obj * const *argv)
  1002. {
  1003. reboot();
  1004. return JIM_OK;
  1005. }
  1006. static int
  1007. zylinjtag_Jim_Command_mac(Jim_Interp *interp,
  1008. int argc,
  1009. Jim_Obj * const *argv)
  1010. {
  1011. Jim_Obj *tclOutput = Jim_NewStringObj(interp, "", 0);
  1012. Jim_AppendString(httpstate.jim_interp, tclOutput, hwaddr, strlen(hwaddr));
  1013. Jim_SetResult(interp, tclOutput);
  1014. return JIM_OK;
  1015. }
  1016. static int
  1017. zylinjtag_Jim_Command_ip(Jim_Interp *interp,
  1018. int argc,
  1019. Jim_Obj * const *argv)
  1020. {
  1021. Jim_Obj *tclOutput = Jim_NewStringObj(interp, "", 0);
  1022. struct ifaddrs *ifa = NULL, *ifp = NULL;
  1023. if (getifaddrs(&ifp) < 0)
  1024. {
  1025. return JIM_ERR;
  1026. }
  1027. for (ifa = ifp; ifa; ifa = ifa->ifa_next)
  1028. {
  1029. char ip[200];
  1030. socklen_t salen;
  1031. if (ifa->ifa_addr->sa_family == AF_INET)
  1032. salen = sizeof(struct sockaddr_in);
  1033. else if (ifa->ifa_addr->sa_family == AF_INET6)
  1034. salen = sizeof(struct sockaddr_in6);
  1035. else
  1036. continue;
  1037. if (getnameinfo(ifa->ifa_addr, salen, ip, sizeof(ip), NULL, 0,
  1038. NI_NUMERICHOST) < 0)
  1039. {
  1040. continue;
  1041. }
  1042. Jim_AppendString(httpstate.jim_interp, tclOutput, ip, strlen(ip));
  1043. break;
  1044. }
  1045. freeifaddrs(ifp);
  1046. Jim_SetResult(interp, tclOutput);
  1047. return JIM_OK;
  1048. }
  1049. extern Jim_Interp *interp;
  1050. static void zylinjtag_startNetwork()
  1051. {
  1052. // Bring TCP/IP up immediately before we're ready to accept commands.
  1053. //
  1054. // That is as soon as a PING responds, we're accepting telnet sessions.
  1055. #if defined(CYGPKG_NET_FREEBSD_STACK)
  1056. phi_init_all_network_interfaces();
  1057. #else
  1058. lwip_init();
  1059. #endif
  1060. if (!eth0_up)
  1061. {
  1062. diag_printf("Network not up and running\n");
  1063. exit(-1);
  1064. }
  1065. #if defined(CYGPKG_NET_FREEBSD_STACK)
  1066. /*start TFTP*/
  1067. tftpd_start(69, &fileops);
  1068. #endif
  1069. cyg_httpd_init_tcl_interpreter();
  1070. interp = httpstate.jim_interp;
  1071. Jim_CreateCommand(httpstate.jim_interp, "log", zylinjtag_Jim_Command_log, NULL, NULL);
  1072. Jim_CreateCommand(httpstate.jim_interp, "reboot", zylinjtag_Jim_Command_reboot, NULL, NULL);
  1073. Jim_CreateCommand(httpstate.jim_interp, "peek", zylinjtag_Jim_Command_peek, NULL, NULL);
  1074. Jim_CreateCommand(httpstate.jim_interp, "zy1000_flash", zylinjtag_Jim_Command_flash, NULL, NULL);
  1075. Jim_CreateCommand(httpstate.jim_interp, "poke", zylinjtag_Jim_Command_poke, NULL, NULL);
  1076. Jim_CreateCommand(httpstate.jim_interp, "ls", zylinjtag_Jim_Command_ls, NULL, NULL);
  1077. Jim_CreateCommand(httpstate.jim_interp, "threads", zylinjtag_Jim_Command_threads, NULL, NULL);
  1078. Jim_CreateCommand(httpstate.jim_interp, "getmem", zylinjtag_Jim_Command_getmem, NULL, NULL);
  1079. Jim_CreateCommand(httpstate.jim_interp, "mac", zylinjtag_Jim_Command_mac, NULL, NULL);
  1080. Jim_CreateCommand(httpstate.jim_interp, "ip", zylinjtag_Jim_Command_ip, NULL, NULL);
  1081. Jim_CreateCommand(httpstate.jim_interp, "rm", zylinjtag_Jim_Command_rm, NULL, NULL);
  1082. cyg_httpd_start();
  1083. webRunning = true;
  1084. diag_printf("Web server running\n");
  1085. int s;
  1086. struct ifreq ifr;
  1087. s = socket(AF_INET, SOCK_DGRAM, 0);
  1088. if (s >= 0)
  1089. {
  1090. strcpy(ifr.ifr_name, "eth0");
  1091. int res;
  1092. res = ioctl(s, SIOCGIFHWADDR, &ifr);
  1093. close(s);
  1094. if (res < 0)
  1095. {
  1096. diag_printf("Can't obtain MAC address\n");
  1097. reboot();
  1098. }
  1099. }
  1100. sprintf(hwaddr, "%02x:%02x:%02x:%02x:%02x:%02x",
  1101. (int) ((unsigned char *) &ifr.ifr_hwaddr.sa_data)[0],
  1102. (int) ((unsigned char *) &ifr.ifr_hwaddr.sa_data)[1],
  1103. (int) ((unsigned char *) &ifr.ifr_hwaddr.sa_data)[2],
  1104. (int) ((unsigned char *) &ifr.ifr_hwaddr.sa_data)[3],
  1105. (int) ((unsigned char *) &ifr.ifr_hwaddr.sa_data)[4],
  1106. (int) ((unsigned char *) &ifr.ifr_hwaddr.sa_data)[5]);
  1107. discover_message=alloc_printf("ZY1000 Zylin JTAG debugger MAC %s", hwaddr);
  1108. discover_launch();
  1109. }
  1110. static void
  1111. print_exception_handler(cyg_addrword_t data, cyg_code_t exception, cyg_addrword_t info)
  1112. {
  1113. writeLog = false;
  1114. serialLog = true;
  1115. char *infoStr = "unknown";
  1116. switch (exception)
  1117. {
  1118. #ifdef CYGNUM_HAL_VECTOR_UNDEF_INSTRUCTION
  1119. case CYGNUM_HAL_VECTOR_UNDEF_INSTRUCTION:
  1120. infoStr = "undefined instruction";
  1121. break;
  1122. case CYGNUM_HAL_VECTOR_SOFTWARE_INTERRUPT:
  1123. infoStr = "software interrupt";
  1124. break;
  1125. case CYGNUM_HAL_VECTOR_ABORT_PREFETCH:
  1126. infoStr = "abort prefetch";
  1127. break;
  1128. case CYGNUM_HAL_VECTOR_ABORT_DATA:
  1129. infoStr = "abort data";
  1130. break;
  1131. #endif
  1132. default:
  1133. break;
  1134. }
  1135. diag_printf("Exception: %08x(%s) %08x\n", exception, infoStr, info);
  1136. diag_printf("Dumping log\n---\n");
  1137. if (logCount >= logSize)
  1138. {
  1139. diag_write(logBuffer + logCount % logSize, logSize - logCount % logSize);
  1140. }
  1141. diag_write(logBuffer, writePtr);
  1142. diag_printf("---\nLogdump complete.\n");
  1143. diag_printf("Exception: %08x(%s) %08x\n", exception, infoStr, info);
  1144. diag_printf("\n---\nRebooting\n");
  1145. HAL_PLATFORM_RESET();
  1146. }
  1147. static void setHandler(cyg_code_t exception)
  1148. {
  1149. cyg_exception_handler_t *old_handler;
  1150. cyg_addrword_t old_data;
  1151. cyg_exception_set_handler(exception,
  1152. print_exception_handler,
  1153. 0,
  1154. &old_handler,
  1155. &old_data);
  1156. }
  1157. static cyg_thread zylinjtag_uart_thread_object;
  1158. static cyg_handle_t zylinjtag_uart_thread_handle;
  1159. static char uart_stack[4096];
  1160. static char forwardBuffer[1024]; // NB! must be smaller than a TCP/IP packet!!!!!
  1161. static char backwardBuffer[1024];
  1162. static cyg_io_handle_t serial_handle;
  1163. void setNoDelay(int session, int flag)
  1164. {
  1165. #if 1
  1166. // This decreases latency dramatically for e.g. GDB load which
  1167. // does not have a sliding window protocol
  1168. //
  1169. // Can cause *lots* of TCP/IP packets to be sent and it would have
  1170. // to be enabled/disabled on the fly to avoid the CPU being
  1171. // overloaded...
  1172. setsockopt(session, /* socket affected */
  1173. IPPROTO_TCP, /* set option at TCP level */
  1174. TCP_NODELAY, /* name of option */
  1175. (char *) &flag, /* the cast is historical
  1176. cruft */
  1177. sizeof(int)); /* length of option value */
  1178. #endif
  1179. }
  1180. struct
  1181. {
  1182. int req;
  1183. int actual;
  1184. int req2;
  1185. int actual2;
  1186. } tcpipSent[512 * 1024];
  1187. int cur;
  1188. static void
  1189. zylinjtag_uart(cyg_addrword_t data)
  1190. {
  1191. int so_reuseaddr_option = 1;
  1192. int fd;
  1193. if ((fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
  1194. {
  1195. LOG_ERROR("error creating socket: %s", strerror(errno));
  1196. exit(-1);
  1197. }
  1198. setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void*)&so_reuseaddr_option, sizeof(int));
  1199. struct sockaddr_in sin;
  1200. unsigned int address_size;
  1201. address_size = sizeof(sin);
  1202. memset(&sin, 0, sizeof(sin));
  1203. sin.sin_family = AF_INET;
  1204. sin.sin_addr.s_addr = INADDR_ANY;
  1205. sin.sin_port = htons(5555);
  1206. if (bind(fd, (struct sockaddr *) &sin, sizeof(sin)) == -1)
  1207. {
  1208. LOG_ERROR("couldn't bind to socket: %s", strerror(errno));
  1209. exit(-1);
  1210. }
  1211. if (listen(fd, 1) == -1)
  1212. {
  1213. LOG_ERROR("couldn't listen on socket: %s", strerror(errno));
  1214. exit(-1);
  1215. }
  1216. // socket_nonblock(fd);
  1217. for (;;)
  1218. {
  1219. int session = accept(fd, (struct sockaddr *) &sin, &address_size);
  1220. if (session < 0)
  1221. {
  1222. continue;
  1223. }
  1224. setNoDelay(session, 1);
  1225. int oldopts = fcntl(session, F_GETFL, 0);
  1226. fcntl(session, F_SETFL, oldopts | O_NONBLOCK); //
  1227. int serHandle = open("/dev/ser0", O_RDWR | O_NONBLOCK);
  1228. if (serHandle < 0)
  1229. {
  1230. close(session);
  1231. continue;
  1232. }
  1233. #ifdef CYGPKG_PROFILE_GPROF
  1234. start_profile();
  1235. #endif
  1236. int actual = 0;
  1237. int actual2 = 0;
  1238. int pos, pos2;
  1239. pos = 0;
  1240. pos2 = 0;
  1241. cur = 0;
  1242. for (;;)
  1243. {
  1244. fd_set write_fds;
  1245. fd_set read_fds;
  1246. FD_ZERO(&write_fds);
  1247. FD_ZERO(&read_fds);
  1248. int fd_max = -1;
  1249. FD_SET(session, &read_fds);
  1250. fd_max = session;
  1251. FD_SET(serHandle, &read_fds);
  1252. if (serHandle > fd_max)
  1253. {
  1254. fd_max = serHandle;
  1255. }
  1256. /* Wait... */
  1257. cyg_thread_delay(5); // 50ms fixed delay to wait for data to be sent/received
  1258. if ((actual == 0) && (actual2 == 0))
  1259. {
  1260. int retval = select(fd_max + 1, &read_fds, NULL, NULL, NULL);
  1261. if (retval <= 0)
  1262. {
  1263. break;
  1264. }
  1265. }
  1266. if (actual2 <= 0)
  1267. {
  1268. memset(backwardBuffer, 's', sizeof(backwardBuffer));
  1269. actual2=read(serHandle, backwardBuffer, sizeof(backwardBuffer));
  1270. if (actual2 < 0)
  1271. {
  1272. if (errno != EAGAIN)
  1273. {
  1274. goto closeSession;
  1275. }
  1276. actual2 = 0;
  1277. }
  1278. pos2 = 0;
  1279. }
  1280. int x = actual2;
  1281. int y = 0;
  1282. if (actual2 > 0)
  1283. {
  1284. int written = write(session, backwardBuffer + pos2, actual2);
  1285. if (written <= 0)
  1286. goto closeSession;
  1287. actual2 -= written;
  1288. pos2 += written;
  1289. y = written;
  1290. }
  1291. if (FD_ISSET(session, &read_fds)&&(sizeof(forwardBuffer)>actual))
  1292. {
  1293. // NB! Here it is important that we empty the TCP/IP read buffer
  1294. // to make transmission tick right
  1295. memmove(forwardBuffer, forwardBuffer + pos, actual);
  1296. pos = 0;
  1297. int t;
  1298. // this will block if there is no data at all
  1299. t=read_socket(session, forwardBuffer+actual, sizeof(forwardBuffer)-actual);
  1300. if (t <= 0)
  1301. {
  1302. goto closeSession;
  1303. }
  1304. actual += t;
  1305. }
  1306. int x2 = actual;
  1307. int y2 = 0;
  1308. if (actual > 0)
  1309. {
  1310. /* Do not put things into the serial buffer if it has something to send
  1311. * as that can cause a single byte to be sent at the time.
  1312. *
  1313. *
  1314. */
  1315. int written = write(serHandle, forwardBuffer + pos, actual);
  1316. if (written < 0)
  1317. {
  1318. if (errno != EAGAIN)
  1319. {
  1320. goto closeSession;
  1321. }
  1322. // The serial buffer is full
  1323. written = 0;
  1324. } else
  1325. {
  1326. actual -= written;
  1327. pos += written;
  1328. }
  1329. y2 = written;
  1330. }
  1331. if (cur < 1024)
  1332. {
  1333. tcpipSent[cur].req = x;
  1334. tcpipSent[cur].actual = y;
  1335. tcpipSent[cur].req2 = x2;
  1336. tcpipSent[cur].actual2 = y2;
  1337. cur++;
  1338. }
  1339. }
  1340. closeSession:
  1341. close(session);
  1342. close(serHandle);
  1343. int i;
  1344. for (i = 0; i < 1024; i++)
  1345. {
  1346. diag_printf("%d %d %d %d\n", tcpipSent[i].req, tcpipSent[i].actual, tcpipSent[i].req2, tcpipSent[i].actual2);
  1347. }
  1348. }
  1349. close(fd);
  1350. }
  1351. void startUart(void)
  1352. {
  1353. cyg_thread_create(1,
  1354. zylinjtag_uart,
  1355. (cyg_addrword_t)0,
  1356. "uart thread",
  1357. (void *)uart_stack,
  1358. sizeof(uart_stack),
  1359. &zylinjtag_uart_thread_handle,
  1360. &zylinjtag_uart_thread_object);
  1361. cyg_thread_set_priority(zylinjtag_uart_thread_handle, 1); // low priority as it sits in a busy loop
  1362. cyg_thread_resume(zylinjtag_uart_thread_handle);
  1363. }
  1364. int handle_uart_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1365. {
  1366. static int current_baud = 38400;
  1367. if (argc == 0)
  1368. {
  1369. command_print(cmd_ctx, "%d", current_baud);
  1370. return ERROR_OK;
  1371. } else if (argc != 1)
  1372. {
  1373. return ERROR_INVALID_ARGUMENTS;
  1374. }
  1375. current_baud = atol(args[0]);
  1376. int baud;
  1377. switch (current_baud)
  1378. {
  1379. case 9600:
  1380. baud = CYGNUM_SERIAL_BAUD_9600;
  1381. break;
  1382. case 19200:
  1383. baud = CYGNUM_SERIAL_BAUD_19200;
  1384. break;
  1385. case 38400:
  1386. baud = CYGNUM_SERIAL_BAUD_38400;
  1387. break;
  1388. case 57600:
  1389. baud = CYGNUM_SERIAL_BAUD_57600;
  1390. break;
  1391. case 115200:
  1392. baud = CYGNUM_SERIAL_BAUD_115200;
  1393. break;
  1394. case 230400:
  1395. baud = CYGNUM_SERIAL_BAUD_230400;
  1396. break;
  1397. default:
  1398. command_print(cmd_ctx, "unsupported baudrate");
  1399. return ERROR_INVALID_ARGUMENTS;
  1400. }
  1401. cyg_serial_info_t buf;
  1402. cyg_uint32 len = 1;
  1403. //get existing serial configuration
  1404. len = sizeof(cyg_serial_info_t);
  1405. int err;
  1406. err = cyg_io_get_config(serial_handle, CYG_IO_GET_CONFIG_SERIAL_OUTPUT_DRAIN, &buf, &len);
  1407. err = cyg_io_get_config(serial_handle, CYG_IO_GET_CONFIG_SERIAL_INFO, &buf, &len);
  1408. if (err != ENOERR)
  1409. {
  1410. command_print(cmd_ctx, "Failed to get serial port settings %d", err);
  1411. return ERROR_OK;
  1412. }
  1413. buf.baud = baud;
  1414. err = cyg_io_set_config(serial_handle, CYG_IO_SET_CONFIG_SERIAL_INFO, &buf, &len);
  1415. if (err != ENOERR)
  1416. {
  1417. command_print(cmd_ctx, "Failed to set serial port settings %d", err);
  1418. return ERROR_OK;
  1419. }
  1420. return ERROR_OK;
  1421. }
  1422. bool logAllToSerial = false;
  1423. /* boolean parameter stored on config */
  1424. bool boolParam(char *var)
  1425. {
  1426. bool result = false;
  1427. char *name = alloc_printf(ZYLIN_CONFIG_DIR "/%s", var);
  1428. if (name == NULL)
  1429. return result;
  1430. void *data;
  1431. int len;
  1432. if (loadFile(name, &data, &len) == ERROR_OK)
  1433. {
  1434. if (len > 1)
  1435. len = 1;
  1436. result = strncmp((char *) data, "1", len) == 0;
  1437. free(data);
  1438. }
  1439. free(name);
  1440. return result;
  1441. }
  1442. command_context_t *setup_command_handler();
  1443. int add_default_dirs(void)
  1444. {
  1445. add_script_search_dir(ZYLIN_CONFIG_DIR);
  1446. add_script_search_dir("/rom/lib/openocd");
  1447. add_script_search_dir("/rom");
  1448. return ERROR_OK;
  1449. }
  1450. static cyg_uint8 *ramblockdevice;
  1451. static const int ramblockdevice_size=4096*1024;
  1452. int main(int argc, char *argv[])
  1453. {
  1454. /* ramblockdevice will be the same address every time. The deflate app uses a buffer 16mBytes out, so we
  1455. * need to allocate towards the end of the heap. */
  1456. ramblockdevice=(cyg_uint8 *)malloc(ramblockdevice_size);
  1457. memset(ramblockdevice, 0xff, ramblockdevice_size);
  1458. #ifdef CYGNUM_HAL_VECTOR_UNDEF_INSTRUCTION
  1459. setHandler(CYGNUM_HAL_VECTOR_UNDEF_INSTRUCTION);
  1460. setHandler(CYGNUM_HAL_VECTOR_ABORT_PREFETCH);
  1461. setHandler(CYGNUM_HAL_VECTOR_ABORT_DATA);
  1462. #endif
  1463. int err;
  1464. err = cyg_io_lookup("/dev/ser0", &serial_handle);
  1465. if (err != ENOERR)
  1466. {
  1467. diag_printf("/dev/ser0 not found\n");
  1468. reboot();
  1469. }
  1470. setPower(true); // on by default
  1471. atexit(keep_webserver);
  1472. err = mount("", "/ram", "ramfs");
  1473. if (err < 0)
  1474. {
  1475. diag_printf("unable to mount ramfs\n");
  1476. }
  1477. chdir("/ram");
  1478. char address[16];
  1479. sprintf(address, "%p", &filedata[0]);
  1480. err = mount(address, "/rom", "romfs");
  1481. if (err < 0)
  1482. {
  1483. diag_printf("unable to mount /rom\n");
  1484. }
  1485. err = mount("", "/log", "logfs");
  1486. if (err < 0)
  1487. {
  1488. diag_printf("unable to mount logfs\n");
  1489. }
  1490. err = mount("", "/tftp", "tftpfs");
  1491. if (err < 0)
  1492. {
  1493. diag_printf("unable to mount logfs\n");
  1494. }
  1495. log = fopen("/log/log", "w");
  1496. if (log == NULL)
  1497. {
  1498. diag_printf("Could not open log file /ram/log\n");
  1499. exit(-1);
  1500. }
  1501. diag_init_putc(_zylinjtag_diag_write_char);
  1502. // We want this in the log.
  1503. diag_printf("Zylin ZY1000. Copyright Zylin AS 2007-2008.\n");
  1504. diag_printf("%s\n", ZYLIN_OPENOCD_VERSION);
  1505. copydir("/rom", "/ram/cgi");
  1506. err = mount("/dev/flash1", "/config", "jffs2");
  1507. if (err < 0)
  1508. {
  1509. diag_printf("unable to mount jffs\n");
  1510. reboot();
  1511. }
  1512. /* are we using a ram disk instead of a flash disk? This is used
  1513. * for ZY1000 live demo...
  1514. *
  1515. * copy over flash disk to ram block device
  1516. */
  1517. if (boolParam("ramdisk"))
  1518. {
  1519. diag_printf("Unmounting /config from flash and using ram instead\n");
  1520. err=umount("/config");
  1521. if (err < 0)
  1522. {
  1523. diag_printf("unable to unmount jffs\n");
  1524. reboot();
  1525. }
  1526. err = mount("/dev/flash1", "/config2", "jffs2");
  1527. if (err < 0)
  1528. {
  1529. diag_printf("unable to mount jffs\n");
  1530. reboot();
  1531. }
  1532. err = mount("/dev/ram", "/config", "jffs2");
  1533. if (err < 0)
  1534. {
  1535. diag_printf("unable to mount ram block device\n");
  1536. reboot();
  1537. }
  1538. // copydir("/config2", "/config");
  1539. copyfile("/config2/ip", "/config/ip");
  1540. copydir("/config2/settings", "/config/settings");
  1541. umount("/config2");
  1542. } else
  1543. {
  1544. /* we're not going to use a ram block disk */
  1545. free(ramblockdevice);
  1546. }
  1547. mkdir(ZYLIN_CONFIG_DIR, 0777);
  1548. mkdir(ZYLIN_CONFIG_DIR "/target", 0777);
  1549. mkdir(ZYLIN_CONFIG_DIR "/event", 0777);
  1550. logAllToSerial = boolParam("logserial");
  1551. // We need the network & web server in case there is something wrong with
  1552. // the config files that invoke exit()
  1553. zylinjtag_startNetwork();
  1554. /* we're going to access the jim interpreter from here on... */
  1555. openocd_sleep_postlude();
  1556. startUart();
  1557. add_default_dirs();
  1558. /* initialize commandline interface */
  1559. command_context_t *cmd_ctx;
  1560. cmd_ctx = setup_command_handler();
  1561. command_set_output_handler(cmd_ctx, configuration_output_handler, NULL);
  1562. command_context_mode(cmd_ctx, COMMAND_CONFIG);
  1563. register_command(cmd_ctx, NULL, "zy1000_version", handle_zy1000_version_command,
  1564. COMMAND_EXEC, "show zy1000 version numbers");
  1565. register_command(cmd_ctx, NULL, "rm", handle_rm_command, COMMAND_ANY,
  1566. "remove file");
  1567. register_command(cmd_ctx, NULL, "fast_load_image", handle_fast_load_image_command, COMMAND_ANY,
  1568. "same args as load_image, image stored in memory");
  1569. register_command(cmd_ctx, NULL, "fast_load", handle_fast_load_command, COMMAND_ANY,
  1570. "loads active fast load image to current target");
  1571. register_command(cmd_ctx, NULL, "cat", handle_cat_command, COMMAND_ANY,
  1572. "display file content");
  1573. register_command(cmd_ctx, NULL, "trunc", handle_trunc_command, COMMAND_ANY,
  1574. "truncate a file to 0 size");
  1575. register_command(cmd_ctx, NULL, "append_file", handle_append_command,
  1576. COMMAND_ANY, "append a variable number of strings to a file");
  1577. register_command(cmd_ctx, NULL, "power", handle_power_command, COMMAND_ANY,
  1578. "power <on/off> - turn power switch to target on/off. No arguments - print status.");
  1579. register_command(cmd_ctx, NULL, "meminfo", handle_meminfo_command,
  1580. COMMAND_ANY, "display available ram memory");
  1581. register_command(cmd_ctx, NULL, "cp", handle_cp_command,
  1582. COMMAND_ANY, "copy a file <from> <to>");
  1583. #ifdef CYGPKG_PROFILE_GPROF
  1584. register_command(cmd_ctx, NULL, "ecosboard_profile", eCosBoard_handle_eCosBoard_profile_command,
  1585. COMMAND_ANY, NULL);
  1586. #endif
  1587. register_command(cmd_ctx, NULL, "uart", handle_uart_command,
  1588. COMMAND_ANY, "uart <baud> - forward uart on port 5555");
  1589. int errVal;
  1590. errVal = log_init(cmd_ctx);
  1591. if (errVal != ERROR_OK)
  1592. {
  1593. diag_printf("log_init() failed %d\n", errVal);
  1594. exit(-1);
  1595. }
  1596. set_log_output(cmd_ctx, log);
  1597. LOG_DEBUG("log init complete");
  1598. // diag_printf("Executing config files\n");
  1599. if (logAllToSerial)
  1600. {
  1601. diag_printf(ZYLIN_CONFIG_DIR "/logserial=1 => sending log output to serial port using \"debug_level 3\" as default.\n");
  1602. command_run_line(cmd_ctx, "debug_level 3");
  1603. }
  1604. zylinjtag_parse_config_file(cmd_ctx, "/rom/openocd.cfg");
  1605. // FIX!!! Yuk!
  1606. // diag_printf() is really invoked from many more places than we trust it
  1607. // not to cause instabilities(e.g. invoking fputc() from an interrupt is *BAD*).
  1608. //
  1609. // Disabling it here is safe and gives us enough logged debug output for now. Crossing
  1610. // fingers that it doesn't cause any crashes.
  1611. diag_printf("Init complete, GDB & telnet servers launched.\n");
  1612. command_set_output_handler(cmd_ctx, zy1000_configuration_output_handler_log, NULL);
  1613. if (!logAllToSerial)
  1614. {
  1615. serialLog = false;
  1616. }
  1617. /* handle network connections */
  1618. server_loop(cmd_ctx);
  1619. openocd_sleep_prelude();
  1620. /* shut server down */
  1621. server_quit();
  1622. /* free commandline interface */
  1623. command_done(cmd_ctx);
  1624. umount("/config");
  1625. exit(0);
  1626. for (;;);
  1627. }
  1628. cyg_int32
  1629. cyg_httpd_exec_cgi_tcl(char *file_name);
  1630. cyg_int32 homeForm(CYG_HTTPD_STATE *p)
  1631. {
  1632. cyg_httpd_exec_cgi_tcl("/ram/cgi/index.tcl");
  1633. return 0;
  1634. }
  1635. CYG_HTTPD_HANDLER_TABLE_ENTRY(root_label, "/", homeForm);
  1636. CYG_HTTPD_MIME_TABLE_ENTRY(text_mime_label, "text", "text/plain");
  1637. CYG_HTTPD_MIME_TABLE_ENTRY(bin_mime_label, "bin", "application/octet-stream");
  1638. #include <pkgconf/system.h>
  1639. #include <pkgconf/hal.h>
  1640. #include <pkgconf/kernel.h>
  1641. #include <pkgconf/io_fileio.h>
  1642. #include <pkgconf/fs_rom.h>
  1643. #include <cyg/kernel/ktypes.h> // base kernel types
  1644. #include <cyg/infra/cyg_trac.h> // tracing macros
  1645. #include <cyg/infra/cyg_ass.h> // assertion macros
  1646. #include <unistd.h>
  1647. #include <sys/types.h>
  1648. #include <fcntl.h>
  1649. #include <sys/stat.h>
  1650. #include <errno.h>
  1651. #include <dirent.h>
  1652. #include <stdarg.h>
  1653. #include <stdio.h>
  1654. #include <stdlib.h>
  1655. #include <string.h>
  1656. #include <cyg/fileio/fileio.h>
  1657. #include <cyg/kernel/kapi.h>
  1658. #include <cyg/infra/diag.h>
  1659. //==========================================================================
  1660. // Eventually we want to eXecute In Place from the ROM in a protected
  1661. // environment, so we'll need executables to be aligned to a boundary
  1662. // suitable for MMU protection. A suitable boundary would be the 4k
  1663. // boundary in all the CPU architectures I am currently aware of.
  1664. // Forward definitions
  1665. // Filesystem operations
  1666. static int tftpfs_mount(cyg_fstab_entry *fste, cyg_mtab_entry *mte);
  1667. static int tftpfs_umount(cyg_mtab_entry *mte);
  1668. static int tftpfs_open(cyg_mtab_entry *mte, cyg_dir dir, const char *name,
  1669. int mode, cyg_file *fte);
  1670. static int tftpfs_fo_read(struct CYG_FILE_TAG *fp, struct CYG_UIO_TAG *uio);
  1671. static int tftpfs_fo_write(struct CYG_FILE_TAG *fp, struct CYG_UIO_TAG *uio);
  1672. // File operations
  1673. static int tftpfs_fo_fsync(struct CYG_FILE_TAG *fp, int mode);
  1674. static int tftpfs_fo_close(struct CYG_FILE_TAG *fp);
  1675. static int tftpfs_fo_lseek(struct CYG_FILE_TAG *fp, off_t *apos, int whence);
  1676. //==========================================================================
  1677. // Filesystem table entries
  1678. // -------------------------------------------------------------------------
  1679. // Fstab entry.
  1680. // This defines the entry in the filesystem table.
  1681. // For simplicity we use _FILESYSTEM synchronization for all accesses since
  1682. // we should never block in any filesystem operations.
  1683. #if 1
  1684. FSTAB_ENTRY( tftpfs_fste, "tftpfs", 0,
  1685. CYG_SYNCMODE_NONE,
  1686. tftpfs_mount,
  1687. tftpfs_umount,
  1688. tftpfs_open,
  1689. (cyg_fsop_unlink *)cyg_fileio_erofs,
  1690. (cyg_fsop_mkdir *)cyg_fileio_erofs,
  1691. (cyg_fsop_rmdir *)cyg_fileio_erofs,
  1692. (cyg_fsop_rename *)cyg_fileio_erofs,
  1693. (cyg_fsop_link *)cyg_fileio_erofs,
  1694. (cyg_fsop_opendir *)cyg_fileio_erofs,
  1695. (cyg_fsop_chdir *)cyg_fileio_erofs,
  1696. (cyg_fsop_stat *)cyg_fileio_erofs,
  1697. (cyg_fsop_getinfo *)cyg_fileio_erofs,
  1698. (cyg_fsop_setinfo *)cyg_fileio_erofs);
  1699. #endif
  1700. // -------------------------------------------------------------------------
  1701. // mtab entry.
  1702. // This defines a single ROMFS loaded into ROM at the configured address
  1703. //
  1704. // MTAB_ENTRY( rom_mte, // structure name
  1705. // "/rom", // mount point
  1706. // "romfs", // FIlesystem type
  1707. // "", // hardware device
  1708. // (CYG_ADDRWORD) CYGNUM_FS_ROM_BASE_ADDRESS // Address in ROM
  1709. // );
  1710. // -------------------------------------------------------------------------
  1711. // File operations.
  1712. // This set of file operations are used for normal open files.
  1713. static cyg_fileops tftpfs_fileops =
  1714. {
  1715. tftpfs_fo_read,
  1716. tftpfs_fo_write,
  1717. tftpfs_fo_lseek,
  1718. (cyg_fileop_ioctl *)cyg_fileio_erofs,
  1719. cyg_fileio_seltrue,
  1720. tftpfs_fo_fsync,
  1721. tftpfs_fo_close,
  1722. (cyg_fileop_fstat *) cyg_fileio_erofs,
  1723. (cyg_fileop_getinfo *) cyg_fileio_erofs,
  1724. (cyg_fileop_setinfo *)cyg_fileio_erofs,
  1725. };
  1726. // -------------------------------------------------------------------------
  1727. // tftpfs_mount()
  1728. // Process a mount request. This mainly finds root for the
  1729. // filesystem.
  1730. static int tftpfs_mount(cyg_fstab_entry *fste, cyg_mtab_entry *mte)
  1731. {
  1732. return ENOERR;
  1733. }
  1734. static int tftpfs_umount(cyg_mtab_entry *mte)
  1735. {
  1736. return ENOERR;
  1737. }
  1738. struct Tftp
  1739. {
  1740. int write;
  1741. int readFile;
  1742. cyg_uint8 *mem;
  1743. int actual;
  1744. char *server;
  1745. char *file;
  1746. };
  1747. static void freeTftp(struct Tftp *t)
  1748. {
  1749. if (t == NULL)
  1750. return;
  1751. if (t->mem)
  1752. free(t->mem);
  1753. if (t->server)
  1754. free(t->server);
  1755. if (t->file)
  1756. free(t->file);
  1757. free(t);
  1758. }
  1759. static const int tftpMaxSize = 8192 * 1024;
  1760. static int tftpfs_open(cyg_mtab_entry *mte, cyg_dir dir, const char *name,
  1761. int mode, cyg_file *file)
  1762. {
  1763. struct Tftp *tftp;
  1764. tftp = malloc(sizeof(struct Tftp));
  1765. if (tftp == NULL)
  1766. return EMFILE;
  1767. memset(tftp, 0, sizeof(struct Tftp));
  1768. file->f_flag |= mode & CYG_FILE_MODE_MASK;
  1769. file->f_type = CYG_FILE_TYPE_FILE;
  1770. file->f_ops = &tftpfs_fileops;
  1771. file->f_offset = 0;
  1772. file->f_data = 0;
  1773. file->f_xops = 0;
  1774. tftp->mem = malloc(tftpMaxSize);
  1775. if (tftp->mem == NULL)
  1776. {
  1777. freeTftp(tftp);
  1778. return EMFILE;
  1779. }
  1780. char *server = strchr(name, '/');
  1781. if (server == NULL)
  1782. {
  1783. freeTftp(tftp);
  1784. return EMFILE;
  1785. }
  1786. tftp->server = malloc(server - name + 1);
  1787. if (tftp->server == NULL)
  1788. {
  1789. freeTftp(tftp);
  1790. return EMFILE;
  1791. }
  1792. strncpy(tftp->server, name, server - name);
  1793. tftp->server[server - name] = 0;
  1794. tftp->file = strdup(server + 1);
  1795. if (tftp->file == NULL)
  1796. {
  1797. freeTftp(tftp);
  1798. return EMFILE;
  1799. }
  1800. file->f_data = (CYG_ADDRWORD) tftp;
  1801. return ENOERR;
  1802. }
  1803. static int fetchTftp(struct Tftp *tftp)
  1804. {
  1805. if (!tftp->readFile)
  1806. {
  1807. int err;
  1808. tftp->actual = tftp_client_get( tftp->file, tftp->server, 0, tftp->mem, tftpMaxSize, TFTP_OCTET, &err);
  1809. if (tftp->actual < 0)
  1810. {
  1811. return EMFILE;
  1812. }
  1813. tftp->readFile = 1;
  1814. }
  1815. return ENOERR;
  1816. }
  1817. // -------------------------------------------------------------------------
  1818. // tftpfs_fo_write()
  1819. // Read data from file.
  1820. static int
  1821. tftpfs_fo_read(struct CYG_FILE_TAG *fp, struct CYG_UIO_TAG *uio)
  1822. {
  1823. struct Tftp *tftp = (struct Tftp *) fp->f_data;
  1824. if (fetchTftp(tftp) != ENOERR)
  1825. return EMFILE;
  1826. int i;
  1827. off_t pos = fp->f_offset;
  1828. int resid = 0;
  1829. for (i = 0; i < uio->uio_iovcnt; i++)
  1830. {
  1831. cyg_iovec *iov = &uio->uio_iov[i];
  1832. char *buf = (char *) iov->iov_base;
  1833. off_t len = iov->iov_len;
  1834. if (len + pos > tftp->actual)
  1835. {
  1836. len = tftp->actual - pos;
  1837. }
  1838. resid += iov->iov_len - len;
  1839. memcpy(buf, tftp->mem + pos, len);
  1840. pos += len;
  1841. }
  1842. uio->uio_resid = resid;
  1843. fp->f_offset = pos;
  1844. return ENOERR;
  1845. }
  1846. static int
  1847. tftpfs_fo_write(struct CYG_FILE_TAG *fp, struct CYG_UIO_TAG *uio)
  1848. {
  1849. struct Tftp *tftp = (struct Tftp *) fp->f_data;
  1850. int i;
  1851. off_t pos = fp->f_offset;
  1852. int resid = 0;
  1853. for (i = 0; i < uio->uio_iovcnt; i++)
  1854. {
  1855. cyg_iovec *iov = &uio->uio_iov[i];
  1856. char *buf = (char *) iov->iov_base;
  1857. off_t len = iov->iov_len;
  1858. if (len + pos > tftpMaxSize)
  1859. {
  1860. len = tftpMaxSize - pos;
  1861. }
  1862. resid += iov->iov_len - len;
  1863. memcpy(tftp->mem + pos, buf, len);
  1864. pos += len;
  1865. }
  1866. uio->uio_resid = resid;
  1867. fp->f_offset = pos;
  1868. tftp->write = 1;
  1869. return ENOERR;
  1870. }
  1871. static int
  1872. tftpfs_fo_fsync(struct CYG_FILE_TAG *fp, int mode)
  1873. {
  1874. int error = ENOERR;
  1875. return error;
  1876. }
  1877. // -------------------------------------------------------------------------
  1878. // romfs_fo_close()
  1879. // Close a file. We just clear out the data pointer.
  1880. static int tftpfs_fo_close(struct CYG_FILE_TAG *fp)
  1881. {
  1882. struct Tftp *tftp = (struct Tftp *) fp->f_data;
  1883. int error = ENOERR;
  1884. if (tftp->write)
  1885. {
  1886. tftp_client_put( tftp->file, tftp->server, 0, tftp->mem, fp->f_offset, TFTP_OCTET, &error);
  1887. }
  1888. freeTftp(tftp);
  1889. fp->f_data = 0;
  1890. return error;
  1891. }
  1892. // -------------------------------------------------------------------------
  1893. // romfs_fo_lseek()
  1894. // Seek to a new file position.
  1895. static int tftpfs_fo_lseek(struct CYG_FILE_TAG *fp, off_t *apos, int whence)
  1896. {
  1897. struct Tftp *tftp = (struct Tftp *) fp->f_data;
  1898. off_t pos = *apos;
  1899. if (fetchTftp(tftp) != ENOERR)
  1900. return EMFILE;
  1901. switch (whence)
  1902. {
  1903. case SEEK_SET:
  1904. // Pos is already where we want to be.
  1905. break;
  1906. case SEEK_CUR:
  1907. // Add pos to current offset.
  1908. pos += fp->f_offset;
  1909. break;
  1910. case SEEK_END:
  1911. // Add pos to file size.
  1912. pos += tftp->actual;
  1913. break;
  1914. default:
  1915. return EINVAL;
  1916. }
  1917. // Check that pos is still within current file size, or at the
  1918. // very end.
  1919. if (pos < 0 || pos > tftp->actual)
  1920. return EINVAL;
  1921. // All OK, set fp offset and return new position.
  1922. *apos = fp->f_offset = pos;
  1923. return ENOERR;
  1924. }
  1925. void usleep(int us)
  1926. {
  1927. if (us > 10000)
  1928. cyg_thread_delay(us / 10000 + 1);
  1929. else
  1930. HAL_DELAY_US(us);
  1931. }
  1932. // Chunked version.
  1933. cyg_int32
  1934. show_log_entry(CYG_HTTPD_STATE *phttpstate)
  1935. {
  1936. cyg_httpd_start_chunked("text");
  1937. if (logCount >= logSize)
  1938. {
  1939. cyg_httpd_write_chunked(logBuffer+logCount%logSize, logSize-logCount%logSize);
  1940. }
  1941. cyg_httpd_write_chunked(logBuffer, writePtr);
  1942. cyg_httpd_end_chunked();
  1943. return -1;
  1944. }
  1945. CYG_HTTPD_HANDLER_TABLE_ENTRY(show_log, "/ram/log", show_log_entry);
  1946. // Filesystem operations
  1947. static int logfs_mount(cyg_fstab_entry *fste, cyg_mtab_entry *mte);
  1948. static int logfs_umount(cyg_mtab_entry *mte);
  1949. static int logfs_open(cyg_mtab_entry *mte, cyg_dir dir, const char *name,
  1950. int mode, cyg_file *fte);
  1951. static int
  1952. logfs_fo_write(struct CYG_FILE_TAG *fp, struct CYG_UIO_TAG *uio);
  1953. // File operations
  1954. static int logfs_fo_fsync(struct CYG_FILE_TAG *fp, int mode);
  1955. static int logfs_fo_close(struct CYG_FILE_TAG *fp);
  1956. #include <cyg/io/devtab.h>
  1957. //==========================================================================
  1958. // Filesystem table entries
  1959. // -------------------------------------------------------------------------
  1960. // Fstab entry.
  1961. // This defines the entry in the filesystem table.
  1962. // For simplicity we use _FILESYSTEM synchronization for all accesses since
  1963. // we should never block in any filesystem operations.
  1964. FSTAB_ENTRY( logfs_fste, "logfs", 0,
  1965. CYG_SYNCMODE_FILE_FILESYSTEM|CYG_SYNCMODE_IO_FILESYSTEM,
  1966. logfs_mount,
  1967. logfs_umount,
  1968. logfs_open,
  1969. (cyg_fsop_unlink *)cyg_fileio_erofs,
  1970. (cyg_fsop_mkdir *)cyg_fileio_erofs,
  1971. (cyg_fsop_rmdir *)cyg_fileio_erofs,
  1972. (cyg_fsop_rename *)cyg_fileio_erofs,
  1973. (cyg_fsop_link *)cyg_fileio_erofs,
  1974. (cyg_fsop_opendir *)cyg_fileio_erofs,
  1975. (cyg_fsop_chdir *)cyg_fileio_erofs,
  1976. (cyg_fsop_stat *)cyg_fileio_erofs,
  1977. (cyg_fsop_getinfo *)cyg_fileio_erofs,
  1978. (cyg_fsop_setinfo *)cyg_fileio_erofs);
  1979. // -------------------------------------------------------------------------
  1980. // File operations.
  1981. // This set of file operations are used for normal open files.
  1982. static cyg_fileops logfs_fileops =
  1983. {
  1984. (cyg_fileop_read *)cyg_fileio_erofs,
  1985. (cyg_fileop_write *)logfs_fo_write,
  1986. (cyg_fileop_lseek *) cyg_fileio_erofs,
  1987. (cyg_fileop_ioctl *)cyg_fileio_erofs,
  1988. cyg_fileio_seltrue,
  1989. logfs_fo_fsync,
  1990. logfs_fo_close,
  1991. (cyg_fileop_fstat *)cyg_fileio_erofs,
  1992. (cyg_fileop_getinfo *) cyg_fileio_erofs,
  1993. (cyg_fileop_setinfo *)cyg_fileio_erofs,
  1994. };
  1995. // -------------------------------------------------------------------------
  1996. // logfs_mount()
  1997. // Process a mount request. This mainly finds root for the
  1998. // filesystem.
  1999. static int logfs_mount(cyg_fstab_entry *fste, cyg_mtab_entry *mte)
  2000. {
  2001. return ENOERR;
  2002. }
  2003. static int logfs_umount(cyg_mtab_entry *mte)
  2004. {
  2005. return ENOERR;
  2006. }
  2007. static int logfs_open(cyg_mtab_entry *mte, cyg_dir dir, const char *name,
  2008. int mode, cyg_file *file)
  2009. {
  2010. file->f_flag |= mode & CYG_FILE_MODE_MASK;
  2011. file->f_type = CYG_FILE_TYPE_FILE;
  2012. file->f_ops = &logfs_fileops;
  2013. file->f_offset = 0;
  2014. file->f_data = 0;
  2015. file->f_xops = 0;
  2016. return ENOERR;
  2017. }
  2018. // -------------------------------------------------------------------------
  2019. // logfs_fo_write()
  2020. // Write data to file.
  2021. static int
  2022. logfs_fo_write(struct CYG_FILE_TAG *fp, struct CYG_UIO_TAG *uio)
  2023. {
  2024. int i;
  2025. for (i = 0; i < uio->uio_iovcnt; i++)
  2026. {
  2027. cyg_iovec *iov = &uio->uio_iov[i];
  2028. char *buf = (char *) iov->iov_base;
  2029. off_t len = iov->iov_len;
  2030. diag_write(buf, len);
  2031. }
  2032. uio->uio_resid = 0;
  2033. return ENOERR;
  2034. }
  2035. static int
  2036. logfs_fo_fsync(struct CYG_FILE_TAG *fp, int mode)
  2037. {
  2038. return ENOERR;
  2039. }
  2040. // -------------------------------------------------------------------------
  2041. // romfs_fo_close()
  2042. // Close a file. We just clear out the data pointer.
  2043. static int logfs_fo_close(struct CYG_FILE_TAG *fp)
  2044. {
  2045. return ENOERR;
  2046. }
  2047. static bool
  2048. ramiodev_init( struct cyg_devtab_entry *tab )
  2049. {
  2050. return true;
  2051. }
  2052. static Cyg_ErrNo
  2053. ramiodev_bread( cyg_io_handle_t handle, void *buf, cyg_uint32 *len,
  2054. cyg_uint32 pos)
  2055. {
  2056. if (*len+pos>ramblockdevice_size)
  2057. {
  2058. *len=ramblockdevice_size-pos;
  2059. }
  2060. memcpy(buf, ramblockdevice+pos, *len);
  2061. return ENOERR;
  2062. }
  2063. static Cyg_ErrNo
  2064. ramiodev_bwrite( cyg_io_handle_t handle, const void *buf, cyg_uint32 *len,
  2065. cyg_uint32 pos )
  2066. {
  2067. if (((pos%4)!=0)||(((*len)%4)!=0))
  2068. {
  2069. diag_printf("Unaligned write %d %d!", pos, *len);
  2070. }
  2071. memcpy(ramblockdevice+pos, buf, *len);
  2072. return ENOERR;
  2073. }
  2074. static Cyg_ErrNo
  2075. ramiodev_get_config( cyg_io_handle_t handle,
  2076. cyg_uint32 key,
  2077. void* buf,
  2078. cyg_uint32* len)
  2079. {
  2080. switch (key) {
  2081. case CYG_IO_GET_CONFIG_FLASH_ERASE:
  2082. {
  2083. if ( *len != sizeof( cyg_io_flash_getconfig_erase_t ) )
  2084. return -EINVAL;
  2085. {
  2086. cyg_io_flash_getconfig_erase_t *e = (cyg_io_flash_getconfig_erase_t *)buf;
  2087. char *startpos = ramblockdevice + e->offset;
  2088. if (((e->offset%(64*1024))!=0)||((e->len%(64*1024))!=0))
  2089. {
  2090. diag_printf("Erease is not aligned %d %d\n", e->offset, e->len);
  2091. }
  2092. memset(startpos, 0xff, e->len);
  2093. e->flasherr = 0;
  2094. }
  2095. return ENOERR;
  2096. }
  2097. case CYG_IO_GET_CONFIG_FLASH_DEVSIZE:
  2098. {
  2099. if ( *len != sizeof( cyg_io_flash_getconfig_devsize_t ) )
  2100. return -EINVAL;
  2101. {
  2102. cyg_io_flash_getconfig_devsize_t *d =
  2103. (cyg_io_flash_getconfig_devsize_t *)buf;
  2104. d->dev_size = ramblockdevice_size;
  2105. }
  2106. return ENOERR;
  2107. }
  2108. case CYG_IO_GET_CONFIG_FLASH_BLOCKSIZE:
  2109. {
  2110. cyg_io_flash_getconfig_blocksize_t *b =
  2111. (cyg_io_flash_getconfig_blocksize_t *)buf;
  2112. if ( *len != sizeof( cyg_io_flash_getconfig_blocksize_t ) )
  2113. return -EINVAL;
  2114. // offset unused for now
  2115. b->block_size = 64*1024;
  2116. return ENOERR;
  2117. }
  2118. default:
  2119. return -EINVAL;
  2120. }
  2121. }
  2122. static Cyg_ErrNo
  2123. ramiodev_set_config( cyg_io_handle_t handle,
  2124. cyg_uint32 key,
  2125. const void* buf,
  2126. cyg_uint32* len)
  2127. {
  2128. switch (key) {
  2129. default:
  2130. return -EINVAL;
  2131. }
  2132. } // ramiodev_set_config()
  2133. // get_config/set_config should be added later to provide the other flash
  2134. // operations possible, like erase etc.
  2135. BLOCK_DEVIO_TABLE( cyg_io_ramdev1_ops,
  2136. &ramiodev_bwrite,
  2137. &ramiodev_bread,
  2138. 0, // no select
  2139. &ramiodev_get_config,
  2140. &ramiodev_set_config
  2141. );
  2142. BLOCK_DEVTAB_ENTRY( cyg_io_ramdev1,
  2143. "/dev/ram",
  2144. 0,
  2145. &cyg_io_ramdev1_ops,
  2146. &ramiodev_init,
  2147. 0, // No lookup required
  2148. NULL );