int reconstruct_uboot(char *buf, int length, char *script_buf, int script_length) { struct spare_boot_ctrl_head *head; int total_length; int script_align; char *tmp_start; head = (struct spare_boot_ctrl_head *)buf; script_align = script_length; if(script_length & (align_size - 1)) { script_align = (script_length + align_size) & (~(align_size - 1)); } total_length = script_align + length; head->uboot_length = length; head->length = total_length; tmp_start = buf + length; memset(tmp_start, 0xff, script_align); memcpy(tmp_start, script_buf, script_length); if(gen_check_sum(buf)) { return -1; } //printf("checksum=%x\n", head->check_sum); return 0; }
int align_uboot(char *source_uboot_name) { FILE *uboot_file = NULL; int source_length; int total_length; int align_size; char *uboot_buf; char buffer[512]; struct spare_boot_ctrl_head *head; int ret = -1; //读取原始uboot uboot_file = fopen(source_uboot_name, "rb+"); if(uboot_file == NULL) { printf("update uboot error : unable to open uboot file\n"); goto _err_align_uboot_out; } fread(buffer, 512, 1, uboot_file); rewind(uboot_file); head = (struct spare_boot_ctrl_head *)buffer; source_length = head->uboot_length; align_size = head->align_size; if(source_length & (align_size - 1)) { total_length = (source_length + align_size) & (~(align_size - 1)); } //printf("source length = %d\n", source_length); //printf("total length = %d\n", total_length); uboot_buf = (char *)malloc(total_length); if(!uboot_buf) { printf("update uboot error : fail to malloc memory for uboot\n"); goto _err_align_uboot_out; } memset(uboot_buf, 0xff, total_length); fread(uboot_buf, source_length, 1, uboot_file); rewind(uboot_file); head = (struct spare_boot_ctrl_head *)uboot_buf; head->uboot_length = total_length; head->length = total_length; gen_check_sum( (void *)uboot_buf ); fwrite(uboot_buf, total_length, 1, uboot_file); ret = 0; _err_align_uboot_out: if(uboot_buf) { free(uboot_buf); } if(uboot_file) { fclose(uboot_file); } return ret; }
/* ************************************************************************************************************ * * function * * 函数名称: * * 参数列表: * * 返回值 : * * 说明 : * * ************************************************************************************************************ */ __s32 update_boot0(void *buf0, char *buf, int sprite_type) { boot0_file_head_t *boot0 = (boot0_file_head_t *)buf0; boot1_file_head_t *boot1 = (boot1_file_head_t *)BOOT1_BASE; __u32 length; //校验数据是否正确 if(check_file( (__u32 *)buf0, boot0->boot_head.length, BOOT0_MAGIC ) != CHECK_IS_CORRECT) { sprite_wrn("The input Boot0 in the RAM %X is checked wrong!\n", buf0); return -1; } //读出dram参数 //填充FLASH信息 if(!sprite_type) { update_boot0_info(buf0, buf); } //memcpy(&boot0->prvt_head.nand_connect_info, &nand_info->nand_para, sizeof(boot_nand_para_t)); memcpy((void *)&boot0->prvt_head.dram_para, (void *)&boot1->prvt_head.dram_para, sizeof(boot_dram_para_t)); boot0->boot_head.platform[7] = 1;//0: try dram para.1: read dram para from head. /* regenerate check sum */ gen_check_sum( (void *)boot0 ); length = boot0->boot_head.length; // 获取Boot0文件的尺寸 /* 校验内存中的Boot0文件 */ if( check_file( (__u32 *)boot0, length, BOOT0_MAGIC ) != CHECK_IS_CORRECT ) { sprite_wrn("The Boot0 in the RAM %X is checked wrong!\n", boot0); return -1; } if(!sprite_type) { return Nand_Burn_Boot0((__u32)buf0, length); } else { return SDMMC_PhyWrite(BOOT0_SDMMC_START_ADDR, length/512, (void *)boot0, 2); } }
int update_for_boot0(char *boot0_name, int storage_type) { FILE *boot0_file = NULL; boot0_file_head_t *boot0_head; char *boot0_buf = NULL; int length = 0; int i; int ret = -1; int value[8]; script_gpio_set_t gpio_set[32]; boot0_file = fopen(boot0_name, "rb+"); if(boot0_file == NULL) { printf("update:unable to open boot0 file\n"); goto _err_boot0_out; } fseek(boot0_file, 0, SEEK_END); length = ftell(boot0_file); fseek(boot0_file, 0, SEEK_SET); if(!length) { goto _err_boot0_out; } boot0_buf = (char *)malloc(length); if(!boot0_buf) { goto _err_boot0_out; } fread(boot0_buf, length, 1, boot0_file); rewind(boot0_file); boot0_head = (boot0_file_head_t *)boot0_buf; //检查boot0的数据结构是否完整 ret = check_file( (unsigned int *)boot0_buf, boot0_head->boot_head.length, BOOT0_MAGIC ); if( ret != CHECK_IS_CORRECT ) { goto _err_boot0_out; } //取出数据进行修正,DRAM参数 if(script_parser_sunkey_all("dram_para", (void *)boot0_head->prvt_head.dram_para)) { printf("script fetch dram para failed\n"); goto _err_boot0_out; } //取出数据进行修正,UART参数 if(!script_parser_fetch("uart_para", "uart_debug_port", value)) { boot0_head->prvt_head.uart_port = value[0]; } if(!script_parser_mainkey_get_gpio_cfg("uart_para", gpio_set, 32)) { for(i=0;i<32;i++) { if(!gpio_set[i].port) { break; } boot0_head->prvt_head.uart_ctrl[i].port = gpio_set[i].port; boot0_head->prvt_head.uart_ctrl[i].port_num = gpio_set[i].port_num; boot0_head->prvt_head.uart_ctrl[i].mul_sel = gpio_set[i].mul_sel; boot0_head->prvt_head.uart_ctrl[i].pull = gpio_set[i].pull; boot0_head->prvt_head.uart_ctrl[i].drv_level = gpio_set[i].drv_level; boot0_head->prvt_head.uart_ctrl[i].data = gpio_set[i].data; } } //取出数据进行修正,debugenable参数 if(!script_parser_fetch("jtag_para", "jtag_enable", value)) { boot0_head->prvt_head.enable_jtag = value[0]; } if(!script_parser_mainkey_get_gpio_cfg("jtag_para", gpio_set, 32)) { for(i=0;i<32;i++) { if(!gpio_set[i].port) { break; } boot0_head->prvt_head.jtag_gpio[i].port = gpio_set[i].port; boot0_head->prvt_head.jtag_gpio[i].port_num = gpio_set[i].port_num; boot0_head->prvt_head.jtag_gpio[i].mul_sel = gpio_set[i].mul_sel; boot0_head->prvt_head.jtag_gpio[i].pull = gpio_set[i].pull; boot0_head->prvt_head.jtag_gpio[i].drv_level = gpio_set[i].drv_level; boot0_head->prvt_head.jtag_gpio[i].data = gpio_set[i].data; } } //取出数据进行修正,NAND参数 if(!storage_type) { if(!script_parser_mainkey_get_gpio_cfg("nand_para", gpio_set, 32)) { for(i=0;i<32;i++) { if(!gpio_set[i].port) { break; } boot0_head->prvt_head.storage_gpio[i].port = gpio_set[i].port; boot0_head->prvt_head.storage_gpio[i].port_num = gpio_set[i].port_num; boot0_head->prvt_head.storage_gpio[i].mul_sel = gpio_set[i].mul_sel; boot0_head->prvt_head.storage_gpio[i].pull = gpio_set[i].pull; boot0_head->prvt_head.storage_gpio[i].drv_level = gpio_set[i].drv_level; boot0_head->prvt_head.storage_gpio[i].data = gpio_set[i].data; } } } else if(1 == storage_type) //取得卡参赛 { if(update_sdcard_info((char *)boot0_head->prvt_head.storage_gpio)) { goto _err_boot0_out; } } else if(2 == storage_type) { if(!script_parser_mainkey_get_gpio_cfg("spi0_para", gpio_set, 32)) { for(i=0;i<32;i++) { if(!gpio_set[i].port) { break; } boot0_head->prvt_head.storage_gpio[i].port = gpio_set[i].port; boot0_head->prvt_head.storage_gpio[i].port_num = gpio_set[i].port_num; boot0_head->prvt_head.storage_gpio[i].mul_sel = gpio_set[i].mul_sel; boot0_head->prvt_head.storage_gpio[i].pull = gpio_set[i].pull; boot0_head->prvt_head.storage_gpio[i].drv_level = gpio_set[i].drv_level; boot0_head->prvt_head.storage_gpio[i].data = gpio_set[i].data; } } } //数据修正完毕 //重新计算校验和 gen_check_sum( (void *)boot0_buf ); //再检查一次 ret = check_file( (unsigned int *)boot0_buf, boot0_head->boot_head.length, BOOT0_MAGIC ); if( ret != CHECK_IS_CORRECT ) { goto _err_boot0_out; } fwrite(boot0_buf, length, 1, boot0_file); _err_boot0_out: if(boot0_buf) { free(boot0_buf); } if(boot0_file) { fclose(boot0_file); } return ret; }
int update_for_uboot(char *uboot_name) { FILE *uboot_file = NULL; struct spare_boot_head_t *uboot_head; char *uboot_buf = NULL; int length = 0; int i; int ret = -1; int value[8]; script_gpio_set_t gpio_set[32]; uboot_file = fopen(uboot_name, "rb+"); if(uboot_file == NULL) { printf("update uboot error : unable to open uboot file\n"); goto _err_uboot_out; } fseek(uboot_file, 0, SEEK_END); length = ftell(uboot_file); fseek(uboot_file, 0, SEEK_SET); if(!length) { printf("update uboot error : uboot length is zero\n"); goto _err_uboot_out; } uboot_buf = (char *)malloc(length); if(!uboot_buf) { printf("update uboot error : fail to malloc memory for uboot\n"); goto _err_uboot_out; } fread(uboot_buf, length, 1, uboot_file); rewind(uboot_file); uboot_head = (struct spare_boot_head_t *)uboot_buf; //检查uboot的数据结构是否完整 align_size = uboot_head->boot_head.align_size; //增加判断:是否是原始uboot if((uboot_head->boot_head.length == uboot_head->boot_head.uboot_length)) { //uboot长度和原始整体长度一致,表示是原始uboot uboot_head->boot_head.length = length; uboot_head->boot_head.uboot_length = length; } //否则,不需要修改文件长度 //printf("size=%d, magic=%s\n", uboot_head->boot_head.length, UBOOT_MAGIC); //ret = check_file( (unsigned int *)uboot_buf, uboot_head->boot_head.length, UBOOT_MAGIC ); ret = check_magic( (unsigned int *)uboot_buf, UBOOT_MAGIC ); if( ret != CHECK_IS_CORRECT ) { printf("update uboot error : uboot pre checksum error\n"); goto _err_uboot_out; } //取出数据进行修正,UART参数 if(!script_parser_fetch("uart_para", "uart_debug_port", value)) { uboot_head->boot_data.uart_port = value[0]; } if(!script_parser_mainkey_get_gpio_cfg("uart_para", gpio_set, 32)) { for(i=0;i<2;i++) { if(!gpio_set[i].port) { break; } uboot_head->boot_data.uart_gpio[i].port = gpio_set[i].port; uboot_head->boot_data.uart_gpio[i].port_num = gpio_set[i].port_num; uboot_head->boot_data.uart_gpio[i].mul_sel = gpio_set[i].mul_sel; uboot_head->boot_data.uart_gpio[i].pull = gpio_set[i].pull; uboot_head->boot_data.uart_gpio[i].drv_level = gpio_set[i].drv_level; uboot_head->boot_data.uart_gpio[i].data = gpio_set[i].data; } } //取出数据进行修正,TWI参数 if(!script_parser_fetch("twi_para", "twi_port", value)) { uboot_head->boot_data.twi_port = value[0]; } if(!script_parser_mainkey_get_gpio_cfg("twi_para", gpio_set, 32)) { for(i=0;i<2;i++) { if(!gpio_set[i].port) { break; } uboot_head->boot_data.twi_gpio[i].port = gpio_set[i].port; uboot_head->boot_data.twi_gpio[i].port_num = gpio_set[i].port_num; uboot_head->boot_data.twi_gpio[i].mul_sel = gpio_set[i].mul_sel; uboot_head->boot_data.twi_gpio[i].pull = gpio_set[i].pull; uboot_head->boot_data.twi_gpio[i].drv_level = gpio_set[i].drv_level; uboot_head->boot_data.twi_gpio[i].data = gpio_set[i].data; } } //根据数据进行修正,修正target参数 if(!script_parser_fetch("target", "boot_clock", value)) { uboot_head->boot_data.run_clock = value[0]; } if(!script_parser_fetch("target", "dcdc3_vol", value)) { uboot_head->boot_data.run_core_vol = value[0]; } //修正存储设备信息 if(update_sdcard_info((void *)uboot_head->boot_data.sdcard_gpio, (void *)uboot_head->boot_data.sdcard_spare_data)) { goto _err_uboot_out; } update_nand_info((void *)uboot_head->boot_data.nand_gpio); //数据修正完毕 //重新计算校验和 gen_check_sum( (void *)uboot_buf ); //再检查一次 //printf("size=%d, magic=%s\n", uboot_head->boot_head.length, UBOOT_MAGIC); ret = check_file( (unsigned int *)uboot_buf, uboot_head->boot_head.length, UBOOT_MAGIC ); if( ret != CHECK_IS_CORRECT ) { printf("update uboot error : uboot after checksum error\n"); goto _err_uboot_out; } fwrite(uboot_buf, length, 1, uboot_file); _err_uboot_out: if(uboot_buf) { free(uboot_buf); } if(uboot_file) { fclose(uboot_file); } return ret; }
int update_for_fes1(char *fes1_name, int storage_type) { FILE *fes1_file = NULL; boot0_file_head_t *fes1_head; char *fes1_buf = NULL; int length = 0; int i; int ret = -1; int value[8]; script_gpio_set_t gpio_set[32]; fes1_file = fopen(fes1_name, "rb+"); if(fes1_file == NULL) { printf("update:unable to open fes1 file\n"); goto _err_fes1_out; } fseek(fes1_file, 0, SEEK_END); length = ftell(fes1_file); fseek(fes1_file, 0, SEEK_SET); if(!length) { printf("fes1 file size is 0\n"); goto _err_fes1_out; } fes1_buf = (char *)malloc(length); if(!fes1_buf) { printf("unable to malloc for fes1 update\n"); goto _err_fes1_out; } fread(fes1_buf, length, 1, fes1_file); rewind(fes1_file); fes1_head = (boot0_file_head_t *)fes1_buf; //检查fes1的数据结构是否完整 ret = check_file( (unsigned int *)fes1_buf, fes1_head->boot_head.length, BOOT0_MAGIC ); if( ret != CHECK_IS_CORRECT ) { printf("stage1 check file is error\n"); goto _err_fes1_out; } //取出数据进行修正,DRAM参数 if(script_parser_sunkey_all("dram_para", (void *)fes1_head->prvt_head.dram_para)) { printf("script fetch dram para failed\n"); goto _err_fes1_out; } //取出数据进行修正,UART参数 if(!script_parser_fetch("uart_para", "uart_debug_port", value)) { fes1_head->prvt_head.uart_port = value[0]; } if(!script_parser_mainkey_get_gpio_cfg("uart_para", gpio_set, 32)) { for(i=0;i<32;i++) { if(!gpio_set[i].port) { break; } fes1_head->prvt_head.uart_ctrl[i].port = gpio_set[i].port; fes1_head->prvt_head.uart_ctrl[i].port_num = gpio_set[i].port_num; fes1_head->prvt_head.uart_ctrl[i].mul_sel = gpio_set[i].mul_sel; fes1_head->prvt_head.uart_ctrl[i].pull = gpio_set[i].pull; fes1_head->prvt_head.uart_ctrl[i].drv_level = gpio_set[i].drv_level; fes1_head->prvt_head.uart_ctrl[i].data = gpio_set[i].data; } } //取出数据进行修正,debugenable参数 if(!script_parser_fetch("jtag_para", "jtag_enable", value)) { fes1_head->prvt_head.enable_jtag = value[0]; } if(!script_parser_mainkey_get_gpio_cfg("jtag_para", gpio_set, 32)) { for(i=0;i<32;i++) { if(!gpio_set[i].port) { break; } fes1_head->prvt_head.jtag_gpio[i].port = gpio_set[i].port; fes1_head->prvt_head.jtag_gpio[i].port_num = gpio_set[i].port_num; fes1_head->prvt_head.jtag_gpio[i].mul_sel = gpio_set[i].mul_sel; fes1_head->prvt_head.jtag_gpio[i].pull = gpio_set[i].pull; fes1_head->prvt_head.jtag_gpio[i].drv_level = gpio_set[i].drv_level; fes1_head->prvt_head.jtag_gpio[i].data = gpio_set[i].data; } } //数据修正完毕 //重新计算校验和 gen_check_sum( (void *)fes1_buf ); //再检查一次 ret = check_file( (unsigned int *)fes1_buf, fes1_head->boot_head.length, BOOT0_MAGIC ); if( ret != CHECK_IS_CORRECT ) { printf("stage2 check file is error\n"); goto _err_fes1_out; } fwrite(fes1_buf, length, 1, fes1_file); _err_fes1_out: if(fes1_buf) { free(fes1_buf); } if(fes1_file) { fclose(fes1_file); } return ret; }
/* ************************************************************************************************************ * * function * * 函数名称: * * 参数列表: * * 返回值 : * * 说明 : * * ************************************************************************************************************ */ __s32 update_boot1(void *buf1, char *buf, int sprite_type) { boot1_file_head_t *boot1 = (boot1_file_head_t *)buf1; __u32 length; // __s32 ret = 0; if(check_file( (__u32 *)buf1, boot1->boot_head.length, BOOT1_MAGIC ) != CHECK_IS_CORRECT) { sprite_wrn("Error!The input Boot1 in the RAM %X is checked wrong!\n", buf1); return -1; } //填充NAND信息 boot1->prvt_head.work_mode = 0x20; if(!sprite_type) { update_boot1_info(buf1, buf); } if(env_exist) { char *base = (char *)boot1->prvt_head.script_buf; dynamic_data_form *dform; dynamic_boot_head *dhead; dhead = (dynamic_boot_head *)base; memset(base, 0, 32 * 1024); strcpy(dhead->magic, "dynamic"); dhead->count = 1; base += sizeof(dynamic_boot_head); dform = (dynamic_data_form *)base; strcpy(dform->name, "mac_addr"); __inf("%s\n", mac_addr_store); dform->datasize = strlen(mac_addr_store); __inf("size = %d\n", dform->datasize); dform->usedsize = ((dform->datasize + 4)>>2)<<2; base += sizeof(dynamic_data_form); memcpy(base, mac_addr_store, dform->datasize); } // else //读出原有的boot1数据,查看是否有动态地址 // { // char *tmp_boot1 = NULL; // // tmp_boot1 = wBoot_malloc(512 * 1024); // if(!tmp_boot1) // { // sprite_wrn("not enough memory to read boot1!\n"); // } // else // { // if(Nand_Load_Boot1(tmp_boot1, 512 * 1024)) // { // sprite_wrn("the boot1 in flash is bad!\n"); // } // else // { // boot1_file_head_t *tmp_boot1_head = (boot1_file_head_t *)tmp_boot1; // // if(!dyanmic_data_check(tmp_boot1_head->prvt_head.script_buf)) // { // memcpy(boot1->prvt_head.script_buf, tmp_boot1_head->prvt_head.script_buf, 32 * 1024); // __inf("dynamic data stored\n"); // } // } // wBoot_free(tmp_boot1); // } // } // if(ret < 0) // { // return -1; // } /* regenerate check sum */ boot1->boot_head.platform[7]=0xff; //boot mode gen_check_sum( (void *)boot1 ); length = boot1->boot_head.length; // 获取Boot1文件的尺寸 /* 校验内存中的Boot1文件 */ if( check_file( (__u32 *)boot1, length, BOOT1_MAGIC ) != CHECK_IS_CORRECT ) { sprite_wrn("The changed Boot1 in the RAM %X is checked wrong!\n", boot1); return -1; } __inf("burn boot1\n"); if(!sprite_type) { return Nand_Burn_Boot1((__u32)boot1, length); } else if(sprite_type == 1) { return SDMMC_PhyWrite(BOOT1_SDMMC_START_ADDR, length/512, (void *)boot1, 2); } return -1; }
/////////////////////////////////////////////////////////////////////// //load identify data from code area to SUP 32K FIFO. //Then send to SATA host. /////////////////////////////////////////////////////////////////////// void identify_device() { u16 i =0; u8 tmp,tmp1; u16_t tmp2; //clear_dma_run; //reset_dma_engine(); //prepare data, read it from code area SFR_ext_sram_addr = 0x0000; SFR_ext_sram_cntl = 0x10; //enable auto increese of the sram address g_tmp_addr = identify_data_addr; for(i=0;i<512;i++) { SFR_ext_sram_data = read_code(g_tmp_addr); g_tmp_addr ++; } #ifdef SUPPORT_SECURITY if(security_mode == show_master)//48GB { SFR_ext_sram_addr = 0x0078; SFR_ext_sram_data = 0x00; SFR_ext_sram_data = 0x00; SFR_ext_sram_data = 0x00; SFR_ext_sram_data = 0x06; SFR_ext_sram_addr = 0x00C8; //identify word 100-103 SFR_ext_sram_data = 0x00; SFR_ext_sram_data = 0x00; SFR_ext_sram_data = 0x00; SFR_ext_sram_data = 0x06; g_tmp_addr = identify_data_addr+0xad;//28bits or 48bits tmp1=read_code(g_tmp_addr); tmp1=tmp1&0xFB;//28bits SFR_ext_sram_addr = 0x00ad; SFR_ext_sram_data=tmp1; } else if(security_mode == show_user) //24GB { SFR_ext_sram_addr = 0x0078; SFR_ext_sram_data = 0x00; SFR_ext_sram_data = 0x00; SFR_ext_sram_data = 0xEE; SFR_ext_sram_data = 0x02; SFR_ext_sram_addr = 0x00C8; //identify word 100-103 SFR_ext_sram_data = 0x00; SFR_ext_sram_data = 0x00; SFR_ext_sram_data = 0xEE; SFR_ext_sram_data = 0x02; g_tmp_addr = identify_data_addr+0xad;//28bits or 48bits tmp1=read_code(g_tmp_addr); tmp1=tmp1&0xFB;//28bits SFR_ext_sram_addr = 0x00ad; SFR_ext_sram_data=tmp1; } #endif #ifdef security_debug_chs g_tmp_addr = identify_data_addr+0x2;//number of logical cylinders tmp2.byte.h=read_code(g_tmp_addr); g_tmp_addr = identify_data_addr+0x3;//number of logical cylinders tmp2.byte.l=read_code(g_tmp_addr); myprintf("\ncylinders:%x%x",tmp2.byte.h,tmp2.byte.l); g_tmp_addr = identify_data_addr+0x6;//number of logical heads tmp2.byte.h=read_code(g_tmp_addr); g_tmp_addr = identify_data_addr+0x7;//number of logical heads tmp2.byte.l=read_code(g_tmp_addr); myprintf("\nheads:%x%x",tmp2.byte.h,tmp2.byte.l); g_tmp_addr = identify_data_addr+0xC;//sectors per track tmp2.byte.h=read_code(g_tmp_addr); g_tmp_addr = identify_data_addr+0xD;//sectors per track tmp2.byte.l=read_code(g_tmp_addr); myprintf("\nsectors:%x%x",tmp2.byte.h,tmp2.byte.l); #endif SFR_ext_sram_addr = 0x0104; //siganature SFR_ext_sram_data = 0x53; //S SFR_ext_sram_data = 0x41; //A SFR_ext_sram_data = 0x47; //G SFR_ext_sram_data = 0x45; //E SFR_ext_sram_cntl = 0x00; //disable auto increae of the sram address tmp = gen_check_sum(); SFR_ext_sram_addr = 0x01ff; SFR_ext_sram_data = tmp; //send PIO_SETUP_FIS tx_fis_5f(status_drq, error_no, pm_pio_read, 0x01, 0x00, status_good); //burst it to sata host sata_burst(PIO_READ,0x01); if(sata_burst_abort) { tx_fis_34(status_bad,error_abort,int_set); reset_engine(); #ifdef SUPPORT_SMART1 updata_smart(smart_crc_num_addr,0x01); reset_engine(); #endif return; } }