xref: /aosp_15_r20/external/flashrom/tests/linux_mtd.c (revision 0d6140be3aa665ecc836e8907834fcd3e3b018fc)
1 /*
2  * This file is part of the flashrom project.
3  *
4  * Copyright 2021 Google LLC
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; version 2 of the License.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  */
15 
16 #include "lifecycle.h"
17 
18 #if CONFIG_LINUX_MTD == 1
19 struct linux_mtd_io_state {
20 	char *fopen_path;
21 };
22 
linux_mtd_fopen(void * state,const char * pathname,const char * mode)23 static FILE *linux_mtd_fopen(void *state, const char *pathname, const char *mode)
24 {
25 	struct linux_mtd_io_state *io_state = state;
26 
27 	io_state->fopen_path = strdup(pathname);
28 
29 	return not_null();
30 }
31 
linux_mtd_fread(void * state,void * buf,size_t size,size_t len,FILE * fp)32 static size_t linux_mtd_fread(void *state, void *buf, size_t size, size_t len, FILE *fp)
33 {
34 	struct linux_mtd_fread_mock_entry {
35 		const char *path;
36 		const char *data;
37 	};
38 	const struct linux_mtd_fread_mock_entry fread_mock_map[] = {
39 		{ "/sys/class/mtd/mtd0//type",            "nor"    },
40 		{ "/sys/class/mtd/mtd0//name",            "Device" },
41 		{ "/sys/class/mtd/mtd0//flags",           ""       },
42 		{ "/sys/class/mtd/mtd0//size",            "1024"   },
43 		{ "/sys/class/mtd/mtd0//erasesize",       "512"    },
44 		{ "/sys/class/mtd/mtd0//numeraseregions", "0"      },
45 	};
46 
47 	struct linux_mtd_io_state *io_state = state;
48 	unsigned int i;
49 
50 	if (!io_state->fopen_path)
51 		return 0;
52 
53 	for (i = 0; i < ARRAY_SIZE(fread_mock_map); i++) {
54 		const struct linux_mtd_fread_mock_entry *entry = &fread_mock_map[i];
55 
56 		if (!strcmp(io_state->fopen_path, entry->path)) {
57 			size_t data_len = min(size * len, strlen(entry->data));
58 			memcpy(buf, entry->data, data_len);
59 			return data_len;
60 		}
61 	}
62 
63 	return 0;
64 }
65 
linux_mtd_fclose(void * state,FILE * fp)66 static int linux_mtd_fclose(void *state, FILE *fp)
67 {
68 	struct linux_mtd_io_state *io_state = state;
69 
70 	free(io_state->fopen_path);
71 
72 	return 0;
73 }
74 
linux_mtd_probe_lifecycle_test_success(void ** state)75 void linux_mtd_probe_lifecycle_test_success(void **state)
76 {
77 	struct linux_mtd_io_state linux_mtd_io_state = { NULL };
78 	struct io_mock_fallback_open_state linux_mtd_fallback_open_state = {
79 		.noc = 0,
80 		.paths = { LOCK_FILE },
81 	};
82 	const struct io_mock linux_mtd_io = {
83 		.state	= &linux_mtd_io_state,
84 		.iom_fopen	= linux_mtd_fopen,
85 		.iom_fread	= linux_mtd_fread,
86 		.iom_fclose = linux_mtd_fclose,
87 		.fallback_open_state = &linux_mtd_fallback_open_state,
88 	};
89 
90 	run_probe_lifecycle(state, &linux_mtd_io, &programmer_linux_mtd, "", "Opaque flash chip");
91 }
92 #else
93 	SKIP_TEST(linux_mtd_probe_lifecycle_test_success)
94 #endif /* CONFIG_LINUX_MTD */
95