⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 usbms_state.c

📁 MTK平台绝密核心代码之 USB驱动
💻 C
📖 第 1 页 / 共 3 页
字号:
/* under construction !*/
/* under construction !*/
/* under construction !*/
#endif
			write_status= USB_Ms_Write(g_UsbMS.current_LUN, g_UsbMS.disk_buffer->ms_buffer[g_UsbMS.rw_cmd.rw_buffer_index], 
							(g_UsbMS.rw_cmd.LBA + g_UsbMS.rw_cmd.msdc_rwindex), sector_num);
			if(write_status == KAL_TRUE)
			{
#ifdef __P_PROPRIETARY_COPYRIGHT__	
				USB_Ms_Update_Read_Protect(g_UsbMS.current_LUN, (g_UsbMS.rw_cmd.LBA + g_UsbMS.rw_cmd.msdc_rwindex), sector_num);
#endif
				g_UsbMS.rw_cmd.msdc_rwindex+=sector_num;
			}
			else
			{
				g_UsbMS.rw_cmd.rw_error_status = KAL_TRUE;
				USB_Ms_Dbg_Trace(USB_DBG_USBMS_CMD_Write_ERROR, g_UsbMS.rw_cmd.rwindex);
				USB_Ms_Dbg_Trace(USB_DBG_USBMS_CMD_Write_ERROR2, g_UsbMS.rw_cmd.msdc_rwindex);
				USB_Ms_Dbg_Trace(USB_DBG_USBMS_CMD_Write_ERROR3,  g_UsbMS.rw_cmd.BlkLen);
			}
		}	
		
		/*rec done*/
		g_UsbMS.nState = USBMS_GETNEXTCMD;
		/*Send CSW*/
		g_UsbMS.CSWDataResidue = (g_UsbMS.rw_cmd.BlkLen-g_UsbMS.rw_cmd.msdc_rwindex)*512;
		USB_Ms_Checkmedia_Exist(g_UsbMS.current_LUN);
		if(g_UsbMS.CSWDataResidue > 0)
		{
			g_UsbMS.CSWStatusError = KAL_TRUE;
			USB_Ms_Dbg_Trace(USB_DBG_USBMS_CMD_Write_ERROR_Residue,  g_UsbMS.CSWDataResidue);
		}
		USB_Ms_Generate_Csw(&g_UsbMS.CSW, CBW);
		USB_Ms_BuildTx(&g_UsbMS.CSW, 13);
		g_UsbMS.rw_cmd.rw_error_status = KAL_FALSE;
	}
	else		/* more data to receive, or ready to receive the first one data packet*/
	{
		/* determine the length to be received*/
		if ((g_UsbMS.rw_cmd.BlkLen - g_UsbMS.rw_cmd.rwindex) <=USBMS_RX_MAX_SECTOR)
		{
			writelen = g_UsbMS.rw_cmd.BlkLen - g_UsbMS.rw_cmd.rwindex;
		}
		else
		{
			writelen = USBMS_RX_MAX_SECTOR;
		}

		USB_Ms_BuildRx((void *)g_UsbMS.disk_buffer->ms_buffer[g_UsbMS.rw_cmd.rw_buffer_index], (512 * writelen));
		g_UsbMS.rw_cmd.rwindex += writelen;


		/* write flash while receive another packet*/
		g_UsbMS.rw_cmd.rw_buffer_index ^= 1;  /*  toggle buffer index */
		if(write_data == KAL_TRUE)
		{
#if 0			
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
#endif

			write_status= USB_Ms_Write(g_UsbMS.current_LUN, g_UsbMS.disk_buffer->ms_buffer[g_UsbMS.rw_cmd.rw_buffer_index], 
								(g_UsbMS.rw_cmd.LBA + g_UsbMS.rw_cmd.msdc_rwindex), sector_num);

#if 0
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
#endif

			if(write_status == KAL_TRUE)
			{
#ifdef __P_PROPRIETARY_COPYRIGHT__	
				USB_Ms_Update_Read_Protect(g_UsbMS.current_LUN, (g_UsbMS.rw_cmd.LBA + g_UsbMS.rw_cmd.msdc_rwindex), sector_num);
#endif
				g_UsbMS.rw_cmd.msdc_rwindex+=sector_num;
			}
			else
			{
				g_UsbMS.rw_cmd.rw_error_status = KAL_TRUE;
				USB_Ms_Dbg_Trace(USB_DBG_USBMS_CMD_Write_ERROR, g_UsbMS.rw_cmd.rwindex);
				USB_Ms_Dbg_Trace(USB_DBG_USBMS_CMD_Write_ERROR2, g_UsbMS.rw_cmd.msdc_rwindex);
				USB_Ms_Dbg_Trace(USB_DBG_USBMS_CMD_Write_ERROR3,  g_UsbMS.rw_cmd.BlkLen);
			}	
		}
	}
}

