linux_spi, file: delete device search, sync with upstream

The check_sysfs() function in linux_spi would try to find a SPI device
for the programmer to use by searching /sys/bus/spi/devices and picking
the first one it found. This was a brittle solution and can be deleted
now that all ARM devices have been been updated to use the MTD interface
instead.

This also allows the helper functions in file.{c,h} to be deleted.

BUG=b:153598437
BRANCH=none
TEST=builds

Cq-Depend: chromium:2654791
Signed-off-by: Nikolai Artemiev <nartemiev@google.com>
Change-Id: I59f6f4ec07e34321f2d102bf69969b7d5c7eaedd
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/flashrom/+/2902170
Commit-Queue: Edward O'Callaghan <quasisec@chromium.org>
Reviewed-by: Edward O'Callaghan <quasisec@chromium.org>
Reviewed-by: Sam McNally <sammc@chromium.org>
diff --git a/Makefile b/Makefile
index 8c35597..084f2ef 100644
--- a/Makefile
+++ b/Makefile
@@ -663,7 +663,7 @@
 LIB_OBJS += $(LOCK_OBJS)
 FEATURE_CFLAGS += -D'USE_BIG_LOCK=1'
 LIB_OBJS += libflashrom.o layout.o flashrom.o udelay.o programmer.o helpers.o ich_descriptors.o fmap.o
-LIB_OBJS += file.o power.o action_descriptor.o
+LIB_OBJS += power.o action_descriptor.o
 
 ###############################################################################
 # Frontend related stuff.
diff --git a/cros_ec_dev.c b/cros_ec_dev.c
index c03d9c1..b7f1c97 100644
--- a/cros_ec_dev.c
+++ b/cros_ec_dev.c
@@ -48,7 +48,6 @@
 #include <linux/ioctl.h>
 #include <linux/types.h>
 
-#include "file.h"
 #include "flash.h"
 #include "cros_ec_commands.h"
 #include "cros_ec.h"
diff --git a/file.c b/file.c
deleted file mode 100644
index a3774bc..0000000
--- a/file.c
+++ /dev/null
@@ -1,173 +0,0 @@
-/*
- * Copyright 2015, The Chromium OS Authors
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are
- * met:
- *
- *    * Redistributions of source code must retain the above copyright
- *      notice, this list of conditions and the following disclaimer.
- *    * Redistributions in binary form must reproduce the above
- *      copyright notice, this list of conditions and the following
- *      disclaimer in the documentation and/or other materials provided
- *      with the distribution.
- *    * Neither the name of Google Inc. nor the names of its
- *      contributors may be used to endorse or promote products derived
- *      from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- * scanft() is derived from mosys source,s which were released under the
- * BSD license
- */
-
-#include <dirent.h>
-#include <errno.h>
-#include <glob.h>
-#include <limits.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
-#include <arpa/inet.h>
-#include <sys/fcntl.h>
-#include <sys/stat.h>
-#include <sys/types.h>
-
-#include "file.h"
-#include "flash.h"
-
-/* Like strstr(), but allowing NULL bytes within haystack */
-static int __find_string(const char *haystack, size_t hlen, const char *needle)
-{
-	const char *p = haystack;
-	const char *end = haystack + hlen;
-
-	while (p < end) {
-		if (strstr(p, needle))
-			return 1;
-		p = memchr(p, '\0', end - p);
-		if (!p)
-			/* Not found? We're at the end */
-			return 0;
-		else
-			/* Skip past the NULL separator */
-			p++;
-	}
-
-	return 0;
-}
-
-/* returns 1 if string if found, 0 if not, and <0 to indicate error */
-static int find_string(const char *path, const char *str)
-{
-	char contents[4096];
-	int fd, ret;
-	ssize_t len;
-
-	msg_pdbg("%s: checking path \"%s\" for contents \"%s\"\n",
-		 __func__, path, str);
-
-	fd = open(path, O_RDONLY, 0);
-	if (fd < 0) {
-		msg_gerr("Cannot open file \"%s\"\n", path);
-		ret = -1;
-		goto find_string_exit_0;
-	}
-
-	/* mmap() (or even read() with a file length) would be nice but these
-	 * might not be implemented for files in sysfs and procfs.
-	 * Let's also leave room for a terminator. */
-	len = read(fd, contents, sizeof(contents) - 1);
-	if (len == -1) {
-		msg_gerr("Cannot read file \"%s\"\n", path);
-		ret = -1;
-		goto find_string_exit_1;
-	}
-	/* Terminate the contents, in case they weren't terminated for us */
-	contents[len++] = '\0';
-	ret = __find_string(contents, len, str);
-
-find_string_exit_1:
-	close(fd);
-find_string_exit_0:
-	return ret;
-}
-
-/*
- * scanft - scan filetree for file with option to search for content
- *
- * @root:	Where to begin search
- * @filename:	Name of file to search for
- * @str:	Optional NULL terminated string to search for
- * @symdepth:	Maximum depth of symlinks to follow. A negative value means
- * 		follow indefinitely. Zero means do not follow symlinks.
- *
- * The caller should be specific enough with root and symdepth arguments
- * to avoid finding duplicate information (especially in sysfs).
- *
- * Also, note that we may only scan a bounded portion of the beginning of the
- * file for a match.
- *
- * returns allocated string with path of matching file if successful
- * returns NULL to indicate failure
- */
-const char *scanft(const char *root, const char *filename,
-		   const char *str, int symdepth)
-{
-	DIR *dp;
-	struct dirent *d;
-	struct stat s;
-	const char *ret = NULL;
-
-	if (lstat(root, &s) < 0) {
-		msg_pdbg("%s: Error stat'ing %s: %s\n",
-		        __func__, root, strerror(errno));
-		return NULL;
-	}
-
-	if (S_ISLNK(s.st_mode)) {
-		if (symdepth == 0)	/* Leaf has been reached */
-			return NULL;
-		else if (symdepth > 0)	/* Follow if not too deep in */
-			symdepth--;
-	} else if (!S_ISDIR(s.st_mode)) {
-		return NULL;
-	}
-
-	if ((dp = opendir(root)) == NULL)
-		return NULL;
-
-	while (!ret && (d = readdir(dp))) {
-		char newpath[PATH_MAX];
-
-		/* Skip "." and ".." */
-		if (!(strcmp(d->d_name, ".")) ||
-		    !(strcmp(d->d_name, "..")))
-			continue;
-
-		snprintf(newpath, sizeof(newpath), "%s/%s", root, d->d_name);
-
-		if (!strcmp(d->d_name, filename)) {
-			if (!str || (find_string(newpath, str) == 1))
-				ret = strdup(newpath);
-		}
-
-		if (!ret)
-			ret = scanft(newpath, filename, str, symdepth);
-	}
-
-	closedir(dp);
-	return ret;
-}
diff --git a/file.h b/file.h
deleted file mode 100644
index 9af84bf..0000000
--- a/file.h
+++ /dev/null
@@ -1,34 +0,0 @@
-/*
- * Copyright 2015, The Chromium OS Authors
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are
- * met:
- *
- *    * Redistributions of source code must retain the above copyright
- *      notice, this list of conditions and the following disclaimer.
- *    * Redistributions in binary form must reproduce the above
- *      copyright notice, this list of conditions and the following
- *      disclaimer in the documentation and/or other materials provided
- *      with the distribution.
- *    * Neither the name of Google Inc. nor the names of its
- *      contributors may be used to endorse or promote products derived
- *      from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-const char *scanft(const char *root, const char *filename,
-		   const char *str, int symdepth);
-int fdt_find_spi_nor_flash(unsigned int *bus, unsigned int *cs);
diff --git a/linux_spi.c b/linux_spi.c
index 59092ad..7d866f4 100644
--- a/linux_spi.c
+++ b/linux_spi.c
@@ -26,7 +26,6 @@
 #include <unistd.h>
 #include <sys/ioctl.h>
 #include <linux/types.h>
-#include "file.h"
 #include "flash.h"
 #include "chipdrivers.h"
 #include "programmer.h"
@@ -45,12 +44,6 @@
  * HummingBoard
  */
 
