/* Free all memory and restore the system to the state it was in before calling CreatePopulateInstallShellProtocol. */
EFI_STATUS CleanUpShellProtocol(IN OUT EFI_SHELL_PROTOCOL *NewShell)
/* Free all memory and restore the system to the state it was in before calling CreatePopulateInstallShellProtocol. */
EFI_STATUS CleanUpShellProtocol(IN OUT EFI_SHELL_PROTOCOL *NewShell)
{
  SHELL_PROTOCOL_HANDLE_LIST  *Node2;
  if (!IsListEmpty (&ShellInfoObject.OldShellList.Link)) {
    for (Node2 = (SHELL_PROTOCOL_HANDLE_LIST *)GetFirstNode (&ShellInfoObject.OldShellList.Link)
         ; !IsListEmpty (&ShellInfoObject.OldShellList.Link)
         ; Node2 = (SHELL_PROTOCOL_HANDLE_LIST *)GetFirstNode (&ShellInfoObject.OldShellList.Link)
         )
    {
      RemoveEntryList (&Node2->Link);
      gBS->ReinstallProtocolInterface (Node2->Handle, &gEfiShellProtocolGuid, NewShell, Node2->Interface);
      FreePool (Node2);
    }
  } else {
    gBS->UninstallProtocolInterface (gImageHandle, &gEfiShellProtocolGuid, NewShell);
  }
  return EFI_SUCCESS;
}