Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

mtd: parsers: add Broadcom's U-Boot parser

Broadcom stores environment variables blocks inside U-Boot partition
itself. This driver finds & registers them.

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
Link: https://lore.kernel.org/linux-mtd/20220711153041.6036-2-zajec5@gmail.com

authored by

Rafał Miłecki and committed by
Miquel Raynal
002181f5 27bfb201

+95
+10
drivers/mtd/parsers/Kconfig
··· 20 20 This provides partition parsing for BCM63xx devices with CFE 21 21 bootloaders. 22 22 23 + config MTD_BRCM_U_BOOT 24 + tristate "Broadcom's U-Boot partition parser" 25 + depends on ARCH_BCM4908 || COMPILE_TEST 26 + help 27 + Broadcom uses a custom way of storing U-Boot environment variables. 28 + They are placed inside U-Boot partition itself at unspecified offset. 29 + It's possible to locate them by looking for a custom header with a 30 + magic value. This driver does that and creates subpartitions for 31 + each found environment variables block. 32 + 23 33 config MTD_CMDLINE_PARTS 24 34 tristate "Command line partition table parsing" 25 35 depends on MTD
+1
drivers/mtd/parsers/Makefile
··· 2 2 obj-$(CONFIG_MTD_AR7_PARTS) += ar7part.o 3 3 obj-$(CONFIG_MTD_BCM47XX_PARTS) += bcm47xxpart.o 4 4 obj-$(CONFIG_MTD_BCM63XX_PARTS) += bcm63xxpart.o 5 + obj-$(CONFIG_MTD_BRCM_U_BOOT) += brcm_u-boot.o 5 6 obj-$(CONFIG_MTD_CMDLINE_PARTS) += cmdlinepart.o 6 7 obj-$(CONFIG_MTD_OF_PARTS) += ofpart.o 7 8 ofpart-y += ofpart_core.o
+84
drivers/mtd/parsers/brcm_u-boot.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-only 2 + /* 3 + * Copyright © 2022 Rafał Miłecki <rafal@milecki.pl> 4 + */ 5 + 6 + #include <linux/module.h> 7 + #include <linux/kernel.h> 8 + #include <linux/slab.h> 9 + #include <linux/mtd/mtd.h> 10 + #include <linux/mtd/partitions.h> 11 + 12 + #define BRCM_U_BOOT_MAX_OFFSET 0x200000 13 + #define BRCM_U_BOOT_STEP 0x1000 14 + 15 + #define BRCM_U_BOOT_MAX_PARTS 2 16 + 17 + #define BRCM_U_BOOT_MAGIC 0x75456e76 /* uEnv */ 18 + 19 + struct brcm_u_boot_header { 20 + __le32 magic; 21 + __le32 length; 22 + } __packed; 23 + 24 + static const char *names[BRCM_U_BOOT_MAX_PARTS] = { 25 + "u-boot-env", 26 + "u-boot-env-backup", 27 + }; 28 + 29 + static int brcm_u_boot_parse(struct mtd_info *mtd, 30 + const struct mtd_partition **pparts, 31 + struct mtd_part_parser_data *data) 32 + { 33 + struct brcm_u_boot_header header; 34 + struct mtd_partition *parts; 35 + size_t bytes_read; 36 + size_t offset; 37 + int err; 38 + int i = 0; 39 + 40 + parts = kcalloc(BRCM_U_BOOT_MAX_PARTS, sizeof(*parts), GFP_KERNEL); 41 + if (!parts) 42 + return -ENOMEM; 43 + 44 + for (offset = 0; 45 + offset < min_t(size_t, mtd->size, BRCM_U_BOOT_MAX_OFFSET); 46 + offset += BRCM_U_BOOT_STEP) { 47 + err = mtd_read(mtd, offset, sizeof(header), &bytes_read, (uint8_t *)&header); 48 + if (err && !mtd_is_bitflip(err)) { 49 + pr_err("Failed to read from %s at 0x%zx: %d\n", mtd->name, offset, err); 50 + continue; 51 + } 52 + 53 + if (le32_to_cpu(header.magic) != BRCM_U_BOOT_MAGIC) 54 + continue; 55 + 56 + parts[i].name = names[i]; 57 + parts[i].offset = offset; 58 + parts[i].size = sizeof(header) + le32_to_cpu(header.length); 59 + i++; 60 + pr_info("offset:0x%zx magic:0x%08x BINGO\n", offset, header.magic); 61 + 62 + if (i == BRCM_U_BOOT_MAX_PARTS) 63 + break; 64 + } 65 + 66 + *pparts = parts; 67 + 68 + return i; 69 + }; 70 + 71 + static const struct of_device_id brcm_u_boot_of_match_table[] = { 72 + { .compatible = "brcm,u-boot" }, 73 + {}, 74 + }; 75 + MODULE_DEVICE_TABLE(of, brcm_u_boot_of_match_table); 76 + 77 + static struct mtd_part_parser brcm_u_boot_mtd_parser = { 78 + .parse_fn = brcm_u_boot_parse, 79 + .name = "brcm_u-boot", 80 + .of_match_table = brcm_u_boot_of_match_table, 81 + }; 82 + module_mtd_part_parser(brcm_u_boot_mtd_parser); 83 + 84 + MODULE_LICENSE("GPL");