#ifdef __P_PROPRIETARY_COPYRIGHT__

/* translate fd type to usb dev type*/
USB_STORAGE_DEV_TYPE USB_Ms_Trans_Fs_Type(FS_DEVICE_TYPE_ENUM type)
{
	USB_STORAGE_DEV_TYPE dev_type;
	
	switch(type)
	{
	case FS_DEVICE_TYPE_NOR:
		dev_type = USB_STORAGE_DEV_NOR;
		break;
 	case FS_DEVICE_TYPE_NAND:
 		dev_type = USB_STORAGE_DEV_NAND;
 		break;
 	case FS_DEVICE_TYPE_CARD:
 		dev_type = USB_STORAGE_DEV_CARD;
 		break;
 	default:
 		EXT_ASSERT(0, type, 0, 0);
 		break;
	}
	return dev_type;
}

/* The return value means whether the item is found or not. parameter *p_index is the item index in this array*/
/* If the return value is false, no item is found, and *p_index is the first one just bigger than searched one*/
/* If all items in this array are bigger than the searched one, *p_index will be larger than array size*/
static kal_bool	USB_Ms_Search_Item(kal_uint32* data_items, kal_uint32 data_size, 
										kal_uint32 search_value, kal_uint32 *p_index)
{
	kal_int64 low_index = -1;
	kal_int64 high_index = (kal_int64)data_size;
	kal_int64 index = 0;
	kal_bool	ret_value = KAL_FALSE;

	if(data_size==0)
	{
		*p_index = 0;
		return KAL_FALSE;
	}

	while((high_index-low_index)>1)
	{
		index = (high_index+low_index)/2;
		if(search_value==data_items[index])
		{
			high_index = index;
			low_index = index;
		}
		else if(search_value<data_items[index])
		{
			high_index = index;
		}
		else 
		{
			low_index = index;
		}	
	}

	if(search_value==data_items[index])
	{
		ret_value = KAL_TRUE;
	}
	else
	{
		ret_value = KAL_FALSE;
		if(search_value>data_items[index])
			index++;
		/* If search value is larger than any one, index will be eqaul to data_size but data_size[data_size] not exist*/
		if((index<data_size)&&(search_value>data_items[index]))
			EXT_ASSERT(0, search_value, data_items[index], index);
	}

	*p_index = index;
	return ret_value;
}

