return -1;
 #endif
 
-#if !defined(CONFIG_SPL_BUILD) && CONFIG_MMC_SUNXI_SLOT_EXTRA == 2
-       /*
-        * On systems with an emmc (mmc2), figure out if we are booting from
-        * the emmc and if we are make it "mmc dev 0" so that boot.scr, etc.
-        * are searched there first. Note we only do this for u-boot proper,
-        * not for the SPL, see spl_boot_device().
-        */
-       if (readb(SPL_ADDR + 0x28) == SUNXI_BOOTED_FROM_MMC2) {
-               /* Booting from emmc / mmc2, swap */
-               mmc0->block_dev.devnum = 1;
-               mmc1->block_dev.devnum = 0;
-       }
-#endif
-
        return 0;
 }
 #endif
 
        env_set("fel_booted", NULL);
        env_set("fel_scriptaddr", NULL);
+       env_set("mmc_bootdev", NULL);
 
        boot = sunxi_get_boot_device();
        /* determine if we are running in FEL mode */
        if (boot == BOOT_DEVICE_BOARD) {
                env_set("fel_booted", "1");
                parse_spl_header(SPL_ADDR);
+       /* or if we booted from MMC, and which one */
+       } else if (boot == BOOT_DEVICE_MMC1) {
+               env_set("mmc_bootdev", "0");
+       } else if (boot == BOOT_DEVICE_MMC2) {
+               env_set("mmc_bootdev", "1");
        }
 
        setup_environment(gd->fdt_blob);
 
 config FASTBOOT_FLASH_MMC_DEV
        int "Define FASTBOOT MMC FLASH default device"
        depends on FASTBOOT_FLASH && MMC
+       default 0 if ARCH_SUNXI && MMC_SUNXI_SLOT_EXTRA = -1
+       default 1 if ARCH_SUNXI && MMC_SUNXI_SLOT_EXTRA != -1
        help
          The fastboot "flash" command requires additional information
          regarding the non-volatile storage device. Define this to
 
 CONFIG_SPL=y
 CONFIG_SPL_I2C_SUPPORT=y
 CONFIG_FASTBOOT_FLASH=y
-CONFIG_FASTBOOT_FLASH_MMC_DEV=0
 # CONFIG_CMD_IMLS is not set
 CONFIG_CMD_DFU=y
 # CONFIG_CMD_FLASH is not set
 
 # CONFIG_SYS_MALLOC_CLEAR_ON_INIT is not set
 CONFIG_SPL=y
 CONFIG_FASTBOOT_FLASH=y
-CONFIG_FASTBOOT_FLASH_MMC_DEV=0
 # CONFIG_CMD_IMLS is not set
 CONFIG_CMD_DFU=y
 # CONFIG_CMD_FLASH is not set
 
 # CONFIG_SYS_MALLOC_CLEAR_ON_INIT is not set
 CONFIG_SPL=y
 CONFIG_FASTBOOT_FLASH=y
-CONFIG_FASTBOOT_FLASH_MMC_DEV=0
 # CONFIG_CMD_IMLS is not set
 # CONFIG_CMD_FLASH is not set
 # CONFIG_CMD_FPGA is not set
 
 #endif
 
 #if defined(CONFIG_ENV_IS_IN_MMC)
-#define CONFIG_SYS_MMC_ENV_DEV         0       /* first detected MMC controller */
+#if CONFIG_MMC_SUNXI_SLOT_EXTRA != -1
+/* If we have two devices (most likely eMMC + MMC), favour the eMMC */
+#define CONFIG_SYS_MMC_ENV_DEV         1
+#else
+/* Otherwise, use the only device we have */
+#define CONFIG_SYS_MMC_ENV_DEV         0
+#endif
 #define CONFIG_SYS_MMC_MAX_DEVICE      4
 #elif defined(CONFIG_ENV_IS_NOWHERE)
 #define CONFIG_ENV_SIZE                        (128 << 10)
        "ramdisk ram " RAMDISK_ADDR_R " 0x4000000\0"
 
 #ifdef CONFIG_MMC
-#define BOOT_TARGET_DEVICES_MMC(func) func(MMC, mmc, 0)
 #if CONFIG_MMC_SUNXI_SLOT_EXTRA != -1
-#define BOOT_TARGET_DEVICES_MMC_EXTRA(func) func(MMC, mmc, 1)
+#define BOOTENV_DEV_MMC_AUTO(devtypeu, devtypel, instance)             \
+       BOOTENV_DEV_MMC(MMC, mmc, 0)                                    \
+       BOOTENV_DEV_MMC(MMC, mmc, 1)                                    \
+       "bootcmd_mmc_auto="                                             \
+               "if test ${mmc_bootdev} -eq 1; then "                   \
+                       "run bootcmd_mmc1; "                            \
+                       "run bootcmd_mmc0; "                            \
+               "elif test ${mmc_bootdev} -eq 0; then "                 \
+                       "run bootcmd_mmc0; "                            \
+                       "run bootcmd_mmc1; "                            \
+               "fi\0"
+
+#define BOOTENV_DEV_NAME_MMC_AUTO(devtypeu, devtypel, instance) \
+       "mmc_auto "
+
+#define BOOT_TARGET_DEVICES_MMC(func) func(MMC_AUTO, mmc_auto, na)
 #else
-#define BOOT_TARGET_DEVICES_MMC_EXTRA(func)
+#define BOOT_TARGET_DEVICES_MMC(func) func(MMC, mmc, 0)
 #endif
 #else
 #define BOOT_TARGET_DEVICES_MMC(func)
-#define BOOT_TARGET_DEVICES_MMC_EXTRA(func)
 #endif
 
 #ifdef CONFIG_AHCI
 #define BOOT_TARGET_DEVICES(func) \
        func(FEL, fel, na) \
        BOOT_TARGET_DEVICES_MMC(func) \
-       BOOT_TARGET_DEVICES_MMC_EXTRA(func) \
        BOOT_TARGET_DEVICES_SCSI(func) \
        BOOT_TARGET_DEVICES_USB(func) \
        func(PXE, pxe, na) \