uboot-mt7621 icon indicating copy to clipboard operation
uboot-mt7621 copied to clipboard

Does the command 'mtk_board_flash_write' have issue?

Open yunhai20082008 opened this issue 1 year ago • 0 comments

int hex2int(char ch)  
{  
    if(isdigit(ch))  
        return ch - 48;  
  
    if( ch < 'A' || (ch > 'F' && ch < 'a') || ch > 'f' )  
        return -1;  
  
    if(isalpha(ch))  
        return isupper(ch) ? ch - 55 : ch - 87;  
  
    return -1;  
}  


static int do_testflash(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]){
	if ( argc <= 3 )
		return cmd_usage(cmdtp);
	
	void *flash;
	flash = mtk_board_get_flash_dev();

	ulong offset=simple_strtoul(argv[2], NULL, 16);

	uint8_t *buf ={0x01};
	int ret;
	
	if ( strcmp(argv[1], "r") == 0 ){
		printf("testflash read  ...\n");
		ulong length=simple_strtoul(argv[3], NULL, 16);
		ret = mtk_board_flash_read(flash, offset, length, buf);
		print_buffer(offset, buf, 1, length, 16);

		int i;
		for(i=0; i<length; i++){
			printf("buf_r[%d]= %d \n", i,buf[i]);
		}
		// unmap_sysmem(buf);
		return 0;
	}
	else if ( strcmp(argv[1], "w") == 0 ) {
		printf("testflash write ...\n");
		size_t length = strlen(argv[3]);
		if  (length % 2 == 1 ) {
			return cmd_usage(cmdtp);
		}
		printf("length= %d \n", length);
		int i,m,n;
		int k=0;
		uint8_t j;
		for(i=0; i<length; i=i+2){
			m = hex2int(argv[3][i]);
			n = hex2int(argv[3][i+1]);
			if ( m == -1 || n == -1 ) {
				return cmd_usage(cmdtp);
				k=1;
				break;
			}
			j = ( ( m << 4 ) + n );
			printf("combine=%d \n", j);
			buf[i>>1]=j;
		}
		
		length = length>>1;
		for(i=0; i<length; i++){
			printf("buf[%d]= %d \n", i,buf[i]);
		}

		if ( k == 0 ){
			ret = mtk_board_flash_write(flash, offset, length, buf);
			printf("ret=%d \n", ret);
		}
		printf("ret=%d \n", ret);
		return 0;
	}
	else
	{
		return cmd_usage(cmdtp);
	}
}

U_BOOT_CMD(testflash, 4, 0, do_testflash,
	"MTK testflash utility",
	"testflash <r|w> <offset> <len>\n"
	"type    - upgrade file type\n"
	"          r|w      - Read or Write\n"
	"          offset   - First offset\n"
	"          len/hex  - Length/HEX\n"
);

testflash r works fine.

testflash w works not fine; usually,it works fine when the content of flash has not been written but if the content of the flash has been written ,Even if the power is replugged,it works maybe not fine

yunhai20082008 avatar Feb 04 '24 02:02 yunhai20082008