-#define MODALIAS_FILE		"modalias"
-#define LINUX_SPI_SYSFS_ROOT	"/sys/bus/spi/devices"
-
-/* At least big enough to fit /dev/spidevX.Y */
-#define DEVFS_PATH_LEN 32
-
 #define BUF_SIZE_FROM_SYSFS	"/sys/module/spidev/parameters/bufsiz"
 
 struct linux_spi_data {
@@ -131,58 +124,6 @@
 	.write_aai	= default_spi_write_aai,
 };
 
-static char *check_sysfs(void)
-{
-	unsigned i;
-	const char *sysfs_path = NULL;
-	char *devfs_path = NULL;
-	char *p;
-	const char *modalias[] = {
-		"spi:spidev",	/* raw access over SPI bus (newer kernels) */
-		"spidev",	/* raw access over SPI bus (older kernels) */
-		"m25p80",	/* generic MTD device */
-	};
-
-	for (i = 0; i < ARRAY_SIZE(modalias); i++) {
-		int major, minor;
-
-		/* Path should look like: /sys/blah/spiX.Y/modalias */
-		sysfs_path = scanft(LINUX_SPI_SYSFS_ROOT,
-				MODALIAS_FILE, modalias[i], 1);
-		if (!sysfs_path)
-			continue;
-
-		p = (char *)sysfs_path + strlen(LINUX_SPI_SYSFS_ROOT);
-		if (p[0] == '/')
-			p++;
-
-		if (sscanf(p, "spi%u.%u", &major, &minor) == 2) {
-			msg_pdbg("Found SPI device %s on spi%u.%u\n",
-				modalias[i], major, minor);
-			devfs_path = calloc(1, DEVFS_PATH_LEN);
-			snprintf(devfs_path, DEVFS_PATH_LEN, "/dev/spidev%u.%u", major, minor);
-			free((void *)sysfs_path);
-			break;
-		}
-		free((void *)sysfs_path);
-	}
-
-	if (i == ARRAY_SIZE(modalias))
-		return NULL;
-	return devfs_path;
-}
-
-static char *linux_spi_probe(void)
-{
-	char *ret;
-
-	ret = check_sysfs();
-	if (ret)
-		return ret;
-
-	return NULL;
-}
-
 /* Read max buffer size from sysfs, or use page size as fallback. */
 static size_t get_max_kernel_buf_size()
 {
@@ -252,10 +193,8 @@
 	free(p);
 
 	dev = extract_programmer_param("dev");
-	if (!dev)
-		dev = linux_spi_probe();
 	if (!dev || !strlen(dev)) {
-		msg_perr("No SPI device found. Use flashrom -p "
+		msg_perr("No SPI device given. Use flashrom -p "
 			 "linux_spi:dev=/dev/spidevX.Y\n");
 		free(dev);
 		return 1;
diff --git a/meson.build b/meson.build
index 3b6ed71..2a12b2d 100644
--- a/meson.build
+++ b/meson.build
@@ -347,7 +347,6 @@
 
 ### CrOS Tree
 srcs += 'power.c'
-srcs += 'file.c'
 srcs += 'action_descriptor.c'
 
 srcs += 'cros_alias.c'