コード例 #1
0
ファイル: activate_fw.c プロジェクト: nfont/powerpc-utils
int main(int argc, char *argv[])
{
	int opt, opt_index = 0;
	char *key = NULL;
	bool e_flag = false;
	int rc;

	if (get_platform() != PLATFORM_PSERIES_LPAR)
		errx(1,	"activate_firmware is not supported on the %s platform",
		     platform_name);

	while ((opt = getopt_long(argc, argv, "e::hVv",
				  long_opts, &opt_index)) != -1) {
		switch (opt) {
		case 'e':
			e_flag = true;
			/* optarg isn't set to the option argument if it's
			 * optional hence using optind
			 */
			if (argv[optind] && argv[optind][0] != '-') {
				key = argv[optind];
				/* So that getopt doesn't try to parse
				 * the keyfile option argument
				 */
				optind++;
			}
			break;
		case 'V':
			printf("activate_firmware - %s\n", VERSION);
			return 0;
		case 'v':
			verbose = 1;
			break;
		case 'h':
		case '?':
			print_usage(argv[0]);
			return (opt == 'h' ? 0 : -1);
		}
	}

	if (e_flag) {
		if (key)
			rc = apply_uak_key(key);
		else
			rc = get_uak_expiry_date();
	} else {
		rc = activate_firmware();
	}
	return rc;
}
コード例 #2
0
ファイル: firmware.c プロジェクト: coyizumi/cs111
void
firmware(int argc, char *argv[])
{
	int				fd = -1, slot = 0;
	int				a_flag, s_flag, f_flag;
	int				activate_action, reboot_required;
	char				ch, *p, *image = NULL;
	char				*controller = NULL, prompt[64];
	void				*buf = NULL;
	int32_t				size = 0;
	struct nvme_controller_data	cdata;

	a_flag = s_flag = f_flag = false;

	while ((ch = getopt(argc, argv, "af:s:")) != -1) {
		switch (ch) {
		case 'a':
			a_flag = true;
			break;
		case 's':
			slot = strtol(optarg, &p, 0);
			if (p != NULL && *p != '\0') {
				fprintf(stderr,
				    "\"%s\" not valid slot.\n",
				    optarg);
				firmware_usage();
			} else if (slot == 0) {
				fprintf(stderr,
				    "0 is not a valid slot number. "
				    "Slot numbers start at 1.\n");
				firmware_usage();
			} else if (slot > 7) {
				fprintf(stderr,
				    "Slot number %s specified which is "
				    "greater than max allowed slot number of "
				    "7.\n", optarg);
				firmware_usage();
			}
			s_flag = true;
			break;
		case 'f':
			image = optarg;
			f_flag = true;
			break;
		}
	}

	/* Check that a controller (and not a namespace) was specified. */
	if (optind >= argc || strstr(argv[optind], NVME_NS_PREFIX) != NULL)
		firmware_usage();

	if (!f_flag && !a_flag) {
		fprintf(stderr,
		    "Neither a replace ([-f path_to_firmware]) nor "
		    "activate ([-a]) firmware image action\n"
		    "was specified.\n");
		firmware_usage();
	}

	if (!f_flag && a_flag && slot == 0) {
		fprintf(stderr,
		    "Slot number to activate not specified.\n");
		firmware_usage();
	}

	controller = argv[optind];
	open_dev(controller, &fd, 1, 1);
	read_controller_data(fd, &cdata);

	if (cdata.oacs.firmware == 0)
		errx(1,
		    "controller does not support firmware activate/download");

	if (f_flag && slot == 1 && cdata.frmw.slot1_ro)
		errx(1, "slot %d is marked as read only", slot);

	if (slot > cdata.frmw.num_slots)
		errx(1,
		    "slot %d specified but controller only supports %d slots",
		    slot, cdata.frmw.num_slots);

	if (a_flag && !f_flag && !slot_has_valid_firmware(fd, slot))
		errx(1,
		    "slot %d does not contain valid firmware,\n"
		    "try 'nvmecontrol logpage -p 3 %s' to get a list "
		    "of available images\n",
		    slot, controller);

	if (f_flag)
		read_image_file(image, &buf, &size);

	if (f_flag && a_flag)
		printf("You are about to download and activate "
		       "firmware image (%s) to controller %s.\n"
		       "This may damage your controller and/or "
		       "overwrite an existing firmware image.\n",
		       image, controller);
	else if (a_flag)
		printf("You are about to activate a new firmware "
		       "image on controller %s.\n"
		       "This may damage your controller.\n",
		       controller);
	else if (f_flag)
		printf("You are about to download firmware image "
		       "(%s) to controller %s.\n"
		       "This may damage your controller and/or "
		       "overwrite an existing firmware image.\n",
		       image, controller);

	printf("Are you sure you want to continue? (yes/no) ");
	while (1) {
		fgets(prompt, sizeof(prompt), stdin);
		if (strncasecmp(prompt, "yes", 3) == 0)
			break;
		if (strncasecmp(prompt, "no", 2) == 0)
			exit(1);
		printf("Please answer \"yes\" or \"no\". ");
	}

	if (f_flag) {
		update_firmware(fd, buf, size);
		if (a_flag)
			activate_action = NVME_AA_REPLACE_ACTIVATE;
		else
			activate_action = NVME_AA_REPLACE_NO_ACTIVATE;
	} else {
		activate_action = NVME_AA_ACTIVATE;
	}

	reboot_required = activate_firmware(fd, slot, activate_action);

	if (a_flag) {
		if (reboot_required) {
			printf("New firmware image activated but requires "
			       "conventional reset (i.e. reboot) to "
			       "complete activation.\n");
		} else {
			printf("New firmware image activated and will take "
			       "effect after next controller reset.\n"
			       "Controller reset can be initiated via "
			       "'nvmecontrol reset %s'\n",
			       controller);
		}
	}

	close(fd);
	exit(0);
}