Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions block/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,17 @@ config BLK_INLINE_ENCRYPTION_FALLBACK
by falling back to the kernel crypto API when inline
encryption hardware is not present.

config BLK_NVMEM
bool "Block device NVMEM provider"
depends on OF
depends on NVMEM
help
Allow block devices (or partitions) to act as NVMEM providers,
exposing factory-provisioned data such as MAC addresses or Wi-Fi
calibration blobs to the drivers that consume them. This is
typically used on embedded devices where such data is stored in a
dedicated area of an eMMC, instead of a separate EEPROM or OTP.

source "block/partitions/Kconfig"

config BLK_PM
Expand Down
1 change: 1 addition & 0 deletions block/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,4 @@ obj-$(CONFIG_BLK_INLINE_ENCRYPTION) += blk-crypto.o blk-crypto-profile.o \
blk-crypto-sysfs.o
obj-$(CONFIG_BLK_INLINE_ENCRYPTION_FALLBACK) += blk-crypto-fallback.o
obj-$(CONFIG_BLOCK_HOLDER_DEPRECATED) += holder.o
obj-$(CONFIG_BLK_NVMEM) += blk-nvmem.o
112 changes: 112 additions & 0 deletions block/blk-nvmem.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* block device NVMEM provider
*
* Copyright (c) 2024 Daniel Golle <daniel@makrotopia.org>
* Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
*
* Useful on devices using a whole disk or a partition (e.g. an eMMC boot
* partition) to store MAC addresses, Bluetooth addresses or Wi-Fi
* calibration EEPROM data.
*
* The NVMEM device is a side channel onto a block device that stays fully
* usable. This is somewhat mitigated by opening the device exclusively.
*/

#include <linux/device.h>
#include <linux/file.h>
#include <linux/fs.h>
#include <linux/nvmem-provider.h>
#include <linux/nvmem-consumer.h>
#include <linux/of.h>
#include <linux/property.h>

#include "blk.h"

static int blk_nvmem_reg_read(void *priv, unsigned int from,
void *val, size_t bytes)
{
struct block_device *bdev = priv;
struct file *bdev_file;
loff_t pos = from;
ssize_t ret;

/* open and prevent other exclusive openers */
bdev_file = bdev_file_open_by_dev(bdev->bd_dev,
BLK_OPEN_READ | BLK_OPEN_EXCL,
blk_nvmem_reg_read, NULL);
if (IS_ERR(bdev_file))
return PTR_ERR(bdev_file);

if (file_bdev(bdev_file) != bdev) {
fput(bdev_file);
return -ENODEV;
}

ret = kernel_read(bdev_file, val, bytes, &pos);
fput(bdev_file);

if (ret < 0)
return ret;
if (ret != bytes)
return -EIO;
return 0;
}

int blk_nvmem_add(struct block_device *bdev)
{
struct device *dev = &bdev->bd_device;
struct device_node *np = dev_of_node(dev);
struct nvmem_config config = {
.id = NVMEM_DEVID_NONE,
.owner = THIS_MODULE,
.word_size = 1,
.stride = 1,
.read_only = true,
.root_only = true,
.ignore_wp = true,
};
struct nvmem_device *nvmem;
struct device_node *child;

/* skip devices which do not have a device tree node */
if (!np)
return 0;

/*
* The layout is described either by an "nvmem-layout" child node or
* by the device node itself being a "fixed-layout" container.
*/
child = of_get_child_by_name(np, "nvmem-layout");
if (child)
of_node_put(child);
else if (!of_device_is_compatible(np, "fixed-layout"))
return 0;

if (bdev_nr_bytes(bdev) > INT_MAX) {
dev_warn(dev, "block device too large to be an NVMEM\n");
return 0;
}

config.dev = dev;
config.name = dev_name(dev);
config.priv = bdev;
config.reg_read = blk_nvmem_reg_read;
config.size = bdev_nr_bytes(bdev);
config.of_node = np;

nvmem = nvmem_register(&config);
if (IS_ERR(nvmem))
return dev_err_probe(dev, PTR_ERR(nvmem),
"Failed to register NVMEM device\n");

bdev->bd_nvmem = nvmem;

return 0;
}

void blk_nvmem_del(struct block_device *bdev)
{
nvmem_unregister(bdev->bd_nvmem);
bdev->bd_nvmem = NULL;
}
8 changes: 8 additions & 0 deletions block/blk.h
Original file line number Diff line number Diff line change
Expand Up @@ -839,4 +839,12 @@ static inline void blk_debugfs_unlock(struct request_queue *q,
memalloc_noio_restore(memflags);
}

#ifdef CONFIG_BLK_NVMEM
int blk_nvmem_add(struct block_device *bdev);
void blk_nvmem_del(struct block_device *bdev);
#else
static inline int blk_nvmem_add(struct block_device *bdev) { return 0; }
static inline void blk_nvmem_del(struct block_device *bdev) {}
#endif

#endif /* BLK_INTERNAL_H */
4 changes: 4 additions & 0 deletions block/genhd.c
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,8 @@ static void add_disk_final(struct gendisk *disk)
*/
dev_set_uevent_suppress(ddev, 0);
disk_uevent(disk, KOBJ_ADD);

blk_nvmem_add(disk->part0);
}

blk_apply_bdi_limits(disk->bdi, &disk->queue->limits);
Expand Down Expand Up @@ -708,6 +710,8 @@ static void __del_gendisk(struct gendisk *disk)

disk_del_events(disk);

