int main()

in tools/ocp_recovery/ocp_recovery.c [1979:2235]


int main (int argc, char *argv[])
{
	const char *opts = "a:bc:d:efhlo:prsS:vwW:";
	int opt;
	int device_num = -1;

	while ((opt = getopt (argc, argv, opts)) != -1) {
		switch (opt) {
			case 'a':
				addr = strtol (optarg, NULL, 16);
				break;

			case 'b':
				raw_bytes = true;
				break;

			case 'c':
				cms_id = strtoul (optarg, NULL, 10);
				break;

			case 'd':
				device_num = strtol (optarg, NULL, 10);
				break;

			case 'e':
				force_pec_error = true;
				break;

			case 'f':
				ignore_errors = true;
				break;

			case 'l':
				is_cerberus_log = true;
				break;

			case 'o':
				cms_offset = strtoul (optarg, NULL, 16);
				break;

			case 'p':
				pec = false;
				break;

			case 'r':
				force_recovery = true;
				break;

			case 's':
				use_write_delay = true;
				break;

			case 'S':
				write_delay = strtoul (optarg, NULL, 10);
				break;

			case 'v':
				verbose++;
				break;

			case 'w':
				is_read = false;
				break;

			case 'W':
				cms_block_write = strtoul (optarg, NULL, 10);
				break;

			case 'h':
				print_help ();

				return 0;
		}
	}

	if (device_num < 0) {
		printf ("No I2C device specified.\n\n");
		print_usage ();

		return 1;
	}

	if (cms_block_write > MAX_CMS_BLOCK_SIZE) {
		printf ("Invalid CMS block write value.\n\n");
		print_usage ();

		return 1;
	}

	if (optind >= argc) {
		print_usage ();

		return 1;
	}

	command = argv[optind++];

	if ((strcmp ("recover", command) == 0) || (strcmp ("load_img", command) == 0) ||
		(strcmp ("verify_img", command) == 0)) {
		if (optind >= argc) {
			printf ("A file must be provided for this command.\n");

			return 1;
		}

		file_name = argv[optind++];
	}
	else if ((strcmp ("read_log", command) == 0) || (strcmp ("read_data", command) == 0)) {
		if (optind < argc) {
			file_name = argv[optind++];
			file_out = true;
		}
	}
	else if (is_raw_command ()) {
		if (optind < argc) {
			size_t i;

			raw_data_count = argc - optind;
			for (i = 0; i < raw_data_count; i++, optind++) {
				raw_data[i] = strtoul (argv[optind], NULL, 16);
			}
		}
		else if (!is_read) {
			printf ("No data provided for the command.\n");

			return 1;
		}
	}

	i2c = i2c_init (device_num);
	if (i2c < 0) {
		return 1;
	}

	if (strcmp ("recover", command) == 0) {
		command_recover ();
	}
	else if (strcmp ("load_img", command) == 0) {
		command_load_image ();
	}
	else if (strcmp ("verify_img", command) == 0) {
		command_verify_image ();
	}
	else if (strcmp ("activate_img", command) == 0) {
		command_activate_image ();
	}
	else if (strcmp ("read_log", command) == 0) {
		command_read_log ();
	}
	else if (strcmp ("read_data", command) == 0) {
		command_read_data ();
	}
	else if (strcmp ("reset_device", command) == 0) {
		command_reset_device ();
	}
	else if (strcmp ("reset_mgmt", command) == 0) {
		command_reset_management ();
	}
	else if (strcmp ("show_all", command) == 0) {
		command_show_all ();
	}
	else if (strcmp ("prot_cap", command) == 0) {
		if (is_read) {
			read_prot_cap (raw_bytes);
		}
		else {
			write_prot_cap ();
		}
	}
	else if (strcmp ("device_id", command) == 0) {
		if (is_read) {
			read_device_id (raw_bytes);
		}
		else {
			write_device_id ();
		}
	}
	else if (strcmp ("device_status", command) == 0) {
		if (is_read) {
			read_device_status (raw_bytes);
		}
		else {
			write_device_status ();
		}
	}
	else if (strcmp ("reset", command) == 0) {
		if (is_read) {
			read_reset (raw_bytes);
		}
		else {
			write_reset (raw_bytes);
		}
	}
	else if (strcmp ("recovery_ctrl", command) == 0) {
		if (is_read) {
			read_recovery_ctrl (raw_bytes);
		}
		else {
			write_recovery_ctrl ();
		}
	}
	else if (strcmp ("recovery_status", command) == 0) {
		if (is_read) {
			read_recovery_status (raw_bytes);
		}
		else {
			write_recovery_status ();
		}
	}
	else if (strcmp ("hw_status", command) == 0) {
		if (is_read) {
			read_hw_status (raw_bytes);
		}
		else {
			write_hw_status ();
		}
	}
	else if (strcmp ("indirect_ctrl", command) == 0) {
		if (is_read) {
			read_indirect_ctrl (raw_bytes);
		}
		else {
			write_indirect_ctrl ();
		}
	}
	else if (strcmp ("indirect_status", command) == 0) {
		if (is_read) {
			read_indirect_status (raw_bytes);
		}
		else {
			write_indirect_status ();
		}
	}
	else if (strcmp ("indirect_data", command) == 0) {
		if (is_read) {
			read_indirect_data ();
		}
		else {
			write_indirect_data ();
		}
	}
	else if (strcmp ("vendor", command) == 0) {
		if (is_read) {
			read_vendor ();
		}
		else {
			write_vendor ();
		}
	}
	else {
		printf ("Uknown command.\n");

		return 1;
	}

	return 0;
}