Skip to contentSkip to conversation

Support EFI partition on NVIDIA Tegra devices

PATCH Superseded Dmitry Osipenko 3 participants 8 messages Mar 27, 2021 – Mar 29, 2021

Subsystems (5)

v1 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

cover Support EFI partition on NVIDIA Tegra devices

Hi,

This series adds support for gpt_sector=<sector> kernel parameter used specifically by Android devices that are based on NVIDIA Tegra SoCs. This parameter is supplied by downstream bootloader, it tells the number of a sector where EFI entry is located on EMMC storage.

A year ago we tried to upstream the support of gpt_sector and then the support of NVIDIA's proprietary partition table, which was suggested by Christoph Hellwig, but that effort didn't get enough attention.

Now, a year later, we have more Tegra devices running mainline kernel and the situation hasn't changed, we still need to support the quirks needed for accessing internal EMMC storage of the Tegra devices. This time I'm trying to get back to variant with supporting the gpt_sector parameter, but in a way which doesn't disturb anything other than the EMMC of NVIDIA Tegra devices, which previously was suggested by Christoph Hellwig as well.

Dmitry Osipenko (3):
mmc: core: Add raw_boot_mult field to mmc_ext_csd
mmc: block: Add mmc_bdev_to_card() helper partitions/efi: Support gpt_sector parameter needed by NVIDIA Tegra
devices

 block/partitions/Kconfig   |   8 +++
 block/partitions/Makefile  |   1 +
 block/partitions/check.h   |   2 +
 block/partitions/core.c    |   3 ++
 block/partitions/efi.c     |  18 +++++++
 block/partitions/tegra.c   | 108 +++++++++++++++++++++++++++++++++++++
 drivers/mmc/core/block.c   |  15 ++++++
 drivers/mmc/core/mmc.c     |   2 +
 include/linux/mmc/blkdev.h |  13 +++++
 include/linux/mmc/card.h   |   1 +
 10 files changed, 171 insertions(+)
 create mode 100644 block/partitions/tegra.c
 create mode 100644 include/linux/mmc/blkdev.h
Signature · 1 line
2.30.2
#1 lore raw

PATCH 1/3 mmc: core: Add raw_boot_mult field to mmc_ext_csd

Bootloader of NVIDIA Tegra devices linearizes the boot0/boot1/main partitions into a single virtual space, and thus, all partition addresses are shifted by the size of boot0 + boot1 partitions. The offset needs to be known in order to find the EFI entry on internal EMMC storage of Tegra devices.

---
 drivers/mmc/core/mmc.c   | 2 ++
 include/linux/mmc/card.h | 1 +
 2 files changed, 3 insertions(+)
drivers/mmc/core/mmc.c +2 -0
index 8741271d3971..ed29f6076472 100644
@@ -417,6 +417,8 @@ static int mmc_decode_ext_csd(struct mmc_card *card, u8 *ext_csd)
 		ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT];
 	card->ext_csd.raw_hc_erase_grp_size =
 		ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
