Example #1
0
static fl_size_t
get_flash_size(char *device_name)
{
	int fd;
	flash_info_t flashinfo;
	int res = -1;

	fd = cfe_open(device_name);
	if ((fd > 0) &&
	    (cfe_ioctl(fd, IOCTL_FLASH_GETINFO,
		(unsigned char *) &flashinfo,
		sizeof(flash_info_t), &res, 0) == 0)) {

		cfe_close(fd);
		return flashinfo.flash_size;
	}

	return -1;
}
Example #2
0
void
cfenet_init(struct iodesc *desc, void *machdep_hint)
{
    u_int8_t eaddr[6];
    int res;

    res = cfe_ioctl(booted_dev_fd,IOCTL_ETHER_GETHWADDR,eaddr,sizeof(eaddr),NULL,0);

    if (res < 0) {
        printf("boot: boot device name does not contain ethernet address.\n");
        goto punt;
    }

    memcpy(desc->myea, eaddr,6);

    printf("boot: ethernet address: %s\n", ether_sprintf(desc->myea));
    return;

punt:
    halt();

}
Example #3
0
/**
 * Initialize a new CFE device-backed I/O context.
 *
 * The caller is responsible for releasing all resources held by the returned
 * I/O context via bhnd_nvram_io_free().
 * 
 * @param[out]	io	On success, will be initialized as an I/O context for
 *			CFE device @p dname.
 * @param	dname	The name of the CFE device to be opened for reading.
 *
 * @retval 0		success.
 * @retval non-zero	if opening @p dname otherwise fails, a standard unix
 *			error will be returned.
 */
