/*
 * Read bootenv from ubi volume
 */
static int
ubi_read_bootenv(uint32_t devno, uint32_t id, bootenv_t env)
{
	libubi_t ulib;
	int rc = 0;
	char path[PATH_MAX];
	FILE* fp_in = NULL;

	ulib = libubi_open();
	if (ulib == NULL) {
		err_msg("Cannot allocate ubi structure\n");
		return -1;
	}

	snprintf(path, PATH_MAX, DEFAULT_VOL_PATTERN, devno, id);

	fp_in = fopen(path, "r");
	if (fp_in == NULL) {
		err_msg("Cannot open volume:%d number:%d\n", devno, id);
		goto err;
	}

	rc = bootenv_read(fp_in, env, BOOTENV_MAXSIZE);
	if (rc != 0) {
		err_msg("Cannot read volume:%d number:%d\n", devno, id);
		goto err;
	}

err:
	if (fp_in)
		fclose(fp_in);
	libubi_close(ulib);
	return rc;
}
Ejemplo n.º 2
0
/**
 * read_bootenv_volume - reads the current bootenv data from id into be_old
 * @devno	UBI device number
 * @id		UBI volume id to remove
 * @bootenv_old	to hold old boot_env data
 *
 * Error handling:
 *	when UBI system couldn't be opened
 *	- returns -PFIFLASH_ERR_UBI_OPEN, err_buf matches text to err
 *	when UBI system couldn't open a volume to read
 *	- returns -PFIFLASH_ERR_UBI_VOL_FOPEN, err_buf matches text to err
 *	when couldn't read bootenv data
 *	- returns -PFIFLASH_ERR_BOOTENV_READ, err_buf matches text to err
 **/
static int
read_bootenv_volume(int devno, uint32_t id, bootenv_t bootenv_old,
		    char *err_buf, size_t err_buf_size)
{
	int rc;
	FILE* fp_in;
	ubi_lib_t ulib;

	rc = 0;
	fp_in = NULL;
	ulib = NULL;

	rc = ubi_open(&ulib);
	if (rc != 0) {
		rc = -PFIFLASH_ERR_UBI_OPEN;
		EBUF(PFIFLASH_ERRSTR[-rc]);
		goto err;
	}

	fp_in = ubi_vol_fopen_read(ulib, devno, id);
	if (!fp_in) {
		rc = -PFIFLASH_ERR_UBI_VOL_FOPEN;
		EBUF(PFIFLASH_ERRSTR[-rc], id);
		goto err;
	}

	log_msg("[ reading old bootenvs ...");

	/* Save old bootenvs for reference */
	rc = bootenv_read(fp_in, bootenv_old, BOOTENV_MAXSIZE);
	if (rc != 0) {
		rc = -PFIFLASH_ERR_BOOTENV_READ;
		EBUF(PFIFLASH_ERRSTR[-rc]);
		goto err;
	}

 err:
	if (fp_in)
		fclose(fp_in);
	if (ulib)
		ubi_close(&ulib);

	return rc;
}
static int
read_bootenv(const char* file, bootenv_t env)
{
	int rc = 0;
	FILE* fp_in = NULL;

	fp_in = fopen(file, "rb");
	if (fp_in == NULL) {
		err_msg("Cannot open file: %s\n", file);
		return -EIO;
	}

	rc = bootenv_read(fp_in, env, BOOTENV_MAXSIZE);
	if (rc != 0) {
		err_msg("Cannot read bootenv from file %s. rc: %d\n",
			file, rc);
		goto err;
	}

err:
	fclose(fp_in);
	return rc;
}
Ejemplo n.º 4
0
static int compare_bootenv(FILE *fp_pfi, FILE **fp_flash, uint32_t ids_size,
		uint32_t data_size, pdd_func_t pdd_f, char *err_buf,
		size_t err_buf_size)
{
	int rc, warnings = 0;
	unsigned int i;
	bootenv_t bootenv_pfi, bootenv_res = NULL, bootenv_flash = NULL;

	rc = bootenv_create(&bootenv_pfi);
	if (rc != 0) {
		rc = -PFIFLASH_ERR_BOOTENV_CREATE;
		goto err;
	}

	rc = bootenv_create(&bootenv_res);
	if (rc != 0) {
		rc = -PFIFLASH_ERR_BOOTENV_CREATE;
		goto err;
	}

	rc = bootenv_read(fp_pfi, bootenv_pfi, data_size);
	if (rc != 0) {
		rc = -PFIFLASH_ERR_BOOTENV_READ;
		goto err;
	}

	for (i = 0; i < ids_size; i++) {
		rc = bootenv_create(&bootenv_flash);
		if (rc != 0) {
			rc = -PFIFLASH_ERR_BOOTENV_CREATE;
			goto err;
		}

		rc = bootenv_read(fp_flash[i], bootenv_flash, BOOTENV_MAXSIZE);
		if (rc != 0) {
			rc = -PFIFLASH_ERR_BOOTENV_READ;
			goto err;
		}

		rc = pdd_f(bootenv_flash, bootenv_pfi, &bootenv_res,
				&warnings, err_buf, err_buf_size);
		if (rc != 0) {
			rc = -PFIFLASH_ERR_PDD_UNKNOWN;
			goto err;
		}

		rc = bootenv_compare(bootenv_flash, bootenv_res);
		if (rc > 0) {
			rc = -PFIFLASH_CMP_DIFF;
			goto err;
		} else if (rc < 0) {
			rc = -PFIFLASH_ERR_COMPARE;
			goto err;
		}

		bootenv_destroy(&bootenv_flash);
		bootenv_flash = NULL;
	}

err:
	if (bootenv_pfi)
		bootenv_destroy(&bootenv_pfi);
	if (bootenv_res)
		bootenv_destroy(&bootenv_res);
	if (bootenv_flash)
		bootenv_destroy(&bootenv_flash);

	return rc;
}