Exemplo n.º 1
0
static int fmap_get_csum_test(struct fmap *fmap)
{
	uint8_t *digest = NULL, *image = NULL;
	/* assume 0x100-0x10100 is marked "static" and is filled with 0x00 */
	int image_size = 0x20000;
	uint8_t csum[SHA_DIGEST_SIZE] = {
		0x1a, 0xdc, 0x95, 0xbe, 0xbe, 0x9e, 0xea, 0x8c,
		0x11, 0x2d, 0x40, 0xcd, 0x04, 0xab, 0x7a, 0x8d,
		0x75, 0xc4, 0xf9, 0x61 };

	status = fail;

	if ((fmap_get_csum(NULL, image_size, &digest) >= 0) ||
	    (fmap_get_csum(image, image_size, NULL) >= 0)) {
		printf("failed to abort on NULL pointer input\n");
		goto fmap_get_csum_test_exit;
	}

	image = calloc(image_size, 1);
	memcpy(image, fmap, fmap_size(fmap));

	if (fmap_get_csum(image, image_size, &digest) != SHA_DIGEST_SIZE) {
		printf("FAILURE: failed to calculate checksum\n");
		goto fmap_get_csum_test_exit;
	}
	if (memcmp(digest, csum, SHA_DIGEST_SIZE)) {
		printf("FAILURE: checksum is incorrect\n");
		goto fmap_get_csum_test_exit;
	}

	status = pass;
fmap_get_csum_test_exit:
	free(image);
	free(digest);
	return status;
}
Exemplo n.º 2
0
int main(int argc, char *argv[])
{
	int fd, len, rc = EXIT_SUCCESS;
	struct stat s;
	char *filename = NULL;
	uint8_t *image;
	int argflag;
	uint8_t *digest = NULL;

	while ((argflag = getopt_long(argc, argv, "d:hlv",
	                      long_options, NULL)) > 0) {
		switch (argflag) {
		case 'v':
			printf("fmap suite version: %d.%d\n",
			       VERSION_MAJOR, VERSION_MINOR);;
			goto do_exit_1;
		case 'h':
			print_help();
			goto do_exit_1;
		default:
			print_help();
			rc = EXIT_FAILURE;
			goto do_exit_1;
		}
	}

	/* quit if filename not specified */
	if (argv[optind]) {
		filename = argv[optind];
	} else {
		print_help();
		rc = EXIT_FAILURE;
		goto do_exit_1;
	}

	fd = open(filename, O_RDONLY);
	if (fd < 0) {
		fprintf(stderr, "unable to open file \"%s\": %s\n",
		                filename, strerror(errno));
		rc = EXIT_FAILURE;
		goto do_exit_1;
	}
	if (fstat(fd, &s) < 0) {
		fprintf(stderr, "unable to stat file \"%s\": %s\n",
		                filename, strerror(errno));
		rc = EXIT_FAILURE;
		goto do_exit_2;
	}

	image = mmap(NULL, s.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
	if (image == MAP_FAILED) {
		fprintf(stderr, "unable to map file \"%s\": %s\n",
		                filename, strerror(errno));
		rc = EXIT_FAILURE;
		goto do_exit_2;
	}

	if ((len = fmap_get_csum(image, s.st_size, &digest)) < 0) {
		fprintf(stderr, "unable to obtain checksum\n");
		rc = EXIT_FAILURE;
		goto do_exit_3;
	}

	print_csum(digest, len);

do_exit_3:
	munmap(image, s.st_size);
	free(digest);
do_exit_2:
	close(fd);
do_exit_1:
	exit(rc);
}