+Index: shellenv/Connect.c\r
+===================================================================\r
+--- shellenv/Connect.c (revision 30)\r
++++ shellenv/Connect.c (working copy)\r
+@@ -831,6 +831,16 @@\r
+ return Status;\r
+ }\r
+ \r
++ //\r
++ // Close proxy console before disconnect all devices.\r
++ //\r
++ SEnvCloseConsoleProxy (\r
++ ST->ConsoleInHandle,\r
++ &ST->ConIn,\r
++ ST->ConsoleOutHandle,\r
++ &ST->ConOut\r
++ );\r
++\r
+ for (Index = 0; Index < AllHandleCount; Index++) {\r
+ //\r
+ // Check whether the handle is still in handle database\r
+@@ -1004,7 +1014,6 @@\r
+ VOID\r
+ SEnvConnectConsole (\r
+ CHAR16 *VariableName,\r
+- EFI_GUID *PrimaryGuid,\r
+ EFI_GUID *ConsoleGuid,\r
+ EFI_HANDLE *ConsoleHandle,\r
+ VOID **ConsoleInterface\r
+@@ -1018,9 +1027,8 @@\r
+ EFI_HANDLE *AllHandleBuffer;\r
+ VOID *Interface;\r
+ \r
+- *ConsoleHandle = NULL;\r
+- *ConsoleInterface = NULL;\r
+ ConsoleIndex = 0;\r
++ AllHandleBuffer = NULL;\r
+ \r
+ DevicePath = LibGetVariable (VariableName, &gEfiGlobalVariableGuid);\r
+ if (DevicePath != NULL) {\r
+@@ -1028,17 +1036,19 @@\r
+ FreePool (DevicePath);\r
+ }\r
+ \r
+- AllHandleBuffer = NULL;\r
+- Status = BS->LocateHandleBuffer (\r
+- ByProtocol,\r
+- PrimaryGuid,\r
+- NULL,\r
+- &AllHandleCount,\r
+- &AllHandleBuffer\r
+- );\r
+- if (!EFI_ERROR (Status) && AllHandleCount > 0) {\r
+- *ConsoleHandle = AllHandleBuffer[0];\r
+- } else if (*ConsoleHandle == NULL) {\r
++ Status = EFI_NOT_FOUND;\r
++ //\r
++ // Check ConsoleHandle validation whatever it was updated or not.\r
++ //\r
++ if (*ConsoleHandle != NULL) {\r
++ Status = BS->HandleProtocol (\r
++ *ConsoleHandle,\r
++ ConsoleGuid,\r
++ &Interface\r
++ );\r
++ }\r
++\r
++ if (EFI_ERROR (Status)) {\r
+ AllHandleBuffer = NULL;\r
+ Status = BS->LocateHandleBuffer (\r
+ ByProtocol,\r
+@@ -1063,16 +1073,16 @@\r
+ &gEfiDevicePathProtocolGuid,\r
+ &Interface\r
+ );\r
+- if (EFI_ERROR (Status)) {\r
++ if (!EFI_ERROR (Status)) {\r
+ ConsoleIndex = Index;\r
+ break;\r
+ }\r
+ }\r
++ \r
++ *ConsoleHandle = AllHandleBuffer[ConsoleIndex];\r
+ }\r
+ }\r
+ \r
+- *ConsoleHandle = AllHandleBuffer[ConsoleIndex];\r
+-\r
+ if (*ConsoleHandle != NULL) {\r
+ BS->HandleProtocol (\r
+ *ConsoleHandle,\r
+@@ -1081,7 +1091,7 @@\r
+ );\r
+ }\r
+ \r
+- if (AllHandleBuffer) {\r
++ if (AllHandleBuffer != NULL) {\r
+ FreePool (AllHandleBuffer);\r
+ }\r
+ }\r
+@@ -1189,8 +1199,7 @@\r
+ EFI_STATUS Status;\r
+ \r
+ //\r
+- // Check current ConIn and ConOut to ensure it is the ConsoleProxy\r
+- // Otherwise, the Console should not be close\r
++ // Check current ConIn and ConOut to judge it is the ConsoleProxy\r
+ //\r
+ Status = SEnvCheckConsoleProxy (\r
+ ST->ConsoleInHandle,\r
+@@ -1203,23 +1212,21 @@\r
+ // Indicate the Console is replaced by redirection operation\r
+ // It is not safe to connect Console here\r
+ //\r
+- if (EFI_ERROR (Status)) {\r
+- return ;\r
++ if (!EFI_ERROR (Status)) {\r
++ //\r
++ // It is proxy console, to prevent the proxy console form being restored to original console,\r
++ // close it temporary before connect all console\r
++ //\r
++ SEnvCloseConsoleProxy (\r
++ ST->ConsoleInHandle,\r
++ &ST->ConIn,\r
++ ST->ConsoleOutHandle,\r
++ &ST->ConOut\r
++ );\r
+ }\r
+- //\r
+- // To prevent the proxy console form being restored to original console,\r
+- // close it temporary before connect all console\r
+- //\r
+- SEnvCloseConsoleProxy (\r
+- ST->ConsoleInHandle,\r
+- &ST->ConIn,\r
+- ST->ConsoleOutHandle,\r
+- &ST->ConOut\r
+- );\r
+ \r
+ SEnvConnectConsole (\r
+ VarErrorOut,\r
+- &gEfiPrimaryStandardErrorDeviceGuid,\r
+ &gEfiSimpleTextOutProtocolGuid,\r
+ &ST->StandardErrorHandle,\r
+ (VOID **) &ST->StdErr\r
+@@ -1227,7 +1234,6 @@\r
+ \r
+ SEnvConnectConsole (\r
+ VarConsoleOut,\r
+- &gEfiPrimaryConsoleOutDeviceGuid,\r
+ &gEfiSimpleTextOutProtocolGuid,\r
+ &ST->ConsoleOutHandle,\r
+ (VOID **) &ST->ConOut\r
+@@ -1235,7 +1241,6 @@\r
+ \r
+ SEnvConnectConsole (\r
+ VarConsoleIn,\r
+- &gEfiPrimaryConsoleInDeviceGuid,\r
+ &gEfiSimpleTextInProtocolGuid,\r
+ &ST->ConsoleInHandle,\r
+ (VOID **) &ST->ConIn\r
+@@ -1254,6 +1259,8 @@\r
+ SetCrc (&ST->Hdr);\r
+ }\r
+ \r
++\r
++\r
+ EFI_STATUS\r
+ EFIAPI\r
+ SEnvCmdReconnect (\r