]>
Commit | Line | Data |
---|---|---|
2c65db5e PB |
1 | /* |
2 | * QEMU firmware and keymap file search | |
3 | * | |
4 | * Copyright (c) 2003-2020 QEMU contributors | |
5 | * | |
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
7 | * of this software and associated documentation files (the "Software"), to deal | |
8 | * in the Software without restriction, including without limitation the rights | |
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | * copies of the Software, and to permit persons to whom the Software is | |
11 | * furnished to do so, subject to the following conditions: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
22 | * THE SOFTWARE. | |
23 | */ | |
24 | ||
25 | #include "qemu/osdep.h" | |
2c65db5e PB |
26 | #include "qemu/datadir.h" |
27 | #include "qemu/cutils.h" | |
28 | #include "trace.h" | |
29 | ||
30 | static const char *data_dir[16]; | |
31 | static int data_dir_idx; | |
32 | ||
33 | char *qemu_find_file(int type, const char *name) | |
34 | { | |
35 | int i; | |
36 | const char *subdir; | |
37 | char *buf; | |
38 | ||
39 | /* Try the name as a straight path first */ | |
40 | if (access(name, R_OK) == 0) { | |
41 | trace_load_file(name, name); | |
42 | return g_strdup(name); | |
43 | } | |
44 | ||
45 | switch (type) { | |
46 | case QEMU_FILE_TYPE_BIOS: | |
47 | subdir = ""; | |
48 | break; | |
49 | case QEMU_FILE_TYPE_KEYMAP: | |
50 | subdir = "keymaps/"; | |
51 | break; | |
52 | default: | |
53 | abort(); | |
54 | } | |
55 | ||
56 | for (i = 0; i < data_dir_idx; i++) { | |
57 | buf = g_strdup_printf("%s/%s%s", data_dir[i], subdir, name); | |
58 | if (access(buf, R_OK) == 0) { | |
59 | trace_load_file(name, buf); | |
60 | return buf; | |
61 | } | |
62 | g_free(buf); | |
63 | } | |
64 | return NULL; | |
65 | } | |
66 | ||
67 | void qemu_add_data_dir(char *path) | |
68 | { | |
69 | int i; | |
70 | ||
71 | if (path == NULL) { | |
72 | return; | |
73 | } | |
74 | if (data_dir_idx == ARRAY_SIZE(data_dir)) { | |
75 | return; | |
76 | } | |
77 | for (i = 0; i < data_dir_idx; i++) { | |
78 | if (strcmp(data_dir[i], path) == 0) { | |
79 | g_free(path); /* duplicate */ | |
80 | return; | |
81 | } | |
82 | } | |
83 | data_dir[data_dir_idx++] = path; | |
84 | } | |
85 | ||
86 | /* | |
87 | * Find a likely location for support files using the location of the binary. | |
88 | * When running from the build tree this will be "$bindir/pc-bios". | |
89 | * Otherwise, this is CONFIG_QEMU_DATADIR (possibly relocated). | |
90 | * | |
91 | * The caller must use g_free() to free the returned data when it is | |
92 | * no longer required. | |
93 | */ | |
94 | static char *find_datadir(void) | |
95 | { | |
96 | g_autofree char *dir = NULL; | |
97 | ||
98 | dir = g_build_filename(qemu_get_exec_dir(), "pc-bios", NULL); | |
99 | if (g_file_test(dir, G_FILE_TEST_IS_DIR)) { | |
100 | return g_steal_pointer(&dir); | |
101 | } | |
102 | ||
103 | return get_relocated_path(CONFIG_QEMU_DATADIR); | |
104 | } | |
105 | ||
106 | void qemu_add_default_firmwarepath(void) | |
107 | { | |
108 | char **dirs; | |
109 | size_t i; | |
110 | ||
111 | /* add configured firmware directories */ | |
112 | dirs = g_strsplit(CONFIG_QEMU_FIRMWAREPATH, G_SEARCHPATH_SEPARATOR_S, 0); | |
113 | for (i = 0; dirs[i] != NULL; i++) { | |
114 | qemu_add_data_dir(get_relocated_path(dirs[i])); | |
115 | } | |
116 | g_strfreev(dirs); | |
117 | ||
118 | /* try to find datadir relative to the executable path */ | |
119 | qemu_add_data_dir(find_datadir()); | |
120 | } | |
121 | ||
122 | void qemu_list_data_dirs(void) | |
123 | { | |
124 | int i; | |
125 | for (i = 0; i < data_dir_idx; i++) { | |
126 | printf("%s\n", data_dir[i]); | |
127 | } | |
128 | } |