static int
bcm_nvram_iocfe_init(struct bcm_nvram_iocfe *iocfe, char *dname)
{
	nvram_info_t		 nvram_info;
	int			 cerr, devinfo, dtype, rlen;
	int64_t			 nv_offset;
	u_int			 nv_size;
	bool			 req_blk_erase;
	int			 error;

	iocfe->io.iops = &bhnd_nvram_iocfe_ops;
	iocfe->dname = dname;

	/* Try to open the device */
	iocfe->fd = cfe_open(dname);
	if (iocfe->fd <= 0) {
		IOCFE_LOG(iocfe, "cfe_open() failed: %d\n", iocfe->fd);

		return (ENXIO);
	}

	/* Try to fetch device info */
	if ((devinfo = cfe_getdevinfo(iocfe->dname)) < 0) {
		IOCFE_LOG(iocfe, "cfe_getdevinfo() failed: %d\n", devinfo);
		error = ENXIO;
		goto failed;
	}

	/* Verify device type */
	dtype = devinfo & CFE_DEV_MASK;
	switch (dtype) {
	case CFE_DEV_FLASH:
	case CFE_DEV_NVRAM:
		/* Valid device type */
		break;
	default:
		IOCFE_LOG(iocfe, "unknown device type: %d\n", dtype);
		error = ENXIO;
		goto failed;
	}

	/* Try to fetch nvram info from CFE */
	cerr = cfe_ioctl(iocfe->fd, IOCTL_NVRAM_GETINFO,
	    (unsigned char *)&nvram_info, sizeof(nvram_info), &rlen, 0);
	if (cerr == CFE_OK) {
		/* Sanity check the result; must not be a negative integer */
		if (nvram_info.nvram_size < 0 ||
		    nvram_info.nvram_offset < 0)
		{
			IOCFE_LOG(iocfe, "invalid NVRAM layout (%d/%d)\n",
			    nvram_info.nvram_size, nvram_info.nvram_offset);
			error = ENXIO;
			goto failed;
		}

		nv_offset	= nvram_info.nvram_offset;
		nv_size		= nvram_info.nvram_size;
		req_blk_erase	= (nvram_info.nvram_eraseflg != 0);
	} else if (cerr != CFE_OK && cerr != CFE_ERR_INV_COMMAND) {
		IOCFE_LOG(iocfe, "IOCTL_NVRAM_GETINFO failed: %d\n", cerr);
		error = ENXIO;
		goto failed;
	}

	/* Fall back on flash info.
	 * 
	 * This is known to be required on the Asus RT-N53 (CFE 5.70.55.33, 
	 * BBP 1.0.37, BCM5358UB0), where IOCTL_NVRAM_GETINFO returns
	 * CFE_ERR_INV_COMMAND.
	 */
	if (cerr == CFE_ERR_INV_COMMAND) {
		flash_info_t fi;

		cerr = cfe_ioctl(iocfe->fd, IOCTL_FLASH_GETINFO,
		    (unsigned char *)&fi, sizeof(fi), &rlen, 0);

		if (cerr != CFE_OK) {
			IOCFE_LOG(iocfe, "IOCTL_FLASH_GETINFO failed %d\n",
			    cerr);
			error = ENXIO;
			goto failed;
		}

		nv_offset	= 0x0;
		nv_size		= fi.flash_size;
		req_blk_erase	= !(fi.flash_flags & FLASH_FLAG_NOERASE);
	}

	
	/* Verify that the full NVRAM layout can be represented via size_t */
	if (nv_size > SIZE_MAX || SIZE_MAX - nv_size < nv_offset) {
		IOCFE_LOG(iocfe, "invalid NVRAM layout (%#x/%#jx)\n",
		    nv_size, (intmax_t)nv_offset);
		error = ENXIO;
		goto failed;
	}

	iocfe->offset = nv_offset;
	iocfe->size = nv_size;
	iocfe->req_blk_erase = req_blk_erase;

	return (CFE_OK);

failed:
	if (iocfe->fd >= 0)
		cfe_close(iocfe->fd);

	return (error);
}
Example #4
0
static int ui_cmd_flashtest(ui_cmdline_t *cmd,int argc,char *argv[])
{
    flash_info_t info;
    int fd;
    int retlen;
    int res = 0;
    int idx;
    flash_sector_t sector;
    nvram_info_t nvraminfo;
    char *devname;
    int showsectors;

    devname = cmd_getarg(cmd,0);
    if (!devname) return ui_showusage(cmd);

    showsectors = cmd_sw_isset(cmd,"-sectors");

    fd = cfe_open(devname);
    if (fd < 0) {
	ui_showerror(fd,"Could not open flash device %s",devname);
	return fd;
	}

    res = cfe_ioctl(fd,IOCTL_FLASH_GETINFO,(uint8_t *) &info,sizeof(flash_info_t),&retlen,0);
    if (res == 0) {
	printf("FLASH: Base %016llX size %08X type %02X(%s) flags %08X\n",
	   info.flash_base,info.flash_size,info.flash_type,flashtypes[info.flash_type],
	       info.flash_flags);
	}
    else {
	printf("FLASH: Could not determine flash information\n");
	}

    res = cfe_ioctl(fd,IOCTL_NVRAM_GETINFO,(uint8_t *) &nvraminfo,sizeof(nvram_info_t),&retlen,0);
    if (res == 0) {
	printf("NVRAM: Offset %08X Size %08X EraseFlg %d\n",
	       nvraminfo.nvram_offset,nvraminfo.nvram_size,nvraminfo.nvram_eraseflg);
	}
    else {
	printf("NVRAM: Not supported by this flash\n");
	}

    if (showsectors && (info.flash_type == FLASH_TYPE_FLASH)) {
	printf("Flash sector information:\n");

	idx = 0;
	for (;;) {
	    sector.flash_sector_idx = idx;
	    res = cfe_ioctl(fd,IOCTL_FLASH_GETSECTORS,(uint8_t *) &sector,sizeof(flash_sector_t),&retlen,0);
	    if (res != 0) {
		printf("ioctl error\n");
		break;
		}
	    if (sector.flash_sector_status == FLASH_SECTOR_INVALID) break;
	    printf("  Sector %d offset %08X size %d\n",
		   sector.flash_sector_idx,
		   sector.flash_sector_offset,
		   sector.flash_sector_size);     
	    idx++;
	    }
	}

    cfe_close(fd);
    return 0;

}
Example #5
0
/*  *********************************************************************
    *  ui_cmd_flashop(cmd,argc,argv)
    *  
    *  The 'flashop' command lives here.  This command does a variety
    *  of flash operations over a range of bytes:
    *
    *  erase, protect, unprotect
    *  
    *  Input parameters: 
    *  	   cmd - command table entry
    *  	   argc,argv - parameters
    *  	   
    *  Return value:
    *  	   0 if ok
    *  	   else error
    ********************************************************************* */
