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.

ecosboard.c 36 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560
  1. /***************************************************************************
  2. * Copyright (C) 2007-2008 by Øyvind Harboe *
  3. * *
  4. * This program is free software; you can redistribute it and/or modify *
  5. * it under the terms of the GNU General Public License as published by *
  6. * the Free Software Foundation; either version 2 of the License, or *
  7. * (at your option) any later version. *
  8. * *
  9. * This program is distributed in the hope that it will be useful, *
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  12. * GNU General Public License for more details. *
  13. * *
  14. * You should have received a copy of the GNU General Public License *
  15. * along with this program; if not, write to the *
  16. * Free Software Foundation, Inc., *
  17. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  18. ***************************************************************************/
  19. #ifdef HAVE_CONFIG_H
  20. #include "config.h"
  21. #endif
  22. #include "log.h"
  23. #include "types.h"
  24. #include "jtag.h"
  25. #include "configuration.h"
  26. #include "xsvf.h"
  27. #include "svf.h"
  28. #include "target.h"
  29. #include "flash.h"
  30. #include "nand.h"
  31. #include "pld.h"
  32. #include "command.h"
  33. #include "server.h"
  34. #include "telnet_server.h"
  35. #include "gdb_server.h"
  36. #include <time_support.h>
  37. #include <sys/time.h>
  38. #include <sys/types.h>
  39. #include <strings.h>
  40. #include <stdio.h>
  41. #include <stdlib.h>
  42. #include <string.h>
  43. #include <unistd.h>
  44. #include <errno.h>
  45. #include <cyg/io/flash.h>
  46. #include <pkgconf/fs_jffs2.h> // Address of JFFS2
  47. #include <network.h>
  48. #include <fcntl.h>
  49. #include <sys/stat.h>
  50. #include <cyg/fileio/fileio.h>
  51. #include <dirent.h>
  52. #include <cyg/athttpd/http.h>
  53. #include <cyg/athttpd/socket.h>
  54. #include <cyg/athttpd/handler.h>
  55. #include <cyg/athttpd/cgi.h>
  56. #include <cyg/athttpd/forms.h>
  57. #include <cyg/discover/discover.h>
  58. #include <cyg/hal/hal_diag.h>
  59. #include <cyg/kernel/kapi.h>
  60. #include <cyg/io/serialio.h>
  61. #include <cyg/io/io.h>
  62. #include <netinet/tcp.h>
  63. #include "rom.h"
  64. #include <sys/ioctl.h>
  65. #include <sys/socket.h>
  66. #include <netinet/in.h>
  67. #include <net/if.h>
  68. #include <arpa/inet.h>
  69. #include <sys/types.h>
  70. #include <sys/socket.h>
  71. #include <netdb.h>
  72. #include <netinet/in.h>
  73. #include <unistd.h>
  74. #include <arpa/inet.h>
  75. #include <stdio.h>
  76. #include <ifaddrs.h>
  77. #include <string.h>
  78. #include <unistd.h>
  79. #include <stdio.h>
  80. #define MAX_IFS 64
  81. #if defined(CYGPKG_NET_FREEBSD_STACK)
  82. #include <tftp_support.h>
  83. /* posix compatibility broken*/
  84. struct tftpd_fileops fileops =
  85. {
  86. (int (*)(const char *, int))open,
  87. close,
  88. (int (*)(int, const void *, int))write,
  89. ( int (*)(int, void *, int))read
  90. };
  91. #endif
  92. void diag_write(char *buf, int len)
  93. {
  94. int j;
  95. for (j = 0; j < len; j++)
  96. {
  97. diag_printf("%c", buf[j]);
  98. }
  99. }
  100. static bool serialLog = true;
  101. static bool writeLog = true;
  102. char hwaddr[512];
  103. extern flash_driver_t *flash_drivers[];
  104. extern target_type_t *target_types[];
  105. #ifdef CYGPKG_PROFILE_GPROF
  106. #include <cyg/profile/profile.h>
  107. extern char _stext, _etext; // Defined by the linker
  108. static char *start_of_code=&_stext;
  109. static char *end_of_code=&_etext;
  110. void start_profile(void)
  111. {
  112. // This starts up the system-wide profiling, gathering
  113. // profile information on all of the code, with a 16 byte
  114. // "bucket" size, at a rate of 100us/profile hit.
  115. // Note: a bucket size of 16 will give pretty good function
  116. // resolution. Much smaller and the buffer becomes
  117. // much too large for very little gain.
  118. // Note: a timer period of 100us is also a reasonable
  119. // compromise. Any smaller and the overhead of
  120. // handling the timter (profile) interrupt could
  121. // swamp the system. A fast processor might get
  122. // by with a smaller value, but a slow one could
  123. // even be swamped by this value. If the value is
  124. // too large, the usefulness of the profile is reduced.
  125. // no more interrupts than 1/10ms.
  126. //profile_on((void *)0, (void *)0x40000, 16, 10000); // SRAM
  127. // profile_on(0, &_etext, 16, 10000); // SRAM & DRAM
  128. profile_on(start_of_code, end_of_code, 16, 10000); // Nios DRAM
  129. }
  130. #endif
  131. extern int eth0_up;
  132. static FILE *log;
  133. static char reboot_stack[2048];
  134. static void zylinjtag_reboot(cyg_addrword_t data)
  135. {
  136. serialLog = true;
  137. diag_printf("Rebooting in 100 ticks..\n");
  138. cyg_thread_delay(100);
  139. diag_printf("Unmounting /config..\n");
  140. umount("/config");
  141. diag_printf("Rebooting..\n");
  142. HAL_PLATFORM_RESET();
  143. }
  144. static cyg_thread zylinjtag_thread_object;
  145. static cyg_handle_t zylinjtag_thread_handle;
  146. void reboot(void)
  147. {
  148. cyg_thread_create(1, zylinjtag_reboot, (cyg_addrword_t) 0, "reboot Thread",
  149. (void *) reboot_stack, sizeof(reboot_stack),
  150. &zylinjtag_thread_handle, &zylinjtag_thread_object);
  151. cyg_thread_resume(zylinjtag_thread_handle);
  152. }
  153. int configuration_output_handler(struct command_context_s *context,
  154. const char* line)
  155. {
  156. diag_printf("%s", line);
  157. return ERROR_OK;
  158. }
  159. int zy1000_configuration_output_handler_log(struct command_context_s *context,
  160. const char* line)
  161. {
  162. LOG_USER_N("%s", line);
  163. return ERROR_OK;
  164. }
  165. #ifdef CYGPKG_PROFILE_GPROF
  166. extern void start_profile(void);
  167. int eCosBoard_handle_eCosBoard_profile_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  168. {
  169. command_print(cmd_ctx, "Profiling started");
  170. start_profile();
  171. return ERROR_OK;
  172. }
  173. #endif
  174. externC void phi_init_all_network_interfaces(void);
  175. command_context_t *cmd_ctx;
  176. static bool webRunning = false;
  177. void keep_webserver(void)
  178. {
  179. // Target initialisation is only attempted at startup, so we sleep forever and
  180. // let the http server bail us out(i.e. get config files set up).
  181. diag_printf("OpenOCD has invoked exit().\n"
  182. "Use web server to correct any configuration settings and reboot.\n");
  183. if (!webRunning)
  184. reboot();
  185. // exit() will terminate the current thread and we we'll then sleep eternally or
  186. // we'll have a reboot scheduled.
  187. }
  188. extern void printDccChar(char c);
  189. static char logBuffer[128 * 1024];
  190. static const int logSize = sizeof(logBuffer);
  191. int writePtr = 0;
  192. int logCount = 0;
  193. void _zylinjtag_diag_write_char(char c, void **param)
  194. {
  195. if (writeLog)
  196. {
  197. logBuffer[writePtr] = c;
  198. writePtr = (writePtr + 1) % logSize;
  199. logCount++;
  200. }
  201. if (serialLog)
  202. {
  203. if (c == '\n')
  204. {
  205. HAL_DIAG_WRITE_CHAR('\r');
  206. }
  207. HAL_DIAG_WRITE_CHAR(c);
  208. }
  209. #ifdef CYGPKG_HAL_ZYLIN_PHI
  210. printDccChar(c);
  211. #endif
  212. }
  213. void copyfile(char *name2, char *name1);
  214. void copydir(char *name, char *destdir);
  215. #if 0
  216. MTAB_ENTRY( romfs_mte1,
  217. "/rom",
  218. "romfs",
  219. "",
  220. (CYG_ADDRWORD) &filedata[0] );
  221. #endif
  222. void openocd_sleep_prelude(void)
  223. {
  224. cyg_mutex_unlock(&httpstate.jim_lock);
  225. }
  226. void openocd_sleep_postlude(void)
  227. {
  228. cyg_mutex_lock(&httpstate.jim_lock);
  229. }
  230. void format(void)
  231. {
  232. diag_printf("Formatting JFFS2...\n");
  233. cyg_io_handle_t handle;
  234. Cyg_ErrNo err;
  235. err = cyg_io_lookup(CYGDAT_IO_FLASH_BLOCK_DEVICE_NAME_1, &handle);
  236. if (err != ENOERR)
  237. {
  238. diag_printf("Flash Error cyg_io_lookup: %d\n", err);
  239. reboot();
  240. }
  241. cyg_uint32 len;
  242. cyg_io_flash_getconfig_devsize_t ds;
  243. len = sizeof(ds);
  244. err = cyg_io_get_config(handle, CYG_IO_GET_CONFIG_FLASH_DEVSIZE, &ds, &len);
  245. if (err != ENOERR)
  246. {
  247. diag_printf("Flash error cyg_io_get_config %d\n", err);
  248. reboot();
  249. }
  250. cyg_io_flash_getconfig_erase_t e;
  251. void *err_addr;
  252. len = sizeof(e);
  253. e.offset = 0;
  254. e.len = ds.dev_size;
  255. e.err_address = &err_addr;
  256. diag_printf("Formatting 0x%08x bytes\n", ds.dev_size);
  257. err = cyg_io_get_config(handle, CYG_IO_GET_CONFIG_FLASH_ERASE, &e, &len);
  258. if (err != ENOERR)
  259. {
  260. diag_printf("Flash erase error %d offset 0x%p\n", err, err_addr);
  261. reboot();
  262. }
  263. diag_printf("Flash formatted successfully\n");
  264. reboot();
  265. }
  266. static int zylinjtag_Jim_Command_format_jffs2(Jim_Interp *interp, int argc,
  267. Jim_Obj * const *argv)
  268. {
  269. if (argc != 1)
  270. {
  271. return JIM_ERR;
  272. }
  273. format();
  274. for (;;)
  275. ;
  276. }
  277. static int zylinjtag_Jim_Command_threads(Jim_Interp *interp, int argc,
  278. Jim_Obj * const *argv)
  279. {
  280. cyg_handle_t thread = 0;
  281. cyg_uint16 id = 0;
  282. Jim_Obj *threads = Jim_NewListObj(interp, NULL, 0);
  283. /* Loop over the threads, and generate a table row for
  284. * each.
  285. */
  286. while (cyg_thread_get_next(&thread, &id))
  287. {
  288. Jim_Obj *threadObj = Jim_NewListObj(interp, NULL, 0);
  289. cyg_thread_info info;
  290. char *state_string;
  291. cyg_thread_get_info(thread, id, &info);
  292. if (info.name == NULL)
  293. info.name = "<no name>";
  294. Jim_ListAppendElement(interp, threadObj, Jim_NewStringObj(interp,
  295. info.name, strlen(info.name)));
  296. /* Translate the state into a string.
  297. */
  298. if (info.state == 0)
  299. state_string = "RUN";
  300. else if (info.state & 0x04)
  301. state_string = "SUSP";
  302. else
  303. switch (info.state & 0x1b)
  304. {
  305. case 0x01:
  306. state_string = "SLEEP";
  307. break;
  308. case 0x02:
  309. state_string = "CNTSLEEP";
  310. break;
  311. case 0x08:
  312. state_string = "CREATE";
  313. break;
  314. case 0x10:
  315. state_string = "EXIT";
  316. break;
  317. default:
  318. state_string = "????";
  319. break;
  320. }
  321. Jim_ListAppendElement(interp, threadObj, Jim_NewStringObj(interp,
  322. state_string, strlen(state_string)));
  323. Jim_ListAppendElement(interp, threadObj, Jim_NewIntObj(interp, id));
  324. Jim_ListAppendElement(interp, threadObj, Jim_NewIntObj(interp,
  325. info.set_pri));
  326. Jim_ListAppendElement(interp, threadObj, Jim_NewIntObj(interp,
  327. info.cur_pri));
  328. Jim_ListAppendElement(interp, threads, threadObj);
  329. }
  330. Jim_SetResult(interp, threads);
  331. return JIM_OK;
  332. }
  333. static int zylinjtag_Jim_Command_log(Jim_Interp *interp, int argc,
  334. Jim_Obj * const *argv)
  335. {
  336. Jim_Obj *tclOutput = Jim_NewStringObj(interp, "", 0);
  337. if (logCount >= logSize)
  338. {
  339. Jim_AppendString(httpstate.jim_interp, tclOutput, logBuffer + logCount
  340. % logSize, logSize - logCount % logSize);
  341. }
  342. Jim_AppendString(httpstate.jim_interp, tclOutput, logBuffer, writePtr);
  343. Jim_SetResult(interp, tclOutput);
  344. return JIM_OK;
  345. }
  346. static int zylinjtag_Jim_Command_reboot(Jim_Interp *interp, int argc,
  347. Jim_Obj * const *argv)
  348. {
  349. reboot();
  350. return JIM_OK;
  351. }
  352. extern Jim_Interp *interp;
  353. static void zylinjtag_startNetwork(void)
  354. {
  355. // Bring TCP/IP up immediately before we're ready to accept commands.
  356. //
  357. // That is as soon as a PING responds, we're accepting telnet sessions.
  358. #if defined(CYGPKG_NET_FREEBSD_STACK)
  359. phi_init_all_network_interfaces();
  360. #else
  361. lwip_init();
  362. #endif
  363. if (!eth0_up)
  364. {
  365. diag_printf("Network not up and running\n");
  366. exit(-1);
  367. }
  368. #if defined(CYGPKG_NET_FREEBSD_STACK)
  369. /*start TFTP*/
  370. tftpd_start(69, &fileops);
  371. #endif
  372. cyg_httpd_init_tcl_interpreter();
  373. interp = httpstate.jim_interp;
  374. Jim_CreateCommand(httpstate.jim_interp, "log", zylinjtag_Jim_Command_log,
  375. NULL, NULL);
  376. Jim_CreateCommand(httpstate.jim_interp, "reboot",
  377. zylinjtag_Jim_Command_reboot, NULL, NULL);
  378. Jim_CreateCommand(httpstate.jim_interp, "threads",
  379. zylinjtag_Jim_Command_threads, NULL, NULL);
  380. Jim_CreateCommand(httpstate.jim_interp, "format_jffs2",
  381. zylinjtag_Jim_Command_format_jffs2, NULL, NULL);
  382. cyg_httpd_start();
  383. webRunning = true;
  384. diag_printf("Web server running\n");
  385. int s;
  386. struct ifreq ifr;
  387. s = socket(AF_INET, SOCK_DGRAM, 0);
  388. if (s >= 0)
  389. {
  390. strcpy(ifr.ifr_name, "eth0");
  391. int res;
  392. res = ioctl(s, SIOCGIFHWADDR, &ifr);
  393. close(s);
  394. if (res < 0)
  395. {
  396. diag_printf("Can't obtain MAC address\n");
  397. reboot();
  398. }
  399. }
  400. sprintf(hwaddr, "%02x:%02x:%02x:%02x:%02x:%02x",
  401. (int) ((unsigned char *) &ifr.ifr_hwaddr.sa_data)[0],
  402. (int) ((unsigned char *) &ifr.ifr_hwaddr.sa_data)[1],
  403. (int) ((unsigned char *) &ifr.ifr_hwaddr.sa_data)[2],
  404. (int) ((unsigned char *) &ifr.ifr_hwaddr.sa_data)[3],
  405. (int) ((unsigned char *) &ifr.ifr_hwaddr.sa_data)[4],
  406. (int) ((unsigned char *) &ifr.ifr_hwaddr.sa_data)[5]);
  407. discover_message
  408. = alloc_printf("ZY1000 Zylin JTAG debugger MAC %s", hwaddr);
  409. discover_launch();
  410. }
  411. static void print_exception_handler(cyg_addrword_t data, cyg_code_t exception,
  412. cyg_addrword_t info)
  413. {
  414. writeLog = false;
  415. serialLog = true;
  416. char *infoStr = "unknown";
  417. switch (exception)
  418. {
  419. #ifdef CYGNUM_HAL_VECTOR_UNDEF_INSTRUCTION
  420. case CYGNUM_HAL_VECTOR_UNDEF_INSTRUCTION:
  421. infoStr = "undefined instruction";
  422. break;
  423. case CYGNUM_HAL_VECTOR_SOFTWARE_INTERRUPT:
  424. infoStr = "software interrupt";
  425. break;
  426. case CYGNUM_HAL_VECTOR_ABORT_PREFETCH:
  427. infoStr = "abort prefetch";
  428. break;
  429. case CYGNUM_HAL_VECTOR_ABORT_DATA:
  430. infoStr = "abort data";
  431. break;
  432. #endif
  433. default:
  434. break;
  435. }
  436. diag_printf("Exception: %08x(%s) %08x\n", exception, infoStr, info);
  437. diag_printf("Dumping log\n---\n");
  438. if (logCount >= logSize)
  439. {
  440. diag_write(logBuffer + logCount % logSize, logSize - logCount % logSize);
  441. }
  442. diag_write(logBuffer, writePtr);
  443. diag_printf("---\nLogdump complete.\n");
  444. diag_printf("Exception: %08x(%s) %08x\n", exception, infoStr, info);
  445. diag_printf("\n---\nRebooting\n");
  446. HAL_PLATFORM_RESET();
  447. }
  448. static void setHandler(cyg_code_t exception)
  449. {
  450. cyg_exception_handler_t *old_handler;
  451. cyg_addrword_t old_data;
  452. cyg_exception_set_handler(exception, print_exception_handler, 0,
  453. &old_handler, &old_data);
  454. }
  455. static cyg_thread zylinjtag_uart_thread_object;
  456. static cyg_handle_t zylinjtag_uart_thread_handle;
  457. static char uart_stack[4096];
  458. static char forwardBuffer[1024]; // NB! must be smaller than a TCP/IP packet!!!!!
  459. static char backwardBuffer[1024];
  460. void setNoDelay(int session, int flag)
  461. {
  462. #if 1
  463. // This decreases latency dramatically for e.g. GDB load which
  464. // does not have a sliding window protocol
  465. //
  466. // Can cause *lots* of TCP/IP packets to be sent and it would have
  467. // to be enabled/disabled on the fly to avoid the CPU being
  468. // overloaded...
  469. setsockopt(session, /* socket affected */
  470. IPPROTO_TCP, /* set option at TCP level */
  471. TCP_NODELAY, /* name of option */
  472. (char *) &flag, /* the cast is historical
  473. cruft */
  474. sizeof(int)); /* length of option value */
  475. #endif
  476. }
  477. struct
  478. {
  479. int req;
  480. int actual;
  481. int req2;
  482. int actual2;
  483. } tcpipSent[512 * 1024];
  484. int cur;
  485. static void zylinjtag_uart(cyg_addrword_t data)
  486. {
  487. int so_reuseaddr_option = 1;
  488. int fd;
  489. if ((fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
  490. {
  491. LOG_ERROR("error creating socket: %s", strerror(errno));
  492. exit(-1);
  493. }
  494. setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void*) &so_reuseaddr_option,
  495. sizeof(int));
  496. struct sockaddr_in sin;
  497. unsigned int address_size;
  498. address_size = sizeof(sin);
  499. memset(&sin, 0, sizeof(sin));
  500. sin.sin_family = AF_INET;
  501. sin.sin_addr.s_addr = INADDR_ANY;
  502. sin.sin_port = htons(5555);
  503. if (bind(fd, (struct sockaddr *) &sin, sizeof(sin)) == -1)
  504. {
  505. LOG_ERROR("couldn't bind to socket: %s", strerror(errno));
  506. exit(-1);
  507. }
  508. if (listen(fd, 1) == -1)
  509. {
  510. LOG_ERROR("couldn't listen on socket: %s", strerror(errno));
  511. exit(-1);
  512. }
  513. // socket_nonblock(fd);
  514. for (;;)
  515. {
  516. int session = accept(fd, (struct sockaddr *) &sin, &address_size);
  517. if (session < 0)
  518. {
  519. continue;
  520. }
  521. setNoDelay(session, 1);
  522. int oldopts = fcntl(session, F_GETFL, 0);
  523. fcntl(session, F_SETFL, oldopts | O_NONBLOCK); //
  524. int serHandle = open("/dev/ser0", O_RDWR | O_NONBLOCK);
  525. if (serHandle < 0)
  526. {
  527. close(session);
  528. continue;
  529. }
  530. #ifdef CYGPKG_PROFILE_GPROF
  531. start_profile();
  532. #endif
  533. int actual = 0;
  534. int actual2 = 0;
  535. int pos, pos2;
  536. pos = 0;
  537. pos2 = 0;
  538. cur = 0;
  539. for (;;)
  540. {
  541. fd_set write_fds;
  542. fd_set read_fds;
  543. FD_ZERO(&write_fds);
  544. FD_ZERO(&read_fds);
  545. int fd_max = -1;
  546. FD_SET(session, &read_fds);
  547. fd_max = session;
  548. FD_SET(serHandle, &read_fds);
  549. if (serHandle > fd_max)
  550. {
  551. fd_max = serHandle;
  552. }
  553. /* Wait... */
  554. cyg_thread_delay(5); // 50ms fixed delay to wait for data to be sent/received
  555. if ((actual == 0) && (actual2 == 0))
  556. {
  557. int retval = select(fd_max + 1, &read_fds, NULL, NULL, NULL);
  558. if (retval <= 0)
  559. {
  560. break;
  561. }
  562. }
  563. if (actual2 <= 0)
  564. {
  565. memset(backwardBuffer, 's', sizeof(backwardBuffer));
  566. actual2 = read(serHandle, backwardBuffer,
  567. sizeof(backwardBuffer));
  568. if (actual2 < 0)
  569. {
  570. if (errno != EAGAIN)
  571. {
  572. goto closeSession;
  573. }
  574. actual2 = 0;
  575. }
  576. pos2 = 0;
  577. }
  578. int x = actual2;
  579. int y = 0;
  580. if (actual2 > 0)
  581. {
  582. int written = write(session, backwardBuffer + pos2, actual2);
  583. if (written <= 0)
  584. goto closeSession;
  585. actual2 -= written;
  586. pos2 += written;
  587. y = written;
  588. }
  589. if (FD_ISSET(session, &read_fds)
  590. && (sizeof(forwardBuffer) > actual))
  591. {
  592. // NB! Here it is important that we empty the TCP/IP read buffer
  593. // to make transmission tick right
  594. memmove(forwardBuffer, forwardBuffer + pos, actual);
  595. pos = 0;
  596. int t;
  597. // this will block if there is no data at all
  598. t = read_socket(session, forwardBuffer + actual,
  599. sizeof(forwardBuffer) - actual);
  600. if (t <= 0)
  601. {
  602. goto closeSession;
  603. }
  604. actual += t;
  605. }
  606. int x2 = actual;
  607. int y2 = 0;
  608. if (actual > 0)
  609. {
  610. /* Do not put things into the serial buffer if it has something to send
  611. * as that can cause a single byte to be sent at the time.
  612. *
  613. *
  614. */
  615. int written = write(serHandle, forwardBuffer + pos, actual);
  616. if (written < 0)
  617. {
  618. if (errno != EAGAIN)
  619. {
  620. goto closeSession;
  621. }
  622. // The serial buffer is full
  623. written = 0;
  624. }
  625. else
  626. {
  627. actual -= written;
  628. pos += written;
  629. }
  630. y2 = written;
  631. }
  632. if (cur < 1024)
  633. {
  634. tcpipSent[cur].req = x;
  635. tcpipSent[cur].actual = y;
  636. tcpipSent[cur].req2 = x2;
  637. tcpipSent[cur].actual2 = y2;
  638. cur++;
  639. }
  640. }
  641. closeSession: close(session);
  642. close(serHandle);
  643. int i;
  644. for (i = 0; i < 1024; i++)
  645. {
  646. diag_printf("%d %d %d %d\n", tcpipSent[i].req, tcpipSent[i].actual,
  647. tcpipSent[i].req2, tcpipSent[i].actual2);
  648. }
  649. }
  650. close(fd);
  651. }
  652. void startUart(void)
  653. {
  654. cyg_thread_create(1, zylinjtag_uart, (cyg_addrword_t) 0, "uart thread",
  655. (void *) uart_stack, sizeof(uart_stack),
  656. &zylinjtag_uart_thread_handle, &zylinjtag_uart_thread_object);
  657. cyg_thread_set_priority(zylinjtag_uart_thread_handle, 1); // low priority as it sits in a busy loop
  658. cyg_thread_resume(zylinjtag_uart_thread_handle);
  659. }
  660. int handle_uart_command(struct command_context_s *cmd_ctx, char *cmd,
  661. char **args, int argc)
  662. {
  663. static int current_baud = 38400;
  664. if (argc == 0)
  665. {
  666. command_print(cmd_ctx, "%d", current_baud);
  667. return ERROR_OK;
  668. }
  669. else if (argc != 1)
  670. {
  671. return ERROR_INVALID_ARGUMENTS;
  672. }
  673. current_baud = atol(args[0]);
  674. int baud;
  675. switch (current_baud)
  676. {
  677. case 9600:
  678. baud = CYGNUM_SERIAL_BAUD_9600;
  679. break;
  680. case 19200:
  681. baud = CYGNUM_SERIAL_BAUD_19200;
  682. break;
  683. case 38400:
  684. baud = CYGNUM_SERIAL_BAUD_38400;
  685. break;
  686. case 57600:
  687. baud = CYGNUM_SERIAL_BAUD_57600;
  688. break;
  689. case 115200:
  690. baud = CYGNUM_SERIAL_BAUD_115200;
  691. break;
  692. case 230400:
  693. baud = CYGNUM_SERIAL_BAUD_230400;
  694. break;
  695. default:
  696. command_print(cmd_ctx, "unsupported baudrate");
  697. return ERROR_INVALID_ARGUMENTS;
  698. }
  699. cyg_serial_info_t buf;
  700. cyg_uint32 len = 1;
  701. //get existing serial configuration
  702. len = sizeof(cyg_serial_info_t);
  703. int err;
  704. cyg_io_handle_t serial_handle;
  705. err = cyg_io_lookup("/dev/ser0", &serial_handle);
  706. if (err != ENOERR)
  707. {
  708. LOG_ERROR("/dev/ser0 not found\n");
  709. return ERROR_FAIL;
  710. }
  711. err = cyg_io_get_config(serial_handle,
  712. CYG_IO_GET_CONFIG_SERIAL_OUTPUT_DRAIN, &buf, &len);
  713. err = cyg_io_get_config(serial_handle, CYG_IO_GET_CONFIG_SERIAL_INFO, &buf,
  714. &len);
  715. if (err != ENOERR)
  716. {
  717. command_print(cmd_ctx, "Failed to get serial port settings %d", err);
  718. return ERROR_OK;
  719. }
  720. buf.baud = baud;
  721. err = cyg_io_set_config(serial_handle, CYG_IO_SET_CONFIG_SERIAL_INFO, &buf,
  722. &len);
  723. if (err != ENOERR)
  724. {
  725. command_print(cmd_ctx, "Failed to set serial port settings %d", err);
  726. return ERROR_OK;
  727. }
  728. return ERROR_OK;
  729. }
  730. bool logAllToSerial = false;
  731. int boolParam(char *var);
  732. command_context_t *setup_command_handler(void);
  733. extern const char *zylin_config_dir;
  734. int add_default_dirs(void)
  735. {
  736. add_script_search_dir(zylin_config_dir);
  737. add_script_search_dir("/rom/lib/openocd");
  738. add_script_search_dir("/rom");
  739. return ERROR_OK;
  740. }
  741. int ioutil_init(struct command_context_s *cmd_ctx);
  742. int main(int argc, char *argv[])
  743. {
  744. /* ramblockdevice will be the same address every time. The deflate app uses a buffer 16mBytes out, so we
  745. * need to allocate towards the end of the heap. */
  746. #ifdef CYGNUM_HAL_VECTOR_UNDEF_INSTRUCTION
  747. setHandler(CYGNUM_HAL_VECTOR_UNDEF_INSTRUCTION);
  748. setHandler(CYGNUM_HAL_VECTOR_ABORT_PREFETCH);
  749. setHandler(CYGNUM_HAL_VECTOR_ABORT_DATA);
  750. #endif
  751. int err;
  752. atexit(keep_webserver);
  753. diag_init_putc(_zylinjtag_diag_write_char);
  754. // We want this in the log.
  755. diag_printf("Zylin ZY1000.\n");
  756. err = mount("", "/ram", "ramfs");
  757. if (err < 0)
  758. {
  759. diag_printf("unable to mount ramfs\n");
  760. }
  761. chdir("/ram");
  762. char address[16];
  763. sprintf(address, "%p", &filedata[0]);
  764. err = mount(address, "/rom", "romfs");
  765. if (err < 0)
  766. {
  767. diag_printf("unable to mount /rom\n");
  768. }
  769. err = mount("", "/log", "logfs");
  770. if (err < 0)
  771. {
  772. diag_printf("unable to mount logfs\n");
  773. }
  774. err = mount("", "/tftp", "tftpfs");
  775. if (err < 0)
  776. {
  777. diag_printf("unable to mount logfs\n");
  778. }
  779. log = fopen("/log/log", "w");
  780. if (log == NULL)
  781. {
  782. diag_printf("Could not open log file /ram/log\n");
  783. exit(-1);
  784. }
  785. copydir("/rom", "/ram/cgi");
  786. err = mount("/dev/flash1", "/config", "jffs2");
  787. if (err < 0)
  788. {
  789. diag_printf("unable to mount jffs2, falling back to ram disk..\n");
  790. err = mount("", "/config", "ramfs");
  791. if (err < 0)
  792. {
  793. diag_printf("unable to mount /config as ramdisk.\n");
  794. reboot();
  795. }
  796. }
  797. else
  798. {
  799. /* are we using a ram disk instead of a flash disk? This is used
  800. * for ZY1000 live demo...
  801. *
  802. * copy over flash disk to ram block device
  803. */
  804. if (boolParam("ramdisk"))
  805. {
  806. diag_printf("Unmounting /config from flash and using ram instead\n");
  807. err = umount("/config");
  808. if (err < 0)
  809. {
  810. diag_printf("unable to unmount jffs\n");
  811. reboot();
  812. }
  813. err = mount("/dev/flash1", "/config2", "jffs2");
  814. if (err < 0)
  815. {
  816. diag_printf("unable to mount jffs\n");
  817. reboot();
  818. }
  819. err = mount("", "/config", "ramfs");
  820. if (err < 0)
  821. {
  822. diag_printf("unable to mount ram block device\n");
  823. reboot();
  824. }
  825. // copydir("/config2", "/config");
  826. copyfile("/config2/ip", "/config/ip");
  827. copydir("/config2/settings", "/config/settings");
  828. umount("/config2");
  829. }
  830. }
  831. mkdir(zylin_config_dir, 0777);
  832. char *dirname=alloc_printf("%s/target", zylin_config_dir);
  833. mkdir(dirname, 0777);
  834. free(dirname);
  835. dirname=alloc_printf("%s/event", zylin_config_dir);
  836. mkdir(dirname, 0777);
  837. free(dirname);
  838. logAllToSerial = boolParam("logserial");
  839. // We need the network & web server in case there is something wrong with
  840. // the config files that invoke exit()
  841. zylinjtag_startNetwork();
  842. /* we're going to access the jim interpreter from here on... */
  843. openocd_sleep_postlude();
  844. startUart();
  845. add_default_dirs();
  846. /* initialize commandline interface */
  847. command_context_t * cmd_ctx;
  848. cmd_ctx = setup_command_handler();
  849. command_set_output_handler(cmd_ctx, configuration_output_handler, NULL);
  850. command_context_mode(cmd_ctx, COMMAND_CONFIG);
  851. #if BUILD_IOUTIL
  852. if (ioutil_init(cmd_ctx) != ERROR_OK)
  853. {
  854. return EXIT_FAILURE;
  855. }
  856. #endif
  857. #ifdef CYGPKG_PROFILE_GPROF
  858. register_command(cmd_ctx, NULL, "ecosboard_profile", eCosBoard_handle_eCosBoard_profile_command,
  859. COMMAND_ANY, NULL);
  860. #endif
  861. register_command(cmd_ctx, NULL, "uart", handle_uart_command, COMMAND_ANY,
  862. "uart <baud> - forward uart on port 5555");
  863. int errVal;
  864. errVal = log_init(cmd_ctx);
  865. if (errVal != ERROR_OK)
  866. {
  867. diag_printf("log_init() failed %d\n", errVal);
  868. exit(-1);
  869. }
  870. set_log_output(cmd_ctx, log);
  871. LOG_DEBUG("log init complete");
  872. // diag_printf("Executing config files\n");
  873. if (logAllToSerial)
  874. {
  875. diag_printf(
  876. "%s/logserial=1 => sending log output to serial port using \"debug_level 3\" as default.\n", zylin_config_dir);
  877. command_run_line(cmd_ctx, "debug_level 3");
  878. }
  879. command_run_linef(cmd_ctx, "script /rom/openocd.cfg");
  880. // FIX!!! Yuk!
  881. // diag_printf() is really invoked from many more places than we trust it
  882. // not to cause instabilities(e.g. invoking fputc() from an interrupt is *BAD*).
  883. //
  884. // Disabling it here is safe and gives us enough logged debug output for now. Crossing
  885. // fingers that it doesn't cause any crashes.
  886. diag_printf("Init complete, GDB & telnet servers launched.\n");
  887. command_set_output_handler(cmd_ctx,
  888. zy1000_configuration_output_handler_log, NULL);
  889. if (!logAllToSerial)
  890. {
  891. serialLog = false;
  892. }
  893. /* handle network connections */
  894. server_loop(cmd_ctx);
  895. openocd_sleep_prelude();
  896. /* shut server down */
  897. server_quit();
  898. /* free commandline interface */
  899. command_done(cmd_ctx);
  900. umount("/config");
  901. exit(0);
  902. for (;;)
  903. ;
  904. }
  905. cyg_int32 cyg_httpd_exec_cgi_tcl(char *file_name);
  906. cyg_int32 homeForm(CYG_HTTPD_STATE *p)
  907. {
  908. cyg_httpd_exec_cgi_tcl("/ram/cgi/index.tcl");
  909. return 0;
  910. }
  911. CYG_HTTPD_HANDLER_TABLE_ENTRY(root_label, "/", homeForm);
  912. CYG_HTTPD_MIME_TABLE_ENTRY(text_mime_label, "text", "text/plain");
  913. CYG_HTTPD_MIME_TABLE_ENTRY(bin_mime_label, "bin", "application/octet-stream");
  914. #include <pkgconf/system.h>
  915. #include <pkgconf/hal.h>
  916. #include <pkgconf/kernel.h>
  917. #include <pkgconf/io_fileio.h>
  918. #include <pkgconf/fs_rom.h>
  919. #include <cyg/kernel/ktypes.h> // base kernel types
  920. #include <cyg/infra/cyg_trac.h> // tracing macros
  921. #include <cyg/infra/cyg_ass.h> // assertion macros
  922. #include <unistd.h>
  923. #include <sys/types.h>
  924. #include <fcntl.h>
  925. #include <sys/stat.h>
  926. #include <errno.h>
  927. #include <dirent.h>
  928. #include <stdarg.h>
  929. #include <stdio.h>
  930. #include <stdlib.h>
  931. #include <string.h>
  932. #include <cyg/fileio/fileio.h>
  933. #include <cyg/kernel/kapi.h>
  934. #include <cyg/infra/diag.h>
  935. //==========================================================================
  936. // Eventually we want to eXecute In Place from the ROM in a protected
  937. // environment, so we'll need executables to be aligned to a boundary
  938. // suitable for MMU protection. A suitable boundary would be the 4k
  939. // boundary in all the CPU architectures I am currently aware of.
  940. // Forward definitions
  941. // Filesystem operations
  942. static int tftpfs_mount(cyg_fstab_entry *fste, cyg_mtab_entry *mte);
  943. static int tftpfs_umount(cyg_mtab_entry *mte);
  944. static int tftpfs_open(cyg_mtab_entry *mte, cyg_dir dir, const char *name,
  945. int mode, cyg_file *fte);
  946. static int tftpfs_fo_read(struct CYG_FILE_TAG *fp, struct CYG_UIO_TAG *uio);
  947. static int tftpfs_fo_write(struct CYG_FILE_TAG *fp, struct CYG_UIO_TAG *uio);
  948. // File operations
  949. static int tftpfs_fo_fsync(struct CYG_FILE_TAG *fp, int mode);
  950. static int tftpfs_fo_close(struct CYG_FILE_TAG *fp);
  951. static int tftpfs_fo_lseek(struct CYG_FILE_TAG *fp, off_t *apos, int whence);
  952. //==========================================================================
  953. // Filesystem table entries
  954. // -------------------------------------------------------------------------
  955. // Fstab entry.
  956. // This defines the entry in the filesystem table.
  957. // For simplicity we use _FILESYSTEM synchronization for all accesses since
  958. // we should never block in any filesystem operations.
  959. #if 1
  960. FSTAB_ENTRY( tftpfs_fste, "tftpfs", 0,
  961. CYG_SYNCMODE_NONE,
  962. tftpfs_mount,
  963. tftpfs_umount,
  964. tftpfs_open,
  965. (cyg_fsop_unlink *)cyg_fileio_erofs,
  966. (cyg_fsop_mkdir *)cyg_fileio_erofs,
  967. (cyg_fsop_rmdir *)cyg_fileio_erofs,
  968. (cyg_fsop_rename *)cyg_fileio_erofs,
  969. (cyg_fsop_link *)cyg_fileio_erofs,
  970. (cyg_fsop_opendir *)cyg_fileio_erofs,
  971. (cyg_fsop_chdir *)cyg_fileio_erofs,
  972. (cyg_fsop_stat *)cyg_fileio_erofs,
  973. (cyg_fsop_getinfo *)cyg_fileio_erofs,
  974. (cyg_fsop_setinfo *)cyg_fileio_erofs);
  975. #endif
  976. // -------------------------------------------------------------------------
  977. // mtab entry.
  978. // This defines a single ROMFS loaded into ROM at the configured address
  979. //
  980. // MTAB_ENTRY( rom_mte, // structure name
  981. // "/rom", // mount point
  982. // "romfs", // FIlesystem type
  983. // "", // hardware device
  984. // (CYG_ADDRWORD) CYGNUM_FS_ROM_BASE_ADDRESS // Address in ROM
  985. // );
  986. // -------------------------------------------------------------------------
  987. // File operations.
  988. // This set of file operations are used for normal open files.
  989. static cyg_fileops tftpfs_fileops =
  990. { tftpfs_fo_read, tftpfs_fo_write, tftpfs_fo_lseek,
  991. (cyg_fileop_ioctl *) cyg_fileio_erofs, cyg_fileio_seltrue,
  992. tftpfs_fo_fsync, tftpfs_fo_close,
  993. (cyg_fileop_fstat *) cyg_fileio_erofs,
  994. (cyg_fileop_getinfo *) cyg_fileio_erofs,
  995. (cyg_fileop_setinfo *) cyg_fileio_erofs, };
  996. // -------------------------------------------------------------------------
  997. // tftpfs_mount()
  998. // Process a mount request. This mainly finds root for the
  999. // filesystem.
  1000. static int tftpfs_mount(cyg_fstab_entry *fste, cyg_mtab_entry *mte)
  1001. {
  1002. return ENOERR;
  1003. }
  1004. static int tftpfs_umount(cyg_mtab_entry *mte)
  1005. {
  1006. return ENOERR;
  1007. }
  1008. struct Tftp
  1009. {
  1010. int write;
  1011. int readFile;
  1012. cyg_uint8 *mem;
  1013. int actual;
  1014. char *server;
  1015. char *file;
  1016. };
  1017. static void freeTftp(struct Tftp *t)
  1018. {
  1019. if (t == NULL)
  1020. return;
  1021. if (t->mem)
  1022. free(t->mem);
  1023. if (t->server)
  1024. free(t->server);
  1025. if (t->file)
  1026. free(t->file);
  1027. free(t);
  1028. }
  1029. static const int tftpMaxSize = 8192 * 1024;
  1030. static int tftpfs_open(cyg_mtab_entry *mte, cyg_dir dir, const char *name,
  1031. int mode, cyg_file *file)
  1032. {
  1033. struct Tftp *tftp;
  1034. tftp = malloc(sizeof(struct Tftp));
  1035. if (tftp == NULL)
  1036. return EMFILE;
  1037. memset(tftp, 0, sizeof(struct Tftp));
  1038. file->f_flag |= mode & CYG_FILE_MODE_MASK;
  1039. file->f_type = CYG_FILE_TYPE_FILE;
  1040. file->f_ops = &tftpfs_fileops;
  1041. file->f_offset = 0;
  1042. file->f_data = 0;
  1043. file->f_xops = 0;
  1044. tftp->mem = malloc(tftpMaxSize);
  1045. if (tftp->mem == NULL)
  1046. {
  1047. freeTftp(tftp);
  1048. return EMFILE;
  1049. }
  1050. char *server = strchr(name, '/');
  1051. if (server == NULL)
  1052. {
  1053. freeTftp(tftp);
  1054. return EMFILE;
  1055. }
  1056. tftp->server = malloc(server - name + 1);
  1057. if (tftp->server == NULL)
  1058. {
  1059. freeTftp(tftp);
  1060. return EMFILE;
  1061. }
  1062. strncpy(tftp->server, name, server - name);
  1063. tftp->server[server - name] = 0;
  1064. tftp->file = strdup(server + 1);
  1065. if (tftp->file == NULL)
  1066. {
  1067. freeTftp(tftp);
  1068. return EMFILE;
  1069. }
  1070. file->f_data = (CYG_ADDRWORD) tftp;
  1071. return ENOERR;
  1072. }
  1073. static int fetchTftp(struct Tftp *tftp)
  1074. {
  1075. if (!tftp->readFile)
  1076. {
  1077. int err;
  1078. tftp->actual = tftp_client_get(tftp->file, tftp->server, 0, tftp->mem,
  1079. tftpMaxSize, TFTP_OCTET, &err);
  1080. if (tftp->actual < 0)
  1081. {
  1082. return EMFILE;
  1083. }
  1084. tftp->readFile = 1;
  1085. }
  1086. return ENOERR;
  1087. }
  1088. // -------------------------------------------------------------------------
  1089. // tftpfs_fo_write()
  1090. // Read data from file.
  1091. static int tftpfs_fo_read(struct CYG_FILE_TAG *fp, struct CYG_UIO_TAG *uio)
  1092. {
  1093. struct Tftp *tftp = (struct Tftp *) fp->f_data;
  1094. if (fetchTftp(tftp) != ENOERR)
  1095. return EMFILE;
  1096. int i;
  1097. off_t pos = fp->f_offset;
  1098. int resid = 0;
  1099. for (i = 0; i < uio->uio_iovcnt; i++)
  1100. {
  1101. cyg_iovec *iov = &uio->uio_iov[i];
  1102. char *buf = (char *) iov->iov_base;
  1103. off_t len = iov->iov_len;
  1104. if (len + pos > tftp->actual)
  1105. {
  1106. len = tftp->actual - pos;
  1107. }
  1108. resid += iov->iov_len - len;
  1109. memcpy(buf, tftp->mem + pos, len);
  1110. pos += len;
  1111. }
  1112. uio->uio_resid = resid;
  1113. fp->f_offset = pos;
  1114. return ENOERR;
  1115. }
  1116. static int tftpfs_fo_write(struct CYG_FILE_TAG *fp, struct CYG_UIO_TAG *uio)
  1117. {
  1118. struct Tftp *tftp = (struct Tftp *) fp->f_data;
  1119. int i;
  1120. off_t pos = fp->f_offset;
  1121. int resid = 0;
  1122. for (i = 0; i < uio->uio_iovcnt; i++)
  1123. {
  1124. cyg_iovec *iov = &uio->uio_iov[i];
  1125. char *buf = (char *) iov->iov_base;
  1126. off_t len = iov->iov_len;
  1127. if (len + pos > tftpMaxSize)
  1128. {
  1129. len = tftpMaxSize - pos;
  1130. }
  1131. resid += iov->iov_len - len;
  1132. memcpy(tftp->mem + pos, buf, len);
  1133. pos += len;
  1134. }
  1135. uio->uio_resid = resid;
  1136. fp->f_offset = pos;
  1137. tftp->write = 1;
  1138. return ENOERR;
  1139. }
  1140. static int tftpfs_fo_fsync(struct CYG_FILE_TAG *fp, int mode)
  1141. {
  1142. int error = ENOERR;
  1143. return error;
  1144. }
  1145. // -------------------------------------------------------------------------
  1146. // romfs_fo_close()
  1147. // Close a file. We just clear out the data pointer.
  1148. static int tftpfs_fo_close(struct CYG_FILE_TAG *fp)
  1149. {
  1150. struct Tftp *tftp = (struct Tftp *) fp->f_data;
  1151. int error = ENOERR;
  1152. if (tftp->write)
  1153. {
  1154. tftp_client_put(tftp->file, tftp->server, 0, tftp->mem, fp->f_offset,
  1155. TFTP_OCTET, &error);
  1156. }
  1157. freeTftp(tftp);
  1158. fp->f_data = 0;
  1159. return error;
  1160. }
  1161. // -------------------------------------------------------------------------
  1162. // romfs_fo_lseek()
  1163. // Seek to a new file position.
  1164. static int tftpfs_fo_lseek(struct CYG_FILE_TAG *fp, off_t *apos, int whence)
  1165. {
  1166. struct Tftp *tftp = (struct Tftp *) fp->f_data;
  1167. off_t pos = *apos;
  1168. if (fetchTftp(tftp) != ENOERR)
  1169. return EMFILE;
  1170. switch (whence)
  1171. {
  1172. case SEEK_SET:
  1173. // Pos is already where we want to be.
  1174. break;
  1175. case SEEK_CUR:
  1176. // Add pos to current offset.
  1177. pos += fp->f_offset;
  1178. break;
  1179. case SEEK_END:
  1180. // Add pos to file size.
  1181. pos += tftp->actual;
  1182. break;
  1183. default:
  1184. return EINVAL;
  1185. }
  1186. // Check that pos is still within current file size, or at the
  1187. // very end.
  1188. if (pos < 0 || pos > tftp->actual)
  1189. return EINVAL;
  1190. // All OK, set fp offset and return new position.
  1191. *apos = fp->f_offset = pos;
  1192. return ENOERR;
  1193. }
  1194. void usleep(int us)
  1195. {
  1196. if (us > 10000)
  1197. cyg_thread_delay(us / 10000 + 1);
  1198. else
  1199. HAL_DELAY_US(us);
  1200. }
  1201. // Chunked version.
  1202. cyg_int32 show_log_entry(CYG_HTTPD_STATE *phttpstate)
  1203. {
  1204. cyg_httpd_start_chunked("text");
  1205. if (logCount >= logSize)
  1206. {
  1207. cyg_httpd_write_chunked(logBuffer + logCount % logSize, logSize
  1208. - logCount % logSize);
  1209. }
  1210. cyg_httpd_write_chunked(logBuffer, writePtr);
  1211. cyg_httpd_end_chunked();
  1212. return -1;
  1213. }
  1214. CYG_HTTPD_HANDLER_TABLE_ENTRY(show_log, "/ram/log", show_log_entry);
  1215. // Filesystem operations
  1216. static int logfs_mount(cyg_fstab_entry *fste, cyg_mtab_entry *mte);
  1217. static int logfs_umount(cyg_mtab_entry *mte);
  1218. static int logfs_open(cyg_mtab_entry *mte, cyg_dir dir, const char *name,
  1219. int mode, cyg_file *fte);
  1220. static int logfs_fo_write(struct CYG_FILE_TAG *fp, struct CYG_UIO_TAG *uio);
  1221. // File operations
  1222. static int logfs_fo_fsync(struct CYG_FILE_TAG *fp, int mode);
  1223. static int logfs_fo_close(struct CYG_FILE_TAG *fp);
  1224. #include <cyg/io/devtab.h>
  1225. //==========================================================================
  1226. // Filesystem table entries
  1227. // -------------------------------------------------------------------------
  1228. // Fstab entry.
  1229. // This defines the entry in the filesystem table.
  1230. // For simplicity we use _FILESYSTEM synchronization for all accesses since
  1231. // we should never block in any filesystem operations.
  1232. FSTAB_ENTRY( logfs_fste, "logfs", 0,
  1233. CYG_SYNCMODE_FILE_FILESYSTEM|CYG_SYNCMODE_IO_FILESYSTEM,
  1234. logfs_mount,
  1235. logfs_umount,
  1236. logfs_open,
  1237. (cyg_fsop_unlink *)cyg_fileio_erofs,
  1238. (cyg_fsop_mkdir *)cyg_fileio_erofs,
  1239. (cyg_fsop_rmdir *)cyg_fileio_erofs,
  1240. (cyg_fsop_rename *)cyg_fileio_erofs,
  1241. (cyg_fsop_link *)cyg_fileio_erofs,
  1242. (cyg_fsop_opendir *)cyg_fileio_erofs,
  1243. (cyg_fsop_chdir *)cyg_fileio_erofs,
  1244. (cyg_fsop_stat *)cyg_fileio_erofs,
  1245. (cyg_fsop_getinfo *)cyg_fileio_erofs,
  1246. (cyg_fsop_setinfo *)cyg_fileio_erofs);
  1247. // -------------------------------------------------------------------------
  1248. // File operations.
  1249. // This set of file operations are used for normal open files.
  1250. static cyg_fileops logfs_fileops =
  1251. { (cyg_fileop_read *) cyg_fileio_erofs, (cyg_fileop_write *) logfs_fo_write,
  1252. (cyg_fileop_lseek *) cyg_fileio_erofs,
  1253. (cyg_fileop_ioctl *) cyg_fileio_erofs, cyg_fileio_seltrue,
  1254. logfs_fo_fsync, logfs_fo_close, (cyg_fileop_fstat *) cyg_fileio_erofs,
  1255. (cyg_fileop_getinfo *) cyg_fileio_erofs,
  1256. (cyg_fileop_setinfo *) cyg_fileio_erofs, };
  1257. // -------------------------------------------------------------------------
  1258. // logfs_mount()
  1259. // Process a mount request. This mainly finds root for the
  1260. // filesystem.
  1261. static int logfs_mount(cyg_fstab_entry *fste, cyg_mtab_entry *mte)
  1262. {
  1263. return ENOERR;
  1264. }
  1265. static int logfs_umount(cyg_mtab_entry *mte)
  1266. {
  1267. return ENOERR;
  1268. }
  1269. static int logfs_open(cyg_mtab_entry *mte, cyg_dir dir, const char *name,
  1270. int mode, cyg_file *file)
  1271. {
  1272. file->f_flag |= mode & CYG_FILE_MODE_MASK;
  1273. file->f_type = CYG_FILE_TYPE_FILE;
  1274. file->f_ops = &logfs_fileops;
  1275. file->f_offset = 0;
  1276. file->f_data = 0;
  1277. file->f_xops = 0;
  1278. return ENOERR;
  1279. }
  1280. // -------------------------------------------------------------------------
  1281. // logfs_fo_write()
  1282. // Write data to file.
  1283. static int logfs_fo_write(struct CYG_FILE_TAG *fp, struct CYG_UIO_TAG *uio)
  1284. {
  1285. int i;
  1286. for (i = 0; i < uio->uio_iovcnt; i++)
  1287. {
  1288. cyg_iovec *iov = &uio->uio_iov[i];
  1289. char *buf = (char *) iov->iov_base;
  1290. off_t len = iov->iov_len;
  1291. diag_write(buf, len);
  1292. }
  1293. uio->uio_resid = 0;
  1294. return ENOERR;
  1295. }
  1296. static int logfs_fo_fsync(struct CYG_FILE_TAG *fp, int mode)
  1297. {
  1298. return ENOERR;
  1299. }
  1300. // -------------------------------------------------------------------------
  1301. // romfs_fo_close()
  1302. // Close a file. We just clear out the data pointer.
  1303. static int logfs_fo_close(struct CYG_FILE_TAG *fp)
  1304. {
  1305. return ENOERR;
  1306. }