+	card->ext_csd.raw_boot_mult =
+		ext_csd[EXT_CSD_BOOT_MULT];
 	if (card->ext_csd.rev >= 3) {
 		u8 sa_shift = ext_csd[EXT_CSD_S_A_TIMEOUT];
 		card->ext_csd.part_config = ext_csd[EXT_CSD_PART_CONFIG];
include/linux/mmc/card.h +1 -0
index f9ad35dd6012..720e2a1ac1cd 100644
@@ -109,6 +109,7 @@ struct mmc_ext_csd {
 	u8			raw_hc_erase_gap_size;	/* 221 */
 	u8			raw_erase_timeout_mult;	/* 223 */
 	u8			raw_hc_erase_grp_size;	/* 224 */
+	u8			raw_boot_mult;		/* 226 */
 	u8			raw_sec_trim_mult;	/* 229 */
 	u8			raw_sec_erase_mult;	/* 230 */
 	u8			raw_sec_feature_support;/* 231 */
Signature · 1 line
2.30.2
#2 lore raw

PATCH 2/3 mmc: block: Add mmc_bdev_to_card() helper

In reply to Dmitry Osipenko

Add mmc_bdev_to_card() helper which is needed for finding EFI entry on EMMC of NVIDIA Tegra devices.

---
 drivers/mmc/core/block.c   | 15 +++++++++++++++
 include/linux/mmc/blkdev.h | 13 +++++++++++++
 2 files changed, 28 insertions(+)
 create mode 100644 include/linux/mmc/blkdev.h
drivers/mmc/core/block.c +15 -0
index fe5892d30778..666066fd6037 100644
@@ -40,6 +40,7 @@
 #include <linux/debugfs.h>
 
 #include <linux/mmc/ioctl.h>
+#include <linux/mmc/blkdev.h>
 #include <linux/mmc/card.h>
 #include <linux/mmc/host.h>
 #include <linux/mmc/mmc.h>
@@ -306,6 +307,20 @@ static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
 	return ret;
 }
 
+struct mmc_card *mmc_bdev_to_card(struct block_device *bdev)
+{
+	struct mmc_blk_data *md;
+
+	if (bdev->bd_disk->major != MMC_BLOCK_MAJOR)
+		return NULL;
+
+	md = mmc_blk_get(bdev->bd_disk);
+	if (!md)
+		return NULL;
+
+	return md->queue.card;
+}
+
 static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
 {
 	struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
include/linux/mmc/blkdev.h new file +13 -0
new file mode 100644
index 000000000000..67608c58de70
@@ -0,0 +1,13 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ *  linux/include/linux/mmc/blkdev.h
+ */
+#ifndef LINUX_MMC_BLOCK_DEVICE_H
+#define LINUX_MMC_BLOCK_DEVICE_H
+
+struct block_device;
+struct mmc_card;
+
+struct mmc_card *mmc_bdev_to_card(struct block_device *bdev);
+
+#endif /* LINUX_MMC_BLOCK_DEVICE_H */
Signature · 1 line
2.30.2
#3 lore raw

PATCH 3/3 partitions/efi: Support gpt_sector parameter needed by NVIDIA Tegra devices

In reply to Dmitry Osipenko

All NVIDIA Tegra20..124 Android devices use proprietary bootloader which supplies the gpt_sector=<sector> kernel cmdline parameter that should be used for looking up the EFI partition table on internal EMMC storage. If the kernel cmdline parameter isn't supplied, then the partition is expected to be placed around the last but one sector of EMMC.

Apparently this was done in order to hide the PT from a usual userspace tools since EFI entry exists only for compatibility with a Linux kernel, while a custom proprietary partition table is what is really used by these Android devices, thus these tools may corrupt the real PT, making device unbootable and very difficult to restore.

Add support for the gpt_sector cmdline parameter which will be used for finding EFI entry on internal EMMC storage of NVIDIA Tegra20+ devices.

---
 block/partitions/Kconfig  |   8 +++
 block/partitions/Makefile |   1 +
 block/partitions/check.h  |   2 +
 block/partitions/core.c   |   3 ++
 block/partitions/efi.c    |  18 +++++++
 block/partitions/tegra.c  | 108 ++++++++++++++++++++++++++++++++++++++
 6 files changed, 140 insertions(+)
 create mode 100644 block/partitions/tegra.c
block/partitions/Kconfig +8 -0
index 6e2a649669e5..be086916c6a6 100644
@@ -268,3 +268,11 @@ config CMDLINE_PARTITION
 	help
 	  Say Y here if you want to read the partition table from bootargs.
 	  The format for the command line is just like mtdparts.
+
+config TEGRA_PARTITION
+	bool "NVIDIA Tegra Partition support" if PARTITION_ADVANCED
+	default y if ARCH_TEGRA
+	depends on EFI_PARTITION && MMC_BLOCK && (ARCH_TEGRA || COMPILE_TEST)
+	help
+	  Say Y here if you would like to be able to read the hard disk
+	  partition table format used by NVIDIA Tegra machines.
block/partitions/Makefile +1 -0
index a7f05cdb02a8..83cb70c6d08d 100644
@@ -20,3 +20,4 @@ obj-$(CONFIG_IBM_PARTITION) += ibm.o
 obj-$(CONFIG_EFI_PARTITION) += efi.o
 obj-$(CONFIG_KARMA_PARTITION) += karma.o
 obj-$(CONFIG_SYSV68_PARTITION) += sysv68.o
+obj-$(CONFIG_TEGRA_PARTITION) += tegra.o
block/partitions/check.h +2 -0
index c577e9ee67f0..5fcc85087465 100644
@@ -22,6 +22,7 @@ struct parsed_partitions {
 	int limit;
 	bool access_beyond_eod;
 	char *pp_buf;
+	sector_t force_gpt_sector;
 };
 
 typedef struct {
@@ -67,4 +68,5 @@ int osf_partition(struct parsed_partitions *state);
 int sgi_partition(struct parsed_partitions *state);
 int sun_partition(struct parsed_partitions *state);
 int sysv68_partition(struct parsed_partitions *state);
+int tegra_partition_forced_gpt(struct parsed_partitions *state);
 int ultrix_partition(struct parsed_partitions *state);
block/partitions/core.c +3 -0
index 1a7558917c47..1a0247f3354c 100644
@@ -82,6 +82,9 @@ static int (*check_part[])(struct parsed_partitions *) = {
 #endif
 #ifdef CONFIG_SYSV68_PARTITION
 	sysv68_partition,
+#endif
+#ifdef CONFIG_TEGRA_PARTITION
+	tegra_partition_forced_gpt,
 #endif
 	NULL
 };
block/partitions/efi.c +18 -0
index b64bfdd4326c..f016a7f11239 100644
@@ -98,6 +98,15 @@ static int force_gpt;
 static int __init
 force_gpt_fn(char *str)
 {
+	/*
+	 * This check allows to properly parse cmdline variants like
+	 * "gpt gpt_sector=<sector>" and "gpt_sector=<sector> gpt" since
+	 * "gpt" overlaps with the "gpt_sector=", see tegra_gpt_sector_fn().
+	 * The argument is absent for a boolean cmdline option.
+	 */
+	if (strlen(str))
+		return 0;
+
 	force_gpt = 1;
 	return 1;
 }
@@ -621,6 +630,15 @@ static int find_valid_gpt(struct parsed_partitions *state, gpt_header **gpt,
         if (!good_agpt && force_gpt)
                 good_agpt = is_gpt_valid(state, lastlba, &agpt, &aptes);
 
+	/*
+	 * The force_gpt_sector is used by NVIDIA Tegra partition parser in
+	 * order to convey a non-standard location of the GPT entry for lookup.
+	 * By default force_gpt_sector is set to 0 and has no effect.
+	 */
+	if (!good_agpt && force_gpt && state->force_gpt_sector)
+		good_agpt = is_gpt_valid(state, state->force_gpt_sector,
+					 &agpt, &aptes);
+
         /* The obviously unsuccessful case */
         if (!good_pgpt && !good_agpt)
                 goto fail;
block/partitions/tegra.c new file +108 -0

108 lines not on this page · raw message

Signature · 1 line
2.30.2
#4 lore raw

Re: [PATCH v1 3/3] partitions/efi: Support gpt_sector parameter needed by NVIDIA Tegra devices

Quoted 8 lines · Dmitry Osipenko

All NVIDIA Tegra20..124 Android devices use proprietary bootloader which supplies the gpt_sector=<sector> kernel cmdline parameter that should be used for looking up the EFI partition table on internal EMMC storage. If the kernel cmdline parameter isn't supplied, then the partition is expected to be placed around the last but one sector of EMMC.

Apparently this was done in order to hide the PT from a usual userspace tools since EFI entry exists only for compatibility with a Linux kernel,

That's weird, because nothin in Linux relies in EFI partition tables.

Quoted 6 lines

while a custom proprietary partition table is what is really used by these Android devices, thus these tools may corrupt the real PT, making device unbootable and very difficult to restore.

Add support for the gpt_sector cmdline parameter which will be used for finding EFI entry on internal EMMC storage of NVIDIA Tegra20+ devices.

Didn't we plan to just support the actual partition table format insted?

#5 lore raw

Re: [PATCH v1 3/3] partitions/efi: Support gpt_sector parameter needed by NVIDIA Tegra devices

29.03.2021 09:18, Christoph Hellwig пишет:

Quoted 12 lines · Dmitry Osipenko
Quoted 8 lines

All NVIDIA Tegra20..124 Android devices use proprietary bootloader which supplies the gpt_sector=<sector> kernel cmdline parameter that should be used for looking up the EFI partition table on internal EMMC storage. If the kernel cmdline parameter isn't supplied, then the partition is expected to be placed around the last but one sector of EMMC.

Apparently this was done in order to hide the PT from a usual userspace tools since EFI entry exists only for compatibility with a Linux kernel,

That's weird, because nothin in Linux relies in EFI partition tables.

Quoted 9 lines
Quoted 6 lines

while a custom proprietary partition table is what is really used by these Android devices, thus these tools may corrupt the real PT, making device unbootable and very difficult to restore.

Add support for the gpt_sector cmdline parameter which will be used for finding EFI entry on internal EMMC storage of NVIDIA Tegra20+ devices.

Didn't we plan to just support the actual partition table format insted?

Ideally we need to support both forced-gpt and tegra-partition. At first we should check whether GPT entry exists and use it on success, otherwise fall back to tegra-partition. This will allow to properly support all kind of devices. The majority of devices have the GPT entry, so will be good to support it at least.

My understanding that this all require involvement from Jens Axboe in order to get the patches merged. Previously he didn't show interest to any of the patches and my assumption was that he didn't want to have another partition, although I don't know for sure because he never answered.

If you could help with reviewing and applying of the patches, then I will be happy to try again with re-sending a full patchset, which includes the tegra-partition support.

maintainer
#6 lore raw

Re: [PATCH v1 3/3] partitions/efi: Support gpt_sector parameter needed by NVIDIA Tegra devices

In reply to Dmitry Osipenko
Quoted 6 lines · Dmitry Osipenko

All NVIDIA Tegra20..124 Android devices use proprietary bootloader which supplies the gpt_sector=<sector> kernel cmdline parameter that should be used for looking up the EFI partition table on internal EMMC storage. If the kernel cmdline parameter isn't supplied, then the partition is expected to be placed around the last but one sector of EMMC.

Quoted 8 lines

Apparently this was done in order to hide the PT from a usual userspace tools since EFI entry exists only for compatibility with a Linux kernel, while a custom proprietary partition table is what is really used by these Android devices, thus these tools may corrupt the real PT, making device unbootable and very difficult to restore.

Add support for the gpt_sector cmdline parameter which will be used for finding EFI entry on internal EMMC storage of NVIDIA Tegra20+ devices.

Since this is proprietary and playing yucky games hiding the pt, why not just force for the fallback on Nvidia's side and always just use the entry at the end of the block device? I'm not loving introducing a generic parameter for an obscure ad-hoc feature.

Thanks,
Davidlohr

#7 lore raw

Re: [PATCH v1 3/3] partitions/efi: Support gpt_sector parameter needed by NVIDIA Tegra devices

29.03.2021 20:31, Davidlohr Bueso пишет:

Quoted 15 lines · Dmitry Osipenko
Quoted 14 lines

All NVIDIA Tegra20..124 Android devices use proprietary bootloader which supplies the gpt_sector=<sector> kernel cmdline parameter that should be used for looking up the EFI partition table on internal EMMC storage.  If the kernel cmdline parameter isn't supplied, then the partition is expected to be placed around the last but one sector of EMMC.

Apparently this was done in order to hide the PT from a usual userspace tools since EFI entry exists only for compatibility with a Linux kernel, while a custom proprietary partition table is what is really used by these Android devices, thus these tools may corrupt the real PT, making device unbootable and very difficult to restore.

Quoted 8 lines
Quoted 3 lines

Add support for the gpt_sector cmdline parameter which will be used for finding EFI entry on internal EMMC storage of NVIDIA Tegra20+ devices.

Since this is proprietary and playing yucky games hiding the pt, why not just force for the fallback on Nvidia's side and always just use the entry at the end of the block device? I'm not loving introducing a generic parameter for an obscure ad-hoc feature.

I understand the reluctance to support the ad-hoc features, I wouldn't want to support this myself if I was in yours position. This all is necessary solely in order to provide a good user experience using mainline kernel on the consumer devices that have Tegra SoC inside. A more advanced users could bypass the secure boot restrictions and reformat the partition table as they wish, but this is very involved. The only reason I'm submitting these patches is to allow more people to have fun with their devices running mainline Linux without a need to bother with extra out-of-tree patches.

There are total four possible variants of the PT that I'm aware about:

1. GPT entry exists at the proper "backup" location in the end of a block device. Unfortunately only Nexus 7 is known to have this.

2. GPT entry exists at the give sector offset using the gpt_sector=<> parameter. This offset may or may not match the offset that we can calculate based on EMMC boot offset. This case is common for the vast majority of devices.

3. GPT entry exists at the expected sector offset which is calculated based on EMMC boot offset. This is common for devices that use older versions of NVIDIA bootloader which doesn't provide the gpt_sector=<> parameter.

4. GPT entry doesn't exist at all and other means are used to convey the partition table info. This is rare, but happens for the oldest devices. Using a reversed-engineered tegra-partition format helps in this case.

Technically we could use the reversed-engineered tegra-partition without caring about GPT at all, but this may result in a different number of a visible partitions to the system in comparison to the GPT entry because some of partitions may be hidden from the GPT, like partition where media keys are stored and etc. This could be confusing and quite inconvenient when /dev/mmcblk0p4 becomes /dev/mmcblk0p6, so it's more preferred to use the existing GPT entry.