FreeBSD/sys/dev/cfi/cfi_dev.c
/*
 * Begin writing into a new block/sector.  We read the sector into
 * memory and keep updating that, until we move into another sector
 * or the process stops writing. At that time we write the whole
 * sector to flash (see cfi_block_finish).  To avoid unneeded erase
 * cycles, keep a pristine copy of the sector on hand.
 */
Begin writing into a new block/sector.  We read the sector into
memory and keep updating that, until we move into another sector
or the process stops writing. At that time we write the whole
sector to flash .  To avoid unneeded erase
cycles, keep a pristine copy of the sector on hand.
Begin writing into a new block / sector . We read the sector into memory and keep updating that until we move into another sector or the process stops writing . At that time we write the whole sector to flash . To avoid unneeded erase cycles keep a pristine copy of the sector on hand .
int
cfi_block_start(struct cfi_softc *sc, u_int ofs)
{
	union {
		uint8_t		*x8;
		uint16_t	*x16;
		uint32_t	*x32;
	} ptr;
	u_int rofs, rsz;
	uint32_t val;
	int r;
	rofs = 0;
	for (r = 0; r < sc->sc_regions; r++) {
		rsz = sc->sc_region[r].r_blocks * sc->sc_region[r].r_blksz;
		if (ofs < rofs + rsz)
			break;
		rofs += rsz;
	}
	if (r == sc->sc_regions)
		return (EFAULT);
	sc->sc_wrbufsz = sc->sc_region[r].r_blksz;
	sc->sc_wrbuf = malloc(sc->sc_wrbufsz, M_TEMP, M_WAITOK);
	sc->sc_wrofs = ofs - (ofs - rofs) % sc->sc_wrbufsz;
	ptr.x8 = sc->sc_wrbuf;
	for (r = 0; r < sc->sc_wrbufsz; r += sc->sc_width) {
		val = cfi_read_raw(sc, sc->sc_wrofs + r);
		switch (sc->sc_width) {
		case 1:
			*(ptr.x8)++ = val;
			break;
		case 2:
			*(ptr.x16)++ = val;
			break;
		case 4:
			*(ptr.x32)++ = val;
			break;
		}
	}
	sc->sc_wrbufcpy = malloc(sc->sc_wrbufsz, M_TEMP, M_WAITOK);
	memcpy(sc->sc_wrbufcpy, sc->sc_wrbuf, sc->sc_wrbufsz);
	sc->sc_writing = 1;
	return (0);
}
int cfi_block_start ( struct cfi_softc * sc , u_int ofs ) { union { uint8_t * x8 ; uint16_t * x16 ; uint32_t * x32 ; } ptr ; u_int rofs , rsz ; uint32_t val ; int r ; rofs = 0 ; for ( r = 0 ; r < sc -> sc_regions ; r ++ ) { rsz = sc -> sc_region [ r ] . r_blocks * sc -> sc_region [ r ] . r_blksz ; if ( ofs < rofs + rsz ) break ; rofs += rsz ; } if ( r == sc -> sc_regions ) return ( EFAULT ) ; sc -> sc_wrbufsz = sc -> sc_region [ r ] . r_blksz ; sc -> sc_wrbuf = malloc ( sc -> sc_wrbufsz , M_TEMP , M_WAITOK ) ; sc -> sc_wrofs = ofs - ( ofs - rofs ) % sc -> sc_wrbufsz ; ptr . x8 = sc -> sc_wrbuf ; for ( r = 0 ; r < sc -> sc_wrbufsz ; r += sc -> sc_width ) { val = cfi_read_raw ( sc , sc -> sc_wrofs + r ) ; switch ( sc -> sc_width ) { case 1 : * ( ptr . x8 ) ++ = val ; break ; case 2 : * ( ptr . x16 ) ++ = val ; break ; case 4 : * ( ptr . x32 ) ++ = val ; break ; } } sc -> sc_wrbufcpy = malloc ( sc -> sc_wrbufsz , M_TEMP , M_WAITOK ) ; memcpy ( sc -> sc_wrbufcpy , sc -> sc_wrbuf , sc -> sc_wrbufsz ) ; sc -> sc_writing = 1 ; return ( 0 ) ; }
Begin writing into a new block/sector.
Begin writing into a new block / sector .
/* Read the block from flash for byte-serving. */