From 5c20de57888f0962e25a0eeec1a59c98056fc42e Mon Sep 17 00:00:00 2001 From: Benjamin Gray Date: Fri, 3 Feb 2023 11:39:46 +1100 Subject: selftests/powerpc: Add {read,write}_{long,ulong} Add helper functions to read and write (unsigned) long values directly from/to files. One of the kernel interfaces uses hex strings, so we need to allow passing a base too. Signed-off-by: Benjamin Gray Signed-off-by: Michael Ellerman Link: https://lore.kernel.org/r/20230203003947.38033-5-bgray@linux.ibm.com --- tools/testing/selftests/powerpc/dscr/dscr.h | 9 ++------- tools/testing/selftests/powerpc/dscr/dscr_sysfs_test.c | 14 +++++++------- 2 files changed, 9 insertions(+), 14 deletions(-) (limited to 'tools/testing/selftests/powerpc/dscr') diff --git a/tools/testing/selftests/powerpc/dscr/dscr.h b/tools/testing/selftests/powerpc/dscr/dscr.h index aaa2b0d89f7e..2c54998d4715 100644 --- a/tools/testing/selftests/powerpc/dscr/dscr.h +++ b/tools/testing/selftests/powerpc/dscr/dscr.h @@ -65,26 +65,21 @@ inline void set_dscr_usr(unsigned long val) unsigned long get_default_dscr(void) { int err; - char buf[16] = {0}; unsigned long val; - err = read_file(DSCR_DEFAULT, buf, sizeof(buf) - 1, NULL); + err = read_ulong(DSCR_DEFAULT, &val, 16); if (err) { perror("read() failed"); exit(1); } - sscanf(buf, "%lx", &val); return val; } void set_default_dscr(unsigned long val) { int err; - char buf[16]; - sprintf(buf, "%lx\n", val); - - err = write_file(DSCR_DEFAULT, buf, strlen(buf)); + err = write_ulong(DSCR_DEFAULT, val, 16); if (err) { perror("write() failed"); exit(1); diff --git a/tools/testing/selftests/powerpc/dscr/dscr_sysfs_test.c b/tools/testing/selftests/powerpc/dscr/dscr_sysfs_test.c index c350f193830a..4f1fef6198fc 100644 --- a/tools/testing/selftests/powerpc/dscr/dscr_sysfs_test.c +++ b/tools/testing/selftests/powerpc/dscr/dscr_sysfs_test.c @@ -12,16 +12,16 @@ static int check_cpu_dscr_default(char *file, unsigned long val) { - char buf[10] = {0}; - int rc; + unsigned long cpu_dscr; + int err; - rc = read_file(file, buf, sizeof(buf) - 1, NULL); - if (rc) - return rc; + err = read_ulong(file, &cpu_dscr, 16); + if (err) + return err; - if (strtol(buf, NULL, 16) != val) { + if (cpu_dscr != val) { printf("DSCR match failed: %ld (system) %ld (cpu)\n", - val, strtol(buf, NULL, 16)); + val, cpu_dscr); return 1; } return 0; -- cgit v1.2.3