static HRESULT cliBuiltInToolGetNumber(uint32 index, char** argv, CLIDescriptor* variable, uint32* value) { HRESULT hResult = NO_ERROR; if (!is_number(argv[index],strlen(argv[index]))) { if (variable) { hResult = cliLookUpConstant(variable, argv[index], value); } else { hResult = E_CLI_BADARGUMENTS; } } if (hResult != NO_ERROR) { cliPrintf("SET ERROR: argument #%i must be a number\n\r", index+1); return hResult; } if (is_number(argv[index],strlen(argv[index]))==2) { *value = hex2int(argv[index],strlen(argv[index])); } else { *value = (uint32) dec2int(argv[index],strlen(argv[index])); } return hResult; }
HRESULT envGetEenvironmentLong (const char * varname, uint32 * pvalue) { char * val; int vlen; uint8 type; if (findEnvString (varname, &val, &vlen)) { type = is_number (val, vlen); if (type == 1) { *pvalue = (uint32) dec2int(val, vlen); return NO_ERROR; } else if (type == 2) { *pvalue = (uint32) hex2int(val, vlen); return NO_ERROR; } else { return E_GEN_ILLEGAL_PARAM; } } return E_GEN_NOMATCH; }
/* Receive a file and write it into the flash using the YMODEM preotocol */ int dmon_ymodem_flash(struct dmon_comm * comm, uint32_t addr, unsigned int size) { /* FIXME: generalize the application load by removing the low level flash calls dependency */ #ifdef STM32_FLASH_MEM /* The YMODEM state machine is allocated at the top of the stack, make sure there is no app running before calling the dmon_ymodem_flash()! */ struct ymodem_rcv * ry = ((struct ymodem_rcv *)&_stack) - 1; uint32_t base = (uint32_t)STM32_FLASH_MEM; uint32_t offs = addr - base; int ret; DCC_LOG2(LOG_INFO, "sp=%p ry=%p", cm3_sp_get(), ry); DCC_LOG2(LOG_INFO, "offs=0x%08x size=%d", offs, size); dmon_ymodem_rcv_init(ry, true, false); ry->fsize = size; DCC_LOG(LOG_INFO, "Starting..."); while ((ret = dmon_ymodem_rcv_pkt(comm, ry)) >= 0) { if ((ret == 0) && (ry->xmodem) ) break; int len = ret; if (ry->pktno == 1) { char * cp; int fsize; cp = (char *)ry->pkt.data; DCC_LOGSTR(LOG_INFO, "fname='%s'", cp); while (*cp != '\0') cp++; /* skip null */ cp++; fsize = dec2int(cp); if (fsize == 0) { ret = 0; break; } DCC_LOG1(LOG_INFO, "fsize='%d'", fsize); ry->fsize = fsize; DCC_LOG(LOG_INFO, "YMODEM first packet..."); } else { if (ry->pktno == 2) { stm32_flash_erase(offs, ry->fsize); } stm32_flash_write(offs, ry->pkt.data, len); offs += len; } } return ret; #else return -1; #endif }
static HRESULT cliBuiltInToolGetAddressHexValue(char* addressarg, uint32 *pAddress, uint32 *pSize) { HRESULT hResult = NO_ERROR; // Check for the address argument as a hex value. // This handles parsing the address of forms: // 0xHHHHHHHH // 0xHHHHHHHH:S if (pSize) { *pSize = 32; } if (addressarg[0] == '0' && addressarg[1] == 'x') { char* p = addressarg; // special case, this is either a hex value, or a hex value with a size. while (*p) { switch (*p) { case ':': *p = 0; p++; if (pSize) { *pSize = (uint32) dec2int(p, strlen(p)); *pSize = *pSize * 8; } else { hResult = E_CLI_INVALIDARGUMENTS; sysLogError(hResult, __LINE__, moduleName); return hResult; } break; default: p++; break; } } *pAddress = hex2int(addressarg, strlen(addressarg)); } else { hResult = E_FAIL; } return hResult; }
int __attribute__((noreturn)) yflash(uint32_t blk_offs, unsigned int blk_size, const struct magic * magic) { struct { struct magic_hdr hdr; struct magic_rec rec[MAGIC_REC_MAX]; } magic_buf; struct ymodem_rcv ry; unsigned int cnt; uint32_t offs; int ret; int i; if (magic != 0) { /* copy magic check block */ cnt = magic->hdr.cnt > MAGIC_REC_MAX ? MAGIC_REC_MAX : magic->hdr.cnt; for (i = 0; i < cnt; ++i) { // usb_send(CDC_TX_EP, "\r\nmagic.", 8); magic_buf.rec[i] = magic->rec[i]; } magic_buf.hdr.cnt = cnt; magic_buf.hdr.pos = magic->hdr.pos; } else { magic_buf.hdr.cnt = 0; magic_buf.hdr.pos = 0; } DCC_LOG(LOG_TRACE, "flash_unlock()"); LOG_CODE(0); LOG_STATE(0); #if 0 if (opt & XFLASH_OPT_BYPASS) { PUTS(s_ok); usb_drain(CDC_TX_EP); return 55; } #endif do { PUTS(s_ymodem); ymodem_rcv_init(&ry); ry.fsize = blk_size; offs = blk_offs; cnt = 0; flash_unlock(); while ((ret = usb_ymodem_rcv_pkt(&ry)) >= 0) { DCC_LOG1(LOG_TRACE, "usb_ymodem_rcv_pkt() ret=%d)", ret); #if XMODEM_FALLBACK if ((ret == 0) && (ry.xmodem)) { LOG_CODE(1); break; } #endif int len = ret; if (ry.pktno == 1) { char * cp; int fsize; cp = (char *)ry.pkt.data; while (*cp != '\0') cp++; cp++; /* skip null */ fsize = dec2int(cp); if (fsize == 0) { LOG_CODE(2); break; } ry.fsize = fsize; } else { if (ry.pktno == 2) { if (!magic_match((struct magic *)&magic_buf, cnt, ry.pkt.data, len)) { DCC_LOG(LOG_WARNING, "invalid file magic!"); #if XFLASH_VERBOSE usb_ymodem_rcv_cancel(&ry); delay(1000); PUTS(s_invalid); #endif LOG_CODE(3); ret = -3; break; } // flash_erase(offs, ry->fsize); } DCC_LOG2(LOG_TRACE, "flash_erase(offs=0x%06x, len=%d)", offs, len); if ((ret = flash_erase(offs, len)) < 0) { DCC_LOG(LOG_WARNING, "flash_erase() failed!"); #if XFLASH_VERBOSE usb_ymodem_rcv_cancel(&ry); delay(1000); PUTS(s_erase); #endif LOG_CODE(4 + (-ret * 0x10000)); ret = -4; break; } DCC_LOG(LOG_TRACE, "flash_write()"); if ((ret = flash_write(offs, ry.pkt.data, len)) < 0) { DCC_LOG(LOG_WARNING, "flash_write() failed!"); #if XFLASH_VERBOSE usb_ymodem_rcv_cancel(&ry); delay(1000); PUTS(s_program); #endif LOG_CODE(5); ret = -5; break; } offs += len; cnt += len; LOG_STATE(cnt); } } // if (opt & XFLASH_OPT_RET_ERR) { // return ret; // } } while ((ret < 0) || (cnt == 0)); PUTS(s_ok); usb_drain(CDC_TX_EP); // if (opt & XFLASH_OPT_RESET) { delay(3000); reset(); // } // return 0; }