<commit_before>/* Copyright 2013-2015 IBM Corp.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 	http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <libflash/libffs.h>
#include <errno.h>

#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/ioctl.h>
#include <mtd/mtd-user.h>

#include "pnor.h"
#include "opal-prd.h"

int pnor_init(struct pnor *pnor)
{
	int rc, fd;
	mtd_info_t mtd_info;

	if (!pnor)
		return -1;

	/* Open device and ffs */
	fd = open(pnor->path, O_RDWR);
	if (fd < 0) {
		perror(pnor->path);
		return -1;
	}

	/* Hack so we can test on non-mtd file descriptors */
#if defined(__powerpc__)
	rc = ioctl(fd, MEMGETINFO, &mtd_info);
	if (rc < 0) {
		pr_log(LOG_ERR, "PNOR: ioctl failed to get pnor info: %m");
		goto out;
	}
	pnor->size = mtd_info.size;
	pnor->erasesize = mtd_info.erasesize;
#else
	pnor->size = lseek(fd, 0, SEEK_END);
	if (pnor->size < 0) {
		perror(pnor->path);
		goto out;
	}
	/* Fake it */
	pnor->erasesize = 1024;
#endif

	pr_debug("PNOR: Found PNOR: %d bytes (%d blocks)", pnor->size,
	       pnor->erasesize);

	rc = ffs_open_image(fd, pnor->size, 0, &pnor->ffsh);
	if (rc)
		pr_log(LOG_ERR, "PNOR: Failed to open pnor partition table");

out:
	close(fd);

	return rc;
}

void pnor_close(struct pnor *pnor)
{
	if (!pnor)
		return;

	if (pnor->ffsh)
		ffs_close(pnor->ffsh);
<commit_msg>external/opal-prd: Drop final use of ffs_open_image()<commit_after>/* Copyright 2013-2015 IBM Corp.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 	http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <libflash/libffs.h>
#include <errno.h>

#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/ioctl.h>
#include <mtd/mtd-user.h>

#include "pnor.h"
#include "opal-prd.h"

int pnor_init(struct pnor *pnor)
{
	int rc, fd;
	struct blocklevel_device *bl;
	mtd_info_t mtd_info;

	if (!pnor)
		return -1;

	/* Open device and ffs */
	fd = open(pnor->path, O_RDWR);
	if (fd < 0) {
		perror(pnor->path);
		return -1;
	}

	/* Hack so we can test on non-mtd file descriptors */
#if defined(__powerpc__)
	rc = ioctl(fd, MEMGETINFO, &mtd_info);
	if (rc < 0) {
		pr_log(LOG_ERR, "PNOR: ioctl failed to get pnor info: %m");
		goto out;
	}
	pnor->size = mtd_info.size;
	pnor->erasesize = mtd_info.erasesize;
#else
	pnor->size = lseek(fd, 0, SEEK_END);
	if (pnor->size < 0) {
		perror(pnor->path);
		goto out;
	}
	/* Fake it */
	pnor->erasesize = 1024;
#endif

	pr_debug("PNOR: Found PNOR: %d bytes (%d blocks)", pnor->size,
	       pnor->erasesize);

	rc = file_init(fd, &bl);
	if (rc) {
		pr_log(LOG_ERR, "PNOR: (libflash) file_init() failed");
		goto out;
	}

	rc = ffs_init(0, pnor->size, 0, bl, &pnor->ffsh, 0);
	if (rc)
		pr_log(LOG_ERR, "PNOR: Failed to open pnor partition table");

	file_exit(bl);
out:
	close(fd);

	return rc;
}

void pnor_close(struct pnor *pnor)
{
	if (!pnor)
		return;

	if (pnor->ffsh)
		ffs_close(pnor->ffsh);

{'fraction_non_alphanumeric': 0.14442778610694654, 'fraction_numerical': 0.01199400299850075, 'mean_word_length': 3.737278106508876, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 53, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6114963', 'n_tokens_mistral': 1657, 'n_tokens_neox': 1562, 'n_words': 467}