static int ui_cmd_showtime(ui_cmdline_t *cmd,int argc,char *argv[]) { uint8_t hr,min,sec; uint8_t mo,day,yr,y2k; int res=0; int fd; uint8_t buf[12]; fd = cfe_open("clock0"); if (fd < 0) { ui_showerror(fd,"could not open clock device"); return fd; } res = cfe_read(fd,buf,8); if (res < 0) { ui_showerror(res,"could not get time/date"); } cfe_close(fd); hr = buf[0]; min = buf[1]; sec = buf[2]; mo = buf[3]; day = buf[4]; yr = buf[5]; y2k = buf[6]; printf("Current date & time is: "); printf("%02X/%02X/%02X%02X %02X:%02X:%02X\n",mo,day,y2k,yr,hr,min,sec); return 0; }
static int ui_cmd_readnvram(ui_cmdline_t *cmd,int argc,char *argv[]) { char *dev; char *tok; int fd; int offset = 0; int res; uint8_t buf[512]; int idx; dev = cmd_getarg(cmd,0); if (!dev) return ui_showusage(cmd); tok = cmd_getarg(cmd,1); if (tok) offset = xtoi(tok); else offset = 0; fd = cfe_open(dev); if (fd < 0) { ui_showerror(fd,"could not open NVRAM"); return fd; } res = cfe_readblk(fd,offset,buf,512); printf("Offset %d Result %d\n",offset,res); for (idx = 0; idx < 512; idx++) { if ((idx % 16) == 0) printf("\n"); printf("%02X ",buf[idx]); } printf("\n"); cfe_close(fd); return 0; }
static void bhnd_nvram_iocfe_free(struct bhnd_nvram_io *io) { struct bcm_nvram_iocfe *iocfe = (struct bcm_nvram_iocfe *)io; /* CFE I/O instances are statically allocated; we do not need to free * the instance itself */ cfe_close(iocfe->fd); }
int BCMINITFN(nvram_commit_internal)(bool nvram_corrupt) { struct nvram_header *header; int ret; uint32 *src, *dst; uint i; if (!(header = (struct nvram_header *) MALLOC(NULL, MAX_NVRAM_SPACE))) { printf("nvram_commit: out of memory\n"); return -12; /* -ENOMEM */ } NVRAM_LOCK(); /* Regenerate NVRAM */ ret = _nvram_commit(header); if (ret) goto done; src = (uint32 *) &header[1]; dst = src; for (i = sizeof(struct nvram_header); i < header->len && i < MAX_NVRAM_SPACE; i += 4) *dst++ = htol32(*src++); #ifdef _CFE_ if ((ret = cfe_open(flashdrv_nvram)) >= 0) { if (nvram_corrupt) { printf("Corrupting NVRAM...\n"); header->magic = NVRAM_INVALID_MAGIC; } cfe_writeblk(ret, 0, (unsigned char *) header, header->len); cfe_close(ret); } #else if (sysFlashInit(NULL) == 0) { /* set/write invalid MAGIC # (in case writing image fails/is interrupted) * write the NVRAM image to flash(with invalid magic) * set/write valid MAGIC # */ header->magic = NVRAM_CLEAR_MAGIC; nvWriteChars((unsigned char *)&header->magic, sizeof(header->magic)); header->magic = NVRAM_INVALID_MAGIC; nvWrite((unsigned short *) header, MAX_NVRAM_SPACE); header->magic = NVRAM_MAGIC; nvWriteChars((unsigned char *)&header->magic, sizeof(header->magic)); } #endif /* ifdef _CFE_ */ done: NVRAM_UNLOCK(); MFREE(NULL, header, MAX_NVRAM_SPACE); return ret; }
static int ui_cmd_settime(ui_cmdline_t *cmd,int argc,char *argv[]) { char *line; char *p; int hr,min,sec; int fd; uint8_t buf[12]; int res=0; if ((line = cmd_getarg(cmd,0)) == NULL) { return ui_showusage(cmd); } /* convert colons to spaces for the gettoken routine */ while ((p = strchr(line,':'))) *p = ' '; /* parse and check command-line args */ hr = -1; min = -1; sec = -1; p = gettoken(&line); if (p) hr = atoi(p); p = gettoken(&line); if (p) min = atoi(p); p = gettoken(&line); if (p) sec = atoi(p); if ((hr < 0) || (hr > 23) || (min < 0) || (min >= 60) || (sec < 0) || (sec >= 60)) { return ui_showusage(cmd); } /* * hour-minute-second-month-day-year1-year2-(time/date flag) * time/date flag (offset 7) is used to let device know what * is being set. */ buf[0] = hr; buf[1] = min; buf[2] = sec; buf[7] = 0x00; /*SET_TIME = 0x00, SET_DATE = 0x01*/ fd = cfe_open("clock0"); if (fd < 0) { ui_showerror(fd,"could not open clock device"); return fd; } res = cfe_write(fd,buf,8); if (res < 0) { ui_showerror(res,"could not set time"); } cfe_close(fd); return 0; }
int console_close(void) { if (console_handle != -1) { cfe_close(console_handle); } console_handle = -1; return 0; }
static void raw_fileop_uninit(void *fsctx_arg) { raw_fsctx_t *fsctx = (raw_fsctx_t *) fsctx_arg; if (fsctx->raw_refcnt) { xprintf("raw_fileop_uninit: warning: refcnt not zero\n"); } if (fsctx->raw_isconsole == FALSE) { cfe_close(fsctx->raw_dev); } KFREE(fsctx); }
static int ui_cmd_erasenvram(ui_cmdline_t *cmd,int argc,char *argv[]) { char *dev; int fd; uint8_t buffer[2048]; int res; char *tok; int offset; int length; uint8_t data; dev = cmd_getarg(cmd,0); if (!dev) return ui_showusage(cmd); offset = 0; if ((tok = cmd_getarg(cmd,1))) offset = xtoi(tok); length = 512; if ((tok = cmd_getarg(cmd,2))) length = xtoi(tok); if (length > 2048) length = 2048; data = 0xFF; if ((tok = cmd_getarg(cmd,3))) data = xtoi(tok); fd = cfe_open(dev); if (fd < 0) { ui_showerror(fd,"could not open NVRAM"); return fd; } if (cmd_sw_isset(cmd,"-pattern")) { memset(buffer,0,sizeof(buffer)); for (res = 0; res < 2048; res++) { buffer[res] = res & 0xFF; } } else memset(buffer,data,sizeof(buffer)); printf("Fill offset %04X length %04X\n",offset,length); res = cfe_writeblk(fd,offset,buffer,length); printf("write returned %d\n",res); cfe_close(fd); return 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; }
int nvram_commit(void) { struct nvram_header *header; int ret; uint32 *src, *dst; uint i; if (!(header = (struct nvram_header *) MALLOC(NVRAM_SPACE))) { printf("nvram_commit: out of memory\n"); return -12; /* -ENOMEM */ } NVRAM_LOCK(); /* Regenerate NVRAM */ ret = _nvram_commit(header); if (ret) goto done; src = (uint32 *) &header[1]; dst = src; for (i = sizeof(struct nvram_header); i < header->len && i < NVRAM_SPACE; i += 4) *dst++ = htol32(*src++); #ifdef ASUS #else #ifdef _CFE_ if ((ret = cfe_open("flash0.nvram")) >= 0) { cfe_writeblk(ret, 0, (unsigned char *) header, NVRAM_SPACE); cfe_close(ret); } #else if (flash_init((void *) FLASH_BASE, NULL) == 0) nvWrite((unsigned short *) header, NVRAM_SPACE); #endif #endif done: NVRAM_UNLOCK(); MFREE(header, NVRAM_SPACE); return ret; }
static struct nvram_header * BCMINITFN(find_devinfo_nvram)(si_t *sih) { int cfe_fd, ret; if (devinfo_nvram_header != NULL) { return (devinfo_nvram_header); } if ((cfe_fd = cfe_open(devinfo_flashdrv_nvram)) < 0) { return NULL; } ret = cfe_read(cfe_fd, (unsigned char *)devinfo_nvram_nvh, NVRAM_SPACE); if (ret >= 0) { devinfo_nvram_header = (struct nvram_header *) devinfo_nvram_nvh; } cfe_close(cfe_fd); return (devinfo_nvram_header); }
/** * 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); }
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 *) §or,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; }
static int ui_cmd_config1250(ui_cmdline_t *cmd,int argc,char *argv[]) { char *tok; int fh; uint8_t *base; int len; int res; tok = cmd_getarg(cmd, 0); if (!tok) { return ui_showusage(cmd); } if (argc > 1) { char *fname; cfe_loadargs_t *la = &cfe_loadargs; int res; fname = cmd_getarg(cmd, 1); if (!fname) { return ui_showusage(cmd); } /* Get the file using tftp and read it into a known location. */ /* (From ui_flash.c): * We can't allocate the space from the heap to store the * file to download, because the heap may not be big enough. * So, grab some unallocated memory at the 1MB line (we could * also calculate something, but this will do for now). * We assume the downloadable file will be no bigger than 4MB. */ /* Fill out the loadargs */ la->la_flags = LOADFLG_NOISY; la->la_device = (char *) net_getparam(NET_DEVNAME); la->la_filesys = "tftp"; la->la_filename = fname; /* Temporary: use a fixed memory buffer. */ la->la_address = KERNADDR(FILE_STAGING_BUFFER); la->la_maxsize = FILE_STAGING_BUFFER_SIZE;; la->la_flags |= LOADFLG_SPECADDR; la->la_options = NULL; xprintf("Loader:raw Filesys:%s Dev:%s File:%s Options:%s\n", la->la_filesys, la->la_device, la->la_filename, la->la_options); res = cfe_load_program("raw", la); if (res < 0) { xprintf("Could not load %s\n", fname); return res; } base = (uint8_t *)(la->la_address); len = res; } else { /* This code casts a function pointer to a byte pointer, which is suspect in ANSI C but appears to work with the gnu MIPS tool chain. Changing the externs to, e.g., uint8_t * does not work with PIC code (generates relocation errors). */ base = (uint8_t *)download_start; len = (uint8_t *)download_end - (uint8_t *)download_start; } xprintf("PCI download: base %p len %d\n", base, len); fh = cfe_open(tok); if (fh < 0) { xprintf("Could not open device: %s\n", cfe_errortext(fh)); return fh; } res = cfe_write(fh, base, len); if (res != 0) { xprintf("Could not download device: %s\n", cfe_errortext(res)); } cfe_close(fh); return res; }
static int ui_cmd_setdate(ui_cmdline_t *cmd,int argc,char *argv[]) { char *line; char *p; int dt,mo,yr,y2k; int fd; uint8_t buf[12]; int res=0; if ((line = cmd_getarg(cmd,0)) == NULL) { return ui_showusage(cmd); } /* convert colons to spaces for the gettoken routine */ while ((p = strchr(line,'/'))) *p = ' '; /* parse and check command-line args */ dt = -1; mo = -1; yr = -1; p = gettoken(&line); if (p) mo = atoi(p); p = gettoken(&line); if (p) dt = atoi(p); p = gettoken(&line); if (p) yr = atoi(p); if ((mo <= 0) || (mo > 12) || (dt <= 0) || (dt > 31) || (yr < 1900) || (yr > 2099)) { return ui_showusage(cmd); } y2k = (yr >= 2000) ? 0x20 : 0x19; yr %= 100; /* * hour-minute-second-month-day-year1-year2-(time/date flag) * time/date flag (offset 7) is used to let device know what * is being set. */ buf[3] = mo; buf[4] = dt; buf[5] = yr; buf[6] = y2k; buf[7] = 0x01; /*SET_TIME = 0x00, SET_DATE = 0x01*/ fd = cfe_open("clock0"); if (fd < 0) { ui_showerror(fd,"could not open clock device"); return fd; } res = cfe_write(fd,buf,8); if (res < 0) { ui_showerror(res,"could not set date"); } cfe_close(fd); return 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; }