Ejemplo n.º 1
0
int parse_options(int argc, char *argv[]) {
	int c;
	while((c = getopt(argc, argv, "b:r:w:e:vn:g:fchus:")) != -1) {
		switch(c) {
			case 'b':
				baudRate = serial_get_baud(strtoul(optarg, NULL, 0));
				if (baudRate == SERIAL_BAUD_INVALID) {
					fprintf(stderr,	"Invalid baud rate, valid options are:\n");
					for(baudRate = SERIAL_BAUD_1200; baudRate != SERIAL_BAUD_INVALID; ++baudRate)
						fprintf(stderr, " %d\n", serial_get_baud_int(baudRate));
					return 1;
				}
				break;

			case 'r':
			case 'w':
				rd = rd || c == 'r';
				wr = wr || c == 'w';
				if (rd && wr) {
					fprintf(stderr, "ERROR: Invalid options, can't read & write at the same time\n");
					return 1;
				}
				filename = optarg;
				break;
			case 'e':
				npages = strtoul(optarg, NULL, 0);
				if (npages > 0xFF || npages < 0) {
					fprintf(stderr, "ERROR: You need to specify a page count between 0 and 255");
					return 1;
				}
				break;
			case 'u':
				wu = 1;
				if (rd || wr) {
					fprintf(stderr, "ERROR: Invalid options, can't write unprotect and read/write at the same time\n");
					return 1;
				}
				break;
			case 'v':
				verify = 1;
				break;

			case 'n':
				retry = strtoul(optarg, NULL, 0);
				break;

			case 'g':
				exec_flag = 1;
				execute   = strtoul(optarg, NULL, 0);
				break;
			case 's':
				spage    = strtoul(optarg, NULL, 0);
				break;

			case 'f':
				force_binary = 1;
				break;

			case 'c':
				init_flag = 0;
				break;

			case 'h':
				show_help(argv[0]);
				return 1;
		}
	}

	for (c = optind; c < argc; ++c) {
		if (device) {
			fprintf(stderr, "ERROR: Invalid parameter specified\n");
			show_help(argv[0]);
			return 1;
		}
		device = argv[c];
	}

	if (device == NULL) {
		fprintf(stderr, "ERROR: Device not specified\n");
		show_help(argv[0]);
		return 1;
	}

	if (!wr && verify) {
		fprintf(stderr, "ERROR: Invalid usage, -v is only valid when writing\n");
		show_help(argv[0]);
		return 1;
	}

	return 0;
}
Ejemplo n.º 2
0
int parse_options(int argc, char *argv[])
{
	int c;
	char *pLen;

	while ((c = getopt(argc, argv, "a:b:m:r:w:e:vn:g:jkfcChuos:S:F:i:R")) != -1) {
		switch (c) {
		case 'a':
			port_opts.bus_addr = strtoul(optarg, NULL, 0);
			break;

		case 'b':
			port_opts.baudRate = serial_get_baud(strtoul(optarg, NULL, 0));
			if (port_opts.baudRate == SERIAL_BAUD_INVALID) {
				serial_baud_t baudrate;
				fprintf(stderr,	"Invalid baud rate, valid options are:\n");
				for (baudrate = SERIAL_BAUD_1200; baudrate != SERIAL_BAUD_INVALID; ++baudrate)
					fprintf(stderr, " %d\n", serial_get_baud_int(baudrate));
				return 1;
			}
			break;

		case 'm':
			if (strlen(optarg) != 3
			    || serial_get_bits(optarg) == SERIAL_BITS_INVALID
			    || serial_get_parity(optarg) == SERIAL_PARITY_INVALID
			    || serial_get_stopbit(optarg) == SERIAL_STOPBIT_INVALID) {
				fprintf(stderr, "Invalid serial mode\n");
				return 1;
			}
			port_opts.serial_mode = optarg;
			break;

		case 'r':
		case 'w':
			rd = rd || c == 'r';
			wr = wr || c == 'w';
			if (rd && wr) {
				fprintf(stderr, "ERROR: Invalid options, can't read & write at the same time\n");
				return 1;
			}
			filename = optarg;
			if (filename[0] == '-') {
				force_binary = 1;
			}
			break;
		case 'e':
			if (readwrite_len || start_addr) {
				fprintf(stderr, "ERROR: Invalid options, can't specify start page / num pages and start address/length\n");
				return 1;
			}
			npages = strtoul(optarg, NULL, 0);
			if (npages > 0xFF || npages < 0) {
				fprintf(stderr, "ERROR: You need to specify a page count between 0 and 255");
				return 1;
			}
			if (!npages)
				no_erase = 1;
			break;
		case 'u':
			wu = 1;
			if (rd || wr) {
				fprintf(stderr, "ERROR: Invalid options, can't write unprotect and read/write at the same time\n");
				return 1;
			}
			break;

		case 'j':
			rp = 1;
			if (rd || wr) {
				fprintf(stderr, "ERROR: Invalid options, can't read protect and read/write at the same time\n");
				return 1;
			}
			break;

		case 'k':
			ur = 1;
			if (rd || wr) {
				fprintf(stderr, "ERROR: Invalid options, can't read unprotect and read/write at the same time\n");
				return 1;
			}
			break;

		case 'o':
			eraseOnly = 1;
			if (rd || wr) {
				fprintf(stderr, "ERROR: Invalid options, can't erase-only and read/write at the same time\n");
				return 1;
			}
			break;

		case 'v':
			verify = 1;
			break;

		case 'n':
			retry = strtoul(optarg, NULL, 0);
			break;

		case 'g':
			exec_flag = 1;
			execute   = strtoul(optarg, NULL, 0);
			if (execute % 4 != 0) {
				fprintf(stderr, "ERROR: Execution address must be word-aligned\n");
				return 1;
			}
			break;
		case 's':
			if (readwrite_len || start_addr) {
				fprintf(stderr, "ERROR: Invalid options, can't specify start page / num pages and start address/length\n");
				return 1;
			}
			spage    = strtoul(optarg, NULL, 0);
			break;
		case 'S':
			if (spage || npages) {
				fprintf(stderr, "ERROR: Invalid options, can't specify start page / num pages and start address/length\n");
				return 1;
			} else {
				start_addr = strtoul(optarg, &pLen, 0);
				if (*pLen == ':') {
					pLen++;
					readwrite_len = strtoul(pLen, NULL, 0);
					if (readwrite_len == 0) {
						fprintf(stderr, "ERROR: Invalid options, can't specify zero length\n");
						return 1;
					}
				}
			}
			break;
		case 'F':
			port_opts.rx_frame_max = strtoul(optarg, &pLen, 0);
			if (*pLen == ':') {
				pLen++;
				port_opts.tx_frame_max = strtoul(pLen, NULL, 0);
			}
			if (port_opts.rx_frame_max < 0
			    || port_opts.tx_frame_max < 0) {
				fprintf(stderr, "ERROR: Invalid negative value for option -F\n");
				return 1;
			}
			if (port_opts.rx_frame_max == 0)
				port_opts.rx_frame_max = STM32_MAX_RX_FRAME;
			if (port_opts.tx_frame_max == 0)
				port_opts.tx_frame_max = STM32_MAX_TX_FRAME;
			if (port_opts.rx_frame_max < 20
			    || port_opts.tx_frame_max < 5) {
				fprintf(stderr, "ERROR: current code cannot work with small frames.\n");
				fprintf(stderr, "min(RX) = 20, min(TX) = 5\n");
				return 1;
			}
			if (port_opts.rx_frame_max > STM32_MAX_RX_FRAME) {
				fprintf(stderr, "WARNING: Ignore RX length in option -F\n");
				port_opts.rx_frame_max = STM32_MAX_RX_FRAME;
			}
			if (port_opts.tx_frame_max > STM32_MAX_TX_FRAME) {
				fprintf(stderr, "WARNING: Ignore TX length in option -F\n");
				port_opts.tx_frame_max = STM32_MAX_TX_FRAME;
			}
			break;
		case 'f':
			force_binary = 1;
			break;

		case 'c':
			init_flag = 0;
			break;

		case 'h':
			show_help(argv[0]);
			exit(0);

		case 'i':
			gpio_seq = optarg;
			break;

		case 'R':
			reset_flag = 1;
			break;

		case 'C':
			crc = 1;
			break;
		}
	}

	for (c = optind; c < argc; ++c) {
		if (port_opts.device) {
			fprintf(stderr, "ERROR: Invalid parameter specified\n");
			show_help(argv[0]);
			return 1;
		}
		port_opts.device = argv[c];
	}

	if (port_opts.device == NULL) {
		fprintf(stderr, "ERROR: Device not specified\n");
		show_help(argv[0]);
		return 1;
	}

	if (!wr && verify) {
		fprintf(stderr, "ERROR: Invalid usage, -v is only valid when writing\n");
		show_help(argv[0]);
		return 1;
	}

	return 0;
}
Ejemplo n.º 3
0
int parse_options(int argc, char *argv[]) {
	int c;
	while((c = getopt(argc, argv, "b:m:r:w:e:vn:g:jkfchuos:S:i:")) != -1) {
		switch(c) {
			case 'b':
				baudRate = serial_get_baud(strtoul(optarg, NULL, 0));
				if (baudRate == SERIAL_BAUD_INVALID) {
					fprintf(stderr,	"Invalid baud rate, valid options are:\n");
					for(baudRate = SERIAL_BAUD_1200; baudRate != SERIAL_BAUD_INVALID; ++baudRate)
						fprintf(stderr, " %d\n", serial_get_baud_int(baudRate));
					return 1;
				}
				break;

			case 'm':
				if (strlen(optarg) != 3
					|| serial_get_bits(optarg) == SERIAL_BITS_INVALID
					|| serial_get_parity(optarg) == SERIAL_PARITY_INVALID
					|| serial_get_stopbit(optarg) == SERIAL_STOPBIT_INVALID) {
					fprintf(stderr, "Invalid serial mode\n");
					return 1;
				}
				serial_mode = optarg;
				break;

			case 'r':
			case 'w':
				rd = rd || c == 'r';
				wr = wr || c == 'w';
				if (rd && wr) {
					fprintf(stderr, "ERROR: Invalid options, can't read & write at the same time\n");
					return 1;
				}
				filename = optarg;
				if (filename[0] == '-') {
					force_binary = 1;
				}
				break;
			case 'e':
				if (readwrite_len || start_addr) {
					fprintf(stderr, "ERROR: Invalid options, can't specify start page / num pages and start address/length\n");
					return 1;
				}
				npages = strtoul(optarg, NULL, 0);
				if (npages > 0xFF || npages < 0) {
					fprintf(stderr, "ERROR: You need to specify a page count between 0 and 255");
					return 1;
				}
				break;
			case 'u':
				wu = 1;
				if (rd || wr) {
					fprintf(stderr, "ERROR: Invalid options, can't write unprotect and read/write at the same time\n");
					return 1;
				}
				break;

			case 'j':
				rp = 1;
				if (rd || wr) {
					fprintf(stderr, "ERROR: Invalid options, can't read protect and read/write at the same time\n");
					return 1;
				}
				break;

			case 'k':
				ur = 1;
				if (rd || wr) {
					fprintf(stderr, "ERROR: Invalid options, can't read unprotect and read/write at the same time\n");
					return 1;
				}
				break;

			case 'o':
				eraseOnly = 1;
				if (rd || wr) {
					fprintf(stderr, "ERROR: Invalid options, can't erase-only and read/write at the same time\n");
					return 1;
				}
				break;				
			
			case 'v':
				verify = 1;
				break;

			case 'n':
				retry = strtoul(optarg, NULL, 0);
				break;

			case 'g':
				exec_flag = 1;
				execute   = strtoul(optarg, NULL, 0);
				if (execute % 4 != 0) {
					fprintf(stderr, "ERROR: Execution address must be word-aligned\n");
					return 1;
				}
				break;
			case 's':
				if (readwrite_len || start_addr) {
					fprintf(stderr, "ERROR: Invalid options, can't specify start page / num pages and start address/length\n");
					return 1;
				}
				spage    = strtoul(optarg, NULL, 0);
				break;
			case 'S':
				if (spage || npages) {
					fprintf(stderr, "ERROR: Invalid options, can't specify start page / num pages and start address/length\n");
					return 1;
				} else {
					char *pLen;
					start_addr = strtoul(optarg, &pLen, 0);
					if (*pLen == ':') {
						pLen++;
						readwrite_len = strtoul(pLen, NULL, 0);
						if (readwrite_len == 0) {
							fprintf(stderr, "ERROR: Invalid options, can't specify zero length\n");
							return 1;
						}
					}
				}
				break;
			case 'f':
				force_binary = 1;
				break;

			case 'c':
				init_flag = 0;
				break;

			case 'h':
				show_help(argv[0]);
				exit(0);

			case 'i':
				gpio_seq = optarg;
				break;
		}
	}

	for (c = optind; c < argc; ++c) {
		if (device) {
			fprintf(stderr, "ERROR: Invalid parameter specified\n");
			show_help(argv[0]);
			return 1;
		}
		device = argv[c];
	}

	if (device == NULL) {
		fprintf(stderr, "ERROR: Device not specified\n");
		show_help(argv[0]);
		return 1;
	}

	if (!wr && verify) {
		fprintf(stderr, "ERROR: Invalid usage, -v is only valid when writing\n");
		show_help(argv[0]);
		return 1;
	}

	return 0;
}