Example #1
0
int main(int argc, char *argv[])
{
	//int result;
	core_options opts(oplist);
	astring aerror("");

	fprintf(stderr, "%s", "WARNING: This is Work In Progress! - It may fail anytime\n");
	if (!opts.parse_command_line(argc, argv, OPTION_PRIORITY_DEFAULT, aerror))
	{
		fprintf(stderr, "%s\n", aerror.cstr());
		usage(opts);
		return 1;
	}

	if (opts.bool_value("h"))
	{
		usage(opts);
		return 1;
	}

	if (opts.bool_value("ld"))
	{
		listdevices();
	}
	else
	{
		run(opts);
	}

	return 0;
}
Example #2
0
int main(int argc, char *argv[])
{
#if (!PSTANDALONE)
    track_memory(true);
    {
#endif
        tool_options_t opts;
        int ret;

        fprintf(stderr, "%s", "WARNING: This is Work In Progress! - It may fail anytime\n");
        fprintf(stderr, "Update dispatching using method %s\n", pmf_verbose[NL_PMF_TYPE]);
        if ((ret = opts.parse(argc, argv)) != argc)
        {
            fprintf(stderr, "Error parsing %s\n", argv[ret]);
            usage(opts);
            return 1;
        }

        if (opts.opt_help())
        {
            usage(opts);
            return 1;
        }

        pstring cmd = opts.opt_cmd();
        if (cmd == "listdevices")
            listdevices();
        else if (cmd == "run")
            run(opts);
        else if (cmd == "convert")
        {
            pstring contents = filetobuf(opts.opt_file());
            nl_convert_base_t *converter = NULL;
            if (opts.opt_type().equals("spice"))
                converter = palloc(nl_convert_spice_t);
            else
                converter = palloc(nl_convert_eagle_t);
            converter->convert(contents);
            /* present result */
            printf("%s\n", converter->result().cstr());
            pfree(converter);
        }
        else
        {
            fprintf(stderr, "Unknown command %s\n", cmd.cstr());
            usage(opts);
            return 1;
        }
#if (!PSTANDALONE)
    }
    dump_unfreed_mem();
#endif
    return 0;
}
Example #3
0
int tool_app_t::execute()
{
	tool_app_t opts;

	/* make SIGFPE actually deliver signals on supoorted platforms */
	plib::fpsignalenabler::global_enable(true);
	plib::fpsignalenabler sigen(plib::FP_ALL & ~plib::FP_INEXACT & ~plib::FP_UNDERFLOW);

	if (opt_help())
	{
		pout(usage());
		return 0;
	}

	if (opt_version())
	{
		pout(
			"nltool (netlist) " PSTRINGIFY(NLTOOL_VERSION) "\n"
			"Copyright (C) 2019 Couriersud\n"
			"License GPLv2+: GNU GPL version 2 or later <http://gnu.org/licenses/gpl.html>.\n"
			"This is free software: you are free to change and redistribute it.\n"
			"There is NO WARRANTY, to the extent permitted by law.\n\n"
			"Written by Couriersud.\n");
		if (opt_verb())
		{
			std::vector<std::pair<pstring, pstring>> defs;
			netlist::netlist_state_t::compile_defines(defs);
			pout("\nCompile defines:\n");
			for (auto &x : defs)
				pout("{1:-30} = {2}\n", x.first, x.second);

		}
		return 0;
	}

	m_options = opt_defines();
	m_options.emplace_back("NLTOOL_VERSION=" PSTRINGIFY(NLTOOL_VERSION));

	try
	{
		pstring cmd = opt_cmd.as_string();
		if (cmd == "listdevices")
			listdevices();
		else if (cmd == "run")
			run();
		else if (cmd == "validate")
			validate();
		else if (cmd == "static")
			static_compile();
		else if (cmd == "header")
			create_header();
		else if (cmd == "docheader")
			create_docheader();
		else if (cmd == "convert")
			convert();
		else
		{
			perr("Unknown command {}\n", cmd.c_str());
			//FIXME: usage_short
			perr(usage());
			return 1;
		}
	}
	catch (netlist::nl_exception &e)
	{
		perr("Netlist exception caught: {}\n", e.text());
		return 2;
	}
	catch (plib::pexception &e)
	{
		perr("plib exception caught: {}\n", e.text());
		return 2;
	}

	return 0;
}
Example #4
0
int main(int argc, char **argv, char **envp)
{
	int fd_stdin=-1, fd_stdout=-1;
	pid_t pid;
	int retries = 0;
	int rootmounted = 0;
	char *rootdev = NULL;
	char *rootfstype = "unknown";
	char *rootfslabel = ROOTFSLABEL;
	char *initprg = "/sbin/init";

	conf.mountflags = MS_SYNCHRONOUS;
	
	open_console();

	/* create mountpoints /dev and /proc /rootfs */
	
	mkmountpoint("/dev");
	mkmountpoint("/proc");
	mkmountpoint("/rootfs");

	open_console();
	
	/* make sure /dev is mounted */
	if(mount("devtmpfs", "/dev",
		 "devtmpfs", 0,
		 "")) {
		if(fstdout) {
			if(fstdout) {
				fprintf(fstdout, "INIT: could not mount devtmpfs on /dev: %s\n", strerror(errno));
				fprintf(fstdout, "INIT: waiting 10 seconds ..\n");
				sleep(5);
				sleep(5);
			}
		}	
	}

	open_console();
	
	/* mount /proc */
	if(mount("proc", "/proc",
		 "proc", 0,
		 "")) {
		if(fstdout) {
			fprintf(fstdout, "INIT: could not mount proc on /proc: %s\n", strerror(errno));
			fprintf(fstdout, "INIT: waiting 10 seconds ..\n");
			sleep(5);
			sleep(5);
		}	
	}
	
	/* parse kernel commandline options */
	cmdline.autoresize = 1;
	cmdline_parse();
	
	if(cmdline.init) {
		initprg = cmdline.init;
	}
	
	if(cmdline.async) {
		conf.mountflags ^= MS_SYNCHRONOUS;
	}
	
	if(cmdline.usbreset) {
		usbreset();
		sleep(1);
	}
	
	if(cmdline.rootdelay > 0) {
		if(fstdout) fprintf(fstdout, "INIT: rootdelay, waiting %d seconds\n", cmdline.rootdelay);
		sleep(cmdline.rootdelay);
	}
	
	if(cmdline.rootdev) {
		struct stat statb;
		if(fstdout) fprintf(fstdout, "INIT: rootdev %s given on commandline\n", cmdline.rootdev);
		rootdev = cmdline.rootdev;
		if(stat(rootdev, &statb)) {
			if(fstdout) fprintf(fstdout, "INIT: rootdev %s not found!\n", rootdev);
			rootdev = NULL;
		}
	}
	
	if(!rootdev) {
		if(cmdline.rootfslabel) {
			if(strncmp(cmdline.rootfslabel, "LABEL=", 6)) {
				rootfslabel = malloc(strlen("LABEL=") + strlen(cmdline.rootfslabel) + 1);
				strcpy(rootfslabel, "LABEL=");
				strcat(rootfslabel, cmdline.rootfslabel);
			} else {
				rootfslabel = cmdline.rootfslabel;
			}
		}
		if(fstdout) fprintf(fstdout, "INIT: probing for rootdev labeled '%s'\n", rootfslabel);
		fsprobe_init();
		while( (rootdev = fsprobe_get_devname_by_spec(rootfslabel)) == NULL ) {
			retries++;
			if(retries == 15) usbreset();
			if(retries > 25) break;
			if(fstdout) fprintf(fstdout, "INIT: waiting for rootdevice to be available\n");
			sleep(2);
			fsprobe_init();
		}
	}
	
	if(!rootdev) {
		if(fstdout) {
			fprintf(fstdout, "INIT: could not find rootdevice\n");
			listdevices();
			fprintf(fstdout, "INIT: waiting 10 seconds ..\n");
			sleep(5);
			sleep(5);
			fprintf(fstdout, "INIT: guessing that rootdev is /dev/sda1\n\n");
		}
		rootdev="/dev/sda1";
	} else {
		if(fstdout) fprintf(fstdout, "INIT: using rootdev %s\n", rootdev);
	}
	sleep(1);

	/* if filesystem is of the ext family */
	if((!cmdline.rootfstype) || (!strncmp(cmdline.rootfstype, "ext", 3))) {
		int status = 0;
		int fsckok = 0;
		/* e2fsck -y rootdev */
		/* fork + exec("/e2fsck", "/e2fsck"-y", rootdev) */
		if((pid=fork())==0) {
			execl("/e2fsck", "/e2fsck", "-y", rootdev, NULL);
			exit(0);
		}
		if(pid != -1) {
			wait(&status);
			if(WIFEXITED(status)) {
				fprintf(fstdout, "INIT: e2fsck exited normally with exit code: %d\n", WEXITSTATUS(status));
				if(WEXITSTATUS(status)==0) fsckok=1;
			} else {
				fprintf(fstdout, "INIT: e2fsck exited abnormally\n");
			}
		}
		if(fsckok) {
			if(cmdline.autoresize) {
				sync();
				if((pid=fork())==0) {
					execl("/resize2fs", "/resize2fs", "-f", rootdev, NULL);
					exit(0);
				}
				if(pid != -1) wait(NULL);
				sync();
			}
		}
	}
	
	/* unlink /e2sck and /resize2fs to save some memory */
	if(unlink("/e2fsck")) {
		fprintf(fstdout, "INIT: unlink(\"/e2fsck\") failed: %s\n", strerror(errno));
	}
	if(unlink("/resize2fs")) {
		fprintf(fstdout, "INIT: unlink(\"/resize2fs\") failed: %s\n", strerror(errno));
	}
	
	if(cmdline.rootfstype) {
		/* mount /rootfs, try fstype supplied on kernel commandline */
		if(mount(rootdev, "/rootfs", cmdline.rootfstype, MS_NOATIME|MS_RDONLY|conf.mountflags, "")) {
			if(fstdout) fprintf(fstdout, "INIT: failed to mount (%s) %s: %s\n",
						 cmdline.rootfstype, rootdev, strerror(errno));
		} else {
			rootmounted = 1;
			rootfstype = cmdline.rootfstype;
		}
	}
	if(!rootmounted) {
		/* mount /rootfs, try ext2 */
		if(mount(rootdev, "/rootfs", "ext2", MS_NOATIME|MS_RDONLY|conf.mountflags, "")) {
			if(fstdout) fprintf(fstdout, "INIT: failed to mount (ext2) %s: %s\n", rootdev, strerror(errno));
			goto forever;
		}
		rootfstype = "ext2";
	}
	
	if(fstdout) fprintf(fstdout, "INIT: (%s) %s mounted.\n", rootfstype, rootdev);
	sleep(1);

	if(cmdline.install) {
		if(fstdout) {
			fprintf(fstdout, "INIT: INSTALL INVOKED!\nINSTALL: PROCEEDING IN 5 SECONDS!\n");
			sleep(5);
		}
		
		/* unpack install archive in initramfs root */
		{
			int fd;
			fd = open("/rootfs/install.tar", O_RDONLY);
			if(fd == -1) {
				if(fstdout) fprintf(fstdout, "INSTALL: failed to open '/rootfs/install.tar'\n");
			} else {
				if(fstdout) fprintf(fstdout, "INSTALL: '/rootfs/install.tar' opened ok\n");
				tar_extract(fd);
				close(fd);
			}
		}

		/* Be nice and prepare stdin and stdout for the install program */
		fd_stdin = open("/dev/console", O_RDONLY|O_NOCTTY);
		if(fd_stdin == -1) {
			if(fstdout)
				fprintf(fstdout, "INSTALL: open(\"/dev/console\", O_RDONLY) failed: %s\n", strerror(errno));
		} else {
			if(fd_stdin != 0) {
				dup2(fd_stdin, 0);
				close(fd_stdin);
			}
		}
		fd_stdout = open("/dev/console", O_WRONLY|O_NOCTTY);
		if(fd_stdout == -1) {
			if(fstdout)
				fprintf(fstdout, "INSTALL: open(\"/dev/console\", O_WRONLY) failed: %s\n", strerror(errno));
		} else { 
			if(fd_stdout != 1) {
				dup2(fd_stdout, 1);
				dup2(fd_stdout, 2);
				close(fd_stdout);
			}
		}

		/* exec "/install" */
		{
			char *iv[2];
		iv[0] = "/install";
		iv[1] = NULL;
		if(fstdout)
			fprintf(fstdout, "INSTALL: exec(\"/install\")\n");
		execve(iv[0], iv, envp);
		if(fstdout)
			fprintf(fstdout, "INSTALL: exec(\"/install\") failed\n");
		sleep(10);
		}
	}
	
	if (mount("/dev", "/rootfs/dev", NULL, MS_MOVE, NULL) < 0) {
		if(fstdout)
			fprintf(fstdout, "INIT: failed to mount moving /dev to /rootfs/dev\n");
		sleep(1);
	}
	if (mount("/proc", "/rootfs/proc", NULL, MS_MOVE, NULL) < 0) {
		if(fstdout)
			fprintf(fstdout, "INIT: failed to mount moving /proc to /rootfs/proc\n");
		sleep(1);
	}

	/*
         * mkdir /sbin
 	 * copy /rootfs/sbin/init to /sbin/init
         * mount --bind /rootfs/sbin/init /sbin/init
         */
	if(mkdir("/sbin", 0755)) {
		if(fstdout)
			fprintf(fstdout, "INIT: mkdir(\"/sbin\") failed: %s\n", strerror(errno));
		sleep(1);
	} else {
		int fd, ofd;
		ssize_t siz;
		struct stat statb;
		
		if(stat("/rootfs/sbin/init", &statb)) {
			if(fstdout) fprintf(fstdout, "INIT: stat(\"/rootfs/sbin/init\") failed: %s\n", strerror(errno));
			sleep(1);
			goto noinitcopy;
		}
		siz = statb.st_size;

		if(mount("tmpfs", "/sbin","tmpfs", 0,"")) {
			if(fstdout) fprintf(fstdout, "INIT: mount(\"tmpfs\", \"/sbin\") failed: %s\n", strerror(errno));
		}
		if( (fd = open("/rootfs/sbin/init", O_RDONLY)) == -1) {
			if(fstdout) fprintf(fstdout, "INIT: open(\"/rootfs/sbin/init\", O_RDONLY) failed: %s\n", strerror(errno));
			sleep(1);
			goto noinitcopy;
		}
		if( (ofd = open("/sbin/init", O_WRONLY|O_CREAT|O_TRUNC, 0755)) == -1) {
			if(fstdout) fprintf(fstdout, "INIT: open(\"/sbin/init\", O_WRONLY|O_CREAT|O_TRUNC) failed: %s\n", strerror(errno));
			sleep(1);
			close(fd);
			goto noinitcopy;
		}
		if(data(fd, siz, ofd)) {
			if(fstdout) fprintf(fstdout, "INIT: datacopy of /sbin/init failed: %s\n", strerror(errno));
			sleep(1);
			close(fd);
			close(ofd);
			goto noinitcopy;
		}
		close(fd);
		if(close(ofd)) {
			if(fstdout) fprintf(fstdout, "INIT: close(\"/sbin/init\") failed: %s\n", strerror(errno));
			sleep(1);
			goto noinitcopy;
		}
		if (mount("rootfs", "/", NULL, MS_REMOUNT|MS_NOATIME|MS_RDONLY, NULL) < 0) {
			if(fstdout)
				fprintf(fstdout, "INIT: failed to remount initramfs as read-only\n");
			sleep(1);
		}
		if (mount("tmpfs", "/sbin", NULL, MS_REMOUNT|MS_NOATIME|MS_RDONLY, NULL) < 0) {
			if(fstdout)
				fprintf(fstdout, "INIT: failed to remount \"/sbin\" as read-only\n");
			sleep(1);
		}
		
		if(mount("/sbin/init", "/rootfs/sbin/init", "tmpfs", MS_BIND|MS_RDONLY, NULL)) {
			if(mount("/sbin/init", "/rootfs/sbin/init", "tmpfs", MS_BIND, NULL)) {
				if(fstdout)
					fprintf(fstdout, "INIT: bind mount(\"/sbin/init\", \"/rootfs/sbin/init\") failed: %s\n", strerror(errno));
				sleep(1);
			}
		}
	}
noinitcopy:

	if(chdir("/rootfs")) {
		if(fstdout)
			fprintf(fstdout, "INIT: chdir(\"/rootfs\") failed: %s\n", strerror(errno));
		goto forever;
	}

	if (mount("/rootfs", "/", NULL, MS_MOVE, NULL) < 0) {
		if(fstdout)
			fprintf(fstdout, "INIT: failed to mount moving /rootfs to /\n");
		goto forever;
	}

        /*
	  Now, the init process may still access the old root via its
	  executable, shared libraries, standard input/output/error, and its
	  current root directory. All these references are dropped by the
	  following command:
	  
	  # exec chroot . /sbin/init <dev/console >dev/console 2>&1
	*/
	
	if(chroot(".")) {
		if(fstdout) {
			fprintf(fstdout, "INIT: chroot(\".\") failed: %s\n", strerror(errno));
			sleep(5);
			sleep(5);
		}
	}

	/* check if we need to copy default versions of some config files */
	{
		int remounted=0;
		chk_cfg_file(&remounted, "ssh/ssh_config");
		chk_cfg_file(&remounted, "ssh/sshd_config");
		chk_cfg_file(&remounted, "inetd.conf");
		chk_cfg_file(&remounted, "inittab");
		chk_cfg_file(&remounted, "login.defs");
		chk_cfg_file(&remounted, "limits");
		chk_cfg_file(&remounted, "login.access");
		if(remounted)
			if (mount("rootfs", "/", NULL, MS_REMOUNT|MS_NOATIME|MS_RDONLY|conf.mountflags, NULL) < 0) {
				if(fstdout)
					fprintf(fstdout, "INIT: failed to remount rootfs as read-only\n");
				sleep(1);
			}
	}
		
	fd_stdin = open("/dev/console", O_RDONLY|O_NOCTTY);
	if(fd_stdin == -1) {
		if(fstdout)
			fprintf(fstdout, "INIT: open(\"/dev/console\", O_RDONLY) failed: %s\n", strerror(errno));
	} else {
		if(fd_stdin != 0) {
			dup2(fd_stdin, 0);
			close(fd_stdin);
		}
	}
	fd_stdout = open("/dev/console", O_WRONLY|O_NOCTTY);
	if(fd_stdout == -1) {
		if(fstdout)
			fprintf(fstdout, "INIT: open(\"/dev/console\", O_WRONLY) failed: %s\n", strerror(errno));
	} else { 
		if(fd_stdout != 1) {
			dup2(fd_stdout, 1);
			dup2(fd_stdout, 2);
			close(fd_stdout);
		}
	}

	if(fstdout)
		fprintf(fstdout, "INIT: now execing \"%s\"\n", initprg);
	sleep(1);

	if(fstdout) {
		fclose(fstdout);
		fstdout = NULL;
	}
	argv[0] = initprg;
	execve(initprg, argv, envp);
	
	open_console();

	if(fstdout) {
		fprintf(fstdout, "INIT: execve(\"%s\") failed: %s\n", initprg, strerror(errno));
	}
forever:
	if(fstdout) {
		fprintf(fstdout, "INIT: pid is %u\n", getpid());
		fprintf(fstdout, "INIT: boot cannot proceed from here.\n");
		fprintf(fstdout, "INIT: turn off the computer.\n");
	}
	while(1) sleep(1000);
	return 2;
}
Example #5
0
int main(int argc, char *argv[])
{
	tool_options_t opts;
	int ret;

	perr("{}", "WARNING: This is Work In Progress! - It may fail anytime\n");
	//perr("Update dispatching using method {}\n", pmf_verbose[NL_PMF_TYPE]);
	if ((ret = opts.parse(argc, argv)) != argc)
	{
		perr("Error parsing {}\n", argv[ret]);
		usage(opts);
		return 1;
	}

	if (opts.opt_help())
	{
		usage(opts);
		return 1;
	}

	pstring cmd = opts.opt_cmd();
	if (cmd == "listdevices")
		listdevices();
	else if (cmd == "run")
		run(opts);
	else if (cmd == "static")
		static_compile(opts);
	else if (cmd == "convert")
	{
		pstring contents;
		plib::postringstream ostrm;
		if (opts.opt_file() == "-")
		{
			plib::pstdin f;
			ostrm.write(f);
		}
		else
		{
			plib::pifilestream f(opts.opt_file());
			ostrm.write(f);
		}
		contents = ostrm.str();

		pstring result;
		if (opts.opt_type().equals("spice"))
		{
			nl_convert_spice_t c;
			c.convert(contents);
			result = c.result();
		}
		else
		{
			nl_convert_eagle_t c;
			c.convert(contents);
			result = c.result();
		}
		/* present result */
		pout_strm.write(result.cstr());
	}
	else
	{
		perr("Unknown command {}\n", cmd.cstr());
		usage(opts);
		return 1;
	}

	pstring::resetmem();
	return 0;
}
Example #6
0
int tool_app_t::execute()
{
	tool_app_t opts;

	/* make SIGFPE actually deliver signals on supoorted platforms */
	plib::fpsignalenabler::global_enable(true);
	plib::fpsignalenabler sigen(plib::FP_ALL & ~plib::FP_INEXACT & ~plib::FP_UNDERFLOW);

	//perr("{}", "WARNING: This is Work In Progress! - It may fail anytime\n");
	//perr("Update dispatching using method {}\n", pmf_verbose[NL_PMF_TYPE]);
	//printf("test2 %f\n", std::exp(-14362.38064713));

	if (opt_help())
	{
		pout(usage());
		return 0;
	}

	if (opt_version())
	{
		pout(
			"nltool (netlist) 0.1\n"
			"Copyright (C) 2017 Couriersud\n"
			"License GPLv2+: GNU GPL version 2 or later <http://gnu.org/licenses/gpl.html>.\n"
			"This is free software: you are free to change and redistribute it.\n"
			"There is NO WARRANTY, to the extent permitted by law.\n\n"
			"Written by Couriersud.\n");
		return 0;
	}

	try
	{
		pstring cmd = opt_cmd();
		if (cmd == "listdevices")
			listdevices();
		else if (cmd == "run")
			run();
		else if (cmd == "static")
			static_compile();
		else if (cmd == "header")
			create_header();
		else if (cmd == "docheader")
			create_docheader();
		else if (cmd == "convert")
		{
			pstring contents;
			plib::postringstream ostrm;
			if (opt_file() == "-")
			{
				plib::pstdin f;
				ostrm.write(f);
			}
			else
			{
				plib::pifilestream f(opt_file());
				ostrm.write(f);
			}
			contents = ostrm.str();

			pstring result;
			if (opt_type().equals("spice"))
			{
				nl_convert_spice_t c;
				c.convert(contents);
				result = c.result();
			}
			else if (opt_type().equals("eagle"))
			{
				nl_convert_eagle_t c;
				c.convert(contents);
				result = c.result();
			}
			else if (opt_type().equals("rinf"))
			{
				nl_convert_rinf_t c;
				c.convert(contents);
				result = c.result();
			}
			/* present result */
			pout.write(result);
		}
		else
		{
			perr("Unknown command {}\n", cmd.c_str());
			//FIXME: usage_short
			perr(usage());
			return 1;
		}
	}
	catch (netlist::nl_exception &e)
	{
		perr("Netlist exception caught: {}\n", e.text());
	}
	catch (plib::pexception &e)
	{
		perr("plib exception caught: {}\n", e.text());
	}

#if 0
#define str(x) # x
#define strx(x) str(x)
#define ttt strx(__cplusplus)
	printf("%s\n", ttt);
#endif

	return 0;
}