diff --git a/drivers/pinctrl/pinctrl-uclass.c b/drivers/pinctrl/pinctrl-uclass.c
index d7e38ae7291c844025b6003dfa4d97accb1e9da2..c38bb212ed74bd74c0f7a3f7194fa1f7b7f24546 100644
--- a/drivers/pinctrl/pinctrl-uclass.c
+++ b/drivers/pinctrl/pinctrl-uclass.c
@@ -198,6 +198,12 @@ static int pinctrl_select_state_simple(struct udevice *dev)
 
 int pinctrl_select_state(struct udevice *dev, const char *statename)
 {
+	/*
+	 * Some device which is logical like mmc.blk, do not have
+	 * a valid ofnode.
+	 */
+	if (!ofnode_valid(dev->node))
+		return 0;
 	/*
 	 * Try full-implemented pinctrl first.
 	 * If it fails or is not implemented, try simple one.
diff --git a/tools/buildman/builderthread.py b/tools/buildman/builderthread.py
index fa9dec043a3b09624350febb24b6e1a1c0e41f02..0efe80d9457b2c4d0ae709fbeed3a60bf3dda76b 100644
--- a/tools/buildman/builderthread.py
+++ b/tools/buildman/builderthread.py
@@ -6,6 +6,7 @@ import errno
 import glob
 import os
 import shutil
+import sys
 import threading
 
 import command
@@ -26,6 +27,9 @@ def Mkdir(dirname, parents = False):
             os.mkdir(dirname)
     except OSError as err:
         if err.errno == errno.EEXIST:
+            if os.path.realpath('.') == os.path.realpath(dirname):
+                print "Cannot create the current working directory '%s'!" % dirname
+                sys.exit(1)
             pass
         else:
             raise
diff --git a/tools/buildman/control.py b/tools/buildman/control.py
index c14b87842da5c9ee04af5772243f5afddb5abb63..4ac4386db62676264d990f657b1972ead56b03f7 100644
--- a/tools/buildman/control.py
+++ b/tools/buildman/control.py
@@ -80,6 +80,28 @@ def ShowActions(series, why_selected, boards_selected, builder, options):
     print ('Total boards to build for each commit: %d\n' %
             len(why_selected['all']))
 
+def CheckOutputDir(output_dir):
+    """Make sure that the output directory is not within the current directory
+
+    If we try to use an output directory which is within the current directory
+    (which is assumed to hold the U-Boot source) we may end up deleting the
+    U-Boot source code. Detect this and print an error in this case.
+
+    Args:
+        output_dir: Output directory path to check
+    """
+    path = os.path.realpath(output_dir)
+    cwd_path = os.path.realpath('.')
+    while True:
+        if os.path.realpath(path) == cwd_path:
+            Print("Cannot use output directory '%s' since it is within the current directtory '%s'" %
+                  (path, cwd_path))
+            sys.exit(1)
+        parent = os.path.dirname(path)
+        if parent == path:
+            break
+        path = parent
+
 def DoBuildman(options, args, toolchains=None, make_func=None, boards=None,
                clean_dir=False):
     """The main control code for buildman
@@ -251,9 +273,9 @@ def DoBuildman(options, args, toolchains=None, make_func=None, boards=None,
         # output directory itself rather than any subdirectory.
         if not options.no_subdirs:
             output_dir = os.path.join(options.output_dir, dirname)
-    if (clean_dir and output_dir != options.output_dir and
-            os.path.exists(output_dir)):
-        shutil.rmtree(output_dir)
+        if clean_dir and os.path.exists(output_dir):
+            shutil.rmtree(output_dir)
+    CheckOutputDir(output_dir)
     builder = Builder(toolchains, output_dir, options.git_dir,
             options.threads, options.jobs, gnu_make=gnu_make, checkout=True,
             show_unknown=options.show_unknown, step=options.step,
diff --git a/tools/buildman/func_test.py b/tools/buildman/func_test.py
index 8d96c1a94da6dc84b2f80931a43e61ff9a5a5286..9206fb299d43a0e61c99538ebbac30dfff33320a 100644
--- a/tools/buildman/func_test.py
+++ b/tools/buildman/func_test.py
@@ -519,3 +519,12 @@ class TestFunctional(unittest.TestCase):
         self._RunControl('-b', self._test_branch, clean_dir=False)
         self.assertEqual(self._builder.count, self._total_builds)
         self.assertEqual(self._builder.fail, 0)
+
+    def testBadOutputDir(self):
+        """Test building with an output dir the same as out current dir"""
+        self._test_branch = '/__dev/__testbranch'
+        with self.assertRaises(SystemExit):
+            self._RunControl('-b', self._test_branch, '-o', os.getcwd())
+        with self.assertRaises(SystemExit):
+            self._RunControl('-b', self._test_branch, '-o',
+                             os.path.join(os.getcwd(), 'test'))