1f08c3bdfSopenharmony_ci/*
2f08c3bdfSopenharmony_ci * Copyright (c) 2017 Cyril Hrubis <chrubis@suse.cz>
3f08c3bdfSopenharmony_ci * Copyright (c) 2020-2021 Petr Vorel <pvorel@suse.cz>
4f08c3bdfSopenharmony_ci *
5f08c3bdfSopenharmony_ci * This program is free software: you can redistribute it and/or modify
6f08c3bdfSopenharmony_ci * it under the terms of the GNU General Public License as published by
7f08c3bdfSopenharmony_ci * the Free Software Foundation, either version 2 of the License, or
8f08c3bdfSopenharmony_ci * (at your option) any later version.
9f08c3bdfSopenharmony_ci *
10f08c3bdfSopenharmony_ci * This program is distributed in the hope that it will be useful,
11f08c3bdfSopenharmony_ci * but WITHOUT ANY WARRANTY; without even the implied warranty of
12f08c3bdfSopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13f08c3bdfSopenharmony_ci * GNU General Public License for more details.
14f08c3bdfSopenharmony_ci *
15f08c3bdfSopenharmony_ci * You should have received a copy of the GNU General Public License
16f08c3bdfSopenharmony_ci * along with this program. If not, see <http://www.gnu.org/licenses/>.
17f08c3bdfSopenharmony_ci */
18f08c3bdfSopenharmony_ci
19f08c3bdfSopenharmony_ci#include <sys/personality.h>
20f08c3bdfSopenharmony_ci#include <sys/utsname.h>
21f08c3bdfSopenharmony_ci#include <limits.h>
22f08c3bdfSopenharmony_ci
23f08c3bdfSopenharmony_ci#include "test.h"
24f08c3bdfSopenharmony_ci#include "tst_kernel.h"
25f08c3bdfSopenharmony_ci#include "old_safe_stdio.h"
26f08c3bdfSopenharmony_ci
27f08c3bdfSopenharmony_cistatic int get_kernel_bits_from_uname(struct utsname *buf)
28f08c3bdfSopenharmony_ci{
29f08c3bdfSopenharmony_ci	if (uname(buf)) {
30f08c3bdfSopenharmony_ci		tst_brkm(TBROK | TERRNO, NULL, "uname()");
31f08c3bdfSopenharmony_ci		return -1;
32f08c3bdfSopenharmony_ci	}
33f08c3bdfSopenharmony_ci
34f08c3bdfSopenharmony_ci	return strstr(buf->machine, "64") ? 64 : 32;
35f08c3bdfSopenharmony_ci}
36f08c3bdfSopenharmony_ci
37f08c3bdfSopenharmony_ciint tst_kernel_bits(void)
38f08c3bdfSopenharmony_ci{
39f08c3bdfSopenharmony_ci	struct utsname buf;
40f08c3bdfSopenharmony_ci	static int kernel_bits;
41f08c3bdfSopenharmony_ci
42f08c3bdfSopenharmony_ci	if (kernel_bits)
43f08c3bdfSopenharmony_ci		return kernel_bits;
44f08c3bdfSopenharmony_ci
45f08c3bdfSopenharmony_ci	kernel_bits = get_kernel_bits_from_uname(&buf);
46f08c3bdfSopenharmony_ci
47f08c3bdfSopenharmony_ci	if (kernel_bits == -1)
48f08c3bdfSopenharmony_ci		return -1;
49f08c3bdfSopenharmony_ci
50f08c3bdfSopenharmony_ci	/*
51f08c3bdfSopenharmony_ci	 * ARM64 (aarch64) defines 32-bit compatibility modes as
52f08c3bdfSopenharmony_ci	 * armv8l and armv8b (little and big endian).
53f08c3bdfSopenharmony_ci	 * s390x is 64bit but not contain 64 in the words.
54f08c3bdfSopenharmony_ci	 */
55f08c3bdfSopenharmony_ci	if (!strcmp(buf.machine, "armv8l") || !strcmp(buf.machine, "armv8b")
56f08c3bdfSopenharmony_ci			|| !strcmp(buf.machine, "s390x"))
57f08c3bdfSopenharmony_ci		kernel_bits = 64;
58f08c3bdfSopenharmony_ci
59f08c3bdfSopenharmony_ci#ifdef __ANDROID__
60f08c3bdfSopenharmony_ci	/* Android's bionic libc sets the PER_LINUX32 personality for all 32-bit
61f08c3bdfSopenharmony_ci	 * programs. This will cause buf.machine to report as i686 even though
62f08c3bdfSopenharmony_ci	 * the kernel itself is 64-bit.
63f08c3bdfSopenharmony_ci	 */
64f08c3bdfSopenharmony_ci	if (!strcmp(buf.machine, "i686") &&
65f08c3bdfSopenharmony_ci			(personality(0xffffffff) & PER_MASK) == PER_LINUX32) {
66f08c3bdfSopenharmony_ci		/* Set the personality back to the default. */
67f08c3bdfSopenharmony_ci		if (personality(PER_LINUX) == -1) {
68f08c3bdfSopenharmony_ci			tst_brkm(TBROK | TERRNO, NULL, "personality()");
69f08c3bdfSopenharmony_ci			return -1;
70f08c3bdfSopenharmony_ci		}
71f08c3bdfSopenharmony_ci
72f08c3bdfSopenharmony_ci		/* Redo the uname check without the PER_LINUX32 personality to
73f08c3bdfSopenharmony_ci		 * determine the actual kernel bits value.
74f08c3bdfSopenharmony_ci		 */
75f08c3bdfSopenharmony_ci		kernel_bits = get_kernel_bits_from_uname(&buf);
76f08c3bdfSopenharmony_ci		if (kernel_bits == -1)
77f08c3bdfSopenharmony_ci			return -1;
78f08c3bdfSopenharmony_ci
79f08c3bdfSopenharmony_ci		/* Set the personality back to PER_LINUX32. */
80f08c3bdfSopenharmony_ci		if (personality(PER_LINUX32) == -1) {
81f08c3bdfSopenharmony_ci			tst_brkm(TBROK | TERRNO, NULL, "personality()");
82f08c3bdfSopenharmony_ci			return -1;
83f08c3bdfSopenharmony_ci		}
84f08c3bdfSopenharmony_ci	}
85f08c3bdfSopenharmony_ci#endif  /* __ANDROID__ */
86f08c3bdfSopenharmony_ci
87f08c3bdfSopenharmony_ci	tst_resm(TINFO, "uname.machine=%s kernel is %ibit",
88f08c3bdfSopenharmony_ci		 buf.machine, kernel_bits);
89f08c3bdfSopenharmony_ci
90f08c3bdfSopenharmony_ci	return kernel_bits;
91f08c3bdfSopenharmony_ci}
92f08c3bdfSopenharmony_ci
93f08c3bdfSopenharmony_cistatic int tst_search_driver_(const char *driver, const char *file)
94f08c3bdfSopenharmony_ci{
95f08c3bdfSopenharmony_ci	struct stat st;
96f08c3bdfSopenharmony_ci	char buf[PATH_MAX];
97f08c3bdfSopenharmony_ci	char *path = NULL, *search = NULL, *sep = NULL;
98f08c3bdfSopenharmony_ci	FILE *f;
99f08c3bdfSopenharmony_ci	int ret = -1;
100f08c3bdfSopenharmony_ci
101f08c3bdfSopenharmony_ci	struct utsname uts;
102f08c3bdfSopenharmony_ci
103f08c3bdfSopenharmony_ci	if (uname(&uts)) {
104f08c3bdfSopenharmony_ci		tst_brkm(TBROK | TERRNO, NULL, "uname() failed");
105f08c3bdfSopenharmony_ci		return -1;
106f08c3bdfSopenharmony_ci	}
107f08c3bdfSopenharmony_ci	SAFE_ASPRINTF(NULL, &path, "/lib/modules/%s/%s", uts.release, file);
108f08c3bdfSopenharmony_ci
109f08c3bdfSopenharmony_ci	if (stat(path, &st) || !(S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))) {
110f08c3bdfSopenharmony_ci		tst_resm(TWARN, "expected file %s does not exist or not a file", path);
111f08c3bdfSopenharmony_ci		return -1;
112f08c3bdfSopenharmony_ci	}
113f08c3bdfSopenharmony_ci
114f08c3bdfSopenharmony_ci	if (access(path, R_OK)) {
115f08c3bdfSopenharmony_ci		tst_resm(TWARN, "file %s cannot be read", path);
116f08c3bdfSopenharmony_ci		return -1;
117f08c3bdfSopenharmony_ci	}
118f08c3bdfSopenharmony_ci
119f08c3bdfSopenharmony_ci	/* always search for x86_64 */
120f08c3bdfSopenharmony_ci	char *fix = strstr(driver, "x86-64");
121f08c3bdfSopenharmony_ci
122f08c3bdfSopenharmony_ci	if (fix)
123f08c3bdfSopenharmony_ci		fix[3] = '_';
124f08c3bdfSopenharmony_ci
125f08c3bdfSopenharmony_ci	SAFE_ASPRINTF(NULL, &search, "/%s.ko", driver);
126f08c3bdfSopenharmony_ci
127f08c3bdfSopenharmony_ci	f = SAFE_FOPEN(NULL, path, "r");
128f08c3bdfSopenharmony_ci
129f08c3bdfSopenharmony_ci	while (fgets(buf, sizeof(buf), f)) {
130f08c3bdfSopenharmony_ci		/* cut dependencies after : */
131f08c3bdfSopenharmony_ci		if ((sep = strchr(buf, ':')))
132f08c3bdfSopenharmony_ci			*sep = 0;
133f08c3bdfSopenharmony_ci
134f08c3bdfSopenharmony_ci		/* driver found */
135f08c3bdfSopenharmony_ci		if (strstr(buf, search) != NULL) {
136f08c3bdfSopenharmony_ci			ret = 0;
137f08c3bdfSopenharmony_ci			break;
138f08c3bdfSopenharmony_ci		}
139f08c3bdfSopenharmony_ci	}
140f08c3bdfSopenharmony_ci
141f08c3bdfSopenharmony_ci	SAFE_FCLOSE(NULL, f);
142f08c3bdfSopenharmony_ci	free(search);
143f08c3bdfSopenharmony_ci	free(path);
144f08c3bdfSopenharmony_ci
145f08c3bdfSopenharmony_ci	return ret;
146f08c3bdfSopenharmony_ci}
147f08c3bdfSopenharmony_ci
148f08c3bdfSopenharmony_cistatic int tst_search_driver(const char *driver, const char *file)
149f08c3bdfSopenharmony_ci{
150f08c3bdfSopenharmony_ci#ifdef __ANDROID__
151f08c3bdfSopenharmony_ci	/*
152f08c3bdfSopenharmony_ci	 * Android may not have properly installed modules.* files. We could
153f08c3bdfSopenharmony_ci	 * search modules in /system/lib/modules, but to determine built-in
154f08c3bdfSopenharmony_ci	 * drivers we need modules.builtin. Therefore assume all drivers are
155f08c3bdfSopenharmony_ci	 * available.
156f08c3bdfSopenharmony_ci	 */
157f08c3bdfSopenharmony_ci	return 0;
158f08c3bdfSopenharmony_ci#endif
159f08c3bdfSopenharmony_ci
160f08c3bdfSopenharmony_ci	if (!tst_search_driver_(driver, file))
161f08c3bdfSopenharmony_ci		return 0;
162f08c3bdfSopenharmony_ci
163f08c3bdfSopenharmony_ci	int ret = -1;
164f08c3bdfSopenharmony_ci
165f08c3bdfSopenharmony_ci	if (strrchr(driver, '-') || strrchr(driver, '_')) {
166f08c3bdfSopenharmony_ci		char *driver2 = strdup(driver);
167f08c3bdfSopenharmony_ci		char *ix = driver2;
168f08c3bdfSopenharmony_ci		char find = '-', replace = '_';
169f08c3bdfSopenharmony_ci
170f08c3bdfSopenharmony_ci		if (strrchr(driver, '_')) {
171f08c3bdfSopenharmony_ci			find = '_';
172f08c3bdfSopenharmony_ci			replace = '-';
173f08c3bdfSopenharmony_ci		}
174f08c3bdfSopenharmony_ci
175f08c3bdfSopenharmony_ci		while ((ix = strchr(ix, find)))
176f08c3bdfSopenharmony_ci			*ix++ = replace;
177f08c3bdfSopenharmony_ci
178f08c3bdfSopenharmony_ci		ret = tst_search_driver_(driver2, file);
179f08c3bdfSopenharmony_ci		free(driver2);
180f08c3bdfSopenharmony_ci	}
181f08c3bdfSopenharmony_ci
182f08c3bdfSopenharmony_ci	return ret;
183f08c3bdfSopenharmony_ci}
184f08c3bdfSopenharmony_ci
185f08c3bdfSopenharmony_ciint tst_check_builtin_driver(const char *driver)
186f08c3bdfSopenharmony_ci{
187f08c3bdfSopenharmony_ci	if (!tst_search_driver(driver, "modules.builtin"))
188f08c3bdfSopenharmony_ci		return 0;
189f08c3bdfSopenharmony_ci
190f08c3bdfSopenharmony_ci	return -1;
191f08c3bdfSopenharmony_ci}
192f08c3bdfSopenharmony_ci
193f08c3bdfSopenharmony_ciint tst_check_driver(const char *driver)
194f08c3bdfSopenharmony_ci{
195f08c3bdfSopenharmony_ci	if (!tst_search_driver(driver, "modules.dep") ||
196f08c3bdfSopenharmony_ci		!tst_search_driver(driver, "modules.builtin"))
197f08c3bdfSopenharmony_ci		return 0;
198f08c3bdfSopenharmony_ci
199f08c3bdfSopenharmony_ci	return -1;
200f08c3bdfSopenharmony_ci}
201