view loadtools/flutil.c @ 964:a96cb97b66a2

ringtools/imy: fix duplicate definition of tdma_durations[] The bug was reported by Vadim Yanitskiy <fixeria@osmocom.org>, although the present fix is slightly different from the contributed patch: because main.c doesn't need this tdma_durations[] array at all, let's simply remove the reference to this array from main.c rather than turn it into an extern. I no longer remember my original thought flow that resulted (by mistake) in tdma_durations[] being multiply defined in main.c and durations.c. My intent might have been to define all globals in main.c and have the reference in durations.c be an extern - and I missed that extern - but without clear memory, I have no certainty. In any case, having this data array defined in the same module that fills it (durations.c) is sensible, so let's make it the new way.
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 31 Aug 2023 19:38:18 +0000
parents 135de08c5d74
children
line wrap: on
line source

/*
 * Miscellaneous utility functions for flash support
 */

#include <sys/types.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include "flash.h"

extern struct flash_bank_info flash_bank_info[2];

get_flash_sector_table(bi)
	struct flash_bank_info *bi;
{
	struct flash_geom *geom;
	struct flash_region_desc *reg;
	struct sector_info *sp;
	uint32_t offset;
	int nr, i;

	if (bi->sectors)
		return(0);
	geom = bi->geom;
	sp = (struct sector_info *) malloc(sizeof(struct sector_info)
						* (geom->total_sectors + 1));
	if (!sp) {
		fprintf(stderr,
			"unable to malloc buffer for flash sector table\n");
		return(-1);
	}
	bi->sectors = sp;
	/* now fill it */
	offset = 0;
	for (nr = 0; nr < geom->nregions; nr++) {
		reg = geom->regions + nr;
		for (i = 0; i < reg->nsectors; i++) {
			sp->start = offset;
			sp->size = reg->sector_size;
			sp++;
			offset += reg->sector_size;
		}
	}
	/* sanity checks */
	if (sp - bi->sectors != geom->total_sectors) {
		fprintf(stderr,
	"BUG in get_flash_sector_table(): wrong # of sectors at the end\n");
		abort();
	}
	if (offset != geom->total_size) {
		fprintf(stderr,
		"BUG in get_flash_sector_table(): wrong offset at the end\n");
		abort();
	}
	/* finish */
	sp->start = 0;
	sp->size = 0;
	return(0);
}

flashcmd_sectors(argc, argv, bank)
	char **argv;
{
	struct flash_bank_info *bi;
	struct sector_info *sp;

	if (argc > 2) {
		fprintf(stderr, "error: too many arguments\n");
		return(-1);
	}
	if (flash_detect(bank, 0) < 0)
		return(-1);
	bi = flash_bank_info + bank;
	if (get_flash_sector_table(bi) < 0)
		return(-1);
	printf("%u sectors in flash bank %d:\n", bi->geom->total_sectors, bank);
	printf("Offset    Size\n");
	for (sp = bi->sectors; sp->size; sp++)
		printf("%08lX  %lx\n", (u_long) sp->start, (u_long) sp->size);
	return(0);
}

get_flash_sector_range(bi, useroff, userlen, startp, endp)
	struct flash_bank_info *bi;
	u_long useroff, userlen;
	struct sector_info **startp, **endp;
{
	struct sector_info *sp;
	uint32_t remlen;

	for (sp = bi->sectors; sp->size; sp++)
		if (sp->start == useroff)
			break;
	if (!sp->size) {
		fprintf(stderr,
	"error: specified offset not aligned to a flash sector boundary\n");
		return(-1);
	}
	*startp = sp;
	for (remlen = userlen; remlen; ) {
		if (remlen < sp->size) {
			fprintf(stderr,
	"error: specified length not aligned to a flash sector boundary\n");
			return(-1);
		}
		remlen -= sp->size;
		sp++;
	}
	*endp = sp;
	return(0);
}