]> git.baikalelectronics.ru Git - uboot.git/commitdiff
bootstd: Add a SPI flash bootdev
authorSimon Glass <sjg@chromium.org>
Tue, 17 Jan 2023 17:48:03 +0000 (10:48 -0700)
committerTom Rini <trini@konsulko.com>
Mon, 23 Jan 2023 23:11:41 +0000 (18:11 -0500)
Add a bootdev for SPI flash so that these devices can be used with
standard boot. It only supports loading a script.

Add a special case for the label, since we want to use "spi", not
"spi_flash".

Enable the new bootdev on sandbox.

Signed-off-by: Simon Glass <sjg@chromium.org>
boot/bootdev-uclass.c
boot/bootflow.c
configs/sandbox_defconfig
configs/sandbox_flattree_defconfig
drivers/mtd/spi/Kconfig
drivers/mtd/spi/Makefile
drivers/mtd/spi/sf-uclass.c
drivers/mtd/spi/sf_bootdev.c [new file with mode: 0644]
include/bootflow.h
test/boot/bootdev.c

index f43307d006dbad2716d1857c2d8eabea94805517..dd9ec668e16079fbeed32e2ec36067610e9b58d2 100644 (file)
@@ -375,8 +375,14 @@ static int label_to_uclass(const char *label, int *seqp)
        log_debug("find %s: seq=%d, id=%d/%s\n", label, seq, id,
                  uclass_get_name(id));
        if (id == UCLASS_INVALID) {
-               log_warning("Unknown uclass '%s' in label\n", label);
-               return -EINVAL;
+               /* try some special cases */
+               if (IS_ENABLED(CONFIG_BOOTDEV_SPI_FLASH) &&
+                   !strncmp("spi", label, len)) {
+                       id = UCLASS_SPI_FLASH;
+               } else {
+                       log_warning("Unknown uclass '%s' in label\n", label);
+                       return -EINVAL;
+               }
        }
        if (id == UCLASS_USB)
                id = UCLASS_MASS_STORAGE;
index 52cc2f9d548eb310c5360be95f3b58ad1ce821e5..d2dbc9d4450852433a3da044c61bd222764f2133 100644 (file)
@@ -440,6 +440,18 @@ int bootflow_iter_check_blk(const struct bootflow_iter *iter)
        return -ENOTSUPP;
 }
 
+int bootflow_iter_check_sf(const struct bootflow_iter *iter)
+{
+       const struct udevice *media = dev_get_parent(iter->dev);
+       enum uclass_id id = device_get_uclass_id(media);
+
+       log_debug("uclass %d: %s\n", id, uclass_get_name(id));
+       if (id == UCLASS_SPI_FLASH)
+               return 0;
+
+       return -ENOTSUPP;
+}
+
 int bootflow_iter_check_net(const struct bootflow_iter *iter)
 {
        const struct udevice *media = dev_get_parent(iter->dev);
index 4a2f94f65bc7560e0c7b75a943636c83bb643300..34c342b6f5879550c987e51e6ba0127678c8a10b 100644 (file)
@@ -211,6 +211,7 @@ CONFIG_MMC_SANDBOX=y
 CONFIG_MMC_SDHCI=y
 CONFIG_MTD=y
 CONFIG_SPI_FLASH_SANDBOX=y
+CONFIG_BOOTDEV_SPI_FLASH=y
 CONFIG_SPI_FLASH_ATMEL=y
 CONFIG_SPI_FLASH_EON=y
 CONFIG_SPI_FLASH_GIGADEVICE=y
index cc1b3ac347bd37cd6f5168df4816d452fc57b908..477763dc9f136979c8ed9b373c876a156e00c470 100644 (file)
@@ -138,6 +138,7 @@ CONFIG_PWRSEQ=y
 CONFIG_I2C_EEPROM=y
 CONFIG_MMC_SANDBOX=y
 CONFIG_SPI_FLASH_SANDBOX=y
+CONFIG_BOOTDEV_SPI_FLASH=y
 CONFIG_SPI_FLASH_ATMEL=y
 CONFIG_SPI_FLASH_EON=y
 CONFIG_SPI_FLASH_GIGADEVICE=y
index 7b858a3a9190da51eacf291c9623d1dc96afe294..a9617c6c58c1d8b85ef40bf677780f1d392a285d 100644 (file)
@@ -80,6 +80,14 @@ config SF_DEFAULT_SPEED
 
 if SPI_FLASH
 
+config BOOTDEV_SPI_FLASH
+       bool "SPI Flash bootdev support"
+       help
+         Enable a boot device for SPI flash. This allows reading a script
+         from SPI flash so that it can be used to boot an Operating System.
+
+         If unsure, say N
+
 config SPI_FLASH_SFDP_SUPPORT
        bool "SFDP table parsing support for SPI NOR flashes"
        depends on !SPI_FLASH_BAR
index 99cc4185522331af8d6597f8670c4c2892615790..409395382f5a829f80b286eafa3cb72bda2e1b66 100644 (file)
@@ -21,3 +21,4 @@ obj-$(CONFIG_SPI_FLASH) += spi-nor.o
 obj-$(CONFIG_SPI_FLASH_DATAFLASH) += sf_dataflash.o
 obj-$(CONFIG_$(SPL_TPL_)SPI_FLASH_MTD) += sf_mtd.o
 obj-$(CONFIG_SPI_FLASH_SANDBOX) += sandbox.o
+obj-$(CONFIG_$(SPL_TPL_)BOOTDEV_SPI_FLASH) += sf_bootdev.o
index e6e650ef8c09d6d5d63f67ba257a50fc7645d91a..df1f75390c4d39ccb5e31994b378d7cdeb121009 100644 (file)
@@ -6,6 +6,7 @@
 #define LOG_CATEGORY UCLASS_SPI_FLASH
 
 #include <common.h>
+#include <bootdev.h>
 #include <dm.h>
 #include <log.h>
 #include <malloc.h>
@@ -13,6 +14,7 @@
 #include <spi_flash.h>
 #include <asm/global_data.h>
 #include <dm/device-internal.h>
+#include <test/test.h>
 #include "sf_internal.h"
 
 DECLARE_GLOBAL_DATA_PTR;
@@ -86,6 +88,14 @@ int spi_flash_probe_bus_cs(unsigned int busnum, unsigned int cs,
 
 static int spi_flash_post_bind(struct udevice *dev)
 {
+       int ret;
+
+       if (CONFIG_IS_ENABLED(BOOTDEV_SPI_FLASH) && test_sf_bootdev_enabled()) {
+               ret = bootdev_setup_for_dev(dev, "sf_bootdev");
+               if (ret)
+                       return log_msg_ret("bd", ret);
+       }
+
 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
        struct dm_spi_flash_ops *ops = sf_get_ops(dev);
        static int reloc_done;
@@ -101,6 +111,7 @@ static int spi_flash_post_bind(struct udevice *dev)
                reloc_done++;
        }
 #endif
+
        return 0;
 }
 
diff --git a/drivers/mtd/spi/sf_bootdev.c b/drivers/mtd/spi/sf_bootdev.c
new file mode 100644 (file)
index 0000000..2272e85
--- /dev/null
@@ -0,0 +1,82 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Read a bootflow from SPI flash
+ *
+ * Copyright 2022 Google LLC
+ */
+
+#include <common.h>
+#include <bootdev.h>
+#include <bootflow.h>
+#include <bootmeth.h>
+#include <dm.h>
+#include <env.h>
+#include <malloc.h>
+#include <spi_flash.h>
+
+static int sf_get_bootflow(struct udevice *dev, struct bootflow_iter *iter,
+                          struct bootflow *bflow)
+{
+       struct udevice *sf = dev_get_parent(dev);
+       uint size;
+       char *buf;
+       int ret;
+
+       /* We only support the whole device, not partitions */
+       if (iter->part)
+               return log_msg_ret("max", -ESHUTDOWN);
+
+       size = env_get_hex("script_size_f", 0);
+       if (!size)
+               return log_msg_ret("sz", -EINVAL);
+
+       buf = malloc(size + 1);
+       if (!buf)
+               return log_msg_ret("buf", -ENOMEM);
+
+       ret = spi_flash_read_dm(sf, env_get_hex("script_offset_f", 0),
+                               size, buf);
+       if (ret)
+               return log_msg_ret("cmd", -EINVAL);
+       bflow->state = BOOTFLOWST_MEDIA;
+
+       ret = bootmeth_set_bootflow(bflow->method, bflow, buf, size);
+       if (ret) {
+               free(buf);
+               return log_msg_ret("method", ret);
+       }
+
+       return 0;
+}
+
+static int sf_bootdev_bind(struct udevice *dev)
+{
+       struct bootdev_uc_plat *ucp = dev_get_uclass_plat(dev);
+
+       ucp->prio = BOOTDEVP_2_SCAN_FAST;
+
+       return 0;
+}
+
+struct bootdev_ops sf_bootdev_ops = {
+       .get_bootflow   = sf_get_bootflow,
+};
+
+static const struct udevice_id sf_bootdev_ids[] = {
+       { .compatible = "u-boot,bootdev-sf" },
+       { }
+};
+
+U_BOOT_DRIVER(sf_bootdev) = {
+       .name           = "sf_bootdev",
+       .id             = UCLASS_BOOTDEV,
+       .ops            = &sf_bootdev_ops,
+       .bind           = sf_bootdev_bind,
+       .of_match       = sf_bootdev_ids,
+};
+
+BOOTDEV_HUNTER(sf_bootdev_hunter) = {
+       .prio           = BOOTDEVP_2_SCAN_FAST,
+       .uclass         = UCLASS_SPI_FLASH,
+       .drv            = DM_DRIVER_REF(sf_bootdev),
+};
index 735ed87a001bbda1c825aa148c2d5490a80ce59d..319dda8e0be2b10d681813f3afd726de4aab9e57 100644 (file)
@@ -331,6 +331,15 @@ void bootflow_remove(struct bootflow *bflow);
  */
 int bootflow_iter_check_blk(const struct bootflow_iter *iter);
 
+/**
+ * bootflow_iter_check_sf() - Check that a bootflow uses SPI FLASH
+ *
+ * This checks the bootdev in the bootflow to make sure it uses SPI flash
+ *
+ * Return: 0 if OK, -ENOTSUPP if some other device is used (e.g. ethernet)
+ */
+int bootflow_iter_check_sf(const struct bootflow_iter *iter);
+
 /**
  * bootflow_iter_check_net() - Check that a bootflow uses a network device
  *
index 2ad31a0ef66aae9ecf1fcec9852a10559a835a50..ea0703fa5c84e870e15398b91fc1f2e8c8c28601 100644 (file)
@@ -243,9 +243,10 @@ static int bootdev_test_hunter(struct unit_test_state *uts)
        ut_assert_nextline("  10        mmc              mmc_bootdev");
        ut_assert_nextline("  30        nvme             nvme_bootdev");
        ut_assert_nextline("  30        scsi             scsi_bootdev");
+       ut_assert_nextline("  30        spi_flash        sf_bootdev");
        ut_assert_nextline("  40        usb              usb_bootdev");
        ut_assert_nextline("  30        virtio           virtio_bootdev");
-       ut_assert_nextline("(total hunters: 7)");
+       ut_assert_nextline("(total hunters: 8)");
        ut_assert_console_end();
 
        ut_assertok(bootdev_hunt("usb1", false));
@@ -253,8 +254,8 @@ static int bootdev_test_hunter(struct unit_test_state *uts)
                "Bus usb@1: scanning bus usb@1 for devices... 5 USB Device(s) found");
        ut_assert_console_end();
 
-       /* USB is fifth in the list, so bit 5 */
-       ut_asserteq(BIT(5), std->hunters_used);
+       /* USB is fifth in the list, so bit 6 */
+       ut_asserteq(BIT(6), std->hunters_used);
 
        return 0;
 }
@@ -274,7 +275,7 @@ static int bootdev_test_cmd_hunt(struct unit_test_state *uts)
        ut_assertok(run_command("bootdev hunt -l", 0));
        ut_assert_nextline("Prio  Used  Uclass           Hunter");
        ut_assert_nextlinen("----");
-       ut_assert_skip_to_line("(total hunters: 7)");
+       ut_assert_skip_to_line("(total hunters: 8)");
        ut_assert_console_end();
 
        /* Scan all hunters */
@@ -288,10 +289,11 @@ static int bootdev_test_cmd_hunt(struct unit_test_state *uts)
        ut_assert_nextline("Hunting with: nvme");
        ut_assert_nextline("Hunting with: scsi");
        ut_assert_nextline("scanning bus for devices...");
-       ut_assert_skip_to_line("Hunting with: usb");
+       ut_assert_skip_to_line("Hunting with: spi_flash");
+       ut_assert_nextline("Hunting with: usb");
        ut_assert_nextline(
                "Bus usb@1: scanning bus usb@1 for devices... 5 USB Device(s) found");
-       ut_assert_skip_to_line("Hunting with: virtio");
+       ut_assert_nextline("Hunting with: virtio");
        ut_assert_console_end();
 
        /* List available hunters */
@@ -303,12 +305,13 @@ static int bootdev_test_cmd_hunt(struct unit_test_state *uts)
        ut_assert_nextline("  10     *  mmc              mmc_bootdev");
        ut_assert_nextline("  30     *  nvme             nvme_bootdev");
        ut_assert_nextline("  30     *  scsi             scsi_bootdev");
+       ut_assert_nextline("  30     *  spi_flash        sf_bootdev");
        ut_assert_nextline("  40     *  usb              usb_bootdev");
        ut_assert_nextline("  30     *  virtio           virtio_bootdev");
-       ut_assert_nextline("(total hunters: 7)");
+       ut_assert_nextline("(total hunters: 8)");
        ut_assert_console_end();
 
-       ut_asserteq(GENMASK(6, 0), std->hunters_used);
+       ut_asserteq(GENMASK(7, 0), std->hunters_used);
 
        return 0;
 }