/* Sort the protected items got from file system */
void USB_Ms_Sort_Protect_Items(kal_uint32* data_item, kal_uint32* second_data_item)
{
	kal_uint32 index = 0;
	kal_uint32 *p_sort_buff = (kal_uint32*)g_UsbMS.disk_buffer;
	kal_bool	b_found = KAL_FALSE;
	kal_uint32 find_index;
	
	/* After sorting, data item is sorted array*/
	g_UsbMS.read_protect_num = data_item[0];

	/* sorting data_item*/
	data_item[0] = data_item[1];
	second_data_item[0] = second_data_item[1];

	/* sort list 1*/
	for(index = 1; index < g_UsbMS.read_protect_num; index++)
	{
		b_found = USB_Ms_Search_Item(data_item, index, data_item[index+1], &find_index);
		/* Should not have two same itmes*/
		if(b_found==KAL_TRUE)
			EXT_ASSERT(0, index, g_UsbMS.read_protect_num, data_item[index+1]);
		/* update list 1*/
		kal_mem_cpy(p_sort_buff, &data_item[find_index], (index-find_index)*sizeof(kal_uint32));
		data_item[find_index] = data_item[index+1];
		kal_mem_cpy(&data_item[find_index+1], p_sort_buff, (index-find_index)*sizeof(kal_uint32));

		/* update list 2*/
		kal_mem_cpy(p_sort_buff, &second_data_item[find_index], (index-find_index)*sizeof(kal_uint32));
		second_data_item[find_index] = second_data_item[index+1];
		kal_mem_cpy(&second_data_item[find_index+1], p_sort_buff, (index-find_index)*sizeof(kal_uint32));
	}

	/* initailize index 1 and index 2*/
	for(index = 0; index < g_UsbMS.read_protect_num; index++)
	{
		g_UsbMS.read_protect_index[index] = index;
	}
	for(index = 0; index < g_UsbMS.read_protect_num; index++)
	{
		g_UsbMS.read_protect_second_index[index] = index;
	}
	
	/* sort list 2 and update index 2*/
	for(index = 1; index < g_UsbMS.read_protect_num; index++)
	{
		b_found = USB_Ms_Search_Item(second_data_item, index, second_data_item[index], &find_index);
		/* If the file dose not have second cluster, the values are all 0xffffffff*/
		//if(b_found==KAL_TRUE)
		//	EXT_ASSERT(0, index, g_UsbMS.read_protect_num, second_data_item[index]);
		/* update list 2 */
		kal_mem_cpy(p_sort_buff, &second_data_item[find_index], (index-find_index)*sizeof(kal_uint32));
		second_data_item[find_index] = second_data_item[index];
		kal_mem_cpy(&second_data_item[find_index+1], p_sort_buff, (index-find_index)*sizeof(kal_uint32));

		/* update index 2 */
		kal_mem_cpy(p_sort_buff, &g_UsbMS.read_protect_second_index[find_index], (index-find_index)*sizeof(kal_uint32));
		g_UsbMS.read_protect_second_index[find_index] = g_UsbMS.read_protect_second_index[index];
		kal_mem_cpy(&g_UsbMS.read_protect_second_index[find_index+1], p_sort_buff, (index-find_index)*sizeof(kal_uint32));
	}

	/* update index1 */
	for(index = 0; index < g_UsbMS.read_protect_num; index++)
	{
		g_UsbMS.read_protect_index[g_UsbMS.read_protect_second_index[index]] = index;
	}
	
	//g_UsbMS.read_protect_items = data_item;
	//g_UsbMS.read_protect_second_items = second_data_item;
}

/* This is used in run time for sorting index list */
static void USB_Ms_Sort_Index_Items(kal_uint32* items, kal_uint32 num)
{
	kal_uint32 index = 0;
	kal_bool	b_found = KAL_FALSE;
	kal_uint32 find_index;
	kal_uint32 sort_index;
	kal_uint32 sort_data;

	if(num==1)
		return;
	
	/* sort list*/
	for(index = 1; index < num; index++)
	{
		b_found = USB_Ms_Search_Item(items, index, items[index], &find_index);
		/* Should not have two same itmes*/
		if(b_found==KAL_TRUE)
			EXT_ASSERT(0, index, num, items[index]);
		/* update list 1*/
		sort_data = items[index];
		for(sort_index = (index-1); sort_index>= find_index; sort_index--)
		{
			items[sort_index+1] = items[sort_index];
			if(sort_index==0)
				break;
		}
		items[find_index] = sort_data;	
	}
}

/* check if it is read protected sectors */
static kal_bool	USB_Ms_Check_Read_Protect(kal_uint8 LUN, kal_uint32 LBA, kal_uint32 sec_num)
{
	kal_uint32 index;
	kal_bool b_found = KAL_FALSE;

	if(USB_Ms_Dev_Type(LUN)==g_UsbMS.read_protect_dev_type)
	{	
		b_found = USB_Ms_Search_Item(g_UsbMS.read_protect_items, g_UsbMS.read_protect_num, LBA, &index);

		if(b_found==KAL_FALSE)
		{
			/* If match this conditino, read should also be protected */
			if((index<g_UsbMS.read_protect_num)&&((LBA+sec_num-1)>=g_UsbMS.read_protect_items[index]))
				b_found = KAL_TRUE;	
		}
	}	

	return b_found;
}

