b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 1 | From 002181f5b150e60c77f21de7ad4dd10e4614cd91 Mon Sep 17 00:00:00 2001 |
| 2 | From: =?UTF-8?q?Rafa=C5=82=20Mi=C5=82ecki?= <rafal@milecki.pl> |
| 3 | Date: Mon, 11 Jul 2022 17:30:41 +0200 |
| 4 | Subject: [PATCH] mtd: parsers: add Broadcom's U-Boot parser |
| 5 | MIME-Version: 1.0 |
| 6 | Content-Type: text/plain; charset=UTF-8 |
| 7 | Content-Transfer-Encoding: 8bit |
| 8 | |
| 9 | Broadcom stores environment variables blocks inside U-Boot partition |
| 10 | itself. This driver finds & registers them. |
| 11 | |
| 12 | Signed-off-by: Rafał Miłecki <rafal@milecki.pl> |
| 13 | Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com> |
| 14 | Link: https://lore.kernel.org/linux-mtd/20220711153041.6036-2-zajec5@gmail.com |
| 15 | --- |
| 16 | drivers/mtd/parsers/Kconfig | 10 ++++ |
| 17 | drivers/mtd/parsers/Makefile | 1 + |
| 18 | drivers/mtd/parsers/brcm_u-boot.c | 84 +++++++++++++++++++++++++++++++ |
| 19 | 3 files changed, 95 insertions(+) |
| 20 | create mode 100644 drivers/mtd/parsers/brcm_u-boot.c |
| 21 | |
| 22 | --- a/drivers/mtd/parsers/Kconfig |
| 23 | +++ b/drivers/mtd/parsers/Kconfig |
| 24 | @@ -20,6 +20,16 @@ config MTD_BCM63XX_PARTS |
| 25 | This provides partition parsing for BCM63xx devices with CFE |
| 26 | bootloaders. |
| 27 | |
| 28 | +config MTD_BRCM_U_BOOT |
| 29 | + tristate "Broadcom's U-Boot partition parser" |
| 30 | + depends on ARCH_BCM4908 || COMPILE_TEST |
| 31 | + help |
| 32 | + Broadcom uses a custom way of storing U-Boot environment variables. |
| 33 | + They are placed inside U-Boot partition itself at unspecified offset. |
| 34 | + It's possible to locate them by looking for a custom header with a |
| 35 | + magic value. This driver does that and creates subpartitions for |
| 36 | + each found environment variables block. |
| 37 | + |
| 38 | config MTD_CMDLINE_PARTS |
| 39 | tristate "Command line partition table parsing" |
| 40 | depends on MTD |
| 41 | --- a/drivers/mtd/parsers/Makefile |
| 42 | +++ b/drivers/mtd/parsers/Makefile |
| 43 | @@ -2,6 +2,7 @@ |
| 44 | obj-$(CONFIG_MTD_AR7_PARTS) += ar7part.o |
| 45 | obj-$(CONFIG_MTD_BCM47XX_PARTS) += bcm47xxpart.o |
| 46 | obj-$(CONFIG_MTD_BCM63XX_PARTS) += bcm63xxpart.o |
| 47 | +obj-$(CONFIG_MTD_BRCM_U_BOOT) += brcm_u-boot.o |
| 48 | obj-$(CONFIG_MTD_CMDLINE_PARTS) += cmdlinepart.o |
| 49 | obj-$(CONFIG_MTD_MYLOADER_PARTS) += myloader.o |
| 50 | obj-$(CONFIG_MTD_OF_PARTS) += ofpart.o |
| 51 | --- /dev/null |
| 52 | +++ b/drivers/mtd/parsers/brcm_u-boot.c |
| 53 | @@ -0,0 +1,84 @@ |
| 54 | +// SPDX-License-Identifier: GPL-2.0-only |
| 55 | +/* |
| 56 | + * Copyright © 2022 Rafał Miłecki <rafal@milecki.pl> |
| 57 | + */ |
| 58 | + |
| 59 | +#include <linux/module.h> |
| 60 | +#include <linux/kernel.h> |
| 61 | +#include <linux/slab.h> |
| 62 | +#include <linux/mtd/mtd.h> |
| 63 | +#include <linux/mtd/partitions.h> |
| 64 | + |
| 65 | +#define BRCM_U_BOOT_MAX_OFFSET 0x200000 |
| 66 | +#define BRCM_U_BOOT_STEP 0x1000 |
| 67 | + |
| 68 | +#define BRCM_U_BOOT_MAX_PARTS 2 |
| 69 | + |
| 70 | +#define BRCM_U_BOOT_MAGIC 0x75456e76 /* uEnv */ |
| 71 | + |
| 72 | +struct brcm_u_boot_header { |
| 73 | + __le32 magic; |
| 74 | + __le32 length; |
| 75 | +} __packed; |
| 76 | + |
| 77 | +static const char *names[BRCM_U_BOOT_MAX_PARTS] = { |
| 78 | + "u-boot-env", |
| 79 | + "u-boot-env-backup", |
| 80 | +}; |
| 81 | + |
| 82 | +static int brcm_u_boot_parse(struct mtd_info *mtd, |
| 83 | + const struct mtd_partition **pparts, |
| 84 | + struct mtd_part_parser_data *data) |
| 85 | +{ |
| 86 | + struct brcm_u_boot_header header; |
| 87 | + struct mtd_partition *parts; |
| 88 | + size_t bytes_read; |
| 89 | + size_t offset; |
| 90 | + int err; |
| 91 | + int i = 0; |
| 92 | + |
| 93 | + parts = kcalloc(BRCM_U_BOOT_MAX_PARTS, sizeof(*parts), GFP_KERNEL); |
| 94 | + if (!parts) |
| 95 | + return -ENOMEM; |
| 96 | + |
| 97 | + for (offset = 0; |
| 98 | + offset < min_t(size_t, mtd->size, BRCM_U_BOOT_MAX_OFFSET); |
| 99 | + offset += BRCM_U_BOOT_STEP) { |
| 100 | + err = mtd_read(mtd, offset, sizeof(header), &bytes_read, (uint8_t *)&header); |
| 101 | + if (err && !mtd_is_bitflip(err)) { |
| 102 | + pr_err("Failed to read from %s at 0x%zx: %d\n", mtd->name, offset, err); |
| 103 | + continue; |
| 104 | + } |
| 105 | + |
| 106 | + if (le32_to_cpu(header.magic) != BRCM_U_BOOT_MAGIC) |
| 107 | + continue; |
| 108 | + |
| 109 | + parts[i].name = names[i]; |
| 110 | + parts[i].offset = offset; |
| 111 | + parts[i].size = sizeof(header) + le32_to_cpu(header.length); |
| 112 | + i++; |
| 113 | + pr_info("offset:0x%zx magic:0x%08x BINGO\n", offset, header.magic); |
| 114 | + |
| 115 | + if (i == BRCM_U_BOOT_MAX_PARTS) |
| 116 | + break; |
| 117 | + } |
| 118 | + |
| 119 | + *pparts = parts; |
| 120 | + |
| 121 | + return i; |
| 122 | +}; |
| 123 | + |
| 124 | +static const struct of_device_id brcm_u_boot_of_match_table[] = { |
| 125 | + { .compatible = "brcm,u-boot" }, |
| 126 | + {}, |
| 127 | +}; |
| 128 | +MODULE_DEVICE_TABLE(of, brcm_u_boot_of_match_table); |
| 129 | + |
| 130 | +static struct mtd_part_parser brcm_u_boot_mtd_parser = { |
| 131 | + .parse_fn = brcm_u_boot_parse, |
| 132 | + .name = "brcm_u-boot", |
| 133 | + .of_match_table = brcm_u_boot_of_match_table, |
| 134 | +}; |
| 135 | +module_mtd_part_parser(brcm_u_boot_mtd_parser); |
| 136 | + |
| 137 | +MODULE_LICENSE("GPL"); |