+\r
+/**\r
+ Read data from debug device and save the data in buffer.\r
+\r
+ Reads NumberOfBytes data bytes from a debug device into the buffer\r
+ specified by Buffer. The number of bytes actually read is returned.\r
+ If the return value is less than NumberOfBytes, then the rest operation failed.\r
+ If NumberOfBytes is zero, then return 0.\r
+\r
+ @param Handle Debug port handle.\r
+ @param Buffer Pointer to the data buffer to store the data read from the debug device.\r
+ @param NumberOfBytes Number of bytes which will be read.\r
+ @param Timeout Timeout value for reading from debug device. It unit is Microsecond.\r
+\r
+ @retval 0 Read data failed, no data is to be read.\r
+ @retval >0 Actual number of bytes read from debug device.\r
+\r
+**/\r
+UINTN\r
+DebugAgentReadBuffer (\r
+ IN DEBUG_PORT_HANDLE Handle,\r
+ IN UINT8 *Buffer,\r
+ IN UINTN NumberOfBytes,\r
+ IN UINTN Timeout\r
+ )\r
+{\r
+ UINTN Index;\r
+ UINT32 Begin;\r
+ UINT32 TimeoutTicker;\r
+ UINT32 TimerRound;\r
+ UINT32 TimerFrequency;\r
+ UINT32 TimerCycle;\r
+ \r
+ Begin = 0;\r
+ TimeoutTicker = 0; \r
+ TimerRound = 0;\r
+ TimerFrequency = GetMailboxPointer()->DebugTimerFrequency;\r
+ TimerCycle = GetApicTimerInitCount ();\r
+\r
+ if (Timeout != 0) {\r
+ Begin = GetApicTimerCurrentCount ();\r
+ TimeoutTicker = (UINT32) DivU64x32 (\r
+ MultU64x64 (\r
+ TimerFrequency,\r
+ Timeout\r
+ ),\r
+ 1000000u\r
+ );\r
+ TimerRound = (UINT32) DivU64x32Remainder (TimeoutTicker, TimerCycle / 2, &TimeoutTicker);\r
+ }\r
+ Index = 0;\r
+ while (Index < NumberOfBytes) {\r
+ if (DebugPortPollBuffer (Handle)) {\r
+ DebugPortReadBuffer (Handle, Buffer + Index, 1, 0);\r
+ Index ++; \r
+ continue;\r
+ }\r
+ if (Timeout != 0) {\r
+ if (TimerRound == 0) {\r
+ if (IsDebugTimerTimeout (TimerCycle, Begin, TimeoutTicker)) {\r
+ //\r
+ // If time out occurs.\r
+ //\r
+ return 0;\r
+ }\r
+ } else {\r
+ if (IsDebugTimerTimeout (TimerCycle, Begin, TimerCycle / 2)) {\r
+ TimerRound --;\r
+ Begin = GetApicTimerCurrentCount ();\r
+ }\r
+ }\r
+ }\r
+ }\r
+\r
+ return Index;\r
+}\r
+\r