static int ui_cmd_flashop(ui_cmdline_t *cmd,int argc,char *argv[])
{
    int fd;
    int res;
    char *flashdev;
    int devtype;
    flash_info_t flashinfo;
    int erase;
    int protect;
    int unprotect;
    int retlen;
    unsigned int startaddr = 0;
    unsigned int endaddr = 0;
    char *x;
    int all;
    flash_range_t range;

    flashdev = cmd_getarg(cmd,0);

    if (!flashdev) flashdev = "flash0";

    /*
     * Make sure it's a flash device.
     */

    res = cfe_getdevinfo(flashdev);
    if (res < 0) {
        return ui_showerror(CFE_ERR_DEVNOTFOUND,flashdev);
        }

    devtype = res & CFE_DEV_MASK;

    if (res != CFE_DEV_FLASH) {
        xprintf("Device '%s' is not a flash device.\n",flashdev);
        return CFE_ERR_INV_PARAM;
    }

    protect = cmd_sw_isset(cmd,"-protect");
    unprotect = cmd_sw_isset(cmd,"-unprotect");
    erase = cmd_sw_isset(cmd,"-erase");

    if (protect == 1) {
        if (erase || unprotect)
            {
            xprintf("Conflicting options\n");
            return CFE_ERR_INV_PARAM;
            }
    }

    if (unprotect == 1) {
        if (erase || protect)
            {
            xprintf("Conflicting options\n");
            return CFE_ERR_INV_PARAM;
            }
    }

    if (erase == 1) {
        if (unprotect || protect)
            {
            xprintf("Conflicting options\n");
            return CFE_ERR_INV_PARAM;
            }
    }

    if (erase == 0 && protect == 0 && unprotect == 0) {
        xprintf("Need one of following options: -erase -protect -unprotect\n");
        return CFE_ERR_INV_PARAM;
    }


    fd = cfe_open(flashdev);
    if (fd < 0) {
	xprintf("Could not open device '%s'\n",flashdev);
	return CFE_ERR_DEVNOTFOUND;
	}

    res = cfe_ioctl(fd,IOCTL_FLASH_GETINFO,(uint8_t *) &flashinfo,sizeof(flash_info_t),&retlen,0);
    if (res != 0) {
        cfe_close (fd);
        return CFE_ERR_IOERR;
    }

    all = cmd_sw_isset(cmd,"-all");
    if (all == 0) {
        if (cmd_sw_value(cmd,"-startaddr",&x)) {
            startaddr = XTOI(x);
            }
        else {
            xprintf("Need option: -startaddr\n");
            cfe_close (fd);
            return CFE_ERR_INV_PARAM;
            }

        if (cmd_sw_value(cmd,"-endaddr",&x)) {
            endaddr = XTOI(x);
            }
        else {
            xprintf("Need option: -endaddr\n");
            cfe_close (fd);
            return CFE_ERR_INV_PARAM;
            }

        if (startaddr > endaddr) {
            xprintf("Final offset (endaddr) must not be less than startaddr \n");
            cfe_close (fd);
            return CFE_ERR_INV_PARAM;
            }
       
        /* Make sure endaddr is within flash */

        if (endaddr >= flashinfo.flash_size) {
            xprintf("Final offset (endaddr) must less than 0x%x\n", flashinfo.flash_size);
            cfe_close (fd);
            return CFE_ERR_INV_PARAM;
        }

        /* We got here, so params are OK  */
        range.range_base = startaddr;
        range.range_length = endaddr-startaddr;
    }
    else {
        range.range_base = 0;
        range.range_length = flashinfo.flash_size;
    }

    if (erase)
        {
        res = cfe_ioctl(fd, IOCTL_FLASH_ERASE_RANGE,
            (void *) &range, NULL, NULL, NULL);
        }

    else if (protect)
        {
        res = cfe_ioctl(fd, IOCTL_FLASH_PROTECT_RANGE,
            (void *) &range, NULL, NULL, NULL);
        }

    else if (unprotect)
        {
        res = cfe_ioctl(fd, IOCTL_FLASH_UNPROTECT_RANGE,
            (void *) &range, NULL, NULL, NULL);
        }

    if (res != 0) {
        printf("ioctl error\n");
        }

    cfe_close (fd);
    return 0;
}