/* check if it is second read protected sectors */
static kal_bool	USB_Ms_Check_Second_Read_Protect(kal_uint8 LUN, kal_uint32 LBA, kal_uint32 sec_num)
{
	kal_uint32 index;
	kal_bool b_found = KAL_FALSE;

	if(USB_Ms_Dev_Type(LUN)==g_UsbMS.read_protect_dev_type)
	{	
		b_found = USB_Ms_Search_Item(g_UsbMS.read_protect_second_items, g_UsbMS.read_protect_num, LBA, &index);

		if(b_found==KAL_FALSE)
		{
			/* If match this conditino, read should also be protected */
			if((index<g_UsbMS.read_protect_num)&&((LBA+sec_num-1)>=g_UsbMS.read_protect_second_items[index]))
				b_found = KAL_TRUE;	
		}
	}	

	return b_found;
}

/* update read protect list when wrtiting these sectors */
static void USB_Ms_Update_Read_Protect(kal_uint8 LUN, kal_uint32 LBA, kal_uint32 sec_num)
{
	kal_uint32 index_min, index_max;
	kal_bool b_found_min = KAL_FALSE;
	kal_bool b_found_max = KAL_FALSE;

	kal_uint32 update_length;
	kal_uint32 index, index_2;
	kal_uint32 protect_num;
	
	/* If media type is not removable, update the read protect region if it falls in this region */
	if(USB_Ms_Dev_Type(LUN)==g_UsbMS.read_protect_dev_type)
	{
		/*******************************/
		/*******update the first list*********/
		/*******************************/
		b_found_min = USB_Ms_Search_Item(g_UsbMS.read_protect_items, g_UsbMS.read_protect_num, LBA, &index_min);
		b_found_max = USB_Ms_Search_Item(g_UsbMS.read_protect_items, g_UsbMS.read_protect_num, LBA+sec_num-1, &index_max);

		update_length = index_max-index_min;
		if(b_found_max==KAL_TRUE)
		{
			update_length++;
			index_max++;
		}
		
		if(update_length>0)
		{
			/****** update list 2 and index 2 *****/
			protect_num = g_UsbMS.read_protect_num;

			/* determine index 2 id */
			for(index = 0; index < update_length; index++)
			{
				/* decrement index 2*/
				for(index_2 = 0; index_2 < protect_num; index_2++)
				{
					if(g_UsbMS.read_protect_second_index[index_2] > 
						g_UsbMS.read_protect_second_index[g_UsbMS.read_protect_index[index_min+index]])
					{
						g_UsbMS.read_protect_second_index[index_2]--;
					}
				}
			}	
			/* *sort index list, and delete list2/index2 by sequence */
			USB_Ms_Sort_Index_Items(&g_UsbMS.read_protect_index[index_min], update_length);
			for(index = 0; index < update_length; index++)
			{
				kal_mem_cpy(&g_UsbMS.read_protect_second_items[g_UsbMS.read_protect_index[index_min+index] -index], 
							&g_UsbMS.read_protect_second_items[g_UsbMS.read_protect_index[index_min+index] -index +1], 
							(protect_num - (g_UsbMS.read_protect_index[index_min+index] - index) - 1)*sizeof(kal_uint32));
				kal_mem_cpy(&g_UsbMS.read_protect_second_index[g_UsbMS.read_protect_index[index_min+index] -index], 
							&g_UsbMS.read_protect_second_index[g_UsbMS.read_protect_index[index_min+index] -index +1], 
							(protect_num - (g_UsbMS.read_protect_index[index_min+index] - index) - 1)*sizeof(kal_uint32));
				protect_num--;
			}


			/****** update list 1 and index 1 *****/
			protect_num = g_UsbMS.read_protect_num;

			/* determine index 1 id */
			for(index = 0; index < update_length; index++)
			{
				/* decrement index 1*/
				for(index_2 = 0; index_2 < protect_num; index_2++)
				{
					if(g_UsbMS.read_protect_index[index_2] > g_UsbMS.read_protect_index[index_min+index])
					{
						g_UsbMS.read_protect_index[index_2]--;
					}
				}
			}

			/* delete list1/index1 */
			kal_mem_cpy(&g_UsbMS.read_protect_items[index_min], &g_UsbMS.read_protect_items[index_max], 
						(g_UsbMS.read_protect_num - index_max)*sizeof(kal_uint32));
			kal_mem_cpy(&g_UsbMS.read_protect_index[index_min], &g_UsbMS.read_protect_index[index_max], 
						(g_UsbMS.read_protect_num - index_max)*sizeof(kal_uint32));

			g_UsbMS.read_protect_num-=update_length;
		}


		/*******************************/
		/*******update the second list******/
		/*******************************/
		b_found_min = USB_Ms_Search_Item(g_UsbMS.read_protect_second_items, g_UsbMS.read_protect_num, LBA, &index_min);
		b_found_max = USB_Ms_Search_Item(g_UsbMS.read_protect_second_items, g_UsbMS.read_protect_num, LBA+sec_num-1, &index_max);

		update_length = index_max-index_min;
		if(b_found_max==KAL_TRUE)
		{
			update_length++;
			index_max++;
		}
		
		if(update_length>0)
		{
			/****** update list 1 and index 1 *****/
			protect_num = g_UsbMS.read_protect_num;

			/* determine index 1 id */
			for(index = 0; index < update_length; index++)
			{
				/* decrement index 1*/
				for(index_2 = 0; index_2 < protect_num; index_2++)
				{
					if(g_UsbMS.read_protect_index[index_2] > 
						g_UsbMS.read_protect_index[g_UsbMS.read_protect_second_index[index_min+index]])
					{
						g_UsbMS.read_protect_index[index_2]--;
					}
				}
			}	
			/* *sort index list, and delete list1/index1 by sequence */
			USB_Ms_Sort_Index_Items(&g_UsbMS.read_protect_second_index[index_min], update_length);
			for(index = 0; index < update_length; index++)
			{
				kal_mem_cpy(&g_UsbMS.read_protect_items[g_UsbMS.read_protect_second_index[index_min+index] -index], 
							&g_UsbMS.read_protect_items[g_UsbMS.read_protect_second_index[index_min+index] -index +1], 
							(protect_num - (g_UsbMS.read_protect_second_index[index_min+index] - index) - 1)*sizeof(kal_uint32));
				kal_mem_cpy(&g_UsbMS.read_protect_index[g_UsbMS.read_protect_second_index[index_min+index] -index], 
							&g_UsbMS.read_protect_index[g_UsbMS.read_protect_second_index[index_min+index] -index +1], 
							(protect_num - (g_UsbMS.read_protect_second_index[index_min+index] - index) - 1)*sizeof(kal_uint32));
				protect_num--;
			}

			/****** update list 2 and index 2 *****/
			protect_num = g_UsbMS.read_protect_num;

			/* determine index 2 id */
			for(index = 0; index < update_length; index++)
			{
				/* decrement index 2*/
				for(index_2 = 0; index_2 < protect_num; index_2++)
				{
					if(g_UsbMS.read_protect_second_index[index_2] > g_UsbMS.read_protect_second_index[index_min+index])
					{
						g_UsbMS.read_protect_second_index[index_2]--;
					}
				}
			}	

			/* delete list2/index2 */
			kal_mem_cpy(&g_UsbMS.read_protect_second_items[index_min], &g_UsbMS.read_protect_second_items[index_max], 
						(g_UsbMS.read_protect_num - index_max)*sizeof(kal_uint32));
			kal_mem_cpy(&g_UsbMS.read_protect_second_index[index_min], &g_UsbMS.read_protect_second_index[index_max], 
						(g_UsbMS.read_protect_num - index_max)*sizeof(kal_uint32));

			g_UsbMS.read_protect_num-=update_length;
		}
	}
}

#endif /* __P_PROPRIETARY_COPYRIGHT__ */


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -