Sebastian Reichel | 40e7751 | 2020-09-02 19:31:38 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * (C) Copyright 2019 Collabora |
| 4 | * (C) Copyright 2019 GE |
| 5 | */ |
| 6 | |
Sebastian Reichel | 40e7751 | 2020-09-02 19:31:38 +0200 | [diff] [blame] | 7 | #include <bootcount.h> |
| 8 | #include <dm.h> |
| 9 | #include <spi_flash.h> |
| 10 | |
| 11 | static const u8 bootcount_magic = 0xbc; |
| 12 | |
| 13 | struct bootcount_spi_flash_priv { |
| 14 | struct udevice *spi_flash; |
| 15 | u32 offset; |
| 16 | }; |
| 17 | |
| 18 | static int bootcount_spi_flash_update(struct udevice *dev, u32 offset, u32 len, const void *buf) |
| 19 | { |
| 20 | struct spi_flash *flash = dev_get_uclass_priv(dev); |
| 21 | u32 sector_size = flash->sector_size; |
| 22 | u32 sector_offset = offset % sector_size; |
| 23 | u32 sector = offset - sector_offset; |
| 24 | int err = 0; |
| 25 | |
| 26 | /* code only supports updating a single sector */ |
| 27 | if (sector_offset + len > sector_size) |
| 28 | return -ENOSYS; |
| 29 | |
| 30 | u8 *buffer = malloc(sector_size); |
| 31 | if (!buffer) |
| 32 | return -ENOMEM; |
| 33 | |
| 34 | err = spi_flash_read_dm(dev, sector, sector_size, buffer); |
| 35 | if (err < 0) |
| 36 | goto out; |
| 37 | |
| 38 | memcpy(buffer + sector_offset, buf, len); |
| 39 | |
| 40 | err = spi_flash_erase_dm(dev, sector, sector_size); |
| 41 | if (err < 0) |
| 42 | goto out; |
| 43 | |
| 44 | err = spi_flash_write_dm(dev, sector, sector_size, buffer); |
| 45 | if (err < 0) |
| 46 | goto out; |
| 47 | |
| 48 | out: |
| 49 | free(buffer); |
| 50 | return err; |
| 51 | } |
| 52 | |
| 53 | static int bootcount_spi_flash_set(struct udevice *dev, const u32 a) |
| 54 | { |
| 55 | struct bootcount_spi_flash_priv *priv = dev_get_priv(dev); |
| 56 | const u16 val = bootcount_magic << 8 | (a & 0xff); |
| 57 | |
| 58 | if (bootcount_spi_flash_update(priv->spi_flash, priv->offset, 2, &val) < 0) { |
| 59 | debug("%s: write failed\n", __func__); |
| 60 | return -EIO; |
| 61 | } |
| 62 | |
| 63 | return 0; |
| 64 | } |
| 65 | |
| 66 | static int bootcount_spi_flash_get(struct udevice *dev, u32 *a) |
| 67 | { |
| 68 | struct bootcount_spi_flash_priv *priv = dev_get_priv(dev); |
| 69 | u16 val; |
| 70 | |
| 71 | if (spi_flash_read_dm(priv->spi_flash, priv->offset, 2, &val) < 0) { |
| 72 | debug("%s: read failed\n", __func__); |
| 73 | return -EIO; |
| 74 | } |
| 75 | |
| 76 | if (val >> 8 == bootcount_magic) { |
| 77 | *a = val & 0xff; |
| 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | debug("%s: bootcount magic does not match on %04x\n", __func__, val); |
| 82 | return -EIO; |
| 83 | } |
| 84 | |
| 85 | static int bootcount_spi_flash_probe(struct udevice *dev) |
| 86 | { |
| 87 | struct ofnode_phandle_args phandle_args; |
| 88 | struct bootcount_spi_flash_priv *priv = dev_get_priv(dev); |
| 89 | struct udevice *spi_flash; |
| 90 | |
| 91 | if (dev_read_phandle_with_args(dev, "spi-flash", NULL, 0, 0, &phandle_args)) { |
| 92 | debug("%s: spi-flash backing device not specified\n", dev->name); |
| 93 | return -ENOENT; |
| 94 | } |
| 95 | |
| 96 | if (uclass_get_device_by_ofnode(UCLASS_SPI_FLASH, phandle_args.node, &spi_flash)) { |
| 97 | debug("%s: could not get backing device\n", dev->name); |
| 98 | return -ENODEV; |
| 99 | } |
| 100 | |
| 101 | priv->spi_flash = spi_flash; |
| 102 | priv->offset = dev_read_u32_default(dev, "offset", 0); |
| 103 | |
| 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | static const struct bootcount_ops bootcount_spi_flash_ops = { |
| 108 | .get = bootcount_spi_flash_get, |
| 109 | .set = bootcount_spi_flash_set, |
| 110 | }; |
| 111 | |
| 112 | static const struct udevice_id bootcount_spi_flash_ids[] = { |
| 113 | { .compatible = "u-boot,bootcount-spi-flash" }, |
| 114 | { } |
| 115 | }; |
| 116 | |
| 117 | U_BOOT_DRIVER(bootcount_spi_flash) = { |
| 118 | .name = "bootcount-spi-flash", |
| 119 | .id = UCLASS_BOOTCOUNT, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 120 | .priv_auto = sizeof(struct bootcount_spi_flash_priv), |
Sebastian Reichel | 40e7751 | 2020-09-02 19:31:38 +0200 | [diff] [blame] | 121 | .probe = bootcount_spi_flash_probe, |
| 122 | .of_match = bootcount_spi_flash_ids, |
| 123 | .ops = &bootcount_spi_flash_ops, |
| 124 | }; |