spl: change return values of spl_*_load_image()

Make spl_*_load_image() functions return a value instead of
hanging if a problem is encountered. This enables main spl code
to make the decision whether to hang or not, thus preparing
it to support alternative boot devices.

Some boot devices (namely nand and spi) do not hang on error.
Instead, they return normally and SPL proceeds to boot the
contents of the load address. This is considered a bug and
is rectified by hanging on error for these devices as well.

Signed-off-by: Nikita Kiryanov <nikita@compulab.co.il>
Cc: Igor Grinberg <grinberg@compulab.co.il>
Cc: Tom Rini <trini@konsulko.com>
Cc: Simon Glass <sjg@chromium.org>
Cc: Ian Campbell <ijc@hellion.org.uk>
Cc: Hans De Goede <hdegoede@redhat.com>
Cc: Albert Aribaud <albert.u.boot@aribaud.net>
Cc: Jagan Teki <jteki@openedev.com>
Reviewed-by: Tom Rini <trini@konsulko.com>
Reviewed-by: Simon Glass <sjg@chromium.org>
diff --git a/common/spl/spl_mmc.c b/common/spl/spl_mmc.c
index 7d100fa..b68190a 100644
--- a/common/spl/spl_mmc.c
+++ b/common/spl/spl_mmc.c
@@ -10,6 +10,7 @@
 #include <dm.h>
 #include <spl.h>
 #include <linux/compiler.h>
+#include <errno.h>
 #include <asm/u-boot.h>
 #include <errno.h>
 #include <mmc.h>
@@ -220,25 +221,27 @@
 }
 #endif
 
-void spl_mmc_load_image(void)
+int spl_mmc_load_image(void)
 {
 	struct mmc *mmc;
 	u32 boot_mode;
 	int err = 0;
 	__maybe_unused int part;
 
-	if (spl_mmc_find_device(&mmc))
-		hang();
+	err = spl_mmc_find_device(&mmc);
+	if (err)
+		return err;
 
 	err = mmc_init(mmc);
 	if (err) {
 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
 		printf("spl: mmc init failed with error: %d\n", err);
 #endif
-		hang();
+		return err;
 	}
 
 	boot_mode = spl_boot_mode();
+	err = -EINVAL;
 	switch (boot_mode) {
 	case MMCSD_MODE_EMMCBOOT:
 			/*
@@ -251,11 +254,12 @@
 			if (part == 7)
 				part = 0;
 
-			if (mmc_switch_part(0, part)) {
+			err = mmc_switch_part(0, part);
+			if (err) {
 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
 				puts("spl: mmc partition switch failed\n");
 #endif
-				hang();
+				return err;
 			}
 			/* Fall through */
 	case MMCSD_MODE_RAW:
@@ -264,18 +268,18 @@
 		if (!spl_start_uboot()) {
 			err = mmc_load_image_raw_os(mmc);
 			if (!err)
-				return;
+				return err;
 		}
 
 		err = mmc_load_image_raw_partition(mmc,
 			CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION);
 		if (!err)
-			return;
+			return err;
 #if defined(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR)
 		err = mmc_load_image_raw_sector(mmc,
 			CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
 		if (!err)
-			return;
+			return err;
 #endif
 		break;
 	case MMCSD_MODE_FS:
@@ -283,7 +287,7 @@
 
 		err = spl_mmc_do_fs_boot(mmc);
 		if (!err)
-			return;
+			return err;
 
 		break;
 	case MMCSD_MODE_UNDEFINED:
@@ -293,5 +297,5 @@
 #endif
 	}
 
-	hang();
+	return err;
 }