Linux Kernel Selftest development
 help / color / mirror / Atom feed
From: Shirisha G <shirisha@linux.ibm.com>
To: linuxppc-dev@lists.ozlabs.org
Cc: linux-kselftest@vger.kernel.org, maddy@linux.ibm.com,
	venkat88@linux.ibm.com, shirisha@linux.ibm.com
Subject: [PATCH 2/7] Subject: [PATCH] selftests/powerpc: Add NVRAM access test
Date: Fri, 18 Sep 2026 10:52:37 +0530	[thread overview]
Message-ID: <20260918052321.932660-2-shirisha@linux.ibm.com> (raw)
In-Reply-To: <20260918052321.932660-1-shirisha@linux.ibm.com>

Add kselftest for NVRAM access on pseries/PowerVM systems. This test
validates basic device operations and partition discovery via pstore,
which replaces the legacy /proc/ppc64/nvram interface.

The test verifies:
- Existence and readability of /dev/nvram
- Discovery of NVRAM partitions from /sys/fs/pstore
- Correct parsing of partition names and indices
- Basic read and lseek operations on the device
- Overflow protection by rejecting reads beyond device size

Signed-off-by: Shirisha G <shirisha@linux.ibm.com>
---
 .../selftests/powerpc/rtas/nvram_test.c       | 131 ++++++++++++++++++
 1 file changed, 131 insertions(+)
 create mode 100644 tools/testing/selftests/powerpc/rtas/nvram_test.c

