21 #include <sys/termios.h> 22 #include <sys/types.h> 24 #include <sys/socket.h> 25 #include <netinet/in.h> 26 #include <arpa/inet.h> 28 #include <rtems/malloc.h> 29 #include <rtems/error.h> 30 #include <rtems/stackchk.h> 31 #include <rtems/rtems_bsdnet.h> 32 #include <rtems/imfs.h> 33 #include <librtemsNfs.h> 36 #include "epicsVersion.h" 43 #include "osiUnistd.h" 50 #define RTEMS_VERSION_INT VERSION_INT(__RTEMS_MAJOR__, __RTEMS_MINOR__, 0, 0) 55 int main(
int argc,
char **argv);
60 void rtems_bsp_reset_cause(
char *buf,
size_t capacity)
__attribute__((weak));
61 void (*fp)(
char *buf,
size_t capacity) = rtems_bsp_reset_cause;
82 delayedPanic (
const char *msg)
86 rtems_task_wake_after (rtemsTicksPerSecond);
109 if ((p = malloc (size)) ==
NULL)
110 LogFatal (
"Can't allocate space for %s.\n", msg);
119 #ifdef OMIT_NFS_SUPPORT 120 # include <rtems/tftp.h> 132 if(epicsRtemsFSImage==(
void*)&epicsRtemsFSImage)
134 else if(epicsRtemsFSImage==
NULL)
137 printf(
"***** Using compiled in file data *****\n");
139 printf(
"Can't unpack tar filesystem\n");
149 initialize_local_filesystem(
char **argv)
155 argv[0] = rtems_bsdnet_bootp_boot_file_name;
158 }
else if (_FlashSize && (_DownloadLocation || _FlashBase)) {
159 extern char _edata[];
160 size_t flashIndex = _edata - _DownloadLocation;
161 char *
header = _FlashBase + flashIndex;
163 if (memcmp(header + 257,
"ustar ", 8) == 0) {
165 printf (
"***** Unpack in-memory file system (IMFS) *****\n");
166 if (rtems_tarfs_load(
"/", (
unsigned char *)header, (
size_t)_FlashSize - flashIndex) != 0) {
167 printf(
"Can't unpack tar filesystem\n");
170 if ((fd = open(rtems_bsdnet_bootp_cmdline, 0)) >= 0) {
172 printf (
"***** Found startup script (%s) in IMFS *****\n", rtems_bsdnet_bootp_cmdline);
173 argv[1] = rtems_bsdnet_bootp_cmdline;
176 printf (
"***** Startup script (%s) not in IMFS *****\n", rtems_bsdnet_bootp_cmdline);
182 #ifndef OMIT_NFS_SUPPORT 183 #if __RTEMS_MAJOR__>4 || \ 184 (__RTEMS_MAJOR__==4 && __RTEMS_MINOR__>9) || \ 185 (__RTEMS_MAJOR__==4 && __RTEMS_MINOR__==9 && __RTEMS_REVISION__==99) 187 nfsMount(
char *uidhost,
char *path,
char *mntpoint)
189 int devl = strlen(uidhost) + strlen(path) + 2;
193 if ((dev = malloc(devl)) ==
NULL) {
194 fprintf(
stderr,
"nfsMount: out of memory\n");
197 sprintf(dev,
"%s:%s", uidhost, path);
198 printf(
"Mount %s on %s\n", dev, mntpoint);
199 if (rtems_mkdir(mntpoint, S_IRWXU | S_IRWXG | S_IRWXO))
200 printf(
"Warning -- unable to make directory \"%s\"\n", mntpoint);
201 if (mount(dev, mntpoint, RTEMS_FILESYSTEM_TYPE_NFS,
202 RTEMS_FILESYSTEM_READ_WRITE,
NULL)) {
203 perror(
"mount failed");
213 #define NFS_INIT rpcUdpInit(); nfsInit(0,0); 218 initialize_remote_filesystem(
char **argv,
int hasLocalFilesystem)
220 #ifdef OMIT_NFS_SUPPORT 221 printf (
"***** Initializing TFTP *****\n");
222 #if __RTEMS_MAJOR__>4 || \ 223 (__RTEMS_MAJOR__==4 && __RTEMS_MINOR__>9) || \ 224 (__RTEMS_MAJOR__==4 && __RTEMS_MINOR__==9 && __RTEMS_REVISION__==99) 225 mount_and_make_target_path(
NULL,
227 RTEMS_FILESYSTEM_TYPE_TFTPFS,
228 RTEMS_FILESYSTEM_READ_WRITE,
231 rtems_bsdnet_initialize_tftp_filesystem ();
233 if (!hasLocalFilesystem) {
238 path =
mustMalloc(pathsize,
"Command path name ");
239 strcpy (path,
"/TFTP/BOOTP_HOST/epics/");
241 if (gethostname (&path[l], pathsize - l - 10) || (path[l] ==
'\0'))
245 strcat (path,
"/st.cmd");
255 printf (
"***** Initializing NFS *****\n");
262 while ((cp = strchr(cp+1,
'/')) !=
NULL) {
264 if ((mkdir (mount_point, 0755) != 0)
265 && (errno != EEXIST))
266 LogFatal(
"Can't create directory \"%s\": %s.\n",
267 mount_point, strerror(errno));
270 argv[1] = rtems_bsdnet_bootp_cmdline;
272 else if (hasLocalFilesystem) {
282 if (rtems_bsdnet_bootp_cmdline[0] ==
'/')
283 cp = rtems_bsdnet_bootp_cmdline + 1;
285 cp = rtems_bsdnet_bootp_cmdline;
286 cp = strchr(cp,
'/');
288 || ((l = cp - rtems_bsdnet_bootp_cmdline) == 0))
289 LogFatal(
"\"%s\" is not a valid command pathname.\n", rtems_bsdnet_bootp_cmdline);
292 server_name = rtems_bsdnet_bootp_server_name;
293 if (rtems_bsdnet_bootp_cmdline[0] ==
'/') {
294 mount_point = server_path;
295 strncpy(mount_point, rtems_bsdnet_bootp_cmdline, l);
296 mount_point[l] =
'\0';
297 argv[1] = rtems_bsdnet_bootp_cmdline;
311 const size_t allocSize = strlen ( server_name ) + 2;
312 char *
const pServerName =
mustMalloc( allocSize,
314 char *
const pServerPath =
mustMalloc ( allocSize,
316 const int scanfStatus = sscanf (
321 if ( scanfStatus == 2 ) {
322 pServerPath[0u]=
'/';
323 server_name = pServerName;
324 server_path = pServerPath;
327 free ( pServerName );
328 free ( pServerPath );
333 char *abspath =
mustMalloc(strlen(rtems_bsdnet_bootp_cmdline)+2,
"Absolute command path");
334 strcpy(server_path,
"/tftpboot/");
335 mount_point = server_path + strlen(server_path);
336 strncpy(mount_point, rtems_bsdnet_bootp_cmdline, l);
337 mount_point[l] =
'\0';
339 strcpy(abspath,
"/");
340 strcat(abspath, rtems_bsdnet_bootp_cmdline);
345 server_name, server_path, mount_point);
346 nfsMount(server_name, server_path, mount_point);
351 const char rtems_etc_hosts[] =
"127.0.0.1 localhost\n";
355 void fixup_hosts(
void)
361 ret=stat(
"/etc/hosts", &STAT);
365 }
else if(errno!=ENOENT) {
366 perror(
"error: fixup_hosts stat /etc/hosts");
370 ret = mkdir(
"/etc", 0775);
371 if(ret!=0 && errno!=EEXIST)
373 perror(
"error: fixup_hosts create /etc");
377 if((fp=fopen(
"/etc/hosts",
"w"))==
NULL)
379 perror(
"error: fixup_hosts create /etc/hosts");
382 if(fwrite(rtems_etc_hosts, 1,
sizeof(rtems_etc_hosts)-1, fp)!=
sizeof(rtems_etc_hosts)-1)
384 perror(
"error: failed to write /etc/hosts");
395 set_directory (
const char *commandline)
401 cp = strrchr(commandline,
'/');
407 l = cp - commandline;
410 directoryPath =
mustMalloc(l + 2,
"Command path directory ");
411 strncpy(directoryPath, cp, l);
412 directoryPath[l] =
'/';
413 directoryPath[l+1] =
'\0';
414 if (chdir (directoryPath) < 0)
415 LogFatal (
"Can't set initial directory(%s): %s\n", directoryPath, strerror(errno));
430 rtems_netstat (
unsigned int level)
432 rtems_bsdnet_show_if_stats ();
433 rtems_bsdnet_show_mbuf_stats ();
435 rtems_bsdnet_show_inet_routes ();
438 rtems_bsdnet_show_ip_stats ();
439 rtems_bsdnet_show_icmp_stats ();
440 rtems_bsdnet_show_udp_stats ();
441 rtems_bsdnet_show_tcp_stats ();
446 static const iocshArg *
const netStatArgs[1] = {&netStatArg0};
447 static const iocshFuncDef netStatFuncDef = {
"netstat",1,netStatArgs};
448 static void netStatCallFunc(
const iocshArgBuf *args)
450 rtems_netstat(args[0].ival);
454 static void heapSpaceCallFunc(
const iocshArgBuf *args)
456 rtems_malloc_statistics_t s;
459 malloc_get_statistics(&s);
460 x = s.space_available - (
unsigned long)(s.lifetime_allocated - s.lifetime_freed);
462 printf(
"Heap space: %.1f MB\n", x / (1024 * 1024));
464 printf(
"Heap space: %.1f kB\n", x / 1024);
467 #ifndef OMIT_NFS_SUPPORT 471 static const iocshArg *
const nfsMountArgs[3] = {&nfsMountArg0,&nfsMountArg1,
473 static const iocshFuncDef nfsMountFuncDef = {
"nfsMount",3,nfsMountArgs};
474 static void nfsMountCallFunc(
const iocshArgBuf *args)
476 char *cp = args[2].
sval;
477 while ((cp = strchr(cp+1,
'/')) !=
NULL) {
479 if ((mkdir (args[2].sval, 0755) != 0) && (errno != EEXIST)) {
480 printf(
"Can't create directory \"%s\": %s.\n",
481 args[2].sval, strerror(errno));
486 nfsMount(args[0].sval, args[1].sval, args[2].sval);
494 setenv(
"TZ", zone, 1);
501 static const iocshArg *
const zonesetArgs[1] = {&zonesetArg0};
502 static const iocshFuncDef zonesetFuncDef = {
"zoneset",1,zonesetArgs};
503 static void zonesetCallFunc(
const iocshArgBuf *args)
512 static void iocshRegisterRTEMS (
void)
516 #ifndef OMIT_NFS_SUPPORT 530 if (tcgetattr (fileno (
stdin), &t) < 0) {
531 printf (
"tcgetattr failed: %s\n", strerror (errno));
534 t.c_iflag &= ~(IXOFF | IXON | IXANY);
535 if (tcsetattr (fileno (
stdin), TCSANOW, &t) < 0) {
536 printf (
"tcsetattr failed: %s\n", strerror (errno));
558 rtems_shutdown_executive(0);
565 Init (rtems_task_argument ignored)
568 char *argv[3] = {
NULL,
NULL, NULL };
570 rtems_task_priority newpri;
571 rtems_status_code sc;
572 rtems_time_of_day now;
583 delayedPanic(
"epicsRtemsInitPreSetBootConfigFromNVRAM");
584 if (rtems_bsdnet_config.bootp == NULL) {
589 delayedPanic(
"epicsRtemsInitPostSetBootConfigFromNVRAM");
594 rtems_task_set_priority (
604 putenv (
"IOCSH_HISTSIZE=20");
609 printf(
"\n***** RTEMS Version: %s *****\n",
610 rtems_get_version_string());
615 if ((cp = getenv(
"EPICS_TS_NTP_INET")) != NULL)
616 rtems_bsdnet_config.ntp_server[0] = cp;
617 if (rtems_bsdnet_config.network_task_priority == 0)
626 printf(
"\n***** Initializing network *****\n");
627 rtems_bsdnet_initialize_network();
628 printf(
"\n***** Setting up file system *****\n");
629 initialize_remote_filesystem(argv, initialize_local_filesystem(argv));
637 gethostname(hostname, 1023);
638 char *cp =
mustMalloc(strlen(hostname)+3,
"iocsh prompt");
639 sprintf(cp,
"%s> ", hostname);
649 if (rtems_clock_get(RTEMS_CLOCK_GET_TOD,&now) != RTEMS_SUCCESSFUL) {
657 if ((sc = rtems_clock_set (&now)) != RTEMS_SUCCESSFUL)
658 printf (
"***** Can't set time: %s\n", rtems_status_text (sc));
660 if (getenv(
"TZ") == NULL) {
663 printf(
"Warning: No timezone information, times will be displayed in UTC.\n");
673 printf (
"***** Preparing EPICS application *****\n");
674 iocshRegisterRTEMS ();
675 set_directory (argv[1]);
679 printf (
"***** Starting EPICS application *****\n");
680 result =
main ((
sizeof argv /
sizeof argv[0]) - 1, argv);
681 printf (
"***** IOC application terminating *****\n");
686 #if defined(QEMU_FIXUPS) 693 #if defined(__i386__) && !USE_COM1_AS_CONSOLE 695 extern int BSPPrintkPort;
696 void bsp_predriver_hook(
void)
698 BSPConsolePort = BSP_CONSOLE_PORT_COM1;
699 BSPPrintkPort = BSP_CONSOLE_PORT_COM1;
704 #if defined(__i386__) && BSP_PRESS_KEY_FOR_RESET 705 void bsp_cleanup(
void)
707 #if RTEMS_VERSION_INT>=VERSION_INT(4,10,0,0)
LIBCOM_API const ENV_PARAM EPICS_TZ
int epicsRtemsInitPreSetBootConfigFromNVRAM(struct rtems_bsdnet_config *config)
struct rtems_bsdnet_config rtems_bsdnet_config
LIBCOM_API const char *epicsStdCall envGetConfigParamPtr(const ENV_PARAM *pParam)
Get a configuration parameter's value or default string.
int errlogVprintf(const char *pFormat, va_list pvar)
Routines to get and set EPICS environment parameters.
void epicsStdCall iocshRegister(const iocshFuncDef *piocshFuncDef, iocshCallFunc func)
void osdTimeRegister(void)
LIBCOM_API void epicsStdCall epicsEnvSet(const char *name, const char *value)
Set an environment variable's value.
#define epicsThreadPriorityIocsh
const void * rtemsConfigArray[]
int main(int argc, char **argv)
int epicsThreadGetOssPriorityValue(unsigned int osiPriority)
const epicsMemFS *epicsRtemsFSImage __attribute__((weak))
LIBCOM_API void epicsExit(int status)
Calls epicsExitCallAtExits(), then the OS exit() routine.
Extended replacement for the Posix exit and atexit routines.
rtems_configuration_table Configuration
rtems_task Init(rtems_task_argument ignored)
int epicsMemFsLoad(const epicsMemFS *fs)
int errlogPrintf(const char *pFormat,...)
int epicsRtemsMountLocalFilesystem(char **argv) __attribute__((weak))
LIBCOM_API epicsThreadBooleanStatus epicsStdCall epicsThreadHighestPriorityLevelBelow(unsigned int priority, unsigned *pPriorityJustBelow)
LIBCOM_API void epicsStdCall epicsThreadSleep(double seconds)
Block the calling thread for at least the specified time.
#define epicsThreadPriorityScanLow
void setBootConfigFromNVRAM(void)
void LogFatal(const char *msg,...)
void * mustMalloc(int size, const char *msg)
EPICS time-stamps (epicsTimeStamp), epicsTime C++ class and C functions for handling wall-clock times...
C++ and C descriptions for a thread.
const epicsMemFS * epicsRtemsFSImage
int epicsRtemsInitPostSetBootConfigFromNVRAM(struct rtems_bsdnet_config *config)
rtems_interval rtemsTicksPerSecond
void zoneset(const char *zone)