xref: /aosp_15_r20/external/ltp/testcases/kernel/firmware/fw_load_user/fw_load.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 /*
2  * Copyright (c) 2013 Oracle and/or its affiliates. All Rights Reserved.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of
7  * the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it would be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write the Free Software Foundation,
16  * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  *
18  * Author:
19  * Alexey Kodanev <[email protected]>
20  *
21  * Test checks device firmware loading.
22  */
23 
24 #define _GNU_SOURCE
25 #include <sys/utsname.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <string.h>
30 
31 #include "test.h"
32 #include "safe_macros.h"
33 #include "old_module.h"
34 
35 /* number of test firmware files */
36 #define FW_FILES	5
37 
38 char *TCID = "fw_load";
39 int TST_TOTAL = FW_FILES;
40 
41 static int fw_size = 0x1000;
42 
43 static const char fw_name[]	= "load_tst.fw";
44 static const char module_name[]	= "ltp_fw_load.ko";
45 
46 /* paths to module's sysfs files */
47 static const char dev_fwnum[]	= "/sys/devices/ltp_fw_load/fwnum";
48 static const char dev_result[]	= "/sys/devices/ltp_fw_load/result";
49 
50 struct fw_file_info {
51 	char *file;
52 	char *dir;
53 	int fake;
54 	int remove_dir;
55 	int remove_file;
56 };
57 
58 static struct fw_file_info fw[FW_FILES];
59 static int fw_num;
60 
61 /* test options */
62 static char *narg;
63 static int nflag;
64 static int skip_cleanup;
65 static int verbose;
66 static const option_t options[] = {
67 	{"n:", &nflag, &narg},
68 	{"s", &skip_cleanup, NULL},
69 	{"v", &verbose, NULL},
70 	{NULL, NULL, NULL}
71 };
72 
73 static void help(void);
74 static void setup(int argc, char *argv[]);
75 static void test_run(void);
76 static void cleanup(void);
77 
78 /*
79  * create firmware files in the fw_paths
80  * @fw_paths: it must be termintated by a NULL pointer
81  */
82 static void create_firmware(char *const fw_paths[]);
83 
main(int argc,char * argv[])84 int main(int argc, char *argv[])
85 {
86 	setup(argc, argv);
87 
88 	test_run();
89 
90 	cleanup();
91 
92 	tst_exit();
93 }
94 
help(void)95 static void help(void)
96 {
97 	printf("  -n x    Write x bytes to firmware file, default is %d\n",
98 		fw_size);
99 	printf("  -s      Skip cleanup\n");
100 	printf("  -v      Verbose\n");
101 }
102 
setup(int argc,char * argv[])103 void setup(int argc, char *argv[])
104 {
105 
106 	tst_parse_opts(argc, argv, options, help);
107 
108 	if (nflag) {
109 		if (sscanf(narg, "%i", &fw_size) != 1)
110 			tst_brkm(TBROK, NULL, "-n option arg is not a number");
111 		if (fw_size < 0)
112 			tst_brkm(TBROK, NULL, "-n option arg is less than 0");
113 	}
114 
115 	tst_require_root();
116 
117 	char fw_size_param[19];
118 	snprintf(fw_size_param, 19, "fw_size=%d", fw_size);
119 	char *const mod_params[2] = { fw_size_param, NULL };
120 	tst_module_load(NULL, module_name, mod_params);
121 
122 	tst_sig(FORK, DEF_HANDLER, cleanup);
123 
124 	/* get current Linux version and make firmware paths */
125 	struct utsname uts_name;
126 	uname(&uts_name);
127 
128 	/* 4 firmware paths + NULL */
129 	char *fw_paths[5] = { "/lib/firmware", "/lib/firmware/updates" };
130 	SAFE_ASPRINTF(cleanup, &fw_paths[2], "%s/%s", fw_paths[0], uts_name.release);
131 	SAFE_ASPRINTF(cleanup, &fw_paths[3], "%s/%s", fw_paths[1], uts_name.release);
132 
133 	/* create firmware in the hard coded firmware search paths */
134 	create_firmware(fw_paths);
135 
136 	free(fw_paths[2]);
137 	free(fw_paths[3]);
138 
139 	/* make non-existent firmware file */
140 	SAFE_ASPRINTF(cleanup, &fw[fw_num].file, "/n%d_%s", fw_num, fw_name);
141 	fw[fw_num].fake = 1;
142 	++fw_num;
143 }
144 
test_run(void)145 static void test_run(void)
146 {
147 	/* initiate firmware requests */
148 	SAFE_FILE_PRINTF(cleanup, dev_fwnum, "%d", fw_num);
149 
150 	/* get module results by reading result bit mask */
151 	int result = 0;
152 	SAFE_FILE_SCANF(cleanup, dev_result, "%d", &result);
153 
154 	int i, fail, offset;
155 	for (i = 0; i < fw_num; ++i) {
156 		fail = (result & (1 << i)) == 0 && !fw[i].fake;
157 		offset = (fw[i].dir) ? strlen(fw[i].dir) : 0;
158 		tst_resm((fail) ? TFAIL : TPASS,
159 			"Expect: %s load firmware '...%s'",
160 			(fw[i].fake) ? "can't" : "can",
161 			fw[i].file + offset);
162 	}
163 }
164 
cleanup(void)165 static void cleanup(void)
166 {
167 	if (skip_cleanup)
168 		return;
169 
170 	int i;
171 	/* remove subdirs first and then upper level dirs */
172 	for (i = fw_num - 1; i >= 0; --i) {
173 		if (fw[i].remove_file && remove(fw[i].file) == -1)
174 			tst_resm(TWARN, "Can't remove: %s", fw[i].file);
175 		free(fw[i].file);
176 
177 		if (fw[i].remove_dir && remove(fw[i].dir) == -1)
178 			tst_resm(TWARN, "Can't remove %s", fw[i].dir);
179 		free(fw[i].dir);
180 	}
181 
182 	tst_module_unload(NULL, module_name);
183 }
184 
create_firmware(char * const fw_paths[])185 static void create_firmware(char *const fw_paths[])
186 {
187 	int i = 0;
188 	while (fw_paths[i] != NULL) {
189 		struct fw_file_info *fi = &fw[fw_num];
190 		fi->dir = strdup(fw_paths[i]);
191 		if (access(fi->dir, X_OK) == -1) {
192 			/* create dir */
193 			SAFE_MKDIR(cleanup, fi->dir, 0755);
194 			fi->remove_dir = 1;
195 		}
196 
197 		/* create test firmware file */
198 		SAFE_ASPRINTF(cleanup, &fi->file, "%s/n%d_%s", fi->dir, fw_num, fw_name);
199 
200 		FILE *f = SAFE_FOPEN(cleanup, fi->file, "w");
201 		fi->remove_file = 1;
202 		int k, byte = fw_num;
203 		++fw_num;
204 		for (k = 0; k < fw_size; ++k)
205 			fputc(byte, f);
206 		SAFE_FCLOSE(cleanup, f);
207 		++i;
208 	}
209 }
210