diff --git a/tools/testing/selftests/powerpc/rtas/nvram_test.c b/tools/testing/selftests/powerpc/rtas/nvram_test.c
new file mode 100644
index 000000000000..b0582d17324f
--- /dev/null
+++ b/tools/testing/selftests/powerpc/rtas/nvram_test.c
@@ -0,0 +1,131 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * NVRAM Access Test
+ * Copyright (C) 2026 IBM Corporation
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <dirent.h>
+#include <errno.h>
+#include "utils.h"
+
+#define NVRAM_DEV       "/dev/nvram"
+#define NVRAM_PSTORE    "/sys/fs/pstore"   /* replaces dead /proc/ppc64/nvram */
+#define NVRAM_SIZE      8192               /* Typical NVRAM size */
+
+struct nvram_partition {
+	char name[64];
+	unsigned int index;
+};
+
+/*
+ * Discover NVRAM partitions from /sys/fs/pstore by listing entries whose
+ * names contain "nvram".  On RTAS/PowerVM systems /proc/ppc64/nvram does
+ * not exist; pstore is the authoritative in-kernel view of NVRAM regions.
+ *
+ * Returns number of partitions found, or -1 if the directory cannot be
+ * opened (caller should SKIP_IF the result is < 0).
+ */
+static int read_nvram_partitions(struct nvram_partition *parts, int max_parts)
+{
+	DIR *dir;
+	struct dirent *ent;
+	int count = 0;
+
+	dir = opendir(NVRAM_PSTORE);
+	if (!dir)
+		return -1;
+
+	while ((ent = readdir(dir)) != NULL && count < max_parts) {
+		/* Only interested in entries that contain "nvram" */
+		if (strstr(ent->d_name, "nvram") == NULL)
+			continue;
+
+		/*
+		 * pstore filenames follow the pattern  <type>-nvram-<id>
+		 * e.g. "rtas-nvram-2", "dmesg-nvram-1".
+		 * Parse the trailing numeric id as the partition index.
+		 */
+		strncpy(parts[count].name, ent->d_name,
+			sizeof(parts[count].name) - 1);
+		parts[count].name[sizeof(parts[count].name) - 1] = '\0';
+
+		/* Extract trailing index after the last '-' */
+		char *p = strrchr(ent->d_name, '-');
+		parts[count].index = p ? (unsigned int)atoi(p + 1) : 0;
+
+		count++;
+	}
+
+	closedir(dir);
+	return count;
+}
+
+int main(void)
+{
+	int fd, ret;
+	char buf[256];
+	struct nvram_partition parts[32];
+	int part_count;
+	off_t offset;
+
+	test_harness_set_timeout(10);
+
+	/* Test 1: Verify /dev/nvram exists — skip gracefully if not */
+	SKIP_IF(access(NVRAM_DEV, R_OK) != 0);
+
+	/* Test 2: Open NVRAM device */
+	fd = open(NVRAM_DEV, O_RDONLY);
+	FAIL_IF(fd < 0);
+
+	/*
+	 * Test 3: Read NVRAM partition table from /sys/fs/pstore.
+	 * Use SKIP_IF when the directory is absent (part_count < 0) so the
+	 * test does not hard-fail on kernels/configs that lack pstore, while
+	 * still failing when the directory exists but is empty (part_count==0).
+	 */
+	part_count = read_nvram_partitions(parts, 32);
+	SKIP_IF(part_count < 0);
+	FAIL_IF_MSG(part_count == 0, "No NVRAM partitions found in " NVRAM_PSTORE);
+
+	printf("Found %d NVRAM partition(s) in " NVRAM_PSTORE ":\n", part_count);
+	for (int i = 0; i < part_count; i++)
+		printf("  %s (index=%u)\n", parts[i].name, parts[i].index);
+
+	/* Test 4: Read from start of device */
+	ret = read(fd, buf, sizeof(buf));
+	FAIL_IF_MSG(ret < 0, "Failed to read from " NVRAM_DEV);
+
+	/* Test 5: lseek to start */
+	offset = lseek(fd, 0, SEEK_SET);
+	FAIL_IF_MSG(offset != 0, "lseek to start failed");
+
+	/* Test 6: lseek to arbitrary offset */
+	offset = lseek(fd, 100, SEEK_SET);
+	FAIL_IF_MSG(offset != 100, "lseek to offset 100 failed");
+
+	/* Test 7: Overflow protection — read past NVRAM_SIZE must not succeed */
+	/*
+     	 * Test 7: Overflow protection.
+     	 * Discover the real device size via SEEK_END; if the driver supports it,
+     	 * attempt a read past that boundary — it must return 0 or error.
+     	 * Do not use a hardcoded NVRAM_SIZE: the actual size varies by platform
+     	 * and the RTAS driver will happily return data if the seek lands within
+     	 * the real (larger) device.                                  FIX 2
+     	 */
+	off_t nvram_size = lseek(fd, 0, SEEK_END);
+	if (nvram_size > 0) {
+		offset = lseek(fd, nvram_size + 1000, SEEK_SET);
+		ret = read(fd, buf, sizeof(buf));
+		FAIL_IF_MSG(ret > 0, "Read succeeded beyond NVRAM size");
+	}
+
+	close(fd);
+	printf("NVRAM test passed\n");
+	return 0;
+}
-- 
2.43.5


  reply	other threads:[~2026-09-18  5:23 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-18  5:22 [PATCH 1/7] Subject: [PATCH] selftests/powerpc: Add RTAS real-time clock test Shirisha G
2026-09-18  5:22 ` Shirisha G [this message]
2026-09-18  5:22 ` [PATCH 3/7] Subject: [PATCH] selftests/powerpc: Add RTAS /proc interface test Shirisha G
2026-09-18  5:22 ` [PATCH 4/7] Subject: [PATCH] selftests/powerpc: Add FADump sysfs " Shirisha G
2026-09-18  5:22 ` [PATCH 5/7] Subject: [PATCH] selftests/powerpc: Add RTAS firmware flash " Shirisha G
2026-09-18  5:22 ` [PATCH 6/7] Subject: [PATCH] selftests/powerpc: Add common test utilities header Shirisha G
2026-09-18  5:22 ` [PATCH 7/7] Subject: [PATCH] selftests/powerpc/rtas: consolidate TEST_GEN_PROGS definitions Shirisha G

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260918052321.932660-2-shirisha@linux.ibm.com \
    --to=shirisha@linux.ibm.com \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linuxppc-dev@lists.ozlabs.org \
    --cc=maddy@linux.ibm.com \
    --cc=venkat88@linux.ibm.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox