spi: intel: Add protected and locked attributes

The manufacturing access to the PCH/SoC SPI device is traditionally
performed via userspace driver accessing registers via /dev/mem but due
to security concerns /dev/mem access is being much restricted, hence the
reason for utilizing dedicated Intel PCH/SoC SPI controller driver,
which is already implemented in the Linux kernel.

Intel PCH/SoC SPI controller protects the flash storage via two
mechanisms one is the via region protection registers and second via
BIOS lock. The BIOS locks only the BIOS regions usually 0 and/or 6.

The device always boots with BIOS lock set, but during manufacturing the
BIOS lock has to be lifted in order to enable the write access. This can
be done by passing "writeable=1" in the command line when the driver is
loaded. This "locked" state is exposed through new sysfs attributes
(intel_spi_locked, intel_spi_bios_locked).

Second, also the region protection status is exposed via sysfs attribute
(intel_spi_protected) as the manufacturing will need the both files in
order to validate that the device is properly sealed.

Signed-off-by: Tamar Mashiah <tamar.mashiah@intel.com>
Signed-off-by: Tomas Winkler <tomas.winkler@intel.com>
Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
This commit is contained in:
Tamar Mashiah 2021-11-01 19:13:11 +02:00 committed by sys_oak on gitlab runner
parent 167a70e208
commit 58a6b58a47
5 changed files with 83 additions and 5 deletions

View File

@ -0,0 +1,20 @@
What: /sys/devices/.../intel_spi_protected
Date: Nov 2021
KernelVersion: 5.15
Contact: Tomas Winkler <tomas.winkler@intel.com>
Description: This attribute allows the userspace to check if the
Intel SPI flash controller is write protected from the host.
What: /sys/devices/.../intel_spi_locked
Date: Nov 2021
KernelVersion: 5.15
Contact: Tomas Winkler <tomas.winkler@intel.com>
Description: This attribute allows the user space to check if the
Intel SPI flash controller locks supported opcodes.
What: /sys/devices/.../intel_spi_bios_locked
Date: Nov 2021
KernelVersion: 5.18
Contact: Tomas Winkler <tomas.winkler@intel.com>
Description: This attribute allows the user space to check if the
Intel SPI flash controller BIOS region is locked for writes.

View File

@ -88,6 +88,7 @@ static struct pci_driver intel_spi_pci_driver = {
.name = "intel-spi",
.id_table = intel_spi_pci_ids,
.probe = intel_spi_pci_probe,
.dev_groups = intel_spi_groups,
};
module_pci_driver(intel_spi_pci_driver);

View File

@ -28,6 +28,7 @@ static struct platform_driver intel_spi_platform_driver = {
.probe = intel_spi_platform_probe,
.driver = {
.name = "intel-spi",
.dev_groups = intel_spi_groups,
},
};

View File

@ -147,6 +147,8 @@
* @pr_num: Maximum number of protected range registers
* @chip0_size: Size of the first flash chip in bytes
* @locked: Is SPI setting locked
* @protected: Whether the regions are write protected
* @bios_locked: Is BIOS region locked
* @swseq_reg: Use SW sequencer in register reads/writes
* @swseq_erase: Use SW sequencer in erase operation
* @atomic_preopcode: Holds preopcode when atomic sequence is requested
@ -165,6 +167,8 @@ struct intel_spi {
size_t pr_num;
size_t chip0_size;
bool locked;
bool protected;
bool bios_locked;
bool swseq_reg;
bool swseq_erase;
u8 atomic_preopcode;
@ -1107,10 +1111,13 @@ static int intel_spi_init(struct intel_spi *ispi)
return -EINVAL;
}
/* Try to disable write protection if user asked to do so */
if (writeable && !intel_spi_set_writeable(ispi)) {
dev_warn(ispi->dev, "can't disable chip write protection\n");
writeable = false;
ispi->bios_locked = true;
/* Try to disable BIOS write protection if user asked to do so */
if (writeable) {
if (intel_spi_set_writeable(ispi))
ispi->bios_locked = false;
else
dev_warn(ispi->dev, "can't disable chip write protection\n");
}
/* Disable #SMI generation from HW sequencer */
@ -1245,8 +1252,10 @@ static void intel_spi_fill_partition(struct intel_spi *ispi,
* Also if the user did not ask the chip to be writeable
* mask the bit too.
*/
if (!writeable || intel_spi_is_protected(ispi, base, limit))
if (!writeable || intel_spi_is_protected(ispi, base, limit)) {
part->mask_flags |= MTD_WRITEABLE;
ispi->protected = true;
}
end = (limit << 12) + 4096;
if (end > part->size)
@ -1382,6 +1391,50 @@ static int intel_spi_populate_chip(struct intel_spi *ispi)
return 0;
}
static ssize_t intel_spi_protected_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct intel_spi *ispi = dev_get_drvdata(dev);
return sysfs_emit(buf, "%d\n", ispi->protected);
}
static DEVICE_ATTR_ADMIN_RO(intel_spi_protected);
static ssize_t intel_spi_locked_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct intel_spi *ispi = dev_get_drvdata(dev);
return sysfs_emit(buf, "%d\n", ispi->locked);
}
static DEVICE_ATTR_ADMIN_RO(intel_spi_locked);
static ssize_t intel_spi_bios_locked_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct intel_spi *ispi = dev_get_drvdata(dev);
return sysfs_emit(buf, "%d\n", ispi->bios_locked);
}
static DEVICE_ATTR_ADMIN_RO(intel_spi_bios_locked);
static struct attribute *intel_spi_attrs[] = {
&dev_attr_intel_spi_protected.attr,
&dev_attr_intel_spi_locked.attr,
&dev_attr_intel_spi_bios_locked.attr,
NULL
};
static const struct attribute_group intel_spi_attr_group = {
.attrs = intel_spi_attrs,
};
const struct attribute_group *intel_spi_groups[] = {
&intel_spi_attr_group,
NULL
};
EXPORT_SYMBOL_GPL(intel_spi_groups);
/**
* intel_spi_probe() - Probe the Intel SPI flash controller
* @dev: Pointer to the parent device
@ -1422,6 +1475,7 @@ int intel_spi_probe(struct device *dev, struct resource *mem,
if (ret)
return ret;
dev_set_drvdata(dev, ispi);
return intel_spi_populate_chip(ispi);
}
EXPORT_SYMBOL_GPL(intel_spi_probe);

View File

@ -13,6 +13,8 @@
struct resource;
extern const struct attribute_group *intel_spi_groups[];
int intel_spi_probe(struct device *dev, struct resource *mem,
const struct intel_spi_boardinfo *info);