blk_nvmem_del(disk->part0);

/*
* Prevent new openers by unlinked the bdev inode.
*/
Expand Down
1 change: 1 addition & 0 deletions block/partitions/check.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ struct parsed_partitions {
int flags;
bool has_info;
struct partition_meta_info info;
struct fwnode_handle *fwnode;
} *parts;
int next;
int limit;
Expand Down
25 changes: 22 additions & 3 deletions block/partitions/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <linux/sysfs.h>
#include <linux/ctype.h>
#include <linux/vmalloc.h>
#include <linux/property.h>
#include <linux/raid/detect.h>
#include "check.h"

Expand Down Expand Up @@ -110,8 +111,17 @@ static struct parsed_partitions *allocate_partitions(struct gendisk *hd)
return state;
}

static void drop_partitions_fwnodes(struct parsed_partitions *state)
{
for (int i = 0; i < state->limit; i++) {
fwnode_handle_put(state->parts[i].fwnode);
state->parts[i].fwnode = NULL;
}
}

static void free_partitions(struct parsed_partitions *state)
{
drop_partitions_fwnodes(state);
vfree(state->parts);
kfree(state);
}
Expand Down Expand Up @@ -139,6 +149,7 @@ static struct parsed_partitions *check_partition(struct gendisk *hd)

i = res = err = 0;
while (!res && check_part[i]) {
drop_partitions_fwnodes(state);
memset(state->parts, 0, state->limit * sizeof(state->parts[0]));
res = check_part[i++](state);
if (res < 0) {
Expand Down Expand Up @@ -247,6 +258,7 @@ static const struct attribute_group *part_attr_groups[] = {

static void part_release(struct device *dev)
{
fwnode_handle_put(dev_fwnode(dev));
put_disk(dev_to_bdev(dev)->bd_disk);
bdev_drop(dev_to_bdev(dev));
}
Expand Down Expand Up @@ -277,6 +289,8 @@ void drop_partition(struct block_device *part)
xa_erase(&part->bd_disk->part_tbl, bdev_partno(part));
kobject_put(part->bd_holder_dir);

blk_nvmem_del(part);

device_del(&part->bd_device);
put_device(&part->bd_device);
}
Expand All @@ -294,7 +308,8 @@ static const DEVICE_ATTR(whole_disk, 0444, whole_disk_show, NULL);
*/
static struct block_device *add_partition(struct gendisk *disk, int partno,
sector_t start, sector_t len, int flags,
struct partition_meta_info *info)
struct partition_meta_info *info,
struct fwnode_handle *fwnode)
{
dev_t devt = MKDEV(0, 0);
struct device *ddev = disk_to_dev(disk);
Expand Down Expand Up @@ -343,6 +358,7 @@ static struct block_device *add_partition(struct gendisk *disk, int partno,
pdev->class = &block_class;
pdev->type = &part_type;
pdev->parent = ddev;
device_set_node(pdev, fwnode_handle_get(fwnode));

/* in consecutive minor range? */
if (bdev_partno(bdev) < disk->minors) {
Expand Down Expand Up @@ -392,6 +408,9 @@ static struct block_device *add_partition(struct gendisk *disk, int partno,
/* suppress uevent if the disk suppresses it */
if (!dev_get_uevent_suppress(ddev))
kobject_uevent(&pdev->kobj, KOBJ_ADD);

blk_nvmem_add(bdev);

return bdev;

out_del:
Expand Down Expand Up @@ -449,7 +468,7 @@ int bdev_add_partition(struct gendisk *disk, int partno, sector_t start,
}

part = add_partition(disk, partno, start, length,
ADDPART_FLAG_NONE, NULL);
ADDPART_FLAG_NONE, NULL, NULL);
ret = PTR_ERR_OR_ZERO(part);
out:
mutex_unlock(&disk->open_mutex);
Expand Down Expand Up @@ -564,7 +583,7 @@ static bool blk_add_partition(struct gendisk *disk,
}

part = add_partition(disk, p, from, size, state->parts[p].flags,
&state->parts[p].info);
&state->parts[p].info, state->parts[p].fwnode);
if (IS_ERR(part)) {
if (PTR_ERR(part) != -ENXIO) {
printk(KERN_ERR " %s: p%d could not be added: %pe\n",
Expand Down
3 changes: 3 additions & 0 deletions block/partitions/of.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <linux/blkdev.h>
#include <linux/major.h>
#include <linux/of.h>
#include <linux/property.h>
#include <linux/string.h>
#include "check.h"

Expand Down Expand Up @@ -62,6 +63,8 @@ static void add_of_partition(struct parsed_partitions *state, int slot,
partname = of_get_property(np, "name", &len);
strscpy(info->volname, partname, sizeof(info->volname));

state->parts[slot].fwnode = fwnode_handle_get(of_fwnode_handle(np));

seq_buf_printf(&state->pp_buf, "(%s)", info->volname);
}

Expand Down
4 changes: 4 additions & 0 deletions include/linux/blk_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
struct bio_set;
struct bio;
struct bio_integrity_payload;
struct nvmem_device;
struct page;
struct io_context;
struct cgroup_subsys_state;
Expand Down Expand Up @@ -73,6 +74,9 @@ struct block_device {
int bd_writers;
#ifdef CONFIG_SECURITY
void *bd_security;
#endif
#ifdef CONFIG_BLK_NVMEM
struct nvmem_device *bd_nvmem;
#endif
/*
* keep this out-of-line as it's both big and not needed in the fast
Expand Down
Loading