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_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; }
static int ui_cmd_msetenv(ui_cmdline_t *cmd,int argc,char *argv[]) { char *varname; char *value; int roflag = ENV_FLG_NORMAL; int res; varname = cmd_getarg(cmd,0); if (!varname) { return ui_showusage(cmd); } value = cmd_getarg(cmd,1); if (!value) { return ui_showusage(cmd); } if (!cmd_sw_isset(cmd,"-p")) { roflag = ENV_FLG_BUILTIN; /* just in memory, not NVRAM */ } if ((res = carmel_setenv(varname,value,roflag)) == 0) { if (roflag != ENV_FLG_BUILTIN) carmel_saveenv(); } else { return ui_showerror(res,"Could not set Monterey environment variable '%s'", varname); } return 0; }
static int ui_cmd_disasm(ui_cmdline_t *cmd,int argc,char *argv[]) { long addr; char buf[512]; int idx; uint32_t inst; int res; getaddrargs(cmd,&prev_wtype,&prev_addr,&prev_dlength); prev_addr &= ~3; addr = prev_addr; if ((prev_wtype & ATYPE_TYPE_MASK) == ATYPE_TYPE_PHYS) { addr = UNCADDR(addr); } for (idx = 0; idx < prev_dlength; idx++) { if ((res = mem_peek(&inst, addr, MEM_WORD))) { ui_showerror(res,"Could not disassemble memory"); return res; } disasm_inst(buf,sizeof(buf),inst,(uint64_t) prev_addr); xprintf("%P%c %08x %s\n",prev_addr,(addr != prev_addr) ? '%' : ':',inst,buf); addr += 4; prev_addr += 4; } return 0; }
static int ui_cmd_memdump(ui_cmdline_t *cmd,int argc,char *argv[]) { long addr; int res; getaddrargs(cmd,&prev_wtype,&prev_addr,&prev_length); addr = prev_addr; if ((prev_wtype & ATYPE_TYPE_MASK) == ATYPE_TYPE_PHYS) { addr = UNCADDR(addr); } res = dumpmem(addr, prev_addr, prev_length, prev_wtype & ATYPE_SIZE_MASK); if (res < 0) { ui_showerror(res,"Could not display memory"); } else { prev_addr += prev_length; } return res; }
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 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 int ui_cmd_save(ui_cmdline_t *cmd,int argc,char *argv[]) { char *x; uint8_t *start,*end; int len; char *fname; int res; fname = cmd_getarg(cmd,0); if ((x = cmd_getarg(cmd,1))) { start = (uint8_t *) getaddr(x); } else { return ui_showusage(cmd); } if ((x = cmd_getarg(cmd,2))) { len = xtoi(x); } else { return ui_showusage(cmd); } end = start+len; res = cfe_savedata("tftp","",fname,start,end); if (res < 0) { return ui_showerror(res,"Could not dump data to network"); } else { xprintf("%d bytes written to %s\n",res,fname); } return 0; }
static int ui_cmd_munsetenv(ui_cmdline_t *cmd,int argc,char *argv[]) { char *varname; int res; int type; varname = cmd_getarg(cmd,0); if (!varname) { return ui_showusage(cmd); } type = carmel_envtype(varname); if ((res = carmel_delenv(varname)) == 0) { if ((type >= 0) && (type != ENV_FLG_BUILTIN)) carmel_saveenv(); } else { return ui_showerror(res,"Could not delete Monterey environment variable '%s'", varname); } return 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 *) §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_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; }
static int ui_cmd_memedit(ui_cmdline_t *cmd,int argc,char *argv[]) { uint8_t b; uint16_t h; uint32_t w; uint64_t q; long addr; char *vtext; int wlen; int count; int idx = 1; int stuffed = 0; int res = 0; getaddrargs(cmd,&prev_wtype,&prev_addr,NULL); wlen = prev_wtype & ATYPE_SIZE_MASK; vtext = cmd_getarg(cmd,idx++); addr = prev_addr; while (vtext) { count = stuffmem(addr,wlen,vtext); if (count < 0) { ui_showerror(count,"Could not modify memory"); return count; /* error */ } addr += count*wlen; prev_addr += count*wlen; stuffed += count; vtext = cmd_getarg(cmd,idx++); } if (stuffed == 0) { char line[256]; char prompt[32]; xprintf("Type '.' to exit, '-' to back up, '=' to dump memory.\n"); for (;;) { addr = prev_addr; if ((prev_wtype & ATYPE_TYPE_MASK) == ATYPE_TYPE_PHYS) { addr = UNCADDR(addr); } xprintf("%P%c ",prev_addr,(addr != prev_addr) ? '%' : ':'); switch (wlen) { default: case 1: if ((res = mem_peek(&b, addr, MEM_BYTE))) { return res; } xsprintf(prompt,"[%02X]: ", b); break; case 2: if ((res = mem_peek(&h, addr, MEM_HALFWORD))) { return res; } xsprintf(prompt,"[%04X]: ",h); break; case 4: if ((res = mem_peek(&w, addr, MEM_WORD))) { return res; } xsprintf(prompt,"[%08X]: ",w); break; case 8: if ((res = mem_peek(&q, addr, MEM_QUADWORD))) { return res; } xsprintf(prompt,"[%016llX]: ",q); break; } console_readline(prompt,line,sizeof(line)); if (line[0] == '-') { prev_addr -= wlen; continue; } if (line[0] == '=') { dumpmem(prev_addr,prev_addr,16,wlen); continue; } if (line[0] == '.') { break; } if (line[0] == '\0') { prev_addr += wlen; continue; } count = stuffmem(addr,wlen,line); if (count < 0) return count; if (count == 0) break; prev_addr += count*wlen; } } return 0; }